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;
38 client_cert = client_cert; /* -Wunused. See bug #350 */
40 if ((new_tls = SSL_new(ssl_ctx)) == NULL) {
41 radlog(L_ERR, "rlm_eap_tls: Error creating new SSL");
42 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
46 /* We use the SSL's "app_data" to indicate a call-back */
47 SSL_set_app_data(new_tls, NULL);
49 state = (tls_session_t *)malloc(sizeof(*state));
50 memset(state, 0, sizeof(*state));
55 * Initialize callbacks
57 state->record_init = record_init;
58 state->record_close = record_close;
59 state->record_plus = record_plus;
60 state->record_minus = record_minus;
63 * Create & hook the BIOs to handle the dirty side of the
64 * SSL. This is *very important* as we want to handle
65 * the transmission part. Now the only IO interface
66 * that SSL is aware of, is our defined BIO buffers.
68 * This means that all SSL IO is done to/from memory,
69 * and we can update those BIOs from the EAP packets we've
72 state->into_ssl = BIO_new(BIO_s_mem());
73 state->from_ssl = BIO_new(BIO_s_mem());
74 SSL_set_bio(state->ssl, state->into_ssl, state->from_ssl);
77 * Add the message callback to identify what type of
78 * message/handshake is passed
80 SSL_set_msg_callback(new_tls, cbtls_msg);
81 SSL_set_msg_callback_arg(new_tls, state);
82 SSL_set_info_callback(new_tls, cbtls_info);
85 * In Server mode we only accept.
87 SSL_set_accept_state(state->ssl);
93 * Print out some text describing the error.
95 static void int_ssl_check(SSL *s, int ret, const char *text)
99 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
100 e = SSL_get_error(s, ret);
104 * These seem to be harmless and already "dealt
105 * with" by our non-blocking environment. NB:
106 * "ZERO_RETURN" is the clean "error"
107 * indicating a successfully closed SSL
108 * tunnel. We let this happen because our IO
109 * loop should not appear to have broken on
110 * this condition - and outside the IO loop, the
111 * "shutdown" state is checked.
113 * Don't print anything if we ignore the error.
116 case SSL_ERROR_WANT_READ:
117 case SSL_ERROR_WANT_WRITE:
118 case SSL_ERROR_WANT_X509_LOOKUP:
119 case SSL_ERROR_ZERO_RETURN:
123 * These seem to be indications of a genuine
124 * error that should result in the SSL tunnel
125 * being regarded as "dead".
127 case SSL_ERROR_SYSCALL:
128 radlog(L_ERR, "rlm_eap_tls: %s failed in a system call (%d), TLS session fails.",
130 SSL_set_app_data(s, (char *)1);
134 radlog(L_ERR, "rlm_eap_tls: %s failed inside of TLS (%d), TLS session fails.",
136 SSL_set_app_data(s, (char *)1);
141 * For any other errors that (a) exist, and (b)
142 * crop up - we need to interpret what to do with
143 * them - so "politely inform" the caller that
144 * the code needs updating here.
146 radlog(L_ERR, "rlm_eap_tls: FATAL SSL error ..... %d\n", e);
152 * We are the server, we always get the dirty data
153 * (Handshake data is also considered as dirty data)
154 * During handshake, since SSL API handles itself,
155 * After clean-up, dirty_out will be filled with
156 * the data required for handshaking. So we check
157 * if dirty_out is empty then we simply send it back.
158 * As of now, if handshake is successful, then it is EAP-Success
159 * or else EAP-failure should be sent
161 * Fill the Bio with the dirty data to clean it
162 * Get the cleaned data from SSL, if it is not Handshake data
164 int tls_handshake_recv(tls_session_t *ssn)
168 BIO_write(ssn->into_ssl, ssn->dirty_in.data, ssn->dirty_in.used);
169 err = SSL_read(ssn->ssl, ssn->clean_out.data,
170 sizeof(ssn->clean_out.data));
172 ssn->clean_out.used = err;
174 int_ssl_check(ssn->ssl, err, "SSL_read");
177 /* Some Extra STATE information for easy debugging */
178 if (SSL_is_init_finished(ssn->ssl)) {
179 DEBUG2("SSL Connection Established\n");
181 if (SSL_in_init(ssn->ssl)) {
182 DEBUG2("In SSL Handshake Phase\n");
184 if (SSL_in_before(ssn->ssl)) {
185 DEBUG2("Before SSL Handshake Phase\n");
187 if (SSL_in_accept_init(ssn->ssl)) {
188 DEBUG2("In SSL Accept mode \n");
190 if (SSL_in_connect_init(ssn->ssl)) {
191 DEBUG2("In SSL Connect mode \n");
194 if (ssn->info.content_type != application_data) {
195 err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
196 sizeof(ssn->dirty_out.data));
198 ssn->dirty_out.used = err;
200 int_ssl_check(ssn->ssl, err, "BIO_read");
201 record_init(&ssn->dirty_in);
205 DEBUG2("rlm_eap_tls: Application Data");
206 /* Its clean application data, do whatever we want */
207 record_init(&ssn->clean_out);
210 /* We are done with dirty_in, reinitialize it */
211 record_init(&ssn->dirty_in);
216 * Take clear-text user data, and encrypt it into the output buffer,
217 * to send to the client at the other end of the SSL connection.
219 int tls_handshake_send(tls_session_t *ssn)
224 * If there's un-encrypted data in 'clean_in', then write
225 * that data to the SSL session, and then call the BIO function
226 * to get that encrypted data from the SSL session, into
227 * a buffer which we can then package into an EAP packet.
229 * Based on Server's logic this clean_in is expected to
230 * contain the data to send to the client.
232 if (ssn->clean_in.used > 0) {
233 SSL_write(ssn->ssl, ssn->clean_in.data, ssn->clean_in.used);
235 /* Get the dirty data from Bio to send it */
236 err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
237 sizeof(ssn->dirty_out.data));
239 ssn->dirty_out.used = err;
241 int_ssl_check(ssn->ssl, err, "handshake_send");
245 record_init(&ssn->clean_in);
249 void session_init(tls_session_t *ssn)
252 ssn->into_ssl = ssn->from_ssl = NULL;
253 record_init(&ssn->clean_in);
254 record_init(&ssn->clean_out);
255 record_init(&ssn->dirty_in);
256 record_init(&ssn->dirty_out);
258 memset(&ssn->info, 0, sizeof(ssn->info));
262 ssn->tls_msg_len = 0;
263 ssn->length_flag = 0;
265 ssn->free_opaque = NULL;
268 void session_close(tls_session_t *ssn)
274 * WARNING: SSL_free seems to decrement the reference counts already,
275 * so doing this might crash the application.
278 BIO_free(ssn->into_ssl);
280 BIO_free(ssn->from_ssl);
282 record_close(&ssn->clean_in);
283 record_close(&ssn->clean_out);
284 record_close(&ssn->dirty_in);
285 record_close(&ssn->dirty_out);
289 void session_free(void *ssn)
291 tls_session_t *sess = (tls_session_t *)ssn;
296 * Free any opaque TTLS or PEAP data.
298 if ((sess->opaque) && (sess->free_opaque)) {
299 sess->free_opaque(sess->opaque);
308 static void record_init(record_t *rec)
313 static void record_close(record_t *rec)
320 * Copy data to the intermediate buffer, before we send
323 static unsigned int record_plus(record_t *rec, const void *ptr,
326 unsigned int added = MAX_RECORD_SIZE - rec->used;
332 memcpy(rec->data + rec->used, ptr, added);
338 * Take data from the buffer, and give it to the caller.
340 static unsigned int record_minus(record_t *rec, void *ptr,
343 unsigned int taken = rec->used;
350 memcpy(ptr, rec->data, taken);
354 * This is pretty bad...
357 memmove(rec->data, rec->data + taken, rec->used);
361 void tls_session_information(tls_session_t *tls_session)
363 const char *str_write_p, *str_version, *str_content_type = "";
364 const char *str_details1 = "", *str_details2= "";
367 * Don't print this out in the normal course of
370 if (debug_flag == 0) {
374 str_write_p = tls_session->info.origin ? ">>>" : "<<<";
376 switch (tls_session->info.version)
379 str_version = "SSL 2.0";
382 str_version = "SSL 3.0 ";
385 str_version = "TLS 1.0 ";
388 str_version = "Unknown TLS version";
392 if (tls_session->info.version == SSL3_VERSION ||
393 tls_session->info.version == TLS1_VERSION) {
394 switch (tls_session->info.content_type) {
395 case SSL3_RT_CHANGE_CIPHER_SPEC:
396 str_content_type = "ChangeCipherSpec";
399 str_content_type = "Alert";
401 case SSL3_RT_HANDSHAKE:
402 str_content_type = "Handshake";
404 case SSL3_RT_APPLICATION_DATA:
405 str_content_type = "ApplicationData";
408 str_content_type = "UnknownContentType";
412 if (tls_session->info.content_type == SSL3_RT_ALERT) {
413 str_details1 = ", ???";
415 if (tls_session->info.record_len == 2) {
417 switch (tls_session->info.alert_level) {
418 case SSL3_AL_WARNING:
419 str_details1 = ", warning";
422 str_details1 = ", fatal";
426 str_details2 = " ???";
427 switch (tls_session->info.alert_description) {
428 case SSL3_AD_CLOSE_NOTIFY:
429 str_details2 = " close_notify";
431 case SSL3_AD_UNEXPECTED_MESSAGE:
432 str_details2 = " unexpected_message";
434 case SSL3_AD_BAD_RECORD_MAC:
435 str_details2 = " bad_record_mac";
437 case TLS1_AD_DECRYPTION_FAILED:
438 str_details2 = " decryption_failed";
440 case TLS1_AD_RECORD_OVERFLOW:
441 str_details2 = " record_overflow";
443 case SSL3_AD_DECOMPRESSION_FAILURE:
444 str_details2 = " decompression_failure";
446 case SSL3_AD_HANDSHAKE_FAILURE:
447 str_details2 = " handshake_failure";
449 case SSL3_AD_BAD_CERTIFICATE:
450 str_details2 = " bad_certificate";
452 case SSL3_AD_UNSUPPORTED_CERTIFICATE:
453 str_details2 = " unsupported_certificate";
455 case SSL3_AD_CERTIFICATE_REVOKED:
456 str_details2 = " certificate_revoked";
458 case SSL3_AD_CERTIFICATE_EXPIRED:
459 str_details2 = " certificate_expired";
461 case SSL3_AD_CERTIFICATE_UNKNOWN:
462 str_details2 = " certificate_unknown";
464 case SSL3_AD_ILLEGAL_PARAMETER:
465 str_details2 = " illegal_parameter";
467 case TLS1_AD_UNKNOWN_CA:
468 str_details2 = " unknown_ca";
470 case TLS1_AD_ACCESS_DENIED:
471 str_details2 = " access_denied";
473 case TLS1_AD_DECODE_ERROR:
474 str_details2 = " decode_error";
476 case TLS1_AD_DECRYPT_ERROR:
477 str_details2 = " decrypt_error";
479 case TLS1_AD_EXPORT_RESTRICTION:
480 str_details2 = " export_restriction";
482 case TLS1_AD_PROTOCOL_VERSION:
483 str_details2 = " protocol_version";
485 case TLS1_AD_INSUFFICIENT_SECURITY:
486 str_details2 = " insufficient_security";
488 case TLS1_AD_INTERNAL_ERROR:
489 str_details2 = " internal_error";
491 case TLS1_AD_USER_CANCELLED:
492 str_details2 = " user_canceled";
494 case TLS1_AD_NO_RENEGOTIATION:
495 str_details2 = " no_renegotiation";
501 if (tls_session->info.content_type == SSL3_RT_HANDSHAKE) {
502 str_details1 = "???";
504 if (tls_session->info.record_len > 0)
505 switch (tls_session->info.handshake_type)
507 case SSL3_MT_HELLO_REQUEST:
508 str_details1 = ", HelloRequest";
510 case SSL3_MT_CLIENT_HELLO:
511 str_details1 = ", ClientHello";
513 case SSL3_MT_SERVER_HELLO:
514 str_details1 = ", ServerHello";
516 case SSL3_MT_CERTIFICATE:
517 str_details1 = ", Certificate";
519 case SSL3_MT_SERVER_KEY_EXCHANGE:
520 str_details1 = ", ServerKeyExchange";
522 case SSL3_MT_CERTIFICATE_REQUEST:
523 str_details1 = ", CertificateRequest";
525 case SSL3_MT_SERVER_DONE:
526 str_details1 = ", ServerHelloDone";
528 case SSL3_MT_CERTIFICATE_VERIFY:
529 str_details1 = ", CertificateVerify";
531 case SSL3_MT_CLIENT_KEY_EXCHANGE:
532 str_details1 = ", ClientKeyExchange";
534 case SSL3_MT_FINISHED:
535 str_details1 = ", Finished";
541 sprintf(tls_session->info.info_description, "%s %s%s [length %04lx]%s%s\n",
542 str_write_p, str_version, str_content_type,
543 (unsigned long)tls_session->info.record_len, str_details1, str_details2);
544 DEBUG2(" rlm_eap_tls: %s\n", tls_session->info.info_description);