| Gilles Peskine | 0d980b8 | 2021-01-05 23:34:27 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Common source code for SSL test programs. This file is included by | 
|  | 3 | *  both ssl_client2.c and ssl_server2.c and is intended for source | 
|  | 4 | *  code that is textually identical in both programs, but that cannot be | 
|  | 5 | *  compiled separately because it refers to types or macros that are | 
|  | 6 | *  different in the two programs, or because it would have an incomplete | 
|  | 7 | *  type. | 
|  | 8 | * | 
|  | 9 | *  This file is meant to be #include'd and cannot be compiled separately. | 
|  | 10 | * | 
|  | 11 | *  Copyright The Mbed TLS Contributors | 
|  | 12 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 13 | * | 
|  | 14 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 15 | *  not use this file except in compliance with the License. | 
|  | 16 | *  You may obtain a copy of the License at | 
|  | 17 | * | 
|  | 18 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 19 | * | 
|  | 20 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 21 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 22 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 23 | *  See the License for the specific language governing permissions and | 
|  | 24 | *  limitations under the License. | 
|  | 25 | */ | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 26 |  | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 27 | void eap_tls_key_derivation( void *p_expkey, | 
|  | 28 | mbedtls_ssl_key_export_type secret_type, | 
|  | 29 | const unsigned char *secret, | 
|  | 30 | size_t secret_len, | 
|  | 31 | const unsigned char client_random[32], | 
|  | 32 | const unsigned char server_random[32], | 
|  | 33 | mbedtls_tls_prf_types tls_prf_type ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 34 | { | 
|  | 35 | eap_tls_keys *keys = (eap_tls_keys *)p_expkey; | 
|  | 36 |  | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 37 | /* We're only interested in the TLS 1.2 master secret */ | 
|  | 38 | if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET ) | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 39 | return; | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 40 | if( secret_len != sizeof( keys->master_secret ) ) | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 41 | return; | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 42 |  | 
|  | 43 | memcpy( keys->master_secret, secret, sizeof( keys->master_secret ) ); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 44 | memcpy( keys->randbytes, client_random, 32 ); | 
|  | 45 | memcpy( keys->randbytes + 32, server_random, 32 ); | 
|  | 46 | keys->tls_prf_type = tls_prf_type; | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 49 | void nss_keylog_export( void *p_expkey, | 
|  | 50 | mbedtls_ssl_key_export_type secret_type, | 
|  | 51 | const unsigned char *secret, | 
|  | 52 | size_t secret_len, | 
|  | 53 | const unsigned char client_random[32], | 
|  | 54 | const unsigned char server_random[32], | 
|  | 55 | mbedtls_tls_prf_types tls_prf_type ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 56 | { | 
|  | 57 | char nss_keylog_line[ 200 ]; | 
|  | 58 | size_t const client_random_len = 32; | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 59 | size_t len = 0; | 
|  | 60 | size_t j; | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 61 |  | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 62 | /* We're only interested in the TLS 1.2 master secret */ | 
|  | 63 | if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET ) | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 64 | return; | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 65 |  | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 66 | ((void) p_expkey); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 67 | ((void) server_random); | 
|  | 68 | ((void) tls_prf_type); | 
|  | 69 |  | 
|  | 70 | len += sprintf( nss_keylog_line + len, | 
|  | 71 | "%s", "CLIENT_RANDOM " ); | 
|  | 72 |  | 
|  | 73 | for( j = 0; j < client_random_len; j++ ) | 
|  | 74 | { | 
|  | 75 | len += sprintf( nss_keylog_line + len, | 
|  | 76 | "%02x", client_random[j] ); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | len += sprintf( nss_keylog_line + len, " " ); | 
|  | 80 |  | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 81 | for( j = 0; j < secret_len; j++ ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 82 | { | 
|  | 83 | len += sprintf( nss_keylog_line + len, | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 84 | "%02x", secret[j] ); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
|  | 87 | len += sprintf( nss_keylog_line + len, "\n" ); | 
|  | 88 | nss_keylog_line[ len ] = '\0'; | 
|  | 89 |  | 
|  | 90 | mbedtls_printf( "\n" ); | 
|  | 91 | mbedtls_printf( "---------------- NSS KEYLOG -----------------\n" ); | 
|  | 92 | mbedtls_printf( "%s", nss_keylog_line ); | 
|  | 93 | mbedtls_printf( "---------------------------------------------\n" ); | 
|  | 94 |  | 
|  | 95 | if( opt.nss_keylog_file != NULL ) | 
|  | 96 | { | 
|  | 97 | FILE *f; | 
|  | 98 |  | 
|  | 99 | if( ( f = fopen( opt.nss_keylog_file, "a" ) ) == NULL ) | 
|  | 100 | { | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 101 | goto exit; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | if( fwrite( nss_keylog_line, 1, len, f ) != len ) | 
|  | 105 | { | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 106 | fclose( f ); | 
|  | 107 | goto exit; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | fclose( f ); | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | exit: | 
|  | 114 | mbedtls_platform_zeroize( nss_keylog_line, | 
|  | 115 | sizeof( nss_keylog_line ) ); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | #if defined( MBEDTLS_SSL_DTLS_SRTP ) | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 119 | void dtls_srtp_key_derivation( void *p_expkey, | 
|  | 120 | mbedtls_ssl_key_export_type secret_type, | 
|  | 121 | const unsigned char *secret, | 
|  | 122 | size_t secret_len, | 
|  | 123 | const unsigned char client_random[32], | 
|  | 124 | const unsigned char server_random[32], | 
|  | 125 | mbedtls_tls_prf_types tls_prf_type ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 126 | { | 
|  | 127 | dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey; | 
|  | 128 |  | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 129 | /* We're only interested in the TLS 1.2 master secret */ | 
|  | 130 | if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET ) | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 131 | return; | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 132 | if( secret_len != sizeof( keys->master_secret ) ) | 
| Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 133 | return; | 
| Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 134 |  | 
|  | 135 | memcpy( keys->master_secret, secret, sizeof( keys->master_secret ) ); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 136 | memcpy( keys->randbytes, client_random, 32 ); | 
|  | 137 | memcpy( keys->randbytes + 32, server_random, 32 ); | 
|  | 138 | keys->tls_prf_type = tls_prf_type; | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 139 | } | 
|  | 140 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 141 |  | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 142 | int ssl_check_record( mbedtls_ssl_context const *ssl, | 
|  | 143 | unsigned char const *buf, size_t len ) | 
|  | 144 | { | 
| Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 145 | int my_ret = 0, ret_cr1, ret_cr2; | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 146 | unsigned char *tmp_buf; | 
|  | 147 |  | 
|  | 148 | /* Record checking may modify the input buffer, | 
|  | 149 | * so make a copy. */ | 
|  | 150 | tmp_buf = mbedtls_calloc( 1, len ); | 
|  | 151 | if( tmp_buf == NULL ) | 
|  | 152 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 153 | memcpy( tmp_buf, buf, len ); | 
|  | 154 |  | 
| Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 155 | ret_cr1 = mbedtls_ssl_check_record( ssl, tmp_buf, len ); | 
|  | 156 | if( ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 157 | { | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 158 | /* Test-only: Make sure that mbedtls_ssl_check_record() | 
|  | 159 | *            doesn't alter state. */ | 
|  | 160 | memcpy( tmp_buf, buf, len ); /* Restore buffer */ | 
| Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 161 | ret_cr2 = mbedtls_ssl_check_record( ssl, tmp_buf, len ); | 
|  | 162 | if( ret_cr2 != ret_cr1 ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 163 | { | 
|  | 164 | mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" ); | 
| Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 165 | my_ret = -1; | 
| Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 166 | goto cleanup; | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
| Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 169 | switch( ret_cr1 ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 170 | { | 
|  | 171 | case 0: | 
|  | 172 | break; | 
|  | 173 |  | 
|  | 174 | case MBEDTLS_ERR_SSL_INVALID_RECORD: | 
|  | 175 | if( opt.debug_level > 1 ) | 
|  | 176 | mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" ); | 
|  | 177 | break; | 
|  | 178 |  | 
|  | 179 | case MBEDTLS_ERR_SSL_INVALID_MAC: | 
|  | 180 | if( opt.debug_level > 1 ) | 
|  | 181 | mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" ); | 
|  | 182 | break; | 
|  | 183 |  | 
|  | 184 | case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD: | 
|  | 185 | if( opt.debug_level > 1 ) | 
|  | 186 | mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" ); | 
|  | 187 | break; | 
|  | 188 |  | 
|  | 189 | default: | 
| Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 190 | mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret_cr1 ); | 
|  | 191 | my_ret = -1; | 
| Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 192 | goto cleanup; | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
|  | 195 | /* Regardless of the outcome, forward the record to the stack. */ | 
|  | 196 | } | 
|  | 197 |  | 
| Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 198 | cleanup: | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 199 | mbedtls_free( tmp_buf ); | 
|  | 200 |  | 
| Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 201 | return( my_ret ); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 202 | } | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 203 |  | 
|  | 204 | int recv_cb( void *ctx, unsigned char *buf, size_t len ) | 
|  | 205 | { | 
|  | 206 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; | 
|  | 207 | size_t recv_len; | 
|  | 208 | int ret; | 
|  | 209 |  | 
|  | 210 | if( opt.nbio == 2 ) | 
|  | 211 | ret = delayed_recv( io_ctx->net, buf, len ); | 
|  | 212 | else | 
|  | 213 | ret = mbedtls_net_recv( io_ctx->net, buf, len ); | 
|  | 214 | if( ret < 0 ) | 
|  | 215 | return( ret ); | 
|  | 216 | recv_len = (size_t) ret; | 
|  | 217 |  | 
|  | 218 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 219 | { | 
|  | 220 | /* Here's the place to do any datagram/record checking | 
|  | 221 | * in between receiving the packet from the underlying | 
|  | 222 | * transport and passing it on to the TLS stack. */ | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 223 | if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) | 
|  | 224 | return( -1 ); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | return( (int) recv_len ); | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len, | 
|  | 231 | uint32_t timeout ) | 
|  | 232 | { | 
|  | 233 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; | 
|  | 234 | int ret; | 
|  | 235 | size_t recv_len; | 
|  | 236 |  | 
|  | 237 | ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout ); | 
|  | 238 | if( ret < 0 ) | 
|  | 239 | return( ret ); | 
|  | 240 | recv_len = (size_t) ret; | 
|  | 241 |  | 
|  | 242 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 243 | { | 
|  | 244 | /* Here's the place to do any datagram/record checking | 
|  | 245 | * in between receiving the packet from the underlying | 
|  | 246 | * transport and passing it on to the TLS stack. */ | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 247 | if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) | 
|  | 248 | return( -1 ); | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
|  | 251 | return( (int) recv_len ); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | int send_cb( void *ctx, unsigned char const *buf, size_t len ) | 
|  | 255 | { | 
|  | 256 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; | 
|  | 257 |  | 
|  | 258 | if( opt.nbio == 2 ) | 
|  | 259 | return( delayed_send( io_ctx->net, buf, len ) ); | 
|  | 260 |  | 
|  | 261 | return( mbedtls_net_send( io_ctx->net, buf, len ) ); | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 265 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_RSA_C) | 
|  | 266 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA), \ | 
|  | 267 | (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA), | 
|  | 268 | #elif defined(MBEDTLS_ECDSA_C) | 
|  | 269 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA), | 
|  | 270 | #elif defined(MBEDTLS_RSA_C) | 
|  | 271 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA), | 
|  | 272 | #else | 
|  | 273 | #define MBEDTLS_SSL_SIG_ALG( hash ) | 
|  | 274 | #endif | 
|  | 275 | uint16_t ssl_sig_algs_for_test[] = { | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 276 | #if defined(MBEDTLS_SHA512_C) | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 277 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 278 | #endif | 
|  | 279 | #if defined(MBEDTLS_SHA384_C) | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 280 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 281 | #endif | 
|  | 282 | #if defined(MBEDTLS_SHA256_C) | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 283 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 284 | #endif | 
|  | 285 | #if defined(MBEDTLS_SHA224_C) | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 286 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA224 ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 287 | #endif | 
|  | 288 | #if defined(MBEDTLS_SHA1_C) | 
|  | 289 | /* Allow SHA-1 as we use it extensively in tests. */ | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 290 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA1 ) | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 291 | #endif | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 292 | MBEDTLS_TLS1_3_SIG_NONE | 
| Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 293 | }; | 
|  | 294 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 295 |  | 
|  | 296 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 297 | /** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function | 
|  | 298 | *  for more info. | 
|  | 299 | */ | 
|  | 300 | int x509_crt_verify_info( char *buf, size_t size, const char *prefix, | 
|  | 301 | uint32_t flags ) | 
|  | 302 | { | 
| Chris Jones | fa1f904 | 2021-04-28 10:04:05 +0100 | [diff] [blame] | 303 | #if !defined(MBEDTLS_X509_REMOVE_INFO) | 
| Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 304 | return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) ); | 
|  | 305 |  | 
|  | 306 | #else /* !MBEDTLS_X509_REMOVE_INFO */ | 
|  | 307 | int ret; | 
|  | 308 | char *p = buf; | 
|  | 309 | size_t n = size; | 
|  | 310 |  | 
|  | 311 | #define X509_CRT_ERROR_INFO( err, err_str, info )                      \ | 
|  | 312 | if( ( flags & err ) != 0 )                                         \ | 
|  | 313 | {                                                                  \ | 
|  | 314 | ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info );        \ | 
|  | 315 | MBEDTLS_X509_SAFE_SNPRINTF;                                    \ | 
|  | 316 | flags ^= err;                                                  \ | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | MBEDTLS_X509_CRT_ERROR_INFO_LIST | 
|  | 320 | #undef X509_CRT_ERROR_INFO | 
|  | 321 |  | 
|  | 322 | if( flags != 0 ) | 
|  | 323 | { | 
|  | 324 | ret = mbedtls_snprintf( p, n, "%sUnknown reason " | 
|  | 325 | "(this should not happen)\n", prefix ); | 
|  | 326 | MBEDTLS_X509_SAFE_SNPRINTF; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | return( (int) ( size - n ) ); | 
|  | 330 | #endif /* MBEDTLS_X509_REMOVE_INFO */ | 
|  | 331 | } | 
|  | 332 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |