blob: 0a3a59a6f1f1e388bae8a6fb26012852616c2c64 [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
Seth Moore708da932022-08-18 14:38:05 -070027#include <string>
28#include <vector>
29
Robert Shihd3c1f7c2023-07-10 13:07:35 -070030#include "DrmRkpAdapter.h"
Seth Moore708da932022-08-18 14:38:05 -070031#include "rkp_factory_extraction_lib.h"
32
Robert Shihd3c1f7c2023-07-10 13:07:35 -070033using aidl::android::hardware::drm::IDrmFactory;
Max Biresf60987e2021-04-16 13:35:20 -070034using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
Seth Mooree44aad22021-06-25 17:38:24 -070035using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Max Biresf60987e2021-04-16 13:35:20 -070036
Max Biresf60987e2021-04-16 13:35:20 -070037using namespace cppbor;
38using namespace cppcose;
39
Seth Moore04756782022-09-13 16:09:15 -070040DEFINE_string(output_format, "build+csr", "How to format the output. Defaults to 'build+csr'.");
Seth Mooredff09d02023-05-31 09:38:47 -070041DEFINE_bool(self_test, true,
42 "If true, this tool performs a self-test, validating the payload for correctness. "
43 "This checks that the device on the factory line is producing valid output "
44 "before attempting to upload the output to the device info service.");
chuanchuan.gao8ef6d1a2023-12-07 16:47:51 +080045DEFINE_string(serialno_prop, "ro.serialno",
46 "The property of getting serial number. Defaults to 'ro.serialno'.");
Seth Mooree44aad22021-06-25 17:38:24 -070047
Max Biresf60987e2021-04-16 13:35:20 -070048namespace {
49
Seth Mooree44aad22021-06-25 17:38:24 -070050// Various supported --output_format values.
51constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
52constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
53 // fingerprint plus CSR.
54
Robert Shihd3c1f7c2023-07-10 13:07:35 -070055std::string getFullServiceName(const char* descriptor, const char* name) {
56 return std::string(descriptor) + "/" + name;
57}
58
Tommy Chiuce82be82022-03-09 04:23:19 +000059void writeOutput(const std::string instance_name, const Array& csr) {
Seth Mooree44aad22021-06-25 17:38:24 -070060 if (FLAGS_output_format == kBinaryCsrOutput) {
61 auto bytes = csr.encode();
62 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
63 } else if (FLAGS_output_format == kBuildPlusCsr) {
chuanchuan.gao8ef6d1a2023-12-07 16:47:51 +080064 auto [json, error] = jsonEncodeCsrWithBuild(instance_name, csr, FLAGS_serialno_prop);
Seth Mooree44aad22021-06-25 17:38:24 -070065 if (!error.empty()) {
66 std::cerr << "Error JSON encoding the output: " << error;
67 exit(1);
68 }
69 std::cout << json << std::endl;
70 } else {
71 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
72 std::cerr << "Valid formats:" << std::endl;
73 std::cerr << " " << kBinaryCsrOutput << std::endl;
74 std::cerr << " " << kBuildPlusCsr << std::endl;
75 exit(1);
76 }
77}
78
Robert Shihd3c1f7c2023-07-10 13:07:35 -070079void getCsrForIRpc(const char* descriptor, const char* name, IRemotelyProvisionedComponent* irpc) {
80 auto [request, errMsg] = getCsr(name, irpc, FLAGS_self_test);
81 auto fullName = getFullServiceName(descriptor, name);
82 if (!request) {
83 std::cerr << "Unable to build CSR for '" << fullName << ": " << errMsg << std::endl;
84 exit(-1);
85 }
86
87 writeOutput(std::string(name), *request);
88}
89
Seth Moore59146252021-07-02 08:59:23 -070090// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
91// for every IRemotelyProvisionedComponent.
92void getCsrForInstance(const char* name, void* /*context*/) {
Robert Shihd3c1f7c2023-07-10 13:07:35 -070093 auto fullName = getFullServiceName(IRemotelyProvisionedComponent::descriptor, name);
Seth Moore59146252021-07-02 08:59:23 -070094 AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str());
95 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
96 auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
97 if (!rkp_service) {
98 std::cerr << "Unable to get binder object for '" << fullName << "', skipping.";
Keith Mokb9462c12021-11-11 19:34:26 +000099 exit(-1);
Seth Moore59146252021-07-02 08:59:23 -0700100 }
101
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700102 getCsrForIRpc(IRemotelyProvisionedComponent::descriptor, name, rkp_service.get());
Seth Moore59146252021-07-02 08:59:23 -0700103}
104
Max Biresf60987e2021-04-16 13:35:20 -0700105} // namespace
106
Seth Moore01688562021-06-22 12:59:32 -0700107int main(int argc, char** argv) {
108 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
109
Seth Moore59146252021-07-02 08:59:23 -0700110 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
111 /*context=*/nullptr, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700112
Robert Shihd3c1f7c2023-07-10 13:07:35 -0700113 // Append drm csr's
114 for (auto const& e : android::mediadrm::getDrmRemotelyProvisionedComponents()) {
115 getCsrForIRpc(IDrmFactory::descriptor, e.first.c_str(), e.second.get());
116 }
117
Seth Moore59146252021-07-02 08:59:23 -0700118 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700119}