Andrew Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 1 | /* |
| 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 Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 21 | #include <android-base/properties.h> |
| 22 | #include <android-base/result.h> |
Andrew Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 23 | #include <android/binder_manager.h> |
| 24 | #include <android/binder_process.h> |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 25 | #include <keymaster/android_keymaster_utils.h> |
| 26 | #include <keymaster/mem.h> |
Andrew Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 27 | #include <keymaster/soft_keymaster_logger.h> |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 28 | #include <openssl/digest.h> |
| 29 | #include <openssl/hkdf.h> |
| 30 | #include <openssl/is_boringssl.h> |
| 31 | #include <openssl/sha.h> |
Andrew Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 32 | |
Andrew Scull | dd07787 | 2021-06-01 10:22:07 +0000 | [diff] [blame] | 33 | #include "MicrodroidKeyMintDevice.h" |
| 34 | |
| 35 | using aidl::android::hardware::security::keymint::MicrodroidKeyMintDevice; |
Andrew Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 36 | using aidl::android::hardware::security::keymint::SecurityLevel; |
| 37 | |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 38 | using android::base::Error; |
| 39 | using android::base::GetProperty; |
| 40 | using android::base::Result; |
| 41 | |
| 42 | using keymaster::KeymasterBlob; |
| 43 | using keymaster::KeymasterKeyBlob; |
| 44 | using keymaster::memset_s; |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | template <typename T, class... Args> |
| 49 | std::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 | |
| 59 | Result<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 Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 93 | int main() { |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 94 | auto rootKey = getRootKey(); |
| 95 | if (!rootKey.ok()) { |
| 96 | LOG(FATAL) << "Failed to get root key: " << rootKey.error(); |
| 97 | } |
| 98 | |
Andrew Scull | 70bbb1f | 2021-07-06 11:00:38 +0000 | [diff] [blame] | 99 | // Zero threads seems like a useless pool, but below we'll join this thread |
| 100 | // to it, increasing the pool size to 1. |
Andrew Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 101 | ABinderProcess_setThreadPoolMaxThreadCount(0); |
Andrew Scull | 70bbb1f | 2021-07-06 11:00:38 +0000 | [diff] [blame] | 102 | |
Andrew Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 103 | // Add Keymint Service |
Andrew Scull | dd07787 | 2021-06-01 10:22:07 +0000 | [diff] [blame] | 104 | std::shared_ptr<MicrodroidKeyMintDevice> keyMint = |
Andrew Scull | 9fadf34 | 2021-06-03 08:38:47 +0000 | [diff] [blame] | 105 | ndk::SharedRefBase::make<MicrodroidKeyMintDevice>(*rootKey); |
Andrew Scull | 70bbb1f | 2021-07-06 11:00:38 +0000 | [diff] [blame] | 106 | 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 Scull | 9ba2657 | 2021-05-27 19:20:46 +0000 | [diff] [blame] | 111 | |
| 112 | ABinderProcess_joinThreadPool(); |
| 113 | return EXIT_FAILURE; // should not reach |
| 114 | } |