blob: c0f6beb1ff83b69dc03869cca9f246f964b26aaf [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
Robert Shihd3c1f7c2023-07-10 13:07:35 -070017#include <aidl/android/hardware/drm/IDrmFactory.h>
Max Biresf60987e2021-04-16 13:35:20 -070018#include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h>
19#include <android/binder_manager.h>
20#include <cppbor.h>
Seth Moore01688562021-06-22 12:59:32 -070021#include <gflags/gflags.h>
Max Biresf60987e2021-04-16 13:35:20 -070022#include <keymaster/cppcose/cppcose.h>
Seth Moore9a4bc972021-07-22 16:46:07 -070023#include <openssl/base64.h>
Seth Moore6dfb02a2021-06-18 15:43:09 -070024#include <remote_prov/remote_prov_utils.h>
Seth Moore5a40fa72021-06-22 13:48:59 -070025#include <sys/random.h>
Max Biresf60987e2021-04-16 13:35:20 -070026
Alice Wangc1b568a2024-05-13 09:15:20 +000027#include <future>
Seth Moore708da932022-08-18 14:38:05 -070028#include <string>
29#include <vector>
30
Robert Shihd3c1f7c2023-07-10 13:07:35 -070031#include "DrmRkpAdapter.h"
Seth Moore708da932022-08-18 14:38:05 -070032#include "rkp_factory_extraction_lib.h"
33
Robert Shihd3c1f7c2023-07-10 13:07:35 -070034using aidl::android::hardware::drm::IDrmFactory;
Max Biresf60987e2021-04-16 13:35:20 -070035using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
Seth Mooree44aad22021-06-25 17:38:24 -070036using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Max Biresf60987e2021-04-16 13:35:20 -070037
Max Biresf60987e2021-04-16 13:35:20 -070038using namespace cppbor;
39using namespace cppcose;
40
Seth Moore04756782022-09-13 16:09:15 -070041DEFINE_string(output_format, "build+csr", "How to format the output. Defaults to 'build+csr'.");
Seth Mooredff09d02023-05-31 09:38:47 -070042DEFINE_bool(self_test, true,
43 "If true, this tool performs a self-test, validating the payload for correctness. "
44 "This checks that the device on the factory line is producing valid output "
45 "before attempting to upload the output to the device info service.");
Karuna Wadherac37901f2024-09-10 22:41:49 +000046DEFINE_bool(allow_degenerate, true,
47 "If true, self_test validation will allow degenerate DICE chains in the CSR.");
chuanchuan.gao8ef6d1a2023-12-07 16:47:51 +080048DEFINE_string(serialno_prop, "ro.serialno",
49 "The property of getting serial number. Defaults to 'ro.serialno'.");
Seth Mooree44aad22021-06-25 17:38:24 -070050
Max Biresf60987e2021-04-16 13:35:20 -070051namespace {
52
Seth Mooree44aad22021-06-25 17:38:24 -070053// Various supported --output_format values.
54constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
55constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
56 // fingerprint plus CSR.
57
Robert Shihd3c1f7c2023-07-10 13:07:35 -070058std::string getFullServiceName(const char* descriptor, const char* name) {
59 return std::string(descriptor) + "/" + name;
60}
61
Tommy Chiuce82be82022-03-09 04:23:19 +000062void writeOutput(const std::string instance_name, const Array& csr) {
Seth Mooree44aad22021-06-25 17:38:24 -070063 if (FLAGS_output_format == kBinaryCsrOutput) {
64 auto bytes = csr.encode();
65 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
66 } else if (FLAGS_output_format == kBuildPlusCsr) {
chuanchuan.gao8ef6d1a2023-12-07 16:47:51 +080067 auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr, FLAGS_serialno_prop);
Seth Mooree44aad22021-06-25 17:38:24 -070068 if (!error.empty()) {
69 std::cerr << "Error JSON encoding the output: " << error;
70 exit(1);
71 }
72 std::cout << json << std::endl;
73 } else {
74 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
75 std::cerr << "Valid formats:" << std::endl;
76 std::cerr << " " << kBinaryCsrOutput << std::endl;
77 std::cerr << " " << kBuildPlusCsr << std::endl;
78 exit(1);
79 }
80}
81
Robert Shihd3c1f7c2023-07-10 13:07:35 -070082void getCsrForIRpc(const char* descriptor, const char* name, IRemotelyProvisionedComponent* irpc) {
Alice Wang16e34422024-06-07 12:41:22 +000083 // AVF RKP HAL is not always supported, so we need to check if it is supported before
84 // generating the CSR.
85 if (std::string(name) == "avf" && !isRemoteProvisioningSupported(irpc)) {
86 return;
87 }
Karuna Wadherac37901f2024-09-10 22:41:49 +000088 auto [request, errMsg] = getCsr(name, irpc, FLAGS_self_test, FLAGS_allow_degenerate);
Robert Shihd3c1f7c2023-07-10 13:07:35 -070089 auto fullName = getFullServiceName(descriptor, name);
90 if (!request) {
91 std::cerr << "Unable to build CSR for '" << fullName << ": " << errMsg << std::endl;
92 exit(-1);
93 }
94
95 writeOutput(std::string(name), *request);
96}
97
Seth Moore59146252021-07-02 08:59:23 -070098// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
99// for every IRemotelyProvisionedComponent.
100void getCsrForInstance(const char* name, void* /*context*/) {
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700101 auto fullName = getFullServiceName(IRemotelyProvisionedComponent::descriptor, name);
Alice Wangc1b568a2024-05-13 09:15:20 +0000102 std::future<AIBinder*> wait_for_service_func =
103 std::async(std::launch::async, AServiceManager_waitForService, fullName.c_str());
104 if (wait_for_service_func.wait_for(std::chrono::seconds(10)) == std::future_status::timeout) {
105 std::cerr << "Wait for service timed out after 10 seconds: " << fullName;
106 exit(-1);
107 }
108 AIBinder* rkpAiBinder = wait_for_service_func.get();
Seth Moore59146252021-07-02 08:59:23 -0700109 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
110 auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
111 if (!rkp_service) {
112 std::cerr << "Unable to get binder object for '" << fullName << "', skipping.";
Keith Mokb9462c12021-11-11 19:34:26 +0000113 exit(-1);
Seth Moore59146252021-07-02 08:59:23 -0700114 }
115
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700116 getCsrForIRpc(IRemotelyProvisionedComponent::descriptor, name, rkp_service.get());
Seth Moore59146252021-07-02 08:59:23 -0700117}
118
Max Biresf60987e2021-04-16 13:35:20 -0700119} // namespace
120
Seth Moore01688562021-06-22 12:59:32 -0700121int main(int argc, char** argv) {
122 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
123
Seth Moore59146252021-07-02 08:59:23 -0700124 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
125 /*context=*/nullptr, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700126
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700127 // Append drm csr's
128 for (auto const& e : android::mediadrm::getDrmRemotelyProvisionedComponents()) {
129 getCsrForIRpc(IDrmFactory::descriptor, e.first.c_str(), e.second.get());
130 }
131
Seth Moore59146252021-07-02 08:59:23 -0700132 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700133}