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 <keymaster/android_keymaster_messages.h>
24 #include <keymaster/km_version.h>
25 #include <keymaster/soft_keymaster_logger.h>
26 #include "guest/hals/keymint/remote/remote_keymint_device.h"
27
28 #include <guest/hals/keymint/remote/remote_keymaster.h>
29 #include <guest/hals/keymint/remote/remote_keymint_device.h>
30 #include <guest/hals/keymint/remote/remote_secure_clock.h>
31 #include <guest/hals/keymint/remote/remote_shared_secret.h>
32 #include "common/libs/fs/shared_fd.h"
33 #include "common/libs/security/keymaster_channel.h"
34
35 static const char device[] = "/dev/hvc3";
36
37 using aidl::android::hardware::security::keymint::RemoteKeyMintDevice;
38 using aidl::android::hardware::security::keymint::SecurityLevel;
39 using aidl::android::hardware::security::secureclock::RemoteSecureClock;
40 using aidl::android::hardware::security::sharedsecret::RemoteSharedSecret;
41
42 template <typename T, class... Args>
addService(Args &&...args)43 static std::shared_ptr<T> addService(Args&&... args) {
44 std::shared_ptr<T> ser =
45 ndk::SharedRefBase::make<T>(std::forward<Args>(args)...);
46 auto instanceName = std::string(T::descriptor) + "/default";
47 LOG(INFO) << "adding keymint service instance: " << instanceName;
48 binder_status_t status =
49 AServiceManager_addService(ser->asBinder().get(), instanceName.c_str());
50 CHECK(status == STATUS_OK);
51 return ser;
52 }
53
main(int,char ** argv)54 int main(int, char** argv) {
55 android::base::InitLogging(argv, android::base::KernelLogger);
56 // Zero threads seems like a useless pool, but below we'll join this thread to
57 // it, increasing the pool size to 1.
58 ABinderProcess_setThreadPoolMaxThreadCount(0);
59 // Add Keymint Service
60 auto fd = cuttlefish::SharedFD::Open(device, O_RDWR);
61 if (!fd->IsOpen()) {
62 LOG(FATAL) << "Could not connect to keymaster: " << fd->StrError();
63 }
64
65 if (fd->SetTerminalRaw() < 0) {
66 LOG(FATAL) << "Could not make " << device
67 << " a raw terminal: " << fd->StrError();
68 }
69
70 cuttlefish::KeymasterChannel keymasterChannel(fd, fd);
71
72 keymaster::RemoteKeymaster remote_keymaster(
73 &keymasterChannel, keymaster::MessageVersion(
74 keymaster::KmVersion::KEYMINT_1, 0 /* km_date */));
75
76 addService<RemoteKeyMintDevice>(remote_keymaster,
77 SecurityLevel::TRUSTED_ENVIRONMENT);
78 addService<RemoteSecureClock>(remote_keymaster);
79 addService<RemoteSharedSecret>(remote_keymaster);
80
81 ABinderProcess_joinThreadPool();
82 return EXIT_FAILURE; // should not reach
83 }
84