File: ssl.c

package info (click to toggle)
netkit-telnet-ssl 0.17.41%2B0.2-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,744 kB
  • ctags: 7,417
  • sloc: cpp: 6,952; ansic: 6,720; sh: 203; makefile: 162
file content (1034 lines) | stat: -rw-r--r-- 25,192 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
#ifdef	USE_SSL
/* 
 * The modifications to support SSLeay were done by Tim Hudson
 * tjh@mincom.oz.au
 *
 * You can do whatever you like with these patches except pretend that
 * you wrote them. 
 *
 * Email ssl-users-request@mincom.oz.au to get instructions on how to
 * join the mailing list that discusses SSLeay and also these patches.
 *
 */

/* ssl.c    - interface to Eric Young's SSLeay library (eay@mincom.oz.au)
 *
 * see LICENSE for details 
 *
 * xx-Aug-96 tjh    reworked the client certificate stuff into a form
 * ................ where it is useful with SSLeay-0.6.x which changed
 * ................ lots of things in the handling of the verify function
 * 01-Jul-95 tjh    merged patches from Steven Schoch 
 * ................ <schoch@sheba.arc.nasa.gov> that add in the certsok
 * ................ option for using signed certificates rather than 
 * ................ explicit passwords for authentication (modified a little
 * ................ to add in an option that controls this feature)
 * 26-Apr-95 tjh    original coding
 *
 * tjh@mincom.oz.au
 * tjh@mincom.com
 *
 * Tim Hudson
 * Mincom Pty Ltd
 * Australia
 * +61 7 3303 3333
 *
 */

#include <sys/types.h>
#include "arpa/telnet.h"
#include <stdio.h>
#ifdef	__STDC__
#include <stdlib.h>
#endif
#ifdef	NO_STRING_H
#include <strings.h>
#else
#include <string.h>
#endif

#include <syslog.h>
#include <unistd.h>
#include <openssl/err.h>

#include "auth.h"
#include "misc.h"

#include "crypto.h"

#if SSLEAY_VERSION_NUMBER >= 0x0800
#define SSLEAY8
#endif
 
#ifdef SSLEAY8
#define ONELINE_NAME(X) X509_NAME_oneline(X,NULL,0)
#else
#define ONELINE_NAME(X) X509_NAME_oneline(X)
#endif

#ifdef SSLEAY8
#include "bio.h"
#endif
#include "buffer.h"

#include "x509.h"
#include "ssl.h"

/* quick translation ... */
#ifndef VERIFY_ERR_UNABLE_TO_GET_ISSUER
#define VERIFY_ERR_UNABLE_TO_GET_ISSUER X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
#endif
#ifndef VERIFY_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
#define VERIFY_ERR_DEPTH_ZERO_SELF_SIGNED_CERT X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 
#endif
#ifndef VERIFY_ERR_SELF_SIGNED_CERT_IN_CHAIN
# define VERIFY_ERR_SELF_SIGNED_CERT_IN_CHAIN X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
#endif
#ifndef VERIFY_OK
#define VERIFY_OK X509_V_OK
#endif
#ifndef VERIFY_ERR_UNABLE_TO_GET_ISSUER
#define VERIFY_ERR_UNABLE_TO_GET_ISSUER X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
#endif

/* need to think about this mapping in terms of what the real
 * equivalent of this actually is
 */
#ifndef VERIFY_ROOT_OK
#define VERIFY_ROOT_OK VERIFY_OK
#endif

/* Two possibilities:
 *   X509_V_ERR_INVALID_PURPOSE
 * or
 *   X509_V_ERR_APPLICATION_VERIFICATION
 *
 * The resulting error messages are not particularly helpful.
 */
#ifndef VERIFY_REJECTED_BY_CERTSOK
# define VERIFY_REJECTED_BY_CERTSOK X509_V_ERR_APPLICATION_VERIFICATION
#endif

#ifndef SSL_USERS_FILE
# define SSL_USERS_FILE "/etc/ssl.users"
#endif

extern int netflush(void);

