6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
26 void cbtls_info(const SSL *s, int where, int ret)
28 const char *str, *state;
31 w = where & ~SSL_ST_MASK;
32 if (w & SSL_ST_CONNECT) str=" TLS_connect";
33 else if (w & SSL_ST_ACCEPT) str=" TLS_accept";
36 state = SSL_state_string_long(s);
37 state = state ? state : "NULL";
39 if (where & SSL_CB_LOOP) {
40 if (debug_flag) radlog(L_INFO, "%s: %s\n", str, state);
41 } else if (where & SSL_CB_HANDSHAKE_START) {
42 if (debug_flag) radlog(L_INFO, "%s: %s\n", str, state);
43 } else if (where & SSL_CB_HANDSHAKE_DONE) {
44 radlog(L_INFO, "%s: %s\n", str, state);
45 } else if (where & SSL_CB_ALERT) {
46 str=(where & SSL_CB_READ)?"read":"write";
47 radlog(L_ERR,"TLS Alert %s:%s:%s\n", str,
48 SSL_alert_type_string_long(ret),
49 SSL_alert_desc_string_long(ret));
50 } else if (where & SSL_CB_EXIT) {
52 radlog(L_ERR, "%s:failed in %s\n", str, state);
54 radlog(L_ERR, "%s:error in %s\n", str, state);
59 * Before trusting a certificate, you must make sure that the
60 * certificate is 'valid'. There are several steps that your
61 * application can take in determining if a certificate is
62 * valid. Commonly used steps are:
64 * 1.Verifying the certificate's signature, and verifying that
65 * the certificate has been issued by a trusted Certificate
68 * 2.Verifying that the certificate is valid for the present date
69 * (i.e. it is being presented within its validity dates).
71 * 3.Verifying that the certificate has not been revoked by its
72 * issuing Certificate Authority, by checking with respect to a
73 * Certificate Revocation List (CRL).
75 * 4.Verifying that the credentials presented by the certificate
76 * fulfill additional requirements specific to the application,
77 * such as with respect to access control lists or with respect
78 * to OCSP (Online Certificate Status Processing).
80 * NOTE: This callback will be called multiple times based on the
81 * depth of the root certificate chain
83 int cbtls_verify(int ok, X509_STORE_CTX *ctx)
85 char subject[256]; /* Used for the subject name */
86 char issuer[256]; /* Used for the issuer name */
88 char *user_name = NULL; /* User-Name */
94 client_cert = X509_STORE_CTX_get_current_cert(ctx);
95 err = X509_STORE_CTX_get_error(ctx);
96 depth = X509_STORE_CTX_get_error_depth(ctx);
99 radlog(L_ERR,"--> verify error:num=%d:%s\n",err,
100 X509_verify_cert_error_string(err));
102 * Catch too long Certificate chains
106 * Retrieve the pointer to the SSL of the connection currently treated
107 * and the application specific data stored into the SSL object.
109 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
110 user_name = (char *)SSL_get_ex_data(ssl, data_index);
113 * Get the Subject & Issuer
115 subject[0] = issuer[0] = '\0';
116 X509_NAME_oneline(X509_get_subject_name(client_cert), subject, 256);
117 X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer, 256);
120 * Get the Common Name
122 X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
123 NID_commonName, buf, 256);
125 switch (ctx->error) {
127 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
128 radlog(L_ERR, "issuer= %s\n", issuer);
130 case X509_V_ERR_CERT_NOT_YET_VALID:
131 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
132 radlog(L_ERR, "notBefore=");
134 ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
137 case X509_V_ERR_CERT_HAS_EXPIRED:
138 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
139 radlog(L_ERR, "notAfter=");
141 ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
146 if (debug_flag > 0) {
147 radlog(L_INFO, "chain-depth=%d, ", depth);
153 radlog(L_INFO, "error=%d", err);
155 radlog(L_INFO, "--> User-Name = %s", user_name);
156 radlog(L_INFO, "--> BUF-Name = %s", buf);
157 radlog(L_INFO, "--> subject = %s", subject);
158 radlog(L_INFO, "--> issuer = %s", issuer);
159 radlog(L_INFO, "--> verify return:%d", ok);
166 * Fill in our 'info' with TLS data.
168 void cbtls_msg(int write_p, int msg_version, int content_type,
169 const void *buf, size_t len,
170 SSL *ssl UNUSED, void *arg)
172 tls_session_t *state = (tls_session_t *)arg;
174 state->info.origin = (unsigned char)write_p;
175 state->info.content_type = (unsigned char)content_type;
176 state->info.record_len = len;
177 state->info.version = msg_version;
178 state->info.initialized = 1;
180 if (content_type == SSL3_RT_ALERT) {
181 state->info.alert_level = ((const unsigned char*)buf)[0];
182 state->info.alert_description = ((const unsigned char*)buf)[1];
183 state->info.handshake_type = 0x00;
185 } else if (content_type == SSL3_RT_HANDSHAKE) {
186 state->info.handshake_type = ((const unsigned char*)buf)[0];
187 state->info.alert_level = 0x00;
188 state->info.alert_description = 0x00;
190 tls_session_information(state);
193 int cbtls_password(char *buf,
198 strcpy(buf, (char *)userdata);
199 return(strlen((char *)userdata));
202 RSA *cbtls_rsa(SSL *s UNUSED, int is_export UNUSED, int keylength)
204 static RSA *rsa_tmp=NULL;
206 if (rsa_tmp == NULL) {
207 radlog(L_INFO, "Generating temp (%d bit) RSA key...", keylength);
208 rsa_tmp=RSA_generate_key(keylength, RSA_F4, NULL, NULL);
213 #endif /* !defined(NO_OPENSSL) */