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 | |
Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 104 | /* Ensure no stdio buffering of secrets, as such buffers cannot be |
| 105 | * wiped. */ |
| 106 | mbedtls_setbuf( f, NULL ); |
| 107 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 108 | if( fwrite( nss_keylog_line, 1, len, f ) != len ) |
| 109 | { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 110 | fclose( f ); |
| 111 | goto exit; |
| 112 | } |
| 113 | |
| 114 | fclose( f ); |
| 115 | } |
| 116 | |
| 117 | exit: |
| 118 | mbedtls_platform_zeroize( nss_keylog_line, |
| 119 | sizeof( nss_keylog_line ) ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | #if defined( MBEDTLS_SSL_DTLS_SRTP ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 123 | void dtls_srtp_key_derivation( void *p_expkey, |
| 124 | mbedtls_ssl_key_export_type secret_type, |
| 125 | const unsigned char *secret, |
| 126 | size_t secret_len, |
| 127 | const unsigned char client_random[32], |
| 128 | const unsigned char server_random[32], |
| 129 | mbedtls_tls_prf_types tls_prf_type ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 130 | { |
| 131 | dtls_srtp_keys *keys = (dtls_srtp_keys *)p_expkey; |
| 132 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 133 | /* We're only interested in the TLS 1.2 master secret */ |
| 134 | if( secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 135 | return; |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 136 | if( secret_len != sizeof( keys->master_secret ) ) |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 137 | return; |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 138 | |
| 139 | memcpy( keys->master_secret, secret, sizeof( keys->master_secret ) ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 140 | memcpy( keys->randbytes, client_random, 32 ); |
| 141 | memcpy( keys->randbytes + 32, server_random, 32 ); |
| 142 | keys->tls_prf_type = tls_prf_type; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 143 | } |
| 144 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 145 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 146 | int ssl_check_record( mbedtls_ssl_context const *ssl, |
| 147 | unsigned char const *buf, size_t len ) |
| 148 | { |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 149 | int my_ret = 0, ret_cr1, ret_cr2; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 150 | unsigned char *tmp_buf; |
| 151 | |
| 152 | /* Record checking may modify the input buffer, |
| 153 | * so make a copy. */ |
| 154 | tmp_buf = mbedtls_calloc( 1, len ); |
| 155 | if( tmp_buf == NULL ) |
| 156 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 157 | memcpy( tmp_buf, buf, len ); |
| 158 | |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 159 | ret_cr1 = mbedtls_ssl_check_record( ssl, tmp_buf, len ); |
| 160 | if( ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 161 | { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 162 | /* Test-only: Make sure that mbedtls_ssl_check_record() |
| 163 | * doesn't alter state. */ |
| 164 | memcpy( tmp_buf, buf, len ); /* Restore buffer */ |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 165 | ret_cr2 = mbedtls_ssl_check_record( ssl, tmp_buf, len ); |
| 166 | if( ret_cr2 != ret_cr1 ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 167 | { |
| 168 | mbedtls_printf( "mbedtls_ssl_check_record() returned inconsistent results.\n" ); |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 169 | my_ret = -1; |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 170 | goto cleanup; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 173 | switch( ret_cr1 ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 174 | { |
| 175 | case 0: |
| 176 | break; |
| 177 | |
| 178 | case MBEDTLS_ERR_SSL_INVALID_RECORD: |
| 179 | if( opt.debug_level > 1 ) |
| 180 | mbedtls_printf( "mbedtls_ssl_check_record() detected invalid record.\n" ); |
| 181 | break; |
| 182 | |
| 183 | case MBEDTLS_ERR_SSL_INVALID_MAC: |
| 184 | if( opt.debug_level > 1 ) |
| 185 | mbedtls_printf( "mbedtls_ssl_check_record() detected unauthentic record.\n" ); |
| 186 | break; |
| 187 | |
| 188 | case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD: |
| 189 | if( opt.debug_level > 1 ) |
| 190 | mbedtls_printf( "mbedtls_ssl_check_record() detected unexpected record.\n" ); |
| 191 | break; |
| 192 | |
| 193 | default: |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 194 | mbedtls_printf( "mbedtls_ssl_check_record() failed fatally with -%#04x.\n", (unsigned int) -ret_cr1 ); |
| 195 | my_ret = -1; |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 196 | goto cleanup; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* Regardless of the outcome, forward the record to the stack. */ |
| 200 | } |
| 201 | |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 202 | cleanup: |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 203 | mbedtls_free( tmp_buf ); |
| 204 | |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 205 | return( my_ret ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 206 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 207 | |
| 208 | int recv_cb( void *ctx, unsigned char *buf, size_t len ) |
| 209 | { |
| 210 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; |
| 211 | size_t recv_len; |
| 212 | int ret; |
| 213 | |
| 214 | if( opt.nbio == 2 ) |
| 215 | ret = delayed_recv( io_ctx->net, buf, len ); |
| 216 | else |
| 217 | ret = mbedtls_net_recv( io_ctx->net, buf, len ); |
| 218 | if( ret < 0 ) |
| 219 | return( ret ); |
| 220 | recv_len = (size_t) ret; |
| 221 | |
| 222 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 223 | { |
| 224 | /* Here's the place to do any datagram/record checking |
| 225 | * in between receiving the packet from the underlying |
| 226 | * transport and passing it on to the TLS stack. */ |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 227 | if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) |
| 228 | return( -1 ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return( (int) recv_len ); |
| 232 | } |
| 233 | |
| 234 | int recv_timeout_cb( void *ctx, unsigned char *buf, size_t len, |
| 235 | uint32_t timeout ) |
| 236 | { |
| 237 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; |
| 238 | int ret; |
| 239 | size_t recv_len; |
| 240 | |
| 241 | ret = mbedtls_net_recv_timeout( io_ctx->net, buf, len, timeout ); |
| 242 | if( ret < 0 ) |
| 243 | return( ret ); |
| 244 | recv_len = (size_t) ret; |
| 245 | |
| 246 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 247 | { |
| 248 | /* Here's the place to do any datagram/record checking |
| 249 | * in between receiving the packet from the underlying |
| 250 | * transport and passing it on to the TLS stack. */ |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 251 | if( ssl_check_record( io_ctx->ssl, buf, recv_len ) != 0 ) |
| 252 | return( -1 ); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | return( (int) recv_len ); |
| 256 | } |
| 257 | |
| 258 | int send_cb( void *ctx, unsigned char const *buf, size_t len ) |
| 259 | { |
| 260 | io_ctx_t *io_ctx = (io_ctx_t*) ctx; |
| 261 | |
| 262 | if( opt.nbio == 2 ) |
| 263 | return( delayed_send( io_ctx->net, buf, len ) ); |
| 264 | |
| 265 | return( mbedtls_net_send( io_ctx->net, buf, len ) ); |
| 266 | } |
| 267 | |
| 268 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 269 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_RSA_C) |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 271 | /* |
Jerry Yu | a1255e6 | 2022-06-24 10:10:47 +0800 | [diff] [blame] | 272 | * When GnuTLS/Openssl server is configured in TLS 1.2 mode with a certificate |
| 273 | * declaring an RSA public key and Mbed TLS is configured in hybrid mode, if |
| 274 | * `rsa_pss_rsae_*` algorithms are before `rsa_pkcs1_*` ones in this list then |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 275 | * the GnuTLS/Openssl server chooses an `rsa_pss_rsae_*` signature algorithm |
| 276 | * for its signature in the key exchange message. As Mbed TLS 1.2 does not |
Jerry Yu | a1255e6 | 2022-06-24 10:10:47 +0800 | [diff] [blame] | 277 | * support them, the handshake fails. |
Jerry Yu | 3896ac6 | 2022-06-19 17:16:38 +0800 | [diff] [blame] | 278 | */ |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 279 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA), \ |
Jerry Yu | 3896ac6 | 2022-06-19 17:16:38 +0800 | [diff] [blame] | 280 | (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA), \ |
| 281 | ( 0x800 | hash ), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 282 | #else |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 283 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA), \ |
| 284 | (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 285 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 286 | #elif defined(MBEDTLS_ECDSA_C) |
| 287 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA), |
| 288 | #elif defined(MBEDTLS_RSA_C) |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 289 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 3896ac6 | 2022-06-19 17:16:38 +0800 | [diff] [blame] | 290 | /* See above */ |
| 291 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA), \ |
| 292 | ( 0x800 | hash ), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 293 | #else |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 294 | #define MBEDTLS_SSL_SIG_ALG( hash ) (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 295 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 296 | #else |
| 297 | #define MBEDTLS_SSL_SIG_ALG( hash ) |
| 298 | #endif |
Andrzej Kurek | 0bc834b | 2022-09-06 17:30:43 -0400 | [diff] [blame] | 299 | |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 300 | uint16_t ssl_sig_algs_for_test[] = { |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 302 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 303 | #endif |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 304 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 305 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 306 | #endif |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 307 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 308 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 309 | #endif |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 310 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 311 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA224 ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 312 | #endif |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 313 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 903c979 | 2022-06-16 16:55:31 +0200 | [diff] [blame] | 314 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Ronald Cron | ba65fbb | 2022-06-22 14:35:05 +0200 | [diff] [blame] | 315 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
Manuel Pégourié-Gonnard | e896705 | 2022-09-15 11:41:16 +0200 | [diff] [blame] | 316 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 317 | /* Allow SHA-1 as we use it extensively in tests. */ |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 318 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA1 ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 319 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 320 | MBEDTLS_TLS1_3_SIG_NONE |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 321 | }; |
| 322 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 323 | |
| 324 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 325 | /** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function |
| 326 | * for more info. |
| 327 | */ |
| 328 | int x509_crt_verify_info( char *buf, size_t size, const char *prefix, |
| 329 | uint32_t flags ) |
| 330 | { |
Chris Jones | fa1f904 | 2021-04-28 10:04:05 +0100 | [diff] [blame] | 331 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 332 | return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) ); |
| 333 | |
| 334 | #else /* !MBEDTLS_X509_REMOVE_INFO */ |
| 335 | int ret; |
| 336 | char *p = buf; |
| 337 | size_t n = size; |
| 338 | |
| 339 | #define X509_CRT_ERROR_INFO( err, err_str, info ) \ |
| 340 | if( ( flags & err ) != 0 ) \ |
| 341 | { \ |
| 342 | ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info ); \ |
| 343 | MBEDTLS_X509_SAFE_SNPRINTF; \ |
| 344 | flags ^= err; \ |
| 345 | } |
| 346 | |
| 347 | MBEDTLS_X509_CRT_ERROR_INFO_LIST |
| 348 | #undef X509_CRT_ERROR_INFO |
| 349 | |
| 350 | if( flags != 0 ) |
| 351 | { |
| 352 | ret = mbedtls_snprintf( p, n, "%sUnknown reason " |
| 353 | "(this should not happen)\n", prefix ); |
| 354 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 355 | } |
| 356 | |
| 357 | return( (int) ( size - n ) ); |
| 358 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
| 359 | } |
| 360 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Jerry Yu | 202919c | 2022-06-27 16:21:00 +0800 | [diff] [blame] | 361 | |
| 362 | void mbedtls_print_supported_sig_algs( void ) |
| 363 | { |
| 364 | mbedtls_printf( "supported signature algorithms:\n" ); |
| 365 | mbedtls_printf("\trsa_pkcs1_sha256 "); |
| 366 | mbedtls_printf("rsa_pkcs1_sha384 "); |
| 367 | mbedtls_printf("rsa_pkcs1_sha512\n"); |
| 368 | mbedtls_printf("\tecdsa_secp256r1_sha256 "); |
| 369 | mbedtls_printf("ecdsa_secp384r1_sha384 "); |
| 370 | mbedtls_printf("ecdsa_secp521r1_sha512\n"); |
| 371 | mbedtls_printf("\trsa_pss_rsae_sha256 "); |
| 372 | mbedtls_printf("rsa_pss_rsae_sha384 "); |
| 373 | mbedtls_printf("rsa_pss_rsae_sha512\n"); |
| 374 | mbedtls_printf("\trsa_pss_pss_sha256 "); |
| 375 | mbedtls_printf("rsa_pss_pss_sha384 "); |
| 376 | mbedtls_printf("rsa_pss_pss_sha512\n"); |
| 377 | mbedtls_printf("\ted25519 "); |
| 378 | mbedtls_printf("ed448 "); |
| 379 | mbedtls_printf("rsa_pkcs1_sha1 "); |
| 380 | mbedtls_printf("ecdsa_sha1\n"); |
| 381 | mbedtls_printf( "\n" ); |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 382 | } |