extern int auth_debug_mode;
static int auth_ssl_valid = 0;
static char *auth_ssl_name = 0;    /* this holds the oneline name */

extern int ssl_only_flag;
extern int ssl_debug_flag;
extern int ssl_active_flag;
extern int ssl_secure_flag;
extern int ssl_verify_flag;
extern int ssl_certsok_flag;       /* if this is set then we enable the
                                    * /etc/ssl.users stuff for allowing
				    * access - just to make sure we don't
				    * switch it on unless we really want it
                                    */
extern int ssl_cert_required;      /* client certificate is mandatory! */

extern int ssl_verbose_flag;
extern int ssl_disabled_flag;

int server_verify_callback();
int client_verify_callback();

extern SSL *ssl_con;

#include "buffer.h"

BIO *bio_err=NULL;

int auth_failed=0;


/* compile this set to 1 to negotiate SSL but not actually start it */
static int ssl_dummy_flag=0;

static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
			  		AUTHTYPE_SSL, };

#define AUTH_SSL_START     1
#define AUTH_SSL_ACCEPT    2
#define AUTH_SSL_REJECT    3


/* this is called by both the ssl.c auth connect and the mainline
 * telnet connect if we are talking straight ssl with no telnet
 * protocol --tjh
 */
void
display_connect_details(ssl_con,verbose)
SSL *ssl_con;
int verbose;
{
    X509 *peer;
    char *p;

    if (ssl_active_flag && verbose) {
#ifdef SSLEAY8
	int i;
#endif /* SSLEAY8 */
	fprintf(stderr,"[SSL cipher=");
#ifdef SSLEAY8
	/* grab the full list of ciphers */
	i=0;
	while((p=(char *)SSL_get_cipher_list(ssl_con,i++))!=NULL) {
	    if (i>1) {
		fprintf(stderr,":");
	    }
	    fprintf(stderr, "%s", p);
	}
	if(i==1) {
	    fprintf(stderr, "<NULL>");
	}
#else /* !SSLEAY8 */
	p=SSL_get_cipher(ssl_con);
	if(p) {
	    fprintf(stderr, "%s", p);
	} else {
	    /* the cipher list *can* be NULL ... useless but it happens! */
	    fprintf(stderr, "<NULL>");
	}
#endif /* !SSLEAY8 */
	fprintf(stderr,"]\r\n");
	peer=SSL_get_peer_certificate(ssl_con);
	if (peer != NULL) {
	    char *str;

	    str=ONELINE_NAME(X509_get_subject_name(peer));
	    fprintf(stderr,"[SSL subject=%s]\r\n",str);
	    free(str);
	    str=ONELINE_NAME(X509_get_issuer_name(peer));
	    fprintf(stderr,"[SSL issuer=%s]\r\n",str);
	    free(str);
	    X509_free(peer);
			    
	}
	fflush(stderr);
    }
}


	void
fprintd(fp, data, cnt)
	FILE *fp;
	unsigned char *data;
	int cnt;
{
	if (cnt > 16)
		cnt = 16;
	while (cnt-- > 0) {
		fprintf(fp," %02x", *data);
		++data;
	}
}

/* support routine to send out authentication message */
static int Data(ap, type, d, c)
Authenticator *ap;
int type;
void *d;
int c;
{
        unsigned char *p = str_data + 4;
	unsigned char *cd = (unsigned char *)d;

	if (c == -1)
		c = strlen((char *)cd);

        if (auth_debug_mode) {
                fprintf(stderr,"%s:%d: [%d] (%d)",
                        str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
                        str_data[3],
                        type, c);
                fprintd(stderr,d, c);
                fprintf(stderr,"\r\n");
        }
	*p++ = ap->type;
	*p++ = ap->way;
	*p++ = type;
        while (c-- > 0) {
                if ((*p++ = *cd++) == IAC)
                        *p++ = IAC;
        }
        *p++ = IAC;
        *p++ = SE;
	if (str_data[3] == TELQUAL_IS)
		printsub('>', &str_data[2], p - (&str_data[2]));
        return(writenet((char *) str_data, p - str_data));
}

