Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 1 | /* |
Tamas Ban | 5179a4d | 2024-01-25 17:05:30 +0100 | [diff] [blame] | 2 | * Copyright (c) 2023-2024, Arm Limited. All rights reserved. |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <assert.h> |
| 9 | #include "dpe_certificate.h" |
| 10 | #include "dpe_context_mngr.h" |
| 11 | #include "dpe_crypto_config.h" |
| 12 | #include "dpe_crypto_interface.h" |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 13 | #include "dpe_plat.h" |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 14 | #include "qcbor/qcbor_encode.h" |
| 15 | #include "t_cose_common.h" |
| 16 | #include "t_cose_sign1_sign.h" |
| 17 | |
| 18 | #define ID_HEX_SIZE (2 * DICE_ID_SIZE) /* Size of CDI encoded to ascii hex */ |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 19 | #define LABEL_HEX_SIZE (2 * DPE_EXTERNAL_LABEL_MAX_SIZE) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 20 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 21 | static void convert_to_ascii_hex(const uint8_t *in, |
| 22 | size_t in_size, |
| 23 | char *out, |
| 24 | size_t out_size) |
| 25 | { |
| 26 | const char hex_table[] = "0123456789abcdef"; |
| 27 | size_t in_pos = 0; |
| 28 | size_t out_pos = 0; |
| 29 | |
| 30 | for (in_pos = 0; in_pos < in_size && out_pos < out_size; in_pos++) { |
| 31 | out[out_pos++] = hex_table[(in[in_pos] & 0xF0 >> 4)]; |
| 32 | out[out_pos++] = hex_table[in[in_pos] & 0x0F]; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static dpe_error_t t_cose_err_to_dpe_err(enum t_cose_err_t err) |
| 37 | { |
| 38 | switch(err) { |
| 39 | |
| 40 | case T_COSE_SUCCESS: |
| 41 | return DPE_NO_ERROR; |
| 42 | |
| 43 | case T_COSE_ERR_TOO_SMALL: |
| 44 | return DPE_INSUFFICIENT_MEMORY; |
| 45 | |
| 46 | default: |
| 47 | /* A lot of the errors are not mapped because they are |
| 48 | * primarily internal errors that should never happen. They |
| 49 | * end up here. |
| 50 | */ |
| 51 | return DPE_INTERNAL_ERROR; |
| 52 | } |
| 53 | } |
| 54 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 55 | static dpe_error_t certificate_encode_start(QCBOREncodeContext *cbor_enc_ctx, |
| 56 | struct t_cose_sign1_sign_ctx *signer_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 57 | psa_key_handle_t private_key) |
| 58 | { |
| 59 | enum t_cose_err_t t_cose_err; |
| 60 | struct t_cose_key attest_key; |
| 61 | UsefulBufC attest_key_id = {NULL, 0}; |
| 62 | |
| 63 | /* DPE Certificate is untagged COSE_Sign1 message */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 64 | t_cose_sign1_sign_init(signer_ctx, T_COSE_OPT_OMIT_CBOR_TAG, DPE_T_COSE_ALG); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 65 | |
| 66 | attest_key.crypto_lib = T_COSE_CRYPTO_LIB_PSA; |
| 67 | attest_key.k.key_handle = private_key; |
| 68 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 69 | t_cose_sign1_set_signing_key(signer_ctx, attest_key, attest_key_id); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 70 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 71 | /* It is expected that the CBOR encoder is already initialized */ |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 72 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 73 | /* This encodes and writes the cose headers to be into the CBOR context. */ |
| 74 | t_cose_err = t_cose_sign1_encode_parameters(signer_ctx, |
| 75 | cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 76 | if (t_cose_err) { |
| 77 | return t_cose_err_to_dpe_err(t_cose_err); |
| 78 | } |
| 79 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 80 | QCBOREncode_OpenMap(cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 81 | |
| 82 | return DPE_NO_ERROR; |
| 83 | } |
| 84 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 85 | static void add_key_usage_claim(QCBOREncodeContext *cbor_enc_ctx) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 86 | { |
| 87 | uint8_t key_usage = DPE_CERT_KEY_USAGE_CERT_SIGN; |
| 88 | |
| 89 | /* Encode key usage as byte string */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 90 | QCBOREncode_AddBytesToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 91 | DPE_CERT_LABEL_KEY_USAGE, |
| 92 | (UsefulBufC){ &key_usage, |
| 93 | sizeof(key_usage) }); |
| 94 | } |
| 95 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 96 | static void add_label_claim(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 97 | const uint8_t *label, |
| 98 | size_t label_size) |
| 99 | { |
| 100 | char label_hex[LABEL_HEX_SIZE]; |
| 101 | |
| 102 | /* If label is supplied, add label claim, else skip */ |
| 103 | if ((label != NULL) && (label_size != 0)) { |
| 104 | convert_to_ascii_hex(&label[0], |
| 105 | label_size, |
| 106 | &label_hex[0], |
| 107 | sizeof(label_hex)); |
| 108 | |
| 109 | /* Encode label as text string */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 110 | QCBOREncode_AddTextToMapN(cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 111 | DPE_CERT_LABEL_EXTERNAL_LABEL, |
| 112 | (UsefulBufC){ &label_hex[0], |
| 113 | label_size }); |
| 114 | } |
| 115 | } |
| 116 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 117 | static void add_cdi_export_claim(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 118 | const struct cert_context_t *cert_ctx) |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 119 | { |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 120 | QCBOREncode_AddBoolToMapN(cbor_enc_ctx, |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 121 | DPE_CERT_LABEL_CDI_EXPORT, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 122 | cert_ctx->is_cdi_to_be_exported); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 125 | static void add_subject_claim(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 126 | const struct cert_context_t *cert_ctx) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 127 | { |
| 128 | char cdi_id_hex[ID_HEX_SIZE]; |
| 129 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 130 | convert_to_ascii_hex(&cert_ctx->data.cdi_id[0], |
| 131 | sizeof(cert_ctx->data.cdi_id), |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 132 | &cdi_id_hex[0], |
| 133 | sizeof(cdi_id_hex)); |
| 134 | /* Encode subject as text string */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 135 | QCBOREncode_AddTextToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 136 | DPE_CERT_LABEL_SUBJECT, |
| 137 | (UsefulBufC){ &cdi_id_hex[0], |
| 138 | sizeof(cdi_id_hex) }); |
| 139 | } |
| 140 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 141 | static void encode_issuer_claim(QCBOREncodeContext *cbor_enc_ctx, |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 142 | const uint8_t *issuer, |
| 143 | size_t issuer_size) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 144 | { |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 145 | char cdi_id_hex[ID_HEX_SIZE]; |
| 146 | |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 147 | convert_to_ascii_hex(issuer, |
| 148 | issuer_size, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 149 | &cdi_id_hex[0], |
| 150 | sizeof(cdi_id_hex)); |
| 151 | |
| 152 | /* Encode issuer as text string */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 153 | QCBOREncode_AddTextToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 154 | DPE_CERT_LABEL_ISSUER, |
| 155 | (UsefulBufC){ &cdi_id_hex[0], |
| 156 | sizeof(cdi_id_hex) }); |
| 157 | } |
| 158 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 159 | static void encode_public_key(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 160 | const uint8_t *pub_key, |
| 161 | size_t pub_key_size) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 162 | { |
| 163 | /* As per RFC8152 */ |
| 164 | const int64_t cose_key_type_value = DPE_T_COSE_KEY_TYPE_VAL; |
| 165 | const int64_t cose_key_ops_value = DPE_T_COSE_KEY_OPS_VAL; |
| 166 | const int64_t cose_key_ec2_curve_value = DPE_T_COSE_KEY_EC2_CURVE_VAL; |
| 167 | const int64_t cose_key_alg_value = DPE_T_COSE_KEY_ALG_VAL; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 168 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 169 | QCBOREncode_OpenMap(cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 170 | |
| 171 | /* Add the key type as int */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 172 | QCBOREncode_AddInt64ToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 173 | DPE_CERT_LABEL_COSE_KEY_TYPE, |
| 174 | cose_key_type_value); |
| 175 | |
| 176 | /* Add the algorithm as int */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 177 | QCBOREncode_AddInt64ToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 178 | DPE_CERT_LABEL_COSE_KEY_ALG, |
| 179 | cose_key_alg_value); |
| 180 | |
| 181 | /* Add the key operation as [+ (tstr/int)] */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 182 | QCBOREncode_OpenArrayInMapN(cbor_enc_ctx, DPE_CERT_LABEL_COSE_KEY_OPS); |
| 183 | QCBOREncode_AddInt64(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 184 | cose_key_ops_value); |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 185 | QCBOREncode_CloseArray(cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 186 | |
| 187 | /* Add the curve */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 188 | QCBOREncode_AddInt64ToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 189 | DPE_CERT_LABEL_COSE_KEY_EC2_CURVE, |
| 190 | cose_key_ec2_curve_value); |
| 191 | |
Tamas Ban | 8ed5280 | 2024-03-14 09:36:05 +0100 | [diff] [blame] | 192 | /* |
| 193 | * From psa/crypto.h: |
| 194 | * |
| 195 | * For other elliptic curve public keys (key types for which |
| 196 | * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed |
| 197 | * representation defined by SEC1 §2.3.3 as the content of an ECPoint. |
| 198 | * Let `m` be the bit size associated with the curve, i.e. the bit size of |
| 199 | * `q` for a curve over `F_q`. The representation consists of: |
| 200 | * - The byte 0x04; |
| 201 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 202 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
| 203 | |
| 204 | * Furthermore, as per rfc5480 section-2.2: |
| 205 | * |
| 206 | * The first octet of the OCTET STRING indicates whether the key is |
| 207 | * compressed or uncompressed. The uncompressed form is indicated by 0x04 |
| 208 | * and the compressed form is indicated by either 0x02 or 0x03. |
| 209 | */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 210 | QCBOREncode_AddBytesToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 211 | DPE_CERT_LABEL_COSE_KEY_EC2_X, |
Tamas Ban | 8ed5280 | 2024-03-14 09:36:05 +0100 | [diff] [blame] | 212 | (UsefulBufC){ &pub_key[1], |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 213 | pub_key_size / 2 }); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 214 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 215 | QCBOREncode_AddBytesToMapN(cbor_enc_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 216 | DPE_CERT_LABEL_COSE_KEY_EC2_Y, |
Tamas Ban | 8ed5280 | 2024-03-14 09:36:05 +0100 | [diff] [blame] | 217 | (UsefulBufC){ &pub_key[1 + (pub_key_size / 2)], |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 218 | pub_key_size / 2 }); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 219 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 220 | QCBOREncode_CloseMap(cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 221 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 224 | static void add_public_key_claim(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 225 | const uint8_t *pub_key, |
| 226 | size_t pub_key_size) |
| 227 | { |
| 228 | UsefulBufC wrapped; |
| 229 | |
| 230 | /* Cose key is encoded as a map. This map is wrapped into the a |
| 231 | * byte string and it is further encoded as a map */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 232 | QCBOREncode_BstrWrapInMapN(cbor_enc_ctx, DPE_CERT_LABEL_SUBJECT_PUBLIC_KEY); |
| 233 | encode_public_key(cbor_enc_ctx, pub_key, pub_key_size); |
| 234 | QCBOREncode_CloseBstrWrap2(cbor_enc_ctx, true, &wrapped); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 235 | assert(wrapped.len <= DICE_MAX_ENCODED_PUBLIC_KEY_SIZE); |
| 236 | } |
| 237 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 238 | static void add_public_key_to_certificate_chain(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 239 | const uint8_t *pub_key, |
| 240 | size_t pub_key_size) |
| 241 | { |
| 242 | UsefulBufC wrapped; |
| 243 | |
| 244 | /* Cose key is encoded as a map wrapped into a byte string */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 245 | QCBOREncode_BstrWrap(cbor_enc_ctx); |
| 246 | encode_public_key(cbor_enc_ctx, pub_key, pub_key_size); |
| 247 | QCBOREncode_CloseBstrWrap2(cbor_enc_ctx, true, &wrapped); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 248 | assert(wrapped.len <= DICE_MAX_ENCODED_PUBLIC_KEY_SIZE); |
| 249 | } |
| 250 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 251 | static dpe_error_t certificate_encode_finish(QCBOREncodeContext *cbor_enc_ctx, |
| 252 | struct t_cose_sign1_sign_ctx *signer_ctx, |
| 253 | bool finish_cbor_encoding, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 254 | UsefulBufC *completed_cert) |
| 255 | { |
| 256 | QCBORError qcbor_result; |
| 257 | enum t_cose_err_t cose_return_value; |
| 258 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 259 | QCBOREncode_CloseMap(cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 260 | |
| 261 | /* -- Finish up the COSE_Sign1. This is where the signing happens -- */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 262 | cose_return_value = t_cose_sign1_encode_signature(signer_ctx, |
| 263 | cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 264 | if (cose_return_value) { |
| 265 | /* Main errors are invoking the hash or signature */ |
| 266 | return t_cose_err_to_dpe_err(cose_return_value); |
| 267 | } |
| 268 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 269 | /* If only a single certificate is created then encoding can be finished. |
| 270 | * Otherwise, when multiple certifcate is encoded in a raw |
| 271 | * (GetCertificateChain) then encoding will be finished |
| 272 | * by close_certificate_chain(). |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 273 | */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 274 | if (finish_cbor_encoding) { |
| 275 | /* Finally close off the CBOR formatting and get the pointer and length |
| 276 | * of the resulting COSE_Sign1. |
| 277 | */ |
| 278 | qcbor_result = QCBOREncode_Finish(cbor_enc_ctx, completed_cert); |
| 279 | if (qcbor_result == QCBOR_ERR_BUFFER_TOO_SMALL) { |
| 280 | return DPE_INSUFFICIENT_MEMORY; |
| 281 | } else if (qcbor_result != QCBOR_SUCCESS) { |
| 282 | /* likely from array not closed, too many closes, ... */ |
| 283 | return DPE_INTERNAL_ERROR; |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 284 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 285 | } |
Maulik Patel | 7718639 | 2024-04-15 12:39:04 +0100 | [diff] [blame] | 286 | |
| 287 | return DPE_NO_ERROR; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void encode_sw_component_measurements(QCBOREncodeContext *encode_ctx, |
| 291 | struct component_context_t *component_ctx) |
| 292 | { |
| 293 | QCBOREncode_OpenMap(encode_ctx); |
| 294 | |
| 295 | /* Encode measurement value as byte string */ |
| 296 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 297 | DPE_CERT_LABEL_CODE_HASH, |
| 298 | (UsefulBufC){ &component_ctx->data.measurement_value, |
| 299 | DICE_HASH_SIZE }); |
| 300 | |
| 301 | /* Encode measurement descriptor version as byte string */ |
| 302 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 303 | DPE_CERT_LABEL_CODE_DESCRIPTOR, |
| 304 | (UsefulBufC){ &component_ctx->data.measurement_descriptor, |
| 305 | component_ctx->data.measurement_descriptor_size }); |
| 306 | |
| 307 | /* Encode signer ID Hash as byte string */ |
| 308 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 309 | DPE_CERT_LABEL_AUTHORITY_HASH, |
| 310 | (UsefulBufC){ &component_ctx->data.signer_id, |
| 311 | DICE_HASH_SIZE }); |
| 312 | |
| 313 | /* Encode signer ID descriptor as byte string */ |
| 314 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 315 | DPE_CERT_LABEL_AUTHORITY_DESCRIPTOR, |
| 316 | (UsefulBufC){ &component_ctx->data.signer_id_descriptor, |
| 317 | component_ctx->data.signer_id_descriptor_size }); |
| 318 | |
| 319 | if (component_ctx->data.config_descriptor_size > 0) { |
| 320 | /* Encode config descriptor as byte string */ |
| 321 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 322 | DPE_CERT_LABEL_CONFIGURATION_DESCRIPTOR, |
| 323 | (UsefulBufC){ &component_ctx->data.config_descriptor, |
| 324 | component_ctx->data.config_descriptor_size }); |
| 325 | /* Encode config value as byte string */ |
| 326 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 327 | DPE_CERT_LABEL_CONFIGURATION_HASH, |
| 328 | (UsefulBufC){ &component_ctx->data.config_value, |
| 329 | DICE_INLINE_CONFIG_SIZE }); |
| 330 | } else { |
| 331 | /* Encode config value as byte string */ |
| 332 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 333 | DPE_CERT_LABEL_CONFIGURATION_DESCRIPTOR, |
| 334 | (UsefulBufC){ &component_ctx->data.config_value, |
| 335 | DICE_INLINE_CONFIG_SIZE }); |
| 336 | } |
| 337 | |
| 338 | /* Encode mode value as byte string */ |
| 339 | QCBOREncode_AddBytesToMapN(encode_ctx, |
| 340 | DPE_CERT_LABEL_MODE, |
| 341 | (UsefulBufC){ &component_ctx->data.mode, |
| 342 | sizeof(DiceMode) }); |
| 343 | |
| 344 | QCBOREncode_CloseMap(encode_ctx); |
| 345 | } |
| 346 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 347 | static dpe_error_t encode_sw_components_array(const struct cert_context_t *cert_ctx, |
| 348 | QCBOREncodeContext *cbor_enc_ctx) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 349 | { |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame] | 350 | int i; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 351 | struct component_context_t *component_ctx; |
| 352 | |
Tamas Ban | cb237ce | 2024-05-03 14:01:33 +0200 | [diff] [blame] | 353 | /* Open array which stores SW components claims. */ |
| 354 | QCBOREncode_OpenArrayInMapN(cbor_enc_ctx, DPE_CERT_LABEL_SW_COMPONENTS); |
| 355 | |
| 356 | /* Add elements to the array if there is any */ |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 357 | for (i = 0; i < cert_ctx->linked_components.count; i++) { |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 358 | component_ctx = cert_ctx->linked_components.ptr[i]; |
Tamas Ban | cb237ce | 2024-05-03 14:01:33 +0200 | [diff] [blame] | 359 | if (component_ctx == NULL) { |
| 360 | return DPE_INTERNAL_ERROR; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 361 | } |
Tamas Ban | cb237ce | 2024-05-03 14:01:33 +0200 | [diff] [blame] | 362 | encode_sw_component_measurements(cbor_enc_ctx, component_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 363 | } |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame] | 364 | |
Tamas Ban | cb237ce | 2024-05-03 14:01:33 +0200 | [diff] [blame] | 365 | /* Close array which stores SW components claims. */ |
| 366 | QCBOREncode_CloseArray(cbor_enc_ctx); |
| 367 | |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame] | 368 | return DPE_NO_ERROR; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 371 | static dpe_error_t add_issuer_claim(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 372 | const struct cert_context_t *cert_ctx, |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 373 | psa_key_id_t root_attest_key_id, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 374 | const struct cert_context_t *parent_cert_ctx) |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 375 | { |
| 376 | uint8_t rot_cdi_id[DICE_ID_SIZE]; |
| 377 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 378 | if (cert_ctx->is_rot_cert_ctx) { |
| 379 | /* For the RoT certificate, issuer id is derived from the root attestation key */ |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 380 | if (derive_cdi_id(root_attest_key_id, rot_cdi_id, |
| 381 | sizeof(rot_cdi_id)) != PSA_SUCCESS) { |
| 382 | return DPE_INTERNAL_ERROR; |
| 383 | } |
| 384 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 385 | encode_issuer_claim(cbor_enc_ctx, |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 386 | rot_cdi_id, |
| 387 | sizeof(rot_cdi_id)); |
| 388 | } else { |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 389 | encode_issuer_claim(cbor_enc_ctx, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 390 | parent_cert_ctx->data.cdi_id, |
| 391 | sizeof(parent_cert_ctx->data.cdi_id)); |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | return DPE_NO_ERROR; |
| 395 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 396 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 397 | static dpe_error_t encode_certificate_internal(const struct cert_context_t *cert_ctx, |
| 398 | QCBOREncodeContext *cbor_enc_ctx, |
| 399 | bool finish_cbor_encoding, |
| 400 | size_t *cert_actual_size) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 401 | { |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 402 | struct t_cose_sign1_sign_ctx signer_ctx; |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 403 | struct cert_context_t *parent_cert_ctx; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 404 | dpe_error_t err; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 405 | UsefulBufC completed_cert; |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 406 | psa_key_id_t attest_key_id; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 407 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 408 | /* Valid options: true & !NULL OR false & NULL */ |
| 409 | assert(finish_cbor_encoding ^ (cert_actual_size == NULL)); |
| 410 | |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 411 | parent_cert_ctx = cert_ctx->parent_cert_ptr; |
| 412 | assert(parent_cert_ctx != NULL); |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 413 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 414 | /* The RoT certificate is signed by the provisioned attestation key, |
| 415 | * all other certificates are signed by the parent certificate's attestation key. |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 416 | */ |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 417 | if (cert_ctx->is_rot_cert_ctx) { |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 418 | attest_key_id = dpe_plat_get_root_attest_key_id(); |
| 419 | } else { |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 420 | attest_key_id = parent_cert_ctx->data.attest_key_id; |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 421 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 422 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 423 | /* Get started creating the certificate. This sets up the CBOR and |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 424 | * COSE contexts which causes the COSE headers to be constructed. |
| 425 | */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 426 | err = certificate_encode_start(cbor_enc_ctx, |
| 427 | &signer_ctx, |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 428 | attest_key_id); |
| 429 | if (err != DPE_NO_ERROR) { |
| 430 | return err; |
| 431 | } |
| 432 | |
| 433 | /* Add all the required claims */ |
| 434 | /* Add issuer/authority claim */ |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 435 | err = add_issuer_claim(cbor_enc_ctx, cert_ctx, attest_key_id, parent_cert_ctx); |
Jamie Fox | 9322523 | 2023-09-22 14:09:30 +0100 | [diff] [blame] | 436 | if (err != DPE_NO_ERROR) { |
| 437 | return err; |
| 438 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 439 | |
| 440 | /* Add subject claim */ |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 441 | add_subject_claim(cbor_enc_ctx, cert_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 442 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 443 | /* Encode all firmware measurements for the components linked to this |
| 444 | * certificate context |
| 445 | */ |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 446 | //TODO: |
| 447 | /* It is not yet defined in the open-dice profile how to represent |
| 448 | * multiple SW components in a single certificate; In current implementation, |
| 449 | * an array is created for all the components' measurements and within the |
| 450 | * array, there are multiple maps, one for each SW component |
| 451 | */ |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 452 | err = encode_sw_components_array(cert_ctx, cbor_enc_ctx); |
Maulik Patel | 009450d | 2024-04-23 12:03:10 +0100 | [diff] [blame] | 453 | if (err != DPE_NO_ERROR) { |
| 454 | return err; |
| 455 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 456 | |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 457 | /* Add label claim */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 458 | add_label_claim(cbor_enc_ctx, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 459 | &cert_ctx->data.external_key_deriv_label[0], |
| 460 | cert_ctx->data.external_key_deriv_label_len); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 461 | |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 462 | /* Add public key claim */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 463 | add_public_key_claim(cbor_enc_ctx, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 464 | &cert_ctx->data.attest_pub_key[0], |
| 465 | cert_ctx->data.attest_pub_key_len); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 466 | |
| 467 | /* Add key usage claim */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 468 | add_key_usage_claim(cbor_enc_ctx); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 469 | |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 470 | /* Add CDI exported claim */ |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 471 | if (cert_ctx->is_cdi_to_be_exported) { |
| 472 | add_cdi_export_claim(cbor_enc_ctx, cert_ctx); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 475 | /* Finish up creating the certificate. This is where the actual signature |
| 476 | * is generated. |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 477 | */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 478 | err = certificate_encode_finish(cbor_enc_ctx, &signer_ctx, |
| 479 | finish_cbor_encoding, &completed_cert); |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 480 | if (err != DPE_NO_ERROR) { |
| 481 | return err; |
| 482 | } |
| 483 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 484 | /* Update the final size of the certificate if requested */ |
| 485 | if (cert_actual_size != NULL) { |
| 486 | *cert_actual_size = completed_cert.len; |
| 487 | } |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 488 | |
| 489 | return err; |
| 490 | } |
| 491 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 492 | dpe_error_t encode_certificate(const struct cert_context_t *cert_ctx, |
| 493 | uint8_t *cert_buf, |
| 494 | size_t cert_buf_size, |
| 495 | size_t *cert_actual_size) |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 496 | { |
| 497 | QCBOREncodeContext cbor_enc_ctx; |
| 498 | |
| 499 | QCBOREncode_Init(&cbor_enc_ctx, |
| 500 | (UsefulBuf){ cert_buf, |
| 501 | cert_buf_size }); |
| 502 | |
| 503 | /* Only a single certificate is encoded */ |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 504 | return encode_certificate_internal(cert_ctx, &cbor_enc_ctx, |
| 505 | true, cert_actual_size); |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 508 | dpe_error_t store_certificate(const struct cert_context_t *cert_ctx) |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 509 | { |
| 510 | //TODO: |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 511 | (void)cert_ctx; |
Maulik Patel | 2358bbb | 2023-07-21 10:56:56 +0100 | [diff] [blame] | 512 | return DPE_NO_ERROR; |
| 513 | } |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 514 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 515 | static void open_certificate_chain(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 516 | uint8_t *cert_chain_buf, |
| 517 | size_t cert_chain_buf_size) |
| 518 | { |
| 519 | /* Set up encoding context with output buffer. */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 520 | QCBOREncode_Init(cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 521 | (UsefulBuf){ &cert_chain_buf[0], |
| 522 | cert_chain_buf_size }); |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 523 | QCBOREncode_OpenArray(cbor_enc_ctx); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 526 | static dpe_error_t close_certificate_chain(QCBOREncodeContext *cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 527 | size_t *cert_chain_actual_size) |
| 528 | { |
| 529 | QCBORError encode_error; |
| 530 | UsefulBufC completed_cert_chain; |
| 531 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 532 | QCBOREncode_CloseArray(cbor_enc_ctx); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 533 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 534 | encode_error = QCBOREncode_Finish(cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 535 | &completed_cert_chain); |
| 536 | |
| 537 | /* Check for any encoding errors. */ |
| 538 | if (encode_error == QCBOR_ERR_BUFFER_TOO_SMALL) { |
| 539 | return DPE_INSUFFICIENT_MEMORY; |
| 540 | } else if (encode_error != QCBOR_SUCCESS) { |
| 541 | return DPE_INTERNAL_ERROR; |
| 542 | } |
| 543 | |
| 544 | *cert_chain_actual_size = completed_cert_chain.len; |
| 545 | |
| 546 | return DPE_NO_ERROR; |
| 547 | } |
| 548 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 549 | static dpe_error_t add_root_attestation_public_key(QCBOREncodeContext *cbor_enc_ctx) |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 550 | { |
| 551 | psa_status_t status; |
| 552 | psa_key_id_t attest_key_id; |
| 553 | uint8_t attest_pub_key[DPE_ATTEST_PUB_KEY_SIZE]; |
| 554 | size_t attest_pub_key_len; |
| 555 | |
| 556 | attest_key_id = dpe_plat_get_root_attest_key_id(); |
| 557 | |
| 558 | status = psa_export_public_key(attest_key_id, attest_pub_key, |
| 559 | sizeof(attest_pub_key), &attest_pub_key_len); |
| 560 | if (status != PSA_SUCCESS) { |
| 561 | return DPE_INTERNAL_ERROR; |
| 562 | } |
| 563 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 564 | add_public_key_to_certificate_chain(cbor_enc_ctx, &attest_pub_key[0], attest_pub_key_len); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 565 | |
| 566 | return DPE_NO_ERROR; |
| 567 | } |
| 568 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 569 | dpe_error_t get_certificate_chain(const struct cert_context_t *cert_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 570 | uint8_t *cert_chain_buf, |
| 571 | size_t cert_chain_buf_size, |
| 572 | size_t *cert_chain_actual_size) |
| 573 | { |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 574 | QCBOREncodeContext cbor_enc_ctx; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 575 | dpe_error_t err; |
| 576 | int i; |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 577 | const struct cert_context_t *cert_chain[MAX_NUM_OF_CERTIFICATES]; |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 578 | uint16_t cert_cnt = 0; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 579 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 580 | open_certificate_chain(&cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 581 | cert_chain_buf, |
| 582 | cert_chain_buf_size); |
| 583 | |
| 584 | /* Add DICE/Root public key (IAK public key) as the first entry of array */ |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 585 | err = add_root_attestation_public_key(&cbor_enc_ctx); |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 586 | if (err != DPE_NO_ERROR) { |
| 587 | return err; |
| 588 | } |
| 589 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 590 | /* Loop from leaf to the RoT certificate & save all the linked certificates in this chain */ |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 591 | while ((cert_ctx != NULL) && (cert_cnt < MAX_NUM_OF_CERTIFICATES)) { |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 592 | |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 593 | /* Save certificate context pointer */ |
| 594 | cert_chain[cert_cnt++] = cert_ctx; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 595 | |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 596 | if (cert_ctx->is_rot_cert_ctx) { |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 597 | /* This is the end of chain */ |
| 598 | break; |
| 599 | } |
| 600 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 601 | /* Move to the parent certificate context */ |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 602 | cert_ctx = cert_ctx->parent_cert_ptr; |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 605 | /* Add certificate from RoT to leaf certificate order */ |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 606 | for (i = cert_cnt - 1; i >= 0; i--) { |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 607 | /* Might multiple certificate is encoded */ |
Maulik Patel | 00d06b6 | 2024-07-03 14:51:50 +0100 | [diff] [blame^] | 608 | err = encode_certificate_internal(cert_chain[i], &cbor_enc_ctx, |
Maulik Patel | 97a61fe | 2024-07-01 15:55:04 +0100 | [diff] [blame] | 609 | false, NULL); |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 610 | if (err != DPE_NO_ERROR) { |
| 611 | return err; |
| 612 | } |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Tamas Ban | 257471b | 2024-03-25 13:49:53 +0100 | [diff] [blame] | 615 | return close_certificate_chain(&cbor_enc_ctx, |
Maulik Patel | e6adc11 | 2023-08-18 14:21:51 +0100 | [diff] [blame] | 616 | cert_chain_actual_size); |
| 617 | } |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 618 | |
Tamas Ban | 5179a4d | 2024-01-25 17:05:30 +0100 | [diff] [blame] | 619 | dpe_error_t encode_cdi(const uint8_t cdi_attest_buf[DICE_CDI_SIZE], |
| 620 | const uint8_t cdi_seal_buf[DICE_CDI_SIZE], |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 621 | uint8_t *encoded_cdi_buf, |
| 622 | size_t encoded_cdi_buf_size, |
| 623 | size_t *encoded_cdi_actual_size) |
| 624 | { |
| 625 | QCBOREncodeContext encode_ctx; |
| 626 | QCBORError encode_err; |
| 627 | UsefulBufC out; |
| 628 | |
| 629 | QCBOREncode_Init(&encode_ctx, (UsefulBuf){ encoded_cdi_buf, encoded_cdi_buf_size }); |
| 630 | QCBOREncode_OpenMap(&encode_ctx); |
| 631 | |
| 632 | /* Encode CDI value as byte string */ |
| 633 | QCBOREncode_AddBytesToMapN(&encode_ctx, |
| 634 | DPE_LABEL_CDI_ATTEST, |
Tamas Ban | 5179a4d | 2024-01-25 17:05:30 +0100 | [diff] [blame] | 635 | (UsefulBufC){ cdi_attest_buf, DICE_CDI_SIZE }); |
| 636 | |
| 637 | QCBOREncode_AddBytesToMapN(&encode_ctx, |
| 638 | DPE_LABEL_CDI_SEAL, |
| 639 | (UsefulBufC){ cdi_seal_buf, DICE_CDI_SIZE }); |
Maulik Patel | 9fd8bd2 | 2023-10-30 10:58:30 +0000 | [diff] [blame] | 640 | |
| 641 | QCBOREncode_CloseMap(&encode_ctx); |
| 642 | encode_err = QCBOREncode_Finish(&encode_ctx, &out); |
| 643 | |
| 644 | /* Check for any encoding errors. */ |
| 645 | if (encode_err == QCBOR_ERR_BUFFER_TOO_SMALL) { |
| 646 | return DPE_INSUFFICIENT_MEMORY; |
| 647 | } else if (encode_err != QCBOR_SUCCESS) { |
| 648 | return DPE_INTERNAL_ERROR; |
| 649 | } |
| 650 | |
| 651 | *encoded_cdi_actual_size = out.len; |
| 652 | |
| 653 | return DPE_NO_ERROR; |
| 654 | } |