blob: 2903a08b9c6d91d6327a99a14ed937d4447d1717 [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>
Andrew Scull11cf0902021-06-22 12:08:10 +000019#include <android-base/result.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090020#include <android-base/unique_fd.h>
Andrew Scull66616612021-06-17 16:41:03 +000021#include <android/binder_auto_utils.h>
22#include <android/binder_manager.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090023#include <fcntl.h>
24#include <linux/vm_sockets.h>
25#include <stdint.h>
Jiyong Parka7266ac2021-05-17 21:57:24 +090026#include <stdio.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090027#include <sys/ioctl.h>
Jiyong Park23934392021-06-16 01:59:10 +090028#include <sys/system_properties.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090029#include <unistd.h>
30
31#include <binder_rpc_unstable.hpp>
Jiyong Parka7266ac2021-05-17 21:57:24 +090032
Andrew Scull11cf0902021-06-22 12:08:10 +000033using aidl::android::hardware::security::keymint::Algorithm;
34using aidl::android::hardware::security::keymint::Digest;
35using aidl::android::hardware::security::keymint::KeyParameter;
36using aidl::android::hardware::security::keymint::KeyParameterValue;
37using aidl::android::hardware::security::keymint::KeyPurpose;
Andrew Scull66616612021-06-17 16:41:03 +000038using aidl::android::hardware::security::keymint::SecurityLevel;
Andrew Scull11cf0902021-06-22 12:08:10 +000039using aidl::android::hardware::security::keymint::Tag;
Andrew Scull66616612021-06-17 16:41:03 +000040
Andrew Scull11cf0902021-06-22 12:08:10 +000041using aidl::android::system::keystore2::CreateOperationResponse;
42using aidl::android::system::keystore2::Domain;
Andrew Scull66616612021-06-17 16:41:03 +000043using aidl::android::system::keystore2::IKeystoreSecurityLevel;
44using aidl::android::system::keystore2::IKeystoreService;
Andrew Scull11cf0902021-06-22 12:08:10 +000045using aidl::android::system::keystore2::KeyDescriptor;
46using aidl::android::system::keystore2::KeyMetadata;
47
Inseob Kim06a64d62021-09-07 21:21:45 +090048using aidl::android::system::virtualmachineservice::IVirtualMachineService;
49
50using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000051using android::base::Error;
52using android::base::Result;
Inseob Kim06a64d62021-09-07 21:21:45 +090053using android::base::unique_fd;
Andrew Scull66616612021-06-17 16:41:03 +000054
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090055extern void testlib_sub();
56
Andrew Scull66616612021-06-17 16:41:03 +000057namespace {
58
Andrew Scull11cf0902021-06-22 12:08:10 +000059Result<void> test_keystore() {
60 // Connect to Keystore.
Andrew Scull66616612021-06-17 16:41:03 +000061 ndk::SpAIBinder binder(
Andrew Sculleb5bd052021-07-13 00:56:03 +000062 AServiceManager_waitForService("android.system.keystore2.IKeystoreService/default"));
Andrew Scull66616612021-06-17 16:41:03 +000063 auto service = IKeystoreService::fromBinder(binder);
64 if (service == nullptr) {
Andrew Scull11cf0902021-06-22 12:08:10 +000065 return Error() << "Failed to find Keystore";
Andrew Scull66616612021-06-17 16:41:03 +000066 }
67 std::shared_ptr<IKeystoreSecurityLevel> securityLevel;
68 auto status = service->getSecurityLevel(SecurityLevel::TRUSTED_ENVIRONMENT, &securityLevel);
69 if (!status.isOk()) {
Andrew Scull11cf0902021-06-22 12:08:10 +000070 return Error() << "Failed to get security level";
Andrew Scull66616612021-06-17 16:41:03 +000071 }
Andrew Scull11cf0902021-06-22 12:08:10 +000072
73 // Create a signing key.
74 std::vector<KeyParameter> params;
75
76 KeyParameter algo;
77 algo.tag = Tag::ALGORITHM;
78 algo.value = KeyParameterValue::make<KeyParameterValue::algorithm>(Algorithm::HMAC);
79 params.push_back(algo);
80
81 KeyParameter key_size;
82 key_size.tag = Tag::KEY_SIZE;
83 key_size.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
84 params.push_back(key_size);
85
86 KeyParameter min_mac_length;
87 min_mac_length.tag = Tag::MIN_MAC_LENGTH;
88 min_mac_length.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
89 params.push_back(min_mac_length);
90
91 KeyParameter digest;
92 digest.tag = Tag::DIGEST;
93 digest.value = KeyParameterValue::make<KeyParameterValue::digest>(Digest::SHA_2_256);
94 params.push_back(digest);
95
96 KeyParameter purposeSign;
97 purposeSign.tag = Tag::PURPOSE;
98 purposeSign.value = KeyParameterValue::make<KeyParameterValue::keyPurpose>(KeyPurpose::SIGN);
99 params.push_back(purposeSign);
100
101 KeyParameter purposeVerify;
102 purposeVerify.tag = Tag::PURPOSE;
103 purposeVerify.value =
104 KeyParameterValue::make<KeyParameterValue::keyPurpose>(KeyPurpose::VERIFY);
105 params.push_back(purposeVerify);
106
107 KeyParameter auth;
108 auth.tag = Tag::NO_AUTH_REQUIRED;
109 auth.value = KeyParameterValue::make<KeyParameterValue::boolValue>(true);
110 params.push_back(auth);
111
112 KeyDescriptor descriptor;
113 descriptor.domain = Domain::SELINUX;
114 descriptor.alias = "payload-test-key";
115 descriptor.nspace = 140; // vm_payload_key
116
117 KeyMetadata metadata;
118 status = securityLevel->generateKey(descriptor, {}, params, 0, {}, &metadata);
119 if (!status.isOk()) {
120 return Error() << "Failed to create new HMAC key";
121 }
122
123 // Sign something.
124 params.clear();
125 params.push_back(algo);
126 params.push_back(digest);
127 params.push_back(purposeSign);
128
129 KeyParameter mac_length;
130 mac_length.tag = Tag::MAC_LENGTH;
131 mac_length.value = KeyParameterValue::make<KeyParameterValue::integer>(256);
132 params.push_back(mac_length);
133
134 CreateOperationResponse opResponse;
135 status = securityLevel->createOperation(descriptor, params, false, &opResponse);
136 if (!status.isOk()) {
137 return Error() << "Failed to create keystore signing operation: "
138 << status.getServiceSpecificError();
139 }
140 auto operation = opResponse.iOperation;
141
142 std::string message = "This is the message to sign";
143 std::optional<std::vector<uint8_t>> out;
144 status = operation->update({message.begin(), message.end()}, &out);
145 if (!status.isOk()) {
146 return Error() << "Failed to call keystore update operation.";
147 }
148
149 std::optional<std::vector<uint8_t>> signature;
150 status = operation->finish({}, {}, &signature);
151 if (!status.isOk()) {
152 return Error() << "Failed to call keystore finish operation.";
153 }
154
155 if (!signature.has_value()) {
156 return Error() << "Didn't receive a signature from keystore finish operation.";
157 }
158
159 // Verify the signature.
160 params.clear();
161 params.push_back(algo);
162 params.push_back(digest);
163 params.push_back(purposeVerify);
164
165 status = securityLevel->createOperation(descriptor, params, false, &opResponse);
166 if (!status.isOk()) {
167 return Error() << "Failed to create keystore verification operation: "
168 << status.getServiceSpecificError();
169 }
170 operation = opResponse.iOperation;
171
172 status = operation->update({message.begin(), message.end()}, &out);
173 if (!status.isOk()) {
174 return Error() << "Failed to call keystore update operation.";
175 }
176
177 std::optional<std::vector<uint8_t>> out_signature;
178 status = operation->finish({}, signature.value(), &out_signature);
179 if (!status.isOk()) {
180 return Error() << "Failed to call keystore finish operation.";
181 }
182
183 return {};
184}
185
186template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +0000187Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +0000188 auto property = "debug.microdroid.test." + name;
189 std::stringstream outcome;
190 if (result.ok()) {
191 outcome << "PASS";
192 } else {
193 outcome << "FAIL: " << result.error();
194 // Pollute stdout with the error in case the property is truncated.
195 std::cout << "[" << name << "] test failed: " << result.error() << "\n";
196 }
197 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +0000198 return result;
Andrew Scull66616612021-06-17 16:41:03 +0000199}
200
Inseob Kim06a64d62021-09-07 21:21:45 +0900201Result<unsigned> get_local_cid() {
202 // TODO: remove this after VS can check the peer addresses of binder clients
203 unique_fd fd(open("/dev/vsock", O_RDONLY));
204 if (fd.get() == -1) {
205 return ErrnoError() << "failed to open /dev/vsock";
206 }
207
208 unsigned cid;
209 if (ioctl(fd.get(), IOCTL_VM_SOCKETS_GET_LOCAL_CID, &cid) == -1) {
210 return ErrnoError() << "failed to IOCTL_VM_SOCKETS_GET_LOCAL_CID";
211 }
212
213 return cid;
214}
215
216Result<void> start_test_service() {
217 class TestService : public aidl::com::android::microdroid::testservice::BnTestService {
218 ndk::ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
219 *out = a + b;
220 return ndk::ScopedAStatus::ok();
221 }
222 };
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 }
235 if (auto res = get_local_cid(); !res.ok()) {
236 std::cerr << "failed to get local cid: " << res.error();
237 } else if (!virtualMachineService->notifyPayloadReady(res.value()).isOk()) {
238 std::cerr << "failed to notify payload ready to virtualizationservice";
239 }
240 };
241
242 if (!RunRpcServerCallback(testService->asBinder().get(), testService->SERVICE_PORT, callback,
243 nullptr)) {
244 return Error() << "RPC Server failed to run";
245 }
246
247 return {};
248}
249
Andrew Scull66616612021-06-17 16:41:03 +0000250} // Anonymous namespace
251
Jiyong Park40699612021-05-24 16:55:06 +0900252extern "C" int android_native_main(int argc, char* argv[]) {
Inseob Kim06a64d62021-09-07 21:21:45 +0900253 // disable buffering to communicate seamlessly
254 setvbuf(stdin, nullptr, _IONBF, 0);
255 setvbuf(stdout, nullptr, _IONBF, 0);
256 setvbuf(stderr, nullptr, _IONBF, 0);
257
Jiyong Park40699612021-05-24 16:55:06 +0900258 printf("Hello Microdroid ");
259 for (int i = 0; i < argc; i++) {
260 printf("%s", argv[i]);
261 bool last = i == (argc - 1);
262 if (!last) {
263 printf(" ");
264 }
265 }
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900266 testlib_sub();
Jiyong Park40699612021-05-24 16:55:06 +0900267 printf("\n");
Jiyong Park23934392021-06-16 01:59:10 +0900268
269 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000270 if (!report_test("keystore", test_keystore()).ok()) return 1;
271
Inseob Kim06a64d62021-09-07 21:21:45 +0900272 if (auto res = start_test_service(); res.ok()) {
273 return 0;
274 } else {
275 std::cerr << "starting service failed: " << res.error();
276 return 1;
277 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900278}