int auth_ssl_init(ap, server)
Authenticator *ap;
int server;
{
	/* ssl only option skips all of this muck ... */
	if (ssl_only_flag || ssl_disabled_flag) {
	    return 0;
	}

	SSL_load_error_strings();

	/* SSLeay-0.6 introduces the BIO stuff ... which we need for
	 * all the error reporting things! 
	 */
	if (bio_err == NULL)
		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE);

	if (server)
		str_data[3] = TELQUAL_REPLY;
	else
		str_data[3] = TELQUAL_IS;
	return(1);
}

/* client received a go-ahead for ssl */
int auth_ssl_send(ap)
Authenticator *ap;
{
	fprintf(stderr,"[SSL - attempting to switch on SSL]\r\n");
	fflush(stderr);

	if (!Data(ap, AUTH_SSL_START, NULL, 0 )) {
		if (auth_debug_mode)
			fprintf(stderr,"Not enough room for start data\r\n");
		return(0);
	}

	return(1);
}

/* server received an IS -- could (only) be SSL START */
void auth_ssl_is(ap, data, cnt)
Authenticator *ap;
unsigned char *data;
int cnt;
{
  /*	int valid; */

	if (cnt-- < 1)
		return;
	switch (*data++) {

	case AUTH_SSL_START:
		Data(ap, AUTH_SSL_ACCEPT, (void *)0, 0);
		netflush();

		auth_ssl_valid = 1;
		auth_finished(ap, AUTH_VALID);

		/* server starts the SSL stuff now ... */
		if (ssl_dummy_flag)
		    return;

		if (ssl_debug_flag && bio_err) {
		    (void) BIO_printf(bio_err, "AUTH SSL is starting\r\n");
		    (void) BIO_flush(bio_err);
		}

		if (!ssl_only_flag) {
		    /* only want/need verify if doing certsok stuff */
		    if (ssl_certsok_flag||ssl_cert_required) 
			SSL_set_verify(ssl_con,ssl_verify_flag,server_verify_callback);
		    if (SSL_accept(ssl_con) <= 0) {
			char *res, *p;
			/*
			syslog(LOG_WARNING, "ssl_accept error");
			*/

			res = ERR_error_string(ERR_peek_last_error(), NULL);
			p = strrchr(res, ':');

			syslog(LOG_NOTICE, "SSL_accept error: %s",
				p ? &p[1] : res);

			if (ssl_debug_flag && bio_err) {
			    (void) BIO_printf(bio_err, "SSL_accept: %s\r\n",
					      p ? &p[1] : res);
			    (void) BIO_flush(bio_err);
			}
			sleep(5);
			SSL_free(ssl_con);

			auth_finished(ap, AUTH_REJECT);

			_exit(1);
		    } else {
			ssl_active_flag=1;
		    }

		    /* now check to see that we got exactly what we 
		     * wanted from the caller ... if a certificate is
		     * required then we make 100% sure that we were
		     * given on during the handshake (as it is an optional
		     * part of SSL)
		     */
		    if ( ssl_cert_required ) {
		    	if (SSL_get_peer_certificate(ssl_con)==NULL) {

			    if (ssl_debug_flag) {
				fprintf(stderr,"[SSL - peer check failed]\r\n");
				fflush(stderr);
			    }
			    if (ssl_debug_flag && bio_err) {
				(void) BIO_printf(bio_err, "SSL - peer sent no certificate\r\n");
				(void) BIO_flush(bio_err);
			    }

			    /* LOGGING REQUIRED HERE! */
			    SSL_free(ssl_con);
			    auth_finished(ap, AUTH_REJECT);
			    _exit(1);
			}
		    }
		}
		break;

	default:
		fprintf(stderr,"[SSL - failed to switch on SSL]\r\n");
		fflush(stderr);

		if (auth_debug_mode) {
			fprintf(stderr,"Unknown SSL option %d\r\n", data[-1]);
			fprintf(stderr,"[SSL - negotiation failed]\r\n");
		}
		Data(ap, AUTH_SSL_REJECT, (void *)0, 0);

		auth_ssl_valid = 0;
		auth_finished(ap, AUTH_REJECT);
		break;
	}
}

