blob: 2e59dbdf9c02501c7ed3443adf53a9ff08031cd8 [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 Moore9a4bc972021-07-22 16:46:07 -070025#include <openssl/base64.h>
Seth Moore6dfb02a2021-06-18 15:43:09 -070026#include <remote_prov/remote_prov_utils.h>
Seth Moore5a40fa72021-06-22 13:48:59 -070027#include <sys/random.h>
Max Biresf60987e2021-04-16 13:35:20 -070028
29using aidl::android::hardware::security::keymint::DeviceInfo;
30using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
31using aidl::android::hardware::security::keymint::MacedPublicKey;
32using aidl::android::hardware::security::keymint::ProtectedData;
Seth Moore6dfb02a2021-06-18 15:43:09 -070033using aidl::android::hardware::security::keymint::remote_prov::generateEekChain;
Seth Moore01688562021-06-22 12:59:32 -070034using aidl::android::hardware::security::keymint::remote_prov::getProdEekChain;
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 Moore01688562021-06-22 12:59:32 -070040DEFINE_bool(test_mode, false, "If enabled, a fake EEK key/cert are used.");
41
Seth Mooree44aad22021-06-25 17:38:24 -070042DEFINE_string(output_format, "csr", "How to format the output. Defaults to 'csr'.");
43
Max Biresf60987e2021-04-16 13:35:20 -070044namespace {
45
Seth Mooree44aad22021-06-25 17:38:24 -070046// Various supported --output_format values.
47constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
48constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
49 // fingerprint plus CSR.
50
Seth Moore5a40fa72021-06-22 13:48:59 -070051constexpr size_t kChallengeSize = 16;
52
Seth Moore9a4bc972021-07-22 16:46:07 -070053std::string toBase64(const std::vector<uint8_t>& buffer) {
54 size_t base64Length;
55 int rc = EVP_EncodedLength(&base64Length, buffer.size());
56 if (!rc) {
57 std::cerr << "Error getting base64 length. Size overflow?" << std::endl;
58 exit(-1);
59 }
60
61 std::string base64(base64Length, ' ');
62 rc = EVP_EncodeBlock(reinterpret_cast<uint8_t*>(base64.data()), buffer.data(), buffer.size());
63 ++rc; // Account for NUL, which BoringSSL does not for some reason.
64 if (rc != base64Length) {
65 std::cerr << "Error writing base64. Expected " << base64Length
66 << " bytes to be written, but " << rc << " bytes were actually written."
67 << std::endl;
68 exit(-1);
69 }
70 return base64;
71}
72
Seth Moore5a40fa72021-06-22 13:48:59 -070073std::vector<uint8_t> generateChallenge() {
74 std::vector<uint8_t> challenge(kChallengeSize);
75
76 ssize_t bytesRemaining = static_cast<ssize_t>(challenge.size());
77 uint8_t* writePtr = challenge.data();
78 while (bytesRemaining > 0) {
79 int bytesRead = getrandom(writePtr, bytesRemaining, /*flags=*/0);
Seth Moore59146252021-07-02 08:59:23 -070080 if (bytesRead < 0 && errno != EINTR) {
81 std::cerr << errno << ": " << strerror(errno) << std::endl;
82 exit(-1);
Seth Moore5a40fa72021-06-22 13:48:59 -070083 }
84 bytesRemaining -= bytesRead;
85 writePtr += bytesRead;
86 }
87
88 return challenge;
Max Biresf60987e2021-04-16 13:35:20 -070089}
90
Seth Moore9ea08f22021-07-22 16:42:17 -070091Array composeCertificateRequest(const ProtectedData& protectedData,
92 const DeviceInfo& verifiedDeviceInfo,
93 const std::vector<uint8_t>& challenge,
94 const std::vector<uint8_t>& keysToSignMac) {
95 Array macedKeysToSign = Array()
96 .add(std::vector<uint8_t>(0)) // empty protected headers as bstr
97 .add(Map()) // empty unprotected headers
98 .add(Null()) // nil for the payload
99 .add(keysToSignMac); // MAC as returned from the HAL
100
101 Array deviceInfo =
102 Array().add(EncodedItem(verifiedDeviceInfo.deviceInfo)).add(Map()); // Empty device info
103
104 Array certificateRequest = Array()
105 .add(std::move(deviceInfo))
106 .add(challenge)
107 .add(EncodedItem(protectedData.protectedData))
108 .add(std::move(macedKeysToSign));
Seth Mooree44aad22021-06-25 17:38:24 -0700109 return certificateRequest;
Max Biresf60987e2021-04-16 13:35:20 -0700110}
111
Seth Moore01688562021-06-22 12:59:32 -0700112std::vector<uint8_t> getEekChain() {
113 if (FLAGS_test_mode) {
114 const std::vector<uint8_t> kFakeEekId = {'f', 'a', 'k', 'e', 0};
115 auto eekOrErr = generateEekChain(3 /* chainlength */, kFakeEekId);
Seth Moore59146252021-07-02 08:59:23 -0700116 if (!eekOrErr) {
117 std::cerr << "Failed to generate test EEK somehow: " << eekOrErr.message() << std::endl;
118 exit(-1);
119 }
Seth Moore9a4bc972021-07-22 16:46:07 -0700120 auto [eek, pubkey, privkey] = eekOrErr.moveValue();
121 std::cout << "EEK raw keypair:" << std::endl;
122 std::cout << " pub: " << toBase64(pubkey) << std::endl;
123 std::cout << " priv: " << toBase64(privkey) << std::endl;
Seth Moore01688562021-06-22 12:59:32 -0700124 return eek;
125 }
126
127 return getProdEekChain();
128}
129
Seth Mooree44aad22021-06-25 17:38:24 -0700130void writeOutput(const Array& csr) {
131 if (FLAGS_output_format == kBinaryCsrOutput) {
132 auto bytes = csr.encode();
133 std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout));
134 } else if (FLAGS_output_format == kBuildPlusCsr) {
135 auto [json, error] = jsonEncodeCsrWithBuild(csr);
136 if (!error.empty()) {
137 std::cerr << "Error JSON encoding the output: " << error;
138 exit(1);
139 }
140 std::cout << json << std::endl;
141 } else {
142 std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl;
143 std::cerr << "Valid formats:" << std::endl;
144 std::cerr << " " << kBinaryCsrOutput << std::endl;
145 std::cerr << " " << kBuildPlusCsr << std::endl;
146 exit(1);
147 }
148}
149
Seth Moore59146252021-07-02 08:59:23 -0700150// Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR
151// for every IRemotelyProvisionedComponent.
152void getCsrForInstance(const char* name, void* /*context*/) {
153 const std::vector<uint8_t> challenge = generateChallenge();
154
155 auto fullName = std::string(IRemotelyProvisionedComponent::descriptor) + "/" + name;
156 AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str());
157 ::ndk::SpAIBinder rkp_binder(rkpAiBinder);
158 auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder);
159 if (!rkp_service) {
160 std::cerr << "Unable to get binder object for '" << fullName << "', skipping.";
161 return;
162 }
163
164 std::vector<uint8_t> keysToSignMac;
165 std::vector<MacedPublicKey> emptyKeys;
Seth Moore9ea08f22021-07-22 16:42:17 -0700166 DeviceInfo verifiedDeviceInfo;
Seth Moore59146252021-07-02 08:59:23 -0700167 ProtectedData protectedData;
168 ::ndk::ScopedAStatus status = rkp_service->generateCertificateRequest(
Seth Moore9ea08f22021-07-22 16:42:17 -0700169 FLAGS_test_mode, emptyKeys, getEekChain(), challenge, &verifiedDeviceInfo, &protectedData,
Seth Moore59146252021-07-02 08:59:23 -0700170 &keysToSignMac);
171 if (!status.isOk()) {
172 std::cerr << "Bundle extraction failed for '" << fullName
173 << "'. Error code: " << status.getServiceSpecificError() << "." << std::endl;
174 exit(-1);
175 }
Seth Moore9ea08f22021-07-22 16:42:17 -0700176 auto request =
177 composeCertificateRequest(protectedData, verifiedDeviceInfo, challenge, keysToSignMac);
178 writeOutput(request);
Seth Moore59146252021-07-02 08:59:23 -0700179}
180
Max Biresf60987e2021-04-16 13:35:20 -0700181} // namespace
182
Seth Moore01688562021-06-22 12:59:32 -0700183int main(int argc, char** argv) {
184 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
185
Seth Moore59146252021-07-02 08:59:23 -0700186 AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor,
187 /*context=*/nullptr, getCsrForInstance);
Seth Moore01688562021-06-22 12:59:32 -0700188
Seth Moore59146252021-07-02 08:59:23 -0700189 return 0;
Max Biresf60987e2021-04-16 13:35:20 -0700190}