blob: 502d931ae93d669c1ec641308312120746f5da2e [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 Mooree44aad22021-06-25 17:38:24 -070037DEFINE_string(output_format, "csr", "How to format the output. Defaults to 'csr'.");
38
Max Biresf60987e2021-04-16 13:35:20 -070039namespace {
40
Seth Mooree44aad22021-06-25 17:38:24 -070041// Various supported --output_format values.
42constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
43constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
44 // fingerprint plus CSR.
45
Seth Moore5a40fa72021-06-22 13:48:59 -070046constexpr size_t kChallengeSize = 16;
47
Tommy Chiuce82be82022-03-09 04:23:19 +000048void writeOutput(const std::string instance_name, const Array& csr) {
Seth Mooree44aad22021-06-25 17:38:24 -070049 if (FLAGS_output_format == kBinaryCsrOutput) {
50 auto bytes = csr.encode();
51 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
52 } else if (FLAGS_output_format == kBuildPlusCsr) {
Tommy Chiuce82be82022-03-09 04:23:19 +000053 auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr);
Seth Mooree44aad22021-06-25 17:38:24 -070054 if (!error.empty()) {
55 std::cerr << "Error JSON encoding the output: " << error;
56 exit(1);
57 }
58 std::cout << json << std::endl;
59 } else {
60 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
61 std::cerr << "Valid formats:" << std::endl;
62 std::cerr << " " << kBinaryCsrOutput << std::endl;
63 std::cerr << " " << kBuildPlusCsr << std::endl;
64 exit(1);
65 }
66}
67
Seth Moore59146252021-07-02 08:59:23 -070068// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
69// for every IRemotelyProvisionedComponent.
70void getCsrForInstance(const char* name, void* /*context*/) {
71 const std::vector<uint8_t> challenge = generateChallenge();
72
73 auto fullName = std::string(IRemotelyProvisionedComponent::descriptor) + "/" + name;
74 AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str());
75 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
76 auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
77 if (!rkp_service) {
78 std::cerr << "Unable to get binder object for '" << fullName << "', skipping.";
Keith Mokb9462c12021-11-11 19:34:26 +000079 exit(-1);
Seth Moore59146252021-07-02 08:59:23 -070080 }
81
Seth Moore708da932022-08-18 14:38:05 -070082 auto [request, errMsg] = getCsr(name, rkp_service.get());
83 if (!request) {
Seth Mooreb84a1fb2022-09-13 12:02:49 -070084 std::cerr << "Unable to build CSR for '" << fullName << ": " << errMsg << std::endl;
Max Biresd0f7b352022-01-27 18:30:46 -080085 exit(-1);
86 }
Seth Moore708da932022-08-18 14:38:05 -070087
88 writeOutput(std::string(name), *request);
Seth Moore59146252021-07-02 08:59:23 -070089}
90
Max Biresf60987e2021-04-16 13:35:20 -070091} // namespace
92
Seth Moore01688562021-06-22 12:59:32 -070093int main(int argc, char** argv) {
94 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
95
Seth Moore59146252021-07-02 08:59:23 -070096 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
97 /*context=*/nullptr, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -070098
Seth Moore59146252021-07-02 08:59:23 -070099 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700100}