/* client received REPLY -- could be SSL ACCEPT or REJECT */
void auth_ssl_reply(ap, data, cnt)
Authenticator *ap;
unsigned char *data;
int cnt;
{
  /*	int i; */
	int status;

	if (cnt-- < 1)
		return;
	switch (*data++) {

	case AUTH_SSL_ACCEPT:
		if (auth_debug_mode)
			fprintf(stderr,"SSL ACCEPT\r\n");
		fprintf(stderr,"[SSL - handshake starting]\r\n");

		auth_finished(ap, AUTH_VALID);

		if (ssl_dummy_flag) {
		    fprintf(stderr,"[SSL - Dummy Connected]\r\n");
		    fflush(stderr);
		    return;
		}

		/* right ... now we drop into the SSL library */
		if (!ssl_only_flag) {
		    SSL_set_verify(ssl_con,ssl_verify_flag,
		    				client_verify_callback);
		    if ((status = SSL_connect(ssl_con)) <= 0) {
			char *res, *p;

			auth_finished(0,AUTH_REJECT);

			res = ERR_error_string(ERR_peek_last_error(), NULL);
			p = strrchr(res, ':');
			fprintf(stderr,"SSL: %s\n", p ? &p[1] : res);
			fprintf(stderr,"telnet: Unable to ssl_connect to remote host\n");
			fflush(stderr);
			auth_failed=1;
			return;
		    } else {

			fprintf(stderr,"[SSL - OK]\r\n");
			fflush(stderr);

			ssl_active_flag=1;
			display_connect_details(ssl_con,ssl_debug_flag);
		    }
		}

		/* this is handy/required? */
		/*
		netflush();
		*/

		break;

	case AUTH_SSL_REJECT:
		if (auth_debug_mode)
			fprintf(stderr,"SSL REJECT\r\n");
		fprintf(stderr,"[SSL - failed to switch on SSL]\r\n");
		fprintf(stderr,"Trying plaintext login:\r\n");
		fflush(stderr);
		auth_finished(0,AUTH_REJECT);
		break;

	default:
		if (auth_debug_mode)
			fprintf(stderr,"Unknown SSL option %d\r\n", data[-1]);
		return;
	}
}

int auth_ssl_status(ap, name, level)
Authenticator *ap;
char *name;
int level;
{
	FILE *user_fp;
	char buf[2048];

	if (level < AUTH_USER)
		return(level);

	/*
	 * Look our name up in /etc/ssl.users.
	 * The format of this file is lines of this form:
	 *   user1,user2:/C=US/.....
	 * where user1 and user2 are usernames
	 */
	if (ssl_certsok_flag) {
	    user_fp = fopen(SSL_USERS_FILE, "r");
	    if (!auth_ssl_name || !user_fp || !UserNameRequested) {
	        /* If we haven't received a certificate, then don't 
		 * return AUTH_VALID. 
		 */
		if (UserNameRequested)
			strcpy(name, UserNameRequested);
		/* be tidy ... */
		if (user_fp)
		    fclose(user_fp);
		return AUTH_USER;
	    }
	    while (fgets(buf, sizeof buf, user_fp)) {
		char *cp;
		char *n;

		/* allow for comments in the file ... always nice
		 * to be able to add a little novel in files and
		 * also disable entries easily --tjh
		 */
		if (buf[0]=='#')
		    continue;

		if ((cp = strchr(buf, '\n')))
		    *cp = '\0';
		cp = strchr(buf, ':');
		if (!cp)
		    continue;
		*cp++ = '\0';
		if (strcasecmp(cp, auth_ssl_name) == 0) {
		    n = buf;
		    while (n) {
			cp = strchr(n, ',');
			if (cp)
			    *cp++ = '\0';
			if (UserNameRequested &&
			            !strcmp(UserNameRequested, n)) {
			    strcpy(name, n);
			    fclose(user_fp);

			    syslog(LOG_AUTH | LOG_INFO,
				   "Certsok autologin %s: %s",
				   UserNameRequested, auth_ssl_name);
			    if (ssl_debug_flag)
				(void) BIO_printf(bio_err,
						  "Certsok for %s: %s\r\n",
						  UserNameRequested,
						  auth_ssl_name);
			    return(AUTH_VALID);
			}
			n = cp;
		    }
		}
	    }
	    fclose(user_fp);
	    return(AUTH_USER);
	} else {
	    return(AUTH_USER);
	}
}

