Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client with certificate authentication |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Simon Butcher | d3138c3 | 2016-04-27 01:26:50 +0100 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #define mbedtls_time time |
| 34 | #define mbedtls_time_t time_t |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #define mbedtls_printf printf |
| 36 | #define mbedtls_fprintf fprintf |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #define mbedtls_snprintf snprintf |
Hanno Becker | d6d100b | 2019-03-30 06:27:43 +0000 | [diff] [blame] | 38 | #define mbedtls_calloc calloc |
| 39 | #define mbedtls_free free |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 40 | #define mbedtls_exit exit |
| 41 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 42 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 45 | #if !defined(MBEDTLS_ENTROPY_C) || \ |
| 46 | !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \ |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 47 | !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 48 | int main( void ) |
| 49 | { |
| 50 | mbedtls_printf("MBEDTLS_ENTROPY_C and/or " |
| 51 | "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or " |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 52 | "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n"); |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 53 | return( 0 ); |
| 54 | } |
| 55 | #else |
| 56 | |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 57 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 58 | #include "mbedtls/ssl.h" |
| 59 | #include "mbedtls/entropy.h" |
| 60 | #include "mbedtls/ctr_drbg.h" |
| 61 | #include "mbedtls/certs.h" |
| 62 | #include "mbedtls/x509.h" |
| 63 | #include "mbedtls/error.h" |
| 64 | #include "mbedtls/debug.h" |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 65 | #include "mbedtls/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | |
Hanno Becker | b2b468b | 2018-11-12 17:46:59 +0000 | [diff] [blame] | 67 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 68 | #include "psa/crypto.h" |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 69 | #include "mbedtls/psa_util.h" |
Hanno Becker | b2b468b | 2018-11-12 17:46:59 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 72 | #include <stdio.h> |
| 73 | #include <stdlib.h> |
| 74 | #include <string.h> |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 75 | |
Hanno Becker | e4ad3e8 | 2017-09-18 15:05:46 +0100 | [diff] [blame] | 76 | #define MAX_REQUEST_SIZE 20000 |
| 77 | #define MAX_REQUEST_SIZE_STR "20000" |
| 78 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 79 | #define DFL_SERVER_NAME "localhost" |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 80 | #define DFL_SERVER_ADDR NULL |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 81 | #define DFL_SERVER_PORT "4433" |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 82 | #define DFL_REQUEST_PAGE "/" |
Manuel Pégourié-Gonnard | dea29c5 | 2014-06-18 13:07:56 +0200 | [diff] [blame] | 83 | #define DFL_REQUEST_SIZE -1 |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 84 | #define DFL_DEBUG_LEVEL 0 |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 85 | #define DFL_CONTEXT_CRT_CB 0 |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 86 | #define DFL_NBIO 0 |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 87 | #define DFL_EVENT 0 |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 88 | #define DFL_READ_TIMEOUT 0 |
Manuel Pégourié-Gonnard | f1e0df3 | 2014-10-02 14:02:32 +0200 | [diff] [blame] | 89 | #define DFL_MAX_RESEND 0 |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 90 | #define DFL_CA_FILE "" |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 91 | #define DFL_CA_PATH "" |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 92 | #define DFL_CRT_FILE "" |
| 93 | #define DFL_KEY_FILE "" |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 94 | #define DFL_KEY_OPAQUE 0 |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 95 | #define DFL_PSK "" |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 96 | #define DFL_PSK_OPAQUE 0 |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 97 | #define DFL_PSK_IDENTITY "Client_identity" |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 98 | #define DFL_ECJPAKE_PW NULL |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 99 | #define DFL_EC_MAX_OPS -1 |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 100 | #define DFL_FORCE_CIPHER 0 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 102 | #define DFL_ALLOW_LEGACY -2 |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 103 | #define DFL_RENEGOTIATE 0 |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 104 | #define DFL_EXCHANGES 1 |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 105 | #define DFL_MIN_VERSION -1 |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 106 | #define DFL_MAX_VERSION -1 |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 107 | #define DFL_ARC4 -1 |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 108 | #define DFL_SHA1 -1 |
Manuel Pégourié-Gonnard | fa44f20 | 2015-03-27 17:52:25 +0100 | [diff] [blame] | 109 | #define DFL_AUTH_MODE -1 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 111 | #define DFL_TRUNC_HMAC -1 |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 112 | #define DFL_RECSPLIT -1 |
Manuel Pégourié-Gonnard | 9096682 | 2015-06-11 17:02:29 +0200 | [diff] [blame] | 113 | #define DFL_DHMLEN -1 |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 114 | #define DFL_RECONNECT 0 |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 115 | #define DFL_RECO_DELAY 0 |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 116 | #define DFL_CID_ENABLED 0 |
| 117 | #define DFL_CID_VALUE "" |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 118 | #define DFL_CID_ENABLED_RENEGO -1 |
| 119 | #define DFL_CID_VALUE_RENEGO NULL |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 120 | #define DFL_RECONNECT_HARD 0 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 122 | #define DFL_ALPN_STRING NULL |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 123 | #define DFL_CURVES NULL |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 125 | #define DFL_HS_TO_MIN 0 |
| 126 | #define DFL_HS_TO_MAX 0 |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 127 | #define DFL_DTLS_MTU -1 |
Hanno Becker | 4d61591 | 2018-08-14 13:33:30 +0100 | [diff] [blame] | 128 | #define DFL_DGRAM_PACKING 1 |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 129 | #define DFL_FALLBACK -1 |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 130 | #define DFL_EXTENDED_MS -1 |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 131 | #define DFL_ETM -1 |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 132 | #define DFL_CA_CALLBACK 0 |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 133 | #define DFL_EAP_TLS 0 |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 134 | #define DFL_REPRODUCIBLE 0 |
Paul Bakker | 0e6975b | 2009-02-10 22:19:10 +0000 | [diff] [blame] | 135 | |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 136 | #define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: " |
| 137 | #define GET_REQUEST_END "\r\n\r\n" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Janos Follath | ae13beb | 2019-04-05 14:06:58 +0100 | [diff] [blame] | 140 | #define USAGE_CONTEXT_CRT_CB \ |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 141 | " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \ |
| 142 | " to the SSL configuration of the SSL context.\n" \ |
| 143 | " Possible values:\n"\ |
| 144 | " - 0 (default): Use CRT callback bound to configuration\n" \ |
| 145 | " - 1: Use CRT callback bound to SSL context\n" |
| 146 | #else |
Janos Follath | ae13beb | 2019-04-05 14:06:58 +0100 | [diff] [blame] | 147 | #define USAGE_CONTEXT_CRT_CB "" |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 148 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 149 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 151 | #define USAGE_IO \ |
Paul Bakker | 1f9d02d | 2012-11-20 10:30:55 +0100 | [diff] [blame] | 152 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 153 | " default: \"\" (pre-loaded)\n" \ |
| 154 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 155 | " default: \"\" (pre-loaded) (overrides ca_file)\n" \ |
| 156 | " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \ |
| 157 | " default: \"\" (pre-loaded)\n" \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 158 | " key_file=%%s default: \"\" (pre-loaded)\n" |
| 159 | #else |
| 160 | #define USAGE_IO \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | " No file operations available (MBEDTLS_FS_IO not defined)\n" |
| 162 | #endif /* MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 163 | #else /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 164 | #define USAGE_IO "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 166 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C) |
| 167 | #define USAGE_KEY_OPAQUE \ |
| 168 | " key_opaque=%%d Handle your private key as if it were opaque\n" \ |
| 169 | " default: 0 (disabled)\n" |
| 170 | #else |
| 171 | #define USAGE_KEY_OPAQUE "" |
| 172 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 173 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 174 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 175 | #define USAGE_CID \ |
| 176 | " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \ |
| 177 | " default: 0 (disabled)\n" \ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 178 | " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \ |
Hanno Becker | 3279822 | 2019-05-23 17:01:06 +0100 | [diff] [blame] | 179 | " default: same as 'cid' parameter\n" \ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 180 | " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 181 | " default: \"\"\n" \ |
| 182 | " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \ |
Hanno Becker | 3279822 | 2019-05-23 17:01:06 +0100 | [diff] [blame] | 183 | " default: same as 'cid_val' parameter\n" |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 184 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 185 | #define USAGE_CID "" |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 186 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 187 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 189 | #define USAGE_PSK_RAW \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 190 | " psk=%%s default: \"\" (in hex, without 0x)\n" \ |
| 191 | " psk_identity=%%s default: \"Client_identity\"\n" |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 192 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 193 | #define USAGE_PSK_SLOT \ |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 194 | " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \ |
| 195 | " Enable this to store the PSK configured through command line\n" \ |
| 196 | " parameter `psk` in a PSA-based key slot.\n" \ |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 197 | " Note: Currently only supported in conjunction with\n" \ |
| 198 | " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \ |
| 199 | " to force a particular PSK-only ciphersuite.\n" \ |
| 200 | " Note: This is to test integration of PSA-based opaque PSKs with\n" \ |
| 201 | " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \ |
| 202 | " with prepopulated key slots instead of importing raw key material.\n" |
| 203 | #else |
| 204 | #define USAGE_PSK_SLOT "" |
| 205 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 1bac87c | 2019-03-29 12:02:26 +0000 | [diff] [blame] | 206 | #define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT |
| 207 | #else |
| 208 | #define USAGE_PSK "" |
| 209 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
| 210 | |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 211 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 212 | #define USAGE_CA_CALLBACK \ |
| 213 | " ca_callback=%%d default: 0 (disabled)\n" \ |
| 214 | " Enable this to use the trusted certificate callback function\n" |
| 215 | #else |
| 216 | #define USAGE_CA_CALLBACK "" |
| 217 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 218 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 220 | #define USAGE_TICKETS \ |
| 221 | " tickets=%%d default: 1 (enabled)\n" |
| 222 | #else |
| 223 | #define USAGE_TICKETS "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 225 | |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 226 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 227 | #define USAGE_EAP_TLS \ |
| 228 | " eap_tls=%%d default: 0 (disabled)\n" |
| 229 | #else |
| 230 | #define USAGE_EAP_TLS "" |
| 231 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 234 | #define USAGE_TRUNC_HMAC \ |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 235 | " trunc_hmac=%%d default: library default\n" |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 236 | #else |
| 237 | #define USAGE_TRUNC_HMAC "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 241 | #define USAGE_MAX_FRAG_LEN \ |
| 242 | " max_frag_len=%%d default: 16384 (tls default)\n" \ |
| 243 | " options: 512, 1024, 2048, 4096\n" |
| 244 | #else |
| 245 | #define USAGE_MAX_FRAG_LEN "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 249 | #define USAGE_RECSPLIT \ |
Manuel Pégourié-Gonnard | bf27eaa | 2015-06-11 17:02:10 +0200 | [diff] [blame] | 250 | " recsplit=0/1 default: (library default: on)\n" |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 251 | #else |
| 252 | #define USAGE_RECSPLIT |
| 253 | #endif |
| 254 | |
Manuel Pégourié-Gonnard | 9096682 | 2015-06-11 17:02:29 +0200 | [diff] [blame] | 255 | #if defined(MBEDTLS_DHM_C) |
| 256 | #define USAGE_DHMLEN \ |
| 257 | " dhmlen=%%d default: (library default: 1024 bits)\n" |
| 258 | #else |
| 259 | #define USAGE_DHMLEN |
| 260 | #endif |
| 261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 263 | #define USAGE_ALPN \ |
| 264 | " alpn=%%s default: \"\" (disabled)\n" \ |
| 265 | " example: spdy/1,http/1.1\n" |
| 266 | #else |
| 267 | #define USAGE_ALPN "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 269 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 270 | #if defined(MBEDTLS_ECP_C) |
| 271 | #define USAGE_CURVES \ |
| 272 | " curves=a,b,c,d default: \"default\" (library default)\n" \ |
| 273 | " example: \"secp521r1,brainpoolP512r1\"\n" \ |
| 274 | " - use \"none\" for empty list\n" \ |
| 275 | " - see mbedtls_ecp_curve_list()\n" \ |
| 276 | " for acceptable curve names\n" |
| 277 | #else |
| 278 | #define USAGE_CURVES "" |
| 279 | #endif |
| 280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 282 | #define USAGE_DTLS \ |
| 283 | " dtls=%%d default: 0 (TLS)\n" \ |
| 284 | " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \ |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 285 | " range of DTLS handshake timeouts in millisecs\n" \ |
Hanno Becker | 4d61591 | 2018-08-14 13:33:30 +0100 | [diff] [blame] | 286 | " mtu=%%d default: (library default: unlimited)\n" \ |
| 287 | " dgram_packing=%%d default: 1 (allowed)\n" \ |
| 288 | " allow or forbid packing of multiple\n" \ |
| 289 | " records within a single datgram.\n" |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 290 | #else |
| 291 | #define USAGE_DTLS "" |
| 292 | #endif |
| 293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 295 | #define USAGE_FALLBACK \ |
| 296 | " fallback=0/1 default: (library default: off)\n" |
| 297 | #else |
| 298 | #define USAGE_FALLBACK "" |
| 299 | #endif |
| 300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 302 | #define USAGE_EMS \ |
| 303 | " extended_ms=0/1 default: (library default: on)\n" |
| 304 | #else |
| 305 | #define USAGE_EMS "" |
| 306 | #endif |
| 307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 309 | #define USAGE_ETM \ |
| 310 | " etm=0/1 default: (library default: on)\n" |
| 311 | #else |
| 312 | #define USAGE_ETM "" |
| 313 | #endif |
| 314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 316 | #define USAGE_RENEGO \ |
| 317 | " renegotiation=%%d default: 0 (disabled)\n" \ |
| 318 | " renegotiate=%%d default: 0 (disabled)\n" |
| 319 | #else |
| 320 | #define USAGE_RENEGO "" |
| 321 | #endif |
| 322 | |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 323 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 324 | #define USAGE_ECJPAKE \ |
| 325 | " ecjpake_pw=%%s default: none (disabled)\n" |
| 326 | #else |
| 327 | #define USAGE_ECJPAKE "" |
| 328 | #endif |
| 329 | |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 330 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 331 | #define USAGE_ECRESTART \ |
| 332 | " ec_max_ops=%%s default: library default (restart disabled)\n" |
| 333 | #else |
| 334 | #define USAGE_ECRESTART "" |
| 335 | #endif |
| 336 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 337 | #define USAGE \ |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 338 | "\n usage: ssl_client2 param=<>...\n" \ |
| 339 | "\n acceptable parameters:\n" \ |
| 340 | " server_name=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 341 | " server_addr=%%s default: given by name\n" \ |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 342 | " server_port=%%d default: 4433\n" \ |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 343 | " request_page=%%s default: \".\"\n" \ |
Andres Amaya Garcia | ce6fbac | 2018-07-04 09:29:34 +0100 | [diff] [blame] | 344 | " request_size=%%d default: about 34 (basic request)\n" \ |
| 345 | " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \ |
| 346 | " If 0, in the first exchange only an empty\n" \ |
| 347 | " application data message is sent followed by\n" \ |
| 348 | " a second non-empty message before attempting\n" \ |
| 349 | " to read a response from the server\n" \ |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 350 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 351 | " nbio=%%d default: 0 (blocking I/O)\n" \ |
| 352 | " options: 1 (non-blocking), 2 (added delays)\n" \ |
| 353 | " event=%%d default: 0 (loop)\n" \ |
| 354 | " options: 1 (level-triggered, implies nbio=1),\n" \ |
| 355 | " read_timeout=%%d default: 0 ms (no timeout)\n" \ |
Manuel Pégourié-Gonnard | f1e0df3 | 2014-10-02 14:02:32 +0200 | [diff] [blame] | 356 | " max_resend=%%d default: 0 (no resend on timeout)\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 357 | "\n" \ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 358 | USAGE_DTLS \ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 359 | USAGE_CID \ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 360 | "\n" \ |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 361 | " auth_mode=%%s default: (library default: none)\n" \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 362 | " options: none, optional, required\n" \ |
| 363 | USAGE_IO \ |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 364 | USAGE_KEY_OPAQUE \ |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 365 | USAGE_CA_CALLBACK \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 366 | "\n" \ |
| 367 | USAGE_PSK \ |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 368 | USAGE_ECJPAKE \ |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 369 | USAGE_ECRESTART \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 370 | "\n" \ |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 371 | " allow_legacy=%%d default: (library default: no)\n" \ |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 372 | USAGE_RENEGO \ |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 373 | " exchanges=%%d default: 1\n" \ |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 374 | " reconnect=%%d default: 0 (disabled)\n" \ |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 375 | " reco_delay=%%d default: 0 seconds\n" \ |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 376 | " reconnect_hard=%%d default: 0 (disabled)\n" \ |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 377 | USAGE_TICKETS \ |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 378 | USAGE_EAP_TLS \ |
Manuel Pégourié-Gonnard | 2fc243d | 2014-02-19 18:22:59 +0100 | [diff] [blame] | 379 | USAGE_MAX_FRAG_LEN \ |
| 380 | USAGE_TRUNC_HMAC \ |
Janos Follath | ae13beb | 2019-04-05 14:06:58 +0100 | [diff] [blame] | 381 | USAGE_CONTEXT_CRT_CB \ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 382 | USAGE_ALPN \ |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 383 | USAGE_FALLBACK \ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 384 | USAGE_EMS \ |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 385 | USAGE_ETM \ |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 386 | USAGE_CURVES \ |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 387 | USAGE_RECSPLIT \ |
Manuel Pégourié-Gonnard | 9096682 | 2015-06-11 17:02:29 +0200 | [diff] [blame] | 388 | USAGE_DHMLEN \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 389 | "\n" \ |
Manuel Pégourié-Gonnard | fa44f20 | 2015-03-27 17:52:25 +0100 | [diff] [blame] | 390 | " arc4=%%d default: (library default: 0)\n" \ |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 391 | " allow_sha1=%%d default: 0\n" \ |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 392 | " min_version=%%s default: (library default: tls1)\n" \ |
| 393 | " max_version=%%s default: (library default: tls1_2)\n" \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 394 | " force_version=%%s default: \"\" (none)\n" \ |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 395 | " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 396 | "\n" \ |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 397 | " force_ciphersuite=<name> default: all enabled\n"\ |
Andres Amaya Garcia | bc81884 | 2018-10-16 21:08:38 +0100 | [diff] [blame] | 398 | " query_config=<name> return 0 if the specified\n" \ |
| 399 | " configuration macro is defined and 1\n" \ |
| 400 | " otherwise. The expansion of the macro\n" \ |
| 401 | " is printed if it is defined\n" \ |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 402 | " acceptable ciphersuite names:\n" |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 403 | |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 404 | #define ALPN_LIST_SIZE 10 |
| 405 | #define CURVE_LIST_SIZE 20 |
| 406 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 407 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 408 | #include "mbedtls/platform_util.h" |
| 409 | void mbedtls_param_failed( const char *failure_condition, |
| 410 | const char *file, |
| 411 | int line ) |
| 412 | { |
| 413 | mbedtls_printf( "%s:%i: Input param failed - %s\n", |
| 414 | file, line, failure_condition ); |
| 415 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
| 416 | } |
| 417 | #endif |
| 418 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 419 | /* |
| 420 | * global options |
| 421 | */ |
| 422 | struct options |
| 423 | { |
| 424 | const char *server_name; /* hostname of the server (client only) */ |
| 425 | const char *server_addr; /* address of the server (client only) */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 426 | const char *server_port; /* port on which the ssl service runs */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 427 | int debug_level; /* level of debugging */ |
| 428 | int nbio; /* should I/O be blocking? */ |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 429 | int event; /* loop or event-driven IO? level or edge triggered? */ |
| 430 | uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */ |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 431 | int max_resend; /* DTLS times to resend on read timeout */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 432 | const char *request_page; /* page on server to request */ |
| 433 | int request_size; /* pad request with header to requested size */ |
| 434 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 435 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
| 436 | const char *crt_file; /* the file with the client certificate */ |
| 437 | const char *key_file; /* the file with the client key */ |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 438 | int key_opaque; /* handle private key as if it were opaque */ |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 439 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 440 | int psk_opaque; |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 441 | #endif |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 442 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 443 | int ca_callback; /* Use callback for trusted certificate list */ |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 444 | #endif |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 445 | const char *psk; /* the pre-shared key */ |
| 446 | const char *psk_identity; /* the pre-shared key identity */ |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 447 | const char *ecjpake_pw; /* the EC J-PAKE password */ |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 448 | int ec_max_ops; /* EC consecutive operations limit */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 449 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 450 | int renegotiation; /* enable / disable renegotiation */ |
| 451 | int allow_legacy; /* allow legacy renegotiation */ |
| 452 | int renegotiate; /* attempt renegotiation? */ |
| 453 | int renego_delay; /* delay before enforcing renegotiation */ |
| 454 | int exchanges; /* number of data exchanges */ |
| 455 | int min_version; /* minimum protocol version accepted */ |
| 456 | int max_version; /* maximum protocol version accepted */ |
| 457 | int arc4; /* flag for arc4 suites support */ |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 458 | int allow_sha1; /* flag for SHA-1 support */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 459 | int auth_mode; /* verify mode for connection */ |
| 460 | unsigned char mfl_code; /* code for maximum fragment length */ |
| 461 | int trunc_hmac; /* negotiate truncated hmac or not */ |
| 462 | int recsplit; /* enable record splitting? */ |
Manuel Pégourié-Gonnard | 9096682 | 2015-06-11 17:02:29 +0200 | [diff] [blame] | 463 | int dhmlen; /* minimum DHM params len in bits */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 464 | int reconnect; /* attempt to resume session */ |
| 465 | int reco_delay; /* delay in seconds before resuming session */ |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 466 | int reconnect_hard; /* unexpectedly reconnect from the same port */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 467 | int tickets; /* enable / disable session tickets */ |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 468 | const char *curves; /* list of supported elliptic curves */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 469 | const char *alpn_string; /* ALPN supported protocols */ |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 470 | int transport; /* TLS or DTLS? */ |
| 471 | uint32_t hs_to_min; /* Initial value of DTLS handshake timer */ |
| 472 | uint32_t hs_to_max; /* Max value of DTLS handshake timer */ |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 473 | int dtls_mtu; /* UDP Maximum tranport unit for DTLS */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 474 | int fallback; /* is this a fallback connection? */ |
Hanno Becker | 4d61591 | 2018-08-14 13:33:30 +0100 | [diff] [blame] | 475 | int dgram_packing; /* allow/forbid datagram packing */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 476 | int extended_ms; /* negotiate extended master secret? */ |
| 477 | int etm; /* negotiate encrypt then mac? */ |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 478 | int context_crt_cb; /* use context-specific CRT verify callback */ |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 479 | int eap_tls; /* derive EAP-TLS keying material? */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 480 | int cid_enabled; /* whether to use the CID extension or not */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 481 | int cid_enabled_renego; /* whether to use the CID extension or not |
| 482 | * during renegotiation */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 483 | const char *cid_val; /* the CID to use for incoming messages */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 484 | const char *cid_val_renego; /* the CID to use for incoming messages |
| 485 | * after renegotiation */ |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 486 | int reproducible; /* make communication reproducible */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 487 | } opt; |
| 488 | |
Andres Amaya Garcia | bc81884 | 2018-10-16 21:08:38 +0100 | [diff] [blame] | 489 | int query_config( const char *config ); |
| 490 | |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 491 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 492 | typedef struct eap_tls_keys |
| 493 | { |
| 494 | unsigned char master_secret[48]; |
| 495 | unsigned char randbytes[64]; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 496 | mbedtls_tls_prf_types tls_prf_type; |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 497 | } eap_tls_keys; |
| 498 | |
| 499 | static int eap_tls_key_derivation ( void *p_expkey, |
| 500 | const unsigned char *ms, |
| 501 | const unsigned char *kb, |
| 502 | size_t maclen, |
| 503 | size_t keylen, |
| 504 | size_t ivlen, |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 505 | unsigned char client_random[32], |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 506 | unsigned char server_random[32], |
| 507 | mbedtls_tls_prf_types tls_prf_type ) |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 508 | { |
| 509 | eap_tls_keys *keys = (eap_tls_keys *)p_expkey; |
| 510 | |
| 511 | ( ( void ) kb ); |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 512 | memcpy( keys->master_secret, ms, sizeof( keys->master_secret ) ); |
| 513 | memcpy( keys->randbytes, client_random, 32 ); |
| 514 | memcpy( keys->randbytes + 32, server_random, 32 ); |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 515 | keys->tls_prf_type = tls_prf_type; |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 516 | |
Ron Eldor | f75e252 | 2019-05-14 20:38:49 +0300 | [diff] [blame] | 517 | if( opt.debug_level > 2 ) |
| 518 | { |
Ron Eldor | 801faf0 | 2019-05-15 17:45:24 +0300 | [diff] [blame] | 519 | mbedtls_printf("exported maclen is %u\n", (unsigned)maclen); |
| 520 | mbedtls_printf("exported keylen is %u\n", (unsigned)keylen); |
| 521 | mbedtls_printf("exported ivlen is %u\n", (unsigned)ivlen); |
Ron Eldor | f75e252 | 2019-05-14 20:38:49 +0300 | [diff] [blame] | 522 | } |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 523 | return( 0 ); |
| 524 | } |
| 525 | #endif |
| 526 | |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 527 | static void my_debug( void *ctx, int level, |
| 528 | const char *file, int line, |
| 529 | const char *str ) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 530 | { |
Manuel Pégourié-Gonnard | 052f288 | 2015-07-01 11:50:23 +0200 | [diff] [blame] | 531 | const char *p, *basename; |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 532 | |
Manuel Pégourié-Gonnard | 052f288 | 2015-07-01 11:50:23 +0200 | [diff] [blame] | 533 | /* Extract basename from file */ |
| 534 | for( p = basename = file; *p != '\0'; p++ ) |
| 535 | if( *p == '/' || *p == '\\' ) |
| 536 | basename = p + 1; |
| 537 | |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 538 | mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", |
| 539 | basename, line, level, str ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 540 | fflush( (FILE *) ctx ); |
| 541 | } |
| 542 | |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 543 | |
| 544 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) |
| 545 | { |
| 546 | (void) time; |
| 547 | return 0x5af2a056; |
| 548 | } |
| 549 | |
| 550 | int dummy_random( void *p_rng, unsigned char *output, size_t output_len ) |
| 551 | { |
| 552 | int ret; |
| 553 | size_t i; |
| 554 | |
| 555 | //use mbedtls_ctr_drbg_random to find bugs in it |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 556 | ret = mbedtls_ctr_drbg_random( p_rng, output, output_len ); |
| 557 | for ( i = 0; i < output_len; i++ ) |
| 558 | { |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 559 | //replace result with pseudo random |
| 560 | output[i] = (unsigned char) rand(); |
| 561 | } |
| 562 | return( ret ); |
| 563 | } |
| 564 | |
| 565 | int dummy_entropy( void *data, unsigned char *output, size_t len ) |
| 566 | { |
| 567 | size_t i; |
| 568 | (void) data; |
Philippe Antoine | 12e85de | 2019-06-11 16:07:53 +0200 | [diff] [blame^] | 569 | int ret; |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 570 | |
Philippe Antoine | 3ca5085 | 2019-06-07 22:31:59 +0200 | [diff] [blame] | 571 | ret = mbedtls_entropy_func( data, output, len ); |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 572 | for ( i = 0; i < len; i++ ) |
| 573 | { |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 574 | //replace result with pseudo random |
| 575 | output[i] = (unsigned char) rand(); |
| 576 | } |
Philippe Antoine | 3ca5085 | 2019-06-07 22:31:59 +0200 | [diff] [blame] | 577 | return( ret ); |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 580 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 581 | int ca_callback( void *data, mbedtls_x509_crt const *child, |
Janos Follath | d7ecbd6 | 2019-04-05 14:52:17 +0100 | [diff] [blame] | 582 | mbedtls_x509_crt **candidates ) |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 583 | { |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 584 | int ret = 0; |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 585 | mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 586 | mbedtls_x509_crt *first; |
| 587 | |
| 588 | /* This is a test-only implementation of the CA callback |
| 589 | * which always returns the entire list of trusted certificates. |
| 590 | * Production implementations managing a large number of CAs |
| 591 | * should use an efficient presentation and lookup for the |
| 592 | * set of trusted certificates (such as a hashtable) and only |
| 593 | * return those trusted certificates which satisfy basic |
| 594 | * parental checks, such as the matching of child `Issuer` |
Jarno Lamsa | f7a7f9e | 2019-04-01 15:11:54 +0300 | [diff] [blame] | 595 | * and parent `Subject` field or matching key identifiers. */ |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 596 | ((void) child); |
| 597 | |
| 598 | first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 599 | if( first == NULL ) |
| 600 | { |
| 601 | ret = -1; |
| 602 | goto exit; |
| 603 | } |
| 604 | mbedtls_x509_crt_init( first ); |
| 605 | |
| 606 | if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) |
| 607 | { |
| 608 | ret = -1; |
| 609 | goto exit; |
| 610 | } |
| 611 | |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 612 | while( ca->next != NULL ) |
| 613 | { |
| 614 | ca = ca->next; |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 615 | if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) |
| 616 | { |
| 617 | ret = -1; |
| 618 | goto exit; |
| 619 | } |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 620 | } |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 621 | |
| 622 | exit: |
| 623 | |
| 624 | if( ret != 0 ) |
| 625 | { |
| 626 | mbedtls_x509_crt_free( first ); |
| 627 | mbedtls_free( first ); |
| 628 | first = NULL; |
| 629 | } |
| 630 | |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 631 | *candidates = first; |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 632 | return( ret ); |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 633 | } |
| 634 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 635 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 636 | /* |
| 637 | * Test recv/send functions that make sure each try returns |
| 638 | * WANT_READ/WANT_WRITE at least once before sucesseding |
| 639 | */ |
| 640 | static int my_recv( void *ctx, unsigned char *buf, size_t len ) |
| 641 | { |
| 642 | static int first_try = 1; |
| 643 | int ret; |
| 644 | |
| 645 | if( first_try ) |
| 646 | { |
| 647 | first_try = 0; |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 648 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | ret = mbedtls_net_recv( ctx, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 652 | if( ret != MBEDTLS_ERR_SSL_WANT_READ ) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 653 | first_try = 1; /* Next call will be a new operation */ |
| 654 | return( ret ); |
| 655 | } |
| 656 | |
| 657 | static int my_send( void *ctx, const unsigned char *buf, size_t len ) |
| 658 | { |
| 659 | static int first_try = 1; |
| 660 | int ret; |
| 661 | |
| 662 | if( first_try ) |
| 663 | { |
| 664 | first_try = 0; |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 665 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | ret = mbedtls_net_send( ctx, buf, len ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 669 | if( ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 670 | first_try = 1; /* Next call will be a new operation */ |
| 671 | return( ret ); |
| 672 | } |
| 673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | a1051b4 | 2019-02-26 11:38:29 +0000 | [diff] [blame] | 675 | static unsigned char peer_crt_info[1024]; |
Hanno Becker | a9766c2c | 2019-02-25 17:43:18 +0000 | [diff] [blame] | 676 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 677 | /* |
| 678 | * Enabled if debug_level > 1 in code below |
| 679 | */ |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 680 | static int my_verify( void *data, mbedtls_x509_crt *crt, |
| 681 | int depth, uint32_t *flags ) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 682 | { |
| 683 | char buf[1024]; |
| 684 | ((void) data); |
| 685 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); |
Hanno Becker | a9766c2c | 2019-02-25 17:43:18 +0000 | [diff] [blame] | 687 | if( depth == 0 ) |
| 688 | memcpy( peer_crt_info, buf, sizeof( buf ) ); |
| 689 | |
| 690 | if( opt.debug_level == 0 ) |
| 691 | return( 0 ); |
| 692 | |
| 693 | mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | mbedtls_printf( "%s", buf ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 695 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 696 | if ( ( *flags ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | mbedtls_printf( " This certificate has no flags\n" ); |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 698 | else |
| 699 | { |
| 700 | mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags ); |
| 701 | mbedtls_printf( "%s\n", buf ); |
| 702 | } |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 703 | |
| 704 | return( 0 ); |
| 705 | } |
Gilles Peskine | cd3c845 | 2017-05-09 14:57:45 +0200 | [diff] [blame] | 706 | |
| 707 | static int ssl_sig_hashes_for_test[] = { |
| 708 | #if defined(MBEDTLS_SHA512_C) |
| 709 | MBEDTLS_MD_SHA512, |
| 710 | MBEDTLS_MD_SHA384, |
| 711 | #endif |
| 712 | #if defined(MBEDTLS_SHA256_C) |
| 713 | MBEDTLS_MD_SHA256, |
| 714 | MBEDTLS_MD_SHA224, |
| 715 | #endif |
| 716 | #if defined(MBEDTLS_SHA1_C) |
| 717 | /* Allow SHA-1 as we use it extensively in tests. */ |
| 718 | MBEDTLS_MD_SHA1, |
| 719 | #endif |
| 720 | MBEDTLS_MD_NONE |
| 721 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 723 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 724 | /* |
| 725 | * Wait for an event from the underlying transport or the timer |
| 726 | * (Used in event-driven IO mode). |
| 727 | */ |
| 728 | #if !defined(MBEDTLS_TIMING_C) |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 729 | int idle( mbedtls_net_context *fd, |
Hanno Becker | 9b2b66e | 2018-03-15 12:21:15 +0000 | [diff] [blame] | 730 | int idle_reason ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 731 | #else |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 732 | int idle( mbedtls_net_context *fd, |
Hanno Becker | 9b2b66e | 2018-03-15 12:21:15 +0000 | [diff] [blame] | 733 | mbedtls_timing_delay_context *timer, |
| 734 | int idle_reason ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 735 | #endif |
Hanno Becker | 9b2b66e | 2018-03-15 12:21:15 +0000 | [diff] [blame] | 736 | { |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 737 | |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 738 | int ret; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 739 | int poll_type = 0; |
| 740 | |
| 741 | if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 742 | poll_type = MBEDTLS_NET_POLL_WRITE; |
| 743 | else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ ) |
| 744 | poll_type = MBEDTLS_NET_POLL_READ; |
| 745 | #if !defined(MBEDTLS_TIMING_C) |
| 746 | else |
Hanno Becker | ef52796 | 2018-03-15 15:49:24 +0000 | [diff] [blame] | 747 | return( 0 ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 748 | #endif |
| 749 | |
| 750 | while( 1 ) |
| 751 | { |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 752 | /* Check if timer has expired */ |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 753 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 754 | if( timer != NULL && |
| 755 | mbedtls_timing_get_delay( timer ) == 2 ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 756 | { |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 757 | break; |
| 758 | } |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 759 | #endif /* MBEDTLS_TIMING_C */ |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 760 | |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 761 | /* Check if underlying transport became available */ |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 762 | if( poll_type != 0 ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 763 | { |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 764 | ret = mbedtls_net_poll( fd, poll_type, 0 ); |
| 765 | if( ret < 0 ) |
| 766 | return( ret ); |
| 767 | if( ret == poll_type ) |
| 768 | break; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 769 | } |
| 770 | } |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 771 | |
| 772 | return( 0 ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Hanno Becker | 1f583ee | 2019-04-09 17:12:56 +0100 | [diff] [blame] | 775 | /* Unhexify `hex` into `dst`. `dst` must have |
| 776 | * size at least `strlen( hex ) / 2`. */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 777 | int unhexify( char const *hex, unsigned char *dst ) |
Hanno Becker | 1f583ee | 2019-04-09 17:12:56 +0100 | [diff] [blame] | 778 | { |
| 779 | unsigned char c; |
| 780 | size_t j; |
| 781 | size_t len = strlen( hex ); |
| 782 | |
| 783 | if( len % 2 != 0 ) |
| 784 | return( -1 ); |
| 785 | |
| 786 | for( j = 0; j < len; j += 2 ) |
| 787 | { |
| 788 | c = hex[j]; |
| 789 | if( c >= '0' && c <= '9' ) |
| 790 | c -= '0'; |
| 791 | else if( c >= 'a' && c <= 'f' ) |
| 792 | c -= 'a' - 10; |
| 793 | else if( c >= 'A' && c <= 'F' ) |
| 794 | c -= 'A' - 10; |
| 795 | else |
| 796 | return( -1 ); |
| 797 | dst[ j / 2 ] = c << 4; |
| 798 | |
| 799 | c = hex[j + 1]; |
| 800 | if( c >= '0' && c <= '9' ) |
| 801 | c -= '0'; |
| 802 | else if( c >= 'a' && c <= 'f' ) |
| 803 | c -= 'a' - 10; |
| 804 | else if( c >= 'A' && c <= 'F' ) |
| 805 | c -= 'A' - 10; |
| 806 | else |
| 807 | return( -1 ); |
| 808 | dst[ j / 2 ] |= c; |
| 809 | } |
| 810 | |
| 811 | return( 0 ); |
| 812 | } |
| 813 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 814 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 815 | int report_cid_usage( mbedtls_ssl_context *ssl, |
| 816 | const char *additional_description ) |
| 817 | { |
| 818 | int ret; |
| 819 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
| 820 | size_t peer_cid_len; |
| 821 | int cid_negotiated; |
| 822 | |
| 823 | if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 824 | return( 0 ); |
| 825 | |
Hanno Becker | 6ae14c0 | 2019-05-22 16:59:25 +0100 | [diff] [blame] | 826 | /* Check if the use of a CID has been negotiated, |
| 827 | * but don't ask for the CID value and length. |
| 828 | * |
| 829 | * Note: Here and below, we're demonstrating the various ways |
| 830 | * in which mbedtls_ssl_get_peer_cid() can be called, |
| 831 | * depending on whether or not the length/value of the |
| 832 | * peer's CID is needed. |
| 833 | * |
| 834 | * An actual application, however, should use |
| 835 | * just one call to mbedtls_ssl_get_peer_cid(). */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 836 | ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated, |
Hanno Becker | 6ae14c0 | 2019-05-22 16:59:25 +0100 | [diff] [blame] | 837 | NULL, NULL ); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 838 | if( ret != 0 ) |
| 839 | { |
| 840 | mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n", |
| 841 | -ret ); |
| 842 | return( ret ); |
| 843 | } |
| 844 | |
| 845 | if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED ) |
| 846 | { |
| 847 | if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED ) |
| 848 | { |
| 849 | mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n", |
| 850 | additional_description ); |
| 851 | } |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | size_t idx=0; |
| 856 | mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n", |
| 857 | additional_description ); |
Hanno Becker | 6ae14c0 | 2019-05-22 16:59:25 +0100 | [diff] [blame] | 858 | |
| 859 | /* Ask for just the length of the peer's CID. */ |
| 860 | ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated, |
| 861 | NULL, &peer_cid_len ); |
| 862 | if( ret != 0 ) |
| 863 | { |
| 864 | mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n", |
| 865 | -ret ); |
| 866 | return( ret ); |
| 867 | } |
| 868 | |
| 869 | /* Ask for just length + value of the peer's CID. */ |
| 870 | ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated, |
| 871 | peer_cid, &peer_cid_len ); |
| 872 | if( ret != 0 ) |
| 873 | { |
| 874 | mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n", |
| 875 | -ret ); |
| 876 | return( ret ); |
| 877 | } |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 878 | mbedtls_printf( "(%s) Peer CID (length %u Bytes): ", |
| 879 | additional_description, |
| 880 | (unsigned) peer_cid_len ); |
| 881 | while( idx < peer_cid_len ) |
| 882 | { |
| 883 | mbedtls_printf( "%02x ", peer_cid[ idx ] ); |
| 884 | idx++; |
| 885 | } |
| 886 | mbedtls_printf( "\n" ); |
| 887 | } |
| 888 | |
| 889 | return( 0 ); |
| 890 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 891 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 892 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 893 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 894 | { |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 895 | int ret = 0, len, tail_len, i, written, frags, retry_left; |
| 896 | mbedtls_net_context server_fd; |
Hanno Becker | e4ad3e8 | 2017-09-18 15:05:46 +0100 | [diff] [blame] | 897 | |
| 898 | unsigned char buf[MAX_REQUEST_SIZE + 1]; |
| 899 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 900 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 901 | unsigned char psk[MBEDTLS_PSK_MAX_LEN]; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 902 | size_t psk_len = 0; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 903 | #endif |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 904 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 905 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 906 | unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 907 | unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 908 | size_t cid_len = 0; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 909 | size_t cid_renego_len = 0; |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 910 | #endif |
| 911 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 912 | #if defined(MBEDTLS_SSL_ALPN) |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 913 | const char *alpn_list[ALPN_LIST_SIZE]; |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 914 | #endif |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 915 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 916 | mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE]; |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 917 | const mbedtls_ecp_curve_info *curve_cur; |
| 918 | #endif |
| 919 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 920 | const char *pers = "ssl_client2"; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 921 | |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 922 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 923 | psa_key_handle_t slot = 0; |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 924 | psa_algorithm_t alg = 0; |
| 925 | psa_key_policy_t policy; |
| 926 | psa_status_t status; |
| 927 | #endif |
| 928 | |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 929 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 930 | mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default; |
| 931 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 932 | mbedtls_entropy_context entropy; |
| 933 | mbedtls_ctr_drbg_context ctr_drbg; |
| 934 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 935 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 936 | mbedtls_ssl_session saved_session; |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 937 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 938 | mbedtls_timing_delay_context timer; |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 939 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 940 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | db1cc76 | 2015-05-12 11:27:25 +0200 | [diff] [blame] | 941 | uint32_t flags; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 942 | mbedtls_x509_crt cacert; |
| 943 | mbedtls_x509_crt clicert; |
| 944 | mbedtls_pk_context pkey; |
Manuel Pégourié-Gonnard | cfdf8f4 | 2018-11-08 09:52:25 +0100 | [diff] [blame] | 945 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 946 | psa_key_handle_t key_slot = 0; /* invalid key slot */ |
Manuel Pégourié-Gonnard | cfdf8f4 | 2018-11-08 09:52:25 +0100 | [diff] [blame] | 947 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 948 | #endif |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 949 | char *p, *q; |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 950 | const int *list; |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 951 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 952 | unsigned char eap_tls_keymaterial[16]; |
| 953 | unsigned char eap_tls_iv[8]; |
| 954 | const char* eap_tls_label = "client EAP encryption"; |
| 955 | eap_tls_keys eap_tls_keying; |
| 956 | #endif |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 957 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 958 | /* |
| 959 | * Make sure memory references are valid. |
| 960 | */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 961 | mbedtls_net_init( &server_fd ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 962 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 963 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 965 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 966 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 967 | mbedtls_x509_crt_init( &cacert ); |
| 968 | mbedtls_x509_crt_init( &clicert ); |
| 969 | mbedtls_pk_init( &pkey ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 970 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 971 | #if defined(MBEDTLS_SSL_ALPN) |
Paul Bakker | 525f875 | 2014-05-01 10:58:57 +0200 | [diff] [blame] | 972 | memset( (void * ) alpn_list, 0, sizeof( alpn_list ) ); |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 973 | #endif |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 974 | |
Hanno Becker | b2b468b | 2018-11-12 17:46:59 +0000 | [diff] [blame] | 975 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 976 | status = psa_crypto_init(); |
| 977 | if( status != PSA_SUCCESS ) |
| 978 | { |
| 979 | mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 980 | (int) status ); |
| 981 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 982 | goto exit; |
| 983 | } |
| 984 | #endif |
| 985 | |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 986 | if( argc == 0 ) |
| 987 | { |
| 988 | usage: |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 989 | if( ret == 0 ) |
| 990 | ret = 1; |
| 991 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | mbedtls_printf( USAGE ); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 994 | list = mbedtls_ssl_list_ciphersuites(); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 995 | while( *list ) |
| 996 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 997 | mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 998 | list++; |
| 999 | if( !*list ) |
| 1000 | break; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1001 | mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1002 | list++; |
| 1003 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1004 | mbedtls_printf("\n"); |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1005 | goto exit; |
| 1006 | } |
| 1007 | |
| 1008 | opt.server_name = DFL_SERVER_NAME; |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 1009 | opt.server_addr = DFL_SERVER_ADDR; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1010 | opt.server_port = DFL_SERVER_PORT; |
| 1011 | opt.debug_level = DFL_DEBUG_LEVEL; |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 1012 | opt.cid_enabled = DFL_CID_ENABLED; |
| 1013 | opt.cid_val = DFL_CID_VALUE; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1014 | opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO; |
| 1015 | opt.cid_val_renego = DFL_CID_VALUE_RENEGO; |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1016 | opt.nbio = DFL_NBIO; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1017 | opt.event = DFL_EVENT; |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 1018 | opt.context_crt_cb = DFL_CONTEXT_CRT_CB; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1019 | opt.read_timeout = DFL_READ_TIMEOUT; |
Manuel Pégourié-Gonnard | f1e0df3 | 2014-10-02 14:02:32 +0200 | [diff] [blame] | 1020 | opt.max_resend = DFL_MAX_RESEND; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1021 | opt.request_page = DFL_REQUEST_PAGE; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 1022 | opt.request_size = DFL_REQUEST_SIZE; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1023 | opt.ca_file = DFL_CA_FILE; |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 1024 | opt.ca_path = DFL_CA_PATH; |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 1025 | opt.crt_file = DFL_CRT_FILE; |
| 1026 | opt.key_file = DFL_KEY_FILE; |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 1027 | opt.key_opaque = DFL_KEY_OPAQUE; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1028 | opt.psk = DFL_PSK; |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1029 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 1030 | opt.psk_opaque = DFL_PSK_OPAQUE; |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1031 | #endif |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 1032 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1033 | opt.ca_callback = DFL_CA_CALLBACK; |
| 1034 | #endif |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1035 | opt.psk_identity = DFL_PSK_IDENTITY; |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 1036 | opt.ecjpake_pw = DFL_ECJPAKE_PW; |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 1037 | opt.ec_max_ops = DFL_EC_MAX_OPS; |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1038 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1039 | opt.renegotiation = DFL_RENEGOTIATION; |
| 1040 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1041 | opt.renegotiate = DFL_RENEGOTIATE; |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1042 | opt.exchanges = DFL_EXCHANGES; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1043 | opt.min_version = DFL_MIN_VERSION; |
| 1044 | opt.max_version = DFL_MAX_VERSION; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1045 | opt.arc4 = DFL_ARC4; |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 1046 | opt.allow_sha1 = DFL_SHA1; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1047 | opt.auth_mode = DFL_AUTH_MODE; |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 1048 | opt.mfl_code = DFL_MFL_CODE; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 1049 | opt.trunc_hmac = DFL_TRUNC_HMAC; |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 1050 | opt.recsplit = DFL_RECSPLIT; |
Manuel Pégourié-Gonnard | 9096682 | 2015-06-11 17:02:29 +0200 | [diff] [blame] | 1051 | opt.dhmlen = DFL_DHMLEN; |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1052 | opt.reconnect = DFL_RECONNECT; |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1053 | opt.reco_delay = DFL_RECO_DELAY; |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 1054 | opt.reconnect_hard = DFL_RECONNECT_HARD; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1055 | opt.tickets = DFL_TICKETS; |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1056 | opt.alpn_string = DFL_ALPN_STRING; |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1057 | opt.curves = DFL_CURVES; |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1058 | opt.transport = DFL_TRANSPORT; |
| 1059 | opt.hs_to_min = DFL_HS_TO_MIN; |
| 1060 | opt.hs_to_max = DFL_HS_TO_MAX; |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 1061 | opt.dtls_mtu = DFL_DTLS_MTU; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1062 | opt.fallback = DFL_FALLBACK; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1063 | opt.extended_ms = DFL_EXTENDED_MS; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1064 | opt.etm = DFL_ETM; |
Hanno Becker | 4d61591 | 2018-08-14 13:33:30 +0100 | [diff] [blame] | 1065 | opt.dgram_packing = DFL_DGRAM_PACKING; |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1066 | opt.eap_tls = DFL_EAP_TLS; |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1067 | opt.reproducible = DFL_REPRODUCIBLE; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1068 | |
| 1069 | for( i = 1; i < argc; i++ ) |
| 1070 | { |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1071 | p = argv[i]; |
| 1072 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 1073 | goto usage; |
| 1074 | *q++ = '\0'; |
| 1075 | |
| 1076 | if( strcmp( p, "server_name" ) == 0 ) |
| 1077 | opt.server_name = q; |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 1078 | else if( strcmp( p, "server_addr" ) == 0 ) |
| 1079 | opt.server_addr = q; |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1080 | else if( strcmp( p, "server_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 1081 | opt.server_port = q; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1082 | else if( strcmp( p, "dtls" ) == 0 ) |
| 1083 | { |
| 1084 | int t = atoi( q ); |
| 1085 | if( t == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1087 | else if( t == 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1089 | else |
| 1090 | goto usage; |
| 1091 | } |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1092 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 1093 | { |
| 1094 | opt.debug_level = atoi( q ); |
| 1095 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 1096 | goto usage; |
| 1097 | } |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 1098 | else if( strcmp( p, "context_crt_cb" ) == 0 ) |
| 1099 | { |
| 1100 | opt.context_crt_cb = atoi( q ); |
| 1101 | if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 ) |
| 1102 | goto usage; |
| 1103 | } |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1104 | else if( strcmp( p, "nbio" ) == 0 ) |
| 1105 | { |
| 1106 | opt.nbio = atoi( q ); |
| 1107 | if( opt.nbio < 0 || opt.nbio > 2 ) |
| 1108 | goto usage; |
| 1109 | } |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1110 | else if( strcmp( p, "event" ) == 0 ) |
| 1111 | { |
| 1112 | opt.event = atoi( q ); |
| 1113 | if( opt.event < 0 || opt.event > 2 ) |
| 1114 | goto usage; |
| 1115 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1116 | else if( strcmp( p, "read_timeout" ) == 0 ) |
| 1117 | opt.read_timeout = atoi( q ); |
Manuel Pégourié-Gonnard | f1e0df3 | 2014-10-02 14:02:32 +0200 | [diff] [blame] | 1118 | else if( strcmp( p, "max_resend" ) == 0 ) |
| 1119 | { |
| 1120 | opt.max_resend = atoi( q ); |
| 1121 | if( opt.max_resend < 0 ) |
| 1122 | goto usage; |
| 1123 | } |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1124 | else if( strcmp( p, "request_page" ) == 0 ) |
| 1125 | opt.request_page = q; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 1126 | else if( strcmp( p, "request_size" ) == 0 ) |
| 1127 | { |
| 1128 | opt.request_size = atoi( q ); |
Hanno Becker | e4ad3e8 | 2017-09-18 15:05:46 +0100 | [diff] [blame] | 1129 | if( opt.request_size < 0 || |
| 1130 | opt.request_size > MAX_REQUEST_SIZE ) |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 1131 | goto usage; |
| 1132 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1133 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 1134 | opt.ca_file = q; |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 1135 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 1136 | opt.ca_path = q; |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 1137 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 1138 | opt.crt_file = q; |
| 1139 | else if( strcmp( p, "key_file" ) == 0 ) |
| 1140 | opt.key_file = q; |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 1141 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1142 | else if( strcmp( p, "key_opaque" ) == 0 ) |
| 1143 | opt.key_opaque = atoi( q ); |
| 1144 | #endif |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1145 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 1146 | else if( strcmp( p, "cid" ) == 0 ) |
| 1147 | { |
| 1148 | opt.cid_enabled = atoi( q ); |
| 1149 | if( opt.cid_enabled != 0 && opt.cid_enabled != 1 ) |
| 1150 | goto usage; |
| 1151 | } |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1152 | else if( strcmp( p, "cid_renego" ) == 0 ) |
| 1153 | { |
| 1154 | opt.cid_enabled_renego = atoi( q ); |
| 1155 | if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 ) |
| 1156 | goto usage; |
| 1157 | } |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 1158 | else if( strcmp( p, "cid_val" ) == 0 ) |
| 1159 | { |
| 1160 | opt.cid_val = q; |
| 1161 | } |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1162 | else if( strcmp( p, "cid_val_renego" ) == 0 ) |
| 1163 | { |
| 1164 | opt.cid_val_renego = q; |
| 1165 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1166 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1167 | else if( strcmp( p, "psk" ) == 0 ) |
| 1168 | opt.psk = q; |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1169 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 1170 | else if( strcmp( p, "psk_opaque" ) == 0 ) |
| 1171 | opt.psk_opaque = atoi( q ); |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1172 | #endif |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 1173 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1174 | else if( strcmp( p, "ca_callback" ) == 0) |
| 1175 | opt.ca_callback = atoi( q ); |
| 1176 | #endif |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1177 | else if( strcmp( p, "psk_identity" ) == 0 ) |
| 1178 | opt.psk_identity = q; |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 1179 | else if( strcmp( p, "ecjpake_pw" ) == 0 ) |
| 1180 | opt.ecjpake_pw = q; |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 1181 | else if( strcmp( p, "ec_max_ops" ) == 0 ) |
| 1182 | opt.ec_max_ops = atoi( q ); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1183 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 1184 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q ); |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1186 | |
Manuel Pégourié-Gonnard | 8de259b | 2014-06-11 14:19:06 +0200 | [diff] [blame] | 1187 | if( opt.force_ciphersuite[0] == 0 ) |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 1188 | { |
| 1189 | ret = 2; |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1190 | goto usage; |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 1191 | } |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 1192 | opt.force_ciphersuite[1] = 0; |
| 1193 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1194 | else if( strcmp( p, "renegotiation" ) == 0 ) |
| 1195 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1196 | opt.renegotiation = (atoi( q )) ? |
| 1197 | MBEDTLS_SSL_RENEGOTIATION_ENABLED : |
| 1198 | MBEDTLS_SSL_RENEGOTIATION_DISABLED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1199 | } |
| 1200 | else if( strcmp( p, "allow_legacy" ) == 0 ) |
| 1201 | { |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1202 | switch( atoi( q ) ) |
| 1203 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1204 | case -1: |
| 1205 | opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; |
| 1206 | break; |
| 1207 | case 0: |
| 1208 | opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 1209 | break; |
| 1210 | case 1: |
| 1211 | opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; |
| 1212 | break; |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1213 | default: goto usage; |
| 1214 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1215 | } |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1216 | else if( strcmp( p, "renegotiate" ) == 0 ) |
| 1217 | { |
| 1218 | opt.renegotiate = atoi( q ); |
| 1219 | if( opt.renegotiate < 0 || opt.renegotiate > 1 ) |
| 1220 | goto usage; |
| 1221 | } |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 1222 | else if( strcmp( p, "exchanges" ) == 0 ) |
| 1223 | { |
| 1224 | opt.exchanges = atoi( q ); |
| 1225 | if( opt.exchanges < 1 ) |
| 1226 | goto usage; |
| 1227 | } |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1228 | else if( strcmp( p, "reconnect" ) == 0 ) |
| 1229 | { |
| 1230 | opt.reconnect = atoi( q ); |
Manuel Pégourié-Gonnard | cf2e97e | 2013-08-02 15:04:36 +0200 | [diff] [blame] | 1231 | if( opt.reconnect < 0 || opt.reconnect > 2 ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 1232 | goto usage; |
| 1233 | } |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1234 | else if( strcmp( p, "reco_delay" ) == 0 ) |
| 1235 | { |
| 1236 | opt.reco_delay = atoi( q ); |
| 1237 | if( opt.reco_delay < 0 ) |
| 1238 | goto usage; |
| 1239 | } |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 1240 | else if( strcmp( p, "reconnect_hard" ) == 0 ) |
| 1241 | { |
| 1242 | opt.reconnect_hard = atoi( q ); |
| 1243 | if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 ) |
| 1244 | goto usage; |
| 1245 | } |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1246 | else if( strcmp( p, "tickets" ) == 0 ) |
| 1247 | { |
| 1248 | opt.tickets = atoi( q ); |
| 1249 | if( opt.tickets < 0 || opt.tickets > 2 ) |
| 1250 | goto usage; |
| 1251 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1252 | else if( strcmp( p, "alpn" ) == 0 ) |
| 1253 | { |
| 1254 | opt.alpn_string = q; |
| 1255 | } |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1256 | else if( strcmp( p, "fallback" ) == 0 ) |
| 1257 | { |
| 1258 | switch( atoi( q ) ) |
| 1259 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1260 | case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break; |
| 1261 | case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 1262 | default: goto usage; |
| 1263 | } |
| 1264 | } |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1265 | else if( strcmp( p, "extended_ms" ) == 0 ) |
| 1266 | { |
| 1267 | switch( atoi( q ) ) |
| 1268 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1269 | case 0: |
| 1270 | opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; |
| 1271 | break; |
| 1272 | case 1: |
| 1273 | opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 1274 | break; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1275 | default: goto usage; |
| 1276 | } |
| 1277 | } |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1278 | else if( strcmp( p, "curves" ) == 0 ) |
| 1279 | opt.curves = q; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1280 | else if( strcmp( p, "etm" ) == 0 ) |
| 1281 | { |
| 1282 | switch( atoi( q ) ) |
| 1283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1284 | case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break; |
| 1285 | case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1286 | default: goto usage; |
| 1287 | } |
| 1288 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1289 | else if( strcmp( p, "min_version" ) == 0 ) |
| 1290 | { |
| 1291 | if( strcmp( q, "ssl3" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1293 | else if( strcmp( q, "tls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1294 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1295 | else if( strcmp( q, "tls1_1" ) == 0 || |
| 1296 | strcmp( q, "dtls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1297 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1298 | else if( strcmp( q, "tls1_2" ) == 0 || |
| 1299 | strcmp( q, "dtls1_2" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1301 | else |
| 1302 | goto usage; |
| 1303 | } |
| 1304 | else if( strcmp( p, "max_version" ) == 0 ) |
| 1305 | { |
| 1306 | if( strcmp( q, "ssl3" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1307 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1308 | else if( strcmp( q, "tls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1309 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1310 | else if( strcmp( q, "tls1_1" ) == 0 || |
| 1311 | strcmp( q, "dtls1" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1312 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2; |
Manuel Pégourié-Gonnard | 83218f1 | 2014-02-12 11:11:12 +0100 | [diff] [blame] | 1313 | else if( strcmp( q, "tls1_2" ) == 0 || |
| 1314 | strcmp( q, "dtls1_2" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1315 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1316 | else |
| 1317 | goto usage; |
| 1318 | } |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1319 | else if( strcmp( p, "arc4" ) == 0 ) |
| 1320 | { |
| 1321 | switch( atoi( q ) ) |
| 1322 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1323 | case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break; |
| 1324 | case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 1325 | default: goto usage; |
| 1326 | } |
| 1327 | } |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 1328 | else if( strcmp( p, "allow_sha1" ) == 0 ) |
| 1329 | { |
| 1330 | switch( atoi( q ) ) |
| 1331 | { |
| 1332 | case 0: opt.allow_sha1 = 0; break; |
| 1333 | case 1: opt.allow_sha1 = 1; break; |
| 1334 | default: goto usage; |
| 1335 | } |
| 1336 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1337 | else if( strcmp( p, "force_version" ) == 0 ) |
| 1338 | { |
| 1339 | if( strcmp( q, "ssl3" ) == 0 ) |
| 1340 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1341 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0; |
| 1342 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1343 | } |
| 1344 | else if( strcmp( q, "tls1" ) == 0 ) |
| 1345 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1346 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1; |
| 1347 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1348 | } |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1349 | else if( strcmp( q, "tls1_1" ) == 0 ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1350 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1351 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
| 1352 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1353 | } |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1354 | else if( strcmp( q, "tls1_2" ) == 0 ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1355 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1356 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1357 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1358 | } |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1359 | else if( strcmp( q, "dtls1" ) == 0 ) |
| 1360 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
| 1362 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2; |
| 1363 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1364 | } |
| 1365 | else if( strcmp( q, "dtls1_2" ) == 0 ) |
| 1366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1367 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1368 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1369 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Manuel Pégourié-Gonnard | fe3f73b | 2014-03-26 12:16:44 +0100 | [diff] [blame] | 1370 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1371 | else |
| 1372 | goto usage; |
| 1373 | } |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1374 | else if( strcmp( p, "auth_mode" ) == 0 ) |
| 1375 | { |
| 1376 | if( strcmp( q, "none" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1377 | opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1378 | else if( strcmp( q, "optional" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1379 | opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1380 | else if( strcmp( q, "required" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1381 | opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1382 | else |
| 1383 | goto usage; |
| 1384 | } |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 1385 | else if( strcmp( p, "max_frag_len" ) == 0 ) |
| 1386 | { |
| 1387 | if( strcmp( q, "512" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1388 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512; |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 1389 | else if( strcmp( q, "1024" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1390 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024; |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 1391 | else if( strcmp( q, "2048" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048; |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 1393 | else if( strcmp( q, "4096" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1394 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096; |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 1395 | else |
| 1396 | goto usage; |
| 1397 | } |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 1398 | else if( strcmp( p, "trunc_hmac" ) == 0 ) |
| 1399 | { |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 1400 | switch( atoi( q ) ) |
| 1401 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break; |
| 1403 | case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break; |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 1404 | default: goto usage; |
| 1405 | } |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 1406 | } |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1407 | else if( strcmp( p, "hs_timeout" ) == 0 ) |
| 1408 | { |
| 1409 | if( ( p = strchr( q, '-' ) ) == NULL ) |
| 1410 | goto usage; |
| 1411 | *p++ = '\0'; |
| 1412 | opt.hs_to_min = atoi( q ); |
| 1413 | opt.hs_to_max = atoi( p ); |
| 1414 | if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min ) |
| 1415 | goto usage; |
| 1416 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 1417 | else if( strcmp( p, "mtu" ) == 0 ) |
| 1418 | { |
| 1419 | opt.dtls_mtu = atoi( q ); |
| 1420 | if( opt.dtls_mtu < 0 ) |
| 1421 | goto usage; |
| 1422 | } |
Hanno Becker | 4d61591 | 2018-08-14 13:33:30 +0100 | [diff] [blame] | 1423 | else if( strcmp( p, "dgram_packing" ) == 0 ) |
| 1424 | { |
| 1425 | opt.dgram_packing = atoi( q ); |
| 1426 | if( opt.dgram_packing != 0 && |
| 1427 | opt.dgram_packing != 1 ) |
| 1428 | { |
| 1429 | goto usage; |
| 1430 | } |
| 1431 | } |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 1432 | else if( strcmp( p, "recsplit" ) == 0 ) |
| 1433 | { |
| 1434 | opt.recsplit = atoi( q ); |
| 1435 | if( opt.recsplit < 0 || opt.recsplit > 1 ) |
| 1436 | goto usage; |
| 1437 | } |
Manuel Pégourié-Gonnard | 9096682 | 2015-06-11 17:02:29 +0200 | [diff] [blame] | 1438 | else if( strcmp( p, "dhmlen" ) == 0 ) |
| 1439 | { |
| 1440 | opt.dhmlen = atoi( q ); |
| 1441 | if( opt.dhmlen < 0 ) |
| 1442 | goto usage; |
| 1443 | } |
Andres Amaya Garcia | bc81884 | 2018-10-16 21:08:38 +0100 | [diff] [blame] | 1444 | else if( strcmp( p, "query_config" ) == 0 ) |
| 1445 | { |
| 1446 | return query_config( q ); |
| 1447 | } |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1448 | else if( strcmp( p, "eap_tls" ) == 0 ) |
| 1449 | { |
| 1450 | opt.eap_tls = atoi( q ); |
| 1451 | if( opt.eap_tls < 0 || opt.eap_tls > 1 ) |
| 1452 | goto usage; |
| 1453 | } |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1454 | else if( strcmp( p, "reproducible" ) == 0 ) |
| 1455 | { |
| 1456 | opt.reproducible = 1; |
| 1457 | } |
Paul Bakker | 9caf2d2 | 2010-02-18 19:37:19 +0000 | [diff] [blame] | 1458 | else |
| 1459 | goto usage; |
| 1460 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1461 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1462 | /* Event-driven IO is incompatible with the above custom |
| 1463 | * receive and send functions, as the polling builds on |
| 1464 | * refers to the underlying net_context. */ |
| 1465 | if( opt.event == 1 && opt.nbio != 1 ) |
| 1466 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 1467 | mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1468 | opt.nbio = 1; |
| 1469 | } |
| 1470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | #if defined(MBEDTLS_DEBUG_C) |
| 1472 | mbedtls_debug_set_threshold( opt.debug_level ); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 1473 | #endif |
| 1474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1475 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1476 | /* |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1477 | * Unhexify the pre-shared key if any is given |
| 1478 | */ |
| 1479 | if( strlen( opt.psk ) ) |
| 1480 | { |
Hanno Becker | 1f583ee | 2019-04-09 17:12:56 +0100 | [diff] [blame] | 1481 | psk_len = strlen( opt.psk ) / 2; |
| 1482 | if( psk_len > sizeof( psk ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1483 | { |
Hanno Becker | 1f583ee | 2019-04-09 17:12:56 +0100 | [diff] [blame] | 1484 | mbedtls_printf( "pre-shared key too long\n" ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1485 | goto exit; |
| 1486 | } |
| 1487 | |
Hanno Becker | 1f583ee | 2019-04-09 17:12:56 +0100 | [diff] [blame] | 1488 | if( unhexify( opt.psk, psk ) != 0 ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1489 | { |
Hanno Becker | 1f583ee | 2019-04-09 17:12:56 +0100 | [diff] [blame] | 1490 | mbedtls_printf( "pre-shared key not valid hex\n" ); |
| 1491 | goto exit; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1492 | } |
| 1493 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1494 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1495 | |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1496 | |
| 1497 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 1498 | if( opt.psk_opaque != 0 ) |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1499 | { |
| 1500 | if( opt.psk == NULL ) |
| 1501 | { |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 1502 | mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" ); |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1503 | ret = 2; |
| 1504 | goto usage; |
| 1505 | } |
| 1506 | |
| 1507 | if( opt.force_ciphersuite[0] <= 0 ) |
| 1508 | { |
| 1509 | mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" ); |
| 1510 | ret = 2; |
| 1511 | goto usage; |
| 1512 | } |
| 1513 | } |
| 1514 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1515 | |
| 1516 | if( opt.force_ciphersuite[0] > 0 ) |
| 1517 | { |
| 1518 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 1519 | ciphersuite_info = |
| 1520 | mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] ); |
| 1521 | |
| 1522 | if( opt.max_version != -1 && |
| 1523 | ciphersuite_info->min_minor_ver > opt.max_version ) |
| 1524 | { |
| 1525 | mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" ); |
| 1526 | ret = 2; |
| 1527 | goto usage; |
| 1528 | } |
| 1529 | if( opt.min_version != -1 && |
| 1530 | ciphersuite_info->max_minor_ver < opt.min_version ) |
| 1531 | { |
| 1532 | mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" ); |
| 1533 | ret = 2; |
| 1534 | goto usage; |
| 1535 | } |
| 1536 | |
| 1537 | /* If the server selects a version that's not supported by |
| 1538 | * this suite, then there will be no common ciphersuite... */ |
| 1539 | if( opt.max_version == -1 || |
| 1540 | opt.max_version > ciphersuite_info->max_minor_ver ) |
| 1541 | { |
| 1542 | opt.max_version = ciphersuite_info->max_minor_ver; |
| 1543 | } |
| 1544 | if( opt.min_version < ciphersuite_info->min_minor_ver ) |
| 1545 | { |
| 1546 | opt.min_version = ciphersuite_info->min_minor_ver; |
| 1547 | /* DTLS starts with TLS 1.1 */ |
| 1548 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 1549 | opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 1550 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2; |
| 1551 | } |
| 1552 | |
| 1553 | /* Enable RC4 if needed and not explicitly disabled */ |
| 1554 | if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) |
| 1555 | { |
| 1556 | if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED ) |
| 1557 | { |
| 1558 | mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" ); |
| 1559 | ret = 2; |
| 1560 | goto usage; |
| 1561 | } |
| 1562 | |
| 1563 | opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; |
| 1564 | } |
| 1565 | |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 1566 | |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1567 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 1568 | if( opt.psk_opaque != 0 ) |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 1569 | { |
| 1570 | /* Ensure that the chosen ciphersuite is PSK-only; we must know |
| 1571 | * the ciphersuite in advance to set the correct policy for the |
| 1572 | * PSK key slot. This limitation might go away in the future. */ |
| 1573 | if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK || |
| 1574 | opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 1575 | { |
| 1576 | mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" ); |
| 1577 | ret = 2; |
| 1578 | goto usage; |
| 1579 | } |
| 1580 | |
| 1581 | /* Determine KDF algorithm the opaque PSK will be used in. */ |
| 1582 | #if defined(MBEDTLS_SHA512_C) |
| 1583 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 1584 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 1585 | else |
| 1586 | #endif /* MBEDTLS_SHA512_C */ |
| 1587 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1588 | } |
| 1589 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1590 | } |
| 1591 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1592 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1593 | cid_len = strlen( opt.cid_val ) / 2; |
| 1594 | if( cid_len > sizeof( cid ) ) |
| 1595 | { |
| 1596 | mbedtls_printf( "CID too long\n" ); |
| 1597 | goto exit; |
| 1598 | } |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 1599 | |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1600 | if( unhexify( opt.cid_val, cid ) != 0 ) |
| 1601 | { |
| 1602 | mbedtls_printf( "CID not valid hex\n" ); |
| 1603 | goto exit; |
| 1604 | } |
| 1605 | |
| 1606 | /* Keep CID settings for renegotiation unless |
| 1607 | * specified otherwise. */ |
| 1608 | if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO ) |
| 1609 | opt.cid_enabled_renego = opt.cid_enabled; |
| 1610 | if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO ) |
| 1611 | opt.cid_val_renego = opt.cid_val; |
| 1612 | |
| 1613 | cid_renego_len = strlen( opt.cid_val_renego ) / 2; |
| 1614 | if( cid_renego_len > sizeof( cid_renego ) ) |
| 1615 | { |
| 1616 | mbedtls_printf( "CID too long\n" ); |
| 1617 | goto exit; |
| 1618 | } |
| 1619 | |
| 1620 | if( unhexify( opt.cid_val_renego, cid_renego ) != 0 ) |
| 1621 | { |
| 1622 | mbedtls_printf( "CID not valid hex\n" ); |
| 1623 | goto exit; |
| 1624 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1625 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 1626 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1627 | #if defined(MBEDTLS_ECP_C) |
| 1628 | if( opt.curves != NULL ) |
| 1629 | { |
| 1630 | p = (char *) opt.curves; |
| 1631 | i = 0; |
| 1632 | |
| 1633 | if( strcmp( p, "none" ) == 0 ) |
| 1634 | { |
| 1635 | curve_list[0] = MBEDTLS_ECP_DP_NONE; |
| 1636 | } |
| 1637 | else if( strcmp( p, "default" ) != 0 ) |
| 1638 | { |
| 1639 | /* Leave room for a final NULL in curve list */ |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1640 | while( i < CURVE_LIST_SIZE - 1 && *p != '\0' ) |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1641 | { |
| 1642 | q = p; |
| 1643 | |
| 1644 | /* Terminate the current string */ |
| 1645 | while( *p != ',' && *p != '\0' ) |
| 1646 | p++; |
| 1647 | if( *p == ',' ) |
| 1648 | *p++ = '\0'; |
| 1649 | |
| 1650 | if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL ) |
| 1651 | { |
| 1652 | curve_list[i++] = curve_cur->grp_id; |
| 1653 | } |
| 1654 | else |
| 1655 | { |
| 1656 | mbedtls_printf( "unknown curve %s\n", q ); |
| 1657 | mbedtls_printf( "supported curves: " ); |
| 1658 | for( curve_cur = mbedtls_ecp_curve_list(); |
| 1659 | curve_cur->grp_id != MBEDTLS_ECP_DP_NONE; |
| 1660 | curve_cur++ ) |
| 1661 | { |
| 1662 | mbedtls_printf( "%s ", curve_cur->name ); |
| 1663 | } |
| 1664 | mbedtls_printf( "\n" ); |
| 1665 | goto exit; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | mbedtls_printf("Number of curves: %d\n", i ); |
| 1670 | |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1671 | if( i == CURVE_LIST_SIZE - 1 && *p != '\0' ) |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1672 | { |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1673 | mbedtls_printf( "curves list too long, maximum %d", |
| 1674 | CURVE_LIST_SIZE - 1 ); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1675 | goto exit; |
| 1676 | } |
| 1677 | |
| 1678 | curve_list[i] = MBEDTLS_ECP_DP_NONE; |
| 1679 | } |
| 1680 | } |
| 1681 | #endif /* MBEDTLS_ECP_C */ |
| 1682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1683 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1684 | if( opt.alpn_string != NULL ) |
| 1685 | { |
| 1686 | p = (char *) opt.alpn_string; |
| 1687 | i = 0; |
| 1688 | |
| 1689 | /* Leave room for a final NULL in alpn_list */ |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1690 | while( i < ALPN_LIST_SIZE - 1 && *p != '\0' ) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1691 | { |
| 1692 | alpn_list[i++] = p; |
| 1693 | |
| 1694 | /* Terminate the current string and move on to next one */ |
| 1695 | while( *p != ',' && *p != '\0' ) |
| 1696 | p++; |
| 1697 | if( *p == ',' ) |
| 1698 | *p++ = '\0'; |
| 1699 | } |
| 1700 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1701 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1702 | |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1703 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1704 | * 0. Initialize the RNG and the session data |
| 1705 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1706 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 1707 | fflush( stdout ); |
| 1708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1709 | mbedtls_entropy_init( &entropy ); |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 1710 | if (opt.reproducible) |
| 1711 | { |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1712 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 1713 | &entropy, (const unsigned char *) pers, |
| 1714 | strlen( pers ) ) ) != 0 ) |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1715 | { |
| 1716 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 1717 | -ret ); |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1718 | goto exit; |
| 1719 | } |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 1720 | } |
| 1721 | else |
| 1722 | { |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1723 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 1724 | &entropy, (const unsigned char *) pers, |
| 1725 | strlen( pers ) ) ) != 0 ) |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1726 | { |
| 1727 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 1728 | -ret ); |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1729 | goto exit; |
| 1730 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1733 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1736 | /* |
| 1737 | * 1.1. Load the trusted CA |
| 1738 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1739 | mbedtls_printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1740 | fflush( stdout ); |
| 1741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 1743 | if( strlen( opt.ca_path ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1744 | if( strcmp( opt.ca_path, "none" ) == 0 ) |
| 1745 | ret = 0; |
| 1746 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1747 | ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path ); |
Paul Bakker | 8d91458 | 2012-06-04 12:46:42 +0000 | [diff] [blame] | 1748 | else if( strlen( opt.ca_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1749 | if( strcmp( opt.ca_file, "none" ) == 0 ) |
| 1750 | ret = 0; |
| 1751 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1752 | ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1753 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1754 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1755 | #if defined(MBEDTLS_CERTS_C) |
| 1756 | for( i = 0; mbedtls_test_cas[i] != NULL; i++ ) |
Manuel Pégourié-Gonnard | 2f16506 | 2015-03-27 10:20:26 +0100 | [diff] [blame] | 1757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1758 | ret = mbedtls_x509_crt_parse( &cacert, |
| 1759 | (const unsigned char *) mbedtls_test_cas[i], |
| 1760 | mbedtls_test_cas_len[i] ); |
Manuel Pégourié-Gonnard | 2f16506 | 2015-03-27 10:20:26 +0100 | [diff] [blame] | 1761 | if( ret != 0 ) |
| 1762 | break; |
| 1763 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1764 | #else |
| 1765 | { |
| 1766 | ret = 1; |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1767 | mbedtls_printf( "MBEDTLS_CERTS_C not defined." ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1768 | } |
| 1769 | #endif |
Paul Bakker | 4248823 | 2012-05-16 08:21:05 +0000 | [diff] [blame] | 1770 | if( ret < 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1771 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1772 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", |
| 1773 | -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1774 | goto exit; |
| 1775 | } |
| 1776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1777 | mbedtls_printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1778 | |
| 1779 | /* |
| 1780 | * 1.2. Load own certificate and private key |
| 1781 | * |
| 1782 | * (can be skipped if client authentication is not required) |
| 1783 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1784 | mbedtls_printf( " . Loading the client cert. and key..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1785 | fflush( stdout ); |
| 1786 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1787 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 1788 | if( strlen( opt.crt_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1789 | if( strcmp( opt.crt_file, "none" ) == 0 ) |
| 1790 | ret = 0; |
| 1791 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1792 | ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1793 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1794 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | #if defined(MBEDTLS_CERTS_C) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1796 | ret = mbedtls_x509_crt_parse( &clicert, |
| 1797 | (const unsigned char *) mbedtls_test_cli_crt, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1798 | mbedtls_test_cli_crt_len ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1799 | #else |
| 1800 | { |
| 1801 | ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1802 | mbedtls_printf("MBEDTLS_CERTS_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1803 | } |
| 1804 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1805 | if( ret != 0 ) |
| 1806 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 1807 | mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", |
| 1808 | -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1809 | goto exit; |
| 1810 | } |
| 1811 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1812 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 1813 | if( strlen( opt.key_file ) ) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 1814 | if( strcmp( opt.key_file, "none" ) == 0 ) |
| 1815 | ret = 0; |
| 1816 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1817 | ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ); |
Paul Bakker | 6796839 | 2010-07-18 08:28:20 +0000 | [diff] [blame] | 1818 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1819 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1820 | #if defined(MBEDTLS_CERTS_C) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1821 | ret = mbedtls_pk_parse_key( &pkey, |
| 1822 | (const unsigned char *) mbedtls_test_cli_key, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1823 | mbedtls_test_cli_key_len, NULL, 0 ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1824 | #else |
| 1825 | { |
| 1826 | ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1827 | mbedtls_printf("MBEDTLS_CERTS_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1828 | } |
| 1829 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1830 | if( ret != 0 ) |
| 1831 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 1832 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", |
| 1833 | -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1834 | goto exit; |
| 1835 | } |
| 1836 | |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 1837 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1838 | if( opt.key_opaque != 0 ) |
| 1839 | { |
Manuel Pégourié-Gonnard | cfdf8f4 | 2018-11-08 09:52:25 +0100 | [diff] [blame] | 1840 | if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot, |
| 1841 | PSA_ALG_SHA_256 ) ) != 0 ) |
| 1842 | { |
| 1843 | mbedtls_printf( " failed\n ! " |
| 1844 | "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret ); |
| 1845 | goto exit; |
| 1846 | } |
Manuel Pégourié-Gonnard | ef68be4 | 2018-11-07 09:42:35 +0100 | [diff] [blame] | 1847 | } |
| 1848 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1849 | |
Manuel Pégourié-Gonnard | cfdf8f4 | 2018-11-08 09:52:25 +0100 | [diff] [blame] | 1850 | mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1851 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1852 | |
| 1853 | /* |
| 1854 | * 2. Start the connection |
| 1855 | */ |
Manuel Pégourié-Gonnard | 0d8780b | 2014-02-25 11:11:26 +0100 | [diff] [blame] | 1856 | if( opt.server_addr == NULL) |
| 1857 | opt.server_addr = opt.server_name; |
| 1858 | |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 1859 | mbedtls_printf( " . Connecting to %s/%s/%s...", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1860 | opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp", |
Manuel Pégourié-Gonnard | 8a06d9c | 2014-03-23 18:23:41 +0100 | [diff] [blame] | 1861 | opt.server_addr, opt.server_port ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1862 | fflush( stdout ); |
| 1863 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1864 | if( ( ret = mbedtls_net_connect( &server_fd, |
| 1865 | opt.server_addr, opt.server_port, |
| 1866 | opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? |
| 1867 | MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1868 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 1869 | mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", |
| 1870 | -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1871 | goto exit; |
| 1872 | } |
| 1873 | |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1874 | if( opt.nbio > 0 ) |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 1875 | ret = mbedtls_net_set_nonblock( &server_fd ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1876 | else |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 1877 | ret = mbedtls_net_set_block( &server_fd ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1878 | if( ret != 0 ) |
| 1879 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 1880 | mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", |
| 1881 | -ret ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1882 | goto exit; |
| 1883 | } |
| 1884 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1886 | |
| 1887 | /* |
| 1888 | * 3. Setup stuff |
| 1889 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1890 | mbedtls_printf( " . Setting up the SSL/TLS structure..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1891 | fflush( stdout ); |
| 1892 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1893 | if( ( ret = mbedtls_ssl_config_defaults( &conf, |
| 1894 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 1895 | opt.transport, |
| 1896 | MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1897 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 1898 | mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", |
| 1899 | -ret ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1900 | goto exit; |
| 1901 | } |
| 1902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1903 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 1904 | /* The default algorithms profile disables SHA-1, but our tests still |
| 1905 | rely on it heavily. */ |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 1906 | if( opt.allow_sha1 > 0 ) |
| 1907 | { |
| 1908 | crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ); |
| 1909 | mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test ); |
| 1910 | mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test ); |
| 1911 | } |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 1912 | |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 1913 | if( opt.context_crt_cb == 0 ) |
| 1914 | mbedtls_ssl_conf_verify( &conf, my_verify, NULL ); |
| 1915 | |
Hanno Becker | a1051b4 | 2019-02-26 11:38:29 +0000 | [diff] [blame] | 1916 | memset( peer_crt_info, 0, sizeof( peer_crt_info ) ); |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 1917 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 915275b | 2012-09-28 07:10:55 +0000 | [diff] [blame] | 1918 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1919 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1920 | if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 1921 | { |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1922 | if( opt.cid_enabled == 1 && |
| 1923 | opt.cid_enabled_renego == 1 && |
| 1924 | cid_len != cid_renego_len ) |
| 1925 | { |
| 1926 | mbedtls_printf( "CID length must not change during renegotiation\n" ); |
| 1927 | goto usage; |
| 1928 | } |
| 1929 | |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1930 | if( opt.cid_enabled == 1 ) |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 1931 | ret = mbedtls_ssl_conf_cid( &conf, cid_len, |
| 1932 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1933 | else |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 1934 | ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len, |
| 1935 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1936 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 1937 | if( ret != 0 ) |
| 1938 | { |
Hanno Becker | d5eed42 | 2019-05-23 16:58:22 +0100 | [diff] [blame] | 1939 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n", |
| 1940 | -ret ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 1941 | goto exit; |
| 1942 | } |
| 1943 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1944 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 1945 | |
Manuel Pégourié-Gonnard | fa44f20 | 2015-03-27 17:52:25 +0100 | [diff] [blame] | 1946 | if( opt.auth_mode != DFL_AUTH_MODE ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1947 | mbedtls_ssl_conf_authmode( &conf, opt.auth_mode ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1948 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1949 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1950 | if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1951 | mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, |
| 1952 | opt.hs_to_max ); |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 1953 | |
Hanno Becker | 4d61591 | 2018-08-14 13:33:30 +0100 | [diff] [blame] | 1954 | if( opt.dgram_packing != DFL_DGRAM_PACKING ) |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1955 | mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1956 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1957 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1958 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1959 | if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1960 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 1961 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", |
| 1962 | ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 1963 | goto exit; |
| 1964 | } |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 1965 | #endif |
Manuel Pégourié-Gonnard | 0df6b1f | 2013-07-16 13:39:57 +0200 | [diff] [blame] | 1966 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1967 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 265fe99 | 2015-01-09 12:43:35 +0100 | [diff] [blame] | 1968 | if( opt.trunc_hmac != DFL_TRUNC_HMAC ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1969 | mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac ); |
Paul Bakker | 1f2bc62 | 2013-08-15 13:45:55 +0200 | [diff] [blame] | 1970 | #endif |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 1971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1972 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1973 | if( opt.extended_ms != DFL_EXTENDED_MS ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1974 | mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms ); |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1975 | #endif |
| 1976 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1977 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1978 | if( opt.etm != DFL_ETM ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1979 | mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm ); |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1980 | #endif |
| 1981 | |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1982 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1983 | if( opt.eap_tls != 0 ) |
| 1984 | mbedtls_ssl_conf_export_keys_ext_cb( &conf, eap_tls_key_derivation, |
| 1985 | &eap_tls_keying ); |
| 1986 | #endif |
| 1987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1988 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 1989 | if( opt.recsplit != DFL_RECSPLIT ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1990 | mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1991 | ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED |
| 1992 | : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ); |
Manuel Pégourié-Gonnard | c82ee35 | 2015-01-07 16:35:25 +0100 | [diff] [blame] | 1993 | #endif |
| 1994 | |
Manuel Pégourié-Gonnard | 9096682 | 2015-06-11 17:02:29 +0200 | [diff] [blame] | 1995 | #if defined(MBEDTLS_DHM_C) |
| 1996 | if( opt.dhmlen != DFL_DHMLEN ) |
| 1997 | mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen ); |
| 1998 | #endif |
| 1999 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2000 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2001 | if( opt.alpn_string != NULL ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2002 | if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2003 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2004 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", |
| 2005 | ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2006 | goto exit; |
| 2007 | } |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2008 | #endif |
| 2009 | |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 2010 | if (opt.reproducible) |
| 2011 | { |
| 2012 | srand( 1 ); |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 2013 | mbedtls_ssl_conf_rng( &conf, dummy_random, &ctr_drbg ); |
Philippe Antoine | f91b372 | 2019-06-11 16:02:43 +0200 | [diff] [blame] | 2014 | #if defined(MBEDTLS_HAVE_TIME) |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 2015 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
| 2016 | mbedtls_platform_set_time( dummy_constant_time ); |
| 2017 | #else |
Philippe Antoine | 7c9d724 | 2019-06-11 12:11:36 +0200 | [diff] [blame] | 2018 | fprintf( stderr, "Warning: reproducible option used without constant time\n" ); |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 2019 | #endif |
Philippe Antoine | 0ff84fb | 2019-06-11 12:15:17 +0200 | [diff] [blame] | 2020 | #endif |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 2021 | } |
| 2022 | else |
| 2023 | { |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 2024 | mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 2025 | } |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2026 | mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 2027 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2028 | mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2030 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2031 | mbedtls_ssl_conf_session_tickets( &conf, opt.tickets ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 2032 | #endif |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2033 | |
Paul Bakker | 645ce3a | 2012-10-31 12:32:41 +0000 | [diff] [blame] | 2034 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2035 | mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 2036 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 2037 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 2038 | if( opt.arc4 != DFL_ARC4 ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2039 | mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 ); |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 2040 | #endif |
Paul Bakker | 5193688 | 2011-02-20 16:05:58 +0000 | [diff] [blame] | 2041 | |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 2042 | if( opt.allow_legacy != DFL_ALLOW_LEGACY ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2043 | mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2044 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2045 | mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2046 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2047 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2048 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 2049 | if( strcmp( opt.ca_path, "none" ) != 0 && |
| 2050 | strcmp( opt.ca_file, "none" ) != 0 ) |
| 2051 | { |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 2052 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 2053 | if( opt.ca_callback != 0 ) |
Hanno Becker | cbb5903 | 2019-03-28 14:14:22 +0000 | [diff] [blame] | 2054 | mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert ); |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 2055 | else |
| 2056 | #endif |
| 2057 | mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 2058 | } |
| 2059 | if( strcmp( opt.crt_file, "none" ) != 0 && |
| 2060 | strcmp( opt.key_file, "none" ) != 0 ) |
| 2061 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2062 | if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2063 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2064 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", |
| 2065 | ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2066 | goto exit; |
| 2067 | } |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 2068 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2069 | #endif |
| 2070 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 2071 | #if defined(MBEDTLS_ECP_C) |
| 2072 | if( opt.curves != NULL && |
| 2073 | strcmp( opt.curves, "default" ) != 0 ) |
| 2074 | { |
| 2075 | mbedtls_ssl_conf_curves( &conf, curve_list ); |
| 2076 | } |
| 2077 | #endif |
| 2078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2079 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 2080 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2081 | if( opt.psk_opaque != 0 ) |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 2082 | { |
| 2083 | /* The algorithm has already been determined earlier. */ |
Hanno Becker | 37519ea | 2019-01-25 14:26:01 +0000 | [diff] [blame] | 2084 | status = psa_allocate_key( &slot ); |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2085 | if( status != PSA_SUCCESS ) |
| 2086 | { |
| 2087 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2088 | goto exit; |
| 2089 | } |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 2090 | |
Hanno Becker | 1387124 | 2019-01-25 14:26:26 +0000 | [diff] [blame] | 2091 | policy = psa_key_policy_init(); |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 2092 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 2093 | |
| 2094 | status = psa_set_key_policy( slot, &policy ); |
| 2095 | if( status != PSA_SUCCESS ) |
| 2096 | { |
| 2097 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2098 | goto exit; |
| 2099 | } |
| 2100 | |
| 2101 | status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len ); |
| 2102 | if( status != PSA_SUCCESS ) |
| 2103 | { |
| 2104 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2105 | goto exit; |
| 2106 | } |
| 2107 | |
| 2108 | if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot, |
| 2109 | (const unsigned char *) opt.psk_identity, |
Hanno Becker | 5cd607b | 2018-11-05 12:52:42 +0000 | [diff] [blame] | 2110 | strlen( opt.psk_identity ) ) ) != 0 ) |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 2111 | { |
| 2112 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n", |
| 2113 | ret ); |
| 2114 | goto exit; |
| 2115 | } |
| 2116 | } |
| 2117 | else |
| 2118 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2119 | if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len, |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2120 | (const unsigned char *) opt.psk_identity, |
| 2121 | strlen( opt.psk_identity ) ) ) != 0 ) |
| 2122 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2123 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", |
| 2124 | ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2125 | goto exit; |
| 2126 | } |
Hanno Becker | e86964c | 2018-10-23 11:37:50 +0100 | [diff] [blame] | 2127 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2128 | |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 2129 | if( opt.min_version != DFL_MIN_VERSION ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2130 | mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 2131 | opt.min_version ); |
Manuel Pégourié-Gonnard | 864a81f | 2014-02-10 14:25:10 +0100 | [diff] [blame] | 2132 | |
Manuel Pégourié-Gonnard | 8c8be1e | 2015-03-31 14:21:11 +0200 | [diff] [blame] | 2133 | if( opt.max_version != DFL_MAX_VERSION ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2134 | mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 2135 | opt.max_version ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2137 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 2138 | if( opt.fallback != DFL_FALLBACK ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2139 | mbedtls_ssl_conf_fallback( &conf, opt.fallback ); |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 2140 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2141 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2142 | if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) |
| 2143 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2144 | mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", |
| 2145 | -ret ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2146 | goto exit; |
| 2147 | } |
| 2148 | |
| 2149 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2150 | if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 ) |
| 2151 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2152 | mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", |
| 2153 | ret ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2154 | goto exit; |
| 2155 | } |
| 2156 | #endif |
| 2157 | |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 2158 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2159 | if( opt.ecjpake_pw != DFL_ECJPAKE_PW ) |
| 2160 | { |
| 2161 | if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl, |
| 2162 | (const unsigned char *) opt.ecjpake_pw, |
| 2163 | strlen( opt.ecjpake_pw ) ) ) != 0 ) |
| 2164 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2165 | mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", |
| 2166 | ret ); |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 2167 | goto exit; |
| 2168 | } |
| 2169 | } |
| 2170 | #endif |
| 2171 | |
Hanno Becker | bb425db | 2019-04-03 12:59:58 +0100 | [diff] [blame] | 2172 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2173 | if( opt.context_crt_cb == 1 ) |
| 2174 | mbedtls_ssl_set_verify( &ssl, my_verify, NULL ); |
| 2175 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2176 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2177 | if( opt.nbio == 2 ) |
| 2178 | mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL ); |
| 2179 | else |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2180 | mbedtls_ssl_set_bio( &ssl, &server_fd, |
| 2181 | mbedtls_net_send, mbedtls_net_recv, |
Manuel Pégourié-Gonnard | d4f04db | 2015-05-14 18:58:17 +0200 | [diff] [blame] | 2182 | opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2183 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2184 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 2185 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 2186 | { |
| 2187 | if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled, |
| 2188 | cid, cid_len ) ) != 0 ) |
| 2189 | { |
| 2190 | mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n", |
| 2191 | ret ); |
| 2192 | goto exit; |
| 2193 | } |
| 2194 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2195 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 2196 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 2197 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2198 | if( opt.dtls_mtu != DFL_DTLS_MTU ) |
| 2199 | mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu ); |
| 2200 | #endif |
| 2201 | |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2202 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 2203 | mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay, |
| 2204 | mbedtls_timing_get_delay ); |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2205 | #endif |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 2206 | |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 2207 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 2208 | if( opt.ec_max_ops != DFL_EC_MAX_OPS ) |
| 2209 | mbedtls_ecp_set_max_ops( opt.ec_max_ops ); |
| 2210 | #endif |
| 2211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2212 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 2213 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2214 | /* |
| 2215 | * 4. Handshake |
| 2216 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2217 | mbedtls_printf( " . Performing the SSL/TLS handshake..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2218 | fflush( stdout ); |
| 2219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2220 | while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2221 | { |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 2222 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 2223 | ret != MBEDTLS_ERR_SSL_WANT_WRITE && |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2224 | ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 2225 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2226 | mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n", |
| 2227 | -ret ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2228 | if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ) |
| 2229 | mbedtls_printf( |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 2230 | " Unable to verify the server's certificate. " |
| 2231 | "Either it is invalid,\n" |
| 2232 | " or you didn't set ca_file or ca_path " |
| 2233 | "to an appropriate value.\n" |
| 2234 | " Alternatively, you may want to use " |
| 2235 | "auth_mode=optional for testing purposes.\n" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2236 | mbedtls_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2237 | goto exit; |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 2238 | } |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2239 | |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2240 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 25781f9 | 2018-10-15 15:28:16 +0200 | [diff] [blame] | 2241 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2242 | continue; |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2243 | #endif |
| 2244 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2245 | /* For event-driven IO, wait for socket to become available */ |
| 2246 | if( opt.event == 1 /* level triggered IO */ ) |
| 2247 | { |
| 2248 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 2249 | ret = idle( &server_fd, &timer, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2250 | #else |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 2251 | ret = idle( &server_fd, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2252 | #endif |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 2253 | if( ret != 0 ) |
| 2254 | goto exit; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2255 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2258 | mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n", |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2259 | mbedtls_ssl_get_version( &ssl ), |
| 2260 | mbedtls_ssl_get_ciphersuite( &ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2262 | if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 ) |
| 2263 | mbedtls_printf( " [ Record expansion is %d ]\n", ret ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 2264 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2265 | mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 2266 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2267 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2268 | mbedtls_printf( " [ Maximum fragment length is %u ]\n", |
| 2269 | (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) ); |
| 2270 | #endif |
| 2271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2272 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2273 | if( opt.alpn_string != NULL ) |
| 2274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2275 | const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl ); |
| 2276 | mbedtls_printf( " [ Application Layer Protocol is %s ]\n", |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2277 | alp ? alp : "(none)" ); |
| 2278 | } |
| 2279 | #endif |
| 2280 | |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2281 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 2282 | if( opt.eap_tls != 0 ) |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2283 | { |
| 2284 | size_t j = 0; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 2285 | |
| 2286 | if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, |
| 2287 | eap_tls_keying.master_secret, |
| 2288 | sizeof( eap_tls_keying.master_secret ), |
| 2289 | eap_tls_label, |
| 2290 | eap_tls_keying.randbytes, |
| 2291 | sizeof( eap_tls_keying.randbytes ), |
| 2292 | eap_tls_keymaterial, |
| 2293 | sizeof( eap_tls_keymaterial ) ) ) |
| 2294 | != 0 ) |
| 2295 | { |
| 2296 | mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n", |
| 2297 | -ret ); |
| 2298 | goto exit; |
| 2299 | } |
| 2300 | |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2301 | mbedtls_printf( " EAP-TLS key material is:" ); |
| 2302 | for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ ) |
| 2303 | { |
| 2304 | if( j % 8 == 0 ) |
| 2305 | mbedtls_printf("\n "); |
| 2306 | mbedtls_printf("%02x ", eap_tls_keymaterial[j] ); |
| 2307 | } |
| 2308 | mbedtls_printf("\n"); |
| 2309 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 2310 | if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0, |
Ron Eldor | 51c4507 | 2019-05-15 17:49:54 +0300 | [diff] [blame] | 2311 | eap_tls_label, |
| 2312 | eap_tls_keying.randbytes, |
| 2313 | sizeof( eap_tls_keying.randbytes ), |
| 2314 | eap_tls_iv, |
| 2315 | sizeof( eap_tls_iv ) ) ) != 0 ) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 2316 | { |
| 2317 | mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n", |
| 2318 | -ret ); |
| 2319 | goto exit; |
| 2320 | } |
| 2321 | |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2322 | mbedtls_printf( " EAP-TLS IV is:" ); |
| 2323 | for( j = 0; j < sizeof( eap_tls_iv ); j++ ) |
| 2324 | { |
| 2325 | if( j % 8 == 0 ) |
| 2326 | mbedtls_printf("\n "); |
| 2327 | mbedtls_printf("%02x ", eap_tls_iv[j] ); |
| 2328 | } |
| 2329 | mbedtls_printf("\n"); |
| 2330 | } |
| 2331 | #endif |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2332 | if( opt.reconnect != 0 ) |
| 2333 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2334 | mbedtls_printf(" . Saving session for reuse..." ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2335 | fflush( stdout ); |
| 2336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2337 | if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2338 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2339 | mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n", |
| 2340 | -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2341 | goto exit; |
| 2342 | } |
| 2343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2344 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2345 | } |
| 2346 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2347 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2348 | /* |
| 2349 | * 5. Verify the server certificate |
| 2350 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2351 | mbedtls_printf( " . Verifying peer X.509 certificate..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2352 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2353 | if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2354 | { |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 2355 | char vrfy_buf[512]; |
| 2356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2357 | mbedtls_printf( " failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2358 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2359 | mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), |
| 2360 | " ! ", flags ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2361 | |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 2362 | mbedtls_printf( "%s\n", vrfy_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2363 | } |
| 2364 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2365 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2366 | |
Hanno Becker | a9766c2c | 2019-02-25 17:43:18 +0000 | [diff] [blame] | 2367 | mbedtls_printf( " . Peer certificate information ...\n" ); |
| 2368 | mbedtls_printf( "%s\n", peer_crt_info ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2369 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2370 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2371 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2372 | ret = report_cid_usage( &ssl, "initial handshake" ); |
| 2373 | if( ret != 0 ) |
| 2374 | goto exit; |
| 2375 | |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 2376 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 2377 | { |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2378 | if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego, |
| 2379 | cid_renego, |
| 2380 | cid_renego_len ) ) != 0 ) |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 2381 | { |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2382 | mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n", |
| 2383 | ret ); |
| 2384 | return( ret ); |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 2385 | } |
| 2386 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2387 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 90cb359 | 2019-04-09 17:24:19 +0100 | [diff] [blame] | 2388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2389 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 2390 | if( opt.renegotiate ) |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 2391 | { |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 2392 | /* |
| 2393 | * Perform renegotiation (this must be done when the server is waiting |
| 2394 | * for input from our side). |
| 2395 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2396 | mbedtls_printf( " . Performing renegotiation..." ); |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 2397 | fflush( stdout ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2398 | while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 2399 | { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2400 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 2401 | ret != MBEDTLS_ERR_SSL_WANT_WRITE && |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2402 | ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 2403 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2404 | mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", |
| 2405 | ret ); |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 2406 | goto exit; |
| 2407 | } |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2408 | |
Manuel Pégourié-Gonnard | 25781f9 | 2018-10-15 15:28:16 +0200 | [diff] [blame] | 2409 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 2410 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2411 | continue; |
| 2412 | #endif |
| 2413 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2414 | /* For event-driven IO, wait for socket to become available */ |
| 2415 | if( opt.event == 1 /* level triggered IO */ ) |
| 2416 | { |
| 2417 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2418 | idle( &server_fd, &timer, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2419 | #else |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2420 | idle( &server_fd, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2421 | #endif |
| 2422 | } |
| 2423 | |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 2424 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 2426 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2427 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 53b3e06 | 2013-10-29 18:16:38 +0100 | [diff] [blame] | 2428 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2429 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2430 | ret = report_cid_usage( &ssl, "after renegotiation" ); |
| 2431 | if( ret != 0 ) |
| 2432 | goto exit; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2433 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2434 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2435 | /* |
| 2436 | * 6. Write the GET request |
| 2437 | */ |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 2438 | retry_left = opt.max_resend; |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2439 | send_request: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2440 | mbedtls_printf( " > Write to server:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2441 | fflush( stdout ); |
| 2442 | |
Hanno Becker | e4ad3e8 | 2017-09-18 15:05:46 +0100 | [diff] [blame] | 2443 | len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST, |
| 2444 | opt.request_page ); |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 2445 | tail_len = (int) strlen( GET_REQUEST_END ); |
Manuel Pégourié-Gonnard | dcab293 | 2014-08-14 17:47:17 +0200 | [diff] [blame] | 2446 | |
| 2447 | /* Add padding to GET request to reach opt.request_size in length */ |
| 2448 | if( opt.request_size != DFL_REQUEST_SIZE && |
| 2449 | len + tail_len < opt.request_size ) |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 2450 | { |
Manuel Pégourié-Gonnard | dcab293 | 2014-08-14 17:47:17 +0200 | [diff] [blame] | 2451 | memset( buf + len, 'A', opt.request_size - len - tail_len ); |
| 2452 | len += opt.request_size - len - tail_len; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 2453 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2454 | |
Hanno Becker | e4ad3e8 | 2017-09-18 15:05:46 +0100 | [diff] [blame] | 2455 | strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 ); |
Manuel Pégourié-Gonnard | dcab293 | 2014-08-14 17:47:17 +0200 | [diff] [blame] | 2456 | len += tail_len; |
| 2457 | |
Manuel Pégourié-Gonnard | dea29c5 | 2014-06-18 13:07:56 +0200 | [diff] [blame] | 2458 | /* Truncate if request size is smaller than the "natural" size */ |
| 2459 | if( opt.request_size != DFL_REQUEST_SIZE && |
| 2460 | len > opt.request_size ) |
| 2461 | { |
| 2462 | len = opt.request_size; |
| 2463 | |
| 2464 | /* Still end with \r\n unless that's really not possible */ |
| 2465 | if( len >= 2 ) buf[len - 2] = '\r'; |
| 2466 | if( len >= 1 ) buf[len - 1] = '\n'; |
| 2467 | } |
| 2468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2469 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2470 | { |
Andres Amaya Garcia | ce6fbac | 2018-07-04 09:29:34 +0100 | [diff] [blame] | 2471 | written = 0; |
| 2472 | frags = 0; |
| 2473 | |
| 2474 | do |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2475 | { |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2476 | while( ( ret = mbedtls_ssl_write( &ssl, buf + written, |
Andres Amaya Garcia | ce6fbac | 2018-07-04 09:29:34 +0100 | [diff] [blame] | 2477 | len - written ) ) < 0 ) |
Manuel Pégourié-Gonnard | 787b658 | 2013-07-16 15:43:17 +0200 | [diff] [blame] | 2478 | { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2479 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 2480 | ret != MBEDTLS_ERR_SSL_WANT_WRITE && |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2481 | ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2482 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2483 | mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n", |
| 2484 | -ret ); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2485 | goto exit; |
| 2486 | } |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2487 | |
| 2488 | /* For event-driven IO, wait for socket to become available */ |
| 2489 | if( opt.event == 1 /* level triggered IO */ ) |
| 2490 | { |
| 2491 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2492 | idle( &server_fd, &timer, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2493 | #else |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2494 | idle( &server_fd, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2495 | #endif |
| 2496 | } |
Manuel Pégourié-Gonnard | 787b658 | 2013-07-16 15:43:17 +0200 | [diff] [blame] | 2497 | } |
Andres Amaya Garcia | ce6fbac | 2018-07-04 09:29:34 +0100 | [diff] [blame] | 2498 | |
| 2499 | frags++; |
| 2500 | written += ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2501 | } |
Andres Amaya Garcia | ce6fbac | 2018-07-04 09:29:34 +0100 | [diff] [blame] | 2502 | while( written < len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2503 | } |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2504 | else /* Not stream, so datagram */ |
| 2505 | { |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2506 | while( 1 ) |
| 2507 | { |
| 2508 | ret = mbedtls_ssl_write( &ssl, buf, len ); |
| 2509 | |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2510 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2511 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2512 | continue; |
| 2513 | #endif |
| 2514 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2515 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 2516 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 2517 | break; |
| 2518 | |
| 2519 | /* For event-driven IO, wait for socket to become available */ |
| 2520 | if( opt.event == 1 /* level triggered IO */ ) |
| 2521 | { |
| 2522 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2523 | idle( &server_fd, &timer, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2524 | #else |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2525 | idle( &server_fd, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2526 | #endif |
| 2527 | } |
| 2528 | } |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2529 | |
| 2530 | if( ret < 0 ) |
| 2531 | { |
Hanno Becker | df4180a | 2017-10-27 13:43:58 +0100 | [diff] [blame] | 2532 | mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", |
| 2533 | ret ); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2534 | goto exit; |
| 2535 | } |
| 2536 | |
| 2537 | frags = 1; |
| 2538 | written = ret; |
Hanno Becker | e4ad3e8 | 2017-09-18 15:05:46 +0100 | [diff] [blame] | 2539 | |
| 2540 | if( written < len ) |
| 2541 | { |
| 2542 | mbedtls_printf( " warning\n ! request didn't fit into single datagram and " |
| 2543 | "was truncated to size %u", (unsigned) written ); |
| 2544 | } |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 2545 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2546 | |
Manuel Pégourié-Gonnard | 787b658 | 2013-07-16 15:43:17 +0200 | [diff] [blame] | 2547 | buf[written] = '\0'; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2548 | mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", |
| 2549 | written, frags, (char *) buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2550 | |
Andres Amaya Garcia | ce6fbac | 2018-07-04 09:29:34 +0100 | [diff] [blame] | 2551 | /* Send a non-empty request if request_size == 0 */ |
| 2552 | if ( len == 0 ) |
| 2553 | { |
| 2554 | opt.request_size = DFL_REQUEST_SIZE; |
| 2555 | goto send_request; |
| 2556 | } |
| 2557 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2558 | /* |
| 2559 | * 7. Read the HTTP response |
| 2560 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2561 | mbedtls_printf( " < Read from server:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2562 | fflush( stdout ); |
| 2563 | |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2564 | /* |
| 2565 | * TLS and DTLS need different reading styles (stream vs datagram) |
| 2566 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2567 | if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2568 | { |
| 2569 | do |
| 2570 | { |
| 2571 | len = sizeof( buf ) - 1; |
| 2572 | memset( buf, 0, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2573 | ret = mbedtls_ssl_read( &ssl, buf, len ); |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2574 | |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2575 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2576 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2577 | continue; |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2578 | #endif |
| 2579 | |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2580 | if( ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2581 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2582 | { |
| 2583 | /* For event-driven IO, wait for socket to become available */ |
| 2584 | if( opt.event == 1 /* level triggered IO */ ) |
| 2585 | { |
| 2586 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2587 | idle( &server_fd, &timer, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2588 | #else |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2589 | idle( &server_fd, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2590 | #endif |
| 2591 | } |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2592 | continue; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2593 | } |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2594 | |
| 2595 | if( ret <= 0 ) |
| 2596 | { |
| 2597 | switch( ret ) |
| 2598 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2599 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 2600 | mbedtls_printf( " connection was closed gracefully\n" ); |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2601 | ret = 0; |
| 2602 | goto close_notify; |
| 2603 | |
| 2604 | case 0: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2605 | case MBEDTLS_ERR_NET_CONN_RESET: |
| 2606 | mbedtls_printf( " connection was reset by peer\n" ); |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2607 | ret = 0; |
| 2608 | goto reconnect; |
| 2609 | |
| 2610 | default: |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2611 | mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", |
| 2612 | -ret ); |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2613 | goto exit; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | len = ret; |
| 2618 | buf[len] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2619 | mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2620 | |
| 2621 | /* End of message should be detected according to the syntax of the |
| 2622 | * application protocol (eg HTTP), just use a dummy test here. */ |
| 2623 | if( ret > 0 && buf[len-1] == '\n' ) |
| 2624 | { |
| 2625 | ret = 0; |
| 2626 | break; |
| 2627 | } |
| 2628 | } |
| 2629 | while( 1 ); |
| 2630 | } |
| 2631 | else /* Not stream, so datagram */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2632 | { |
| 2633 | len = sizeof( buf ) - 1; |
| 2634 | memset( buf, 0, sizeof( buf ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2635 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2636 | while( 1 ) |
| 2637 | { |
| 2638 | ret = mbedtls_ssl_read( &ssl, buf, len ); |
| 2639 | |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2640 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2641 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2642 | continue; |
| 2643 | #endif |
| 2644 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2645 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 2646 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 2647 | break; |
| 2648 | |
| 2649 | /* For event-driven IO, wait for socket to become available */ |
| 2650 | if( opt.event == 1 /* level triggered IO */ ) |
| 2651 | { |
| 2652 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2653 | idle( &server_fd, &timer, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2654 | #else |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2655 | idle( &server_fd, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2656 | #endif |
| 2657 | } |
| 2658 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2659 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2660 | if( ret <= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2661 | { |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2662 | switch( ret ) |
| 2663 | { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2664 | case MBEDTLS_ERR_SSL_TIMEOUT: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | mbedtls_printf( " timeout\n" ); |
Manuel Pégourié-Gonnard | e63582a | 2014-10-14 11:47:21 +0200 | [diff] [blame] | 2666 | if( retry_left-- > 0 ) |
Manuel Pégourié-Gonnard | f1e0df3 | 2014-10-02 14:02:32 +0200 | [diff] [blame] | 2667 | goto send_request; |
| 2668 | goto exit; |
| 2669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2670 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 2671 | mbedtls_printf( " connection was closed gracefully\n" ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2672 | ret = 0; |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 2673 | goto close_notify; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2674 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2675 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2676 | mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2677 | goto exit; |
| 2678 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2681 | len = ret; |
Paul Bakker | 93c32b2 | 2014-04-25 13:40:05 +0200 | [diff] [blame] | 2682 | buf[len] = '\0'; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Manuel Pégourié-Gonnard | ae5050c | 2014-07-11 14:14:15 +0200 | [diff] [blame] | 2684 | ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2685 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2686 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2687 | /* |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 2688 | * 7b. Simulate hard reset and reconnect from same port? |
| 2689 | */ |
| 2690 | if( opt.reconnect_hard != 0 ) |
| 2691 | { |
| 2692 | opt.reconnect_hard = 0; |
| 2693 | |
| 2694 | mbedtls_printf( " . Restarting connection from same port..." ); |
| 2695 | fflush( stdout ); |
| 2696 | |
Hanno Becker | bdf75eb | 2019-02-27 08:34:31 +0000 | [diff] [blame] | 2697 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 23699ef | 2019-02-26 12:36:53 +0000 | [diff] [blame] | 2698 | memset( peer_crt_info, 0, sizeof( peer_crt_info ) ); |
Hanno Becker | bdf75eb | 2019-02-27 08:34:31 +0000 | [diff] [blame] | 2699 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Hanno Becker | 23699ef | 2019-02-26 12:36:53 +0000 | [diff] [blame] | 2700 | |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 2701 | if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 ) |
| 2702 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2703 | mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", |
| 2704 | -ret ); |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 2705 | goto exit; |
| 2706 | } |
| 2707 | |
| 2708 | while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) |
| 2709 | { |
| 2710 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 2711 | ret != MBEDTLS_ERR_SSL_WANT_WRITE && |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2712 | ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 2713 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2714 | mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", |
| 2715 | -ret ); |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 2716 | goto exit; |
| 2717 | } |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2718 | |
| 2719 | /* For event-driven IO, wait for socket to become available */ |
| 2720 | if( opt.event == 1 /* level triggered IO */ ) |
| 2721 | { |
| 2722 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2723 | idle( &server_fd, &timer, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2724 | #else |
Hanno Becker | 197a91c | 2017-10-31 10:58:53 +0000 | [diff] [blame] | 2725 | idle( &server_fd, ret ); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2726 | #endif |
| 2727 | } |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | mbedtls_printf( " ok\n" ); |
| 2731 | |
| 2732 | goto send_request; |
| 2733 | } |
| 2734 | |
| 2735 | /* |
| 2736 | * 7c. Continue doing data exchanges? |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2737 | */ |
| 2738 | if( --opt.exchanges > 0 ) |
| 2739 | goto send_request; |
| 2740 | |
| 2741 | /* |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 2742 | * 8. Done, cleanly close the connection |
| 2743 | */ |
| 2744 | close_notify: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | mbedtls_printf( " . Closing the connection..." ); |
Manuel Pégourié-Gonnard | dbd2307 | 2015-09-04 10:20:17 +0200 | [diff] [blame] | 2746 | fflush( stdout ); |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 2747 | |
Manuel Pégourié-Gonnard | 994f8b5 | 2014-10-09 19:56:44 +0200 | [diff] [blame] | 2748 | /* No error checking, the connection might be closed already */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2749 | do ret = mbedtls_ssl_close_notify( &ssl ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2750 | while( ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Manuel Pégourié-Gonnard | 994f8b5 | 2014-10-09 19:56:44 +0200 | [diff] [blame] | 2751 | ret = 0; |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 2752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2753 | mbedtls_printf( " done\n" ); |
Manuel Pégourié-Gonnard | f138874 | 2014-08-19 16:14:36 +0200 | [diff] [blame] | 2754 | |
| 2755 | /* |
| 2756 | * 9. Reconnect? |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2757 | */ |
| 2758 | reconnect: |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2759 | if( opt.reconnect != 0 ) |
| 2760 | { |
Manuel Pégourié-Gonnard | cf2e97e | 2013-08-02 15:04:36 +0200 | [diff] [blame] | 2761 | --opt.reconnect; |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2762 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 2763 | mbedtls_net_free( &server_fd ); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 2764 | |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2765 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 2766 | if( opt.reco_delay > 0 ) |
Manuel Pégourié-Gonnard | a63bc94 | 2015-05-14 18:22:47 +0200 | [diff] [blame] | 2767 | mbedtls_net_usleep( 1000000 * opt.reco_delay ); |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2768 | #endif |
Manuel Pégourié-Gonnard | 38d1eba | 2013-08-23 10:44:29 +0200 | [diff] [blame] | 2769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2770 | mbedtls_printf( " . Reconnecting with saved session..." ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2771 | |
Hanno Becker | bdf75eb | 2019-02-27 08:34:31 +0000 | [diff] [blame] | 2772 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | a1051b4 | 2019-02-26 11:38:29 +0000 | [diff] [blame] | 2773 | memset( peer_crt_info, 0, sizeof( peer_crt_info ) ); |
Hanno Becker | bdf75eb | 2019-02-27 08:34:31 +0000 | [diff] [blame] | 2774 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Hanno Becker | a1051b4 | 2019-02-26 11:38:29 +0000 | [diff] [blame] | 2775 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2776 | if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2777 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2778 | mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n", |
| 2779 | -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2780 | goto exit; |
| 2781 | } |
| 2782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2783 | if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 ) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2784 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2785 | mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n", |
| 2786 | ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2787 | goto exit; |
| 2788 | } |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2789 | |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2790 | if( ( ret = mbedtls_net_connect( &server_fd, |
| 2791 | opt.server_addr, opt.server_port, |
| 2792 | opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? |
| 2793 | MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2794 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2795 | mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n", |
| 2796 | -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2797 | goto exit; |
| 2798 | } |
| 2799 | |
Manuel Pégourié-Gonnard | 85beb30 | 2014-10-02 17:59:19 +0200 | [diff] [blame] | 2800 | if( opt.nbio > 0 ) |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 2801 | ret = mbedtls_net_set_nonblock( &server_fd ); |
Manuel Pégourié-Gonnard | 85beb30 | 2014-10-02 17:59:19 +0200 | [diff] [blame] | 2802 | else |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 2803 | ret = mbedtls_net_set_block( &server_fd ); |
Manuel Pégourié-Gonnard | 85beb30 | 2014-10-02 17:59:19 +0200 | [diff] [blame] | 2804 | if( ret != 0 ) |
| 2805 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2806 | mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2807 | -ret ); |
Manuel Pégourié-Gonnard | 85beb30 | 2014-10-02 17:59:19 +0200 | [diff] [blame] | 2808 | goto exit; |
| 2809 | } |
| 2810 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2811 | while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2812 | { |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2813 | if( ret != MBEDTLS_ERR_SSL_WANT_READ && |
Manuel Pégourié-Gonnard | b3c8307 | 2017-05-16 08:50:24 +0200 | [diff] [blame] | 2814 | ret != MBEDTLS_ERR_SSL_WANT_WRITE && |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 2815 | ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2816 | { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 2817 | mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", |
| 2818 | -ret ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2819 | goto exit; |
| 2820 | } |
| 2821 | } |
| 2822 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2823 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | aaa1eab | 2013-07-30 13:43:43 +0200 | [diff] [blame] | 2824 | |
| 2825 | goto send_request; |
| 2826 | } |
| 2827 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2828 | /* |
| 2829 | * Cleanup and exit |
| 2830 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2831 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2832 | #ifdef MBEDTLS_ERROR_C |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2833 | if( ret != 0 ) |
| 2834 | { |
| 2835 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2836 | mbedtls_strerror( ret, error_buf, 100 ); |
| 2837 | mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2838 | } |
| 2839 | #endif |
| 2840 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 2841 | mbedtls_net_free( &server_fd ); |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 2842 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2843 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2844 | mbedtls_x509_crt_free( &clicert ); |
| 2845 | mbedtls_x509_crt_free( &cacert ); |
| 2846 | mbedtls_pk_free( &pkey ); |
Manuel Pégourié-Gonnard | cfdf8f4 | 2018-11-08 09:52:25 +0100 | [diff] [blame] | 2847 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2848 | psa_destroy_key( key_slot ); |
| 2849 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2850 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2851 | mbedtls_ssl_session_free( &saved_session ); |
| 2852 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2853 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2854 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 2855 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2856 | |
Hanno Becker | 3f24ea9 | 2018-11-05 13:25:17 +0000 | [diff] [blame] | 2857 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \ |
| 2858 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2859 | if( opt.psk_opaque != 0 ) |
Hanno Becker | 3f24ea9 | 2018-11-05 13:25:17 +0000 | [diff] [blame] | 2860 | { |
| 2861 | /* This is ok even if the slot hasn't been |
| 2862 | * initialized (we might have jumed here |
| 2863 | * immediately because of bad cmd line params, |
| 2864 | * for example). */ |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2865 | status = psa_destroy_key( slot ); |
Hanno Becker | 3f24ea9 | 2018-11-05 13:25:17 +0000 | [diff] [blame] | 2866 | if( status != PSA_SUCCESS ) |
| 2867 | { |
| 2868 | mbedtls_printf( "Failed to destroy key slot %u - error was %d", |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2869 | (unsigned) slot, (int) status ); |
Hanno Becker | 3f24ea9 | 2018-11-05 13:25:17 +0000 | [diff] [blame] | 2870 | if( ret == 0 ) |
| 2871 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2872 | } |
| 2873 | } |
| 2874 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED && |
| 2875 | MBEDTLS_USE_PSA_CRYPTO */ |
| 2876 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 2877 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2878 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2879 | fflush( stdout ); getchar(); |
| 2880 | #endif |
| 2881 | |
Paul Bakker | dbd79ca | 2013-07-24 16:28:35 +0200 | [diff] [blame] | 2882 | // Shell can not handle large exit numbers -> 1 for errors |
| 2883 | if( ret < 0 ) |
| 2884 | ret = 1; |
| 2885 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2886 | return( ret ); |
| 2887 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && |
| 2889 | MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && |
Manuel Pégourié-Gonnard | d2377e7 | 2015-05-13 13:58:56 +0200 | [diff] [blame] | 2890 | MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */ |