blob: 0f6af6b2dbaf38ce31e604cf28584d6d665b1df6 [file] [log] [blame]
Paul Lawrence707fd6c2015-04-28 22:14:15 +00001#include "Ext4Crypt.h"
2
Paul Lawrence5e7f0042015-04-10 07:48:51 -07003#include <iomanip>
Paul Lawrence707fd6c2015-04-28 22:14:15 +00004#include <map>
Paul Lawrence5e7f0042015-04-10 07:48:51 -07005#include <fstream>
6#include <string>
7#include <sstream>
Paul Lawrence707fd6c2015-04-28 22:14:15 +00008
9#include <errno.h>
10#include <sys/mount.h>
11#include <cutils/properties.h>
Paul Lawrence5e7f0042015-04-10 07:48:51 -070012#include <openssl/sha.h>
Paul Lawrence707fd6c2015-04-28 22:14:15 +000013
14#include "unencrypted_properties.h"
15#include "key_control.h"
16#include "cryptfs.h"
17
18#define LOG_TAG "Ext4Crypt"
19#include "cutils/log.h"
20#include <cutils/klog.h>
21
22namespace {
23 // Key length in bits
24 const int key_length = 128;
Paul Lawrence5e7f0042015-04-10 07:48:51 -070025 static_assert(key_length % 8 == 0,
26 "Key length must be multiple of 8 bits");
Paul Lawrence707fd6c2015-04-28 22:14:15 +000027
28 // How is device encrypted
29 struct keys {
30 std::string master_key;
31 std::string password;
32 };
33 std::map<std::string, keys> s_key_store;
34
Paul Lawrence5e7f0042015-04-10 07:48:51 -070035 // ext4enc:TODO get these consts from somewhere good
36 const int SHA512_LENGTH = 64;
37 const int EXT4_KEY_DESCRIPTOR_SIZE = 8;
38
Paul Lawrence707fd6c2015-04-28 22:14:15 +000039 // ext4enc:TODO Include structure from somewhere sensible
40 // MUST be in sync with ext4_crypto.c in kernel
Paul Lawrence5e7f0042015-04-10 07:48:51 -070041 const int EXT4_MAX_KEY_SIZE = 64;
42 const int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1;
Paul Lawrence707fd6c2015-04-28 22:14:15 +000043 struct ext4_encryption_key {
Paul Lawrence5e7f0042015-04-10 07:48:51 -070044 uint32_t mode;
45 char raw[EXT4_MAX_KEY_SIZE];
46 uint32_t size;
Paul Lawrence707fd6c2015-04-28 22:14:15 +000047 };
48
49 namespace tag {
50 const char* magic = "magic";
51 const char* major_version = "major_version";
52 const char* minor_version = "minor_version";
53 const char* flags = "flags";
54 const char* crypt_type = "crypt_type";
55 const char* failed_decrypt_count = "failed_decrypt_count";
56 const char* crypto_type_name = "crypto_type_name";
57 const char* master_key = "master_key";
58 const char* salt = "salt";
59 const char* kdf_type = "kdf_type";
60 const char* N_factor = "N_factor";
61 const char* r_factor = "r_factor";
62 const char* p_factor = "p_factor";
63 const char* keymaster_blob = "keymaster_blob";
64 const char* scrypted_intermediate_key = "scrypted_intermediate_key";
65 }
66}
67
68static int put_crypt_ftr_and_key(const crypt_mnt_ftr& crypt_ftr,
69 UnencryptedProperties& props)
70{
71 SLOGI("Putting crypt footer");
72
73 bool success = props.Set<int>(tag::magic, crypt_ftr.magic)
74 && props.Set<int>(tag::major_version, crypt_ftr.major_version)
75 && props.Set<int>(tag::minor_version, crypt_ftr.minor_version)
76 && props.Set<int>(tag::flags, crypt_ftr.flags)
77 && props.Set<int>(tag::crypt_type, crypt_ftr.crypt_type)
78 && props.Set<int>(tag::failed_decrypt_count,
79 crypt_ftr.failed_decrypt_count)
80 && props.Set<std::string>(tag::crypto_type_name,
81 std::string(reinterpret_cast<const char*>(crypt_ftr.crypto_type_name)))
82 && props.Set<std::string>(tag::master_key,
83 std::string((const char*) crypt_ftr.master_key,
84 crypt_ftr.keysize))
85 && props.Set<std::string>(tag::salt,
86 std::string((const char*) crypt_ftr.salt,
87 SALT_LEN))
88 && props.Set<int>(tag::kdf_type, crypt_ftr.kdf_type)
89 && props.Set<int>(tag::N_factor, crypt_ftr.N_factor)
90 && props.Set<int>(tag::r_factor, crypt_ftr.r_factor)
91 && props.Set<int>(tag::p_factor, crypt_ftr.p_factor)
92 && props.Set<std::string>(tag::keymaster_blob,
93 std::string((const char*) crypt_ftr.keymaster_blob,
94 crypt_ftr.keymaster_blob_size))
95 && props.Set<std::string>(tag::scrypted_intermediate_key,
96 std::string((const char*) crypt_ftr.scrypted_intermediate_key,
97 SCRYPT_LEN));
98 return success ? 0 : -1;
99}
100
101static int get_crypt_ftr_and_key(crypt_mnt_ftr& crypt_ftr,
102 const UnencryptedProperties& props)
103{
104 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
105 crypt_ftr.magic = props.Get<int>(tag::magic);
106 crypt_ftr.major_version = props.Get<int>(tag::major_version);
107 crypt_ftr.minor_version = props.Get<int>(tag::minor_version);
108 crypt_ftr.flags = props.Get<int>(tag::flags);
109 crypt_ftr.crypt_type = props.Get<int>(tag::crypt_type);
110 crypt_ftr.failed_decrypt_count = props.Get<int>(tag::failed_decrypt_count);
111 std::string crypto_type_name = props.Get<std::string>(tag::crypto_type_name);
112 strlcpy(reinterpret_cast<char*>(crypt_ftr.crypto_type_name),
113 crypto_type_name.c_str(),
114 sizeof(crypt_ftr.crypto_type_name));
115 std::string master_key = props.Get<std::string>(tag::master_key);
116 crypt_ftr.keysize = master_key.size();
117 if (crypt_ftr.keysize > sizeof(crypt_ftr.master_key)) {
118 SLOGE("Master key size too long");
119 return -1;
120 }
121 memcpy(crypt_ftr.master_key, &master_key[0], crypt_ftr.keysize);
122 std::string salt = props.Get<std::string>(tag::salt);
123 if (salt.size() != SALT_LEN) {
124 SLOGE("Salt wrong length");
125 return -1;
126 }
127 memcpy(crypt_ftr.salt, &salt[0], SALT_LEN);
128 crypt_ftr.kdf_type = props.Get<int>(tag::kdf_type);
129 crypt_ftr.N_factor = props.Get<int>(tag::N_factor);
130 crypt_ftr.r_factor = props.Get<int>(tag::r_factor);
131 crypt_ftr.p_factor = props.Get<int>(tag::p_factor);
132 std::string keymaster_blob = props.Get<std::string>(tag::keymaster_blob);
133 crypt_ftr.keymaster_blob_size = keymaster_blob.size();
134 if (crypt_ftr.keymaster_blob_size > sizeof(crypt_ftr.keymaster_blob)) {
135 SLOGE("Keymaster blob too long");
136 return -1;
137 }
138 memcpy(crypt_ftr.keymaster_blob, &keymaster_blob[0],
139 crypt_ftr.keymaster_blob_size);
140 std::string scrypted_intermediate_key = props.Get<std::string>(tag::scrypted_intermediate_key);
141 if (scrypted_intermediate_key.size() != SCRYPT_LEN) {
142 SLOGE("scrypted intermediate key wrong length");
143 return -1;
144 }
145 memcpy(crypt_ftr.scrypted_intermediate_key, &scrypted_intermediate_key[0],
146 SCRYPT_LEN);
147
148 return 0;
149}
150
151static UnencryptedProperties GetProps(const char* path)
152{
153 return UnencryptedProperties(path);
154}
155
156static UnencryptedProperties GetAltProps(const char* path)
157{
158 return UnencryptedProperties((std::string() + path + "/tmp_mnt").c_str());
159}
160
161static UnencryptedProperties GetPropsOrAltProps(const char* path)
162{
163 UnencryptedProperties props = GetProps(path);
164 if (props.OK()) {
165 return props;
166 }
167 return GetAltProps(path);
168}
169
170int e4crypt_enable(const char* path)
171{
172 // Already enabled?
173 if (s_key_store.find(path) != s_key_store.end()) {
174 return 0;
175 }
176
177 // Not an encryptable device?
178 UnencryptedProperties key_props = GetProps(path).GetChild(properties::key);
179 if (!key_props.OK()) {
180 return 0;
181 }
182
183 if (key_props.Get<std::string>(tag::master_key).empty()) {
184 crypt_mnt_ftr ftr;
185 if (cryptfs_create_default_ftr(&ftr, key_length)) {
186 SLOGE("Failed to create crypto footer");
187 return -1;
188 }
189
190 if (put_crypt_ftr_and_key(ftr, key_props)) {
191 SLOGE("Failed to write crypto footer");
192 return -1;
193 }
194
195 crypt_mnt_ftr ftr2;
196 if (get_crypt_ftr_and_key(ftr2, key_props)) {
197 SLOGE("Failed to read crypto footer back");
198 return -1;
199 }
200
201 if (memcmp(&ftr, &ftr2, sizeof(ftr)) != 0) {
202 SLOGE("Crypto footer not correctly written");
203 // ex4enc:TODO why is this failing?
204 //return -1;
205 }
206 }
207
208 if (!UnencryptedProperties(path).Remove(properties::ref)) {
209 SLOGE("Failed to remove key ref");
210 return -1;
211 }
212
213 return e4crypt_check_passwd(path, "");
214}
215
216int e4crypt_change_password(const char* path, int crypt_type,
217 const char* password)
218{
219 SLOGI("e4crypt_change_password");
220
221 UnencryptedProperties key_props = GetProps(path).GetChild(properties::key);
222
223 crypt_mnt_ftr ftr;
224 if (get_crypt_ftr_and_key(ftr, key_props)) {
225 SLOGE("Failed to read crypto footer back");
226 return -1;
227 }
228
229 auto mki = s_key_store.find(path);
230 if (mki == s_key_store.end()) {
231 SLOGE("No stored master key - can't change password");
232 return -1;
233 }
234
235 const unsigned char* master_key
236 = reinterpret_cast<const unsigned char*>(&mki->second.master_key[0]);
237
238 if (cryptfs_set_password(&ftr, password, master_key)) {
239 SLOGE("Failed to set password");
240 return -1;
241 }
242
243 ftr.crypt_type = crypt_type;
244
245 if (put_crypt_ftr_and_key(ftr, key_props)) {
246 SLOGE("Failed to write crypto footer");
247 return -1;
248 }
249
250 if (!UnencryptedProperties(path).Set(properties::is_default,
251 crypt_type == CRYPT_TYPE_DEFAULT)) {
252 SLOGE("Failed to update default flag");
253 return -1;
254 }
255
256 return 0;
257}
258
259int e4crypt_crypto_complete(const char* path)
260{
261 SLOGI("ext4 crypto complete called on %s", path);
262 UnencryptedProperties key_props
263 = GetPropsOrAltProps(path).GetChild(properties::key);
264 if (key_props.Get<std::string>(tag::master_key).empty()) {
265 SLOGI("No master key, so not ext4enc");
266 return -1;
267 }
268
269 return 0;
270}
271
Paul Lawrence5e7f0042015-04-10 07:48:51 -0700272static std::string generate_key_ref(const char* key, int length)
273{
274 SHA512_CTX c;
275
276 SHA512_Init(&c);
277 SHA512_Update(&c, key, length);
278 unsigned char key_ref1[SHA512_LENGTH];
279 SHA512_Final(key_ref1, &c);
280
281 SHA512_Init(&c);
282 SHA512_Update(&c, key_ref1, SHA512_LENGTH);
283 unsigned char key_ref2[SHA512_LENGTH];
284 SHA512_Final(key_ref2, &c);
285
286 return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
287}
288
Paul Lawrence707fd6c2015-04-28 22:14:15 +0000289int e4crypt_check_passwd(const char* path, const char* password)
290{
291 SLOGI("e4crypt_check_password");
292
293 // ext4enc:TODO once we have password checking, fix this to be
294 // GetKeyOrAltKey
295 UnencryptedProperties props = *password ? GetAltProps(path)
296 : GetProps(path);
297 UnencryptedProperties key_props = props.GetChild(properties::key);
298
299 crypt_mnt_ftr ftr;
300 if (get_crypt_ftr_and_key(ftr, key_props)) {
301 SLOGE("Failed to read crypto footer back");
302 return -1;
303 }
304
305 unsigned char master_key[key_length / 8];
306 if (cryptfs_get_master_key (&ftr, password, master_key)){
307 SLOGI("Incorrect password");
Paul Lawrence3ca21e22015-04-14 15:26:29 -0700308 ftr.failed_decrypt_count++;
309 if (put_crypt_ftr_and_key(ftr, key_props)) {
310 SLOGW("Failed to update failed_decrypt_count");
311 }
312 return ftr.failed_decrypt_count;
313 }
314
315 if (ftr.failed_decrypt_count) {
316 ftr.failed_decrypt_count = 0;
317 if (put_crypt_ftr_and_key(ftr, key_props)) {
318 SLOGW("Failed to reset failed_decrypt_count");
319 }
Paul Lawrence707fd6c2015-04-28 22:14:15 +0000320 }
321
322 s_key_store[path] = keys{std::string(reinterpret_cast<char*>(master_key),
323 sizeof(master_key)),
324 password};
325
326 // Install password into global keyring
Paul Lawrence5e7f0042015-04-10 07:48:51 -0700327 // ext4enc:TODO Currently raw key is required to be of length
328 // sizeof(ext4_key.raw) == EXT4_MAX_KEY_SIZE, so zero pad to
329 // this length. Change when kernel bug is fixed.
330 ext4_encryption_key ext4_key = {EXT4_ENCRYPTION_MODE_AES_256_XTS,
331 {0},
332 sizeof(ext4_key.raw)};
333 memset(ext4_key.raw, 0, sizeof(ext4_key.raw));
334 static_assert(key_length / 8 <= sizeof(ext4_key.raw),
335 "Key too long!");
336 memcpy(ext4_key.raw, master_key, key_length / 8);
Paul Lawrence707fd6c2015-04-28 22:14:15 +0000337
Paul Lawrence5e7f0042015-04-10 07:48:51 -0700338 // Get raw keyref - used to make keyname and to pass to ioctl
339 auto raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size);
340
341 // Generate keyname
342 std::ostringstream o;
343 for (auto i = raw_ref.begin(); i != raw_ref.end(); ++i) {
344 o << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
345 }
346 auto ref = std::string("ext4:") + o.str();
347
348 // Find existing keyring
Paul Lawrence707fd6c2015-04-28 22:14:15 +0000349 key_serial_t device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING,
350 "keyring", "e4crypt", 0);
351
352 SLOGI("Found device_keyring - id is %d", device_keyring);
353
Paul Lawrence5e7f0042015-04-10 07:48:51 -0700354 // Add key ...
355 key_serial_t key_id = add_key("logon", ref.c_str(),
Paul Lawrence707fd6c2015-04-28 22:14:15 +0000356 (void*)&ext4_key, sizeof(ext4_key),
357 device_keyring);
358
359 if (key_id == -1) {
360 SLOGE("Failed to insert key into keyring with error %s",
361 strerror(errno));
362 return -1;
363 }
364
Paul Lawrence5e7f0042015-04-10 07:48:51 -0700365 SLOGI("Added key %d (%s) to keyring %d in process %d",
366 key_id, ref.c_str(), device_keyring, getpid());
Paul Lawrence707fd6c2015-04-28 22:14:15 +0000367
368 // ext4enc:TODO set correct permissions
369 long result = keyctl_setperm(key_id, 0x3f3f3f3f);
370 if (result) {
371 SLOGE("KEYCTL_SETPERM failed with error %ld", result);
372 return -1;
373 }
374
375 // Save reference to key so we can set policy later
Paul Lawrence5e7f0042015-04-10 07:48:51 -0700376 if (!props.Set(properties::ref, raw_ref)) {
Paul Lawrence707fd6c2015-04-28 22:14:15 +0000377 SLOGE("Cannot save key reference");
378 return -1;
379 }
380
381 return 0;
382}
383
384int e4crypt_restart(const char* path)
385{
386 SLOGI("e4crypt_restart");
387
388 int rc = 0;
389
390 SLOGI("ext4 restart called on %s", path);
391 property_set("vold.decrypt", "trigger_reset_main");
392 SLOGI("Just asked init to shut down class main");
393 sleep(2);
394
395 std::string tmp_path = std::string() + path + "/tmp_mnt";
396
397 // ext4enc:TODO add retry logic
398 rc = umount(tmp_path.c_str());
399 if (rc) {
400 SLOGE("umount %s failed with rc %d, msg %s",
401 tmp_path.c_str(), rc, strerror(errno));
402 return rc;
403 }
404
405 // ext4enc:TODO add retry logic
406 rc = umount(path);
407 if (rc) {
408 SLOGE("umount %s failed with rc %d, msg %s",
409 path, rc, strerror(errno));
410 return rc;
411 }
412
413 return 0;
414}
415
416const char* e4crypt_get_password(const char* path)
417{
418 SLOGI("e4crypt_get_password");
419
420 // ext4enc:TODO scrub password after timeout
421 auto i = s_key_store.find(path);
422 if (i == s_key_store.end()) {
423 return 0;
424 } else {
425 return i->second.password.c_str();
426 }
427}
428
429int e4crypt_get_password_type(const char* path)
430{
431 SLOGI("e4crypt_get_password_type");
432 return GetPropsOrAltProps(path).GetChild(properties::key)
433 .Get<int>(tag::crypt_type, CRYPT_TYPE_DEFAULT);
434}
Paul Lawrence4e727452015-04-15 14:12:00 -0700435
436int e4crypt_get_field(const char* path, const char* fieldname,
437 char* value, size_t len)
438{
439 auto v = GetPropsOrAltProps(path).GetChild(properties::props)
440 .Get<std::string>(fieldname);
441
442 if (v == "") {
443 return CRYPTO_GETFIELD_ERROR_NO_FIELD;
444 }
445
446 if (v.length() >= len) {
447 return CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
448 }
449
450 strlcpy(value, v.c_str(), len);
451 return 0;
452}
453
454int e4crypt_set_field(const char* path, const char* fieldname,
455 const char* value)
456{
457 return GetPropsOrAltProps(path).GetChild(properties::props)
458 .Set(fieldname, std::string(value)) ? 0 : -1;
459}