1ccd202364452fb707c493ebf3ffd536d9d2230a
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_tls / 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                 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) {
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 *user_name = NULL; /* User-Name */
89         X509 *client_cert;
90         SSL *ssl;
91         int err, depth;
92         int data_index = 0;
93
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);
97
98         if(!ok)
99                 radlog(L_ERR,"--> verify error:num=%d:%s\n",err,
100                         X509_verify_cert_error_string(err));
101         /*
102          *      Catch too long Certificate chains
103          */
104
105         /*
106          * Retrieve the pointer to the SSL of the connection currently treated
107          * and the application specific data stored into the SSL object.
108          */
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);
111
112         /*
113          *      Get the Subject & Issuer
114          */
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);
118
119         /*
120          *      Get the Common Name
121          */
122         X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
123              NID_commonName, buf, 256);
124
125         switch (ctx->error) {
126
127         case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
128                 radlog(L_ERR, "issuer= %s\n", issuer);
129                 break;
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=");
133 #if 0
134                 ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
135 #endif
136                 break;
137         case X509_V_ERR_CERT_HAS_EXPIRED:
138         case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
139                 radlog(L_ERR, "notAfter=");
140 #if 0
141                 ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
142 #endif
143                 break;
144         }
145
146         if (debug_flag > 0) {
147                 radlog(L_INFO, "chain-depth=%d, ", depth);
148                 /*
149                   if (depth > 0) {
150                   return ok;
151                   }
152                 */
153                 radlog(L_INFO, "error=%d", err);
154                 
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);
160         }
161         return ok;
162 }
163
164
165 /*
166  *      Fill in our 'info' with TLS data.
167  */
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)
171 {
172         tls_session_t *state = (tls_session_t *)arg;
173
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;
179
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;
184         
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;
189         }
190         tls_session_information(state);
191 }
192
193 int cbtls_password(char *buf, 
194                    int num UNUSED, 
195                    int rwflag UNUSED, 
196                    void *userdata)
197 {
198         strcpy(buf, (char *)userdata);
199         return(strlen((char *)userdata));
200 }
201
202 RSA *cbtls_rsa(SSL *s UNUSED, int is_export UNUSED, int keylength)
203 {
204         static RSA *rsa_tmp=NULL;
205
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);
209         }
210         return(rsa_tmp);
211 }
212
213 #endif /* !defined(NO_OPENSSL) */