blob: 37398e6a706e7dc672f4182fd1dbf83beb976523 [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
23#include <ext4_utils/key_control.h>
24
25#include <openssl/sha.h>
26
27#include <android-base/file.h>
28#include <android-base/logging.h>
29
30#include "KeyStorage.h"
31#include "Utils.h"
32
33namespace android {
34namespace vold {
35
36bool randomKey(std::string* key) {
37 if (ReadRandomBytes(EXT4_AES_256_XTS_KEY_SIZE, *key) != 0) {
38 // TODO status_t plays badly with PLOG, fix it.
39 LOG(ERROR) << "Random read failed";
40 return false;
41 }
42 return true;
43}
44
45// Get raw keyref - used to make keyname and to pass to ioctl
46static std::string generateKeyRef(const char* key, int length) {
47 SHA512_CTX c;
48
49 SHA512_Init(&c);
50 SHA512_Update(&c, key, length);
51 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
52 SHA512_Final(key_ref1, &c);
53
54 SHA512_Init(&c);
55 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
56 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
57 SHA512_Final(key_ref2, &c);
58
59 static_assert(EXT4_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH,
60 "Hash too short for descriptor");
61 return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
62}
63
64static bool fillKey(const std::string& key, ext4_encryption_key* ext4_key) {
65 if (key.size() != EXT4_AES_256_XTS_KEY_SIZE) {
66 LOG(ERROR) << "Wrong size key " << key.size();
67 return false;
68 }
69 static_assert(EXT4_AES_256_XTS_KEY_SIZE <= sizeof(ext4_key->raw), "Key too long!");
70 ext4_key->mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
71 ext4_key->size = key.size();
72 memset(ext4_key->raw, 0, sizeof(ext4_key->raw));
73 memcpy(ext4_key->raw, key.data(), key.size());
74 return true;
75}
76
77static std::string keyname(const std::string& raw_ref) {
78 std::ostringstream o;
79 o << "ext4:";
80 for (auto i : raw_ref) {
81 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
82 }
83 return o.str();
84}
85
86// Get the keyring we store all keys in
87static bool e4cryptKeyring(key_serial_t* device_keyring) {
88 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0);
89 if (*device_keyring == -1) {
90 PLOG(ERROR) << "Unable to find device keyring";
91 return false;
92 }
93 return true;
94}
95
96// Install password into global keyring
97// Return raw key reference for use in policy
98bool installKey(const std::string& key, std::string* raw_ref) {
99 ext4_encryption_key ext4_key;
100 if (!fillKey(key, &ext4_key)) return false;
101 *raw_ref = generateKeyRef(ext4_key.raw, ext4_key.size);
102 auto ref = keyname(*raw_ref);
103 key_serial_t device_keyring;
104 if (!e4cryptKeyring(&device_keyring)) return false;
105 key_serial_t key_id =
106 add_key("logon", ref.c_str(), (void*)&ext4_key, sizeof(ext4_key), device_keyring);
107 if (key_id == -1) {
108 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
109 return false;
110 }
111 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
112 << " in process " << getpid();
113
114 // *TODO* Remove this code when kernel is fixed - see b/28373400
115 // Kernel preserves caches across a key insertion with ext4ice, which leads
116 // to contradictory dirents
117 if (!android::base::WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
118 PLOG(ERROR) << "Failed to drop_caches";
119 }
120
121 return true;
122}
123
124bool evictKey(const std::string& raw_ref) {
125 auto ref = keyname(raw_ref);
126 key_serial_t device_keyring;
127 if (!e4cryptKeyring(&device_keyring)) return false;
128 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
129
130 // Unlink the key from the keyring. Prefer unlinking to revoking or
131 // invalidating, since unlinking is actually no less secure currently, and
132 // it avoids bugs in certain kernel versions where the keyring key is
133 // referenced from places it shouldn't be.
134 if (keyctl_unlink(key_serial, device_keyring) != 0) {
135 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
136 return false;
137 }
138 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
139 return true;
140}
141
142bool retrieveAndInstallKey(bool create_if_absent, const std::string& key_path,
143 const std::string& tmp_path, std::string* key_ref) {
144 std::string key;
145 if (pathExists(key_path)) {
146 LOG(DEBUG) << "Key exists, using: " << key_path;
147 if (!retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
148 } else {
149 if (!create_if_absent) {
150 LOG(ERROR) << "No key found in " << key_path;
151 return false;
152 }
153 LOG(INFO) << "Creating new key in " << key_path;
154 if (!randomKey(&key)) return false;
155 if (!storeKeyAtomically(key_path, tmp_path,
156 kEmptyAuthentication, key)) return false;
157 }
158
159 if (!installKey(key, key_ref)) {
160 LOG(ERROR) << "Failed to install key in " << key_path;
161 return false;
162 }
163 return true;
164}
165
166} // namespace vold
167} // namespace android