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
17 #define LOG_TAG "android.hardware.security.keymint-service"
18
19 #include <android-base/logging.h>
20 #include <android/binder_manager.h>
21 #include <android/binder_process.h>
22
23 #include <AndroidKeyMintDevice.h>
24 #include <AndroidRemotelyProvisionedComponentDevice.h>
25 #include <AndroidSecureClock.h>
26 #include <AndroidSharedSecret.h>
27 #include <keymaster/soft_keymaster_logger.h>
28
29 using aidl::android::hardware::security::keymint::AndroidKeyMintDevice;
30 using aidl::android::hardware::security::keymint::AndroidRemotelyProvisionedComponentDevice;
31 using aidl::android::hardware::security::keymint::SecurityLevel;
32 using aidl::android::hardware::security::secureclock::AndroidSecureClock;
33 using aidl::android::hardware::security::sharedsecret::AndroidSharedSecret;
34
35 template <typename T, class... Args>
addService(Args &&...args)36 std::shared_ptr<T> addService(Args&&... args) {
37 std::shared_ptr<T> ser = ndk::SharedRefBase::make<T>(std::forward<Args>(args)...);
38 auto instanceName = std::string(T::descriptor) + "/default";
39 LOG(INFO) << "adding keymint service instance: " << instanceName;
40 binder_status_t status =
41 AServiceManager_addService(ser->asBinder().get(), instanceName.c_str());
42 CHECK_EQ(status, STATUS_OK);
43 return ser;
44 }
45
main()46 int main() {
47 // The global logger object required by keymaster's logging macros in keymaster/logger.h.
48 keymaster::SoftKeymasterLogger km_logger;
49 // Zero threads seems like a useless pool, but below we'll join this thread to it, increasing
50 // the pool size to 1.
51 ABinderProcess_setThreadPoolMaxThreadCount(0);
52 // Add Keymint Service
53 std::shared_ptr<AndroidKeyMintDevice> keyMint =
54 addService<AndroidKeyMintDevice>(SecurityLevel::SOFTWARE);
55 // Add Secure Clock Service
56 addService<AndroidSecureClock>(keyMint);
57 // Add Shared Secret Service
58 addService<AndroidSharedSecret>(keyMint);
59 // Add Remotely Provisioned Component Service
60 addService<AndroidRemotelyProvisionedComponentDevice>(keyMint);
61 ABinderProcess_joinThreadPool();
62 return EXIT_FAILURE; // should not reach
63 }
64