blob: 29ba699257a9f1b5e0090315be6fc062062ac423 [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"
30#include "Utils.h"
31
32namespace android {
33namespace vold {
34
Pavel Grafove2e2d302017-08-01 17:15:53 +010035// ext4enc:TODO get this const from somewhere good
36const int EXT4_KEY_DESCRIPTOR_SIZE = 8;
37
38// ext4enc:TODO Include structure from somewhere sensible
39// MUST be in sync with ext4_crypto.c in kernel
40constexpr int EXT4_ENCRYPTION_MODE_AES_256_XTS = 1;
41constexpr int EXT4_AES_256_XTS_KEY_SIZE = 64;
42constexpr int EXT4_MAX_KEY_SIZE = 64;
43struct ext4_encryption_key {
44 uint32_t mode;
45 char raw[EXT4_MAX_KEY_SIZE];
46 uint32_t size;
47};
48
49bool randomKey(KeyBuffer* key) {
50 *key = KeyBuffer(EXT4_AES_256_XTS_KEY_SIZE);
51 if (ReadRandomBytes(key->size(), key->data()) != 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070052 // TODO status_t plays badly with PLOG, fix it.
53 LOG(ERROR) << "Random read failed";
54 return false;
55 }
56 return true;
57}
58
59// Get raw keyref - used to make keyname and to pass to ioctl
60static std::string generateKeyRef(const char* key, int length) {
61 SHA512_CTX c;
62
63 SHA512_Init(&c);
64 SHA512_Update(&c, key, length);
65 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
66 SHA512_Final(key_ref1, &c);
67
68 SHA512_Init(&c);
69 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
70 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
71 SHA512_Final(key_ref2, &c);
72
73 static_assert(EXT4_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH,
74 "Hash too short for descriptor");
75 return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
76}
77
Pavel Grafove2e2d302017-08-01 17:15:53 +010078static bool fillKey(const KeyBuffer& key, ext4_encryption_key* ext4_key) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070079 if (key.size() != EXT4_AES_256_XTS_KEY_SIZE) {
80 LOG(ERROR) << "Wrong size key " << key.size();
81 return false;
82 }
83 static_assert(EXT4_AES_256_XTS_KEY_SIZE <= sizeof(ext4_key->raw), "Key too long!");
84 ext4_key->mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
85 ext4_key->size = key.size();
86 memset(ext4_key->raw, 0, sizeof(ext4_key->raw));
87 memcpy(ext4_key->raw, key.data(), key.size());
88 return true;
89}
90
Paul Crowley14c8c072018-09-18 13:30:21 -070091static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
Paul Crowleycd8bfe32017-06-19 16:05:55 -070092
93static std::string keyname(const std::string& prefix, const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070094 std::ostringstream o;
Paul Crowleycd8bfe32017-06-19 16:05:55 -070095 o << prefix << ":";
Chen, Luhai5744dfe2017-08-18 14:49:45 +080096 for (unsigned char i : raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070097 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
98 }
99 return o.str();
100}
101
102// Get the keyring we store all keys in
103static bool e4cryptKeyring(key_serial_t* device_keyring) {
104 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0);
105 if (*device_keyring == -1) {
106 PLOG(ERROR) << "Unable to find device keyring";
107 return false;
108 }
109 return true;
110}
111
112// Install password into global keyring
113// Return raw key reference for use in policy
Pavel Grafove2e2d302017-08-01 17:15:53 +0100114bool installKey(const KeyBuffer& key, std::string* raw_ref) {
115 // Place ext4_encryption_key into automatically zeroing buffer.
116 KeyBuffer ext4KeyBuffer(sizeof(ext4_encryption_key));
Paul Crowley14c8c072018-09-18 13:30:21 -0700117 ext4_encryption_key& ext4_key = *reinterpret_cast<ext4_encryption_key*>(ext4KeyBuffer.data());
Pavel Grafove2e2d302017-08-01 17:15:53 +0100118
Paul Crowleyf71ace32016-06-02 11:01:19 -0700119 if (!fillKey(key, &ext4_key)) return false;
120 *raw_ref = generateKeyRef(ext4_key.raw, ext4_key.size);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700121 key_serial_t device_keyring;
122 if (!e4cryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700123 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
124 auto ref = keyname(*name_prefix, *raw_ref);
125 key_serial_t key_id =
126 add_key("logon", ref.c_str(), (void*)&ext4_key, sizeof(ext4_key), device_keyring);
127 if (key_id == -1) {
128 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
129 return false;
130 }
131 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
132 << " in process " << getpid();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700133 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700134 return true;
135}
136
137bool evictKey(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700138 key_serial_t device_keyring;
139 if (!e4cryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700140 bool success = true;
141 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
142 auto ref = keyname(*name_prefix, raw_ref);
143 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700144
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700145 // Unlink the key from the keyring. Prefer unlinking to revoking or
146 // invalidating, since unlinking is actually no less secure currently, and
147 // it avoids bugs in certain kernel versions where the keyring key is
148 // referenced from places it shouldn't be.
149 if (keyctl_unlink(key_serial, device_keyring) != 0) {
150 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
151 success = false;
152 } else {
153 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
154 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700155 }
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700156 return success;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700157}
158
Paul Crowley26a53882017-10-26 11:16:39 -0700159bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
160 const std::string& key_path, const std::string& tmp_path,
161 std::string* key_ref) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100162 KeyBuffer key;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700163 if (pathExists(key_path)) {
164 LOG(DEBUG) << "Key exists, using: " << key_path;
Paul Crowley26a53882017-10-26 11:16:39 -0700165 if (!retrieveKey(key_path, key_authentication, &key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700166 } else {
167 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700168 LOG(ERROR) << "No key found in " << key_path;
169 return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700170 }
171 LOG(INFO) << "Creating new key in " << key_path;
172 if (!randomKey(&key)) return false;
Paul Crowley26a53882017-10-26 11:16:39 -0700173 if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700174 }
175
176 if (!installKey(key, key_ref)) {
177 LOG(ERROR) << "Failed to install key in " << key_path;
178 return false;
179 }
180 return true;
181}
182
Paul Crowley14c8c072018-09-18 13:30:21 -0700183bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
184 KeyBuffer* key) {
Paul Crowleyd5759812016-06-02 11:04:27 -0700185 if (pathExists(key_path)) {
186 LOG(DEBUG) << "Key exists, using: " << key_path;
187 if (!retrieveKey(key_path, kEmptyAuthentication, key)) return false;
188 } else {
189 if (!create_if_absent) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700190 LOG(ERROR) << "No key found in " << key_path;
191 return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700192 }
193 LOG(INFO) << "Creating new key in " << key_path;
194 if (!randomKey(key)) return false;
Paul Crowley14c8c072018-09-18 13:30:21 -0700195 if (!storeKeyAtomically(key_path, tmp_path, kEmptyAuthentication, *key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700196 }
197 return true;
198}
199
Paul Crowleyf71ace32016-06-02 11:01:19 -0700200} // namespace vold
201} // namespace android