Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [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 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 17 | #define LOG_TAG "keystore" |
| 18 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 20 | #include <android/hidl/manager/1.1/IServiceManager.h> |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame] | 21 | #include <android/security/IKeystoreService.h> |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 22 | #include <android/system/wifi/keystore/1.0/IKeystore.h> |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame] | 25 | #include <hidl/HidlTransportSupport.h> |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 26 | #include <utils/StrongPointer.h> |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 27 | #include <wifikeystorehal/keystore.h> |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 28 | |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame] | 29 | #include <keystore/keystore_hidl_support.h> |
| 30 | #include <keystore/keystore_return_types.h> |
| 31 | |
Shawn Willden | fa5702f | 2017-12-03 15:14:58 -0700 | [diff] [blame] | 32 | #include "KeyStore.h" |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 33 | #include "Keymaster3.h" |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 34 | #include "Keymaster4.h" |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 35 | #include "entropy.h" |
Shawn Willden | fa5702f | 2017-12-03 15:14:58 -0700 | [diff] [blame] | 36 | #include "key_store_service.h" |
| 37 | #include "legacy_keymaster_device_wrapper.h" |
| 38 | #include "permissions.h" |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 39 | |
| 40 | /* KeyStore is a secured storage for key-value pairs. In this implementation, |
| 41 | * each file stores one key-value pair. Keys are encoded in file names, and |
| 42 | * values are encrypted with checksums. The encryption key is protected by a |
| 43 | * user-defined password. To keep things simple, buffers are always larger than |
| 44 | * the maximum space we needed, so boundary checks on buffers are omitted. */ |
| 45 | |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 46 | using ::android::sp; |
Shawn Willden | fa5702f | 2017-12-03 15:14:58 -0700 | [diff] [blame] | 47 | using ::android::hardware::configureRpcThreadpool; |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 48 | using ::android::system::wifi::keystore::V1_0::IKeystore; |
| 49 | using ::android::system::wifi::keystore::V1_0::implementation::Keystore; |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 50 | using ::android::hidl::manager::V1_1::IServiceManager; |
| 51 | using ::android::hardware::hidl_string; |
| 52 | using ::android::hardware::hidl_vec; |
| 53 | using ::android::hardware::keymaster::V4_0::SecurityLevel; |
Janis Danisevskis | 3506d33 | 2017-12-19 16:27:28 -0800 | [diff] [blame^] | 54 | using ::android::hardware::keymaster::V4_0::HmacSharingParameters; |
| 55 | using ::android::hardware::keymaster::V4_0::ErrorCode; |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 56 | |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 57 | using keystore::Keymaster; |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 58 | using keystore::Keymaster3; |
| 59 | using keystore::Keymaster4; |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 60 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 61 | using keystore::KeymasterDevices; |
| 62 | |
| 63 | template <typename Wrapper> |
| 64 | KeymasterDevices enumerateKeymasterDevices(IServiceManager* serviceManager) { |
| 65 | KeymasterDevices result; |
| 66 | serviceManager->listByInterface( |
| 67 | Wrapper::WrappedIKeymasterDevice::descriptor, [&](const hidl_vec<hidl_string>& names) { |
| 68 | auto try_get_device = [&](const auto& name, bool fail_silent) { |
| 69 | auto device = Wrapper::WrappedIKeymasterDevice::getService(name); |
| 70 | if (fail_silent && !device) return; |
| 71 | CHECK(device) << "Failed to get service for \"" |
| 72 | << Wrapper::WrappedIKeymasterDevice::descriptor |
| 73 | << "\" with interface name \"" << name << "\""; |
| 74 | |
| 75 | sp<Keymaster> kmDevice(new Wrapper(device)); |
| 76 | auto halVersion = kmDevice->halVersion(); |
| 77 | SecurityLevel securityLevel = halVersion.securityLevel; |
| 78 | LOG(INFO) << "found " << Wrapper::WrappedIKeymasterDevice::descriptor |
| 79 | << " with interface name " << name << " and seclevel " |
| 80 | << toString(securityLevel); |
| 81 | CHECK(static_cast<uint32_t>(securityLevel) < result.size()) |
| 82 | << "Security level of \"" << Wrapper::WrappedIKeymasterDevice::descriptor |
| 83 | << "\" with interface name \"" << name << "\" out of range"; |
| 84 | auto& deviceSlot = result[securityLevel]; |
| 85 | if (deviceSlot) { |
| 86 | if (!fail_silent) { |
| 87 | LOG(WARNING) << "Implementation of \"" |
| 88 | << Wrapper::WrappedIKeymasterDevice::descriptor |
| 89 | << "\" with interface name \"" << name |
| 90 | << "\" and security level: " << toString(securityLevel) |
| 91 | << " Masked by other implementation of Keymaster"; |
| 92 | } |
| 93 | } else { |
| 94 | deviceSlot = kmDevice; |
| 95 | } |
| 96 | }; |
| 97 | bool has_default = false; |
| 98 | for (auto& n : names) { |
| 99 | try_get_device(n, false); |
| 100 | if (n == "default") has_default = true; |
| 101 | } |
| 102 | // Make sure that we always check the default device. If we enumerate only what is |
| 103 | // known to hwservicemanager, we miss a possible passthrough HAL. |
| 104 | if (!has_default) { |
| 105 | try_get_device("default", true /* fail_silent */); |
| 106 | } |
| 107 | }); |
| 108 | return result; |
| 109 | } |
| 110 | |
Janis Danisevskis | 3506d33 | 2017-12-19 16:27:28 -0800 | [diff] [blame^] | 111 | void performHmacKeyHandshake(std::initializer_list<const sp<Keymaster>> keymasters) { |
| 112 | hidl_vec<HmacSharingParameters> hmacSharingParams(keymasters.size()); |
| 113 | int index = 0; |
| 114 | for (const auto& km : keymasters) { |
| 115 | if (!km) continue; |
| 116 | ErrorCode ec = ErrorCode::OK; |
| 117 | auto rc = |
| 118 | km->getHmacSharingParameters([&](ErrorCode error, const HmacSharingParameters& params) { |
| 119 | ec = error; |
| 120 | if (error == ErrorCode::OK) hmacSharingParams[index] = params; |
| 121 | }); |
| 122 | CHECK(rc.isOk()) << "Communication error while calling getHmacSharingParameters on" |
| 123 | " Keymaster with index: " |
| 124 | << index; |
| 125 | CHECK(ec == ErrorCode::OK) << "Failed to get HmacSharingParameters from" |
| 126 | " Keymaster with index: " |
| 127 | << index; |
| 128 | ++index; |
| 129 | } |
| 130 | hmacSharingParams.resize(index); |
| 131 | hidl_vec<uint8_t> sharingCheck; |
| 132 | index = 0; |
| 133 | for (const auto& km : keymasters) { |
| 134 | if (!km) continue; |
| 135 | ErrorCode ec = ErrorCode::OK; |
| 136 | auto rc = km->computeSharedHmac( |
| 137 | hmacSharingParams, [&](ErrorCode error, const hidl_vec<uint8_t>& sharingCheck_) { |
| 138 | ec = error; |
| 139 | if (error != ErrorCode::OK) return; |
| 140 | if (index == 0) { |
| 141 | sharingCheck = sharingCheck_; |
| 142 | } else { |
| 143 | CHECK(sharingCheck == sharingCheck_) |
| 144 | << "Hmac Key computation failed (current index: " << index << ")"; |
| 145 | } |
| 146 | }); |
| 147 | CHECK(rc.isOk()) << "Communication error while calling computeSharedHmac on" |
| 148 | " Keymaster with index: " |
| 149 | << index; |
| 150 | CHECK(ec == ErrorCode::OK) << "Failed to compute shared hmac key from" |
| 151 | " Keymaster with index: " |
| 152 | << index; |
| 153 | ++index; |
| 154 | } |
| 155 | } |
| 156 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 157 | KeymasterDevices initializeKeymasters() { |
| 158 | auto serviceManager = android::hidl::manager::V1_1::IServiceManager::getService(); |
| 159 | CHECK(serviceManager.get()) << "Failed to get ServiceManager"; |
| 160 | auto result = enumerateKeymasterDevices<Keymaster4>(serviceManager.get()); |
| 161 | CHECK(result[SecurityLevel::TRUSTED_ENVIRONMENT] || !result[SecurityLevel::STRONGBOX]) |
| 162 | << "We cannot have a Strongbox keymaster implementation without a TEE implementation"; |
| 163 | auto softKeymaster = result[SecurityLevel::SOFTWARE]; |
Janis Danisevskis | 3506d33 | 2017-12-19 16:27:28 -0800 | [diff] [blame^] | 164 | if (result[SecurityLevel::TRUSTED_ENVIRONMENT]) { |
| 165 | performHmacKeyHandshake( |
| 166 | {result[SecurityLevel::TRUSTED_ENVIRONMENT], result[SecurityLevel::STRONGBOX]}); |
| 167 | } else { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 168 | result = enumerateKeymasterDevices<Keymaster3>(serviceManager.get()); |
| 169 | } |
| 170 | if (softKeymaster) result[SecurityLevel::SOFTWARE] = softKeymaster; |
| 171 | if (result[SecurityLevel::SOFTWARE] && !result[SecurityLevel::TRUSTED_ENVIRONMENT]) { |
| 172 | LOG(WARNING) << "No secure Keymaster implementation found, but device offers insecure" |
| 173 | " Keymaster HAL. Using as default."; |
| 174 | result[SecurityLevel::TRUSTED_ENVIRONMENT] = result[SecurityLevel::SOFTWARE]; |
| 175 | result[SecurityLevel::SOFTWARE] = nullptr; |
| 176 | } |
| 177 | if (!result[SecurityLevel::SOFTWARE]) { |
| 178 | auto fbdev = android::keystore::makeSoftwareKeymasterDevice(); |
| 179 | CHECK(fbdev.get()) << "Unable to create Software Keymaster Device"; |
| 180 | result[SecurityLevel::SOFTWARE] = new keystore::Keymaster3(fbdev); |
| 181 | } |
| 182 | return result; |
| 183 | } |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 184 | |
| 185 | int main(int argc, char* argv[]) { |
Shawn Willden | b8550a0 | 2017-02-23 11:06:05 -0700 | [diff] [blame] | 186 | using android::hardware::hidl_string; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 187 | CHECK(argc >= 2) << "A directory must be specified!"; |
| 188 | CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 189 | |
| 190 | Entropy entropy; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 191 | CHECK(entropy.open()) << "Failed to open entropy source."; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 192 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 193 | auto kmDevices = initializeKeymasters(); |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 194 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 195 | CHECK(kmDevices[SecurityLevel::SOFTWARE]) << "Missing software Keymaster device"; |
| 196 | CHECK(kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]) |
| 197 | << "Error no viable keymaster device found"; |
Shawn Willden | 814a6e7 | 2016-03-15 08:37:29 -0600 | [diff] [blame] | 198 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 199 | CHECK(configure_selinux() != -1) << "Failed to configure SELinux."; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 200 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 201 | auto halVersion = kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]->halVersion(); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 202 | CHECK(halVersion.error == keystore::ErrorCode::OK) |
| 203 | << "Error " << toString(halVersion.error) << " getting HAL version"; |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 204 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 205 | // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import |
| 206 | // or generation of keys. The fallback device is only used for legacy keys present on the |
| 207 | // device. |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 208 | SecurityLevel minimalAllowedSecurityLevelForNewKeys = |
| 209 | halVersion.majorVersion >= 2 ? SecurityLevel::TRUSTED_ENVIRONMENT : SecurityLevel::SOFTWARE; |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 210 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 211 | keystore::KeyStore keyStore(&entropy, kmDevices, minimalAllowedSecurityLevelForNewKeys); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 212 | keyStore.initialize(); |
| 213 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 214 | android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 215 | android::status_t ret = sm->addService(android::String16("android.security.keystore"), service); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 216 | CHECK(ret == android::OK) << "Couldn't register binder service!"; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 217 | |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Register the wifi keystore HAL service to run in passthrough mode. |
| 220 | * This will spawn off a new thread which will service the HIDL |
| 221 | * transactions. |
| 222 | */ |
| 223 | configureRpcThreadpool(1, false /* callerWillJoin */); |
| 224 | android::sp<IKeystore> wifiKeystoreHalService = new Keystore(); |
| 225 | android::status_t err = wifiKeystoreHalService->registerAsService(); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 226 | CHECK(ret == android::OK) << "Cannot register wifi keystore HAL service: " << err; |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 227 | |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 228 | /* |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 229 | * This thread is just going to process Binder transactions. |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 230 | */ |
| 231 | android::IPCThreadState::self()->joinThreadPool(); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 232 | return 1; |
| 233 | } |