home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ck208 / cku208.tar.gz / cku208.tar / ck_ssl.c < prev    next >
C/C++ Source or Header  |  2003-02-27  |  141KB  |  4,136 lines

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