blob: eaa0acc91d7769fa11f968861aae5ffb18c1d940 [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;
38using aidl::android::hardware::security::keymint::remote_prov::deviceSuffix;
Seth Mooree44aad22021-06-25 17:38:24 -070039using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Sean Thomas61c3ed52024-10-16 11:25:42 +000040using aidl::android::hardware::security::keymint::remote_prov::RKPVM_INSTANCE_NAME;
Max Biresf60987e2021-04-16 13:35:20 -070041
Seth Moore04756782022-09-13 16:09:15 -070042DEFINE_string(output_format, "build+csr", "How to format the output. Defaults to 'build+csr'.");
Seth Mooredff09d02023-05-31 09:38:47 -070043DEFINE_bool(self_test, true,
44 "If true, this tool performs a self-test, validating the payload for correctness. "
45 "This checks that the device on the factory line is producing valid output "
46 "before attempting to upload the output to the device info service.");
Karuna Wadherac37901f2024-09-10 22:41:49 +000047DEFINE_bool(allow_degenerate, true,
48 "If true, self_test validation will allow degenerate DICE chains in the CSR.");
chuanchuan.gao8ef6d1a2023-12-07 16:47:51 +080049DEFINE_string(serialno_prop, "ro.serialno",
50 "The property of getting serial number. Defaults to 'ro.serialno'.");
Sean Thomas61c3ed52024-10-16 11:25:42 +000051DEFINE_string(require_uds_certs, "",
52 "The comma-delimited names of remotely provisioned "
53 "components whose UDS certificate chains are required to be present in the CSR. "
54 "Example: avf,default,strongbox");
Seth Mooree44aad22021-06-25 17:38:24 -070055
Max Biresf60987e2021-04-16 13:35:20 -070056namespace {
57
Seth Mooree44aad22021-06-25 17:38:24 -070058// Various supported --output_format values.
59constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
60constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
61 // fingerprint plus CSR.
62
Robert Shihd3c1f7c2023-07-10 13:07:35 -070063std::string getFullServiceName(const char* descriptor, const char* name) {
64 return std::string(descriptor) + "/" + name;
65}
66
Sean Thomas61c3ed52024-10-16 11:25:42 +000067void writeOutput(const std::string instance_name, const cppbor::Array& csr) {
Seth Mooree44aad22021-06-25 17:38:24 -070068 if (FLAGS_output_format == kBinaryCsrOutput) {
69 auto bytes = csr.encode();
70 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
71 } else if (FLAGS_output_format == kBuildPlusCsr) {
chuanchuan.gao8ef6d1a2023-12-07 16:47:51 +080072 auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr, FLAGS_serialno_prop);
Seth Mooree44aad22021-06-25 17:38:24 -070073 if (!error.empty()) {
Sean Thomas61c3ed52024-10-16 11:25:42 +000074 std::cerr << "Error JSON encoding the output: " << error << std::endl;
75 exit(-1);
Seth Mooree44aad22021-06-25 17:38:24 -070076 }
77 std::cout << json << std::endl;
78 } else {
79 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
80 std::cerr << "Valid formats:" << std::endl;
81 std::cerr << " " << kBinaryCsrOutput << std::endl;
82 std::cerr << " " << kBuildPlusCsr << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +000083 exit(-1);
Seth Mooree44aad22021-06-25 17:38:24 -070084 }
85}
86
Sean Thomas61c3ed52024-10-16 11:25:42 +000087void getCsrForIRpc(const char* descriptor, const char* name, IRemotelyProvisionedComponent* irpc,
88 bool requireUdsCerts) {
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 Thomas61c3ed52024-10-16 11:25:42 +000091 if (std::string(name) == deviceSuffix(RKPVM_INSTANCE_NAME)) {
92 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 auto fullName = getFullServiceName(descriptor, name);
103 std::cerr << "Unable to build CSR for '" << fullName << "': " << errMsg << ", exiting."
104 << std::endl;
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700105 exit(-1);
106 }
107
108 writeOutput(std::string(name), *request);
109}
110
Seth Moore59146252021-07-02 08:59:23 -0700111// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
112// for every IRemotelyProvisionedComponent.
Sean Thomas61c3ed52024-10-16 11:25:42 +0000113void getCsrForInstance(const char* name, void* context) {
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700114 auto fullName = getFullServiceName(IRemotelyProvisionedComponent::descriptor, name);
Sean Thomas61c3ed52024-10-16 11:25:42 +0000115 std::future<AIBinder*> waitForServiceFunc =
Alice Wangc1b568a2024-05-13 09:15:20 +0000116 std::async(std::launch::async, AServiceManager_waitForService, fullName.c_str());
Sean Thomas61c3ed52024-10-16 11:25:42 +0000117 if (waitForServiceFunc.wait_for(std::chrono::seconds(10)) == std::future_status::timeout) {
118 std::cerr << "Wait for service timed out after 10 seconds: '" << fullName << "', exiting."
119 << std::endl;
Alice Wangc1b568a2024-05-13 09:15:20 +0000120 exit(-1);
121 }
Sean Thomas61c3ed52024-10-16 11:25:42 +0000122 AIBinder* rkpAiBinder = waitForServiceFunc.get();
Seth Moore59146252021-07-02 08:59:23 -0700123 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
Sean Thomas61c3ed52024-10-16 11:25:42 +0000124 auto rkpService = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
125 if (!rkpService) {
126 std::cerr << "Unable to get binder object for '" << fullName << "', exiting." << std::endl;
Keith Mokb9462c12021-11-11 19:34:26 +0000127 exit(-1);
Seth Moore59146252021-07-02 08:59:23 -0700128 }
129
Sean Thomas61c3ed52024-10-16 11:25:42 +0000130 if (context == nullptr) {
131 std::cerr << "Unable to get context for '" << fullName << "', exiting." << std::endl;
132 exit(-1);
133 }
134
135 auto requireUdsCertsRpcNames = static_cast<std::unordered_set<std::string>*>(context);
136 auto requireUdsCerts = requireUdsCertsRpcNames->count(name) != 0;
137 requireUdsCertsRpcNames->erase(name);
138 getCsrForIRpc(IRemotelyProvisionedComponent::descriptor, name, rkpService.get(),
139 requireUdsCerts);
Seth Moore59146252021-07-02 08:59:23 -0700140}
141
Max Biresf60987e2021-04-16 13:35:20 -0700142} // namespace
143
Seth Moore01688562021-06-22 12:59:32 -0700144int main(int argc, char** argv) {
145 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
146
Sean Thomas61c3ed52024-10-16 11:25:42 +0000147 auto requireUdsCertsRpcNames = parseCommaDelimited(FLAGS_require_uds_certs);
148
Seth Moore59146252021-07-02 08:59:23 -0700149 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
Sean Thomas61c3ed52024-10-16 11:25:42 +0000150 &requireUdsCertsRpcNames, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700151
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700152 // Append drm csr's
Sean Thomas61c3ed52024-10-16 11:25:42 +0000153 for (auto const& [name, irpc] : android::mediadrm::getDrmRemotelyProvisionedComponents()) {
154 auto requireUdsCerts = requireUdsCertsRpcNames.count(name) != 0;
155 requireUdsCertsRpcNames.erase(name);
156 getCsrForIRpc(IDrmFactory::descriptor, name.c_str(), irpc.get(), requireUdsCerts);
157 }
158
159 for (auto const& rpcName : requireUdsCertsRpcNames) {
160 std::cerr << "WARNING: You requested to enforce the presence of UDS Certs for '" << rpcName
161 << "', but no Remotely Provisioned Component had that name." << std::endl;
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700162 }
163
Seth Moore59146252021-07-02 08:59:23 -0700164 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700165}