#define	BUMP(buf, len)		while (*(buf)) {++(buf), --(len);}
#define	ADDC(buf, len, c)	if ((len) > 0) {*(buf)++ = (c); --(len);}

void auth_ssl_printsub(data, cnt, buf, buflen)
unsigned char *data, *buf;
int cnt, buflen;
{
	char lbuf[32];
	register int i;

	buf[buflen-1] = '\0';		/* make sure its NULL terminated */
	buflen -= 1;

	switch(data[3]) {

	case AUTH_SSL_START:
		strncpy((char *)buf, " START ", buflen);
		goto common;

	case AUTH_SSL_REJECT:		/* Rejected (reason might follow) */
		strncpy((char *)buf, " REJECT ", buflen);
		goto common;

	case AUTH_SSL_ACCEPT:		/* Accepted (name might follow) */
		strncpy((char *)buf, " ACCEPT ", buflen);

	common:
		BUMP(buf, buflen);
		if (cnt <= 4)
			break;
		ADDC(buf, buflen, '"');
		for (i = 4; i < cnt; i++)
			ADDC(buf, buflen, data[i]);
		ADDC(buf, buflen, '"');
		ADDC(buf, buflen, '\0');
		break;

	default:
		sprintf(lbuf, " %d (unknown)", data[3]);
		strncpy((char *)buf, lbuf, buflen);
/*	common2: */
		BUMP(buf, buflen);
		for (i = 4; i < cnt; i++) {
			sprintf(lbuf, " %d", data[i]);
			strncpy((char *)buf, lbuf, buflen);
			BUMP(buf, buflen);
		}
		break;
	}
}


