README.md
1# Remote Provisioning HAL
2
3## Objective
4
5Design a HAL to support over-the-air provisioning of certificates for asymmetric
6keys. The HAL must interact effectively with Keystore (and other daemons) and
7protect device privacy and security.
8
9Note that this API was originally designed for KeyMint, with the intention that
10it should be usable for other HALs that require certificate provisioning.
11Throughout this document we'll refer to the Keystore and KeyMint (formerly
12called Keymaster) components, but only for concreteness and convenience; those
13labels could be replaced with the names of any system and secure area
14components, respectively, that need certificates provisioned.
15
16## Key design decisions
17
18### General approach
19
20To more securely and reliably get keys and certificates to Android devices, we
21need to create a system where no party outside of the device's secure components
22is responsible for managing private keys. The strategy we've chosen is to
23deliver certificates over the air, using an asymmetric key pair created
24on-device in the factory as a root of trust to create an authenticated, secure
25channel. In this document we refer to this device-unique asymmetric key pair as
26Device Key (DK), its public half DK\_pub, its private half DK\_priv and a Device
27Key Certificate containing DK\_pub is denoted DKC.
28
29In order for the provisioning service to use DK (or a key authenticated by DK),
30it must know whether a given DK\_pub is known and trusted. To prove trust, we
31ask device OEMs to use one of two mechanisms:
32
331. (Preferred, recommended) The device OEM extracts DK\_pub from each device it
34 manufactures and uploads the public keys to a backend server.
35
361. The device OEM signs the DK\_pub to produce DKC and stores it on the device.
37 This has the advantage that they don't need to upload a DK\_pub for every
38 device immediately, but the disadvantage that they have to manage their
39 private signing keys, which means they have to have HSMs, configure and
40 secure them correctly, etc. Some backend providers may also require that the
41 OEM passes a factory security audit, and additionally promises to upload the
42 keys eventually as well.
43
44Note that in the full elaboration of this plan, DK\_pub is not the key used to
45establish a secure channel. Instead, DK\_pub is just the first public key in a
46chain of public keys which ends with the KeyMint public key, KM\_pub. All keys
47in the chain are device-unique and are joined in a certificate chain called the
48_Boot Certificate Chain_ (BCC), because in phases 2 and 3 of the remote
49provisioning project it is a chain of certificates corresponding to boot phases.
50We speak of the BCC even for phase 1, though in phase 1 it contains only a
51single self-signed DKC. This is described in more depth in the Phases section
52below.
53
54The BCC is authenticated by DK\_pub. To authenticate DK\_pub, we may have
55additional DKCs, from the SoC vendor, the device OEM, or both. Those are not
56part of the BCC but included as optional fields in the certificate request
57structure.
58
59The format of the the DK and BCC is specified within [Open Profile for DICE]
60(https://pigweed.googlesource.com/open-dice/+/HEAD/docs/specification.md). To
61map phrases within this document to their equivalent terminology in the DICE
62specification, read the terms as follows: the DK corresponds to the UDS-derived
63key pair, DKC corresponds to the UDS certificate, and the BCC entries between
64DK\_pub and KM\_pub correspond to a chain of CDI certificates.
65
66Note: In addition to allowing 32 byte hash values for fields in the BCC payload,
67this spec additionally constrains some of the choices allowed in open-DICE.
68Specifically, these include which entries are required and which are optional in
69the BCC payload, and which algorithms are acceptable for use.
70
71### Phases
72
73RKP will be deployed in three phases, in terms of managing the root of trust
74binding between the device and the backend. To briefly describe them:
75
76* Phase 1: In phase 1 there is only one entry in the BCC; DK_pub and KM_pub are
77 the same key and the certificate is self-signed.
78* Phase 2: This is identical to phase 1, except it leverages the hardware root
79 of trust process described by DICE. Instead of trust being rooted in the TEE,
80 it is now rooted in the ROM by key material blown into fuses which are only
81 accessible to the ROM code.
82* Phase 3: This is identical to Phase 2, except the SoC vendor also does the
83 public key extraction or certification in their facilities, along with the OEM
84 doing it in the factory. This tightens up the "supply chain" and aims to make
85 key upload management more secure.
86
87### Privacy considerations
88
89Because DK and the DKCs are unique, immutable, unspoofable hardware-bound
90identifiers for the device, we must limit access to them to the absolute minimum
91possible. We do this in two ways:
92
931. We require KeyMint (which knows the BCC and either knows or at least has the
94ability to use KM\_priv) to refuse to ever divulge the BCC or additional
95signatures in plaintext. Instead, KeyMint requires the caller to provide an
96_Endpoint Encryption Key_ (EEK), with which it will encrypt the data before
97returning it. When provisioning production keys, the EEK must be signed by an
98approved authority whose public key is embedded in KeyMint. When certifying test
99keys, KeyMint will accept any EEK without checking the signature, but will
100encrypt and return a test BCC, rather than the real one. The result is that
101only an entity in possession of an Trusted EEK (TEEK) private key can discover
102the plaintext of the production BCC.
1031. Having thus limited access to the public keys to the trusted party only, we
104need to prevent the entity from abusing this unique device identifier. The
105approach and mechanisms for doing that are beyond the scope of this document
106(they must be addressed in the server design), but generally involve taking care
107to ensure that we do not create any links between user IDs, IP addresses or
108issued certificates and the device pubkey.
109
110Although the details of the mechanisms for preventing the entity from abusing
111the BCC are, as stated, beyond the scope of this document, there is a subtle
112design decision here made specifically to enable abuse prevention. Specifically
113the `CertificateRequest` message sent to the server is (in
114[CDDL](https://tools.ietf.org/html/rfc8610)):
115
116```
117cddl
118CertificateRequest = [
119 DeviceInfo,
120 challenge : bstr,
121 ProtectedData,
122 MacedKeysToSign
123]
124```
125
126The public keys to be attested by the server are in `MacedKeysToSign`, which is
127a COSE\_Mac0 structure, MACed with a key that is found in `ProtectedData`. The
128MAC key is signed by DK\_pub.
129
130This structure allows the backend component that has access to EEK\_priv to
131decrypt `ProtectedData`, validate that the request is from an authorized device,
132check that the request is fresh and verify and extract the MAC key. That backend
133component never sees any data related to the keys to be signed, but can provide
134the MAC key to another backend component that can verify `MacedKeysToSign` and
135proceed to generate the certificates.
136
137In this way, we can partition the provisioning server into one component that
138knows the device identity, as represented by DK\_pub, but never sees the keys to
139be certified or certificates generated, and another component that sees the keys
140to be certified and certificates generated but does not know the device
141identity.
142
143### Key and cryptographic message formatting
144
145For simplicity of generation and parsing, compactness of wire representation,
146and flexibility and standardization, we've settled on using the CBOR Object
147Signing and Encryption (COSE) standard, defined in [RFC
1488152](https://tools.ietf.org/html/rfc8152). COSE provides compact and reasonably
149simple, yet easily-extensible, wire formats for:
150
151* Keys,
152* MACed messages,
153* Signed messages, and
154* Encrypted messages
155
156COSE enables easy layering of these message formats, such as using a COSE\_Sign
157structure to contain a COSE\_Key with a public key in it. We call this a
158"certificate".
159
160Due to the complexity of the standard, we'll spell out the COSE structures
161completely in this document and in the HAL and other documentation, so that
162although implementors will need to understand CBOR and the CBOR Data Definition
163Language ([CDDL, defined in RFC 8610](https://tools.ietf.org/html/rfc8610)),
164they shouldn't need to understand COSE.
165
166Note, however, that the certificate chains returned from the provisioning server
167are standard X.509 certificates.
168
169### Algorithm choices
170
171This document uses:
172
173* ECDSA P-256 for attestation signing keys;
174* Remote provisioning protocol signing keys:
175 * Ed25519 / P-256 / P-384
176* ECDH keys:
177 * X25519 / P-256
178* AES-GCM for all encryption;
179* SHA-256 / SHA-384 / SHA-512 for message digesting;
180* HMAC with a supported message digest for all MACing; and
181* HKDF with a supported message digest for all key derivation.
182
183We believe that Curve25519 offers the best tradeoff in terms of security,
184efficiency and global trustworthiness, and that it is now sufficiently
185widely-used and widely-implemented to make it a practical choice.
186
187However, since hardware such as Secure Elements (SE) do not currently offer
188support for curve 25519, we are allowing implementations to instead make use of
189ECDSA and ECDH.
190
191The CDDL in the rest of the document will use the '/' operator to show areas
192where either curve 25519, P-256 or P-384 may be used. Since there is no easy way
193to bind choices across different CDDL groups, it is important that the
194implementor stays consistent in which type is chosen. E.g. taking ES256 as the
195choice for algorithm implies the implementor should also choose the P256 public
196key group further down in the COSE structure.
197
198### Testability
199
200It's critical that the remote provisioning implementation be testable, to
201minimize the probability that broken devices are sold to end users. To support
202testing, the remote provisioning HAL methods take a `testMode` argument. Keys
203created in test mode are tagged to indicate this. The provisioning server will
204check for the test mode tag and issue test certificates that do not chain back
205to a trusted public key. In test mode, any EEK will be accepted, enabling
206testing tools to use EEKs for which they have the private key so they can
207validate the content of certificate requests. The BCC included in the
208`CertificateRequest` must contain freshly-generated keys, not the real BCC keys.
209
210Keystore (or similar) will need to be able to handle both testMode keys and
211production keys and keep them distinct, generating test certificate requests
212when asked with a test EEK and production certificate requests when asked with a
213production EEK. Likewise, the interface used to instruct Keystore to create keys
214will need to be able to specify whether test or production keys are desired.
215
216## Design
217
218### Certificate provisioning flow
219
220TODO(jbires): Replace this with a `.png` containing a sequence diagram. The
221provisioning flow looks something like this:
222
223Provisioner -> Keystore: Prepare N keys
224Keystore -> KeyMint: generateKeyPair
225KeyMint -> KeyMint: Generate key pair
226KeyMint --> Keystore: key\_blob,pubkey
227Keystore -> Keystore: Store key\_blob,pubkey
228Provisioner -> Server: Get TEEK
229Server --> Provisioner: TEEK
230Provisioner -> Keystore: genCertReq(N, TEEK)
231Keystore -> KeyMint: genCertReq(pubkeys, TEEK)
232KeyMint -> KeyMint: Sign pubkeys & encrypt BCC
233KeyMint --> Keystore: signature, encrypted BCC
234Keystore -> Keystore: Construct cert\_request
235Keystore --> Provisioner: cert\_request
236Provisioner --> Server: cert\_request
237Server -> Server: Validate cert\_request
238Server -> Server: Generate certificates
239Server --> Provisioner: certificates
240Provisioner -> Keystore: certificates
241Keystore -> Keystore: Store certificates
242
243The actors in the above diagram are:
244
245* **Server** is the backend certificate provisioning server. It has access to
246 the uploaded device public keys and is responsible for providing encryption
247 keys, decrypting and validating requests, and generating certificates in
248 response to requests.
249* **Provisioner** is an application that is responsible for communicating with
250 the server and all of the system components that require key certificates
251 from the server. It also implements the policy that defines how many key
252 pairs each client should keep in their pool.
253* **Keystore** is the [Android keystore
254 daemon](https://developer.android.com/training/articles/keystore) (or, more
255 generally, whatever system component manages communications with a
256 particular secure aread component).
257* **KeyMint** is the secure area component that manages cryptographic keys and
258 performs attestations (or perhaps some other secure area component).
259
260### `BCC`
261
262The _Boot Certificate Chain_ (BCC) is the chain of certificates that contains
263DK\_pub as well as other often device-unique certificates. The BCC is
264represented as a COSE\_Key containing DK\_pub followed by an array of
265COSE\_Sign1 "certificates" containing public keys and optional additional
266information, ordered from root to leaf, with each certificate signing the next.
267The first certificate in the array is signed by DK\_pub, the last certificate
268has the KeyMint (or whatever) signing key's public key, KM\_pub. In phase 1
269there is only one entry; DK\_pub and KM\_pub are the same key and the
270certificate is self-signed.
271
272Each COSE\_Sign1 certificate is a CBOR Web Token (CWT) as described in [RFC
2738392](https://tools.ietf.org/html/rfc8392) with additional fields as described
274in the Open Profile for DICE. Of these additional fields, only the
275_subjectPublicKey_ and _keyUsage_ fields are expected to be present for the
276KM\_pub entry (that is, the last entry) in a BCC, but all fields required by the
277Open Profile for DICE are expected for other entries (each of which corresponds
278to a particular firmware component or boot stage). The CWT fields _iss_ and
279_sub_ identify the issuer and subject of the certificate and are consistent
280along the BCC entries; the issuer of a given entry matches the subject of the
281previous entry.
282
283The BCC is designed to be constructed using the Open Profile for DICE. In this
284case the DK key pair is derived from the UDS as described by that profile and
285all BCC entries before the leaf are CBOR CDI certificates chained from DK\_pub.
286The KM key pair is not part of the derived DICE chain. It is generated (not
287derived) by the KeyMint module, certified by the last key in the DICE chain, and
288added as the leaf BCC entry. The key usage field in this leaf certificate must
289indicate the key is not used to sign certificates. If a UDS certificate is
290available on the device it should appear in the certificate request as the leaf
291of a DKCertChain in AdditionalDKSignatures (see
292[CertificateRequest](#certificaterequest)).
293
294#### Mode
295
296The Open Profile for DICE specifies four possible modes with the most important
297mode being `normal`. A certificate must only set the mode to `normal` when all
298of the following conditions are met when loading and verifying the software
299component that is being described by the certificate:
300
301* verified boot with anti-rollback protection is enabled
302* only the verified boot authorities for production images are enabled
303* debug ports, fuses or other debug facilities are disabled
304* device booted software from the normal primary source e.g. internal flash
305
306If any of these conditions are not met then it is recommended to explicitly
307acknowledge this fact by using the `debug` mode. The mode should never be `not
308configured`.
309
310#### Configuration descriptor
311
312The Open Profile for DICE allows for an arbitrary configuration descriptor. For
313BCC entries, this configuration descriptor is a CBOR map with the following
314optional fields. If no fields are relevant, an empty map should be encoded.
315Additional implementation-specific fields may be added using key values not in
316the range \[-70000, -70999\] (these are reserved for future additions here).
317
318```
319| Name | Key | Value type | Meaning |
320| ----------------- | ------ | ---------- | ----------------------------------|
321| Component name | -70002 | tstr | Name of firmware component / boot |
322: : : : stage :
323| Component version | -70003 | int / tstr | Version of firmware component / |
324: : : : boot stage :
325| Resettable | -70004 | null | If present, key changes on factory|
326: : : : reset :
327| Security version | -70005 | uint | Machine-comparable, monotonically |
328: : : : increasing version of the firmware:
329: : : : component / boot stage where a :
330: : : : greater value indicates a newer :
331: : : : version :
332```
333
334Please see
335[ProtectedData.aidl](https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/ProtectedData.aidl)
336for a full CDDL definition of the BCC.
337
338### `CertificateRequest`
339
340The full CBOR message that will be sent to the server to request certificates
341is:
342
343```cddl
344CertificateRequest = [
345 DeviceInfo,
346 challenge : bstr, // Provided by the server
347 ProtectedData, // See ProtectedData.aidl
348 MacedKeysToSign // See IRemotelyProvisionedComponent.aidl
349]
350
351DeviceInfo = [
352 VerifiedDeviceInfo, // See DeviceInfo.aidl
353 UnverifiedDeviceInfo
354]
355
356// Unverified info is anything provided by the HLOS. Subject to change out of
357// step with the HAL.
358UnverifiedDeviceInfo = {
359 ? "fingerprint" : tstr,
360}
361
362```
363
364It will be the responsibility of Keystore and the Provisioner to construct the
365`CertificateRequest`. The HAL provides a method to generate the elements that
366need to be constructed on the secure side, which are the tag field of
367`MacedKeysToSign`, `VerifiedDeviceInfo`, and the ciphertext field of
368`ProtectedData`.
369
370### HAL
371
372The remote provisioning HAL provides a simple interface that can be implemented
373by multiple secure components that require remote provisioning. It would be
374slightly simpler to extend the KeyMint API, but that approach would only serve
375the needs of KeyMint, this is more general.
376
377NOTE the data structures defined in this HAL may look a little bloated and
378complex. This is because the COSE data structures are fully spelled-out; we
379could make it much more compact by not re-specifying the standardized elements
380and instead just referencing the standard, but it seems better to fully specify
381them. If the apparent complexity seems daunting, consider what the same would
382look like if traditional ASN.1 DER-based structures from X.509 and related
383standards were used and also fully elaborated.
384
385Please see the related HAL documentation directly in the source code at the
386following links:
387
388* [IRemotelyProvisionedComponent
389 HAL](https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/IRemotelyProvisionedComponent.aidl)
390* [ProtectedData](https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/ProtectedData.aidl)
391* [MacedPublicKey](https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/MacedPublicKey.aidl)
392* [RpcHardwareInfo](https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/RpcHardwareInfo.aidl)
393* [DeviceInfo](https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/DeviceInfo.aidl)
394
395