Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1 | /* ssl/ssltest.c */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. |
| 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: |
| 26 | * 1. Redistributions of source code must retain the copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * 2. Redistributions in binary form must reproduce the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer in the |
| 30 | * documentation and/or other materials provided with the distribution. |
| 31 | * 3. All advertising materials mentioning features or use of this software |
| 32 | * must display the following acknowledgement: |
| 33 | * "This product includes cryptographic software written by |
| 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. |
| 52 | * |
| 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] |
| 57 | */ |
| 58 | |
| 59 | #include <stdio.h> |
| 60 | #include <stdlib.h> |
| 61 | #include <string.h> |
| 62 | #include <errno.h> |
Bodo Möller | 17e3dd1 | 1999-05-20 21:59:20 +0000 | [diff] [blame] | 63 | |
Bodo Möller | 7e70181 | 1999-05-21 11:16:48 +0000 | [diff] [blame] | 64 | #include "openssl/e_os.h" |
Bodo Möller | 17e3dd1 | 1999-05-20 21:59:20 +0000 | [diff] [blame] | 65 | |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 66 | #include <openssl/bio.h> |
| 67 | #include <openssl/crypto.h> |
| 68 | #include <openssl/x509.h> |
| 69 | #include <openssl/ssl.h> |
| 70 | #include <openssl/err.h> |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 71 | #ifdef WINDOWS |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 72 | #include "../crypto/bio/bss_file.c" |
| 73 | #endif |
| 74 | |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 75 | #if defined(NO_RSA) && !defined(NO_SSL2) |
| 76 | #define NO_SSL2 |
| 77 | #endif |
| 78 | |
Ulf Möller | 7d7d2cb | 1999-05-13 11:37:32 +0000 | [diff] [blame] | 79 | #ifdef VMS |
| 80 | # define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM" |
| 81 | # define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM" |
| 82 | #else |
| 83 | # define TEST_SERVER_CERT "../apps/server.pem" |
| 84 | # define TEST_CLIENT_CERT "../apps/client.pem" |
| 85 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 86 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 87 | int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx); |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 88 | #ifndef NO_RSA |
Ulf Möller | df63a38 | 1999-06-09 16:33:18 +0000 | [diff] [blame] | 89 | static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength); |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 90 | #endif |
| 91 | #ifndef NO_DH |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 92 | static DH *get_dh512(void); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 93 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 94 | BIO *bio_err=NULL; |
| 95 | BIO *bio_stdout=NULL; |
| 96 | |
| 97 | static char *cipher=NULL; |
| 98 | int verbose=0; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 99 | int debug=0; |
Bodo Möller | d58d092 | 1999-06-10 16:29:32 +0000 | [diff] [blame^] | 100 | #if 0 |
| 101 | /* Not used yet. */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 102 | #ifdef FIONBIO |
| 103 | static int s_nbio=0; |
| 104 | #endif |
Bodo Möller | d58d092 | 1999-06-10 16:29:32 +0000 | [diff] [blame^] | 105 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 106 | |
| 107 | |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 108 | int doit(SSL *s_ssl,SSL *c_ssl,long bytes); |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 109 | static void sv_usage(void) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 110 | { |
| 111 | fprintf(stderr,"usage: ssltest [args ...]\n"); |
| 112 | fprintf(stderr,"\n"); |
| 113 | fprintf(stderr," -server_auth - check server certificate\n"); |
| 114 | fprintf(stderr," -client_auth - do client authentication\n"); |
| 115 | fprintf(stderr," -v - more output\n"); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 116 | fprintf(stderr," -d - debug output\n"); |
| 117 | fprintf(stderr," -reuse - use session-id reuse\n"); |
| 118 | fprintf(stderr," -num <val> - number of connections to perform\n"); |
| 119 | fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 120 | #ifndef NO_SSL2 |
| 121 | fprintf(stderr," -ssl2 - use SSLv2\n"); |
| 122 | #endif |
| 123 | #ifndef NO_SSL3 |
| 124 | fprintf(stderr," -ssl3 - use SSLv3\n"); |
| 125 | #endif |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 126 | #ifndef NO_TLS1 |
| 127 | fprintf(stderr," -tls1 - use TLSv1\n"); |
| 128 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 129 | fprintf(stderr," -CApath arg - PEM format directory of CA's\n"); |
| 130 | fprintf(stderr," -CAfile arg - PEM format file of CA's\n"); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 131 | fprintf(stderr," -cert arg - Certificate file\n"); |
| 132 | fprintf(stderr," -s_cert arg - Just the server certificate file\n"); |
| 133 | fprintf(stderr," -c_cert arg - Just the client certificate file\n"); |
| 134 | fprintf(stderr," -cipher arg - The cipher list\n"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 137 | int main(int argc, char *argv[]) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 138 | { |
| 139 | char *CApath=NULL,*CAfile=NULL; |
| 140 | int badop=0; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 141 | int tls1=0,ssl2=0,ssl3=0,ret=1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 142 | int client_auth=0; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 143 | int server_auth=0,i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 144 | char *server_cert=TEST_SERVER_CERT; |
| 145 | char *client_cert=TEST_CLIENT_CERT; |
| 146 | SSL_CTX *s_ctx=NULL; |
| 147 | SSL_CTX *c_ctx=NULL; |
| 148 | SSL_METHOD *meth=NULL; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 149 | SSL *c_ssl,*s_ssl; |
| 150 | int number=1,reuse=0; |
| 151 | long bytes=1L; |
| 152 | SSL_CIPHER *ciph; |
| 153 | #ifndef NO_DH |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 154 | DH *dh; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 155 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 156 | |
| 157 | bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); |
| 158 | bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE); |
| 159 | |
| 160 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); |
| 161 | |
| 162 | argc--; |
| 163 | argv++; |
| 164 | |
| 165 | while (argc >= 1) |
| 166 | { |
| 167 | if (strcmp(*argv,"-server_auth") == 0) |
| 168 | server_auth=1; |
| 169 | else if (strcmp(*argv,"-client_auth") == 0) |
| 170 | client_auth=1; |
| 171 | else if (strcmp(*argv,"-v") == 0) |
| 172 | verbose=1; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 173 | else if (strcmp(*argv,"-d") == 0) |
| 174 | debug=1; |
| 175 | else if (strcmp(*argv,"-reuse") == 0) |
| 176 | reuse=1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 177 | else if (strcmp(*argv,"-ssl2") == 0) |
| 178 | ssl2=1; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 179 | else if (strcmp(*argv,"-tls1") == 0) |
| 180 | tls1=1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 181 | else if (strcmp(*argv,"-ssl3") == 0) |
| 182 | ssl3=1; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 183 | else if (strncmp(*argv,"-num",4) == 0) |
| 184 | { |
| 185 | if (--argc < 1) goto bad; |
| 186 | number= atoi(*(++argv)); |
| 187 | if (number == 0) number=1; |
| 188 | } |
| 189 | else if (strcmp(*argv,"-bytes") == 0) |
| 190 | { |
| 191 | if (--argc < 1) goto bad; |
| 192 | bytes= atol(*(++argv)); |
| 193 | if (bytes == 0L) bytes=1L; |
| 194 | i=strlen(argv[0]); |
| 195 | if (argv[0][i-1] == 'k') bytes*=1024L; |
| 196 | if (argv[0][i-1] == 'm') bytes*=1024L*1024L; |
| 197 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 198 | else if (strcmp(*argv,"-cert") == 0) |
| 199 | { |
| 200 | if (--argc < 1) goto bad; |
| 201 | server_cert= *(++argv); |
| 202 | } |
| 203 | else if (strcmp(*argv,"-s_cert") == 0) |
| 204 | { |
| 205 | if (--argc < 1) goto bad; |
| 206 | server_cert= *(++argv); |
| 207 | } |
| 208 | else if (strcmp(*argv,"-c_cert") == 0) |
| 209 | { |
| 210 | if (--argc < 1) goto bad; |
| 211 | client_cert= *(++argv); |
| 212 | } |
| 213 | else if (strcmp(*argv,"-cipher") == 0) |
| 214 | { |
| 215 | if (--argc < 1) goto bad; |
| 216 | cipher= *(++argv); |
| 217 | } |
| 218 | else if (strcmp(*argv,"-CApath") == 0) |
| 219 | { |
| 220 | if (--argc < 1) goto bad; |
| 221 | CApath= *(++argv); |
| 222 | } |
| 223 | else if (strcmp(*argv,"-CAfile") == 0) |
| 224 | { |
| 225 | if (--argc < 1) goto bad; |
| 226 | CAfile= *(++argv); |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | fprintf(stderr,"unknown option %s\n",*argv); |
| 231 | badop=1; |
| 232 | break; |
| 233 | } |
| 234 | argc--; |
| 235 | argv++; |
| 236 | } |
| 237 | if (badop) |
| 238 | { |
| 239 | bad: |
| 240 | sv_usage(); |
| 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */ |
| 245 | |
Mark J. Cox | 413c4f4 | 1999-02-16 09:22:21 +0000 | [diff] [blame] | 246 | SSL_library_init(); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 247 | SSL_load_error_strings(); |
| 248 | |
| 249 | #if !defined(NO_SSL2) && !defined(NO_SSL3) |
| 250 | if (ssl2) |
| 251 | meth=SSLv2_method(); |
| 252 | else |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 253 | if (tls1) |
| 254 | meth=TLSv1_method(); |
| 255 | else |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 256 | if (ssl3) |
| 257 | meth=SSLv3_method(); |
| 258 | else |
| 259 | meth=SSLv23_method(); |
| 260 | #else |
| 261 | #ifdef NO_SSL2 |
| 262 | meth=SSLv3_method(); |
| 263 | #else |
| 264 | meth=SSLv2_method(); |
| 265 | #endif |
| 266 | #endif |
| 267 | |
| 268 | c_ctx=SSL_CTX_new(meth); |
| 269 | s_ctx=SSL_CTX_new(meth); |
| 270 | if ((c_ctx == NULL) || (s_ctx == NULL)) |
| 271 | { |
| 272 | ERR_print_errors(bio_err); |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | if (cipher != NULL) |
| 277 | { |
| 278 | SSL_CTX_set_cipher_list(c_ctx,cipher); |
| 279 | SSL_CTX_set_cipher_list(s_ctx,cipher); |
| 280 | } |
| 281 | |
| 282 | #ifndef NO_DH |
| 283 | dh=get_dh512(); |
| 284 | SSL_CTX_set_tmp_dh(s_ctx,dh); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 285 | DH_free(dh); |
| 286 | #endif |
| 287 | |
| 288 | #ifndef NO_RSA |
| 289 | SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 290 | #endif |
| 291 | |
| 292 | if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM)) |
| 293 | { |
| 294 | ERR_print_errors(bio_err); |
| 295 | } |
| 296 | else if (!SSL_CTX_use_PrivateKey_file(s_ctx,server_cert, |
| 297 | SSL_FILETYPE_PEM)) |
| 298 | { |
| 299 | ERR_print_errors(bio_err); |
| 300 | goto end; |
| 301 | } |
| 302 | |
| 303 | if (client_auth) |
| 304 | { |
| 305 | SSL_CTX_use_certificate_file(c_ctx,client_cert, |
| 306 | SSL_FILETYPE_PEM); |
| 307 | SSL_CTX_use_PrivateKey_file(c_ctx,client_cert, |
| 308 | SSL_FILETYPE_PEM); |
| 309 | } |
| 310 | |
| 311 | if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) || |
| 312 | (!SSL_CTX_set_default_verify_paths(s_ctx)) || |
| 313 | (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) || |
| 314 | (!SSL_CTX_set_default_verify_paths(c_ctx))) |
| 315 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 316 | /* fprintf(stderr,"SSL_load_verify_locations\n"); */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 317 | ERR_print_errors(bio_err); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 318 | /* goto end; */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | if (client_auth) |
| 322 | { |
| 323 | fprintf(stderr,"client authentication\n"); |
| 324 | SSL_CTX_set_verify(s_ctx, |
| 325 | SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 326 | verify_callback); |
| 327 | } |
| 328 | if (server_auth) |
| 329 | { |
| 330 | fprintf(stderr,"server authentication\n"); |
| 331 | SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER, |
| 332 | verify_callback); |
| 333 | } |
| 334 | |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 335 | c_ssl=SSL_new(c_ctx); |
| 336 | s_ssl=SSL_new(s_ctx); |
| 337 | |
| 338 | for (i=0; i<number; i++) |
| 339 | { |
| 340 | if (!reuse) SSL_set_session(c_ssl,NULL); |
| 341 | ret=doit(s_ssl,c_ssl,bytes); |
| 342 | } |
| 343 | |
| 344 | if (!verbose) |
| 345 | { |
| 346 | ciph=SSL_get_current_cipher(c_ssl); |
| 347 | fprintf(stdout,"Protocol %s, cipher %s, %s\n", |
| 348 | SSL_get_version(c_ssl), |
| 349 | SSL_CIPHER_get_version(ciph), |
| 350 | SSL_CIPHER_get_name(ciph)); |
| 351 | } |
| 352 | if ((number > 1) || (bytes > 1L)) |
| 353 | printf("%d handshakes of %ld bytes done\n",number,bytes); |
| 354 | |
| 355 | SSL_free(s_ssl); |
| 356 | SSL_free(c_ssl); |
| 357 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 358 | end: |
| 359 | if (s_ctx != NULL) SSL_CTX_free(s_ctx); |
| 360 | if (c_ctx != NULL) SSL_CTX_free(c_ctx); |
| 361 | |
| 362 | if (bio_stdout != NULL) BIO_free(bio_stdout); |
| 363 | |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 364 | ERR_free_strings(); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 365 | ERR_remove_state(0); |
| 366 | EVP_cleanup(); |
| 367 | CRYPTO_mem_leaks(bio_err); |
| 368 | EXIT(ret); |
| 369 | } |
| 370 | |
| 371 | #define W_READ 1 |
| 372 | #define W_WRITE 2 |
| 373 | #define C_DONE 1 |
| 374 | #define S_DONE 2 |
| 375 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 376 | int doit(SSL *s_ssl, SSL *c_ssl, long count) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 377 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 378 | MS_STATIC char cbuf[1024*8],sbuf[1024*8]; |
| 379 | long cw_num=count,cr_num=count; |
| 380 | long sw_num=count,sr_num=count; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 381 | int ret=1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 382 | BIO *c_to_s=NULL; |
| 383 | BIO *s_to_c=NULL; |
| 384 | BIO *c_bio=NULL; |
| 385 | BIO *s_bio=NULL; |
| 386 | int c_r,c_w,s_r,s_w; |
| 387 | int c_want,s_want; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 388 | int i,j; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 389 | int done=0; |
| 390 | int c_write,s_write; |
| 391 | int do_server=0,do_client=0; |
| 392 | SSL_CIPHER *ciph; |
| 393 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 394 | c_to_s=BIO_new(BIO_s_mem()); |
| 395 | s_to_c=BIO_new(BIO_s_mem()); |
| 396 | if ((s_to_c == NULL) || (c_to_s == NULL)) |
| 397 | { |
| 398 | ERR_print_errors(bio_err); |
| 399 | goto err; |
| 400 | } |
| 401 | |
| 402 | c_bio=BIO_new(BIO_f_ssl()); |
| 403 | s_bio=BIO_new(BIO_f_ssl()); |
| 404 | if ((c_bio == NULL) || (s_bio == NULL)) |
| 405 | { |
| 406 | ERR_print_errors(bio_err); |
| 407 | goto err; |
| 408 | } |
| 409 | |
| 410 | SSL_set_connect_state(c_ssl); |
| 411 | SSL_set_bio(c_ssl,s_to_c,c_to_s); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 412 | BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 413 | |
| 414 | SSL_set_accept_state(s_ssl); |
| 415 | SSL_set_bio(s_ssl,c_to_s,s_to_c); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 416 | BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 417 | |
| 418 | c_r=0; s_r=1; |
| 419 | c_w=1; s_w=0; |
| 420 | c_want=W_WRITE; |
| 421 | s_want=0; |
| 422 | c_write=1,s_write=0; |
| 423 | |
| 424 | /* We can always do writes */ |
| 425 | for (;;) |
| 426 | { |
| 427 | do_server=0; |
| 428 | do_client=0; |
| 429 | |
| 430 | i=(int)BIO_pending(s_bio); |
| 431 | if ((i && s_r) || s_w) do_server=1; |
| 432 | |
| 433 | i=(int)BIO_pending(c_bio); |
| 434 | if ((i && c_r) || c_w) do_client=1; |
| 435 | |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 436 | if (do_server && debug) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 437 | { |
| 438 | if (SSL_in_init(s_ssl)) |
| 439 | printf("server waiting in SSL_accept - %s\n", |
| 440 | SSL_state_string_long(s_ssl)); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 441 | /* else if (s_write) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 442 | printf("server:SSL_write()\n"); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 443 | else |
| 444 | printf("server:SSL_read()\n"); */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 447 | if (do_client && debug) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 448 | { |
| 449 | if (SSL_in_init(c_ssl)) |
| 450 | printf("client waiting in SSL_connect - %s\n", |
| 451 | SSL_state_string_long(c_ssl)); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 452 | /* else if (c_write) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 453 | printf("client:SSL_write()\n"); |
| 454 | else |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 455 | printf("client:SSL_read()\n"); */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | if (!do_client && !do_server) |
| 459 | { |
| 460 | fprintf(stdout,"ERROR IN STARTUP\n"); |
| 461 | ERR_print_errors(bio_err); |
| 462 | break; |
| 463 | } |
| 464 | if (do_client && !(done & C_DONE)) |
| 465 | { |
| 466 | if (c_write) |
| 467 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 468 | j=(cw_num > (long)sizeof(cbuf)) |
| 469 | ?sizeof(cbuf):(int)cw_num; |
| 470 | i=BIO_write(c_bio,cbuf,j); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 471 | if (i < 0) |
| 472 | { |
| 473 | c_r=0; |
| 474 | c_w=0; |
| 475 | if (BIO_should_retry(c_bio)) |
| 476 | { |
| 477 | if (BIO_should_read(c_bio)) |
| 478 | c_r=1; |
| 479 | if (BIO_should_write(c_bio)) |
| 480 | c_w=1; |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | fprintf(stderr,"ERROR in CLIENT\n"); |
| 485 | ERR_print_errors(bio_err); |
| 486 | goto err; |
| 487 | } |
| 488 | } |
| 489 | else if (i == 0) |
| 490 | { |
| 491 | fprintf(stderr,"SSL CLIENT STARTUP FAILED\n"); |
| 492 | goto err; |
| 493 | } |
| 494 | else |
| 495 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 496 | if (debug) |
| 497 | printf("client wrote %d\n",i); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 498 | /* ok */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 499 | s_r=1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 500 | c_write=0; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 501 | cw_num-=i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | else |
| 505 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 506 | i=BIO_read(c_bio,cbuf,sizeof(cbuf)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 507 | if (i < 0) |
| 508 | { |
| 509 | c_r=0; |
| 510 | c_w=0; |
| 511 | if (BIO_should_retry(c_bio)) |
| 512 | { |
| 513 | if (BIO_should_read(c_bio)) |
| 514 | c_r=1; |
| 515 | if (BIO_should_write(c_bio)) |
| 516 | c_w=1; |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | fprintf(stderr,"ERROR in CLIENT\n"); |
| 521 | ERR_print_errors(bio_err); |
| 522 | goto err; |
| 523 | } |
| 524 | } |
| 525 | else if (i == 0) |
| 526 | { |
| 527 | fprintf(stderr,"SSL CLIENT STARTUP FAILED\n"); |
| 528 | goto err; |
| 529 | } |
| 530 | else |
| 531 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 532 | if (debug) |
| 533 | printf("client read %d\n",i); |
| 534 | cr_num-=i; |
| 535 | if (sw_num > 0) |
| 536 | { |
| 537 | s_write=1; |
| 538 | s_w=1; |
| 539 | } |
| 540 | if (cr_num <= 0) |
| 541 | { |
| 542 | s_write=1; |
| 543 | s_w=1; |
| 544 | done=S_DONE|C_DONE; |
| 545 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (do_server && !(done & S_DONE)) |
| 551 | { |
| 552 | if (!s_write) |
| 553 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 554 | i=BIO_read(s_bio,sbuf,sizeof(cbuf)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 555 | if (i < 0) |
| 556 | { |
| 557 | s_r=0; |
| 558 | s_w=0; |
| 559 | if (BIO_should_retry(s_bio)) |
| 560 | { |
| 561 | if (BIO_should_read(s_bio)) |
| 562 | s_r=1; |
| 563 | if (BIO_should_write(s_bio)) |
| 564 | s_w=1; |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | fprintf(stderr,"ERROR in SERVER\n"); |
| 569 | ERR_print_errors(bio_err); |
| 570 | goto err; |
| 571 | } |
| 572 | } |
| 573 | else if (i == 0) |
| 574 | { |
| 575 | ERR_print_errors(bio_err); |
| 576 | fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n"); |
| 577 | goto err; |
| 578 | } |
| 579 | else |
| 580 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 581 | if (debug) |
| 582 | printf("server read %d\n",i); |
| 583 | sr_num-=i; |
| 584 | if (cw_num > 0) |
| 585 | { |
| 586 | c_write=1; |
| 587 | c_w=1; |
| 588 | } |
| 589 | if (sr_num <= 0) |
| 590 | { |
| 591 | s_write=1; |
| 592 | s_w=1; |
| 593 | c_write=0; |
| 594 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | else |
| 598 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 599 | j=(sw_num > (long)sizeof(sbuf))? |
| 600 | sizeof(sbuf):(int)sw_num; |
| 601 | i=BIO_write(s_bio,sbuf,j); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 602 | if (i < 0) |
| 603 | { |
| 604 | s_r=0; |
| 605 | s_w=0; |
| 606 | if (BIO_should_retry(s_bio)) |
| 607 | { |
| 608 | if (BIO_should_read(s_bio)) |
| 609 | s_r=1; |
| 610 | if (BIO_should_write(s_bio)) |
| 611 | s_w=1; |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | fprintf(stderr,"ERROR in SERVER\n"); |
| 616 | ERR_print_errors(bio_err); |
| 617 | goto err; |
| 618 | } |
| 619 | } |
| 620 | else if (i == 0) |
| 621 | { |
| 622 | ERR_print_errors(bio_err); |
| 623 | fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n"); |
| 624 | goto err; |
| 625 | } |
| 626 | else |
| 627 | { |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 628 | if (debug) |
| 629 | printf("server wrote %d\n",i); |
| 630 | sw_num-=i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 631 | s_write=0; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 632 | c_r=1; |
| 633 | if (sw_num <= 0) |
| 634 | done|=S_DONE; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if ((done & S_DONE) && (done & C_DONE)) break; |
| 640 | } |
| 641 | |
| 642 | ciph=SSL_get_current_cipher(c_ssl); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 643 | if (verbose) |
| 644 | fprintf(stdout,"DONE, protocol %s, cipher %s, %s\n", |
| 645 | SSL_get_version(c_ssl), |
| 646 | SSL_CIPHER_get_version(ciph), |
| 647 | SSL_CIPHER_get_name(ciph)); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 648 | ret=0; |
| 649 | err: |
| 650 | /* We have to set the BIO's to NULL otherwise they will be |
| 651 | * Free()ed twice. Once when th s_ssl is SSL_free()ed and |
| 652 | * again when c_ssl is SSL_free()ed. |
| 653 | * This is a hack required because s_ssl and c_ssl are sharing the same |
| 654 | * BIO structure and SSL_set_bio() and SSL_free() automatically |
| 655 | * BIO_free non NULL entries. |
| 656 | * You should not normally do this or be required to do this */ |
| 657 | if (s_ssl != NULL) |
| 658 | { |
| 659 | s_ssl->rbio=NULL; |
| 660 | s_ssl->wbio=NULL; |
| 661 | } |
| 662 | if (c_ssl != NULL) |
| 663 | { |
| 664 | c_ssl->rbio=NULL; |
| 665 | c_ssl->wbio=NULL; |
| 666 | } |
| 667 | |
| 668 | if (c_to_s != NULL) BIO_free(c_to_s); |
| 669 | if (s_to_c != NULL) BIO_free(s_to_c); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 670 | if (c_bio != NULL) BIO_free_all(c_bio); |
| 671 | if (s_bio != NULL) BIO_free_all(s_bio); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 672 | return(ret); |
| 673 | } |
| 674 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 675 | int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 676 | { |
| 677 | char *s,buf[256]; |
| 678 | |
| 679 | s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,256); |
| 680 | if (s != NULL) |
| 681 | { |
| 682 | if (ok) |
| 683 | fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf); |
| 684 | else |
| 685 | fprintf(stderr,"depth=%d error=%d %s\n", |
| 686 | ctx->error_depth,ctx->error,buf); |
| 687 | } |
| 688 | |
| 689 | if (ok == 0) |
| 690 | { |
| 691 | switch (ctx->error) |
| 692 | { |
| 693 | case X509_V_ERR_CERT_NOT_YET_VALID: |
| 694 | case X509_V_ERR_CERT_HAS_EXPIRED: |
| 695 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 696 | ok=1; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return(ok); |
| 701 | } |
| 702 | |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 703 | #ifndef NO_DH |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 704 | static unsigned char dh512_p[]={ |
| 705 | 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75, |
| 706 | 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F, |
| 707 | 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3, |
| 708 | 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12, |
| 709 | 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C, |
| 710 | 0x47,0x74,0xE8,0x33, |
| 711 | }; |
| 712 | static unsigned char dh512_g[]={ |
| 713 | 0x02, |
| 714 | }; |
| 715 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 716 | static DH *get_dh512(void) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 717 | { |
| 718 | DH *dh=NULL; |
| 719 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 720 | if ((dh=DH_new()) == NULL) return(NULL); |
| 721 | dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL); |
| 722 | dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL); |
| 723 | if ((dh->p == NULL) || (dh->g == NULL)) |
| 724 | return(NULL); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 725 | return(dh); |
| 726 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 727 | #endif |
| 728 | |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 729 | #ifndef NO_RSA |
Ulf Möller | df63a38 | 1999-06-09 16:33:18 +0000 | [diff] [blame] | 730 | static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength) |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 731 | { |
| 732 | static RSA *rsa_tmp=NULL; |
| 733 | |
| 734 | if (rsa_tmp == NULL) |
| 735 | { |
Ben Laurie | 60e31c3 | 1999-02-21 21:58:59 +0000 | [diff] [blame] | 736 | BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength); |
Bodo Möller | d58d092 | 1999-06-10 16:29:32 +0000 | [diff] [blame^] | 737 | (void)BIO_flush(bio_err); |
Ben Laurie | 60e31c3 | 1999-02-21 21:58:59 +0000 | [diff] [blame] | 738 | rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 739 | BIO_printf(bio_err,"\n"); |
Bodo Möller | d58d092 | 1999-06-10 16:29:32 +0000 | [diff] [blame^] | 740 | (void)BIO_flush(bio_err); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 741 | } |
| 742 | return(rsa_tmp); |
| 743 | } |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 744 | #endif |