blob: 8e20f44c3caf06839a9db3694f8d575750f76d7a [file] [log] [blame]
Andrew Scull9ba26572021-05-27 19:20:46 +00001/*
2 * Copyright 2021, 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_TAG "android.hardware.security.keymint-service"
18
19#include <AndroidKeyMintDevice.h>
20#include <android-base/logging.h>
Andrew Scull6f3e5fe2021-07-02 12:38:21 +000021#include <android-base/properties.h>
22#include <android-base/result.h>
Andrew Scull9ba26572021-05-27 19:20:46 +000023#include <android/binder_manager.h>
24#include <android/binder_process.h>
Andrew Scull6f3e5fe2021-07-02 12:38:21 +000025#include <keymaster/android_keymaster_utils.h>
26#include <keymaster/mem.h>
Andrew Scull9ba26572021-05-27 19:20:46 +000027#include <keymaster/soft_keymaster_logger.h>
Andrew Scull6f3e5fe2021-07-02 12:38:21 +000028#include <openssl/digest.h>
29#include <openssl/hkdf.h>
30#include <openssl/is_boringssl.h>
31#include <openssl/sha.h>
Andrew Scull9ba26572021-05-27 19:20:46 +000032
Andrew Sculldd077872021-06-01 10:22:07 +000033#include "MicrodroidKeyMintDevice.h"
34
35using aidl::android::hardware::security::keymint::MicrodroidKeyMintDevice;
Andrew Scull9ba26572021-05-27 19:20:46 +000036using aidl::android::hardware::security::keymint::SecurityLevel;
37
Andrew Scull6f3e5fe2021-07-02 12:38:21 +000038using android::base::Error;
39using android::base::GetProperty;
40using android::base::Result;
41
42using keymaster::KeymasterBlob;
43using keymaster::KeymasterKeyBlob;
44using keymaster::memset_s;
45
46namespace {
47
48template <typename T, class... Args>
49std::shared_ptr<T> addService(Args&&... args) {
50 std::shared_ptr<T> ser = ndk::SharedRefBase::make<T>(std::forward<Args>(args)...);
51 auto instanceName = std::string(T::descriptor) + "/default";
52 LOG(INFO) << "adding keymint service instance: " << instanceName;
53 binder_status_t status =
54 AServiceManager_addService(ser->asBinder().get(), instanceName.c_str());
55 CHECK(status == STATUS_OK);
56 return ser;
57}
58
59Result<KeymasterKeyBlob> getRootKey() {
60 const std::string prop = "ro.vmsecret.keymint";
61 const std::chrono::seconds timeout(15);
62 while (!android::base::WaitForPropertyCreation(prop, timeout)) {
63 LOG(WARNING) << "waited " << timeout.count() << "seconds for " << prop
64 << ", still waiting...";
65 }
66
67 // In a small effort to avoid spreading the secret around too widely in
68 // memory, move the secert into a buffer that will wipe itself and clear
69 // the original string.
70 std::string secretProp = GetProperty(prop, "");
71 KeymasterBlob secret(reinterpret_cast<const uint8_t*>(secretProp.data()), secretProp.size());
72 memset_s(secretProp.data(), 0, secretProp.size());
73 if (secret.size() < 64u) return Error() << "secret is too small";
74
75 // Derive the root key from the secret to avoid getting locked into using
76 // the secret directly.
77 KeymasterKeyBlob rootKey(SHA512_DIGEST_LENGTH);
78 const uint8_t kRootKeyIkm[] = "keymint_root_key";
79 const uint8_t* kNoSalt = nullptr;
80 const size_t kNoSaltLen = 0;
81 if (!HKDF(rootKey.writable_data(), rootKey.size(), EVP_sha512(), (uint8_t*)secret.begin(),
82 secret.size(), kNoSalt, kNoSaltLen, kRootKeyIkm, sizeof(kRootKeyIkm))) {
83 return Error() << "Failed to derive a key";
84 }
85 if (rootKey.size() < 64u) return Error() << "root key is too small";
86
87 LOG(INFO) << "root key obtained";
88 return rootKey;
89}
90
91} // namespace
92
Andrew Scull9ba26572021-05-27 19:20:46 +000093int main() {
Andrew Scull6f3e5fe2021-07-02 12:38:21 +000094 auto rootKey = getRootKey();
95 if (!rootKey.ok()) {
96 LOG(FATAL) << "Failed to get root key: " << rootKey.error();
97 }
98
Andrew Scull70bbb1f2021-07-06 11:00:38 +000099 // Zero threads seems like a useless pool, but below we'll join this thread
100 // to it, increasing the pool size to 1.
Andrew Scull9ba26572021-05-27 19:20:46 +0000101 ABinderProcess_setThreadPoolMaxThreadCount(0);
Andrew Scull70bbb1f2021-07-06 11:00:38 +0000102
Andrew Scull9ba26572021-05-27 19:20:46 +0000103 // Add Keymint Service
Andrew Sculldd077872021-06-01 10:22:07 +0000104 std::shared_ptr<MicrodroidKeyMintDevice> keyMint =
Andrew Sculla003f852021-07-06 16:09:07 +0000105 ndk::SharedRefBase::make<MicrodroidKeyMintDevice>();
Andrew Scull70bbb1f2021-07-06 11:00:38 +0000106 auto instanceName = std::string(MicrodroidKeyMintDevice::descriptor) + "/default";
107 LOG(INFO) << "adding keymint service instance: " << instanceName;
108 binder_status_t status =
109 AServiceManager_addService(keyMint->asBinder().get(), instanceName.c_str());
110 CHECK(status == STATUS_OK);
Andrew Scull9ba26572021-05-27 19:20:46 +0000111
112 ABinderProcess_joinThreadPool();
113 return EXIT_FAILURE; // should not reach
114}