blob: 599b52a4b9322657f4f4185829c88e5341aa744c [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>
Sean Thomas61c3ed52024-10-16 11:25:42 +000029#include <unordered_set>
Seth Moore708da932022-08-18 14:38:05 -070030#include <vector>
31
Robert Shihd3c1f7c2023-07-10 13:07:35 -070032#include "DrmRkpAdapter.h"
Seth Moore708da932022-08-18 14:38:05 -070033#include "rkp_factory_extraction_lib.h"
34
Robert Shihd3c1f7c2023-07-10 13:07:35 -070035using aidl::android::hardware::drm::IDrmFactory;
Max Biresf60987e2021-04-16 13:35:20 -070036using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
Sean Thomas61c3ed52024-10-16 11:25:42 +000037using aidl::android::hardware::security::keymint::RpcHardwareInfo;
Seth Mooree44aad22021-06-25 17:38:24 -070038using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Sean Thomas61c3ed52024-10-16 11:25:42 +000039using aidl::android::hardware::security::keymint::remote_prov::RKPVM_INSTANCE_NAME;
Max Biresf60987e2021-04-16 13:35:20 -070040
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'.");
Sean Thomas61c3ed52024-10-16 11:25:42 +000050DEFINE_string(require_uds_certs, "",
51 "The comma-delimited names of remotely provisioned "
52 "components whose UDS certificate chains are required to be present in the CSR. "
53 "Example: avf,default,strongbox");
Seth Mooree44aad22021-06-25 17:38:24 -070054
Max Biresf60987e2021-04-16 13:35:20 -070055namespace {
56
Seth Mooree44aad22021-06-25 17:38:24 -070057// Various supported --output_format values.
58constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
59constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
60 // fingerprint plus CSR.
61
Robert Shihd3c1f7c2023-07-10 13:07:35 -070062std::string getFullServiceName(const char* descriptor, const char* name) {
63 return std::string(descriptor) + "/" + name;
64}
65
Sean Thomas61c3ed52024-10-16 11:25:42 +000066void writeOutput(const std::string instance_name, const cppbor::Array& csr) {
Seth Mooree44aad22021-06-25 17:38:24 -070067 if (FLAGS_output_format == kBinaryCsrOutput) {
68 auto bytes = csr.encode();
69 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
70 } else if (FLAGS_output_format == kBuildPlusCsr) {
chuanchuan.gao8ef6d1a2023-12-07 16:47:51 +080071 auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr, FLAGS_serialno_prop);
Seth Mooree44aad22021-06-25 17:38:24 -070072 if (!error.empty()) {
Sean Thomas61c3ed52024-10-16 11:25:42 +000073 std::cerr << "Error JSON encoding the output: " << error << std::endl;
74 exit(-1);
Seth Mooree44aad22021-06-25 17:38:24 -070075 }
76 std::cout << json << std::endl;
77 } else {
78 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
79 std::cerr << "Valid formats:" << std::endl;
80 std::cerr << " " << kBinaryCsrOutput << std::endl;
81 std::cerr << " " << kBuildPlusCsr << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +000082 exit(-1);
Seth Mooree44aad22021-06-25 17:38:24 -070083 }
84}
85
Sean Thomas61c3ed52024-10-16 11:25:42 +000086void getCsrForIRpc(const char* descriptor, const char* name, IRemotelyProvisionedComponent* irpc,
87 bool requireUdsCerts) {
Sean Thomas734e42a2024-11-07 23:19:43 +000088 auto fullName = getFullServiceName(descriptor, name);
Alice Wang16e34422024-06-07 12:41:22 +000089 // AVF RKP HAL is not always supported, so we need to check if it is supported before
90 // generating the CSR.
Sean Thomas734e42a2024-11-07 23:19:43 +000091 if (fullName == RKPVM_INSTANCE_NAME) {
Sean Thomas61c3ed52024-10-16 11:25:42 +000092 RpcHardwareInfo hwInfo;
93 auto status = irpc->getHardwareInfo(&hwInfo);
94 if (!status.isOk()) {
95 return;
96 }
Alice Wang16e34422024-06-07 12:41:22 +000097 }
Sean Thomas61c3ed52024-10-16 11:25:42 +000098
99 auto [request, errMsg] =
100 getCsr(name, irpc, FLAGS_self_test, FLAGS_allow_degenerate, requireUdsCerts);
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700101 if (!request) {
Sean Thomas61c3ed52024-10-16 11:25:42 +0000102 std::cerr << "Unable to build CSR for '" << fullName << "': " << errMsg << ", exiting."
103 << std::endl;
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700104 exit(-1);
105 }
106
107 writeOutput(std::string(name), *request);
108}
109
Seth Moore59146252021-07-02 08:59:23 -0700110// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
111// for every IRemotelyProvisionedComponent.
Sean Thomas61c3ed52024-10-16 11:25:42 +0000112void getCsrForInstance(const char* name, void* context) {
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700113 auto fullName = getFullServiceName(IRemotelyProvisionedComponent::descriptor, name);
Sean Thomas61c3ed52024-10-16 11:25:42 +0000114 std::future<AIBinder*> waitForServiceFunc =
Alice Wangc1b568a2024-05-13 09:15:20 +0000115 std::async(std::launch::async, AServiceManager_waitForService, fullName.c_str());
Sean Thomas61c3ed52024-10-16 11:25:42 +0000116 if (waitForServiceFunc.wait_for(std::chrono::seconds(10)) == std::future_status::timeout) {
117 std::cerr << "Wait for service timed out after 10 seconds: '" << fullName << "', exiting."
118 << std::endl;
Alice Wangc1b568a2024-05-13 09:15:20 +0000119 exit(-1);
120 }
Sean Thomas61c3ed52024-10-16 11:25:42 +0000121 AIBinder* rkpAiBinder = waitForServiceFunc.get();
Seth Moore59146252021-07-02 08:59:23 -0700122 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
Sean Thomas61c3ed52024-10-16 11:25:42 +0000123 auto rkpService = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
124 if (!rkpService) {
125 std::cerr << "Unable to get binder object for '" << fullName << "', exiting." << std::endl;
Keith Mokb9462c12021-11-11 19:34:26 +0000126 exit(-1);
Seth Moore59146252021-07-02 08:59:23 -0700127 }
128
Sean Thomas61c3ed52024-10-16 11:25:42 +0000129 if (context == nullptr) {
130 std::cerr << "Unable to get context for '" << fullName << "', exiting." << std::endl;
131 exit(-1);
132 }
133
134 auto requireUdsCertsRpcNames = static_cast<std::unordered_set<std::string>*>(context);
135 auto requireUdsCerts = requireUdsCertsRpcNames->count(name) != 0;
136 requireUdsCertsRpcNames->erase(name);
137 getCsrForIRpc(IRemotelyProvisionedComponent::descriptor, name, rkpService.get(),
138 requireUdsCerts);
Seth Moore59146252021-07-02 08:59:23 -0700139}
140
Max Biresf60987e2021-04-16 13:35:20 -0700141} // namespace
142
Seth Moore01688562021-06-22 12:59:32 -0700143int main(int argc, char** argv) {
144 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
145
Sean Thomas61c3ed52024-10-16 11:25:42 +0000146 auto requireUdsCertsRpcNames = parseCommaDelimited(FLAGS_require_uds_certs);
147
Seth Moore59146252021-07-02 08:59:23 -0700148 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
Sean Thomas61c3ed52024-10-16 11:25:42 +0000149 &requireUdsCertsRpcNames, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700150
Sean Thomas734e42a2024-11-07 23:19:43 +0000151 // Append drm CSRs
Sean Thomas61c3ed52024-10-16 11:25:42 +0000152 for (auto const& [name, irpc] : android::mediadrm::getDrmRemotelyProvisionedComponents()) {
153 auto requireUdsCerts = requireUdsCertsRpcNames.count(name) != 0;
154 requireUdsCertsRpcNames.erase(name);
155 getCsrForIRpc(IDrmFactory::descriptor, name.c_str(), irpc.get(), requireUdsCerts);
156 }
157
158 for (auto const& rpcName : requireUdsCertsRpcNames) {
159 std::cerr << "WARNING: You requested to enforce the presence of UDS Certs for '" << rpcName
160 << "', but no Remotely Provisioned Component had that name." << std::endl;
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700161 }
162
Seth Moore59146252021-07-02 08:59:23 -0700163 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700164}