Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 17 | #include <aidl/android/hardware/drm/IDrmFactory.h> |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 18 | #include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h> |
| 19 | #include <android/binder_manager.h> |
| 20 | #include <cppbor.h> |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 21 | #include <gflags/gflags.h> |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 22 | #include <keymaster/cppcose/cppcose.h> |
Seth Moore | 9a4bc97 | 2021-07-22 16:46:07 -0700 | [diff] [blame] | 23 | #include <openssl/base64.h> |
Seth Moore | 6dfb02a | 2021-06-18 15:43:09 -0700 | [diff] [blame] | 24 | #include <remote_prov/remote_prov_utils.h> |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 25 | #include <sys/random.h> |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 26 | |
Alice Wang | c1b568a | 2024-05-13 09:15:20 +0000 | [diff] [blame] | 27 | #include <future> |
Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 28 | #include <string> |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 29 | #include <unordered_set> |
Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
Robert Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 32 | #include "DrmRkpAdapter.h" |
Seth Moore | 708da93 | 2022-08-18 14:38:05 -0700 | [diff] [blame] | 33 | #include "rkp_factory_extraction_lib.h" |
| 34 | |
Robert Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 35 | using aidl::android::hardware::drm::IDrmFactory; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 36 | using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent; |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 37 | using aidl::android::hardware::security::keymint::RpcHardwareInfo; |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 38 | using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild; |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 39 | using aidl::android::hardware::security::keymint::remote_prov::RKPVM_INSTANCE_NAME; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 40 | |
Seth Moore | 0475678 | 2022-09-13 16:09:15 -0700 | [diff] [blame] | 41 | DEFINE_string(output_format, "build+csr", "How to format the output. Defaults to 'build+csr'."); |
Seth Moore | dff09d0 | 2023-05-31 09:38:47 -0700 | [diff] [blame] | 42 | DEFINE_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 Wadhera | c37901f | 2024-09-10 22:41:49 +0000 | [diff] [blame] | 46 | DEFINE_bool(allow_degenerate, true, |
| 47 | "If true, self_test validation will allow degenerate DICE chains in the CSR."); |
chuanchuan.gao | 8ef6d1a | 2023-12-07 16:47:51 +0800 | [diff] [blame] | 48 | DEFINE_string(serialno_prop, "ro.serialno", |
| 49 | "The property of getting serial number. Defaults to 'ro.serialno'."); |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 50 | DEFINE_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 Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 54 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 55 | namespace { |
| 56 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 57 | // Various supported --output_format values. |
| 58 | constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary |
| 59 | constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build |
| 60 | // fingerprint plus CSR. |
| 61 | |
Robert Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 62 | std::string getFullServiceName(const char* descriptor, const char* name) { |
| 63 | return std::string(descriptor) + "/" + name; |
| 64 | } |
| 65 | |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 66 | void writeOutput(const std::string instance_name, const cppbor::Array& csr) { |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 67 | 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.gao | 8ef6d1a | 2023-12-07 16:47:51 +0800 | [diff] [blame] | 71 | auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr, FLAGS_serialno_prop); |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 72 | if (!error.empty()) { |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 73 | std::cerr << "Error JSON encoding the output: " << error << std::endl; |
| 74 | exit(-1); |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 75 | } |
| 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 Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 82 | exit(-1); |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 86 | void getCsrForIRpc(const char* descriptor, const char* name, IRemotelyProvisionedComponent* irpc, |
| 87 | bool requireUdsCerts) { |
Sean Thomas | 734e42a | 2024-11-07 23:19:43 +0000 | [diff] [blame^] | 88 | auto fullName = getFullServiceName(descriptor, name); |
Alice Wang | 16e3442 | 2024-06-07 12:41:22 +0000 | [diff] [blame] | 89 | // AVF RKP HAL is not always supported, so we need to check if it is supported before |
| 90 | // generating the CSR. |
Sean Thomas | 734e42a | 2024-11-07 23:19:43 +0000 | [diff] [blame^] | 91 | if (fullName == RKPVM_INSTANCE_NAME) { |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 92 | RpcHardwareInfo hwInfo; |
| 93 | auto status = irpc->getHardwareInfo(&hwInfo); |
| 94 | if (!status.isOk()) { |
| 95 | return; |
| 96 | } |
Alice Wang | 16e3442 | 2024-06-07 12:41:22 +0000 | [diff] [blame] | 97 | } |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 98 | |
| 99 | auto [request, errMsg] = |
| 100 | getCsr(name, irpc, FLAGS_self_test, FLAGS_allow_degenerate, requireUdsCerts); |
Robert Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 101 | if (!request) { |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 102 | std::cerr << "Unable to build CSR for '" << fullName << "': " << errMsg << ", exiting." |
| 103 | << std::endl; |
Robert Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 104 | exit(-1); |
| 105 | } |
| 106 | |
| 107 | writeOutput(std::string(name), *request); |
| 108 | } |
| 109 | |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 110 | // Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR |
| 111 | // for every IRemotelyProvisionedComponent. |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 112 | void getCsrForInstance(const char* name, void* context) { |
Robert Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 113 | auto fullName = getFullServiceName(IRemotelyProvisionedComponent::descriptor, name); |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 114 | std::future<AIBinder*> waitForServiceFunc = |
Alice Wang | c1b568a | 2024-05-13 09:15:20 +0000 | [diff] [blame] | 115 | std::async(std::launch::async, AServiceManager_waitForService, fullName.c_str()); |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 116 | 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 Wang | c1b568a | 2024-05-13 09:15:20 +0000 | [diff] [blame] | 119 | exit(-1); |
| 120 | } |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 121 | AIBinder* rkpAiBinder = waitForServiceFunc.get(); |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 122 | ::ndk::SpAIBinder rkp_binder(rkpAiBinder); |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 123 | auto rkpService = IRemotelyProvisionedComponent::fromBinder(rkp_binder); |
| 124 | if (!rkpService) { |
| 125 | std::cerr << "Unable to get binder object for '" << fullName << "', exiting." << std::endl; |
Keith Mok | b9462c1 | 2021-11-11 19:34:26 +0000 | [diff] [blame] | 126 | exit(-1); |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 129 | 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 Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 141 | } // namespace |
| 142 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 143 | int main(int argc, char** argv) { |
| 144 | gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true); |
| 145 | |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 146 | auto requireUdsCertsRpcNames = parseCommaDelimited(FLAGS_require_uds_certs); |
| 147 | |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 148 | AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor, |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 149 | &requireUdsCertsRpcNames, getCsrForInstance); |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 150 | |
Sean Thomas | 734e42a | 2024-11-07 23:19:43 +0000 | [diff] [blame^] | 151 | // Append drm CSRs |
Sean Thomas | 61c3ed5 | 2024-10-16 11:25:42 +0000 | [diff] [blame] | 152 | 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 Shih | d3c1f7c | 2023-07-10 13:07:35 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 163 | return 0; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 164 | } |