Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "keystore" |
| 19 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <signal.h> |
| 25 | #include <errno.h> |
| 26 | #include <dirent.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <limits.h> |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 29 | #include <assert.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
| 36 | #include <openssl/aes.h> |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 37 | #include <openssl/bio.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 38 | #include <openssl/evp.h> |
| 39 | #include <openssl/md5.h> |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 40 | #include <openssl/pem.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 41 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 42 | #include <hardware/keymaster.h> |
| 43 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 44 | #include <utils/UniquePtr.h> |
| 45 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 46 | #include <cutils/list.h> |
| 47 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 48 | #include <keystore/IKeystoreService.h> |
| 49 | #include <binder/IPCThreadState.h> |
| 50 | #include <binder/IServiceManager.h> |
| 51 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 52 | #include <cutils/log.h> |
| 53 | #include <cutils/sockets.h> |
| 54 | #include <private/android_filesystem_config.h> |
| 55 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 56 | #include <keystore/keystore.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 57 | |
| 58 | /* KeyStore is a secured storage for key-value pairs. In this implementation, |
| 59 | * each file stores one key-value pair. Keys are encoded in file names, and |
| 60 | * values are encrypted with checksums. The encryption key is protected by a |
| 61 | * user-defined password. To keep things simple, buffers are always larger than |
| 62 | * the maximum space we needed, so boundary checks on buffers are omitted. */ |
| 63 | |
| 64 | #define KEY_SIZE ((NAME_MAX - 15) / 2) |
| 65 | #define VALUE_SIZE 32768 |
| 66 | #define PASSWORD_SIZE VALUE_SIZE |
| 67 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 68 | |
| 69 | struct BIO_Delete { |
| 70 | void operator()(BIO* p) const { |
| 71 | BIO_free(p); |
| 72 | } |
| 73 | }; |
| 74 | typedef UniquePtr<BIO, BIO_Delete> Unique_BIO; |
| 75 | |
| 76 | struct EVP_PKEY_Delete { |
| 77 | void operator()(EVP_PKEY* p) const { |
| 78 | EVP_PKEY_free(p); |
| 79 | } |
| 80 | }; |
| 81 | typedef UniquePtr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY; |
| 82 | |
| 83 | struct PKCS8_PRIV_KEY_INFO_Delete { |
| 84 | void operator()(PKCS8_PRIV_KEY_INFO* p) const { |
| 85 | PKCS8_PRIV_KEY_INFO_free(p); |
| 86 | } |
| 87 | }; |
| 88 | typedef UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO; |
| 89 | |
| 90 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 91 | struct Value { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 92 | Value(const uint8_t* orig, int origLen) { |
| 93 | assert(origLen <= VALUE_SIZE); |
| 94 | memcpy(value, orig, origLen); |
| 95 | length = origLen; |
| 96 | } |
| 97 | |
| 98 | Value() { |
| 99 | } |
| 100 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 101 | int length; |
| 102 | uint8_t value[VALUE_SIZE]; |
| 103 | }; |
| 104 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 105 | class ValueString { |
| 106 | public: |
| 107 | ValueString(const Value* orig) { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 108 | assert(length <= VALUE_SIZE); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 109 | length = orig->length; |
| 110 | value = new char[length + 1]; |
| 111 | memcpy(value, orig->value, length); |
| 112 | value[length] = '\0'; |
| 113 | } |
| 114 | |
| 115 | ~ValueString() { |
| 116 | delete[] value; |
| 117 | } |
| 118 | |
| 119 | const char* c_str() const { |
| 120 | return value; |
| 121 | } |
| 122 | |
| 123 | char* release() { |
| 124 | char* ret = value; |
| 125 | value = NULL; |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | char* value; |
| 131 | size_t length; |
| 132 | }; |
| 133 | |
| 134 | static int keymaster_device_initialize(keymaster_device_t** dev) { |
| 135 | int rc; |
| 136 | |
| 137 | const hw_module_t* mod; |
| 138 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 139 | if (rc) { |
| 140 | ALOGE("could not find any keystore module"); |
| 141 | goto out; |
| 142 | } |
| 143 | |
| 144 | rc = keymaster_open(mod, dev); |
| 145 | if (rc) { |
| 146 | ALOGE("could not open keymaster device in %s (%s)", |
| 147 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | |
| 153 | out: |
| 154 | *dev = NULL; |
| 155 | return rc; |
| 156 | } |
| 157 | |
| 158 | static void keymaster_device_release(keymaster_device_t* dev) { |
| 159 | keymaster_close(dev); |
| 160 | } |
| 161 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 162 | /*************** |
| 163 | * PERMISSIONS * |
| 164 | ***************/ |
| 165 | |
| 166 | /* Here are the permissions, actions, users, and the main function. */ |
| 167 | typedef enum { |
| 168 | P_TEST = 1 << 0, |
| 169 | P_GET = 1 << 1, |
| 170 | P_INSERT = 1 << 2, |
| 171 | P_DELETE = 1 << 3, |
| 172 | P_EXIST = 1 << 4, |
| 173 | P_SAW = 1 << 5, |
| 174 | P_RESET = 1 << 6, |
| 175 | P_PASSWORD = 1 << 7, |
| 176 | P_LOCK = 1 << 8, |
| 177 | P_UNLOCK = 1 << 9, |
| 178 | P_ZERO = 1 << 10, |
| 179 | P_SIGN = 1 << 11, |
| 180 | P_VERIFY = 1 << 12, |
| 181 | P_GRANT = 1 << 13, |
| 182 | } perm_t; |
| 183 | |
| 184 | static struct user_euid { |
| 185 | uid_t uid; |
| 186 | uid_t euid; |
| 187 | } user_euids[] = { |
| 188 | {AID_VPN, AID_SYSTEM}, |
| 189 | {AID_WIFI, AID_SYSTEM}, |
| 190 | {AID_ROOT, AID_SYSTEM}, |
| 191 | }; |
| 192 | |
| 193 | static struct user_perm { |
| 194 | uid_t uid; |
| 195 | perm_t perms; |
| 196 | } user_perms[] = { |
| 197 | {AID_SYSTEM, static_cast<perm_t>((uint32_t)(~0)) }, |
| 198 | {AID_VPN, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY) }, |
| 199 | {AID_WIFI, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY) }, |
| 200 | {AID_ROOT, static_cast<perm_t>(P_GET) }, |
| 201 | }; |
| 202 | |
| 203 | static const perm_t DEFAULT_PERMS = static_cast<perm_t>(P_TEST | P_GET | P_INSERT | P_DELETE | P_EXIST | P_SAW | P_SIGN |
| 204 | | P_VERIFY); |
| 205 | |
| 206 | static bool has_permission(uid_t uid, perm_t perm) { |
| 207 | for (size_t i = 0; i < sizeof(user_perms)/sizeof(user_perms[0]); i++) { |
| 208 | struct user_perm user = user_perms[i]; |
| 209 | if (user.uid == uid) { |
| 210 | return user.perms & perm; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return DEFAULT_PERMS & perm; |
| 215 | } |
| 216 | |
| 217 | static uid_t get_keystore_euid(uid_t uid) { |
| 218 | for (size_t i = 0; i < sizeof(user_euids)/sizeof(user_euids[0]); i++) { |
| 219 | struct user_euid user = user_euids[i]; |
| 220 | if (user.uid == uid) { |
| 221 | return user.euid; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return uid; |
| 226 | } |
| 227 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 228 | /* Here is the encoding of keys. This is necessary in order to allow arbitrary |
| 229 | * characters in keys. Characters in [0-~] are not encoded. Others are encoded |
| 230 | * into two bytes. The first byte is one of [+-.] which represents the first |
| 231 | * two bits of the character. The second byte encodes the rest of the bits into |
| 232 | * [0-o]. Therefore in the worst case the length of a key gets doubled. Note |
| 233 | * that Base64 cannot be used here due to the need of prefix match on keys. */ |
| 234 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 235 | static int encode_key(char* out, const android::String8& keyName) { |
| 236 | const uint8_t* in = reinterpret_cast<const uint8_t*>(keyName.string()); |
| 237 | size_t length = keyName.length(); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 238 | for (int i = length; i > 0; --i, ++in, ++out) { |
| 239 | if (*in >= '0' && *in <= '~') { |
| 240 | *out = *in; |
| 241 | } else { |
| 242 | *out = '+' + (*in >> 6); |
| 243 | *++out = '0' + (*in & 0x3F); |
| 244 | ++length; |
| 245 | } |
| 246 | } |
| 247 | *out = '\0'; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 248 | return length; |
| 249 | } |
| 250 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 251 | static int encode_key_for_uid(char* out, uid_t uid, const android::String8& keyName) { |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 252 | int n = snprintf(out, NAME_MAX, "%u_", uid); |
| 253 | out += n; |
| 254 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 255 | return n + encode_key(out, keyName); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 258 | /* |
| 259 | * Converts from the "escaped" format on disk to actual name. |
| 260 | * This will be smaller than the input string. |
| 261 | * |
| 262 | * Characters that should combine with the next at the end will be truncated. |
| 263 | */ |
| 264 | static size_t decode_key_length(const char* in, size_t length) { |
| 265 | size_t outLength = 0; |
| 266 | |
| 267 | for (const char* end = in + length; in < end; in++) { |
| 268 | /* This combines with the next character. */ |
| 269 | if (*in < '0' || *in > '~') { |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | outLength++; |
| 274 | } |
| 275 | return outLength; |
| 276 | } |
| 277 | |
| 278 | static void decode_key(char* out, const char* in, size_t length) { |
| 279 | for (const char* end = in + length; in < end; in++) { |
| 280 | if (*in < '0' || *in > '~') { |
| 281 | /* Truncate combining characters at the end. */ |
| 282 | if (in + 1 >= end) { |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | *out = (*in++ - '+') << 6; |
| 287 | *out++ |= (*in - '0') & 0x3F; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 288 | } else { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 289 | *out++ = *in; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | *out = '\0'; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static size_t readFully(int fd, uint8_t* data, size_t size) { |
| 296 | size_t remaining = size; |
| 297 | while (remaining > 0) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 298 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining)); |
Kenny Root | 5281edb | 2012-11-21 15:14:04 -0800 | [diff] [blame] | 299 | if (n <= 0) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 300 | return size - remaining; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 301 | } |
| 302 | data += n; |
| 303 | remaining -= n; |
| 304 | } |
| 305 | return size; |
| 306 | } |
| 307 | |
| 308 | static size_t writeFully(int fd, uint8_t* data, size_t size) { |
| 309 | size_t remaining = size; |
| 310 | while (remaining > 0) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 311 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining)); |
| 312 | if (n < 0) { |
| 313 | ALOGW("write failed: %s", strerror(errno)); |
| 314 | return size - remaining; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 315 | } |
| 316 | data += n; |
| 317 | remaining -= n; |
| 318 | } |
| 319 | return size; |
| 320 | } |
| 321 | |
| 322 | class Entropy { |
| 323 | public: |
| 324 | Entropy() : mRandom(-1) {} |
| 325 | ~Entropy() { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 326 | if (mRandom >= 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 327 | close(mRandom); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | bool open() { |
| 332 | const char* randomDevice = "/dev/urandom"; |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 333 | mRandom = TEMP_FAILURE_RETRY(::open(randomDevice, O_RDONLY)); |
| 334 | if (mRandom < 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 335 | ALOGE("open: %s: %s", randomDevice, strerror(errno)); |
| 336 | return false; |
| 337 | } |
| 338 | return true; |
| 339 | } |
| 340 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 341 | bool generate_random_data(uint8_t* data, size_t size) const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 342 | return (readFully(mRandom, data, size) == size); |
| 343 | } |
| 344 | |
| 345 | private: |
| 346 | int mRandom; |
| 347 | }; |
| 348 | |
| 349 | /* Here is the file format. There are two parts in blob.value, the secret and |
| 350 | * the description. The secret is stored in ciphertext, and its original size |
| 351 | * can be found in blob.length. The description is stored after the secret in |
| 352 | * plaintext, and its size is specified in blob.info. The total size of the two |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 353 | * parts must be no more than VALUE_SIZE bytes. The first field is the version, |
| 354 | * the second is the blob's type, and the third byte is reserved. Fields other |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 355 | * than blob.info, blob.length, and blob.value are modified by encryptBlob() |
| 356 | * and decryptBlob(). Thus they should not be accessed from outside. */ |
| 357 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 358 | /* ** Note to future implementors of encryption: ** |
| 359 | * Currently this is the construction: |
| 360 | * metadata || Enc(MD5(data) || data) |
| 361 | * |
| 362 | * This should be the construction used for encrypting if re-implementing: |
| 363 | * |
| 364 | * Derive independent keys for encryption and MAC: |
| 365 | * Kenc = AES_encrypt(masterKey, "Encrypt") |
| 366 | * Kmac = AES_encrypt(masterKey, "MAC") |
| 367 | * |
| 368 | * Store this: |
| 369 | * metadata || AES_CTR_encrypt(Kenc, rand_IV, data) || |
| 370 | * HMAC(Kmac, metadata || Enc(data)) |
| 371 | */ |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 372 | struct __attribute__((packed)) blob { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 373 | uint8_t version; |
| 374 | uint8_t type; |
| 375 | uint8_t reserved; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 376 | uint8_t info; |
| 377 | uint8_t vector[AES_BLOCK_SIZE]; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 378 | uint8_t encrypted[0]; // Marks offset to encrypted data. |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 379 | uint8_t digest[MD5_DIGEST_LENGTH]; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 380 | uint8_t digested[0]; // Marks offset to digested data. |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 381 | int32_t length; // in network byte order when encrypted |
| 382 | uint8_t value[VALUE_SIZE + AES_BLOCK_SIZE]; |
| 383 | }; |
| 384 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 385 | typedef enum { |
| 386 | TYPE_GENERIC = 1, |
| 387 | TYPE_MASTER_KEY = 2, |
| 388 | TYPE_KEY_PAIR = 3, |
| 389 | } BlobType; |
| 390 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 391 | static const uint8_t CURRENT_BLOB_VERSION = 1; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 392 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 393 | class Blob { |
| 394 | public: |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 395 | Blob(const uint8_t* value, int32_t valueLength, const uint8_t* info, uint8_t infoLength, |
| 396 | BlobType type) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 397 | mBlob.length = valueLength; |
| 398 | memcpy(mBlob.value, value, valueLength); |
| 399 | |
| 400 | mBlob.info = infoLength; |
| 401 | memcpy(mBlob.value + valueLength, info, infoLength); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 402 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 403 | mBlob.version = CURRENT_BLOB_VERSION; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 404 | mBlob.type = uint8_t(type); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | Blob(blob b) { |
| 408 | mBlob = b; |
| 409 | } |
| 410 | |
| 411 | Blob() {} |
| 412 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 413 | const uint8_t* getValue() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 414 | return mBlob.value; |
| 415 | } |
| 416 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 417 | int32_t getLength() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 418 | return mBlob.length; |
| 419 | } |
| 420 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 421 | const uint8_t* getInfo() const { |
| 422 | return mBlob.value + mBlob.length; |
| 423 | } |
| 424 | |
| 425 | uint8_t getInfoLength() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 426 | return mBlob.info; |
| 427 | } |
| 428 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 429 | uint8_t getVersion() const { |
| 430 | return mBlob.version; |
| 431 | } |
| 432 | |
| 433 | void setVersion(uint8_t version) { |
| 434 | mBlob.version = version; |
| 435 | } |
| 436 | |
| 437 | BlobType getType() const { |
| 438 | return BlobType(mBlob.type); |
| 439 | } |
| 440 | |
| 441 | void setType(BlobType type) { |
| 442 | mBlob.type = uint8_t(type); |
| 443 | } |
| 444 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 445 | ResponseCode encryptBlob(const char* filename, AES_KEY *aes_key, Entropy* entropy) { |
| 446 | if (!entropy->generate_random_data(mBlob.vector, AES_BLOCK_SIZE)) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 447 | ALOGW("Could not read random data for: %s", filename); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 448 | return SYSTEM_ERROR; |
| 449 | } |
| 450 | |
| 451 | // data includes the value and the value's length |
| 452 | size_t dataLength = mBlob.length + sizeof(mBlob.length); |
| 453 | // pad data to the AES_BLOCK_SIZE |
| 454 | size_t digestedLength = ((dataLength + AES_BLOCK_SIZE - 1) |
| 455 | / AES_BLOCK_SIZE * AES_BLOCK_SIZE); |
| 456 | // encrypted data includes the digest value |
| 457 | size_t encryptedLength = digestedLength + MD5_DIGEST_LENGTH; |
| 458 | // move info after space for padding |
| 459 | memmove(&mBlob.encrypted[encryptedLength], &mBlob.value[mBlob.length], mBlob.info); |
| 460 | // zero padding area |
| 461 | memset(mBlob.value + mBlob.length, 0, digestedLength - dataLength); |
| 462 | |
| 463 | mBlob.length = htonl(mBlob.length); |
| 464 | MD5(mBlob.digested, digestedLength, mBlob.digest); |
| 465 | |
| 466 | uint8_t vector[AES_BLOCK_SIZE]; |
| 467 | memcpy(vector, mBlob.vector, AES_BLOCK_SIZE); |
| 468 | AES_cbc_encrypt(mBlob.encrypted, mBlob.encrypted, encryptedLength, |
| 469 | aes_key, vector, AES_ENCRYPT); |
| 470 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 471 | mBlob.reserved = 0; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 472 | size_t headerLength = (mBlob.encrypted - (uint8_t*) &mBlob); |
| 473 | size_t fileLength = encryptedLength + headerLength + mBlob.info; |
| 474 | |
| 475 | const char* tmpFileName = ".tmp"; |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 476 | int out = TEMP_FAILURE_RETRY(open(tmpFileName, |
| 477 | O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)); |
| 478 | if (out < 0) { |
| 479 | ALOGW("could not open file: %s: %s", tmpFileName, strerror(errno)); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 480 | return SYSTEM_ERROR; |
| 481 | } |
| 482 | size_t writtenBytes = writeFully(out, (uint8_t*) &mBlob, fileLength); |
| 483 | if (close(out) != 0) { |
| 484 | return SYSTEM_ERROR; |
| 485 | } |
| 486 | if (writtenBytes != fileLength) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 487 | ALOGW("blob not fully written %zu != %zu", writtenBytes, fileLength); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 488 | unlink(tmpFileName); |
| 489 | return SYSTEM_ERROR; |
| 490 | } |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 491 | if (rename(tmpFileName, filename) == -1) { |
| 492 | ALOGW("could not rename blob to %s: %s", filename, strerror(errno)); |
| 493 | return SYSTEM_ERROR; |
| 494 | } |
| 495 | return NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | ResponseCode decryptBlob(const char* filename, AES_KEY *aes_key) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 499 | int in = TEMP_FAILURE_RETRY(open(filename, O_RDONLY)); |
| 500 | if (in < 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 501 | return (errno == ENOENT) ? KEY_NOT_FOUND : SYSTEM_ERROR; |
| 502 | } |
| 503 | // fileLength may be less than sizeof(mBlob) since the in |
| 504 | // memory version has extra padding to tolerate rounding up to |
| 505 | // the AES_BLOCK_SIZE |
| 506 | size_t fileLength = readFully(in, (uint8_t*) &mBlob, sizeof(mBlob)); |
| 507 | if (close(in) != 0) { |
| 508 | return SYSTEM_ERROR; |
| 509 | } |
| 510 | size_t headerLength = (mBlob.encrypted - (uint8_t*) &mBlob); |
| 511 | if (fileLength < headerLength) { |
| 512 | return VALUE_CORRUPTED; |
| 513 | } |
| 514 | |
| 515 | ssize_t encryptedLength = fileLength - (headerLength + mBlob.info); |
| 516 | if (encryptedLength < 0 || encryptedLength % AES_BLOCK_SIZE != 0) { |
| 517 | return VALUE_CORRUPTED; |
| 518 | } |
| 519 | AES_cbc_encrypt(mBlob.encrypted, mBlob.encrypted, encryptedLength, aes_key, |
| 520 | mBlob.vector, AES_DECRYPT); |
| 521 | size_t digestedLength = encryptedLength - MD5_DIGEST_LENGTH; |
| 522 | uint8_t computedDigest[MD5_DIGEST_LENGTH]; |
| 523 | MD5(mBlob.digested, digestedLength, computedDigest); |
| 524 | if (memcmp(mBlob.digest, computedDigest, MD5_DIGEST_LENGTH) != 0) { |
| 525 | return VALUE_CORRUPTED; |
| 526 | } |
| 527 | |
| 528 | ssize_t maxValueLength = digestedLength - sizeof(mBlob.length); |
| 529 | mBlob.length = ntohl(mBlob.length); |
| 530 | if (mBlob.length < 0 || mBlob.length > maxValueLength) { |
| 531 | return VALUE_CORRUPTED; |
| 532 | } |
| 533 | if (mBlob.info != 0) { |
| 534 | // move info from after padding to after data |
| 535 | memmove(&mBlob.value[mBlob.length], &mBlob.value[maxValueLength], mBlob.info); |
| 536 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 537 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | private: |
| 541 | struct blob mBlob; |
| 542 | }; |
| 543 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 544 | typedef struct { |
| 545 | uint32_t uid; |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 546 | const uint8_t* filename; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 547 | |
| 548 | struct listnode plist; |
| 549 | } grant_t; |
| 550 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 551 | class KeyStore { |
| 552 | public: |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 553 | KeyStore(Entropy* entropy, keymaster_device_t* device) |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 554 | : mEntropy(entropy) |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 555 | , mDevice(device) |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 556 | , mRetry(MAX_RETRY) |
| 557 | { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 558 | if (access(MASTER_KEY_FILE, R_OK) == 0) { |
| 559 | setState(STATE_LOCKED); |
| 560 | } else { |
| 561 | setState(STATE_UNINITIALIZED); |
| 562 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 563 | |
| 564 | list_init(&mGrants); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 567 | State getState() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 568 | return mState; |
| 569 | } |
| 570 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 571 | int8_t getRetry() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 572 | return mRetry; |
| 573 | } |
| 574 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 575 | keymaster_device_t* getDevice() const { |
| 576 | return mDevice; |
| 577 | } |
| 578 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 579 | ResponseCode initialize(const android::String8& pw) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 580 | if (!generateMasterKey()) { |
| 581 | return SYSTEM_ERROR; |
| 582 | } |
| 583 | ResponseCode response = writeMasterKey(pw); |
| 584 | if (response != NO_ERROR) { |
| 585 | return response; |
| 586 | } |
| 587 | setupMasterKeys(); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 588 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 591 | ResponseCode writeMasterKey(const android::String8& pw) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 592 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 593 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, mSalt); |
| 594 | AES_KEY passwordAesKey; |
| 595 | AES_set_encrypt_key(passwordKey, MASTER_KEY_SIZE_BITS, &passwordAesKey); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 596 | Blob masterKeyBlob(mMasterKey, sizeof(mMasterKey), mSalt, sizeof(mSalt), TYPE_MASTER_KEY); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 597 | return masterKeyBlob.encryptBlob(MASTER_KEY_FILE, &passwordAesKey, mEntropy); |
| 598 | } |
| 599 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 600 | ResponseCode readMasterKey(const android::String8& pw) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 601 | int in = TEMP_FAILURE_RETRY(open(MASTER_KEY_FILE, O_RDONLY)); |
| 602 | if (in < 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 603 | return SYSTEM_ERROR; |
| 604 | } |
| 605 | |
| 606 | // we read the raw blob to just to get the salt to generate |
| 607 | // the AES key, then we create the Blob to use with decryptBlob |
| 608 | blob rawBlob; |
| 609 | size_t length = readFully(in, (uint8_t*) &rawBlob, sizeof(rawBlob)); |
| 610 | if (close(in) != 0) { |
| 611 | return SYSTEM_ERROR; |
| 612 | } |
| 613 | // find salt at EOF if present, otherwise we have an old file |
| 614 | uint8_t* salt; |
| 615 | if (length > SALT_SIZE && rawBlob.info == SALT_SIZE) { |
| 616 | salt = (uint8_t*) &rawBlob + length - SALT_SIZE; |
| 617 | } else { |
| 618 | salt = NULL; |
| 619 | } |
| 620 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 621 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, salt); |
| 622 | AES_KEY passwordAesKey; |
| 623 | AES_set_decrypt_key(passwordKey, MASTER_KEY_SIZE_BITS, &passwordAesKey); |
| 624 | Blob masterKeyBlob(rawBlob); |
| 625 | ResponseCode response = masterKeyBlob.decryptBlob(MASTER_KEY_FILE, &passwordAesKey); |
| 626 | if (response == SYSTEM_ERROR) { |
| 627 | return SYSTEM_ERROR; |
| 628 | } |
| 629 | if (response == NO_ERROR && masterKeyBlob.getLength() == MASTER_KEY_SIZE_BYTES) { |
| 630 | // if salt was missing, generate one and write a new master key file with the salt. |
| 631 | if (salt == NULL) { |
| 632 | if (!generateSalt()) { |
| 633 | return SYSTEM_ERROR; |
| 634 | } |
| 635 | response = writeMasterKey(pw); |
| 636 | } |
| 637 | if (response == NO_ERROR) { |
| 638 | memcpy(mMasterKey, masterKeyBlob.getValue(), MASTER_KEY_SIZE_BYTES); |
| 639 | setupMasterKeys(); |
| 640 | } |
| 641 | return response; |
| 642 | } |
| 643 | if (mRetry <= 0) { |
| 644 | reset(); |
| 645 | return UNINITIALIZED; |
| 646 | } |
| 647 | --mRetry; |
| 648 | switch (mRetry) { |
| 649 | case 0: return WRONG_PASSWORD_0; |
| 650 | case 1: return WRONG_PASSWORD_1; |
| 651 | case 2: return WRONG_PASSWORD_2; |
| 652 | case 3: return WRONG_PASSWORD_3; |
| 653 | default: return WRONG_PASSWORD_3; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | bool reset() { |
| 658 | clearMasterKeys(); |
| 659 | setState(STATE_UNINITIALIZED); |
| 660 | |
| 661 | DIR* dir = opendir("."); |
| 662 | struct dirent* file; |
| 663 | |
| 664 | if (!dir) { |
| 665 | return false; |
| 666 | } |
| 667 | while ((file = readdir(dir)) != NULL) { |
| 668 | unlink(file->d_name); |
| 669 | } |
| 670 | closedir(dir); |
| 671 | return true; |
| 672 | } |
| 673 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 674 | bool isEmpty() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 675 | DIR* dir = opendir("."); |
| 676 | struct dirent* file; |
| 677 | if (!dir) { |
| 678 | return true; |
| 679 | } |
| 680 | bool result = true; |
| 681 | while ((file = readdir(dir)) != NULL) { |
| 682 | if (isKeyFile(file->d_name)) { |
| 683 | result = false; |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | closedir(dir); |
| 688 | return result; |
| 689 | } |
| 690 | |
| 691 | void lock() { |
| 692 | clearMasterKeys(); |
| 693 | setState(STATE_LOCKED); |
| 694 | } |
| 695 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 696 | ResponseCode get(const char* filename, Blob* keyBlob, const BlobType type) { |
| 697 | ResponseCode rc = keyBlob->decryptBlob(filename, &mMasterKeyDecryption); |
| 698 | if (rc != NO_ERROR) { |
| 699 | return rc; |
| 700 | } |
| 701 | |
| 702 | const uint8_t version = keyBlob->getVersion(); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 703 | if (version < CURRENT_BLOB_VERSION) { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 704 | upgrade(filename, keyBlob, version, type); |
| 705 | } |
| 706 | |
| 707 | if (keyBlob->getType() != type) { |
| 708 | ALOGW("key found but type doesn't match: %d vs %d", keyBlob->getType(), type); |
| 709 | return KEY_NOT_FOUND; |
| 710 | } |
| 711 | |
| 712 | return rc; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | ResponseCode put(const char* filename, Blob* keyBlob) { |
| 716 | return keyBlob->encryptBlob(filename, &mMasterKeyEncryption, mEntropy); |
| 717 | } |
| 718 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 719 | void addGrant(const char* filename, uid_t granteeUid) { |
| 720 | grant_t *grant = getGrant(filename, granteeUid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 721 | if (grant == NULL) { |
| 722 | grant = new grant_t; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 723 | grant->uid = granteeUid; |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 724 | grant->filename = reinterpret_cast<const uint8_t*>(strdup(filename)); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 725 | list_add_tail(&mGrants, &grant->plist); |
| 726 | } |
| 727 | } |
| 728 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 729 | bool removeGrant(const char* filename, uid_t granteeUid) { |
| 730 | grant_t *grant = getGrant(filename, granteeUid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 731 | if (grant != NULL) { |
| 732 | list_remove(&grant->plist); |
| 733 | delete grant; |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | return false; |
| 738 | } |
| 739 | |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 740 | bool hasGrant(const char* filename, const uid_t uid) const { |
| 741 | return getGrant(filename, uid) != NULL; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 744 | ResponseCode importKey(const uint8_t* key, size_t keyLen, const char* filename) { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 745 | uint8_t* data; |
| 746 | size_t dataLength; |
| 747 | int rc; |
| 748 | |
| 749 | if (mDevice->import_keypair == NULL) { |
| 750 | ALOGE("Keymaster doesn't support import!"); |
| 751 | return SYSTEM_ERROR; |
| 752 | } |
| 753 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 754 | rc = mDevice->import_keypair(mDevice, key, keyLen, &data, &dataLength); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 755 | if (rc) { |
| 756 | ALOGE("Error while importing keypair: %d", rc); |
| 757 | return SYSTEM_ERROR; |
| 758 | } |
| 759 | |
| 760 | Blob keyBlob(data, dataLength, NULL, 0, TYPE_KEY_PAIR); |
| 761 | free(data); |
| 762 | |
| 763 | return put(filename, &keyBlob); |
| 764 | } |
| 765 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 766 | private: |
| 767 | static const char* MASTER_KEY_FILE; |
| 768 | static const int MASTER_KEY_SIZE_BYTES = 16; |
| 769 | static const int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8; |
| 770 | |
| 771 | static const int MAX_RETRY = 4; |
| 772 | static const size_t SALT_SIZE = 16; |
| 773 | |
| 774 | Entropy* mEntropy; |
| 775 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 776 | keymaster_device_t* mDevice; |
| 777 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 778 | State mState; |
| 779 | int8_t mRetry; |
| 780 | |
| 781 | uint8_t mMasterKey[MASTER_KEY_SIZE_BYTES]; |
| 782 | uint8_t mSalt[SALT_SIZE]; |
| 783 | |
| 784 | AES_KEY mMasterKeyEncryption; |
| 785 | AES_KEY mMasterKeyDecryption; |
| 786 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 787 | struct listnode mGrants; |
| 788 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 789 | void setState(State state) { |
| 790 | mState = state; |
| 791 | if (mState == STATE_NO_ERROR || mState == STATE_UNINITIALIZED) { |
| 792 | mRetry = MAX_RETRY; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | bool generateSalt() { |
| 797 | return mEntropy->generate_random_data(mSalt, sizeof(mSalt)); |
| 798 | } |
| 799 | |
| 800 | bool generateMasterKey() { |
| 801 | if (!mEntropy->generate_random_data(mMasterKey, sizeof(mMasterKey))) { |
| 802 | return false; |
| 803 | } |
| 804 | if (!generateSalt()) { |
| 805 | return false; |
| 806 | } |
| 807 | return true; |
| 808 | } |
| 809 | |
| 810 | void setupMasterKeys() { |
| 811 | AES_set_encrypt_key(mMasterKey, MASTER_KEY_SIZE_BITS, &mMasterKeyEncryption); |
| 812 | AES_set_decrypt_key(mMasterKey, MASTER_KEY_SIZE_BITS, &mMasterKeyDecryption); |
| 813 | setState(STATE_NO_ERROR); |
| 814 | } |
| 815 | |
| 816 | void clearMasterKeys() { |
| 817 | memset(mMasterKey, 0, sizeof(mMasterKey)); |
| 818 | memset(mSalt, 0, sizeof(mSalt)); |
| 819 | memset(&mMasterKeyEncryption, 0, sizeof(mMasterKeyEncryption)); |
| 820 | memset(&mMasterKeyDecryption, 0, sizeof(mMasterKeyDecryption)); |
| 821 | } |
| 822 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 823 | static void generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw, |
| 824 | uint8_t* salt) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 825 | size_t saltSize; |
| 826 | if (salt != NULL) { |
| 827 | saltSize = SALT_SIZE; |
| 828 | } else { |
| 829 | // pre-gingerbread used this hardwired salt, readMasterKey will rewrite these when found |
| 830 | salt = (uint8_t*) "keystore"; |
| 831 | // sizeof = 9, not strlen = 8 |
| 832 | saltSize = sizeof("keystore"); |
| 833 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 834 | |
| 835 | PKCS5_PBKDF2_HMAC_SHA1(reinterpret_cast<const char*>(pw.string()), pw.length(), salt, |
| 836 | saltSize, 8192, keySize, key); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static bool isKeyFile(const char* filename) { |
| 840 | return ((strcmp(filename, MASTER_KEY_FILE) != 0) |
| 841 | && (strcmp(filename, ".") != 0) |
| 842 | && (strcmp(filename, "..") != 0)); |
| 843 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 844 | |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 845 | grant_t* getGrant(const char* filename, uid_t uid) const { |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 846 | struct listnode *node; |
| 847 | grant_t *grant; |
| 848 | |
| 849 | list_for_each(node, &mGrants) { |
| 850 | grant = node_to_item(node, grant_t, plist); |
| 851 | if (grant->uid == uid |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 852 | && !strcmp(reinterpret_cast<const char*>(grant->filename), |
| 853 | filename)) { |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 854 | return grant; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | return NULL; |
| 859 | } |
| 860 | |
| 861 | bool convertToUid(const Value* uidValue, uid_t* uid) const { |
| 862 | ValueString uidString(uidValue); |
| 863 | char* end = NULL; |
| 864 | *uid = strtol(uidString.c_str(), &end, 10); |
| 865 | return *end == '\0'; |
| 866 | } |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 867 | |
| 868 | /** |
| 869 | * Upgrade code. This will upgrade the key from the current version |
| 870 | * to whatever is newest. |
| 871 | */ |
| 872 | void upgrade(const char* filename, Blob* blob, const uint8_t oldVersion, const BlobType type) { |
| 873 | bool updated = false; |
| 874 | uint8_t version = oldVersion; |
| 875 | |
| 876 | /* From V0 -> V1: All old types were unknown */ |
| 877 | if (version == 0) { |
| 878 | ALOGV("upgrading to version 1 and setting type %d", type); |
| 879 | |
| 880 | blob->setType(type); |
| 881 | if (type == TYPE_KEY_PAIR) { |
| 882 | importBlobAsKey(blob, filename); |
| 883 | } |
| 884 | version = 1; |
| 885 | updated = true; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * If we've updated, set the key blob to the right version |
| 890 | * and write it. |
| 891 | * */ |
| 892 | if (updated) { |
| 893 | ALOGV("updated and writing file %s", filename); |
| 894 | blob->setVersion(version); |
| 895 | this->put(filename, blob); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Takes a blob that is an PEM-encoded RSA key as a byte array and |
| 901 | * converts it to a DER-encoded PKCS#8 for import into a keymaster. |
| 902 | * Then it overwrites the original blob with the new blob |
| 903 | * format that is returned from the keymaster. |
| 904 | */ |
| 905 | ResponseCode importBlobAsKey(Blob* blob, const char* filename) { |
| 906 | // We won't even write to the blob directly with this BIO, so const_cast is okay. |
| 907 | Unique_BIO b(BIO_new_mem_buf(const_cast<uint8_t*>(blob->getValue()), blob->getLength())); |
| 908 | if (b.get() == NULL) { |
| 909 | ALOGE("Problem instantiating BIO"); |
| 910 | return SYSTEM_ERROR; |
| 911 | } |
| 912 | |
| 913 | Unique_EVP_PKEY pkey(PEM_read_bio_PrivateKey(b.get(), NULL, NULL, NULL)); |
| 914 | if (pkey.get() == NULL) { |
| 915 | ALOGE("Couldn't read old PEM file"); |
| 916 | return SYSTEM_ERROR; |
| 917 | } |
| 918 | |
| 919 | Unique_PKCS8_PRIV_KEY_INFO pkcs8(EVP_PKEY2PKCS8(pkey.get())); |
| 920 | int len = i2d_PKCS8_PRIV_KEY_INFO(pkcs8.get(), NULL); |
| 921 | if (len < 0) { |
| 922 | ALOGE("Couldn't measure PKCS#8 length"); |
| 923 | return SYSTEM_ERROR; |
| 924 | } |
| 925 | |
| 926 | Value pkcs8key; |
| 927 | pkcs8key.length = len; |
| 928 | uint8_t* tmp = pkcs8key.value; |
| 929 | if (i2d_PKCS8_PRIV_KEY_INFO(pkcs8.get(), &tmp) != len) { |
| 930 | ALOGE("Couldn't convert to PKCS#8"); |
| 931 | return SYSTEM_ERROR; |
| 932 | } |
| 933 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 934 | ResponseCode rc = importKey(pkcs8key.value, pkcs8key.length, filename); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 935 | if (rc != NO_ERROR) { |
| 936 | return rc; |
| 937 | } |
| 938 | |
| 939 | return get(filename, blob, TYPE_KEY_PAIR); |
| 940 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 941 | }; |
| 942 | |
| 943 | const char* KeyStore::MASTER_KEY_FILE = ".masterkey"; |
| 944 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 945 | static ResponseCode get_key_for_name(KeyStore* keyStore, Blob* keyBlob, |
| 946 | const android::String8& keyName, const uid_t uid, const BlobType type) { |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 947 | char filename[NAME_MAX]; |
| 948 | |
| 949 | encode_key_for_uid(filename, uid, keyName); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 950 | ResponseCode responseCode = keyStore->get(filename, keyBlob, type); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 951 | if (responseCode == NO_ERROR) { |
| 952 | return responseCode; |
| 953 | } |
| 954 | |
| 955 | // If this is the Wifi or VPN user, they actually want system |
| 956 | // UID keys. |
| 957 | if (uid == AID_WIFI || uid == AID_VPN) { |
| 958 | encode_key_for_uid(filename, AID_SYSTEM, keyName); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 959 | responseCode = keyStore->get(filename, keyBlob, type); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 960 | if (responseCode == NO_ERROR) { |
| 961 | return responseCode; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // They might be using a granted key. |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 966 | encode_key(filename, keyName); |
| 967 | if (!keyStore->hasGrant(filename, uid)) { |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 968 | return responseCode; |
| 969 | } |
| 970 | |
| 971 | // It is a granted key. Try to load it. |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 972 | return keyStore->get(filename, keyBlob, type); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 973 | } |
| 974 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 975 | namespace android { |
| 976 | class KeyStoreProxy : public BnKeystoreService, public IBinder::DeathRecipient { |
| 977 | public: |
| 978 | KeyStoreProxy(KeyStore* keyStore) |
| 979 | : mKeyStore(keyStore) |
| 980 | { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 981 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 982 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 983 | void binderDied(const wp<IBinder>&) { |
| 984 | ALOGE("binder death detected"); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 985 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 986 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 987 | int32_t test() { |
| 988 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 989 | if (!has_permission(uid, P_TEST)) { |
| 990 | ALOGW("permission denied for %d: test", uid); |
| 991 | return ::PERMISSION_DENIED; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 992 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 993 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 994 | return mKeyStore->getState(); |
Kenny Root | 298e7b1 | 2012-03-26 13:54:44 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 997 | int32_t get(const String16& name, uint8_t** item, size_t* itemLength) { |
| 998 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 999 | if (!has_permission(uid, P_GET)) { |
| 1000 | ALOGW("permission denied for %d: get", uid); |
| 1001 | return ::PERMISSION_DENIED; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1002 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1003 | uid = get_keystore_euid(uid); |
| 1004 | |
| 1005 | State state = checkState(); |
| 1006 | if (state != STATE_NO_ERROR) { |
| 1007 | ALOGD("calling get in state: %d", state); |
| 1008 | return state; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1009 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1010 | |
| 1011 | String8 name8(name); |
| 1012 | char filename[NAME_MAX]; |
| 1013 | |
| 1014 | encode_key_for_uid(filename, uid, name8); |
| 1015 | |
| 1016 | Blob keyBlob; |
| 1017 | ResponseCode responseCode = mKeyStore->get(filename, &keyBlob, TYPE_GENERIC); |
| 1018 | if (responseCode != ::NO_ERROR) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 1019 | ALOGW("Could not read %s", filename); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1020 | *item = NULL; |
| 1021 | *itemLength = 0; |
| 1022 | return responseCode; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1023 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1024 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1025 | *item = (uint8_t*) malloc(keyBlob.getLength()); |
| 1026 | memcpy(*item, keyBlob.getValue(), keyBlob.getLength()); |
| 1027 | *itemLength = keyBlob.getLength(); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1028 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1029 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1032 | int32_t insert(const String16& name, const uint8_t* item, size_t itemLength) { |
| 1033 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1034 | if (!has_permission(uid, P_INSERT)) { |
| 1035 | ALOGW("permission denied for %d: insert", uid); |
| 1036 | return ::PERMISSION_DENIED; |
| 1037 | } |
| 1038 | uid = get_keystore_euid(uid); |
| 1039 | |
| 1040 | State state = checkState(); |
| 1041 | if (state != STATE_NO_ERROR) { |
| 1042 | ALOGD("calling insert in state: %d", state); |
| 1043 | return state; |
| 1044 | } |
| 1045 | |
| 1046 | String8 name8(name); |
| 1047 | char filename[NAME_MAX]; |
| 1048 | |
| 1049 | encode_key_for_uid(filename, uid, name8); |
| 1050 | |
| 1051 | Blob keyBlob(item, itemLength, NULL, 0, ::TYPE_GENERIC); |
| 1052 | return mKeyStore->put(filename, &keyBlob); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1055 | int32_t del(const String16& name) { |
| 1056 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1057 | if (!has_permission(uid, P_DELETE)) { |
| 1058 | ALOGW("permission denied for %d: del", uid); |
| 1059 | return ::PERMISSION_DENIED; |
| 1060 | } |
| 1061 | uid = get_keystore_euid(uid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1062 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1063 | String8 name8(name); |
| 1064 | char filename[NAME_MAX]; |
| 1065 | |
| 1066 | encode_key_for_uid(filename, uid, name8); |
| 1067 | |
| 1068 | Blob keyBlob; |
| 1069 | ResponseCode responseCode = mKeyStore->get(filename, &keyBlob, TYPE_GENERIC); |
| 1070 | if (responseCode != ::NO_ERROR) { |
| 1071 | return responseCode; |
| 1072 | } |
| 1073 | return (unlink(filename) && errno != ENOENT) ? ::SYSTEM_ERROR : ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1076 | int32_t exist(const String16& name) { |
| 1077 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1078 | if (!has_permission(uid, P_EXIST)) { |
| 1079 | ALOGW("permission denied for %d: exist", uid); |
| 1080 | return ::PERMISSION_DENIED; |
| 1081 | } |
| 1082 | uid = get_keystore_euid(uid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1083 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1084 | String8 name8(name); |
| 1085 | char filename[NAME_MAX]; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1086 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1087 | encode_key_for_uid(filename, uid, name8); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1088 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1089 | if (access(filename, R_OK) == -1) { |
| 1090 | return (errno != ENOENT) ? ::SYSTEM_ERROR : ::KEY_NOT_FOUND; |
| 1091 | } |
| 1092 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1095 | int32_t saw(const String16& prefix, Vector<String16>* matches) { |
| 1096 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1097 | if (!has_permission(uid, P_SAW)) { |
| 1098 | ALOGW("permission denied for %d: saw", uid); |
| 1099 | return ::PERMISSION_DENIED; |
| 1100 | } |
| 1101 | uid = get_keystore_euid(uid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1102 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1103 | DIR* dir = opendir("."); |
| 1104 | if (!dir) { |
| 1105 | return ::SYSTEM_ERROR; |
| 1106 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1107 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1108 | const String8 prefix8(prefix); |
| 1109 | char filename[NAME_MAX]; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1110 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1111 | int n = encode_key_for_uid(filename, uid, prefix8); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1112 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1113 | struct dirent* file; |
| 1114 | while ((file = readdir(dir)) != NULL) { |
| 1115 | if (!strncmp(filename, file->d_name, n)) { |
| 1116 | const char* p = &file->d_name[n]; |
| 1117 | size_t plen = strlen(p); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1118 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1119 | size_t extra = decode_key_length(p, plen); |
| 1120 | char *match = (char*) malloc(extra + 1); |
| 1121 | if (match != NULL) { |
| 1122 | decode_key(match, p, plen); |
| 1123 | matches->push(String16(match, extra)); |
| 1124 | free(match); |
| 1125 | } else { |
| 1126 | ALOGW("could not allocate match of size %zd", extra); |
| 1127 | } |
Kenny Root | 9a53d3e | 2012-08-14 10:47:54 -0700 | [diff] [blame] | 1128 | } |
| 1129 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1130 | closedir(dir); |
| 1131 | |
| 1132 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1135 | int32_t reset() { |
| 1136 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1137 | if (!has_permission(uid, P_RESET)) { |
| 1138 | ALOGW("permission denied for %d: reset", uid); |
| 1139 | return ::PERMISSION_DENIED; |
| 1140 | } |
| 1141 | |
| 1142 | ResponseCode rc = mKeyStore->reset() ? ::NO_ERROR : ::SYSTEM_ERROR; |
| 1143 | |
| 1144 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1145 | if (device == NULL) { |
| 1146 | ALOGE("No keymaster device!"); |
| 1147 | return ::SYSTEM_ERROR; |
| 1148 | } |
| 1149 | |
| 1150 | if (device->delete_all == NULL) { |
| 1151 | ALOGV("keymaster device doesn't implement delete_all"); |
| 1152 | return rc; |
| 1153 | } |
| 1154 | |
| 1155 | if (device->delete_all(device)) { |
| 1156 | ALOGE("Problem calling keymaster's delete_all"); |
| 1157 | return ::SYSTEM_ERROR; |
| 1158 | } |
| 1159 | |
Kenny Root | 9a53d3e | 2012-08-14 10:47:54 -0700 | [diff] [blame] | 1160 | return rc; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1163 | /* |
| 1164 | * Here is the history. To improve the security, the parameters to generate the |
| 1165 | * master key has been changed. To make a seamless transition, we update the |
| 1166 | * file using the same password when the user unlock it for the first time. If |
| 1167 | * any thing goes wrong during the transition, the new file will not overwrite |
| 1168 | * the old one. This avoids permanent damages of the existing data. |
| 1169 | */ |
| 1170 | int32_t password(const String16& password) { |
| 1171 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1172 | if (!has_permission(uid, P_PASSWORD)) { |
| 1173 | ALOGW("permission denied for %d: password", uid); |
| 1174 | return ::PERMISSION_DENIED; |
| 1175 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1176 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1177 | const String8 password8(password); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1178 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1179 | switch (mKeyStore->getState()) { |
| 1180 | case ::STATE_UNINITIALIZED: { |
| 1181 | // generate master key, encrypt with password, write to file, initialize mMasterKey*. |
| 1182 | return mKeyStore->initialize(password8); |
| 1183 | } |
| 1184 | case ::STATE_NO_ERROR: { |
| 1185 | // rewrite master key with new password. |
| 1186 | return mKeyStore->writeMasterKey(password8); |
| 1187 | } |
| 1188 | case ::STATE_LOCKED: { |
| 1189 | // read master key, decrypt with password, initialize mMasterKey*. |
| 1190 | return mKeyStore->readMasterKey(password8); |
| 1191 | } |
| 1192 | } |
| 1193 | return ::SYSTEM_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1196 | int32_t lock() { |
| 1197 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1198 | if (!has_permission(uid, P_LOCK)) { |
| 1199 | ALOGW("permission denied for %d: lock", uid); |
| 1200 | return ::PERMISSION_DENIED; |
| 1201 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1202 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1203 | State state = checkState(); |
| 1204 | if (state != STATE_NO_ERROR) { |
| 1205 | ALOGD("calling lock in state: %d", state); |
| 1206 | return state; |
| 1207 | } |
| 1208 | |
| 1209 | mKeyStore->lock(); |
| 1210 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1213 | int32_t unlock(const String16& pw) { |
| 1214 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1215 | if (!has_permission(uid, P_UNLOCK)) { |
| 1216 | ALOGW("permission denied for %d: unlock", uid); |
| 1217 | return ::PERMISSION_DENIED; |
| 1218 | } |
| 1219 | |
| 1220 | State state = checkState(); |
| 1221 | if (state != STATE_LOCKED) { |
| 1222 | ALOGD("calling unlock when not locked"); |
| 1223 | return state; |
| 1224 | } |
| 1225 | |
| 1226 | const String8 password8(pw); |
| 1227 | return password(pw); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1230 | int32_t zero() { |
| 1231 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1232 | if (!has_permission(uid, P_ZERO)) { |
| 1233 | ALOGW("permission denied for %d: zero", uid); |
| 1234 | return -1; |
| 1235 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1236 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1237 | return mKeyStore->isEmpty() ? ::KEY_NOT_FOUND : ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1240 | int32_t generate(const String16& name) { |
| 1241 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1242 | if (!has_permission(uid, P_INSERT)) { |
| 1243 | ALOGW("permission denied for %d: generate", uid); |
| 1244 | return ::PERMISSION_DENIED; |
| 1245 | } |
| 1246 | uid = get_keystore_euid(uid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1247 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1248 | State state = checkState(); |
| 1249 | if (state != STATE_NO_ERROR) { |
| 1250 | ALOGD("calling generate in state: %d", state); |
| 1251 | return state; |
| 1252 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1253 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1254 | String8 name8(name); |
| 1255 | char filename[NAME_MAX]; |
| 1256 | |
| 1257 | uint8_t* data; |
| 1258 | size_t dataLength; |
| 1259 | int rc; |
| 1260 | |
| 1261 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1262 | if (device == NULL) { |
| 1263 | return ::SYSTEM_ERROR; |
| 1264 | } |
| 1265 | |
| 1266 | if (device->generate_keypair == NULL) { |
| 1267 | return ::SYSTEM_ERROR; |
| 1268 | } |
| 1269 | |
| 1270 | keymaster_rsa_keygen_params_t rsa_params; |
| 1271 | rsa_params.modulus_size = 2048; |
| 1272 | rsa_params.public_exponent = 0x10001; |
| 1273 | |
| 1274 | rc = device->generate_keypair(device, TYPE_RSA, &rsa_params, &data, &dataLength); |
| 1275 | if (rc) { |
| 1276 | return ::SYSTEM_ERROR; |
| 1277 | } |
| 1278 | |
| 1279 | encode_key_for_uid(filename, uid, name8); |
| 1280 | |
| 1281 | Blob keyBlob(data, dataLength, NULL, 0, TYPE_KEY_PAIR); |
| 1282 | free(data); |
| 1283 | |
| 1284 | return mKeyStore->put(filename, &keyBlob); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1287 | int32_t import(const String16& name, const uint8_t* data, size_t length) { |
| 1288 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1289 | if (!has_permission(uid, P_INSERT)) { |
| 1290 | ALOGW("permission denied for %d: import", uid); |
| 1291 | return ::PERMISSION_DENIED; |
| 1292 | } |
| 1293 | uid = get_keystore_euid(uid); |
| 1294 | |
| 1295 | State state = checkState(); |
| 1296 | if (state != STATE_NO_ERROR) { |
| 1297 | ALOGD("calling import in state: %d", state); |
| 1298 | return state; |
| 1299 | } |
| 1300 | |
| 1301 | String8 name8(name); |
| 1302 | char filename[NAME_MAX]; |
| 1303 | |
| 1304 | encode_key_for_uid(filename, uid, name8); |
| 1305 | |
| 1306 | return mKeyStore->importKey(data, length, filename); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1309 | int32_t sign(const String16& name, const uint8_t* data, size_t length, uint8_t** out, |
| 1310 | size_t* outLength) { |
| 1311 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1312 | if (!has_permission(uid, P_SIGN)) { |
| 1313 | ALOGW("permission denied for %d: saw", uid); |
| 1314 | return ::PERMISSION_DENIED; |
| 1315 | } |
| 1316 | uid = get_keystore_euid(uid); |
| 1317 | |
| 1318 | State state = checkState(); |
| 1319 | if (state != STATE_NO_ERROR) { |
| 1320 | ALOGD("calling sign in state: %d", state); |
| 1321 | return state; |
| 1322 | } |
| 1323 | |
| 1324 | Blob keyBlob; |
| 1325 | String8 name8(name); |
| 1326 | |
| 1327 | ALOGV("sign %s from uid %d", name8.string(), uid); |
| 1328 | int rc; |
| 1329 | |
| 1330 | ResponseCode responseCode = get_key_for_name(mKeyStore, &keyBlob, name8, uid, ::TYPE_KEY_PAIR); |
| 1331 | if (responseCode != ::NO_ERROR) { |
| 1332 | return responseCode; |
| 1333 | } |
| 1334 | |
| 1335 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1336 | if (device == NULL) { |
| 1337 | ALOGE("no keymaster device; cannot sign"); |
| 1338 | return ::SYSTEM_ERROR; |
| 1339 | } |
| 1340 | |
| 1341 | if (device->sign_data == NULL) { |
| 1342 | ALOGE("device doesn't implement signing"); |
| 1343 | return ::SYSTEM_ERROR; |
| 1344 | } |
| 1345 | |
| 1346 | keymaster_rsa_sign_params_t params; |
| 1347 | params.digest_type = DIGEST_NONE; |
| 1348 | params.padding_type = PADDING_NONE; |
| 1349 | |
| 1350 | rc = device->sign_data(device, ¶ms, keyBlob.getValue(), keyBlob.getLength(), |
| 1351 | data, length, out, outLength); |
| 1352 | if (rc) { |
| 1353 | ALOGW("device couldn't sign data"); |
| 1354 | return ::SYSTEM_ERROR; |
| 1355 | } |
| 1356 | |
| 1357 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1360 | int32_t verify(const String16& name, const uint8_t* data, size_t dataLength, |
| 1361 | const uint8_t* signature, size_t signatureLength) { |
| 1362 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1363 | if (!has_permission(uid, P_VERIFY)) { |
| 1364 | ALOGW("permission denied for %d: verify", uid); |
| 1365 | return ::PERMISSION_DENIED; |
| 1366 | } |
| 1367 | uid = get_keystore_euid(uid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1368 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1369 | State state = checkState(); |
| 1370 | if (state != STATE_NO_ERROR) { |
| 1371 | ALOGD("calling verify in state: %d", state); |
| 1372 | return state; |
| 1373 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1374 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1375 | Blob keyBlob; |
| 1376 | String8 name8(name); |
| 1377 | int rc; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1378 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1379 | ResponseCode responseCode = get_key_for_name(mKeyStore, &keyBlob, name8, uid, TYPE_KEY_PAIR); |
| 1380 | if (responseCode != ::NO_ERROR) { |
| 1381 | return responseCode; |
| 1382 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1383 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1384 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1385 | if (device == NULL) { |
| 1386 | return ::SYSTEM_ERROR; |
| 1387 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1388 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1389 | if (device->verify_data == NULL) { |
| 1390 | return ::SYSTEM_ERROR; |
| 1391 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1392 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1393 | keymaster_rsa_sign_params_t params; |
| 1394 | params.digest_type = DIGEST_NONE; |
| 1395 | params.padding_type = PADDING_NONE; |
Kenny Root | 344e0bc | 2012-08-15 10:44:03 -0700 | [diff] [blame] | 1396 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1397 | rc = device->verify_data(device, ¶ms, keyBlob.getValue(), keyBlob.getLength(), |
| 1398 | data, dataLength, signature, signatureLength); |
| 1399 | if (rc) { |
| 1400 | return ::SYSTEM_ERROR; |
| 1401 | } else { |
| 1402 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1403 | } |
| 1404 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1405 | |
| 1406 | /* |
| 1407 | * TODO: The abstraction between things stored in hardware and regular blobs |
| 1408 | * of data stored on the filesystem should be moved down to keystore itself. |
| 1409 | * Unfortunately the Java code that calls this has naming conventions that it |
| 1410 | * knows about. Ideally keystore shouldn't be used to store random blobs of |
| 1411 | * data. |
| 1412 | * |
| 1413 | * Until that happens, it's necessary to have a separate "get_pubkey" and |
| 1414 | * "del_key" since the Java code doesn't really communicate what it's |
| 1415 | * intentions are. |
| 1416 | */ |
| 1417 | int32_t get_pubkey(const String16& name, uint8_t** pubkey, size_t* pubkeyLength) { |
| 1418 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1419 | if (!has_permission(uid, P_GET)) { |
| 1420 | ALOGW("permission denied for %d: get_pubkey", uid); |
| 1421 | return ::PERMISSION_DENIED; |
| 1422 | } |
| 1423 | uid = get_keystore_euid(uid); |
| 1424 | |
| 1425 | State state = checkState(); |
| 1426 | if (state != STATE_NO_ERROR) { |
| 1427 | ALOGD("calling get_pubkey in state: %d", state); |
| 1428 | return state; |
| 1429 | } |
| 1430 | |
| 1431 | Blob keyBlob; |
| 1432 | String8 name8(name); |
| 1433 | |
| 1434 | ALOGV("get_pubkey '%s' from uid %d", name8.string(), uid); |
| 1435 | |
| 1436 | ResponseCode responseCode = get_key_for_name(mKeyStore, &keyBlob, name8, uid, |
| 1437 | TYPE_KEY_PAIR); |
| 1438 | if (responseCode != ::NO_ERROR) { |
| 1439 | return responseCode; |
| 1440 | } |
| 1441 | |
| 1442 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1443 | if (device == NULL) { |
| 1444 | return ::SYSTEM_ERROR; |
| 1445 | } |
| 1446 | |
| 1447 | if (device->get_keypair_public == NULL) { |
| 1448 | ALOGE("device has no get_keypair_public implementation!"); |
| 1449 | return ::SYSTEM_ERROR; |
| 1450 | } |
| 1451 | |
| 1452 | int rc = device->get_keypair_public(device, keyBlob.getValue(), keyBlob.getLength(), pubkey, |
| 1453 | pubkeyLength); |
| 1454 | if (rc) { |
| 1455 | return ::SYSTEM_ERROR; |
| 1456 | } |
| 1457 | |
| 1458 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1459 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1460 | |
| 1461 | int32_t del_key(const String16& name) { |
| 1462 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1463 | if (!has_permission(uid, P_DELETE)) { |
| 1464 | ALOGW("permission denied for %d: del_key", uid); |
| 1465 | return ::PERMISSION_DENIED; |
| 1466 | } |
| 1467 | uid = get_keystore_euid(uid); |
| 1468 | |
| 1469 | String8 name8(name); |
| 1470 | char filename[NAME_MAX]; |
| 1471 | |
| 1472 | encode_key_for_uid(filename, uid, name8); |
| 1473 | |
| 1474 | Blob keyBlob; |
| 1475 | ResponseCode responseCode = mKeyStore->get(filename, &keyBlob, ::TYPE_KEY_PAIR); |
| 1476 | if (responseCode != ::NO_ERROR) { |
| 1477 | return responseCode; |
| 1478 | } |
| 1479 | |
| 1480 | ResponseCode rc = ::NO_ERROR; |
| 1481 | |
| 1482 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1483 | if (device == NULL) { |
| 1484 | rc = ::SYSTEM_ERROR; |
| 1485 | } else { |
| 1486 | // A device doesn't have to implement delete_keypair. |
| 1487 | if (device->delete_keypair != NULL) { |
| 1488 | if (device->delete_keypair(device, keyBlob.getValue(), keyBlob.getLength())) { |
| 1489 | rc = ::SYSTEM_ERROR; |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | if (rc != ::NO_ERROR) { |
| 1495 | return rc; |
| 1496 | } |
| 1497 | |
| 1498 | return (unlink(filename) && errno != ENOENT) ? ::SYSTEM_ERROR : ::NO_ERROR; |
| 1499 | } |
| 1500 | |
| 1501 | int32_t grant(const String16& name, int32_t granteeUid) { |
| 1502 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1503 | if (!has_permission(uid, P_GRANT)) { |
| 1504 | ALOGW("permission denied for %d: grant", uid); |
| 1505 | return ::PERMISSION_DENIED; |
| 1506 | } |
| 1507 | uid = get_keystore_euid(uid); |
| 1508 | |
| 1509 | State state = checkState(); |
| 1510 | if (state != STATE_NO_ERROR) { |
| 1511 | ALOGD("calling grant in state: %d", state); |
| 1512 | return state; |
| 1513 | } |
| 1514 | |
| 1515 | String8 name8(name); |
| 1516 | char filename[NAME_MAX]; |
| 1517 | |
| 1518 | encode_key_for_uid(filename, uid, name8); |
| 1519 | |
| 1520 | if (access(filename, R_OK) == -1) { |
| 1521 | return (errno != ENOENT) ? ::SYSTEM_ERROR : ::KEY_NOT_FOUND; |
| 1522 | } |
| 1523 | |
| 1524 | mKeyStore->addGrant(filename, granteeUid); |
| 1525 | return ::NO_ERROR; |
| 1526 | } |
| 1527 | |
| 1528 | int32_t ungrant(const String16& name, int32_t granteeUid) { |
| 1529 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1530 | if (!has_permission(uid, P_GRANT)) { |
| 1531 | ALOGW("permission denied for %d: ungrant", uid); |
| 1532 | return ::PERMISSION_DENIED; |
| 1533 | } |
| 1534 | uid = get_keystore_euid(uid); |
| 1535 | |
| 1536 | State state = checkState(); |
| 1537 | if (state != STATE_NO_ERROR) { |
| 1538 | ALOGD("calling ungrant in state: %d", state); |
| 1539 | return state; |
| 1540 | } |
| 1541 | |
| 1542 | String8 name8(name); |
| 1543 | char filename[NAME_MAX]; |
| 1544 | |
| 1545 | encode_key_for_uid(filename, uid, name8); |
| 1546 | |
| 1547 | if (access(filename, R_OK) == -1) { |
| 1548 | return (errno != ENOENT) ? ::SYSTEM_ERROR : ::KEY_NOT_FOUND; |
| 1549 | } |
| 1550 | |
| 1551 | return mKeyStore->removeGrant(filename, granteeUid) ? ::NO_ERROR : ::KEY_NOT_FOUND; |
| 1552 | } |
| 1553 | |
| 1554 | int64_t getmtime(const String16& name) { |
| 1555 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1556 | if (!has_permission(uid, P_GET)) { |
| 1557 | ALOGW("permission denied for %d: getmtime", uid); |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 1558 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1559 | } |
| 1560 | uid = get_keystore_euid(uid); |
| 1561 | |
| 1562 | String8 name8(name); |
| 1563 | char filename[NAME_MAX]; |
| 1564 | |
| 1565 | encode_key_for_uid(filename, uid, name8); |
| 1566 | |
| 1567 | if (access(filename, R_OK) == -1) { |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 1568 | ALOGW("could not access %s for getmtime", filename); |
| 1569 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 1572 | int fd = TEMP_FAILURE_RETRY(open(filename, O_NOFOLLOW, O_RDONLY)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1573 | if (fd < 0) { |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 1574 | ALOGW("could not open %s for getmtime", filename); |
| 1575 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | struct stat s; |
| 1579 | int ret = fstat(fd, &s); |
| 1580 | close(fd); |
| 1581 | if (ret == -1) { |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 1582 | ALOGW("could not stat %s for getmtime", filename); |
| 1583 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 1586 | return static_cast<int64_t>(s.st_mtime); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | private: |
| 1590 | inline State checkState() { |
| 1591 | return mKeyStore->getState(); |
| 1592 | } |
| 1593 | |
| 1594 | ::KeyStore* mKeyStore; |
| 1595 | }; |
| 1596 | |
| 1597 | }; // namespace android |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1598 | |
| 1599 | int main(int argc, char* argv[]) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1600 | if (argc < 2) { |
| 1601 | ALOGE("A directory must be specified!"); |
| 1602 | return 1; |
| 1603 | } |
| 1604 | if (chdir(argv[1]) == -1) { |
| 1605 | ALOGE("chdir: %s: %s", argv[1], strerror(errno)); |
| 1606 | return 1; |
| 1607 | } |
| 1608 | |
| 1609 | Entropy entropy; |
| 1610 | if (!entropy.open()) { |
| 1611 | return 1; |
| 1612 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1613 | |
| 1614 | keymaster_device_t* dev; |
| 1615 | if (keymaster_device_initialize(&dev)) { |
| 1616 | ALOGE("keystore keymaster could not be initialized; exiting"); |
| 1617 | return 1; |
| 1618 | } |
| 1619 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1620 | KeyStore keyStore(&entropy, dev); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1621 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); |
| 1622 | android::sp<android::KeyStoreProxy> proxy = new android::KeyStoreProxy(&keyStore); |
| 1623 | android::status_t ret = sm->addService(android::String16("android.security.keystore"), proxy); |
| 1624 | if (ret != android::OK) { |
| 1625 | ALOGE("Couldn't register binder service!"); |
| 1626 | return -1; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1627 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1628 | |
| 1629 | /* |
| 1630 | * We're the only thread in existence, so we're just going to process |
| 1631 | * Binder transaction as a single-threaded program. |
| 1632 | */ |
| 1633 | android::IPCThreadState::self()->joinThreadPool(); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1634 | |
| 1635 | keymaster_device_release(dev); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1636 | return 1; |
| 1637 | } |