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> |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 27 | #include <errno.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 28 | #include <fcntl.h> |
| 29 | #include <limits.h> |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 30 | #include <assert.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/time.h> |
| 35 | #include <arpa/inet.h> |
| 36 | |
| 37 | #include <openssl/aes.h> |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 38 | #include <openssl/bio.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 39 | #include <openssl/evp.h> |
| 40 | #include <openssl/md5.h> |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 41 | #include <openssl/pem.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 42 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 43 | #include <hardware/keymaster.h> |
| 44 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 45 | #include <keymaster/softkeymaster.h> |
| 46 | |
Kenny Root | 26cfc08 | 2013-09-11 14:38:56 -0700 | [diff] [blame] | 47 | #include <UniquePtr.h> |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 48 | #include <utils/String8.h> |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 49 | #include <utils/Vector.h> |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 50 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 51 | #include <keystore/IKeystoreService.h> |
| 52 | #include <binder/IPCThreadState.h> |
| 53 | #include <binder/IServiceManager.h> |
| 54 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 55 | #include <cutils/log.h> |
| 56 | #include <cutils/sockets.h> |
| 57 | #include <private/android_filesystem_config.h> |
| 58 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 59 | #include <keystore/keystore.h> |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 60 | |
Kenny Root | 6071179 | 2013-08-16 14:02:41 -0700 | [diff] [blame] | 61 | #include "defaults.h" |
| 62 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 63 | /* KeyStore is a secured storage for key-value pairs. In this implementation, |
| 64 | * each file stores one key-value pair. Keys are encoded in file names, and |
| 65 | * values are encrypted with checksums. The encryption key is protected by a |
| 66 | * user-defined password. To keep things simple, buffers are always larger than |
| 67 | * the maximum space we needed, so boundary checks on buffers are omitted. */ |
| 68 | |
| 69 | #define KEY_SIZE ((NAME_MAX - 15) / 2) |
| 70 | #define VALUE_SIZE 32768 |
| 71 | #define PASSWORD_SIZE VALUE_SIZE |
| 72 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 73 | |
Kenny Root | 6071179 | 2013-08-16 14:02:41 -0700 | [diff] [blame] | 74 | struct BIGNUM_Delete { |
| 75 | void operator()(BIGNUM* p) const { |
| 76 | BN_free(p); |
| 77 | } |
| 78 | }; |
| 79 | typedef UniquePtr<BIGNUM, BIGNUM_Delete> Unique_BIGNUM; |
| 80 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 81 | struct BIO_Delete { |
| 82 | void operator()(BIO* p) const { |
| 83 | BIO_free(p); |
| 84 | } |
| 85 | }; |
| 86 | typedef UniquePtr<BIO, BIO_Delete> Unique_BIO; |
| 87 | |
| 88 | struct EVP_PKEY_Delete { |
| 89 | void operator()(EVP_PKEY* p) const { |
| 90 | EVP_PKEY_free(p); |
| 91 | } |
| 92 | }; |
| 93 | typedef UniquePtr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY; |
| 94 | |
| 95 | struct PKCS8_PRIV_KEY_INFO_Delete { |
| 96 | void operator()(PKCS8_PRIV_KEY_INFO* p) const { |
| 97 | PKCS8_PRIV_KEY_INFO_free(p); |
| 98 | } |
| 99 | }; |
| 100 | typedef UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO; |
| 101 | |
| 102 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 103 | static int keymaster_device_initialize(keymaster_device_t** dev) { |
| 104 | int rc; |
| 105 | |
| 106 | const hw_module_t* mod; |
| 107 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 108 | if (rc) { |
| 109 | ALOGE("could not find any keystore module"); |
| 110 | goto out; |
| 111 | } |
| 112 | |
| 113 | rc = keymaster_open(mod, dev); |
| 114 | if (rc) { |
| 115 | ALOGE("could not open keymaster device in %s (%s)", |
| 116 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 117 | goto out; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | |
| 122 | out: |
| 123 | *dev = NULL; |
| 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | static void keymaster_device_release(keymaster_device_t* dev) { |
| 128 | keymaster_close(dev); |
| 129 | } |
| 130 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 131 | /*************** |
| 132 | * PERMISSIONS * |
| 133 | ***************/ |
| 134 | |
| 135 | /* Here are the permissions, actions, users, and the main function. */ |
| 136 | typedef enum { |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 137 | P_TEST = 1 << 0, |
| 138 | P_GET = 1 << 1, |
| 139 | P_INSERT = 1 << 2, |
| 140 | P_DELETE = 1 << 3, |
| 141 | P_EXIST = 1 << 4, |
| 142 | P_SAW = 1 << 5, |
| 143 | P_RESET = 1 << 6, |
| 144 | P_PASSWORD = 1 << 7, |
| 145 | P_LOCK = 1 << 8, |
| 146 | P_UNLOCK = 1 << 9, |
| 147 | P_ZERO = 1 << 10, |
| 148 | P_SIGN = 1 << 11, |
| 149 | P_VERIFY = 1 << 12, |
| 150 | P_GRANT = 1 << 13, |
| 151 | P_DUPLICATE = 1 << 14, |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 152 | P_CLEAR_UID = 1 << 15, |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 153 | } perm_t; |
| 154 | |
| 155 | static struct user_euid { |
| 156 | uid_t uid; |
| 157 | uid_t euid; |
| 158 | } user_euids[] = { |
| 159 | {AID_VPN, AID_SYSTEM}, |
| 160 | {AID_WIFI, AID_SYSTEM}, |
| 161 | {AID_ROOT, AID_SYSTEM}, |
| 162 | }; |
| 163 | |
| 164 | static struct user_perm { |
| 165 | uid_t uid; |
| 166 | perm_t perms; |
| 167 | } user_perms[] = { |
| 168 | {AID_SYSTEM, static_cast<perm_t>((uint32_t)(~0)) }, |
| 169 | {AID_VPN, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY) }, |
| 170 | {AID_WIFI, static_cast<perm_t>(P_GET | P_SIGN | P_VERIFY) }, |
| 171 | {AID_ROOT, static_cast<perm_t>(P_GET) }, |
| 172 | }; |
| 173 | |
| 174 | static const perm_t DEFAULT_PERMS = static_cast<perm_t>(P_TEST | P_GET | P_INSERT | P_DELETE | P_EXIST | P_SAW | P_SIGN |
| 175 | | P_VERIFY); |
| 176 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Returns the app ID (in the Android multi-user sense) for the current |
| 179 | * UNIX UID. |
| 180 | */ |
| 181 | static uid_t get_app_id(uid_t uid) { |
| 182 | return uid % AID_USER; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns the user ID (in the Android multi-user sense) for the current |
| 187 | * UNIX UID. |
| 188 | */ |
| 189 | static uid_t get_user_id(uid_t uid) { |
| 190 | return uid / AID_USER; |
| 191 | } |
| 192 | |
| 193 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 194 | static bool has_permission(uid_t uid, perm_t perm) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 195 | // All system users are equivalent for multi-user support. |
| 196 | if (get_app_id(uid) == AID_SYSTEM) { |
| 197 | uid = AID_SYSTEM; |
| 198 | } |
| 199 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 200 | for (size_t i = 0; i < sizeof(user_perms)/sizeof(user_perms[0]); i++) { |
| 201 | struct user_perm user = user_perms[i]; |
| 202 | if (user.uid == uid) { |
| 203 | return user.perms & perm; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return DEFAULT_PERMS & perm; |
| 208 | } |
| 209 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Returns the UID that the callingUid should act as. This is here for |
| 212 | * legacy support of the WiFi and VPN systems and should be removed |
| 213 | * when WiFi can operate in its own namespace. |
| 214 | */ |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 215 | static uid_t get_keystore_euid(uid_t uid) { |
| 216 | for (size_t i = 0; i < sizeof(user_euids)/sizeof(user_euids[0]); i++) { |
| 217 | struct user_euid user = user_euids[i]; |
| 218 | if (user.uid == uid) { |
| 219 | return user.euid; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return uid; |
| 224 | } |
| 225 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Returns true if the callingUid is allowed to interact in the targetUid's |
| 228 | * namespace. |
| 229 | */ |
| 230 | static bool is_granted_to(uid_t callingUid, uid_t targetUid) { |
| 231 | for (size_t i = 0; i < sizeof(user_euids)/sizeof(user_euids[0]); i++) { |
| 232 | struct user_euid user = user_euids[i]; |
| 233 | if (user.euid == callingUid && user.uid == targetUid) { |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 241 | /* Here is the encoding of keys. This is necessary in order to allow arbitrary |
| 242 | * characters in keys. Characters in [0-~] are not encoded. Others are encoded |
| 243 | * into two bytes. The first byte is one of [+-.] which represents the first |
| 244 | * two bits of the character. The second byte encodes the rest of the bits into |
| 245 | * [0-o]. Therefore in the worst case the length of a key gets doubled. Note |
| 246 | * that Base64 cannot be used here due to the need of prefix match on keys. */ |
| 247 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 248 | static size_t encode_key_length(const android::String8& keyName) { |
| 249 | const uint8_t* in = reinterpret_cast<const uint8_t*>(keyName.string()); |
| 250 | size_t length = keyName.length(); |
| 251 | for (int i = length; i > 0; --i, ++in) { |
| 252 | if (*in < '0' || *in > '~') { |
| 253 | ++length; |
| 254 | } |
| 255 | } |
| 256 | return length; |
| 257 | } |
| 258 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 259 | static int encode_key(char* out, const android::String8& keyName) { |
| 260 | const uint8_t* in = reinterpret_cast<const uint8_t*>(keyName.string()); |
| 261 | size_t length = keyName.length(); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 262 | for (int i = length; i > 0; --i, ++in, ++out) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 263 | if (*in < '0' || *in > '~') { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 264 | *out = '+' + (*in >> 6); |
| 265 | *++out = '0' + (*in & 0x3F); |
| 266 | ++length; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 267 | } else { |
| 268 | *out = *in; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | *out = '\0'; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 272 | return length; |
| 273 | } |
| 274 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 275 | 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] | 276 | int n = snprintf(out, NAME_MAX, "%u_", uid); |
| 277 | out += n; |
| 278 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 279 | return n + encode_key(out, keyName); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 282 | /* |
| 283 | * Converts from the "escaped" format on disk to actual name. |
| 284 | * This will be smaller than the input string. |
| 285 | * |
| 286 | * Characters that should combine with the next at the end will be truncated. |
| 287 | */ |
| 288 | static size_t decode_key_length(const char* in, size_t length) { |
| 289 | size_t outLength = 0; |
| 290 | |
| 291 | for (const char* end = in + length; in < end; in++) { |
| 292 | /* This combines with the next character. */ |
| 293 | if (*in < '0' || *in > '~') { |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | outLength++; |
| 298 | } |
| 299 | return outLength; |
| 300 | } |
| 301 | |
| 302 | static void decode_key(char* out, const char* in, size_t length) { |
| 303 | for (const char* end = in + length; in < end; in++) { |
| 304 | if (*in < '0' || *in > '~') { |
| 305 | /* Truncate combining characters at the end. */ |
| 306 | if (in + 1 >= end) { |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | *out = (*in++ - '+') << 6; |
| 311 | *out++ |= (*in - '0') & 0x3F; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 312 | } else { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 313 | *out++ = *in; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | *out = '\0'; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static size_t readFully(int fd, uint8_t* data, size_t size) { |
| 320 | size_t remaining = size; |
| 321 | while (remaining > 0) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 322 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining)); |
Kenny Root | 5281edb | 2012-11-21 15:14:04 -0800 | [diff] [blame] | 323 | if (n <= 0) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 324 | return size - remaining; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 325 | } |
| 326 | data += n; |
| 327 | remaining -= n; |
| 328 | } |
| 329 | return size; |
| 330 | } |
| 331 | |
| 332 | static size_t writeFully(int fd, uint8_t* data, size_t size) { |
| 333 | size_t remaining = size; |
| 334 | while (remaining > 0) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 335 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining)); |
| 336 | if (n < 0) { |
| 337 | ALOGW("write failed: %s", strerror(errno)); |
| 338 | return size - remaining; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 339 | } |
| 340 | data += n; |
| 341 | remaining -= n; |
| 342 | } |
| 343 | return size; |
| 344 | } |
| 345 | |
| 346 | class Entropy { |
| 347 | public: |
| 348 | Entropy() : mRandom(-1) {} |
| 349 | ~Entropy() { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 350 | if (mRandom >= 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 351 | close(mRandom); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | bool open() { |
| 356 | const char* randomDevice = "/dev/urandom"; |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 357 | mRandom = TEMP_FAILURE_RETRY(::open(randomDevice, O_RDONLY)); |
| 358 | if (mRandom < 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 359 | ALOGE("open: %s: %s", randomDevice, strerror(errno)); |
| 360 | return false; |
| 361 | } |
| 362 | return true; |
| 363 | } |
| 364 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 365 | bool generate_random_data(uint8_t* data, size_t size) const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 366 | return (readFully(mRandom, data, size) == size); |
| 367 | } |
| 368 | |
| 369 | private: |
| 370 | int mRandom; |
| 371 | }; |
| 372 | |
| 373 | /* Here is the file format. There are two parts in blob.value, the secret and |
| 374 | * the description. The secret is stored in ciphertext, and its original size |
| 375 | * can be found in blob.length. The description is stored after the secret in |
| 376 | * 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] | 377 | * parts must be no more than VALUE_SIZE bytes. The first field is the version, |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 378 | * the second is the blob's type, and the third byte is flags. Fields other |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 379 | * than blob.info, blob.length, and blob.value are modified by encryptBlob() |
| 380 | * and decryptBlob(). Thus they should not be accessed from outside. */ |
| 381 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 382 | /* ** Note to future implementors of encryption: ** |
| 383 | * Currently this is the construction: |
| 384 | * metadata || Enc(MD5(data) || data) |
| 385 | * |
| 386 | * This should be the construction used for encrypting if re-implementing: |
| 387 | * |
| 388 | * Derive independent keys for encryption and MAC: |
| 389 | * Kenc = AES_encrypt(masterKey, "Encrypt") |
| 390 | * Kmac = AES_encrypt(masterKey, "MAC") |
| 391 | * |
| 392 | * Store this: |
| 393 | * metadata || AES_CTR_encrypt(Kenc, rand_IV, data) || |
| 394 | * HMAC(Kmac, metadata || Enc(data)) |
| 395 | */ |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 396 | struct __attribute__((packed)) blob { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 397 | uint8_t version; |
| 398 | uint8_t type; |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 399 | uint8_t flags; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 400 | uint8_t info; |
| 401 | uint8_t vector[AES_BLOCK_SIZE]; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 402 | uint8_t encrypted[0]; // Marks offset to encrypted data. |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 403 | uint8_t digest[MD5_DIGEST_LENGTH]; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 404 | uint8_t digested[0]; // Marks offset to digested data. |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 405 | int32_t length; // in network byte order when encrypted |
| 406 | uint8_t value[VALUE_SIZE + AES_BLOCK_SIZE]; |
| 407 | }; |
| 408 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 409 | typedef enum { |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 410 | TYPE_ANY = 0, // meta type that matches anything |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 411 | TYPE_GENERIC = 1, |
| 412 | TYPE_MASTER_KEY = 2, |
| 413 | TYPE_KEY_PAIR = 3, |
| 414 | } BlobType; |
| 415 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 416 | static const uint8_t CURRENT_BLOB_VERSION = 2; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 417 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 418 | class Blob { |
| 419 | public: |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 420 | Blob(const uint8_t* value, int32_t valueLength, const uint8_t* info, uint8_t infoLength, |
| 421 | BlobType type) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 422 | mBlob.length = valueLength; |
| 423 | memcpy(mBlob.value, value, valueLength); |
| 424 | |
| 425 | mBlob.info = infoLength; |
| 426 | memcpy(mBlob.value + valueLength, info, infoLength); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 427 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 428 | mBlob.version = CURRENT_BLOB_VERSION; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 429 | mBlob.type = uint8_t(type); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 430 | |
| 431 | mBlob.flags = KEYSTORE_FLAG_NONE; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | Blob(blob b) { |
| 435 | mBlob = b; |
| 436 | } |
| 437 | |
| 438 | Blob() {} |
| 439 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 440 | const uint8_t* getValue() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 441 | return mBlob.value; |
| 442 | } |
| 443 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 444 | int32_t getLength() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 445 | return mBlob.length; |
| 446 | } |
| 447 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 448 | const uint8_t* getInfo() const { |
| 449 | return mBlob.value + mBlob.length; |
| 450 | } |
| 451 | |
| 452 | uint8_t getInfoLength() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 453 | return mBlob.info; |
| 454 | } |
| 455 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 456 | uint8_t getVersion() const { |
| 457 | return mBlob.version; |
| 458 | } |
| 459 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 460 | bool isEncrypted() const { |
| 461 | if (mBlob.version < 2) { |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | return mBlob.flags & KEYSTORE_FLAG_ENCRYPTED; |
| 466 | } |
| 467 | |
| 468 | void setEncrypted(bool encrypted) { |
| 469 | if (encrypted) { |
| 470 | mBlob.flags |= KEYSTORE_FLAG_ENCRYPTED; |
| 471 | } else { |
| 472 | mBlob.flags &= ~KEYSTORE_FLAG_ENCRYPTED; |
| 473 | } |
| 474 | } |
| 475 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 476 | bool isFallback() const { |
| 477 | return mBlob.flags & KEYSTORE_FLAG_FALLBACK; |
| 478 | } |
| 479 | |
| 480 | void setFallback(bool fallback) { |
| 481 | if (fallback) { |
| 482 | mBlob.flags |= KEYSTORE_FLAG_FALLBACK; |
| 483 | } else { |
| 484 | mBlob.flags &= ~KEYSTORE_FLAG_FALLBACK; |
| 485 | } |
| 486 | } |
| 487 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 488 | void setVersion(uint8_t version) { |
| 489 | mBlob.version = version; |
| 490 | } |
| 491 | |
| 492 | BlobType getType() const { |
| 493 | return BlobType(mBlob.type); |
| 494 | } |
| 495 | |
| 496 | void setType(BlobType type) { |
| 497 | mBlob.type = uint8_t(type); |
| 498 | } |
| 499 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 500 | ResponseCode writeBlob(const char* filename, AES_KEY *aes_key, State state, Entropy* entropy) { |
| 501 | ALOGV("writing blob %s", filename); |
| 502 | if (isEncrypted()) { |
| 503 | if (state != STATE_NO_ERROR) { |
| 504 | ALOGD("couldn't insert encrypted blob while not unlocked"); |
| 505 | return LOCKED; |
| 506 | } |
| 507 | |
| 508 | if (!entropy->generate_random_data(mBlob.vector, AES_BLOCK_SIZE)) { |
| 509 | ALOGW("Could not read random data for: %s", filename); |
| 510 | return SYSTEM_ERROR; |
| 511 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // data includes the value and the value's length |
| 515 | size_t dataLength = mBlob.length + sizeof(mBlob.length); |
| 516 | // pad data to the AES_BLOCK_SIZE |
| 517 | size_t digestedLength = ((dataLength + AES_BLOCK_SIZE - 1) |
| 518 | / AES_BLOCK_SIZE * AES_BLOCK_SIZE); |
| 519 | // encrypted data includes the digest value |
| 520 | size_t encryptedLength = digestedLength + MD5_DIGEST_LENGTH; |
| 521 | // move info after space for padding |
| 522 | memmove(&mBlob.encrypted[encryptedLength], &mBlob.value[mBlob.length], mBlob.info); |
| 523 | // zero padding area |
| 524 | memset(mBlob.value + mBlob.length, 0, digestedLength - dataLength); |
| 525 | |
| 526 | mBlob.length = htonl(mBlob.length); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 527 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 528 | if (isEncrypted()) { |
| 529 | MD5(mBlob.digested, digestedLength, mBlob.digest); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 530 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 531 | uint8_t vector[AES_BLOCK_SIZE]; |
| 532 | memcpy(vector, mBlob.vector, AES_BLOCK_SIZE); |
| 533 | AES_cbc_encrypt(mBlob.encrypted, mBlob.encrypted, encryptedLength, |
| 534 | aes_key, vector, AES_ENCRYPT); |
| 535 | } |
| 536 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 537 | size_t headerLength = (mBlob.encrypted - (uint8_t*) &mBlob); |
| 538 | size_t fileLength = encryptedLength + headerLength + mBlob.info; |
| 539 | |
| 540 | const char* tmpFileName = ".tmp"; |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 541 | int out = TEMP_FAILURE_RETRY(open(tmpFileName, |
| 542 | O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)); |
| 543 | if (out < 0) { |
| 544 | ALOGW("could not open file: %s: %s", tmpFileName, strerror(errno)); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 545 | return SYSTEM_ERROR; |
| 546 | } |
| 547 | size_t writtenBytes = writeFully(out, (uint8_t*) &mBlob, fileLength); |
| 548 | if (close(out) != 0) { |
| 549 | return SYSTEM_ERROR; |
| 550 | } |
| 551 | if (writtenBytes != fileLength) { |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 552 | ALOGW("blob not fully written %zu != %zu", writtenBytes, fileLength); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 553 | unlink(tmpFileName); |
| 554 | return SYSTEM_ERROR; |
| 555 | } |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 556 | if (rename(tmpFileName, filename) == -1) { |
| 557 | ALOGW("could not rename blob to %s: %s", filename, strerror(errno)); |
| 558 | return SYSTEM_ERROR; |
| 559 | } |
| 560 | return NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 563 | ResponseCode readBlob(const char* filename, AES_KEY *aes_key, State state) { |
| 564 | ALOGV("reading blob %s", filename); |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 565 | int in = TEMP_FAILURE_RETRY(open(filename, O_RDONLY)); |
| 566 | if (in < 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 567 | return (errno == ENOENT) ? KEY_NOT_FOUND : SYSTEM_ERROR; |
| 568 | } |
| 569 | // fileLength may be less than sizeof(mBlob) since the in |
| 570 | // memory version has extra padding to tolerate rounding up to |
| 571 | // the AES_BLOCK_SIZE |
| 572 | size_t fileLength = readFully(in, (uint8_t*) &mBlob, sizeof(mBlob)); |
| 573 | if (close(in) != 0) { |
| 574 | return SYSTEM_ERROR; |
| 575 | } |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 576 | |
| 577 | if (isEncrypted() && (state != STATE_NO_ERROR)) { |
| 578 | return LOCKED; |
| 579 | } |
| 580 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 581 | size_t headerLength = (mBlob.encrypted - (uint8_t*) &mBlob); |
| 582 | if (fileLength < headerLength) { |
| 583 | return VALUE_CORRUPTED; |
| 584 | } |
| 585 | |
| 586 | ssize_t encryptedLength = fileLength - (headerLength + mBlob.info); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 587 | if (encryptedLength < 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 588 | return VALUE_CORRUPTED; |
| 589 | } |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 590 | |
| 591 | ssize_t digestedLength; |
| 592 | if (isEncrypted()) { |
| 593 | if (encryptedLength % AES_BLOCK_SIZE != 0) { |
| 594 | return VALUE_CORRUPTED; |
| 595 | } |
| 596 | |
| 597 | AES_cbc_encrypt(mBlob.encrypted, mBlob.encrypted, encryptedLength, aes_key, |
| 598 | mBlob.vector, AES_DECRYPT); |
| 599 | digestedLength = encryptedLength - MD5_DIGEST_LENGTH; |
| 600 | uint8_t computedDigest[MD5_DIGEST_LENGTH]; |
| 601 | MD5(mBlob.digested, digestedLength, computedDigest); |
| 602 | if (memcmp(mBlob.digest, computedDigest, MD5_DIGEST_LENGTH) != 0) { |
| 603 | return VALUE_CORRUPTED; |
| 604 | } |
| 605 | } else { |
| 606 | digestedLength = encryptedLength; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | ssize_t maxValueLength = digestedLength - sizeof(mBlob.length); |
| 610 | mBlob.length = ntohl(mBlob.length); |
| 611 | if (mBlob.length < 0 || mBlob.length > maxValueLength) { |
| 612 | return VALUE_CORRUPTED; |
| 613 | } |
| 614 | if (mBlob.info != 0) { |
| 615 | // move info from after padding to after data |
| 616 | memmove(&mBlob.value[mBlob.length], &mBlob.value[maxValueLength], mBlob.info); |
| 617 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 618 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | private: |
| 622 | struct blob mBlob; |
| 623 | }; |
| 624 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 625 | class UserState { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 626 | public: |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 627 | UserState(uid_t userId) : mUserId(userId), mRetry(MAX_RETRY) { |
| 628 | asprintf(&mUserDir, "user_%u", mUserId); |
| 629 | asprintf(&mMasterKeyFile, "%s/.masterkey", mUserDir); |
| 630 | } |
| 631 | |
| 632 | ~UserState() { |
| 633 | free(mUserDir); |
| 634 | free(mMasterKeyFile); |
| 635 | } |
| 636 | |
| 637 | bool initialize() { |
| 638 | if ((mkdir(mUserDir, S_IRUSR | S_IWUSR | S_IXUSR) < 0) && (errno != EEXIST)) { |
| 639 | ALOGE("Could not create directory '%s'", mUserDir); |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | if (access(mMasterKeyFile, R_OK) == 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 644 | setState(STATE_LOCKED); |
| 645 | } else { |
| 646 | setState(STATE_UNINITIALIZED); |
| 647 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 648 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 649 | return true; |
| 650 | } |
| 651 | |
| 652 | uid_t getUserId() const { |
| 653 | return mUserId; |
| 654 | } |
| 655 | |
| 656 | const char* getUserDirName() const { |
| 657 | return mUserDir; |
| 658 | } |
| 659 | |
| 660 | const char* getMasterKeyFileName() const { |
| 661 | return mMasterKeyFile; |
| 662 | } |
| 663 | |
| 664 | void setState(State state) { |
| 665 | mState = state; |
| 666 | if (mState == STATE_NO_ERROR || mState == STATE_UNINITIALIZED) { |
| 667 | mRetry = MAX_RETRY; |
| 668 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 671 | State getState() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 672 | return mState; |
| 673 | } |
| 674 | |
Kenny Root | 5187818 | 2012-03-13 12:53:19 -0700 | [diff] [blame] | 675 | int8_t getRetry() const { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 676 | return mRetry; |
| 677 | } |
| 678 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 679 | void zeroizeMasterKeysInMemory() { |
| 680 | memset(mMasterKey, 0, sizeof(mMasterKey)); |
| 681 | memset(mSalt, 0, sizeof(mSalt)); |
| 682 | memset(&mMasterKeyEncryption, 0, sizeof(mMasterKeyEncryption)); |
| 683 | memset(&mMasterKeyDecryption, 0, sizeof(mMasterKeyDecryption)); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 686 | ResponseCode initialize(const android::String8& pw, Entropy* entropy) { |
| 687 | if (!generateMasterKey(entropy)) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 688 | return SYSTEM_ERROR; |
| 689 | } |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 690 | ResponseCode response = writeMasterKey(pw, entropy); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 691 | if (response != NO_ERROR) { |
| 692 | return response; |
| 693 | } |
| 694 | setupMasterKeys(); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 695 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 698 | ResponseCode writeMasterKey(const android::String8& pw, Entropy* entropy) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 699 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 700 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, mSalt); |
| 701 | AES_KEY passwordAesKey; |
| 702 | AES_set_encrypt_key(passwordKey, MASTER_KEY_SIZE_BITS, &passwordAesKey); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 703 | Blob masterKeyBlob(mMasterKey, sizeof(mMasterKey), mSalt, sizeof(mSalt), TYPE_MASTER_KEY); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 704 | return masterKeyBlob.writeBlob(mMasterKeyFile, &passwordAesKey, STATE_NO_ERROR, entropy); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 707 | ResponseCode readMasterKey(const android::String8& pw, Entropy* entropy) { |
| 708 | int in = TEMP_FAILURE_RETRY(open(mMasterKeyFile, O_RDONLY)); |
Kenny Root | 150ca93 | 2012-11-14 14:29:02 -0800 | [diff] [blame] | 709 | if (in < 0) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 710 | return SYSTEM_ERROR; |
| 711 | } |
| 712 | |
| 713 | // we read the raw blob to just to get the salt to generate |
| 714 | // the AES key, then we create the Blob to use with decryptBlob |
| 715 | blob rawBlob; |
| 716 | size_t length = readFully(in, (uint8_t*) &rawBlob, sizeof(rawBlob)); |
| 717 | if (close(in) != 0) { |
| 718 | return SYSTEM_ERROR; |
| 719 | } |
| 720 | // find salt at EOF if present, otherwise we have an old file |
| 721 | uint8_t* salt; |
| 722 | if (length > SALT_SIZE && rawBlob.info == SALT_SIZE) { |
| 723 | salt = (uint8_t*) &rawBlob + length - SALT_SIZE; |
| 724 | } else { |
| 725 | salt = NULL; |
| 726 | } |
| 727 | uint8_t passwordKey[MASTER_KEY_SIZE_BYTES]; |
| 728 | generateKeyFromPassword(passwordKey, MASTER_KEY_SIZE_BYTES, pw, salt); |
| 729 | AES_KEY passwordAesKey; |
| 730 | AES_set_decrypt_key(passwordKey, MASTER_KEY_SIZE_BITS, &passwordAesKey); |
| 731 | Blob masterKeyBlob(rawBlob); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 732 | ResponseCode response = masterKeyBlob.readBlob(mMasterKeyFile, &passwordAesKey, |
| 733 | STATE_NO_ERROR); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 734 | if (response == SYSTEM_ERROR) { |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 735 | return response; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 736 | } |
| 737 | if (response == NO_ERROR && masterKeyBlob.getLength() == MASTER_KEY_SIZE_BYTES) { |
| 738 | // if salt was missing, generate one and write a new master key file with the salt. |
| 739 | if (salt == NULL) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 740 | if (!generateSalt(entropy)) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 741 | return SYSTEM_ERROR; |
| 742 | } |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 743 | response = writeMasterKey(pw, entropy); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 744 | } |
| 745 | if (response == NO_ERROR) { |
| 746 | memcpy(mMasterKey, masterKeyBlob.getValue(), MASTER_KEY_SIZE_BYTES); |
| 747 | setupMasterKeys(); |
| 748 | } |
| 749 | return response; |
| 750 | } |
| 751 | if (mRetry <= 0) { |
| 752 | reset(); |
| 753 | return UNINITIALIZED; |
| 754 | } |
| 755 | --mRetry; |
| 756 | switch (mRetry) { |
| 757 | case 0: return WRONG_PASSWORD_0; |
| 758 | case 1: return WRONG_PASSWORD_1; |
| 759 | case 2: return WRONG_PASSWORD_2; |
| 760 | case 3: return WRONG_PASSWORD_3; |
| 761 | default: return WRONG_PASSWORD_3; |
| 762 | } |
| 763 | } |
| 764 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 765 | AES_KEY* getEncryptionKey() { |
| 766 | return &mMasterKeyEncryption; |
| 767 | } |
| 768 | |
| 769 | AES_KEY* getDecryptionKey() { |
| 770 | return &mMasterKeyDecryption; |
| 771 | } |
| 772 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 773 | bool reset() { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 774 | DIR* dir = opendir(getUserDirName()); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 775 | if (!dir) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 776 | ALOGW("couldn't open user directory: %s", strerror(errno)); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 777 | return false; |
| 778 | } |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 779 | |
| 780 | struct dirent* file; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 781 | while ((file = readdir(dir)) != NULL) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 782 | // We only care about files. |
| 783 | if (file->d_type != DT_REG) { |
| 784 | continue; |
| 785 | } |
| 786 | |
| 787 | // Skip anything that starts with a "." |
| 788 | if (file->d_name[0] == '.') { |
| 789 | continue; |
| 790 | } |
| 791 | |
| 792 | // Find the current file's UID. |
| 793 | char* end; |
| 794 | unsigned long thisUid = strtoul(file->d_name, &end, 10); |
| 795 | if (end[0] != '_' || end[1] == 0) { |
| 796 | continue; |
| 797 | } |
| 798 | |
| 799 | // Skip if this is not our user. |
| 800 | if (get_user_id(thisUid) != mUserId) { |
| 801 | continue; |
| 802 | } |
| 803 | |
| 804 | unlinkat(dirfd(dir), file->d_name, 0); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 805 | } |
| 806 | closedir(dir); |
| 807 | return true; |
| 808 | } |
| 809 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 810 | private: |
| 811 | static const int MASTER_KEY_SIZE_BYTES = 16; |
| 812 | static const int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8; |
| 813 | |
| 814 | static const int MAX_RETRY = 4; |
| 815 | static const size_t SALT_SIZE = 16; |
| 816 | |
| 817 | void generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw, |
| 818 | uint8_t* salt) { |
| 819 | size_t saltSize; |
| 820 | if (salt != NULL) { |
| 821 | saltSize = SALT_SIZE; |
| 822 | } else { |
| 823 | // pre-gingerbread used this hardwired salt, readMasterKey will rewrite these when found |
| 824 | salt = (uint8_t*) "keystore"; |
| 825 | // sizeof = 9, not strlen = 8 |
| 826 | saltSize = sizeof("keystore"); |
| 827 | } |
| 828 | |
| 829 | PKCS5_PBKDF2_HMAC_SHA1(reinterpret_cast<const char*>(pw.string()), pw.length(), salt, |
| 830 | saltSize, 8192, keySize, key); |
| 831 | } |
| 832 | |
| 833 | bool generateSalt(Entropy* entropy) { |
| 834 | return entropy->generate_random_data(mSalt, sizeof(mSalt)); |
| 835 | } |
| 836 | |
| 837 | bool generateMasterKey(Entropy* entropy) { |
| 838 | if (!entropy->generate_random_data(mMasterKey, sizeof(mMasterKey))) { |
| 839 | return false; |
| 840 | } |
| 841 | if (!generateSalt(entropy)) { |
| 842 | return false; |
| 843 | } |
| 844 | return true; |
| 845 | } |
| 846 | |
| 847 | void setupMasterKeys() { |
| 848 | AES_set_encrypt_key(mMasterKey, MASTER_KEY_SIZE_BITS, &mMasterKeyEncryption); |
| 849 | AES_set_decrypt_key(mMasterKey, MASTER_KEY_SIZE_BITS, &mMasterKeyDecryption); |
| 850 | setState(STATE_NO_ERROR); |
| 851 | } |
| 852 | |
| 853 | uid_t mUserId; |
| 854 | |
| 855 | char* mUserDir; |
| 856 | char* mMasterKeyFile; |
| 857 | |
| 858 | State mState; |
| 859 | int8_t mRetry; |
| 860 | |
| 861 | uint8_t mMasterKey[MASTER_KEY_SIZE_BYTES]; |
| 862 | uint8_t mSalt[SALT_SIZE]; |
| 863 | |
| 864 | AES_KEY mMasterKeyEncryption; |
| 865 | AES_KEY mMasterKeyDecryption; |
| 866 | }; |
| 867 | |
| 868 | typedef struct { |
| 869 | uint32_t uid; |
| 870 | const uint8_t* filename; |
| 871 | } grant_t; |
| 872 | |
| 873 | class KeyStore { |
| 874 | public: |
| 875 | KeyStore(Entropy* entropy, keymaster_device_t* device) |
| 876 | : mEntropy(entropy) |
| 877 | , mDevice(device) |
| 878 | { |
| 879 | memset(&mMetaData, '\0', sizeof(mMetaData)); |
| 880 | } |
| 881 | |
| 882 | ~KeyStore() { |
| 883 | for (android::Vector<grant_t*>::iterator it(mGrants.begin()); |
| 884 | it != mGrants.end(); it++) { |
| 885 | delete *it; |
| 886 | mGrants.erase(it); |
| 887 | } |
| 888 | |
| 889 | for (android::Vector<UserState*>::iterator it(mMasterKeys.begin()); |
| 890 | it != mMasterKeys.end(); it++) { |
| 891 | delete *it; |
| 892 | mMasterKeys.erase(it); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | keymaster_device_t* getDevice() const { |
| 897 | return mDevice; |
| 898 | } |
| 899 | |
| 900 | ResponseCode initialize() { |
| 901 | readMetaData(); |
| 902 | if (upgradeKeystore()) { |
| 903 | writeMetaData(); |
| 904 | } |
| 905 | |
| 906 | return ::NO_ERROR; |
| 907 | } |
| 908 | |
| 909 | State getState(uid_t uid) { |
| 910 | return getUserState(uid)->getState(); |
| 911 | } |
| 912 | |
| 913 | ResponseCode initializeUser(const android::String8& pw, uid_t uid) { |
| 914 | UserState* userState = getUserState(uid); |
| 915 | return userState->initialize(pw, mEntropy); |
| 916 | } |
| 917 | |
| 918 | ResponseCode writeMasterKey(const android::String8& pw, uid_t uid) { |
| 919 | uid_t user_id = get_user_id(uid); |
| 920 | UserState* userState = getUserState(user_id); |
| 921 | return userState->writeMasterKey(pw, mEntropy); |
| 922 | } |
| 923 | |
| 924 | ResponseCode readMasterKey(const android::String8& pw, uid_t uid) { |
| 925 | uid_t user_id = get_user_id(uid); |
| 926 | UserState* userState = getUserState(user_id); |
| 927 | return userState->readMasterKey(pw, mEntropy); |
| 928 | } |
| 929 | |
| 930 | android::String8 getKeyName(const android::String8& keyName) { |
Douglas Leung | a77e809 | 2013-06-13 16:34:43 -0700 | [diff] [blame] | 931 | char encoded[encode_key_length(keyName) + 1]; // add 1 for null char |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 932 | encode_key(encoded, keyName); |
| 933 | return android::String8(encoded); |
| 934 | } |
| 935 | |
| 936 | android::String8 getKeyNameForUid(const android::String8& keyName, uid_t uid) { |
Douglas Leung | a77e809 | 2013-06-13 16:34:43 -0700 | [diff] [blame] | 937 | char encoded[encode_key_length(keyName) + 1]; // add 1 for null char |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 938 | encode_key(encoded, keyName); |
| 939 | return android::String8::format("%u_%s", uid, encoded); |
| 940 | } |
| 941 | |
| 942 | android::String8 getKeyNameForUidWithDir(const android::String8& keyName, uid_t uid) { |
Douglas Leung | a77e809 | 2013-06-13 16:34:43 -0700 | [diff] [blame] | 943 | char encoded[encode_key_length(keyName) + 1]; // add 1 for null char |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 944 | encode_key(encoded, keyName); |
| 945 | return android::String8::format("%s/%u_%s", getUserState(uid)->getUserDirName(), uid, |
| 946 | encoded); |
| 947 | } |
| 948 | |
| 949 | bool reset(uid_t uid) { |
| 950 | UserState* userState = getUserState(uid); |
| 951 | userState->zeroizeMasterKeysInMemory(); |
| 952 | userState->setState(STATE_UNINITIALIZED); |
| 953 | return userState->reset(); |
| 954 | } |
| 955 | |
| 956 | bool isEmpty(uid_t uid) const { |
| 957 | const UserState* userState = getUserState(uid); |
| 958 | if (userState == NULL) { |
| 959 | return true; |
| 960 | } |
| 961 | |
| 962 | DIR* dir = opendir(userState->getUserDirName()); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 963 | struct dirent* file; |
| 964 | if (!dir) { |
| 965 | return true; |
| 966 | } |
| 967 | bool result = true; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 968 | |
| 969 | char filename[NAME_MAX]; |
| 970 | int n = snprintf(filename, sizeof(filename), "%u_", uid); |
| 971 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 972 | while ((file = readdir(dir)) != NULL) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 973 | // We only care about files. |
| 974 | if (file->d_type != DT_REG) { |
| 975 | continue; |
| 976 | } |
| 977 | |
| 978 | // Skip anything that starts with a "." |
| 979 | if (file->d_name[0] == '.') { |
| 980 | continue; |
| 981 | } |
| 982 | |
| 983 | if (!strncmp(file->d_name, filename, n)) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 984 | result = false; |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | closedir(dir); |
| 989 | return result; |
| 990 | } |
| 991 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 992 | void lock(uid_t uid) { |
| 993 | UserState* userState = getUserState(uid); |
| 994 | userState->zeroizeMasterKeysInMemory(); |
| 995 | userState->setState(STATE_LOCKED); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 996 | } |
| 997 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 998 | ResponseCode get(const char* filename, Blob* keyBlob, const BlobType type, uid_t uid) { |
| 999 | UserState* userState = getUserState(uid); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1000 | ResponseCode rc = keyBlob->readBlob(filename, userState->getDecryptionKey(), |
| 1001 | userState->getState()); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1002 | if (rc != NO_ERROR) { |
| 1003 | return rc; |
| 1004 | } |
| 1005 | |
| 1006 | const uint8_t version = keyBlob->getVersion(); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1007 | if (version < CURRENT_BLOB_VERSION) { |
Kenny Root | cfeae07 | 2013-04-04 08:39:57 -0700 | [diff] [blame] | 1008 | /* If we upgrade the key, we need to write it to disk again. Then |
| 1009 | * it must be read it again since the blob is encrypted each time |
| 1010 | * it's written. |
| 1011 | */ |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1012 | if (upgradeBlob(filename, keyBlob, version, type, uid)) { |
| 1013 | if ((rc = this->put(filename, keyBlob, uid)) != NO_ERROR |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1014 | || (rc = keyBlob->readBlob(filename, userState->getDecryptionKey(), |
| 1015 | userState->getState())) != NO_ERROR) { |
Kenny Root | cfeae07 | 2013-04-04 08:39:57 -0700 | [diff] [blame] | 1016 | return rc; |
| 1017 | } |
| 1018 | } |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1021 | /* |
| 1022 | * This will upgrade software-backed keys to hardware-backed keys when |
| 1023 | * the HAL for the device supports the newer key types. |
| 1024 | */ |
| 1025 | if (rc == NO_ERROR && type == TYPE_KEY_PAIR |
| 1026 | && mDevice->common.module->module_api_version >= KEYMASTER_MODULE_API_VERSION_0_2 |
| 1027 | && keyBlob->isFallback()) { |
| 1028 | ResponseCode imported = importKey(keyBlob->getValue(), keyBlob->getLength(), filename, |
| 1029 | uid, keyBlob->isEncrypted() ? KEYSTORE_FLAG_ENCRYPTED : KEYSTORE_FLAG_NONE); |
| 1030 | |
| 1031 | // The HAL allowed the import, reget the key to have the "fresh" |
| 1032 | // version. |
| 1033 | if (imported == NO_ERROR) { |
| 1034 | rc = get(filename, keyBlob, TYPE_KEY_PAIR, uid); |
| 1035 | } |
| 1036 | } |
| 1037 | |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 1038 | if (type != TYPE_ANY && keyBlob->getType() != type) { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1039 | ALOGW("key found but type doesn't match: %d vs %d", keyBlob->getType(), type); |
| 1040 | return KEY_NOT_FOUND; |
| 1041 | } |
| 1042 | |
| 1043 | return rc; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1046 | ResponseCode put(const char* filename, Blob* keyBlob, uid_t uid) { |
| 1047 | UserState* userState = getUserState(uid); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1048 | return keyBlob->writeBlob(filename, userState->getEncryptionKey(), userState->getState(), |
| 1049 | mEntropy); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1052 | void addGrant(const char* filename, uid_t granteeUid) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1053 | const grant_t* existing = getGrant(filename, granteeUid); |
| 1054 | if (existing == NULL) { |
| 1055 | grant_t* grant = new grant_t; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1056 | grant->uid = granteeUid; |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 1057 | grant->filename = reinterpret_cast<const uint8_t*>(strdup(filename)); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1058 | mGrants.add(grant); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1062 | bool removeGrant(const char* filename, uid_t granteeUid) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1063 | for (android::Vector<grant_t*>::iterator it(mGrants.begin()); |
| 1064 | it != mGrants.end(); it++) { |
| 1065 | grant_t* grant = *it; |
| 1066 | if (grant->uid == granteeUid |
| 1067 | && !strcmp(reinterpret_cast<const char*>(grant->filename), filename)) { |
| 1068 | mGrants.erase(it); |
| 1069 | return true; |
| 1070 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1071 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1072 | return false; |
| 1073 | } |
| 1074 | |
Brian Carlstrom | a8c703d | 2012-07-17 14:43:46 -0700 | [diff] [blame] | 1075 | bool hasGrant(const char* filename, const uid_t uid) const { |
| 1076 | return getGrant(filename, uid) != NULL; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1079 | ResponseCode importKey(const uint8_t* key, size_t keyLen, const char* filename, uid_t uid, |
| 1080 | int32_t flags) { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1081 | uint8_t* data; |
| 1082 | size_t dataLength; |
| 1083 | int rc; |
| 1084 | |
| 1085 | if (mDevice->import_keypair == NULL) { |
| 1086 | ALOGE("Keymaster doesn't support import!"); |
| 1087 | return SYSTEM_ERROR; |
| 1088 | } |
| 1089 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1090 | bool isFallback = false; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1091 | rc = mDevice->import_keypair(mDevice, key, keyLen, &data, &dataLength); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1092 | if (rc) { |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1093 | // If this is an old device HAL, try to fall back to an old version |
| 1094 | if (mDevice->common.module->module_api_version < KEYMASTER_MODULE_API_VERSION_0_2) { |
| 1095 | rc = openssl_import_keypair(mDevice, key, keyLen, &data, &dataLength); |
| 1096 | isFallback = true; |
| 1097 | } |
| 1098 | |
| 1099 | if (rc) { |
| 1100 | ALOGE("Error while importing keypair: %d", rc); |
| 1101 | return SYSTEM_ERROR; |
| 1102 | } |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | Blob keyBlob(data, dataLength, NULL, 0, TYPE_KEY_PAIR); |
| 1106 | free(data); |
| 1107 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1108 | keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1109 | keyBlob.setFallback(isFallback); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1110 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1111 | return put(filename, &keyBlob, uid); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Kenny Root | 70f16c1 | 2013-09-05 13:06:32 -0700 | [diff] [blame] | 1114 | bool isHardwareBacked(const android::String16& keyType) const { |
| 1115 | if (mDevice == NULL) { |
| 1116 | ALOGW("can't get keymaster device"); |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
| 1120 | if (sRSAKeyType == keyType) { |
| 1121 | return (mDevice->flags & KEYMASTER_SOFTWARE_ONLY) == 0; |
| 1122 | } else { |
| 1123 | return (mDevice->flags & KEYMASTER_SOFTWARE_ONLY) == 0 |
| 1124 | && (mDevice->common.module->module_api_version |
| 1125 | >= KEYMASTER_MODULE_API_VERSION_0_2); |
| 1126 | } |
Kenny Root | 8ddf35a | 2013-03-29 11:15:50 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1129 | ResponseCode getKeyForName(Blob* keyBlob, const android::String8& keyName, const uid_t uid, |
| 1130 | const BlobType type) { |
| 1131 | char filename[NAME_MAX]; |
| 1132 | encode_key_for_uid(filename, uid, keyName); |
| 1133 | |
| 1134 | UserState* userState = getUserState(uid); |
| 1135 | android::String8 filepath8; |
| 1136 | |
| 1137 | filepath8 = android::String8::format("%s/%s", userState->getUserDirName(), filename); |
| 1138 | if (filepath8.string() == NULL) { |
| 1139 | ALOGW("can't create filepath for key %s", filename); |
| 1140 | return SYSTEM_ERROR; |
| 1141 | } |
| 1142 | |
| 1143 | ResponseCode responseCode = get(filepath8.string(), keyBlob, type, uid); |
| 1144 | if (responseCode == NO_ERROR) { |
| 1145 | return responseCode; |
| 1146 | } |
| 1147 | |
| 1148 | // If this is one of the legacy UID->UID mappings, use it. |
| 1149 | uid_t euid = get_keystore_euid(uid); |
| 1150 | if (euid != uid) { |
| 1151 | encode_key_for_uid(filename, euid, keyName); |
| 1152 | filepath8 = android::String8::format("%s/%s", userState->getUserDirName(), filename); |
| 1153 | responseCode = get(filepath8.string(), keyBlob, type, uid); |
| 1154 | if (responseCode == NO_ERROR) { |
| 1155 | return responseCode; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // They might be using a granted key. |
| 1160 | encode_key(filename, keyName); |
| 1161 | char* end; |
| 1162 | strtoul(filename, &end, 10); |
| 1163 | if (end[0] != '_' || end[1] == 0) { |
| 1164 | return KEY_NOT_FOUND; |
| 1165 | } |
| 1166 | filepath8 = android::String8::format("%s/%s", userState->getUserDirName(), filename); |
| 1167 | if (!hasGrant(filepath8.string(), uid)) { |
| 1168 | return responseCode; |
| 1169 | } |
| 1170 | |
| 1171 | // It is a granted key. Try to load it. |
| 1172 | return get(filepath8.string(), keyBlob, type, uid); |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * Returns any existing UserState or creates it if it doesn't exist. |
| 1177 | */ |
| 1178 | UserState* getUserState(uid_t uid) { |
| 1179 | uid_t userId = get_user_id(uid); |
| 1180 | |
| 1181 | for (android::Vector<UserState*>::iterator it(mMasterKeys.begin()); |
| 1182 | it != mMasterKeys.end(); it++) { |
| 1183 | UserState* state = *it; |
| 1184 | if (state->getUserId() == userId) { |
| 1185 | return state; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | UserState* userState = new UserState(userId); |
| 1190 | if (!userState->initialize()) { |
| 1191 | /* There's not much we can do if initialization fails. Trying to |
| 1192 | * unlock the keystore for that user will fail as well, so any |
| 1193 | * subsequent request for this user will just return SYSTEM_ERROR. |
| 1194 | */ |
| 1195 | ALOGE("User initialization failed for %u; subsuquent operations will fail", userId); |
| 1196 | } |
| 1197 | mMasterKeys.add(userState); |
| 1198 | return userState; |
| 1199 | } |
| 1200 | |
| 1201 | /** |
| 1202 | * Returns NULL if the UserState doesn't already exist. |
| 1203 | */ |
| 1204 | const UserState* getUserState(uid_t uid) const { |
| 1205 | uid_t userId = get_user_id(uid); |
| 1206 | |
| 1207 | for (android::Vector<UserState*>::const_iterator it(mMasterKeys.begin()); |
| 1208 | it != mMasterKeys.end(); it++) { |
| 1209 | UserState* state = *it; |
| 1210 | if (state->getUserId() == userId) { |
| 1211 | return state; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1218 | private: |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1219 | static const char* sOldMasterKey; |
| 1220 | static const char* sMetaDataFile; |
Kenny Root | 70f16c1 | 2013-09-05 13:06:32 -0700 | [diff] [blame] | 1221 | static const android::String16 sRSAKeyType; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1222 | Entropy* mEntropy; |
| 1223 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1224 | keymaster_device_t* mDevice; |
| 1225 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1226 | android::Vector<UserState*> mMasterKeys; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1227 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1228 | android::Vector<grant_t*> mGrants; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1229 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1230 | typedef struct { |
| 1231 | uint32_t version; |
| 1232 | } keystore_metadata_t; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1233 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1234 | keystore_metadata_t mMetaData; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1235 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1236 | const grant_t* getGrant(const char* filename, uid_t uid) const { |
| 1237 | for (android::Vector<grant_t*>::const_iterator it(mGrants.begin()); |
| 1238 | it != mGrants.end(); it++) { |
| 1239 | grant_t* grant = *it; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1240 | if (grant->uid == uid |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1241 | && !strcmp(reinterpret_cast<const char*>(grant->filename), filename)) { |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1242 | return grant; |
| 1243 | } |
| 1244 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1245 | return NULL; |
| 1246 | } |
| 1247 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1248 | /** |
| 1249 | * Upgrade code. This will upgrade the key from the current version |
| 1250 | * to whatever is newest. |
| 1251 | */ |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1252 | bool upgradeBlob(const char* filename, Blob* blob, const uint8_t oldVersion, |
| 1253 | const BlobType type, uid_t uid) { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1254 | bool updated = false; |
| 1255 | uint8_t version = oldVersion; |
| 1256 | |
| 1257 | /* From V0 -> V1: All old types were unknown */ |
| 1258 | if (version == 0) { |
| 1259 | ALOGV("upgrading to version 1 and setting type %d", type); |
| 1260 | |
| 1261 | blob->setType(type); |
| 1262 | if (type == TYPE_KEY_PAIR) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1263 | importBlobAsKey(blob, filename, uid); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1264 | } |
| 1265 | version = 1; |
| 1266 | updated = true; |
| 1267 | } |
| 1268 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1269 | /* From V1 -> V2: All old keys were encrypted */ |
| 1270 | if (version == 1) { |
| 1271 | ALOGV("upgrading to version 2"); |
| 1272 | |
| 1273 | blob->setEncrypted(true); |
| 1274 | version = 2; |
| 1275 | updated = true; |
| 1276 | } |
| 1277 | |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1278 | /* |
| 1279 | * If we've updated, set the key blob to the right version |
| 1280 | * and write it. |
Kenny Root | cfeae07 | 2013-04-04 08:39:57 -0700 | [diff] [blame] | 1281 | */ |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1282 | if (updated) { |
| 1283 | ALOGV("updated and writing file %s", filename); |
| 1284 | blob->setVersion(version); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1285 | } |
Kenny Root | cfeae07 | 2013-04-04 08:39:57 -0700 | [diff] [blame] | 1286 | |
| 1287 | return updated; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | /** |
| 1291 | * Takes a blob that is an PEM-encoded RSA key as a byte array and |
| 1292 | * converts it to a DER-encoded PKCS#8 for import into a keymaster. |
| 1293 | * Then it overwrites the original blob with the new blob |
| 1294 | * format that is returned from the keymaster. |
| 1295 | */ |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1296 | ResponseCode importBlobAsKey(Blob* blob, const char* filename, uid_t uid) { |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1297 | // We won't even write to the blob directly with this BIO, so const_cast is okay. |
| 1298 | Unique_BIO b(BIO_new_mem_buf(const_cast<uint8_t*>(blob->getValue()), blob->getLength())); |
| 1299 | if (b.get() == NULL) { |
| 1300 | ALOGE("Problem instantiating BIO"); |
| 1301 | return SYSTEM_ERROR; |
| 1302 | } |
| 1303 | |
| 1304 | Unique_EVP_PKEY pkey(PEM_read_bio_PrivateKey(b.get(), NULL, NULL, NULL)); |
| 1305 | if (pkey.get() == NULL) { |
| 1306 | ALOGE("Couldn't read old PEM file"); |
| 1307 | return SYSTEM_ERROR; |
| 1308 | } |
| 1309 | |
| 1310 | Unique_PKCS8_PRIV_KEY_INFO pkcs8(EVP_PKEY2PKCS8(pkey.get())); |
| 1311 | int len = i2d_PKCS8_PRIV_KEY_INFO(pkcs8.get(), NULL); |
| 1312 | if (len < 0) { |
| 1313 | ALOGE("Couldn't measure PKCS#8 length"); |
| 1314 | return SYSTEM_ERROR; |
| 1315 | } |
| 1316 | |
Kenny Root | 70c9889 | 2013-02-07 09:10:36 -0800 | [diff] [blame] | 1317 | UniquePtr<unsigned char[]> pkcs8key(new unsigned char[len]); |
| 1318 | uint8_t* tmp = pkcs8key.get(); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1319 | if (i2d_PKCS8_PRIV_KEY_INFO(pkcs8.get(), &tmp) != len) { |
| 1320 | ALOGE("Couldn't convert to PKCS#8"); |
| 1321 | return SYSTEM_ERROR; |
| 1322 | } |
| 1323 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1324 | ResponseCode rc = importKey(pkcs8key.get(), len, filename, uid, |
| 1325 | blob->isEncrypted() ? KEYSTORE_FLAG_ENCRYPTED : KEYSTORE_FLAG_NONE); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1326 | if (rc != NO_ERROR) { |
| 1327 | return rc; |
| 1328 | } |
| 1329 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1330 | return get(filename, blob, TYPE_KEY_PAIR, uid); |
| 1331 | } |
| 1332 | |
| 1333 | void readMetaData() { |
| 1334 | int in = TEMP_FAILURE_RETRY(open(sMetaDataFile, O_RDONLY)); |
| 1335 | if (in < 0) { |
| 1336 | return; |
| 1337 | } |
| 1338 | size_t fileLength = readFully(in, (uint8_t*) &mMetaData, sizeof(mMetaData)); |
| 1339 | if (fileLength != sizeof(mMetaData)) { |
| 1340 | ALOGI("Metadata file is %zd bytes (%zd experted); upgrade?", fileLength, |
| 1341 | sizeof(mMetaData)); |
| 1342 | } |
| 1343 | close(in); |
| 1344 | } |
| 1345 | |
| 1346 | void writeMetaData() { |
| 1347 | const char* tmpFileName = ".metadata.tmp"; |
| 1348 | int out = TEMP_FAILURE_RETRY(open(tmpFileName, |
| 1349 | O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)); |
| 1350 | if (out < 0) { |
| 1351 | ALOGE("couldn't write metadata file: %s", strerror(errno)); |
| 1352 | return; |
| 1353 | } |
| 1354 | size_t fileLength = writeFully(out, (uint8_t*) &mMetaData, sizeof(mMetaData)); |
| 1355 | if (fileLength != sizeof(mMetaData)) { |
| 1356 | ALOGI("Could only write %zd bytes to metadata file (%zd expected)", fileLength, |
| 1357 | sizeof(mMetaData)); |
| 1358 | } |
| 1359 | close(out); |
| 1360 | rename(tmpFileName, sMetaDataFile); |
| 1361 | } |
| 1362 | |
| 1363 | bool upgradeKeystore() { |
| 1364 | bool upgraded = false; |
| 1365 | |
| 1366 | if (mMetaData.version == 0) { |
| 1367 | UserState* userState = getUserState(0); |
| 1368 | |
| 1369 | // Initialize first so the directory is made. |
| 1370 | userState->initialize(); |
| 1371 | |
| 1372 | // Migrate the old .masterkey file to user 0. |
| 1373 | if (access(sOldMasterKey, R_OK) == 0) { |
| 1374 | if (rename(sOldMasterKey, userState->getMasterKeyFileName()) < 0) { |
| 1375 | ALOGE("couldn't migrate old masterkey: %s", strerror(errno)); |
| 1376 | return false; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | // Initialize again in case we had a key. |
| 1381 | userState->initialize(); |
| 1382 | |
| 1383 | // Try to migrate existing keys. |
| 1384 | DIR* dir = opendir("."); |
| 1385 | if (!dir) { |
| 1386 | // Give up now; maybe we can upgrade later. |
| 1387 | ALOGE("couldn't open keystore's directory; something is wrong"); |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
| 1391 | struct dirent* file; |
| 1392 | while ((file = readdir(dir)) != NULL) { |
| 1393 | // We only care about files. |
| 1394 | if (file->d_type != DT_REG) { |
| 1395 | continue; |
| 1396 | } |
| 1397 | |
| 1398 | // Skip anything that starts with a "." |
| 1399 | if (file->d_name[0] == '.') { |
| 1400 | continue; |
| 1401 | } |
| 1402 | |
| 1403 | // Find the current file's user. |
| 1404 | char* end; |
| 1405 | unsigned long thisUid = strtoul(file->d_name, &end, 10); |
| 1406 | if (end[0] != '_' || end[1] == 0) { |
| 1407 | continue; |
| 1408 | } |
| 1409 | UserState* otherUser = getUserState(thisUid); |
| 1410 | if (otherUser->getUserId() != 0) { |
| 1411 | unlinkat(dirfd(dir), file->d_name, 0); |
| 1412 | } |
| 1413 | |
| 1414 | // Rename the file into user directory. |
| 1415 | DIR* otherdir = opendir(otherUser->getUserDirName()); |
| 1416 | if (otherdir == NULL) { |
| 1417 | ALOGW("couldn't open user directory for rename"); |
| 1418 | continue; |
| 1419 | } |
| 1420 | if (renameat(dirfd(dir), file->d_name, dirfd(otherdir), file->d_name) < 0) { |
| 1421 | ALOGW("couldn't rename blob: %s: %s", file->d_name, strerror(errno)); |
| 1422 | } |
| 1423 | closedir(otherdir); |
| 1424 | } |
| 1425 | closedir(dir); |
| 1426 | |
| 1427 | mMetaData.version = 1; |
| 1428 | upgraded = true; |
| 1429 | } |
| 1430 | |
| 1431 | return upgraded; |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1432 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1433 | }; |
| 1434 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1435 | const char* KeyStore::sOldMasterKey = ".masterkey"; |
| 1436 | const char* KeyStore::sMetaDataFile = ".metadata"; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1437 | |
Kenny Root | 70f16c1 | 2013-09-05 13:06:32 -0700 | [diff] [blame] | 1438 | const android::String16 KeyStore::sRSAKeyType("RSA"); |
| 1439 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1440 | namespace android { |
| 1441 | class KeyStoreProxy : public BnKeystoreService, public IBinder::DeathRecipient { |
| 1442 | public: |
| 1443 | KeyStoreProxy(KeyStore* keyStore) |
| 1444 | : mKeyStore(keyStore) |
| 1445 | { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1446 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1447 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1448 | void binderDied(const wp<IBinder>&) { |
| 1449 | ALOGE("binder death detected"); |
Kenny Root | 822c3a9 | 2012-03-23 16:34:39 -0700 | [diff] [blame] | 1450 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1451 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1452 | int32_t test() { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1453 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1454 | if (!has_permission(callingUid, P_TEST)) { |
| 1455 | ALOGW("permission denied for %d: test", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1456 | return ::PERMISSION_DENIED; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1457 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1458 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1459 | return mKeyStore->getState(callingUid); |
Kenny Root | 298e7b1 | 2012-03-26 13:54:44 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1462 | int32_t get(const String16& name, uint8_t** item, size_t* itemLength) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1463 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1464 | if (!has_permission(callingUid, P_GET)) { |
| 1465 | ALOGW("permission denied for %d: get", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1466 | return ::PERMISSION_DENIED; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1467 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1468 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1469 | String8 name8(name); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1470 | Blob keyBlob; |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1471 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1472 | ResponseCode responseCode = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1473 | TYPE_GENERIC); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1474 | if (responseCode != ::NO_ERROR) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1475 | ALOGW("Could not read %s", name8.string()); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1476 | *item = NULL; |
| 1477 | *itemLength = 0; |
| 1478 | return responseCode; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1479 | } |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1480 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1481 | *item = (uint8_t*) malloc(keyBlob.getLength()); |
| 1482 | memcpy(*item, keyBlob.getValue(), keyBlob.getLength()); |
| 1483 | *itemLength = keyBlob.getLength(); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1484 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1485 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1488 | int32_t insert(const String16& name, const uint8_t* item, size_t itemLength, int targetUid, |
| 1489 | int32_t flags) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1490 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1491 | if (!has_permission(callingUid, P_INSERT)) { |
| 1492 | ALOGW("permission denied for %d: insert", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1493 | return ::PERMISSION_DENIED; |
| 1494 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1495 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1496 | State state = mKeyStore->getState(callingUid); |
| 1497 | if ((flags & KEYSTORE_FLAG_ENCRYPTED) && !isKeystoreUnlocked(state)) { |
| 1498 | ALOGD("calling get in state: %d", state); |
| 1499 | return state; |
| 1500 | } |
| 1501 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1502 | if (targetUid == -1) { |
| 1503 | targetUid = callingUid; |
| 1504 | } else if (!is_granted_to(callingUid, targetUid)) { |
Kenny Root | b88c3eb | 2013-02-13 14:43:43 -0800 | [diff] [blame] | 1505 | return ::PERMISSION_DENIED; |
| 1506 | } |
| 1507 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1508 | String8 name8(name); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1509 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1510 | |
| 1511 | Blob keyBlob(item, itemLength, NULL, 0, ::TYPE_GENERIC); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1512 | return mKeyStore->put(filename.string(), &keyBlob, callingUid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1513 | } |
| 1514 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1515 | int32_t del(const String16& name, int targetUid) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1516 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1517 | if (!has_permission(callingUid, P_DELETE)) { |
| 1518 | ALOGW("permission denied for %d: del", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1519 | return ::PERMISSION_DENIED; |
| 1520 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1521 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1522 | if (targetUid == -1) { |
| 1523 | targetUid = callingUid; |
| 1524 | } else if (!is_granted_to(callingUid, targetUid)) { |
Kenny Root | b88c3eb | 2013-02-13 14:43:43 -0800 | [diff] [blame] | 1525 | return ::PERMISSION_DENIED; |
| 1526 | } |
| 1527 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1528 | String8 name8(name); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1529 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1530 | |
| 1531 | Blob keyBlob; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1532 | ResponseCode responseCode = mKeyStore->get(filename.string(), &keyBlob, TYPE_GENERIC, |
| 1533 | callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1534 | if (responseCode != ::NO_ERROR) { |
| 1535 | return responseCode; |
| 1536 | } |
| 1537 | return (unlink(filename) && errno != ENOENT) ? ::SYSTEM_ERROR : ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1540 | int32_t exist(const String16& name, int targetUid) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1541 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1542 | if (!has_permission(callingUid, P_EXIST)) { |
| 1543 | ALOGW("permission denied for %d: exist", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1544 | return ::PERMISSION_DENIED; |
| 1545 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1546 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1547 | if (targetUid == -1) { |
| 1548 | targetUid = callingUid; |
| 1549 | } else if (!is_granted_to(callingUid, targetUid)) { |
Kenny Root | b88c3eb | 2013-02-13 14:43:43 -0800 | [diff] [blame] | 1550 | return ::PERMISSION_DENIED; |
| 1551 | } |
| 1552 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1553 | String8 name8(name); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1554 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid)); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1555 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1556 | if (access(filename.string(), R_OK) == -1) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1557 | return (errno != ENOENT) ? ::SYSTEM_ERROR : ::KEY_NOT_FOUND; |
| 1558 | } |
| 1559 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1562 | int32_t saw(const String16& prefix, int targetUid, Vector<String16>* matches) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1563 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1564 | if (!has_permission(callingUid, P_SAW)) { |
| 1565 | ALOGW("permission denied for %d: saw", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1566 | return ::PERMISSION_DENIED; |
| 1567 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1568 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1569 | if (targetUid == -1) { |
| 1570 | targetUid = callingUid; |
| 1571 | } else if (!is_granted_to(callingUid, targetUid)) { |
Kenny Root | b88c3eb | 2013-02-13 14:43:43 -0800 | [diff] [blame] | 1572 | return ::PERMISSION_DENIED; |
| 1573 | } |
| 1574 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1575 | UserState* userState = mKeyStore->getUserState(targetUid); |
| 1576 | DIR* dir = opendir(userState->getUserDirName()); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1577 | if (!dir) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1578 | ALOGW("can't open directory for user: %s", strerror(errno)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1579 | return ::SYSTEM_ERROR; |
| 1580 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1581 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1582 | const String8 prefix8(prefix); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1583 | String8 filename(mKeyStore->getKeyNameForUid(prefix8, targetUid)); |
| 1584 | size_t n = filename.length(); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1585 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1586 | struct dirent* file; |
| 1587 | while ((file = readdir(dir)) != NULL) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1588 | // We only care about files. |
| 1589 | if (file->d_type != DT_REG) { |
| 1590 | continue; |
| 1591 | } |
| 1592 | |
| 1593 | // Skip anything that starts with a "." |
| 1594 | if (file->d_name[0] == '.') { |
| 1595 | continue; |
| 1596 | } |
| 1597 | |
| 1598 | if (!strncmp(filename.string(), file->d_name, n)) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1599 | const char* p = &file->d_name[n]; |
| 1600 | size_t plen = strlen(p); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1601 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1602 | size_t extra = decode_key_length(p, plen); |
| 1603 | char *match = (char*) malloc(extra + 1); |
| 1604 | if (match != NULL) { |
| 1605 | decode_key(match, p, plen); |
| 1606 | matches->push(String16(match, extra)); |
| 1607 | free(match); |
| 1608 | } else { |
| 1609 | ALOGW("could not allocate match of size %zd", extra); |
| 1610 | } |
Kenny Root | 9a53d3e | 2012-08-14 10:47:54 -0700 | [diff] [blame] | 1611 | } |
| 1612 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1613 | closedir(dir); |
| 1614 | |
| 1615 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1618 | int32_t reset() { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1619 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1620 | if (!has_permission(callingUid, P_RESET)) { |
| 1621 | ALOGW("permission denied for %d: reset", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1622 | return ::PERMISSION_DENIED; |
| 1623 | } |
| 1624 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1625 | ResponseCode rc = mKeyStore->reset(callingUid) ? ::NO_ERROR : ::SYSTEM_ERROR; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1626 | |
| 1627 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1628 | if (device == NULL) { |
| 1629 | ALOGE("No keymaster device!"); |
| 1630 | return ::SYSTEM_ERROR; |
| 1631 | } |
| 1632 | |
| 1633 | if (device->delete_all == NULL) { |
| 1634 | ALOGV("keymaster device doesn't implement delete_all"); |
| 1635 | return rc; |
| 1636 | } |
| 1637 | |
| 1638 | if (device->delete_all(device)) { |
| 1639 | ALOGE("Problem calling keymaster's delete_all"); |
| 1640 | return ::SYSTEM_ERROR; |
| 1641 | } |
| 1642 | |
Kenny Root | 9a53d3e | 2012-08-14 10:47:54 -0700 | [diff] [blame] | 1643 | return rc; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1646 | /* |
| 1647 | * Here is the history. To improve the security, the parameters to generate the |
| 1648 | * master key has been changed. To make a seamless transition, we update the |
| 1649 | * file using the same password when the user unlock it for the first time. If |
| 1650 | * any thing goes wrong during the transition, the new file will not overwrite |
| 1651 | * the old one. This avoids permanent damages of the existing data. |
| 1652 | */ |
| 1653 | int32_t password(const String16& password) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1654 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1655 | if (!has_permission(callingUid, P_PASSWORD)) { |
| 1656 | ALOGW("permission denied for %d: password", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1657 | return ::PERMISSION_DENIED; |
| 1658 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1659 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1660 | const String8 password8(password); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1661 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1662 | switch (mKeyStore->getState(callingUid)) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1663 | case ::STATE_UNINITIALIZED: { |
| 1664 | // generate master key, encrypt with password, write to file, initialize mMasterKey*. |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1665 | return mKeyStore->initializeUser(password8, callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1666 | } |
| 1667 | case ::STATE_NO_ERROR: { |
| 1668 | // rewrite master key with new password. |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1669 | return mKeyStore->writeMasterKey(password8, callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1670 | } |
| 1671 | case ::STATE_LOCKED: { |
| 1672 | // read master key, decrypt with password, initialize mMasterKey*. |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1673 | return mKeyStore->readMasterKey(password8, callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | return ::SYSTEM_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1679 | int32_t lock() { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1680 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1681 | if (!has_permission(callingUid, P_LOCK)) { |
| 1682 | ALOGW("permission denied for %d: lock", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1683 | return ::PERMISSION_DENIED; |
| 1684 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1685 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1686 | State state = mKeyStore->getState(callingUid); |
Kenny Root | 9d45d1c | 2013-02-14 10:32:30 -0800 | [diff] [blame] | 1687 | if (state != ::STATE_NO_ERROR) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1688 | ALOGD("calling lock in state: %d", state); |
| 1689 | return state; |
| 1690 | } |
| 1691 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1692 | mKeyStore->lock(callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1693 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1696 | int32_t unlock(const String16& pw) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1697 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1698 | if (!has_permission(callingUid, P_UNLOCK)) { |
| 1699 | ALOGW("permission denied for %d: unlock", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1700 | return ::PERMISSION_DENIED; |
| 1701 | } |
| 1702 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1703 | State state = mKeyStore->getState(callingUid); |
Kenny Root | 9d45d1c | 2013-02-14 10:32:30 -0800 | [diff] [blame] | 1704 | if (state != ::STATE_LOCKED) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1705 | ALOGD("calling unlock when not locked"); |
| 1706 | return state; |
| 1707 | } |
| 1708 | |
| 1709 | const String8 password8(pw); |
| 1710 | return password(pw); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1713 | int32_t zero() { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1714 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1715 | if (!has_permission(callingUid, P_ZERO)) { |
| 1716 | ALOGW("permission denied for %d: zero", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1717 | return -1; |
| 1718 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1719 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1720 | return mKeyStore->isEmpty(callingUid) ? ::KEY_NOT_FOUND : ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
Kenny Root | 6071179 | 2013-08-16 14:02:41 -0700 | [diff] [blame] | 1723 | int32_t generate(const String16& name, int32_t targetUid, int32_t keyType, int32_t keySize, |
| 1724 | int32_t flags, Vector<sp<KeystoreArg> >* args) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1725 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1726 | if (!has_permission(callingUid, P_INSERT)) { |
| 1727 | ALOGW("permission denied for %d: generate", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1728 | return ::PERMISSION_DENIED; |
| 1729 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1730 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1731 | if (targetUid == -1) { |
| 1732 | targetUid = callingUid; |
| 1733 | } else if (!is_granted_to(callingUid, targetUid)) { |
Kenny Root | b88c3eb | 2013-02-13 14:43:43 -0800 | [diff] [blame] | 1734 | return ::PERMISSION_DENIED; |
| 1735 | } |
| 1736 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1737 | State state = mKeyStore->getState(callingUid); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1738 | if ((flags & KEYSTORE_FLAG_ENCRYPTED) && !isKeystoreUnlocked(state)) { |
| 1739 | ALOGW("calling generate in state: %d", state); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1740 | return state; |
| 1741 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1742 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1743 | uint8_t* data; |
| 1744 | size_t dataLength; |
| 1745 | int rc; |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1746 | bool isFallback = false; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1747 | |
| 1748 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1749 | if (device == NULL) { |
| 1750 | return ::SYSTEM_ERROR; |
| 1751 | } |
| 1752 | |
| 1753 | if (device->generate_keypair == NULL) { |
| 1754 | return ::SYSTEM_ERROR; |
| 1755 | } |
| 1756 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1757 | if (keyType == EVP_PKEY_DSA) { |
Kenny Root | 6071179 | 2013-08-16 14:02:41 -0700 | [diff] [blame] | 1758 | keymaster_dsa_keygen_params_t dsa_params; |
| 1759 | memset(&dsa_params, '\0', sizeof(dsa_params)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1760 | |
Kenny Root | 6071179 | 2013-08-16 14:02:41 -0700 | [diff] [blame] | 1761 | if (keySize == -1) { |
| 1762 | keySize = DSA_DEFAULT_KEY_SIZE; |
| 1763 | } else if ((keySize % 64) != 0 || keySize < DSA_MIN_KEY_SIZE |
| 1764 | || keySize > DSA_MAX_KEY_SIZE) { |
| 1765 | ALOGI("invalid key size %d", keySize); |
| 1766 | return ::SYSTEM_ERROR; |
| 1767 | } |
| 1768 | dsa_params.key_size = keySize; |
| 1769 | |
| 1770 | if (args->size() == 3) { |
| 1771 | sp<KeystoreArg> gArg = args->itemAt(0); |
| 1772 | sp<KeystoreArg> pArg = args->itemAt(1); |
| 1773 | sp<KeystoreArg> qArg = args->itemAt(2); |
| 1774 | |
| 1775 | if (gArg != NULL && pArg != NULL && qArg != NULL) { |
| 1776 | dsa_params.generator = reinterpret_cast<const uint8_t*>(gArg->data()); |
| 1777 | dsa_params.generator_len = gArg->size(); |
| 1778 | |
| 1779 | dsa_params.prime_p = reinterpret_cast<const uint8_t*>(pArg->data()); |
| 1780 | dsa_params.prime_p_len = pArg->size(); |
| 1781 | |
| 1782 | dsa_params.prime_q = reinterpret_cast<const uint8_t*>(qArg->data()); |
| 1783 | dsa_params.prime_q_len = qArg->size(); |
| 1784 | } else { |
| 1785 | ALOGI("not all DSA parameters were read"); |
| 1786 | return ::SYSTEM_ERROR; |
| 1787 | } |
| 1788 | } else if (args->size() != 0) { |
| 1789 | ALOGI("DSA args must be 3"); |
| 1790 | return ::SYSTEM_ERROR; |
| 1791 | } |
| 1792 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1793 | if (device->common.module->module_api_version >= KEYMASTER_MODULE_API_VERSION_0_2) { |
| 1794 | rc = device->generate_keypair(device, TYPE_DSA, &dsa_params, &data, &dataLength); |
| 1795 | } else { |
| 1796 | isFallback = true; |
| 1797 | rc = openssl_generate_keypair(device, TYPE_DSA, &dsa_params, &data, &dataLength); |
| 1798 | } |
| 1799 | } else if (keyType == EVP_PKEY_EC) { |
Kenny Root | 6071179 | 2013-08-16 14:02:41 -0700 | [diff] [blame] | 1800 | keymaster_ec_keygen_params_t ec_params; |
| 1801 | memset(&ec_params, '\0', sizeof(ec_params)); |
| 1802 | |
| 1803 | if (keySize == -1) { |
| 1804 | keySize = EC_DEFAULT_KEY_SIZE; |
| 1805 | } else if (keySize < EC_MIN_KEY_SIZE || keySize > EC_MAX_KEY_SIZE) { |
| 1806 | ALOGI("invalid key size %d", keySize); |
| 1807 | return ::SYSTEM_ERROR; |
| 1808 | } |
| 1809 | ec_params.field_size = keySize; |
| 1810 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1811 | if (device->common.module->module_api_version >= KEYMASTER_MODULE_API_VERSION_0_2) { |
| 1812 | rc = device->generate_keypair(device, TYPE_EC, &ec_params, &data, &dataLength); |
| 1813 | } else { |
| 1814 | isFallback = true; |
| 1815 | rc = openssl_generate_keypair(device, TYPE_EC, &ec_params, &data, &dataLength); |
| 1816 | } |
Kenny Root | 6071179 | 2013-08-16 14:02:41 -0700 | [diff] [blame] | 1817 | } else if (keyType == EVP_PKEY_RSA) { |
| 1818 | keymaster_rsa_keygen_params_t rsa_params; |
| 1819 | memset(&rsa_params, '\0', sizeof(rsa_params)); |
| 1820 | rsa_params.public_exponent = RSA_DEFAULT_EXPONENT; |
| 1821 | |
| 1822 | if (keySize == -1) { |
| 1823 | keySize = RSA_DEFAULT_KEY_SIZE; |
| 1824 | } else if (keySize < RSA_MIN_KEY_SIZE || keySize > RSA_MAX_KEY_SIZE) { |
| 1825 | ALOGI("invalid key size %d", keySize); |
| 1826 | return ::SYSTEM_ERROR; |
| 1827 | } |
| 1828 | rsa_params.modulus_size = keySize; |
| 1829 | |
| 1830 | if (args->size() > 1) { |
| 1831 | ALOGI("invalid number of arguments: %d", args->size()); |
| 1832 | return ::SYSTEM_ERROR; |
| 1833 | } else if (args->size() == 1) { |
| 1834 | sp<KeystoreArg> pubExpBlob = args->itemAt(0); |
| 1835 | if (pubExpBlob != NULL) { |
| 1836 | Unique_BIGNUM pubExpBn( |
| 1837 | BN_bin2bn(reinterpret_cast<const unsigned char*>(pubExpBlob->data()), |
| 1838 | pubExpBlob->size(), NULL)); |
| 1839 | if (pubExpBn.get() == NULL) { |
| 1840 | ALOGI("Could not convert public exponent to BN"); |
| 1841 | return ::SYSTEM_ERROR; |
| 1842 | } |
| 1843 | unsigned long pubExp = BN_get_word(pubExpBn.get()); |
| 1844 | if (pubExp == 0xFFFFFFFFL) { |
| 1845 | ALOGI("cannot represent public exponent as a long value"); |
| 1846 | return ::SYSTEM_ERROR; |
| 1847 | } |
| 1848 | rsa_params.public_exponent = pubExp; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | rc = device->generate_keypair(device, TYPE_RSA, &rsa_params, &data, &dataLength); |
| 1853 | } else { |
| 1854 | ALOGW("Unsupported key type %d", keyType); |
| 1855 | rc = -1; |
| 1856 | } |
| 1857 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1858 | if (rc) { |
| 1859 | return ::SYSTEM_ERROR; |
| 1860 | } |
| 1861 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1862 | String8 name8(name); |
| 1863 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1864 | |
| 1865 | Blob keyBlob(data, dataLength, NULL, 0, TYPE_KEY_PAIR); |
| 1866 | free(data); |
| 1867 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1868 | keyBlob.setFallback(isFallback); |
| 1869 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1870 | return mKeyStore->put(filename.string(), &keyBlob, callingUid); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1873 | int32_t import(const String16& name, const uint8_t* data, size_t length, int targetUid, |
| 1874 | int32_t flags) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1875 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1876 | if (!has_permission(callingUid, P_INSERT)) { |
| 1877 | ALOGW("permission denied for %d: import", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1878 | return ::PERMISSION_DENIED; |
| 1879 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1880 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1881 | if (targetUid == -1) { |
| 1882 | targetUid = callingUid; |
| 1883 | } else if (!is_granted_to(callingUid, targetUid)) { |
Kenny Root | b88c3eb | 2013-02-13 14:43:43 -0800 | [diff] [blame] | 1884 | return ::PERMISSION_DENIED; |
| 1885 | } |
| 1886 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1887 | State state = mKeyStore->getState(callingUid); |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1888 | if ((flags & KEYSTORE_FLAG_ENCRYPTED) && !isKeystoreUnlocked(state)) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1889 | ALOGD("calling import in state: %d", state); |
| 1890 | return state; |
| 1891 | } |
| 1892 | |
| 1893 | String8 name8(name); |
Kenny Root | 6089889 | 2013-04-16 18:08:03 -0700 | [diff] [blame] | 1894 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1895 | |
Kenny Root | f9119d6 | 2013-04-03 09:22:15 -0700 | [diff] [blame] | 1896 | return mKeyStore->importKey(data, length, filename.string(), callingUid, flags); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1899 | int32_t sign(const String16& name, const uint8_t* data, size_t length, uint8_t** out, |
| 1900 | size_t* outLength) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1901 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1902 | if (!has_permission(callingUid, P_SIGN)) { |
| 1903 | ALOGW("permission denied for %d: saw", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1904 | return ::PERMISSION_DENIED; |
| 1905 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1906 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1907 | Blob keyBlob; |
| 1908 | String8 name8(name); |
| 1909 | |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1910 | ALOGV("sign %s from uid %d", name8.string(), callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1911 | int rc; |
| 1912 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1913 | ResponseCode responseCode = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1914 | ::TYPE_KEY_PAIR); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1915 | if (responseCode != ::NO_ERROR) { |
| 1916 | return responseCode; |
| 1917 | } |
| 1918 | |
| 1919 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1920 | if (device == NULL) { |
| 1921 | ALOGE("no keymaster device; cannot sign"); |
| 1922 | return ::SYSTEM_ERROR; |
| 1923 | } |
| 1924 | |
| 1925 | if (device->sign_data == NULL) { |
| 1926 | ALOGE("device doesn't implement signing"); |
| 1927 | return ::SYSTEM_ERROR; |
| 1928 | } |
| 1929 | |
| 1930 | keymaster_rsa_sign_params_t params; |
| 1931 | params.digest_type = DIGEST_NONE; |
| 1932 | params.padding_type = PADDING_NONE; |
| 1933 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1934 | if (keyBlob.isFallback()) { |
| 1935 | rc = openssl_sign_data(device, ¶ms, keyBlob.getValue(), keyBlob.getLength(), data, |
| 1936 | length, out, outLength); |
| 1937 | } else { |
| 1938 | rc = device->sign_data(device, ¶ms, keyBlob.getValue(), keyBlob.getLength(), data, |
| 1939 | length, out, outLength); |
| 1940 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1941 | if (rc) { |
| 1942 | ALOGW("device couldn't sign data"); |
| 1943 | return ::SYSTEM_ERROR; |
| 1944 | } |
| 1945 | |
| 1946 | return ::NO_ERROR; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1947 | } |
| 1948 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1949 | int32_t verify(const String16& name, const uint8_t* data, size_t dataLength, |
| 1950 | const uint8_t* signature, size_t signatureLength) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 1951 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1952 | if (!has_permission(callingUid, P_VERIFY)) { |
| 1953 | ALOGW("permission denied for %d: verify", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1954 | return ::PERMISSION_DENIED; |
| 1955 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1956 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1957 | State state = mKeyStore->getState(callingUid); |
Kenny Root | 9d45d1c | 2013-02-14 10:32:30 -0800 | [diff] [blame] | 1958 | if (!isKeystoreUnlocked(state)) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1959 | ALOGD("calling verify in state: %d", state); |
| 1960 | return state; |
| 1961 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1962 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1963 | Blob keyBlob; |
| 1964 | String8 name8(name); |
| 1965 | int rc; |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1966 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 1967 | ResponseCode responseCode = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 1968 | TYPE_KEY_PAIR); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1969 | if (responseCode != ::NO_ERROR) { |
| 1970 | return responseCode; |
| 1971 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1972 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1973 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 1974 | if (device == NULL) { |
| 1975 | return ::SYSTEM_ERROR; |
| 1976 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1977 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1978 | if (device->verify_data == NULL) { |
| 1979 | return ::SYSTEM_ERROR; |
| 1980 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 1981 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1982 | keymaster_rsa_sign_params_t params; |
| 1983 | params.digest_type = DIGEST_NONE; |
| 1984 | params.padding_type = PADDING_NONE; |
Kenny Root | 344e0bc | 2012-08-15 10:44:03 -0700 | [diff] [blame] | 1985 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 1986 | if (keyBlob.isFallback()) { |
| 1987 | rc = openssl_verify_data(device, ¶ms, keyBlob.getValue(), keyBlob.getLength(), data, |
| 1988 | dataLength, signature, signatureLength); |
| 1989 | } else { |
| 1990 | rc = device->verify_data(device, ¶ms, keyBlob.getValue(), keyBlob.getLength(), data, |
| 1991 | dataLength, signature, signatureLength); |
| 1992 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1993 | if (rc) { |
| 1994 | return ::SYSTEM_ERROR; |
| 1995 | } else { |
| 1996 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 1997 | } |
| 1998 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 1999 | |
| 2000 | /* |
| 2001 | * TODO: The abstraction between things stored in hardware and regular blobs |
| 2002 | * of data stored on the filesystem should be moved down to keystore itself. |
| 2003 | * Unfortunately the Java code that calls this has naming conventions that it |
| 2004 | * knows about. Ideally keystore shouldn't be used to store random blobs of |
| 2005 | * data. |
| 2006 | * |
| 2007 | * Until that happens, it's necessary to have a separate "get_pubkey" and |
| 2008 | * "del_key" since the Java code doesn't really communicate what it's |
| 2009 | * intentions are. |
| 2010 | */ |
| 2011 | int32_t get_pubkey(const String16& name, uint8_t** pubkey, size_t* pubkeyLength) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 2012 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 2013 | if (!has_permission(callingUid, P_GET)) { |
| 2014 | ALOGW("permission denied for %d: get_pubkey", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2015 | return ::PERMISSION_DENIED; |
| 2016 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2017 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2018 | Blob keyBlob; |
| 2019 | String8 name8(name); |
| 2020 | |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 2021 | ALOGV("get_pubkey '%s' from uid %d", name8.string(), callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2022 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2023 | ResponseCode responseCode = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2024 | TYPE_KEY_PAIR); |
| 2025 | if (responseCode != ::NO_ERROR) { |
| 2026 | return responseCode; |
| 2027 | } |
| 2028 | |
| 2029 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 2030 | if (device == NULL) { |
| 2031 | return ::SYSTEM_ERROR; |
| 2032 | } |
| 2033 | |
| 2034 | if (device->get_keypair_public == NULL) { |
| 2035 | ALOGE("device has no get_keypair_public implementation!"); |
| 2036 | return ::SYSTEM_ERROR; |
| 2037 | } |
| 2038 | |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 2039 | int rc; |
| 2040 | if (keyBlob.isFallback()) { |
| 2041 | rc = openssl_get_keypair_public(device, keyBlob.getValue(), keyBlob.getLength(), pubkey, |
| 2042 | pubkeyLength); |
| 2043 | } else { |
| 2044 | rc = device->get_keypair_public(device, keyBlob.getValue(), keyBlob.getLength(), pubkey, |
| 2045 | pubkeyLength); |
| 2046 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2047 | if (rc) { |
| 2048 | return ::SYSTEM_ERROR; |
| 2049 | } |
| 2050 | |
| 2051 | return ::NO_ERROR; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 2052 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2053 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 2054 | int32_t del_key(const String16& name, int targetUid) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 2055 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 2056 | if (!has_permission(callingUid, P_DELETE)) { |
| 2057 | ALOGW("permission denied for %d: del_key", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2058 | return ::PERMISSION_DENIED; |
| 2059 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2060 | |
Kenny Root | 4946890 | 2013-03-19 13:41:33 -0700 | [diff] [blame] | 2061 | if (targetUid == -1) { |
| 2062 | targetUid = callingUid; |
| 2063 | } else if (!is_granted_to(callingUid, targetUid)) { |
Kenny Root | b88c3eb | 2013-02-13 14:43:43 -0800 | [diff] [blame] | 2064 | return ::PERMISSION_DENIED; |
| 2065 | } |
| 2066 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2067 | String8 name8(name); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2068 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2069 | |
| 2070 | Blob keyBlob; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2071 | ResponseCode responseCode = mKeyStore->get(filename.string(), &keyBlob, ::TYPE_KEY_PAIR, |
| 2072 | callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2073 | if (responseCode != ::NO_ERROR) { |
| 2074 | return responseCode; |
| 2075 | } |
| 2076 | |
| 2077 | ResponseCode rc = ::NO_ERROR; |
| 2078 | |
| 2079 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 2080 | if (device == NULL) { |
| 2081 | rc = ::SYSTEM_ERROR; |
| 2082 | } else { |
| 2083 | // A device doesn't have to implement delete_keypair. |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 2084 | if (device->delete_keypair != NULL && !keyBlob.isFallback()) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2085 | if (device->delete_keypair(device, keyBlob.getValue(), keyBlob.getLength())) { |
| 2086 | rc = ::SYSTEM_ERROR; |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | if (rc != ::NO_ERROR) { |
| 2092 | return rc; |
| 2093 | } |
| 2094 | |
| 2095 | return (unlink(filename) && errno != ENOENT) ? ::SYSTEM_ERROR : ::NO_ERROR; |
| 2096 | } |
| 2097 | |
| 2098 | int32_t grant(const String16& name, int32_t granteeUid) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 2099 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 2100 | if (!has_permission(callingUid, P_GRANT)) { |
| 2101 | ALOGW("permission denied for %d: grant", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2102 | return ::PERMISSION_DENIED; |
| 2103 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2104 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2105 | State state = mKeyStore->getState(callingUid); |
Kenny Root | 9d45d1c | 2013-02-14 10:32:30 -0800 | [diff] [blame] | 2106 | if (!isKeystoreUnlocked(state)) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2107 | ALOGD("calling grant in state: %d", state); |
| 2108 | return state; |
| 2109 | } |
| 2110 | |
| 2111 | String8 name8(name); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2112 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2113 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2114 | if (access(filename.string(), R_OK) == -1) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2115 | return (errno != ENOENT) ? ::SYSTEM_ERROR : ::KEY_NOT_FOUND; |
| 2116 | } |
| 2117 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2118 | mKeyStore->addGrant(filename.string(), granteeUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2119 | return ::NO_ERROR; |
| 2120 | } |
| 2121 | |
| 2122 | int32_t ungrant(const String16& name, int32_t granteeUid) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 2123 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 2124 | if (!has_permission(callingUid, P_GRANT)) { |
| 2125 | ALOGW("permission denied for %d: ungrant", callingUid); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2126 | return ::PERMISSION_DENIED; |
| 2127 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2128 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2129 | State state = mKeyStore->getState(callingUid); |
Kenny Root | 9d45d1c | 2013-02-14 10:32:30 -0800 | [diff] [blame] | 2130 | if (!isKeystoreUnlocked(state)) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2131 | ALOGD("calling ungrant in state: %d", state); |
| 2132 | return state; |
| 2133 | } |
| 2134 | |
| 2135 | String8 name8(name); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2136 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2137 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2138 | if (access(filename.string(), R_OK) == -1) { |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2139 | return (errno != ENOENT) ? ::SYSTEM_ERROR : ::KEY_NOT_FOUND; |
| 2140 | } |
| 2141 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2142 | return mKeyStore->removeGrant(filename.string(), granteeUid) ? ::NO_ERROR : ::KEY_NOT_FOUND; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | int64_t getmtime(const String16& name) { |
Kenny Root | d38a0b0 | 2013-02-13 12:59:14 -0800 | [diff] [blame] | 2146 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 2147 | if (!has_permission(callingUid, P_GET)) { |
| 2148 | ALOGW("permission denied for %d: getmtime", callingUid); |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 2149 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2150 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2151 | |
| 2152 | String8 name8(name); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2153 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2154 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2155 | if (access(filename.string(), R_OK) == -1) { |
| 2156 | ALOGW("could not access %s for getmtime", filename.string()); |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 2157 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2160 | int fd = TEMP_FAILURE_RETRY(open(filename.string(), O_NOFOLLOW, O_RDONLY)); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2161 | if (fd < 0) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2162 | ALOGW("could not open %s for getmtime", filename.string()); |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 2163 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | struct stat s; |
| 2167 | int ret = fstat(fd, &s); |
| 2168 | close(fd); |
| 2169 | if (ret == -1) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2170 | ALOGW("could not stat %s for getmtime", filename.string()); |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 2171 | return -1L; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Kenny Root | 36a9e23 | 2013-02-04 14:24:15 -0800 | [diff] [blame] | 2174 | return static_cast<int64_t>(s.st_mtime); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2177 | int32_t duplicate(const String16& srcKey, int32_t srcUid, const String16& destKey, |
| 2178 | int32_t destUid) { |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2179 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2180 | if (!has_permission(callingUid, P_DUPLICATE)) { |
| 2181 | ALOGW("permission denied for %d: duplicate", callingUid); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2182 | return -1L; |
| 2183 | } |
| 2184 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2185 | State state = mKeyStore->getState(callingUid); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2186 | if (!isKeystoreUnlocked(state)) { |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2187 | ALOGD("calling duplicate in state: %d", state); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2188 | return state; |
| 2189 | } |
| 2190 | |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2191 | if (srcUid == -1 || static_cast<uid_t>(srcUid) == callingUid) { |
| 2192 | srcUid = callingUid; |
| 2193 | } else if (!is_granted_to(callingUid, srcUid)) { |
| 2194 | ALOGD("migrate not granted from source: %d -> %d", callingUid, srcUid); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2195 | return ::PERMISSION_DENIED; |
| 2196 | } |
| 2197 | |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2198 | if (destUid == -1) { |
| 2199 | destUid = callingUid; |
| 2200 | } |
| 2201 | |
| 2202 | if (srcUid != destUid) { |
| 2203 | if (static_cast<uid_t>(srcUid) != callingUid) { |
| 2204 | ALOGD("can only duplicate from caller to other or to same uid: " |
| 2205 | "calling=%d, srcUid=%d, destUid=%d", callingUid, srcUid, destUid); |
| 2206 | return ::PERMISSION_DENIED; |
| 2207 | } |
| 2208 | |
| 2209 | if (!is_granted_to(callingUid, destUid)) { |
| 2210 | ALOGD("duplicate not granted to dest: %d -> %d", callingUid, destUid); |
| 2211 | return ::PERMISSION_DENIED; |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | String8 source8(srcKey); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2216 | String8 sourceFile(mKeyStore->getKeyNameForUidWithDir(source8, srcUid)); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2217 | |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2218 | String8 target8(destKey); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2219 | String8 targetFile(mKeyStore->getKeyNameForUidWithDir(target8, srcUid)); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2220 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2221 | if (access(targetFile.string(), W_OK) != -1 || errno != ENOENT) { |
| 2222 | ALOGD("destination already exists: %s", targetFile.string()); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2223 | return ::SYSTEM_ERROR; |
| 2224 | } |
| 2225 | |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2226 | Blob keyBlob; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2227 | ResponseCode responseCode = mKeyStore->get(sourceFile.string(), &keyBlob, TYPE_ANY, |
| 2228 | callingUid); |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2229 | if (responseCode != ::NO_ERROR) { |
| 2230 | return responseCode; |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2231 | } |
Kenny Root | d53bc92 | 2013-03-21 14:10:15 -0700 | [diff] [blame] | 2232 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2233 | return mKeyStore->put(targetFile.string(), &keyBlob, callingUid); |
Kenny Root | 0225407 | 2013-03-20 11:48:19 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
Kenny Root | 70f16c1 | 2013-09-05 13:06:32 -0700 | [diff] [blame] | 2236 | int32_t is_hardware_backed(const String16& keyType) { |
| 2237 | return mKeyStore->isHardwareBacked(keyType) ? 1 : 0; |
Kenny Root | 8ddf35a | 2013-03-29 11:15:50 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2240 | int32_t clear_uid(int64_t targetUid) { |
| 2241 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 2242 | if (!has_permission(callingUid, P_CLEAR_UID)) { |
| 2243 | ALOGW("permission denied for %d: clear_uid", callingUid); |
| 2244 | return ::PERMISSION_DENIED; |
| 2245 | } |
| 2246 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2247 | State state = mKeyStore->getState(callingUid); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2248 | if (!isKeystoreUnlocked(state)) { |
| 2249 | ALOGD("calling clear_uid in state: %d", state); |
| 2250 | return state; |
| 2251 | } |
| 2252 | |
| 2253 | const keymaster_device_t* device = mKeyStore->getDevice(); |
| 2254 | if (device == NULL) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2255 | ALOGW("can't get keymaster device"); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2256 | return ::SYSTEM_ERROR; |
| 2257 | } |
| 2258 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2259 | UserState* userState = mKeyStore->getUserState(callingUid); |
| 2260 | DIR* dir = opendir(userState->getUserDirName()); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2261 | if (!dir) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2262 | ALOGW("can't open user directory: %s", strerror(errno)); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2263 | return ::SYSTEM_ERROR; |
| 2264 | } |
| 2265 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2266 | char prefix[NAME_MAX]; |
| 2267 | int n = snprintf(prefix, NAME_MAX, "%u_", static_cast<uid_t>(targetUid)); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2268 | |
| 2269 | ResponseCode rc = ::NO_ERROR; |
| 2270 | |
| 2271 | struct dirent* file; |
| 2272 | while ((file = readdir(dir)) != NULL) { |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2273 | // We only care about files. |
| 2274 | if (file->d_type != DT_REG) { |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2275 | continue; |
| 2276 | } |
| 2277 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2278 | // Skip anything that starts with a "." |
| 2279 | if (file->d_name[0] == '.') { |
| 2280 | continue; |
| 2281 | } |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2282 | |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2283 | if (strncmp(prefix, file->d_name, n)) { |
| 2284 | continue; |
| 2285 | } |
| 2286 | |
| 2287 | String8 filename(String8::format("%s/%s", userState->getUserDirName(), file->d_name)); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2288 | Blob keyBlob; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2289 | if (mKeyStore->get(filename.string(), &keyBlob, ::TYPE_ANY, callingUid) |
| 2290 | != ::NO_ERROR) { |
| 2291 | ALOGW("couldn't open %s", filename.string()); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2292 | continue; |
| 2293 | } |
| 2294 | |
| 2295 | if (keyBlob.getType() == ::TYPE_KEY_PAIR) { |
| 2296 | // A device doesn't have to implement delete_keypair. |
Kenny Root | b4d2e02 | 2013-09-04 13:56:03 -0700 | [diff] [blame] | 2297 | if (device->delete_keypair != NULL && !keyBlob.isFallback()) { |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2298 | if (device->delete_keypair(device, keyBlob.getValue(), keyBlob.getLength())) { |
| 2299 | rc = ::SYSTEM_ERROR; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2300 | ALOGW("device couldn't remove %s", filename.string()); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2301 | } |
| 2302 | } |
| 2303 | } |
| 2304 | |
Kenny Root | 5f53124 | 2013-04-12 11:31:50 -0700 | [diff] [blame] | 2305 | if (unlinkat(dirfd(dir), file->d_name, 0) && errno != ENOENT) { |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2306 | rc = ::SYSTEM_ERROR; |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2307 | ALOGW("couldn't unlink %s", filename.string()); |
Kenny Root | a9bb549 | 2013-04-01 16:29:11 -0700 | [diff] [blame] | 2308 | } |
| 2309 | } |
| 2310 | closedir(dir); |
| 2311 | |
| 2312 | return rc; |
| 2313 | } |
| 2314 | |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2315 | private: |
Kenny Root | 9d45d1c | 2013-02-14 10:32:30 -0800 | [diff] [blame] | 2316 | inline bool isKeystoreUnlocked(State state) { |
| 2317 | switch (state) { |
| 2318 | case ::STATE_NO_ERROR: |
| 2319 | return true; |
| 2320 | case ::STATE_UNINITIALIZED: |
| 2321 | case ::STATE_LOCKED: |
| 2322 | return false; |
| 2323 | } |
| 2324 | return false; |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | ::KeyStore* mKeyStore; |
| 2328 | }; |
| 2329 | |
| 2330 | }; // namespace android |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 2331 | |
| 2332 | int main(int argc, char* argv[]) { |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 2333 | if (argc < 2) { |
| 2334 | ALOGE("A directory must be specified!"); |
| 2335 | return 1; |
| 2336 | } |
| 2337 | if (chdir(argv[1]) == -1) { |
| 2338 | ALOGE("chdir: %s: %s", argv[1], strerror(errno)); |
| 2339 | return 1; |
| 2340 | } |
| 2341 | |
| 2342 | Entropy entropy; |
| 2343 | if (!entropy.open()) { |
| 2344 | return 1; |
| 2345 | } |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 2346 | |
| 2347 | keymaster_device_t* dev; |
| 2348 | if (keymaster_device_initialize(&dev)) { |
| 2349 | ALOGE("keystore keymaster could not be initialized; exiting"); |
| 2350 | return 1; |
| 2351 | } |
| 2352 | |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 2353 | KeyStore keyStore(&entropy, dev); |
Kenny Root | 655b958 | 2013-04-04 08:37:42 -0700 | [diff] [blame] | 2354 | keyStore.initialize(); |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2355 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); |
| 2356 | android::sp<android::KeyStoreProxy> proxy = new android::KeyStoreProxy(&keyStore); |
| 2357 | android::status_t ret = sm->addService(android::String16("android.security.keystore"), proxy); |
| 2358 | if (ret != android::OK) { |
| 2359 | ALOGE("Couldn't register binder service!"); |
| 2360 | return -1; |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 2361 | } |
Kenny Root | 07438c8 | 2012-11-02 15:41:02 -0700 | [diff] [blame] | 2362 | |
| 2363 | /* |
| 2364 | * We're the only thread in existence, so we're just going to process |
| 2365 | * Binder transaction as a single-threaded program. |
| 2366 | */ |
| 2367 | android::IPCThreadState::self()->joinThreadPool(); |
Kenny Root | 70e3a86 | 2012-02-15 17:20:23 -0800 | [diff] [blame] | 2368 | |
| 2369 | keymaster_device_release(dev); |
Kenny Root | a91203b | 2012-02-15 15:00:46 -0800 | [diff] [blame] | 2370 | return 1; |
| 2371 | } |