08b928e75ace6c6ba6d461a724e37779b3a9a2dd
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
21  * Copyright 2006  The FreeRADIUS server project
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include "eap_tls.h"
28
29 #ifndef NO_OPENSSL
30
31 void cbtls_info(const SSL *s, int where, int ret)
32 {
33         const char *str, *state;
34         int w;
35         EAP_HANDLER *handler = (EAP_HANDLER *)SSL_get_ex_data(s, 0);
36         REQUEST *request = NULL;
37
38         if (handler) request = handler->request;
39
40         w = where & ~SSL_ST_MASK;
41         if (w & SSL_ST_CONNECT) str="    TLS_connect";
42         else if (w & SSL_ST_ACCEPT) str="    TLS_accept";
43         else str="    (other)";
44
45         state = SSL_state_string_long(s);
46         state = state ? state : "NULL";
47
48         if (where & SSL_CB_LOOP) {
49                 RDEBUG2("%s: %s\n", str, state);
50         } else if (where & SSL_CB_HANDSHAKE_START) {
51                 RDEBUG2("%s: %s\n", str, state);
52         } else if (where & SSL_CB_HANDSHAKE_DONE) {
53                 RDEBUG2("%s: %s\n", str, state);
54         } else if (where & SSL_CB_ALERT) {
55                 str=(where & SSL_CB_READ)?"read":"write";
56                 radlog(L_ERR,"TLS Alert %s:%s:%s\n", str,
57                         SSL_alert_type_string_long(ret),
58                         SSL_alert_desc_string_long(ret));
59         } else if (where & SSL_CB_EXIT) {
60                 if (ret == 0) {
61                         radlog(L_ERR, "%s:failed in %s", str, state);
62
63                         if (request) {
64                                 char buffer[128];
65
66                                 snprintf(buffer, sizeof(buffer), "%s:failed in %s");
67
68                                 radius_pairmake(request, &request->packet->vps,
69                                                 "Module-Failure-Message", buffer, T_OP_ADD);
70                         }
71
72                 } else if (ret < 0) {
73                         if (SSL_want_read(s)) {
74                                 RDEBUG2("%s: Need to read more data: %s",
75                                        str, state);
76                         } else {
77                                 radlog(L_ERR, "%s:error in %s\n", str, state);
78                         }
79                 }
80         }
81 }
82
83 /*
84  *      Fill in our 'info' with TLS data.
85  */
86 void cbtls_msg(int write_p, int msg_version, int content_type,
87                const void *buf, size_t len,
88                SSL *ssl UNUSED, void *arg)
89 {
90         tls_session_t *state = (tls_session_t *)arg;
91
92         /*
93          *      Work around bug #298, where we may be called with a NULL
94          *      argument.  We should really log a serious error
95          */
96         if (!arg) return;
97
98         state->info.origin = (unsigned char)write_p;
99         state->info.content_type = (unsigned char)content_type;
100         state->info.record_len = len;
101         state->info.version = msg_version;
102         state->info.initialized = 1;
103
104         if (content_type == SSL3_RT_ALERT) {
105                 state->info.alert_level = ((const unsigned char*)buf)[0];
106                 state->info.alert_description = ((const unsigned char*)buf)[1];
107                 state->info.handshake_type = 0x00;
108
109         } else if (content_type == SSL3_RT_HANDSHAKE) {
110                 state->info.handshake_type = ((const unsigned char*)buf)[0];
111                 state->info.alert_level = 0x00;
112                 state->info.alert_description = 0x00;
113         }
114         tls_session_information(state);
115 }
116
117 int cbtls_password(char *buf,
118                    int num UNUSED,
119                    int rwflag UNUSED,
120                    void *userdata)
121 {
122         strcpy(buf, (char *)userdata);
123         return(strlen((char *)userdata));
124 }
125
126 /*
127  *      For callbacks
128  */
129 int eaptls_handle_idx = -1;
130 int eaptls_conf_idx = -1;
131 int eaptls_session_idx = -1;
132
133 #endif /* !defined(NO_OPENSSL) */