blob: c05d14280a1f2621eedc56055b69f079eccb6d00 [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>
Janis Danisevskisc1460142017-12-18 16:48:46 -080020#include <android/hidl/manager/1.1/IServiceManager.h>
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070021#include <android/security/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 Willden6507c272016-01-05 22:51:48 -070035#include "entropy.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070036#include "key_store_service.h"
37#include "legacy_keymaster_device_wrapper.h"
38#include "permissions.h"
Shawn Willden6507c272016-01-05 22:51:48 -070039
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 Willdenc67a8aa2017-12-03 17:51:29 -070046using ::android::sp;
Shawn Willdenfa5702f2017-12-03 15:14:58 -070047using ::android::hardware::configureRpcThreadpool;
Roshan Piuse653c932017-03-29 10:08:47 -070048using ::android::system::wifi::keystore::V1_0::IKeystore;
49using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
Janis Danisevskisc1460142017-12-18 16:48:46 -080050using ::android::hidl::manager::V1_1::IServiceManager;
51using ::android::hardware::hidl_string;
52using ::android::hardware::hidl_vec;
53using ::android::hardware::keymaster::V4_0::SecurityLevel;
Janis Danisevskis3506d332017-12-19 16:27:28 -080054using ::android::hardware::keymaster::V4_0::HmacSharingParameters;
55using ::android::hardware::keymaster::V4_0::ErrorCode;
Roshan Piuse653c932017-03-29 10:08:47 -070056
Shawn Willdeneedcfe92018-01-18 15:35:46 -070057using ::keystore::keymaster::support::Keymaster;
58using ::keystore::keymaster::support::Keymaster3;
59using ::keystore::keymaster::support::Keymaster4;
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070060
Janis Danisevskisc1460142017-12-18 16:48:46 -080061using keystore::KeymasterDevices;
62
63template <typename Wrapper>
64KeymasterDevices 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
Shawn Willdencd416c52018-01-22 09:37:43 -070075 sp<Keymaster> kmDevice(new Wrapper(device, name));
Janis Danisevskisc1460142017-12-18 16:48:46 -080076 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
111KeymasterDevices initializeKeymasters() {
112 auto serviceManager = android::hidl::manager::V1_1::IServiceManager::getService();
113 CHECK(serviceManager.get()) << "Failed to get ServiceManager";
114 auto result = enumerateKeymasterDevices<Keymaster4>(serviceManager.get());
Janis Danisevskisc1460142017-12-18 16:48:46 -0800115 auto softKeymaster = result[SecurityLevel::SOFTWARE];
Shawn Willden5a6afd02018-05-09 15:47:15 -0600116 if (!result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
Janis Danisevskisc1460142017-12-18 16:48:46 -0800117 result = enumerateKeymasterDevices<Keymaster3>(serviceManager.get());
118 }
119 if (softKeymaster) result[SecurityLevel::SOFTWARE] = softKeymaster;
120 if (result[SecurityLevel::SOFTWARE] && !result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
121 LOG(WARNING) << "No secure Keymaster implementation found, but device offers insecure"
122 " Keymaster HAL. Using as default.";
123 result[SecurityLevel::TRUSTED_ENVIRONMENT] = result[SecurityLevel::SOFTWARE];
124 result[SecurityLevel::SOFTWARE] = nullptr;
125 }
126 if (!result[SecurityLevel::SOFTWARE]) {
127 auto fbdev = android::keystore::makeSoftwareKeymasterDevice();
128 CHECK(fbdev.get()) << "Unable to create Software Keymaster Device";
Shawn Willdencd416c52018-01-22 09:37:43 -0700129 result[SecurityLevel::SOFTWARE] = new Keymaster3(fbdev, "Software");
Janis Danisevskisc1460142017-12-18 16:48:46 -0800130 }
131 return result;
132}
Shawn Willden6507c272016-01-05 22:51:48 -0700133
134int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -0700135 using android::hardware::hidl_string;
Shawn Willden0329a822017-12-04 13:55:14 -0700136 CHECK(argc >= 2) << "A directory must be specified!";
137 CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno);
Shawn Willden6507c272016-01-05 22:51:48 -0700138
139 Entropy entropy;
Shawn Willden0329a822017-12-04 13:55:14 -0700140 CHECK(entropy.open()) << "Failed to open entropy source.";
Shawn Willden6507c272016-01-05 22:51:48 -0700141
Janis Danisevskisc1460142017-12-18 16:48:46 -0800142 auto kmDevices = initializeKeymasters();
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700143
Janis Danisevskisc1460142017-12-18 16:48:46 -0800144 CHECK(kmDevices[SecurityLevel::SOFTWARE]) << "Missing software Keymaster device";
145 CHECK(kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT])
146 << "Error no viable keymaster device found";
Shawn Willden814a6e72016-03-15 08:37:29 -0600147
Shawn Willden0329a822017-12-04 13:55:14 -0700148 CHECK(configure_selinux() != -1) << "Failed to configure SELinux.";
Shawn Willden6507c272016-01-05 22:51:48 -0700149
Janis Danisevskisc1460142017-12-18 16:48:46 -0800150 auto halVersion = kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]->halVersion();
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000151
Shawn Willden0329a822017-12-04 13:55:14 -0700152 // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import
153 // or generation of keys. The fallback device is only used for legacy keys present on the
154 // device.
Janis Danisevskisc1460142017-12-18 16:48:46 -0800155 SecurityLevel minimalAllowedSecurityLevelForNewKeys =
156 halVersion.majorVersion >= 2 ? SecurityLevel::TRUSTED_ENVIRONMENT : SecurityLevel::SOFTWARE;
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000157
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700158 android::sp<keystore::KeyStore> keyStore(
159 new keystore::KeyStore(&entropy, kmDevices, minimalAllowedSecurityLevelForNewKeys));
160 keyStore->initialize();
Shawn Willden6507c272016-01-05 22:51:48 -0700161 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700162 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -0700163 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
Shawn Willden0329a822017-12-04 13:55:14 -0700164 CHECK(ret == android::OK) << "Couldn't register binder service!";
Shawn Willden6507c272016-01-05 22:51:48 -0700165
Roshan Piuse653c932017-03-29 10:08:47 -0700166 /**
167 * Register the wifi keystore HAL service to run in passthrough mode.
168 * This will spawn off a new thread which will service the HIDL
169 * transactions.
170 */
171 configureRpcThreadpool(1, false /* callerWillJoin */);
172 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
173 android::status_t err = wifiKeystoreHalService->registerAsService();
Shawn Willden0329a822017-12-04 13:55:14 -0700174 CHECK(ret == android::OK) << "Cannot register wifi keystore HAL service: " << err;
Roshan Piuse653c932017-03-29 10:08:47 -0700175
Shawn Willden6507c272016-01-05 22:51:48 -0700176 /*
Roshan Piuse653c932017-03-29 10:08:47 -0700177 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -0700178 */
179 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700180 return 1;
181}