home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku201.zip / ck_ssl.c < prev    next >
C/C++ Source or Header  |  2002-01-31  |  139KB  |  3,993 lines

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