blob: f4ccf0460949a8ea7f72ebd5a6c94e481e97300c [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
Shawn Willden0329a822017-12-04 13:55:14 -070017#include <android-base/logging.h>
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070018#include <android/system/wifi/keystore/1.0/IKeystore.h>
Shawn Willden6507c272016-01-05 22:51:48 -070019#include <binder/IPCThreadState.h>
20#include <binder/IServiceManager.h>
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070021#include <utils/StrongPointer.h>
Roshan Piuse653c932017-03-29 10:08:47 -070022#include <wifikeystorehal/keystore.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010023
Shawn Willdenfa5702f2017-12-03 15:14:58 -070024#include "KeyStore.h"
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070025#include "Keymaster3.h"
Shawn Willden6507c272016-01-05 22:51:48 -070026#include "entropy.h"
Janis Danisevskise8ba1802017-01-30 10:49:51 +000027#include "include/keystore/keystore_hidl_support.h"
28#include "include/keystore/keystore_return_types.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070029#include "key_store_service.h"
30#include "legacy_keymaster_device_wrapper.h"
31#include "permissions.h"
32#include <android/security/IKeystoreService.h>
Shawn Willden6507c272016-01-05 22:51:48 -070033
34/* KeyStore is a secured storage for key-value pairs. In this implementation,
35 * each file stores one key-value pair. Keys are encoded in file names, and
36 * values are encrypted with checksums. The encryption key is protected by a
37 * user-defined password. To keep things simple, buffers are always larger than
38 * the maximum space we needed, so boundary checks on buffers are omitted. */
39
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070040using ::android::sp;
Shawn Willdenfa5702f2017-12-03 15:14:58 -070041using ::android::hardware::configureRpcThreadpool;
Roshan Piuse653c932017-03-29 10:08:47 -070042using ::android::system::wifi::keystore::V1_0::IKeystore;
43using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
Roshan Piuse653c932017-03-29 10:08:47 -070044
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070045using keystore::Keymaster;
46
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010047/**
48 * TODO implement keystore daemon using binderized keymaster HAL.
49 */
Shawn Willden6507c272016-01-05 22:51:48 -070050
51int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -070052 using android::hardware::hidl_string;
Shawn Willden0329a822017-12-04 13:55:14 -070053 CHECK(argc >= 2) << "A directory must be specified!";
54 CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno);
Shawn Willden6507c272016-01-05 22:51:48 -070055
56 Entropy entropy;
Shawn Willden0329a822017-12-04 13:55:14 -070057 CHECK(entropy.open()) << "Failed to open entropy source.";
Shawn Willden6507c272016-01-05 22:51:48 -070058
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070059 auto hwdev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
Shawn Willden0329a822017-12-04 13:55:14 -070060 CHECK(hwdev.get()) << "Failed to load @3.0::IKeymasterDevice";
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070061 sp<Keymaster> dev = new keystore::Keymaster3(hwdev);
62
63 auto fbdev = android::keystore::makeSoftwareKeymasterDevice();
64 if (fbdev.get() == nullptr) return -1;
65 sp<Keymaster> fallback = new keystore::Keymaster3(fbdev);
Shawn Willden814a6e72016-03-15 08:37:29 -060066
Shawn Willden0329a822017-12-04 13:55:14 -070067 CHECK(configure_selinux() != -1) << "Failed to configure SELinux.";
Shawn Willden6507c272016-01-05 22:51:48 -070068
Shawn Willden0329a822017-12-04 13:55:14 -070069 auto halVersion = dev->halVersion();
70 CHECK(halVersion.error == keystore::ErrorCode::OK)
71 << "Error " << toString(halVersion.error) << " getting HAL version";
Janis Danisevskise8ba1802017-01-30 10:49:51 +000072
Shawn Willden0329a822017-12-04 13:55:14 -070073 // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import
74 // or generation of keys. The fallback device is only used for legacy keys present on the
75 // device.
76 bool allowNewFallbackDevice = halVersion.majorVersion >= 2 && halVersion.isSecure;
Janis Danisevskise8ba1802017-01-30 10:49:51 +000077
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070078 keystore::KeyStore keyStore(&entropy, dev, fallback, allowNewFallbackDevice);
Shawn Willden6507c272016-01-05 22:51:48 -070079 keyStore.initialize();
80 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010081 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -070082 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
Shawn Willden0329a822017-12-04 13:55:14 -070083 CHECK(ret == android::OK) << "Couldn't register binder service!";
Shawn Willden6507c272016-01-05 22:51:48 -070084
Roshan Piuse653c932017-03-29 10:08:47 -070085 /**
86 * Register the wifi keystore HAL service to run in passthrough mode.
87 * This will spawn off a new thread which will service the HIDL
88 * transactions.
89 */
90 configureRpcThreadpool(1, false /* callerWillJoin */);
91 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
92 android::status_t err = wifiKeystoreHalService->registerAsService();
Shawn Willden0329a822017-12-04 13:55:14 -070093 CHECK(ret == android::OK) << "Cannot register wifi keystore HAL service: " << err;
Roshan Piuse653c932017-03-29 10:08:47 -070094
Shawn Willden6507c272016-01-05 22:51:48 -070095 /*
Roshan Piuse653c932017-03-29 10:08:47 -070096 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -070097 */
98 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -070099 return 1;
100}