blob: c439b990a558bbbea8cda58dd88363a57a030117 [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
17#include <string>
18#include <vector>
19
20#include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h>
21#include <android/binder_manager.h>
22#include <cppbor.h>
Seth Moore01688562021-06-22 12:59:32 -070023#include <gflags/gflags.h>
Max Biresf60987e2021-04-16 13:35:20 -070024#include <keymaster/cppcose/cppcose.h>
Seth Moore6dfb02a2021-06-18 15:43:09 -070025#include <remote_prov/remote_prov_utils.h>
Seth Moore5a40fa72021-06-22 13:48:59 -070026#include <sys/random.h>
Max Biresf60987e2021-04-16 13:35:20 -070027
28using aidl::android::hardware::security::keymint::DeviceInfo;
29using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
30using aidl::android::hardware::security::keymint::MacedPublicKey;
31using aidl::android::hardware::security::keymint::ProtectedData;
Seth Moore6dfb02a2021-06-18 15:43:09 -070032using aidl::android::hardware::security::keymint::remote_prov::generateEekChain;
Seth Moore01688562021-06-22 12:59:32 -070033using aidl::android::hardware::security::keymint::remote_prov::getProdEekChain;
Seth Mooree44aad22021-06-25 17:38:24 -070034using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Max Biresf60987e2021-04-16 13:35:20 -070035
Max Biresf60987e2021-04-16 13:35:20 -070036using namespace cppbor;
37using namespace cppcose;
38
Seth Moore01688562021-06-22 12:59:32 -070039DEFINE_bool(test_mode, false, "If enabled, a fake EEK key/cert are used.");
40
Seth Mooree44aad22021-06-25 17:38:24 -070041DEFINE_string(output_format, "csr", "How to format the output. Defaults to 'csr'.");
42
Max Biresf60987e2021-04-16 13:35:20 -070043namespace {
44
Seth Mooree44aad22021-06-25 17:38:24 -070045// Various supported --output_format values.
46constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
47constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
48 // fingerprint plus CSR.
49
Seth Moore5a40fa72021-06-22 13:48:59 -070050constexpr size_t kChallengeSize = 16;
51
52std::vector<uint8_t> generateChallenge() {
53 std::vector<uint8_t> challenge(kChallengeSize);
54
55 ssize_t bytesRemaining = static_cast<ssize_t>(challenge.size());
56 uint8_t* writePtr = challenge.data();
57 while (bytesRemaining > 0) {
58 int bytesRead = getrandom(writePtr, bytesRemaining, /*flags=*/0);
Seth Moore59146252021-07-02 08:59:23 -070059 if (bytesRead < 0 && errno != EINTR) {
60 std::cerr << errno << ": " << strerror(errno) << std::endl;
61 exit(-1);
Seth Moore5a40fa72021-06-22 13:48:59 -070062 }
63 bytesRemaining -= bytesRead;
64 writePtr += bytesRead;
65 }
66
67 return challenge;
Max Biresf60987e2021-04-16 13:35:20 -070068}
69
Seth Moore9ea08f22021-07-22 16:42:17 -070070Array composeCertificateRequest(const ProtectedData& protectedData,
71 const DeviceInfo& verifiedDeviceInfo,
72 const std::vector<uint8_t>& challenge,
73 const std::vector<uint8_t>& keysToSignMac) {
74 Array macedKeysToSign = Array()
75 .add(std::vector<uint8_t>(0)) // empty protected headers as bstr
76 .add(Map()) // empty unprotected headers
77 .add(Null()) // nil for the payload
78 .add(keysToSignMac); // MAC as returned from the HAL
79
80 Array deviceInfo =
81 Array().add(EncodedItem(verifiedDeviceInfo.deviceInfo)).add(Map()); // Empty device info
82
83 Array certificateRequest = Array()
84 .add(std::move(deviceInfo))
85 .add(challenge)
86 .add(EncodedItem(protectedData.protectedData))
87 .add(std::move(macedKeysToSign));
Seth Mooree44aad22021-06-25 17:38:24 -070088 return certificateRequest;
Max Biresf60987e2021-04-16 13:35:20 -070089}
90
Seth Moore01688562021-06-22 12:59:32 -070091std::vector<uint8_t> getEekChain() {
92 if (FLAGS_test_mode) {
93 const std::vector<uint8_t> kFakeEekId = {'f', 'a', 'k', 'e', 0};
94 auto eekOrErr = generateEekChain(3 /* chainlength */, kFakeEekId);
Seth Moore59146252021-07-02 08:59:23 -070095 if (!eekOrErr) {
96 std::cerr << "Failed to generate test EEK somehow: " << eekOrErr.message() << std::endl;
97 exit(-1);
98 }
Seth Moore01688562021-06-22 12:59:32 -070099 auto [eek, ignored_pubkey, ignored_privkey] = eekOrErr.moveValue();
100 return eek;
101 }
102
103 return getProdEekChain();
104}
105
Seth Mooree44aad22021-06-25 17:38:24 -0700106void writeOutput(const Array& csr) {
107 if (FLAGS_output_format == kBinaryCsrOutput) {
108 auto bytes = csr.encode();
109 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
110 } else if (FLAGS_output_format == kBuildPlusCsr) {
111 auto [json, error] = jsonEncodeCsrWithBuild(csr);
112 if (!error.empty()) {
113 std::cerr << "Error JSON encoding the output: " << error;
114 exit(1);
115 }
116 std::cout << json << std::endl;
117 } else {
118 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
119 std::cerr << "Valid formats:" << std::endl;
120 std::cerr << " " << kBinaryCsrOutput << std::endl;
121 std::cerr << " " << kBuildPlusCsr << std::endl;
122 exit(1);
123 }
124}
125
Seth Moore59146252021-07-02 08:59:23 -0700126// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
127// for every IRemotelyProvisionedComponent.
128void getCsrForInstance(const char* name, void* /*context*/) {
129 const std::vector<uint8_t> challenge = generateChallenge();
130
131 auto fullName = std::string(IRemotelyProvisionedComponent::descriptor) + "/" + name;
132 AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str());
133 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
134 auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
135 if (!rkp_service) {
136 std::cerr << "Unable to get binder object for '" << fullName << "', skipping.";
137 return;
138 }
139
140 std::vector<uint8_t> keysToSignMac;
141 std::vector<MacedPublicKey> emptyKeys;
Seth Moore9ea08f22021-07-22 16:42:17 -0700142 DeviceInfo verifiedDeviceInfo;
Seth Moore59146252021-07-02 08:59:23 -0700143 ProtectedData protectedData;
144 ::ndk::ScopedAStatus status = rkp_service->generateCertificateRequest(
Seth Moore9ea08f22021-07-22 16:42:17 -0700145 FLAGS_test_mode, emptyKeys, getEekChain(), challenge, &verifiedDeviceInfo, &protectedData,
Seth Moore59146252021-07-02 08:59:23 -0700146 &keysToSignMac);
147 if (!status.isOk()) {
148 std::cerr << "Bundle extraction failed for '" << fullName
149 << "'. Error code: " << status.getServiceSpecificError() << "." << std::endl;
150 exit(-1);
151 }
Seth Moore9ea08f22021-07-22 16:42:17 -0700152 auto request =
153 composeCertificateRequest(protectedData, verifiedDeviceInfo, challenge, keysToSignMac);
154 writeOutput(request);
Seth Moore59146252021-07-02 08:59:23 -0700155}
156
Max Biresf60987e2021-04-16 13:35:20 -0700157} // namespace
158
Seth Moore01688562021-06-22 12:59:32 -0700159int main(int argc, char** argv) {
160 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
161
Seth Moore59146252021-07-02 08:59:23 -0700162 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
163 /*context=*/nullptr, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700164
Seth Moore59146252021-07-02 08:59:23 -0700165 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700166}