blob: e173b68e95a516b2ca05558bb5dba2511fb7d24d [file] [log] [blame]
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001/*
2 * Copyright (C) 2015 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#define LOG_TAG "keystore"
18
19#include <arpa/inet.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <string.h>
23
24#include <cutils/log.h>
25
26#include "blob.h"
27#include "entropy.h"
28
29#include "keystore_utils.h"
30
31Blob::Blob(const uint8_t* value, size_t valueLength, const uint8_t* info, uint8_t infoLength,
32 BlobType type) {
33 memset(&mBlob, 0, sizeof(mBlob));
34 if (valueLength > VALUE_SIZE) {
35 valueLength = VALUE_SIZE;
36 ALOGW("Provided blob length too large");
37 }
38 if (infoLength + valueLength > VALUE_SIZE) {
39 infoLength = VALUE_SIZE - valueLength;
40 ALOGW("Provided info length too large");
41 }
42 mBlob.length = valueLength;
43 memcpy(mBlob.value, value, valueLength);
44
45 mBlob.info = infoLength;
46 memcpy(mBlob.value + valueLength, info, infoLength);
47
48 mBlob.version = CURRENT_BLOB_VERSION;
49 mBlob.type = uint8_t(type);
50
51 if (type == TYPE_MASTER_KEY) {
52 mBlob.flags = KEYSTORE_FLAG_ENCRYPTED;
53 } else {
54 mBlob.flags = KEYSTORE_FLAG_NONE;
55 }
56}
57
58Blob::Blob(blob b) {
59 mBlob = b;
60}
61
62Blob::Blob() {
63 memset(&mBlob, 0, sizeof(mBlob));
64}
65
66bool Blob::isEncrypted() const {
67 if (mBlob.version < 2) {
68 return true;
69 }
70
71 return mBlob.flags & KEYSTORE_FLAG_ENCRYPTED;
72}
73
Shawn Willdend5a24e62017-02-28 13:53:24 -070074bool Blob::isSuperEncrypted() const {
75 return mBlob.flags & KEYSTORE_FLAG_SUPER_ENCRYPTED;
76}
77
78inline uint8_t setFlag(uint8_t flags, bool set, KeyStoreFlag flag) {
79 return set ? (flags | flag) : (flags & ~flag);
80}
81
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070082void Blob::setEncrypted(bool encrypted) {
Shawn Willdend5a24e62017-02-28 13:53:24 -070083 mBlob.flags = setFlag(mBlob.flags, encrypted, KEYSTORE_FLAG_ENCRYPTED);
84}
85
86void Blob::setSuperEncrypted(bool superEncrypted) {
87 mBlob.flags = setFlag(mBlob.flags, superEncrypted, KEYSTORE_FLAG_SUPER_ENCRYPTED);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070088}
89
90void Blob::setFallback(bool fallback) {
91 if (fallback) {
92 mBlob.flags |= KEYSTORE_FLAG_FALLBACK;
93 } else {
94 mBlob.flags &= ~KEYSTORE_FLAG_FALLBACK;
95 }
96}
97
98ResponseCode Blob::writeBlob(const char* filename, AES_KEY* aes_key, State state,
99 Entropy* entropy) {
100 ALOGV("writing blob %s", filename);
Shawn Willdend5a24e62017-02-28 13:53:24 -0700101 if (isEncrypted() || isSuperEncrypted()) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700102 if (state != STATE_NO_ERROR) {
103 ALOGD("couldn't insert encrypted blob while not unlocked");
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100104 return ResponseCode::LOCKED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700105 }
106
107 if (!entropy->generate_random_data(mBlob.vector, AES_BLOCK_SIZE)) {
108 ALOGW("Could not read random data for: %s", filename);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100109 return ResponseCode::SYSTEM_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700110 }
111 }
112
113 // data includes the value and the value's length
114 size_t dataLength = mBlob.length + sizeof(mBlob.length);
115 // pad data to the AES_BLOCK_SIZE
116 size_t digestedLength = ((dataLength + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE * AES_BLOCK_SIZE);
117 // encrypted data includes the digest value
118 size_t encryptedLength = digestedLength + MD5_DIGEST_LENGTH;
119 // move info after space for padding
120 memmove(&mBlob.encrypted[encryptedLength], &mBlob.value[mBlob.length], mBlob.info);
121 // zero padding area
122 memset(mBlob.value + mBlob.length, 0, digestedLength - dataLength);
123
124 mBlob.length = htonl(mBlob.length);
125
Shawn Willdend5a24e62017-02-28 13:53:24 -0700126 if (isEncrypted() || isSuperEncrypted()) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700127 MD5(mBlob.digested, digestedLength, mBlob.digest);
128
129 uint8_t vector[AES_BLOCK_SIZE];
130 memcpy(vector, mBlob.vector, AES_BLOCK_SIZE);
131 AES_cbc_encrypt(mBlob.encrypted, mBlob.encrypted, encryptedLength, aes_key, vector,
132 AES_ENCRYPT);
133 }
134
135 size_t headerLength = (mBlob.encrypted - (uint8_t*)&mBlob);
136 size_t fileLength = encryptedLength + headerLength + mBlob.info;
137
138 const char* tmpFileName = ".tmp";
139 int out =
140 TEMP_FAILURE_RETRY(open(tmpFileName, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR));
141 if (out < 0) {
142 ALOGW("could not open file: %s: %s", tmpFileName, strerror(errno));
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100143 return ResponseCode::SYSTEM_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700144 }
145 size_t writtenBytes = writeFully(out, (uint8_t*)&mBlob, fileLength);
146 if (close(out) != 0) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100147 return ResponseCode::SYSTEM_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700148 }
149 if (writtenBytes != fileLength) {
150 ALOGW("blob not fully written %zu != %zu", writtenBytes, fileLength);
151 unlink(tmpFileName);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100152 return ResponseCode::SYSTEM_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700153 }
154 if (rename(tmpFileName, filename) == -1) {
155 ALOGW("could not rename blob to %s: %s", filename, strerror(errno));
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100156 return ResponseCode::SYSTEM_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700157 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100158 return ResponseCode::NO_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700159}
160
161ResponseCode Blob::readBlob(const char* filename, AES_KEY* aes_key, State state) {
162 ALOGV("reading blob %s", filename);
163 int in = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
164 if (in < 0) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100165 return (errno == ENOENT) ? ResponseCode::KEY_NOT_FOUND : ResponseCode::SYSTEM_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700166 }
167 // fileLength may be less than sizeof(mBlob) since the in
168 // memory version has extra padding to tolerate rounding up to
169 // the AES_BLOCK_SIZE
170 size_t fileLength = readFully(in, (uint8_t*)&mBlob, sizeof(mBlob));
171 if (close(in) != 0) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100172 return ResponseCode::SYSTEM_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700173 }
174
175 if (fileLength == 0) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100176 return ResponseCode::VALUE_CORRUPTED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700177 }
178
Shawn Willdend5a24e62017-02-28 13:53:24 -0700179 if ((isEncrypted() || isSuperEncrypted()) && (state != STATE_NO_ERROR)) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100180 return ResponseCode::LOCKED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700181 }
182
183 size_t headerLength = (mBlob.encrypted - (uint8_t*)&mBlob);
184 if (fileLength < headerLength) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100185 return ResponseCode::VALUE_CORRUPTED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700186 }
187
188 ssize_t encryptedLength = fileLength - (headerLength + mBlob.info);
189 if (encryptedLength < 0) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100190 return ResponseCode::VALUE_CORRUPTED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700191 }
192
193 ssize_t digestedLength;
Shawn Willden45c112a2017-04-17 09:02:42 -0600194 if (isEncrypted() || isSuperEncrypted()) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700195 if (encryptedLength % AES_BLOCK_SIZE != 0) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100196 return ResponseCode::VALUE_CORRUPTED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700197 }
198
199 AES_cbc_encrypt(mBlob.encrypted, mBlob.encrypted, encryptedLength, aes_key, mBlob.vector,
200 AES_DECRYPT);
201 digestedLength = encryptedLength - MD5_DIGEST_LENGTH;
202 uint8_t computedDigest[MD5_DIGEST_LENGTH];
203 MD5(mBlob.digested, digestedLength, computedDigest);
204 if (memcmp(mBlob.digest, computedDigest, MD5_DIGEST_LENGTH) != 0) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100205 return ResponseCode::VALUE_CORRUPTED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700206 }
207 } else {
208 digestedLength = encryptedLength;
209 }
210
211 ssize_t maxValueLength = digestedLength - sizeof(mBlob.length);
212 mBlob.length = ntohl(mBlob.length);
213 if (mBlob.length < 0 || mBlob.length > maxValueLength) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100214 return ResponseCode::VALUE_CORRUPTED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700215 }
216 if (mBlob.info != 0) {
217 // move info from after padding to after data
218 memmove(&mBlob.value[mBlob.length], &mBlob.value[maxValueLength], mBlob.info);
219 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100220 return ResponseCode::NO_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700221}