Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 1 | /* |
| 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 Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 16 | #include <aidl/android/system/keystore2/IKeystoreService.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 17 | #include <aidl/android/system/virtualmachineservice/IVirtualMachineService.h> |
| 18 | #include <aidl/com/android/microdroid/testservice/BnTestService.h> |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 19 | #include <android-base/result.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 20 | #include <android-base/unique_fd.h> |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 21 | #include <android/binder_auto_utils.h> |
| 22 | #include <android/binder_manager.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 23 | #include <fcntl.h> |
| 24 | #include <linux/vm_sockets.h> |
| 25 | #include <stdint.h> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 26 | #include <stdio.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 27 | #include <sys/ioctl.h> |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 28 | #include <sys/system_properties.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <binder_rpc_unstable.hpp> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 32 | |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 33 | using aidl::android::hardware::security::keymint::Algorithm; |
| 34 | using aidl::android::hardware::security::keymint::Digest; |
| 35 | using aidl::android::hardware::security::keymint::KeyParameter; |
| 36 | using aidl::android::hardware::security::keymint::KeyParameterValue; |
| 37 | using aidl::android::hardware::security::keymint::KeyPurpose; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 38 | using aidl::android::hardware::security::keymint::SecurityLevel; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 39 | using aidl::android::hardware::security::keymint::Tag; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 40 | |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 41 | using aidl::android::system::keystore2::CreateOperationResponse; |
| 42 | using aidl::android::system::keystore2::Domain; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 43 | using aidl::android::system::keystore2::IKeystoreSecurityLevel; |
| 44 | using aidl::android::system::keystore2::IKeystoreService; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 45 | using aidl::android::system::keystore2::KeyDescriptor; |
| 46 | using aidl::android::system::keystore2::KeyMetadata; |
| 47 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 48 | using aidl::android::system::virtualmachineservice::IVirtualMachineService; |
| 49 | |
| 50 | using android::base::ErrnoError; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 51 | using android::base::Error; |
| 52 | using android::base::Result; |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 53 | using android::base::unique_fd; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 54 | |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 55 | extern void testlib_sub(); |
| 56 | |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 57 | namespace { |
| 58 | |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 59 | Result<void> test_keystore() { |
| 60 | // Connect to Keystore. |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 61 | ndk::SpAIBinder binder( |
Andrew Scull | eb5bd05 | 2021-07-13 00:56:03 +0000 | [diff] [blame] | 62 | AServiceManager_waitForService("android.system.keystore2.IKeystoreService/default")); |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 63 | auto service = IKeystoreService::fromBinder(binder); |
| 64 | if (service == nullptr) { |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 65 | return Error() << "Failed to find Keystore"; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 66 | } |
| 67 | std::shared_ptr<IKeystoreSecurityLevel> securityLevel; |
| 68 | auto status = service->getSecurityLevel(SecurityLevel::TRUSTED_ENVIRONMENT, &securityLevel); |
| 69 | if (!status.isOk()) { |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 70 | return Error() << "Failed to get security level"; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 71 | } |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 72 | |
| 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 | |
| 186 | template <typename T> |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 187 | Result<T> report_test(std::string name, Result<T> result) { |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 188 | 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 Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 198 | return result; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 201 | Result<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 | |
| 216 | Result<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 Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 250 | } // Anonymous namespace |
| 251 | |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 252 | extern "C" int android_native_main(int argc, char* argv[]) { |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 253 | // 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 Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 258 | 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 Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 266 | testlib_sub(); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 267 | printf("\n"); |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 268 | |
| 269 | __system_property_set("debug.microdroid.app.run", "true"); |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 270 | if (!report_test("keystore", test_keystore()).ok()) return 1; |
| 271 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame^] | 272 | 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 Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 278 | } |