blob: 8a00a8df47bbd7d2df03caee8eb2b72c7ef7c964 [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 Willdenc67a8aa2017-12-03 17:51:29 -070026#include <utils/StrongPointer.h>
Roshan Piuse653c932017-03-29 10:08:47 -070027#include <wifikeystorehal/keystore.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010028
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070029#include <keystore/keystore_hidl_support.h>
30#include <keystore/keystore_return_types.h>
31
Shawn Willdenfa5702f2017-12-03 15:14:58 -070032#include "KeyStore.h"
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070033#include "Keymaster3.h"
Janis Danisevskisc1460142017-12-18 16:48:46 -080034#include "Keymaster4.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;
Roshan Piuse653c932017-03-29 10:08:47 -070054
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070055using keystore::Keymaster;
Janis Danisevskisc1460142017-12-18 16:48:46 -080056using keystore::Keymaster3;
57using keystore::Keymaster4;
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070058
Janis Danisevskisc1460142017-12-18 16:48:46 -080059using keystore::KeymasterDevices;
60
61template <typename Wrapper>
62KeymasterDevices enumerateKeymasterDevices(IServiceManager* serviceManager) {
63 KeymasterDevices result;
64 serviceManager->listByInterface(
65 Wrapper::WrappedIKeymasterDevice::descriptor, [&](const hidl_vec<hidl_string>& names) {
66 auto try_get_device = [&](const auto& name, bool fail_silent) {
67 auto device = Wrapper::WrappedIKeymasterDevice::getService(name);
68 if (fail_silent && !device) return;
69 CHECK(device) << "Failed to get service for \""
70 << Wrapper::WrappedIKeymasterDevice::descriptor
71 << "\" with interface name \"" << name << "\"";
72
73 sp<Keymaster> kmDevice(new Wrapper(device));
74 auto halVersion = kmDevice->halVersion();
75 SecurityLevel securityLevel = halVersion.securityLevel;
76 LOG(INFO) << "found " << Wrapper::WrappedIKeymasterDevice::descriptor
77 << " with interface name " << name << " and seclevel "
78 << toString(securityLevel);
79 CHECK(static_cast<uint32_t>(securityLevel) < result.size())
80 << "Security level of \"" << Wrapper::WrappedIKeymasterDevice::descriptor
81 << "\" with interface name \"" << name << "\" out of range";
82 auto& deviceSlot = result[securityLevel];
83 if (deviceSlot) {
84 if (!fail_silent) {
85 LOG(WARNING) << "Implementation of \""
86 << Wrapper::WrappedIKeymasterDevice::descriptor
87 << "\" with interface name \"" << name
88 << "\" and security level: " << toString(securityLevel)
89 << " Masked by other implementation of Keymaster";
90 }
91 } else {
92 deviceSlot = kmDevice;
93 }
94 };
95 bool has_default = false;
96 for (auto& n : names) {
97 try_get_device(n, false);
98 if (n == "default") has_default = true;
99 }
100 // Make sure that we always check the default device. If we enumerate only what is
101 // known to hwservicemanager, we miss a possible passthrough HAL.
102 if (!has_default) {
103 try_get_device("default", true /* fail_silent */);
104 }
105 });
106 return result;
107}
108
109KeymasterDevices initializeKeymasters() {
110 auto serviceManager = android::hidl::manager::V1_1::IServiceManager::getService();
111 CHECK(serviceManager.get()) << "Failed to get ServiceManager";
112 auto result = enumerateKeymasterDevices<Keymaster4>(serviceManager.get());
113 CHECK(result[SecurityLevel::TRUSTED_ENVIRONMENT] || !result[SecurityLevel::STRONGBOX])
114 << "We cannot have a Strongbox keymaster implementation without a TEE implementation";
115 auto softKeymaster = result[SecurityLevel::SOFTWARE];
116 if (!result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
117 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";
129 result[SecurityLevel::SOFTWARE] = new keystore::Keymaster3(fbdev);
130 }
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();
Shawn Willden0329a822017-12-04 13:55:14 -0700151 CHECK(halVersion.error == keystore::ErrorCode::OK)
152 << "Error " << toString(halVersion.error) << " getting HAL version";
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000153
Shawn Willden0329a822017-12-04 13:55:14 -0700154 // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import
155 // or generation of keys. The fallback device is only used for legacy keys present on the
156 // device.
Janis Danisevskisc1460142017-12-18 16:48:46 -0800157 SecurityLevel minimalAllowedSecurityLevelForNewKeys =
158 halVersion.majorVersion >= 2 ? SecurityLevel::TRUSTED_ENVIRONMENT : SecurityLevel::SOFTWARE;
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000159
Janis Danisevskisc1460142017-12-18 16:48:46 -0800160 keystore::KeyStore keyStore(&entropy, kmDevices, minimalAllowedSecurityLevelForNewKeys);
Shawn Willden6507c272016-01-05 22:51:48 -0700161 keyStore.initialize();
162 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100163 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -0700164 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
Shawn Willden0329a822017-12-04 13:55:14 -0700165 CHECK(ret == android::OK) << "Couldn't register binder service!";
Shawn Willden6507c272016-01-05 22:51:48 -0700166
Roshan Piuse653c932017-03-29 10:08:47 -0700167 /**
168 * Register the wifi keystore HAL service to run in passthrough mode.
169 * This will spawn off a new thread which will service the HIDL
170 * transactions.
171 */
172 configureRpcThreadpool(1, false /* callerWillJoin */);
173 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
174 android::status_t err = wifiKeystoreHalService->registerAsService();
Shawn Willden0329a822017-12-04 13:55:14 -0700175 CHECK(ret == android::OK) << "Cannot register wifi keystore HAL service: " << err;
Roshan Piuse653c932017-03-29 10:08:47 -0700176
Shawn Willden6507c272016-01-05 22:51:48 -0700177 /*
Roshan Piuse653c932017-03-29 10:08:47 -0700178 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -0700179 */
180 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700181 return 1;
182}