blob: 9b046263036bb443b736ecc7f4498eedbea97e7f [file] [log] [blame]
Seth Moore708da932022-08-18 14:38:05 -07001/*
2 * Copyright 2022 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 "rkp_factory_extraction_lib.h"
18
19#include <aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h>
Seth Moore7fc83ab2023-02-24 16:25:07 -080020#include <android-base/properties.h>
Seth Moore708da932022-08-18 14:38:05 -070021#include <android/binder_manager.h>
22#include <cppbor.h>
Seth Mooreb84a1fb2022-09-13 12:02:49 -070023#include <cstddef>
24#include <cstdint>
25#include <cstring>
26#include <iterator>
Seth Moore708da932022-08-18 14:38:05 -070027#include <keymaster/cppcose/cppcose.h>
Seth Moore708da932022-08-18 14:38:05 -070028#include <remote_prov/remote_prov_utils.h>
29#include <sys/random.h>
30
31#include <memory>
32#include <optional>
33#include <string>
34#include <string_view>
Sean Thomas61c3ed52024-10-16 11:25:42 +000035#include <unordered_set>
Seth Moore708da932022-08-18 14:38:05 -070036#include <vector>
37
38#include "cppbor_parse.h"
39
40using aidl::android::hardware::security::keymint::DeviceInfo;
41using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
42using aidl::android::hardware::security::keymint::MacedPublicKey;
43using aidl::android::hardware::security::keymint::ProtectedData;
44using aidl::android::hardware::security::keymint::RpcHardwareInfo;
Sean Thomas61c3ed52024-10-16 11:25:42 +000045using aidl::android::hardware::security::keymint::remote_prov::BccEntryData;
Seth Moore04756782022-09-13 16:09:15 -070046using aidl::android::hardware::security::keymint::remote_prov::EekChain;
47using aidl::android::hardware::security::keymint::remote_prov::generateEekChain;
Seth Moore708da932022-08-18 14:38:05 -070048using aidl::android::hardware::security::keymint::remote_prov::getProdEekChain;
49using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Seth Moore04756782022-09-13 16:09:15 -070050using aidl::android::hardware::security::keymint::remote_prov::parseAndValidateFactoryDeviceInfo;
Tri Voee773a22022-10-26 16:07:52 -070051using aidl::android::hardware::security::keymint::remote_prov::verifyFactoryCsr;
Seth Moore04756782022-09-13 16:09:15 -070052using aidl::android::hardware::security::keymint::remote_prov::verifyFactoryProtectedData;
Seth Moore708da932022-08-18 14:38:05 -070053
Sean Thomas61c3ed52024-10-16 11:25:42 +000054using cppbor::Array;
55using cppbor::Map;
56using cppbor::Null;
57template <class T> using ErrMsgOr = cppcose::ErrMsgOr<T>;
Seth Moore708da932022-08-18 14:38:05 -070058
Tri Voee773a22022-10-26 16:07:52 -070059constexpr size_t kVersionWithoutSuperencryption = 3;
60
Seth Moore708da932022-08-18 14:38:05 -070061std::vector<uint8_t> generateChallenge() {
62 std::vector<uint8_t> challenge(kChallengeSize);
63
64 ssize_t bytesRemaining = static_cast<ssize_t>(challenge.size());
65 uint8_t* writePtr = challenge.data();
66 while (bytesRemaining > 0) {
67 int bytesRead = getrandom(writePtr, bytesRemaining, /*flags=*/0);
68 if (bytesRead < 0) {
69 if (errno == EINTR) {
70 continue;
71 } else {
Sean Thomas61c3ed52024-10-16 11:25:42 +000072 std::cerr << "generateChallenge: getrandom returned an error with errno " << errno
73 << ": " << strerror(errno) << ". Exiting..." << std::endl;
Seth Moore708da932022-08-18 14:38:05 -070074 exit(-1);
75 }
76 }
77 bytesRemaining -= bytesRead;
78 writePtr += bytesRead;
79 }
80
81 return challenge;
82}
83
Tri Voee773a22022-10-26 16:07:52 -070084CborResult<Array> composeCertificateRequestV1(const ProtectedData& protectedData,
85 const DeviceInfo& verifiedDeviceInfo,
86 const std::vector<uint8_t>& challenge,
87 const std::vector<uint8_t>& keysToSignMac,
Sean Thomas6d713f22024-11-08 00:01:23 +000088 const RpcHardwareInfo& rpcHardwareInfo) {
Seth Moore708da932022-08-18 14:38:05 -070089 Array macedKeysToSign = Array()
90 .add(Map().add(1, 5).encode()) // alg: hmac-sha256
91 .add(Map()) // empty unprotected headers
92 .add(Null()) // nil for the payload
93 .add(keysToSignMac); // MAC as returned from the HAL
94
Seth Mooreb84a1fb2022-09-13 12:02:49 -070095 ErrMsgOr<std::unique_ptr<Map>> parsedVerifiedDeviceInfo =
Sean Thomas6d713f22024-11-08 00:01:23 +000096 parseAndValidateFactoryDeviceInfo(verifiedDeviceInfo.deviceInfo, rpcHardwareInfo);
Seth Moore708da932022-08-18 14:38:05 -070097 if (!parsedVerifiedDeviceInfo) {
Seth Mooreb84a1fb2022-09-13 12:02:49 -070098 return {nullptr, parsedVerifiedDeviceInfo.moveMessage()};
Seth Moore708da932022-08-18 14:38:05 -070099 }
100
Sean Thomas61c3ed52024-10-16 11:25:42 +0000101 auto [parsedProtectedData, ignore2, errMsg] = cppbor::parse(protectedData.protectedData);
Seth Moore708da932022-08-18 14:38:05 -0700102 if (!parsedProtectedData) {
Seth Mooreb84a1fb2022-09-13 12:02:49 -0700103 std::cerr << "Error parsing protected data: '" << errMsg << "'" << std::endl;
Seth Moore708da932022-08-18 14:38:05 -0700104 return {nullptr, errMsg};
105 }
106
Seth Mooreb84a1fb2022-09-13 12:02:49 -0700107 Array deviceInfo = Array().add(parsedVerifiedDeviceInfo.moveValue()).add(Map());
Seth Moore708da932022-08-18 14:38:05 -0700108
109 auto certificateRequest = std::make_unique<Array>();
110 (*certificateRequest)
111 .add(std::move(deviceInfo))
112 .add(challenge)
113 .add(std::move(parsedProtectedData))
114 .add(std::move(macedKeysToSign));
Seth Mooreb84a1fb2022-09-13 12:02:49 -0700115 return {std::move(certificateRequest), ""};
Seth Moore708da932022-08-18 14:38:05 -0700116}
117
Tri Voee773a22022-10-26 16:07:52 -0700118CborResult<Array> getCsrV1(std::string_view componentName, IRemotelyProvisionedComponent* irpc) {
Seth Moore708da932022-08-18 14:38:05 -0700119 std::vector<uint8_t> keysToSignMac;
120 std::vector<MacedPublicKey> emptyKeys;
121 DeviceInfo verifiedDeviceInfo;
122 ProtectedData protectedData;
123 RpcHardwareInfo hwInfo;
124 ::ndk::ScopedAStatus status = irpc->getHardwareInfo(&hwInfo);
125 if (!status.isOk()) {
126 std::cerr << "Failed to get hardware info for '" << componentName
Robert Shih6c3e15b2023-12-18 20:09:02 -0800127 << "'. Description: " << status.getDescription() << "." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000128 return {nullptr, status.getDescription()};
Seth Moore708da932022-08-18 14:38:05 -0700129 }
130
131 const std::vector<uint8_t> eek = getProdEekChain(hwInfo.supportedEekCurve);
132 const std::vector<uint8_t> challenge = generateChallenge();
133 status = irpc->generateCertificateRequest(
134 /*test_mode=*/false, emptyKeys, eek, challenge, &verifiedDeviceInfo, &protectedData,
135 &keysToSignMac);
136 if (!status.isOk()) {
137 std::cerr << "Bundle extraction failed for '" << componentName
Robert Shih6c3e15b2023-12-18 20:09:02 -0800138 << "'. Description: " << status.getDescription() << "." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000139 return {nullptr, status.getDescription()};
Seth Moore708da932022-08-18 14:38:05 -0700140 }
Tri Voee773a22022-10-26 16:07:52 -0700141 return composeCertificateRequestV1(protectedData, verifiedDeviceInfo, challenge, keysToSignMac,
Sean Thomas6d713f22024-11-08 00:01:23 +0000142 hwInfo);
Seth Moore708da932022-08-18 14:38:05 -0700143}
Seth Moore04756782022-09-13 16:09:15 -0700144
Sean Thomas61c3ed52024-10-16 11:25:42 +0000145std::optional<std::string> selfTestGetCsrV1(std::string_view componentName,
146 IRemotelyProvisionedComponent* irpc) {
Seth Moore04756782022-09-13 16:09:15 -0700147 std::vector<uint8_t> keysToSignMac;
148 std::vector<MacedPublicKey> emptyKeys;
149 DeviceInfo verifiedDeviceInfo;
150 ProtectedData protectedData;
151 RpcHardwareInfo hwInfo;
152 ::ndk::ScopedAStatus status = irpc->getHardwareInfo(&hwInfo);
153 if (!status.isOk()) {
154 std::cerr << "Failed to get hardware info for '" << componentName
Robert Shih6c3e15b2023-12-18 20:09:02 -0800155 << "'. Description: " << status.getDescription() << "." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000156 return status.getDescription();
Seth Moore04756782022-09-13 16:09:15 -0700157 }
158
159 const std::vector<uint8_t> eekId = {0, 1, 2, 3, 4, 5, 6, 7};
160 ErrMsgOr<EekChain> eekChain = generateEekChain(hwInfo.supportedEekCurve, /*length=*/3, eekId);
161 if (!eekChain) {
162 std::cerr << "Error generating test EEK certificate chain: " << eekChain.message();
Sean Thomas61c3ed52024-10-16 11:25:42 +0000163 return eekChain.message();
Seth Moore04756782022-09-13 16:09:15 -0700164 }
165 const std::vector<uint8_t> challenge = generateChallenge();
166 status = irpc->generateCertificateRequest(
167 /*test_mode=*/true, emptyKeys, eekChain->chain, challenge, &verifiedDeviceInfo,
168 &protectedData, &keysToSignMac);
169 if (!status.isOk()) {
170 std::cerr << "Error generating test cert chain for '" << componentName
Robert Shih6c3e15b2023-12-18 20:09:02 -0800171 << "'. Description: " << status.getDescription() << "." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000172 return status.getDescription();
Seth Moore04756782022-09-13 16:09:15 -0700173 }
174
Sean Thomas6d713f22024-11-08 00:01:23 +0000175 auto result = verifyFactoryProtectedData(verifiedDeviceInfo, /*keysToSign=*/{}, keysToSignMac,
176 protectedData, *eekChain, eekId, hwInfo,
177 std::string(componentName), challenge);
Seth Moore04756782022-09-13 16:09:15 -0700178
Seth Mooredff09d02023-05-31 09:38:47 -0700179 if (!result) {
180 std::cerr << "Self test failed for IRemotelyProvisionedComponent '" << componentName
181 << "'. Error message: '" << result.message() << "'." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000182 return result.message();
Seth Mooredff09d02023-05-31 09:38:47 -0700183 }
Sean Thomas61c3ed52024-10-16 11:25:42 +0000184 return std::nullopt;
Tri Voee773a22022-10-26 16:07:52 -0700185}
186
187CborResult<Array> composeCertificateRequestV3(const std::vector<uint8_t>& csr) {
Seth Moore7fc83ab2023-02-24 16:25:07 -0800188 const std::string kFingerprintProp = "ro.build.fingerprint";
189
Tri Voee773a22022-10-26 16:07:52 -0700190 auto [parsedCsr, _, csrErrMsg] = cppbor::parse(csr);
191 if (!parsedCsr) {
192 return {nullptr, csrErrMsg};
193 }
194 if (!parsedCsr->asArray()) {
195 return {nullptr, "CSR is not a CBOR array."};
196 }
197
Seth Moore7fc83ab2023-02-24 16:25:07 -0800198 if (!::android::base::WaitForPropertyCreation(kFingerprintProp)) {
199 return {nullptr, "Unable to read build fingerprint"};
200 }
201
202 Map unverifiedDeviceInfo =
203 Map().add("fingerprint", ::android::base::GetProperty(kFingerprintProp, /*default=*/""));
204 parsedCsr->asArray()->add(std::move(unverifiedDeviceInfo));
Tri Voee773a22022-10-26 16:07:52 -0700205 return {std::unique_ptr<Array>(parsedCsr.release()->asArray()), ""};
206}
207
Sean Thomas61c3ed52024-10-16 11:25:42 +0000208CborResult<Array> getCsrV3(std::string_view componentName, IRemotelyProvisionedComponent* irpc,
209 bool selfTest, bool allowDegenerate, bool requireUdsCerts) {
Tri Voee773a22022-10-26 16:07:52 -0700210 std::vector<uint8_t> csr;
211 std::vector<MacedPublicKey> emptyKeys;
212 const std::vector<uint8_t> challenge = generateChallenge();
213
Sean Thomas6d713f22024-11-08 00:01:23 +0000214 RpcHardwareInfo hwInfo;
215 auto status = irpc->getHardwareInfo(&hwInfo);
216 if (!status.isOk()) {
217 std::cerr << "Failed to get hardware info for '" << componentName
218 << "'. Description: " << status.getDescription() << "." << std::endl;
219 return {nullptr, status.getDescription()};
220 }
221
222 status = irpc->generateCertificateRequestV2(emptyKeys, challenge, &csr);
Tri Voee773a22022-10-26 16:07:52 -0700223 if (!status.isOk()) {
224 std::cerr << "Bundle extraction failed for '" << componentName
Robert Shih6c3e15b2023-12-18 20:09:02 -0800225 << "'. Description: " << status.getDescription() << "." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000226 return {nullptr, status.getDescription()};
Tri Voee773a22022-10-26 16:07:52 -0700227 }
228
Seth Mooredff09d02023-05-31 09:38:47 -0700229 if (selfTest) {
Sean Thomas6d713f22024-11-08 00:01:23 +0000230 auto result = verifyFactoryCsr(/*keysToSign=*/cppbor::Array(), csr, hwInfo,
231 std::string(componentName), challenge, allowDegenerate,
232 requireUdsCerts);
Seth Mooredff09d02023-05-31 09:38:47 -0700233 if (!result) {
234 std::cerr << "Self test failed for IRemotelyProvisionedComponent '" << componentName
235 << "'. Error message: '" << result.message() << "'." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000236 return {nullptr, result.message()};
Seth Mooredff09d02023-05-31 09:38:47 -0700237 }
238 }
239
Tri Voee773a22022-10-26 16:07:52 -0700240 return composeCertificateRequestV3(csr);
241}
242
Seth Mooredff09d02023-05-31 09:38:47 -0700243CborResult<Array> getCsr(std::string_view componentName, IRemotelyProvisionedComponent* irpc,
Sean Thomas61c3ed52024-10-16 11:25:42 +0000244 bool selfTest, bool allowDegenerate, bool requireUdsCerts) {
Tri Voee773a22022-10-26 16:07:52 -0700245 RpcHardwareInfo hwInfo;
246 auto status = irpc->getHardwareInfo(&hwInfo);
247 if (!status.isOk()) {
248 std::cerr << "Failed to get hardware info for '" << componentName
Robert Shih6c3e15b2023-12-18 20:09:02 -0800249 << "'. Description: " << status.getDescription() << "." << std::endl;
Sean Thomas61c3ed52024-10-16 11:25:42 +0000250 return {nullptr, status.getDescription()};
Tri Voee773a22022-10-26 16:07:52 -0700251 }
252
253 if (hwInfo.versionNumber < kVersionWithoutSuperencryption) {
Seth Mooredff09d02023-05-31 09:38:47 -0700254 if (selfTest) {
Sean Thomas61c3ed52024-10-16 11:25:42 +0000255 auto errMsg = selfTestGetCsrV1(componentName, irpc);
256 if (errMsg) {
257 return {nullptr, *errMsg};
258 }
Seth Mooredff09d02023-05-31 09:38:47 -0700259 }
Tri Voee773a22022-10-26 16:07:52 -0700260 return getCsrV1(componentName, irpc);
261 } else {
Sean Thomas61c3ed52024-10-16 11:25:42 +0000262 return getCsrV3(componentName, irpc, selfTest, allowDegenerate, requireUdsCerts);
Tri Voee773a22022-10-26 16:07:52 -0700263 }
264}
Alice Wang16e34422024-06-07 12:41:22 +0000265
Sean Thomas61c3ed52024-10-16 11:25:42 +0000266std::unordered_set<std::string> parseCommaDelimited(const std::string& input) {
267 std::stringstream ss(input);
268 std::unordered_set<std::string> result;
269 while (ss.good()) {
270 std::string name;
271 std::getline(ss, name, ',');
Sean Thomas3f38c1c2024-11-05 10:51:56 +0000272 if (!name.empty()) {
273 result.insert(name);
274 }
Alice Wang16e34422024-06-07 12:41:22 +0000275 }
Sean Thomas61c3ed52024-10-16 11:25:42 +0000276 return result;
277}