int
#ifdef SSLEAY8
server_verify_callback(ok, ctx)
int ok;
X509_STORE_CTX *ctx;
#else /* !SSLEAY8 */
server_verify_callback(ok, xs, xi, depth, error)
int ok;
char *xs, *xi;
int depth, error;
#endif /* SSLEAY8 */
{
    static char *saved_subject=NULL;
    /*    X509 *peer; */
    char *subject, *issuer;
#ifdef SSLEAY8
    int depth,error;
    X509 *xs;

    depth = X509_STORE_CTX_get_error_depth(ctx);
    error = X509_STORE_CTX_get_error(ctx);
    xs = X509_STORE_CTX_get_current_cert(ctx);

#endif /* SSLEAY8 */

#ifdef EXTRA_DEBUGGING
    if (ssl_debug_flag) {
	(void) BIO_printf(bio_err,"ssl:server_verify_callback:depth=%d ok=%d err=%d - %s\n",
	    depth,ok,error,X509_verify_cert_error_string(error));
	(void) BIO_flush(bio_err);
    }
#endif /* EXTRA_DEBUGGING */

    subject=issuer=NULL;

    /* first thing is to have a meaningful name for the current
     * certificate that is being verified ... and if we cannot
     * determine that then something is seriously wrong!
     */
    subject=(char *)ONELINE_NAME(X509_get_subject_name((X509 *)xs));
    if (subject==NULL) {
	if (ssl_debug_flag) 
	    ERR_print_errors(bio_err);
	ok=0;
	goto return_time;
    }
    issuer=(char *)ONELINE_NAME(X509_get_issuer_name((X509 *)xs));
    if (issuer==NULL) {
	if (ssl_debug_flag)
	    ERR_print_errors(bio_err);
	ok=0;
	goto return_time;
    }

    /* save the name of the first level subject as this is
     * the name we want to use to match for a username in
     * /etc/ssl.users later ... but *only* if we pass the
     * full verification of the certificate chain
     */
    if (depth==0) {
	/* clear things */
	if (saved_subject!=NULL) {
	    free(saved_subject);
	    saved_subject=NULL;
	}
	if (auth_ssl_name!=NULL) {
	    free(auth_ssl_name);
	    auth_ssl_name=NULL;
	}

	/* save the name if at least the first level is okay */
	if (ok)
	    saved_subject=strdup(subject);
    }

    /* if the client is using a self signed certificate then 
     * we need to decide if that is good enough for us to 
     * accept ... it certainly isn't good enough for anything
     * that wants to use the certificate as it is basically
     * junk of no value in this context!
     */
    if (error == VERIFY_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
	|| error == VERIFY_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
	if (ssl_cert_required) {
	    /* make 100% sure that in secure mode we drop the 
	     * connection if the server does not have a 
	     * real certificate!
	     */
	    if (ssl_debug_flag) {
		(void) BIO_printf(bio_err,"SSL: rejecting connection - self-signed cert\n");
		(void) BIO_flush(bio_err);
	    }

	    ok=0;
	    goto return_time;
	} else {
	    ok=1;
	    goto return_time;
	}
    }

    /* if we have any form of error in secure mode we reject the connection */
    if (! ((error==VERIFY_OK)||(error==VERIFY_ROOT_OK)) ) {
	if (ssl_cert_required) {
	    if (ssl_debug_flag) {
		(void) BIO_printf(bio_err,"SSL: rejecting connection - ");
		if (error==VERIFY_ERR_UNABLE_TO_GET_ISSUER) {
		    (void) BIO_printf(bio_err,"unknown issuer: %s\n",issuer);
		} else {
		    ERR_print_errors(bio_err);
		}
		(void) BIO_flush(bio_err);
	    }
	    ok=0;
	    goto return_time;
	} else {
	    /* be nice and display a lot more meaningful stuff 
	     * so that we know which issuer is unknown no matter
	     * what the callers options are ...
	     */
	    if (error==VERIFY_ERR_UNABLE_TO_GET_ISSUER) {
		if (ssl_debug_flag) {
		    (void) BIO_printf(bio_err,"SSL: unknown issuer: %s\n",issuer);
		    (void) BIO_flush(bio_err);
		}
	    }
	}
    } else {
	/* if we got all the way to the top of the tree then
	 * we *can* use this certificate for a username to 
	 * match ... in all other cases we must not!
	 */
	if ( (error==VERIFY_ROOT_OK) ) {
	    auth_ssl_name = saved_subject;
	    saved_subject = NULL;
	}
    }

return_time: ;

    /* clean up things */
    if (subject!=NULL)
	free(subject);
    if (issuer!=NULL)
	free(issuer);

    return ok;
}

