blob: 54355476195eaf220fd082af0271c175ea22a641 [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 Kim691df6a2022-01-20 12:54:30 +090019#include <android-base/properties.h>
Andrew Scull11cf0902021-06-22 12:08:10 +000020#include <android-base/result.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090021#include <android-base/unique_fd.h>
Andrew Scull66616612021-06-17 16:41:03 +000022#include <android/binder_auto_utils.h>
23#include <android/binder_manager.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090024#include <fcntl.h>
25#include <linux/vm_sockets.h>
26#include <stdint.h>
Jiyong Parka7266ac2021-05-17 21:57:24 +090027#include <stdio.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090028#include <sys/ioctl.h>
Jiyong Park23934392021-06-16 01:59:10 +090029#include <sys/system_properties.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090030#include <unistd.h>
31
32#include <binder_rpc_unstable.hpp>
Inseob Kim691df6a2022-01-20 12:54:30 +090033#include <string>
Jiyong Parka7266ac2021-05-17 21:57:24 +090034
Andrew Scull11cf0902021-06-22 12:08:10 +000035using aidl::android::hardware::security::keymint::Algorithm;
36using aidl::android::hardware::security::keymint::Digest;
37using aidl::android::hardware::security::keymint::KeyParameter;
38using aidl::android::hardware::security::keymint::KeyParameterValue;
39using aidl::android::hardware::security::keymint::KeyPurpose;
Andrew Scull66616612021-06-17 16:41:03 +000040using aidl::android::hardware::security::keymint::SecurityLevel;
Andrew Scull11cf0902021-06-22 12:08:10 +000041using aidl::android::hardware::security::keymint::Tag;
Andrew Scull66616612021-06-17 16:41:03 +000042
Andrew Scull11cf0902021-06-22 12:08:10 +000043using aidl::android::system::keystore2::CreateOperationResponse;
44using aidl::android::system::keystore2::Domain;
Andrew Scull66616612021-06-17 16:41:03 +000045using aidl::android::system::keystore2::IKeystoreSecurityLevel;
46using aidl::android::system::keystore2::IKeystoreService;
Andrew Scull11cf0902021-06-22 12:08:10 +000047using aidl::android::system::keystore2::KeyDescriptor;
48using aidl::android::system::keystore2::KeyMetadata;
49
Inseob Kim06a64d62021-09-07 21:21:45 +090050using aidl::android::system::virtualmachineservice::IVirtualMachineService;
51
52using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000053using android::base::Error;
54using android::base::Result;
Inseob Kim06a64d62021-09-07 21:21:45 +090055using android::base::unique_fd;
Andrew Scull66616612021-06-17 16:41:03 +000056
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090057extern void testlib_sub();
58
Andrew Scull66616612021-06-17 16:41:03 +000059namespace {
60
Andrew Scull11cf0902021-06-22 12:08:10 +000061Result<void> test_keystore() {
62 // Connect to Keystore.
Andrew Scull66616612021-06-17 16:41:03 +000063 ndk::SpAIBinder binder(
Andrew Sculleb5bd052021-07-13 00:56:03 +000064 AServiceManager_waitForService("android.system.keystore2.IKeystoreService/default"));
Andrew Scull66616612021-06-17 16:41:03 +000065 auto service = IKeystoreService::fromBinder(binder);
66 if (service == nullptr) {
Andrew Scull11cf0902021-06-22 12:08:10 +000067 return Error() << "Failed to find Keystore";
Andrew Scull66616612021-06-17 16:41:03 +000068 }
69 std::shared_ptr<IKeystoreSecurityLevel> securityLevel;
70 auto status = service->getSecurityLevel(SecurityLevel::TRUSTED_ENVIRONMENT, &securityLevel);
71 if (!status.isOk()) {
Andrew Scull11cf0902021-06-22 12:08:10 +000072 return Error() << "Failed to get security level";
Andrew Scull66616612021-06-17 16:41:03 +000073 }
Andrew Scull11cf0902021-06-22 12:08:10 +000074
75 // Create a signing key.
76 std::vector<KeyParameter> params;
77
78 KeyParameter algo;
79 algo.tag = Tag::ALGORITHM;
80 algo.value = KeyParameterValue::make<KeyParameterValue::algorithm>(Algorithm::HMAC);
81 params.push_back(algo);
82
83 KeyParameter key_size;
84 key_size.tag = Tag::KEY_SIZE;
85 key_size.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
86 params.push_back(key_size);
87
88 KeyParameter min_mac_length;
89 min_mac_length.tag = Tag::MIN_MAC_LENGTH;
90 min_mac_length.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
91 params.push_back(min_mac_length);
92
93 KeyParameter digest;
94 digest.tag = Tag::DIGEST;
95 digest.value = KeyParameterValue::make<KeyParameterValue::digest>(Digest::SHA_2_256);
96 params.push_back(digest);
97
98 KeyParameter purposeSign;
99 purposeSign.tag = Tag::PURPOSE;
100 purposeSign.value = KeyParameterValue::make<KeyParameterValue::keyPurpose>(KeyPurpose::SIGN);
101 params.push_back(purposeSign);
102
103 KeyParameter purposeVerify;
104 purposeVerify.tag = Tag::PURPOSE;
105 purposeVerify.value =
106 KeyParameterValue::make<KeyParameterValue::keyPurpose>(KeyPurpose::VERIFY);
107 params.push_back(purposeVerify);
108
109 KeyParameter auth;
110 auth.tag = Tag::NO_AUTH_REQUIRED;
111 auth.value = KeyParameterValue::make<KeyParameterValue::boolValue>(true);
112 params.push_back(auth);
113
114 KeyDescriptor descriptor;
115 descriptor.domain = Domain::SELINUX;
116 descriptor.alias = "payload-test-key";
117 descriptor.nspace = 140; // vm_payload_key
118
119 KeyMetadata metadata;
120 status = securityLevel->generateKey(descriptor, {}, params, 0, {}, &metadata);
121 if (!status.isOk()) {
122 return Error() << "Failed to create new HMAC key";
123 }
124
125 // Sign something.
126 params.clear();
127 params.push_back(algo);
128 params.push_back(digest);
129 params.push_back(purposeSign);
130
131 KeyParameter mac_length;
132 mac_length.tag = Tag::MAC_LENGTH;
133 mac_length.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
134 params.push_back(mac_length);
135
136 CreateOperationResponse opResponse;
137 status = securityLevel->createOperation(descriptor, params, false, &opResponse);
138 if (!status.isOk()) {
139 return Error() << "Failed to create keystore signing operation: "
140 << status.getServiceSpecificError();
141 }
142 auto operation = opResponse.iOperation;
143
144 std::string message = "This is the message to sign";
145 std::optional<std::vector<uint8_t>> out;
146 status = operation->update({message.begin(), message.end()}, &out);
147 if (!status.isOk()) {
148 return Error() << "Failed to call keystore update operation.";
149 }
150
151 std::optional<std::vector<uint8_t>> signature;
152 status = operation->finish({}, {}, &signature);
153 if (!status.isOk()) {
154 return Error() << "Failed to call keystore finish operation.";
155 }
156
157 if (!signature.has_value()) {
158 return Error() << "Didn't receive a signature from keystore finish operation.";
159 }
160
161 // Verify the signature.
162 params.clear();
163 params.push_back(algo);
164 params.push_back(digest);
165 params.push_back(purposeVerify);
166
167 status = securityLevel->createOperation(descriptor, params, false, &opResponse);
168 if (!status.isOk()) {
169 return Error() << "Failed to create keystore verification operation: "
170 << status.getServiceSpecificError();
171 }
172 operation = opResponse.iOperation;
173
174 status = operation->update({message.begin(), message.end()}, &out);
175 if (!status.isOk()) {
176 return Error() << "Failed to call keystore update operation.";
177 }
178
179 std::optional<std::vector<uint8_t>> out_signature;
180 status = operation->finish({}, signature.value(), &out_signature);
181 if (!status.isOk()) {
182 return Error() << "Failed to call keystore finish operation.";
183 }
184
185 return {};
186}
187
188template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +0000189Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +0000190 auto property = "debug.microdroid.test." + name;
191 std::stringstream outcome;
192 if (result.ok()) {
193 outcome << "PASS";
194 } else {
195 outcome << "FAIL: " << result.error();
196 // Pollute stdout with the error in case the property is truncated.
197 std::cout << "[" << name << "] test failed: " << result.error() << "\n";
198 }
199 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +0000200 return result;
Andrew Scull66616612021-06-17 16:41:03 +0000201}
202
Inseob Kim06a64d62021-09-07 21:21:45 +0900203Result<void> start_test_service() {
204 class TestService : public aidl::com::android::microdroid::testservice::BnTestService {
205 ndk::ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
206 *out = a + b;
207 return ndk::ScopedAStatus::ok();
208 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900209
210 ndk::ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
211 *out = android::base::GetProperty(prop, "");
212 if (out->empty()) {
213 std::string msg = "cannot find property " + prop;
214 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
215 msg.c_str());
216 }
217
218 return ndk::ScopedAStatus::ok();
219 }
Inseob Kim06a64d62021-09-07 21:21:45 +0900220 };
221 auto testService = ndk::SharedRefBase::make<TestService>();
222
223 auto callback = []([[maybe_unused]] void* param) {
224 // Tell microdroid_manager that we're ready.
225 // Failing to notify is not a fatal error; the payload can continue.
226 ndk::SpAIBinder binder(
227 RpcClient(VMADDR_CID_HOST, IVirtualMachineService::VM_BINDER_SERVICE_PORT));
228 auto virtualMachineService = IVirtualMachineService::fromBinder(binder);
229 if (virtualMachineService == nullptr) {
230 std::cerr << "failed to connect VirtualMachineService";
231 return;
232 }
Inseob Kimc7d28c72021-10-25 14:28:10 +0000233 if (!virtualMachineService->notifyPayloadReady().isOk()) {
Inseob Kim06a64d62021-09-07 21:21:45 +0900234 std::cerr << "failed to notify payload ready to virtualizationservice";
235 }
236 };
237
238 if (!RunRpcServerCallback(testService->asBinder().get(), testService->SERVICE_PORT, callback,
239 nullptr)) {
240 return Error() << "RPC Server failed to run";
241 }
242
243 return {};
244}
245
Andrew Scull66616612021-06-17 16:41:03 +0000246} // Anonymous namespace
247
Jiyong Park40699612021-05-24 16:55:06 +0900248extern "C" int android_native_main(int argc, char* argv[]) {
Inseob Kim06a64d62021-09-07 21:21:45 +0900249 // disable buffering to communicate seamlessly
250 setvbuf(stdin, nullptr, _IONBF, 0);
251 setvbuf(stdout, nullptr, _IONBF, 0);
252 setvbuf(stderr, nullptr, _IONBF, 0);
253
Jiyong Park40699612021-05-24 16:55:06 +0900254 printf("Hello Microdroid ");
255 for (int i = 0; i < argc; i++) {
256 printf("%s", argv[i]);
257 bool last = i == (argc - 1);
258 if (!last) {
259 printf(" ");
260 }
261 }
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900262 testlib_sub();
Jiyong Park40699612021-05-24 16:55:06 +0900263 printf("\n");
Jiyong Park23934392021-06-16 01:59:10 +0900264
265 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000266 if (!report_test("keystore", test_keystore()).ok()) return 1;
267
Inseob Kim06a64d62021-09-07 21:21:45 +0900268 if (auto res = start_test_service(); res.ok()) {
269 return 0;
270 } else {
271 std::cerr << "starting service failed: " << res.error();
272 return 1;
273 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900274}