Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef KEYSTORE_KEYSTORE_H_ |
| 18 | #define KEYSTORE_KEYSTORE_H_ |
| 19 | |
| 20 | #include "user_state.h" |
| 21 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 22 | #include <android/hardware/keymaster/3.0/IKeymasterDevice.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 23 | |
| 24 | #include <utils/Vector.h> |
| 25 | |
| 26 | #include "blob.h" |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 27 | #include "include/keystore/keymaster_tags.h" |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame^] | 28 | #include "grant_store.h" |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 29 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 30 | using ::keystore::NullOr; |
| 31 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 32 | class KeyStore { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 33 | typedef ::android::sp<::android::hardware::keymaster::V3_0::IKeymasterDevice> km_device_t; |
| 34 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 35 | public: |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 36 | KeyStore(Entropy* entropy, const km_device_t& device, const km_device_t& fallback, |
| 37 | bool allowNewFallback); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 38 | ~KeyStore(); |
| 39 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 40 | km_device_t& getDevice() { return mDevice; } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 41 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 42 | NullOr<km_device_t&> getFallbackDevice() { |
| 43 | // we only return the fallback device if the creation of new fallback key blobs is |
| 44 | // allowed. (also see getDevice below) |
| 45 | if (mAllowNewFallback) { |
| 46 | return mFallbackDevice; |
| 47 | } else { |
| 48 | return {}; |
| 49 | } |
| 50 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 51 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 52 | km_device_t& getDevice(const Blob& blob) { |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 53 | // We return a device, based on the nature of the blob to provide backward |
| 54 | // compatibility with old key blobs generated using the fallback device. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 55 | return blob.isFallback() ? mFallbackDevice : mDevice; |
| 56 | } |
| 57 | |
| 58 | ResponseCode initialize(); |
| 59 | |
| 60 | State getState(uid_t userId) { return getUserState(userId)->getState(); } |
| 61 | |
| 62 | ResponseCode initializeUser(const android::String8& pw, uid_t userId); |
| 63 | |
| 64 | ResponseCode copyMasterKey(uid_t srcUser, uid_t dstUser); |
| 65 | ResponseCode writeMasterKey(const android::String8& pw, uid_t userId); |
| 66 | ResponseCode readMasterKey(const android::String8& pw, uid_t userId); |
| 67 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 68 | android::String8 getKeyName(const android::String8& keyName, const BlobType type); |
| 69 | android::String8 getKeyNameForUid(const android::String8& keyName, uid_t uid, |
| 70 | const BlobType type); |
| 71 | android::String8 getKeyNameForUidWithDir(const android::String8& keyName, uid_t uid, |
| 72 | const BlobType type); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Delete entries owned by userId. If keepUnencryptedEntries is true |
| 76 | * then only encrypted entries will be removed, otherwise all entries will |
| 77 | * be removed. |
| 78 | */ |
| 79 | void resetUser(uid_t userId, bool keepUnenryptedEntries); |
| 80 | bool isEmpty(uid_t userId) const; |
| 81 | |
| 82 | void lock(uid_t userId); |
| 83 | |
| 84 | ResponseCode get(const char* filename, Blob* keyBlob, const BlobType type, uid_t userId); |
| 85 | ResponseCode put(const char* filename, Blob* keyBlob, uid_t userId); |
| 86 | ResponseCode del(const char* filename, const BlobType type, uid_t userId); |
| 87 | ResponseCode list(const android::String8& prefix, android::Vector<android::String16>* matches, |
| 88 | uid_t userId); |
| 89 | |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame^] | 90 | std::string addGrant(const char* filename, const char* alias, uid_t granteeUid); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 91 | bool removeGrant(const char* filename, uid_t granteeUid); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 92 | |
| 93 | ResponseCode importKey(const uint8_t* key, size_t keyLen, const char* filename, uid_t userId, |
| 94 | int32_t flags); |
| 95 | |
| 96 | bool isHardwareBacked(const android::String16& keyType) const; |
| 97 | |
| 98 | ResponseCode getKeyForName(Blob* keyBlob, const android::String8& keyName, const uid_t uid, |
| 99 | const BlobType type); |
| 100 | |
| 101 | /** |
| 102 | * Returns any existing UserState or creates it if it doesn't exist. |
| 103 | */ |
| 104 | UserState* getUserState(uid_t userId); |
| 105 | |
| 106 | /** |
| 107 | * Returns any existing UserState or creates it if it doesn't exist. |
| 108 | */ |
| 109 | UserState* getUserStateByUid(uid_t uid); |
| 110 | |
| 111 | /** |
| 112 | * Returns NULL if the UserState doesn't already exist. |
| 113 | */ |
| 114 | const UserState* getUserState(uid_t userId) const; |
| 115 | |
| 116 | /** |
| 117 | * Returns NULL if the UserState doesn't already exist. |
| 118 | */ |
| 119 | const UserState* getUserStateByUid(uid_t uid) const; |
| 120 | |
| 121 | private: |
| 122 | static const char* sOldMasterKey; |
| 123 | static const char* sMetaDataFile; |
| 124 | static const android::String16 sRSAKeyType; |
| 125 | Entropy* mEntropy; |
| 126 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 127 | km_device_t mDevice; |
| 128 | km_device_t mFallbackDevice; |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 129 | bool mAllowNewFallback; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 130 | |
| 131 | android::Vector<UserState*> mMasterKeys; |
| 132 | |
Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame^] | 133 | ::keystore::GrantStore mGrants; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 134 | |
| 135 | typedef struct { uint32_t version; } keystore_metadata_t; |
| 136 | |
| 137 | keystore_metadata_t mMetaData; |
| 138 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Upgrade the key from the current version to whatever is newest. |
| 141 | */ |
| 142 | bool upgradeBlob(const char* filename, Blob* blob, const uint8_t oldVersion, |
| 143 | const BlobType type, uid_t uid); |
| 144 | |
| 145 | /** |
| 146 | * Takes a blob that is an PEM-encoded RSA key as a byte array and converts it to a DER-encoded |
| 147 | * PKCS#8 for import into a keymaster. Then it overwrites the original blob with the new blob |
| 148 | * format that is returned from the keymaster. |
| 149 | */ |
| 150 | ResponseCode importBlobAsKey(Blob* blob, const char* filename, uid_t uid); |
| 151 | |
| 152 | void readMetaData(); |
| 153 | void writeMetaData(); |
| 154 | |
| 155 | bool upgradeKeystore(); |
| 156 | }; |
| 157 | |
| 158 | #endif // KEYSTORE_KEYSTORE_H_ |