int
#ifdef SSLEAY8
client_verify_callback(ok, ctx)
int ok;
X509_STORE_CTX *ctx;
#else /* !SSLEAY8 */
client_verify_callback(ok, xs, xi, depth, error)
int ok;
char *xs, *xi;
int depth, error;
#endif /* SSLEAY8 */
{
  /*    X509 *peer; */
    char *subject, *issuer, *cnsubj;
#ifdef SSLEAY8
    int depth,error;
    X509 *xs;

    depth = X509_STORE_CTX_get_error_depth(ctx);
    error = X509_STORE_CTX_get_error(ctx);
    xs = X509_STORE_CTX_get_current_cert(ctx);

#endif /* SSLEAY8 */

    if(ssl_debug_flag && !ok) {
      (void) BIO_printf(bio_err,"ssl:client_verify_callback:depth=%d ok=%d err=%d - %s\r\n",
	      depth,ok,error,X509_verify_cert_error_string(error));
      (void) BIO_flush(bio_err);
    }

    subject=issuer=cnsubj=NULL;

    /* first thing is to have a meaningful name for the current
     * certificate that is being verified ... and if we cannot
     * determine that then something is seriously wrong!
     */
    subject=(char *)ONELINE_NAME(X509_get_subject_name((X509 *)xs));
    if (subject==NULL) {
	ERR_print_errors(bio_err);
	ok=0;
	goto return_time;
    }
    issuer=(char *)ONELINE_NAME(X509_get_issuer_name((X509 *)xs));
    if (issuer==NULL) {
	ERR_print_errors(bio_err);
	ok=0;
	goto return_time;
    }

    /* if the user wants us to be chatty about things then this
     * is a good time to wizz the certificate chain past quickly :-)
     */
    if (ssl_verbose_flag) {
	fprintf(stderr,"Certificate[%d] subject=%s\n",depth,subject);
	fprintf(stderr,"Certificate[%d] issuer =%s\n",depth,issuer);
	fflush(stderr);
    }

    /* verify commonName matches hostname */
    if(ssl_cert_required && depth == 0) {
      char *cn,*p;	
	  
      cnsubj=strdup(subject);
      if(cnsubj == NULL) {
	fprintf(stderr,"SSL: Out of memory.\n");
	ok=0;
	goto return_time;
      }
      cn=strstr(cnsubj,"/CN=");
      if(cn == NULL) {
	fprintf(stderr,"SSL: Cannot extract CN from certificate subject.\n");
	ok=0;
	goto return_time;
      }
      cn+=4; /* skip /CN= */
      p=strchr(cn,'/');
      if(p != NULL) {
	*p='\0';
      }
      if(strcasecmp(cn,RemoteHostName) != 0) {
	fprintf(stderr,"SSL: Certificate CN (%s) does not match hostname (%s)\n",
		cn,RemoteHostName);
	ok=0;
	goto return_time;
      }
    }

    if((error==VERIFY_OK) || (error==VERIFY_ROOT_OK)) {
      goto return_time;
    }

    switch(error) {
    case VERIFY_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
      fprintf(stderr,"SSL: Server has a self-signed certificate\n");
      fprintf(stderr, "SSL: unknown Issuer: %s\n", issuer);
      break;
    case VERIFY_ERR_SELF_SIGNED_CERT_IN_CHAIN:
      fprintf(stderr, "SSL: Server uses self-signed certificate in chain.\n");
      fprintf(stderr, "SSL: unknown Issuer: %s\n", issuer);
      break;
    case VERIFY_ERR_UNABLE_TO_GET_ISSUER:
      fprintf(stderr,"SSL: unknown issuer: %s\n",issuer);
      break;
    case X509_V_ERR_CERT_NOT_YET_VALID:
      fprintf(stderr,"SSL: Certificate not yet valid\n");
      BIO_printf(bio_err,"notBefore=");
      ASN1_TIME_print(bio_err, X509_get_notBefore((X509 *) xs));
      BIO_printf(bio_err,"\n");
      break;
    case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
      fprintf(stderr,"SSL: Error in certificate notBefore field\n");
      BIO_printf(bio_err,"notBefore=");
      ASN1_TIME_print(bio_err, X509_get_notBefore((X509 *) xs));
      BIO_printf(bio_err,"\n");
      break;
    case X509_V_ERR_CERT_HAS_EXPIRED:
      fprintf(stderr,"SSL: Certificate has expired\n");
      BIO_printf(bio_err,"notAfter=");
      ASN1_TIME_print(bio_err, X509_get_notAfter((X509 *) xs));
      BIO_printf(bio_err,"\n");
      break;
    case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
      fprintf(stderr,"SSL: Error in certificate notAfter field\n");
      BIO_printf(bio_err,"notAfter=");
      ASN1_TIME_print(bio_err, X509_get_notAfter((X509 *) xs));
      BIO_printf(bio_err,"\n");
      break;
    default:
      fprintf(stderr,"SSL: %s (%d)\n", X509_verify_cert_error_string(error),error);
      break;
    }

    /* If we are here there was an error */
    ok=0;
    
return_time: ;

    /* clean up things */
    if (subject!=NULL)
	free(subject);
    if (issuer!=NULL)
	free(issuer);
    if (cnsubj!=NULL)
	free(cnsubj);
    if(!ok && ssl_cert_required) {
      if(ssl_debug_flag) {
	(void) BIO_printf(bio_err,"SSL: debug -> ignoring cert required!\n");
	(void) BIO_flush(bio_err);
	ok=1;
      }
      else {
	fprintf(stderr,"SSL: Rejecting connection\n");
	ok=0;
      }
    }
    fflush(stderr);
      
    return ok;
}

