blob: 7cad0687728a1f5df178b77589c1dd902edb0f64 [file] [log] [blame]
David Zeuthen630de2a2020-05-11 14:04:54 -04001/*
2 * Copyright 2020, 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#if !defined(EIC_INSIDE_LIBEIC_H) && !defined(EIC_COMPILATION)
18#error "Never include this file directly, include libeic.h instead."
19#endif
20
21#ifndef ANDROID_HARDWARE_IDENTITY_EIC_PRESENTATION_H
22#define ANDROID_HARDWARE_IDENTITY_EIC_PRESENTATION_H
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#include "EicCbor.h"
29
30// The maximum size we support for public keys in reader certificates.
31#define EIC_PRESENTATION_MAX_READER_PUBLIC_KEY_SIZE 65
32
33typedef struct {
David Zeuthen49f2d252020-10-16 11:27:24 -040034 int featureLevel;
35
David Zeuthen630de2a2020-05-11 14:04:54 -040036 uint8_t storageKey[EIC_AES_128_KEY_SIZE];
37 uint8_t credentialPrivateKey[EIC_P256_PRIV_KEY_SIZE];
38
39 uint8_t ephemeralPrivateKey[EIC_P256_PRIV_KEY_SIZE];
40
41 // The challenge generated with eicPresentationCreateAuthChallenge()
42 uint64_t authChallenge;
43
44 // Set by eicPresentationSetAuthToken() and contains the fields
45 // from the passed in authToken and verificationToken.
46 //
47 uint64_t authTokenChallenge;
48 uint64_t authTokenSecureUserId;
49 uint64_t authTokenTimestamp;
50 uint64_t verificationTokenTimestamp;
51
52 // The public key for the reader.
53 //
54 // (During the process of pushing reader certificates, this is also used to store
55 // the public key of the previously pushed certificate.)
56 //
57 uint8_t readerPublicKey[EIC_PRESENTATION_MAX_READER_PUBLIC_KEY_SIZE];
58 size_t readerPublicKeySize;
59
60 // This is set to true only if eicPresentationValidateRequestMessage() successfully
61 // validated the requestMessage.
62 //
63 // Why even record this? Because there's no requirement the HAL actually calls that
64 // function and we validate ACPs before it's called... so it's possible that a
65 // compromised HAL could trick us into marking ACPs as authorized while they in fact
66 // aren't.
67 bool requestMessageValidated;
68 bool buildCbor;
69
70 // Set to true initialized as a test credential.
71 bool testCredential;
72
73 // These are bitmasks indicating which of the possible 32 access control profiles are
74 // authorized. They are built up by eicPresentationValidateAccessControlProfile().
75 //
76 uint32_t accessControlProfileMaskValidated; // True if the profile was validated.
77 uint32_t accessControlProfileMaskUsesReaderAuth; // True if the ACP is using reader auth
78 uint32_t accessControlProfileMaskFailedReaderAuth; // True if failed reader auth
79 uint32_t accessControlProfileMaskFailedUserAuth; // True if failed user auth
80
81 // SHA-256 for AdditionalData, updated for each entry.
82 uint8_t additionalDataSha256[EIC_SHA256_DIGEST_SIZE];
83
David Zeuthen49f2d252020-10-16 11:27:24 -040084 // SHA-256 of ProofOfProvisioning. Set to NUL-bytes or initialized from CredentialKeys data
85 // if credential was created with feature version 202101 or later.
86 uint8_t proofOfProvisioningSha256[EIC_SHA256_DIGEST_SIZE];
87
David Zeuthen630de2a2020-05-11 14:04:54 -040088 size_t expectedCborSizeAtEnd;
89 EicCbor cbor;
90} EicPresentation;
91
92bool eicPresentationInit(EicPresentation* ctx, bool testCredential, const char* docType,
David Zeuthen49f2d252020-10-16 11:27:24 -040093 const uint8_t* encryptedCredentialKeys,
94 size_t encryptedCredentialKeysSize);
David Zeuthen630de2a2020-05-11 14:04:54 -040095
96bool eicPresentationGenerateSigningKeyPair(EicPresentation* ctx, const char* docType, time_t now,
97 uint8_t* publicKeyCert, size_t* publicKeyCertSize,
98 uint8_t signingKeyBlob[60]);
99
100// Create an ephemeral key-pair.
101//
102// The private key is stored in |ctx->ephemeralPrivateKey| and also returned in
103// |ephemeralPrivateKey|.
104//
105bool eicPresentationCreateEphemeralKeyPair(EicPresentation* ctx,
106 uint8_t ephemeralPrivateKey[EIC_P256_PRIV_KEY_SIZE]);
107
108// Returns a non-zero challenge in |authChallenge|.
109bool eicPresentationCreateAuthChallenge(EicPresentation* ctx, uint64_t* authChallenge);
110
111// Starts retrieveing entries.
112//
113bool eicPresentationStartRetrieveEntries(EicPresentation* ctx);
114
115// Sets the auth-token.
116bool eicPresentationSetAuthToken(EicPresentation* ctx, uint64_t challenge, uint64_t secureUserId,
117 uint64_t authenticatorId, int hardwareAuthenticatorType,
118 uint64_t timeStamp, const uint8_t* mac, size_t macSize,
119 uint64_t verificationTokenChallenge,
120 uint64_t verificationTokenTimeStamp,
121 int verificationTokenSecurityLevel,
122 const uint8_t* verificationTokenMac,
123 size_t verificationTokenMacSize);
124
125// Function to push certificates in the reader certificate chain.
126//
127// This should start with the root certificate (e.g. the last in the chain) and
128// continue up the chain, ending with the certificate for the reader.
129//
130// Calls to this function should be interleaved with calls to the
131// eicPresentationValidateAccessControlProfile() function, see below.
132//
133bool eicPresentationPushReaderCert(EicPresentation* ctx, const uint8_t* certX509,
134 size_t certX509Size);
135
136// Checks an access control profile.
137//
138// Returns false if an error occurred while checking the profile (e.g. MAC doesn't check out).
139//
140// Returns in |accessGranted| whether access is granted.
141//
142// If |readerCertificate| is non-empty and the public key of one of those
143// certificates appear in the chain presented by the reader, this function must
144// be called after pushing that certificate using
145// eicPresentationPushReaderCert().
146//
147bool eicPresentationValidateAccessControlProfile(EicPresentation* ctx, int id,
148 const uint8_t* readerCertificate,
149 size_t readerCertificateSize,
150 bool userAuthenticationRequired, int timeoutMillis,
151 uint64_t secureUserId, const uint8_t mac[28],
152 bool* accessGranted);
153
154// Validates that the given requestMessage is signed by the public key in the
155// certificate last set with eicPresentationPushReaderCert().
156//
157// The format of the signature is the same encoding as the 'signature' field of
158// COSE_Sign1 - that is, it's the R and S integers both with the same length as
159// the key-size.
160//
161// Must be called after eicPresentationPushReaderCert() have been used to push
162// the final certificate. Which is the certificate of the reader itself.
163//
164bool eicPresentationValidateRequestMessage(EicPresentation* ctx, const uint8_t* sessionTranscript,
165 size_t sessionTranscriptSize,
166 const uint8_t* requestMessage, size_t requestMessageSize,
167 int coseSignAlg,
168 const uint8_t* readerSignatureOfToBeSigned,
169 size_t readerSignatureOfToBeSignedSize);
170
171typedef enum {
172 // Returned if access is granted.
173 EIC_ACCESS_CHECK_RESULT_OK,
174
175 // Returned if an error occurred checking for access.
176 EIC_ACCESS_CHECK_RESULT_FAILED,
177
178 // Returned if access was denied because item is configured without any
179 // access control profiles.
180 EIC_ACCESS_CHECK_RESULT_NO_ACCESS_CONTROL_PROFILES,
181
182 // Returned if access was denied because of user authentication.
183 EIC_ACCESS_CHECK_RESULT_USER_AUTHENTICATION_FAILED,
184
185 // Returned if access was denied because of reader authentication.
186 EIC_ACCESS_CHECK_RESULT_READER_AUTHENTICATION_FAILED,
187} EicAccessCheckResult;
188
189// Passes enough information to calculate the MACing key
190//
191bool eicPresentationCalcMacKey(EicPresentation* ctx, const uint8_t* sessionTranscript,
192 size_t sessionTranscriptSize,
193 const uint8_t readerEphemeralPublicKey[EIC_P256_PUB_KEY_SIZE],
194 const uint8_t signingKeyBlob[60], const char* docType,
195 unsigned int numNamespacesWithValues,
196 size_t expectedDeviceNamespacesSize);
197
198// The scratchSpace should be set to a buffer at least 512 bytes (ideally 1024
199// bytes, the bigger the better). It's done this way to avoid allocating stack
200// space.
201//
202EicAccessCheckResult eicPresentationStartRetrieveEntryValue(
203 EicPresentation* ctx, const char* nameSpace, const char* name,
204 unsigned int newNamespaceNumEntries, int32_t entrySize, const int* accessControlProfileIds,
205 size_t numAccessControlProfileIds, uint8_t* scratchSpace, size_t scratchSpaceSize);
206
207// Note: |content| must be big enough to hold |encryptedContentSize| - 28 bytes.
208//
209// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
210// avoid allocating stack space.
211//
212bool eicPresentationRetrieveEntryValue(EicPresentation* ctx, const uint8_t* encryptedContent,
213 size_t encryptedContentSize, uint8_t* content,
214 const char* nameSpace, const char* name,
215 const int* accessControlProfileIds,
216 size_t numAccessControlProfileIds, uint8_t* scratchSpace,
217 size_t scratchSpaceSize);
218
219// Returns the HMAC-SHA256 of |ToBeMaced| as per RFC 8051 "6.3. How to Compute
220// and Verify a MAC".
221bool eicPresentationFinishRetrieval(EicPresentation* ctx, uint8_t* digestToBeMaced,
222 size_t* digestToBeMacedSize);
223
224// The data returned in |signatureOfToBeSigned| contains the ECDSA signature of
225// the ToBeSigned CBOR from RFC 8051 "4.4. Signing and Verification Process"
226// where content is set to the ProofOfDeletion CBOR.
227//
228bool eicPresentationDeleteCredential(EicPresentation* ctx, const char* docType,
David Zeuthen49f2d252020-10-16 11:27:24 -0400229 const uint8_t* challenge, size_t challengeSize,
230 bool includeChallenge, size_t proofOfDeletionCborSize,
David Zeuthen630de2a2020-05-11 14:04:54 -0400231 uint8_t signatureOfToBeSigned[EIC_ECDSA_P256_SIGNATURE_SIZE]);
232
David Zeuthen49f2d252020-10-16 11:27:24 -0400233// The data returned in |signatureOfToBeSigned| contains the ECDSA signature of
234// the ToBeSigned CBOR from RFC 8051 "4.4. Signing and Verification Process"
235// where content is set to the ProofOfOwnership CBOR.
236//
237bool eicPresentationProveOwnership(EicPresentation* ctx, const char* docType, bool testCredential,
238 const uint8_t* challenge, size_t challengeSize,
239 size_t proofOfOwnershipCborSize,
240 uint8_t signatureOfToBeSigned[EIC_ECDSA_P256_SIGNATURE_SIZE]);
241
David Zeuthen630de2a2020-05-11 14:04:54 -0400242#ifdef __cplusplus
243}
244#endif
245
246#endif // ANDROID_HARDWARE_IDENTITY_EIC_PRESENTATION_H