blob: 0fe7d74901ff49ae0d6631014a456fa823a3c287 [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
Seth Moore5a40fa72021-06-22 13:48:59 -070050constexpr size_t kChallengeSize = 16;
51
Tommy Chiuce82be82022-03-09 04:23:19 +000052void writeOutput(const std::string instance_name, const Array& csr) {
Seth Mooree44aad22021-06-25 17:38:24 -070053 if (FLAGS_output_format == kBinaryCsrOutput) {
54 auto bytes = csr.encode();
55 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
56 } else if (FLAGS_output_format == kBuildPlusCsr) {
Tommy Chiuce82be82022-03-09 04:23:19 +000057 auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr);
Seth Mooree44aad22021-06-25 17:38:24 -070058 if (!error.empty()) {
59 std::cerr << "Error JSON encoding the output: " << error;
60 exit(1);
61 }
62 std::cout << json << std::endl;
63 } else {
64 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
65 std::cerr << "Valid formats:" << std::endl;
66 std::cerr << " " << kBinaryCsrOutput << std::endl;
67 std::cerr << " " << kBuildPlusCsr << std::endl;
68 exit(1);
69 }
70}
71
Seth Moore59146252021-07-02 08:59:23 -070072// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
73// for every IRemotelyProvisionedComponent.
74void getCsrForInstance(const char* name, void* /*context*/) {
75 const std::vector<uint8_t> challenge = generateChallenge();
76
77 auto fullName = std::string(IRemotelyProvisionedComponent::descriptor) + "/" + name;
78 AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str());
79 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
80 auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
81 if (!rkp_service) {
82 std::cerr << "Unable to get binder object for '" << fullName << "', skipping.";
Keith Mokb9462c12021-11-11 19:34:26 +000083 exit(-1);
Seth Moore59146252021-07-02 08:59:23 -070084 }
85
Seth Moore04756782022-09-13 16:09:15 -070086 if (FLAGS_self_test) {
87 selfTestGetCsr(name, rkp_service.get());
88 } else {
89 auto [request, errMsg] = getCsr(name, rkp_service.get());
90 if (!request) {
91 std::cerr << "Unable to build CSR for '" << fullName << ": " << errMsg << std::endl;
92 exit(-1);
93 }
Seth Moore708da932022-08-18 14:38:05 -070094
Seth Moore04756782022-09-13 16:09:15 -070095 writeOutput(std::string(name), *request);
96 }
Seth Moore59146252021-07-02 08:59:23 -070097}
98
Max Biresf60987e2021-04-16 13:35:20 -070099} // namespace
100
Seth Moore01688562021-06-22 12:59:32 -0700101int main(int argc, char** argv) {
102 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
103
Seth Moore59146252021-07-02 08:59:23 -0700104 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
105 /*context=*/nullptr, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700106
Seth Moore59146252021-07-02 08:59:23 -0700107 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700108}