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