Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020, 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 Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 17 | #define LOG_TAG "android.hardware.security.keymint-service" |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android/binder_manager.h> |
| 21 | #include <android/binder_process.h> |
| 22 | |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 23 | #include <AndroidKeyMintDevice.h> |
Chirag Pathak | 8960aae | 2021-01-25 21:37:06 +0000 | [diff] [blame] | 24 | #include <AndroidSecureClock.h> |
| 25 | #include <AndroidSharedSecret.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 26 | #include <keymaster/soft_keymaster_logger.h> |
| 27 | |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 28 | using aidl::android::hardware::security::keymint::AndroidKeyMintDevice; |
| 29 | using aidl::android::hardware::security::keymint::SecurityLevel; |
Chirag Pathak | 8960aae | 2021-01-25 21:37:06 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::security::secureclock::AndroidSecureClock; |
| 31 | using aidl::android::hardware::security::sharedsecret::AndroidSharedSecret; |
| 32 | |
| 33 | template <typename T, class... Args> |
| 34 | std::shared_ptr<T> addService(Args&&... args) { |
| 35 | std::shared_ptr<T> ser = ndk::SharedRefBase::make<T>(std::forward<Args>(args)...); |
| 36 | auto instanceName = std::string(T::descriptor) + "/default"; |
| 37 | LOG(INFO) << "adding keymint service instance: " << instanceName; |
| 38 | binder_status_t status = |
| 39 | AServiceManager_addService(ser->asBinder().get(), instanceName.c_str()); |
| 40 | CHECK(status == STATUS_OK); |
| 41 | return ser; |
| 42 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 43 | |
| 44 | int main() { |
| 45 | // Zero threads seems like a useless pool, but below we'll join this thread to it, increasing |
| 46 | // the pool size to 1. |
| 47 | ABinderProcess_setThreadPoolMaxThreadCount(0); |
Chirag Pathak | 8960aae | 2021-01-25 21:37:06 +0000 | [diff] [blame] | 48 | |
| 49 | // Add Keymint Service |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 50 | std::shared_ptr<AndroidKeyMintDevice> keyMint = |
Chirag Pathak | 8960aae | 2021-01-25 21:37:06 +0000 | [diff] [blame] | 51 | addService<AndroidKeyMintDevice>(SecurityLevel::SOFTWARE); |
| 52 | // Add Secure Clock Service |
| 53 | addService<AndroidSecureClock>(keyMint); |
| 54 | // Add Shared Secret Service |
| 55 | addService<AndroidSharedSecret>(keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 56 | ABinderProcess_joinThreadPool(); |
| 57 | return EXIT_FAILURE; // should not reach |
| 58 | } |