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.trusty" 18 #include <android-base/logging.h> 19 #include <android/binder_manager.h> 20 #include <android/binder_process.h> 21 22 #include <trusty_keymaster/TrustyKeyMintDevice.h> 23 #include <trusty_keymaster/TrustyRemotelyProvisionedComponentDevice.h> 24 #include <trusty_keymaster/TrustySecureClock.h> 25 #include <trusty_keymaster/TrustySharedSecret.h> 26 27 using aidl::android::hardware::security::keymint::trusty::TrustyKeyMintDevice; 28 using aidl::android::hardware::security::keymint::trusty::TrustyRemotelyProvisionedComponentDevice; 29 using aidl::android::hardware::security::secureclock::trusty::TrustySecureClock; 30 using aidl::android::hardware::security::sharedsecret::trusty::TrustySharedSecret; 31 32 template <typename T, class... Args> addService(Args &&...args)33std::shared_ptr<T> addService(Args&&... args) { 34 std::shared_ptr<T> service = ndk::SharedRefBase::make<T>(std::forward<Args>(args)...); 35 auto instanceName = std::string(T::descriptor) + "/default"; 36 LOG(ERROR) << "Adding service instance: " << instanceName; 37 auto status = AServiceManager_addService(service->asBinder().get(), instanceName.c_str()); 38 CHECK(status == STATUS_OK) << "Failed to add service " << instanceName; 39 return service; 40 } 41 main()42int main() { 43 auto trustyKeymaster = std::make_shared<keymaster::TrustyKeymaster>(); 44 int err = trustyKeymaster->Initialize(keymaster::KmVersion::KEYMINT_3); 45 if (err != 0) { 46 LOG(FATAL) << "Could not initialize TrustyKeymaster for KeyMint (" << err << ")"; 47 return -1; 48 } 49 50 // Zero threads seems like a useless pool but below we'll join this thread to it, increasing 51 // the pool size to 1. 52 ABinderProcess_setThreadPoolMaxThreadCount(0); 53 54 auto keyMint = addService<TrustyKeyMintDevice>(trustyKeymaster); 55 auto secureClock = addService<TrustySecureClock>(trustyKeymaster); 56 auto sharedSecret = addService<TrustySharedSecret>(trustyKeymaster); 57 auto remotelyProvisionedComponent = 58 addService<TrustyRemotelyProvisionedComponentDevice>(trustyKeymaster); 59 ABinderProcess_joinThreadPool(); 60 return EXIT_FAILURE; // should not reach 61 } 62