blob: e1fdd3f896f4401085d5042ed3b7b208186feb0c [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
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 Danisevskis3506d332017-12-19 16:27:28 -0800111void 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 Danisevskisc1460142017-12-18 16:48:46 -0800157KeymasterDevices 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());
Janis Danisevskisc1460142017-12-18 16:48:46 -0800161 auto softKeymaster = result[SecurityLevel::SOFTWARE];
Janis Danisevskis3506d332017-12-19 16:27:28 -0800162 if (result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
163 performHmacKeyHandshake(
164 {result[SecurityLevel::TRUSTED_ENVIRONMENT], result[SecurityLevel::STRONGBOX]});
165 } else {
Janis Danisevskisc1460142017-12-18 16:48:46 -0800166 result = enumerateKeymasterDevices<Keymaster3>(serviceManager.get());
167 }
168 if (softKeymaster) result[SecurityLevel::SOFTWARE] = softKeymaster;
169 if (result[SecurityLevel::SOFTWARE] && !result[SecurityLevel::TRUSTED_ENVIRONMENT]) {
170 LOG(WARNING) << "No secure Keymaster implementation found, but device offers insecure"
171 " Keymaster HAL. Using as default.";
172 result[SecurityLevel::TRUSTED_ENVIRONMENT] = result[SecurityLevel::SOFTWARE];
173 result[SecurityLevel::SOFTWARE] = nullptr;
174 }
175 if (!result[SecurityLevel::SOFTWARE]) {
176 auto fbdev = android::keystore::makeSoftwareKeymasterDevice();
177 CHECK(fbdev.get()) << "Unable to create Software Keymaster Device";
Shawn Willdeneedcfe92018-01-18 15:35:46 -0700178 result[SecurityLevel::SOFTWARE] = new Keymaster3(fbdev);
Janis Danisevskisc1460142017-12-18 16:48:46 -0800179 }
180 return result;
181}
Shawn Willden6507c272016-01-05 22:51:48 -0700182
183int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -0700184 using android::hardware::hidl_string;
Shawn Willden0329a822017-12-04 13:55:14 -0700185 CHECK(argc >= 2) << "A directory must be specified!";
186 CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno);
Shawn Willden6507c272016-01-05 22:51:48 -0700187
188 Entropy entropy;
Shawn Willden0329a822017-12-04 13:55:14 -0700189 CHECK(entropy.open()) << "Failed to open entropy source.";
Shawn Willden6507c272016-01-05 22:51:48 -0700190
Janis Danisevskisc1460142017-12-18 16:48:46 -0800191 auto kmDevices = initializeKeymasters();
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700192
Janis Danisevskisc1460142017-12-18 16:48:46 -0800193 CHECK(kmDevices[SecurityLevel::SOFTWARE]) << "Missing software Keymaster device";
194 CHECK(kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT])
195 << "Error no viable keymaster device found";
Shawn Willden814a6e72016-03-15 08:37:29 -0600196
Shawn Willden0329a822017-12-04 13:55:14 -0700197 CHECK(configure_selinux() != -1) << "Failed to configure SELinux.";
Shawn Willden6507c272016-01-05 22:51:48 -0700198
Janis Danisevskisc1460142017-12-18 16:48:46 -0800199 auto halVersion = kmDevices[SecurityLevel::TRUSTED_ENVIRONMENT]->halVersion();
Shawn Willden0329a822017-12-04 13:55:14 -0700200 CHECK(halVersion.error == keystore::ErrorCode::OK)
201 << "Error " << toString(halVersion.error) << " getting HAL version";
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000202
Shawn Willden0329a822017-12-04 13:55:14 -0700203 // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import
204 // or generation of keys. The fallback device is only used for legacy keys present on the
205 // device.
Janis Danisevskisc1460142017-12-18 16:48:46 -0800206 SecurityLevel minimalAllowedSecurityLevelForNewKeys =
207 halVersion.majorVersion >= 2 ? SecurityLevel::TRUSTED_ENVIRONMENT : SecurityLevel::SOFTWARE;
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000208
Janis Danisevskisc1460142017-12-18 16:48:46 -0800209 keystore::KeyStore keyStore(&entropy, kmDevices, minimalAllowedSecurityLevelForNewKeys);
Shawn Willden6507c272016-01-05 22:51:48 -0700210 keyStore.initialize();
211 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100212 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -0700213 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
Shawn Willden0329a822017-12-04 13:55:14 -0700214 CHECK(ret == android::OK) << "Couldn't register binder service!";
Shawn Willden6507c272016-01-05 22:51:48 -0700215
Roshan Piuse653c932017-03-29 10:08:47 -0700216 /**
217 * Register the wifi keystore HAL service to run in passthrough mode.
218 * This will spawn off a new thread which will service the HIDL
219 * transactions.
220 */
221 configureRpcThreadpool(1, false /* callerWillJoin */);
222 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
223 android::status_t err = wifiKeystoreHalService->registerAsService();
Shawn Willden0329a822017-12-04 13:55:14 -0700224 CHECK(ret == android::OK) << "Cannot register wifi keystore HAL service: " << err;
Roshan Piuse653c932017-03-29 10:08:47 -0700225
Shawn Willden6507c272016-01-05 22:51:48 -0700226 /*
Roshan Piuse653c932017-03-29 10:08:47 -0700227 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -0700228 */
229 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700230 return 1;
231}