blob: c748b2a7a9746251ddf6449cbbd500877a7e140e [file] [log] [blame]
Jiyong Parka7266ac2021-05-17 21:57:24 +09001/*
2 * Copyright (C) 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 */
Andrew Scull66616612021-06-17 16:41:03 +000016#include <aidl/android/system/keystore2/IKeystoreService.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090017#include <aidl/android/system/virtualmachineservice/IVirtualMachineService.h>
18#include <aidl/com/android/microdroid/testservice/BnTestService.h>
Inseob Kimdb319702022-01-20 13:12:43 +090019#include <android-base/file.h>
Inseob Kim691df6a2022-01-20 12:54:30 +090020#include <android-base/properties.h>
Andrew Scull11cf0902021-06-22 12:08:10 +000021#include <android-base/result.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090022#include <android-base/unique_fd.h>
Andrew Scull66616612021-06-17 16:41:03 +000023#include <android/binder_auto_utils.h>
24#include <android/binder_manager.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090025#include <fcntl.h>
Inseob Kimdb319702022-01-20 13:12:43 +090026#include <fsverity_digests.pb.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090027#include <linux/vm_sockets.h>
28#include <stdint.h>
Jiyong Parka7266ac2021-05-17 21:57:24 +090029#include <stdio.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090030#include <sys/ioctl.h>
Jiyong Park23934392021-06-16 01:59:10 +090031#include <sys/system_properties.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090032#include <unistd.h>
33
34#include <binder_rpc_unstable.hpp>
Inseob Kim691df6a2022-01-20 12:54:30 +090035#include <string>
Jiyong Parka7266ac2021-05-17 21:57:24 +090036
Andrew Scull11cf0902021-06-22 12:08:10 +000037using aidl::android::hardware::security::keymint::Algorithm;
38using aidl::android::hardware::security::keymint::Digest;
39using aidl::android::hardware::security::keymint::KeyParameter;
40using aidl::android::hardware::security::keymint::KeyParameterValue;
41using aidl::android::hardware::security::keymint::KeyPurpose;
Andrew Scull66616612021-06-17 16:41:03 +000042using aidl::android::hardware::security::keymint::SecurityLevel;
Andrew Scull11cf0902021-06-22 12:08:10 +000043using aidl::android::hardware::security::keymint::Tag;
Andrew Scull66616612021-06-17 16:41:03 +000044
Andrew Scull11cf0902021-06-22 12:08:10 +000045using aidl::android::system::keystore2::CreateOperationResponse;
46using aidl::android::system::keystore2::Domain;
Andrew Scull66616612021-06-17 16:41:03 +000047using aidl::android::system::keystore2::IKeystoreSecurityLevel;
48using aidl::android::system::keystore2::IKeystoreService;
Andrew Scull11cf0902021-06-22 12:08:10 +000049using aidl::android::system::keystore2::KeyDescriptor;
50using aidl::android::system::keystore2::KeyMetadata;
51
Inseob Kim06a64d62021-09-07 21:21:45 +090052using aidl::android::system::virtualmachineservice::IVirtualMachineService;
53
54using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000055using android::base::Error;
56using android::base::Result;
Inseob Kim06a64d62021-09-07 21:21:45 +090057using android::base::unique_fd;
Andrew Scull66616612021-06-17 16:41:03 +000058
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090059extern void testlib_sub();
60
Andrew Scull66616612021-06-17 16:41:03 +000061namespace {
62
Andrew Scull11cf0902021-06-22 12:08:10 +000063Result<void> test_keystore() {
64 // Connect to Keystore.
Andrew Scull66616612021-06-17 16:41:03 +000065 ndk::SpAIBinder binder(
Andrew Sculleb5bd052021-07-13 00:56:03 +000066 AServiceManager_waitForService("android.system.keystore2.IKeystoreService/default"));
Andrew Scull66616612021-06-17 16:41:03 +000067 auto service = IKeystoreService::fromBinder(binder);
68 if (service == nullptr) {
Andrew Scull11cf0902021-06-22 12:08:10 +000069 return Error() << "Failed to find Keystore";
Andrew Scull66616612021-06-17 16:41:03 +000070 }
71 std::shared_ptr<IKeystoreSecurityLevel> securityLevel;
72 auto status = service->getSecurityLevel(SecurityLevel::TRUSTED_ENVIRONMENT, &securityLevel);
73 if (!status.isOk()) {
Andrew Scull11cf0902021-06-22 12:08:10 +000074 return Error() << "Failed to get security level";
Andrew Scull66616612021-06-17 16:41:03 +000075 }
Andrew Scull11cf0902021-06-22 12:08:10 +000076
77 // Create a signing key.
78 std::vector<KeyParameter> params;
79
80 KeyParameter algo;
81 algo.tag = Tag::ALGORITHM;
82 algo.value = KeyParameterValue::make<KeyParameterValue::algorithm>(Algorithm::HMAC);
83 params.push_back(algo);
84
85 KeyParameter key_size;
86 key_size.tag = Tag::KEY_SIZE;
87 key_size.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
88 params.push_back(key_size);
89
90 KeyParameter min_mac_length;
91 min_mac_length.tag = Tag::MIN_MAC_LENGTH;
92 min_mac_length.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
93 params.push_back(min_mac_length);
94
95 KeyParameter digest;
96 digest.tag = Tag::DIGEST;
97 digest.value = KeyParameterValue::make<KeyParameterValue::digest>(Digest::SHA_2_256);
98 params.push_back(digest);
99
100 KeyParameter purposeSign;
101 purposeSign.tag = Tag::PURPOSE;
102 purposeSign.value = KeyParameterValue::make<KeyParameterValue::keyPurpose>(KeyPurpose::SIGN);
103 params.push_back(purposeSign);
104
105 KeyParameter purposeVerify;
106 purposeVerify.tag = Tag::PURPOSE;
107 purposeVerify.value =
108 KeyParameterValue::make<KeyParameterValue::keyPurpose>(KeyPurpose::VERIFY);
109 params.push_back(purposeVerify);
110
111 KeyParameter auth;
112 auth.tag = Tag::NO_AUTH_REQUIRED;
113 auth.value = KeyParameterValue::make<KeyParameterValue::boolValue>(true);
114 params.push_back(auth);
115
116 KeyDescriptor descriptor;
117 descriptor.domain = Domain::SELINUX;
118 descriptor.alias = "payload-test-key";
119 descriptor.nspace = 140; // vm_payload_key
120
121 KeyMetadata metadata;
122 status = securityLevel->generateKey(descriptor, {}, params, 0, {}, &metadata);
123 if (!status.isOk()) {
124 return Error() << "Failed to create new HMAC key";
125 }
126
127 // Sign something.
128 params.clear();
129 params.push_back(algo);
130 params.push_back(digest);
131 params.push_back(purposeSign);
132
133 KeyParameter mac_length;
134 mac_length.tag = Tag::MAC_LENGTH;
135 mac_length.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
136 params.push_back(mac_length);
137
138 CreateOperationResponse opResponse;
139 status = securityLevel->createOperation(descriptor, params, false, &opResponse);
140 if (!status.isOk()) {
141 return Error() << "Failed to create keystore signing operation: "
142 << status.getServiceSpecificError();
143 }
144 auto operation = opResponse.iOperation;
145
146 std::string message = "This is the message to sign";
147 std::optional<std::vector<uint8_t>> out;
148 status = operation->update({message.begin(), message.end()}, &out);
149 if (!status.isOk()) {
150 return Error() << "Failed to call keystore update operation.";
151 }
152
153 std::optional<std::vector<uint8_t>> signature;
154 status = operation->finish({}, {}, &signature);
155 if (!status.isOk()) {
156 return Error() << "Failed to call keystore finish operation.";
157 }
158
159 if (!signature.has_value()) {
160 return Error() << "Didn't receive a signature from keystore finish operation.";
161 }
162
163 // Verify the signature.
164 params.clear();
165 params.push_back(algo);
166 params.push_back(digest);
167 params.push_back(purposeVerify);
168
169 status = securityLevel->createOperation(descriptor, params, false, &opResponse);
170 if (!status.isOk()) {
171 return Error() << "Failed to create keystore verification operation: "
172 << status.getServiceSpecificError();
173 }
174 operation = opResponse.iOperation;
175
176 status = operation->update({message.begin(), message.end()}, &out);
177 if (!status.isOk()) {
178 return Error() << "Failed to call keystore update operation.";
179 }
180
181 std::optional<std::vector<uint8_t>> out_signature;
182 status = operation->finish({}, signature.value(), &out_signature);
183 if (!status.isOk()) {
184 return Error() << "Failed to call keystore finish operation.";
185 }
186
187 return {};
188}
189
190template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +0000191Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +0000192 auto property = "debug.microdroid.test." + name;
193 std::stringstream outcome;
194 if (result.ok()) {
195 outcome << "PASS";
196 } else {
197 outcome << "FAIL: " << result.error();
Inseob Kimdb319702022-01-20 13:12:43 +0900198 // Pollute stderr with the error in case the property is truncated.
199 std::cerr << "[" << name << "] test failed: " << result.error() << "\n";
Andrew Scull11cf0902021-06-22 12:08:10 +0000200 }
201 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +0000202 return result;
Andrew Scull66616612021-06-17 16:41:03 +0000203}
204
Inseob Kim06a64d62021-09-07 21:21:45 +0900205Result<void> start_test_service() {
206 class TestService : public aidl::com::android::microdroid::testservice::BnTestService {
207 ndk::ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
208 *out = a + b;
209 return ndk::ScopedAStatus::ok();
210 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900211
212 ndk::ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
213 *out = android::base::GetProperty(prop, "");
214 if (out->empty()) {
215 std::string msg = "cannot find property " + prop;
216 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
217 msg.c_str());
218 }
219
220 return ndk::ScopedAStatus::ok();
221 }
Inseob Kim06a64d62021-09-07 21:21:45 +0900222 };
223 auto testService = ndk::SharedRefBase::make<TestService>();
224
225 auto callback = []([[maybe_unused]] void* param) {
226 // Tell microdroid_manager that we're ready.
227 // Failing to notify is not a fatal error; the payload can continue.
228 ndk::SpAIBinder binder(
229 RpcClient(VMADDR_CID_HOST, IVirtualMachineService::VM_BINDER_SERVICE_PORT));
230 auto virtualMachineService = IVirtualMachineService::fromBinder(binder);
231 if (virtualMachineService == nullptr) {
232 std::cerr << "failed to connect VirtualMachineService";
233 return;
234 }
Inseob Kimc7d28c72021-10-25 14:28:10 +0000235 if (!virtualMachineService->notifyPayloadReady().isOk()) {
Inseob Kim06a64d62021-09-07 21:21:45 +0900236 std::cerr << "failed to notify payload ready to virtualizationservice";
237 }
238 };
239
240 if (!RunRpcServerCallback(testService->asBinder().get(), testService->SERVICE_PORT, callback,
241 nullptr)) {
242 return Error() << "RPC Server failed to run";
243 }
244
245 return {};
246}
247
Inseob Kimdb319702022-01-20 13:12:43 +0900248Result<void> verify_apk() {
249 const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb";
250
251 std::string str;
252 if (!android::base::ReadFileToString(path, &str)) {
253 return ErrnoError() << "failed to read build_manifest.pb";
254 }
255
256 if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) {
257 return Error() << "invalid build_manifest.pb";
258 }
259
260 return {};
261}
262
Andrew Scull66616612021-06-17 16:41:03 +0000263} // Anonymous namespace
264
Jiyong Park40699612021-05-24 16:55:06 +0900265extern "C" int android_native_main(int argc, char* argv[]) {
Inseob Kim06a64d62021-09-07 21:21:45 +0900266 // disable buffering to communicate seamlessly
267 setvbuf(stdin, nullptr, _IONBF, 0);
268 setvbuf(stdout, nullptr, _IONBF, 0);
269 setvbuf(stderr, nullptr, _IONBF, 0);
270
Jiyong Park40699612021-05-24 16:55:06 +0900271 printf("Hello Microdroid ");
272 for (int i = 0; i < argc; i++) {
273 printf("%s", argv[i]);
274 bool last = i == (argc - 1);
275 if (!last) {
276 printf(" ");
277 }
278 }
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900279 testlib_sub();
Jiyong Park40699612021-05-24 16:55:06 +0900280 printf("\n");
Jiyong Park23934392021-06-16 01:59:10 +0900281
Inseob Kimdb319702022-01-20 13:12:43 +0900282 // Extra apks may be missing; this is not a fatal error
283 report_test("extra_apk", verify_apk());
284
Jiyong Park23934392021-06-16 01:59:10 +0900285 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000286 if (!report_test("keystore", test_keystore()).ok()) return 1;
287
Inseob Kim06a64d62021-09-07 21:21:45 +0900288 if (auto res = start_test_service(); res.ok()) {
289 return 0;
290 } else {
291 std::cerr << "starting service failed: " << res.error();
292 return 1;
293 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900294}