Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Team Win Recovery Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Decrypt.h" |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 18 | #ifdef USE_KEYSTORAGE_4 |
| 19 | #include "Ext4CryptPie.h" |
| 20 | #else |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 21 | #include "Ext4Crypt.h" |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 22 | #endif |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 23 | |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 24 | #include <map> |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 25 | #include <string> |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <stdio.h> |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 29 | #include <stdlib.h> |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 33 | #ifndef HAVE_LIBKEYUTILS |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 34 | #include "key_control.h" |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 35 | #else |
| 36 | #include <keyutils.h> |
| 37 | #endif |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 38 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 39 | #ifdef HAVE_SYNTH_PWD_SUPPORT |
| 40 | #include "Weaver1.h" |
| 41 | #include "cutils/properties.h" |
| 42 | |
| 43 | #include <openssl/sha.h> |
| 44 | #include <openssl/aes.h> |
| 45 | #include <openssl/evp.h> |
| 46 | #include <openssl/rand.h> |
| 47 | |
| 48 | #include <dirent.h> |
| 49 | #include <stdio.h> |
| 50 | #include <stdint.h> |
| 51 | #include <string.h> |
| 52 | #include <sys/types.h> |
| 53 | #include <fstream> |
| 54 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 55 | #include "ext4_crypt.h" |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 56 | |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 57 | #ifdef USE_KEYSTORAGE_4 |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 58 | #include <android/hardware/confirmationui/1.0/types.h> |
| 59 | #include <android/security/BnConfirmationPromptCallback.h> |
| 60 | #include <android/security/keystore/IKeystoreService.h> |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 61 | #else |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 62 | #include <keystore/IKeystoreService.h> |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 63 | #include <keystore/authorization_set.h> |
| 64 | #endif |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 65 | #include <binder/IPCThreadState.h> |
| 66 | #include <binder/IServiceManager.h> |
| 67 | |
| 68 | #include <keystore/keystore.h> |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 69 | |
| 70 | #include <algorithm> |
| 71 | extern "C" { |
| 72 | #include "crypto_scrypt.h" |
| 73 | } |
| 74 | #else |
| 75 | #include "ext4_crypt.h" |
| 76 | #endif //ifdef HAVE_SYNTH_PWD_SUPPORT |
| 77 | |
| 78 | #ifdef HAVE_GATEKEEPER1 |
| 79 | #include <android/hardware/gatekeeper/1.0/IGatekeeper.h> |
| 80 | #else |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 81 | #include <hardware/gatekeeper.h> |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 82 | #endif |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 83 | #include "HashPassword.h" |
| 84 | |
| 85 | #include <android-base/file.h> |
| 86 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 87 | #ifdef USE_KEYSTORAGE_4 |
| 88 | using android::security::keystore::IKeystoreService; |
| 89 | #endif |
| 90 | |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 91 | // Store main DE raw ref / policy |
| 92 | extern std::string de_raw_ref; |
| 93 | extern std::map<userid_t, std::string> s_de_key_raw_refs; |
| 94 | extern std::map<userid_t, std::string> s_ce_key_raw_refs; |
| 95 | |
| 96 | static bool lookup_ref_key_internal(std::map<userid_t, std::string>& key_map, const char* policy, userid_t* user_id) { |
| 97 | for (std::map<userid_t, std::string>::iterator it=key_map.begin(); it!=key_map.end(); ++it) { |
| 98 | if (strncmp(it->second.c_str(), policy, it->second.size()) == 0) { |
| 99 | *user_id = it->first; |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | extern "C" bool lookup_ref_key(const char* policy, char* policy_type) { |
| 107 | userid_t user_id = 0; |
| 108 | if (strncmp(de_raw_ref.c_str(), policy, de_raw_ref.size()) == 0) { |
| 109 | strcpy(policy_type, "1DK"); |
| 110 | return true; |
| 111 | } |
| 112 | if (!lookup_ref_key_internal(s_de_key_raw_refs, policy, &user_id)) { |
| 113 | if (!lookup_ref_key_internal(s_ce_key_raw_refs, policy, &user_id)) { |
| 114 | return false; |
| 115 | } else |
| 116 | sprintf(policy_type, "1CE%d", user_id); |
| 117 | } else |
| 118 | sprintf(policy_type, "1DE%d", user_id); |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | extern "C" bool lookup_ref_tar(const char* policy_type, char* policy) { |
| 123 | if (strncmp(policy_type, "1", 1) != 0) { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 124 | printf("Unexpected version %c\n", policy_type[0]); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | const char* ptr = policy_type + 1; // skip past the version number |
| 128 | if (strncmp(ptr, "DK", 2) == 0) { |
| 129 | strncpy(policy, de_raw_ref.data(), de_raw_ref.size()); |
| 130 | return true; |
| 131 | } |
| 132 | userid_t user_id = atoi(ptr + 2); |
| 133 | std::string raw_ref; |
| 134 | if (*ptr == 'D') { |
| 135 | if (lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref)) { |
| 136 | strncpy(policy, raw_ref.data(), raw_ref.size()); |
| 137 | } else |
| 138 | return false; |
| 139 | } else if (*ptr == 'C') { |
| 140 | if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) { |
| 141 | strncpy(policy, raw_ref.data(), raw_ref.size()); |
| 142 | } else |
| 143 | return false; |
| 144 | } else { |
| 145 | printf("unknown policy type '%s'\n", policy_type); |
| 146 | return false; |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 151 | #ifndef HAVE_GATEKEEPER1 |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 152 | int gatekeeper_device_initialize(gatekeeper_device_t **dev) { |
| 153 | int ret; |
| 154 | const hw_module_t *mod; |
| 155 | ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &mod); |
| 156 | |
| 157 | if (ret!=0) { |
| 158 | printf("failed to get hw module\n"); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | ret = gatekeeper_open(mod, dev); |
| 163 | |
| 164 | if (ret!=0) |
| 165 | printf("failed to open gatekeeper\n"); |
| 166 | return ret; |
| 167 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 168 | #endif //ifndef HAVE_GATEKEEPER1 |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 169 | |
| 170 | bool Decrypt_DE() { |
| 171 | if (!e4crypt_initialize_global_de()) { // this deals with the overarching device encryption |
| 172 | printf("e4crypt_initialize_global_de returned fail\n"); |
| 173 | return false; |
| 174 | } |
| 175 | if (!e4crypt_init_user0()) { |
| 176 | printf("e4crypt_init_user0 returned fail\n"); |
| 177 | return false; |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 182 | #ifdef HAVE_SYNTH_PWD_SUPPORT |
| 183 | // Crappy functions for debugging, please ignore unless you need to debug |
| 184 | /*void output_hex(const std::string& in) { |
| 185 | const char *buf = in.data(); |
| 186 | char hex[in.size() * 2 + 1]; |
| 187 | unsigned int index; |
| 188 | for (index = 0; index < in.size(); index++) |
| 189 | sprintf(&hex[2 * index], "%02X", buf[index]); |
| 190 | printf("%s", hex); |
| 191 | } |
| 192 | |
| 193 | void output_hex(const char* buf, const int size) { |
| 194 | char hex[size * 2 + 1]; |
| 195 | int index; |
| 196 | for (index = 0; index < size; index++) |
| 197 | sprintf(&hex[2 * index], "%02X", buf[index]); |
| 198 | printf("%s", hex); |
| 199 | } |
| 200 | |
| 201 | void output_hex(const unsigned char* buf, const int size) { |
| 202 | char hex[size * 2 + 1]; |
| 203 | int index; |
| 204 | for (index = 0; index < size; index++) |
| 205 | sprintf(&hex[2 * index], "%02X", buf[index]); |
| 206 | printf("%s", hex); |
| 207 | } |
| 208 | |
| 209 | void output_hex(std::vector<uint8_t>* vec) { |
| 210 | char hex[3]; |
| 211 | unsigned int index; |
| 212 | for (index = 0; index < vec->size(); index++) { |
| 213 | sprintf(&hex[0], "%02X", vec->at(index)); |
| 214 | printf("%s", hex); |
| 215 | } |
| 216 | }*/ |
| 217 | |
| 218 | /* An alternative is to use: |
| 219 | * sqlite3 /data/system/locksettings.db "SELECT value FROM locksettings WHERE name='sp-handle' AND user=0;" |
| 220 | * but we really don't want to include the 1.1MB libsqlite in TWRP. We scan the spblob folder for the |
| 221 | * password data file (*.pwd) and get the handle from the filename instead. This is a replacement for |
| 222 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/LockSettingsService.java#2017 |
| 223 | * We never use this data as an actual long. We always use it as a string. */ |
| 224 | bool Find_Handle(const std::string& spblob_path, std::string& handle_str) { |
| 225 | DIR* dir = opendir(spblob_path.c_str()); |
| 226 | if (!dir) { |
| 227 | printf("Error opening '%s'\n", spblob_path.c_str()); |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | struct dirent* de = 0; |
| 232 | |
| 233 | while ((de = readdir(dir)) != 0) { |
| 234 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) |
| 235 | continue; |
| 236 | size_t len = strlen(de->d_name); |
| 237 | if (len <= 4) |
| 238 | continue; |
| 239 | char* p = de->d_name; |
| 240 | p += len - 4; |
| 241 | if (strncmp(p, ".pwd", 4) == 0) { |
| 242 | handle_str = de->d_name; |
| 243 | handle_str = handle_str.substr(0, len - 4); |
| 244 | //*handle = strtoull(handle_str.c_str(), 0 , 16); |
| 245 | closedir(dir); |
| 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | closedir(dir); |
| 250 | return false; |
| 251 | } |
| 252 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 253 | /* This is the structure of the data in the password data (*.pwd) file which the structure can be found |
| 254 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#187 */ |
| 255 | struct password_data_struct { |
| 256 | int password_type; |
| 257 | unsigned char scryptN; |
| 258 | unsigned char scryptR; |
| 259 | unsigned char scryptP; |
| 260 | int salt_len; |
| 261 | void* salt; |
| 262 | int handle_len; |
| 263 | void* password_handle; |
| 264 | }; |
| 265 | |
| 266 | /* C++ replacement for |
| 267 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#764 */ |
| 268 | bool Get_Password_Data(const std::string& spblob_path, const std::string& handle_str, password_data_struct *pwd) { |
| 269 | std::string pwd_file = spblob_path + handle_str + ".pwd"; |
| 270 | std::string pwd_data; |
| 271 | if (!android::base::ReadFileToString(pwd_file, &pwd_data)) { |
| 272 | printf("Failed to read '%s'\n", pwd_file.c_str()); |
| 273 | return false; |
| 274 | } |
| 275 | //output_hex(pwd_data.data(), pwd_data.size());printf("\n"); |
| 276 | const int* intptr = (const int*)pwd_data.data(); |
| 277 | pwd->password_type = *intptr; |
| 278 | endianswap(&pwd->password_type); |
| 279 | //printf("password type %i\n", pwd->password_type); // 2 was PIN, 1 for pattern, 2 also for password, -1 for default password |
| 280 | const unsigned char* byteptr = (const unsigned char*)pwd_data.data() + sizeof(int); |
| 281 | pwd->scryptN = *byteptr; |
| 282 | byteptr++; |
| 283 | pwd->scryptR = *byteptr; |
| 284 | byteptr++; |
| 285 | pwd->scryptP = *byteptr; |
| 286 | byteptr++; |
| 287 | intptr = (const int*)byteptr; |
| 288 | pwd->salt_len = *intptr; |
| 289 | endianswap(&pwd->salt_len); |
| 290 | if (pwd->salt_len != 0) { |
| 291 | pwd->salt = malloc(pwd->salt_len); |
| 292 | if (!pwd->salt) { |
| 293 | printf("Get_Password_Data malloc salt\n"); |
| 294 | return false; |
| 295 | } |
| 296 | memcpy(pwd->salt, intptr + 1, pwd->salt_len); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 297 | intptr++; |
| 298 | byteptr = (const unsigned char*)intptr; |
| 299 | byteptr += pwd->salt_len; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 300 | } else { |
| 301 | printf("Get_Password_Data salt_len is 0\n"); |
| 302 | return false; |
| 303 | } |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 304 | intptr = (const int*)byteptr; |
| 305 | pwd->handle_len = *intptr; |
| 306 | endianswap(&pwd->handle_len); |
| 307 | if (pwd->handle_len != 0) { |
| 308 | pwd->password_handle = malloc(pwd->handle_len); |
| 309 | if (!pwd->password_handle) { |
| 310 | printf("Get_Password_Data malloc password_handle\n"); |
| 311 | return false; |
| 312 | } |
| 313 | memcpy(pwd->password_handle, intptr + 1, pwd->handle_len); |
| 314 | } else { |
| 315 | printf("Get_Password_Data handle_len is 0\n"); |
| 316 | // Not an error if using weaver |
| 317 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | |
| 321 | /* C++ replacement for |
| 322 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#765 |
| 323 | * called here |
| 324 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#1050 */ |
| 325 | bool Get_Password_Token(const password_data_struct *pwd, const std::string& Password, unsigned char* password_token) { |
| 326 | if (!password_token) { |
| 327 | printf("password_token is null\n"); |
| 328 | return false; |
| 329 | } |
| 330 | unsigned int N = 1 << pwd->scryptN; |
| 331 | unsigned int r = 1 << pwd->scryptR; |
| 332 | unsigned int p = 1 << pwd->scryptP; |
| 333 | //printf("N %i r %i p %i\n", N, r, p); |
| 334 | int ret = crypto_scrypt(reinterpret_cast<const uint8_t*>(Password.data()), Password.size(), |
| 335 | reinterpret_cast<const uint8_t*>(pwd->salt), pwd->salt_len, |
| 336 | N, r, p, |
| 337 | password_token, 32); |
| 338 | if (ret != 0) { |
| 339 | printf("scrypt error\n"); |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | // Data structure for the *.weaver file, see Get_Weaver_Data below |
| 346 | struct weaver_data_struct { |
| 347 | unsigned char version; |
| 348 | int slot; |
| 349 | }; |
| 350 | |
| 351 | /* C++ replacement for |
| 352 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#501 |
| 353 | * called here |
| 354 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#768 */ |
| 355 | bool Get_Weaver_Data(const std::string& spblob_path, const std::string& handle_str, weaver_data_struct *wd) { |
| 356 | std::string weaver_file = spblob_path + handle_str + ".weaver"; |
| 357 | std::string weaver_data; |
| 358 | if (!android::base::ReadFileToString(weaver_file, &weaver_data)) { |
| 359 | printf("Failed to read '%s'\n", weaver_file.c_str()); |
| 360 | return false; |
| 361 | } |
| 362 | //output_hex(weaver_data.data(), weaver_data.size());printf("\n"); |
| 363 | const unsigned char* byteptr = (const unsigned char*)weaver_data.data(); |
| 364 | wd->version = *byteptr; |
| 365 | //printf("weaver version %i\n", wd->version); |
| 366 | const int* intptr = (const int*)weaver_data.data() + sizeof(unsigned char); |
| 367 | wd->slot = *intptr; |
| 368 | //endianswap(&wd->slot); not needed |
| 369 | //printf("weaver slot %i\n", wd->slot); |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | namespace android { |
| 374 | |
| 375 | // On Android 8.0 for some reason init can't seem to completely stop keystore |
| 376 | // so we have to kill it too if it doesn't die on its own. |
| 377 | static void kill_keystore() { |
| 378 | DIR* dir = opendir("/proc"); |
| 379 | if (dir) { |
| 380 | struct dirent* de = 0; |
| 381 | |
| 382 | while ((de = readdir(dir)) != 0) { |
| 383 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) |
| 384 | continue; |
| 385 | |
| 386 | int pid = -1; |
| 387 | int ret = sscanf(de->d_name, "%d", &pid); |
| 388 | |
| 389 | if (ret == 1) { |
| 390 | char cmdpath[PATH_MAX]; |
| 391 | sprintf(cmdpath, "/proc/%d/cmdline", pid); |
| 392 | |
| 393 | FILE* file = fopen(cmdpath, "r"); |
| 394 | size_t task_size = PATH_MAX; |
| 395 | char task[PATH_MAX]; |
| 396 | char* p = task; |
| 397 | if (getline(&p, &task_size, file) > 0) { |
| 398 | if (strstr(task, "keystore") != 0) { |
| 399 | printf("keystore pid %d found, sending kill.\n", pid); |
| 400 | kill(pid, SIGINT); |
| 401 | usleep(5000); |
| 402 | kill(pid, SIGKILL); |
| 403 | } |
| 404 | } |
| 405 | fclose(file); |
| 406 | } |
| 407 | } |
| 408 | closedir(dir); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // The keystore holds a file open on /data so we have to stop / kill it |
| 413 | // if we want to be able to unmount /data for things like formatting. |
| 414 | static void stop_keystore() { |
| 415 | printf("Stopping keystore...\n"); |
| 416 | property_set("ctl.stop", "keystore"); |
| 417 | usleep(5000); |
| 418 | kill_keystore(); |
| 419 | } |
| 420 | |
| 421 | /* These next 2 functions try to get the keystore service 50 times because |
| 422 | * the keystore is not always ready when TWRP boots */ |
| 423 | sp<IBinder> getKeystoreBinder() { |
| 424 | sp<IServiceManager> sm = defaultServiceManager(); |
| 425 | return sm->getService(String16("android.security.keystore")); |
| 426 | } |
| 427 | |
| 428 | sp<IBinder> getKeystoreBinderRetry() { |
| 429 | printf("Starting keystore...\n"); |
| 430 | property_set("ctl.start", "keystore"); |
| 431 | int retry_count = 50; |
| 432 | sp<IBinder> binder = getKeystoreBinder(); |
| 433 | while (binder == NULL && retry_count) { |
| 434 | printf("Waiting for keystore service... %i\n", retry_count--); |
| 435 | sleep(1); |
| 436 | binder = getKeystoreBinder(); |
| 437 | } |
| 438 | return binder; |
| 439 | } |
| 440 | |
| 441 | namespace keystore { |
| 442 | |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 443 | #define SYNTHETIC_PASSWORD_VERSION_V1 1 |
Peter Cai | ea1764c | 2019-05-23 21:44:35 +0800 | [diff] [blame] | 444 | #define SYNTHETIC_PASSWORD_VERSION_V2 2 |
| 445 | #define SYNTHETIC_PASSWORD_VERSION_V3 3 |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 446 | #define SYNTHETIC_PASSWORD_PASSWORD_BASED 0 |
| 447 | #define SYNTHETIC_PASSWORD_KEY_PREFIX "USRSKEY_synthetic_password_" |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 448 | #define USR_PRIVATE_KEY_PREFIX "USRPKEY_synthetic_password_" |
| 449 | |
| 450 | static std::string mKey_Prefix; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 451 | |
| 452 | /* The keystore alias subid is sometimes the same as the handle, but not always. |
| 453 | * In the case of handle 0c5303fd2010fe29, the alias subid used c5303fd2010fe29 |
| 454 | * without the leading 0. We could try to parse the data from a previous |
| 455 | * keystore request, but I think this is an easier solution because there |
| 456 | * is little to no documentation on the format of data we get back from |
| 457 | * the keystore in this instance. We also want to copy everything to a temp |
| 458 | * folder so that any key upgrades that might take place do not actually |
| 459 | * upgrade the keys on the data partition. We rename all 1000 uid files to 0 |
| 460 | * to pass the keystore permission checks. */ |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 461 | bool Find_Keystore_Alias_SubID_And_Prep_Files(const userid_t user_id, std::string& keystoreid, const std::string& handle_str) { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 462 | char path_c[PATH_MAX]; |
| 463 | sprintf(path_c, "/data/misc/keystore/user_%d", user_id); |
| 464 | char user_dir[PATH_MAX]; |
| 465 | sprintf(user_dir, "user_%d", user_id); |
| 466 | std::string source_path = "/data/misc/keystore/"; |
| 467 | source_path += user_dir; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 468 | std::string handle_sub = handle_str; |
| 469 | while (handle_sub.substr(0,1) == "0") { |
| 470 | std::string temp = handle_sub.substr(1); |
| 471 | handle_sub = temp; |
| 472 | } |
| 473 | mKey_Prefix = ""; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 474 | |
| 475 | mkdir("/tmp/misc", 0755); |
| 476 | mkdir("/tmp/misc/keystore", 0755); |
| 477 | std::string destination_path = "/tmp/misc/keystore/"; |
| 478 | destination_path += user_dir; |
| 479 | if (mkdir(destination_path.c_str(), 0755) && errno != EEXIST) { |
| 480 | printf("failed to mkdir '%s' %s\n", destination_path.c_str(), strerror(errno)); |
| 481 | return false; |
| 482 | } |
| 483 | destination_path += "/"; |
| 484 | |
| 485 | DIR* dir = opendir(source_path.c_str()); |
| 486 | if (!dir) { |
| 487 | printf("Error opening '%s'\n", source_path.c_str()); |
| 488 | return false; |
| 489 | } |
| 490 | source_path += "/"; |
| 491 | |
| 492 | struct dirent* de = 0; |
| 493 | size_t prefix_len = strlen(SYNTHETIC_PASSWORD_KEY_PREFIX); |
| 494 | bool found_subid = false; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 495 | bool has_pkey = false; // PKEY has priority over SKEY |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 496 | |
| 497 | while ((de = readdir(dir)) != 0) { |
| 498 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) |
| 499 | continue; |
| 500 | if (!found_subid) { |
| 501 | size_t len = strlen(de->d_name); |
| 502 | if (len <= prefix_len) |
| 503 | continue; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 504 | if (strstr(de->d_name, SYNTHETIC_PASSWORD_KEY_PREFIX) && !has_pkey) |
| 505 | mKey_Prefix = SYNTHETIC_PASSWORD_KEY_PREFIX; |
| 506 | else if (strstr(de->d_name, USR_PRIVATE_KEY_PREFIX)) { |
| 507 | mKey_Prefix = USR_PRIVATE_KEY_PREFIX; |
| 508 | has_pkey = true; |
| 509 | } else |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 510 | continue; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 511 | if (strstr(de->d_name, handle_sub.c_str())) { |
| 512 | keystoreid = handle_sub; |
| 513 | printf("keystoreid matched handle_sub: '%s'\n", keystoreid.c_str()); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 514 | found_subid = true; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 515 | } else { |
| 516 | std::string file = de->d_name; |
| 517 | std::size_t found = file.find_last_of("_"); |
| 518 | if (found != std::string::npos) { |
| 519 | keystoreid = file.substr(found + 1); |
| 520 | printf("possible keystoreid: '%s'\n", keystoreid.c_str()); |
| 521 | //found_subid = true; // we'll keep going in hopes that we find a pkey or a match to the handle_sub |
| 522 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | std::string src = source_path; |
| 526 | src += de->d_name; |
| 527 | std::ifstream srcif(src.c_str(), std::ios::binary); |
| 528 | std::string dst = destination_path; |
| 529 | dst += de->d_name; |
| 530 | std::size_t source_uid = dst.find("1000"); |
| 531 | if (source_uid != std::string::npos) |
| 532 | dst.replace(source_uid, 4, "0"); |
| 533 | std::ofstream dstof(dst.c_str(), std::ios::binary); |
| 534 | printf("copying '%s' to '%s'\n", src.c_str(), dst.c_str()); |
| 535 | dstof << srcif.rdbuf(); |
| 536 | srcif.close(); |
| 537 | dstof.close(); |
| 538 | } |
| 539 | closedir(dir); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 540 | if (!found_subid && !mKey_Prefix.empty() && !keystoreid.empty()) |
| 541 | found_subid = true; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 542 | return found_subid; |
| 543 | } |
| 544 | |
| 545 | /* C++ replacement for function of the same name |
| 546 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#867 |
| 547 | * returning an empty string indicates an error */ |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 548 | std::string unwrapSyntheticPasswordBlob(const std::string& spblob_path, const std::string& handle_str, const userid_t user_id, const void* application_id, const size_t application_id_size, uint32_t auth_token_len) { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 549 | std::string disk_decryption_secret_key = ""; |
| 550 | |
| 551 | std::string keystore_alias_subid; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 552 | if (!Find_Keystore_Alias_SubID_And_Prep_Files(user_id, keystore_alias_subid, handle_str)) { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 553 | printf("failed to scan keystore alias subid and prep keystore files\n"); |
| 554 | return disk_decryption_secret_key; |
| 555 | } |
| 556 | |
| 557 | // First get the keystore service |
| 558 | sp<IBinder> binder = getKeystoreBinderRetry(); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 559 | #ifdef USE_KEYSTORAGE_4 |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 560 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 561 | #else |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 562 | sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 563 | #endif |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 564 | if (service == NULL) { |
| 565 | printf("error: could not connect to keystore service\n"); |
| 566 | return disk_decryption_secret_key; |
| 567 | } |
| 568 | |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 569 | if (auth_token_len > 0) { |
| 570 | printf("Starting keystore_auth service...\n"); |
| 571 | property_set("ctl.start", "keystore_auth"); |
| 572 | } |
| 573 | |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 574 | // Read the data from the .spblob file per: https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#869 |
| 575 | std::string spblob_file = spblob_path + handle_str + ".spblob"; |
| 576 | std::string spblob_data; |
| 577 | if (!android::base::ReadFileToString(spblob_file, &spblob_data)) { |
| 578 | printf("Failed to read '%s'\n", spblob_file.c_str()); |
| 579 | return disk_decryption_secret_key; |
| 580 | } |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 581 | unsigned char* byteptr = (unsigned char*)spblob_data.data(); |
Peter Cai | ea1764c | 2019-05-23 21:44:35 +0800 | [diff] [blame] | 582 | if (*byteptr != SYNTHETIC_PASSWORD_VERSION_V2 && *byteptr != SYNTHETIC_PASSWORD_VERSION_V1 |
| 583 | && *byteptr != SYNTHETIC_PASSWORD_VERSION_V3) { |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 584 | printf("Unsupported synthetic password version %i\n", *byteptr); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 585 | return disk_decryption_secret_key; |
| 586 | } |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 587 | const unsigned char* synthetic_password_version = byteptr; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 588 | byteptr++; |
| 589 | if (*byteptr != SYNTHETIC_PASSWORD_PASSWORD_BASED) { |
| 590 | printf("spblob data is not SYNTHETIC_PASSWORD_PASSWORD_BASED\n"); |
| 591 | return disk_decryption_secret_key; |
| 592 | } |
| 593 | byteptr++; // Now we're pointing to the blob data itself |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 594 | if (*synthetic_password_version == SYNTHETIC_PASSWORD_VERSION_V1) { |
| 595 | printf("spblob v1\n"); |
| 596 | /* We're now going to handle decryptSPBlob: https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#115 |
| 597 | * Called from https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#879 |
| 598 | * This small function ends up being quite a headache. The call to get data from the keystore basically is not needed in TWRP at this time. |
| 599 | * The keystore data seems to be the serialized data from an entire class in Java. Specifically I think it represents: |
| 600 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreCipherSpiBase.java |
| 601 | * or perhaps |
| 602 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreAuthenticatedAESCipherSpi.java |
| 603 | * but the only things we "need" from this keystore are a user ID and the keyAlias which ends up being USRSKEY_synthetic_password_{handle_str} |
| 604 | * the latter of which we already have. We may need to figure out how to get the user ID if we ever support decrypting mulitple users. |
| 605 | * There are 2 calls to a Java decrypt funcion that is overloaded. These 2 calls go in completely different directions despite the seemingly |
| 606 | * similar use of decrypt() and decrypt parameters. To figure out where things were going, I added logging to: |
| 607 | * https://android.googlesource.com/platform/libcore/+/android-8.0.0_r23/ojluni/src/main/java/javax/crypto/Cipher.java#2575 |
| 608 | * Logger.global.severe("Cipher tryCombinations " + prov.getName() + " - " + prov.getInfo()); |
| 609 | * To make logging work in libcore, import java.util.logging.Logger; and either set a better logging level or modify the framework to log everything |
| 610 | * regardless of logging level. This will give you some strings that you can grep for and find the actual crypto provider in use. In our case there were |
| 611 | * 2 different providers in use. The first stage to get the intermediate key used: |
| 612 | * https://android.googlesource.com/platform/external/conscrypt/+/android-8.0.0_r23/common/src/main/java/org/conscrypt/OpenSSLProvider.java |
| 613 | * which is a pretty straight-forward OpenSSL implementation of AES/GCM/NoPadding. */ |
| 614 | // First we personalize as seen https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#102 |
| 615 | void* personalized_application_id = PersonalizedHashBinary(PERSONALISATION_APPLICATION_ID, (const char*)application_id, application_id_size); |
| 616 | if (!personalized_application_id) { |
| 617 | printf("malloc personalized_application_id\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 618 | return disk_decryption_secret_key; |
| 619 | } |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 620 | //printf("personalized application id: "); output_hex((unsigned char*)personalized_application_id, SHA512_DIGEST_LENGTH); printf("\n"); |
| 621 | // Now we'll decrypt using openssl AES/GCM/NoPadding |
| 622 | OpenSSL_add_all_ciphers(); |
| 623 | int actual_size=0, final_size=0; |
| 624 | EVP_CIPHER_CTX *d_ctx = EVP_CIPHER_CTX_new(); |
| 625 | const unsigned char* iv = (const unsigned char*)byteptr; // The IV is the first 12 bytes of the spblob |
| 626 | //printf("iv: "); output_hex((const unsigned char*)iv, 12); printf("\n"); |
| 627 | const unsigned char* cipher_text = (const unsigned char*)byteptr + 12; // The cipher text comes immediately after the IV |
| 628 | //printf("cipher_text: "); output_hex((const unsigned char*)cipher_text, spblob_data.size() - 2 - 12); printf("\n"); |
| 629 | const unsigned char* key = (const unsigned char*)personalized_application_id; // The key is the now personalized copy of the application ID |
| 630 | //printf("key: "); output_hex((const unsigned char*)key, 32); printf("\n"); |
| 631 | EVP_DecryptInit(d_ctx, EVP_aes_256_gcm(), key, iv); |
| 632 | std::vector<unsigned char> intermediate_key; |
| 633 | intermediate_key.resize(spblob_data.size() - 2 - 12, '\0'); |
| 634 | EVP_DecryptUpdate(d_ctx, &intermediate_key[0], &actual_size, cipher_text, spblob_data.size() - 2 - 12); |
| 635 | unsigned char tag[AES_BLOCK_SIZE]; |
| 636 | EVP_CIPHER_CTX_ctrl(d_ctx, EVP_CTRL_GCM_SET_TAG, 16, tag); |
| 637 | EVP_DecryptFinal_ex(d_ctx, &intermediate_key[actual_size], &final_size); |
| 638 | EVP_CIPHER_CTX_free(d_ctx); |
| 639 | free(personalized_application_id); |
| 640 | //printf("spblob_data size: %lu actual_size %i, final_size: %i\n", spblob_data.size(), actual_size, final_size); |
| 641 | intermediate_key.resize(actual_size + final_size - 16, '\0');// not sure why we have to trim the size by 16 as I don't see where this is done in Java side |
| 642 | //printf("intermediate key: "); output_hex((const unsigned char*)intermediate_key.data(), intermediate_key.size()); printf("\n"); |
| 643 | |
| 644 | // When using secdis (aka not weaver) you must supply an auth token to the keystore prior to the begin operation |
| 645 | if (auth_token_len > 0) { |
| 646 | /*::keystore::KeyStoreServiceReturnCode auth_result = service->addAuthToken(auth_token, auth_token_len); |
| 647 | if (!auth_result.isOk()) { |
| 648 | // The keystore checks the uid of the calling process and will return a permission denied on this operation for user 0 |
| 649 | printf("keystore error adding auth token\n"); |
| 650 | return disk_decryption_secret_key; |
| 651 | }*/ |
| 652 | // The keystore refuses to allow the root user to supply auth tokens, so we write the auth token to a file earlier and |
| 653 | // run a separate service that runs user the system user to add the auth token. We wait for the auth token file to be |
| 654 | // deleted by the keymaster_auth service and check for a /auth_error file in case of errors. We quit after after a while if |
| 655 | // the /auth_token file never gets deleted. |
| 656 | int auth_wait_count = 20; |
| 657 | while (access("/auth_token", F_OK) == 0 && auth_wait_count-- > 0) |
| 658 | usleep(5000); |
| 659 | if (auth_wait_count == 0 || access("/auth_error", F_OK) == 0) { |
| 660 | printf("error during keymaster_auth service\n"); |
| 661 | /* If you are getting this error, make sure that you have the keymaster_auth service defined in your init scripts, preferrably in init.recovery.{ro.hardware}.rc |
| 662 | * service keystore_auth /sbin/keystore_auth |
| 663 | * disabled |
| 664 | * oneshot |
| 665 | * user system |
| 666 | * group root |
| 667 | * seclabel u:r:recovery:s0 |
| 668 | * |
| 669 | * And check dmesg for error codes regarding this service if needed. */ |
| 670 | return disk_decryption_secret_key; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | int32_t ret; |
| 675 | |
| 676 | /* We only need a keyAlias which is USRSKEY_synthetic_password_b6f71045af7bd042 which we find and a uid which is -1 or 1000, I forget which |
| 677 | * as the key data will be read again by the begin function later via the keystore. |
| 678 | * The data is in a hidl_vec format which consists of a type and a value. */ |
| 679 | /*::keystore::hidl_vec<uint8_t> data; |
| 680 | std::string keystoreid = SYNTHETIC_PASSWORD_KEY_PREFIX; |
| 681 | keystoreid += handle_str; |
| 682 | |
| 683 | ret = service->get(String16(keystoreid.c_str()), user_id, &data); |
| 684 | if (ret < 0) { |
| 685 | printf("Could not connect to keystore service %i\n", ret); |
| 686 | return disk_decryption_secret_key; |
| 687 | } else if (ret != 1 /*android::keystore::ResponseCode::NO_ERROR*//*) { |
| 688 | printf("keystore error: (%d)\n", /*responses[ret],*//* ret); |
| 689 | return disk_decryption_secret_key; |
| 690 | } else { |
| 691 | printf("keystore returned: "); output_hex(&data[0], data.size()); printf("\n"); |
| 692 | }*/ |
| 693 | |
| 694 | // Now we'll break up the intermediate key into the IV (first 12 bytes) and the cipher text (the rest of it). |
| 695 | std::vector<unsigned char> nonce = intermediate_key; |
| 696 | nonce.resize(12); |
| 697 | intermediate_key.erase (intermediate_key.begin(),intermediate_key.begin()+12); |
| 698 | //printf("nonce: "); output_hex((const unsigned char*)nonce.data(), nonce.size()); printf("\n"); |
| 699 | //printf("cipher text: "); output_hex((const unsigned char*)intermediate_key.data(), intermediate_key.size()); printf("\n"); |
| 700 | |
| 701 | /* Now we will begin the second decrypt call found in |
| 702 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#122 |
| 703 | * This time we will use https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreCipherSpiBase.java |
| 704 | * and https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreAuthenticatedAESCipherSpi.java |
| 705 | * First we set some algorithm parameters as seen in two places: |
| 706 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreAuthenticatedAESCipherSpi.java#297 |
| 707 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreAuthenticatedAESCipherSpi.java#216 */ |
| 708 | size_t maclen = 128; |
| 709 | ::keystore::AuthorizationSetBuilder begin_params; |
| 710 | begin_params.Authorization(::keystore::TAG_ALGORITHM, ::keystore::Algorithm::AES); |
| 711 | begin_params.Authorization(::keystore::TAG_BLOCK_MODE, ::keystore::BlockMode::GCM); |
| 712 | begin_params.Padding(::keystore::PaddingMode::NONE); |
| 713 | begin_params.Authorization(::keystore::TAG_NONCE, nonce); |
| 714 | begin_params.Authorization(::keystore::TAG_MAC_LENGTH, maclen); |
| 715 | //keymasterArgs.addEnum(KeymasterDefs.KM_TAG_ALGORITHM, KeymasterDefs.KM_ALGORITHM_AES); |
| 716 | //keymasterArgs.addEnum(KeymasterDefs.KM_TAG_BLOCK_MODE, mKeymasterBlockMode); |
| 717 | //keymasterArgs.addEnum(KeymasterDefs.KM_TAG_PADDING, mKeymasterPadding); |
| 718 | //keymasterArgs.addUnsignedInt(KeymasterDefs.KM_TAG_MAC_LENGTH, mTagLengthBits); |
| 719 | ::keystore::hidl_vec<uint8_t> entropy; // No entropy is needed for decrypt |
| 720 | entropy.resize(0); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 721 | std::string keystore_alias = mKey_Prefix; |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 722 | keystore_alias += keystore_alias_subid; |
| 723 | String16 keystore_alias16(keystore_alias.c_str()); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 724 | #ifdef USE_KEYSTORAGE_4 |
| 725 | android::hardware::keymaster::V4_0::KeyPurpose purpose = android::hardware::keymaster::V4_0::KeyPurpose::DECRYPT; |
| 726 | security::keymaster::OperationResult begin_result; |
| 727 | security::keymaster::OperationResult update_result; |
| 728 | security::keymaster::OperationResult finish_result; |
| 729 | ::android::security::keymaster::KeymasterArguments empty_params; |
| 730 | // These parameters are mostly driven by the cipher.init call https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#63 |
| 731 | service->begin(binder, keystore_alias16, (int32_t)purpose, true, android::security::keymaster::KeymasterArguments(begin_params.hidl_data()), entropy, -1, &begin_result); |
| 732 | #else |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 733 | ::keystore::KeyPurpose purpose = ::keystore::KeyPurpose::DECRYPT; |
| 734 | OperationResult begin_result; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 735 | OperationResult update_result; |
| 736 | OperationResult finish_result; |
| 737 | ::keystore::hidl_vec<::keystore::KeyParameter> empty_params; |
| 738 | empty_params.resize(0); |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 739 | // These parameters are mostly driven by the cipher.init call https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#63 |
| 740 | service->begin(binder, keystore_alias16, purpose, true, begin_params.hidl_data(), entropy, -1, &begin_result); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 741 | #endif |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 742 | ret = begin_result.resultCode; |
| 743 | if (ret != 1 /*android::keystore::ResponseCode::NO_ERROR*/) { |
| 744 | printf("keystore begin error: (%d)\n", /*responses[ret],*/ ret); |
| 745 | return disk_decryption_secret_key; |
| 746 | } else { |
| 747 | //printf("keystore begin operation successful\n"); |
| 748 | } |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 749 | // The cipher.doFinal call triggers an update to the keystore followed by a finish https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#64 |
| 750 | // See also https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/KeyStoreCryptoOperationChunkedStreamer.java#208 |
| 751 | service->update(begin_result.token, empty_params, intermediate_key, &update_result); |
| 752 | ret = update_result.resultCode; |
| 753 | if (ret != 1 /*android::keystore::ResponseCode::NO_ERROR*/) { |
| 754 | printf("keystore update error: (%d)\n", /*responses[ret],*/ ret); |
| 755 | return disk_decryption_secret_key; |
| 756 | } else { |
| 757 | //printf("keystore update operation successful\n"); |
| 758 | //printf("keystore update returned: "); output_hex(&update_result.data[0], update_result.data.size()); printf("\n"); // this ends up being the synthetic password |
| 759 | } |
| 760 | // We must use the data in update_data.data before we call finish below or the data will be gone |
| 761 | // The payload data from the keystore update is further personalized at https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#153 |
| 762 | // We now have the disk decryption key! |
| 763 | disk_decryption_secret_key = PersonalizedHash(PERSONALIZATION_FBE_KEY, (const char*)&update_result.data[0], update_result.data.size()); |
| 764 | //printf("disk_decryption_secret_key: '%s'\n", disk_decryption_secret_key.c_str()); |
| 765 | ::keystore::hidl_vec<uint8_t> signature; |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 766 | service->finish(begin_result.token, empty_params, signature, entropy, &finish_result); |
| 767 | ret = finish_result.resultCode; |
| 768 | if (ret != 1 /*android::keystore::ResponseCode::NO_ERROR*/) { |
| 769 | printf("keystore finish error: (%d)\n", /*responses[ret],*/ ret); |
| 770 | return disk_decryption_secret_key; |
| 771 | } else { |
| 772 | //printf("keystore finish operation successful\n"); |
| 773 | } |
| 774 | stop_keystore(); |
| 775 | return disk_decryption_secret_key; |
Peter Cai | ea1764c | 2019-05-23 21:44:35 +0800 | [diff] [blame] | 776 | } else if (*synthetic_password_version == SYNTHETIC_PASSWORD_VERSION_V2 |
| 777 | || *synthetic_password_version == SYNTHETIC_PASSWORD_VERSION_V3) { |
| 778 | printf("spblob v2 / v3\n"); |
| 779 | /* Version 2 / 3 of the spblob is basically the same as version 1, but the order of getting the intermediate key and disk decryption key have been flip-flopped |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 780 | * as seen in https://android.googlesource.com/platform/frameworks/base/+/5025791ac6d1538224e19189397de8d71dcb1a12 |
| 781 | */ |
| 782 | /* First decrypt call found in |
| 783 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.1.0_r18/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#135 |
| 784 | * We will use https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreCipherSpiBase.java |
| 785 | * and https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreAuthenticatedAESCipherSpi.java |
| 786 | * First we set some algorithm parameters as seen in two places: |
| 787 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreAuthenticatedAESCipherSpi.java#297 |
| 788 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/AndroidKeyStoreAuthenticatedAESCipherSpi.java#216 */ |
| 789 | // When using secdis (aka not weaver) you must supply an auth token to the keystore prior to the begin operation |
| 790 | if (auth_token_len > 0) { |
| 791 | /*::keystore::KeyStoreServiceReturnCode auth_result = service->addAuthToken(auth_token, auth_token_len); |
| 792 | if (!auth_result.isOk()) { |
| 793 | // The keystore checks the uid of the calling process and will return a permission denied on this operation for user 0 |
| 794 | printf("keystore error adding auth token\n"); |
| 795 | return disk_decryption_secret_key; |
| 796 | }*/ |
| 797 | // The keystore refuses to allow the root user to supply auth tokens, so we write the auth token to a file earlier and |
| 798 | // run a separate service that runs user the system user to add the auth token. We wait for the auth token file to be |
| 799 | // deleted by the keymaster_auth service and check for a /auth_error file in case of errors. We quit after after a while if |
| 800 | // the /auth_token file never gets deleted. |
| 801 | int auth_wait_count = 20; |
| 802 | while (access("/auth_token", F_OK) == 0 && auth_wait_count-- > 0) |
| 803 | usleep(5000); |
| 804 | if (auth_wait_count == 0 || access("/auth_error", F_OK) == 0) { |
| 805 | printf("error during keymaster_auth service\n"); |
| 806 | /* If you are getting this error, make sure that you have the keymaster_auth service defined in your init scripts, preferrably in init.recovery.{ro.hardware}.rc |
| 807 | * service keystore_auth /sbin/keystore_auth |
| 808 | * disabled |
| 809 | * oneshot |
| 810 | * user system |
| 811 | * group root |
| 812 | * seclabel u:r:recovery:s0 |
| 813 | * |
| 814 | * And check dmesg for error codes regarding this service if needed. */ |
| 815 | return disk_decryption_secret_key; |
| 816 | } |
| 817 | } |
| 818 | int32_t ret; |
| 819 | size_t maclen = 128; |
| 820 | unsigned char* iv = (unsigned char*)byteptr; // The IV is the first 12 bytes of the spblob |
| 821 | ::keystore::hidl_vec<uint8_t> iv_hidlvec; |
| 822 | iv_hidlvec.setToExternal((unsigned char*)byteptr, 12); |
| 823 | //printf("iv: "); output_hex((const unsigned char*)iv, 12); printf("\n"); |
| 824 | unsigned char* cipher_text = (unsigned char*)byteptr + 12; // The cipher text comes immediately after the IV |
| 825 | ::keystore::hidl_vec<uint8_t> cipher_text_hidlvec; |
| 826 | cipher_text_hidlvec.setToExternal(cipher_text, spblob_data.size() - 14 /* 1 each for version and SYNTHETIC_PASSWORD_PASSWORD_BASED and 12 for the iv */); |
| 827 | ::keystore::AuthorizationSetBuilder begin_params; |
| 828 | begin_params.Authorization(::keystore::TAG_ALGORITHM, ::keystore::Algorithm::AES); |
| 829 | begin_params.Authorization(::keystore::TAG_BLOCK_MODE, ::keystore::BlockMode::GCM); |
| 830 | begin_params.Padding(::keystore::PaddingMode::NONE); |
| 831 | begin_params.Authorization(::keystore::TAG_NONCE, iv_hidlvec); |
| 832 | begin_params.Authorization(::keystore::TAG_MAC_LENGTH, maclen); |
| 833 | ::keystore::hidl_vec<uint8_t> entropy; // No entropy is needed for decrypt |
| 834 | entropy.resize(0); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 835 | std::string keystore_alias = mKey_Prefix; |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 836 | keystore_alias += keystore_alias_subid; |
| 837 | String16 keystore_alias16(keystore_alias.c_str()); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 838 | #ifdef USE_KEYSTORAGE_4 |
| 839 | android::hardware::keymaster::V4_0::KeyPurpose purpose = android::hardware::keymaster::V4_0::KeyPurpose::DECRYPT; |
| 840 | security::keymaster::OperationResult begin_result; |
| 841 | security::keymaster::OperationResult update_result; |
| 842 | security::keymaster::OperationResult finish_result; |
| 843 | ::android::security::keymaster::KeymasterArguments empty_params; |
| 844 | // These parameters are mostly driven by the cipher.init call https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#63 |
| 845 | service->begin(binder, keystore_alias16, (int32_t)purpose, true, android::security::keymaster::KeymasterArguments(begin_params.hidl_data()), entropy, -1, &begin_result); |
| 846 | #else |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 847 | ::keystore::KeyPurpose purpose = ::keystore::KeyPurpose::DECRYPT; |
| 848 | OperationResult begin_result; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 849 | OperationResult update_result; |
| 850 | OperationResult finish_result; |
| 851 | ::keystore::hidl_vec<::keystore::KeyParameter> empty_params; |
| 852 | empty_params.resize(0); |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 853 | // These parameters are mostly driven by the cipher.init call https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#63 |
| 854 | service->begin(binder, keystore_alias16, purpose, true, begin_params.hidl_data(), entropy, -1, &begin_result); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 855 | #endif |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 856 | ret = begin_result.resultCode; |
| 857 | if (ret != 1 /*android::keystore::ResponseCode::NO_ERROR*/) { |
| 858 | printf("keystore begin error: (%d)\n", /*responses[ret],*/ ret); |
| 859 | return disk_decryption_secret_key; |
| 860 | } /*else { |
| 861 | printf("keystore begin operation successful\n"); |
| 862 | }*/ |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 863 | // The cipher.doFinal call triggers an update to the keystore followed by a finish https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#64 |
| 864 | // See also https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/keystore/java/android/security/keystore/KeyStoreCryptoOperationChunkedStreamer.java#208 |
| 865 | service->update(begin_result.token, empty_params, cipher_text_hidlvec, &update_result); |
| 866 | ret = update_result.resultCode; |
| 867 | if (ret != 1 /*android::keystore::ResponseCode::NO_ERROR*/) { |
| 868 | printf("keystore update error: (%d)\n", /*responses[ret],*/ ret); |
| 869 | return disk_decryption_secret_key; |
| 870 | } /*else { |
| 871 | printf("keystore update operation successful\n"); |
| 872 | printf("keystore update returned: "); output_hex(&update_result.data[0], update_result.data.size()); printf("\n"); // this ends up being the synthetic password |
| 873 | }*/ |
| 874 | //printf("keystore resulting data: "); output_hex((unsigned char*)&update_result.data[0], update_result.data.size()); printf("\n"); |
| 875 | // We must copy the data in update_data.data before we call finish below or the data will be gone |
| 876 | size_t keystore_result_size = update_result.data.size(); |
| 877 | unsigned char* keystore_result = (unsigned char*)malloc(keystore_result_size); |
| 878 | if (!keystore_result) { |
| 879 | printf("malloc on keystore_result\n"); |
| 880 | return disk_decryption_secret_key; |
| 881 | } |
| 882 | memcpy(keystore_result, &update_result.data[0], update_result.data.size()); |
| 883 | //printf("keystore_result data: "); output_hex(keystore_result, keystore_result_size); printf("\n"); |
| 884 | ::keystore::hidl_vec<uint8_t> signature; |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 885 | service->finish(begin_result.token, empty_params, signature, entropy, &finish_result); |
| 886 | ret = finish_result.resultCode; |
| 887 | if (ret != 1 /*android::keystore::ResponseCode::NO_ERROR*/) { |
| 888 | printf("keystore finish error: (%d)\n", /*responses[ret],*/ ret); |
| 889 | free(keystore_result); |
| 890 | return disk_decryption_secret_key; |
| 891 | } /*else { |
| 892 | printf("keystore finish operation successful\n"); |
| 893 | }*/ |
| 894 | stop_keystore(); |
| 895 | |
| 896 | /* Now we do the second decrypt call as seen in: |
| 897 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.1.0_r18/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#136 |
| 898 | */ |
| 899 | const unsigned char* intermediate_iv = keystore_result; |
| 900 | //printf("intermediate_iv: "); output_hex((const unsigned char*)intermediate_iv, 12); printf("\n"); |
| 901 | const unsigned char* intermediate_cipher_text = (const unsigned char*)keystore_result + 12; // The cipher text comes immediately after the IV |
| 902 | int cipher_size = keystore_result_size - 12; |
| 903 | //printf("intermediate_cipher_text: "); output_hex((const unsigned char*)intermediate_cipher_text, cipher_size); printf("\n"); |
| 904 | // First we personalize as seen https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordCrypto.java#102 |
| 905 | void* personalized_application_id = PersonalizedHashBinary(PERSONALISATION_APPLICATION_ID, (const char*)application_id, application_id_size); |
| 906 | if (!personalized_application_id) { |
| 907 | printf("malloc personalized_application_id\n"); |
| 908 | free(keystore_result); |
| 909 | return disk_decryption_secret_key; |
| 910 | } |
| 911 | //printf("personalized application id: "); output_hex((unsigned char*)personalized_application_id, SHA512_DIGEST_LENGTH); printf("\n"); |
| 912 | // Now we'll decrypt using openssl AES/GCM/NoPadding |
| 913 | OpenSSL_add_all_ciphers(); |
| 914 | int actual_size=0, final_size=0; |
| 915 | EVP_CIPHER_CTX *d_ctx = EVP_CIPHER_CTX_new(); |
| 916 | const unsigned char* key = (const unsigned char*)personalized_application_id; // The key is the now personalized copy of the application ID |
| 917 | //printf("key: "); output_hex((const unsigned char*)key, 32); printf("\n"); |
| 918 | EVP_DecryptInit(d_ctx, EVP_aes_256_gcm(), key, intermediate_iv); |
| 919 | unsigned char* secret_key = (unsigned char*)malloc(cipher_size); |
| 920 | EVP_DecryptUpdate(d_ctx, secret_key, &actual_size, intermediate_cipher_text, cipher_size); |
| 921 | unsigned char tag[AES_BLOCK_SIZE]; |
| 922 | EVP_CIPHER_CTX_ctrl(d_ctx, EVP_CTRL_GCM_SET_TAG, 16, tag); |
| 923 | EVP_DecryptFinal_ex(d_ctx, secret_key + actual_size, &final_size); |
| 924 | EVP_CIPHER_CTX_free(d_ctx); |
| 925 | free(personalized_application_id); |
| 926 | free(keystore_result); |
| 927 | int secret_key_real_size = actual_size - 16; |
| 928 | //printf("secret key: "); output_hex((const unsigned char*)secret_key, secret_key_real_size); printf("\n"); |
| 929 | // The payload data from the keystore update is further personalized at https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#153 |
| 930 | // We now have the disk decryption key! |
Peter Cai | ea1764c | 2019-05-23 21:44:35 +0800 | [diff] [blame] | 931 | if (*synthetic_password_version == SYNTHETIC_PASSWORD_VERSION_V3) { |
| 932 | // V3 uses SP800 instead of SHA512 |
| 933 | disk_decryption_secret_key = PersonalizedHashSP800(PERSONALIZATION_FBE_KEY, PERSONALISATION_CONTEXT, (const char*)secret_key, secret_key_real_size); |
| 934 | } else { |
| 935 | disk_decryption_secret_key = PersonalizedHash(PERSONALIZATION_FBE_KEY, (const char*)secret_key, secret_key_real_size); |
| 936 | } |
Ethan Yonker | c5dd579 | 2018-03-08 21:26:44 -0600 | [diff] [blame] | 937 | //printf("disk_decryption_secret_key: '%s'\n", disk_decryption_secret_key.c_str()); |
| 938 | free(secret_key); |
| 939 | return disk_decryption_secret_key; |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 940 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 941 | return disk_decryption_secret_key; |
| 942 | } |
| 943 | |
| 944 | }} |
| 945 | |
| 946 | #define PASSWORD_TOKEN_SIZE 32 |
| 947 | |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 948 | /* C++ replacement for |
| 949 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#992 |
| 950 | * called here |
| 951 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#813 */ |
| 952 | bool Get_Secdis(const std::string& spblob_path, const std::string& handle_str, std::string& secdis_data) { |
| 953 | std::string secdis_file = spblob_path + handle_str + ".secdis"; |
| 954 | if (!android::base::ReadFileToString(secdis_file, &secdis_data)) { |
| 955 | printf("Failed to read '%s'\n", secdis_file.c_str()); |
| 956 | return false; |
| 957 | } |
| 958 | //output_hex(secdis_data.data(), secdis_data.size());printf("\n"); |
| 959 | return true; |
| 960 | } |
| 961 | |
| 962 | // C++ replacement for https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#1033 |
| 963 | userid_t fakeUid(const userid_t uid) { |
| 964 | return 100000 + uid; |
| 965 | } |
| 966 | |
| 967 | bool Is_Weaver(const std::string& spblob_path, const std::string& handle_str) { |
| 968 | std::string weaver_file = spblob_path + handle_str + ".weaver"; |
| 969 | struct stat st; |
| 970 | if (stat(weaver_file.c_str(), &st) == 0) |
| 971 | return true; |
| 972 | return false; |
| 973 | } |
| 974 | |
| 975 | bool Free_Return(bool retval, void* weaver_key, password_data_struct* pwd) { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 976 | if (weaver_key) |
| 977 | free(weaver_key); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 978 | if (pwd->salt) |
| 979 | free(pwd->salt); |
| 980 | if (pwd->password_handle) |
| 981 | free(pwd->password_handle); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 982 | return retval; |
| 983 | } |
| 984 | |
| 985 | /* Decrypt_User_Synth_Pass is the TWRP C++ equivalent to spBasedDoVerifyCredential |
| 986 | * https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/LockSettingsService.java#1998 */ |
| 987 | bool Decrypt_User_Synth_Pass(const userid_t user_id, const std::string& Password) { |
| 988 | bool retval = false; |
| 989 | void* weaver_key = NULL; |
| 990 | password_data_struct pwd; |
| 991 | pwd.salt = NULL; |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 992 | pwd.salt_len = 0; |
| 993 | pwd.password_handle = NULL; |
| 994 | pwd.handle_len = 0; |
| 995 | char application_id[PASSWORD_TOKEN_SIZE + SHA512_DIGEST_LENGTH]; |
| 996 | |
| 997 | uint32_t auth_token_len = 0; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 998 | |
| 999 | std::string secret; // this will be the disk decryption key that is sent to vold |
| 1000 | std::string token = "!"; // there is no token used for this kind of decrypt, key escrow is handled by weaver |
| 1001 | int flags = FLAG_STORAGE_DE; |
| 1002 | if (user_id == 0) |
| 1003 | flags = FLAG_STORAGE_DE; |
| 1004 | else |
| 1005 | flags = FLAG_STORAGE_CE; |
| 1006 | char spblob_path_char[PATH_MAX]; |
| 1007 | sprintf(spblob_path_char, "/data/system_de/%d/spblob/", user_id); |
| 1008 | std::string spblob_path = spblob_path_char; |
| 1009 | long handle = 0; |
| 1010 | std::string handle_str; |
| 1011 | // Get the handle: https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/LockSettingsService.java#2017 |
| 1012 | if (!Find_Handle(spblob_path, handle_str)) { |
| 1013 | printf("Error getting handle\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1014 | return Free_Return(retval, weaver_key, &pwd); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1015 | } |
| 1016 | printf("Handle is '%s'\n", handle_str.c_str()); |
| 1017 | // Now we begin driving unwrapPasswordBasedSyntheticPassword from: https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#758 |
| 1018 | // First we read the password data which contains scrypt parameters |
| 1019 | if (!Get_Password_Data(spblob_path, handle_str, &pwd)) { |
| 1020 | printf("Failed to Get_Password_Data\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1021 | return Free_Return(retval, weaver_key, &pwd); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1022 | } |
| 1023 | //printf("pwd N %i R %i P %i salt ", pwd.scryptN, pwd.scryptR, pwd.scryptP); output_hex((char*)pwd.salt, pwd.salt_len); printf("\n"); |
| 1024 | unsigned char password_token[PASSWORD_TOKEN_SIZE]; |
| 1025 | //printf("Password: '%s'\n", Password.c_str()); |
| 1026 | // The password token is the password scrypted with the parameters from the password data file |
| 1027 | if (!Get_Password_Token(&pwd, Password, &password_token[0])) { |
| 1028 | printf("Failed to Get_Password_Token\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1029 | return Free_Return(retval, weaver_key, &pwd); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1030 | } |
| 1031 | //output_hex(&password_token[0], PASSWORD_TOKEN_SIZE);printf("\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1032 | if (Is_Weaver(spblob_path, handle_str)) { |
| 1033 | printf("using weaver\n"); |
| 1034 | // BEGIN PIXEL 2 WEAVER |
| 1035 | // Get the weaver data from the .weaver file which tells us which slot to use when we ask weaver for the escrowed key |
| 1036 | // https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#768 |
| 1037 | weaver_data_struct wd; |
| 1038 | if (!Get_Weaver_Data(spblob_path, handle_str, &wd)) { |
| 1039 | printf("Failed to get weaver data\n"); |
| 1040 | return Free_Return(retval, weaver_key, &pwd); |
| 1041 | } |
| 1042 | // The weaver key is the the password token prefixed with "weaver-key" padded to 128 with nulls with the password token appended then SHA512 |
| 1043 | // https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#1059 |
| 1044 | weaver_key = PersonalizedHashBinary(PERSONALISATION_WEAVER_KEY, (char*)&password_token[0], PASSWORD_TOKEN_SIZE); |
| 1045 | if (!weaver_key) { |
| 1046 | printf("malloc error getting weaver_key\n"); |
| 1047 | return Free_Return(retval, weaver_key, &pwd); |
| 1048 | } |
| 1049 | // Now we start driving weaverVerify: https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#343 |
| 1050 | // Called from https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#776 |
| 1051 | android::vold::Weaver weaver; |
| 1052 | if (!weaver) { |
| 1053 | printf("Failed to get weaver service\n"); |
| 1054 | return Free_Return(retval, weaver_key, &pwd); |
| 1055 | } |
| 1056 | // Get the key size from weaver service |
| 1057 | uint32_t weaver_key_size = 0; |
| 1058 | if (!weaver.GetKeySize(&weaver_key_size)) { |
| 1059 | printf("Failed to get weaver key size\n"); |
| 1060 | return Free_Return(retval, weaver_key, &pwd); |
| 1061 | } else { |
| 1062 | //printf("weaver key size is %u\n", weaver_key_size); |
| 1063 | } |
| 1064 | //printf("weaver key: "); output_hex((unsigned char*)weaver_key, weaver_key_size); printf("\n"); |
| 1065 | // Send the slot from the .weaver file, the computed weaver key, and get the escrowed key data |
| 1066 | std::vector<uint8_t> weaver_payload; |
| 1067 | // TODO: we should return more information about the status including time delays before the next retry |
| 1068 | if (!weaver.WeaverVerify(wd.slot, weaver_key, &weaver_payload)) { |
| 1069 | printf("failed to weaver verify\n"); |
| 1070 | return Free_Return(retval, weaver_key, &pwd); |
| 1071 | } |
| 1072 | //printf("weaver payload: "); output_hex(&weaver_payload); printf("\n"); |
| 1073 | // Done with weaverVerify |
| 1074 | // Now we will compute the application ID |
| 1075 | // https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#964 |
| 1076 | // Called from https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#780 |
| 1077 | // The escrowed weaver key data is prefixed with "weaver-pwd" padded to 128 with nulls with the weaver payload appended then SHA512 |
| 1078 | void* weaver_secret = PersonalizedHashBinary(PERSONALISATION_WEAVER_PASSWORD, (const char*)weaver_payload.data(), weaver_payload.size()); |
| 1079 | //printf("weaver secret: "); output_hex((unsigned char*)weaver_secret, SHA512_DIGEST_LENGTH); printf("\n"); |
| 1080 | // The application ID is the password token and weaver secret appended to each other |
| 1081 | memcpy((void*)&application_id[0], (void*)&password_token[0], PASSWORD_TOKEN_SIZE); |
| 1082 | memcpy((void*)&application_id[PASSWORD_TOKEN_SIZE], weaver_secret, SHA512_DIGEST_LENGTH); |
| 1083 | //printf("application ID: "); output_hex((unsigned char*)application_id, PASSWORD_TOKEN_SIZE + SHA512_DIGEST_LENGTH); printf("\n"); |
| 1084 | // END PIXEL 2 WEAVER |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1085 | } else { |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1086 | printf("using secdis\n"); |
| 1087 | std::string secdis_data; |
| 1088 | if (!Get_Secdis(spblob_path, handle_str, secdis_data)) { |
| 1089 | printf("Failed to get secdis data\n"); |
| 1090 | return Free_Return(retval, weaver_key, &pwd); |
| 1091 | } |
| 1092 | void* secdiscardable = PersonalizedHashBinary(PERSONALISATION_SECDISCARDABLE, (char*)secdis_data.data(), secdis_data.size()); |
| 1093 | if (!secdiscardable) { |
| 1094 | printf("malloc error getting secdiscardable\n"); |
| 1095 | return Free_Return(retval, weaver_key, &pwd); |
| 1096 | } |
| 1097 | memcpy((void*)&application_id[0], (void*)&password_token[0], PASSWORD_TOKEN_SIZE); |
| 1098 | memcpy((void*)&application_id[PASSWORD_TOKEN_SIZE], secdiscardable, SHA512_DIGEST_LENGTH); |
| 1099 | |
| 1100 | int ret = -1; |
| 1101 | bool request_reenroll = false; |
| 1102 | android::sp<android::hardware::gatekeeper::V1_0::IGatekeeper> gk_device; |
| 1103 | gk_device = ::android::hardware::gatekeeper::V1_0::IGatekeeper::getService(); |
| 1104 | if (gk_device == nullptr) { |
| 1105 | printf("failed to get gatekeeper service\n"); |
| 1106 | return Free_Return(retval, weaver_key, &pwd); |
| 1107 | } |
| 1108 | if (pwd.handle_len <= 0) { |
| 1109 | printf("no password handle supplied\n"); |
| 1110 | return Free_Return(retval, weaver_key, &pwd); |
| 1111 | } |
| 1112 | android::hardware::hidl_vec<uint8_t> pwd_handle_hidl; |
| 1113 | pwd_handle_hidl.setToExternal(const_cast<uint8_t *>((const uint8_t *)pwd.password_handle), pwd.handle_len); |
| 1114 | void* gk_pwd_token = PersonalizedHashBinary(PERSONALIZATION_USER_GK_AUTH, (char*)&password_token[0], PASSWORD_TOKEN_SIZE); |
| 1115 | if (!gk_pwd_token) { |
| 1116 | printf("malloc error getting gatekeeper_key\n"); |
| 1117 | return Free_Return(retval, weaver_key, &pwd); |
| 1118 | } |
| 1119 | android::hardware::hidl_vec<uint8_t> gk_pwd_token_hidl; |
| 1120 | gk_pwd_token_hidl.setToExternal(const_cast<uint8_t *>((const uint8_t *)gk_pwd_token), SHA512_DIGEST_LENGTH); |
| 1121 | android::hardware::Return<void> hwRet = |
| 1122 | gk_device->verify(fakeUid(user_id), 0 /* challange */, |
| 1123 | pwd_handle_hidl, |
| 1124 | gk_pwd_token_hidl, |
| 1125 | [&ret, &request_reenroll, &auth_token_len] |
| 1126 | (const android::hardware::gatekeeper::V1_0::GatekeeperResponse &rsp) { |
| 1127 | ret = static_cast<int>(rsp.code); // propagate errors |
| 1128 | if (rsp.code >= android::hardware::gatekeeper::V1_0::GatekeeperStatusCode::STATUS_OK) { |
| 1129 | auth_token_len = rsp.data.size(); |
| 1130 | request_reenroll = (rsp.code == android::hardware::gatekeeper::V1_0::GatekeeperStatusCode::STATUS_REENROLL); |
| 1131 | ret = 0; // all success states are reported as 0 |
| 1132 | // The keystore refuses to allow the root user to supply auth tokens, so we write the auth token to a file here and later |
| 1133 | // run a separate service that runs as the system user to add the auth token. We wait for the auth token file to be |
| 1134 | // deleted by the keymaster_auth service and check for a /auth_error file in case of errors. We quit after a while seconds if |
| 1135 | // the /auth_token file never gets deleted. |
| 1136 | unlink("/auth_token"); |
| 1137 | FILE* auth_file = fopen("/auth_token","wb"); |
| 1138 | if (auth_file != NULL) { |
| 1139 | fwrite(rsp.data.data(), sizeof(uint8_t), rsp.data.size(), auth_file); |
| 1140 | fclose(auth_file); |
| 1141 | } else { |
| 1142 | printf("failed to open /auth_token for writing\n"); |
| 1143 | ret = -2; |
| 1144 | } |
| 1145 | } else if (rsp.code == android::hardware::gatekeeper::V1_0::GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && rsp.timeout > 0) { |
| 1146 | ret = rsp.timeout; |
| 1147 | } |
| 1148 | } |
| 1149 | ); |
| 1150 | free(gk_pwd_token); |
| 1151 | if (!hwRet.isOk() || ret != 0) { |
| 1152 | printf("gatekeeper verification failed\n"); |
| 1153 | return Free_Return(retval, weaver_key, &pwd); |
| 1154 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1155 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1156 | // Now we will handle https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#816 |
| 1157 | // Plus we will include the last bit that computes the disk decrypt key found in: |
| 1158 | // https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r23/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java#153 |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1159 | secret = android::keystore::unwrapSyntheticPasswordBlob(spblob_path, handle_str, user_id, (const void*)&application_id[0], PASSWORD_TOKEN_SIZE + SHA512_DIGEST_LENGTH, auth_token_len); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1160 | if (!secret.size()) { |
| 1161 | printf("failed to unwrapSyntheticPasswordBlob\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1162 | return Free_Return(retval, weaver_key, &pwd); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1163 | } |
| 1164 | if (!e4crypt_unlock_user_key(user_id, 0, token.c_str(), secret.c_str())) { |
| 1165 | printf("e4crypt_unlock_user_key returned fail\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1166 | return Free_Return(retval, weaver_key, &pwd); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1167 | } |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 1168 | #ifdef USE_KEYSTORAGE_4 |
| 1169 | if (!e4crypt_prepare_user_storage("", user_id, 0, flags)) { |
| 1170 | #else |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1171 | if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 1172 | #endif |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1173 | printf("failed to e4crypt_prepare_user_storage\n"); |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1174 | return Free_Return(retval, weaver_key, &pwd); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1175 | } |
| 1176 | printf("Decrypted Successfully!\n"); |
| 1177 | retval = true; |
Ethan Yonker | e131bec | 2017-12-15 23:48:02 -0600 | [diff] [blame] | 1178 | return Free_Return(retval, weaver_key, &pwd); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1179 | } |
| 1180 | #endif //HAVE_SYNTH_PWD_SUPPORT |
| 1181 | |
| 1182 | int Get_Password_Type(const userid_t user_id, std::string& filename) { |
| 1183 | struct stat st; |
| 1184 | char spblob_path_char[PATH_MAX]; |
| 1185 | sprintf(spblob_path_char, "/data/system_de/%d/spblob/", user_id); |
| 1186 | if (stat(spblob_path_char, &st) == 0) { |
| 1187 | #ifdef HAVE_SYNTH_PWD_SUPPORT |
| 1188 | printf("Using synthetic password method\n"); |
| 1189 | std::string spblob_path = spblob_path_char; |
| 1190 | std::string handle_str; |
| 1191 | if (!Find_Handle(spblob_path, handle_str)) { |
| 1192 | printf("Error getting handle\n"); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | printf("Handle is '%s'\n", handle_str.c_str()); |
| 1196 | password_data_struct pwd; |
| 1197 | if (!Get_Password_Data(spblob_path, handle_str, &pwd)) { |
| 1198 | printf("Failed to Get_Password_Data\n"); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | if (pwd.password_type == 1) // In Android this means pattern |
| 1202 | return 2; // In TWRP this means pattern |
| 1203 | else if (pwd.password_type == 2) // In Android this means PIN or password |
| 1204 | return 1; // In TWRP this means PIN or password |
| 1205 | return 0; // We'll try the default password |
| 1206 | #else |
| 1207 | printf("Synthetic password support not present in TWRP\n"); |
| 1208 | return -1; |
| 1209 | #endif |
| 1210 | } |
| 1211 | std::string path; |
| 1212 | if (user_id == 0) { |
| 1213 | path = "/data/system/"; |
| 1214 | } else { |
| 1215 | char user_id_str[5]; |
| 1216 | sprintf(user_id_str, "%i", user_id); |
| 1217 | path = "/data/system/users/"; |
| 1218 | path += user_id_str; |
| 1219 | path += "/"; |
| 1220 | } |
| 1221 | filename = path + "gatekeeper.password.key"; |
| 1222 | if (stat(filename.c_str(), &st) == 0 && st.st_size > 0) |
| 1223 | return 1; |
| 1224 | filename = path + "gatekeeper.pattern.key"; |
| 1225 | if (stat(filename.c_str(), &st) == 0 && st.st_size > 0) |
| 1226 | return 2; |
| 1227 | printf("Unable to locate gatekeeper password file '%s'\n", filename.c_str()); |
| 1228 | filename = ""; |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1232 | bool Decrypt_User(const userid_t user_id, const std::string& Password) { |
| 1233 | uint8_t *auth_token; |
| 1234 | uint32_t auth_token_len; |
| 1235 | int ret; |
| 1236 | |
| 1237 | struct stat st; |
| 1238 | if (user_id > 9999) { |
| 1239 | printf("user_id is too big\n"); |
| 1240 | return false; |
| 1241 | } |
| 1242 | std::string filename; |
| 1243 | bool Default_Password = (Password == "!"); |
| 1244 | if (Get_Password_Type(user_id, filename) == 0 && !Default_Password) { |
| 1245 | printf("Unknown password type\n"); |
| 1246 | return false; |
| 1247 | } |
| 1248 | int flags = FLAG_STORAGE_DE; |
| 1249 | if (user_id == 0) |
| 1250 | flags = FLAG_STORAGE_DE; |
| 1251 | else |
| 1252 | flags = FLAG_STORAGE_CE; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1253 | if (Default_Password) { |
| 1254 | if (!e4crypt_unlock_user_key(user_id, 0, "!", "!")) { |
| 1255 | printf("e4crypt_unlock_user_key returned fail\n"); |
| 1256 | return false; |
| 1257 | } |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 1258 | #ifdef USE_KEYSTORAGE_4 |
| 1259 | if (!e4crypt_prepare_user_storage("", user_id, 0, flags)) { |
| 1260 | #else |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1261 | if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 1262 | #endif |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1263 | printf("failed to e4crypt_prepare_user_storage\n"); |
| 1264 | return false; |
| 1265 | } |
| 1266 | printf("Decrypted Successfully!\n"); |
| 1267 | return true; |
| 1268 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1269 | if (stat("/data/system_de/0/spblob", &st) == 0) { |
| 1270 | #ifdef HAVE_SYNTH_PWD_SUPPORT |
| 1271 | printf("Using synthetic password method\n"); |
| 1272 | return Decrypt_User_Synth_Pass(user_id, Password); |
| 1273 | #else |
| 1274 | printf("Synthetic password support not present in TWRP\n"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1275 | return false; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1276 | #endif |
| 1277 | } |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1278 | printf("password filename is '%s'\n", filename.c_str()); |
| 1279 | if (stat(filename.c_str(), &st) != 0) { |
| 1280 | printf("error stat'ing key file: %s\n", strerror(errno)); |
| 1281 | return false; |
| 1282 | } |
| 1283 | std::string handle; |
| 1284 | if (!android::base::ReadFileToString(filename, &handle)) { |
| 1285 | printf("Failed to read '%s'\n", filename.c_str()); |
| 1286 | return false; |
| 1287 | } |
| 1288 | bool should_reenroll; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1289 | #ifdef HAVE_GATEKEEPER1 |
| 1290 | bool request_reenroll = false; |
| 1291 | android::sp<android::hardware::gatekeeper::V1_0::IGatekeeper> gk_device; |
| 1292 | gk_device = ::android::hardware::gatekeeper::V1_0::IGatekeeper::getService(); |
| 1293 | if (gk_device == nullptr) |
| 1294 | return false; |
| 1295 | android::hardware::hidl_vec<uint8_t> curPwdHandle; |
| 1296 | curPwdHandle.setToExternal(const_cast<uint8_t *>((const uint8_t *)handle.c_str()), st.st_size); |
| 1297 | android::hardware::hidl_vec<uint8_t> enteredPwd; |
| 1298 | enteredPwd.setToExternal(const_cast<uint8_t *>((const uint8_t *)Password.c_str()), Password.size()); |
| 1299 | |
| 1300 | android::hardware::Return<void> hwRet = |
| 1301 | gk_device->verify(user_id, 0 /* challange */, |
| 1302 | curPwdHandle, |
| 1303 | enteredPwd, |
| 1304 | [&ret, &request_reenroll, &auth_token, &auth_token_len] |
| 1305 | (const android::hardware::gatekeeper::V1_0::GatekeeperResponse &rsp) { |
| 1306 | ret = static_cast<int>(rsp.code); // propagate errors |
| 1307 | if (rsp.code >= android::hardware::gatekeeper::V1_0::GatekeeperStatusCode::STATUS_OK) { |
| 1308 | auth_token = new uint8_t[rsp.data.size()]; |
| 1309 | auth_token_len = rsp.data.size(); |
| 1310 | memcpy(auth_token, rsp.data.data(), auth_token_len); |
| 1311 | request_reenroll = (rsp.code == android::hardware::gatekeeper::V1_0::GatekeeperStatusCode::STATUS_REENROLL); |
| 1312 | ret = 0; // all success states are reported as 0 |
| 1313 | } else if (rsp.code == android::hardware::gatekeeper::V1_0::GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && rsp.timeout > 0) { |
| 1314 | ret = rsp.timeout; |
| 1315 | } |
| 1316 | } |
| 1317 | ); |
| 1318 | if (!hwRet.isOk()) { |
| 1319 | return false; |
| 1320 | } |
| 1321 | #else |
| 1322 | gatekeeper_device_t *gk_device; |
| 1323 | ret = gatekeeper_device_initialize(&gk_device); |
| 1324 | if (ret!=0) |
| 1325 | return false; |
| 1326 | ret = gk_device->verify(gk_device, user_id, 0, (const uint8_t *)handle.c_str(), st.st_size, |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1327 | (const uint8_t *)Password.c_str(), (uint32_t)Password.size(), &auth_token, &auth_token_len, |
| 1328 | &should_reenroll); |
| 1329 | if (ret !=0) { |
| 1330 | printf("failed to verify\n"); |
| 1331 | return false; |
| 1332 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 1333 | #endif |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1334 | char token_hex[(auth_token_len*2)+1]; |
| 1335 | token_hex[(auth_token_len*2)] = 0; |
| 1336 | uint32_t i; |
| 1337 | for (i=0;i<auth_token_len;i++) { |
| 1338 | sprintf(&token_hex[2*i], "%02X", auth_token[i]); |
| 1339 | } |
| 1340 | // The secret is "Android FBE credential hash" plus appended 0x00 to reach 128 bytes then append the user's password then feed that to sha512sum |
| 1341 | std::string secret = HashPassword(Password); |
| 1342 | if (!e4crypt_unlock_user_key(user_id, 0, token_hex, secret.c_str())) { |
| 1343 | printf("e4crypt_unlock_user_key returned fail\n"); |
| 1344 | return false; |
| 1345 | } |
codeworkx | 22e3aa9 | 2019-04-23 12:02:26 +0200 | [diff] [blame] | 1346 | #ifdef USE_KEYSTORAGE_4 |
| 1347 | if (!e4crypt_prepare_user_storage("", user_id, 0, flags)) { |
| 1348 | #else |
| 1349 | if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) { |
| 1350 | #endif |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1351 | printf("failed to e4crypt_prepare_user_storage\n"); |
| 1352 | return false; |
| 1353 | } |
| 1354 | printf("Decrypted Successfully!\n"); |
| 1355 | return true; |
| 1356 | } |