blob: def1a32dfe55aa2d4351278813b44d2d8a2e4fcb [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
Paul Crowley05720802016-02-08 15:55:41 +000040const KeyAuthentication kEmptyAuthentication { "", "" };
41
Paul Crowley1ef25582016-01-21 20:26:12 +000042static constexpr size_t AES_KEY_BYTES = 32;
43static constexpr size_t GCM_NONCE_BYTES = 12;
44static constexpr size_t GCM_MAC_BYTES = 16;
45// FIXME: better name than "secdiscardable" sought!
46static constexpr size_t SECDISCARDABLE_BYTES = 1<<14;
47
Paul Crowley05720802016-02-08 15:55:41 +000048static const char* kCurrentVersion = "1";
Paul Crowley1ef25582016-01-21 20:26:12 +000049static const char* kRmPath = "/system/bin/rm";
50static const char* kSecdiscardPath = "/system/bin/secdiscard";
Paul Crowley1ef25582016-01-21 20:26:12 +000051static const char* kFn_encrypted_key = "encrypted_key";
Paul Crowley05720802016-02-08 15:55:41 +000052static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
Paul Crowley1ef25582016-01-21 20:26:12 +000053static const char* kFn_secdiscardable = "secdiscardable";
Paul Crowley05720802016-02-08 15:55:41 +000054static const char* kFn_stretching = "stretching";
55static const char* kFn_version = "version";
Paul Crowley1ef25582016-01-21 20:26:12 +000056
Paul Crowley13ffd8e2016-01-27 14:30:22 +000057static bool checkSize(const std::string& kind, size_t actual, size_t expected) {
Paul Crowley1ef25582016-01-21 20:26:12 +000058 if (actual != expected) {
59 LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected
60 << " got " << actual;
61 return false;
62 }
63 return true;
64}
65
Paul Crowley13ffd8e2016-01-27 14:30:22 +000066static std::string hashSecdiscardable(const std::string &secdiscardable) {
Paul Crowley1ef25582016-01-21 20:26:12 +000067 SHA512_CTX c;
68
69 SHA512_Init(&c);
70 // Personalise the hashing by introducing a fixed prefix.
71 // Hashing applications should use personalization except when there is a
72 // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf
Paul Crowley13ffd8e2016-01-27 14:30:22 +000073 std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512";
74 secdiscardableHashingPrefix.resize(SHA512_CBLOCK);
75 SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
Paul Crowley1ef25582016-01-21 20:26:12 +000076 SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
77 std::string res(SHA512_DIGEST_LENGTH, '\0');
78 SHA512_Final(reinterpret_cast<uint8_t *>(&res[0]), &c);
79 return res;
80}
81
Paul Crowley13ffd8e2016-01-27 14:30:22 +000082static bool generateKeymasterKey(Keymaster &keymaster,
83 const keymaster::AuthorizationSet &extraParams,
Paul Crowley1ef25582016-01-21 20:26:12 +000084 std::string &key) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000085 auto params = keymaster::AuthorizationSetBuilder()
Paul Crowley1ef25582016-01-21 20:26:12 +000086 .AesEncryptionKey(AES_KEY_BYTES * 8)
87 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
88 .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
89 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
Paul Crowley13ffd8e2016-01-27 14:30:22 +000090 .Authorization(keymaster::TAG_NO_AUTH_REQUIRED) // FIXME integrate with gatekeeper
91 .build();
92 params.push_back(extraParams);
93 return keymaster.generateKey(params, key);
Paul Crowley1ef25582016-01-21 20:26:12 +000094}
95
Paul Crowley13ffd8e2016-01-27 14:30:22 +000096static bool encryptWithKeymasterKey(
Paul Crowley1ef25582016-01-21 20:26:12 +000097 Keymaster &keymaster,
98 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +000099 const keymaster::AuthorizationSet &extraParams,
Paul Crowley1ef25582016-01-21 20:26:12 +0000100 const std::string &message,
101 std::string &ciphertext) {
102 // FIXME fix repetition
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000103 auto params = keymaster::AuthorizationSetBuilder()
Paul Crowley1ef25582016-01-21 20:26:12 +0000104 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
105 .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000106 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
107 .build();
108 params.push_back(extraParams);
109 keymaster::AuthorizationSet outParams;
110 auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, outParams);
111 if (!opHandle) return false;
112 keymaster_blob_t nonceBlob;
113 if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000114 LOG(ERROR) << "GCM encryption but no nonce generated";
115 return false;
116 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000117 // nonceBlob here is just a pointer into existing data, must not be freed
118 std::string nonce(reinterpret_cast<const char *>(nonceBlob.data), nonceBlob.data_length);
119 if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000120 std::string body;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000121 if (!opHandle.updateCompletely(message, body)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000122
123 std::string mac;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000124 if (!opHandle.finishWithOutput(mac)) return false;
125 if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000126 ciphertext = nonce + body + mac;
127 return true;
128}
129
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000130static bool decryptWithKeymasterKey(
Paul Crowley1ef25582016-01-21 20:26:12 +0000131 Keymaster &keymaster, const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000132 const keymaster::AuthorizationSet &extraParams,
Paul Crowley1ef25582016-01-21 20:26:12 +0000133 const std::string &ciphertext,
134 std::string &message) {
135 auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000136 auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
Paul Crowley1ef25582016-01-21 20:26:12 +0000137 // FIXME fix repetition
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000138 auto params = addStringParam(keymaster::AuthorizationSetBuilder(), keymaster::TAG_NONCE, nonce)
Paul Crowley1ef25582016-01-21 20:26:12 +0000139 .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
140 .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000141 .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
142 .build();
143 params.push_back(extraParams);
Paul Crowley1ef25582016-01-21 20:26:12 +0000144
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000145 auto opHandle = keymaster.begin(KM_PURPOSE_DECRYPT, key, params);
146 if (!opHandle) return false;
147 if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
148 if (!opHandle.finish()) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000149 return true;
150}
151
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000152static bool readFileToString(const std::string &filename, std::string &result) {
153 if (!android::base::ReadFileToString(filename, &result)) {
154 PLOG(ERROR) << "Failed to read from " << filename;
155 return false;
156 }
157 return true;
158}
159
160static bool writeStringToFile(const std::string &payload, const std::string &filename) {
161 if (!android::base::WriteStringToFile(payload, filename)) {
162 PLOG(ERROR) << "Failed to write to " << filename;
163 return false;
164 }
165 return true;
166}
167
Paul Crowley05720802016-02-08 15:55:41 +0000168static keymaster::AuthorizationSet buildParams(
169 const KeyAuthentication &auth, const std::string &secdiscardable) {
170 keymaster::AuthorizationSetBuilder paramBuilder;
171 auto appId = hashSecdiscardable(secdiscardable) + auth.secret;
172 addStringParam(paramBuilder, keymaster::TAG_APPLICATION_ID, appId);
173 if (!auth.token.empty()) {
174 addStringParam(paramBuilder, keymaster::TAG_AUTH_TOKEN, auth.token);
175 }
176 return paramBuilder.build();
177}
178
179bool storeKey(const std::string &dir, const KeyAuthentication &auth, const std::string &key) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000180 if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) {
181 PLOG(ERROR) << "key mkdir " << dir;
182 return false;
183 }
Paul Crowley05720802016-02-08 15:55:41 +0000184 if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000185 std::string secdiscardable;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000186 if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000187 // TODO status_t plays badly with PLOG, fix it.
188 LOG(ERROR) << "Random read failed";
189 return false;
190 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000191 if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000192 // Future proofing for when we add key stretching per b/27056334
193 auto stretching = auth.secret.empty() ? "nopassword" : "none";
194 if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false;
195 auto extraParams = buildParams(auth, secdiscardable);
Paul Crowley1ef25582016-01-21 20:26:12 +0000196 Keymaster keymaster;
197 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000198 std::string kmKey;
199 if (!generateKeymasterKey(keymaster, extraParams, kmKey)) return false;
200 std::string encryptedKey;
201 if (!encryptWithKeymasterKey(
202 keymaster, kmKey, extraParams, key, encryptedKey)) return false;
203 if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
204 if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000205 return true;
206}
207
Paul Crowley05720802016-02-08 15:55:41 +0000208bool retrieveKey(const std::string &dir, const KeyAuthentication &auth, std::string &key) {
209 std::string version;
210 if (!readFileToString(dir + "/" + kFn_version, version)) return false;
211 if (version != kCurrentVersion) {
212 LOG(ERROR) << "Version mismatch, expected " << kCurrentVersion << " got " << version;
213 return false;
214 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000215 std::string secdiscardable;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000216 if (!readFileToString(dir + "/" + kFn_secdiscardable, secdiscardable)) return false;
Paul Crowley05720802016-02-08 15:55:41 +0000217 auto extraParams = buildParams(auth, secdiscardable);
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000218 std::string kmKey;
219 if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, kmKey)) return false;
220 std::string encryptedMessage;
221 if (!readFileToString(dir + "/" + kFn_encrypted_key, encryptedMessage)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000222 Keymaster keymaster;
223 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000224 return decryptWithKeymasterKey(keymaster, kmKey, extraParams, encryptedMessage, key);
Paul Crowley1ef25582016-01-21 20:26:12 +0000225}
226
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000227static bool deleteKey(const std::string &dir) {
228 std::string kmKey;
229 if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000230 Keymaster keymaster;
231 if (!keymaster) return false;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000232 if (!keymaster.deleteKey(kmKey)) return false;
Paul Crowley1ef25582016-01-21 20:26:12 +0000233 return true;
234}
235
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000236static bool secdiscardSecdiscardable(const std::string &dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000237 if (ForkExecvp(std::vector<std::string> {
238 kSecdiscardPath, "--", dir + "/" + kFn_secdiscardable}) != 0) {
239 LOG(ERROR) << "secdiscard failed";
240 return false;
241 }
242 return true;
243}
244
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000245static bool recursiveDeleteKey(const std::string &dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000246 if (ForkExecvp(std::vector<std::string> {
247 kRmPath, "-rf", dir}) != 0) {
248 LOG(ERROR) << "recursive delete failed";
249 return false;
250 }
251 return true;
252}
253
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000254bool destroyKey(const std::string &dir) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000255 bool success = true;
256 // Try each thing, even if previous things failed.
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000257 success &= deleteKey(dir);
258 success &= secdiscardSecdiscardable(dir);
259 success &= recursiveDeleteKey(dir);
Paul Crowley1ef25582016-01-21 20:26:12 +0000260 return success;
261}
262
263} // namespace vold
264} // namespace android