Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 23 | #include <gflags/gflags.h> |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 24 | #include <keymaster/cppcose/cppcose.h> |
Seth Moore | 9a4bc97 | 2021-07-22 16:46:07 -0700 | [diff] [blame^] | 25 | #include <openssl/base64.h> |
Seth Moore | 6dfb02a | 2021-06-18 15:43:09 -0700 | [diff] [blame] | 26 | #include <remote_prov/remote_prov_utils.h> |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 27 | #include <sys/random.h> |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 28 | |
| 29 | using aidl::android::hardware::security::keymint::DeviceInfo; |
| 30 | using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent; |
| 31 | using aidl::android::hardware::security::keymint::MacedPublicKey; |
| 32 | using aidl::android::hardware::security::keymint::ProtectedData; |
Seth Moore | 6dfb02a | 2021-06-18 15:43:09 -0700 | [diff] [blame] | 33 | using aidl::android::hardware::security::keymint::remote_prov::generateEekChain; |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 34 | using aidl::android::hardware::security::keymint::remote_prov::getProdEekChain; |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 35 | using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 36 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 37 | using namespace cppbor; |
| 38 | using namespace cppcose; |
| 39 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 40 | DEFINE_bool(test_mode, false, "If enabled, a fake EEK key/cert are used."); |
| 41 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 42 | DEFINE_string(output_format, "csr", "How to format the output. Defaults to 'csr'."); |
| 43 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 46 | // Various supported --output_format values. |
| 47 | constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary |
| 48 | constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build |
| 49 | // fingerprint plus CSR. |
| 50 | |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 51 | constexpr size_t kChallengeSize = 16; |
| 52 | |
Seth Moore | 9a4bc97 | 2021-07-22 16:46:07 -0700 | [diff] [blame^] | 53 | std::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 Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 73 | std::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 Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 80 | if (bytesRead < 0 && errno != EINTR) { |
| 81 | std::cerr << errno << ": " << strerror(errno) << std::endl; |
| 82 | exit(-1); |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 83 | } |
| 84 | bytesRemaining -= bytesRead; |
| 85 | writePtr += bytesRead; |
| 86 | } |
| 87 | |
| 88 | return challenge; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Seth Moore | 9ea08f2 | 2021-07-22 16:42:17 -0700 | [diff] [blame] | 91 | Array 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 Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 109 | return certificateRequest; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 112 | std::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 Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 116 | if (!eekOrErr) { |
| 117 | std::cerr << "Failed to generate test EEK somehow: " << eekOrErr.message() << std::endl; |
| 118 | exit(-1); |
| 119 | } |
Seth Moore | 9a4bc97 | 2021-07-22 16:46:07 -0700 | [diff] [blame^] | 120 | 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 Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 124 | return eek; |
| 125 | } |
| 126 | |
| 127 | return getProdEekChain(); |
| 128 | } |
| 129 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame] | 130 | void 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 Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 150 | // Callback for AServiceManager_forEachDeclaredInstance that writes out a CSR |
| 151 | // for every IRemotelyProvisionedComponent. |
| 152 | void 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 Moore | 9ea08f2 | 2021-07-22 16:42:17 -0700 | [diff] [blame] | 166 | DeviceInfo verifiedDeviceInfo; |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 167 | ProtectedData protectedData; |
| 168 | ::ndk::ScopedAStatus status = rkp_service->generateCertificateRequest( |
Seth Moore | 9ea08f2 | 2021-07-22 16:42:17 -0700 | [diff] [blame] | 169 | FLAGS_test_mode, emptyKeys, getEekChain(), challenge, &verifiedDeviceInfo, &protectedData, |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 170 | &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 Moore | 9ea08f2 | 2021-07-22 16:42:17 -0700 | [diff] [blame] | 176 | auto request = |
| 177 | composeCertificateRequest(protectedData, verifiedDeviceInfo, challenge, keysToSignMac); |
| 178 | writeOutput(request); |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 181 | } // namespace |
| 182 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 183 | int main(int argc, char** argv) { |
| 184 | gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true); |
| 185 | |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 186 | AServiceManager_forEachDeclaredInstance(IRemotelyProvisionedComponent::descriptor, |
| 187 | /*context=*/nullptr, getCsrForInstance); |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 188 | |
Seth Moore | 5914625 | 2021-07-02 08:59:23 -0700 | [diff] [blame] | 189 | return 0; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 190 | } |