blob: 56fe0d5664f9be64a0458bb92ea64a00a69277cd [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
Shawn Willden0329a822017-12-04 13:55:14 -070017#include <android-base/logging.h>
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070018#include <android/security/IKeystoreService.h>
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070019#include <android/system/wifi/keystore/1.0/IKeystore.h>
Shawn Willden6507c272016-01-05 22:51:48 -070020#include <binder/IPCThreadState.h>
21#include <binder/IServiceManager.h>
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070022#include <hidl/HidlTransportSupport.h>
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070023#include <utils/StrongPointer.h>
Roshan Piuse653c932017-03-29 10:08:47 -070024#include <wifikeystorehal/keystore.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010025
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070026#include <keystore/keystore_hidl_support.h>
27#include <keystore/keystore_return_types.h>
28
Shawn Willdenfa5702f2017-12-03 15:14:58 -070029#include "KeyStore.h"
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070030#include "Keymaster3.h"
Shawn Willden6507c272016-01-05 22:51:48 -070031#include "entropy.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"
Shawn Willden6507c272016-01-05 22:51:48 -070035
36/* KeyStore is a secured storage for key-value pairs. In this implementation,
37 * each file stores one key-value pair. Keys are encoded in file names, and
38 * values are encrypted with checksums. The encryption key is protected by a
39 * user-defined password. To keep things simple, buffers are always larger than
40 * the maximum space we needed, so boundary checks on buffers are omitted. */
41
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070042using ::android::sp;
Shawn Willdenfa5702f2017-12-03 15:14:58 -070043using ::android::hardware::configureRpcThreadpool;
Roshan Piuse653c932017-03-29 10:08:47 -070044using ::android::system::wifi::keystore::V1_0::IKeystore;
45using ::android::system::wifi::keystore::V1_0::implementation::Keystore;
Roshan Piuse653c932017-03-29 10:08:47 -070046
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070047using keystore::Keymaster;
48
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010049/**
50 * TODO implement keystore daemon using binderized keymaster HAL.
51 */
Shawn Willden6507c272016-01-05 22:51:48 -070052
53int main(int argc, char* argv[]) {
Shawn Willdenb8550a02017-02-23 11:06:05 -070054 using android::hardware::hidl_string;
Shawn Willden0329a822017-12-04 13:55:14 -070055 CHECK(argc >= 2) << "A directory must be specified!";
56 CHECK(chdir(argv[1]) != -1) << "chdir: " << argv[1] << ": " << strerror(errno);
Shawn Willden6507c272016-01-05 22:51:48 -070057
58 Entropy entropy;
Shawn Willden0329a822017-12-04 13:55:14 -070059 CHECK(entropy.open()) << "Failed to open entropy source.";
Shawn Willden6507c272016-01-05 22:51:48 -070060
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070061 auto hwdev = android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
Shawn Willden0329a822017-12-04 13:55:14 -070062 CHECK(hwdev.get()) << "Failed to load @3.0::IKeymasterDevice";
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070063 sp<Keymaster> dev = new keystore::Keymaster3(hwdev);
64
65 auto fbdev = android::keystore::makeSoftwareKeymasterDevice();
66 if (fbdev.get() == nullptr) return -1;
67 sp<Keymaster> fallback = new keystore::Keymaster3(fbdev);
Shawn Willden814a6e72016-03-15 08:37:29 -060068
Shawn Willden0329a822017-12-04 13:55:14 -070069 CHECK(configure_selinux() != -1) << "Failed to configure SELinux.";
Shawn Willden6507c272016-01-05 22:51:48 -070070
Shawn Willden0329a822017-12-04 13:55:14 -070071 auto halVersion = dev->halVersion();
72 CHECK(halVersion.error == keystore::ErrorCode::OK)
73 << "Error " << toString(halVersion.error) << " getting HAL version";
Janis Danisevskise8ba1802017-01-30 10:49:51 +000074
Shawn Willden0329a822017-12-04 13:55:14 -070075 // If the hardware is keymaster 2.0 or higher we will not allow the fallback device for import
76 // or generation of keys. The fallback device is only used for legacy keys present on the
77 // device.
78 bool allowNewFallbackDevice = halVersion.majorVersion >= 2 && halVersion.isSecure;
Janis Danisevskise8ba1802017-01-30 10:49:51 +000079
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070080 keystore::KeyStore keyStore(&entropy, dev, fallback, allowNewFallbackDevice);
Shawn Willden6507c272016-01-05 22:51:48 -070081 keyStore.initialize();
82 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010083 android::sp<keystore::KeyStoreService> service = new keystore::KeyStoreService(&keyStore);
Shawn Willden6507c272016-01-05 22:51:48 -070084 android::status_t ret = sm->addService(android::String16("android.security.keystore"), service);
Shawn Willden0329a822017-12-04 13:55:14 -070085 CHECK(ret == android::OK) << "Couldn't register binder service!";
Shawn Willden6507c272016-01-05 22:51:48 -070086
Roshan Piuse653c932017-03-29 10:08:47 -070087 /**
88 * Register the wifi keystore HAL service to run in passthrough mode.
89 * This will spawn off a new thread which will service the HIDL
90 * transactions.
91 */
92 configureRpcThreadpool(1, false /* callerWillJoin */);
93 android::sp<IKeystore> wifiKeystoreHalService = new Keystore();
94 android::status_t err = wifiKeystoreHalService->registerAsService();
Shawn Willden0329a822017-12-04 13:55:14 -070095 CHECK(ret == android::OK) << "Cannot register wifi keystore HAL service: " << err;
Roshan Piuse653c932017-03-29 10:08:47 -070096
Shawn Willden6507c272016-01-05 22:51:48 -070097 /*
Roshan Piuse653c932017-03-29 10:08:47 -070098 * This thread is just going to process Binder transactions.
Shawn Willden6507c272016-01-05 22:51:48 -070099 */
100 android::IPCThreadState::self()->joinThreadPool();
Shawn Willden6507c272016-01-05 22:51:48 -0700101 return 1;
102}