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; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 40 | |
| 41 | using android::vintf::HalManifest; |
| 42 | using android::vintf::VintfObject; |
| 43 | |
| 44 | using namespace cppbor; |
| 45 | using namespace cppcose; |
| 46 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 47 | DEFINE_bool(test_mode, false, "If enabled, a fake EEK key/cert are used."); |
| 48 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | const string kPackage = "android.hardware.security.keymint"; |
| 52 | const string kInterface = "IRemotelyProvisionedComponent"; |
| 53 | const string kFormattedName = kPackage + "." + kInterface + "/"; |
| 54 | |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame^] | 55 | constexpr size_t kChallengeSize = 16; |
| 56 | |
| 57 | std::vector<uint8_t> generateChallenge() { |
| 58 | std::vector<uint8_t> challenge(kChallengeSize); |
| 59 | |
| 60 | ssize_t bytesRemaining = static_cast<ssize_t>(challenge.size()); |
| 61 | uint8_t* writePtr = challenge.data(); |
| 62 | while (bytesRemaining > 0) { |
| 63 | int bytesRead = getrandom(writePtr, bytesRemaining, /*flags=*/0); |
| 64 | if (bytesRead < 0) { |
| 65 | LOG_FATAL_IF(errno != EINTR, "%d - %s", errno, strerror(errno)); |
| 66 | } |
| 67 | bytesRemaining -= bytesRead; |
| 68 | writePtr += bytesRead; |
| 69 | } |
| 70 | |
| 71 | return challenge; |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | std::vector<uint8_t> composeCertificateRequest(ProtectedData&& protectedData, |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame^] | 75 | DeviceInfo&& deviceInfo, |
| 76 | const std::vector<uint8_t>& challenge) { |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 77 | Array emptyMacedKeysToSign; |
| 78 | emptyMacedKeysToSign |
| 79 | .add(std::vector<uint8_t>(0)) // empty protected headers as bstr |
| 80 | .add(Map()) // empty unprotected headers |
| 81 | .add(Null()) // nil for the payload |
| 82 | .add(std::vector<uint8_t>(0)); // empty tag as bstr |
| 83 | Array certificateRequest; |
| 84 | certificateRequest.add(EncodedItem(std::move(deviceInfo.deviceInfo))) |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame^] | 85 | .add(challenge) |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 86 | .add(EncodedItem(std::move(protectedData.protectedData))) |
| 87 | .add(std::move(emptyMacedKeysToSign)); |
| 88 | return certificateRequest.encode(); |
| 89 | } |
| 90 | |
| 91 | int32_t errorMsg(string name) { |
| 92 | std::cerr << "Failed for rkp instance: " << name; |
| 93 | return -1; |
| 94 | } |
| 95 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 96 | std::vector<uint8_t> getEekChain() { |
| 97 | if (FLAGS_test_mode) { |
| 98 | const std::vector<uint8_t> kFakeEekId = {'f', 'a', 'k', 'e', 0}; |
| 99 | auto eekOrErr = generateEekChain(3 /* chainlength */, kFakeEekId); |
| 100 | LOG_FATAL_IF(!eekOrErr, "Failed to generate test EEK somehow: %s", |
| 101 | eekOrErr.message().c_str()); |
| 102 | auto [eek, ignored_pubkey, ignored_privkey] = eekOrErr.moveValue(); |
| 103 | return eek; |
| 104 | } |
| 105 | |
| 106 | return getProdEekChain(); |
| 107 | } |
| 108 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 109 | } // namespace |
| 110 | |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 111 | int main(int argc, char** argv) { |
| 112 | gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true); |
| 113 | |
| 114 | const std::vector<uint8_t> eek_chain = getEekChain(); |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame^] | 115 | const std::vector<uint8_t> challenge = generateChallenge(); |
Seth Moore | 0168856 | 2021-06-22 12:59:32 -0700 | [diff] [blame] | 116 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 117 | std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest(); |
| 118 | set<string> rkpNames = manifest->getAidlInstances(kPackage, kInterface); |
| 119 | for (auto name : rkpNames) { |
| 120 | string fullName = kFormattedName + name; |
| 121 | if (!AServiceManager_isDeclared(fullName.c_str())) { |
| 122 | ALOGE("Could not find the following instance declared in the manifest: %s\n", |
| 123 | fullName.c_str()); |
| 124 | return errorMsg(name); |
| 125 | } |
| 126 | AIBinder* rkpAiBinder = AServiceManager_getService(fullName.c_str()); |
| 127 | ::ndk::SpAIBinder rkp_binder(rkpAiBinder); |
| 128 | auto rkp_service = IRemotelyProvisionedComponent::fromBinder(rkp_binder); |
| 129 | std::vector<uint8_t> keysToSignMac; |
| 130 | std::vector<MacedPublicKey> emptyKeys; |
| 131 | |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 132 | DeviceInfo deviceInfo; |
| 133 | ProtectedData protectedData; |
| 134 | if (rkp_service) { |
| 135 | ALOGE("extracting bundle"); |
| 136 | ::ndk::ScopedAStatus status = rkp_service->generateCertificateRequest( |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame^] | 137 | FLAGS_test_mode, emptyKeys, eek_chain, challenge, &deviceInfo, &protectedData, |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 138 | &keysToSignMac); |
| 139 | if (!status.isOk()) { |
| 140 | ALOGE("Bundle extraction failed. Error code: %d", status.getServiceSpecificError()); |
| 141 | return errorMsg(name); |
| 142 | } |
Seth Moore | 5a40fa7 | 2021-06-22 13:48:59 -0700 | [diff] [blame^] | 143 | std::vector<uint8_t> certificateRequest = composeCertificateRequest( |
| 144 | std::move(protectedData), std::move(deviceInfo), challenge); |
Max Bires | f60987e | 2021-04-16 13:35:20 -0700 | [diff] [blame] | 145 | std::copy(certificateRequest.begin(), certificateRequest.end(), |
| 146 | std::ostream_iterator<char>(std::cout)); |
| 147 | } |
| 148 | } |
| 149 | } |