1 /* 2 * Copyright (c) 2019, 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 #pragma once 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include "aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.h" 23 24 #include <hwtrust/hwtrust.h> 25 #include <keymaster/cppcose/cppcose.h> 26 27 namespace aidl::android::hardware::security::keymint::remote_prov { 28 29 using bytevec = std::vector<uint8_t>; 30 using namespace cppcose; 31 32 constexpr std::string_view kErrorChallengeMismatch = "challenges do not match"; 33 constexpr std::string_view kErrorUdsCertsAreRequired = "UdsCerts are required"; 34 constexpr std::string_view kErrorKeysToSignMismatch = "KeysToSign do not match"; 35 constexpr std::string_view kErrorDiceChainIsDegenerate = "DICE chain is degenerate"; 36 37 extern bytevec kTestMacKey; 38 39 // The Google root key for the Endpoint Encryption Key chain, encoded as COSE_Sign1 40 inline constexpr uint8_t kCoseEncodedRootCert[] = { 41 0x84, 0x43, 0xa1, 0x01, 0x27, 0xa0, 0x58, 0x2a, 0xa4, 0x01, 0x01, 0x03, 0x27, 0x20, 0x06, 42 0x21, 0x58, 0x20, 0x99, 0xb9, 0xee, 0xdd, 0x5e, 0xe4, 0x52, 0xf6, 0x85, 0xc6, 0x4c, 0x62, 43 0xdc, 0x3e, 0x61, 0xab, 0x57, 0x48, 0x7d, 0x75, 0x37, 0x29, 0xad, 0x76, 0x80, 0x32, 0xd2, 44 0xb3, 0xcb, 0x63, 0x58, 0xd9, 0x58, 0x40, 0x1e, 0x22, 0x08, 0x4b, 0xa4, 0xb7, 0xa4, 0xc8, 45 0xd7, 0x4e, 0x03, 0x0e, 0xfe, 0xb8, 0xaf, 0x14, 0x4c, 0xa7, 0x3b, 0x6f, 0xa5, 0xcd, 0xdc, 46 0xda, 0x79, 0xc6, 0x2b, 0x64, 0xfe, 0x99, 0x39, 0xaf, 0x76, 0xe7, 0x80, 0xfa, 0x66, 0x00, 47 0x85, 0x0d, 0x07, 0x98, 0x2a, 0xac, 0x91, 0x5c, 0xa7, 0x25, 0x14, 0x49, 0x06, 0x34, 0x75, 48 0xca, 0x8a, 0x27, 0x7a, 0xd9, 0xe3, 0x5a, 0x49, 0xeb, 0x02, 0x03}; 49 50 // The Google Endpoint Encryption Key certificate, encoded as COSE_Sign1 51 inline constexpr uint8_t kCoseEncodedGeekCert[] = { 52 0x84, 0x43, 0xa1, 0x01, 0x27, 0xa0, 0x58, 0x4e, 0xa5, 0x01, 0x01, 0x02, 0x58, 0x20, 53 0xd0, 0xae, 0xc1, 0x15, 0xca, 0x2a, 0xcf, 0x73, 0xae, 0x6b, 0xcc, 0xcb, 0xd1, 0x96, 54 0x1d, 0x65, 0xe8, 0xb1, 0xdd, 0xd7, 0x4a, 0x1a, 0x37, 0xb9, 0x43, 0x3a, 0x97, 0xd5, 55 0x99, 0xdf, 0x98, 0x08, 0x03, 0x38, 0x18, 0x20, 0x04, 0x21, 0x58, 0x20, 0xbe, 0x85, 56 0xe7, 0x46, 0xc4, 0xa3, 0x42, 0x5a, 0x40, 0xd9, 0x36, 0x3a, 0xa6, 0x15, 0xd0, 0x2c, 57 0x58, 0x7e, 0x3d, 0xdc, 0x33, 0x02, 0x32, 0xd2, 0xfc, 0x5e, 0x1e, 0x87, 0x25, 0x5f, 58 0x72, 0x60, 0x58, 0x40, 0x9b, 0xcf, 0x90, 0xe2, 0x2e, 0x4b, 0xab, 0xd1, 0x18, 0xb1, 59 0x0e, 0x8e, 0x5d, 0x20, 0x27, 0x4b, 0x84, 0x58, 0xfe, 0xfc, 0x32, 0x90, 0x7e, 0x72, 60 0x05, 0x83, 0xbc, 0xd7, 0x82, 0xbe, 0xfa, 0x64, 0x78, 0x2d, 0x54, 0x10, 0x4b, 0xc0, 61 0x31, 0xbf, 0x6b, 0xe8, 0x1e, 0x35, 0xe2, 0xf0, 0x2d, 0xce, 0x6c, 0x2f, 0x4f, 0xf2, 62 0xf5, 0x4f, 0xa5, 0xd4, 0x83, 0xad, 0x96, 0xa2, 0xf1, 0x87, 0x58, 0x04}; 63 64 // The Google ECDSA P256 root key for the Endpoint Encryption Key chain, encoded as COSE_Sign1 65 inline constexpr uint8_t kCoseEncodedEcdsa256RootCert[] = { 66 0x84, 0x43, 0xa1, 0x01, 0x26, 0xa0, 0x58, 0x4d, 0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 67 0x58, 0x20, 0xf7, 0x14, 0x8a, 0xdb, 0x97, 0xf4, 0xcc, 0x53, 0xef, 0xd2, 0x64, 0x11, 0xc4, 0xe3, 68 0x75, 0x1f, 0x66, 0x1f, 0xa4, 0x71, 0x0c, 0x6c, 0xcf, 0xfa, 0x09, 0x46, 0x80, 0x74, 0x87, 0x54, 69 0xf2, 0xad, 0x22, 0x58, 0x20, 0x5e, 0x7f, 0x5b, 0xf6, 0xec, 0xe4, 0xf6, 0x19, 0xcc, 0xff, 0x13, 70 0x37, 0xfd, 0x0f, 0xa1, 0xc8, 0x93, 0xdb, 0x18, 0x06, 0x76, 0xc4, 0x5d, 0xe6, 0xd7, 0x6a, 0x77, 71 0x86, 0xc3, 0x2d, 0xaf, 0x8f, 0x58, 0x40, 0x2f, 0x97, 0x8e, 0x42, 0xfb, 0xbe, 0x07, 0x2d, 0x95, 72 0x47, 0x85, 0x47, 0x93, 0x40, 0xb0, 0x1f, 0xd4, 0x9b, 0x47, 0xa4, 0xc4, 0x44, 0xa9, 0xf2, 0xa1, 73 0x07, 0x87, 0x10, 0xc7, 0x9f, 0xcb, 0x11, 0xf4, 0xbf, 0x9f, 0xe8, 0x3b, 0xe0, 0xe7, 0x34, 0x4c, 74 0x15, 0xfc, 0x7b, 0xc3, 0x7e, 0x33, 0x05, 0xf4, 0xd1, 0x34, 0x3c, 0xed, 0x02, 0x04, 0x60, 0x7a, 75 0x15, 0xe0, 0x79, 0xd3, 0x8a, 0xff, 0x24}; 76 77 // The Google ECDSA P256 Endpoint Encryption Key certificate, encoded as COSE_Sign1 78 inline constexpr uint8_t kCoseEncodedEcdsa256GeekCert[] = { 79 0x84, 0x43, 0xa1, 0x01, 0x26, 0xa0, 0x58, 0x71, 0xa6, 0x01, 0x02, 0x02, 0x58, 0x20, 0x35, 0x73, 80 0xb7, 0x3f, 0xa0, 0x8a, 0x80, 0x89, 0xb1, 0x26, 0x67, 0xe9, 0xcb, 0x7c, 0x75, 0xa1, 0xaf, 0x02, 81 0x61, 0xfc, 0x6e, 0x65, 0x03, 0x91, 0x3b, 0xd3, 0x4b, 0x7d, 0x14, 0x94, 0x3e, 0x46, 0x03, 0x38, 82 0x18, 0x20, 0x01, 0x21, 0x58, 0x20, 0xe0, 0x41, 0xcf, 0x2f, 0x0f, 0x34, 0x0f, 0x1c, 0x33, 0x2c, 83 0x41, 0xb0, 0xcf, 0xd7, 0x0c, 0x30, 0x55, 0x35, 0xd2, 0x1e, 0x6a, 0x47, 0x13, 0x4b, 0x2e, 0xd1, 84 0x48, 0x96, 0x7e, 0x24, 0x9c, 0x68, 0x22, 0x58, 0x20, 0x1f, 0xce, 0x45, 0xc5, 0xfb, 0x61, 0xba, 85 0x81, 0x21, 0xf9, 0xe5, 0x05, 0x9b, 0x9b, 0x39, 0x0e, 0x76, 0x86, 0x86, 0x47, 0xb8, 0x1e, 0x2f, 86 0x45, 0xf1, 0xce, 0xaf, 0xda, 0x3f, 0x80, 0x68, 0xdb, 0x58, 0x40, 0x8c, 0xb3, 0xba, 0x7e, 0x20, 87 0x3e, 0x32, 0xb0, 0x68, 0xdf, 0x60, 0xd1, 0x1d, 0x7d, 0xf0, 0xac, 0x38, 0x8e, 0x51, 0xbc, 0xff, 88 0x6c, 0xe1, 0x67, 0x3b, 0x4a, 0x79, 0xbc, 0x56, 0x78, 0xb3, 0x99, 0xd8, 0x7c, 0x8a, 0x07, 0xd8, 89 0xda, 0xb5, 0xb5, 0x7f, 0x71, 0xf4, 0xd8, 0x6b, 0xdf, 0x33, 0x27, 0x34, 0x7b, 0x65, 0xd1, 0x2a, 90 0xeb, 0x86, 0x99, 0x98, 0xab, 0x3a, 0xb4, 0x80, 0xaa, 0xbd, 0x50}; 91 92 /** 93 * Generates random bytes. 94 */ 95 bytevec randomBytes(size_t numBytes); 96 97 const std::string DEFAULT_INSTANCE_NAME = 98 "android.hardware.security.keymint.IRemotelyProvisionedComponent/default"; 99 const std::string RKPVM_INSTANCE_NAME = 100 "android.hardware.security.keymint.IRemotelyProvisionedComponent/avf"; 101 102 /** 103 * Returns the portion of an instance name after the / 104 * e.g. for "android.hardware.security.keymint.IRemotelyProvisionedComponent/avf", 105 * it returns "avf". 106 */ 107 std::string_view deviceSuffix(std::string_view name); 108 109 struct EekChain { 110 bytevec chain; 111 bytevec last_pubkey; 112 bytevec last_privkey; 113 }; 114 115 /** 116 * Based on the supportedEekCurve, Generates an X25519/ECDH with the specified eekId 117 * and an Ed25519/ECDSA chain of the specified length. All keys are generated randomly. 118 */ 119 ErrMsgOr<EekChain> generateEekChain(int32_t supportedEekCurve, size_t length, const bytevec& eekId); 120 121 /** 122 * Returns the CBOR-encoded, production Google Endpoint Encryption Key chain. 123 */ 124 bytevec getProdEekChain(int32_t supportedEekCurve); 125 126 struct BccEntryData { 127 bytevec pubKey; 128 }; 129 130 struct JsonOutput { OkJsonOutput131 static JsonOutput Ok(std::string json) { return {std::move(json), ""}; } ErrorJsonOutput132 static JsonOutput Error(std::string error) { return {"", std::move(error)}; } 133 134 std::string output; 135 std::string error; // if non-empty, this describes what went wrong 136 }; 137 138 /** 139 * Take a given instance name and certificate request, then output a JSON blob 140 * containing the name, build fingerprint and certificate request. This data may 141 * be serialized, then later uploaded to the remote provisioning service. The 142 * input csr is not validated, only encoded. 143 * 144 * Output format: 145 * { 146 * "build_fingerprint": <string> 147 * "csr": <base64 CBOR CSR> 148 * "name": <string> 149 * } 150 */ 151 JsonOutput jsonEncodeCsrWithBuild(const std::string& instance_name, const cppbor::Array& csr, 152 const std::string& serialno_prop); 153 154 /** 155 * Parses a DeviceInfo structure from the given CBOR data. The parsed data is then validated to 156 * ensure it contains the minimum required data at the time of manufacturing. This is only a 157 * partial validation, as some fields may not be provisioned yet at the time this information 158 * is parsed in the manufacturing process. 159 */ 160 ErrMsgOr<std::unique_ptr<cppbor::Map>> parseAndValidateFactoryDeviceInfo( 161 const std::vector<uint8_t>& deviceInfoBytes, const RpcHardwareInfo& info); 162 163 /** 164 * Parses a DeviceInfo structure from the given CBOR data. The parsed data is then validated to 165 * ensure it is formatted correctly and that it contains the required values for Remote Key 166 * Provisioning. This is a full validation, and assumes the device is provisioned as if it is 167 * suitable for the end user. 168 */ 169 ErrMsgOr<std::unique_ptr<cppbor::Map>> parseAndValidateProductionDeviceInfo( 170 const std::vector<uint8_t>& deviceInfoBytes, const RpcHardwareInfo& info); 171 172 /** 173 * Verify the protected data as if the device is still early in the factory process and may not 174 * have all device identifiers provisioned yet. 175 */ 176 ErrMsgOr<std::vector<BccEntryData>> verifyFactoryProtectedData( 177 const DeviceInfo& deviceInfo, const cppbor::Array& keysToSign, 178 const std::vector<uint8_t>& keysToSignMac, const ProtectedData& protectedData, 179 const EekChain& eekChain, const std::vector<uint8_t>& eekId, const RpcHardwareInfo& info, 180 const std::string& instanceName, const std::vector<uint8_t>& challenge); 181 /** 182 * Verify the protected data as if the device is a final production sample. 183 */ 184 ErrMsgOr<std::vector<BccEntryData>> verifyProductionProtectedData( 185 const DeviceInfo& deviceInfo, const cppbor::Array& keysToSign, 186 const std::vector<uint8_t>& keysToSignMac, const ProtectedData& protectedData, 187 const EekChain& eekChain, const std::vector<uint8_t>& eekId, const RpcHardwareInfo& info, 188 const std::string& instanceName, const std::vector<uint8_t>& challenge, 189 bool allowAnyMode = false); 190 191 /** 192 * Verify the CSR as if the device is still early in the factory process and may not 193 * have all device identifiers provisioned yet. 194 */ 195 ErrMsgOr<std::unique_ptr<cppbor::Array>> verifyFactoryCsr(const cppbor::Array& keysToSign, 196 const std::vector<uint8_t>& csr, 197 const RpcHardwareInfo& info, 198 const std::string& instanceName, 199 const std::vector<uint8_t>& challenge, 200 bool allowDegenerate = true, 201 bool requireUdsCerts = false); 202 203 /** 204 * Verify the CSR as if the device is a final production sample. 205 */ 206 ErrMsgOr<std::unique_ptr<cppbor::Array>> verifyProductionCsr(const cppbor::Array& keysToSign, 207 const std::vector<uint8_t>& csr, 208 const RpcHardwareInfo& info, 209 const std::string& instanceName, 210 const std::vector<uint8_t>& challenge, 211 bool allowAnyMode = false); 212 213 /** Checks whether the CSR has a proper DICE chain. */ 214 ErrMsgOr<bool> isCsrWithProperDiceChain(const std::vector<uint8_t>& csr, 215 const std::string& instanceName); 216 217 /** Checks whether the CSRs contain DICE certificate chains that have root certificates 218 * with the same public key. 219 */ 220 ErrMsgOr<bool> compareRootPublicKeysInDiceChains(const std::vector<uint8_t>& csr1, 221 std::string_view instanceName1, 222 const std::vector<uint8_t>& csr2, 223 std::string_view instanceName2); 224 225 /** Checks whether the component name in the configuration descriptor in the leaf certificate 226 * of the primary KeyMint instance's DICE certificate chain contains "keymint" 227 */ 228 ErrMsgOr<bool> verifyComponentNameInKeyMintDiceChain(const std::vector<uint8_t>& csr); 229 230 /** Checks whether the DICE chain in the CSR has a certificate with a non-normal mode. */ 231 ErrMsgOr<bool> hasNonNormalModeInDiceChain(const std::vector<uint8_t>& csr, 232 std::string_view instanceName); 233 234 /** Verify the DICE chain. */ 235 ErrMsgOr<std::vector<BccEntryData>> validateBcc(const cppbor::Array* bcc, 236 hwtrust::DiceChain::Kind kind, bool allowAnyMode, 237 bool allowDegenerate, 238 const std::string& instanceName); 239 240 } // namespace aidl::android::hardware::security::keymint::remote_prov 241