blob: 3b69d46514050fe0bf07a00bae04b71fe7fe579b [file] [log] [blame]
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001/*
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"
18#include "Ext4Crypt.h"
19
Ethan Yonker79f88bd2016-12-09 14:52:12 -060020#include <map>
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060021#include <string>
22
23#include <errno.h>
24#include <stdio.h>
Ethan Yonker79f88bd2016-12-09 14:52:12 -060025#include <stdlib.h>
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060026#include <sys/stat.h>
27#include <sys/types.h>
28
29#include "ext4_crypt.h"
30#include "key_control.h"
31
32#include <hardware/gatekeeper.h>
33#include "HashPassword.h"
34
35#include <android-base/file.h>
36
Ethan Yonker79f88bd2016-12-09 14:52:12 -060037// Store main DE raw ref / policy
38extern std::string de_raw_ref;
39extern std::map<userid_t, std::string> s_de_key_raw_refs;
40extern std::map<userid_t, std::string> s_ce_key_raw_refs;
41
42static bool lookup_ref_key_internal(std::map<userid_t, std::string>& key_map, const char* policy, userid_t* user_id) {
43 for (std::map<userid_t, std::string>::iterator it=key_map.begin(); it!=key_map.end(); ++it) {
44 if (strncmp(it->second.c_str(), policy, it->second.size()) == 0) {
45 *user_id = it->first;
46 return true;
47 }
48 }
49 return false;
50}
51
52extern "C" bool lookup_ref_key(const char* policy, char* policy_type) {
53 userid_t user_id = 0;
54 if (strncmp(de_raw_ref.c_str(), policy, de_raw_ref.size()) == 0) {
55 strcpy(policy_type, "1DK");
56 return true;
57 }
58 if (!lookup_ref_key_internal(s_de_key_raw_refs, policy, &user_id)) {
59 if (!lookup_ref_key_internal(s_ce_key_raw_refs, policy, &user_id)) {
60 return false;
61 } else
62 sprintf(policy_type, "1CE%d", user_id);
63 } else
64 sprintf(policy_type, "1DE%d", user_id);
65 return true;
66}
67
68extern "C" bool lookup_ref_tar(const char* policy_type, char* policy) {
69 if (strncmp(policy_type, "1", 1) != 0) {
70 printf("Unexpected version %c\n", policy_type);
71 return false;
72 }
73 const char* ptr = policy_type + 1; // skip past the version number
74 if (strncmp(ptr, "DK", 2) == 0) {
75 strncpy(policy, de_raw_ref.data(), de_raw_ref.size());
76 return true;
77 }
78 userid_t user_id = atoi(ptr + 2);
79 std::string raw_ref;
80 if (*ptr == 'D') {
81 if (lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref)) {
82 strncpy(policy, raw_ref.data(), raw_ref.size());
83 } else
84 return false;
85 } else if (*ptr == 'C') {
86 if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
87 strncpy(policy, raw_ref.data(), raw_ref.size());
88 } else
89 return false;
90 } else {
91 printf("unknown policy type '%s'\n", policy_type);
92 return false;
93 }
94 return true;
95}
96
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060097int gatekeeper_device_initialize(gatekeeper_device_t **dev) {
98 int ret;
99 const hw_module_t *mod;
100 ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &mod);
101
102 if (ret!=0) {
103 printf("failed to get hw module\n");
104 return ret;
105 }
106
107 ret = gatekeeper_open(mod, dev);
108
109 if (ret!=0)
110 printf("failed to open gatekeeper\n");
111 return ret;
112}
113
114int Get_Password_Type(const userid_t user_id, std::string& filename) {
115 std::string path;
116 if (user_id == 0) {
117 path = "/data/system/";
118 } else {
119 char user_id_str[5];
120 sprintf(user_id_str, "%i", user_id);
121 path = "/data/system/users/";
122 path += user_id_str;
123 path += "/";
124 }
125 filename = path + "gatekeeper.password.key";
126 struct stat st;
127 if (stat(filename.c_str(), &st) == 0 && st.st_size > 0)
128 return 1;
129 filename = path + "gatekeeper.pattern.key";
130 if (stat(filename.c_str(), &st) == 0 && st.st_size > 0)
131 return 2;
132 printf("Unable to locate gatekeeper password file '%s'\n", filename.c_str());
133 filename = "";
134 return 0;
135}
136
137bool Decrypt_DE() {
138 if (!e4crypt_initialize_global_de()) { // this deals with the overarching device encryption
139 printf("e4crypt_initialize_global_de returned fail\n");
140 return false;
141 }
142 if (!e4crypt_init_user0()) {
143 printf("e4crypt_init_user0 returned fail\n");
144 return false;
145 }
146 return true;
147}
148
149bool Decrypt_User(const userid_t user_id, const std::string& Password) {
150 uint8_t *auth_token;
151 uint32_t auth_token_len;
152 int ret;
153
154 struct stat st;
155 if (user_id > 9999) {
156 printf("user_id is too big\n");
157 return false;
158 }
159 std::string filename;
160 bool Default_Password = (Password == "!");
161 if (Get_Password_Type(user_id, filename) == 0 && !Default_Password) {
162 printf("Unknown password type\n");
163 return false;
164 }
165 int flags = FLAG_STORAGE_DE;
166 if (user_id == 0)
167 flags = FLAG_STORAGE_DE;
168 else
169 flags = FLAG_STORAGE_CE;
170 gatekeeper_device_t *device;
171 ret = gatekeeper_device_initialize(&device);
172 if (Default_Password) {
173 if (!e4crypt_unlock_user_key(user_id, 0, "!", "!")) {
174 printf("e4crypt_unlock_user_key returned fail\n");
175 return false;
176 }
177 if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) {
178 printf("failed to e4crypt_prepare_user_storage\n");
179 return false;
180 }
181 printf("Decrypted Successfully!\n");
182 return true;
183 }
184 if (ret!=0)
185 return false;
186 printf("password filename is '%s'\n", filename.c_str());
187 if (stat(filename.c_str(), &st) != 0) {
188 printf("error stat'ing key file: %s\n", strerror(errno));
189 return false;
190 }
191 std::string handle;
192 if (!android::base::ReadFileToString(filename, &handle)) {
193 printf("Failed to read '%s'\n", filename.c_str());
194 return false;
195 }
196 bool should_reenroll;
197 ret = device->verify(device, user_id, 0, (const uint8_t *)handle.c_str(), st.st_size,
198 (const uint8_t *)Password.c_str(), (uint32_t)Password.size(), &auth_token, &auth_token_len,
199 &should_reenroll);
200 if (ret !=0) {
201 printf("failed to verify\n");
202 return false;
203 }
204 char token_hex[(auth_token_len*2)+1];
205 token_hex[(auth_token_len*2)] = 0;
206 uint32_t i;
207 for (i=0;i<auth_token_len;i++) {
208 sprintf(&token_hex[2*i], "%02X", auth_token[i]);
209 }
210 // 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
211 std::string secret = HashPassword(Password);
212 if (!e4crypt_unlock_user_key(user_id, 0, token_hex, secret.c_str())) {
213 printf("e4crypt_unlock_user_key returned fail\n");
214 return false;
215 }
216 if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) {
217 printf("failed to e4crypt_prepare_user_storage\n");
218 return false;
219 }
220 printf("Decrypted Successfully!\n");
221 return true;
222}