Change #include filenames from <foo.h> to <openssl.h>.
[openssl.git] / ssl / ssltest.c
1 /* ssl/ssltest.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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>
63 #include <openssl/e_os.h>
64 #include <openssl/bio.h>
65 #include <openssl/crypto.h>
66 #include <openssl/x509.h>
67 #include <openssl/ssl.h>
68 #include <openssl/err.h>
69 #ifdef WINDOWS
70 #include "../crypto/bio/bss_file.c"
71 #endif
72
73 #define TEST_SERVER_CERT "../apps/server.pem"
74 #define TEST_CLIENT_CERT "../apps/client.pem"
75
76 #ifndef NOPROTO
77 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
78 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int export,int keylength);
79 #ifndef NO_DSA
80 static DH *get_dh512(void);
81 #endif
82 #else
83 int MS_CALLBACK verify_callback();
84 static RSA MS_CALLBACK *tmp_rsa_cb();
85 #ifndef NO_DSA
86 static DH *get_dh512();
87 #endif
88 #endif
89
90 BIO *bio_err=NULL;
91 BIO *bio_stdout=NULL;
92
93 static char *cipher=NULL;
94 int verbose=0;
95 int debug=0;
96 #ifdef FIONBIO
97 static int s_nbio=0;
98 #endif
99
100
101 #ifndef  NOPROTO
102 int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
103 #else
104 int doit();
105 #endif
106
107 static void sv_usage(void)
108         {
109         fprintf(stderr,"usage: ssltest [args ...]\n");
110         fprintf(stderr,"\n");
111         fprintf(stderr," -server_auth  - check server certificate\n");
112         fprintf(stderr," -client_auth  - do client authentication\n");
113         fprintf(stderr," -v            - more output\n");
114         fprintf(stderr," -d            - debug output\n");
115         fprintf(stderr," -reuse        - use session-id reuse\n");
116         fprintf(stderr," -num <val>    - number of connections to perform\n");
117         fprintf(stderr," -bytes <val>  - number of bytes to swap between client/server\n");
118 #ifndef NO_SSL2
119         fprintf(stderr," -ssl2         - use SSLv2\n");
120 #endif
121 #ifndef NO_SSL3
122         fprintf(stderr," -ssl3         - use SSLv3\n");
123 #endif
124 #ifndef NO_TLS1
125         fprintf(stderr," -tls1         - use TLSv1\n");
126 #endif
127         fprintf(stderr," -CApath arg   - PEM format directory of CA's\n");
128         fprintf(stderr," -CAfile arg   - PEM format file of CA's\n");
129         fprintf(stderr," -cert arg     - Certificate file\n");
130         fprintf(stderr," -s_cert arg   - Just the server certificate file\n");
131         fprintf(stderr," -c_cert arg   - Just the client certificate file\n");
132         fprintf(stderr," -cipher arg   - The cipher list\n");
133         }
134
135 int main(int argc, char *argv[])
136         {
137         char *CApath=NULL,*CAfile=NULL;
138         int badop=0;
139         int tls1=0,ssl2=0,ssl3=0,ret=1;
140         int client_auth=0;
141         int server_auth=0,i;
142         char *server_cert=TEST_SERVER_CERT;
143         char *client_cert=TEST_CLIENT_CERT;
144         SSL_CTX *s_ctx=NULL;
145         SSL_CTX *c_ctx=NULL;
146         SSL_METHOD *meth=NULL;
147         SSL *c_ssl,*s_ssl;
148         int number=1,reuse=0;
149         long bytes=1L;
150         SSL_CIPHER *ciph;
151 #ifndef NO_DH
152         DH *dh;
153 #endif
154
155         bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
156         bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
157
158         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
159
160         argc--;
161         argv++;
162
163         while (argc >= 1)
164                 {
165                 if      (strcmp(*argv,"-server_auth") == 0)
166                         server_auth=1;
167                 else if (strcmp(*argv,"-client_auth") == 0)
168                         client_auth=1;
169                 else if (strcmp(*argv,"-v") == 0)
170                         verbose=1;
171                 else if (strcmp(*argv,"-d") == 0)
172                         debug=1;
173                 else if (strcmp(*argv,"-reuse") == 0)
174                         reuse=1;
175                 else if (strcmp(*argv,"-ssl2") == 0)
176                         ssl2=1;
177                 else if (strcmp(*argv,"-tls1") == 0)
178                         tls1=1;
179                 else if (strcmp(*argv,"-ssl3") == 0)
180                         ssl3=1;
181                 else if (strncmp(*argv,"-num",4) == 0)
182                         {
183                         if (--argc < 1) goto bad;
184                         number= atoi(*(++argv));
185                         if (number == 0) number=1;
186                         }
187                 else if (strcmp(*argv,"-bytes") == 0)
188                         {
189                         if (--argc < 1) goto bad;
190                         bytes= atol(*(++argv));
191                         if (bytes == 0L) bytes=1L;
192                         i=strlen(argv[0]);
193                         if (argv[0][i-1] == 'k') bytes*=1024L;
194                         if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
195                         }
196                 else if (strcmp(*argv,"-cert") == 0)
197                         {
198                         if (--argc < 1) goto bad;
199                         server_cert= *(++argv);
200                         }
201                 else if (strcmp(*argv,"-s_cert") == 0)
202                         {
203                         if (--argc < 1) goto bad;
204                         server_cert= *(++argv);
205                         }
206                 else if (strcmp(*argv,"-c_cert") == 0)
207                         {
208                         if (--argc < 1) goto bad;
209                         client_cert= *(++argv);
210                         }
211                 else if (strcmp(*argv,"-cipher") == 0)
212                         {
213                         if (--argc < 1) goto bad;
214                         cipher= *(++argv);
215                         }
216                 else if (strcmp(*argv,"-CApath") == 0)
217                         {
218                         if (--argc < 1) goto bad;
219                         CApath= *(++argv);
220                         }
221                 else if (strcmp(*argv,"-CAfile") == 0)
222                         {
223                         if (--argc < 1) goto bad;
224                         CAfile= *(++argv);
225                         }
226                 else
227                         {
228                         fprintf(stderr,"unknown option %s\n",*argv);
229                         badop=1;
230                         break;
231                         }
232                 argc--;
233                 argv++;
234                 }
235         if (badop)
236                 {
237 bad:
238                 sv_usage();
239                 goto end;
240                 }
241
242 /*      if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
243
244         SSL_library_init();
245         SSL_load_error_strings();
246
247 #if !defined(NO_SSL2) && !defined(NO_SSL3)
248         if (ssl2)
249                 meth=SSLv2_method();
250         else 
251         if (tls1)
252                 meth=TLSv1_method();
253         else
254         if (ssl3)
255                 meth=SSLv3_method();
256         else
257                 meth=SSLv23_method();
258 #else
259 #ifdef NO_SSL2
260         meth=SSLv3_method();
261 #else
262         meth=SSLv2_method();
263 #endif
264 #endif
265
266         c_ctx=SSL_CTX_new(meth);
267         s_ctx=SSL_CTX_new(meth);
268         if ((c_ctx == NULL) || (s_ctx == NULL))
269                 {
270                 ERR_print_errors(bio_err);
271                 goto end;
272                 }
273
274         if (cipher != NULL)
275                 {
276                 SSL_CTX_set_cipher_list(c_ctx,cipher);
277                 SSL_CTX_set_cipher_list(s_ctx,cipher);
278                 }
279
280 #ifndef NO_DH
281         dh=get_dh512();
282         SSL_CTX_set_tmp_dh(s_ctx,dh);
283         DH_free(dh);
284 #endif
285
286 #ifndef NO_RSA
287         SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
288 #endif
289
290         if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
291                 {
292                 ERR_print_errors(bio_err);
293                 }
294         else if (!SSL_CTX_use_PrivateKey_file(s_ctx,server_cert,
295                 SSL_FILETYPE_PEM))
296                 {
297                 ERR_print_errors(bio_err);
298                 goto end;
299                 }
300
301         if (client_auth)
302                 {
303                 SSL_CTX_use_certificate_file(c_ctx,client_cert,
304                         SSL_FILETYPE_PEM);
305                 SSL_CTX_use_PrivateKey_file(c_ctx,client_cert,
306                         SSL_FILETYPE_PEM);
307                 }
308
309         if (    (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
310                 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
311                 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
312                 (!SSL_CTX_set_default_verify_paths(c_ctx)))
313                 {
314                 /* fprintf(stderr,"SSL_load_verify_locations\n"); */
315                 ERR_print_errors(bio_err);
316                 /* goto end; */
317                 }
318
319         if (client_auth)
320                 {
321                 fprintf(stderr,"client authentication\n");
322                 SSL_CTX_set_verify(s_ctx,
323                         SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
324                         verify_callback);
325                 }
326         if (server_auth)
327                 {
328                 fprintf(stderr,"server authentication\n");
329                 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
330                         verify_callback);
331                 }
332
333         c_ssl=SSL_new(c_ctx);
334         s_ssl=SSL_new(s_ctx);
335
336         for (i=0; i<number; i++)
337                 {
338                 if (!reuse) SSL_set_session(c_ssl,NULL);
339                 ret=doit(s_ssl,c_ssl,bytes);
340                 }
341
342         if (!verbose)
343                 {
344                 ciph=SSL_get_current_cipher(c_ssl);
345                 fprintf(stdout,"Protocol %s, cipher %s, %s\n",
346                         SSL_get_version(c_ssl),
347                         SSL_CIPHER_get_version(ciph),
348                         SSL_CIPHER_get_name(ciph));
349                 }
350         if ((number > 1) || (bytes > 1L))
351                 printf("%d handshakes of %ld bytes done\n",number,bytes);
352
353         SSL_free(s_ssl);
354         SSL_free(c_ssl);
355
356 end:
357         if (s_ctx != NULL) SSL_CTX_free(s_ctx);
358         if (c_ctx != NULL) SSL_CTX_free(c_ctx);
359
360         if (bio_stdout != NULL) BIO_free(bio_stdout);
361
362         ERR_free_strings();
363         ERR_remove_state(0);
364         EVP_cleanup();
365         CRYPTO_mem_leaks(bio_err);
366         EXIT(ret);
367         }
368
369 #define W_READ  1
370 #define W_WRITE 2
371 #define C_DONE  1
372 #define S_DONE  2
373
374 int doit(SSL *s_ssl, SSL *c_ssl, long count)
375         {
376         MS_STATIC char cbuf[1024*8],sbuf[1024*8];
377         long cw_num=count,cr_num=count;
378         long sw_num=count,sr_num=count;
379         int ret=1;
380         BIO *c_to_s=NULL;
381         BIO *s_to_c=NULL;
382         BIO *c_bio=NULL;
383         BIO *s_bio=NULL;
384         int c_r,c_w,s_r,s_w;
385         int c_want,s_want;
386         int i,j;
387         int done=0;
388         int c_write,s_write;
389         int do_server=0,do_client=0;
390         SSL_CIPHER *ciph;
391
392         c_to_s=BIO_new(BIO_s_mem());
393         s_to_c=BIO_new(BIO_s_mem());
394         if ((s_to_c == NULL) || (c_to_s == NULL))
395                 {
396                 ERR_print_errors(bio_err);
397                 goto err;
398                 }
399
400         c_bio=BIO_new(BIO_f_ssl());
401         s_bio=BIO_new(BIO_f_ssl());
402         if ((c_bio == NULL) || (s_bio == NULL))
403                 {
404                 ERR_print_errors(bio_err);
405                 goto err;
406                 }
407
408         SSL_set_connect_state(c_ssl);
409         SSL_set_bio(c_ssl,s_to_c,c_to_s);
410         BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
411
412         SSL_set_accept_state(s_ssl);
413         SSL_set_bio(s_ssl,c_to_s,s_to_c);
414         BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
415
416         c_r=0; s_r=1;
417         c_w=1; s_w=0;
418         c_want=W_WRITE;
419         s_want=0;
420         c_write=1,s_write=0;
421
422         /* We can always do writes */
423         for (;;)
424                 {
425                 do_server=0;
426                 do_client=0;
427
428                 i=(int)BIO_pending(s_bio);
429                 if ((i && s_r) || s_w) do_server=1;
430
431                 i=(int)BIO_pending(c_bio);
432                 if ((i && c_r) || c_w) do_client=1;
433
434                 if (do_server && debug)
435                         {
436                         if (SSL_in_init(s_ssl))
437                                 printf("server waiting in SSL_accept - %s\n",
438                                         SSL_state_string_long(s_ssl));
439 /*                      else if (s_write)
440                                 printf("server:SSL_write()\n");
441                         else
442                                 printf("server:SSL_read()\n"); */
443                         }
444
445                 if (do_client && debug)
446                         {
447                         if (SSL_in_init(c_ssl))
448                                 printf("client waiting in SSL_connect - %s\n",
449                                         SSL_state_string_long(c_ssl));
450 /*                      else if (c_write)
451                                 printf("client:SSL_write()\n");
452                         else
453                                 printf("client:SSL_read()\n"); */
454                         }
455
456                 if (!do_client && !do_server)
457                         {
458                         fprintf(stdout,"ERROR IN STARTUP\n");
459                         ERR_print_errors(bio_err);
460                         break;
461                         }
462                 if (do_client && !(done & C_DONE))
463                         {
464                         if (c_write)
465                                 {
466                                 j=(cw_num > (long)sizeof(cbuf))
467                                         ?sizeof(cbuf):(int)cw_num;
468                                 i=BIO_write(c_bio,cbuf,j);
469                                 if (i < 0)
470                                         {
471                                         c_r=0;
472                                         c_w=0;
473                                         if (BIO_should_retry(c_bio))
474                                                 {
475                                                 if (BIO_should_read(c_bio))
476                                                         c_r=1;
477                                                 if (BIO_should_write(c_bio))
478                                                         c_w=1;
479                                                 }
480                                         else
481                                                 {
482                                                 fprintf(stderr,"ERROR in CLIENT\n");
483                                                 ERR_print_errors(bio_err);
484                                                 goto err;
485                                                 }
486                                         }
487                                 else if (i == 0)
488                                         {
489                                         fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
490                                         goto err;
491                                         }
492                                 else
493                                         {
494                                         if (debug)
495                                                 printf("client wrote %d\n",i);
496                                         /* ok */
497                                         s_r=1;
498                                         c_write=0;
499                                         cw_num-=i;
500                                         }
501                                 }
502                         else
503                                 {
504                                 i=BIO_read(c_bio,cbuf,sizeof(cbuf));
505                                 if (i < 0)
506                                         {
507                                         c_r=0;
508                                         c_w=0;
509                                         if (BIO_should_retry(c_bio))
510                                                 {
511                                                 if (BIO_should_read(c_bio))
512                                                         c_r=1;
513                                                 if (BIO_should_write(c_bio))
514                                                         c_w=1;
515                                                 }
516                                         else
517                                                 {
518                                                 fprintf(stderr,"ERROR in CLIENT\n");
519                                                 ERR_print_errors(bio_err);
520                                                 goto err;
521                                                 }
522                                         }
523                                 else if (i == 0)
524                                         {
525                                         fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
526                                         goto err;
527                                         }
528                                 else
529                                         {
530                                         if (debug)
531                                                 printf("client read %d\n",i);
532                                         cr_num-=i;
533                                         if (sw_num > 0)
534                                                 {
535                                                 s_write=1;
536                                                 s_w=1;
537                                                 }
538                                         if (cr_num <= 0)
539                                                 {
540                                                 s_write=1;
541                                                 s_w=1;
542                                                 done=S_DONE|C_DONE;
543                                                 }
544                                         }
545                                 }
546                         }
547
548                 if (do_server && !(done & S_DONE))
549                         {
550                         if (!s_write)
551                                 {
552                                 i=BIO_read(s_bio,sbuf,sizeof(cbuf));
553                                 if (i < 0)
554                                         {
555                                         s_r=0;
556                                         s_w=0;
557                                         if (BIO_should_retry(s_bio))
558                                                 {
559                                                 if (BIO_should_read(s_bio))
560                                                         s_r=1;
561                                                 if (BIO_should_write(s_bio))
562                                                         s_w=1;
563                                                 }
564                                         else
565                                                 {
566                                                 fprintf(stderr,"ERROR in SERVER\n");
567                                                 ERR_print_errors(bio_err);
568                                                 goto err;
569                                                 }
570                                         }
571                                 else if (i == 0)
572                                         {
573                                         ERR_print_errors(bio_err);
574                                         fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
575                                         goto err;
576                                         }
577                                 else
578                                         {
579                                         if (debug)
580                                                 printf("server read %d\n",i);
581                                         sr_num-=i;
582                                         if (cw_num > 0)
583                                                 {
584                                                 c_write=1;
585                                                 c_w=1;
586                                                 }
587                                         if (sr_num <= 0)
588                                                 {
589                                                 s_write=1;
590                                                 s_w=1;
591                                                 c_write=0;
592                                                 }
593                                         }
594                                 }
595                         else
596                                 {
597                                 j=(sw_num > (long)sizeof(sbuf))?
598                                         sizeof(sbuf):(int)sw_num;
599                                 i=BIO_write(s_bio,sbuf,j);
600                                 if (i < 0)
601                                         {
602                                         s_r=0;
603                                         s_w=0;
604                                         if (BIO_should_retry(s_bio))
605                                                 {
606                                                 if (BIO_should_read(s_bio))
607                                                         s_r=1;
608                                                 if (BIO_should_write(s_bio))
609                                                         s_w=1;
610                                                 }
611                                         else
612                                                 {
613                                                 fprintf(stderr,"ERROR in SERVER\n");
614                                                 ERR_print_errors(bio_err);
615                                                 goto err;
616                                                 }
617                                         }
618                                 else if (i == 0)
619                                         {
620                                         ERR_print_errors(bio_err);
621                                         fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
622                                         goto err;
623                                         }
624                                 else
625                                         {
626                                         if (debug)
627                                                 printf("server wrote %d\n",i);
628                                         sw_num-=i;
629                                         s_write=0;
630                                         c_r=1;
631                                         if (sw_num <= 0)
632                                                 done|=S_DONE;
633                                         }
634                                 }
635                         }
636
637                 if ((done & S_DONE) && (done & C_DONE)) break;
638                 }
639
640         ciph=SSL_get_current_cipher(c_ssl);
641         if (verbose)
642                 fprintf(stdout,"DONE, protocol %s, cipher %s, %s\n",
643                         SSL_get_version(c_ssl),
644                         SSL_CIPHER_get_version(ciph),
645                         SSL_CIPHER_get_name(ciph));
646         ret=0;
647 err:
648         /* We have to set the BIO's to NULL otherwise they will be
649          * Free()ed twice.  Once when th s_ssl is SSL_free()ed and
650          * again when c_ssl is SSL_free()ed.
651          * This is a hack required because s_ssl and c_ssl are sharing the same
652          * BIO structure and SSL_set_bio() and SSL_free() automatically
653          * BIO_free non NULL entries.
654          * You should not normally do this or be required to do this */
655         if (s_ssl != NULL)
656                 {
657                 s_ssl->rbio=NULL;
658                 s_ssl->wbio=NULL;
659                 }
660         if (c_ssl != NULL)
661                 {
662                 c_ssl->rbio=NULL;
663                 c_ssl->wbio=NULL;
664                 }
665
666         if (c_to_s != NULL) BIO_free(c_to_s);
667         if (s_to_c != NULL) BIO_free(s_to_c);
668         if (c_bio != NULL) BIO_free_all(c_bio);
669         if (s_bio != NULL) BIO_free_all(s_bio);
670         return(ret);
671         }
672
673 int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
674         {
675         char *s,buf[256];
676
677         s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,256);
678         if (s != NULL)
679                 {
680                 if (ok)
681                         fprintf(stderr,"depth=%d %s\n",ctx->error_depth,buf);
682                 else
683                         fprintf(stderr,"depth=%d error=%d %s\n",
684                                 ctx->error_depth,ctx->error,buf);
685                 }
686
687         if (ok == 0)
688                 {
689                 switch (ctx->error)
690                         {
691                 case X509_V_ERR_CERT_NOT_YET_VALID:
692                 case X509_V_ERR_CERT_HAS_EXPIRED:
693                 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
694                         ok=1;
695                         }
696                 }
697
698         return(ok);
699         }
700
701 #ifndef NO_DH
702 static unsigned char dh512_p[]={
703         0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
704         0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
705         0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
706         0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
707         0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
708         0x47,0x74,0xE8,0x33,
709         };
710 static unsigned char dh512_g[]={
711         0x02,
712         };
713
714 static DH *get_dh512(void)
715         {
716         DH *dh=NULL;
717
718         if ((dh=DH_new()) == NULL) return(NULL);
719         dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
720         dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
721         if ((dh->p == NULL) || (dh->g == NULL))
722                 return(NULL);
723         return(dh);
724         }
725 #endif
726
727 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int export, int keylength)
728         {
729         static RSA *rsa_tmp=NULL;
730
731         if (rsa_tmp == NULL)
732                 {
733                 BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
734                 BIO_flush(bio_err);
735 #ifndef NO_RSA
736                 rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
737 #endif
738                 BIO_printf(bio_err,"\n");
739                 BIO_flush(bio_err);
740                 }
741         return(rsa_tmp);
742         }
743
744