blob: 070b79d1430cd1d63314235fe75ee4a914277085 [file] [log] [blame]
Paul Crowley1ef25582016-01-21 20:26:12 +00001/*
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 "KeyStorage.h"
18
19#include "Keymaster.h"
20#include "Utils.h"
21
22#include <vector>
23
24#include <errno.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <unistd.h>
29
30#include <openssl/sha.h>
31
32#include <android-base/file.h>
33#include <android-base/logging.h>
34
35#include <keymaster/authorization_set.h>
36
37namespace android {
38namespace vold {
39
40static constexpr size_t AES_KEY_BYTES = 32;
41static constexpr size_t GCM_NONCE_BYTES = 12;
42static constexpr size_t GCM_MAC_BYTES = 16;
43// FIXME: better name than "secdiscardable" sought!
44static constexpr size_t SECDISCARDABLE_BYTES = 1<<14;
45
46static const char* kRmPath = "/system/bin/rm";
47static const char* kSecdiscardPath = "/system/bin/secdiscard";
48static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
49static const char* kFn_encrypted_key = "encrypted_key";
50static const char* kFn_secdiscardable = "secdiscardable";
51
Paul Crowley13ffd8e2016-01-27 14:30:22 +000052static bool checkSize(const std::string& kind, size_t actual, size_t expected) {
Paul Crowley1ef25582016-01-21 20:26:12 +000053 if (actual != expected) {
54 LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected
55 << " got " << actual;
56 return false;
57 }
58 return true;
59}
60
Paul Crowley13ffd8e2016-01-27 14:30:22 +000061static std::string hashSecdiscardable(const std::string &secdiscardable) {
Paul Crowley1ef25582016-01-21 20:26:12 +000062 SHA512_CTX c;
63
64 SHA512_Init(&c);
65 // Personalise the hashing by introducing a fixed prefix.
66 // Hashing applications should use personalization except when there is a
67 // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf
Paul Crowley13ffd8e2016-01-27 14:30:22 +000068 std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512";
69 secdiscardableHashingPrefix.resize(SHA512_CBLOCK);
70 SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
Paul Crowley1ef25582016-01-21 20:26:12 +000071 SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
72 std::string res(SHA512_DIGEST_LENGTH, '\0');
73 SHA512_Final(reinterpret_cast<uint8_t *>(&res[0]), &c);
74 return res;
75}
76
Paul Crowley13ffd8e2016-01-27 14:30:22 +000077static bool generateKeymasterKey(Keymaster &keymaster,
78 const keymaster::AuthorizationSet &extraParams,
Paul Crowley1ef25582016-01-21 20:26:12 +000079 std::string &key) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000080 auto params = keymaster::AuthorizationSetBuilder()
Paul Crowley1ef25582016-01-21 20:26:12 +000081 .AesEncryptionKey(AES_KEY_BYTES * 8)
82 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
83 .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
84 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
Paul Crowley13ffd8e2016-01-27 14:30:22 +000085 .Authorization(keymaster::TAG_NO_AUTH_REQUIRED) // FIXME integrate with gatekeeper
86 .build();
87 params.push_back(extraParams);
88 return keymaster.generateKey(params, key);
Paul Crowley1ef25582016-01-21 20:26:12 +000089}
90
Paul Crowley13ffd8e2016-01-27 14:30:22 +000091static bool encryptWithKeymasterKey(
Paul Crowley1ef25582016-01-21 20:26:12 +000092 Keymaster &keymaster,
93 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +000094 const keymaster::AuthorizationSet &extraParams,
Paul Crowley1ef25582016-01-21 20:26:12 +000095 const std::string &message,
96 std::string &ciphertext) {
97 // FIXME fix repetition
Paul Crowley13ffd8e2016-01-27 14:30:22 +000098 auto params = keymaster::AuthorizationSetBuilder()
Paul Crowley1ef25582016-01-21 20:26:12 +000099 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
100 .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000101 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
102 .build();
103 params.push_back(extraParams);
104 keymaster::AuthorizationSet outParams;
105 auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, outParams);
106 if (!opHandle) return false;
107 keymaster_blob_t nonceBlob;
108 if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000109 LOG(ERROR) << "GCM encryption but no nonce generated";
110 return false;
111 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000112 // nonceBlob here is just a pointer into existing data, must not be freed
113 std::string nonce(reinterpret_cast<const char *>(nonceBlob.data), nonceBlob.data_length);
114 if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000115 std::string body;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000116 if (!opHandle.updateCompletely(message, body)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000117
118 std::string mac;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000119 if (!opHandle.finishWithOutput(mac)) return false;
120 if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000121 ciphertext = nonce + body + mac;
122 return true;
123}
124
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000125static bool decryptWithKeymasterKey(
Paul Crowley1ef25582016-01-21 20:26:12 +0000126 Keymaster &keymaster, const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000127 const keymaster::AuthorizationSet &extraParams,
Paul Crowley1ef25582016-01-21 20:26:12 +0000128 const std::string &ciphertext,
129 std::string &message) {
130 auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000131 auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
Paul Crowley1ef25582016-01-21 20:26:12 +0000132 // FIXME fix repetition
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000133 auto params = addStringParam(keymaster::AuthorizationSetBuilder(), keymaster::TAG_NONCE, nonce)
Paul Crowley1ef25582016-01-21 20:26:12 +0000134 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
135 .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000136 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
137 .build();
138 params.push_back(extraParams);
Paul Crowley1ef25582016-01-21 20:26:12 +0000139
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000140 auto opHandle = keymaster.begin(KM_PURPOSE_DECRYPT, key, params);
141 if (!opHandle) return false;
142 if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
143 if (!opHandle.finish()) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000144 return true;
145}
146
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000147static bool readFileToString(const std::string &filename, std::string &result) {
148 if (!android::base::ReadFileToString(filename, &result)) {
149 PLOG(ERROR) << "Failed to read from " << filename;
150 return false;
151 }
152 return true;
153}
154
155static bool writeStringToFile(const std::string &payload, const std::string &filename) {
156 if (!android::base::WriteStringToFile(payload, filename)) {
157 PLOG(ERROR) << "Failed to write to " << filename;
158 return false;
159 }
160 return true;
161}
162
163bool storeKey(const std::string &dir, const std::string &key) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000164 if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) {
165 PLOG(ERROR) << "key mkdir " << dir;
166 return false;
167 }
168 std::string secdiscardable;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000169 if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000170 // TODO status_t plays badly with PLOG, fix it.
171 LOG(ERROR) << "Random read failed";
172 return false;
173 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000174 if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
175 auto extraParams = addStringParam(keymaster::AuthorizationSetBuilder(),
176 keymaster::TAG_APPLICATION_ID, hashSecdiscardable(secdiscardable)).build();
Paul Crowley1ef25582016-01-21 20:26:12 +0000177 Keymaster keymaster;
178 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000179 std::string kmKey;
180 if (!generateKeymasterKey(keymaster, extraParams, kmKey)) return false;
181 std::string encryptedKey;
182 if (!encryptWithKeymasterKey(
183 keymaster, kmKey, extraParams, key, encryptedKey)) return false;
184 if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
185 if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000186 return true;
187}
188
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000189bool retrieveKey(const std::string &dir, std::string &key) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000190 std::string secdiscardable;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000191 if (!readFileToString(dir + "/" + kFn_secdiscardable, secdiscardable)) return false;
192 auto extraParams = addStringParam(keymaster::AuthorizationSetBuilder(),
193 keymaster::TAG_APPLICATION_ID, hashSecdiscardable(secdiscardable)).build();
194 std::string kmKey;
195 if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, kmKey)) return false;
196 std::string encryptedMessage;
197 if (!readFileToString(dir + "/" + kFn_encrypted_key, encryptedMessage)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000198 Keymaster keymaster;
199 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000200 return decryptWithKeymasterKey(keymaster, kmKey, extraParams, encryptedMessage, key);
Paul Crowley1ef25582016-01-21 20:26:12 +0000201}
202
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000203static bool deleteKey(const std::string &dir) {
204 std::string kmKey;
205 if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000206 Keymaster keymaster;
207 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000208 if (!keymaster.deleteKey(kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000209 return true;
210}
211
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000212static bool secdiscardSecdiscardable(const std::string &dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000213 if (ForkExecvp(std::vector<std::string> {
214 kSecdiscardPath, "--", dir + "/" + kFn_secdiscardable}) != 0) {
215 LOG(ERROR) << "secdiscard failed";
216 return false;
217 }
218 return true;
219}
220
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000221static bool recursiveDeleteKey(const std::string &dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000222 if (ForkExecvp(std::vector<std::string> {
223 kRmPath, "-rf", dir}) != 0) {
224 LOG(ERROR) << "recursive delete failed";
225 return false;
226 }
227 return true;
228}
229
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000230bool destroyKey(const std::string &dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000231 bool success = true;
232 // Try each thing, even if previous things failed.
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000233 success &= deleteKey(dir);
234 success &= secdiscardSecdiscardable(dir);
235 success &= recursiveDeleteKey(dir);
Paul Crowley1ef25582016-01-21 20:26:12 +0000236 return success;
237}
238
239} // namespace vold
240} // namespace android