blob: bf6b9a6b2f82a8181b50e26e5afe05c233abc8c9 [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;
Seth Mooree44aad22021-06-25 17:38:24 -070040using aidl::android::hardware::security::keymint::remote_prov::jsonEncodeCsrWithBuild;
Max Biresf60987e2021-04-16 13:35:20 -070041
42using android::vintf::HalManifest;
43using android::vintf::VintfObject;
44
45using namespace cppbor;
46using namespace cppcose;
47
Seth Moore01688562021-06-22 12:59:32 -070048DEFINE_bool(test_mode, false, "If enabled, a fake EEK key/cert are used.");
49
Seth Mooree44aad22021-06-25 17:38:24 -070050DEFINE_string(output_format, "csr", "How to format the output. Defaults to 'csr'.");
51
Max Biresf60987e2021-04-16 13:35:20 -070052namespace {
53
54const string kPackage = "android.hardware.security.keymint";
55const string kInterface = "IRemotelyProvisionedComponent";
56const string kFormattedName = kPackage + "." + kInterface + "/";
57
Seth Mooree44aad22021-06-25 17:38:24 -070058// Various supported --output_format values.
59constexpr std::string_view kBinaryCsrOutput = "csr"; // Just the raw csr as binary
60constexpr std::string_view kBuildPlusCsr = "build+csr"; // Text-encoded (JSON) build
61 // fingerprint plus CSR.
62
Seth Moore5a40fa72021-06-22 13:48:59 -070063constexpr size_t kChallengeSize = 16;
64
65std::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 Biresf60987e2021-04-16 13:35:20 -070080}
81
Seth Mooree44aad22021-06-25 17:38:24 -070082Array composeCertificateRequest(ProtectedData&& protectedData, DeviceInfo&& deviceInfo,
83 const std::vector<uint8_t>& challenge) {
Max Biresf60987e2021-04-16 13:35:20 -070084 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 Moore5a40fa72021-06-22 13:48:59 -070092 .add(challenge)
Max Biresf60987e2021-04-16 13:35:20 -070093 .add(EncodedItem(std::move(protectedData.protectedData)))
94 .add(std::move(emptyMacedKeysToSign));
Seth Mooree44aad22021-06-25 17:38:24 -070095 return certificateRequest;
Max Biresf60987e2021-04-16 13:35:20 -070096}
97
98int32_t errorMsg(string name) {
99 std::cerr << "Failed for rkp instance: " << name;
100 return -1;
101}
102
Seth Moore01688562021-06-22 12:59:32 -0700103std::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 Mooree44aad22021-06-25 17:38:24 -0700116void 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 Biresf60987e2021-04-16 13:35:20 -0700136} // namespace
137
Seth Moore01688562021-06-22 12:59:32 -0700138int main(int argc, char** argv) {
139 gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
140
141 const std::vector<uint8_t> eek_chain = getEekChain();
Seth Moore5a40fa72021-06-22 13:48:59 -0700142 const std::vector<uint8_t> challenge = generateChallenge();
Seth Moore01688562021-06-22 12:59:32 -0700143
Max Biresf60987e2021-04-16 13:35:20 -0700144 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 Biresf60987e2021-04-16 13:35:20 -0700159 DeviceInfo deviceInfo;
160 ProtectedData protectedData;
161 if (rkp_service) {
162 ALOGE("extracting bundle");
163 ::ndk::ScopedAStatus status = rkp_service->generateCertificateRequest(
Seth Moore5a40fa72021-06-22 13:48:59 -0700164 FLAGS_test_mode, emptyKeys, eek_chain, challenge, &deviceInfo, &protectedData,
Max Biresf60987e2021-04-16 13:35:20 -0700165 &keysToSignMac);
166 if (!status.isOk()) {
167 ALOGE("Bundle extraction failed. Error code: %d", status.getServiceSpecificError());
168 return errorMsg(name);
169 }
Seth Mooree44aad22021-06-25 17:38:24 -0700170 writeOutput(composeCertificateRequest(std::move(protectedData), std::move(deviceInfo),
171 challenge));
Max Biresf60987e2021-04-16 13:35:20 -0700172 }
173 }
174}