home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv208.zip / ck_ssl.c < prev    next >
C/C++ Source or Header  |  2003-03-14  |  147KB  |  4,184 lines

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