New TTLS module. Verified to work with tunneled PAP, CHAP, MS-CHAP,
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_ttls / rlm_eap_ttls.c
1 /*
2  * rlm_eap_ttls.c  contains the interfaces that are called from eap
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 2003 Alan DeKok <aland@freeradius.org>
21  */
22
23 #include "autoconf.h"
24 #include "eap_ttls.h"
25
26
27 typedef struct rlm_eap_ttls_t {
28         /*
29          *      Use the reply attributes from the tunneled session in
30          *      the non-tunneled reply to the client.
31          */
32         int     use_tunneled_reply;
33
34         /*
35          *      Default tunneled EAP type
36          */
37         char    *default_eap_type_name;
38         int     default_eap_type;
39 } rlm_eap_ttls_t;
40
41
42 static CONF_PARSER module_config[] = {
43         { "use_tunneled_reply", PW_TYPE_BOOLEAN,
44           offsetof(rlm_eap_ttls_t, use_tunneled_reply), NULL, "no" },
45
46         { "default_eap_type", PW_TYPE_STRING_PTR,
47           offsetof(rlm_eap_ttls_t, default_eap_type_name), NULL, "md5" },
48
49         { NULL, -1, 0, NULL, NULL }           /* end the list */
50 };
51
52 /*
53  *      Detach the module.
54  */
55 static int eapttls_detach(void *arg)
56 {
57         rlm_eap_ttls_t *inst = (rlm_eap_ttls_t *) arg;
58
59         if (inst->default_eap_type_name) free(inst->default_eap_type_name);
60
61         free(inst);
62
63         return 0;
64 }
65
66 /*
67  *      Attach the module.
68  */
69 static int eapttls_attach(CONF_SECTION *cs, void **instance)
70 {
71         rlm_eap_ttls_t *inst;
72
73         inst = malloc(sizeof(*inst));
74         if (!inst) {
75                 radlog(L_ERR, "rlm_eap_ttls: out of memory");
76                 return -1;
77         }
78         memset(inst, 0, sizeof(*inst));
79
80         /*
81          *      Parse the configuration attributes.
82          */
83         if (cf_section_parse(cs, inst, module_config) < 0) {
84                 eapttls_detach(inst);
85                 return -1;
86         }
87
88         /*
89          *      Convert the name to an integer, to make it easier to
90          *      handle.
91          */
92         inst->default_eap_type = eaptype_name2type(inst->default_eap_type_name);
93         if (inst->default_eap_type < 0) {
94                 radlog(L_ERR, "rlm_eap_ttls: Unknown EAP type %s",
95                        inst->default_eap_type_name);
96                 eapttls_detach(inst);
97                 return -1;
98         }
99
100         /*
101          *      Can't tunnel TLS inside of TLS, we don't like it.
102          *
103          *      More realistically, we haven't tested it, so we don't
104          *      claim it works.
105          */
106         if ((inst->default_eap_type == PW_EAP_TLS) ||
107             (inst->default_eap_type == PW_EAP_TTLS) ||
108             (inst->default_eap_type == PW_EAP_PEAP)) {
109                 radlog(L_ERR, "rlm_eap_ttls: Cannot tunnel EAP-Type/%s inside of TTLS",
110                        inst->default_eap_type_name);
111                 eapttls_detach(inst);
112                 return -1;
113         }
114
115         *instance = inst;
116         return 0;
117 }
118
119
120 /*
121  *      Free the TTLS per-session data
122  */
123 static void ttls_free(void *p)
124 {
125         ttls_tunnel_t *t = (ttls_tunnel_t *) p;
126
127         if (!t) return;
128
129         if (t->username) {
130                 DEBUG2("  TTLS: Freeing handler for user %s",
131                        t->username->strvalue);
132         }
133
134         pairfree(&t->username);
135         pairfree(&t->state);
136         free(t);
137 }
138
139
140 /*
141  *      Free the TTLS per-session data
142  */
143 static ttls_tunnel_t *ttls_alloc(rlm_eap_ttls_t *inst)
144 {
145         ttls_tunnel_t *t;
146
147         t = rad_malloc(sizeof(*t));
148         memset(t, 0, sizeof(*t));
149
150         t->use_tunneled_reply = inst->use_tunneled_reply;
151         t->default_eap_type = inst->default_eap_type;
152         return t;
153 }
154
155
156 /*
157  *      Do authentication, by letting EAP-TLS do most of the work.
158  */
159 static int eapttls_authenticate(void *arg, EAP_HANDLER *handler)
160 {
161         int rcode;
162         eaptls_status_t status;
163         rlm_eap_ttls_t *inst = (rlm_eap_ttls_t *) arg;
164         tls_session_t *tls_session = (tls_session_t *) handler->opaque;
165
166         DEBUG2("  rlm_eap_ttls: Authenticate");
167
168         /*
169          *      Process TLS layer until done.
170          */
171         status = eaptls_process(handler);
172         DEBUG2("  eaptls_process returned %d\n", status);
173         switch (status) {
174                 /*
175                  *      EAP-TLS handshake was successful, tell the
176                  *      client to keep talking.
177                  *
178                  *      If this was EAP-TLS, we would just return
179                  *      an EAP-TLS-Success packet here.
180                  */
181         case EAPTLS_SUCCESS:
182                 eaptls_request(handler->eap_ds, tls_session);
183                 return 1;
184
185                 /*
186                  *      The TLS code is still working on the TLS
187                  *      exchange, and it's a valid TLS request.
188                  *      do nothing.
189                  */
190         case EAPTLS_HANDLED:
191                 return 1;
192
193                 /*
194                  *      Handshake is done, proceed with decoding tunneled
195                  *      data.
196                  */
197         case EAPTLS_OK:
198                 break;
199
200                 /*
201                  *      Anything else: fail.
202                  */
203         default:
204                 return 0;
205         }
206
207         /*
208          *      Session is established, proceed with decoding
209          *      tunneled data.
210          */
211         DEBUG2("  rlm_eap_ttls: Session established.  Proceeding to decode tunneled attributes.");
212
213         /*
214          *      We may need TTLS data associated with the session, so
215          *      allocate it here, if it wasn't already alloacted.
216          */
217         if (!tls_session->opaque) {
218                 tls_session->opaque = ttls_alloc(inst);
219                 tls_session->free_opaque = ttls_free;
220         }
221
222         /*
223          *      Process the TTLS portion of the request.
224          */
225         rcode = eapttls_process(handler->request, tls_session);
226         if (rcode == PW_AUTHENTICATION_REJECT) {
227                 eaptls_fail(handler->eap_ds, 0);
228                 return 0;
229         }
230
231         /*
232          *      Access-Challenge, continue tunneled conversation.
233          */
234         if (rcode == PW_ACCESS_CHALLENGE) {
235                 eaptls_request(handler->eap_ds, tls_session);
236                 return 1;
237         }
238
239         /*
240          *      Success: Return MPPE keys.
241          */
242         if (rcode == PW_AUTHENTICATION_ACK) {
243                 eaptls_success(handler->eap_ds, 0);
244                 eaptls_gen_mppe_keys(&handler->request->reply->vps, 
245                                      tls_session->ssl,
246                                      "ttls keying material");
247                 return 1;
248         }
249
250         /*
251          *      Something we don't understand: Reject it.
252          */
253         eaptls_fail(handler->eap_ds, 0);
254         return 0;
255 }
256
257 /*
258  *      The module name should be the only globally exported symbol.
259  *      That is, everything else should be 'static'.
260  */
261 EAP_TYPE rlm_eap_ttls = {
262         "eap_ttls",
263         eapttls_attach,                 /* attach */
264         /*
265          *      Note! There is NO eapttls_initate() function, as the
266          *      main EAP module takes care of calling
267          *      eaptls_initiate().
268          *
269          *      This is because TTLS is a protocol on top of TLS, so
270          *      before we need to do TTLS, we've got to initiate a TLS
271          *      session.
272          */
273         NULL,                           /* Start the initial request */
274         NULL,                           /* authorization */
275         eapttls_authenticate,           /* authentication */
276         eapttls_detach                  /* detach */
277 };