blob: 6c47add0ccd5f693e2e2a7833cdaaf3e5acf06bc [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
20#include <string>
21
22#include <errno.h>
23#include <stdio.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include "ext4_crypt.h"
28#include "key_control.h"
29
30#include <hardware/gatekeeper.h>
31#include "HashPassword.h"
32
33#include <android-base/file.h>
34
35int gatekeeper_device_initialize(gatekeeper_device_t **dev) {
36 int ret;
37 const hw_module_t *mod;
38 ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &mod);
39
40 if (ret!=0) {
41 printf("failed to get hw module\n");
42 return ret;
43 }
44
45 ret = gatekeeper_open(mod, dev);
46
47 if (ret!=0)
48 printf("failed to open gatekeeper\n");
49 return ret;
50}
51
52int Get_Password_Type(const userid_t user_id, std::string& filename) {
53 std::string path;
54 if (user_id == 0) {
55 path = "/data/system/";
56 } else {
57 char user_id_str[5];
58 sprintf(user_id_str, "%i", user_id);
59 path = "/data/system/users/";
60 path += user_id_str;
61 path += "/";
62 }
63 filename = path + "gatekeeper.password.key";
64 struct stat st;
65 if (stat(filename.c_str(), &st) == 0 && st.st_size > 0)
66 return 1;
67 filename = path + "gatekeeper.pattern.key";
68 if (stat(filename.c_str(), &st) == 0 && st.st_size > 0)
69 return 2;
70 printf("Unable to locate gatekeeper password file '%s'\n", filename.c_str());
71 filename = "";
72 return 0;
73}
74
75bool Decrypt_DE() {
76 if (!e4crypt_initialize_global_de()) { // this deals with the overarching device encryption
77 printf("e4crypt_initialize_global_de returned fail\n");
78 return false;
79 }
80 if (!e4crypt_init_user0()) {
81 printf("e4crypt_init_user0 returned fail\n");
82 return false;
83 }
84 return true;
85}
86
87bool Decrypt_User(const userid_t user_id, const std::string& Password) {
88 uint8_t *auth_token;
89 uint32_t auth_token_len;
90 int ret;
91
92 struct stat st;
93 if (user_id > 9999) {
94 printf("user_id is too big\n");
95 return false;
96 }
97 std::string filename;
98 bool Default_Password = (Password == "!");
99 if (Get_Password_Type(user_id, filename) == 0 && !Default_Password) {
100 printf("Unknown password type\n");
101 return false;
102 }
103 int flags = FLAG_STORAGE_DE;
104 if (user_id == 0)
105 flags = FLAG_STORAGE_DE;
106 else
107 flags = FLAG_STORAGE_CE;
108 gatekeeper_device_t *device;
109 ret = gatekeeper_device_initialize(&device);
110 if (Default_Password) {
111 if (!e4crypt_unlock_user_key(user_id, 0, "!", "!")) {
112 printf("e4crypt_unlock_user_key returned fail\n");
113 return false;
114 }
115 if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) {
116 printf("failed to e4crypt_prepare_user_storage\n");
117 return false;
118 }
119 printf("Decrypted Successfully!\n");
120 return true;
121 }
122 if (ret!=0)
123 return false;
124 printf("password filename is '%s'\n", filename.c_str());
125 if (stat(filename.c_str(), &st) != 0) {
126 printf("error stat'ing key file: %s\n", strerror(errno));
127 return false;
128 }
129 std::string handle;
130 if (!android::base::ReadFileToString(filename, &handle)) {
131 printf("Failed to read '%s'\n", filename.c_str());
132 return false;
133 }
134 bool should_reenroll;
135 ret = device->verify(device, user_id, 0, (const uint8_t *)handle.c_str(), st.st_size,
136 (const uint8_t *)Password.c_str(), (uint32_t)Password.size(), &auth_token, &auth_token_len,
137 &should_reenroll);
138 if (ret !=0) {
139 printf("failed to verify\n");
140 return false;
141 }
142 char token_hex[(auth_token_len*2)+1];
143 token_hex[(auth_token_len*2)] = 0;
144 uint32_t i;
145 for (i=0;i<auth_token_len;i++) {
146 sprintf(&token_hex[2*i], "%02X", auth_token[i]);
147 }
148 // 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
149 std::string secret = HashPassword(Password);
150 if (!e4crypt_unlock_user_key(user_id, 0, token_hex, secret.c_str())) {
151 printf("e4crypt_unlock_user_key returned fail\n");
152 return false;
153 }
154 if (!e4crypt_prepare_user_storage(nullptr, user_id, 0, flags)) {
155 printf("failed to e4crypt_prepare_user_storage\n");
156 return false;
157 }
158 printf("Decrypted Successfully!\n");
159 return true;
160}