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>
21 * Copyright 2003 Alan DeKok <aland@freeradius.org>
26 static void record_init(record_t *buf);
27 static void record_close(record_t *buf);
28 static unsigned int record_plus(record_t *buf, const void *ptr,
30 static unsigned int record_minus(record_t *buf, void *ptr,
33 tls_session_t *eaptls_new_session(SSL_CTX *ssl_ctx, int client_cert)
35 tls_session_t *state = NULL;
37 int verify_mode = SSL_VERIFY_NONE;
39 if ((new_tls = SSL_new(ssl_ctx)) == NULL) {
40 radlog(L_ERR, "rlm_eap_tls: Error creating new SSL");
41 ERR_print_errors_fp(stderr);
45 /* We use the SSL's "app_data" to indicate a call-back */
46 SSL_set_app_data(new_tls, NULL);
48 state = (tls_session_t *)malloc(sizeof(*state));
49 memset(state, 0, sizeof(*state));
54 * Initialize callbacks
56 state->record_init = record_init;
57 state->record_close = record_close;
58 state->record_plus = record_plus;
59 state->record_minus = record_minus;
62 * Create & hook the BIOs to handle the dirty side of the
63 * SSL. This is *very important* as we want to handle
64 * the transmission part. Now the only IO interface
65 * that SSL is aware of, is our defined BIO buffers.
67 * This means that all SSL IO is done to/from memory,
68 * and we can update those BIOs from the EAP packets we've
71 state->into_ssl = BIO_new(BIO_s_mem());
72 state->from_ssl = BIO_new(BIO_s_mem());
73 SSL_set_bio(state->ssl, state->into_ssl, state->from_ssl);
76 * Add the message callback to identify what type of
77 * message/handshake is passed
79 SSL_set_msg_callback(new_tls, cbtls_msg);
80 SSL_set_msg_callback_arg(new_tls, state);
81 SSL_set_info_callback(new_tls, cbtls_info);
84 * Verify the peer certificate, if asked.
87 DEBUG2(" rlm_eap_tls: Requiring client certificate");
88 verify_mode = SSL_VERIFY_PEER;
89 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
90 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
92 SSL_set_verify(state->ssl, verify_mode, cbtls_verify);
95 * In Server mode we only accept.
97 SSL_set_accept_state(state->ssl);
103 * Print out some text describing the error.
105 static void int_ssl_check(SSL *s, int ret, const char *text)
109 ERR_print_errors_fp(stderr);
110 e = SSL_get_error(s, ret);
114 * These seem to be harmless and already "dealt
115 * with" by our non-blocking environment. NB:
116 * "ZERO_RETURN" is the clean "error"
117 * indicating a successfully closed SSL
118 * tunnel. We let this happen because our IO
119 * loop should not appear to have broken on
120 * this condition - and outside the IO loop, the
121 * "shutdown" state is checked.
123 * Don't print anything if we ignore the error.
126 case SSL_ERROR_WANT_READ:
127 case SSL_ERROR_WANT_WRITE:
128 case SSL_ERROR_WANT_X509_LOOKUP:
129 case SSL_ERROR_ZERO_RETURN:
133 * These seem to be indications of a genuine
134 * error that should result in the SSL tunnel
135 * being regarded as "dead".
137 case SSL_ERROR_SYSCALL:
138 radlog(L_ERR, "rlm_eap_tls: %s failed in a system call (%d), TLS session fails.",
140 SSL_set_app_data(s, (char *)1);
144 radlog(L_ERR, "rlm_eap_tls: %s failed inside of TLS (%d), TLS session fails.",
146 SSL_set_app_data(s, (char *)1);
151 * For any other errors that (a) exist, and (b)
152 * crop up - we need to interpret what to do with
153 * them - so "politely inform" the caller that
154 * the code needs updating here.
156 radlog(L_ERR, "rlm_eap_tls: FATAL SSL error ..... %d\n", e);
162 * We are the server, we always get the dirty data
163 * (Handshake data is also considered as dirty data)
164 * During handshake, since SSL API handles itself,
165 * After clean-up, dirty_out will be filled with
166 * the data required for handshaking. So we check
167 * if dirty_out is empty then we simply send it back.
168 * As of now, if handshake is successful, then it is EAP-Success
169 * or else EAP-failure should be sent
171 * Fill the Bio with the dirty data to clean it
172 * Get the cleaned data from SSL, if it is not Handshake data
174 int tls_handshake_recv(tls_session_t *ssn)
178 BIO_write(ssn->into_ssl, ssn->dirty_in.data, ssn->dirty_in.used);
179 err = SSL_read(ssn->ssl, ssn->clean_out.data,
180 sizeof(ssn->clean_out.data));
182 ssn->clean_out.used = err;
184 int_ssl_check(ssn->ssl, err, "SSL_read");
187 /* Some Extra STATE information for easy debugging */
188 if (SSL_is_init_finished(ssn->ssl)) {
189 DEBUG2("SSL Connection Established\n");
191 if (SSL_in_init(ssn->ssl)) {
192 DEBUG2("In SSL Handshake Phase\n");
194 if (SSL_in_before(ssn->ssl)) {
195 DEBUG2("Before SSL Handshake Phase\n");
197 if (SSL_in_accept_init(ssn->ssl)) {
198 DEBUG2("In SSL Accept mode \n");
200 if (SSL_in_connect_init(ssn->ssl)) {
201 DEBUG2("In SSL Connect mode \n");
204 if (ssn->info.content_type != application_data) {
205 err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
206 sizeof(ssn->dirty_out.data));
208 ssn->dirty_out.used = err;
210 int_ssl_check(ssn->ssl, err, "BIO_read");
211 record_init(&ssn->dirty_in);
215 radlog(L_INFO, "rlm_eap_tls: Application Data");
216 /* Its clean application data, do whatever we want */
217 record_init(&ssn->clean_out);
220 /* We are done with dirty_in, reinitialize it */
221 record_init(&ssn->dirty_in);
226 * Take clear-text user data, and encrypt it into the output buffer,
227 * to send to the client at the other end of the SSL connection.
229 int tls_handshake_send(tls_session_t *ssn)
234 * If there's un-encrypted data in 'clean_in', then write
235 * that data to the SSL session, and then call the BIO function
236 * to get that encrypted data from the SSL session, into
237 * a buffer which we can then package into an EAP packet.
239 * Based on Server's logic this clean_in is expected to
240 * contain the data to send to the client.
242 if (ssn->clean_in.used > 0) {
243 SSL_write(ssn->ssl, ssn->clean_in.data, ssn->clean_in.used);
245 /* Get the dirty data from Bio to send it */
246 err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
247 sizeof(ssn->dirty_out.data));
249 ssn->dirty_out.used = err;
251 int_ssl_check(ssn->ssl, err, "handshake_send");
255 record_init(&ssn->clean_in);
259 void session_init(tls_session_t *ssn)
262 ssn->into_ssl = ssn->from_ssl = NULL;
263 record_init(&ssn->clean_in);
264 record_init(&ssn->clean_out);
265 record_init(&ssn->dirty_in);
266 record_init(&ssn->dirty_out);
268 memset(&ssn->info, 0, sizeof(ssn->info));
272 ssn->tls_msg_len = 0;
273 ssn->length_flag = 0;
275 ssn->free_opaque = NULL;
278 void session_close(tls_session_t *ssn)
284 * WARNING: SSL_free seems to decrement the reference counts already,
285 * so doing this might crash the application.
288 BIO_free(ssn->into_ssl);
290 BIO_free(ssn->from_ssl);
292 record_close(&ssn->clean_in);
293 record_close(&ssn->clean_out);
294 record_close(&ssn->dirty_in);
295 record_close(&ssn->dirty_out);
299 void session_free(void *ssn)
301 tls_session_t *sess = (tls_session_t *)ssn;
306 * Free any opaque TTLS or PEAP data.
308 if ((sess->opaque) && (sess->free_opaque)) {
309 sess->free_opaque(sess->opaque);
318 static void record_init(record_t *rec)
323 static void record_close(record_t *rec)
330 * Copy data to the intermediate buffer, before we send
333 static unsigned int record_plus(record_t *rec, const void *ptr,
336 unsigned int added = MAX_RECORD_SIZE - rec->used;
342 memcpy(rec->data + rec->used, ptr, added);
348 * Take data from the buffer, and give it to the caller.
350 static unsigned int record_minus(record_t *rec, void *ptr,
353 unsigned int taken = rec->used;
360 memcpy(ptr, rec->data, taken);
364 * This is pretty bad...
367 memmove(rec->data, rec->data + taken, rec->used);
371 void tls_session_information(tls_session_t *tls_session)
373 const char *str_write_p, *str_version, *str_content_type = "";
374 const char *str_details1 = "", *str_details2= "";
377 * Don't print this out in the normal course of
380 if (debug_flag == 0) {
384 str_write_p = tls_session->info.origin ? ">>>" : "<<<";
386 switch (tls_session->info.version)
389 str_version = "SSL 2.0";
392 str_version = "SSL 3.0 ";
395 str_version = "TLS 1.0 ";
398 str_version = "Unknown TLS version";
402 if (tls_session->info.version == SSL3_VERSION ||
403 tls_session->info.version == TLS1_VERSION) {
404 switch (tls_session->info.content_type) {
405 case SSL3_RT_CHANGE_CIPHER_SPEC:
406 str_content_type = "ChangeCipherSpec";
409 str_content_type = "Alert";
411 case SSL3_RT_HANDSHAKE:
412 str_content_type = "Handshake";
414 case SSL3_RT_APPLICATION_DATA:
415 str_content_type = "ApplicationData";
418 str_content_type = "UnknownContentType";
422 if (tls_session->info.content_type == SSL3_RT_ALERT) {
423 str_details1 = ", ???";
425 if (tls_session->info.record_len == 2) {
427 switch (tls_session->info.alert_level) {
428 case SSL3_AL_WARNING:
429 str_details1 = ", warning";
432 str_details1 = ", fatal";
436 str_details2 = " ???";
437 switch (tls_session->info.alert_description) {
438 case SSL3_AD_CLOSE_NOTIFY:
439 str_details2 = " close_notify";
441 case SSL3_AD_UNEXPECTED_MESSAGE:
442 str_details2 = " unexpected_message";
444 case SSL3_AD_BAD_RECORD_MAC:
445 str_details2 = " bad_record_mac";
447 case TLS1_AD_DECRYPTION_FAILED:
448 str_details2 = " decryption_failed";
450 case TLS1_AD_RECORD_OVERFLOW:
451 str_details2 = " record_overflow";
453 case SSL3_AD_DECOMPRESSION_FAILURE:
454 str_details2 = " decompression_failure";
456 case SSL3_AD_HANDSHAKE_FAILURE:
457 str_details2 = " handshake_failure";
459 case SSL3_AD_BAD_CERTIFICATE:
460 str_details2 = " bad_certificate";
462 case SSL3_AD_UNSUPPORTED_CERTIFICATE:
463 str_details2 = " unsupported_certificate";
465 case SSL3_AD_CERTIFICATE_REVOKED:
466 str_details2 = " certificate_revoked";
468 case SSL3_AD_CERTIFICATE_EXPIRED:
469 str_details2 = " certificate_expired";
471 case SSL3_AD_CERTIFICATE_UNKNOWN:
472 str_details2 = " certificate_unknown";
474 case SSL3_AD_ILLEGAL_PARAMETER:
475 str_details2 = " illegal_parameter";
477 case TLS1_AD_UNKNOWN_CA:
478 str_details2 = " unknown_ca";
480 case TLS1_AD_ACCESS_DENIED:
481 str_details2 = " access_denied";
483 case TLS1_AD_DECODE_ERROR:
484 str_details2 = " decode_error";
486 case TLS1_AD_DECRYPT_ERROR:
487 str_details2 = " decrypt_error";
489 case TLS1_AD_EXPORT_RESTRICTION:
490 str_details2 = " export_restriction";
492 case TLS1_AD_PROTOCOL_VERSION:
493 str_details2 = " protocol_version";
495 case TLS1_AD_INSUFFICIENT_SECURITY:
496 str_details2 = " insufficient_security";
498 case TLS1_AD_INTERNAL_ERROR:
499 str_details2 = " internal_error";
501 case TLS1_AD_USER_CANCELLED:
502 str_details2 = " user_canceled";
504 case TLS1_AD_NO_RENEGOTIATION:
505 str_details2 = " no_renegotiation";
511 if (tls_session->info.content_type == SSL3_RT_HANDSHAKE) {
512 str_details1 = "???";
514 if (tls_session->info.record_len > 0)
515 switch (tls_session->info.handshake_type)
517 case SSL3_MT_HELLO_REQUEST:
518 str_details1 = ", HelloRequest";
520 case SSL3_MT_CLIENT_HELLO:
521 str_details1 = ", ClientHello";
523 case SSL3_MT_SERVER_HELLO:
524 str_details1 = ", ServerHello";
526 case SSL3_MT_CERTIFICATE:
527 str_details1 = ", Certificate";
529 case SSL3_MT_SERVER_KEY_EXCHANGE:
530 str_details1 = ", ServerKeyExchange";
532 case SSL3_MT_CERTIFICATE_REQUEST:
533 str_details1 = ", CertificateRequest";
535 case SSL3_MT_SERVER_DONE:
536 str_details1 = ", ServerHelloDone";
538 case SSL3_MT_CERTIFICATE_VERIFY:
539 str_details1 = ", CertificateVerify";
541 case SSL3_MT_CLIENT_KEY_EXCHANGE:
542 str_details1 = ", ClientKeyExchange";
544 case SSL3_MT_FINISHED:
545 str_details1 = ", Finished";
551 sprintf(tls_session->info.info_description, "%s %s%s [length %04lx]%s%s\n",
552 str_write_p, str_version, str_content_type,
553 (unsigned long)tls_session->info.record_len, str_details1, str_details2);
554 DEBUG2(" rlm_eap_tls: %s\n", tls_session->info.info_description);