blob: fe0f3cba0689eb54d759c60bb9e96594c8570662 [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
Paul Crowleyd5759812016-06-02 11:04:27 -070077std::string keyname(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070078 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
Paul Crowleyf71ace32016-06-02 11:01:19 -0700114 return true;
115}
116
117bool evictKey(const std::string& raw_ref) {
118 auto ref = keyname(raw_ref);
119 key_serial_t device_keyring;
120 if (!e4cryptKeyring(&device_keyring)) return false;
121 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
122
123 // Unlink the key from the keyring. Prefer unlinking to revoking or
124 // invalidating, since unlinking is actually no less secure currently, and
125 // it avoids bugs in certain kernel versions where the keyring key is
126 // referenced from places it shouldn't be.
127 if (keyctl_unlink(key_serial, device_keyring) != 0) {
128 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
129 return false;
130 }
131 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
132 return true;
133}
134
135bool retrieveAndInstallKey(bool create_if_absent, const std::string& key_path,
136 const std::string& tmp_path, std::string* key_ref) {
137 std::string key;
138 if (pathExists(key_path)) {
139 LOG(DEBUG) << "Key exists, using: " << key_path;
140 if (!retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
141 } else {
142 if (!create_if_absent) {
143 LOG(ERROR) << "No key found in " << key_path;
144 return false;
145 }
146 LOG(INFO) << "Creating new key in " << key_path;
147 if (!randomKey(&key)) return false;
148 if (!storeKeyAtomically(key_path, tmp_path,
149 kEmptyAuthentication, key)) return false;
150 }
151
152 if (!installKey(key, key_ref)) {
153 LOG(ERROR) << "Failed to install key in " << key_path;
154 return false;
155 }
156 return true;
157}
158
Paul Crowleyd5759812016-06-02 11:04:27 -0700159bool retrieveKey(bool create_if_absent, const std::string& key_path,
160 const std::string& tmp_path, std::string* key) {
161 if (pathExists(key_path)) {
162 LOG(DEBUG) << "Key exists, using: " << key_path;
163 if (!retrieveKey(key_path, kEmptyAuthentication, key)) return false;
164 } else {
165 if (!create_if_absent) {
166 LOG(ERROR) << "No key found in " << key_path;
167 return false;
168 }
169 LOG(INFO) << "Creating new key in " << key_path;
170 if (!randomKey(key)) return false;
171 if (!storeKeyAtomically(key_path, tmp_path,
172 kEmptyAuthentication, *key)) return false;
173 }
174 return true;
175}
176
Paul Crowleyf71ace32016-06-02 11:01:19 -0700177} // namespace vold
178} // namespace android