David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | |
| 19 | void eicCborInit(EicCbor* cbor, uint8_t* buffer, size_t bufferSize) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 20 | eicMemSet(cbor, '\0', sizeof(EicCbor)); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | |
| 28 | void eicCborInitHmacSha256(EicCbor* cbor, uint8_t* buffer, size_t bufferSize, |
| 29 | const uint8_t* hmacKey, size_t hmacKeySize) { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 30 | eicMemSet(cbor, '\0', sizeof(EicCbor)); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 38 | void eicCborEnableSecondaryDigesterSha256(EicCbor* cbor, EicSha256Ctx* sha256) { |
| 39 | cbor->secondaryDigesterSha256 = sha256; |
| 40 | } |
| 41 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | |
| 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 | } |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 62 | if (cbor->secondaryDigesterSha256 != NULL) { |
| 63 | eicOpsSha256Update(cbor->secondaryDigesterSha256, data, size); |
| 64 | } |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | |
| 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 | |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 94 | void eicCborBegin(EicCbor* cbor, int majorType, uint64_t size) { |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 { |
Andrew Scull | 29ba064 | 2021-05-12 12:18:08 +0000 | [diff] [blame] | 117 | data[0] = (majorType << 5) | 27; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | |
| 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 | |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 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 | |
| 140 | void eicCborAppendStringZ(EicCbor* cbor, const char* str) { |
| 141 | eicCborAppendString(cbor, str, eicStrLen(str)); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void eicCborAppendSimple(EicCbor* cbor, uint8_t simpleValue) { |
| 145 | eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_SIMPLE, simpleValue); |
| 146 | } |
| 147 | |
| 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 | |
| 153 | void eicCborAppendSemantic(EicCbor* cbor, uint64_t value) { |
| 154 | size_t encoded = value; |
| 155 | eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_SEMANTIC, encoded); |
| 156 | } |
| 157 | |
| 158 | void eicCborAppendUnsigned(EicCbor* cbor, uint64_t value) { |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 159 | uint64_t encoded = value; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 160 | eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_UNSIGNED, encoded); |
| 161 | } |
| 162 | |
| 163 | void eicCborAppendNumber(EicCbor* cbor, int64_t value) { |
| 164 | if (value < 0) { |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 165 | uint64_t encoded = -1 - value; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 166 | eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_NEGATIVE, encoded); |
| 167 | } else { |
| 168 | eicCborAppendUnsigned(cbor, value); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void eicCborAppendArray(EicCbor* cbor, size_t numElements) { |
| 173 | eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_ARRAY, numElements); |
| 174 | } |
| 175 | |
| 176 | void eicCborAppendMap(EicCbor* cbor, size_t numPairs) { |
| 177 | eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_MAP, numPairs); |
| 178 | } |
| 179 | |
| 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); |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 194 | eicCborAppendStringZ(cborBuilder, "id"); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 195 | eicCborAppendUnsigned(cborBuilder, id); |
| 196 | if (readerCertificateSize > 0) { |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 197 | eicCborAppendStringZ(cborBuilder, "readerCertificate"); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 198 | eicCborAppendByteString(cborBuilder, readerCertificate, readerCertificateSize); |
| 199 | } |
| 200 | if (userAuthenticationRequired) { |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 201 | eicCborAppendStringZ(cborBuilder, "userAuthenticationRequired"); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 202 | eicCborAppendBool(cborBuilder, userAuthenticationRequired); |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 203 | eicCborAppendStringZ(cborBuilder, "timeoutMillis"); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 204 | eicCborAppendUnsigned(cborBuilder, timeoutMillis); |
| 205 | if (secureUserId > 0) { |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 206 | eicCborAppendStringZ(cborBuilder, "secureUserId"); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 220 | bool eicCborCalcEntryAdditionalData(const uint8_t* accessControlProfileIds, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 221 | size_t numAccessControlProfileIds, const char* nameSpace, |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 222 | size_t nameSpaceLength, const char* name, |
| 223 | size_t nameLength, uint8_t* cborBuffer, |
| 224 | size_t cborBufferSize, size_t* outAdditionalDataCborSize, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 225 | uint8_t additionalDataSha256[EIC_SHA256_DIGEST_SIZE]) { |
| 226 | EicCbor cborBuilder; |
| 227 | |
| 228 | eicCborInit(&cborBuilder, cborBuffer, cborBufferSize); |
| 229 | eicCborAppendMap(&cborBuilder, 3); |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame] | 230 | eicCborAppendStringZ(&cborBuilder, "Namespace"); |
| 231 | eicCborAppendString(&cborBuilder, nameSpace, nameSpaceLength); |
| 232 | eicCborAppendStringZ(&cborBuilder, "Name"); |
| 233 | eicCborAppendString(&cborBuilder, name, nameLength); |
| 234 | eicCborAppendStringZ(&cborBuilder, "AccessControlProfileIds"); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 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 | } |