blob: ef68d92a3caedc7fdf6a3f23a3b322cf44f03c5a [file] [log] [blame]
Shawn Willden6507c272016-01-05 22:51:48 -07001/*
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 Danisevskisc1460142017-12-18 16:48:46 -080017#define LOG_TAG "keystore"
18
Shawn Willden0329a822017-12-04 13:55:14 -070019#include <android-base/logging.h>
Steven Moreland13e5a2c2019-05-13 12:55:54 -070020#include <android/hidl/manager/1.2/IServiceManager.h>
Rob Barnesbb6cabd2018-10-04 17:10:37 -060021#include <android/security/keystore/IKeystoreService.h>
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070022#include <android/system/wifi/keystore/1.0/IKeystore.h>
Shawn Willden6507c272016-01-05 22:51:48 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070025#include <hidl/HidlTransportSupport.h>
Shawn Willdeneedcfe92018-01-18 15:35:46 -070026#include <keymasterV4_0/Keymaster3.h>
27#include <keymasterV4_0/Keymaster4.h>
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070028#include <utils/StrongPointer.h>
Roshan Piuse653c932017-03-29 10:08:47 -070029#include <wifikeystorehal/keystore.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010030
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070031#include <keystore/keystore_hidl_support.h>
32#include <keystore/keystore_return_types.h>
33
Shawn Willdenfa5702f2017-12-03 15:14:58 -070034#include "KeyStore.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070035#include "key_store_service.h"
36#include "legacy_keymaster_device_wrapper.h"
37#include "permissions.h"
Shawn Willden6507c272016-01-05 22:51:48 -070038
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 Willdenc67a8aa2017-12-03 17:51:29 -070045using ::android::sp;
Shawn Willdenfa5702f2017-12-03 15:14:58 -070046using ::android::hardware::configureRpcThreadpool;
Janis Danisevskisc1460142017-12-18 16:48:46 -080047using ::android::hardware::hidl_string;
48using ::android::hardware::hidl_vec;
Janis Danisevskis3506d332017-12-19 16:27:28 -080049using ::android::hardware::keymaster::V4_0::ErrorCode;
Steven Moreland13e5a2c2019-05-13 12:55:54 -070050using ::android::hardware::keymaster::V4_0::HmacSharingParameters;
51using ::android::hardware::keymaster::V4_0::SecurityLevel;
52using ::android::hidl::manager::V1_2::IServiceManager;
53using ::android::system::wifi::keystore::V1_0::IKeystore;
54using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
Roshan Piuse653c932017-03-29 10:08:47 -070055
Shawn Willdeneedcfe92018-01-18 15:35:46 -070056using ::keystore::keymaster::support::Keymaster;
57using ::keystore::keymaster::support::Keymaster3;
58using ::keystore::keymaster::support::Keymaster4;
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070059
Janis Danisevskisc1460142017-12-18 16:48:46 -080060using keystore::KeymasterDevices;
61
62template <typename Wrapper>
63KeymasterDevices enumerateKeymasterDevices(IServiceManager* serviceManager) {
64 KeymasterDevices result;
Steven Moreland13e5a2c2019-05-13 12:55:54 -070065 serviceManager->listManifestByInterface(
Janis Danisevskisc1460142017-12-18 16:48:46 -080066 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 Willdencd416c52018-01-22 09:37:43 -070074 sp<Keymaster> kmDevice(new Wrapper(device, name));
Janis Danisevskisc1460142017-12-18 16:48:46 -080075 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
110KeymasterDevices initializeKeymasters() {
Steven Moreland13e5a2c2019-05-13 12:55:54 -0700111 auto serviceManager = IServiceManager::getService();
Janis Danisevskisc1460142017-12-18 16:48:46 -0800112 CHECK(serviceManager.get()) << "Failed to get ServiceManager";
113 auto result = enumerateKeymasterDevices<Keymaster4>(serviceManager.get());
Janis Danisevskisc1460142017-12-18 16:48:46 -0800114 auto softKeymaster = result[SecurityLevel::SOFTWARE];
Shawn Willden5a6afd02018-05-09 15:47:15 -0600115 if (!result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
Janis Danisevskisc1460142017-12-18 16:48:46 -0800116 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 Willdencd416c52018-01-22 09:37:43 -0700128 result[SecurityLevel::SOFTWARE] = new Keymaster3(fbdev, "Software");
Janis Danisevskisc1460142017-12-18 16:48:46 -0800129 }
130 return result;
131}
Shawn Willden6507c272016-01-05 22:51:48 -0700132
133int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -0700134 using android::hardware::hidl_string;
Shawn Willden0329a822017-12-04 13:55:14 -0700135 CHECK(argc >= 2) << "A directory must be specified!";
136 CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno);
Shawn Willden6507c272016-01-05 22:51:48 -0700137
Janis Danisevskisc1460142017-12-18 16:48:46 -0800138 auto kmDevices = initializeKeymasters();
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700139
Janis Danisevskisc1460142017-12-18 16:48:46 -0800140 CHECK(kmDevices[SecurityLevel::SOFTWARE]) << "Missing software Keymaster device";
141 CHECK(kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT])
142 << "Error no viable keymaster device found";
Shawn Willden814a6e72016-03-15 08:37:29 -0600143
Shawn Willden0329a822017-12-04 13:55:14 -0700144 CHECK(configure_selinux() != -1) << "Failed to configure SELinux.";
Shawn Willden6507c272016-01-05 22:51:48 -0700145
Janis Danisevskisc1460142017-12-18 16:48:46 -0800146 auto halVersion = kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]->halVersion();
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000147
Shawn Willden0329a822017-12-04 13:55:14 -0700148 // 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 Danisevskisc1460142017-12-18 16:48:46 -0800151 SecurityLevel minimalAllowedSecurityLevelForNewKeys =
152 halVersion.majorVersion >= 2 ? SecurityLevel::TRUSTED_ENVIRONMENT : SecurityLevel::SOFTWARE;
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000153
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700154 android::sp<keystore::KeyStore> keyStore(
Branden Archer44d1afa2018-12-28 09:10:49 -0800155 new keystore::KeyStore(kmDevices, minimalAllowedSecurityLevelForNewKeys));
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700156 keyStore->initialize();
Shawn Willden6507c272016-01-05 22:51:48 -0700157 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700158 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(keyStore);
Steven Moreland23115b02019-01-10 16:20:20 -0800159 service->setRequestingSid(true);
Shawn Willden6507c272016-01-05 22:51:48 -0700160 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
Shawn Willden0329a822017-12-04 13:55:14 -0700161 CHECK(ret == android::OK) << "Couldn't register binder service!";
Shawn Willden6507c272016-01-05 22:51:48 -0700162
Roshan Piuse653c932017-03-29 10:08:47 -0700163 /**
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 Wang0ff25682019-08-21 09:32:56 -0700171 CHECK(err == android::OK) << "Cannot register wifi keystore HAL service: " << err;
Roshan Piuse653c932017-03-29 10:08:47 -0700172
Shawn Willden6507c272016-01-05 22:51:48 -0700173 /*
Roshan Piuse653c932017-03-29 10:08:47 -0700174 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -0700175 */
176 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700177 return 1;
178}