/* To be used by server when ssl_only_flag is set.  */
int
ssl_only_verify_callback(int ok, X509_STORE_CTX *ctx)
{
    static char *saved_subject = NULL;
    char *subject;
    int err, depth;
    X509 *cert;

    cert  = X509_STORE_CTX_get_current_cert(ctx);
    err   = X509_STORE_CTX_get_error(ctx);
    depth = X509_STORE_CTX_get_error_depth(ctx);

# ifdef EXTRA_DEBUGGING
    if (ssl_debug_flag) {
	(void) BIO_printf(bio_err, "Verify callback: depth %d, err %d, %s\r\n",
			  depth, err, X509_verify_cert_error_string(err));
	(void) BIO_flush(bio_err);
    }
# endif /* EXTRA_DEBUGGING */

    subject = ONELINE_NAME(X509_get_subject_name(cert));
    if (subject == NULL) {
	ok = 0;

	if (ssl_debug_flag) 
	    (void) BIO_printf(bio_err, "Subject name: %s\r\n",
			      ERR_reason_error_string(ERR_peek_error()));
    }

    if (depth == 0 && subject) {
	free(auth_ssl_name);
	free(saved_subject);
	auth_ssl_name = saved_subject = NULL;

	if (ok)
	    saved_subject = strdup(subject);

	/* Check to see if the certsok list contains this
	 * particular certificate subject.
	 */
	if (ssl_certsok_flag && ssl_cert_required) {
	    char buf[2048];
	    FILE *fp = fopen(SSL_USERS_FILE, "r");

	    if (!fp) {
		/* Missing file is treated as verification failure.  */
		ok = 0;
		SSL_set_verify_result(ssl_con, VERIFY_REJECTED_BY_CERTSOK);
		if (ssl_debug_flag)
		    (void) BIO_printf(bio_err, "Accessing %s: %s\r\n",
				      SSL_USERS_FILE, strerror(errno));
	    } else {
		while(fgets(buf, sizeof(buf), fp)) {
		    char *p;

		    if ((p = strchr(buf, '\n')))
			*p = '\0';

		    p = buf;

		    while (*p && strchr(" \t", *p))
			p++;

		    if (*p == '#')
			continue;

		    p = strchr(buf, ':');
		    if (!p)
			continue;

		    if (strcmp(++p, subject) == 0)
			/* An acceptable subject has been found.  */
			break;
		}

		if (feof(fp)) {
		    /* The file is at EOF, so no acceptable subject name
		     * was included.  Treat this as verification failure.
		     */
		    ok = 0;
		    SSL_set_verify_result(ssl_con, VERIFY_REJECTED_BY_CERTSOK);

		    if (ssl_debug_flag)
			(void) BIO_printf(bio_err, "Rejected by certsok: %s\r\n",
					  subject);
		} else if (ssl_debug_flag)
		    (void) BIO_printf(bio_err, "Certsok found: %s\r\n",
				      subject);

		fclose(fp);
	    }
	}
    }

    /* Depth zero is examined as the very last chained certificate.
     * An acceptable verification makes the subject name relevant.
     */
    if (ok && (err == VERIFY_ROOT_OK) && (depth == 0)) {
	auth_ssl_name = saved_subject;
	saved_subject = NULL;
    }

    free(subject);

    return ok;
} /* ssl_only_verify_callback */

#endif /* USE_SSL */