1 /*
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 #include "EicCbor.h"
18
eicCborInit(EicCbor * cbor,uint8_t * buffer,size_t bufferSize)19 void eicCborInit(EicCbor* cbor, uint8_t* buffer, size_t bufferSize) {
20 eicMemSet(cbor, '\0', sizeof(EicCbor));
21 cbor->size = 0;
22 cbor->bufferSize = bufferSize;
23 cbor->buffer = buffer;
24 cbor->digestType = EIC_CBOR_DIGEST_TYPE_SHA256;
25 eicOpsSha256Init(&cbor->digester.sha256);
26 }
27
eicCborInitHmacSha256(EicCbor * cbor,uint8_t * buffer,size_t bufferSize,const uint8_t * hmacKey,size_t hmacKeySize)28 void eicCborInitHmacSha256(EicCbor* cbor, uint8_t* buffer, size_t bufferSize,
29 const uint8_t* hmacKey, size_t hmacKeySize) {
30 eicMemSet(cbor, '\0', sizeof(EicCbor));
31 cbor->size = 0;
32 cbor->bufferSize = bufferSize;
33 cbor->buffer = buffer;
34 cbor->digestType = EIC_CBOR_DIGEST_TYPE_HMAC_SHA256;
35 eicOpsHmacSha256Init(&cbor->digester.hmacSha256, hmacKey, hmacKeySize);
36 }
37
eicCborEnableSecondaryDigesterSha256(EicCbor * cbor,EicSha256Ctx * sha256)38 void eicCborEnableSecondaryDigesterSha256(EicCbor* cbor, EicSha256Ctx* sha256) {
39 cbor->secondaryDigesterSha256 = sha256;
40 }
41
eicCborFinal(EicCbor * cbor,uint8_t digest[EIC_SHA256_DIGEST_SIZE])42 void eicCborFinal(EicCbor* cbor, uint8_t digest[EIC_SHA256_DIGEST_SIZE]) {
43 switch (cbor->digestType) {
44 case EIC_CBOR_DIGEST_TYPE_SHA256:
45 eicOpsSha256Final(&cbor->digester.sha256, digest);
46 break;
47 case EIC_CBOR_DIGEST_TYPE_HMAC_SHA256:
48 eicOpsHmacSha256Final(&cbor->digester.hmacSha256, digest);
49 break;
50 }
51 }
52
eicCborAppend(EicCbor * cbor,const uint8_t * data,size_t size)53 void eicCborAppend(EicCbor* cbor, const uint8_t* data, size_t size) {
54 switch (cbor->digestType) {
55 case EIC_CBOR_DIGEST_TYPE_SHA256:
56 eicOpsSha256Update(&cbor->digester.sha256, data, size);
57 break;
58 case EIC_CBOR_DIGEST_TYPE_HMAC_SHA256:
59 eicOpsHmacSha256Update(&cbor->digester.hmacSha256, data, size);
60 break;
61 }
62 if (cbor->secondaryDigesterSha256 != NULL) {
63 eicOpsSha256Update(cbor->secondaryDigesterSha256, data, size);
64 }
65
66 if (cbor->size >= cbor->bufferSize) {
67 cbor->size += size;
68 return;
69 }
70
71 size_t numBytesLeft = cbor->bufferSize - cbor->size;
72 size_t numBytesToCopy = size;
73 if (numBytesToCopy > numBytesLeft) {
74 numBytesToCopy = numBytesLeft;
75 }
76 eicMemCpy(cbor->buffer + cbor->size, data, numBytesToCopy);
77
78 cbor->size += size;
79 }
80
eicCborAdditionalLengthBytesFor(size_t size)81 size_t eicCborAdditionalLengthBytesFor(size_t size) {
82 if (size < 24) {
83 return 0;
84 } else if (size <= 0xff) {
85 return 1;
86 } else if (size <= 0xffff) {
87 return 2;
88 } else if (size <= 0xffffffff) {
89 return 4;
90 }
91 return 8;
92 }
93
eicCborBegin(EicCbor * cbor,int majorType,uint64_t size)94 void eicCborBegin(EicCbor* cbor, int majorType, uint64_t size) {
95 uint8_t data[9];
96
97 if (size < 24) {
98 data[0] = (majorType << 5) | size;
99 eicCborAppend(cbor, data, 1);
100 } else if (size <= 0xff) {
101 data[0] = (majorType << 5) | 24;
102 data[1] = size;
103 eicCborAppend(cbor, data, 2);
104 } else if (size <= 0xffff) {
105 data[0] = (majorType << 5) | 25;
106 data[1] = size >> 8;
107 data[2] = size & 0xff;
108 eicCborAppend(cbor, data, 3);
109 } else if (size <= 0xffffffff) {
110 data[0] = (majorType << 5) | 26;
111 data[1] = (size >> 24) & 0xff;
112 data[2] = (size >> 16) & 0xff;
113 data[3] = (size >> 8) & 0xff;
114 data[4] = size & 0xff;
115 eicCborAppend(cbor, data, 5);
116 } else {
117 data[0] = (majorType << 5) | 27;
118 data[1] = (((uint64_t)size) >> 56) & 0xff;
119 data[2] = (((uint64_t)size) >> 48) & 0xff;
120 data[3] = (((uint64_t)size) >> 40) & 0xff;
121 data[4] = (((uint64_t)size) >> 32) & 0xff;
122 data[5] = (((uint64_t)size) >> 24) & 0xff;
123 data[6] = (((uint64_t)size) >> 16) & 0xff;
124 data[7] = (((uint64_t)size) >> 8) & 0xff;
125 data[8] = ((uint64_t)size) & 0xff;
126 eicCborAppend(cbor, data, 9);
127 }
128 }
129
eicCborAppendByteString(EicCbor * cbor,const uint8_t * data,size_t dataSize)130 void eicCborAppendByteString(EicCbor* cbor, const uint8_t* data, size_t dataSize) {
131 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_BYTE_STRING, dataSize);
132 eicCborAppend(cbor, data, dataSize);
133 }
134
eicCborAppendString(EicCbor * cbor,const char * str,size_t strLength)135 void eicCborAppendString(EicCbor* cbor, const char* str, size_t strLength) {
136 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_STRING, strLength);
137 eicCborAppend(cbor, (const uint8_t*)str, strLength);
138 }
139
eicCborAppendStringZ(EicCbor * cbor,const char * str)140 void eicCborAppendStringZ(EicCbor* cbor, const char* str) {
141 eicCborAppendString(cbor, str, eicStrLen(str));
142 }
143
eicCborAppendSimple(EicCbor * cbor,uint8_t simpleValue)144 void eicCborAppendSimple(EicCbor* cbor, uint8_t simpleValue) {
145 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_SIMPLE, simpleValue);
146 }
147
eicCborAppendBool(EicCbor * cbor,bool value)148 void eicCborAppendBool(EicCbor* cbor, bool value) {
149 uint8_t simpleValue = value ? EIC_CBOR_SIMPLE_VALUE_TRUE : EIC_CBOR_SIMPLE_VALUE_FALSE;
150 eicCborAppendSimple(cbor, simpleValue);
151 }
152
eicCborAppendSemantic(EicCbor * cbor,uint64_t value)153 void eicCborAppendSemantic(EicCbor* cbor, uint64_t value) {
154 size_t encoded = value;
155 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_SEMANTIC, encoded);
156 }
157
eicCborAppendUnsigned(EicCbor * cbor,uint64_t value)158 void eicCborAppendUnsigned(EicCbor* cbor, uint64_t value) {
159 uint64_t encoded = value;
160 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_UNSIGNED, encoded);
161 }
162
eicCborAppendNumber(EicCbor * cbor,int64_t value)163 void eicCborAppendNumber(EicCbor* cbor, int64_t value) {
164 if (value < 0) {
165 uint64_t encoded = -1 - value;
166 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_NEGATIVE, encoded);
167 } else {
168 eicCborAppendUnsigned(cbor, value);
169 }
170 }
171
eicCborAppendArray(EicCbor * cbor,size_t numElements)172 void eicCborAppendArray(EicCbor* cbor, size_t numElements) {
173 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_ARRAY, numElements);
174 }
175
eicCborAppendMap(EicCbor * cbor,size_t numPairs)176 void eicCborAppendMap(EicCbor* cbor, size_t numPairs) {
177 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_MAP, numPairs);
178 }
179
eicCborCalcAccessControl(EicCbor * cborBuilder,int id,const uint8_t * readerCertificate,size_t readerCertificateSize,bool userAuthenticationRequired,uint64_t timeoutMillis,uint64_t secureUserId)180 bool eicCborCalcAccessControl(EicCbor* cborBuilder, int id, const uint8_t* readerCertificate,
181 size_t readerCertificateSize, bool userAuthenticationRequired,
182 uint64_t timeoutMillis, uint64_t secureUserId) {
183 size_t numPairs = 1;
184 if (readerCertificateSize > 0) {
185 numPairs += 1;
186 }
187 if (userAuthenticationRequired) {
188 numPairs += 2;
189 if (secureUserId > 0) {
190 numPairs += 1;
191 }
192 }
193 eicCborAppendMap(cborBuilder, numPairs);
194 eicCborAppendStringZ(cborBuilder, "id");
195 eicCborAppendUnsigned(cborBuilder, id);
196 if (readerCertificateSize > 0) {
197 eicCborAppendStringZ(cborBuilder, "readerCertificate");
198 eicCborAppendByteString(cborBuilder, readerCertificate, readerCertificateSize);
199 }
200 if (userAuthenticationRequired) {
201 eicCborAppendStringZ(cborBuilder, "userAuthenticationRequired");
202 eicCborAppendBool(cborBuilder, userAuthenticationRequired);
203 eicCborAppendStringZ(cborBuilder, "timeoutMillis");
204 eicCborAppendUnsigned(cborBuilder, timeoutMillis);
205 if (secureUserId > 0) {
206 eicCborAppendStringZ(cborBuilder, "secureUserId");
207 eicCborAppendUnsigned(cborBuilder, secureUserId);
208 }
209 }
210
211 if (cborBuilder->size > cborBuilder->bufferSize) {
212 eicDebug("Buffer for ACP CBOR is too small (%zd) - need %zd bytes", cborBuilder->bufferSize,
213 cborBuilder->size);
214 return false;
215 }
216
217 return true;
218 }
219
eicCborCalcEntryAdditionalData(const uint8_t * accessControlProfileIds,size_t numAccessControlProfileIds,const char * nameSpace,size_t nameSpaceLength,const char * name,size_t nameLength,uint8_t * cborBuffer,size_t cborBufferSize,size_t * outAdditionalDataCborSize,uint8_t additionalDataSha256[EIC_SHA256_DIGEST_SIZE])220 bool eicCborCalcEntryAdditionalData(const uint8_t* accessControlProfileIds,
221 size_t numAccessControlProfileIds, const char* nameSpace,
222 size_t nameSpaceLength, const char* name,
223 size_t nameLength, uint8_t* cborBuffer,
224 size_t cborBufferSize, size_t* outAdditionalDataCborSize,
225 uint8_t additionalDataSha256[EIC_SHA256_DIGEST_SIZE]) {
226 EicCbor cborBuilder;
227
228 eicCborInit(&cborBuilder, cborBuffer, cborBufferSize);
229 eicCborAppendMap(&cborBuilder, 3);
230 eicCborAppendStringZ(&cborBuilder, "Namespace");
231 eicCborAppendString(&cborBuilder, nameSpace, nameSpaceLength);
232 eicCborAppendStringZ(&cborBuilder, "Name");
233 eicCborAppendString(&cborBuilder, name, nameLength);
234 eicCborAppendStringZ(&cborBuilder, "AccessControlProfileIds");
235 eicCborAppendArray(&cborBuilder, numAccessControlProfileIds);
236 for (size_t n = 0; n < numAccessControlProfileIds; n++) {
237 eicCborAppendNumber(&cborBuilder, accessControlProfileIds[n]);
238 }
239 if (cborBuilder.size > cborBufferSize) {
240 eicDebug("Not enough space for additionalData - buffer is only %zd bytes, content is %zd",
241 cborBufferSize, cborBuilder.size);
242 return false;
243 }
244 if (outAdditionalDataCborSize != NULL) {
245 *outAdditionalDataCborSize = cborBuilder.size;
246 }
247 eicCborFinal(&cborBuilder, additionalDataSha256);
248 return true;
249 }
250