1 char *cksslv = "SSL/TLS support, 9.0.232, 5 Feb 2015";
3 C K _ S S L . C -- OpenSSL Interface for C-Kermit
5 Copyright (C) 1985, 2015,
6 Trustees of Columbia University in the City of New York.
7 All rights reserved. See the C-Kermit COPYING.TXT file or the
8 copyright text in the ckcmai.c module for disclaimer and permissions.
10 Author: Jeffrey E Altman (jaltman@secure-endpoints.com)
11 Secure Endpoints Inc., New York City
15 . Telnet Auth SSL option compatible with Tim Hudson's hack.
16 . Telnet START_TLS option
17 . Configuration of certificate and key files
18 . Certificate verification and revocation list checks
19 . Client certificate to user id routine
21 Note: This code is written to be compatible with OpenSSL 0.9.6[abcdefgh]
22 and 0.9.7 beta 5 and later, and (since July 2012) 1.0.x.
23 It will also compile with version 0.9.5 although that is discouraged
24 due to security weaknesses in that release.
37 #include <netinet/in.h>
39 #include <arpa/inet.h>
46 #endif /* DEC_TCPIP */
51 char * GetAppData(int);
55 extern int quiet; /* fdc - Mon Nov 28 11:44:15 2005 */
57 static int ssl_installed = 1;
65 extern HINSTANCE hCRYPTO;
67 extern HMODULE hCRYPTO;
69 debug(F111,"ck_ssh_is_installed","hCRYPTO",hCRYPTO);
70 return(ssl_installed && (hCRYPTO != NULL));
72 return(ssl_installed);
81 ck_ssleay_is_installed(void)
83 ck_ssleay_is_installed()
89 extern HINSTANCE hSSL, hCRYPTO;
91 extern HMODULE hSSL, hCRYPTO;
93 debug(F111,"ck_ssleay_is_installed","hSSL",hSSL);
94 debug(F111,"ck_ssleay_is_installed","hCRYPTO",hCRYPTO);
95 return(ssl_installed && (hSSL != NULL) && (hCRYPTO != NULL));
97 return(ssl_installed);
106 #include "ckucmd.h" /* For struct keytab */
110 #include <pwd.h> /* Password file for home directory */
120 int ssl_finished_messages = 0;
122 #ifdef OPENSSL_VERSION_NUMBER
123 int ssl_finished_messages = (OPENSSL_VERSION_NUMBER >= 0x0090581fL);
125 !ERROR This module requires OpenSSL 0.9.5a or higher
126 #endif /* OPENSSL_VERSION_NUMBER */
129 static int auth_ssl_valid = 0;
130 static char *auth_ssl_name = 0; /* this holds the oneline name */
131 char ssl_err[SSL_ERR_BFSZ]="";
134 X509_STORE *crl_store = NULL;
138 SSL *ssl_ftp_con = NULL;
139 SSL_CTX *ssl_ftp_ctx = NULL;
140 SSL *ssl_ftp_data_con = NULL;
141 int ssl_ftp_active_flag = 0;
142 int ssl_ftp_data_active_flag = 0;
147 SSL *tls_http_con = NULL;
148 SSL_CTX *tls_http_ctx = NULL;
149 int tls_http_active_flag = 0;
150 int ssl_http_initialized = 0;
153 SSL_CTX *ssl_ctx = NULL;
155 int ssl_debug_flag = 0;
156 int ssl_verbose_flag = 0;
157 int ssl_only_flag = 0;
158 int ssl_raw_flag = 0;
159 int ssl_active_flag = 0;
160 int ssl_verify_flag = SSL_VERIFY_PEER;
161 int ssl_certsok_flag = 0;
162 char *ssl_rsa_cert_file = NULL;
163 char *ssl_rsa_cert_chain_file = NULL;
164 char *ssl_rsa_key_file = NULL;
165 char *ssl_dsa_cert_file = NULL;
166 char *ssl_dsa_cert_chain_file = NULL;
167 char *ssl_dh_key_file = NULL;
168 char *ssl_crl_file = NULL;
169 char *ssl_crl_dir = NULL;
170 char *ssl_verify_file = NULL;
171 char *ssl_verify_dir = NULL;
172 char *ssl_dh_param_file = NULL;
173 char *ssl_cipher_list = NULL;
174 char *ssl_rnd_file = NULL;
176 SSL_CTX *tls_ctx = NULL;
178 int tls_only_flag = 0;
179 int tls_raw_flag = 0;
180 int tls_active_flag = 0;
182 int ssl_initialized = 0;
183 int ssl_verify_depth = -1; /* used to track depth in verify routines */
185 /* compile this set to 1 to negotiate SSL/TLS but not actually start it */
186 int ssl_dummy_flag=0;
190 extern int accept_complete;
191 extern char szHostName[], szUserNameRequested[], szUserNameAuthenticated[];
193 _PROTOTYP(int X509_to_user,(X509 *, char *, int));
195 static int verbosity = 0; /* Message control */
198 verbosity = ssl_verbose_flag;
199 if (quiet) verbosity = 0;
204 ssl_server_verify_callback(int ok, X509_STORE_CTX * ctx)
206 ssl_server_verify_callback(ok, ctx)
209 #endif /* CK_ANSIC */
211 static char *saved_subject=NULL;
212 char *subject=NULL, *issuer=NULL;
216 if ( ssl_certsok_flag )
221 error=X509_STORE_CTX_get_error(ctx);
222 depth=X509_STORE_CTX_get_error_depth(ctx);
223 xs=X509_STORE_CTX_get_current_cert(ctx);
227 if (saved_subject!=NULL) {
231 if (auth_ssl_name!=NULL) {
238 if (ssl_debug_flag && !inserver) {
239 printf("ssl:server_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
240 depth,ok,error,X509_verify_cert_error_string(error));
243 /* first thing is to have a meaningful name for the current
244 * certificate that is being verified ... and if we cannot
245 * determine that then something is seriously wrong!
248 (char *)X509_NAME_oneline(X509_get_subject_name(xs),NULL,0));
250 (char *)X509_NAME_oneline(X509_get_issuer_name(xs),NULL,0));
251 if (!subject || !subject[0] || !issuer || !issuer[0]) {
256 if (verbosity && !inserver && depth != ssl_verify_depth) {
257 printf("[%d] Certificate Subject:\r\n%s\r\n",depth,subject);
258 printf("[%d] Certificate Issuer:\r\n%s\r\n",depth,issuer);
259 ssl_verify_depth = depth;
262 /* make sure that the certificate that has been presented */
263 /* has not been revoked (if we have been given a CRL. */
264 ok = ssl_verify_crl(ok, ctx);
266 /* if we have any form of error in secure mode we reject the connection */
267 if (error!=X509_V_OK) {
270 if (ckxsyslog >= SYSLG_LI && ckxlogging) {
271 cksyslog(SYSLG_LI, 0,
272 "X.509 Certificate verify failure",
274 (char *)X509_verify_cert_error_string(error)
277 #endif /* CKSYSLOG */
280 if ( ssl_verify_flag &
281 (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
286 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
287 printf("Certificate is self signed.\r\n");
289 case X509_V_ERR_CERT_HAS_EXPIRED:
290 printf("Certificate has expired.\r\n");
292 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
294 "Certificate issuer's certificate isn't available locally.\r\n");
296 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
297 printf("Unable to verify leaf signature.\r\n");
299 case X509_V_ERR_CERT_REVOKED:
300 printf("Certificate revoked.\r\n");
303 printf("Error %d while verifying certificate.\r\n",
308 ok = !(ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
310 /* if we got all the way to the top of the tree then
311 * we *can* use this certificate for a username to
312 * match ... in all other cases we must not!
314 auth_ssl_name = saved_subject;
315 saved_subject = NULL;
320 /* save the name if at least the first level is okay */
321 if (depth == 0 && ok)
322 makestr(&saved_subject,subject);
324 /* clean up things */
335 ssl_client_verify_callback(int ok, X509_STORE_CTX * ctx)
337 ssl_client_verify_callback(ok, ctx)
342 char subject[256]="", issuer[256]="";
343 int depth, error, len;
348 xs=X509_STORE_CTX_get_current_cert(ctx);
349 error=X509_STORE_CTX_get_error(ctx);
350 depth=X509_STORE_CTX_get_error_depth(ctx);
352 if ( ssl_debug_flag )
353 printf("ssl:client_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
354 depth,ok,error,X509_verify_cert_error_string(error));
356 if ( ssl_certsok_flag ) {
360 /* first thing is to have a meaningful name for the current
361 * certificate that is being verified ... and if we cannot
362 * determine that then something is seriously wrong!
364 #ifdef XN_FLAG_SEP_MULTILINE
365 X509_NAME_print_ex(bio_err,X509_get_subject_name(xs),4,
366 XN_FLAG_SEP_MULTILINE);
367 len = BIO_read(bio_err,subject,256);
368 subject[len < 256 ? len : 255] = '\0';
370 ERR_print_errors(bio_err);
371 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
372 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
373 uq_ok("X.509 Subject Name unavailable", ssl_err, 1, NULL, 0);
378 X509_NAME_print_ex(bio_err,X509_get_issuer_name(xs),4,
379 XN_FLAG_SEP_MULTILINE);
380 len = BIO_read(bio_err,issuer,256);
381 issuer[len < 256 ? len : 255] = '\0';
383 ERR_print_errors(bio_err);
384 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
385 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
386 uq_ok("X.509 Issuer Name unavailable", ssl_err, 1, NULL, 0);
390 #else /* XN_FLAG_SEP_MULTILINE */
391 X509_NAME_oneline(X509_get_subject_name(xs),subject,256);
394 ERR_print_errors(bio_err);
395 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
396 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
397 uq_ok("X.509 Subject Name unavailable", ssl_err, 1, NULL, 0);
402 X509_NAME_oneline(X509_get_issuer_name(xs),issuer,256);
405 ERR_print_errors(bio_err);
406 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
407 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
408 uq_ok("X.509 Issuer Name unavailable", ssl_err, 1, NULL, 0);
412 #endif /* XN_FLAG_SEP_MULTILINE */
414 if (verbosity && depth != ssl_verify_depth) {
415 printf("[%d] Certificate Subject:\r\n%s\r\n",depth,subject);
416 printf("[%d] Certificate Issuer:\r\n%s\r\n",depth,issuer);
417 ssl_verify_depth = depth;
420 ok = ssl_verify_crl(ok, ctx);
424 /* if the server is using a self signed certificate then
425 * we need to decide if that is good enough for us to
430 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: {
431 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
432 /* make 100% sure that in secure more we drop the
433 * connection if the server does not have a
436 ckmakxmsg(prefix,1024,
437 "Error: Server has a self-signed certificate\n",
438 "[",ckitoa(depth),"] Certificate Subject=\n",subject,
439 "\n[",ckitoa(depth),"] Certificate Issuer=\n",issuer,
442 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
444 /* sometimes it is really handy to be able to debug things
445 * and still get a connection!
447 if (ssl_debug_flag) {
448 printf("SSL: debug -> ignoring cert required!\r\n");
454 } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
455 ckmakxmsg(prefix,1024,
456 "Warning: Server has a self-signed certificate\n",
457 "[",ckitoa(depth),"] Certificate Subject=\n",subject,
458 "\n[",ckitoa(depth),"] Certificate Issuer=\n",issuer,
469 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
470 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
471 /* make 100% sure that in secure more we drop the
472 * connection if the server does not have a
475 ckmakxmsg(prefix,1024,
477 (char *)X509_verify_cert_error_string(error),
478 "\nCertificate Issuer=\n",issuer,
479 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
480 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
482 /* sometimes it is really handy to be able to debug things
483 * and still get a connection!
485 if (ssl_debug_flag) {
486 printf("SSL: debug -> ignoring cert required!\r\n");
492 } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
493 ckmakxmsg(prefix,1024,
495 (char *)X509_verify_cert_error_string(error),
496 "\nCertificate Issuer=\n",issuer,
497 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
498 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
502 case X509_V_ERR_CERT_NOT_YET_VALID:
503 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
504 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
506 /* make 100% sure that in secure more we drop the
507 * connection if the server does not have a
510 ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
511 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
512 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
513 ckmakxmsg(prefix,1024,
515 (char *)X509_verify_cert_error_string(error),
516 "\nCertificate Subject=\n",subject,
517 "\nnotBefore=",ssl_err,
518 NULL,NULL,NULL,NULL,NULL,NULL);
519 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
520 /* sometimes it is really handy to be able to debug things
521 * and still get a connection!
523 if (ssl_debug_flag) {
524 printf("SSL: debug -> ignoring cert required!\r\n");
530 } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
532 ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
533 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
534 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
535 ckmakxmsg(prefix,1024,
537 (char *)X509_verify_cert_error_string(error),
538 "\nCertificate Subject=\n",subject,
539 "\n notBefore=",ssl_err,
540 NULL,NULL,NULL,NULL,NULL,NULL);
541 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
544 case X509_V_ERR_CERT_HAS_EXPIRED:
545 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
546 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
548 /* make 100% sure that in secure more we drop the
549 * connection if the server does not have a
552 ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
553 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
554 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
556 ckmakxmsg(prefix,1024,
558 (char *)X509_verify_cert_error_string(error),
559 "\nCertificate Subject=\n",subject,
560 "\n notAfter=",ssl_err,
561 NULL,NULL,NULL,NULL,NULL,NULL);
562 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
564 /* sometimes it is really handy to be able to debug things
565 * and still get a connection!
567 if (ssl_debug_flag) {
568 printf("SSL: debug -> ignoring cert required!\r\n");
574 } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
576 ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
577 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
578 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
579 ckmakxmsg(prefix,1024,
581 (char *)X509_verify_cert_error_string(error),
582 "\nCertificate Subject=\n",subject,
583 "\n notAfter=",ssl_err,
584 NULL,NULL,NULL,NULL,NULL,NULL);
585 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
588 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
589 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
591 * When an SSL server sends its certificates to the client there
592 * are two" conventions": one is to send the complete certificate
593 * chain and the other is to send the whole chain apart from the
596 * You don't usually need the root because the root is normally
597 * stored and trusted locally.
599 * So if you get the whole chain it will complain about the self
600 * signed certificate whereas if the root is missing it says it
601 * can't find the issuer certificate.
603 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
604 /* make 100% sure that in secure more we drop the
605 * connection if the server does not have a
608 ckmakxmsg(prefix,1024,
610 (char *)X509_verify_cert_error_string(error),
611 "\nCertificate Issuer=\n",issuer,
612 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
613 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
614 /* sometimes it is really handy to be able to debug things
615 * and still get a connection!
617 if (ssl_debug_flag) {
618 printf("SSL: debug -> ignoring cert required!\r\n");
624 } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
625 ckmakxmsg(prefix,1024,
627 (char *)X509_verify_cert_error_string(error),
628 "\nCertificate Issuer=\n",issuer,
629 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
630 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
633 /* if the user decides to accept the certificate
634 * offer to store it for future connections in
635 * the user's private store
638 "Do you wish to store the certificate to verify future connections?",
639 "Continue (Y/N)", 3, NULL, 0);
641 ck_X509_save_cert_to_user_store(xs);
646 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
647 case X509_V_ERR_UNABLE_TO_GET_CRL:
648 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
649 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
650 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
651 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
652 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
653 case X509_V_ERR_CRL_NOT_YET_VALID:
654 case X509_V_ERR_CRL_HAS_EXPIRED:
655 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
656 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
657 case X509_V_ERR_OUT_OF_MEM:
658 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
659 case X509_V_ERR_CERT_REVOKED:
660 case X509_V_ERR_APPLICATION_VERIFICATION:
662 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
663 /* make 100% sure that in secure mode we drop the
664 * connection if the server does not have a
667 ckmakxmsg(prefix,1024,
669 (char *)X509_verify_cert_error_string(error),
670 "\nCertificate Subject=\n",subject,
671 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
672 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
674 /* sometimes it is really handy to be able to debug things
675 * and still get a connection!
677 if (ssl_debug_flag) {
678 printf("SSL: debug -> ignoring cert required!\r\n");
684 } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
685 ckmakxmsg(prefix,1024,
687 (char *)X509_verify_cert_error_string(error),
688 "\nCertificate Subject=\n",subject,
689 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
690 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
697 if ( ssl_debug_flag )
698 printf("ssl:client_verify_callback => ok: %d\r\n",ok);
704 ssl_client_info_callback(const SSL *s, int where, int ret)
706 ssl_client_info_callback(s,where,ret)
710 #endif /* CK_ANSIC */
712 if (inserver || !ssl_debug_flag)
718 case SSL_CB_CONNECT_LOOP:
719 printf("SSL_connect:%s %s\r\n",
720 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
722 case SSL_CB_CONNECT_EXIT:
724 printf("SSL_connect:failed in %s %s\r\n",
725 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
726 } else if (ret < 0) {
727 printf("SSL_connect:error in %s %s\r\n",
728 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
731 case SSL_CB_ACCEPT_LOOP:
732 printf("SSL_accept:%s %s\r\n",
733 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
735 case SSL_CB_ACCEPT_EXIT:
737 printf("SSL_accept:failed in %s %s\r\n",
738 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
739 } else if (ret < 0) {
740 printf("SSL_accept:error in %s %s\r\n",
741 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
744 case SSL_CB_READ_ALERT:
745 printf("SSL_read_alert\r\n");
747 case SSL_CB_WRITE_ALERT:
748 printf("SSL_write_alert\r\n");
750 case SSL_CB_HANDSHAKE_START:
751 printf("SSL_handshake:%s %s\r\n",
752 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
754 case SSL_CB_HANDSHAKE_DONE:
755 printf("SSL_handshake:%s %s\r\n",
756 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
762 /* Return 1, client cert is available */
763 /* Return 0, no client cert is available */
764 /* Return -1, callback must be called again. SSL_want_x509_lookup() == 1 */
767 ssl_client_cert_callback(SSL * s, X509 ** x509, EVP_PKEY ** pkey)
769 ssl_client_cert_callback(s, x509, pkey)
773 #endif /* CK_ANSIC */
777 if ( ssl_debug_flag ) {
778 const char * cipher_list=SSL_get_cipher(s);
779 printf("ssl_client_cert_callback called (%s)\r\n",
780 cipher_list?cipher_list:"UNKNOWN");
783 if ( s == tls_con ) {
784 if (tls_load_certs(tls_cts,tls_con,0)) {
785 *x509 = SSL_get_certificate(s);
786 *pkey = SSL_get_privatekey(s);
789 } else if ( s == ssl_con ) {
790 if (tls_load_certs(ssl_ctx,ssl_con,0)) {
791 *x509 = SSL_get_certificate(s);
792 *pkey = SSL_get_privatekey(s);
801 #endif /* USE_CERT_CB */
805 #endif /* MS_CALLBACK */
807 static RSA MS_CALLBACK *
809 tmp_rsa_cb(SSL * s, int export, int keylength)
811 tmp_rsa_cb(s,export,keylength)
815 #endif /* CK_ANSIC */
817 static RSA *rsa_tmp=NULL;
823 printf("Generating temporary (%d bit) RSA key...\r\n",keylength);
825 rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
832 printf("Unable to generate temporary RSA key...\r\n");
839 static unsigned char dh512_p[]={
840 0xE9,0x4E,0x3A,0x64,0xFA,0x65,0x5F,0xA6,0x44,0xC7,0xFC,0xF1,
841 0x16,0x8B,0x11,0x11,0x7A,0xF0,0xB2,0x49,0x80,0x56,0xA3,0xF8,
842 0x0F,0x7D,0x01,0x68,0x5D,0xF6,0x8A,0xEA,0x8C,0xDD,0x01,0xDC,
843 0x43,0x18,0xE0,0xC4,0x89,0x80,0xE6,0x2D,0x44,0x77,0x45,0xFD,
844 0xBA,0xFC,0x43,0x35,0x12,0xC0,0xED,0x32,0xD3,0x16,0xEF,0x51,
847 static unsigned char dh512_g[]={
851 static unsigned char dh768_p[]={
852 0x8B,0x2A,0x8C,0x6C,0x0F,0x87,0xC7,0x34,0xEE,0x2E,0xFB,0x60,
853 0x94,0xB3,0xBF,0x95,0xBA,0x84,0x74,0x86,0xEA,0xE0,0xA4,0x33,
854 0xE0,0x8F,0x7C,0x79,0x5C,0x62,0xE2,0x91,0xC5,0x6D,0x68,0xB9,
855 0x6C,0x5E,0x4E,0x94,0x0C,0x8E,0x56,0x8E,0xEB,0x98,0x7C,0x6E,
856 0x0E,0xF2,0xD5,0xAA,0x22,0x27,0x3F,0x0F,0xAF,0x10,0xB5,0x0B,
857 0x16,0xCC,0x05,0x27,0xBB,0x58,0x6D,0x61,0x4B,0x2B,0xAB,0xDC,
858 0x6A,0x15,0xBC,0x36,0x75,0x4D,0xEC,0xAB,0xFA,0xB6,0xE1,0xB1,
859 0x13,0x70,0xD8,0x77,0xCD,0x5E,0x51,0x77,0x81,0x0D,0x77,0x43,
861 static unsigned char dh768_g[]={
865 static unsigned char dh1024_p[]={
866 0xA4,0x75,0xCF,0x35,0x00,0xAF,0x3C,0x17,0xCE,0xB0,0xD0,0x52,
867 0x43,0xA0,0x0E,0xFA,0xA2,0xC9,0xBE,0x0B,0x76,0x7A,0xD9,0x2E,
868 0xF4,0x97,0xAC,0x02,0x24,0x69,0xF6,0x36,0x4F,0xAB,0xCC,0x43,
869 0xC1,0x74,0xFF,0xA3,0xD4,0x04,0x0F,0x11,0x2B,0x6D,0x8C,0x47,
870 0xC9,0xCF,0x40,0x93,0x9B,0x7D,0x1E,0x52,0x85,0xB2,0x17,0x55,
871 0x9C,0xF2,0x41,0x02,0x2A,0x9D,0x5F,0x24,0x22,0xC6,0x04,0xC4,
872 0xAB,0x92,0x6D,0xC7,0xC8,0xF3,0x41,0x58,0x6C,0x86,0xFD,0xB8,
873 0x0F,0x2D,0xDD,0xBF,0xA8,0x40,0x0C,0x58,0xC8,0xF2,0x3F,0x18,
874 0xEF,0xF1,0x93,0x3E,0xBA,0x16,0x41,0xBE,0x32,0x6C,0xC5,0x63,
875 0xFF,0x8A,0x02,0x3D,0xAC,0xD5,0x5A,0x49,0x64,0x34,0x14,0x2E,
876 0xFB,0x2E,0xE7,0x39,0x1A,0x0F,0x3C,0x33,
878 static unsigned char dh1024_g[]={
882 static unsigned char dh1536_p[]={
883 0xA3,0x2B,0x75,0x0E,0x7B,0x31,0x82,0xCA,0xF2,0xFC,0xF3,0x3D,
884 0xCE,0x5F,0xCD,0x5B,0x95,0xF6,0x2F,0xA4,0x5D,0x08,0x26,0xD2,
885 0x5F,0xC0,0x3F,0xC5,0xD8,0xA2,0xFE,0x83,0x26,0xBC,0xEB,0x7D,
886 0xF0,0x4E,0xD2,0xA6,0xBB,0x3C,0x88,0x63,0xCE,0x98,0xDE,0x08,
887 0xE2,0xE1,0xAF,0xE2,0x38,0xA8,0xFA,0x68,0x76,0x8D,0xBF,0xDF,
888 0xBB,0x30,0x15,0xFE,0xBD,0x22,0xCC,0x03,0x4E,0x5E,0x33,0xA3,
889 0x6D,0xD6,0x68,0x12,0x97,0x17,0x4B,0xB5,0x84,0x5F,0x5F,0xA3,
890 0x5C,0x2F,0xA4,0x10,0xC1,0xAD,0xBF,0xAC,0x30,0xCA,0x47,0x64,
891 0x63,0xFE,0xEE,0xEE,0xA1,0x64,0x73,0x70,0xAA,0xF9,0xFE,0xC6,
892 0xAD,0x5E,0xF6,0xF3,0x9C,0xDF,0x34,0x53,0x34,0x72,0xA6,0xA4,
893 0xBB,0x81,0x5A,0x43,0x41,0xFD,0x41,0x05,0x5B,0x77,0x7B,0x84,
894 0x03,0xFA,0x8A,0xFA,0xF7,0x8E,0x0F,0xCB,0x51,0xA2,0xB8,0x45,
895 0xFF,0x59,0x42,0xEF,0xCF,0xF6,0x25,0x37,0xE2,0x6D,0xFF,0x69,
896 0x11,0xF5,0x77,0x59,0x79,0x1C,0x5F,0x05,0xFC,0x7A,0x65,0x81,
897 0x03,0x4A,0x78,0xC6,0xE9,0x48,0x73,0xF6,0x10,0xBC,0x99,0x1C,
898 0xEE,0x44,0x2F,0x8B,0x70,0xCA,0xA8,0xB6,0x02,0x83,0x3E,0x0B,
900 static unsigned char dh1536_g[]={
904 static unsigned char dh2048_p[]={
905 0xFA,0x4E,0xE4,0x3B,0xFA,0xC1,0x87,0xDD,0xE7,0xC6,0x8B,0xE6,
906 0x13,0x85,0xBC,0x9B,0x2B,0x8B,0x5B,0x46,0xBB,0x8B,0x86,0x6D,
907 0xD7,0xB6,0xD5,0x49,0xC5,0x54,0xF2,0x3E,0xD2,0x39,0x64,0x9B,
908 0x0E,0x33,0x39,0x8F,0xFA,0xFA,0xD9,0x78,0xED,0x34,0x82,0x29,
909 0x37,0x58,0x4D,0x5D,0x40,0xCB,0x69,0xE3,0x8A,0x9F,0x17,0x0C,
910 0x01,0x23,0x6B,0x05,0x01,0xAF,0x33,0xDE,0xDF,0x1A,0xBB,0x7B,
911 0x6A,0x9F,0xD8,0xED,0x8D,0x5E,0x44,0x19,0x5B,0xE0,0xB6,0x23,
912 0xF9,0x7A,0x96,0x6E,0x94,0x33,0x31,0x49,0xBA,0x84,0xD5,0x12,
913 0xD7,0x6D,0xDC,0x35,0x54,0x64,0xA3,0xD8,0x04,0x26,0xC5,0xAF,
914 0x7F,0xE3,0xFE,0x6F,0xBE,0xD5,0x17,0x72,0x4B,0xA6,0xD0,0xA7,
915 0x5F,0x18,0xF5,0xF0,0x2D,0x11,0x9A,0xF6,0xD5,0x3B,0x6C,0x61,
916 0x3C,0x6F,0x8E,0x09,0x4F,0x2C,0xE1,0x26,0x06,0x51,0xB3,0x19,
917 0x85,0x85,0x13,0xF9,0xC2,0x6E,0x80,0x28,0x9E,0x8A,0xA0,0x01,
918 0x46,0xD1,0x85,0x44,0x8C,0xE6,0xEE,0x7E,0x1E,0x17,0x3D,0xBA,
919 0x54,0xFF,0xE8,0x0E,0xDD,0x51,0xF3,0x74,0x7F,0x0D,0x0B,0xAB,
920 0xCA,0x84,0x8D,0x24,0x5D,0x56,0xD4,0x47,0x02,0xFC,0x93,0x9F,
921 0xAE,0x9B,0x5C,0xDB,0x63,0xEB,0x65,0x01,0x38,0xC2,0x7B,0x30,
922 0x1E,0x17,0x1C,0x75,0xF5,0x16,0x3B,0x4F,0x5F,0x41,0x32,0xB5,
923 0xFF,0x9E,0x61,0xFD,0xD2,0x62,0x6E,0xFD,0x8A,0x28,0x93,0x59,
924 0x2D,0x70,0x14,0x4D,0xE1,0x86,0xD5,0x90,0xB4,0xDF,0x72,0x71,
925 0xE0,0xB4,0xD0,0xD6,0x82,0x3A,0x4A,0x04,0x58,0x32,0x0B,0xD3,
928 static unsigned char dh2048_g[]={
937 if ((dh=DH_new()) == NULL)
939 dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
940 dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
941 if ((dh->p == NULL) || (dh->g == NULL))
951 if ((dh=DH_new()) == NULL)
953 dh->p=BN_bin2bn(dh768_p,sizeof(dh768_p),NULL);
954 dh->g=BN_bin2bn(dh768_g,sizeof(dh768_g),NULL);
955 if ((dh->p == NULL) || (dh->g == NULL))
965 if ((dh=DH_new()) == NULL)
967 dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
968 dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
969 if ((dh->p == NULL) || (dh->g == NULL))
979 if ((dh=DH_new()) == NULL)
981 dh->p=BN_bin2bn(dh1536_p,sizeof(dh1536_p),NULL);
982 dh->g=BN_bin2bn(dh1536_g,sizeof(dh1536_g),NULL);
983 if ((dh->p == NULL) || (dh->g == NULL))
993 if ((dh=DH_new()) == NULL)
995 dh->p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);
996 dh->g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
997 if ((dh->p == NULL) || (dh->g == NULL))
1003 static DH MS_CALLBACK *
1005 tmp_dh_cb(SSL * s, int export, int keylength)
1006 #else /* CK_ANSIC */
1007 tmp_dh_cb(s,export,keylength)
1011 #endif /* CK_ANSIC */
1013 static DH *dh_tmp=NULL;
1019 if (ssl_dh_param_file &&
1020 (bio=BIO_new_file(ssl_dh_param_file,"r")) != NULL)
1021 dh_tmp=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
1025 if ( dh_tmp == NULL ) {
1026 if ( keylength < 768 )
1027 dh_tmp = get_dh512();
1028 else if ( keylength < 1024 )
1029 dh_tmp = get_dh768();
1030 else if ( keylength < 1536 )
1031 dh_tmp = get_dh1024();
1032 else if ( keylength < 2048 )
1033 dh_tmp = get_dh1536();
1035 dh_tmp = get_dh2048();
1040 printf("DH not supported...\r\n");
1046 ssl_display_comp(SSL * ssl)
1048 if ( quiet ) /* fdc - Mon Nov 28 11:44:15 2005 */
1051 if ( !ck_ssleay_is_installed() )
1057 if (ssl->expand == NULL || ssl->expand->meth == NULL)
1058 printf("Compression: None\r\n");
1060 printf("Compression: %s\r\n",ssl->expand->meth->name);
1066 ssl_display_connect_details(SSL * ssl_con, int server, int verbose)
1067 #else /* CK_ANSIC */
1068 ssl_display_connect_details(ssl_con,server,verbose)
1072 #endif /* CK_ANSIC */
1075 SSL_CIPHER * cipher;
1076 const char *cipher_list;
1079 if ( quiet ) /* fdc - Mon Nov 28 11:44:15 2005 */
1082 if ( !ck_ssleay_is_installed() )
1085 if ( inserver && !tn_deb )
1088 /* the cipher list *can* be NULL ... useless but it happens! */
1089 cipher = SSL_get_current_cipher(ssl_con);
1090 cipher_list = SSL_CIPHER_get_name(cipher);
1091 SSL_CIPHER_description(cipher,buf,sizeof(buf));
1092 if (cipher_list==NULL)
1093 cipher_list="<NULL>";
1094 printf("[TLS - %s",buf);
1095 ssl_display_comp(ssl_con);
1098 cipher_list=SSL_get_shared_ciphers(ssl_con,buf,512);
1099 if (cipher_list==NULL)
1100 cipher_list="<NULL>";
1101 printf("[TLS - shared ciphers=%s]\r\n",
1104 if ( server || tn_deb ) {
1105 peer=SSL_get_peer_certificate(ssl_con);
1107 X509_NAME_oneline(X509_get_subject_name(peer),buf,512);
1108 printf("[TLS - subject=%s]\r\n",buf);
1109 X509_NAME_oneline(X509_get_issuer_name(peer),buf,512);
1110 printf("[TLS - issuer=%s]\r\n",buf);
1111 /* X509_free(peer); */
1112 } else if (!tls_is_krb5(0)) {
1113 if ( !sstelnet && !tcp_incoming ) {
1114 printf("[TLS - No certificate provided.]\r\n");
1116 "[TLS - The identity of the host could not be verified.]\r\n");
1124 * Use SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *, void * userdata)
1125 * to set the value of the userdata. We are going to use it to store the
1131 ssl_passwd_callback(char *buf, int len, int rwflag, VOID * userdata)
1132 #else /* CK_ANSIC */
1133 ssl_passwd_callback(buf,len,rwflag,userdata)
1134 char * buf; int len; int rwflag; VOID *userdata;
1135 #endif /* CK_ANSIC */
1137 extern char pwbuf[];
1138 extern int pwflg, pwcrypt;
1142 if ( pwbuf[0] && pwflg ) {
1144 n = ckstrncpy(buf,pwbuf,len);
1147 ck_encrypt((char *)buf);
1152 if ( userdata == NULL )
1153 prompt="Enter certificate passphrase: ";
1155 prompt=(char*)userdata;
1156 ok = uq_txt(NULL,prompt,2,NULL,buf,len,NULL,DEFAULT_UQ_TIMEOUT);
1157 return(ok > 0 ? strlen(buf) : 0);
1161 /* Attempts to load certificate data into the TLS context structures */
1162 /* Returns 1 on success; 0 on failure */
1164 tls_load_certs(SSL_CTX * ctx, SSL * con, int server)
1168 if ( !ck_ssleay_is_installed() )
1171 debug(F110,"tls_load_certs","SSL_CTX",0);
1172 debug(F110,"tls_load_certs","SSL",0);
1173 debug(F110,"tls_load_certs","server",0);
1176 if (ssl_rsa_cert_file) {
1177 if ( ssl_debug_flag )
1178 printf("Loading RSA certificate into SSL\r\n");
1180 rc = SSL_use_certificate_file(con, ssl_rsa_cert_file,
1184 if ( !quiet || ssl_debug_flag )
1185 printf("Error loading certificate from %s\r\n",
1188 if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
1189 makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
1191 rc = SSL_use_PrivateKey_file(con, ssl_rsa_key_file,
1194 rc = SSL_use_PrivateKey_file(con, ssl_rsa_cert_file,
1198 if ( !quiet || ssl_debug_flag )
1199 printf("Error loading key from %s\r\n",
1202 rc = SSL_check_private_key(con);
1205 if ( ssl_debug_flag )
1207 "Private key does not match the certificate public key\r\n");
1213 if (ssl_dsa_cert_file) {
1214 if ( ssl_debug_flag )
1215 printf("Loading DSA certificate into SSL\r\n");
1217 rc = SSL_use_certificate_file(con, ssl_dsa_cert_file,
1221 if ( ssl_debug_flag ) {
1222 printf("Error loading certificate from %s\r\n",
1226 if (!ssl_dh_key_file || !ssl_dh_key_file[0])
1227 makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
1228 rc = SSL_use_PrivateKey_file(con, ssl_dh_key_file,
1231 rc = SSL_use_PrivateKey_file(con, ssl_dsa_cert_file,
1235 if ( !quiet || ssl_debug_flag ) {
1236 printf("Error loading key from %s\r\n",
1240 rc = SSL_check_private_key(con);
1243 if ( ssl_debug_flag )
1245 "Private key does not match the certificate public key\n");
1251 if (ssl_rsa_cert_file) {
1252 if ( ssl_debug_flag )
1253 printf("Loading RSA certificate into SSL\r\n");
1255 rc = SSL_CTX_use_certificate_file(ctx, ssl_rsa_cert_file,
1259 if ( !quiet || ssl_debug_flag )
1260 printf("Error loading certificate from %s\r\n",
1263 if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
1264 makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
1266 rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_key_file,
1269 rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_cert_file,
1272 if ( ssl_debug_flag )
1273 printf("Error loading key from %s\r\n",ssl_rsa_key_file);
1275 rc = SSL_CTX_check_private_key(ctx);
1277 if ( ssl_debug_flag )
1279 "Private key does not match the certificate public key\r\n");
1284 if (ssl_dsa_cert_file) {
1285 if ( ssl_debug_flag )
1286 printf("Loading DSA certificate into SSL\r\n");
1288 rc = SSL_CTX_use_certificate_file(ctx, ssl_dsa_cert_file,
1291 if ( ssl_debug_flag ) {
1292 printf("Error loading certificate from %s\r\n",
1296 if (!ssl_dh_key_file || !ssl_dh_key_file[0])
1297 makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
1298 rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dh_key_file,
1301 rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dsa_cert_file,
1304 if ( ssl_debug_flag )
1305 printf("Error loading key from %s\r\n",ssl_dh_key_file);
1307 rc = SSL_CTX_check_private_key(ctx);
1309 if ( ssl_debug_flag )
1311 "Private key does not match the certificate public key\n");
1318 if (ssl_rsa_cert_chain_file && server) {
1321 printf("Loading RSA Certificate Chain into SSL\r\n");
1322 if (!ckstrcmp(ssl_rsa_cert_chain_file,ssl_rsa_cert_file,-1,
1330 rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_rsa_cert_chain_file);
1331 if (!rc && ssl_debug_flag)
1332 printf("Error loading RSA Certificate Chain into SSL\r\n");
1334 if (ssl_dsa_cert_chain_file && server) {
1337 printf("Loading DSA Certificate Chain into SSL\r\n");
1338 if (!ckstrcmp(ssl_dsa_cert_chain_file,ssl_dsa_cert_file,-1,
1346 rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_dsa_cert_chain_file);
1347 if (!rc && ssl_debug_flag)
1348 printf("Error loading DSA Certificate Chain into SSL\r\n");
1358 #endif /* CK_ANSIC */
1363 if ( !ck_ssleay_is_installed() )
1366 Pre-OpenSSL 1.0.0 comment:
1367 OpenSSL does not provide for ABI compatibility between releases prior
1368 to version 1.0.0. If the version does not match, it is not safe to
1369 assume that any function you call takes the same parameters or does
1370 the same thing with them. Removing this test prior to the OpenSSL 1.0.0
1371 release will result in an increase in unexplained or incorrect behaviors.
1372 The test should be revised once OpenSSL 1.0.0 is released and we see what
1373 its claims are as to ABI compatibility.
1376 Post-OpenSSL 1.0.0 comment:
1377 OpenSSL does not provide for ABI compatibility between releases prior
1378 to version 1.0.0. After 1.0, the following holds:
1380 Changes to last letter: security and bugfix only, no new features.
1382 Changes to last number: new ABI compatible features.
1384 Changes to middle number: major release, ABI compatibility not guaranteed.
1387 (per Dr. Stephen Henson)
1389 debug(F111,"Kermit built for OpenSSL",OPENSSL_VERSION_TEXT,SSLEAY_VERSION_NUMBER);
1391 debug(F111,"OpenSSL Library",SSLeay_version(SSLEAY_VERSION),
1393 debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_BUILT_ON),0);
1394 debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_CFLAGS),0);
1395 debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_PLATFORM),0);
1397 /* The following test is suggested by Richard Levitte */
1398 /* if (((OPENSSL_VERSION_NUMBER ^ SSLeay()) & 0xffffff0f) */
1399 /* Modified by Adam Friedlander for OpenSSL >= 1.0.0 */
1400 if (OPENSSL_VERSION_NUMBER > SSLeay()
1401 || ((OPENSSL_VERSION_NUMBER ^ SSLeay()) & COMPAT_VERSION_MASK)
1403 || ckstrcmp(OPENSSL_VERSION_TEXT,(char *)SSLeay_version(SSLEAY_VERSION),-1,1)
1407 debug(F111,"OpenSSL Version does not match. Built with",
1408 SSLeay_version(SSLEAY_VERSION),SSLEAY_VERSION_NUMBER);
1409 printf("?OpenSSL libraries do not match required version:\r\n");
1410 printf(" . C-Kermit built with %s\r\n",OPENSSL_VERSION_TEXT);
1411 printf(" . Version found %s\r\n",SSLeay_version(SSLEAY_VERSION));
1413 printf(" OpenSSL versions 1.0.0 or newer must be the same\r\n");
1414 printf(" major and minor version number, and Kermit may not\r\n");
1415 printf(" be used with a version of OpenSSL older than the one\r\n");
1416 printf(" supplied at compile time.\r\n");
1418 printf(" OpenSSL versions prior to 1.0.0 must be the same.\r\n");
1419 #endif /* OPENSSL_100 */
1423 printf(" Set CD_LIBRARY_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
1425 #endif /* SOLARIS */
1428 printf(" Set SHLIB_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
1433 printf(" Set LIBPATH for %s.\r\n",OPENSSL_VERSION_TEXT);
1438 printf(" Set LD_LIBRARY_PATH for %s.\r\n",OPENSSL_VERSION_TEXT);
1442 printf(" %sebuild C-Kermit from source on this computer to make \
1443 versions agree.\r\n",s);
1450 if (!*s) s = "(unknown)";
1451 printf(" C-Kermit makefile target: %s\r\n",s);
1453 #endif /* KTARGET */
1454 printf(" Or if that is what you did then try to find out why\r\n");
1455 printf(" the program loader (image activator) is choosing a\r\n");
1456 printf(" different OpenSSL library than the one specified in \
1457 the build.\r\n\r\n");
1458 printf(" All SSL/TLS features disabled.\r\n\r\n");
1462 ck_crypto_unloaddll();
1466 #endif /* OS2ONLY */
1468 /* init things so we will get meaningful error messages
1469 * rather than numbers
1471 SSL_load_error_strings();
1474 OPENSSL_add_all_algorithms_noconf();
1476 /* SSL_library_init() only loads those ciphers needs for SSL */
1477 /* These happen to be a similar set to those required for SSH */
1478 /* but they are not a complete set of ciphers provided by the */
1479 /* crypto library. */
1481 #endif /* SSHBUILTIN */
1485 if (cm != NULL && cm->type != NID_undef) {
1486 SSL_COMP_add_compression_method(0xe0, cm); /* EAY's ZLIB ID */
1490 if (cm != NULL && cm->type != NID_undef)
1491 SSL_COMP_add_compression_method(0xe1, cm); /* EAY's RLE ID */
1493 /* Ensure the Random number generator has enough entropy */
1494 if ( !RAND_status() ) {
1495 char buffer[256]="";
1496 char randombytes[256];
1497 int rc1 = -1, rc2 = 1; /* assume failure and success */
1499 debug(F110,"ssl_once_init","!RAND_status()",0);
1501 if ( ssl_rnd_file == NULL ) {
1502 debug(F110,"ssl_rnd_file","ssl_rnd_file is NULL",0);
1503 RAND_file_name(buffer,256);
1505 makestr(&ssl_rnd_file, buffer);
1507 makestr(&ssl_rnd_file,".rnd");
1509 debug(F110,"ssl_rnd_file",ssl_rnd_file,0);
1511 rc1 = RAND_egd(ssl_rnd_file);
1512 debug(F111,"ssl_once_init","RAND_egd()",rc1);
1514 rc2 = RAND_load_file(ssl_rnd_file, -1);
1515 debug(F111,"ssl_once_init","RAND_load_file()",rc1);
1518 if ( rc1 <= 0 && !rc2 )
1520 time_t t = time(NULL);
1521 int tlen = sizeof(time_t);
1523 int plen = sizeof(int);
1526 #define RAND_MAX 0x7FFF
1528 debug(F110,"ssl_once_init","calling RAND_seed()",0);
1530 RAND_seed((unsigned char *)&t, tlen);
1531 RAND_seed((unsigned char *)&pid, plen);
1533 srand((unsigned int)t);
1534 sprintf(buffer, "%.0f", (((double)(rand()%RAND_MAX)/RAND_MAX)*
1535 (sizeof(randombytes)-128-1)));
1536 n = (atoi(buffer)+1)%(sizeof(randombytes)-128-1);
1537 RAND_seed(randombytes, 128);
1540 if ( !RAND_status() ) {
1541 debug(F110,"ssl_once_init","Unable to initialize PRNG",0);
1542 printf(" Unable to load 'random state'\n");
1543 printf(" SSL and TLS are unavailble.\n");
1544 printf(" Use SET AUTH SSL RANDOM-FILE <file> command to provide random data.\n");
1545 printf(" Specified file will be overwritten with new random data after use.\n");
1549 if ( ssl_rnd_file ) {
1550 int rc = RAND_write_file(ssl_rnd_file);
1551 debug(F111,"ssl_once_init","RAND_write_file()",rc);
1556 // Initialize additional OID types for use when saving certs to a file
1557 OBJ_create("2.99999.3","SET.ex3","SET x509v3 extension 3");
1560 /* make sure we have somewhere we can log errors to */
1561 bio_err=BIO_new(BIO_s_mem());
1563 debug(F100,"ssl_once_init() complete","",0);
1568 ssl_tn_init(int mode)
1570 ssl_tn_init(mode) int mode;
1571 #endif /* CK_ANSIC */
1574 extern char * k5_keytab;
1575 extern char * krb5_d_srv;
1577 static int last_ssl_mode = -1;
1578 SSL * ssl_conx=NULL, * tls_conx=NULL;
1580 ssl_initialized = 0;
1582 if ( !ck_ssleay_is_installed() )
1585 debug(F111,"ssl_tn_init","mode",mode);
1588 printf("SSL_DEBUG_FLAG on\r\n");
1590 if (last_ssl_mode != mode) {
1592 SSL_CTX_free(ssl_ctx);
1596 SSL_CTX_free(tls_ctx);
1601 if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
1602 if ( mode == SSL_CLIENT ) {
1603 ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
1604 /* This can fail because we do not have RSA available */
1606 debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
1607 #ifndef DISABLE_SSLV3
1608 ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
1611 debug(F110,"ssl_tn_init","SSLv3_client_method failed",0);
1617 TLS 1.0 is the new default as of 5 Feb 2015.
1618 Previously this was commented out because
1619 "too many web servers still do not support TLSv1".
1620 Now we try TLS 1.0 first, falling back to SSL 2.3
1621 and SSL 3.0 in that order. Maybe there should be
1622 an option not to fall back.
1624 tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_client_method());
1626 debug(F110,"ssl_tn_init","TLSv1_client_method OK",0);
1628 debug(F110,"ssl_tn_init","TLSv1_client_method failed",0);
1629 /* This can fail because we do not have RSA available */
1630 tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
1632 debug(F110,"ssl_tn_init","SSLv23_client_method OK",0);
1634 debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
1635 #ifndef DISABLE_SSLV3
1636 tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
1637 #endif /* DISABLE_SSLV3 */
1639 #ifndef DISABLE_SSLV3
1641 "ssl_tn_init","SSLv3_client_method failed",0);
1642 #endif /* DISABLE_SSLV3 */
1644 "ssl_tn_init","All SSL client methods failed",0);
1651 SSL_CTX_set_client_cert_cb(ssl_ctx,ssl_client_cert_callback);
1652 SSL_CTX_set_client_cert_cb(tls_ctx,ssl_client_cert_callback);
1653 #endif /* USE_CERT_CB */
1654 } else if (mode == SSL_SERVER) {
1655 /* We are a server */
1656 ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
1657 /* This can fail because we do not have RSA available */
1659 debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
1660 #ifndef DISABLE_SSLV3
1661 ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_server_method());
1664 debug(F110,"ssl_tn_init","SSLv3_server_method failed",0);
1670 tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_server_method());
1672 tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
1673 /* This can fail because we do not have RSA available */
1675 debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
1676 tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_server_method());
1678 #endif /* COMMENT */
1680 debug(F110,"ssl_tn_init","TLSv1_server_method failed",0);
1684 } else /* Unknown mode */
1688 SSL_CTX_set_default_passwd_cb(ssl_ctx,
1689 (pem_password_cb *)ssl_passwd_callback);
1690 SSL_CTX_set_default_passwd_cb(tls_ctx,
1691 (pem_password_cb *)ssl_passwd_callback);
1694 /* for SSL switch on all the interoperability and bug
1695 * workarounds so that we will communicate with people
1696 * that cannot read poorly written specs :-)
1697 * for TLS be sure to prevent use of SSLv2
1699 SSL_CTX_set_options(ssl_ctx,SSL_OP_ALL|SSL_OP_NO_SSLv2
1700 #ifdef DISABLE_SSLV3
1704 SSL_CTX_set_options(tls_ctx,
1705 SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
1706 #ifdef DISABLE_SSLV3
1711 SSL_CTX_set_info_callback(ssl_ctx,ssl_client_info_callback);
1712 SSL_CTX_set_info_callback(tls_ctx,ssl_client_info_callback);
1715 /* Set the proper caching mode */
1716 if ( mode == SSL_SERVER ) {
1717 SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_SERVER);
1718 SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_SERVER);
1720 SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_CLIENT);
1721 SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_CLIENT);
1723 SSL_CTX_set_session_id_context(ssl_ctx,(CHAR *)"1",1);
1724 SSL_CTX_set_session_id_context(tls_ctx,(CHAR *)"2",1);
1726 SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_OFF);
1727 SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_OFF);
1728 #endif /* COMMENT */
1731 /* The server uses defaults for the certificate files. */
1732 /* The client does not. */
1733 if (mode == SSL_SERVER) {
1734 char cert_filepath[1024];
1735 const char * defdir = NULL;
1738 defdir = getenv("SSL_CERT_DIR");
1743 defdir = X509_get_default_cert_dir();
1745 debug(F110,"ssl_tn_init - setting default directory to",defdir,0);
1750 if (!ssl_rsa_cert_file) {
1751 /* we need to know the fullpath to the location of the
1752 * certificate that we will be running with as we cannot
1753 * be sure of the cwd when we are launched
1755 sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa.pem");
1756 if (zchki(cert_filepath) > 0)
1757 makestr(&ssl_rsa_cert_file,cert_filepath);
1759 if (ssl_rsa_cert_file && !ssl_rsa_key_file) {
1760 /* we need to know the fullpath to the location of the
1761 * certificate that we will be running with as we cannot
1762 * be sure of the cwd when we are launched
1764 sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa-key.pem");
1765 if (zchki(cert_filepath) > 0)
1766 makestr(&ssl_rsa_key_file,cert_filepath);
1768 if (!ssl_dsa_cert_file) {
1769 /* we need to know the fullpath to the location of the
1770 * certificate that we will be running with as we cannot
1771 * be sure of the cwd when we are launched
1773 sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa.pem");
1774 if (zchki(cert_filepath) > 0)
1775 makestr(&ssl_dsa_cert_file,cert_filepath);
1777 if (ssl_dsa_cert_file && !ssl_dh_key_file) {
1778 /* we need to know the fullpath to the location of the
1779 * certificate that we will be running with as we cannot
1780 * be sure of the cwd when we are launched
1782 sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa-key.pem");
1783 if (zchki(cert_filepath) > 0)
1784 makestr(&ssl_dh_key_file,cert_filepath);
1787 /* we need to know the fullpath to the location of the
1788 * certificate that we will be running with as we cannot
1789 * be sure of the cwd when we are launched
1791 sprintf(cert_filepath,"%s/crl",defdir);
1792 if (zchki(cert_filepath) > 0)
1793 makestr(&ssl_crl_dir,cert_filepath);
1796 if (ssl_only_flag && !tls_load_certs(ssl_ctx,ssl_con,1)) {
1797 debug(F110,"ssl_tn_init","Unable to load SSL certs",0);
1801 if (tls_only_flag && !tls_load_certs(tls_ctx,tls_con,1)) {
1802 debug(F110,"ssl_tn_init","Unable to load TLS certs",0);
1807 if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
1808 /* we may require a temp 512 bit RSA key because of the
1809 * wonderful way export things work ... if so we generate
1813 SSL_CTX_set_tmp_rsa_callback(ssl_ctx, tmp_rsa_cb);
1814 SSL_CTX_set_tmp_dh_callback( ssl_ctx, tmp_dh_cb);
1815 SSL_CTX_set_tmp_rsa_callback(tls_ctx, tmp_rsa_cb);
1816 SSL_CTX_set_tmp_dh_callback( tls_ctx, tmp_dh_cb);
1818 dh = tmp_dh_cb(NULL,0,512);
1819 SSL_CTX_set_tmp_dh(ssl_ctx,dh);
1820 SSL_CTX_set_tmp_dh(tls_ctx,dh);
1822 /* The following code is only called if we are using a
1823 * certificate with an RSA public key and where the
1824 * certificate has a key length less than 512 bits or is
1825 * marked for signing only. This is so we can support
1826 * the greatest legal privacy level with exportable clients.
1829 if (SSL_CTX_need_tmp_RSA(ssl_ctx) ||
1830 SSL_CTX_need_tmp_RSA(tls_ctx))
1834 if ( ssl_debug_flag )
1835 printf("Generating temp (512 bit) RSA key ...\r\n");
1836 rsa=RSA_generate_key(512,RSA_F4,NULL,NULL);
1837 if ( ssl_debug_flag )
1838 printf("Generation of temp (512 bit) RSA key done\r\n");
1840 if (SSL_CTX_need_tmp_RSA(ssl_ctx)) {
1841 if (!SSL_CTX_set_tmp_rsa(ssl_ctx,rsa)) {
1842 if ( ssl_debug_flag )
1844 "Failed to assign generated temp RSA key to SSL!\r\n");
1847 if (SSL_CTX_need_tmp_RSA(tls_ctx)) {
1848 if (!SSL_CTX_set_tmp_rsa(tls_ctx,rsa)) {
1849 if ( ssl_debug_flag )
1851 "Failed to assign generated temp RSA key to TLS!\r\n");
1855 if ( ssl_debug_flag )
1856 printf("Assigned temp (512 bit) RSA key\r\n");
1861 /* make sure we will find certificates in the standard
1862 * location ... otherwise we don't look anywhere for
1863 * these things which is going to make client certificate
1864 * exchange rather useless :-)
1865 * In OS2, default values for ssl_verify_file and ssl_verify_path.
1871 /* The defaults in the SSL crypto library are not appropriate for OS/2 */
1872 char path[CKMAXPATH];
1874 ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
1876 SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
1877 debug(F110,"ssl_tn_init certificate verify dir",path,0);
1879 printf(" Certificate Verification Directory: %s\r\n",path);
1880 SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
1882 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
1884 SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
1885 debug(F110,"ssl_tn_init certificate verify dir",path,0);
1887 printf(" Certificate Verification Directory: %s\r\n",path);
1888 SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
1890 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
1892 SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
1893 debug(F110,"ssl_tn_init certificate verify dir",path,0);
1895 printf(" Certificate Verification Directory: %s\r\n",path);
1896 SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
1898 ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
1899 if (zchki(path) > 0 &&
1900 SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
1901 debug(F110,"ssl_tn_init certificate verify file",path,0);
1903 printf(" Certificate Verification File: %s\r\n",path);
1904 SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
1906 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
1907 if (zchki(path) > 0 &&
1908 SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
1909 debug(F110,"ssl_tn_init certificate verify file",path,0);
1911 printf(" Certificate Verification File: %s\r\n",path);
1912 SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
1914 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
1915 if (zchki(path) > 0 &&
1916 SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
1917 debug(F110,"ssl_tn_init certificate verify file",path,0);
1919 printf(" Certificate Verification File: %s\r\n",path);
1920 SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
1925 /* The defaults in the SSL crypto library are not appropriate for OS/2 */
1926 char path[CKMAXPATH];
1928 ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
1930 SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 1) {
1931 debug(F110,"ssl_tn_init certificate verify dir",path,0);
1933 printf(" Certificate Verification Directory: %s\r\n",path);
1934 SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
1936 ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
1937 if (zchki(path) > 0 &&
1938 SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 1) {
1939 debug(F110,"ssl_tn_init certificate verify file",path,0);
1941 printf(" Certificate Verification File: %s\r\n",path);
1942 SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
1947 SSL_CTX_set_default_verify_paths(ssl_ctx);
1948 SSL_CTX_set_default_verify_paths(tls_ctx);
1951 if (ssl_verify_file) {
1952 if (zchki(ssl_verify_file) > 0 &&
1953 SSL_CTX_load_verify_locations(tls_ctx,ssl_verify_file,NULL) == 1) {
1954 debug(F110,"ssl_tn_init certificate verify file",ssl_verify_file,0);
1956 printf(" Certificate Verification File: %s\r\n",ssl_verify_file);
1957 SSL_CTX_load_verify_locations(ssl_ctx,ssl_verify_file,NULL);
1960 if (ssl_verify_dir && isdir(ssl_verify_dir)) {
1961 if (SSL_CTX_load_verify_locations(tls_ctx,NULL,ssl_verify_dir) == 1) {
1962 debug(F110,"ssl_tn_init certificate verify dir",ssl_verify_dir,0);
1964 printf(" Certificate Verification Directory: %s\r\n",ssl_verify_dir);
1965 SSL_CTX_load_verify_locations(ssl_ctx,NULL,ssl_verify_dir);
1968 if (mode == SSL_SERVER) {
1969 SSL_CTX_set_verify(ssl_ctx,
1970 ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
1971 ssl_server_verify_callback);
1972 SSL_CTX_set_verify(tls_ctx,
1973 ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
1974 ssl_server_verify_callback);
1976 SSL_CTX_set_verify(ssl_ctx,ssl_verify_flag,
1977 ssl_client_verify_callback);
1978 SSL_CTX_set_verify(tls_ctx,ssl_verify_flag,
1979 ssl_client_verify_callback);
1982 /* Free the existing CRL Store */
1984 X509_STORE_free(crl_store);
1988 /* set up the new CRL Store */
1989 crl_store = X509_STORE_new();
1992 char path[CKMAXPATH];
1994 ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
1996 X509_STORE_load_locations(crl_store,NULL,path) == 1) {
1997 debug(F110,"ssl_tn_init crl dir",path,0);
1999 printf(" CRL Directory: %s\r\n",path);
2002 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
2004 X509_STORE_load_locations(crl_store,NULL,path) == 1) {
2005 debug(F110,"ssl_tn_init crl dir",path,0);
2007 printf(" CRL Directory: %s\r\n",path);
2009 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
2011 X509_STORE_load_locations(crl_store,NULL,path) == 1) {
2012 debug(F110,"ssl_tn_init crl dir",path,0);
2014 printf(" CRL Directory: %s\r\n",path);
2018 ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
2019 if (zchki(path) > 0 &&
2020 X509_STORE_load_locations(crl_store,path,NULL) == 1) {
2021 debug(F110,"ssl_tn_init crl file",path,0);
2023 printf(" CRL File: %s\r\n",path);
2026 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
2027 if (zchki(path) > 0 &&
2028 X509_STORE_load_locations(crl_store,path,NULL) == 1) {
2029 debug(F110,"ssl_tn_init crl file",path,0);
2031 printf(" CRL File: %s\r\n",path);
2033 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
2034 if (zchki(path) > 0 &&
2035 X509_STORE_load_locations(crl_store,path,NULL) == 1) {
2036 debug(F110,"ssl_tn_init crl file",path,0);
2038 printf(" CRL File: %s\r\n",path);
2043 if (ssl_crl_file || ssl_crl_dir) {
2044 if (ssl_crl_file && zchki(ssl_crl_file) > 0 &&
2045 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 1) {
2046 debug(F110,"ssl_tn_init crl file",ssl_crl_file,0);
2048 printf(" CRL File: %s\r\n",ssl_crl_file);
2050 if (ssl_crl_dir && isdir(ssl_crl_dir) &&
2051 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 1) {
2052 debug(F110,"ssl_tn_init crl dir",ssl_crl_dir,0);
2054 printf(" CRL Directory: %s\r\n",ssl_crl_dir);
2059 X509_STORE_set_default_paths(crl_store);
2066 ssl_con=(SSL *)SSL_new(ssl_ctx);
2068 debug(F110,"ssl_tn_init","SSL_new(ssl_con) failed",0);
2074 if ( mode == SSL_CLIENT ) {
2075 SSL_set_session(ssl_con, SSL_get_session(ssl_conx));
2078 if (ssl_conx->kssl_ctx) {
2079 kssl_ctx_free(ssl_conx->kssl_ctx);
2080 ssl_conx->kssl_ctx = NULL;
2082 #endif /* SSL_KRB5 */
2087 tls_con=(SSL *)SSL_new(tls_ctx);
2089 debug(F110,"ssl_tn_init","SSL_new(tls_con) failed",0);
2095 if ( mode == SSL_CLIENT )
2096 SSL_set_session(tls_con, SSL_get_session(tls_conx));
2098 if (tls_conx->kssl_ctx) {
2099 kssl_ctx_free(tls_conx->kssl_ctx);
2100 tls_conx->kssl_ctx = NULL;
2102 #endif /* SSL_KRB5 */
2107 /* I don't know why this does not work to reuse the connection. */
2110 SSL_set_session(ssl_con,NULL);
2111 SSL_set_accept_state(ssl_con) ;
2113 ssl_con=(SSL *)SSL_new(ssl_ctx);
2115 debug(F110,"ssl_tn_init","SSL_new(ssl_ctx) failed",0);
2124 SSL_set_session(tls_con,NULL);
2125 SSL_set_accept_state(tls_con) ;
2127 tls_con=(SSL *)SSL_new(tls_ctx);
2129 debug(F110,"ssl_tn_init","SSL_new(tls_ctx) failed",0);
2135 #endif /* COMMENT */
2138 #ifndef KRB5_SERVICE_NAME
2139 #define KRB5_SERVICE_NAME "host"
2142 if (ssl_con->kssl_ctx == NULL)
2143 ssl_con->kssl_ctx = kssl_ctx_new();
2144 if (tls_con->kssl_ctx == NULL)
2145 tls_con->kssl_ctx = kssl_ctx_new();
2146 if (mode == SSL_SERVER) {
2147 if (ssl_con->kssl_ctx != NULL)
2148 kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
2149 if (tls_con->kssl_ctx != NULL)
2150 kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
2152 if (ssl_con->kssl_ctx != NULL)
2153 kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVER, szHostName);
2154 if (tls_con->kssl_ctx != NULL)
2155 kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVER, szHostName);
2157 kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVICE,
2158 krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
2159 kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVICE,
2160 krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
2161 #endif /* SSL_KRB5 */
2163 if (ssl_cipher_list) {
2164 SSL_set_cipher_list(ssl_con,ssl_cipher_list);
2165 SSL_set_cipher_list(tls_con,ssl_cipher_list);
2168 if (p = getenv("SSL_CIPHER")) {
2169 SSL_set_cipher_list(ssl_con,p);
2170 SSL_set_cipher_list(tls_con,p);
2172 SSL_set_cipher_list(ssl_con,DEFAULT_CIPHER_LIST);
2173 SSL_set_cipher_list(tls_con,DEFAULT_CIPHER_LIST);
2177 ssl_verify_depth = -1;
2179 if ( ssl_debug_flag )
2180 printf("SSL/TLS init done!\r\n");
2182 ssl_initialized = 1;
2183 last_ssl_mode = mode;
2184 debug(F110,"ssl_tn_init","done",0);
2191 ssl_http_init(char * hostname)
2193 ssl_http_init(hostname) char * hostname;
2194 #endif /* CK_ANSIC */
2197 extern char * k5_keytab;
2198 extern char * krb5_d_srv;
2200 SSL * tls_conx=NULL;
2202 ssl_http_initialized = 0;
2204 if ( !ck_ssleay_is_installed() )
2206 debug(F110,"ssl_http_init",hostname,0);
2209 printf("SSL_DEBUG_FLAG on\r\n");
2211 if (!tls_http_ctx ) {
2213 TLS 1.0 is the new default as of 5 Feb 2015.
2214 Previously this was commented out because
2215 "too many web servers still do not support TLSv1".
2216 Now we try TLS 1.0 first, falling back to SSL 2.3
2217 and SSL 3.0 in that order. Maybe there should be
2218 an option not to fall back.
2220 tls_http_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_client_method());
2221 if ( tls_http_ctx ) {
2222 debug(F110,"ssl_http_init","TLSv1_client_method OK",0);
2225 SSL_CTX_set_default_passwd_cb(tls_http_ctx,
2226 (pem_password_cb *)ssl_passwd_callback);
2228 /* for SSL switch on all the interoperability and bug
2229 * workarounds so that we will communicate with people
2230 * who cannot read poorly written specs :-)
2231 * for TLS be sure to prevent use of SSLv2
2233 SSL_CTX_set_options(tls_http_ctx,
2234 SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
2235 #ifdef DISABLE_SSLV3
2240 SSL_CTX_set_info_callback(tls_http_ctx,ssl_client_info_callback);
2243 SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_CLIENT);
2244 SSL_CTX_set_session_id_context(tls_http_ctx,(CHAR *)"3",1);
2246 SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_OFF);
2247 #endif /* COMMENT */
2249 /* make sure we will find certificates in the standard
2250 * location ... otherwise we don't look anywhere for
2251 * these things which is going to make client certificate
2252 * exchange rather useless :-)
2258 /* The defaults in the SSL crypto library are not appropriate for OS/2 */
2259 char path[CKMAXPATH];
2261 ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
2262 if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
2263 debug(F110,"ssl_http_init unable to load path",path,0);
2265 printf("?Unable to load verify-dir: %s\r\n",path);
2268 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
2269 if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
2270 debug(F110,"ssl_http_init unable to load path",path,0);
2272 printf("?Unable to load verify-dir: %s\r\n",path);
2275 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
2276 if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
2277 debug(F110,"ssl_http_init unable to load path",path,0);
2279 printf("?Unable to load verify-dir: %s\r\n",path);
2282 ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
2283 if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
2284 debug(F110,"ssl_http_init unable to load path",path,0);
2286 printf("?Unable to load verify-file: %s\r\n",path);
2289 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
2290 if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
2291 debug(F110,"ssl_http_init unable to load path",path,0);
2293 printf("?Unable to load verify-file: %s\r\n",path);
2296 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
2297 if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
2298 debug(F110,"ssl_http_init unable to load path",path,0);
2300 printf("?Unable to load verify-file: %s\r\n",path);
2305 /* The defaults in the SSL crypto library are not appropriate for OS/2 */
2306 char path[CKMAXPATH];
2308 ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
2309 if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0) {
2310 debug(F110,"ssl_http_init unable to load path",path,0);
2312 printf("?Unable to load verify-dir: %s\r\n",path);
2314 ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
2315 if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
2316 debug(F110,"ssl_http_init unable to load path",path,0);
2318 printf("?Unable to load verify-file: %s\r\n",path);
2323 SSL_CTX_set_default_verify_paths(tls_http_ctx);
2326 if (ssl_verify_file &&
2327 SSL_CTX_load_verify_locations(tls_http_ctx,ssl_verify_file,NULL) == 0) {
2328 debug(F110,"ssl_http_init unable to load ssl_verify_file",ssl_verify_file,0);
2330 printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
2332 if (ssl_verify_dir &&
2333 SSL_CTX_load_verify_locations(tls_http_ctx,NULL,ssl_verify_dir) == 0) {
2334 debug(F110,"ssl_http_init unable to load ssl_verify_dir",ssl_verify_dir,0);
2336 printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
2339 SSL_CTX_set_verify(tls_http_ctx,ssl_verify_flag,
2340 ssl_client_verify_callback);
2342 /* Free the existing CRL Store */
2344 X509_STORE_free(crl_store);
2348 /* set up the new CRL Store */
2349 crl_store = X509_STORE_new();
2352 char path[CKMAXPATH];
2354 ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
2355 if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
2356 debug(F110,"ssl_http_init unable to load dir",path,0);
2358 printf("?Unable to load crl-dir: %s\r\n",path);
2361 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
2362 if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
2363 debug(F110,"ssl_http_init unable to load dir",path,0);
2365 printf("?Unable to load crl-dir: %s\r\n",path);
2367 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
2368 if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
2369 debug(F110,"ssl_http_init unable to load dir",path,0);
2371 printf("?Unable to load crl-dir: %s\r\n",path);
2375 ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
2376 if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
2377 debug(F110,"ssl_http_init unable to load file",path,0);
2379 printf("?Unable to load crl-file: %s\r\n",path);
2382 ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
2383 if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
2384 debug(F110,"ssl_http_init unable to load file",path,0);
2386 printf("?Unable to load crl-file: %s\r\n",path);
2388 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
2389 if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
2390 debug(F110,"ssl_http_init unable to load file",path,0);
2392 printf("?Unable to load crl-file: %s\r\n",path);
2397 if (ssl_crl_file || ssl_crl_dir) {
2399 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
2400 debug(F110,"ssl_http_init unable to load ssl_crl_file",ssl_crl_file,0);
2402 printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
2405 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
2406 debug(F110,"ssl_http_init unable to load ssl_crl_dir",ssl_crl_dir,0);
2408 printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
2411 X509_STORE_set_default_paths(crl_store);
2416 tls_conx = tls_http_con;
2417 tls_http_con=(SSL *)SSL_new(tls_http_ctx);
2418 if ( !tls_http_con ) {
2419 debug(F110,"ssl_http_init","SSL_new(tls_http_con) failed",0);
2420 tls_http_con = tls_conx;
2424 SSL_set_session(tls_http_con, SSL_get_session(tls_conx));
2426 if (tls_conx->kssl_ctx) {
2427 kssl_ctx_free(tls_conx->kssl_ctx);
2428 tls_conx->kssl_ctx = NULL;
2430 #endif /* SSL_KRB5 */
2435 /* I don't know why this does not work to reuse the connection. */
2436 if ( tls_http_con ) {
2437 SSL_clear(tls_http_con);
2438 SSL_set_session(tls_http_con,NULL);
2439 SSL_set_accept_state(tls_http_con) ;
2441 tls_http_con=(SSL *)SSL_new(tls_http_ctx);
2442 if ( !tls_http_con ) {
2443 debug(F110,"ssl_http_init","SSL_new(tls_http_ctx) failed",0);
2444 tls_http_con = tls_conx;
2448 #endif /* COMMENT */
2451 #ifndef KRB5_SERVICE_NAME
2452 #define KRB5_SERVICE_NAME "host"
2455 if (tls_http_con->kssl_ctx == NULL)
2456 tls_http_con->kssl_ctx = kssl_ctx_new();
2457 if (tls_http_con->kssl_ctx != NULL)
2458 kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVER, hostname);
2460 kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVICE,
2461 krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
2462 #endif /* SSL_KRB5 */
2464 if (ssl_cipher_list)
2465 SSL_set_cipher_list(tls_http_con,ssl_cipher_list);
2468 if (p = getenv("SSL_CIPHER")) {
2469 SSL_set_cipher_list(tls_http_con,p);
2471 SSL_set_cipher_list(tls_http_con,DEFAULT_CIPHER_LIST);
2475 ssl_verify_depth = -1;
2477 if ( ssl_debug_flag )
2478 printf("SSL/TLS init done!\r\n");
2480 ssl_http_initialized = 1;
2486 ssl_get_dNSName(ssl) SSL * ssl;
2488 static char *dns = NULL;
2489 X509 *server_cert = NULL;
2491 X509_EXTENSION *ext = NULL;
2492 STACK_OF(GENERAL_NAME) *ialt = NULL;
2493 GENERAL_NAME *gen = NULL;
2500 if (server_cert = SSL_get_peer_certificate(ssl)) {
2501 if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1))<0)
2503 if (!(ext = X509_get_ext(server_cert, i)))
2505 X509V3_add_standard_extensions();
2506 if (!(ialt = X509V3_EXT_d2i(ext)))
2508 for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
2509 gen = sk_GENERAL_NAME_value(ialt, i);
2510 if (gen->type == GEN_DNS) {
2511 if (!gen->d.ia5 || !gen->d.ia5->length)
2513 if (strlen((char *)gen->d.ia5->data) != gen->d.ia5->length) {
2514 /* Ignoring IA5String containing null character */
2517 dns = malloc(gen->d.ia5->length + 1);
2519 memcpy(dns, gen->d.ia5->data, gen->d.ia5->length);
2520 dns[gen->d.ia5->length] = 0;
2525 X509V3_EXT_cleanup();
2528 if (ialt) sk_GENERAL_NAME_free(ialt);
2529 if (server_cert) X509_free(server_cert);
2534 ssl_get_commonName(ssl) SSL * ssl; {
2535 static char name[256];
2541 if (server_cert = SSL_get_peer_certificate(ssl)) {
2543 X509_NAME_get_text_by_NID(X509_get_subject_name(server_cert),
2544 NID_commonName, name, sizeof(name));
2545 X509_free(server_cert);
2547 if (name_text_len <= 0) {
2548 /* Common Name was empty or not retrieved */
2550 } else if (strlen(name) != name_text_len) {
2551 /* Ignoring Common Name containing null character */
2563 ssl_get_issuer_name(ssl) SSL * ssl;
2565 static char name[256];
2569 if (server_cert = SSL_get_peer_certificate(ssl)) {
2570 X509_NAME_oneline(X509_get_issuer_name(server_cert),name,sizeof(name));
2571 X509_free(server_cert);
2576 fprintf(stderr, "Warning: No certificate from server!\r\n");
2577 #endif /* COMMENT */
2583 ssl_get_subject_name(ssl) SSL * ssl;
2585 static char name[256];
2589 if (server_cert = SSL_get_peer_certificate(ssl)) {
2590 X509_NAME_oneline(X509_get_subject_name(server_cert),name,sizeof(name));
2591 X509_free(server_cert);
2600 && !(ck_ssleay_is_installed() &&
2601 (tls_active_flag || ssl_active_flag) &&
2602 ssl_anonymous_cipher(tls_active_flag?tls_con:ssl_con))
2606 ssl_anonymous_cipher(ssl) SSL * ssl;
2611 cert = SSL_get_certificate(ssl);
2613 cert = SSL_get_peer_certificate(ssl);
2621 #endif /* COMMENT */
2624 This one is (very much!) based on work by
2625 Ralf S. Engelschall <rse@engelschall.com>.
2629 ssl_verify_crl(int ok, X509_STORE_CTX *ctx)
2632 X509_NAME *subject = NULL;
2633 X509_NAME *issuer = NULL;
2635 X509_CRL *crl = NULL;
2636 X509_REVOKED *revoked = NULL;
2637 X509_STORE_CTX * store_ctx = NULL;
2645 * Unless a revocation store for CRLs was created we
2646 * cannot do any CRL-based verification, of course.
2651 store_ctx = X509_STORE_CTX_new();
2656 * Determine certificate ingredients in advance
2658 xs = X509_STORE_CTX_get_current_cert(ctx);
2659 subject = X509_get_subject_name(xs);
2660 issuer = X509_get_issuer_name(xs);
2663 * OpenSSL provides the general mechanism to deal with CRLs but does not
2664 * use them automatically when verifying certificates, so we do it
2665 * explicitly here. We will check the CRL for the currently checked
2666 * certificate, if there is such a CRL in the store.
2668 * We come through this procedure for each certificate in the certificate
2669 * chain, starting with the root-CA's certificate. At each step we've to
2670 * both verify the signature on the CRL (to make sure it's a valid CRL)
2671 * and it's revocation list (to make sure the current certificate isn't
2672 * revoked). But because to check the signature on the CRL we need the
2673 * public key of the issuing CA certificate (which was already processed
2674 * one round before), we've a little problem. But we can both solve it and
2675 * at the same time optimize the processing by using the following
2676 * verification scheme (idea and code snippets borrowed from the GLOBUS
2679 * 1. We'll check the signature of a CRL in each step when we find a CRL
2680 * through the _subject_ name of the current certificate. This CRL
2681 * itself will be needed the first time in the next round, of course.
2682 * But we do the signature processing one round before this where the
2683 * public key of the CA is available.
2685 * 2. We'll check the revocation list of a CRL in each step when
2686 * we find a CRL through the _issuer_ name of the current certificate.
2687 * This CRLs signature was then already verified one round before.
2689 * This verification scheme allows a CA to revoke its own certificate as
2694 * Try to retrieve a CRL corresponding to the _subject_ of
2695 * the current certificate in order to verify it's integrity.
2697 memset((char *)&obj, 0, sizeof(obj));
2698 X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
2699 rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, subject, &obj);
2700 X509_STORE_CTX_cleanup(store_ctx);
2702 if (rc > 0 && crl != NULL) {
2704 * Verify the signature on this CRL
2706 if (X509_CRL_verify(crl, X509_get_pubkey(xs)) <= 0) {
2707 fprintf(stderr, "Invalid signature on CRL!\n");
2708 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
2709 X509_OBJECT_free_contents(&obj);
2710 X509_STORE_CTX_free(store_ctx);
2715 * Check date of CRL to make sure it's not expired
2717 i = X509_cmp_current_time(X509_CRL_get_nextUpdate(crl));
2719 fprintf(stderr, "Found CRL has invalid nextUpdate field.\n");
2720 X509_STORE_CTX_set_error(ctx,
2721 X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
2722 X509_OBJECT_free_contents(&obj);
2723 X509_STORE_CTX_free(store_ctx);
2728 "Found CRL is expired - revoking all certificates until you get updated CRL.\n"
2730 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_HAS_EXPIRED);
2731 X509_OBJECT_free_contents(&obj);
2732 X509_STORE_CTX_free(store_ctx);
2735 X509_OBJECT_free_contents(&obj);
2739 * Try to retrieve a CRL corresponding to the _issuer_ of
2740 * the current certificate in order to check for revocation.
2742 memset((char *)&obj, 0, sizeof(obj));
2743 X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
2744 rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, issuer, &obj);
2745 X509_STORE_CTX_free(store_ctx); /* calls X509_STORE_CTX_cleanup() */
2747 if (rc > 0 && crl != NULL) {
2749 * Check if the current certificate is revoked by this CRL
2751 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
2752 for (i = 0; i < n; i++) {
2753 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
2754 if (ASN1_INTEGER_cmp(revoked->serialNumber,
2755 X509_get_serialNumber(xs)) == 0) {
2757 serial = ASN1_INTEGER_get(revoked->serialNumber);
2758 cp = X509_NAME_oneline(issuer, NULL, 0);
2761 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REVOKED);
2762 X509_OBJECT_free_contents(&obj);
2766 X509_OBJECT_free_contents(&obj);
2772 tls_userid_from_client_cert(ssl) SSL * ssl;
2774 static char cn[256];
2775 static char *r = cn;
2779 if (client_cert = SSL_get_peer_certificate(ssl)) {
2780 /* call the custom function */
2781 err = X509_to_user(client_cert, cn, sizeof(cn));
2782 X509_free(client_cert);
2793 tls_get_SAN_objs(SSL * ssl, int type)
2794 /* returns NULL or an array of malloc'ed objects of type `type' from the server's
2795 * subjectAltName, remember to free() them all!
2798 #define NUM_SAN_OBJS 64
2799 static unsigned char *objs[NUM_SAN_OBJS];
2800 unsigned char **rv = NULL;
2801 X509 *server_cert = NULL;
2803 X509_EXTENSION *ext = NULL;
2804 STACK_OF(GENERAL_NAME) *ialt = NULL;
2805 GENERAL_NAME *gen = NULL;
2807 memset(objs, 0, sizeof(objs));
2808 if (server_cert = SSL_get_peer_certificate(ssl)) {
2809 if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1)) < 0)
2811 if (!(ext = X509_get_ext(server_cert, i)))
2813 X509V3_add_standard_extensions();
2814 if (!(ialt = X509V3_EXT_d2i(ext)))
2817 for (i = 0, j = 0; i < sk_GENERAL_NAME_num(ialt) && j < NUM_SAN_OBJS - 2; i++) {
2818 gen = sk_GENERAL_NAME_value(ialt, i);
2819 /* The use of V_ASN1_CONTEXT_SPECIFIC is because OpenSSL 0.9.6 defined its
2820 * types | V_ASN1_CONTEXT_SPECIFIC. 0.9.7 does not. In case, we are built
2821 * with one and linked to the other we use this hack.
2823 if ((gen->type | V_ASN1_CONTEXT_SPECIFIC) == (type | V_ASN1_CONTEXT_SPECIFIC)) {
2824 if (!gen->d.ia5 || !gen->d.ia5->length)
2826 if (strlen((char *)gen->d.ia5->data) != gen->d.ia5->length) {
2827 /* Ignoring IA5String containing null character */
2830 objs[j] = malloc(gen->d.ia5->length + 1);
2832 memcpy(objs[j], gen->d.ia5->data, gen->d.ia5->length);
2833 objs[j][gen->d.ia5->length] = 0;
2838 X509V3_EXT_cleanup();
2841 if (ialt) sk_GENERAL_NAME_free(ialt);
2842 if (server_cert) X509_free(server_cert);
2848 dNSName_cmp(const char *host, const char *dNSName)
2850 int c1 = 1, c2 = 1, num_comp, rv = -1;
2851 char *p, *p1, *p2, *host_copy=NULL, *dNSName_copy=NULL;
2853 /* first we count the number of domain name components in both parameters.
2854 * they should be equal many, or it's not a match
2857 while (p = strstr(p, ".")) {
2861 p = (char *) dNSName;
2862 while (p = strstr(p, ".")) {
2870 makestr(&host_copy,host);
2871 makestr(&dNSName_copy,dNSName);
2872 if (host_copy == NULL || dNSName_copy == NULL)
2874 /* make substrings by replacing '.' with '\0' */
2876 while (p = strstr(p, ".")) {
2881 while (p = strstr(p, ".")) {
2886 /* compare each component */
2889 for (; num_comp; num_comp--) {
2890 if (!ckmatch(p2, p1,0,1))
2893 p1 += strlen(p1) + 1;
2894 p2 += strlen(p2) + 1;
2900 if (dNSName_copy) free(dNSName_copy);
2901 if (host_copy) free(host_copy);
2908 show_hostname_warning(char *s1, char *s2)
2913 ckmakxmsg(prefix,1024,
2914 "Warning: Hostname (\"", s1,
2915 "\") does not match server's certificate (\"", s2, "\")",
2916 NULL,NULL,NULL,NULL,NULL,NULL,NULL);
2917 if (ssl_verify_flag)
2942 #define inet_aton INET_ATON
2943 #endif /* DEC_TCPIP */
2945 #ifndef NO_DCL_INET_ATON
2947 inet_aton(char * ipaddress, struct in_addr * ia) {
2948 struct stringarray * q;
2954 q = cksplit(1,0,ipaddress,".","0123456789abcdefACDEF",8,0,0);
2955 if (q->a_size == 4) {
2956 dummy.b[0] = atoi(q->a_head[1]);
2957 dummy.b[1] = atoi(q->a_head[2]);
2958 dummy.b[2] = atoi(q->a_head[3]);
2959 dummy.b[3] = atoi(q->a_head[4]);
2960 ia->s_addr = dummy.l;
2961 return(ia->s_addr != 0);
2965 #endif /* NO_DCL_INET_ATON */
2968 #endif /* SOLARIS7 */
2969 #endif /* SOLARIS8 */
2970 #endif /* SOLARIS9 */
2975 #endif /* NETBSD15 */
2976 #endif /* FREEBSD4 */
2977 #endif /* OpenBSD */
2978 #endif /* SCO_OSR505 */
2979 #endif /* HPUX1100 */
2984 ssl_check_server_name(SSL * ssl, char * hostname)
2985 /* returns 0 if hostname and server's cert matches, else -1 */
2988 unsigned char ** dNSName;
2989 unsigned char ** ipAddress;
2994 if (verbosity && !inserver) {
2995 if (dNSName = tls_get_SAN_objs(ssl,GEN_DNS)) {
2997 for (i = 0; dNSName[i]; i++) {
2998 printf("Certificate[0] altSubjectName DNS=%s\r\n",dNSName[i]);
3002 if (ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD)) {
3007 for (i = 0; ipAddress[i]; i++) {
3009 ia.s_addr = *(unsigned long *)ipAddress[i];
3010 server_ip = inet_ntoa(ia);
3011 printf("Certificate[0] altSubjectName IPAddr=%s\r\n",server_ip);
3015 /* ipAddress points to a static - don't free */
3017 if (dNSName = tls_get_SAN_objs(ssl,GEN_EMAIL)) {
3019 for (i = 0; dNSName[i]; i++) {
3020 printf("Certificate[0] altSubjectName Email=%s\r\n",dNSName[i]);
3024 if (dNSName = tls_get_SAN_objs(ssl,GEN_URI)) {
3026 for (i = 0; dNSName[i]; i++) {
3027 printf("Certificate[0] altSubjectName URI=%s\r\n",dNSName[i]);
3031 if (dNSName = tls_get_SAN_objs(ssl,GEN_OTHERNAME)) {
3033 for (i = 0; dNSName[i]; i++) {
3034 printf("Certificate[0] altSubjectName Other=%s\r\n",dNSName[i]);
3040 /* first we check if `hostname' is in fact an ip address */
3041 if (inet_aton(hostname, &ia)) {
3042 ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD);
3045 char *server_ip = "UNKNOWN";
3047 for (i = 0; ipAddress[i]; i++)
3048 if (*(unsigned long *)ipAddress[i] == ia.s_addr)
3051 if (ipAddress[i - 1]) {
3052 ia.s_addr = *(unsigned long *)ipAddress[i - 1];
3053 server_ip = inet_ntoa(ia);
3055 rv = show_hostname_warning(hostname, server_ip) ? 0 : -1;
3056 for (i = 0; ipAddress[i]; i++)
3059 rv = show_hostname_warning(hostname, "NO IP IN CERT") ? 0 : -1;
3064 /* look for dNSName(s) in subjectAltName in the server's certificate */
3065 dNSName = tls_get_SAN_objs(ssl,GEN_DNS);
3068 for (i = 0; dNSName[i]; i++) {
3069 if (!dNSName_cmp(hostname,(char *)dNSName[i]))
3072 rv = show_hostname_warning(hostname,
3073 (char *)((dNSName[i - 1] == NULL) ?
3074 (char *)"UNKNOWN" : (char *)dNSName[i - 1]))
3076 for (i = 0; dNSName[i]; i++)
3079 } else if ((commonName = ssl_get_commonName(ssl))) {
3080 /* so the server didn't have any dNSName's, check the commonName */
3081 if (!dNSName_cmp(hostname, commonName))
3084 return (show_hostname_warning(hostname, commonName) ? 0 : -1);
3089 /* Is 'user' authorized to access the system without a login */
3091 tls_is_user_valid(SSL * ssl, const char *user)
3096 if ( !ssl || !user || !user[0] )
3099 if (!(client_cert = SSL_get_peer_certificate(ssl)))
3102 /* Use user supplied function */
3103 r = X509_userok(client_cert,user);
3105 X509_free(client_cert);
3113 SSL_CIPHER * cipher;
3119 case 1: /* ftp command */
3120 if ( ssl_ftp_active_flag )
3125 case 2: /* ftp data */
3126 if ( ssl_ftp_data_active_flag )
3127 ssl = ssl_ftp_data_con;
3134 if (tls_active_flag)
3136 else if (ssl_active_flag)
3142 cipher = SSL_get_current_cipher(ssl);
3143 if (SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
3144 if (ckindex("Au=None",buf,0,0,0) != 0)
3145 return(1); /* anonymous */
3146 return(0); /* known */
3148 /* could not get cipher description. Assume anonymous */
3157 SSL_CIPHER * cipher;
3163 case 1: /* ftp command */
3164 if ( ssl_ftp_active_flag )
3169 case 2: /* ftp data */
3170 if ( ssl_ftp_data_active_flag )
3171 ssl = ssl_ftp_data_con;
3179 if ( tls_http_active_flag )
3184 if (tls_active_flag)
3186 else if (ssl_active_flag)
3192 cipher = SSL_get_current_cipher(ssl);
3193 if (cipher && SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
3194 if (ckindex("Au=KRB5",buf,0,0,0) != 0)
3195 return(1); /* krb5 */
3197 return(0); /* not */
3201 ssl_get_client_finished(char *buf, int count)
3203 #ifdef NO_GET_FINISHED
3206 if (sstelnet || tcp_incoming) {
3207 return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
3210 return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
3213 #endif /* NO_GET_FINISHED */
3217 ssl_get_server_finished(char *buf, int count)
3219 #ifdef NO_GET_FINISHED
3222 if (sstelnet || tcp_incoming) {
3223 return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
3226 return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
3229 #endif /* NO_GET_FINISHED */
3233 #ifdef CK_AUTHENTICATION
3236 ssl_reply(int how, unsigned char *data, int cnt)
3238 ssl_reply(how,data,cnt) int how; unsigned char *data; int cnt;
3244 data += 4; /* Point to status byte */
3248 auth_finished(AUTH_REJECT);
3249 return AUTH_FAILURE;
3254 if (tn_deb || debses)
3255 tn_debug("[SSL - handshake starting]");
3256 else if ( verbosity )
3257 printf("[SSL - handshake starting]\r\n");
3258 debug(F110,"ssl_reply","[SSL - handshake starting]",0);
3260 /* right ... now we drop into the SSL library */
3261 if (!ssl_only_flag) {
3262 if (ssl_dummy_flag) {
3263 if (tn_deb || debses)
3264 tn_debug("[SSL - Dummy Connected]");
3265 else if ( verbosity ) {
3266 printf("[SSL - Dummy Connected]\r\n");
3268 debug(F110,"ssl_reply","[SSL - Dummy Connected]",0);
3269 auth_finished(AUTH_UNKNOWN);
3270 accept_complete = 1;
3271 return AUTH_SUCCESS;
3274 if (SSL_connect(ssl_con) <= 0) {
3276 if (tn_deb || debses) {
3277 tn_debug("[SSL - FAILED]");
3278 ERR_print_errors(bio_err);
3279 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
3280 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
3282 } else if ( verbosity ) {
3283 printf("[SSL - FAILED]\r\n");
3284 ERR_print_errors(bio_err);
3285 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
3286 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
3289 debug(F110,"ssl_reply","[SSL - FAILED]",0);
3290 auth_finished(AUTH_REJECT);
3292 return AUTH_FAILURE;
3294 if (tn_deb || debses)
3295 tn_debug("[SSL - OK]");
3296 else if ( verbosity ) {
3297 printf("[SSL - OK]\r\n");
3299 debug(F110,"ssl_reply","[SSL - OK]",0);
3301 ssl_active_flag = 1;
3302 ssl_display_connect_details(ssl_con,0,verbosity);
3305 auth_finished(AUTH_UNKNOWN);
3306 accept_complete = 1;
3310 if (tn_deb || debses) {
3312 "[SSL - failed to switch on SSL - trying plaintext login]");
3313 } else if ( verbosity ) {
3314 printf("[SSL - failed to switch on SSL]\r\n");
3315 printf("Trying plaintext login:\r\n");
3317 debug(F110,"ssl_reply","[SSL - failed to switch on SSL]",0);
3318 auth_finished(AUTH_REJECT);
3319 return AUTH_FAILURE;
3322 return AUTH_FAILURE;
3324 return AUTH_SUCCESS;
3329 ssl_is(unsigned char *data, int cnt)
3331 ssl_is(data,cnt) unsigned char *data; int cnt;
3335 return AUTH_FAILURE;
3341 /* server starts the SSL stuff now ... */
3342 if (!ssl_only_flag) {
3343 if ( !tls_load_certs(ssl_ctx,ssl_con,1) ) {
3344 auth_finished(AUTH_REJECT);
3345 return AUTH_FAILURE;
3348 if (tn_deb || debses)
3349 tn_debug("[SSL - handshake starting]");
3350 else if ( verbosity )
3351 printf("[SSL - handshake starting]\r\n");
3352 debug(F110,"ssl_is","[SSL - handshake starting]",0);
3354 SendSSLAuthSB(SSL_ACCEPT, (void *)0, 0);
3358 if (ssl_dummy_flag) {
3359 if (tn_deb || debses)
3360 tn_debug("[SSL - Dummy Connected]");
3361 else if ( verbosity ) {
3362 printf("[SSL - Dummy Connected]\r\n");
3364 debug(F110,"ssl_is","[SSL - Dummy Connected]",0);
3365 accept_complete = 1;
3366 auth_finished(AUTH_UNKNOWN);
3367 return AUTH_SUCCESS;
3370 if (SSL_accept(ssl_con) <= 0) {
3373 sprintf(errbuf,"[SSL - SSL_accept error: %s",
3374 ERR_error_string(ERR_get_error(),NULL));
3376 if (tn_deb || debses)
3378 else if ( ssl_debug_flag )
3379 printf("%s\r\n",errbuf);
3380 else if ( verbosity )
3381 printf("[SSL - SSL_accept error]\r\n");
3383 debug(F110,"ssl_is",errbuf,0);
3385 auth_finished(AUTH_REJECT);
3387 return AUTH_FAILURE;
3390 if (tn_deb || debses)
3391 tn_debug("[SSL - OK]");
3392 else if ( verbosity ) {
3393 printf("[SSL - OK]\r\n");
3395 debug(F110,"ssl_is","[SSL - OK]",0);
3396 ssl_active_flag = 1;
3397 ssl_display_connect_details(ssl_con,1,verbosity);
3399 /* now check to see that we got exactly what we
3400 * wanted from the caller ... if a certificate is
3401 * required then we make 100% sure that we were
3402 * given one during the handshake (as it is an optional
3407 if ( tls_is_krb5(0) ) {
3408 if (ssl_con->kssl_ctx->client_princ)
3409 debug(F110,"ssl_is KRB5",ssl_con->kssl_ctx->client_princ,0);
3411 #endif /* SSL_KRB5 */
3412 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
3413 X509 * peer = SSL_get_peer_certificate(ssl_con);
3415 if (tn_deb || debses)
3416 tn_debug("[SSL - peer check failed]");
3417 else if (ssl_debug_flag)
3418 printf("[SSL - peer check failed]\r\n");
3419 debug(F110,"ssl_is","[SSL - peer check failed]",0);
3421 /* LOGGING REQUIRED HERE! */
3422 auth_finished(AUTH_REJECT);
3423 return AUTH_FAILURE;
3426 auth_finished(AUTH_UNKNOWN);
3427 accept_complete = 1;
3432 SendSSLAuthSB(SSL_REJECT, (void *) "Unknown option received", -1);
3433 if (tn_deb || debses)
3434 tn_debug("[SSL - Unknown option received]");
3436 printf("Unknown SSL option %d\r\n", data[-1]);
3437 debug(F111,"ssl_is","[SSL - Unknown option received]",data[-1]);
3439 auth_finished(AUTH_REJECT);
3440 return(AUTH_FAILURE);
3442 return AUTH_SUCCESS;
3445 #endif /* CK_AUTHENTICATION */
3448 ck_tn_tls_negotiate(VOID)
3451 char str[256], *uid=NULL;
3452 extern int sstelnet;
3454 if ( !ck_ssleay_is_installed() )
3459 /* server starts the TLS stuff now ... */
3460 if (!tls_only_flag) {
3461 if ( !tls_load_certs(tls_ctx,tls_con,1) ) {
3462 auth_finished(AUTH_REJECT);
3466 if (tn_deb || debses)
3467 tn_debug("[TLS - handshake starting]");
3468 else if ( verbosity )
3469 printf("[TLS - handshake starting]\r\n");
3470 debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
3472 if (ssl_dummy_flag) {
3473 if (tn_deb || debses)
3474 tn_debug("[TLS - Dummy Connected]");
3475 else if ( verbosity ) {
3476 printf("[TLS - Dummy Connected]\r\n");
3478 debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
3479 accept_complete = 1;
3480 auth_finished(AUTH_REJECT);
3484 if (SSL_accept(tls_con) <= 0) {
3487 sprintf(errbuf,"[TLS - SSL_accept error: %s",
3488 ERR_error_string(ERR_get_error(),NULL));
3490 if (tn_deb || debses)
3492 else if ( ssl_debug_flag )
3493 printf("%s\r\n",errbuf);
3494 else if ( verbosity )
3495 printf("[TLS - SSL_accept error]\r\n");
3497 debug(F110,"ck_tn_tls_negotiate",errbuf,0);
3498 auth_finished(AUTH_REJECT);
3502 if (tn_deb || debses)
3503 tn_debug("[TLS - OK]");
3504 else if ( verbosity ) {
3505 printf("[TLS - OK]\r\n");
3508 debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
3509 tls_active_flag = 1;
3510 ssl_display_connect_details(tls_con,1,verbosity);
3514 if ( tls_is_krb5(0) ) {
3515 if (tls_con->kssl_ctx->client_princ) {
3517 ckstrncpy(szUserNameAuthenticated,
3518 tls_con->kssl_ctx->client_princ,
3520 ckstrncpy(szUserNameRequested,
3521 tls_con->kssl_ctx->client_princ,
3523 for ( p = szUserNameRequested; *p ; p++ ) {
3524 if ( *p == '@' || *p == '/' ) {
3530 szUserNameRequested[0] = '\0';
3531 szUserNameAuthenticated[0] = '\0';
3534 if (zvuser(szUserNameRequested))
3535 auth_finished(AUTH_VALID);
3537 #endif /* CK_LOGIN */
3538 auth_finished(AUTH_USER);
3540 #endif /* SSL_KRB5 */
3542 /* now check to see that we got exactly what we
3543 * wanted from the caller ... if a certificate is
3544 * required then we make 100% sure that we were
3545 * given one during the handshake (as it is an optional
3548 peer=SSL_get_peer_certificate(tls_con);
3550 debug(F100,"SSL_get_peer_certificate() == NULL","",0);
3551 auth_finished(AUTH_REJECT);
3552 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
3553 if (tn_deb || debses)
3554 tn_debug("[TLS - peer check failed]");
3555 else if (ssl_debug_flag) {
3556 printf("[TLS - peer check failed]\r\n");
3559 "ck_tn_tls_negotiate",
3560 "[TLS - peer check failed]",
3563 /* LOGGING REQUIRED HERE! */
3567 debug(F100,"SSL_get_peer_certificate() != NULL","",0);
3568 X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
3573 printf("[TLS - commonName=%s]\r\n",str);
3575 X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
3576 #ifndef NID_x500UniqueIdentifier
3577 NID_uniqueIdentifier,
3579 NID_x500UniqueIdentifier,
3585 printf("[TLS - uniqueIdentifier=%s]\r\n",str);
3587 /* Try to determine user name */
3588 uid = tls_userid_from_client_cert(tls_con);
3590 /* This code is very questionable.
3591 * How should it behave?
3592 * The client has presented a certificate that
3593 * contains a username. We have validated the
3594 * certificate but we do not automatically
3595 * log the user in unless there is a .tlslogin
3599 ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
3602 auth_finished(AUTH_VALID);
3604 #endif /* CK_LOGIN */
3605 auth_finished(AUTH_USER);
3608 szUserNameRequested[0] = '\0';
3609 auth_finished(AUTH_REJECT);
3617 if (tn_deb || debses)
3618 tn_debug("[TLS - handshake starting]");
3619 else if ( verbosity )
3620 printf("[TLS - handshake starting]\r\n");
3621 debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
3623 /* right ... now we drop into the SSL library */
3624 if (!tls_only_flag) {
3625 char *subject=NULL, *issuer=NULL, *commonName=NULL, *dNSName=NULL;
3627 if (ssl_dummy_flag) {
3628 if (tn_deb || debses)
3629 tn_debug("[TLS - Dummy Connected]");
3630 else if ( verbosity ) {
3631 printf("[TLS - Dummy Connected]\r\n");
3633 debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
3634 auth_finished(AUTH_REJECT);
3635 accept_complete = 1;
3640 if (!tls_load_certs(tls_ctx,tls_con,0))
3642 #endif /* USE_CERT_CB */
3643 if (SSL_connect(tls_con) <= 0) {
3645 if (tn_deb || debses) {
3646 tn_debug("[TLS - FAILED]");
3647 ERR_print_errors(bio_err);
3648 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
3649 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
3651 } else if ( verbosity ) {
3652 printf("[TLS - FAILED]\r\n");
3653 ERR_print_errors(bio_err);
3654 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
3655 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
3658 debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
3659 auth_finished(AUTH_REJECT);
3663 tls_active_flag = 1;
3664 if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER)
3665 && !tls_is_krb5(0)) {
3667 subject = ssl_get_subject_name(tls_con);
3670 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
3672 if (tn_deb || debses)
3673 tn_debug("[TLS - FAILED]");
3674 else if ( verbosity )
3675 printf("[TLS - FAILED]\r\n");
3676 debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
3677 auth_finished(AUTH_REJECT);
3681 ok = uq_ok("Warning: Server didn't provide a certificate",
3682 "Continue? (Y/N)", 3, NULL, 0);
3684 if (tn_deb || debses)
3685 tn_debug("[TLS - FAILED]");
3686 else if ( verbosity )
3687 printf("[TLS - FAILED]\r\n");
3689 "ck_tn_tls_negotiate","[TLS - FAILED]",0);
3690 auth_finished(AUTH_REJECT);
3694 } else if (ssl_check_server_name(tls_con, szHostName)) {
3695 if (tn_deb || debses)
3696 tn_debug("[TLS - FAILED]");
3697 else if ( verbosity )
3698 printf("[TLS - FAILED]\r\n");
3700 "ck_tn_tls_negotiate","[TLS - FAILED]",0);
3701 auth_finished(AUTH_REJECT);
3706 if ( ssl_debug_flag && ssl_finished_messages) {
3709 extern char tn_msg[], hexbuf[];
3712 len = ssl_get_client_finished(msg,len);
3714 for ( i=0;i<len;i++ ) {
3715 sprintf(hexbuf,"%02X ",msg[i]);
3716 ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
3718 printf("TLS client finished: %s\r\n",tn_msg);
3721 len = ssl_get_server_finished(msg,len);
3723 for ( i=0;i<len;i++ ) {
3724 sprintf(hexbuf,"%02X ",msg[i]);
3725 ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
3727 printf("TLS server finished: %s\r\n",tn_msg);
3731 if (tn_deb || debses)
3732 tn_debug("[TLS - OK]");
3733 else if ( verbosity )
3734 printf("[TLS - OK]\r\n");
3735 debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
3737 ssl_display_connect_details(tls_con,0,verbosity);
3739 auth_finished(AUTH_REJECT);
3741 accept_complete = 1;
3747 ck_ssl_incoming(fd) int fd;
3749 /* if we are not running in debug then any error
3750 * stuff from SSL debug *must* not go down
3751 * the socket (which 0,1,2 are all pointing to by
3758 if ( !ck_ssleay_is_installed() )
3761 /* do the SSL stuff now ... before we play with pty's */
3762 SSL_set_fd(ssl_con,fd);
3763 SSL_set_fd(tls_con,fd);
3765 if (tls_only_flag) {
3766 if (tn_deb || debses)
3767 tn_debug("[TLS - handshake starting]");
3768 else if ( verbosity )
3769 printf("[TLS - handshake starting]\r\n");
3770 debug(F110,"ck_ssl_incoming","[TLS - handshake starting]",0);
3772 /* hmm ... only when running talking to things like
3773 * https servers should we hit this code and then
3774 * we really don't care *who* we talk to :-)
3776 if (SSL_accept(tls_con) <= 0) {
3779 sprintf(errbuf,"[TLS - SSL_accept error: %s",
3780 ERR_error_string(ERR_get_error(),NULL));
3782 if (tn_deb || debses)
3784 else if ( ssl_debug_flag )
3785 printf("%s\r\n",errbuf);
3786 else if ( verbosity )
3787 printf("[TLS - SSL_accept error]\r\n");
3789 debug(F110,"ck_ssl_incoming",errbuf,0);
3792 if (tn_deb || debses)
3793 tn_debug("[TLS - OK]");
3794 else if ( verbosity )
3795 printf("[TLS - OK]\r\n");
3796 debug(F110,"ck_ssl_incoming","[TLS - OK]",0);
3797 tls_active_flag = 1;
3799 } else if (ssl_only_flag) {
3800 if (tn_deb || debses)
3801 tn_debug("[SSL - handshake starting]");
3802 else if ( verbosity )
3803 printf("[SSL - handshake starting]\r\n");
3804 debug(F110,"ck_ssl_incoming","[SSL - handshake starting]",0);
3806 /* hmm ... only when running talking to things like
3807 * https servers should we hit this code and then
3808 * we really don't care *who* we talk to :-)
3810 if (SSL_accept(ssl_con) <= 0) {
3813 sprintf(errbuf,"[SSL - SSL_accept error: %s",
3814 ERR_error_string(ERR_get_error(),NULL));
3816 if (tn_deb || debses)
3818 else if ( ssl_debug_flag )
3819 printf("%s\r\n",errbuf);
3820 else if ( verbosity )
3821 printf("[SSL - SSL_accept error]\r\n");
3823 debug(F110,"ck_ssl_incoming",errbuf,0);
3826 if (tn_deb || debses)
3827 tn_debug("[SSL - OK]");
3828 else if ( verbosity )
3829 printf("[SSL - OK]\r\n");
3830 debug(F110,"ssl_is","[SSL - OK]",0);
3831 ssl_active_flag = 1;
3834 if (ssl_active_flag || tls_active_flag) {
3836 char str[256], *uid=NULL;
3838 /* now check to see that we got exactly what we
3839 * wanted from the caller ... if a certificate is
3840 * required then we make 100% sure that we were
3841 * given on during the handshake (as it is an optional
3842 * part of SSL and TLS)
3845 if ( tls_active_flag ) {
3846 peer=SSL_get_peer_certificate(tls_con);
3847 } else if ( ssl_active_flag ) {
3848 peer=SSL_get_peer_certificate(ssl_con);
3852 debug(F100,"SSL_get_peer_certificate() == NULL","",0);
3853 auth_finished(AUTH_REJECT);
3855 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
3856 if (tn_deb || debses)
3857 tn_debug("[SSL/TLS - peer check failed]");
3858 else if (ssl_debug_flag) {
3859 printf("[SSL/TLS - peer check failed]\r\n");
3862 "ck_tn_tls_negotiate",
3863 "[SSL/TLS - peer check failed]",
3866 /* LOGGING REQUIRED HERE! */
3871 debug(F100,"SSL_get_peer_certificate() != NULL","",0);
3872 X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
3876 printf("[TLS - commonName=%s]\r\n",str);
3878 X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
3879 #ifndef NID_x500UniqueIdentifier
3880 NID_uniqueIdentifier,
3882 NID_x500UniqueIdentifier,
3886 printf("[TLS - uniqueIdentifier=%s]\r\n",str);
3888 /* Try to determine user name */
3889 uid = tls_userid_from_client_cert(tls_con);
3891 /* This code is very questionable.
3892 * How should it behave?
3893 * The client has presented a certificate that
3894 * contains a username. We have validated the
3895 * certificate but we do not automatically
3896 * log the user in unless there is a .tlslogin
3900 ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
3903 auth_finished(AUTH_VALID);
3905 #endif /* CK_LOGIN */
3906 auth_finished(AUTH_USER);
3909 szUserNameRequested[0] = '\0';
3910 auth_finished(AUTH_REJECT);
3914 return(0); /* success */
3918 ck_ssl_outgoing(fd) int fd;
3923 if ( !ck_ssleay_is_installed() )
3926 /* bind in the network descriptor */
3927 SSL_set_fd(ssl_con,fd);
3928 SSL_set_fd(tls_con,fd);
3930 /* If we are doing raw TLS then start it now ... */
3931 if (tls_only_flag) {
3933 if (!tls_load_certs(tls_ctx,tls_con,0)) {
3934 debug(F110,"ck_ssl_outgoing","tls_load_certs() failed",0);
3937 #endif /* USE_CERT_CB */
3938 if (tn_deb || debses)
3939 tn_debug("[TLS - handshake starting]");
3941 printf("[TLS - handshake starting]\r\n");
3942 debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
3943 if (SSL_connect(tls_con) <= 0) {
3946 sprintf(errbuf,"[TLS - SSL_connect error: %s",
3947 ERR_error_string(ERR_get_error(),NULL));
3949 if (tn_deb || debses)
3951 else if ( ssl_debug_flag )
3952 printf("%s\r\n",errbuf);
3954 if (tn_deb || debses)
3955 tn_debug("[TLS - FAILED]");
3956 else if ( verbosity )
3957 printf("[TLS - FAILED]\r\n");
3958 debug(F110,"ck_ssl_outgoing","[TLS - FAILED]",0);
3962 tls_active_flag = 1;
3963 if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
3965 char *subject = ssl_get_subject_name(tls_con);
3968 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
3970 if (tn_deb || debses)
3971 tn_debug("[TLS - FAILED]");
3972 else if ( verbosity )
3973 printf("[TLS - FAILED]\r\n");
3974 debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
3976 auth_finished(AUTH_REJECT);
3981 ok = uq_ok("Warning: Server didn't provide a certificate",
3982 "Continue? (Y/N)", 3, NULL, 0);
3984 if (tn_deb || debses)
3985 tn_debug("[TLS - FAILED]");
3986 else if ( verbosity )
3987 printf("[TLS - FAILED]\r\n");
3989 "ck_tn_tls_negotiate","[TLS - FAILED]",0);
3990 auth_finished(AUTH_REJECT);
3994 } else if (ssl_check_server_name(tls_con, szHostName)) {
3995 if (tn_deb || debses)
3996 tn_debug("[TLS - FAILED]");
3997 else if ( verbosity )
3998 printf("[TLS - FAILED]\r\n");
4000 "ck_tn_tls_negotiate","[TLS - FAILED]",0);
4001 auth_finished(AUTH_REJECT);
4005 if (tn_deb || debses)
4006 tn_debug("[TLS - OK]");
4008 printf("[TLS - OK]\r\n");
4009 debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
4010 ssl_display_connect_details(tls_con,0,verbosity);
4013 /* if we are doing raw SSL then start it now ... */
4014 else if (ssl_only_flag) {
4016 if (!tls_load_certs(ssl_ctx,ssl_con,0))
4018 #endif /* USE_CERT_CB */
4019 if (tn_deb || debses)
4020 tn_debug("[SSL - handshake starting]");
4021 else if ( verbosity )
4022 printf("[SSL - handshake starting]\r\n");
4023 debug(F110,"ck_ssl_outgoing","[SSL - handshake starting]",0);
4024 if (SSL_connect(ssl_con) <= 0) {
4025 if ( ssl_debug_flag ) {
4028 sprintf(errbuf,"[SSL - SSL_connect error: %s",
4029 ERR_error_string(ERR_get_error(),NULL));
4030 printf("%s\r\n",errbuf);
4032 if (tn_deb || debses)
4033 tn_debug("[SSL - FAILED]");
4034 else if ( verbosity )
4035 printf("[SSL - FAILED]\r\n");
4036 debug(F110,"ck_ssl_outgoing","[SSL - FAILED]",0);
4039 ssl_active_flag = 1;
4041 if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
4043 char *subject = ssl_get_subject_name(ssl_con);
4046 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
4048 if (tn_deb || debses)
4049 tn_debug("[SSL - FAILED]");
4050 else if ( verbosity )
4051 printf("[SSL - FAILED]\r\n");
4052 debug(F110,"ck_tn_tls_negotiate","[SSL - FAILED]",0);
4054 auth_finished(AUTH_REJECT);
4059 ok = uq_ok("Warning: Server didn't provide a certificate",
4060 "Continue? (Y/N)", 3, NULL, 0);
4062 if (tn_deb || debses)
4063 tn_debug("[SSL - FAILED]");
4064 else if ( verbosity )
4065 printf("[SSL - FAILED]\r\n");
4067 "ck_tn_tls_negotiate","[SSL - FAILED]",0);
4068 auth_finished(AUTH_REJECT);
4072 } else if (ssl_check_server_name(ssl_con, szHostName)) {
4073 if (tn_deb || debses)
4074 tn_debug("[SSL - FAILED]");
4075 else if ( verbosity )
4076 printf("[SSL - FAILED]\r\n");
4077 debug(F110, "ck_tn_tls_negotiate","[SSL - FAILED]",0);
4078 auth_finished(AUTH_REJECT);
4082 if (tn_deb || debses)
4083 tn_debug("[SSL - OK]");
4085 printf("[SSL - OK]\r\n");
4086 debug(F110,"ck_ssl_outgoing","[SSL - OK]",0);
4087 ssl_display_connect_details(ssl_con,0,verbosity);
4090 return(0); /* success */
4095 ck_ssl_http_client(fd, hostname) int fd; char * hostname;
4099 if ( !ck_ssleay_is_installed() )
4104 /* bind in the network descriptor */
4105 SSL_set_fd(tls_http_con,fd);
4107 /* If we are doing raw TLS then start it now ... */
4110 if (!tls_load_certs(tls_http_ctx,tls_http_con,0)) {
4111 debug(F110,"ck_ssl_http_client","tls_load_certs() failed",0);
4114 #endif /* USE_CERT_CB */
4115 if (tn_deb || debses)
4116 tn_debug("[TLS - handshake starting]");
4118 printf("[TLS - handshake starting]\r\n");
4119 debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
4120 if (SSL_connect(tls_http_con) <= 0) {
4123 sprintf(errbuf,"[TLS - SSL_connect error: %s",
4124 ERR_error_string(ERR_get_error(),NULL));
4126 if (tn_deb || debses)
4128 else if ( ssl_debug_flag )
4129 printf("%s\r\n",errbuf);
4131 if (tn_deb || debses)
4132 tn_debug("[TLS - FAILED]");
4133 else if ( verbosity )
4134 printf("[TLS - FAILED]\r\n");
4135 debug(F110,"ck_ssl_http_client","[TLS - FAILED]",0);
4139 tls_http_active_flag = 1;
4140 if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
4142 char *subject = ssl_get_subject_name(tls_http_con);
4145 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
4147 if (tn_deb || debses)
4148 tn_debug("[TLS - FAILED]");
4149 else if ( verbosity )
4150 printf("[TLS - FAILED]\r\n");
4151 debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
4156 ok = uq_ok("Warning: Server didn't provide a certificate",
4157 "Continue? (Y/N)", 3, NULL, 0);
4159 if (tn_deb || debses)
4160 tn_debug("[TLS - FAILED]");
4161 else if ( verbosity )
4162 printf("[TLS - FAILED]\r\n");
4164 "ck_tn_tls_negotiate","[TLS - FAILED]",0);
4168 } else if (ssl_check_server_name(tls_http_con, hostname)) {
4169 if (tn_deb || debses)
4170 tn_debug("[TLS - FAILED]");
4171 else if ( verbosity )
4172 printf("[TLS - FAILED]\r\n");
4174 "ck_tn_tls_negotiate","[TLS - FAILED]",0);
4179 printf("[TLS - OK]\r\n");
4180 if (tn_deb || debses)
4181 tn_debug("[TLS - OK]");
4182 debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
4183 ssl_display_connect_details(tls_http_con,0,verbosity);
4186 return(0); /* success */
4190 ck_ssl_renegotiate_ciphers()
4193 if ( !ck_ssleay_is_installed() )
4199 if ( ssl_active_flag )
4200 return SSL_renegotiate(ssl_con);
4201 else if ( tls_active_flag )
4202 return SSL_renegotiate(tls_con);
4208 ck_X509_save_cert_to_user_store(X509 *cert)
4210 #ifdef X509V3_EXT_DUMP_UNKNOWN
4211 char path[CKMAXPATH];
4213 char * GetAppData(int);
4219 sprintf(hash,"%08lx",X509_subject_name_hash(cert));
4220 ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs/",
4224 out=BIO_new(BIO_s_file());
4227 ERR_print_errors(bio_err);
4230 if (BIO_write_filename(out,path) <= 0) {
4235 X509_print_ex(out, cert, XN_FLAG_SEP_MULTILINE, X509V3_EXT_DUMP_UNKNOWN);
4236 if (!PEM_write_bio_X509(out,cert)) {
4237 BIO_printf(bio_err,"unable to write certificate\n");
4238 ERR_print_errors(bio_err);
4244 #else /* X509V3_EXT_DUMP_UNKNOWN */
4246 #endif /* X509V3_EXT_DUMP_UNKNOWN */
4251 /* The following function should be replaced by institution specific */
4252 /* code that will convert an X509 cert structure to a userid for the */
4253 /* purposes of client to host login. The example code included */
4254 /* simply returns the UID field of the Subject if it exists. */
4256 /* X509_to_user() returns 0 if valid userid in 'userid', else -1 */
4258 X509_to_user(X509 *peer_cert, char *userid, int len)
4260 #ifdef X509_UID_TO_USER
4264 if (!(peer_cert && userid) || len <= 0)
4268 debug(F110,"X509_to_user() subject",
4269 X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
4271 /* userid is in cert subject /UID */
4272 err = X509_NAME_get_text_by_NID(X509_get_subject_name(peer_cert),
4273 #ifndef NID_x500UniqueIdentifier
4274 NID_uniqueIdentifier,
4276 NID_x500UniqueIdentifier,
4280 debug(F111,"X509_to_user() userid",userid,err);
4285 #else /* X509_UID_TO_USER */
4286 #ifdef X509_SUBJECT_ALT_NAME_TO_USER
4289 X509_EXTENSION *ext = NULL;
4290 STACK_OF(GENERAL_NAME) *ialt = NULL;
4291 GENERAL_NAME *gen = NULL;
4294 if (!(peer_cert && userid) || len <= 0)
4299 debug(F110,"X509_to_user() subject",
4300 X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
4302 if ((i = X509_get_ext_by_NID(peer_cert, NID_subject_alt_name, -1))<0)
4304 if (!(ext = X509_get_ext(peer_cert, i)))
4306 X509V3_add_standard_extensions();
4307 if (!(ialt = X509V3_EXT_d2i(ext)))
4309 for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
4310 gen = sk_GENERAL_NAME_value(ialt, i);
4311 if (gen->type == GEN_DNS) {
4312 if (!gen->d.ia5 || !gen->d.ia5->length)
4314 if (strlen(gen->d.ia5->data) != gen->d.ia5->length) {
4315 /* Ignoring IA5String containing null character */
4318 if ( gen->d.ia5->length + 1 > sizeof(email) ) {
4321 memcpy(email, gen->d.ia5->data, gen->d.ia5->length);
4322 email[gen->d.ia5->length] = 0;
4327 X509V3_EXT_cleanup();
4329 sk_GENERAL_NAME_free(ialt);
4331 debug(F110,"X509_to_user() email",email,0);
4334 char * domain = NULL;
4337 for ( i=0 ; email[i] ; i++ ) {
4338 if ( email[i] == '@' ) {
4340 domain = &email[i+1];
4346 /* XXX - Put code to Verify domain here */
4348 if ( /* domain is okay */ 1 )
4349 ckstrncpy(userid,email,len);
4353 return(userid[0] ? 0 : -1);
4355 #endif /* X509_SUBJECT_ALT_NAME_TO_USER */
4356 #endif /* X509_UID_TO_USER */
4360 /* The following function should be replaced by institution specific */
4361 /* code that will determine whether or not the combination of the */
4362 /* provided X509 certificate and username is valid for automatic */
4363 /* login. Whereas X509_to_user() is used to provide authentication */
4364 /* of the user, the X509_userok() function is used to provide */
4365 /* authorization. The certificate passed into X509_userok() does */
4366 /* need to map to a userid; nor would the userid it would map to */
4367 /* need to match the userid provided to the function. There are */
4368 /* numerous circumstances in which it is beneficial to have the ability */
4369 /* for multiple users to gain access to a common account such as */
4370 /* 'root' on Unix; or a class account on a web server. In Unix we */
4371 /* implement this capability with the ~userid/.tlslogin file which */
4372 /* a list of X509 certificates which may be used to access the */
4373 /* account 'userid'. */
4375 /* X509_to_user() returns 0 if access is denied; 1 is access is permitted */
4377 X509_userok(X509 * peer_cert, const char * userid)
4380 /* check if clients cert is in "user"'s ~/.tlslogin file */
4387 if ( peer_cert == NULL )
4390 if (!(pwd = getpwnam(userid)))
4392 if (strlen(pwd->pw_dir) > 500)
4394 sprintf(buf, "%s/.tlslogin", pwd->pw_dir);
4396 if (!(fp = fopen(buf, "r")))
4398 while (!r && (file_cert = PEM_read_X509(fp, NULL, NULL, NULL))) {
4399 if (!ASN1_STRING_cmp(peer_cert->signature, file_cert->signature))
4401 X509_free(file_cert);
4406 /* Need to implement an appropriate function for VMS */