Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 1 | /* |
Gilles Peskine | 0d980b8 | 2021-01-05 23:34:27 +0100 | [diff] [blame] | 2 | * Common code library for SSL test programs. |
| 3 | * |
| 4 | * In addition to the functions in this file, there is shared source code |
| 5 | * that cannot be compiled separately in "ssl_test_common_source.c". |
Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 6 | * |
| 7 | * Copyright The Mbed TLS Contributors |
| 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
| 21 | */ |
| 22 | |
| 23 | #include "ssl_test_lib.h" |
| 24 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 25 | #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) |
| 26 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame^] | 27 | void my_debug( void *ctx, int level, |
| 28 | const char *file, int line, |
| 29 | const char *str ) |
| 30 | { |
| 31 | const char *p, *basename; |
| 32 | |
| 33 | /* Extract basename from file */ |
| 34 | for( p = basename = file; *p != '\0'; p++ ) |
| 35 | if( *p == '/' || *p == '\\' ) |
| 36 | basename = p + 1; |
| 37 | |
| 38 | mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", |
| 39 | basename, line, level, str ); |
| 40 | fflush( (FILE *) ctx ); |
| 41 | } |
| 42 | |
| 43 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) |
| 44 | { |
| 45 | (void) time; |
| 46 | return 0x5af2a056; |
| 47 | } |
| 48 | |
| 49 | int dummy_entropy( void *data, unsigned char *output, size_t len ) |
| 50 | { |
| 51 | size_t i; |
| 52 | int ret; |
| 53 | (void) data; |
| 54 | |
| 55 | ret = mbedtls_entropy_func( data, output, len ); |
| 56 | for( i = 0; i < len; i++ ) |
| 57 | { |
| 58 | //replace result with pseudo random |
| 59 | output[i] = (unsigned char) rand(); |
| 60 | } |
| 61 | return( ret ); |
| 62 | } |
| 63 | |
| 64 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 65 | int ca_callback( void *data, mbedtls_x509_crt const *child, |
| 66 | mbedtls_x509_crt **candidates ) |
| 67 | { |
| 68 | int ret = 0; |
| 69 | mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; |
| 70 | mbedtls_x509_crt *first; |
| 71 | |
| 72 | /* This is a test-only implementation of the CA callback |
| 73 | * which always returns the entire list of trusted certificates. |
| 74 | * Production implementations managing a large number of CAs |
| 75 | * should use an efficient presentation and lookup for the |
| 76 | * set of trusted certificates (such as a hashtable) and only |
| 77 | * return those trusted certificates which satisfy basic |
| 78 | * parental checks, such as the matching of child `Issuer` |
| 79 | * and parent `Subject` field or matching key identifiers. */ |
| 80 | ((void) child); |
| 81 | |
| 82 | first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 83 | if( first == NULL ) |
| 84 | { |
| 85 | ret = -1; |
| 86 | goto exit; |
| 87 | } |
| 88 | mbedtls_x509_crt_init( first ); |
| 89 | |
| 90 | if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) |
| 91 | { |
| 92 | ret = -1; |
| 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | while( ca->next != NULL ) |
| 97 | { |
| 98 | ca = ca->next; |
| 99 | if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) |
| 100 | { |
| 101 | ret = -1; |
| 102 | goto exit; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | exit: |
| 107 | |
| 108 | if( ret != 0 ) |
| 109 | { |
| 110 | mbedtls_x509_crt_free( first ); |
| 111 | mbedtls_free( first ); |
| 112 | first = NULL; |
| 113 | } |
| 114 | |
| 115 | *candidates = first; |
| 116 | return( ret ); |
| 117 | } |
| 118 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 119 | |
| 120 | int delayed_recv( void *ctx, unsigned char *buf, size_t len ) |
| 121 | { |
| 122 | static int first_try = 1; |
| 123 | int ret; |
| 124 | |
| 125 | if( first_try ) |
| 126 | { |
| 127 | first_try = 0; |
| 128 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
| 129 | } |
| 130 | |
| 131 | ret = mbedtls_net_recv( ctx, buf, len ); |
| 132 | if( ret != MBEDTLS_ERR_SSL_WANT_READ ) |
| 133 | first_try = 1; /* Next call will be a new operation */ |
| 134 | return( ret ); |
| 135 | } |
| 136 | |
| 137 | int delayed_send( void *ctx, const unsigned char *buf, size_t len ) |
| 138 | { |
| 139 | static int first_try = 1; |
| 140 | int ret; |
| 141 | |
| 142 | if( first_try ) |
| 143 | { |
| 144 | first_try = 0; |
| 145 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 146 | } |
| 147 | |
| 148 | ret = mbedtls_net_send( ctx, buf, len ); |
| 149 | if( ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 150 | first_try = 1; /* Next call will be a new operation */ |
| 151 | return( ret ); |
| 152 | } |
| 153 | |
| 154 | #if !defined(MBEDTLS_TIMING_C) |
| 155 | int idle( mbedtls_net_context *fd, |
| 156 | int idle_reason ) |
| 157 | #else |
| 158 | int idle( mbedtls_net_context *fd, |
| 159 | mbedtls_timing_delay_context *timer, |
| 160 | int idle_reason ) |
| 161 | #endif |
| 162 | { |
| 163 | int ret; |
| 164 | int poll_type = 0; |
| 165 | |
| 166 | if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 167 | poll_type = MBEDTLS_NET_POLL_WRITE; |
| 168 | else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ ) |
| 169 | poll_type = MBEDTLS_NET_POLL_READ; |
| 170 | #if !defined(MBEDTLS_TIMING_C) |
| 171 | else |
| 172 | return( 0 ); |
| 173 | #endif |
| 174 | |
| 175 | while( 1 ) |
| 176 | { |
| 177 | /* Check if timer has expired */ |
| 178 | #if defined(MBEDTLS_TIMING_C) |
| 179 | if( timer != NULL && |
| 180 | mbedtls_timing_get_delay( timer ) == 2 ) |
| 181 | { |
| 182 | break; |
| 183 | } |
| 184 | #endif /* MBEDTLS_TIMING_C */ |
| 185 | |
| 186 | /* Check if underlying transport became available */ |
| 187 | if( poll_type != 0 ) |
| 188 | { |
| 189 | ret = mbedtls_net_poll( fd, poll_type, 0 ); |
| 190 | if( ret < 0 ) |
| 191 | return( ret ); |
| 192 | if( ret == poll_type ) |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return( 0 ); |
| 198 | } |
| 199 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 200 | #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ |