blob: 1416a2b582081916867f14b1234de9e9ef042cdb [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[]) {
46 if (argc < 2) {
47 ALOGE("A directory must be specified!");
48 return 1;
49 }
50 if (chdir(argv[1]) == -1) {
51 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
52 return 1;
53 }
54
55 Entropy entropy;
56 if (!entropy.open()) {
57 return 1;
58 }
59
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010060 auto dev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService("keymaster");
61 if (dev.get() == nullptr) {
62 return -1;
Shawn Willden6507c272016-01-05 22:51:48 -070063 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010064 auto fallback = android::keystore::makeSoftwareKeymasterDevice();
65 if (dev.get() == nullptr) {
66 return -1;
Shawn Willden814a6e72016-03-15 08:37:29 -060067 }
68
Shawn Willden6507c272016-01-05 22:51:48 -070069 if (configure_selinux() == -1) {
70 return -1;
71 }
72
Janis Danisevskise8ba1802017-01-30 10:49:51 +000073 bool allowNewFallbackDevice = false;
74
75 keystore::KeyStoreServiceReturnCode rc;
76 rc = KS_HANDLE_HIDL_ERROR(dev->getHardwareFeatures(
77 [&] (bool, bool, bool, bool supportsAttestation) {
78 // Attestation support indicates the hardware is keymaster 2.0 or higher.
79 // For these devices we will not allow the fallback device for import or generation
80 // of keys. The fallback device is only used for legacy keys present on the device.
81 allowNewFallbackDevice = !supportsAttestation;
82 }));
83
84 if (!rc.isOk()) {
85 return -1;
86 }
87
88 KeyStore keyStore(&entropy, dev, fallback, allowNewFallbackDevice);
Shawn Willden6507c272016-01-05 22:51:48 -070089 keyStore.initialize();
90 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010091 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -070092 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
93 if (ret != android::OK) {
94 ALOGE("Couldn't register binder service!");
95 return -1;
96 }
97
98 /*
99 * We're the only thread in existence, so we're just going to process
100 * Binder transaction as a single-threaded program.
101 */
102 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700103 return 1;
104}