blob: 2aeabe0a4ba04c671fcc50fb0469eac24587a7c7 [file] [log] [blame]
Max Biresf60987e2021-04-16 13:35:20 -07001/*
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
Max Biresf60987e2021-04-16 13:35:20 -070017#include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h>
18#include <android/binder_manager.h>
19#include <cppbor.h>
Seth Moore01688562021-06-22 12:59:32 -070020#include <gflags/gflags.h>
Max Biresf60987e2021-04-16 13:35:20 -070021#include <keymaster/cppcose/cppcose.h>
Seth Moore9a4bc972021-07-22 16:46:07 -070022#include <openssl/base64.h>
Seth Moore6dfb02a2021-06-18 15:43:09 -070023#include <remote_prov/remote_prov_utils.h>
Seth Moore5a40fa72021-06-22 13:48:59 -070024#include <sys/random.h>
Max Biresf60987e2021-04-16 13:35:20 -070025
Seth Moore708da932022-08-18 14:38:05 -070026#include <string>
27#include <vector>
28
29#include "rkp_factory_extraction_lib.h"
30
Max Biresf60987e2021-04-16 13:35:20 -070031using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
Seth Mooree44aad22021-06-25 17:38:24 -070032using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Max Biresf60987e2021-04-16 13:35:20 -070033
Max Biresf60987e2021-04-16 13:35:20 -070034using namespace cppbor;
35using namespace cppcose;
36
Seth Moore04756782022-09-13 16:09:15 -070037DEFINE_string(output_format, "build+csr", "How to format the output. Defaults to 'build+csr'.");
38DEFINE_bool(self_test, false,
39 "If true, the tool does not output CSR data, but instead performs a self-test, "
40 "validating a test payload for correctness. This may be used to verify a device on the "
41 "factory line before attempting to upload the output to the device info service.");
Seth Mooree44aad22021-06-25 17:38:24 -070042
Max Biresf60987e2021-04-16 13:35:20 -070043namespace {
44
Seth Mooree44aad22021-06-25 17:38:24 -070045// Various supported --output_format values.
46constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
47constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
48 // fingerprint plus CSR.
49
Tommy Chiuce82be82022-03-09 04:23:19 +000050void writeOutput(const std::string instance_name, const Array& csr) {
Seth Mooree44aad22021-06-25 17:38:24 -070051 if (FLAGS_output_format == kBinaryCsrOutput) {
52 auto bytes = csr.encode();
53 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
54 } else if (FLAGS_output_format == kBuildPlusCsr) {
Tommy Chiuce82be82022-03-09 04:23:19 +000055 auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr);
Seth Mooree44aad22021-06-25 17:38:24 -070056 if (!error.empty()) {
57 std::cerr << "Error JSON encoding the output: " << error;
58 exit(1);
59 }
60 std::cout << json << std::endl;
61 } else {
62 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
63 std::cerr << "Valid formats:" << std::endl;
64 std::cerr << " " << kBinaryCsrOutput << std::endl;
65 std::cerr << " " << kBuildPlusCsr << std::endl;
66 exit(1);
67 }
68}
69
Seth Moore59146252021-07-02 08:59:23 -070070// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
71// for every IRemotelyProvisionedComponent.
72void getCsrForInstance(const char* name, void* /*context*/) {
73 const std::vector<uint8_t> challenge = generateChallenge();
74
75 auto fullName = std::string(IRemotelyProvisionedComponent::descriptor) + "/" + name;
76 AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str());
77 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
78 auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
79 if (!rkp_service) {
80 std::cerr << "Unable to get binder object for '" << fullName << "', skipping.";
Keith Mokb9462c12021-11-11 19:34:26 +000081 exit(-1);
Seth Moore59146252021-07-02 08:59:23 -070082 }
83
Seth Moore04756782022-09-13 16:09:15 -070084 if (FLAGS_self_test) {
85 selfTestGetCsr(name, rkp_service.get());
86 } else {
87 auto [request, errMsg] = getCsr(name, rkp_service.get());
88 if (!request) {
89 std::cerr << "Unable to build CSR for '" << fullName << ": " << errMsg << std::endl;
90 exit(-1);
91 }
Seth Moore708da932022-08-18 14:38:05 -070092
Seth Moore04756782022-09-13 16:09:15 -070093 writeOutput(std::string(name), *request);
94 }
Seth Moore59146252021-07-02 08:59:23 -070095}
96
Max Biresf60987e2021-04-16 13:35:20 -070097} // namespace
98
Seth Moore01688562021-06-22 12:59:32 -070099int main(int argc, char** argv) {
100 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
101
Seth Moore59146252021-07-02 08:59:23 -0700102 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
103 /*context=*/nullptr, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700104
Seth Moore59146252021-07-02 08:59:23 -0700105 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700106}