1 /*
2 **
3 ** Copyright 2018, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #include <android-base/logging.h>
19 #include <android/hardware/keymaster/4.1/IKeymasterDevice.h>
20 #include <cutils/properties.h>
21 #include <gflags/gflags.h>
22 #include <hidl/HidlTransportSupport.h>
23 
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/security/keymaster_channel.h"
26 #include <guest/hals/keymaster/remote/remote_keymaster.h>
27 #include <guest/hals/keymaster/remote/remote_keymaster4_device.h>
28 
29 DEFINE_uint32(
30     port,
31     static_cast<uint32_t>(property_get_int64("ro.boot.vsock_keymaster_port", 0)),
32     "virtio socket port to send keymaster commands to");
33 
main(int argc,char ** argv)34 int main(int argc, char** argv) {
35     ::android::base::InitLogging(argv);
36     gflags::ParseCommandLineFlags(&argc, &argv, true);
37     ::android::hardware::configureRpcThreadpool(1, true);
38 
39     auto vsockFd = cuttlefish::SharedFD::VsockClient(2, FLAGS_port, SOCK_STREAM);
40     if (!vsockFd->IsOpen()) {
41         LOG(FATAL) << "Could not connect to keymaster server: "
42                    << vsockFd->StrError();
43     }
44     cuttlefish::KeymasterChannel keymasterChannel(vsockFd);
45     auto remoteKeymaster = new keymaster::RemoteKeymaster(&keymasterChannel);
46 
47     if (!remoteKeymaster->Initialize()) {
48       LOG(FATAL) << "Could not initialize keymaster";
49     }
50 
51     auto keymaster = new ::keymaster::V4_1::RemoteKeymaster4Device(remoteKeymaster);
52 
53     auto status = keymaster->registerAsService();
54     if (status != android::OK) {
55         LOG(FATAL) << "Could not register service for Keymaster 4.1 (" << status << ")";
56         return -1;
57     }
58 
59     android::hardware::joinRpcThreadpool();
60     return -1;  // Should never get here.
61 }
62