blob: a739c5e1fa9022972c0ddbd6f67af6c52627a153 [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
17//#define LOG_NDEBUG 0
18#define LOG_TAG "keystore"
19
Shawn Willden6507c272016-01-05 22:51:48 -070020#include <binder/IPCThreadState.h>
21#include <binder/IServiceManager.h>
22
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010023#include <android/hardware/keymaster/3.0/IHwKeymasterDevice.h>
Roshan Piuse653c932017-03-29 10:08:47 -070024#include <android/system/wifi/keystore/1.0/IKeystore.h>
25#include <wifikeystorehal/keystore.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010026
Shawn Willden6507c272016-01-05 22:51:48 -070027#include <cutils/log.h>
28
29#include "entropy.h"
30#include "key_store_service.h"
31#include "keystore.h"
32#include "permissions.h"
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010033#include "legacy_keymaster_device_wrapper.h"
Janis Danisevskise8ba1802017-01-30 10:49:51 +000034#include "include/keystore/keystore_hidl_support.h"
35#include "include/keystore/keystore_return_types.h"
Shawn Willden6507c272016-01-05 22:51:48 -070036
37/* KeyStore is a secured storage for key-value pairs. In this implementation,
38 * each file stores one key-value pair. Keys are encoded in file names, and
39 * values are encrypted with checksums. The encryption key is protected by a
40 * user-defined password. To keep things simple, buffers are always larger than
41 * the maximum space we needed, so boundary checks on buffers are omitted. */
42
Roshan Piuse653c932017-03-29 10:08:47 -070043using ::android::system::wifi::keystore::V1_0::IKeystore;
44using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
45using ::android::hardware::configureRpcThreadpool;
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 Willden6507c272016-01-05 22:51:48 -070053 if (argc < 2) {
54 ALOGE("A directory must be specified!");
55 return 1;
56 }
57 if (chdir(argv[1]) == -1) {
58 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
59 return 1;
60 }
61
62 Entropy entropy;
63 if (!entropy.open()) {
64 return 1;
65 }
66
Chris Phoenix9b3791c2017-01-25 15:15:35 -080067 auto dev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010068 if (dev.get() == nullptr) {
69 return -1;
Shawn Willden6507c272016-01-05 22:51:48 -070070 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010071 auto fallback = android::keystore::makeSoftwareKeymasterDevice();
72 if (dev.get() == nullptr) {
73 return -1;
Shawn Willden814a6e72016-03-15 08:37:29 -060074 }
75
Shawn Willden6507c272016-01-05 22:51:48 -070076 if (configure_selinux() == -1) {
77 return -1;
78 }
79
Janis Danisevskise8ba1802017-01-30 10:49:51 +000080 bool allowNewFallbackDevice = false;
81
82 keystore::KeyStoreServiceReturnCode rc;
83 rc = KS_HANDLE_HIDL_ERROR(dev->getHardwareFeatures(
Shawn Willdenb8550a02017-02-23 11:06:05 -070084 [&] (bool, bool, bool, bool supportsAttestation, bool, const hidl_string&,
85 const hidl_string&) {
Janis Danisevskise8ba1802017-01-30 10:49:51 +000086 // Attestation support indicates the hardware is keymaster 2.0 or higher.
87 // For these devices we will not allow the fallback device for import or generation
88 // of keys. The fallback device is only used for legacy keys present on the device.
89 allowNewFallbackDevice = !supportsAttestation;
90 }));
91
92 if (!rc.isOk()) {
93 return -1;
94 }
95
96 KeyStore keyStore(&entropy, dev, fallback, allowNewFallbackDevice);
Shawn Willden6507c272016-01-05 22:51:48 -070097 keyStore.initialize();
98 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010099 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -0700100 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
101 if (ret != android::OK) {
102 ALOGE("Couldn't register binder service!");
103 return -1;
104 }
105
Roshan Piuse653c932017-03-29 10:08:47 -0700106 /**
107 * Register the wifi keystore HAL service to run in passthrough mode.
108 * This will spawn off a new thread which will service the HIDL
109 * transactions.
110 */
111 configureRpcThreadpool(1, false /* callerWillJoin */);
112 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
113 android::status_t err = wifiKeystoreHalService->registerAsService();
114 if (ret != android::OK) {
115 ALOGE("Cannot register wifi keystore HAL service: %d", err);
116 }
117
Shawn Willden6507c272016-01-05 22:51:48 -0700118 /*
Roshan Piuse653c932017-03-29 10:08:47 -0700119 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -0700120 */
121 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700122 return 1;
123}