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> |
Steven Moreland | 13e5a2c | 2019-05-13 12:55:54 -0700 | [diff] [blame] | 20 | #include <android/hidl/manager/1.2/IServiceManager.h> |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 21 | #include <android/security/keystore/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 | eedcfe9 | 2018-01-18 15:35:46 -0700 | [diff] [blame] | 26 | #include <keymasterV4_0/Keymaster3.h> |
| 27 | #include <keymasterV4_0/Keymaster4.h> |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 28 | #include <utils/StrongPointer.h> |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 29 | #include <wifikeystorehal/keystore.h> |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 30 | |
Shawn Willden | bb22a6c | 2017-12-06 19:35:28 -0700 | [diff] [blame] | 31 | #include <keystore/keystore_hidl_support.h> |
| 32 | #include <keystore/keystore_return_types.h> |
| 33 | |
Shawn Willden | fa5702f | 2017-12-03 15:14:58 -0700 | [diff] [blame] | 34 | #include "KeyStore.h" |
Shawn Willden | fa5702f | 2017-12-03 15:14:58 -0700 | [diff] [blame] | 35 | #include "key_store_service.h" |
| 36 | #include "legacy_keymaster_device_wrapper.h" |
| 37 | #include "permissions.h" |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 38 | |
| 39 | /* KeyStore is a secured storage for key-value pairs. In this implementation, |
| 40 | * each file stores one key-value pair. Keys are encoded in file names, and |
| 41 | * values are encrypted with checksums. The encryption key is protected by a |
| 42 | * user-defined password. To keep things simple, buffers are always larger than |
| 43 | * the maximum space we needed, so boundary checks on buffers are omitted. */ |
| 44 | |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 45 | using ::android::sp; |
Shawn Willden | fa5702f | 2017-12-03 15:14:58 -0700 | [diff] [blame] | 46 | using ::android::hardware::configureRpcThreadpool; |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 47 | using ::android::hardware::hidl_string; |
| 48 | using ::android::hardware::hidl_vec; |
Janis Danisevskis | 3506d33 | 2017-12-19 16:27:28 -0800 | [diff] [blame] | 49 | using ::android::hardware::keymaster::V4_0::ErrorCode; |
Steven Moreland | 13e5a2c | 2019-05-13 12:55:54 -0700 | [diff] [blame] | 50 | using ::android::hardware::keymaster::V4_0::HmacSharingParameters; |
| 51 | using ::android::hardware::keymaster::V4_0::SecurityLevel; |
| 52 | using ::android::hidl::manager::V1_2::IServiceManager; |
| 53 | using ::android::system::wifi::keystore::V1_0::IKeystore; |
| 54 | using ::android::system::wifi::keystore::V1_0::implementation::Keystore; |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 55 | |
Shawn Willden | eedcfe9 | 2018-01-18 15:35:46 -0700 | [diff] [blame] | 56 | using ::keystore::keymaster::support::Keymaster; |
| 57 | using ::keystore::keymaster::support::Keymaster3; |
| 58 | using ::keystore::keymaster::support::Keymaster4; |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 59 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 60 | using keystore::KeymasterDevices; |
| 61 | |
| 62 | template <typename Wrapper> |
| 63 | KeymasterDevices enumerateKeymasterDevices(IServiceManager* serviceManager) { |
| 64 | KeymasterDevices result; |
Steven Moreland | 13e5a2c | 2019-05-13 12:55:54 -0700 | [diff] [blame] | 65 | serviceManager->listManifestByInterface( |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 66 | Wrapper::WrappedIKeymasterDevice::descriptor, [&](const hidl_vec<hidl_string>& names) { |
| 67 | auto try_get_device = [&](const auto& name, bool fail_silent) { |
| 68 | auto device = Wrapper::WrappedIKeymasterDevice::getService(name); |
| 69 | if (fail_silent && !device) return; |
| 70 | CHECK(device) << "Failed to get service for \"" |
| 71 | << Wrapper::WrappedIKeymasterDevice::descriptor |
| 72 | << "\" with interface name \"" << name << "\""; |
| 73 | |
Shawn Willden | cd416c5 | 2018-01-22 09:37:43 -0700 | [diff] [blame] | 74 | sp<Keymaster> kmDevice(new Wrapper(device, name)); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 75 | auto halVersion = kmDevice->halVersion(); |
| 76 | SecurityLevel securityLevel = halVersion.securityLevel; |
| 77 | LOG(INFO) << "found " << Wrapper::WrappedIKeymasterDevice::descriptor |
| 78 | << " with interface name " << name << " and seclevel " |
| 79 | << toString(securityLevel); |
| 80 | CHECK(static_cast<uint32_t>(securityLevel) < result.size()) |
| 81 | << "Security level of \"" << Wrapper::WrappedIKeymasterDevice::descriptor |
| 82 | << "\" with interface name \"" << name << "\" out of range"; |
| 83 | auto& deviceSlot = result[securityLevel]; |
| 84 | if (deviceSlot) { |
| 85 | if (!fail_silent) { |
| 86 | LOG(WARNING) << "Implementation of \"" |
| 87 | << Wrapper::WrappedIKeymasterDevice::descriptor |
| 88 | << "\" with interface name \"" << name |
| 89 | << "\" and security level: " << toString(securityLevel) |
| 90 | << " Masked by other implementation of Keymaster"; |
| 91 | } |
| 92 | } else { |
| 93 | deviceSlot = kmDevice; |
| 94 | } |
| 95 | }; |
| 96 | bool has_default = false; |
| 97 | for (auto& n : names) { |
| 98 | try_get_device(n, false); |
| 99 | if (n == "default") has_default = true; |
| 100 | } |
| 101 | // Make sure that we always check the default device. If we enumerate only what is |
| 102 | // known to hwservicemanager, we miss a possible passthrough HAL. |
| 103 | if (!has_default) { |
| 104 | try_get_device("default", true /* fail_silent */); |
| 105 | } |
| 106 | }); |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | KeymasterDevices initializeKeymasters() { |
Steven Moreland | 13e5a2c | 2019-05-13 12:55:54 -0700 | [diff] [blame] | 111 | auto serviceManager = IServiceManager::getService(); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 112 | CHECK(serviceManager.get()) << "Failed to get ServiceManager"; |
| 113 | auto result = enumerateKeymasterDevices<Keymaster4>(serviceManager.get()); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 114 | auto softKeymaster = result[SecurityLevel::SOFTWARE]; |
Shawn Willden | 5a6afd0 | 2018-05-09 15:47:15 -0600 | [diff] [blame] | 115 | if (!result[SecurityLevel::TRUSTED_ENVIRONMENT]) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 116 | result = enumerateKeymasterDevices<Keymaster3>(serviceManager.get()); |
| 117 | } |
| 118 | if (softKeymaster) result[SecurityLevel::SOFTWARE] = softKeymaster; |
| 119 | if (result[SecurityLevel::SOFTWARE] && !result[SecurityLevel::TRUSTED_ENVIRONMENT]) { |
| 120 | LOG(WARNING) << "No secure Keymaster implementation found, but device offers insecure" |
| 121 | " Keymaster HAL. Using as default."; |
| 122 | result[SecurityLevel::TRUSTED_ENVIRONMENT] = result[SecurityLevel::SOFTWARE]; |
| 123 | result[SecurityLevel::SOFTWARE] = nullptr; |
| 124 | } |
| 125 | if (!result[SecurityLevel::SOFTWARE]) { |
| 126 | auto fbdev = android::keystore::makeSoftwareKeymasterDevice(); |
| 127 | CHECK(fbdev.get()) << "Unable to create Software Keymaster Device"; |
Shawn Willden | cd416c5 | 2018-01-22 09:37:43 -0700 | [diff] [blame] | 128 | result[SecurityLevel::SOFTWARE] = new Keymaster3(fbdev, "Software"); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 129 | } |
| 130 | return result; |
| 131 | } |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 132 | |
| 133 | int main(int argc, char* argv[]) { |
Shawn Willden | b8550a0 | 2017-02-23 11:06:05 -0700 | [diff] [blame] | 134 | using android::hardware::hidl_string; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 135 | CHECK(argc >= 2) << "A directory must be specified!"; |
| 136 | CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 137 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 138 | auto kmDevices = initializeKeymasters(); |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 139 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 140 | CHECK(kmDevices[SecurityLevel::SOFTWARE]) << "Missing software Keymaster device"; |
| 141 | CHECK(kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]) |
| 142 | << "Error no viable keymaster device found"; |
Shawn Willden | 814a6e7 | 2016-03-15 08:37:29 -0600 | [diff] [blame] | 143 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 144 | CHECK(configure_selinux() != -1) << "Failed to configure SELinux."; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 145 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 146 | auto halVersion = kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]->halVersion(); |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 147 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 148 | // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import |
| 149 | // or generation of keys. The fallback device is only used for legacy keys present on the |
| 150 | // device. |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 151 | SecurityLevel minimalAllowedSecurityLevelForNewKeys = |
| 152 | halVersion.majorVersion >= 2 ? SecurityLevel::TRUSTED_ENVIRONMENT : SecurityLevel::SOFTWARE; |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 153 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 154 | android::sp<keystore::KeyStore> keyStore( |
Branden Archer | 44d1afa | 2018-12-28 09:10:49 -0800 | [diff] [blame] | 155 | new keystore::KeyStore(kmDevices, minimalAllowedSecurityLevelForNewKeys)); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 156 | keyStore->initialize(); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 157 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 158 | android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(keyStore); |
Steven Moreland | 23115b0 | 2019-01-10 16:20:20 -0800 | [diff] [blame] | 159 | service->setRequestingSid(true); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 160 | android::status_t ret = sm->addService(android::String16("android.security.keystore"), service); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 161 | CHECK(ret == android::OK) << "Couldn't register binder service!"; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 162 | |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Register the wifi keystore HAL service to run in passthrough mode. |
| 165 | * This will spawn off a new thread which will service the HIDL |
| 166 | * transactions. |
| 167 | */ |
| 168 | configureRpcThreadpool(1, false /* callerWillJoin */); |
| 169 | android::sp<IKeystore> wifiKeystoreHalService = new Keystore(); |
| 170 | android::status_t err = wifiKeystoreHalService->registerAsService(); |
Wenhao Wang | 0ff2568 | 2019-08-21 09:32:56 -0700 | [diff] [blame] | 171 | CHECK(err == android::OK) << "Cannot register wifi keystore HAL service: " << err; |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 172 | |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 173 | /* |
Roshan Pius | e653c93 | 2017-03-29 10:08:47 -0700 | [diff] [blame] | 174 | * This thread is just going to process Binder transactions. |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 175 | */ |
| 176 | android::IPCThreadState::self()->joinThreadPool(); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 177 | return 1; |
| 178 | } |