blob: 70a1952dd425a693e3b4c6800575464296f011f5 [file] [log] [blame]
Paul Crowleyf71ace32016-06-02 11:01:19 -07001/*
2 * Copyright (C) 2016 The Android Open Source 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 "KeyUtil.h"
18
19#include <iomanip>
20#include <sstream>
21#include <string>
22
Paul Crowleyf71ace32016-06-02 11:01:19 -070023#include <openssl/sha.h>
24
25#include <android-base/file.h>
26#include <android-base/logging.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070027#include <keyutils.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070028
29#include "KeyStorage.h"
Shivaprasad Hongal92292622018-07-05 14:49:12 -070030#include "Ext4Crypt.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070031#include "Utils.h"
32
Shivaprasad Hongal92292622018-07-05 14:49:12 -070033#define MAX_USER_ID 0xFFFFFFFF
34
35using android::hardware::keymaster::V4_0::KeyFormat;
36using android::vold::KeyType;
Paul Crowleyf71ace32016-06-02 11:01:19 -070037namespace android {
38namespace vold {
39
Pavel Grafove2e2d302017-08-01 17:15:53 +010040// ext4enc:TODO get this const from somewhere good
41const int EXT4_KEY_DESCRIPTOR_SIZE = 8;
42
43// ext4enc:TODO Include structure from somewhere sensible
44// MUST be in sync with ext4_crypto.c in kernel
45constexpr int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1;
46constexpr int EXT4_AES_256_XTS_KEY_SIZE = 64;
47constexpr int EXT4_MAX_KEY_SIZE = 64;
48struct ext4_encryption_key {
49 uint32_t mode;
50 char raw[EXT4_MAX_KEY_SIZE];
51 uint32_t size;
52};
53
54bool randomKey(KeyBuffer* key) {
55 *key = KeyBuffer(EXT4_AES_256_XTS_KEY_SIZE);
56 if (ReadRandomBytes(key->size(), key->data()) != 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070057 // TODO status_t plays badly with PLOG, fix it.
58 LOG(ERROR) << "Random read failed";
59 return false;
60 }
61 return true;
62}
63
64// Get raw keyref - used to make keyname and to pass to ioctl
65static std::string generateKeyRef(const char* key, int length) {
66 SHA512_CTX c;
67
68 SHA512_Init(&c);
69 SHA512_Update(&c, key, length);
70 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
71 SHA512_Final(key_ref1, &c);
72
73 SHA512_Init(&c);
74 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
75 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
76 SHA512_Final(key_ref2, &c);
77
78 static_assert(EXT4_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH,
79 "Hash too short for descriptor");
80 return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
81}
82
Pavel Grafove2e2d302017-08-01 17:15:53 +010083static bool fillKey(const KeyBuffer& key, ext4_encryption_key* ext4_key) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070084 if (key.size() != EXT4_AES_256_XTS_KEY_SIZE) {
85 LOG(ERROR) << "Wrong size key " << key.size();
86 return false;
87 }
88 static_assert(EXT4_AES_256_XTS_KEY_SIZE <= sizeof(ext4_key->raw), "Key too long!");
89 ext4_key->mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
90 ext4_key->size = key.size();
91 memset(ext4_key->raw, 0, sizeof(ext4_key->raw));
92 memcpy(ext4_key->raw, key.data(), key.size());
93 return true;
94}
95
Paul Crowley14c8c072018-09-18 13:30:21 -070096static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
Paul Crowleycd8bfe32017-06-19 16:05:55 -070097
98static std::string keyname(const std::string& prefix, const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070099 std::ostringstream o;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700100 o << prefix << ":";
Chen, Luhai5744dfe2017-08-18 14:49:45 +0800101 for (unsigned char i : raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700102 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
103 }
104 return o.str();
105}
106
107// Get the keyring we store all keys in
108static bool e4cryptKeyring(key_serial_t* device_keyring) {
109 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0);
110 if (*device_keyring == -1) {
111 PLOG(ERROR) << "Unable to find device keyring";
112 return false;
113 }
114 return true;
115}
116
117// Install password into global keyring
118// Return raw key reference for use in policy
Pavel Grafove2e2d302017-08-01 17:15:53 +0100119bool installKey(const KeyBuffer& key, std::string* raw_ref) {
120 // Place ext4_encryption_key into automatically zeroing buffer.
121 KeyBuffer ext4KeyBuffer(sizeof(ext4_encryption_key));
Paul Crowley14c8c072018-09-18 13:30:21 -0700122 ext4_encryption_key& ext4_key = *reinterpret_cast<ext4_encryption_key*>(ext4KeyBuffer.data());
Pavel Grafove2e2d302017-08-01 17:15:53 +0100123
Paul Crowleyf71ace32016-06-02 11:01:19 -0700124 if (!fillKey(key, &ext4_key)) return false;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700125 if (is_wrapped_key_supported()) {
126 /* When wrapped key is supported, only the first 32 bytes are
127 the same per boot. The second 32 bytes can change as the ephemeral
128 key is different. */
129 *raw_ref = generateKeyRef(ext4_key.raw, (ext4_key.size)/2);
130 } else {
131 *raw_ref = generateKeyRef(ext4_key.raw, ext4_key.size);
132 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700133 key_serial_t device_keyring;
134 if (!e4cryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700135 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
136 auto ref = keyname(*name_prefix, *raw_ref);
137 key_serial_t key_id =
138 add_key("logon", ref.c_str(), (void*)&ext4_key, sizeof(ext4_key), device_keyring);
139 if (key_id == -1) {
140 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
141 return false;
142 }
143 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
144 << " in process " << getpid();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700145 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700146 return true;
147}
148
149bool evictKey(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700150 key_serial_t device_keyring;
151 if (!e4cryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700152 bool success = true;
153 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
154 auto ref = keyname(*name_prefix, raw_ref);
155 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700156
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700157 // Unlink the key from the keyring. Prefer unlinking to revoking or
158 // invalidating, since unlinking is actually no less secure currently, and
159 // it avoids bugs in certain kernel versions where the keyring key is
160 // referenced from places it shouldn't be.
161 if (keyctl_unlink(key_serial, device_keyring) != 0) {
162 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
163 success = false;
164 } else {
165 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
166 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700167 }
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700168 return success;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700169}
170
Paul Crowley26a53882017-10-26 11:16:39 -0700171bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
172 const std::string& key_path, const std::string& tmp_path,
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700173 std::string* key_ref, bool wrapped_key_supported) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100174 KeyBuffer key;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700175 if (pathExists(key_path)) {
176 LOG(DEBUG) << "Key exists, using: " << key_path;
Paul Crowley26a53882017-10-26 11:16:39 -0700177 if (!retrieveKey(key_path, key_authentication, &key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700178 } else {
179 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700180 LOG(ERROR) << "No key found in " << key_path;
181 return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700182 }
183 LOG(INFO) << "Creating new key in " << key_path;
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700184 if (wrapped_key_supported) {
185 if(!generateWrappedKey(MAX_USER_ID, KeyType::DE_SYS, &key)) return false;
186 } else {
187 if (!randomKey(&key)) return false;
188 }
Paul Crowley26a53882017-10-26 11:16:39 -0700189 if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700190 }
191
Shivaprasad Hongal92292622018-07-05 14:49:12 -0700192 if (wrapped_key_supported) {
193 KeyBuffer ephemeral_wrapped_key;
194 if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) {
195 LOG(ERROR) << "Failed to export key in retrieveAndInstallKey";
196 return false;
197 }
198 key = std::move(ephemeral_wrapped_key);
199 }
200
Paul Crowleyf71ace32016-06-02 11:01:19 -0700201 if (!installKey(key, key_ref)) {
202 LOG(ERROR) << "Failed to install key in " << key_path;
203 return false;
204 }
205 return true;
206}
207
Paul Crowley14c8c072018-09-18 13:30:21 -0700208bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
209 KeyBuffer* key) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700210 if (pathExists(key_path)) {
211 LOG(DEBUG) << "Key exists, using: " << key_path;
212 if (!retrieveKey(key_path, kEmptyAuthentication, key)) return false;
213 } else {
214 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700215 LOG(ERROR) << "No key found in " << key_path;
216 return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700217 }
218 LOG(INFO) << "Creating new key in " << key_path;
219 if (!randomKey(key)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700220 if (!storeKeyAtomically(key_path, tmp_path, kEmptyAuthentication, *key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700221 }
222 return true;
223}
224
Paul Crowleyf71ace32016-06-02 11:01:19 -0700225} // namespace vold
226} // namespace android