blob: a6c7d728b34e3af7b19d1a897df8bc61699c05f5 [file] [log] [blame]
Max Biresf60987e2021-04-16 13:35:20 -07001/*
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 Moore01688562021-06-22 12:59:32 -070023#include <gflags/gflags.h>
Max Biresf60987e2021-04-16 13:35:20 -070024#include <keymaster/cppcose/cppcose.h>
25#include <log/log.h>
Seth Moore6dfb02a2021-06-18 15:43:09 -070026#include <remote_prov/remote_prov_utils.h>
Seth Moore5a40fa72021-06-22 13:48:59 -070027#include <sys/random.h>
Max Biresf60987e2021-04-16 13:35:20 -070028#include <vintf/VintfObject.h>
29
30using std::set;
31using std::string;
32using std::vector;
33
34using aidl::android::hardware::security::keymint::DeviceInfo;
35using aidl::android::hardware::security::keymint::IRemotelyProvisionedComponent;
36using aidl::android::hardware::security::keymint::MacedPublicKey;
37using aidl::android::hardware::security::keymint::ProtectedData;
Seth Moore6dfb02a2021-06-18 15:43:09 -070038using aidl::android::hardware::security::keymint::remote_prov::generateEekChain;
Seth Moore01688562021-06-22 12:59:32 -070039using aidl::android::hardware::security::keymint::remote_prov::getProdEekChain;
Max Biresf60987e2021-04-16 13:35:20 -070040
41using android::vintf::HalManifest;
42using android::vintf::VintfObject;
43
44using namespace cppbor;
45using namespace cppcose;
46
Seth Moore01688562021-06-22 12:59:32 -070047DEFINE_bool(test_mode, false, "If enabled, a fake EEK key/cert are used.");
48
Max Biresf60987e2021-04-16 13:35:20 -070049namespace {
50
51const string kPackage = "android.hardware.security.keymint";
52const string kInterface = "IRemotelyProvisionedComponent";
53const string kFormattedName = kPackage + "." + kInterface + "/";
54
Seth Moore5a40fa72021-06-22 13:48:59 -070055constexpr size_t kChallengeSize = 16;
56
57std::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 Biresf60987e2021-04-16 13:35:20 -070072}
73
74std::vector<uint8_t> composeCertificateRequest(ProtectedData&& protectedData,
Seth Moore5a40fa72021-06-22 13:48:59 -070075 DeviceInfo&& deviceInfo,
76 const std::vector<uint8_t>& challenge) {
Max Biresf60987e2021-04-16 13:35:20 -070077 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 Moore5a40fa72021-06-22 13:48:59 -070085 .add(challenge)
Max Biresf60987e2021-04-16 13:35:20 -070086 .add(EncodedItem(std::move(protectedData.protectedData)))
87 .add(std::move(emptyMacedKeysToSign));
88 return certificateRequest.encode();
89}
90
91int32_t errorMsg(string name) {
92 std::cerr << "Failed for rkp instance: " << name;
93 return -1;
94}
95
Seth Moore01688562021-06-22 12:59:32 -070096std::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 Biresf60987e2021-04-16 13:35:20 -0700109} // namespace
110
Seth Moore01688562021-06-22 12:59:32 -0700111int main(int argc, char** argv) {
112 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
113
114 const std::vector<uint8_t> eek_chain = getEekChain();
Seth Moore5a40fa72021-06-22 13:48:59 -0700115 const std::vector<uint8_t> challenge = generateChallenge();
Seth Moore01688562021-06-22 12:59:32 -0700116
Max Biresf60987e2021-04-16 13:35:20 -0700117 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 Biresf60987e2021-04-16 13:35:20 -0700132 DeviceInfo deviceInfo;
133 ProtectedData protectedData;
134 if (rkp_service) {
135 ALOGE("extracting bundle");
136 ::ndk::ScopedAStatus status = rkp_service->generateCertificateRequest(
Seth Moore5a40fa72021-06-22 13:48:59 -0700137 FLAGS_test_mode, emptyKeys, eek_chain, challenge, &deviceInfo, &protectedData,
Max Biresf60987e2021-04-16 13:35:20 -0700138 &keysToSignMac);
139 if (!status.isOk()) {
140 ALOGE("Bundle extraction failed. Error code: %d", status.getServiceSpecificError());
141 return errorMsg(name);
142 }
Seth Moore5a40fa72021-06-22 13:48:59 -0700143 std::vector<uint8_t> certificateRequest = composeCertificateRequest(
144 std::move(protectedData), std::move(deviceInfo), challenge);
Max Biresf60987e2021-04-16 13:35:20 -0700145 std::copy(certificateRequest.begin(), certificateRequest.end(),
146 std::ostream_iterator<char>(std::cout));
147 }
148 }
149}