1 /*
2  * Copyright (C) 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 #include "VehicleBindingUtil.h"
18 
19 #include <android-base/logging.h>
20 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
21 #include <binder/IServiceManager.h>
22 #include <binder/Status.h>
23 
24 #include <iostream>
25 #include <map>
26 #include <string>
27 
28 namespace {
29 
30 using android::defaultServiceManager;
31 using android::automotive::security::BindingStatus;
32 using android::automotive::security::DefaultCsrng;
33 using android::automotive::security::DefaultExecutor;
34 using android::hardware::automotive::vehicle::V2_0::IVehicle;
35 
36 static int printHelp(int argc, char* argv[]);
37 static int setBinding(int /*argc*/, char*[] /*argv*/);
38 
39 // Avoid calling complex destructor on cleanup.
40 const auto& subcommandTable = *new std::map<std::string, std::function<int(int, char*[])>>{
41         {"help", printHelp},
42         {"set_binding", setBinding},
43 };
44 
setBinding(int,char * [])45 static int setBinding(int /*argc*/, char*[] /*argv*/) {
46     auto status = setVehicleBindingSeed(IVehicle::getService(), DefaultExecutor{}, DefaultCsrng{});
47     if (status != BindingStatus::OK) {
48         LOG(ERROR) << "Unable to set the binding seed. Encryption keys are not "
49                    << "bound to the platform.";
50         return static_cast<int>(status);
51     }
52 
53     return 0;
54 }
55 
printHelp(int,char * argv[])56 static int printHelp(int /*argc*/, char* argv[]) {
57     std::cout << "Usage: " << argv[0] << " <subcommand> [args]" << std::endl
58               << "Valid subcommands: " << std::endl;
59     for (const auto& i : subcommandTable) {
60         std::cout << "    " << i.first << std::endl;
61     }
62     return 0;
63 }
64 
65 }  // namespace
66 
main(int argc,char * argv[])67 int main(int argc, char* argv[]) {
68     setenv("ANDROID_LOG_TAGS", "*:v", 1);
69     android::base::InitLogging(argv,
70                                (getppid() == 1) ? &android::base::KernelLogger
71                                                 : &android::base::StderrLogger);
72     if (argc < 2) {
73         LOG(ERROR) << "Please specify a subcommand.";
74         printHelp(argc, argv);
75         return -1;
76     }
77 
78     auto subcommand = subcommandTable.find(argv[1]);
79     if (subcommand == subcommandTable.end()) {
80         LOG(ERROR) << "Invalid subcommand: " << argv[1];
81         printHelp(argc, argv);
82         return -1;
83     }
84 
85     return subcommand->second(argc, argv);
86 }
87