home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku206.tar.gz / cku206.tar / ck_ssl.c < prev    next >
C/C++ Source or Header  |  2002-09-06  |  141KB  |  4,128 lines

  1. char *cksslv = "SSL/TLS support, 8.0.203,  9 Jul 2002";
  2. /*
  3.   C K _ S S L . C --  OpenSSL Interface for C-Kermit
  4.  
  5.   Copyright (C) 1985, 2002,
  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.
  9.  
  10.   Author:  Jeffrey E Altman (jaltman@columbia.edu)
  11.  
  12.   Provides:
  13.  
  14.   . Telnet Auth SSL option compatible with Tim Hudson's hack.
  15.   . Telnet START_TLS option
  16.   . Configuration of certificate and key files
  17.   . Certificate verification and revocation list checks
  18.   . Client certificate to user id routine
  19.  
  20.   Note: This code is written to be compatible with OpenSSL 0.9.6[abc].
  21.   It will also compile with version 0.9.5 although that is discouraged
  22.   due to security weaknesses in that release.   This code may not
  23.   compile with 0.9.7 development releases.
  24. */
  25.  
  26. #include "ckcsym.h"
  27. #include "ckcdeb.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifdef UNIX
  31. #include <netinet/in.h>
  32. #ifndef FREEBSD4
  33. #include <arpa/inet.h>
  34. #endif /* FREEBSD4 */
  35. #endif /* UNIX */
  36.  
  37. #ifdef CK_SSL
  38. static int ssl_installed = 1;
  39. #endif /* CK_SSL */
  40. int
  41. ck_ssh_is_installed()
  42. {
  43. #ifdef SSHBUILTIN
  44. #ifdef SSLDLL
  45. #ifdef NT
  46.     extern HINSTANCE hCRYPTO;
  47. #else /* NT */
  48.     extern HMODULE hCRYPTO;
  49. #endif /* NT */
  50.     debug(F111,"ck_ssh_is_installed","hCRYPTO",hCRYPTO);
  51.     return(ssl_installed && (hCRYPTO != NULL));
  52. #else /* SSLDLL */
  53.     return(ssl_installed);
  54. #endif /* SSLDLL */
  55. #else
  56.     return 0;
  57. #endif
  58. }
  59.  
  60. int
  61. ck_ssleay_is_installed()
  62. {
  63. #ifdef CK_SSL
  64. #ifdef SSLDLL
  65. #ifdef NT
  66.     extern HINSTANCE hSSL, hCRYPTO;
  67. #else /* NT */
  68.     extern HMODULE hSSL, hCRYPTO;
  69. #endif /* NT */
  70.     debug(F111,"ck_ssleay_is_installed","hSSL",hSSL);
  71.     debug(F111,"ck_ssleay_is_installed","hCRYPTO",hCRYPTO);
  72.     return(ssl_installed && (hSSL != NULL) && (hCRYPTO != NULL));
  73. #else /* SSLDLL */
  74.     return(ssl_installed);
  75. #endif /* SSLDLL */
  76. #else /* CK_SSL */
  77.     return(0);
  78. #endif /* CK_SSL */
  79. }
  80.  
  81. #ifdef CK_SSL
  82. #include "ckcker.h"
  83. #include "ckucmd.h"                             /* For struct keytab */
  84. #include "ckctel.h"
  85. #include "ck_ssl.h"
  86. #ifdef UNIX
  87. #include <pwd.h>                    /* Password file for home directory */
  88. #endif /* UNIX */
  89. #ifdef OS2
  90. #include <process.h>
  91. #endif /* OS2 */
  92. #ifdef OS2ONLY
  93. #include "ckotcp.h"
  94. #endif /* OS2ONLY */
  95.  
  96. #ifdef SSLDLL
  97. int ssl_finished_messages = 0;
  98. #else /* SSLDLL */
  99. #ifdef OPENSSL_VERSION_NUMBER
  100. int ssl_finished_messages = (OPENSSL_VERSION_NUMBER >= 0x0090581fL);
  101. #else
  102. !ERROR This module requires OpenSSL 0.9.5a or higher
  103. #endif /* OPENSSL_VERSION_NUMBER */
  104. #endif /* SSLDLL */
  105.  
  106. static int auth_ssl_valid = 0;
  107. static char *auth_ssl_name = 0;    /* this holds the oneline name */
  108. char ssl_err[SSL_ERR_BFSZ]="";
  109.  
  110. BIO *bio_err=NULL;
  111. X509_STORE *crl_store = NULL;
  112.  
  113. #ifndef NOFTP
  114. #ifndef SYSFTP
  115. SSL *ssl_ftp_con             = NULL;
  116. SSL_CTX *ssl_ftp_ctx         = NULL;
  117. SSL *ssl_ftp_data_con        = NULL;
  118. int ssl_ftp_active_flag      = 0;
  119. int ssl_ftp_data_active_flag = 0;
  120. #endif /* SYSFTP */
  121. #endif /* NOFTP */
  122.  
  123. #ifndef NOHTTP
  124. SSL *tls_http_con            = NULL;
  125. SSL_CTX *tls_http_ctx        = NULL;
  126. int tls_http_active_flag     = 0;
  127. int ssl_http_initialized = 0;
  128. #endif /* NOHTTP */
  129.  
  130. SSL_CTX *ssl_ctx = NULL;
  131. SSL *ssl_con = NULL;
  132. int ssl_debug_flag = 0;
  133. int ssl_verbose_flag = 0;
  134. int ssl_only_flag = 0;
  135. int ssl_active_flag = 0;
  136. int ssl_verify_flag = SSL_VERIFY_PEER;
  137. int ssl_certsok_flag = 0;
  138. char *ssl_rsa_cert_file = NULL;
  139. char *ssl_rsa_cert_chain_file = NULL;
  140. char *ssl_rsa_key_file = NULL;
  141. char *ssl_dsa_cert_file = NULL;
  142. char *ssl_dsa_cert_chain_file = NULL;
  143. char *ssl_dh_key_file = NULL;
  144. char *ssl_crl_file = NULL;
  145. char *ssl_crl_dir = NULL;
  146. char *ssl_verify_file = NULL;
  147. char *ssl_verify_dir = NULL;
  148. char *ssl_dh_param_file = NULL;
  149. char *ssl_cipher_list = NULL;
  150. char *ssl_rnd_file = NULL;
  151.  
  152. SSL_CTX *tls_ctx = NULL;
  153. SSL *tls_con = NULL;
  154. int tls_only_flag = 0;
  155. int tls_active_flag = 0;
  156.  
  157. int ssl_initialized = 0;
  158. int ssl_verify_depth = -1; /* used to track depth in verify routines */
  159.  
  160. /* compile this set to 1 to negotiate SSL/TLS but not actually start it */
  161. int ssl_dummy_flag=0;
  162.  
  163. extern int inserver;
  164. extern int debses;
  165. extern int accept_complete;
  166. extern char szHostName[], szUserNameRequested[], szUserNameAuthenticated[];
  167.  
  168. _PROTOTYP(int X509_to_user,(X509 *, char *, int));
  169.  
  170. int
  171. #ifdef CK_ANSIC
  172. ssl_server_verify_callback(int ok, X509_STORE_CTX * ctx)
  173. #else /* CK_ANSIC */
  174. ssl_server_verify_callback(ok, ctx)
  175. int ok;
  176. X509_STORE_CTX *ctx;
  177. #endif /* CK_ANSIC */
  178. {
  179.     static char *saved_subject=NULL;
  180.     char *subject=NULL, *issuer=NULL;
  181.     int depth,error;
  182.     X509 *xs = NULL;
  183.  
  184.     if ( ssl_certsok_flag )
  185.         return(1);
  186.  
  187.     error=X509_STORE_CTX_get_error(ctx);
  188.     depth=X509_STORE_CTX_get_error_depth(ctx);
  189.     xs=X509_STORE_CTX_get_current_cert(ctx);
  190.  
  191.     if (depth==0) {
  192.         /* clear things */
  193.         if (saved_subject!=NULL) {
  194.             free(saved_subject);
  195.             saved_subject=NULL;
  196.         }
  197.         if (auth_ssl_name!=NULL) {
  198.             free(auth_ssl_name);
  199.             auth_ssl_name=NULL;
  200.         }
  201.     }
  202.  
  203.  
  204.     if (ssl_debug_flag && !inserver) {
  205.         printf("ssl:server_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
  206.             depth,ok,error,X509_verify_cert_error_string(error));
  207.     }
  208.  
  209.     /* first thing is to have a meaningful name for the current
  210.      * certificate that is being verified ... and if we cannot
  211.      * determine that then something is seriously wrong!
  212.      */
  213.     makestr(&subject,
  214.             (char *)X509_NAME_oneline(X509_get_subject_name(xs),NULL,0));
  215.     makestr(&issuer,
  216.             (char *)X509_NAME_oneline(X509_get_issuer_name(xs),NULL,0));
  217.     if (!subject || !subject[0] || !issuer || !issuer[0]) {
  218.         ok = 0;
  219.         goto return_time;
  220.     }
  221.  
  222.     if (ssl_verbose_flag && !inserver && depth != ssl_verify_depth) {
  223.         printf("Certificate[%d] subject=%s\r\n",depth,subject);
  224.         printf("Certificate[%d] issuer =%s\r\n",depth,issuer);
  225.         ssl_verify_depth = depth;
  226.     }
  227.  
  228.     /* make sure that the certificate that has been presented */
  229.     /* has not been revoked (if we have been given a CRL.     */
  230.     ok =  ssl_verify_crl(ok, ctx);
  231.  
  232.     /* if we have any form of error in secure mode we reject the connection */
  233.     if (error!=X509_V_OK) {
  234.         if (inserver) {
  235. #ifdef CKSYSLOG
  236.             if (ckxsyslog >= SYSLG_LI && ckxlogging) {
  237.                 cksyslog(SYSLG_LI, 0,
  238.                           "X.509 Certificate verify failure",
  239.                           (char *) subject,
  240.                           (char *)X509_verify_cert_error_string(error)
  241.                           );
  242.             }
  243. #endif /* CKSYSLOG */
  244.  
  245.         } else {
  246.             if ( ssl_verify_flag &
  247.                  (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  248.                 printf("Error: ");
  249.             else
  250.                 printf("Warning: ");
  251.             switch (error) {
  252.             case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  253.                 printf("Certificate is self signed.\r\n");
  254.                 break;
  255.             case X509_V_ERR_CERT_HAS_EXPIRED:
  256.                 printf("Certificate has expired.\r\n");
  257.                 break;
  258.             case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  259.                 printf(
  260.   "Certificate issuer's certificate isn't available locally.\r\n");
  261.                 break;
  262.             case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  263.                 printf("Unable to verify leaf signature.\r\n");
  264.                 break;
  265.             case X509_V_ERR_CERT_REVOKED:
  266.                 printf("Certificate revoked.\r\n");
  267.                 break;
  268.             default:
  269.                 printf("Error %d while verifying certificate.\r\n",
  270.                        ctx->error);
  271.                 break;
  272.             }
  273.         }
  274.         ok = !(ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  275.     } else {
  276.         /* if we got all the way to the top of the tree then
  277.          * we *can* use this certificate for a username to
  278.          * match ... in all other cases we must not!
  279.          */
  280.         auth_ssl_name = saved_subject;
  281.         saved_subject = NULL;
  282.     }
  283.  
  284.   return_time:
  285.  
  286.     /* save the name if at least the first level is okay */
  287.     if (depth == 0 && ok)
  288.         makestr(&saved_subject,subject);
  289.  
  290.     /* clean up things */
  291.     if (subject!=NULL)
  292.         free(subject);
  293.     if (issuer!=NULL)
  294.         free(issuer);
  295.  
  296.     return ok;
  297. }
  298.  
  299. int
  300. #ifdef CK_ANSIC
  301. ssl_client_verify_callback(int ok, X509_STORE_CTX * ctx)
  302. #else
  303. ssl_client_verify_callback(ok, ctx)
  304. int ok;
  305. X509_STORE_CTX *ctx;
  306. #endif
  307. {
  308.     char subject[256]="", issuer[256]="";
  309.     int depth, error;
  310.     X509 *xs;
  311.  
  312.     xs=X509_STORE_CTX_get_current_cert(ctx);
  313.     error=X509_STORE_CTX_get_error(ctx);
  314.     depth=X509_STORE_CTX_get_error_depth(ctx);
  315.  
  316.     if ( ssl_debug_flag )
  317.         printf("ssl:client_verify_callback:depth=%d ok=%d err=%d-%s\r\n",
  318.                 depth,ok,error,X509_verify_cert_error_string(error));
  319.  
  320.     if ( ssl_certsok_flag ) {
  321.         ok = 1;
  322.     }
  323.  
  324.     /* first thing is to have a meaningful name for the current
  325.      * certificate that is being verified ... and if we cannot
  326.      * determine that then something is seriously wrong!
  327.      */
  328.     X509_NAME_oneline(X509_get_subject_name(xs),subject,256);
  329.     if (!subject[0]) {
  330.         int len;
  331.         ERR_print_errors(bio_err);
  332.         len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  333.         ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  334.         uq_ok("X.509 Subject Name unavailable\n", ssl_err, 1, NULL, 0);
  335.         ok=0;
  336.         goto return_time;
  337.     }
  338.  
  339.     X509_NAME_oneline(X509_get_issuer_name(xs),issuer,256);
  340.     if (!issuer[0]) {
  341.         int len;
  342.         ERR_print_errors(bio_err);
  343.         len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  344.         ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  345.         uq_ok("X.509 Issuer Name unavailable\n", ssl_err, 1, NULL, 0);
  346.         ok=0;
  347.         goto return_time;
  348.     }
  349.  
  350.     if (ssl_verbose_flag && depth != ssl_verify_depth) {
  351.         printf("Certificate[%d] subject=%s\r\n",depth,subject);
  352.         printf("Certificate[%d] issuer =%s\r\n",depth,issuer);
  353.         ssl_verify_depth = depth;
  354.     }
  355.  
  356.     ok = ssl_verify_crl(ok, ctx);
  357.  
  358.     if ( !ok ) {
  359.         char prefix[1024];
  360.         /* if the server is using a self signed certificate then
  361.          * we need to decide if that is good enough for us to
  362.          * accept ...
  363.          */
  364.  
  365.         switch ( error ) {
  366.         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: {
  367.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  368.                 /* make 100% sure that in secure more we drop the
  369.                  * connection if the server does not have a
  370.                  * real certificate!
  371.                  */
  372.                 ckmakxmsg(prefix,1024,
  373.                            "Error: Server has a self-signed certificate\n",
  374.                            "[",ckitoa(depth),"] subject=",subject,
  375.                            "[",ckitoa(depth),"] issuer=",issuer,
  376.                            "\n",NULL,NULL);
  377.  
  378.                 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  379.  
  380.                 /* sometimes it is really handy to be able to debug things
  381.                 * and still get a connection!
  382.                 */
  383.                 if (ssl_debug_flag) {
  384.                     printf("SSL: debug -> ignoring cert required!\r\n");
  385.                     ok=1;
  386.                 } else {
  387.                     ok=0;
  388.                 }
  389.                 goto return_time;
  390.             } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  391.                 ckmakxmsg(prefix,1024,
  392.                            "Warning: Server has a self-signed certificate\n",
  393.                            "[",ckitoa(depth),"] subject=",subject,
  394.                            "[",ckitoa(depth),"] issuer=",issuer,
  395.                            "\n",NULL,NULL);
  396.  
  397.                 ok = uq_ok(prefix,
  398.                            "Continue? (Y/N) ",
  399.                            3, NULL, 0);
  400.                 if ( ok < 0 )
  401.                     ok = 0;
  402.                 goto return_time;
  403.             }
  404.         }
  405.         case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  406.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  407.                 /* make 100% sure that in secure more we drop the
  408.                  * connection if the server does not have a
  409.                  * real certificate!
  410.                  */
  411.                 ckmakxmsg(prefix,1024,
  412.                            "Error: ",
  413.                            (char *)X509_verify_cert_error_string(error),
  414.                            "\r\n",
  415.                            "issuer=",issuer,
  416.                            "\n",NULL,NULL,NULL,NULL,NULL,NULL);
  417.                 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  418.  
  419.                 /* sometimes it is really handy to be able to debug things
  420.                 * and still get a connection!
  421.                 */
  422.                 if (ssl_debug_flag) {
  423.                     printf("SSL: debug -> ignoring cert required!\r\n");
  424.                     ok=1;
  425.                 } else {
  426.                     ok=0;
  427.                 }
  428.                 goto return_time;
  429.             } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  430.                 ckmakxmsg(prefix,1024,
  431.                            "Warning: ",
  432.                            (char *)X509_verify_cert_error_string(error),
  433.                            "\r\n",
  434.                            "issuer=",issuer,
  435.                            "\n",NULL,NULL,NULL,NULL,NULL,NULL);
  436.                 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  437.                 goto return_time;
  438.             }
  439.             break;
  440.         case X509_V_ERR_CERT_NOT_YET_VALID:
  441.         case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  442.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  443.                 int len;
  444.                 /* make 100% sure that in secure more we drop the
  445.                  * connection if the server does not have a
  446.                  * real certificate!
  447.                  */
  448.                 ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
  449.                 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  450.                 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  451.                 ckmakxmsg(prefix,1024,
  452.                            "Error: ",
  453.                            (char *)X509_verify_cert_error_string(error),
  454.                            "\r\n",
  455.                            "subject=",subject,
  456.                            "\r\nnotBefore=",ssl_err,
  457.                            "\r\n",NULL,NULL,NULL,NULL);
  458.                 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  459.                 /* sometimes it is really handy to be able to debug things
  460.                 * and still get a connection!
  461.                 */
  462.                 if (ssl_debug_flag) {
  463.                     printf("SSL: debug -> ignoring cert required!\r\n");
  464.                     ok=1;
  465.                 } else {
  466.                     ok=0;
  467.                 }
  468.                 goto return_time;
  469.             } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  470.                 int len;
  471.                 ASN1_TIME_print(bio_err,X509_get_notBefore(xs));
  472.                 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  473.                 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  474.                 ckmakxmsg(prefix,1024,
  475.                            "Warning: ",
  476.                            (char *)X509_verify_cert_error_string(error),
  477.                            "\r\n",
  478.                            "subject=",subject,
  479.                            "\r\nnotBefore=",ssl_err,
  480.                            "\r\n",NULL,NULL,NULL,NULL);
  481.                 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  482.             }
  483.             break;
  484.         case X509_V_ERR_CERT_HAS_EXPIRED:
  485.         case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  486.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  487.                 int len;
  488.                 /* make 100% sure that in secure more we drop the
  489.                  * connection if the server does not have a
  490.                  * real certificate!
  491.                  */
  492.                 ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
  493.                 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  494.                 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  495.  
  496.                 ckmakxmsg(prefix,1024,
  497.                            "Error: ",
  498.                            (char *)X509_verify_cert_error_string(error),
  499.                            "\r\n",
  500.                            "subject=",subject,
  501.                            "\r\nnotAfter=",ssl_err,
  502.                            "\r\n",NULL,NULL,NULL,NULL);
  503.                 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  504.    
  505.                 /* sometimes it is really handy to be able to debug things
  506.                 * and still get a connection!
  507.                 */
  508.                 if (ssl_debug_flag) {
  509.                     printf("SSL: debug -> ignoring cert required!\r\n");
  510.                     ok=1;
  511.                 } else {
  512.                     ok=0;
  513.                 }
  514.                 goto return_time;
  515.             } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  516.                 int len;
  517.                 ASN1_TIME_print(bio_err,X509_get_notAfter(xs));
  518.                 len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  519.                 ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  520.                 ckmakxmsg(prefix,1024,
  521.                            "Warning: ",
  522.                            (char *)X509_verify_cert_error_string(error),
  523.                            "\r\n",
  524.                            "subject=",subject,
  525.                            "\r\nnotAfter=",ssl_err,
  526.                            "\r\n",NULL,NULL,NULL,NULL);
  527.                 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  528.             }
  529.             break;
  530.         case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  531.         case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  532.             /*
  533.              * When an SSL server sends its certificates to the client there
  534.              * are two" conventions": one is to send the complete certificate
  535.              * chain and the other is to send the whole chain apart from the
  536.              * root.
  537.              *
  538.              * You don't usually need the root because the root is normally
  539.              * stored and trusted locally.
  540.              *
  541.              * So if you get the whole chain it will complain about the self
  542.              * signed certificate whereas if the root is missing it says it
  543.              * can't find the issuer certificate.
  544.              */
  545.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  546.                 /* make 100% sure that in secure more we drop the
  547.                  * connection if the server does not have a
  548.                  * real certificate!
  549.                  */
  550.                 ckmakxmsg(prefix,1024,
  551.                            "Error: ",
  552.                            (char *)X509_verify_cert_error_string(error),
  553.                            "\r\n",
  554.                            "issuer=",issuer,
  555.                            "\r\n",NULL,NULL,NULL,NULL,NULL,NULL);
  556.                 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  557.                 /* sometimes it is really handy to be able to debug things
  558.                 * and still get a connection!
  559.                 */
  560.                 if (ssl_debug_flag) {
  561.                     printf("SSL: debug -> ignoring cert required!\r\n");
  562.                     ok=1;
  563.                 } else {
  564.                     ok=0;
  565.                 }
  566.                 goto return_time;
  567.             } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  568.                 ckmakxmsg(prefix,1024,
  569.                            "Warning: ",
  570.                            (char *)X509_verify_cert_error_string(error),
  571.                            "\r\n",
  572.                            "issuer=",issuer,
  573.                            "\r\n",NULL,NULL,NULL,NULL,NULL,NULL);
  574.                 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  575.             }
  576.             break;
  577.         case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  578.         case X509_V_ERR_UNABLE_TO_GET_CRL:
  579.         case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  580.         case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  581.         case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  582.         case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  583.         case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  584.         case X509_V_ERR_CRL_NOT_YET_VALID:
  585.         case X509_V_ERR_CRL_HAS_EXPIRED:
  586.         case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  587.         case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  588.         case X509_V_ERR_OUT_OF_MEM:
  589.         case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  590.         case X509_V_ERR_CERT_REVOKED:
  591.         case X509_V_ERR_APPLICATION_VERIFICATION:
  592.         default:
  593.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  594.                 /* make 100% sure that in secure mode we drop the
  595.                  * connection if the server does not have a
  596.                  * real certificate!
  597.                  */
  598.                 ckmakxmsg(prefix,1024,
  599.                            "Error: ",
  600.                            (char *)X509_verify_cert_error_string(error),
  601.                            "\r\n",
  602.                            "subject=",subject,
  603.                            "\r\n",NULL,NULL,NULL,NULL,NULL,NULL);
  604.                 uq_ok(prefix, "Rejecting Connection", 1, NULL, 0);
  605.  
  606.                 /* sometimes it is really handy to be able to debug things
  607.                 * and still get a connection!
  608.                 */
  609.                 if (ssl_debug_flag) {
  610.                     printf("SSL: debug -> ignoring cert required!\r\n");
  611.                     ok=1;
  612.                 } else {
  613.                     ok=0;
  614.                 }
  615.                 goto return_time;
  616.             } else if (ssl_verify_flag != SSL_VERIFY_NONE) {
  617.                 ckmakxmsg(prefix,1024,
  618.                            "Warning: ",
  619.                            (char *)X509_verify_cert_error_string(error),
  620.                            "\r\n",
  621.                            "subject=",subject,
  622.                            "\r\n",NULL,NULL,NULL,NULL,NULL,NULL);
  623.                 ok = uq_ok(prefix, "Continue (Y/N)", 3, NULL, 0);
  624.             }
  625.             break;
  626.         }
  627.     }
  628.  
  629.   return_time:
  630.     if ( ssl_debug_flag )
  631.         printf("ssl:client_verify_callback => ok: %d\r\n",ok);
  632.     return ok;
  633. }
  634.  
  635. VOID
  636. #ifdef CK_ANSIC
  637. ssl_client_info_callback(const SSL *s, int where, int ret)
  638. #else
  639. ssl_client_info_callback(s,where,ret)
  640. const SSL *s;
  641. int where;
  642. int ret;
  643. #endif /* CK_ANSIC */
  644. {
  645.     if (inserver || !ssl_debug_flag)
  646.         return;
  647.  
  648.     switch ( where ) {
  649.     case SSL_CB_CONNECT_LOOP:
  650.         printf("SSL_connect:%s %s\r\n",
  651.                 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  652.         break;
  653.     case SSL_CB_CONNECT_EXIT:
  654.         if (ret == 0) {
  655.             printf("SSL_connect:failed in %s %s\r\n",
  656.                     SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  657.         } else if (ret < 0) {
  658.             printf("SSL_connect:error in %s %s\r\n",
  659.                     SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  660.         }
  661.         break;
  662.     case SSL_CB_ACCEPT_LOOP:
  663.         printf("SSL_accept:%s %s\r\n",
  664.                 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  665.         break;
  666.     case SSL_CB_ACCEPT_EXIT:
  667.         if (ret == 0) {
  668.             printf("SSL_accept:failed in %s %s\r\n",
  669.                     SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  670.         } else if (ret < 0) {
  671.             printf("SSL_accept:error in %s %s\r\n",
  672.                     SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  673.         }
  674.         break;
  675.     case SSL_CB_READ_ALERT:
  676.         printf("SSL_read_alert\r\n");
  677.         break;
  678.     case SSL_CB_WRITE_ALERT:
  679.         printf("SSL_write_alert\r\n");
  680.         break;
  681.     case SSL_CB_HANDSHAKE_START:
  682.         printf("SSL_handshake:%s %s\r\n",
  683.                 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  684.         break;
  685.     case SSL_CB_HANDSHAKE_DONE:
  686.         printf("SSL_handshake:%s %s\r\n",
  687.                 SSL_state_string((SSL *)s),SSL_state_string_long((SSL *)s));
  688.         break;
  689.     }
  690. }
  691.  
  692. #ifdef USE_CERT_CB
  693. /* Return 1, client cert is available */
  694. /* Return 0, no client cert is available */
  695. /* Return -1, callback must be called again. SSL_want_x509_lookup() == 1 */
  696. int
  697. #ifdef CK_ANSIC
  698. ssl_client_cert_callback(SSL * s, X509 ** x509, EVP_PKEY ** pkey)
  699. #else /* CK_ANSIC */
  700. ssl_client_cert_callback(s, x509, pkey)
  701.     SSL * s;
  702.     X509 ** x509;
  703.     EVP_PKEY ** pkey;
  704. #endif /* CK_ANSIC */
  705. {
  706.     if ( ssl_debug_flag ) {
  707.         const char * cipher_list=SSL_get_cipher(s);
  708.         printf("ssl_client_cert_callback called (%s)\r\n",
  709.                 cipher_list?cipher_list:"UNKNOWN");
  710.     }
  711. #ifdef COMMENT
  712.     if ( s == tls_con ) {
  713.         if (tls_load_certs(tls_cts,tls_con,0)) {
  714.             *x509 = SSL_get_certificate(s);
  715.             *pkey = SSL_get_privatekey(s);
  716.             return(1);
  717.         }
  718.     } else if ( s == ssl_con ) {
  719.         if (tls_load_certs(ssl_ctx,ssl_con,0)) {
  720.             *x509 = SSL_get_certificate(s);
  721.             *pkey = SSL_get_privatekey(s);
  722.             return(1);
  723.         }
  724.     }
  725.     return(0);
  726. #else /* COMMENT */
  727.     return(0);
  728. #endif /* COMMENT */
  729. }
  730. #endif /* USE_CERT_CB */
  731.  
  732. #ifndef MS_CALLBACK
  733. #define MS_CALLBACK
  734. #endif /* MS_CALLBACK */
  735.  
  736. static RSA MS_CALLBACK *
  737. #ifdef CK_ANSIC
  738. tmp_rsa_cb(SSL * s, int export, int keylength)
  739. #else /* CK_ANSIC */
  740. tmp_rsa_cb(s,export,keylength)
  741. SSL *s;
  742. int export;
  743. int keylength;
  744. #endif /* CK_ANSIC */
  745. {
  746.     static RSA *rsa_tmp=NULL;
  747.     extern int quiet;
  748.  
  749. #ifndef NO_RSA
  750.     if (rsa_tmp == NULL)
  751.     {
  752.         if (ssl_debug_flag)
  753.             printf("Generating temporary (%d bit) RSA key...\r\n",keylength);
  754.  
  755.         rsa_tmp=RSA_generate_key(keylength,RSA_F4,NULL,NULL);
  756.  
  757.         if (ssl_debug_flag)
  758.             printf("\r\n");
  759.     }
  760. #else /* NO_RSA */
  761.     if (ssl_debug_flag)
  762.         printf("Unable to generate temporary RSA key...\r\n");
  763. #endif
  764.     return(rsa_tmp);
  765. }
  766.  
  767.  
  768. #ifndef NO_DH
  769. static unsigned char dh512_p[]={
  770.     0xE9,0x4E,0x3A,0x64,0xFA,0x65,0x5F,0xA6,0x44,0xC7,0xFC,0xF1,
  771.     0x16,0x8B,0x11,0x11,0x7A,0xF0,0xB2,0x49,0x80,0x56,0xA3,0xF8,
  772.     0x0F,0x7D,0x01,0x68,0x5D,0xF6,0x8A,0xEA,0x8C,0xDD,0x01,0xDC,
  773.     0x43,0x18,0xE0,0xC4,0x89,0x80,0xE6,0x2D,0x44,0x77,0x45,0xFD,
  774.     0xBA,0xFC,0x43,0x35,0x12,0xC0,0xED,0x32,0xD3,0x16,0xEF,0x51,
  775.     0x09,0x44,0xA2,0xDB,
  776. };
  777. static unsigned char dh512_g[]={
  778.     0x05,
  779. };
  780.  
  781. static unsigned char dh768_p[]={
  782.     0x8B,0x2A,0x8C,0x6C,0x0F,0x87,0xC7,0x34,0xEE,0x2E,0xFB,0x60,
  783.     0x94,0xB3,0xBF,0x95,0xBA,0x84,0x74,0x86,0xEA,0xE0,0xA4,0x33,
  784.     0xE0,0x8F,0x7C,0x79,0x5C,0x62,0xE2,0x91,0xC5,0x6D,0x68,0xB9,
  785.     0x6C,0x5E,0x4E,0x94,0x0C,0x8E,0x56,0x8E,0xEB,0x98,0x7C,0x6E,
  786.     0x0E,0xF2,0xD5,0xAA,0x22,0x27,0x3F,0x0F,0xAF,0x10,0xB5,0x0B,
  787.     0x16,0xCC,0x05,0x27,0xBB,0x58,0x6D,0x61,0x4B,0x2B,0xAB,0xDC,
  788.     0x6A,0x15,0xBC,0x36,0x75,0x4D,0xEC,0xAB,0xFA,0xB6,0xE1,0xB1,
  789.     0x13,0x70,0xD8,0x77,0xCD,0x5E,0x51,0x77,0x81,0x0D,0x77,0x43,
  790. };
  791. static unsigned char dh768_g[]={
  792.     0x05,
  793. };
  794.  
  795. static unsigned char dh1024_p[]={
  796.     0xA4,0x75,0xCF,0x35,0x00,0xAF,0x3C,0x17,0xCE,0xB0,0xD0,0x52,
  797.     0x43,0xA0,0x0E,0xFA,0xA2,0xC9,0xBE,0x0B,0x76,0x7A,0xD9,0x2E,
  798.     0xF4,0x97,0xAC,0x02,0x24,0x69,0xF6,0x36,0x4F,0xAB,0xCC,0x43,
  799.     0xC1,0x74,0xFF,0xA3,0xD4,0x04,0x0F,0x11,0x2B,0x6D,0x8C,0x47,
  800.     0xC9,0xCF,0x40,0x93,0x9B,0x7D,0x1E,0x52,0x85,0xB2,0x17,0x55,
  801.     0x9C,0xF2,0x41,0x02,0x2A,0x9D,0x5F,0x24,0x22,0xC6,0x04,0xC4,
  802.     0xAB,0x92,0x6D,0xC7,0xC8,0xF3,0x41,0x58,0x6C,0x86,0xFD,0xB8,
  803.     0x0F,0x2D,0xDD,0xBF,0xA8,0x40,0x0C,0x58,0xC8,0xF2,0x3F,0x18,
  804.     0xEF,0xF1,0x93,0x3E,0xBA,0x16,0x41,0xBE,0x32,0x6C,0xC5,0x63,
  805.     0xFF,0x8A,0x02,0x3D,0xAC,0xD5,0x5A,0x49,0x64,0x34,0x14,0x2E,
  806.     0xFB,0x2E,0xE7,0x39,0x1A,0x0F,0x3C,0x33,
  807. };
  808. static unsigned char dh1024_g[]={
  809.     0x05,
  810. };
  811.  
  812. static unsigned char dh1536_p[]={
  813.     0xA3,0x2B,0x75,0x0E,0x7B,0x31,0x82,0xCA,0xF2,0xFC,0xF3,0x3D,
  814.     0xCE,0x5F,0xCD,0x5B,0x95,0xF6,0x2F,0xA4,0x5D,0x08,0x26,0xD2,
  815.     0x5F,0xC0,0x3F,0xC5,0xD8,0xA2,0xFE,0x83,0x26,0xBC,0xEB,0x7D,
  816.     0xF0,0x4E,0xD2,0xA6,0xBB,0x3C,0x88,0x63,0xCE,0x98,0xDE,0x08,
  817.     0xE2,0xE1,0xAF,0xE2,0x38,0xA8,0xFA,0x68,0x76,0x8D,0xBF,0xDF,
  818.     0xBB,0x30,0x15,0xFE,0xBD,0x22,0xCC,0x03,0x4E,0x5E,0x33,0xA3,
  819.     0x6D,0xD6,0x68,0x12,0x97,0x17,0x4B,0xB5,0x84,0x5F,0x5F,0xA3,
  820.     0x5C,0x2F,0xA4,0x10,0xC1,0xAD,0xBF,0xAC,0x30,0xCA,0x47,0x64,
  821.     0x63,0xFE,0xEE,0xEE,0xA1,0x64,0x73,0x70,0xAA,0xF9,0xFE,0xC6,
  822.     0xAD,0x5E,0xF6,0xF3,0x9C,0xDF,0x34,0x53,0x34,0x72,0xA6,0xA4,
  823.     0xBB,0x81,0x5A,0x43,0x41,0xFD,0x41,0x05,0x5B,0x77,0x7B,0x84,
  824.     0x03,0xFA,0x8A,0xFA,0xF7,0x8E,0x0F,0xCB,0x51,0xA2,0xB8,0x45,
  825.     0xFF,0x59,0x42,0xEF,0xCF,0xF6,0x25,0x37,0xE2,0x6D,0xFF,0x69,
  826.     0x11,0xF5,0x77,0x59,0x79,0x1C,0x5F,0x05,0xFC,0x7A,0x65,0x81,
  827.     0x03,0x4A,0x78,0xC6,0xE9,0x48,0x73,0xF6,0x10,0xBC,0x99,0x1C,
  828.     0xEE,0x44,0x2F,0x8B,0x70,0xCA,0xA8,0xB6,0x02,0x83,0x3E,0x0B,
  829. };
  830. static unsigned char dh1536_g[]={
  831.     0x05,
  832. };
  833.  
  834. static unsigned char dh2048_p[]={
  835.     0xFA,0x4E,0xE4,0x3B,0xFA,0xC1,0x87,0xDD,0xE7,0xC6,0x8B,0xE6,
  836.     0x13,0x85,0xBC,0x9B,0x2B,0x8B,0x5B,0x46,0xBB,0x8B,0x86,0x6D,
  837.     0xD7,0xB6,0xD5,0x49,0xC5,0x54,0xF2,0x3E,0xD2,0x39,0x64,0x9B,
  838.     0x0E,0x33,0x39,0x8F,0xFA,0xFA,0xD9,0x78,0xED,0x34,0x82,0x29,
  839.     0x37,0x58,0x4D,0x5D,0x40,0xCB,0x69,0xE3,0x8A,0x9F,0x17,0x0C,
  840.     0x01,0x23,0x6B,0x05,0x01,0xAF,0x33,0xDE,0xDF,0x1A,0xBB,0x7B,
  841.     0x6A,0x9F,0xD8,0xED,0x8D,0x5E,0x44,0x19,0x5B,0xE0,0xB6,0x23,
  842.     0xF9,0x7A,0x96,0x6E,0x94,0x33,0x31,0x49,0xBA,0x84,0xD5,0x12,
  843.     0xD7,0x6D,0xDC,0x35,0x54,0x64,0xA3,0xD8,0x04,0x26,0xC5,0xAF,
  844.     0x7F,0xE3,0xFE,0x6F,0xBE,0xD5,0x17,0x72,0x4B,0xA6,0xD0,0xA7,
  845.     0x5F,0x18,0xF5,0xF0,0x2D,0x11,0x9A,0xF6,0xD5,0x3B,0x6C,0x61,
  846.     0x3C,0x6F,0x8E,0x09,0x4F,0x2C,0xE1,0x26,0x06,0x51,0xB3,0x19,
  847.     0x85,0x85,0x13,0xF9,0xC2,0x6E,0x80,0x28,0x9E,0x8A,0xA0,0x01,
  848.     0x46,0xD1,0x85,0x44,0x8C,0xE6,0xEE,0x7E,0x1E,0x17,0x3D,0xBA,
  849.     0x54,0xFF,0xE8,0x0E,0xDD,0x51,0xF3,0x74,0x7F,0x0D,0x0B,0xAB,
  850.     0xCA,0x84,0x8D,0x24,0x5D,0x56,0xD4,0x47,0x02,0xFC,0x93,0x9F,
  851.     0xAE,0x9B,0x5C,0xDB,0x63,0xEB,0x65,0x01,0x38,0xC2,0x7B,0x30,
  852.     0x1E,0x17,0x1C,0x75,0xF5,0x16,0x3B,0x4F,0x5F,0x41,0x32,0xB5,
  853.     0xFF,0x9E,0x61,0xFD,0xD2,0x62,0x6E,0xFD,0x8A,0x28,0x93,0x59,
  854.     0x2D,0x70,0x14,0x4D,0xE1,0x86,0xD5,0x90,0xB4,0xDF,0x72,0x71,
  855.     0xE0,0xB4,0xD0,0xD6,0x82,0x3A,0x4A,0x04,0x58,0x32,0x0B,0xD3,
  856.     0x51,0x13,0x32,0x63,
  857. };
  858. static unsigned char dh2048_g[]={
  859.     0x02,
  860. };
  861.  
  862. static DH *
  863. get_dh512()
  864. {
  865.     DH *dh=NULL;
  866.  
  867.     if ((dh=DH_new()) == NULL)
  868.         return(NULL);
  869.     dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  870.     dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  871.     if ((dh->p == NULL) || (dh->g == NULL))
  872.         return(NULL);
  873.     return(dh);
  874. }
  875.  
  876. static DH *
  877. get_dh768()
  878. {
  879.     DH *dh=NULL;
  880.  
  881.     if ((dh=DH_new()) == NULL)
  882.         return(NULL);
  883.     dh->p=BN_bin2bn(dh768_p,sizeof(dh768_p),NULL);
  884.     dh->g=BN_bin2bn(dh768_g,sizeof(dh768_g),NULL);
  885.     if ((dh->p == NULL) || (dh->g == NULL))
  886.         return(NULL);
  887.     return(dh);
  888. }
  889.  
  890. static DH *
  891. get_dh1024()
  892. {
  893.     DH *dh=NULL;
  894.  
  895.     if ((dh=DH_new()) == NULL)
  896.         return(NULL);
  897.     dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  898.     dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  899.     if ((dh->p == NULL) || (dh->g == NULL))
  900.         return(NULL);
  901.     return(dh);
  902. }
  903.  
  904. static DH *
  905. get_dh1536()
  906. {
  907.     DH *dh=NULL;
  908.  
  909.     if ((dh=DH_new()) == NULL)
  910.         return(NULL);
  911.     dh->p=BN_bin2bn(dh1536_p,sizeof(dh1536_p),NULL);
  912.     dh->g=BN_bin2bn(dh1536_g,sizeof(dh1536_g),NULL);
  913.     if ((dh->p == NULL) || (dh->g == NULL))
  914.         return(NULL);
  915.     return(dh);
  916. }
  917.  
  918. static DH *
  919. get_dh2048()
  920. {
  921.     DH *dh=NULL;
  922.  
  923.     if ((dh=DH_new()) == NULL)
  924.         return(NULL);
  925.     dh->p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);
  926.     dh->g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
  927.     if ((dh->p == NULL) || (dh->g == NULL))
  928.         return(NULL);
  929.     return(dh);
  930. }
  931. #endif /* NO_DH */
  932.  
  933. static DH MS_CALLBACK *
  934. #ifdef CK_ANSIC
  935. tmp_dh_cb(SSL * s, int export, int keylength)
  936. #else /* CK_ANSIC */
  937. tmp_dh_cb(s,export,keylength)
  938. SSL *s;
  939. int export;
  940. int keylength;
  941. #endif /* CK_ANSIC */
  942. {
  943.     static DH *dh_tmp=NULL;
  944.     BIO *bio=NULL;
  945.     extern int quiet;
  946.  
  947. #ifndef NO_DH
  948.     if (dh_tmp == NULL)
  949.     {
  950.         if (ssl_dh_param_file  &&
  951.              (bio=BIO_new_file(ssl_dh_param_file,"r")) != NULL)
  952.             dh_tmp=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
  953.         if (bio != NULL)
  954.             BIO_free(bio);
  955.  
  956.         if ( dh_tmp == NULL ) {
  957.             if ( keylength < 768 )
  958.                 dh_tmp = get_dh512();
  959.             else if ( keylength < 1024 )
  960.                 dh_tmp = get_dh768();
  961.             else if ( keylength < 1536 )
  962.                 dh_tmp = get_dh1024();
  963.             else if ( keylength < 2048 )
  964.                 dh_tmp = get_dh1536();
  965.             else
  966.                 dh_tmp = get_dh2048();
  967.         }
  968.     }
  969. #else /* NO_DH */
  970.     if (ssl_debug_flag)
  971.         printf("DH not supported...\r\n");
  972. #endif /* NO_DH */
  973.     return(dh_tmp);
  974. }
  975.  
  976. static void
  977. ssl_display_comp(SSL * ssl)
  978. {
  979.     if ( !ck_ssleay_is_installed() )
  980.         return;
  981.  
  982.     if (ssl == NULL)
  983.         return;
  984.  
  985.     if (ssl->expand == NULL || ssl->expand->meth == NULL)
  986.         printf("Compression: None\r\n");
  987.     else {
  988.         printf("Compression: %s\r\n",ssl->expand->meth->name);
  989.     }
  990. }
  991.  
  992. int
  993. #ifdef CK_ANSIC
  994. ssl_display_connect_details(SSL * ssl_con, int server, int verbose)
  995. #else /* CK_ANSIC */
  996. ssl_display_connect_details(ssl_con,server,verbose)
  997. SSL *ssl_con;
  998. int server;
  999. int verbose;
  1000. #endif /* CK_ANSIC */
  1001. {
  1002.     X509 *peer;
  1003.     SSL_CIPHER * cipher;
  1004.     const char *cipher_list;
  1005.     char buf[512]="";
  1006.  
  1007.     if ( !ck_ssleay_is_installed() )
  1008.         return(0);
  1009.  
  1010.     if ( inserver && !tn_deb )
  1011.         return(0);
  1012.  
  1013.     /* the cipher list *can* be NULL ... useless but it happens! */
  1014.     cipher = SSL_get_current_cipher(ssl_con);
  1015.     cipher_list = SSL_CIPHER_get_name(cipher);
  1016.     SSL_CIPHER_description(cipher,buf,sizeof(buf));
  1017.     if (cipher_list==NULL)
  1018.         cipher_list="<NULL>";
  1019.     printf("[TLS - %s",buf);
  1020.     ssl_display_comp(ssl_con);
  1021.  
  1022.     if ( server ) {
  1023.         cipher_list=SSL_get_shared_ciphers(ssl_con,buf,512);
  1024.         if (cipher_list==NULL)
  1025.             cipher_list="<NULL>";
  1026.         printf("[TLS - shared ciphers=%s]\r\n",
  1027.                 cipher_list);
  1028.         }       
  1029.     if ( server || tn_deb ) {
  1030.         peer=SSL_get_peer_certificate(ssl_con);
  1031.         if (peer != NULL) {
  1032.             X509_NAME_oneline(X509_get_subject_name(peer),buf,512);
  1033.             printf("[TLS - subject=%s]\r\n",buf);
  1034.             X509_NAME_oneline(X509_get_issuer_name(peer),buf,512);
  1035.             printf("[TLS - issuer=%s]\r\n",buf);
  1036.             /* X509_free(peer); */
  1037.         } else if (!tls_is_krb5(0)) {
  1038.             if ( !sstelnet && !tcp_incoming ) {
  1039.                 printf("[TLS - No certificate provided.]\r\n");
  1040.                 printf(
  1041.      "[TLS - The identity of the host could not be verified.]\r\n");
  1042.             }
  1043.         }
  1044.     }
  1045.     return(0);
  1046. }
  1047.  
  1048. /*
  1049.  * Use SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *, void * userdata)
  1050.  * to set the value of the userdata.  We are going to use it to store the
  1051.  * prompt.
  1052.  */
  1053.  
  1054. int
  1055. #ifdef CK_ANSIC
  1056. ssl_passwd_callback(char *buf, int len, int rwflag, VOID * userdata)
  1057. #else /* CK_ANSIC */
  1058. ssl_passwd_callback(buf,len,w)
  1059.     char * buf; int len; int rwflag; VOID *userdata;
  1060. #endif /* CK_ANSIC */
  1061. {
  1062.     extern char pwbuf[];
  1063.     extern int  pwflg, pwcrypt;
  1064.     int   ok;
  1065.     char *prompt=NULL;
  1066.  
  1067.     if ( pwbuf[0] && pwflg ) {
  1068.         int n;
  1069.         n = ckstrncpy(buf,pwbuf,len);
  1070. #ifdef OS2
  1071.         if ( pwcrypt )
  1072.             ck_encrypt((char *)buf);
  1073. #endif /* OS2 */
  1074.         return(n);
  1075.     }
  1076.  
  1077.     if ( userdata == NULL )
  1078.         prompt="Enter pass phrase: ";
  1079.     else
  1080.         prompt=(char*)userdata;
  1081.     ok = uq_txt(NULL,prompt,1,NULL,buf,len,NULL);
  1082.     return(ok > 0 ? strlen(buf) : 0);
  1083. }
  1084.  
  1085.  
  1086. /* Attempts to load certificate data into the TLS context structures */
  1087. /* Returns 1 on success; 0 on failure */
  1088. int
  1089. tls_load_certs(SSL_CTX * ctx, SSL * con, int server)
  1090. {
  1091.     int rc = 1;
  1092.     extern int quiet;
  1093.  
  1094.     if ( !ck_ssleay_is_installed() )
  1095.         return(0);
  1096.  
  1097.     debug(F111,"tls_load_certs","SSL_CTX",ctx);
  1098.     debug(F111,"tls_load_certs","SSL",con);
  1099.     debug(F111,"tls_load_certs","server",server);
  1100.  
  1101.     if ( con ) {
  1102.         if (ssl_rsa_cert_file) {
  1103.             if ( ssl_debug_flag )
  1104.                 printf("Loading RSA certificate into SSL\r\n");
  1105.  
  1106.             rc = SSL_use_certificate_file(con, ssl_rsa_cert_file,
  1107.                                                X509_FILETYPE_PEM);
  1108.             if (!rc)
  1109.             {
  1110.                 if ( !quiet || ssl_debug_flag )
  1111.                     printf("Error loading certificate from %s\r\n",
  1112.                             ssl_rsa_cert_file);
  1113.             } else {
  1114.                 if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
  1115.                     makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
  1116.  
  1117.                 rc = SSL_use_PrivateKey_file(con, ssl_rsa_key_file,
  1118.                                                   X509_FILETYPE_PEM);
  1119.                 if (!rc)
  1120.                     rc = SSL_use_PrivateKey_file(con, ssl_rsa_cert_file,
  1121.                                                       X509_FILETYPE_PEM);
  1122.                 if (!rc)
  1123.                 {
  1124.                     if ( !quiet || ssl_debug_flag )
  1125.                         printf("Error loading key from %s\r\n",
  1126.                                 ssl_rsa_key_file);
  1127.                 } else {
  1128.                     rc = SSL_check_private_key(con);
  1129.                     if (!rc)
  1130.                     {
  1131.                         if ( ssl_debug_flag )
  1132.                             printf(
  1133.                 "Private key does not match the certificate public key\r\n");
  1134.                     }
  1135.                 }
  1136.             }
  1137.         }
  1138.  
  1139.         if (ssl_dsa_cert_file) {
  1140.             if ( ssl_debug_flag )
  1141.                 printf("Loading DSA certificate into SSL\r\n");
  1142.  
  1143.             rc = SSL_use_certificate_file(con, ssl_dsa_cert_file,
  1144.                                                X509_FILETYPE_PEM);
  1145.             if (!rc)
  1146.             {
  1147.                 if ( ssl_debug_flag ) {
  1148.                     printf("Error loading certificate from %s\r\n",
  1149.                             ssl_dsa_cert_file);
  1150.                 }
  1151.             } else {
  1152.                 if (!ssl_dh_key_file || !ssl_dh_key_file[0])
  1153.                     makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
  1154.                 rc = SSL_use_PrivateKey_file(con, ssl_dh_key_file,
  1155.                                                   X509_FILETYPE_PEM);
  1156.                 if (!rc)
  1157.                     rc = SSL_use_PrivateKey_file(con, ssl_dsa_cert_file,
  1158.                                                       X509_FILETYPE_PEM);
  1159.                 if (!rc)
  1160.                 {
  1161.                     if ( !quiet || ssl_debug_flag ) {
  1162.                         printf("Error loading key from %s\r\n",
  1163.                                 ssl_dh_key_file);
  1164.                     }
  1165.                 } else {
  1166.                     rc = SSL_check_private_key(con);
  1167.                     if (!rc)
  1168.                     {
  1169.                         if ( ssl_debug_flag )
  1170.                             printf(
  1171.                    "Private key does not match the certificate public key\n");
  1172.                     }
  1173.                 }
  1174.             }
  1175.         }
  1176.     } else {
  1177.         if (ssl_rsa_cert_file) {
  1178.             if ( ssl_debug_flag )
  1179.                 printf("Loading RSA certificate into SSL\r\n");
  1180.  
  1181.             rc = SSL_CTX_use_certificate_file(ctx, ssl_rsa_cert_file,
  1182.                                        X509_FILETYPE_PEM);
  1183.             if (!rc)
  1184.             {
  1185.                 if ( !quiet || ssl_debug_flag )
  1186.                     printf("Error loading certificate from %s\r\n",
  1187.                             ssl_rsa_cert_file);
  1188.             } else {
  1189.                 if (!ssl_rsa_key_file || !ssl_rsa_key_file[0])
  1190.                     makestr(&ssl_rsa_key_file,ssl_rsa_cert_file);
  1191.  
  1192.                 rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_key_file,
  1193.                                                   X509_FILETYPE_PEM);
  1194.                 if (!rc)
  1195.                   rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_rsa_cert_file,
  1196.                                                    X509_FILETYPE_PEM);
  1197.                 if (!rc) {
  1198.                     if ( ssl_debug_flag )
  1199.                       printf("Error loading key from %s\r\n",ssl_rsa_key_file);
  1200.                 } else {
  1201.                     rc = SSL_CTX_check_private_key(ctx);
  1202.                     if (!rc) {
  1203.                         if ( ssl_debug_flag )
  1204.                           printf(
  1205.                 "Private key does not match the certificate public key\r\n");
  1206.                     }
  1207.                 }
  1208.             }
  1209.         }
  1210.         if (ssl_dsa_cert_file) {
  1211.             if ( ssl_debug_flag )
  1212.               printf("Loading DSA certificate into SSL\r\n");
  1213.  
  1214.             rc = SSL_CTX_use_certificate_file(ctx, ssl_dsa_cert_file,
  1215.                                               X509_FILETYPE_PEM);
  1216.             if (!rc) {
  1217.                 if ( ssl_debug_flag ) {
  1218.                     printf("Error loading certificate from %s\r\n",
  1219.                            ssl_dsa_cert_file);
  1220.                 }
  1221.             } else {
  1222.                 if (!ssl_dh_key_file || !ssl_dh_key_file[0])
  1223.                     makestr(&ssl_dh_key_file,ssl_dsa_cert_file);
  1224.                 rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dh_key_file,
  1225.                                                   X509_FILETYPE_PEM);
  1226.                 if (!rc)
  1227.                   rc = SSL_CTX_use_PrivateKey_file(ctx, ssl_dsa_cert_file,
  1228.                                                       X509_FILETYPE_PEM);
  1229.                 if (!rc) {
  1230.                     if ( ssl_debug_flag )
  1231.                       printf("Error loading key from %s\r\n",ssl_dh_key_file);
  1232.                 } else {
  1233.                     rc = SSL_CTX_check_private_key(ctx);
  1234.                     if (!rc) {
  1235.                         if ( ssl_debug_flag )
  1236.                           printf(
  1237.                    "Private key does not match the certificate public key\n");
  1238.                     }
  1239.                 }
  1240.             }
  1241.         }
  1242.     }
  1243.  
  1244.     if (ssl_rsa_cert_chain_file && server) {
  1245.         int skip1st = 0;
  1246.         if (ssl_debug_flag)
  1247.             printf("Loading RSA Certificate Chain into SSL\r\n");
  1248.         if (!ckstrcmp(ssl_rsa_cert_chain_file,ssl_rsa_cert_file,-1,
  1249. #ifdef OS2
  1250.                        0
  1251. #else
  1252.                        1
  1253. #endif /* OS2 */
  1254.                        ))
  1255.             skip1st = 1;
  1256.         rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_rsa_cert_chain_file);
  1257.         if (!rc && ssl_debug_flag)
  1258.                 printf("Error loading RSA Certificate Chain into SSL\r\n");
  1259.     }
  1260.     if (ssl_dsa_cert_chain_file && server) {
  1261.         int skip1st = 0;
  1262.         if (ssl_debug_flag)
  1263.             printf("Loading DSA Certificate Chain into SSL\r\n");
  1264.         if (!ckstrcmp(ssl_dsa_cert_chain_file,ssl_dsa_cert_file,-1,
  1265. #ifdef OS2
  1266.                        0
  1267. #else
  1268.                        1
  1269. #endif /* OS2 */
  1270.                        ))
  1271.             skip1st = 1;
  1272.         rc = SSL_CTX_use_certificate_chain_file(ctx,ssl_dsa_cert_chain_file);
  1273.         if (!rc && ssl_debug_flag)
  1274.                 printf("Error loading DSA Certificate Chain into SSL\r\n");
  1275.     }
  1276.     return(rc);
  1277. }
  1278.  
  1279. VOID
  1280. #ifdef CK_ANSIC
  1281. ssl_once_init()
  1282. #else
  1283. ssl_once_init()
  1284. #endif /* CK_ANSIC */
  1285. {
  1286.     COMP_METHOD * cm;
  1287.  
  1288.     if ( !ck_ssleay_is_installed() )
  1289.         return;
  1290.  
  1291.     debug(F111,"Kermit built for OpenSSL",OPENSSL_VERSION_TEXT,SSLEAY_VERSION_NUMBER);
  1292.     debug(F111,"OpenSSL Library",SSLeay_version(SSLEAY_VERSION),
  1293.            SSLeay());
  1294.     debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_BUILT_ON),0);
  1295.     debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_CFLAGS),0);
  1296.     debug(F110,"OpenSSL Library",SSLeay_version(SSLEAY_PLATFORM),0);
  1297.  
  1298.     /* The following test is suggested by Richard Levitte */
  1299.     if (((OPENSSL_VERSION_NUMBER ^ SSLeay()) & 0xffffff0f) 
  1300. #ifdef OS2
  1301.          || ckstrcmp(OPENSSL_VERSION_TEXT,(char *)SSLeay_version(SSLEAY_VERSION),-1,1)
  1302. #endif /* OS2 */
  1303.          ) {
  1304.         ssl_installed = 0;
  1305.         debug(F111,"OpenSSL Version does not match.  Built with",
  1306.                SSLeay_version(SSLEAY_VERSION),SSLEAY_VERSION_NUMBER);
  1307.         printf("?OpenSSL libraries do not match required version.");
  1308.         printf("  SSL\\TLS support disabled\r\n\r\n");
  1309.         bleep(BP_FAIL);
  1310. #ifdef SSLDLL
  1311.         ck_ssl_unloaddll();
  1312.         ck_crypto_unloaddll();
  1313. #endif /* SSLDLL */
  1314.         return;
  1315.     }
  1316.  
  1317.     /* init things so we will get meaningful error messages
  1318.     * rather than numbers
  1319.     */
  1320.     SSL_load_error_strings();
  1321.  
  1322.     /* SSL_library_init() only loads those ciphers needs for SSL  */
  1323.     /* These happen to be a similar set to those requires for SSH */
  1324.     /* but they are not a complete set of ciphers provided by the */
  1325.     /* crypto library.                                            */
  1326.     SSL_library_init();
  1327.  
  1328. #ifdef ZLIB
  1329.     cm = COMP_zlib();
  1330.     if (cm != NULL && cm->type != NID_undef) {
  1331.         SSL_COMP_add_compression_method(0xe0, cm); /* EAY's ZLIB ID */
  1332.     }
  1333. #endif /* ZLIB */
  1334.     cm = COMP_rle();
  1335.     if (cm != NULL && cm->type != NID_undef)
  1336.         SSL_COMP_add_compression_method(0xe1, cm); /* EAY's RLE ID */
  1337.  
  1338.     /* Ensure the Random number generator has enough entropy */
  1339.     if ( !RAND_status() ) {
  1340.         char buffer[256]="";
  1341.         char randombytes[256];
  1342.         int rc1 = -1, rc2 = 1;  /* assume failure and success */
  1343.  
  1344.         debug(F110,"ssl_once_init","!RAND_status()",0);
  1345.  
  1346.         if ( ssl_rnd_file == NULL ) {
  1347.             debug(F110,"ssl_rnd_file","ssl_rnd_file is NULL",0);
  1348.             RAND_file_name(buffer,256);
  1349.             if ( buffer[0] )
  1350.                 makestr(&ssl_rnd_file, buffer);
  1351.             else
  1352.                 makestr(&ssl_rnd_file,".rnd");
  1353.         }
  1354.         debug(F110,"ssl_rnd_file",ssl_rnd_file,0);
  1355.  
  1356.         rc1 = RAND_egd(ssl_rnd_file);
  1357.         debug(F111,"ssl_once_init","RAND_egd()",rc1);
  1358.         if ( rc1 <= 0 ) {
  1359.             rc2 = RAND_load_file(ssl_rnd_file, -1);
  1360.             debug(F111,"ssl_once_init","RAND_load_file()",rc1);
  1361.         }
  1362.  
  1363.         if ( rc1 <= 0 && !rc2 )
  1364.         {
  1365.             time_t t = time(NULL);
  1366.             int tlen = sizeof(time_t);
  1367.             int pid = getpid();
  1368.             int plen = sizeof(int);
  1369.             int n;
  1370. #ifndef RAND_MAX
  1371. #define RAND_MAX 0x7FFF
  1372. #endif
  1373.             debug(F110,"ssl_once_init","calling RAND_seed()",0);
  1374.  
  1375.             RAND_seed((unsigned char *)&t, tlen);
  1376.             RAND_seed((unsigned char *)&pid, plen);
  1377.  
  1378.             srand((unsigned int)t);
  1379.             sprintf(buffer, "%.0f", (((double)(rand()%RAND_MAX)/RAND_MAX)*
  1380.                                       (sizeof(randombytes)-128-1)));
  1381.             n = (atoi(buffer)+1)%(sizeof(randombytes)-128-1);
  1382.             RAND_seed(randombytes, 128);
  1383.         }
  1384.  
  1385.         if ( !RAND_status() ) {
  1386.             debug(F110,"ssl_once_init","Unable to initialize PRNG",0);
  1387.             printf(" Unable to load 'random state'\n");
  1388.             printf(" SSL and TLS are unavailble.\n");
  1389.             printf(" Use SET AUTH SSL RANDOM-FILE <file> command to provide random data.\n");
  1390.             printf(" Specified file will be overwritten with new random data after use.\n");
  1391.             return;
  1392.         }
  1393.  
  1394.         if ( ssl_rnd_file ) {
  1395.             int rc = RAND_write_file(ssl_rnd_file);
  1396.             debug(F111,"ssl_once_init","RAND_write_file()",rc);
  1397.         }
  1398.     }
  1399.     debug(F100,"ssl_once_init() complete","",0);
  1400. }
  1401.  
  1402. int
  1403. #ifdef CK_ANSIC
  1404. ssl_tn_init(int mode)
  1405. #else
  1406. ssl_tn_init(mode) int mode;
  1407. #endif /* CK_ANSIC */
  1408. {
  1409. #ifdef KRB5
  1410.     extern char * k5_keytab;
  1411.     extern char * krb5_d_srv;
  1412. #endif /* KRB5 */
  1413.     static int last_ssl_mode = -1;
  1414.     SSL * ssl_conx=NULL, * tls_conx=NULL;
  1415.  
  1416.     ssl_initialized = 0;
  1417.  
  1418.     if ( !ck_ssleay_is_installed() )
  1419.         return(0);
  1420.  
  1421.     debug(F111,"ssl_tn_init","mode",mode);
  1422.  
  1423.     /* make sure we have somewhere we can log errors to */
  1424.     if (bio_err == NULL)
  1425.         bio_err=BIO_new(BIO_s_mem());
  1426.  
  1427.     if (ssl_debug_flag)
  1428.         printf("SSL_DEBUG_FLAG on\r\n");
  1429.  
  1430.     if (last_ssl_mode != mode) {
  1431.         if (ssl_ctx) {
  1432.             SSL_CTX_free(ssl_ctx);
  1433.             ssl_ctx = NULL;
  1434.         }
  1435.         if (tls_ctx) {
  1436.             SSL_CTX_free(tls_ctx);
  1437.             tls_ctx = NULL;
  1438.         }
  1439.     }
  1440.  
  1441.     if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
  1442.         if ( mode == SSL_CLIENT ) {
  1443.             ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
  1444.             /* This can fail because we do not have RSA available */
  1445.             if ( !ssl_ctx ) {
  1446.                 debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
  1447.                 ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
  1448.             }
  1449.             if ( !ssl_ctx ) {
  1450.                 debug(F110,"ssl_tn_init","SSLv3_client_method failed",0);
  1451.                 last_ssl_mode = -1;
  1452.                 return(0);
  1453.             }
  1454. #ifndef COMMENT
  1455.             tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_client_method());
  1456. #else /* COMMENT */
  1457.             tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
  1458.             /* This can fail because we do not have RSA available */
  1459.             if ( !tls_ctx ) {
  1460.                 debug(F110,"ssl_tn_init","SSLv23_client_method failed",0);
  1461.                 tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
  1462.             }
  1463. #endif /* COMMENT */
  1464.             if ( !tls_ctx ) {
  1465.                 debug(F110,"ssl_tn_init","TLSv1_client_method failed",0);
  1466.                 last_ssl_mode = -1;
  1467.                 return(0);
  1468.             }
  1469. #ifdef USE_CERT_CB
  1470.             SSL_CTX_set_client_cert_cb(ssl_ctx,ssl_client_cert_callback);
  1471.             SSL_CTX_set_client_cert_cb(tls_ctx,ssl_client_cert_callback);
  1472. #endif /* USE_CERT_CB */
  1473.         } else if (mode == SSL_SERVER) {
  1474.             /* We are a server */
  1475.             ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
  1476.             /* This can fail because we do not have RSA available */
  1477.             if ( !ssl_ctx ) {
  1478.                 debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
  1479.                 ssl_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_server_method());
  1480.             }
  1481.             if ( !ssl_ctx ) {
  1482.                 debug(F110,"ssl_tn_init","SSLv3_server_method failed",0);
  1483.                 last_ssl_mode = -1;
  1484.                 return(0);
  1485.             }
  1486. #ifdef COMMENT
  1487.             tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_server_method());
  1488. #else /* COMMENT */
  1489.             tls_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_server_method());
  1490.             /* This can fail because we do not have RSA available */
  1491.             if ( !tls_ctx ) {
  1492.                 debug(F110,"ssl_tn_init","SSLv23_server_method failed",0);
  1493.                 tls_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_server_method());
  1494.             }
  1495. #endif /* COMMENT */
  1496.             if ( !tls_ctx ) {
  1497.                 debug(F110,"ssl_tn_init","TLSv1_server_method failed",0);
  1498.                 last_ssl_mode = -1;
  1499.                 return(0);
  1500.             }
  1501.         } else /* Unknown mode */
  1502.             return(0);
  1503.  
  1504.         if ( !inserver ) {
  1505.             SSL_CTX_set_default_passwd_cb(ssl_ctx,
  1506.                                    (pem_password_cb *)ssl_passwd_callback);
  1507.             SSL_CTX_set_default_passwd_cb(tls_ctx,
  1508.                                    (pem_password_cb *)ssl_passwd_callback);
  1509.         }
  1510.  
  1511.         /* for SSL switch on all the interoperability and bug
  1512.          * workarounds so that we will communicate with people
  1513.          * that cannot read poorly written specs :-)
  1514.          * for TLS be sure to prevent use of SSLv2
  1515.          */
  1516.         SSL_CTX_set_options(ssl_ctx,SSL_OP_ALL|SSL_OP_NO_SSLv2);
  1517.         SSL_CTX_set_options(tls_ctx,
  1518.                  SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA);
  1519.  
  1520.         SSL_CTX_set_info_callback(ssl_ctx,ssl_client_info_callback);
  1521.         SSL_CTX_set_info_callback(tls_ctx,ssl_client_info_callback);
  1522.  
  1523. #ifndef COMMENT
  1524.         /* Set the proper caching mode */
  1525.         if ( mode == SSL_SERVER ) {
  1526.             SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_SERVER);
  1527.             SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_SERVER);
  1528.         } else {
  1529.             SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_CLIENT);
  1530.             SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_CLIENT);
  1531.         }
  1532.         SSL_CTX_set_session_id_context(ssl_ctx,"1",1);
  1533.         SSL_CTX_set_session_id_context(tls_ctx,"2",1);
  1534. #else /* COMMENT */
  1535.         SSL_CTX_set_session_cache_mode(ssl_ctx,SSL_SESS_CACHE_OFF);
  1536.         SSL_CTX_set_session_cache_mode(tls_ctx,SSL_SESS_CACHE_OFF);
  1537. #endif /* COMMENT */
  1538.     }
  1539.  
  1540.     /* The server uses defaults for the certificate files. */
  1541.     /* The client does not.                                */
  1542.     if (mode == SSL_SERVER) {
  1543.         char cert_filepath[1024];
  1544.         const char * defdir = NULL;
  1545.         DH * dh = NULL;
  1546.  
  1547.         defdir = getenv("SSL_CERT_DIR");
  1548.         if ( !defdir )
  1549.             defdir = X509_get_default_cert_dir();
  1550.         if ( !defdir )
  1551.             defdir = "";
  1552.  
  1553.         if (!ssl_rsa_cert_file) {
  1554.             /* we need to know the fullpath to the location of the
  1555.             * certificate that we will be running with as we cannot
  1556.             * be sure of the cwd when we are launched
  1557.             */
  1558.             sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa.pem");
  1559.             if (zchki(cert_filepath) > 0)
  1560.                 makestr(&ssl_rsa_cert_file,cert_filepath);
  1561.         }
  1562.         if (ssl_rsa_cert_file && !ssl_rsa_key_file) {
  1563.             /* we need to know the fullpath to the location of the
  1564.             * certificate that we will be running with as we cannot
  1565.             * be sure of the cwd when we are launched
  1566.             */
  1567.             sprintf(cert_filepath,"%s/%s",defdir,"telnetd-rsa-key.pem");
  1568.             if (zchki(cert_filepath) > 0)
  1569.                 makestr(&ssl_rsa_key_file,cert_filepath);
  1570.         }
  1571.         if (!ssl_dsa_cert_file) {
  1572.             /* we need to know the fullpath to the location of the
  1573.             * certificate that we will be running with as we cannot
  1574.             * be sure of the cwd when we are launched
  1575.             */
  1576.             sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa.pem");
  1577.             if (zchki(cert_filepath) > 0)
  1578.                 makestr(&ssl_dsa_cert_file,cert_filepath);
  1579.         }
  1580.         if (ssl_dsa_cert_file && !ssl_dh_key_file) {
  1581.             /* we need to know the fullpath to the location of the
  1582.             * certificate that we will be running with as we cannot
  1583.             * be sure of the cwd when we are launched
  1584.             */
  1585.             sprintf(cert_filepath,"%s/%s",defdir,"telnetd-dsa-key.pem");
  1586.             if (zchki(cert_filepath) > 0)
  1587.                 makestr(&ssl_dh_key_file,cert_filepath);
  1588.         }
  1589.         if (!ssl_crl_dir) {
  1590.             /* we need to know the fullpath to the location of the
  1591.             * certificate that we will be running with as we cannot
  1592.             * be sure of the cwd when we are launched
  1593.             */
  1594.             sprintf(cert_filepath,"%s/crl",defdir);
  1595.             if (zchki(cert_filepath) > 0)
  1596.                 makestr(&ssl_crl_dir,cert_filepath);
  1597.         }
  1598.  
  1599.         if (ssl_only_flag && !tls_load_certs(ssl_ctx,ssl_con,1)) {
  1600.             debug(F110,"ssl_tn_init","Unable to load SSL certs",0);
  1601.             last_ssl_mode = -1;
  1602.             return(0);
  1603.         }
  1604.         if (tls_only_flag && !tls_load_certs(tls_ctx,tls_con,1)) {
  1605.             debug(F110,"ssl_tn_init","Unable to load TLS certs",0);
  1606.             last_ssl_mode = -1;
  1607.             return(0);
  1608.         }
  1609.  
  1610.         if ( (last_ssl_mode != mode) || !ssl_ctx || !tls_ctx ) {
  1611.             /* we may require a temp 512 bit RSA key because of the
  1612.              * wonderful way export things work ... if so we generate
  1613.              * one now!
  1614.              */
  1615.  
  1616.             SSL_CTX_set_tmp_rsa_callback(ssl_ctx, tmp_rsa_cb);
  1617.             SSL_CTX_set_tmp_dh_callback( ssl_ctx, tmp_dh_cb);
  1618.             SSL_CTX_set_tmp_rsa_callback(tls_ctx, tmp_rsa_cb);
  1619.             SSL_CTX_set_tmp_dh_callback( tls_ctx, tmp_dh_cb);
  1620.  
  1621.             dh = tmp_dh_cb(NULL,0,512);
  1622.             SSL_CTX_set_tmp_dh(ssl_ctx,dh);
  1623.             SSL_CTX_set_tmp_dh(tls_ctx,dh);
  1624.  
  1625.             /* The following code is only called if we are using a
  1626.              * certificate with an RSA public key and where the
  1627.              * certificate has a key length less than 512 bits or is
  1628.              * marked for signing only.  This is so we can support
  1629.              * the greatest legal privacy level with exportable clients.
  1630.              */
  1631.  
  1632.             if (SSL_CTX_need_tmp_RSA(ssl_ctx) ||
  1633.                  SSL_CTX_need_tmp_RSA(tls_ctx))
  1634.             {
  1635.                 RSA *rsa;
  1636.  
  1637.                 if ( ssl_debug_flag )
  1638.                     printf("Generating temp (512 bit) RSA key ...\r\n");
  1639.                 rsa=RSA_generate_key(512,RSA_F4,NULL,NULL);
  1640.                 if ( ssl_debug_flag )
  1641.                     printf("Generation of temp (512 bit) RSA key done\r\n");
  1642.  
  1643.                 if (SSL_CTX_need_tmp_RSA(ssl_ctx)) {
  1644.                     if (!SSL_CTX_set_tmp_rsa(ssl_ctx,rsa)) {
  1645.                         if ( ssl_debug_flag )
  1646.                             printf(
  1647.   "Failed to assign generated temp RSA key to SSL!\r\n");
  1648.                     }
  1649.                 }
  1650.                 if (SSL_CTX_need_tmp_RSA(tls_ctx)) {
  1651.                     if (!SSL_CTX_set_tmp_rsa(tls_ctx,rsa)) {
  1652.                         if ( ssl_debug_flag )
  1653.                             printf(
  1654.   "Failed to assign generated temp RSA key to TLS!\r\n");
  1655.                     }
  1656.                 }
  1657.                 RSA_free(rsa);
  1658.                 if ( ssl_debug_flag )
  1659.                     printf("Assigned temp (512 bit) RSA key\r\n");
  1660.             }
  1661.         }
  1662.     }
  1663.  
  1664.     /* make sure we will find certificates in the standard
  1665.      * location ... otherwise we don't look anywhere for
  1666.      * these things which is going to make client certificate
  1667.      * exchange rather useless :-)
  1668.      * In OS2, default values for ssl_verify_file and ssl_verify_path.
  1669.      */
  1670.  
  1671. #ifdef OS2
  1672. #ifdef NT
  1673.     {
  1674.         /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  1675.         char path[CKMAXPATH];
  1676.         extern char exedir[];
  1677.         char * GetAppData(int);
  1678.  
  1679.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  1680.         if (SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 0)  {
  1681.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1682.             if (ssl_debug_flag)
  1683.                 printf("?Unable to load verify-dir: %s\r\n",path);
  1684.         } else
  1685.             SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1686.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
  1687.         if (SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 0)  {
  1688.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1689.             if (ssl_debug_flag)
  1690.                 printf("?Unable to load verify-dir: %s\r\n",path);
  1691.         } else
  1692.             SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1693.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
  1694.         if (SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 0)  {
  1695.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1696.             if (ssl_debug_flag)
  1697.                 printf("?Unable to load verify-dir: %s\r\n",path);
  1698.         } else
  1699.             SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1700.  
  1701.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  1702.         if (SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 0) {
  1703.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1704.             if (ssl_debug_flag)
  1705.                 printf("?Unable to load verify-file: %s\r\n",path);
  1706.         } else
  1707.             SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1708.  
  1709.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
  1710.         if (SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 0) {
  1711.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1712.             if (ssl_debug_flag)
  1713.                 printf("?Unable to load verify-file: %s\r\n",path);
  1714.         } else
  1715.             SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1716.  
  1717.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
  1718.         if (SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 0) {
  1719.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1720.             if (ssl_debug_flag)
  1721.                 printf("?Unable to load verify-file: %s\r\n",path);
  1722.         } else
  1723.             SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1724.     }
  1725. #else /* NT */
  1726.     {
  1727.         /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  1728.         char path[CKMAXPATH];
  1729.         extern char exedir[];
  1730.  
  1731.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  1732.         if (SSL_CTX_load_verify_locations(tls_ctx,NULL,path) == 0)  {
  1733.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1734.             if (ssl_debug_flag)
  1735.                 printf("?Unable to load verify-dir: %s\r\n",path);
  1736.         } else
  1737.             SSL_CTX_load_verify_locations(ssl_ctx,NULL,path);
  1738.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  1739.         if (SSL_CTX_load_verify_locations(tls_ctx,path,NULL) == 0) {
  1740.             debug(F110,"ssl_tn_init unable to load path",path,0);
  1741.             if (ssl_debug_flag)
  1742.                 printf("?Unable to load verify-file: %s\r\n",path);
  1743.         } else
  1744.             SSL_CTX_load_verify_locations(ssl_ctx,path,NULL);
  1745.     }
  1746. #endif /* NT */
  1747. #else /* OS2 */
  1748.     SSL_CTX_set_default_verify_paths(ssl_ctx);
  1749.     SSL_CTX_set_default_verify_paths(tls_ctx);
  1750. #endif /* OS2 */
  1751.  
  1752.     if (ssl_verify_file) {
  1753.         if (SSL_CTX_load_verify_locations(tls_ctx,ssl_verify_file,NULL) == 0) {
  1754.             debug(F110,"ssl_tn_init unable to load ssl_verify_file",ssl_verify_file,0);
  1755.             if (ssl_debug_flag)
  1756.                 printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  1757.         } else
  1758.             SSL_CTX_load_verify_locations(ssl_ctx,ssl_verify_file,NULL);
  1759.     }
  1760.     if (ssl_verify_dir) {
  1761.         if (SSL_CTX_load_verify_locations(tls_ctx,NULL,ssl_verify_dir) == 0)  {
  1762.             debug(F110,"ssl_tn_init unable to load ssl_verify_dir",ssl_verify_dir,0);
  1763.             if (ssl_debug_flag)
  1764.                 printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  1765.         } else
  1766.             SSL_CTX_load_verify_locations(ssl_ctx,NULL,ssl_verify_dir);
  1767.     }
  1768.  
  1769.     if (mode == SSL_SERVER) {
  1770.         SSL_CTX_set_verify(ssl_ctx,
  1771.                      ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
  1772.                            ssl_server_verify_callback);
  1773.         SSL_CTX_set_verify(tls_ctx,
  1774.                      ssl_verify_flag?ssl_verify_flag|SSL_VERIFY_CLIENT_ONCE:0,
  1775.                            ssl_server_verify_callback);
  1776.     } else {
  1777.         SSL_CTX_set_verify(ssl_ctx,ssl_verify_flag,
  1778.                            ssl_client_verify_callback);
  1779.         SSL_CTX_set_verify(tls_ctx,ssl_verify_flag,
  1780.                            ssl_client_verify_callback);
  1781.     }
  1782.  
  1783.     /* Free the existing CRL Store */
  1784.     if (crl_store) {
  1785.         X509_STORE_free(crl_store);
  1786.         crl_store = NULL;
  1787.     }
  1788.  
  1789.     /* set up the new CRL Store */
  1790.     crl_store = X509_STORE_new();
  1791.     if (crl_store) {
  1792. #ifdef OS2
  1793.         char path[CKMAXPATH];
  1794.         extern char exedir[];
  1795.  
  1796.         ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  1797.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  1798.             debug(F110,"ssl_tn_init unable to load dir",path,0);
  1799.             if (ssl_debug_flag)
  1800.                 printf("?Unable to load crl-dir: %s\r\n",path);
  1801.         }
  1802. #ifdef NT
  1803.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
  1804.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  1805.             debug(F110,"ssl_tn_init unable to load dir",path,0);
  1806.             if (ssl_debug_flag)
  1807.                 printf("?Unable to load crl-dir: %s\r\n",path);
  1808.         }
  1809.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
  1810.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  1811.             debug(F110,"ssl_tn_init unable to load dir",path,0);
  1812.             if (ssl_debug_flag)
  1813.                 printf("?Unable to load crl-dir: %s\r\n",path);
  1814.         }
  1815. #endif /* NT */
  1816.         
  1817.         ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  1818.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  1819.             debug(F110,"ssl_tn_init unable to load dir",path,0);
  1820.             if (ssl_debug_flag)
  1821.                 printf("?Unable to load crl-file: %s\r\n",path);
  1822.         }
  1823. #ifdef NT
  1824.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
  1825.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  1826.             debug(F110,"ssl_tn_init unable to load dir",path,0);
  1827.             if (ssl_debug_flag)
  1828.                 printf("?Unable to load crl-file: %s\r\n",path);
  1829.         }
  1830.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
  1831.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  1832.             debug(F110,"ssl_tn_init unable to load dir",path,0);
  1833.             if (ssl_debug_flag)
  1834.                 printf("?Unable to load crl-file: %s\r\n",path);
  1835.         }
  1836. #endif /* NT */
  1837. #endif /* OS2 */
  1838.  
  1839.         if (ssl_crl_file || ssl_crl_dir) {
  1840.             if (ssl_crl_file &&
  1841.                 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  1842.                 debug(F110,"ssl_tn_init unable to load ssl_crl_file",ssl_crl_file,0);
  1843.                 if (ssl_debug_flag)
  1844.                     printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  1845.             }
  1846.             if (ssl_crl_dir &&
  1847.                 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  1848.                 debug(F110,"ssl_tn_init unable to load ssl_crl_dir",ssl_crl_dir,0);
  1849.                 if (ssl_debug_flag)
  1850.                     printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  1851.             }
  1852.         } 
  1853. #ifndef OS2
  1854.         else {
  1855.             X509_STORE_set_default_paths(crl_store);
  1856.         }
  1857. #endif /* OS2 */
  1858.     }
  1859.  
  1860. #ifndef COMMENT
  1861.     ssl_conx = ssl_con;
  1862.     ssl_con=(SSL *)SSL_new(ssl_ctx);
  1863.     if ( !ssl_con ) {
  1864.         debug(F110,"ssl_tn_init","SSL_new(ssl_con) failed",0);
  1865.         last_ssl_mode = -1;
  1866.         ssl_con = ssl_conx;
  1867.         return(0);
  1868.     }
  1869.     if (ssl_conx) {
  1870.         if ( mode == SSL_CLIENT ) {
  1871.             SSL_set_session(ssl_con, SSL_get_session(ssl_conx));
  1872.         }
  1873. #ifdef SSL_KRB5
  1874.         kssl_ctx_free(ssl_conx->kssl_ctx);
  1875. #endif /* SSL_KRB5 */
  1876.         SSL_free(ssl_conx);
  1877.         ssl_conx = NULL;
  1878.     }
  1879.     tls_conx = tls_con;
  1880.     tls_con=(SSL *)SSL_new(tls_ctx);
  1881.     if ( !tls_con ) {
  1882.         debug(F110,"ssl_tn_init","SSL_new(tls_con) failed",0);
  1883.         last_ssl_mode = -1;
  1884.         tls_con = tls_conx;
  1885.         return(0);
  1886.     }
  1887.     if (tls_conx) {
  1888.         if ( mode == SSL_CLIENT )
  1889.             SSL_set_session(tls_con, SSL_get_session(tls_conx));
  1890. #ifdef SSL_KRB5
  1891.         kssl_ctx_free(tls_conx->kssl_ctx);
  1892. #endif /* SSL_KRB5 */
  1893.         SSL_free(tls_conx);
  1894.         tls_conx = NULL;
  1895.     }
  1896. #else /* COMMENT */
  1897.     /* I don't know why this does not work to reuse the connection. */
  1898.     if ( ssl_con ) {
  1899.         SSL_clear(ssl_con);
  1900.         SSL_set_session(ssl_con,NULL);
  1901.         SSL_set_accept_state(ssl_con) ;
  1902.     } else {
  1903.         ssl_con=(SSL *)SSL_new(ssl_ctx);
  1904.         if (!ssl_con) {
  1905.             debug(F110,"ssl_tn_init","SSL_new(ssl_ctx) failed",0);
  1906.             last_ssl_mode = -1;
  1907.             ssl_con = ssl_conx;
  1908.             return(0);
  1909.         }
  1910.     }
  1911.  
  1912.     if ( tls_con ) {
  1913.         SSL_clear(tls_con);
  1914.         SSL_set_session(tls_con,NULL);
  1915.         SSL_set_accept_state(tls_con) ;
  1916.     } else {
  1917.         tls_con=(SSL *)SSL_new(tls_ctx);
  1918.         if ( !tls_con ) {
  1919.             debug(F110,"ssl_tn_init","SSL_new(tls_ctx) failed",0);
  1920.             last_ssl_mode = -1;
  1921.             tls_con = tls_conx;
  1922.             return(0);
  1923.         }
  1924.     }
  1925. #endif /* COMMENT */
  1926.  
  1927. #ifdef SSL_KRB5
  1928. #ifndef KRB5_SERVICE_NAME
  1929. #define KRB5_SERVICE_NAME    "host"
  1930. #endif
  1931.  
  1932.     if (ssl_con->kssl_ctx == NULL)
  1933.         ssl_con->kssl_ctx = kssl_ctx_new();
  1934.     if (tls_con->kssl_ctx == NULL)
  1935.     tls_con->kssl_ctx = kssl_ctx_new();
  1936.     if (mode == SSL_SERVER) {
  1937.         if (ssl_con->kssl_ctx != NULL)
  1938.             kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
  1939.         if (tls_con->kssl_ctx != NULL)
  1940.             kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_KEYTAB, k5_keytab);
  1941.     } else {
  1942.         if (ssl_con->kssl_ctx != NULL)
  1943.             kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVER, szHostName);
  1944.         if (tls_con->kssl_ctx != NULL)
  1945.             kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVER, szHostName);
  1946.     }
  1947.     kssl_ctx_setstring(ssl_con->kssl_ctx, KSSL_SERVICE,
  1948.                         krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
  1949.     kssl_ctx_setstring(tls_con->kssl_ctx, KSSL_SERVICE,
  1950.                         krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
  1951. #endif /* SSL_KRB5 */
  1952.  
  1953.     if (ssl_cipher_list) {
  1954.         SSL_set_cipher_list(ssl_con,ssl_cipher_list);
  1955.         SSL_set_cipher_list(tls_con,ssl_cipher_list);
  1956.     } else {
  1957.         char * p;
  1958.         if (p = getenv("SSL_CIPHER")) {
  1959.             SSL_set_cipher_list(ssl_con,p);
  1960.             SSL_set_cipher_list(tls_con,p);
  1961.         } else {
  1962. #ifdef SSL_KRB5
  1963.             SSL_set_cipher_list(ssl_con,
  1964.                             "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP:KRB5");
  1965.             SSL_set_cipher_list(tls_con,
  1966.                             "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP:KRB5");
  1967. #else /* KRB5 */
  1968.             SSL_set_cipher_list(ssl_con,
  1969.                             "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP");
  1970.             SSL_set_cipher_list(tls_con,
  1971.                             "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP");
  1972. #endif /* KRB5 */
  1973.         }
  1974.     }
  1975.  
  1976.     ssl_verify_depth = -1;
  1977.  
  1978.     if ( ssl_debug_flag )
  1979.         printf("SSL/TLS init done!\r\n");
  1980.  
  1981.     ssl_initialized = 1;
  1982.     last_ssl_mode = mode;
  1983.     debug(F110,"ssl_tn_init","done",0);
  1984.     return(1);
  1985. }
  1986.  
  1987. #ifndef NOHTTP
  1988. int
  1989. #ifdef CK_ANSIC
  1990. ssl_http_init(char * hostname)
  1991. #else
  1992. ssl_http_init(hostname) char * hostname;
  1993. #endif /* CK_ANSIC */
  1994. {
  1995. #ifdef KRB5
  1996.     extern char * k5_keytab;
  1997.     extern char * krb5_d_srv;
  1998. #endif /* KRB5 */
  1999.     SSL * tls_conx=NULL;
  2000.  
  2001.     ssl_http_initialized = 0;
  2002.  
  2003.     if ( !ck_ssleay_is_installed() )
  2004.         return(0);
  2005.     debug(F110,"ssl_http_init",hostname,0);
  2006.  
  2007.     /* make sure we have somewhere we can log errors to */
  2008.     if (bio_err == NULL)
  2009.         bio_err=BIO_new(BIO_s_mem());
  2010.  
  2011.     if (ssl_debug_flag)
  2012.         printf("SSL_DEBUG_FLAG on\r\n");
  2013.  
  2014.     if (!tls_http_ctx ) {
  2015. #ifdef COMMENT
  2016.         /* too many web servers still do not support TLSv1 */
  2017.         tls_http_ctx=(SSL_CTX *)SSL_CTX_new(TLSv1_client_method());
  2018. #else /* COMMENT */
  2019.         tls_http_ctx=(SSL_CTX *)SSL_CTX_new(SSLv23_client_method());
  2020.         /* This can fail because we do not have RSA available */
  2021.         if ( !tls_http_ctx ) {
  2022.             debug(F110,"ssl_http_init","SSLv23_client_method failed",0);
  2023.             tls_http_ctx=(SSL_CTX *)SSL_CTX_new(SSLv3_client_method());
  2024.         }
  2025. #endif /* COMMENT */
  2026.         if ( !tls_http_ctx ) {
  2027.             debug(F110,"ssl_http_init","TLSv1_client_method failed",0);
  2028.             return(0);
  2029.         }
  2030. #ifdef USE_CERT_CB
  2031.         SSL_CTX_set_client_cert_cb(tls_http_ctx,ssl_client_cert_callback);
  2032. #endif /* USE_CERT_CB */
  2033.     }
  2034.  
  2035.     SSL_CTX_set_default_passwd_cb(tls_http_ctx,
  2036.                                   (pem_password_cb *)ssl_passwd_callback);
  2037.  
  2038.     /* for SSL switch on all the interoperability and bug
  2039.      * workarounds so that we will communicate with people
  2040.      * that cannot read poorly written specs :-)
  2041.      * for TLS be sure to prevent use of SSLv2
  2042.      */
  2043.     SSL_CTX_set_options(tls_http_ctx,
  2044.             SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA);
  2045.  
  2046.     SSL_CTX_set_info_callback(tls_http_ctx,ssl_client_info_callback);
  2047.  
  2048. #ifndef COMMENT
  2049.     SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_CLIENT);
  2050.     SSL_CTX_set_session_id_context(tls_http_ctx,"3",1);
  2051. #else /* COMMENT */
  2052.     SSL_CTX_set_session_cache_mode(tls_http_ctx,SSL_SESS_CACHE_OFF);
  2053. #endif /* COMMENT */
  2054.  
  2055.     /* make sure we will find certificates in the standard
  2056.      * location ... otherwise we don't look anywhere for
  2057.      * these things which is going to make client certificate
  2058.      * exchange rather useless :-)
  2059.      */
  2060.  
  2061. #ifdef OS2
  2062. #ifdef NT
  2063.     {
  2064.         /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  2065.         char path[CKMAXPATH];
  2066.         extern char exedir[];
  2067.  
  2068.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  2069.         if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0)  {
  2070.             debug(F110,"ssl_http_init unable to load path",path,0);
  2071.             if (ssl_debug_flag)
  2072.                 printf("?Unable to load verify-dir: %s\r\n",path);
  2073.         }
  2074.  
  2075.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/certs",NULL,NULL);
  2076.         if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0)  {
  2077.             debug(F110,"ssl_http_init unable to load path",path,0);
  2078.             if (ssl_debug_flag)
  2079.                 printf("?Unable to load verify-dir: %s\r\n",path);
  2080.         }
  2081.  
  2082.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/certs",NULL,NULL);
  2083.         if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0)  {
  2084.             debug(F110,"ssl_http_init unable to load path",path,0);
  2085.             if (ssl_debug_flag)
  2086.                 printf("?Unable to load verify-dir: %s\r\n",path);
  2087.         }
  2088.  
  2089.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  2090.         if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2091.             debug(F110,"ssl_http_init unable to load path",path,0);
  2092.             if (ssl_debug_flag)
  2093.                 printf("?Unable to load verify-file: %s\r\n",path);
  2094.         }
  2095.  
  2096.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_certs.pem",NULL,NULL);
  2097.         if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2098.             debug(F110,"ssl_http_init unable to load path",path,0);
  2099.             if (ssl_debug_flag)
  2100.                 printf("?Unable to load verify-file: %s\r\n",path);
  2101.         }
  2102.  
  2103.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_certs.pem",NULL,NULL);
  2104.         if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2105.             debug(F110,"ssl_http_init unable to load path",path,0);
  2106.             if (ssl_debug_flag)
  2107.                 printf("?Unable to load verify-file: %s\r\n",path);
  2108.         }
  2109.     }
  2110. #else /* NT */
  2111.     {
  2112.         /* The defaults in the SSL crypto library are not appropriate for OS/2 */
  2113.         char path[CKMAXPATH];
  2114.         extern char exedir[];
  2115.  
  2116.         ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
  2117.         if (SSL_CTX_load_verify_locations(tls_http_ctx,NULL,path) == 0)  {
  2118.             debug(F110,"ssl_http_init unable to load path",path,0);
  2119.             if (ssl_debug_flag)
  2120.                 printf("?Unable to load verify-dir: %s\r\n",path);
  2121.         }
  2122.         ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
  2123.         if (SSL_CTX_load_verify_locations(tls_http_ctx,path,NULL) == 0) {
  2124.             debug(F110,"ssl_http_init unable to load path",path,0);
  2125.             if (ssl_debug_flag)
  2126.                 printf("?Unable to load verify-file: %s\r\n",path);
  2127.         }
  2128.     }
  2129. #endif /* NT */
  2130. #else /* OS2 */
  2131.     SSL_CTX_set_default_verify_paths(tls_http_ctx);
  2132. #endif /* OS2 */
  2133.  
  2134.     if (ssl_verify_file &&
  2135.         SSL_CTX_load_verify_locations(tls_http_ctx,ssl_verify_file,NULL) == 0)  {
  2136.         debug(F110,"ssl_http_init unable to load ssl_verify_file",ssl_verify_file,0);
  2137.         if (ssl_debug_flag)
  2138.             printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
  2139.     }
  2140.     if (ssl_verify_dir &&
  2141.         SSL_CTX_load_verify_locations(tls_http_ctx,NULL,ssl_verify_dir) == 0)  {
  2142.         debug(F110,"ssl_http_init unable to load ssl_verify_dir",ssl_verify_dir,0);
  2143.         if (ssl_debug_flag)
  2144.             printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
  2145.     }
  2146.  
  2147.     SSL_CTX_set_verify(tls_http_ctx,ssl_verify_flag,
  2148.                            ssl_client_verify_callback);
  2149.  
  2150.     /* Free the existing CRL Store */
  2151.     if (crl_store) {
  2152.         X509_STORE_free(crl_store);
  2153.         crl_store = NULL;
  2154.     }
  2155.  
  2156.     /* set up the new CRL Store */
  2157.     crl_store = X509_STORE_new();
  2158.     if (crl_store) {
  2159. #ifdef OS2
  2160.         char path[CKMAXPATH];
  2161.         extern char exedir[];
  2162.  
  2163.         ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
  2164.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  2165.             debug(F110,"ssl_http_init unable to load dir",path,0);
  2166.             if (ssl_debug_flag)
  2167.                 printf("?Unable to load crl-dir: %s\r\n",path);
  2168.         }
  2169. #ifdef NT
  2170.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/crls",NULL,NULL);
  2171.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  2172.             debug(F110,"ssl_http_init unable to load dir",path,0);
  2173.             if (ssl_debug_flag)
  2174.                 printf("?Unable to load crl-dir: %s\r\n",path);
  2175.         }
  2176.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/crls",NULL,NULL);
  2177.         if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
  2178.             debug(F110,"ssl_http_init unable to load dir",path,0);
  2179.             if (ssl_debug_flag)
  2180.                 printf("?Unable to load crl-dir: %s\r\n",path);
  2181.         }
  2182. #endif /* NT */
  2183.         
  2184.         ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
  2185.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  2186.             debug(F110,"ssl_http_init unable to load file",path,0);
  2187.             if (ssl_debug_flag)
  2188.                 printf("?Unable to load crl-file: %s\r\n",path);
  2189.         }
  2190. #ifdef NT
  2191.         ckmakmsg(path,CKMAXPATH,GetAppData(1),"kermit 95/ca_crls.pem",NULL,NULL);
  2192.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  2193.             debug(F110,"ssl_http_init unable to load file",path,0);
  2194.             if (ssl_debug_flag)
  2195.                 printf("?Unable to load crl-file: %s\r\n",path);
  2196.         }
  2197.         ckmakmsg(path,CKMAXPATH,GetAppData(0),"kermit 95/ca_crls.pem",NULL,NULL);
  2198.         if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
  2199.             debug(F110,"ssl_http_init unable to load file",path,0);
  2200.             if (ssl_debug_flag)
  2201.                 printf("?Unable to load crl-file: %s\r\n",path);
  2202.         }
  2203. #endif /* NT */
  2204. #endif /* OS2 */
  2205.  
  2206.         if (ssl_crl_file || ssl_crl_dir) {
  2207.             if (ssl_crl_file &&
  2208.                 X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
  2209.                 debug(F110,"ssl_http_init unable to load ssl_crl_file",ssl_crl_file,0);
  2210.                 if (ssl_debug_flag)
  2211.                     printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
  2212.             }
  2213.             if (ssl_crl_dir &&
  2214.                 X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
  2215.                 debug(F110,"ssl_http_init unable to load ssl_crl_dir",ssl_crl_dir,0);
  2216.                 if (ssl_debug_flag)
  2217.                     printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
  2218.             }
  2219.         } else {
  2220.             X509_STORE_set_default_paths(crl_store);
  2221.         }
  2222.     }
  2223.  
  2224. #ifndef COMMENT
  2225.     tls_conx = tls_http_con;
  2226.     tls_http_con=(SSL *)SSL_new(tls_http_ctx);
  2227.     if ( !tls_http_con ) {
  2228.         debug(F110,"ssl_http_init","SSL_new(tls_http_con) failed",0);
  2229.         tls_http_con = tls_conx;
  2230.         return(0);
  2231.     }
  2232.     if (tls_conx) {
  2233.         SSL_set_session(tls_http_con, SSL_get_session(tls_conx));
  2234. #ifdef SSL_KRB5
  2235.         kssl_ctx_free(tls_conx->kssl_ctx);
  2236. #endif /* SSL_KRB5 */
  2237.         SSL_free(tls_conx);
  2238.         tls_conx = NULL;
  2239.     }
  2240. #else /* COMMENT */
  2241.     /* I don't know why this does not work to reuse the connection. */
  2242.     if ( tls_http_con ) {
  2243.         SSL_clear(tls_http_con);
  2244.         SSL_set_session(tls_http_con,NULL);
  2245.         SSL_set_accept_state(tls_http_con) ;
  2246.     } else {
  2247.         tls_http_con=(SSL *)SSL_new(tls_http_ctx);
  2248.         if ( !tls_http_con ) {
  2249.             debug(F110,"ssl_http_init","SSL_new(tls_http_ctx) failed",0);
  2250.             tls_http_con = tls_conx;
  2251.             return(0);
  2252.         }
  2253.     }
  2254. #endif /* COMMENT */
  2255.  
  2256. #ifdef SSL_KRB5
  2257. #ifndef KRB5_SERVICE_NAME
  2258. #define KRB5_SERVICE_NAME    "host"
  2259. #endif
  2260.  
  2261.     if (tls_http_con->kssl_ctx == NULL)
  2262.     tls_http_con->kssl_ctx = kssl_ctx_new();
  2263.     if (tls_http_con->kssl_ctx != NULL)
  2264.         kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVER, hostname);
  2265.  
  2266.     kssl_ctx_setstring(tls_http_con->kssl_ctx, KSSL_SERVICE,
  2267.                         krb5_d_srv ? krb5_d_srv : KRB5_SERVICE_NAME);
  2268. #endif /* SSL_KRB5 */
  2269.  
  2270.     if (ssl_cipher_list)
  2271.         SSL_set_cipher_list(tls_http_con,ssl_cipher_list);
  2272.     else {
  2273.         char * p;
  2274.         if (p = getenv("SSL_CIPHER")) {
  2275.             SSL_set_cipher_list(tls_http_con,p);
  2276.         } else {
  2277. #ifdef SSL_KRB5
  2278.             SSL_set_cipher_list(tls_http_con,
  2279.                             "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP:KRB5");
  2280. #else /* KRB5 */
  2281.             SSL_set_cipher_list(tls_http_con,
  2282.                             "HIGH:MEDIUM:LOW:ADH+3DES:ADH+RC4:ADH+DES:+EXP");
  2283. #endif /* KRB5 */
  2284.         }
  2285.     }
  2286.  
  2287.     ssl_verify_depth = -1;
  2288.  
  2289.     if ( ssl_debug_flag )
  2290.         printf("SSL/TLS init done!\r\n");
  2291.  
  2292.     ssl_http_initialized = 1;
  2293.     return(1);
  2294. }
  2295. #endif /* NOHTTP */
  2296.  
  2297. char *
  2298. ssl_get_dNSName(ssl) SSL * ssl;
  2299. {
  2300.     static char *dns = NULL;
  2301.     X509 *server_cert = NULL;
  2302.     int i;
  2303.     X509_EXTENSION *ext = NULL;
  2304.     STACK_OF(GENERAL_NAME) *ialt = NULL;
  2305.     GENERAL_NAME *gen = NULL;
  2306.  
  2307.     if ( dns ) {
  2308.         free(dns);
  2309.         dns = NULL;
  2310.     }
  2311.  
  2312.     if (server_cert = SSL_get_peer_certificate(ssl)) {
  2313.         if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1))<0)
  2314.             return NULL;
  2315.         if (!(ext = X509_get_ext(server_cert, i)))
  2316.             return NULL;
  2317.         X509V3_add_standard_extensions();
  2318.         if (!(ialt = X509V3_EXT_d2i(ext)))
  2319.             return NULL;
  2320.         for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
  2321.             gen = sk_GENERAL_NAME_value(ialt, i);
  2322.             if (gen->type == GEN_DNS) {
  2323.                 if(!gen->d.ia5 || !gen->d.ia5->length)
  2324.                     break;
  2325.                 dns = malloc(gen->d.ia5->length + 1);
  2326.                 if (dns) {
  2327.                     memcpy(dns, gen->d.ia5->data, gen->d.ia5->length);
  2328.                     dns[gen->d.ia5->length] = 0;
  2329.                 }
  2330.                 break;
  2331.             }
  2332.         }
  2333.         X509V3_EXT_cleanup();
  2334.     }
  2335. cleanup:
  2336.     if (ialt)           sk_GENERAL_NAME_free(ialt);
  2337.     if (server_cert)    X509_free(server_cert);
  2338.     return dns;
  2339. }
  2340.  
  2341. char *
  2342. ssl_get_commonName(ssl) SSL * ssl;
  2343. {
  2344.     static char name[256];
  2345.     int err;
  2346.     X509 *server_cert;
  2347.  
  2348.     if (server_cert = SSL_get_peer_certificate(ssl)) {
  2349.         err = X509_NAME_get_text_by_NID(X509_get_subject_name(server_cert),
  2350.                 NID_commonName, name, sizeof(name));
  2351.         X509_free(server_cert);
  2352.     }
  2353.     if (err > 0)
  2354.         return name;
  2355.     else
  2356.         return NULL;
  2357. }
  2358.  
  2359. char *
  2360. ssl_get_issuer_name(ssl) SSL * ssl;
  2361. {
  2362.     static char name[256];
  2363.     X509 *server_cert;
  2364.  
  2365.     name[0] = '\0';
  2366.     if (server_cert = SSL_get_peer_certificate(ssl)) {
  2367.         X509_NAME_oneline(X509_get_issuer_name(server_cert),name,sizeof(name));
  2368.         X509_free(server_cert);
  2369.         return name;
  2370.     }
  2371.     else {
  2372. #ifdef COMMENT
  2373.       fprintf(stderr, "Warning: No certificate from server!\r\n");
  2374. #endif /* COMMENT */
  2375.         return NULL;
  2376.     }
  2377. }
  2378.  
  2379. char *
  2380. ssl_get_subject_name(ssl) SSL * ssl;
  2381. {
  2382.     static char name[256];
  2383.     X509 *server_cert;
  2384.  
  2385.     name[0] = '\0';
  2386.     if (server_cert = SSL_get_peer_certificate(ssl)) {
  2387.        X509_NAME_oneline(X509_get_subject_name(server_cert),name,sizeof(name));
  2388.        X509_free(server_cert);
  2389.        return name;
  2390.     }
  2391.     else
  2392.         return NULL;
  2393. }
  2394.  
  2395. #ifdef COMMENT
  2396. #ifdef CK_SSL
  2397.             && !(ck_ssleay_is_installed() &&
  2398.                (tls_active_flag || ssl_active_flag) &&
  2399.                ssl_anonymous_cipher(tls_active_flag?tls_con:ssl_con))
  2400. #endif /* CK_SSL */
  2401.  
  2402. int
  2403. ssl_anonymous_cipher(ssl) SSL * ssl;
  2404. {
  2405.     X509 * cert;
  2406.  
  2407.     if (sstelnet)
  2408.         cert = SSL_get_certificate(ssl);
  2409.     else
  2410.         cert = SSL_get_peer_certificate(ssl);
  2411.  
  2412.     if ( cert ) {
  2413.         X509_free(cert);
  2414.         return 0;
  2415.     }
  2416.     return 1;
  2417. }
  2418. #endif /* COMMENT */
  2419.  
  2420. /*
  2421.   This one is (very much!) based on work by
  2422.   Ralf S. Engelschall <rse@engelschall.com>.
  2423.   Comments by Ralf.
  2424. */
  2425. int
  2426. ssl_verify_crl(int ok, X509_STORE_CTX *ctx)
  2427. {
  2428.     X509_OBJECT obj;
  2429.     X509_NAME *subject = NULL;
  2430.     X509_NAME *issuer = NULL;
  2431.     X509 *xs = NULL;
  2432.     X509_CRL *crl = NULL;
  2433.     X509_REVOKED *revoked = NULL;
  2434.     X509_STORE_CTX * store_ctx = NULL;
  2435.     long serial;
  2436.     BIO *bio = NULL;
  2437.     int i, n, rc;
  2438.     char *cp;
  2439.     char *cp2;
  2440.  
  2441.     /*
  2442.      * Unless a revocation store for CRLs was created we
  2443.      * cannot do any CRL-based verification, of course.
  2444.      */
  2445.     if (!crl_store)
  2446.         return ok;
  2447.  
  2448.     store_ctx = X509_STORE_CTX_new();
  2449.     if ( !store_ctx )
  2450.         return(ok);
  2451.  
  2452.     /*
  2453.      * Determine certificate ingredients in advance
  2454.      */
  2455.     xs      = X509_STORE_CTX_get_current_cert(ctx);
  2456.     subject = X509_get_subject_name(xs);
  2457.     issuer  = X509_get_issuer_name(xs);
  2458.  
  2459.     /*
  2460.      * OpenSSL provides the general mechanism to deal with CRLs but does not
  2461.      * use them automatically when verifying certificates, so we do it
  2462.      * explicitly here. We will check the CRL for the currently checked
  2463.      * certificate, if there is such a CRL in the store.
  2464.      *
  2465.      * We come through this procedure for each certificate in the certificate
  2466.      * chain, starting with the root-CA's certificate. At each step we've to
  2467.      * both verify the signature on the CRL (to make sure it's a valid CRL)
  2468.      * and it's revocation list (to make sure the current certificate isn't
  2469.      * revoked).  But because to check the signature on the CRL we need the
  2470.      * public key of the issuing CA certificate (which was already processed
  2471.      * one round before), we've a little problem. But we can both solve it and
  2472.      * at the same time optimize the processing by using the following
  2473.      * verification scheme (idea and code snippets borrowed from the GLOBUS
  2474.      * project):
  2475.      *
  2476.      * 1. We'll check the signature of a CRL in each step when we find a CRL
  2477.      *    through the _subject_ name of the current certificate. This CRL
  2478.      *    itself will be needed the first time in the next round, of course.
  2479.      *    But we do the signature processing one round before this where the
  2480.      *    public key of the CA is available.
  2481.      *
  2482.      * 2. We'll check the revocation list of a CRL in each step when
  2483.      *    we find a CRL through the _issuer_ name of the current certificate.
  2484.      *    This CRLs signature was then already verified one round before.
  2485.      *
  2486.      * This verification scheme allows a CA to revoke its own certificate as
  2487.      * well, of course.
  2488.      */
  2489.  
  2490.     /*
  2491.      * Try to retrieve a CRL corresponding to the _subject_ of
  2492.      * the current certificate in order to verify it's integrity.
  2493.      */
  2494.     memset((char *)&obj, 0, sizeof(obj));
  2495.     X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
  2496.     rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, subject, &obj);
  2497.     X509_STORE_CTX_cleanup(store_ctx);
  2498.     crl = obj.data.crl;
  2499.     if (rc > 0 && crl != NULL) {
  2500.         /*
  2501.          * Verify the signature on this CRL
  2502.          */
  2503.         if (X509_CRL_verify(crl, X509_get_pubkey(xs)) <= 0) {
  2504.             fprintf(stderr, "Invalid signature on CRL!\n");
  2505.             X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
  2506.             X509_OBJECT_free_contents(&obj);
  2507.             X509_STORE_CTX_free(store_ctx);
  2508.             return 0;
  2509.         }
  2510.  
  2511.         /*
  2512.          * Check date of CRL to make sure it's not expired
  2513.          */
  2514.         i = X509_cmp_current_time(X509_CRL_get_nextUpdate(crl));
  2515.         if (i == 0) {
  2516.             fprintf(stderr, "Found CRL has invalid nextUpdate field.\n");
  2517.             X509_STORE_CTX_set_error(ctx,
  2518.                                     X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
  2519.             X509_OBJECT_free_contents(&obj);
  2520.             X509_STORE_CTX_free(store_ctx);
  2521.             return 0;
  2522.         }
  2523.         if (i < 0) {
  2524.             fprintf(stderr,
  2525. "Found CRL is expired - revoking all certificates until you get updated CRL.\n"
  2526.                     );
  2527.             X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_HAS_EXPIRED);
  2528.             X509_OBJECT_free_contents(&obj);
  2529.             X509_STORE_CTX_free(store_ctx);
  2530.             return 0;
  2531.         }
  2532.         X509_OBJECT_free_contents(&obj);
  2533.     }
  2534.  
  2535.     /*
  2536.      * Try to retrieve a CRL corresponding to the _issuer_ of
  2537.      * the current certificate in order to check for revocation.
  2538.      */
  2539.     memset((char *)&obj, 0, sizeof(obj));
  2540.     X509_STORE_CTX_init(store_ctx, crl_store, NULL, NULL);
  2541.     rc = X509_STORE_get_by_subject(store_ctx, X509_LU_CRL, issuer, &obj);
  2542.     X509_STORE_CTX_cleanup(store_ctx);
  2543.     crl = obj.data.crl;
  2544.     if (rc > 0 && crl != NULL) {
  2545.         /*
  2546.          * Check if the current certificate is revoked by this CRL
  2547.          */
  2548.         n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
  2549.         for (i = 0; i < n; i++) {
  2550.             revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
  2551.             if (ASN1_INTEGER_cmp(revoked->serialNumber,
  2552.                                  X509_get_serialNumber(xs)) == 0) {
  2553.  
  2554.                 serial = ASN1_INTEGER_get(revoked->serialNumber);
  2555.                 cp = X509_NAME_oneline(issuer, NULL, 0);
  2556.                 free(cp);
  2557.  
  2558.                 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REVOKED);
  2559.                 X509_OBJECT_free_contents(&obj);
  2560.                 X509_STORE_CTX_free(store_ctx);
  2561.                 return 0;
  2562.             }
  2563.         }
  2564.         X509_OBJECT_free_contents(&obj);
  2565.     }
  2566.  
  2567.     X509_STORE_CTX_free(store_ctx);
  2568.     return ok;
  2569. }
  2570.  
  2571. char *
  2572. tls_userid_from_client_cert(ssl) SSL * ssl;
  2573. {
  2574.     static char cn[256];
  2575.     static char *r = cn;
  2576.     int err;
  2577.     X509 *client_cert;
  2578.  
  2579.     if (client_cert = SSL_get_peer_certificate(ssl)) {
  2580.         /* call the custom function */
  2581.         err = X509_to_user(client_cert, cn, sizeof(cn));
  2582.         X509_free(client_cert);
  2583.         if (err)
  2584.             return r = NULL;
  2585.         else
  2586.             return r;
  2587.     }
  2588.     else
  2589.         return r = NULL;
  2590. }
  2591.  
  2592. unsigned char **
  2593. tls_get_SAN_objs(SSL * ssl, int type)
  2594. /* returns NULL or an array of malloc'ed objects of type `type' from the server's
  2595.  * subjectAltName, remember to free() them all!
  2596.  */
  2597. {
  2598. #define NUM_SAN_OBJS 64
  2599.     static unsigned char *objs[NUM_SAN_OBJS];
  2600.     unsigned char **rv = NULL;
  2601.     X509 *server_cert = NULL;
  2602.     int i, j;
  2603.     X509_EXTENSION *ext = NULL;
  2604.     STACK_OF(GENERAL_NAME) *ialt = NULL;
  2605.     GENERAL_NAME *gen = NULL;
  2606.  
  2607.     memset(objs, 0, sizeof(objs));
  2608.     if (server_cert = SSL_get_peer_certificate(ssl)) {
  2609.         if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1)) < 0)
  2610.             goto eject;
  2611.         if (!(ext = X509_get_ext(server_cert, i)))
  2612.             goto eject;
  2613.         X509V3_add_standard_extensions();
  2614.         if (!(ialt = X509V3_EXT_d2i(ext)))
  2615.             goto eject;
  2616.         rv = objs;
  2617.         for (i = 0, j = 0; i < sk_GENERAL_NAME_num(ialt) && j < NUM_SAN_OBJS - 2; i++) {
  2618.             gen = sk_GENERAL_NAME_value(ialt, i);
  2619.             /* The use of V_ASN1_CONTEXT_SPECIFIC is because OpenSSL 0.9.6 defined its
  2620.              * types | V_ASN1_CONTEXT_SPECIFIC.  0.9.7 does not.  In case, we are built
  2621.              * with one and linked to the other we use this hack.
  2622.              */
  2623.             if ((gen->type | V_ASN1_CONTEXT_SPECIFIC) == (type | V_ASN1_CONTEXT_SPECIFIC)) {
  2624.                 if(!gen->d.ia5 || !gen->d.ia5->length)
  2625.                     break;
  2626.                 objs[j] = malloc(gen->d.ia5->length + 1);
  2627.                 if (objs[j]) {
  2628.                     memcpy(objs[j], gen->d.ia5->data, gen->d.ia5->length);
  2629.                     objs[j][gen->d.ia5->length] = 0;
  2630.                     j++;
  2631.                 }
  2632.             }
  2633.         }
  2634.         X509V3_EXT_cleanup();
  2635.     }
  2636. eject:
  2637.     if (ialt)           sk_GENERAL_NAME_free(ialt);
  2638.     if (server_cert)    X509_free(server_cert);
  2639.     return rv;
  2640. }
  2641.  
  2642.  
  2643. static int
  2644. dNSName_cmp(const char *host, const char *dNSName)
  2645. {
  2646.     int c1 = 0, c2 = 0, num_comp, rv = -1;
  2647.     char *p, *p1, *p2, *host_copy, *dNSName_copy;
  2648.  
  2649.     /* first we count the number of domain name components in both parameters.
  2650.      * they should be equal many, or it's not a match
  2651.      */
  2652.     p = (char *) host;
  2653.     while (p = strstr(p, ".")) {
  2654.         c1++;
  2655.         p++;
  2656.     }
  2657.     p = (char *) dNSName;
  2658.     while (p = strstr(p, ".")) {
  2659.         c2++;
  2660.         p++;
  2661.     }
  2662.     if (c1 != c2)
  2663.         return -1;
  2664.     num_comp = c1;
  2665.  
  2666.     host_copy = strdup(host);
  2667.     dNSName_copy = strdup(dNSName);
  2668.     if (host_copy == NULL || dNSName_copy == NULL)
  2669.         goto eject;
  2670.     /* make substrings by replacing '.' with '\0' */
  2671.     p = dNSName_copy;
  2672.     while (p = strstr(p, ".")) {
  2673.         *p = '\0';
  2674.         p++;
  2675.     }
  2676.     p = host_copy;
  2677.     while (p = strstr(p, ".")) {
  2678.         *p = '\0';
  2679.         p++;
  2680.     }
  2681.  
  2682.     /* compare each component */
  2683.     p1 = host_copy;
  2684.     p2 = dNSName_copy;
  2685.     for (; num_comp; num_comp--) {
  2686.         if (!ckmatch(p2, p1,0,1))
  2687.             /* failed match */
  2688.             goto eject;
  2689.         p1 += strlen(p1) + 1;
  2690.         p2 += strlen(p2) + 1;
  2691.     }
  2692.     /* match ok */
  2693.     rv = 0;
  2694.  
  2695.   eject:
  2696.     if (dNSName_copy)   free(dNSName_copy);
  2697.     if (host_copy)      free(host_copy);
  2698.     return rv;
  2699. }
  2700.  
  2701.  
  2702.  
  2703. static int
  2704. show_hostname_warning(char *s1, char *s2)
  2705. {
  2706.     char prefix[1024];
  2707.     int ok = 1;
  2708.     ckmakxmsg(prefix,1024,
  2709.               "Warning: Hostname (\"", s1, 
  2710.               "\") does not match server's certificate (\"", s2, "\")\r\n",
  2711.               NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  2712.     if (ssl_verify_flag)
  2713.         ok = uq_ok(prefix,
  2714.                     "Continue? (Y/N) ",
  2715.                     3, NULL, 0);
  2716.     else if (ssl_verbose_flag)
  2717.         printf(prefix);
  2718.     return(ok);
  2719. }
  2720.  
  2721. #ifndef HPUX1100
  2722. #ifndef SCO_OSR505
  2723. #ifndef OpenBSD
  2724. #ifndef FREEBSD4
  2725. #ifndef LINUX
  2726. #ifndef AIX41
  2727. #ifndef UW7
  2728. #ifndef SOLARIS9
  2729. #ifndef SOLARIS8
  2730. #ifndef SOLARIS7
  2731. static int
  2732. inet_aton(char * ipaddress, struct in_addr * ia) {
  2733.     struct stringarray * q;
  2734.     union {
  2735.         unsigned long l;
  2736.         unsigned char b[4];
  2737.     } dummy;
  2738.  
  2739.     q = cksplit(1,0,ipaddress,".","0123456789abcdefACDEF",8,0,0);
  2740.     if (q->a_size == 4) {
  2741.         dummy.b[0] = atoi(q->a_head[1]);
  2742.         dummy.b[1] = atoi(q->a_head[2]);
  2743.         dummy.b[2] = atoi(q->a_head[3]);
  2744.         dummy.b[3] = atoi(q->a_head[4]);
  2745.         ia->s_addr = dummy.l;
  2746.         return(ia->s_addr != 0);
  2747.     }
  2748.     return(0);
  2749. }
  2750. #endif /* SOLARIS7 */
  2751. #endif /* SOLARIS8 */
  2752. #endif /* SOLARIS9 */
  2753. #endif /* UW7 */
  2754. #endif /* AIX41 */
  2755. #endif /* LINUX */
  2756. #endif /* FREEBSD4 */
  2757. #endif /* OpenBSD */
  2758. #endif /* SCO_OSR505 */
  2759. #endif /* HPUX1100 */
  2760.  
  2761. int
  2762. ssl_check_server_name(SSL * ssl, char * hostname)
  2763. /* returns 0 if hostname and server's cert matches, else -1 */
  2764. {
  2765.     char * commonName;
  2766.     unsigned char ** dNSName;
  2767.     unsigned char ** ipAddress;
  2768.     struct in_addr ia;
  2769.     int rv;
  2770.  
  2771.     if (ssl_verbose_flag && !inserver) {
  2772.         if (dNSName = tls_get_SAN_objs(ssl,GEN_DNS)) {
  2773.             int i = 0;
  2774.             for (i = 0; dNSName[i]; i++) {
  2775.                 printf("Certificate[0] altSubjectName DNS=%s\r\n",dNSName[i]);
  2776.                 free(dNSName[i]);
  2777.             }
  2778.         }
  2779.         if (ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD)) {
  2780.             int i = 0;
  2781.             char *server_ip;
  2782.             struct in_addr ia;
  2783.  
  2784.             for (i = 0; ipAddress[i]; i++) {
  2785.                 if (ipAddress[i]) {
  2786.                     ia.s_addr = *(unsigned long *)ipAddress[i];
  2787.                     server_ip = inet_ntoa(ia);
  2788.                     printf("Certificate[0] altSubjectName IPAddr=%s\r\n",server_ip);
  2789.                 }
  2790.                 free(ipAddress[i]);
  2791.             }
  2792.             /* ipAddress points to a static - don't free */
  2793.         }
  2794.         if (dNSName = tls_get_SAN_objs(ssl,GEN_EMAIL)) {
  2795.             int i = 0;
  2796.             for (i = 0; dNSName[i]; i++) {
  2797.                 printf("Certificate[0] altSubjectName Email=%s\r\n",dNSName[i]);
  2798.                 free(dNSName[i]);
  2799.             }
  2800.         }
  2801.         if (dNSName = tls_get_SAN_objs(ssl,GEN_URI)) {
  2802.             int i = 0;
  2803.             for (i = 0; dNSName[i]; i++) {
  2804.                 printf("Certificate[0] altSubjectName URI=%s\r\n",dNSName[i]);
  2805.                 free(dNSName[i]);
  2806.             }
  2807.         }
  2808.         if (dNSName = tls_get_SAN_objs(ssl,GEN_OTHERNAME)) {
  2809.             int i = 0;
  2810.             for (i = 0; dNSName[i]; i++) {
  2811.                 printf("Certificate[0] altSubjectName Other=%s\r\n",dNSName[i]);
  2812.                 free(dNSName[i]);
  2813.             }
  2814.         }
  2815.     }
  2816.  
  2817.     /* first we check if `hostname' is in fact an ip address */
  2818.     if (inet_aton(hostname, &ia)) {
  2819.         ipAddress = tls_get_SAN_objs(ssl,GEN_IPADD);
  2820.         if (ipAddress) {
  2821.             int i = 0;
  2822.             char *server_ip = "UNKNOWN";
  2823.  
  2824.             for (i = 0; ipAddress[i]; i++)
  2825.                 if (*(unsigned long *)ipAddress[i] == ia.s_addr)
  2826.                     return 0;
  2827.  
  2828.             if (ipAddress[i - 1]) {
  2829.                 ia.s_addr = *(unsigned long *)ipAddress[i - 1];
  2830.                 server_ip = inet_ntoa(ia);
  2831.             }
  2832.             rv = show_hostname_warning(hostname, server_ip) ? 0 : -1;
  2833.             for (i = 0; ipAddress[i]; i++)
  2834.                 free(ipAddress[i]);
  2835.         } else {
  2836.             rv = show_hostname_warning(hostname, "NO IP IN CERT") ? 0 : -1;
  2837.         }
  2838.         return(rv);
  2839.     }
  2840.  
  2841.     /* look for dNSName(s) in subjectAltName in the server's certificate */
  2842.     dNSName = tls_get_SAN_objs(ssl,GEN_DNS);
  2843.     if (dNSName) {
  2844.         int i = 0;
  2845.         for (i = 0; dNSName[i]; i++) {
  2846.             if (!dNSName_cmp(hostname, dNSName[i]))
  2847.                 return 0;
  2848.         }
  2849.         rv = show_hostname_warning(hostname,
  2850.         dNSName[i - 1] ? dNSName[i - 1] : (unsigned char *)"UNKNOWN") ? 0 : -1;
  2851.         for (i = 0; dNSName[i]; i++)
  2852.             free(dNSName[i]);
  2853.         return rv;
  2854.     } else if ((commonName = ssl_get_commonName(ssl))) {
  2855.        /* so the server didn't have any dNSName's, check the commonName */
  2856.        if (!dNSName_cmp(hostname, commonName))
  2857.            return 0;
  2858.        else
  2859.            return (show_hostname_warning(hostname, commonName) ? 0 : -1);
  2860.     }
  2861.     return -1;
  2862. }
  2863.  
  2864. /* Is 'user' authorized to access the system without a login */
  2865. int
  2866. tls_is_user_valid(SSL * ssl, const char *user)
  2867. {
  2868.     X509 *client_cert;
  2869.     int r = 0;
  2870.  
  2871.     if ( !ssl || !user || !user[0] )
  2872.         return(0);
  2873.  
  2874.     if (!(client_cert = SSL_get_peer_certificate(ssl)))
  2875.         return 0;
  2876.  
  2877.     /* Use user supplied function */
  2878.     r = X509_userok(client_cert,user);
  2879.  
  2880.     X509_free(client_cert);
  2881.     return r;
  2882. }
  2883.  
  2884. int
  2885. tls_is_anon(int x)
  2886. {
  2887.     char buf[128];
  2888.     SSL_CIPHER * cipher;
  2889.     SSL * ssl = NULL;
  2890.  
  2891.     switch ( x ) {
  2892. #ifndef NOFTP
  2893. #ifndef SYSFTP
  2894.     case 1:     /* ftp command */
  2895.         if ( ssl_ftp_active_flag )
  2896.             ssl = ssl_ftp_con;
  2897.         else
  2898.             return(0);
  2899.         break;
  2900.     case 2:     /* ftp data */
  2901.         if ( ssl_ftp_data_active_flag )
  2902.             ssl = ssl_ftp_data_con;
  2903.         else
  2904.             return(0);
  2905.         break;
  2906. #endif /* SYSFTP */
  2907. #endif /* NOFTP */
  2908.     default:
  2909.         if (tls_active_flag)
  2910.             ssl = tls_con;
  2911.         else if (ssl_active_flag)
  2912.             ssl = ssl_con;
  2913.         else
  2914.             return(0);
  2915.     }
  2916.  
  2917.     cipher = SSL_get_current_cipher(ssl);
  2918.     if (SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
  2919.         if (ckindex("Au=None",buf,0,0,0) != 0)
  2920.             return(1);                  /* anonymous */
  2921.         return(0);                  /* known */
  2922.     } else {
  2923.         /* could not get cipher description.  Assume anonymous */
  2924.         return(1);
  2925.     }
  2926. }
  2927.  
  2928. int
  2929. tls_is_krb5(int x)
  2930. {
  2931.     char buf[128];
  2932.     SSL_CIPHER * cipher;
  2933.     SSL * ssl = NULL;
  2934.  
  2935.     switch ( x ) {
  2936. #ifndef NOFTP
  2937. #ifndef SYSFTP
  2938.     case 1:     /* ftp command */
  2939.         if ( ssl_ftp_active_flag )
  2940.             ssl = ssl_ftp_con;
  2941.         else
  2942.             return(0);
  2943.         break;
  2944.     case 2:     /* ftp data */
  2945.         if ( ssl_ftp_data_active_flag )
  2946.             ssl = ssl_ftp_data_con;
  2947.         else
  2948.             return(0);
  2949.         break;
  2950. #endif /* SYSFTP */
  2951. #endif /* NOFTP */
  2952. #ifndef NOHTTP
  2953.     case 3:
  2954.         if ( tls_http_active_flag )
  2955.             ssl = tls_http_con;
  2956.         break;
  2957. #endif /* NOHTTP */
  2958.     default:
  2959.         if (tls_active_flag)
  2960.             ssl = tls_con;
  2961.         else if (ssl_active_flag)
  2962.             ssl = ssl_con;
  2963.         else
  2964.             return(0);
  2965.     }
  2966.  
  2967.     cipher = SSL_get_current_cipher(ssl);
  2968.     if (cipher && SSL_CIPHER_description(cipher,buf,sizeof(buf))) {
  2969.         if (ckindex("Au=KRB5",buf,0,0,0) != 0)
  2970.             return(1);                  /* krb5 */
  2971.     }
  2972.     return(0);                          /* not */
  2973. }
  2974.  
  2975. int
  2976. ssl_get_client_finished(char *buf, int count)
  2977. {
  2978. #ifdef NO_GET_FINISHED
  2979.     return(0);
  2980. #else
  2981.     if (sstelnet || tcp_incoming) {
  2982.         return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
  2983.                                       buf,count));
  2984.     } else {
  2985.         return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
  2986.                                       buf,count));
  2987.     }
  2988. #endif /* NO_GET_FINISHED */
  2989. }
  2990.  
  2991. int
  2992. ssl_get_server_finished(char *buf, int count)
  2993. {
  2994. #ifdef NO_GET_FINISHED
  2995.     return(0);
  2996. #else
  2997.     if (sstelnet || tcp_incoming) {
  2998.         return(SSL_get_finished(ssl_active_flag?ssl_con:tls_con,
  2999.                                       buf,count));
  3000.     } else {
  3001.         return(SSL_get_peer_finished(ssl_active_flag?ssl_con:tls_con,
  3002.                                       buf,count));
  3003.     }
  3004. #endif /* NO_GET_FINISHED */
  3005. }
  3006.  
  3007.  
  3008. #ifdef CK_AUTHENTICATION
  3009. int
  3010. #ifdef CK_ANSIC
  3011. ssl_reply(int how, unsigned char *data, int cnt)
  3012. #else
  3013. ssl_reply(how,data,cnt) int how; unsigned char *data; int cnt;
  3014. #endif
  3015. {
  3016.     char * str=NULL;
  3017.  
  3018.     data += 4;                          /* Point to status byte */
  3019.     cnt  -= 4;
  3020.  
  3021.     if(cnt-- < 1) {
  3022.         auth_finished(AUTH_REJECT);
  3023.         return AUTH_FAILURE;
  3024.     }
  3025.  
  3026.     switch(*data++) {
  3027.     case SSL_ACCEPT:
  3028.         if (tn_deb || debses)
  3029.             tn_debug("[SSL - handshake starting]");
  3030.         else if ( ssl_verbose_flag )
  3031.             printf("[SSL - handshake starting]\r\n");
  3032.         debug(F110,"ssl_reply","[SSL - handshake starting]",0);
  3033.  
  3034.         /* right ... now we drop into the SSL library */
  3035.         if (!ssl_only_flag) {
  3036.             if (ssl_dummy_flag) {
  3037.                 if (tn_deb || debses)
  3038.                     tn_debug("[SSL - Dummy Connected]");
  3039.                 else if ( ssl_verbose_flag ) {
  3040.                     printf("[SSL - Dummy Connected]\r\n");
  3041.                 }
  3042.                 debug(F110,"ssl_reply","[SSL - Dummy Connected]",0);
  3043.                 auth_finished(AUTH_UNKNOWN);
  3044.                 accept_complete = 1;
  3045.                 return AUTH_SUCCESS;
  3046.             }
  3047.  
  3048.             if (SSL_connect(ssl_con) <= 0) {
  3049.                 int len;
  3050.                 if (tn_deb || debses) {
  3051.                     tn_debug("[SSL - FAILED]");
  3052.                     ERR_print_errors(bio_err);
  3053.                     len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3054.                     ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3055.                     printf(ssl_err);
  3056.                 } else if ( ssl_verbose_flag ) {
  3057.                     printf("[SSL - FAILED]\r\n");
  3058.                     ERR_print_errors(bio_err);
  3059.                     len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3060.                     ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3061.                     printf(ssl_err);
  3062.                 }
  3063.                 debug(F110,"ssl_reply","[SSL - FAILED]",0);
  3064.                 auth_finished(AUTH_REJECT);
  3065.                 ttclos(0);
  3066.                 return AUTH_FAILURE;
  3067.             } else {
  3068.                 if (tn_deb || debses)
  3069.                     tn_debug("[SSL - OK]");
  3070.                 else if ( ssl_verbose_flag ) {
  3071.                     printf("[SSL - OK]\r\n");
  3072.                 }
  3073.                 debug(F110,"ssl_reply","[SSL - OK]",0);
  3074.  
  3075.                 ssl_active_flag = 1;
  3076.                 ssl_display_connect_details(ssl_con,0,ssl_verbose_flag);
  3077.             }
  3078.         }
  3079.         auth_finished(AUTH_UNKNOWN);
  3080.         accept_complete = 1;
  3081.         break;
  3082.  
  3083.     case SSL_REJECT:
  3084.         if (tn_deb || debses) {
  3085.             tn_debug(
  3086.                  "[SSL - failed to switch on SSL - trying plaintext login]");
  3087.         } else if ( ssl_verbose_flag ) {
  3088.             printf("[SSL - failed to switch on SSL]\r\n");
  3089.             printf("Trying plaintext login:\r\n");
  3090.         }
  3091.         debug(F110,"ssl_reply","[SSL - failed to switch on SSL]",0);
  3092.         auth_finished(AUTH_REJECT);
  3093.         return AUTH_FAILURE;
  3094.  
  3095.     default:
  3096.         return AUTH_FAILURE;
  3097.     }
  3098.     return AUTH_SUCCESS;
  3099. }
  3100.  
  3101. int
  3102. #ifdef CK_ANSIC
  3103. ssl_is(unsigned char *data, int cnt)
  3104. #else
  3105. ssl_is(data,cnt) unsigned char *data; int cnt;
  3106. #endif
  3107. {
  3108.     if ((cnt -= 4) < 1)
  3109.         return AUTH_FAILURE;
  3110.  
  3111.     data += 4;
  3112.     switch(*data++) {
  3113.     case SSL_START:
  3114.         /* server starts the SSL stuff now ... */
  3115.         if (!ssl_only_flag) {
  3116.             if ( !tls_load_certs(ssl_ctx,ssl_con,1) ) {
  3117.                 auth_finished(AUTH_REJECT);
  3118.                 return AUTH_FAILURE;
  3119.             }
  3120.  
  3121.             if (tn_deb || debses)
  3122.                 tn_debug("[SSL - handshake starting]");
  3123.             else if ( ssl_verbose_flag )
  3124.                 printf("[SSL - handshake starting]\r\n");
  3125.             debug(F110,"ssl_is","[SSL - handshake starting]",0);
  3126.  
  3127.             SendSSLAuthSB(SSL_ACCEPT, (void *)0, 0);
  3128.  
  3129.             auth_ssl_valid = 1;
  3130.  
  3131.             if (ssl_dummy_flag) {
  3132.                 if (tn_deb || debses)
  3133.                     tn_debug("[SSL - Dummy Connected]");
  3134.                 else if ( ssl_verbose_flag ) {
  3135.                     printf("[SSL - Dummy Connected]\r\n");
  3136.                 }
  3137.                 debug(F110,"ssl_is","[SSL - Dummy Connected]",0);
  3138.                 accept_complete = 1;
  3139.                 auth_finished(AUTH_UNKNOWN);
  3140.                 return AUTH_SUCCESS;
  3141.             }
  3142.  
  3143.             if (SSL_accept(ssl_con) <= 0) {
  3144.                 char errbuf[1024];
  3145.  
  3146.                 sprintf(errbuf,"[SSL - SSL_accept error: %s",
  3147.                          ERR_error_string(ERR_get_error(),NULL));
  3148.  
  3149.                 if (tn_deb || debses)
  3150.                     tn_debug(errbuf);
  3151.                 else if ( ssl_debug_flag )
  3152.                     printf("%s\r\n",errbuf);
  3153.                 else if ( ssl_verbose_flag )
  3154.                     printf("[SSL - SSL_accept error]\r\n");
  3155.  
  3156.                 debug(F110,"ssl_is",errbuf,0);
  3157.  
  3158.                 auth_finished(AUTH_REJECT);
  3159.                 ttclos(0);
  3160.                 return AUTH_FAILURE;
  3161.             }
  3162.  
  3163.             if (tn_deb || debses)
  3164.                 tn_debug("[SSL - OK]");
  3165.             else if ( ssl_verbose_flag ) {
  3166.                 printf("[SSL - OK]\r\n");
  3167.             }
  3168.             debug(F110,"ssl_is","[SSL - OK]",0);
  3169.             ssl_active_flag = 1;
  3170.             ssl_display_connect_details(ssl_con,1,ssl_verbose_flag);
  3171.  
  3172.             /* now check to see that we got exactly what we
  3173.             * wanted from the caller ... if a certificate is
  3174.             * required then we make 100% sure that we were
  3175.             * given one during the handshake (as it is an optional
  3176.             * part of SSL)
  3177.             */
  3178.  
  3179. #ifdef SSL_KRB5
  3180.             if ( tls_is_krb5(0) ) {
  3181.                 if (ssl_con->kssl_ctx->client_princ)
  3182.                     debug(F110,"ssl_is KRB5",ssl_con->kssl_ctx->client_princ,0);
  3183.             } else
  3184. #endif /* SSL_KRB5 */
  3185.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  3186.                 X509 * peer = SSL_get_peer_certificate(ssl_con);
  3187.                 if (peer == NULL) {
  3188.                     if (tn_deb || debses)
  3189.                         tn_debug("[SSL - peer check failed]");
  3190.                     else if (ssl_debug_flag)
  3191.                         printf("[SSL - peer check failed]\r\n");
  3192.                     debug(F110,"ssl_is","[SSL - peer check failed]",0);
  3193.  
  3194.                     /* LOGGING REQUIRED HERE! */
  3195.                     auth_finished(AUTH_REJECT);
  3196.                     return AUTH_FAILURE;
  3197.                 }
  3198.             }
  3199.             auth_finished(AUTH_UNKNOWN);
  3200.             accept_complete = 1;
  3201.         }
  3202.         break;
  3203.  
  3204.     default:
  3205.         SendSSLAuthSB(SSL_REJECT, (void *) "Unknown option received", -1);
  3206.         if (tn_deb || debses)
  3207.             tn_debug("[SSL - Unknown option received]");
  3208.         else
  3209.             printf("Unknown SSL option %d\r\n", data[-1]);
  3210.         debug(F111,"ssl_is","[SSL - Unknown option received]",data[-1]);
  3211.         auth_ssl_valid = 0;
  3212.         auth_finished(AUTH_REJECT);
  3213.         return(AUTH_FAILURE);
  3214.     }
  3215.     return AUTH_SUCCESS;
  3216. }
  3217.  
  3218. #endif /* CK_AUTHENTICATION */
  3219.  
  3220. int
  3221. ck_tn_tls_negotiate(VOID)
  3222. {
  3223.     X509 * peer = NULL;
  3224.     char str[256], *uid=NULL;
  3225.     extern int sstelnet;
  3226.  
  3227.     if ( !ck_ssleay_is_installed() )
  3228.         return(-1);
  3229.  
  3230.     if (sstelnet) {
  3231.         /* server starts the TLS stuff now ... */
  3232.         if (!tls_only_flag) {
  3233.             if ( !tls_load_certs(tls_ctx,tls_con,1) ) {
  3234.                 auth_finished(AUTH_REJECT);
  3235.                 return -1;
  3236.             }
  3237.  
  3238.             if (tn_deb || debses)
  3239.                 tn_debug("[TLS - handshake starting]");
  3240.             else if ( ssl_verbose_flag )
  3241.                 printf("[TLS - handshake starting]\r\n");
  3242.             debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
  3243.  
  3244.             if (ssl_dummy_flag) {
  3245.                 if (tn_deb || debses)
  3246.                     tn_debug("[TLS - Dummy Connected]");
  3247.                 else if ( ssl_verbose_flag ) {
  3248.                     printf("[TLS - Dummy Connected]\r\n");
  3249.                 }
  3250.                 debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
  3251.                 accept_complete = 1;
  3252.                 auth_finished(AUTH_REJECT);
  3253.                 return 0;
  3254.             }
  3255.  
  3256.             if (SSL_accept(tls_con) <= 0) {
  3257.                 char errbuf[1024];
  3258.  
  3259.                 sprintf(errbuf,"[TLS - SSL_accept error: %s",
  3260.                          ERR_error_string(ERR_get_error(),NULL));
  3261.  
  3262.                 if (tn_deb || debses)
  3263.                     tn_debug(errbuf);
  3264.                 else if ( ssl_debug_flag )
  3265.                     printf("%s\r\n",errbuf);
  3266.                 else if ( ssl_verbose_flag )
  3267.                     printf("[TLS - SSL_accept error]\r\n");
  3268.  
  3269.                 debug(F110,"ck_tn_tls_negotiate",errbuf,0);
  3270.                 auth_finished(AUTH_REJECT);
  3271.                 return -1;
  3272.             }
  3273.  
  3274.             if (tn_deb || debses)
  3275.                 tn_debug("[TLS - OK]");
  3276.             else if ( ssl_verbose_flag ) {
  3277.                 printf("[TLS - OK]\r\n");
  3278.             }
  3279.  
  3280.             debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
  3281.             tls_active_flag = 1;
  3282.             ssl_display_connect_details(tls_con,1,ssl_verbose_flag);
  3283.  
  3284.  
  3285. #ifdef SSL_KRB5
  3286.             if ( tls_is_krb5(0) ) {
  3287.                 if (tls_con->kssl_ctx->client_princ) {
  3288.                     char *p;
  3289.                     ckstrncpy(szUserNameAuthenticated,
  3290.                                tls_con->kssl_ctx->client_princ,
  3291.                                UIDBUFLEN);
  3292.                     ckstrncpy(szUserNameRequested,
  3293.                                tls_con->kssl_ctx->client_princ,
  3294.                                UIDBUFLEN);
  3295.                     for ( p = szUserNameRequested; *p ; p++ ) {
  3296.                         if ( *p == '@' || *p == '/' ) {
  3297.                             *p = '\0';
  3298.                             break;
  3299.                         }
  3300.                     }
  3301.                 } else {
  3302.                     szUserNameRequested[0] = '\0';
  3303.                     szUserNameAuthenticated[0] = '\0';
  3304.                 }
  3305. #ifdef CK_LOGIN
  3306.                 if (zvuser(szUserNameRequested))
  3307.                     auth_finished(AUTH_VALID);
  3308.                 else
  3309. #endif /* CK_LOGIN */
  3310.                     auth_finished(AUTH_USER);
  3311.             } else
  3312. #endif /* SSL_KRB5 */
  3313.             {
  3314.             /* now check to see that we got exactly what we
  3315.             * wanted from the caller ... if a certificate is
  3316.             * required then we make 100% sure that we were
  3317.             * given one during the handshake (as it is an optional
  3318.             * part of TLS)
  3319.             */
  3320.             peer=SSL_get_peer_certificate(tls_con);
  3321.             if (peer == NULL) {
  3322.                 debug(F100,"SSL_get_peer_certificate() == NULL","",0);
  3323.                 auth_finished(AUTH_REJECT);
  3324.                 if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  3325.                     if (tn_deb || debses)
  3326.                         tn_debug("[TLS - peer check failed]");
  3327.                     else if (ssl_debug_flag) {
  3328.                         printf("[TLS - peer check failed]\r\n");
  3329.                     }
  3330.                     debug(F110,
  3331.                            "ck_tn_tls_negotiate",
  3332.                            "[TLS - peer check failed]",
  3333.                            0
  3334.                            );
  3335.                     /* LOGGING REQUIRED HERE! */
  3336.                     return -1;
  3337.                 }
  3338.             } else {
  3339.                 debug(F100,"SSL_get_peer_certificate() != NULL","",0);
  3340.                 X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3341.                                            NID_commonName,str,
  3342.                                            256
  3343.                                            );
  3344.                 if ( ssl_verbose_flag )
  3345.                     printf("[TLS - commonName=%s]\r\n",str);
  3346.  
  3347.                 X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3348. #ifndef NID_x500UniqueIdentifier
  3349.                                            NID_uniqueIdentifier,
  3350. #else
  3351.                                            NID_x500UniqueIdentifier,
  3352. #endif
  3353.                                            str,
  3354.                                            256
  3355.                                            );
  3356.                 if ( ssl_verbose_flag )
  3357.                     printf("[TLS - uniqueIdentifier=%s]\r\n",str);
  3358.  
  3359.                 /* Try to determine user name */
  3360.                 uid = tls_userid_from_client_cert(tls_con);
  3361.                 if ( uid ) {
  3362.                     /* This code is very questionable.
  3363.                      * How should it behave?
  3364.                      * The client has presented a certificate that
  3365.                      * contains a username.  We have validated the
  3366.                      * certificate but we do not automatically
  3367.                      * log the user in unless there is a .tlslogin
  3368.                      * file.
  3369.                      */
  3370.  
  3371.                     ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
  3372. #ifdef CK_LOGIN
  3373.                     if (zvuser(uid))
  3374.                         auth_finished(AUTH_VALID);
  3375.                     else
  3376. #endif /* CK_LOGIN */
  3377.                         auth_finished(AUTH_USER);
  3378.                 }
  3379.                 else {
  3380.                     szUserNameRequested[0] = '\0';
  3381.                     auth_finished(AUTH_REJECT);
  3382.                 }
  3383.             }
  3384.             }
  3385.         }
  3386.     } else {
  3387.         char * str=NULL;
  3388.  
  3389.         if (tn_deb || debses)
  3390.             tn_debug("[TLS - handshake starting]");
  3391.         else if ( ssl_verbose_flag )
  3392.             printf("[TLS - handshake starting]\r\n");
  3393.         debug(F110,"ck_tn_tls_negotiate","[TLS - handshake starting]",0);
  3394.  
  3395.         /* right ... now we drop into the SSL library */
  3396.         if (!tls_only_flag) {
  3397.             char *subject=NULL, *issuer=NULL, *commonName=NULL, *dNSName=NULL;
  3398.  
  3399.             if (ssl_dummy_flag) {
  3400.                 if (tn_deb || debses)
  3401.                     tn_debug("[TLS - Dummy Connected]");
  3402.                 else if ( ssl_verbose_flag ) {
  3403.                     printf("[TLS - Dummy Connected]\r\n");
  3404.                 }
  3405.                 debug(F110,"ck_tn_tls_negotiate","[TLS - Dummy Connected]",0);
  3406.                 auth_finished(AUTH_REJECT);
  3407.                 accept_complete = 1;
  3408.                 return 0;
  3409.             }
  3410.  
  3411. #ifndef USE_CERT_CB
  3412.             if (!tls_load_certs(tls_ctx,tls_con,0))
  3413.                 return(-1);
  3414. #endif /* USE_CERT_CB */
  3415.             if (SSL_connect(tls_con) <= 0) {
  3416.                 int len;
  3417.                 if (tn_deb || debses) {
  3418.                     tn_debug("[TLS - FAILED]");
  3419.                     ERR_print_errors(bio_err);
  3420.                     len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3421.                     ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3422.                     printf(ssl_err);
  3423.                 } else if ( ssl_verbose_flag ) {
  3424.                     printf("[TLS - FAILED]\r\n");
  3425.                     ERR_print_errors(bio_err);
  3426.                     len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  3427.                     ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  3428.                     printf(ssl_err);
  3429.                 }
  3430.                 debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3431.                 auth_finished(AUTH_REJECT);
  3432.                 return -1;
  3433.             }
  3434.  
  3435.             tls_active_flag = 1;
  3436.             if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER)
  3437.                  && !tls_is_krb5(0)) {
  3438.                 char prmpt[1024];
  3439.                 subject = ssl_get_subject_name(tls_con);
  3440.  
  3441.                 if (!subject) {
  3442.                     if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3443.                     {
  3444.                         if (tn_deb || debses)
  3445.                             tn_debug("[TLS - FAILED]");
  3446.                         else if ( ssl_verbose_flag )
  3447.                             printf("[TLS - FAILED]\r\n");
  3448.                         debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3449.                         auth_finished(AUTH_REJECT);
  3450.                         return -1;
  3451.                     } else {
  3452.                         int ok;
  3453.                         ok = uq_ok("Warning: Server didn't provide a certificate\r\n",
  3454.                                    "Continue? (Y/N)", 3, NULL, 0);
  3455.                         if (!ok) {
  3456.                             if (tn_deb || debses)
  3457.                                 tn_debug("[TLS - FAILED]");
  3458.                             else if ( ssl_verbose_flag )
  3459.                                 printf("[TLS - FAILED]\r\n");
  3460.                             debug(F110,
  3461.                                    "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3462.                             auth_finished(AUTH_REJECT);
  3463.                             return -1;
  3464.                         }
  3465.                     }
  3466.                 } else if (ssl_check_server_name(tls_con, szHostName)) {
  3467.                     if (tn_deb || debses)
  3468.                         tn_debug("[TLS - FAILED]");
  3469.                     else if ( ssl_verbose_flag )
  3470.                         printf("[TLS - FAILED]\r\n");
  3471.                     debug(F110,
  3472.                            "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3473.                     auth_finished(AUTH_REJECT);
  3474.                     return -1;
  3475.                 }
  3476.             }
  3477.  
  3478.             if ( ssl_debug_flag && ssl_finished_messages) {
  3479.                 char msg[32];
  3480.                 int i, len=32;
  3481.                 extern char tn_msg[], hexbuf[];
  3482.  
  3483.                 tn_msg[0] = '\0';
  3484.                 len = ssl_get_client_finished(msg,len);
  3485.                 if ( len > 0 ) {
  3486.                     for ( i=0;i<len;i++ ) {
  3487.                         sprintf(hexbuf,"%02X ",msg[i]);
  3488.                         ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
  3489.                     }
  3490.                     printf("TLS client finished: %s\r\n",tn_msg);
  3491.                 }
  3492.                 tn_msg[0] = '\0';
  3493.                 len = ssl_get_server_finished(msg,len);
  3494.                 if ( len > 0 ) {
  3495.                     for ( i=0;i<len;i++ ) {
  3496.                         sprintf(hexbuf,"%02X ",msg[i]);
  3497.                         ckstrncat(tn_msg,hexbuf,TN_MSG_LEN);
  3498.                     }
  3499.                     printf("TLS server finished: %s\r\n",tn_msg);
  3500.                 }
  3501.             }
  3502.  
  3503.             if (tn_deb || debses)
  3504.                 tn_debug("[TLS - OK]");
  3505.             else if ( ssl_verbose_flag )
  3506.                 printf("[TLS - OK]\r\n");
  3507.             debug(F110,"ck_tn_tls_negotiate","[TLS - OK]",0);
  3508.  
  3509.             ssl_display_connect_details(tls_con,0,ssl_verbose_flag);
  3510.         }
  3511.         auth_finished(AUTH_REJECT);
  3512.     }
  3513.     accept_complete = 1;
  3514.     auth_ssl_valid = 1;
  3515.     return(0);
  3516. }
  3517.  
  3518. int
  3519. ck_ssl_incoming(fd) int fd;
  3520. {
  3521.     /* if we are not running in debug then any error
  3522.     * stuff from SSL debug *must* not go down
  3523.     * the socket (which 0,1,2 are all pointing to by
  3524.     * default)
  3525.     */
  3526.  
  3527.     int timo = 2000;
  3528.  
  3529.     if ( !ck_ssleay_is_installed() )
  3530.         return(-1);
  3531.  
  3532.     /* do the SSL stuff now ... before we play with pty's */
  3533.     SSL_set_fd(ssl_con,fd);
  3534.     SSL_set_fd(tls_con,fd);
  3535.  
  3536.     if (tls_only_flag) {
  3537.         if (tn_deb || debses)
  3538.             tn_debug("[TLS - handshake starting]");
  3539.         else if ( ssl_verbose_flag )
  3540.             printf("[TLS - handshake starting]\r\n");
  3541.         debug(F110,"ck_ssl_incoming","[TLS - handshake starting]",0);
  3542.  
  3543.         /* hmm ... only when running talking to things like
  3544.         * https servers should we hit this code and then
  3545.         * we really don't care *who* we talk to :-)
  3546.         */
  3547.         if (SSL_accept(tls_con) <= 0) {
  3548.             char errbuf[1024];
  3549.  
  3550.             sprintf(errbuf,"[TLS - SSL_accept error: %s",
  3551.                      ERR_error_string(ERR_get_error(),NULL));
  3552.  
  3553.             if (tn_deb || debses)
  3554.                 tn_debug(errbuf);
  3555.             else if ( ssl_debug_flag )
  3556.                 printf("%s\r\n",errbuf);
  3557.             else if ( ssl_verbose_flag )
  3558.                 printf("[TLS - SSL_accept error]\r\n");
  3559.  
  3560.             debug(F110,"ck_ssl_incoming",errbuf,0);
  3561.             return(-1);
  3562.         } else {
  3563.             if (tn_deb || debses)
  3564.                 tn_debug("[TLS - OK]");
  3565.             else if ( ssl_verbose_flag )
  3566.                 printf("[TLS - OK]\r\n");
  3567.             debug(F110,"ck_ssl_incoming","[TLS - OK]",0);
  3568.             tls_active_flag = 1;
  3569.         }
  3570.     } else if (ssl_only_flag) {
  3571.         if (tn_deb || debses)
  3572.             tn_debug("[SSL - handshake starting]");
  3573.         else if ( ssl_verbose_flag )
  3574.             printf("[SSL - handshake starting]\r\n");
  3575.         debug(F110,"ck_ssl_incoming","[SSL - handshake starting]",0);
  3576.  
  3577.         /* hmm ... only when running talking to things like
  3578.          * https servers should we hit this code and then
  3579.          * we really don't care *who* we talk to :-)
  3580.          */
  3581.         if (SSL_accept(ssl_con) <= 0) {
  3582.             char errbuf[1024];
  3583.  
  3584.             sprintf(errbuf,"[SSL - SSL_accept error: %s",
  3585.                      ERR_error_string(ERR_get_error(),NULL));
  3586.  
  3587.             if (tn_deb || debses)
  3588.                 tn_debug(errbuf);
  3589.             else if ( ssl_debug_flag )
  3590.                 printf("%s\r\n",errbuf);
  3591.             else if ( ssl_verbose_flag )
  3592.                 printf("[SSL - SSL_accept error]\r\n");
  3593.  
  3594.             debug(F110,"ck_ssl_incoming",errbuf,0);
  3595.             return(-1);
  3596.         } else {
  3597.             if (tn_deb || debses)
  3598.                 tn_debug("[SSL - OK]");
  3599.             else if ( ssl_verbose_flag )
  3600.             printf("[SSL - OK]\r\n");
  3601.             debug(F110,"ssl_is","[SSL - OK]",0);
  3602.             ssl_active_flag = 1;
  3603.         }
  3604.     }
  3605.     if (ssl_active_flag || tls_active_flag) {
  3606.         X509 *peer;
  3607.         char str[256], *uid=NULL;
  3608.  
  3609.         /* now check to see that we got exactly what we
  3610.          * wanted from the caller ... if a certificate is
  3611.          * required then we make 100% sure that we were
  3612.          * given on during the handshake (as it is an optional
  3613.          * part of SSL and TLS)
  3614.          */
  3615.  
  3616.         if ( tls_active_flag ) {
  3617.             peer=SSL_get_peer_certificate(tls_con);
  3618.         } else if ( ssl_active_flag ) {
  3619.             peer=SSL_get_peer_certificate(ssl_con);
  3620.         }
  3621.  
  3622.         if (peer == NULL) {
  3623.             debug(F100,"SSL_get_peer_certificate() == NULL","",0);
  3624.             auth_finished(AUTH_REJECT);
  3625.  
  3626.             if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  3627.                 if (tn_deb || debses)
  3628.                     tn_debug("[SSL/TLS - peer check failed]");
  3629.                 else if (ssl_debug_flag) {
  3630.                     printf("[SSL/TLS - peer check failed]\r\n");
  3631.                 }
  3632.                 debug(F110,
  3633.                        "ck_tn_tls_negotiate",
  3634.                        "[SSL/TLS - peer check failed]",
  3635.                        0
  3636.                        );
  3637.                 /* LOGGING REQUIRED HERE! */
  3638.                 return -1;
  3639.             }
  3640.  
  3641.         } else {
  3642.             debug(F100,"SSL_get_peer_certificate() != NULL","",0);
  3643.             X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3644.                                        NID_commonName,str,
  3645.                                        256
  3646.                                        );
  3647.             printf("[TLS - commonName=%s]\r\n",str);
  3648.  
  3649.             X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
  3650. #ifndef NID_x500UniqueIdentifier
  3651.                                        NID_uniqueIdentifier,
  3652. #else   
  3653.                                        NID_x500UniqueIdentifier,
  3654. #endif
  3655.                                        str,256
  3656.                                        );
  3657.             printf("[TLS - uniqueIdentifier=%s]\r\n",str);
  3658.  
  3659.             /* Try to determine user name */
  3660.             uid = tls_userid_from_client_cert(tls_con);
  3661.             if ( uid ) {
  3662.                 /* This code is very questionable.
  3663.                 * How should it behave?
  3664.                 * The client has presented a certificate that
  3665.                 * contains a username.  We have validated the
  3666.                 * certificate but we do not automatically
  3667.                 * log the user in unless there is a .tlslogin
  3668.                 * file.
  3669.                 */
  3670.  
  3671.                 ckstrncpy(szUserNameRequested,uid,UIDBUFLEN);
  3672. #ifdef CK_LOGIN
  3673.                 if (zvuser(uid))
  3674.                     auth_finished(AUTH_VALID);
  3675.                 else
  3676. #endif /* CK_LOGIN */
  3677.                     auth_finished(AUTH_USER);
  3678.             }
  3679.             else {
  3680.                 szUserNameRequested[0] = '\0';
  3681.                 auth_finished(AUTH_REJECT);
  3682.             }
  3683.         }
  3684.     }
  3685.     return(0);  /* success */
  3686. }
  3687.  
  3688. int
  3689. ck_ssl_outgoing(fd) int fd;
  3690. {
  3691.     int timo = 2000;
  3692.  
  3693.     if ( !ck_ssleay_is_installed() )
  3694.         return(-1);
  3695.  
  3696.     /* bind in the network descriptor */
  3697.     SSL_set_fd(ssl_con,fd);
  3698.     SSL_set_fd(tls_con,fd);
  3699.  
  3700.     /* If we are doing raw TLS then start it now ... */
  3701.     if (tls_only_flag) {
  3702. #ifndef USE_CERT_CB
  3703.         if (!tls_load_certs(tls_ctx,tls_con,0)) {
  3704.             debug(F110,"ck_ssl_outgoing","tls_load_certs() failed",0);
  3705.             return(-1);
  3706.         }
  3707. #endif /* USE_CERT_CB */
  3708.         if (tn_deb || debses)
  3709.             tn_debug("[TLS - handshake starting]");
  3710.         else if (ssl_verbose_flag)
  3711.             printf("[TLS - handshake starting]\r\n");
  3712.         debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
  3713.         if (SSL_connect(tls_con) <= 0) {
  3714.             char errbuf[1024];
  3715.  
  3716.             sprintf(errbuf,"[TLS - SSL_connect error: %s",
  3717.                      ERR_error_string(ERR_get_error(),NULL));
  3718.  
  3719.             if (tn_deb || debses)
  3720.                 tn_debug(errbuf);
  3721.             else if ( ssl_debug_flag )
  3722.                 printf("%s\r\n",errbuf);
  3723.  
  3724.             if (tn_deb || debses)
  3725.                 tn_debug("[TLS - FAILED]");
  3726.             else if ( ssl_verbose_flag )
  3727.                 printf("[TLS - FAILED]\r\n");
  3728.             debug(F110,"ck_ssl_outgoing","[TLS - FAILED]",0);
  3729.             netclos();
  3730.             return(-1);
  3731.         } else {
  3732.             tls_active_flag = 1;
  3733.             if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) && 
  3734.                  !tls_is_krb5(0) ) {
  3735.                 char *subject = ssl_get_subject_name(tls_con);
  3736.  
  3737.                 if (!subject) {
  3738.                     if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3739.                     {
  3740.                         if (tn_deb || debses)
  3741.                             tn_debug("[TLS - FAILED]");
  3742.                         else if ( ssl_verbose_flag )
  3743.                             printf("[TLS - FAILED]\r\n");
  3744.                         debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3745.  
  3746.                         auth_finished(AUTH_REJECT);
  3747.                         return -1;
  3748.                     } else {
  3749.                         char prmpt[1024];
  3750.                         int ok;
  3751.                         ok = uq_ok("Warning: Server didn't provide a certificate\r\n",
  3752.                                    "Continue? (Y/N)", 3, NULL, 0);
  3753.                         if (!ok) {
  3754.                             if (tn_deb || debses)
  3755.                                 tn_debug("[TLS - FAILED]");
  3756.                             else if ( ssl_verbose_flag )
  3757.                                 printf("[TLS - FAILED]\r\n");
  3758.                             debug(F110,
  3759.                                    "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3760.                             auth_finished(AUTH_REJECT);
  3761.                             return -1;
  3762.                         }
  3763.                     }
  3764.                 } else if (ssl_check_server_name(tls_con, szHostName)) {
  3765.                     if (tn_deb || debses)
  3766.                         tn_debug("[TLS - FAILED]");
  3767.                     else if ( ssl_verbose_flag )
  3768.                         printf("[TLS - FAILED]\r\n");
  3769.                     debug(F110,
  3770.                            "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3771.                     auth_finished(AUTH_REJECT);
  3772.                     return -1;
  3773.                 }
  3774.             }
  3775.  
  3776.             printf("[TLS - OK]\r\n");
  3777.             if (tn_deb || debses)
  3778.                 tn_debug("[TLS - OK]");
  3779.             debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
  3780.             ssl_display_connect_details(tls_con,0,ssl_verbose_flag);
  3781.         }
  3782.     }
  3783.     /* if we are doing raw SSL then start it now ... */
  3784.     else if (ssl_only_flag) {
  3785. #ifndef USE_CERT_CB
  3786.         if (!tls_load_certs(ssl_ctx,ssl_con,0))
  3787.             return(-1);
  3788. #endif /* USE_CERT_CB */
  3789.         if (tn_deb || debses)
  3790.             tn_debug("[SSL - handshake starting]");
  3791.         else if ( ssl_verbose_flag )
  3792.             printf("[SSL - handshake starting]\r\n");
  3793.         debug(F110,"ck_ssl_outgoing","[SSL - handshake starting]",0);
  3794.         if (SSL_connect(ssl_con) <= 0) {
  3795.             if ( ssl_debug_flag ) {
  3796.                 char errbuf[1024];
  3797.  
  3798.                 sprintf(errbuf,"[SSL - SSL_connect error: %s",
  3799.                          ERR_error_string(ERR_get_error(),NULL));
  3800.                 printf("%s\r\n",errbuf);
  3801.             }
  3802.             if (tn_deb || debses)
  3803.                 tn_debug("[SSL - FAILED]");
  3804.             else if ( ssl_verbose_flag )
  3805.                 printf("[SSL - FAILED]\r\n");
  3806.             debug(F110,"ck_ssl_outgoing","[SSL - FAILED]",0);
  3807.             return(-1);
  3808.         } else {
  3809.             ssl_active_flag = 1;
  3810.  
  3811.             if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
  3812.                  !tls_is_krb5(0)) {
  3813.                 char *subject = ssl_get_subject_name(ssl_con);
  3814.  
  3815.                 if (!subject) {
  3816.                     if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3817.                     {
  3818.                         if (tn_deb || debses)
  3819.                             tn_debug("[SSL - FAILED]");
  3820.                         else if ( ssl_verbose_flag )
  3821.                             printf("[SSL - FAILED]\r\n");
  3822.                         debug(F110,"ck_tn_tls_negotiate","[SSL - FAILED]",0);
  3823.  
  3824.                         auth_finished(AUTH_REJECT);
  3825.                         return -1;
  3826.                     } else {
  3827.                         char prmpt[1024];
  3828.                         int ok;
  3829.                         ok = uq_ok("Warning: Server didn't provide a certificate\r\n",
  3830.                                    "Continue? (Y/N)", 3, NULL, 0);
  3831.                         if (!ok) {
  3832.                             if (tn_deb || debses)
  3833.                                 tn_debug("[SSL - FAILED]");
  3834.                             else if ( ssl_verbose_flag )
  3835.                                 printf("[SSL - FAILED]\r\n");
  3836.                             debug(F110,
  3837.                                    "ck_tn_tls_negotiate","[SSL - FAILED]",0);
  3838.                             auth_finished(AUTH_REJECT);
  3839.                             return -1;
  3840.                         }
  3841.                     }
  3842.                 } else if (ssl_check_server_name(ssl_con, szHostName)) {
  3843.                     if (tn_deb || debses)
  3844.                         tn_debug("[SSL - FAILED]");
  3845.                     else if ( ssl_verbose_flag )
  3846.                         printf("[SSL - FAILED]\r\n");
  3847.                     debug(F110, "ck_tn_tls_negotiate","[SSL - FAILED]",0);
  3848.                     auth_finished(AUTH_REJECT);
  3849.                     return -1;
  3850.                 }
  3851.             }
  3852.  
  3853.             printf("[SSL - OK]\r\n");
  3854.             if (tn_deb || debses)
  3855.                 tn_debug("[SSL - OK]");
  3856.             debug(F110,"ck_ssl_outgoing","[SSL - OK]",0);
  3857.             ssl_display_connect_details(ssl_con,0,ssl_verbose_flag);
  3858.         }
  3859.     }
  3860.     return(0);  /* success */
  3861. }
  3862.  
  3863. #ifndef NOHTTP
  3864. int
  3865. ck_ssl_http_client(fd, hostname) int fd; char * hostname;
  3866. {
  3867.     int timo = 2000;
  3868.  
  3869.     if ( !ck_ssleay_is_installed() )
  3870.         return(-1);
  3871.  
  3872.     /* bind in the network descriptor */
  3873.     SSL_set_fd(tls_http_con,fd);
  3874.  
  3875.     /* If we are doing raw TLS then start it now ... */
  3876.     if (1) {
  3877. #ifndef USE_CERT_CB
  3878.         if (!tls_load_certs(tls_http_ctx,tls_http_con,0)) {
  3879.             debug(F110,"ck_ssl_http_client","tls_load_certs() failed",0);
  3880.             return(-1);
  3881.         }
  3882. #endif /* USE_CERT_CB */
  3883.         if (tn_deb || debses)
  3884.             tn_debug("[TLS - handshake starting]");
  3885.         else if (ssl_verbose_flag)
  3886.             printf("[TLS - handshake starting]\r\n");
  3887.         debug(F110,"ck_ssl_outgoing","[TLS - handshake starting]",0);
  3888.         if (SSL_connect(tls_http_con) <= 0) {
  3889.             char errbuf[1024];
  3890.  
  3891.             sprintf(errbuf,"[TLS - SSL_connect error: %s",
  3892.                      ERR_error_string(ERR_get_error(),NULL));
  3893.  
  3894.             if (tn_deb || debses)
  3895.                 tn_debug(errbuf);
  3896.             else if ( ssl_debug_flag )
  3897.                 printf("%s\r\n",errbuf);
  3898.  
  3899.             if (tn_deb || debses)
  3900.                 tn_debug("[TLS - FAILED]");
  3901.             else if ( ssl_verbose_flag )
  3902.                 printf("[TLS - FAILED]\r\n");
  3903.             debug(F110,"ck_ssl_http_client","[TLS - FAILED]",0);
  3904.             http_close();
  3905.             return(-1);
  3906.         } else {
  3907.             tls_http_active_flag = 1;
  3908.             if ( !ssl_certsok_flag && (ssl_verify_flag & SSL_VERIFY_PEER) &&
  3909.                  !tls_is_krb5(3) ) {
  3910.                 char *subject = ssl_get_subject_name(tls_http_con);
  3911.  
  3912.                 if (!subject) {
  3913.                     if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3914.                     {
  3915.                         if (tn_deb || debses)
  3916.                             tn_debug("[TLS - FAILED]");
  3917.                         else if ( ssl_verbose_flag )
  3918.                             printf("[TLS - FAILED]\r\n");
  3919.                         debug(F110,"ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3920.                         return -1;
  3921.                     } else {
  3922.                         char prmpt[1024];
  3923.                         int ok;
  3924.                         ok = uq_ok("Warning: Server didn't provide a certificate\r\n",
  3925.                                    "Continue? (Y/N)", 3, NULL, 0);
  3926.                         if (!ok) {
  3927.                             if (tn_deb || debses)
  3928.                                 tn_debug("[TLS - FAILED]");
  3929.                             else if ( ssl_verbose_flag )
  3930.                                 printf("[TLS - FAILED]\r\n");
  3931.                             debug(F110,
  3932.                                    "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3933.                             return -1;
  3934.                         }
  3935.                     }
  3936.                 } else if (ssl_check_server_name(tls_http_con, hostname)) {
  3937.                     if (tn_deb || debses)
  3938.                         tn_debug("[TLS - FAILED]");
  3939.                     else if ( ssl_verbose_flag )
  3940.                         printf("[TLS - FAILED]\r\n");
  3941.                     debug(F110,
  3942.                            "ck_tn_tls_negotiate","[TLS - FAILED]",0);
  3943.                     return -1;
  3944.                 }
  3945.             }
  3946.  
  3947.             printf("[TLS - OK]\r\n");
  3948.             if (tn_deb || debses)
  3949.                 tn_debug("[TLS - OK]");
  3950.             debug(F110,"ck_ssl_outgoing","[TLS - OK]",0);
  3951.             ssl_display_connect_details(tls_http_con,0,ssl_verbose_flag);
  3952.         }
  3953.     }
  3954.     return(0);  /* success */
  3955. }
  3956. #endif /* NOHTTP */
  3957. int
  3958. ck_ssl_renegotiate_ciphers()
  3959. {
  3960.  
  3961.     if ( !ck_ssleay_is_installed() )
  3962.         return(0);
  3963.  
  3964.     if ( !sstelnet )
  3965.         return(0);
  3966.  
  3967.     if ( ssl_active_flag )
  3968.         return SSL_renegotiate(ssl_con);
  3969.     else if ( tls_active_flag )
  3970.         return SSL_renegotiate(tls_con);
  3971.     return(0);
  3972. }
  3973.  
  3974. #ifndef OS2
  3975. /* The following function should be replaced by institution specific */
  3976. /* code that will convert an X509 cert structure to a userid for the */
  3977. /* purposes of client to host login.  The example code included      */
  3978. /* simply returns the UID field of the Subject if it exists.         */
  3979.  
  3980. /* X509_to_user() returns 0 if valid userid in 'userid', else -1 */
  3981. int
  3982. X509_to_user(X509 *peer_cert, char *userid, int len)
  3983. {
  3984. #ifdef X509_UID_TO_USER
  3985.     /* BEGIN EXAMPLE */
  3986.     int err;
  3987.  
  3988.     if (!(peer_cert && userid) || len <= 0)
  3989.         return -1;
  3990.  
  3991.     userid[0] = '\0';
  3992.     debug(F110,"X509_to_user() subject",
  3993.            X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
  3994.  
  3995.     /* userid is in cert subject /UID */
  3996.     err = X509_NAME_get_text_by_NID(X509_get_subject_name(peer_cert),
  3997. #ifndef NID_x500UniqueIdentifier
  3998.                                      NID_uniqueIdentifier,
  3999. #else
  4000.                                      NID_x500UniqueIdentifier,
  4001. #endif
  4002.                                      userid, len);
  4003.  
  4004.     debug(F111,"X509_to_user() userid",userid,err);
  4005.     if (err > 0)
  4006.         return 0;
  4007.  
  4008.     /* END EXAMPLE */
  4009. #else /* X509_UID_TO_USER */
  4010. #ifdef X509_SUBJECT_ALT_NAME_TO_USER
  4011.     /* BEGIN EXAMPLE */
  4012.     int i;
  4013.     X509_EXTENSION *ext = NULL;
  4014.     STACK_OF(GENERAL_NAME) *ialt = NULL;
  4015.     GENERAL_NAME *gen = NULL;
  4016.     char email[256];
  4017.  
  4018.     if (!(peer_cert && userid) || len <= 0)
  4019.         return -1;
  4020.  
  4021.     userid[0] = '\0';
  4022.     email[0] = '\0';
  4023.     debug(F110,"X509_to_user() subject",
  4024.            X509_NAME_oneline(X509_get_subject_name(peer_cert),NULL,0),0);
  4025.  
  4026.     if ((i = X509_get_ext_by_NID(server_cert, NID_subject_alt_name, -1))<0)
  4027.         return -1;
  4028.     if (!(ext = X509_get_ext(server_cert, i)))
  4029.         return -1;
  4030.     X509V3_add_standard_extensions();
  4031.     if (!(ialt = X509V3_EXT_d2i(ext)))
  4032.         return -1;
  4033.     for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
  4034.         gen = sk_GENERAL_NAME_value(ialt, i);
  4035.         if (gen->type == GEN_DNS) {
  4036.             if(!gen->d.ia5 || !gen->d.ia5->length)
  4037.                 break;
  4038.             if ( gen->d.ia5->length + 1 > sizeof(email) ) {
  4039.                 goto cleanup;
  4040.             }
  4041.             memcpy(email, gen->d.ia5->data, gen->d.ia5->length);
  4042.             email[gen->d.ia5->length] = 0;
  4043.             break;
  4044.         }
  4045.     }
  4046.   cleanup:
  4047.     X509V3_EXT_cleanup();
  4048.     if (ialt)
  4049.         sk_GENERAL_NAME_free(ialt);
  4050.  
  4051.     debug(F110,"X509_to_user() email",email,0);
  4052.  
  4053.     if ( email[0] ) {
  4054.         char * domain = NULL;
  4055.  
  4056.         /* Find domain */
  4057.         for ( i=0 ; email[i] ; i++ ) {
  4058.             if ( email[i] == '@' ) {
  4059.                 email[i] = '\0';
  4060.                 domain = &email[i+1];
  4061.                 break;
  4062.             }
  4063.         }
  4064.  
  4065.         if ( domain ) {
  4066.             /* XXX - Put code to Verify domain here */
  4067.  
  4068.             if ( /* domain is okay */ 1 )
  4069.                 ckstrncpy(userid,email,len);
  4070.         }
  4071.     }
  4072.  
  4073.     return(userid[0] ? 0 : -1);
  4074.     /* END EXAMPLE */
  4075. #endif /* X509_SUBJECT_ALT_NAME_TO_USER */
  4076. #endif /* X509_UID_TO_USER */
  4077.     return -1;
  4078. }
  4079.  
  4080. /* The following function should be replaced by institution specific */
  4081. /* code that will determine whether or not the combination of the    */
  4082. /* provided X509 certificate and username is valid for automatic     */
  4083. /* login.   Whereas X509_to_user() is used to provide authentication */
  4084. /* of the user, the X509_userok() function is used to provide        */
  4085. /* authorization.  The certificate passed into X509_userok() does    */
  4086. /* need to map to a userid; nor would the userid it would map to     */
  4087. /* need to match the userid provided to the function.  There are     */
  4088. /* numerous circumstances in which it is beneficial to have the ability */
  4089. /* for multiple users to gain access to a common account such as     */
  4090. /* 'root' on Unix; or a class account on a web server.  In Unix we   */
  4091. /* implement this capability with the ~userid/.tlslogin file which   */
  4092. /* a list of X509 certificates which may be used to access the       */
  4093. /* account 'userid'.                                                 */
  4094.  
  4095. /* X509_to_user() returns 0 if access is denied; 1 is access is permitted */
  4096. int
  4097. X509_userok(X509 * peer_cert, const char * userid)
  4098. {
  4099.     /* check if clients cert is in "user"'s ~/.tlslogin file */
  4100.     char buf[512];
  4101.     int r = 0;
  4102.     FILE *fp;
  4103.     struct passwd *pwd;
  4104.     X509 *file_cert;
  4105.  
  4106.     if ( peer_cert == NULL )
  4107.         return(0);
  4108.  
  4109.     if (!(pwd = getpwnam(userid)))
  4110.        return 0;
  4111.     if (strlen(pwd->pw_dir) > 500)
  4112.        return(0);
  4113.     sprintf(buf, "%s/.tlslogin", pwd->pw_dir);
  4114.  
  4115.     if (!(fp = fopen(buf, "r")))
  4116.         return 0;
  4117.     while (!r && (file_cert = PEM_read_X509(fp, NULL, NULL, NULL))) {
  4118.         if (!ASN1_STRING_cmp(peer_cert->signature, file_cert->signature))
  4119.             r = 1;
  4120.         X509_free(file_cert);
  4121.     }
  4122.     fclose(fp);
  4123.     return(r);
  4124. }
  4125.  
  4126. #endif /* OS2 */
  4127. #endif /* CK_SSL */
  4128.