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