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