Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #pragma once |
| 18 | |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | #include <stddef.h> |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 21 | #include <stdint.h> |
Alan Stokes | d4ea5a8 | 2022-11-10 12:17:42 +0000 | [diff] [blame] | 22 | #include <sys/cdefs.h> |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 23 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame] | 24 | #include "vm_main.h" |
| 25 | |
Alan Stokes | d4ea5a8 | 2022-11-10 12:17:42 +0000 | [diff] [blame] | 26 | __BEGIN_DECLS |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 27 | |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 28 | struct AIBinder; |
| 29 | typedef struct AIBinder AIBinder; |
| 30 | |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 31 | /** |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 32 | * Introduced in API 35. |
| 33 | * Remote attestation result if the attestation succeeds. |
| 34 | */ |
| 35 | struct AVmAttestationResult; |
| 36 | |
| 37 | /** |
| 38 | * Introduced in API 35. |
| 39 | * Remote attestation status types returned from remote attestation functions. |
| 40 | */ |
| 41 | typedef enum attestation_status_t : int32_t { |
| 42 | /** The remote attestation completes successfully. */ |
| 43 | ATTESTATION_OK = 0, |
| 44 | |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 45 | /** The challenge size is not between 0 and 64. */ |
| 46 | ATTESTATION_ERROR_INVALID_CHALLENGE = -10001, |
Alice Wang | 677a5b8 | 2023-11-09 08:21:59 +0000 | [diff] [blame] | 47 | |
| 48 | /** Failed to attest the VM. Please retry at a later time. */ |
| 49 | ATTESTATION_ERROR_ATTESTATION_FAILED = -10002, |
| 50 | |
| 51 | /** Remote attestation is not supported in the current environment. */ |
| 52 | ATTESTATION_ERROR_UNSUPPORTED = -10003, |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 53 | } attestation_status_t; |
| 54 | |
| 55 | /** |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 56 | * Notifies the host that the payload is ready. |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 57 | * |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 58 | * If the host app has set a `VirtualMachineCallback` for the VM, its |
| 59 | * `onPayloadReady` method will be called. |
| 60 | * |
| 61 | * Note that subsequent calls to this function after the first have no effect; |
| 62 | * `onPayloadReady` is never called more than once. |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 63 | */ |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 64 | void AVmPayload_notifyPayloadReady(void); |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 65 | |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 66 | /** |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 67 | * Runs a binder RPC server, serving the supplied binder service implementation on the given vsock |
| 68 | * port. |
| 69 | * |
| 70 | * If and when the server is ready for connections (it is listening on the port), `on_ready` is |
| 71 | * called to allow appropriate action to be taken - e.g. to notify clients that they may now |
| 72 | * attempt to connect with `AVmPayload_notifyPayloadReady`. |
| 73 | * |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 74 | * Note that this function does not return. The calling thread joins the binder |
| 75 | * thread pool to handle incoming messages. |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 76 | * |
| 77 | * \param service the service to bind to the given port. |
| 78 | * \param port vsock port. |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 79 | * \param on_ready the callback to execute once the server is ready for connections. If not null the |
| 80 | * callback will be called at most once. |
| 81 | * \param param parameter to be passed to the `on_ready` callback. |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 82 | */ |
Henri Chataing | d6377b4 | 2023-11-10 16:21:02 +0000 | [diff] [blame] | 83 | __attribute__((noreturn)) void AVmPayload_runVsockRpcServer( |
| 84 | AIBinder* _Nonnull service, uint32_t port, |
| 85 | void (*_Nullable on_ready)(void* _Nullable param), void* _Nullable param); |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
Alan Stokes | 6979b5f | 2023-01-04 15:41:39 +0000 | [diff] [blame] | 88 | * Returns all or part of a 32-byte secret that is bound to this unique VM |
| 89 | * instance and the supplied identifier. The secret can be used e.g. as an |
| 90 | * encryption key. |
| 91 | * |
| 92 | * Every VM has a secret that is derived from a device-specific value known to |
| 93 | * the hypervisor, the code that runs in the VM and its non-modifiable |
| 94 | * configuration; it is not made available to the host OS. |
| 95 | * |
| 96 | * This function performs a further derivation from the VM secret and the |
| 97 | * supplied identifier. As long as the VM identity doesn't change the same value |
| 98 | * will be returned for the same identifier, even if the VM is stopped & |
| 99 | * restarted or the device rebooted. |
| 100 | * |
| 101 | * If multiple secrets are required for different purposes, a different |
| 102 | * identifier should be used for each. The identifiers otherwise are arbitrary |
| 103 | * byte sequences and do not need to be kept secret; typically they are |
| 104 | * hardcoded in the calling code. |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 105 | * |
| 106 | * \param identifier identifier of the secret to return. |
| 107 | * \param identifier_size size of the secret identifier. |
| 108 | * \param secret pointer to size bytes where the secret is written. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 109 | * \param size number of bytes of the secret to get, <= 32. |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 110 | */ |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 111 | void AVmPayload_getVmInstanceSecret(const void* _Nonnull identifier, size_t identifier_size, |
| 112 | void* _Nonnull secret, size_t size); |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 115 | * Gets the path to the APK contents. It is a directory, under which are |
| 116 | * the unzipped contents of the APK containing the payload, all read-only |
| 117 | * but accessible to the payload. |
| 118 | * |
| 119 | * \return the path to the APK contents. The returned string should not be |
| 120 | * deleted or freed by the application. The string remains valid for the |
| 121 | * lifetime of the VM. |
| 122 | */ |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 123 | const char* _Nonnull AVmPayload_getApkContentsPath(void); |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 124 | |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Gets the path to the encrypted persistent storage for the VM, if any. This is |
| 127 | * a directory under which any files or directories created will be stored on |
| 128 | * behalf of the VM by the host app. All data is encrypted using a key known |
| 129 | * only to the VM, so the host cannot decrypt it, but may delete it. |
| 130 | * |
Alan Stokes | 159d3d6 | 2023-09-07 16:24:20 +0100 | [diff] [blame] | 131 | * \return the path to the encrypted storage directory, or NULL if no encrypted |
| 132 | * storage was requested in the VM configuration. If non-null the returned |
| 133 | * string should not be deleted or freed by the application and remains valid |
| 134 | * for the lifetime of the VM. |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 135 | */ |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 136 | const char* _Nullable AVmPayload_getEncryptedStoragePath(void); |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 137 | |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Requests the remote attestation of the client VM. |
| 140 | * |
| 141 | * The challenge will be included in the certificate chain in the attestation result, |
| 142 | * serving as proof of the freshness of the result. |
| 143 | * |
| 144 | * \param challenge A pointer to the challenge buffer. |
| 145 | * \param challenge_size size of the challenge. The maximum supported challenge size is |
| 146 | * 64 bytes. The status ATTESTATION_ERROR_INVALID_CHALLENGE will be returned if |
| 147 | * an invalid challenge is passed. |
| 148 | * \param result The remote attestation result will be filled here if the attestation |
| 149 | * succeeds. The result remains valid until it is freed with |
| 150 | * `AVmPayload_freeAttestationResult`. |
| 151 | * |
| 152 | * \return ATTESTATION_OK upon successful attestation. |
| 153 | */ |
| 154 | attestation_status_t AVmPayload_requestAttestation( |
| 155 | const void* _Nonnull challenge, size_t challenge_size, |
| 156 | struct AVmAttestationResult* _Nullable* _Nonnull result) __INTRODUCED_IN(__ANDROID_API_V__); |
| 157 | |
| 158 | /** |
| 159 | * Converts the return value from `AVmPayload_requestAttestation` to a text string |
| 160 | * representing the status code. |
| 161 | * |
| 162 | * \return a constant string value representing the status code. The string should not |
| 163 | * be deleted or freed by the application and remains valid for the lifetime of the VM. |
| 164 | */ |
| 165 | const char* _Nonnull AVmAttestationResult_resultToString(attestation_status_t status) |
| 166 | __INTRODUCED_IN(__ANDROID_API_V__); |
| 167 | |
| 168 | /** |
| 169 | * Frees all the data owned by the provided attestation result, including the result itself. |
| 170 | * |
| 171 | * Callers should ensure to invoke this API only once on a valid attestation result |
| 172 | * returned by `AVmPayload_requestAttestation` to avoid undefined behavior. |
| 173 | * |
| 174 | * \param result A pointer to the attestation result. |
| 175 | */ |
| 176 | void AVmAttestationResult_free(struct AVmAttestationResult* _Nullable result) |
| 177 | __INTRODUCED_IN(__ANDROID_API_V__); |
| 178 | |
| 179 | /** |
| 180 | * Reads the DER-encoded ECPrivateKey structure specified in [RFC 5915 s3] for the |
| 181 | * EC P-256 private key from the provided attestation result. |
| 182 | * |
| 183 | * \param result A pointer to the attestation result filled in |
| 184 | * `AVmPayload_requestAttestation` when the attestation succeeds. |
| 185 | * \param data A pointer to the memory where the private key will be written |
| 186 | * (can be null if size is 0). |
| 187 | * \param size The maximum number of bytes that can be written to the data buffer. |
| 188 | * If `size` is smaller than the total size of the private key, the key data will be |
| 189 | * truncated to this `size`. |
| 190 | * |
| 191 | * \return The total size of the private key. |
| 192 | * |
| 193 | * [RFC 5915 s3]: https://datatracker.ietf.org/doc/html/rfc5915#section-3 |
| 194 | */ |
| 195 | size_t AVmAttestationResult_getPrivateKey(const struct AVmAttestationResult* _Nonnull result, |
| 196 | void* _Nullable data, size_t size) |
| 197 | __INTRODUCED_IN(__ANDROID_API_V__); |
| 198 | |
| 199 | /** |
| 200 | * Signs the given message using ECDSA P-256, the message is first hashed with SHA-256 and |
| 201 | * then it is signed with the attested EC P-256 private key in the attestation result. |
| 202 | * |
| 203 | * \param result A pointer to the attestation result filled in |
| 204 | * `AVmPayload_requestAttestation` when the attestation succeeds. |
| 205 | * \param message A pointer to the message buffer. |
| 206 | * \param message_size size of the message. |
| 207 | * \param data A pointer to the memory where the signature will be written |
| 208 | * (can be null if size is 0). The signature is a DER-encoded ECDSASignature structure |
| 209 | * detailed in the [RFC 6979]. |
| 210 | * \param size The maximum number of bytes that can be written to the data buffer. |
| 211 | * If `size` is smaller than the total size of the signature, the signature will be |
| 212 | * truncated to this `size`. |
| 213 | * |
| 214 | * \return The total size of the signature. |
| 215 | * |
| 216 | * [RFC 6979]: https://datatracker.ietf.org/doc/html/rfc6979 |
| 217 | */ |
| 218 | size_t AVmAttestationResult_sign(const struct AVmAttestationResult* _Nonnull result, |
| 219 | const void* _Nonnull message, size_t message_size, |
| 220 | void* _Nullable data, size_t size) |
| 221 | __INTRODUCED_IN(__ANDROID_API_V__); |
| 222 | |
| 223 | /** |
| 224 | * Gets the number of certificates in the certificate chain. |
| 225 | * |
| 226 | * The certificate chain consists of a sequence of DER-encoded X.509 certificates that form |
Alice Wang | 4c6c558 | 2023-11-23 15:07:18 +0000 | [diff] [blame^] | 227 | * the attestation key's certificate chain. It starts with a leaf certificate covering the attested |
| 228 | * public key and ends with a root certificate. |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 229 | * |
| 230 | * \param result A pointer to the attestation result obtained from `AVmPayload_requestAttestation` |
| 231 | * when the attestation succeeds. |
| 232 | * |
| 233 | * \return The number of certificates in the certificate chain. |
| 234 | */ |
| 235 | size_t AVmAttestationResult_getCertificateCount(const struct AVmAttestationResult* _Nonnull result) |
| 236 | __INTRODUCED_IN(__ANDROID_API_V__); |
| 237 | |
| 238 | /** |
| 239 | * Retrieves the certificate at the given `index` from the certificate chain in the provided |
| 240 | * attestation result. |
| 241 | * |
| 242 | * The certificate chain consists of a sequence of DER-encoded X.509 certificates that form |
Alice Wang | 4c6c558 | 2023-11-23 15:07:18 +0000 | [diff] [blame^] | 243 | * the attestation key's certificate chain. It starts with a leaf certificate covering the attested |
| 244 | * public key and ends with a root certificate. |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 245 | * |
| 246 | * \param result A pointer to the attestation result obtained from `AVmPayload_requestAttestation` |
| 247 | * when the attestation succeeds. |
| 248 | * \param index Index of the certificate to retrieve. The `index` must be within the range of |
| 249 | * [0, number of certificates). The number of certificates can be obtained with |
| 250 | * `AVmAttestationResult_getCertificateCount`. |
| 251 | * \param data A pointer to the memory where the certificate will be written |
| 252 | * (can be null if size is 0). |
| 253 | * \param size The maximum number of bytes that can be written to the data buffer. If `size` |
| 254 | * is smaller than the total size of the certificate, the certificate will be |
| 255 | * truncated to this `size`. |
| 256 | * |
| 257 | * \return The total size of the certificate at the given `index`. |
| 258 | */ |
| 259 | size_t AVmAttestationResult_getCertificateAt(const struct AVmAttestationResult* _Nonnull result, |
| 260 | size_t index, void* _Nullable data, size_t size) |
| 261 | __INTRODUCED_IN(__ANDROID_API_V__); |
| 262 | |
Alan Stokes | d4ea5a8 | 2022-11-10 12:17:42 +0000 | [diff] [blame] | 263 | __END_DECLS |