Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 1 | /* |
Maulik Patel | d551100 | 2025-05-19 14:17:28 +0100 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors |
| 3 | * |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Maulik Patel | d551100 | 2025-05-19 14:17:28 +0100 | [diff] [blame] | 5 | * |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 8 | #include <string.h> |
Maulik Patel | a0a2246 | 2023-02-02 11:24:48 +0000 | [diff] [blame] | 9 | #include "psa_adac_sda.h" |
| 10 | #include "platform/msg_interface.h" |
| 11 | #include "psa_adac.h" |
| 12 | #include "psa_adac_crypto_api.h" |
| 13 | #include "psa_adac_cryptosystems.h" |
| 14 | #include "psa_adac_debug.h" |
| 15 | |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 16 | |
| 17 | #ifdef PSA_ADAC_AUTHENTICATOR_IMPLICIT_TRANSPORT |
| 18 | |
| 19 | static inline request_packet_t *authenticator_request_packet_receive(authentication_context_t *auth_ctx) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 20 | (void) auth_ctx; /* misra-c2012-2.7 */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 21 | return request_packet_receive(); |
| 22 | } |
| 23 | |
| 24 | static inline int authenticator_request_packet_release(authentication_context_t *auth_ctx, request_packet_t *packet) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 25 | (void) auth_ctx; /* misra-c2012-2.7 */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 26 | return request_packet_release(packet); |
| 27 | } |
| 28 | |
| 29 | static inline int authenticator_response_packet_release(authentication_context_t *auth_ctx, response_packet_t *packet) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 30 | (void) auth_ctx; /* misra-c2012-2.7 */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 31 | return response_packet_release(packet); |
| 32 | } |
| 33 | |
| 34 | static inline response_packet_t *authenticator_response_packet_build(authentication_context_t *auth_ctx, |
| 35 | uint16_t status, uint8_t *data, size_t data_size) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 36 | (void) auth_ctx; /* misra-c2012-2.7 */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 37 | return response_packet_build(status, data, data_size); |
| 38 | } |
| 39 | |
| 40 | static inline response_packet_t *authenticator_response_packet_lock(authentication_context_t *auth_ctx, |
| 41 | size_t *max_data_size) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 42 | (void) auth_ctx; /* misra-c2012-2.7 */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 43 | return response_packet_lock(max_data_size); |
| 44 | } |
| 45 | |
| 46 | static inline int authenticator_response_packet_send(authentication_context_t *auth_ctx, response_packet_t *packet) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 47 | (void) auth_ctx; /* misra-c2012-2.7 */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 48 | return response_packet_send(packet); |
| 49 | } |
| 50 | |
| 51 | #endif |
| 52 | |
| 53 | int authenticator_send_response(authentication_context_t *auth_ctx, response_packet_t *response) { |
| 54 | int r = 1; |
| 55 | if (response != NULL) { |
| 56 | if (authenticator_response_packet_send(auth_ctx, response) == 0) { |
| 57 | r = 0; |
| 58 | } |
| 59 | (void) authenticator_response_packet_release(auth_ctx, response); |
| 60 | } |
| 61 | return r; |
| 62 | } |
| 63 | |
| 64 | response_packet_t *authentication_discovery(authentication_context_t *auth_ctx, request_packet_t *request) { |
| 65 | (void) authenticator_request_packet_release(auth_ctx, request); |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 66 | PSA_ADAC_LOG_DEBUG("auth", "Discovery\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 67 | |
| 68 | size_t max = 0; |
| 69 | response_packet_t *r = authenticator_response_packet_lock(auth_ctx, &max); |
| 70 | if (r != NULL) { |
| 71 | size_t size = psa_adac_platform_discovery((uint8_t *) r->data, max); |
| 72 | while (((size % 4UL) != 0UL) && (size < max)) { |
| 73 | ((uint8_t *) r->data)[size] = 0x0; |
| 74 | size += 1UL; |
| 75 | } |
| 76 | r->data_count = size >> 2; |
| 77 | r->status = 0x0; |
| 78 | } |
| 79 | return r; |
| 80 | } |
| 81 | |
| 82 | response_packet_t *authentication_start(authentication_context_t *auth_ctx, request_packet_t *request) { |
| 83 | (void) authenticator_request_packet_release(auth_ctx, request); |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 84 | PSA_ADAC_LOG_DEBUG("auth", "Starting authentication\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 85 | auth_ctx->state = AUTH_CHALLENGE; |
| 86 | |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 87 | PSA_ADAC_LOG_DEBUG("auth", "Generating challenge (%d)\r\n", CHALLENGE_SIZE); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 88 | auth_ctx->challenge.format_version.major = 0x01; |
| 89 | auth_ctx->challenge.format_version.minor = 0x00; |
| 90 | auth_ctx->challenge._reserved = 0x00; |
| 91 | psa_adac_generate_challenge(auth_ctx->challenge.challenge_vector, sizeof(auth_ctx->challenge.challenge_vector)); |
| 92 | response_packet_t *response = authenticator_response_packet_build(auth_ctx, 0x0, (uint8_t *) &auth_ctx->challenge, |
| 93 | sizeof(auth_ctx->challenge)); |
| 94 | return response; |
| 95 | } |
| 96 | |
| 97 | int is_hashed_rotpk_entry(uint8_t key_type, psa_algorithm_t algo, size_t rotpk_size) { |
| 98 | int ret = 1; |
| 99 | if ((key_type == CMAC_AES) || (key_type == HMAC_SHA256)) { |
| 100 | ret = 0; |
| 101 | } |
| 102 | |
| 103 | if (algo == PSA_ALG_SHA_256) { |
| 104 | if (rotpk_size != 32UL) { |
| 105 | ret = 0; |
| 106 | } |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 107 | } else { |
| 108 | if (rotpk_size != PSA_HASH_LENGTH(algo)) { |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 109 | ret = 0; |
| 110 | } |
| 111 | } |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | psa_status_t psa_adac_certificate_check(uint8_t *crt, size_t crt_size, rotpk_context_t *rotpk_ctx, |
| 117 | validation_context_t *context, int is_root) { |
| 118 | certificate_header_t *header = (certificate_header_t *) crt; |
| 119 | psa_status_t r = psa_adac_certificate_sanity_check(crt, crt_size); |
| 120 | |
| 121 | if (PSA_SUCCESS != r) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 122 | /* Certificate failed sanity check */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 123 | } else if (is_root != 0) { |
| 124 | int found = 0; |
| 125 | for (int i = 0; (i < rotpk_ctx->rotpk_count) && (!found); i++) { |
| 126 | if ((rotpk_ctx->rotpk_type[i] != header->key_type) || |
| 127 | (rotpk_ctx->rotpk_type[i] != header->signature_type)) { |
| 128 | continue; |
| 129 | } |
| 130 | if (is_hashed_rotpk_entry(rotpk_ctx->rotpk_type[i], rotpk_ctx->rotpk_algo, rotpk_ctx->rotpk_size[i]) != 0) { |
| 131 | size_t pubkey_size; |
| 132 | uint8_t key_type; |
| 133 | uint8_t *pubkey = NULL; |
| 134 | |
| 135 | if ((PSA_SUCCESS != psa_adac_extract_public_key(crt, crt_size, &key_type, &pubkey, &pubkey_size)) || |
| 136 | (PSA_SUCCESS != psa_adac_hash_verify(rotpk_ctx->rotpk_algo, pubkey, pubkey_size, |
| 137 | rotpk_ctx->rotpk[i], rotpk_ctx->rotpk_size[i])) || |
| 138 | (PSA_SUCCESS != psa_adac_context_load_key(context, key_type, pubkey, pubkey_size)) || |
| 139 | (PSA_SUCCESS != psa_adac_certificate_verify_sig(crt, crt_size, context->key_type, |
| 140 | context->content, context->size))) { |
| 141 | continue; |
| 142 | } |
| 143 | found = 1; |
| 144 | } else { |
| 145 | if ((PSA_SUCCESS != psa_adac_context_load_key(context, rotpk_ctx->rotpk_type[i], rotpk_ctx->rotpk[i], |
| 146 | rotpk_ctx->rotpk_size[i])) || |
| 147 | (PSA_SUCCESS != psa_adac_certificate_verify_sig(crt, crt_size, context->key_type, |
| 148 | context->content, context->size)) || |
| 149 | (PSA_SUCCESS != psa_adac_update_context(crt, crt_size, context))) { |
| 150 | continue; |
| 151 | } |
| 152 | found = 1; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | r = found ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST; |
| 157 | } else { |
| 158 | r = psa_adac_certificate_verify_sig(crt, crt_size, context->key_type, |
| 159 | context->content, context->size); |
| 160 | if (PSA_SUCCESS == r) { |
| 161 | r = psa_adac_update_context(crt, crt_size, context); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return r; |
| 166 | } |
| 167 | |
| 168 | psa_status_t certificate_check(authentication_context_t *ctx, uint8_t *crt, size_t crt_size, int is_root) { |
| 169 | psa_status_t r = psa_adac_certificate_check(crt, crt_size, &(ctx->rotpk_ctx), &(ctx->context), is_root); |
| 170 | if (PSA_SUCCESS != r) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 171 | PSA_ADAC_LOG_ERR("auth", "Error validating %s\r\n", is_root ? "root certificate" : "certificate"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 172 | } else { |
| 173 | r = psa_adac_platform_check_certificate(crt, crt_size); |
| 174 | if (PSA_SUCCESS != r) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 175 | PSA_ADAC_LOG_ERR("auth", "Certificate rejected by platform\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | return r; |
| 180 | } |
| 181 | |
| 182 | psa_status_t authentication_crt(authentication_context_t *auth_ctx, uint8_t *crt, size_t crt_size) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 183 | psa_status_t r = ADAC_FAILURE; |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 184 | certificate_header_t *header = (certificate_header_t *) crt; |
| 185 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 186 | /* Check format version before reading any other fields. */ |
| 187 | if (header->format_version.major != ADAC_CERT_MAJOR) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 188 | PSA_ADAC_LOG_ERR("auth", "Unsupported certificate major version\r\n"); |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 189 | return r; |
| 190 | } |
| 191 | |
| 192 | if (header->role == ADAC_CRT_ROLE_ROOT) { |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 193 | if ((auth_ctx->state != AUTH_CHALLENGE) && (auth_ctx->state != AUTH_ROT_META)) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 194 | PSA_ADAC_LOG_ERR("auth", "State inconsistent with receiving a root certificate\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 195 | |
| 196 | } else if (PSA_SUCCESS == certificate_check(auth_ctx, crt, crt_size, 1)) { |
| 197 | auth_ctx->state = AUTH_ROOT; |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 198 | r = ADAC_NEED_MORE_DATA; |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 199 | |
| 200 | for (size_t i = 0; i < sizeof(auth_ctx->permissions_mask); i++) { |
| 201 | auth_ctx->permissions_mask[i] &= header->permissions_mask[i]; |
| 202 | } |
| 203 | } else { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 204 | r = ADAC_FAILURE; /* [misra-c2012-15.7] */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 205 | } |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 206 | } else if ((header->role == ADAC_CRT_ROLE_INT) || (header->role == ADAC_CRT_ROLE_LEAF)) { |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 207 | if ((auth_ctx->state != AUTH_ROOT) && (auth_ctx->state != AUTH_CHAIN)) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 208 | PSA_ADAC_LOG_ERR("auth", "State inconsistent with receiving an intermediate or leaf certificate\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 209 | } else if (PSA_SUCCESS == certificate_check(auth_ctx, crt, crt_size, 0)) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 210 | auth_ctx->state = (header->role == ADAC_CRT_ROLE_INT) ? AUTH_CHAIN : AUTH_LEAF; |
| 211 | r = ADAC_NEED_MORE_DATA; |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 212 | |
| 213 | for (size_t i = 0; i < sizeof(auth_ctx->permissions_mask); i++) { |
| 214 | auth_ctx->permissions_mask[i] &= header->permissions_mask[i]; |
| 215 | } |
| 216 | } else { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 217 | r = ADAC_FAILURE; /* [misra-c2012-15.7] */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 218 | } |
| 219 | } else { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 220 | PSA_ADAC_LOG_ERR("auth_crt", "Inconsistent certificate role\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 223 | if (r == ADAC_FAILURE) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 224 | PSA_ADAC_LOG_ERR("auth_crt", "Authentication failure\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 225 | auth_ctx->state = AUTH_FAILURE; |
| 226 | } |
| 227 | return r; |
| 228 | } |
| 229 | |
| 230 | psa_status_t authentication_token(authentication_context_t *auth_ctx, uint8_t *token, size_t token_size) { |
| 231 | size_t sig_size; |
| 232 | size_t body_size; |
| 233 | size_t tbs_size; |
| 234 | psa_algorithm_t hash_algo; |
| 235 | psa_algorithm_t sig_algo; |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 236 | psa_status_t r = ADAC_FAILURE; |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 237 | uint8_t *sig; |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 238 | token_header_t *header = (token_header_t *) token; |
| 239 | |
| 240 | /* Check format version before reading any other fields. */ |
| 241 | if (header->format_version.major != ADAC_TOKEN_MAJOR) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 242 | PSA_ADAC_LOG_ERR("auth", "Unsupported token major version\r\n"); |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 243 | return r; |
| 244 | } |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 245 | |
| 246 | if ((auth_ctx->state != AUTH_ROOT) && (auth_ctx->state != AUTH_LEAF)) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 247 | PSA_ADAC_LOG_ERR("auth", "State inconsistent with receiving a token\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 248 | } else if (PSA_SUCCESS != psa_adac_token_verify_info(token, token_size, &sig, &sig_size, &tbs_size, |
| 249 | &body_size, &hash_algo, &sig_algo)) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 250 | PSA_ADAC_LOG_ERR("auth", "Error checking token\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 251 | } else if (PSA_SUCCESS != psa_adac_platform_check_token(token, token_size)) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 252 | PSA_ADAC_LOG_ERR("auth", "Token rejected by platform\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 253 | } else if (PSA_SUCCESS != |
| 254 | psa_adac_verify_token_signature(token, token_size, auth_ctx->challenge.challenge_vector, |
| 255 | sizeof(auth_ctx->challenge.challenge_vector), |
| 256 | auth_ctx->context.key_type, auth_ctx->context.content, |
| 257 | auth_ctx->context.size)) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 258 | PSA_ADAC_LOG_ERR("auth", "Invalid token signature\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 259 | } else { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 260 | PSA_ADAC_LOG_INFO("auth", "Authentication successful\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 261 | for (size_t i = 0; i < sizeof(auth_ctx->permissions_mask); i++) { |
| 262 | auth_ctx->permissions_mask[i] &= header->requested_permissions[i]; |
| 263 | } |
| 264 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 265 | /* TODO: report failures while applying permissions */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 266 | psa_adac_apply_permissions(auth_ctx->permissions_mask); |
| 267 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 268 | r = ADAC_SUCCESS; |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 269 | auth_ctx->state = AUTH_SUCCESS; |
| 270 | } |
| 271 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 272 | if (r == ADAC_FAILURE) { |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 273 | auth_ctx->state = AUTH_FAILURE; |
| 274 | } |
| 275 | return r; |
| 276 | } |
| 277 | |
| 278 | response_packet_t *authentication_response(authentication_context_t *auth_ctx, request_packet_t *request) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 279 | PSA_ADAC_LOG_DEBUG("auth", "Received Authentication Response (%d)\r\n", request->data_count * 4); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 280 | psa_tlv_t *fragment = (psa_tlv_t *) &request->data; |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 281 | psa_status_t r = ADAC_FAILURE; |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 282 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 283 | if (((request->data_count * 4) < sizeof(psa_tlv_t)) |
| 284 | || (((request->data_count * 4) - sizeof(psa_tlv_t)) < fragment->length_in_bytes)) { |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 285 | auth_ctx->state = AUTH_FAILURE; |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 286 | PSA_ADAC_LOG_DEBUG("auth", "Request size too small\r\n"); |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 287 | } else if (fragment->type_id == PSA_BINARY_CRT) { /* TODO: Add support for RoT Metadata fragment */ |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 288 | if ((auth_ctx->state != AUTH_CHALLENGE) && (auth_ctx->state != AUTH_ROT_META) && |
| 289 | (auth_ctx->state != AUTH_ROOT) && (auth_ctx->state != AUTH_CHAIN)) { |
| 290 | auth_ctx->state = AUTH_FAILURE; |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 291 | PSA_ADAC_LOG_DEBUG("auth", "State inconsistent with receiving a certificate\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 292 | } else if (fragment->length_in_bytes <= sizeof(certificate_header_t)) { |
| 293 | auth_ctx->state = AUTH_FAILURE; |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 294 | PSA_ADAC_LOG_DEBUG("auth", "Size inconsistent with certificate\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 295 | } else { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 296 | PSA_ADAC_LOG_TRACE("auth", "Received a certificate\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 297 | r = authentication_crt(auth_ctx, fragment->value, fragment->length_in_bytes); |
| 298 | } |
| 299 | } else if (fragment->type_id == PSA_BINARY_TOKEN) { |
| 300 | if ((auth_ctx->state != AUTH_ROOT) && (auth_ctx->state != AUTH_LEAF)) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 301 | PSA_ADAC_LOG_DEBUG("auth", "State inconsistent with receiving a token\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 302 | } else if (fragment->length_in_bytes <= sizeof(token_header_t)) { |
| 303 | auth_ctx->state = AUTH_FAILURE; |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 304 | PSA_ADAC_LOG_DEBUG("auth", "Size inconsistent with token\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 305 | } else { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 306 | PSA_ADAC_LOG_TRACE("auth", "Received a token\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 307 | r = authentication_token(auth_ctx, fragment->value, fragment->length_in_bytes); |
| 308 | } |
| 309 | } else { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 310 | PSA_ADAC_LOG_WARN("auth", "Received neither a certificate nor a token\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | (void) authenticator_request_packet_release(auth_ctx, request); |
| 314 | return authenticator_response_packet_build(auth_ctx, r, NULL, 0); |
| 315 | } |
| 316 | |
| 317 | void authentication_context_init(authentication_context_t *auth_ctx, uint8_t *buffer, size_t size, |
| 318 | psa_algorithm_t rotpk_algo, uint8_t **rotpk, size_t *rotpk_size, |
| 319 | uint8_t *rotpk_type, size_t rotpk_count) { |
| 320 | for (size_t i = 0UL; i < sizeof(auth_ctx->permissions_mask); i++) { |
| 321 | auth_ctx->permissions_mask[i] = 0xFF; |
| 322 | } |
| 323 | |
| 324 | auth_ctx->rotpk_ctx.rotpk = rotpk; |
| 325 | auth_ctx->rotpk_ctx.rotpk_algo = rotpk_algo; |
| 326 | auth_ctx->rotpk_ctx.rotpk_size = rotpk_size; |
| 327 | auth_ctx->rotpk_ctx.rotpk_type = rotpk_type; |
| 328 | auth_ctx->rotpk_ctx.rotpk_count = rotpk_count; |
| 329 | |
| 330 | auth_ctx->context.content = buffer; |
| 331 | auth_ctx->context.max = size; |
| 332 | auth_ctx->context.size = 0; |
| 333 | |
| 334 | auth_ctx->state = AUTH_INIT; |
| 335 | } |
| 336 | |
Maulik Patel | a0a2246 | 2023-02-02 11:24:48 +0000 | [diff] [blame] | 337 | static response_packet_t *authentication_change_lcs(authentication_context_t *auth_ctx, request_packet_t *request) |
| 338 | { |
| 339 | adac_status_t status; |
| 340 | (void) authenticator_request_packet_release(auth_ctx, request); |
| 341 | psa_tlv_t *fragment = (psa_tlv_t *) &request->data; |
| 342 | |
| 343 | if (((request->data_count * 4) < sizeof(psa_tlv_t)) |
| 344 | || (((request->data_count * 4) - sizeof(psa_tlv_t)) < fragment->length_in_bytes)) { |
| 345 | auth_ctx->state = AUTH_FAILURE; |
| 346 | status = ADAC_INVALID_PARAMETERS; |
| 347 | } else { |
| 348 | status = psa_adac_change_life_cycle_state(fragment->value, fragment->length_in_bytes); |
| 349 | } |
| 350 | |
| 351 | return authenticator_response_packet_build(auth_ctx, status, NULL, 0); |
| 352 | } |
| 353 | |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 354 | int authentication_handle(authentication_context_t *auth_ctx) { |
| 355 | int done = 0; |
| 356 | request_packet_t *request; |
| 357 | |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 358 | PSA_ADAC_LOG_INFO("auth", "Starting authentication loop\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 359 | while (done == 0) { |
| 360 | request = authenticator_request_packet_receive(auth_ctx); |
| 361 | if (NULL == request) { |
| 362 | break; |
| 363 | } |
| 364 | |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 365 | PSA_ADAC_LOG_DEBUG("auth", "Receiving request %x\r\n", request->command); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 366 | |
| 367 | int ret; |
| 368 | response_packet_t *response; |
| 369 | switch (request->command) { |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 370 | case ADAC_DISCOVERY_CMD: |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 371 | response = authentication_discovery(auth_ctx, request); |
| 372 | ret = authenticator_send_response(auth_ctx, response); |
| 373 | break; |
| 374 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 375 | case ADAC_AUTH_START_CMD: |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 376 | response = authentication_start(auth_ctx, request); |
| 377 | ret = authenticator_send_response(auth_ctx, response); |
| 378 | break; |
| 379 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 380 | case ADAC_AUTH_RESPONSE_CMD: |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 381 | response = authentication_response(auth_ctx, request); |
| 382 | ret = authenticator_send_response(auth_ctx, response); |
| 383 | break; |
| 384 | |
Maulik Patel | 381aa15 | 2025-05-21 09:57:15 +0100 | [diff] [blame^] | 385 | /* Send success status and terminate command loop. */ |
| 386 | case ADAC_CLOSE_SESSION_CMD: |
| 387 | PSA_ADAC_LOG_DEBUG("auth", "Terminate session \r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 388 | (void) authenticator_request_packet_release(auth_ctx, request); |
Maulik Patel | 381aa15 | 2025-05-21 09:57:15 +0100 | [diff] [blame^] | 389 | psa_adac_close_session(); |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 390 | response = authenticator_response_packet_build(auth_ctx, ADAC_SUCCESS, NULL, 0); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 391 | ret = authenticator_send_response(auth_ctx, response); |
Maulik Patel | 381aa15 | 2025-05-21 09:57:15 +0100 | [diff] [blame^] | 392 | done = 1; |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 393 | break; |
| 394 | |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 395 | /* Send success status and terminate command loop. */ |
Maulik Patel | 040ea17 | 2025-05-19 14:26:43 +0100 | [diff] [blame] | 396 | case ADAC_RESUME_CMD: |
| 397 | PSA_ADAC_LOG_DEBUG("auth", "Resuming after ADAC session\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 398 | (void) authenticator_request_packet_release(auth_ctx, request); |
Maulik Patel | e056d21 | 2025-05-19 14:46:40 +0100 | [diff] [blame] | 399 | psa_adac_resume(); |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 400 | response = authenticator_response_packet_build(auth_ctx, ADAC_SUCCESS, NULL, 0); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 401 | ret = authenticator_send_response(auth_ctx, response); |
| 402 | done = 1; |
| 403 | break; |
| 404 | |
Maulik Patel | 381aa15 | 2025-05-21 09:57:15 +0100 | [diff] [blame^] | 405 | /* Send success status but otherwise do nothing. */ |
| 406 | case ADAC_LOCK_DEBUG_CMD: |
| 407 | PSA_ADAC_LOG_DEBUG("auth", "Lock debug\r\n"); |
| 408 | (void) authenticator_request_packet_release(auth_ctx, request); |
| 409 | psa_adac_platform_lock(); |
| 410 | response = authenticator_response_packet_build(auth_ctx, ADAC_SUCCESS, NULL, 0); |
| 411 | ret = authenticator_send_response(auth_ctx, response); |
| 412 | break; |
| 413 | |
Maulik Patel | d551100 | 2025-05-19 14:17:28 +0100 | [diff] [blame] | 414 | case ADAC_LCS_CHANGE: |
Maulik Patel | a0a2246 | 2023-02-02 11:24:48 +0000 | [diff] [blame] | 415 | PSA_ADAC_LOG_DEBUG("auth", "Change LCS \n"); |
| 416 | response = authentication_change_lcs(auth_ctx, request); |
| 417 | ret = authenticator_send_response(auth_ctx, response); |
| 418 | break; |
| 419 | |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 420 | default: |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 421 | PSA_ADAC_LOG_ERR("auth", "Unknown command: %04x\r\n", request->command); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 422 | (void) authenticator_request_packet_release(auth_ctx, request); |
Maulik Patel | 38544dc | 2022-11-29 15:02:58 +0000 | [diff] [blame] | 423 | response = authenticator_response_packet_build(auth_ctx, ADAC_INVALID_COMMAND, NULL, 0); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 424 | ret = authenticator_send_response(auth_ctx, response); |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | if (ret != 0) { |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 429 | PSA_ADAC_LOG_ERR("auth", "Error sending response: %04x\r\n", ret); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if ((auth_ctx->state == AUTH_SUCCESS) || (auth_ctx->state == AUTH_FAILURE)) { |
| 433 | done = 1; |
| 434 | } |
| 435 | } |
| 436 | |
Maulik Patel | 1d6c0b2 | 2022-12-20 15:14:53 +0000 | [diff] [blame] | 437 | PSA_ADAC_LOG_INFO("auth", "Ending authentication loop\r\n"); |
Satish Kumar | 427923c | 2021-10-05 07:21:53 +0100 | [diff] [blame] | 438 | |
| 439 | return done; |
| 440 | } |