import from HEAD:
[freeradius.git] / src / modules / rlm_eap / libeap / cb.c
1 /*
2  * cb.c
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
21  */
22 #include "eap_tls.h"
23
24 #ifndef NO_OPENSSL
25
26 void cbtls_info(const SSL *s, int where, int ret)
27 {
28         const char *str, *state;
29         int w;
30
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";
34         else str="    (other)";
35
36         state = SSL_state_string_long(s);
37         state = state ? state : "NULL";
38
39         if (where & SSL_CB_LOOP) {
40                 DEBUG2("%s: %s\n", str, state);
41         } else if (where & SSL_CB_HANDSHAKE_START) {
42                 DEBUG2("%s: %s\n", str, state);
43         } else if (where & SSL_CB_HANDSHAKE_DONE) {
44                 DEBUG2("%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) {
51                 if (ret == 0)
52                         radlog(L_ERR, "%s:failed in %s\n", str, state);
53                 else if (ret < 0)
54                         radlog(L_ERR, "%s:error in %s\n", str, state);
55         }
56 }
57
58 /*
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:
63  *
64  *      1.Verifying the certificate's signature, and verifying that
65  *      the certificate has been issued by a trusted Certificate
66  *      Authority.
67  *
68  *      2.Verifying that the certificate is valid for the present date
69  *      (i.e. it is being presented within its validity dates).
70  *
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).
74  *
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).
79  *
80  *      NOTE: This callback will be called multiple times based on the
81  *      depth of the root certificate chain
82  */
83 int cbtls_verify(int ok, X509_STORE_CTX *ctx)
84 {
85         char subject[256]; /* Used for the subject name */
86         char issuer[256]; /* Used for the issuer name */
87         char buf[256];
88         char cn_str[256];
89         EAP_HANDLER *handler = NULL;
90         X509 *client_cert;
91         SSL *ssl;
92         int err, depth;
93         EAP_TLS_CONF *conf;
94         int my_ok = ok;
95
96         client_cert = X509_STORE_CTX_get_current_cert(ctx);
97         err = X509_STORE_CTX_get_error(ctx);
98         depth = X509_STORE_CTX_get_error_depth(ctx);
99
100         if (!my_ok) {
101                 radlog(L_ERR,"--> verify error:num=%d:%s\n",err,
102                         X509_verify_cert_error_string(err));
103                 return my_ok;
104         }
105         /*
106          *      Catch too long Certificate chains
107          */
108
109         /*
110          * Retrieve the pointer to the SSL of the connection currently treated
111          * and the application specific data stored into the SSL object.
112          */
113         ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
114         handler = (EAP_HANDLER *)SSL_get_ex_data(ssl, 0);
115         conf = (EAP_TLS_CONF *)SSL_get_ex_data(ssl, 1);
116
117         /*
118          *      Get the Subject & Issuer
119          */
120         subject[0] = issuer[0] = '\0';
121         X509_NAME_oneline(X509_get_subject_name(client_cert), subject, 256);
122         X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer, 256);
123
124         /*
125          *      Get the Common Name
126          */
127         X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
128              NID_commonName, buf, 256);
129
130         switch (ctx->error) {
131
132         case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
133                 radlog(L_ERR, "issuer= %s\n", issuer);
134                 break;
135         case X509_V_ERR_CERT_NOT_YET_VALID:
136         case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
137                 radlog(L_ERR, "notBefore=");
138 #if 0
139                 ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
140 #endif
141                 break;
142         case X509_V_ERR_CERT_HAS_EXPIRED:
143         case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
144                 radlog(L_ERR, "notAfter=");
145 #if 0
146                 ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
147 #endif
148                 break;
149         }
150
151         /*
152          *      If we're at the actual client cert and the conf tells
153          *      us to, check the CN in the cert against the xlat'ed
154          *      value
155          */
156         if (depth == 0 && conf->check_cert_cn != NULL) {
157                 if (!radius_xlat(cn_str, sizeof(cn_str), conf->check_cert_cn, handler->request, NULL)) {
158                         radlog(L_ERR, "rlm_eap_tls (%s): xlat failed.",
159                                conf->check_cert_cn);
160                         /* if this fails, fail the verification */
161                         my_ok = 0;
162                 }
163                 DEBUG2("    rlm_eap_tls: checking certificate CN (%s) with xlat'ed value (%s)", buf, cn_str);
164                 if (strncmp(cn_str, buf, sizeof(buf)) != 0) {
165                         my_ok = 0;
166                         radlog(L_AUTH, "rlm_eap_tls: Certificate CN (%s) does not match specified value (%s)!", buf, cn_str);
167                 }
168         }
169
170         if (debug_flag > 0) {
171                 radlog(L_INFO, "chain-depth=%d, ", depth);
172                 /*
173                   if (depth > 0) {
174                   return ok;
175                   }
176                 */
177                 radlog(L_INFO, "error=%d", err);
178
179                 radlog(L_INFO, "--> User-Name = %s", handler->identity);
180                 radlog(L_INFO, "--> BUF-Name = %s", buf);
181                 radlog(L_INFO, "--> subject = %s", subject);
182                 radlog(L_INFO, "--> issuer  = %s", issuer);
183                 radlog(L_INFO, "--> verify return:%d", my_ok);
184         }
185         return my_ok;
186 }
187
188
189 /*
190  *      Fill in our 'info' with TLS data.
191  */
192 void cbtls_msg(int write_p, int msg_version, int content_type,
193                const void *buf, size_t len,
194                SSL *ssl UNUSED, void *arg)
195 {
196         tls_session_t *state = (tls_session_t *)arg;
197
198         state->info.origin = (unsigned char)write_p;
199         state->info.content_type = (unsigned char)content_type;
200         state->info.record_len = len;
201         state->info.version = msg_version;
202         state->info.initialized = 1;
203
204         if (content_type == SSL3_RT_ALERT) {
205                 state->info.alert_level = ((const unsigned char*)buf)[0];
206                 state->info.alert_description = ((const unsigned char*)buf)[1];
207                 state->info.handshake_type = 0x00;
208
209         } else if (content_type == SSL3_RT_HANDSHAKE) {
210                 state->info.handshake_type = ((const unsigned char*)buf)[0];
211                 state->info.alert_level = 0x00;
212                 state->info.alert_description = 0x00;
213         }
214         tls_session_information(state);
215 }
216
217 int cbtls_password(char *buf,
218                    int num UNUSED,
219                    int rwflag UNUSED,
220                    void *userdata)
221 {
222         strcpy(buf, (char *)userdata);
223         return(strlen((char *)userdata));
224 }
225
226 RSA *cbtls_rsa(SSL *s UNUSED, int is_export UNUSED, int keylength)
227 {
228         static RSA *rsa_tmp=NULL;
229
230         if (rsa_tmp == NULL) {
231                 DEBUG2("Generating temp (%d bit) RSA key...", keylength);
232                 rsa_tmp=RSA_generate_key(keylength, RSA_F4, NULL, NULL);
233         }
234         return(rsa_tmp);
235 }
236
237 #endif /* !defined(NO_OPENSSL) */