blob: e048f8889bcdc57fa22d2a727dda64b376513a54 [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
Shawn Willdenfa5702f2017-12-03 15:14:58 -070029#include "KeyStore.h"
Shawn Willden6507c272016-01-05 22:51:48 -070030#include "entropy.h"
Janis Danisevskise8ba1802017-01-30 10:49:51 +000031#include "include/keystore/keystore_hidl_support.h"
32#include "include/keystore/keystore_return_types.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070033#include "key_store_service.h"
34#include "legacy_keymaster_device_wrapper.h"
35#include "permissions.h"
36#include <android/security/IKeystoreService.h>
Shawn Willden6507c272016-01-05 22:51:48 -070037
38/* KeyStore is a secured storage for key-value pairs. In this implementation,
39 * each file stores one key-value pair. Keys are encoded in file names, and
40 * values are encrypted with checksums. The encryption key is protected by a
41 * user-defined password. To keep things simple, buffers are always larger than
42 * the maximum space we needed, so boundary checks on buffers are omitted. */
43
Shawn Willdenfa5702f2017-12-03 15:14:58 -070044using ::android::hardware::configureRpcThreadpool;
Roshan Piuse653c932017-03-29 10:08:47 -070045using ::android::system::wifi::keystore::V1_0::IKeystore;
46using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
Roshan Piuse653c932017-03-29 10:08:47 -070047
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010048/**
49 * TODO implement keystore daemon using binderized keymaster HAL.
50 */
Shawn Willden6507c272016-01-05 22:51:48 -070051
52int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -070053 using android::hardware::hidl_string;
Shawn Willden6507c272016-01-05 22:51:48 -070054 if (argc < 2) {
55 ALOGE("A directory must be specified!");
56 return 1;
57 }
58 if (chdir(argv[1]) == -1) {
59 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
60 return 1;
61 }
62
63 Entropy entropy;
64 if (!entropy.open()) {
65 return 1;
66 }
67
Chris Phoenix9b3791c2017-01-25 15:15:35 -080068 auto dev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010069 if (dev.get() == nullptr) {
70 return -1;
Shawn Willden6507c272016-01-05 22:51:48 -070071 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010072 auto fallback = android::keystore::makeSoftwareKeymasterDevice();
73 if (dev.get() == nullptr) {
74 return -1;
Shawn Willden814a6e72016-03-15 08:37:29 -060075 }
76
Shawn Willden6507c272016-01-05 22:51:48 -070077 if (configure_selinux() == -1) {
78 return -1;
79 }
80
Janis Danisevskise8ba1802017-01-30 10:49:51 +000081 bool allowNewFallbackDevice = false;
82
83 keystore::KeyStoreServiceReturnCode rc;
Shawn Willdenfa5702f2017-12-03 15:14:58 -070084 rc = KS_HANDLE_HIDL_ERROR(
85 dev->getHardwareFeatures([&](bool, bool, bool, bool supportsAttestation, bool,
86 const hidl_string&, const hidl_string&) {
87 // Attestation support indicates the hardware is keymaster 2.0 or higher.
88 // For these devices we will not allow the fallback device for import or generation
89 // of keys. The fallback device is only used for legacy keys present on the device.
90 allowNewFallbackDevice = !supportsAttestation;
91 }));
Janis Danisevskise8ba1802017-01-30 10:49:51 +000092
93 if (!rc.isOk()) {
94 return -1;
95 }
96
97 KeyStore keyStore(&entropy, dev, fallback, allowNewFallbackDevice);
Shawn Willden6507c272016-01-05 22:51:48 -070098 keyStore.initialize();
99 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100100 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -0700101 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
102 if (ret != android::OK) {
103 ALOGE("Couldn't register binder service!");
104 return -1;
105 }
106
Roshan Piuse653c932017-03-29 10:08:47 -0700107 /**
108 * Register the wifi keystore HAL service to run in passthrough mode.
109 * This will spawn off a new thread which will service the HIDL
110 * transactions.
111 */
112 configureRpcThreadpool(1, false /* callerWillJoin */);
113 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
114 android::status_t err = wifiKeystoreHalService->registerAsService();
115 if (ret != android::OK) {
116 ALOGE("Cannot register wifi keystore HAL service: %d", err);
117 }
118
Shawn Willden6507c272016-01-05 22:51:48 -0700119 /*
Roshan Piuse653c932017-03-29 10:08:47 -0700120 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -0700121 */
122 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700123 return 1;
124}