blob: 1cdb3984be1d7a5774a4d414a7e0bc758dc5d320 [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>
24
Shawn Willden6507c272016-01-05 22:51:48 -070025#include <cutils/log.h>
26
27#include "entropy.h"
28#include "key_store_service.h"
29#include "keystore.h"
30#include "permissions.h"
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010031#include "legacy_keymaster_device_wrapper.h"
Janis Danisevskise8ba1802017-01-30 10:49:51 +000032#include "include/keystore/keystore_hidl_support.h"
33#include "include/keystore/keystore_return_types.h"
Shawn Willden6507c272016-01-05 22:51:48 -070034
35/* KeyStore is a secured storage for key-value pairs. In this implementation,
36 * each file stores one key-value pair. Keys are encoded in file names, and
37 * values are encrypted with checksums. The encryption key is protected by a
38 * user-defined password. To keep things simple, buffers are always larger than
39 * the maximum space we needed, so boundary checks on buffers are omitted. */
40
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010041/**
42 * TODO implement keystore daemon using binderized keymaster HAL.
43 */
Shawn Willden6507c272016-01-05 22:51:48 -070044
45int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -070046 using android::hardware::hidl_string;
Shawn Willden6507c272016-01-05 22:51:48 -070047 if (argc < 2) {
48 ALOGE("A directory must be specified!");
49 return 1;
50 }
51 if (chdir(argv[1]) == -1) {
52 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
53 return 1;
54 }
55
56 Entropy entropy;
57 if (!entropy.open()) {
58 return 1;
59 }
60
Chris Phoenix9b3791c2017-01-25 15:15:35 -080061 auto dev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010062 if (dev.get() == nullptr) {
63 return -1;
Shawn Willden6507c272016-01-05 22:51:48 -070064 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010065 auto fallback = android::keystore::makeSoftwareKeymasterDevice();
66 if (dev.get() == nullptr) {
67 return -1;
Shawn Willden814a6e72016-03-15 08:37:29 -060068 }
69
Shawn Willden6507c272016-01-05 22:51:48 -070070 if (configure_selinux() == -1) {
71 return -1;
72 }
73
Janis Danisevskise8ba1802017-01-30 10:49:51 +000074 bool allowNewFallbackDevice = false;
75
76 keystore::KeyStoreServiceReturnCode rc;
77 rc = KS_HANDLE_HIDL_ERROR(dev->getHardwareFeatures(
Shawn Willdenb8550a02017-02-23 11:06:05 -070078 [&] (bool, bool, bool, bool supportsAttestation, bool, const hidl_string&,
79 const hidl_string&) {
Janis Danisevskise8ba1802017-01-30 10:49:51 +000080 // Attestation support indicates the hardware is keymaster 2.0 or higher.
81 // For these devices we will not allow the fallback device for import or generation
82 // of keys. The fallback device is only used for legacy keys present on the device.
83 allowNewFallbackDevice = !supportsAttestation;
84 }));
85
86 if (!rc.isOk()) {
87 return -1;
88 }
89
90 KeyStore keyStore(&entropy, dev, fallback, allowNewFallbackDevice);
Shawn Willden6507c272016-01-05 22:51:48 -070091 keyStore.initialize();
92 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010093 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -070094 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
95 if (ret != android::OK) {
96 ALOGE("Couldn't register binder service!");
97 return -1;
98 }
99
100 /*
101 * We're the only thread in existence, so we're just going to process
102 * Binder transaction as a single-threaded program.
103 */
104 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700105 return 1;
106}