Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 20 | #include <binder/IPCThreadState.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 23 | #include <android/hardware/keymaster/3.0/IHwKeymasterDevice.h> |
| 24 | |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 25 | #include <cutils/log.h> |
| 26 | |
| 27 | #include "entropy.h" |
| 28 | #include "key_store_service.h" |
| 29 | #include "keystore.h" |
| 30 | #include "permissions.h" |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 31 | #include "legacy_keymaster_device_wrapper.h" |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 32 | #include "include/keystore/keystore_hidl_support.h" |
| 33 | #include "include/keystore/keystore_return_types.h" |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 34 | |
| 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 Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 41 | /** |
| 42 | * TODO implement keystore daemon using binderized keymaster HAL. |
| 43 | */ |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 44 | |
| 45 | int main(int argc, char* argv[]) { |
Shawn Willden | b8550a0 | 2017-02-23 11:06:05 -0700 | [diff] [blame] | 46 | using android::hardware::hidl_string; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 47 | 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 Phoenix | 9b3791c | 2017-01-25 15:15:35 -0800 | [diff] [blame] | 61 | auto dev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 62 | if (dev.get() == nullptr) { |
| 63 | return -1; |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 64 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 65 | auto fallback = android::keystore::makeSoftwareKeymasterDevice(); |
| 66 | if (dev.get() == nullptr) { |
| 67 | return -1; |
Shawn Willden | 814a6e7 | 2016-03-15 08:37:29 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 70 | if (configure_selinux() == -1) { |
| 71 | return -1; |
| 72 | } |
| 73 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 74 | bool allowNewFallbackDevice = false; |
| 75 | |
| 76 | keystore::KeyStoreServiceReturnCode rc; |
| 77 | rc = KS_HANDLE_HIDL_ERROR(dev->getHardwareFeatures( |
Shawn Willden | b8550a0 | 2017-02-23 11:06:05 -0700 | [diff] [blame] | 78 | [&] (bool, bool, bool, bool supportsAttestation, bool, const hidl_string&, |
| 79 | const hidl_string&) { |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 80 | // 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 Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 91 | keyStore.initialize(); |
| 92 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 93 | android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore); |
Shawn Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 94 | 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 Willden | 6507c27 | 2016-01-05 22:51:48 -0700 | [diff] [blame] | 105 | return 1; |
| 106 | } |