blob: 59361b415c9cfa63888bc76790933184d6a4a102 [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 Willdenc67a8aa2017-12-03 17:51:29 -070020#include <android/system/wifi/keystore/1.0/IKeystore.h>
Shawn Willden6507c272016-01-05 22:51:48 -070021#include <binder/IPCThreadState.h>
22#include <binder/IServiceManager.h>
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070023#include <cutils/log.h>
24#include <utils/StrongPointer.h>
Roshan Piuse653c932017-03-29 10:08:47 -070025#include <wifikeystorehal/keystore.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010026
Shawn Willdenfa5702f2017-12-03 15:14:58 -070027#include "KeyStore.h"
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070028#include "Keymaster3.h"
Shawn Willden6507c272016-01-05 22:51:48 -070029#include "entropy.h"
Janis Danisevskise8ba1802017-01-30 10:49:51 +000030#include "include/keystore/keystore_hidl_support.h"
31#include "include/keystore/keystore_return_types.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070032#include "key_store_service.h"
33#include "legacy_keymaster_device_wrapper.h"
34#include "permissions.h"
35#include <android/security/IKeystoreService.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
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070043using ::android::sp;
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
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070048using keystore::Keymaster;
49
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010050/**
51 * TODO implement keystore daemon using binderized keymaster HAL.
52 */
Shawn Willden6507c272016-01-05 22:51:48 -070053
54int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -070055 using android::hardware::hidl_string;
Shawn Willden6507c272016-01-05 22:51:48 -070056 if (argc < 2) {
57 ALOGE("A directory must be specified!");
58 return 1;
59 }
60 if (chdir(argv[1]) == -1) {
61 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
62 return 1;
63 }
64
65 Entropy entropy;
66 if (!entropy.open()) {
67 return 1;
68 }
69
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070070 auto hwdev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
71 if (hwdev.get() == nullptr) return -1;
72 sp<Keymaster> dev = new keystore::Keymaster3(hwdev);
73
74 auto fbdev = android::keystore::makeSoftwareKeymasterDevice();
75 if (fbdev.get() == nullptr) return -1;
76 sp<Keymaster> fallback = new keystore::Keymaster3(fbdev);
Shawn Willden814a6e72016-03-15 08:37:29 -060077
Shawn Willden6507c272016-01-05 22:51:48 -070078 if (configure_selinux() == -1) {
79 return -1;
80 }
81
Janis Danisevskise8ba1802017-01-30 10:49:51 +000082 bool allowNewFallbackDevice = false;
83
84 keystore::KeyStoreServiceReturnCode rc;
Shawn Willdenfa5702f2017-12-03 15:14:58 -070085 rc = KS_HANDLE_HIDL_ERROR(
86 dev->getHardwareFeatures([&](bool, bool, bool, bool supportsAttestation, bool,
87 const hidl_string&, const hidl_string&) {
88 // Attestation support indicates the hardware is keymaster 2.0 or higher.
89 // For these devices we will not allow the fallback device for import or generation
90 // of keys. The fallback device is only used for legacy keys present on the device.
91 allowNewFallbackDevice = !supportsAttestation;
92 }));
Janis Danisevskise8ba1802017-01-30 10:49:51 +000093
94 if (!rc.isOk()) {
95 return -1;
96 }
97
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070098 keystore::KeyStore keyStore(&entropy, dev, fallback, allowNewFallbackDevice);
Shawn Willden6507c272016-01-05 22:51:48 -070099 keyStore.initialize();
100 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100101 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -0700102 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
103 if (ret != android::OK) {
104 ALOGE("Couldn't register binder service!");
105 return -1;
106 }
107
Roshan Piuse653c932017-03-29 10:08:47 -0700108 /**
109 * Register the wifi keystore HAL service to run in passthrough mode.
110 * This will spawn off a new thread which will service the HIDL
111 * transactions.
112 */
113 configureRpcThreadpool(1, false /* callerWillJoin */);
114 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
115 android::status_t err = wifiKeystoreHalService->registerAsService();
116 if (ret != android::OK) {
117 ALOGE("Cannot register wifi keystore HAL service: %d", err);
118 }
119
Shawn Willden6507c272016-01-05 22:51:48 -0700120 /*
Roshan Piuse653c932017-03-29 10:08:47 -0700121 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -0700122 */
123 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700124 return 1;
125}