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> |
| 25 | #include <log/log.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 | #include <vintf/VintfObject.h> |
| 29 | |
| 30 | using std::set; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | |
| 34 | using aidl::android::hardware::security::keymint::DeviceInfo; |
| 35 | using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent; |
| 36 | using aidl::android::hardware::security::keymint::MacedPublicKey; |
| 37 | using aidl::android::hardware::security::keymint::ProtectedData; |
Seth Moore | 6dfb02a | 2021-06-18 15:43:09 -0700 | [diff] [blame] | 38 | using aidl::android::hardware::security::keymint::remote_prov::generateEekChain; |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 39 | using aidl::android::hardware::security::keymint::remote_prov::getProdEekChain; |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame^] | 40 | using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 41 | |
| 42 | using android::vintf::HalManifest; |
| 43 | using android::vintf::VintfObject; |
| 44 | |
| 45 | using namespace cppbor; |
| 46 | using namespace cppcose; |
| 47 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 48 | DEFINE_bool(test_mode, false, "If enabled, a fake EEK key/cert are used."); |
| 49 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame^] | 50 | DEFINE_string(output_format, "csr", "How to format the output. Defaults to 'csr'."); |
| 51 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 52 | namespace { |
| 53 | |
| 54 | const string kPackage = "android.hardware.security.keymint"; |
| 55 | const string kInterface = "IRemotelyProvisionedComponent"; |
| 56 | const string kFormattedName = kPackage + "." + kInterface + "/"; |
| 57 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame^] | 58 | // Various supported --output_format values. |
| 59 | constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary |
| 60 | constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build |
| 61 | // fingerprint plus CSR. |
| 62 | |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 63 | constexpr size_t kChallengeSize = 16; |
| 64 | |
| 65 | std::vector<uint8_t> generateChallenge() { |
| 66 | std::vector<uint8_t> challenge(kChallengeSize); |
| 67 | |
| 68 | ssize_t bytesRemaining = static_cast<ssize_t>(challenge.size()); |
| 69 | uint8_t* writePtr = challenge.data(); |
| 70 | while (bytesRemaining > 0) { |
| 71 | int bytesRead = getrandom(writePtr, bytesRemaining, /*flags=*/0); |
| 72 | if (bytesRead < 0) { |
| 73 | LOG_FATAL_IF(errno != EINTR, "%d - %s", errno, strerror(errno)); |
| 74 | } |
| 75 | bytesRemaining -= bytesRead; |
| 76 | writePtr += bytesRead; |
| 77 | } |
| 78 | |
| 79 | return challenge; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame^] | 82 | Array composeCertificateRequest(ProtectedData&& protectedData, DeviceInfo&& deviceInfo, |
| 83 | const std::vector<uint8_t>& challenge) { |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 84 | Array emptyMacedKeysToSign; |
| 85 | emptyMacedKeysToSign |
| 86 | .add(std::vector<uint8_t>(0)) // empty protected headers as bstr |
| 87 | .add(Map()) // empty unprotected headers |
| 88 | .add(Null()) // nil for the payload |
| 89 | .add(std::vector<uint8_t>(0)); // empty tag as bstr |
| 90 | Array certificateRequest; |
| 91 | certificateRequest.add(EncodedItem(std::move(deviceInfo.deviceInfo))) |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 92 | .add(challenge) |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 93 | .add(EncodedItem(std::move(protectedData.protectedData))) |
| 94 | .add(std::move(emptyMacedKeysToSign)); |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame^] | 95 | return certificateRequest; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | int32_t errorMsg(string name) { |
| 99 | std::cerr << "Failed for rkp instance: " << name; |
| 100 | return -1; |
| 101 | } |
| 102 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 103 | std::vector<uint8_t> getEekChain() { |
| 104 | if (FLAGS_test_mode) { |
| 105 | const std::vector<uint8_t> kFakeEekId = {'f', 'a', 'k', 'e', 0}; |
| 106 | auto eekOrErr = generateEekChain(3 /* chainlength */, kFakeEekId); |
| 107 | LOG_FATAL_IF(!eekOrErr, "Failed to generate test EEK somehow: %s", |
| 108 | eekOrErr.message().c_str()); |
| 109 | auto [eek, ignored_pubkey, ignored_privkey] = eekOrErr.moveValue(); |
| 110 | return eek; |
| 111 | } |
| 112 | |
| 113 | return getProdEekChain(); |
| 114 | } |
| 115 | |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame^] | 116 | void writeOutput(const Array& csr) { |
| 117 | if (FLAGS_output_format == kBinaryCsrOutput) { |
| 118 | auto bytes = csr.encode(); |
| 119 | std::copy(bytes.begin(), bytes.end(), std::ostream_iterator<char>(std::cout)); |
| 120 | } else if (FLAGS_output_format == kBuildPlusCsr) { |
| 121 | auto [json, error] = jsonEncodeCsrWithBuild(csr); |
| 122 | if (!error.empty()) { |
| 123 | std::cerr << "Error JSON encoding the output: " << error; |
| 124 | exit(1); |
| 125 | } |
| 126 | std::cout << json << std::endl; |
| 127 | } else { |
| 128 | std::cerr << "Unexpected output_format '" << FLAGS_output_format << "'" << std::endl; |
| 129 | std::cerr << "Valid formats:" << std::endl; |
| 130 | std::cerr << " " << kBinaryCsrOutput << std::endl; |
| 131 | std::cerr << " " << kBuildPlusCsr << std::endl; |
| 132 | exit(1); |
| 133 | } |
| 134 | } |
| 135 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 136 | } // namespace |
| 137 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 138 | int main(int argc, char** argv) { |
| 139 | gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true); |
| 140 | |
| 141 | const std::vector<uint8_t> eek_chain = getEekChain(); |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 142 | const std::vector<uint8_t> challenge = generateChallenge(); |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 143 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 144 | std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest(); |
| 145 | set<string> rkpNames = manifest->getAidlInstances(kPackage, kInterface); |
| 146 | for (auto name : rkpNames) { |
| 147 | string fullName = kFormattedName + name; |
| 148 | if (!AServiceManager_isDeclared(fullName.c_str())) { |
| 149 | ALOGE("Could not find the following instance declared in the manifest: %s\n", |
| 150 | fullName.c_str()); |
| 151 | return errorMsg(name); |
| 152 | } |
| 153 | AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str()); |
| 154 | ::ndk::SpAIBinder rkp_binder(rkpAiBinder); |
| 155 | auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder); |
| 156 | std::vector<uint8_t> keysToSignMac; |
| 157 | std::vector<MacedPublicKey> emptyKeys; |
| 158 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 159 | DeviceInfo deviceInfo; |
| 160 | ProtectedData protectedData; |
| 161 | if (rkp_service) { |
| 162 | ALOGE("extracting bundle"); |
| 163 | ::ndk::ScopedAStatus status = rkp_service->generateCertificateRequest( |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame] | 164 | FLAGS_test_mode, emptyKeys, eek_chain, challenge, &deviceInfo, &protectedData, |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 165 | &keysToSignMac); |
| 166 | if (!status.isOk()) { |
| 167 | ALOGE("Bundle extraction failed. Error code: %d", status.getServiceSpecificError()); |
| 168 | return errorMsg(name); |
| 169 | } |
Seth Moore | e44aad2 | 2021-06-25 17:38:24 -0700 | [diff] [blame^] | 170 | writeOutput(composeCertificateRequest(std::move(protectedData), std::move(deviceInfo), |
| 171 | challenge)); |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |