more spelling issues cleared
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_tls / rlm_eap_tls.c
1 /*
2  * rlm_eap_tls.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
21  * Copyright 2003  Alan DeKok <aland@freeradius.org>
22  * Copyright 2006  The FreeRADIUS server project
23  *
24  */
25
26 RCSID("$Id$")
27 USES_APPLE_DEPRECATED_API       /* OpenSSL API has been deprecated by Apple */
28
29 #ifdef HAVE_OPENSSL_RAND_H
30 #include <openssl/rand.h>
31 #endif
32
33 #ifdef HAVE_OPENSSL_EVP_H
34 #include <openssl/evp.h>
35 #endif
36
37 #include "rlm_eap_tls.h"
38
39 #ifdef HAVE_SYS_STAT_H
40 #include <sys/stat.h>
41 #endif
42
43 static CONF_PARSER module_config[] = {
44         { "tls", PW_TYPE_STRING_PTR,
45           offsetof(rlm_eap_tls_t, tls_conf_name), NULL, NULL },
46
47         { "virtual_server", PW_TYPE_STRING_PTR,
48           offsetof(rlm_eap_tls_t, virtual_server), NULL, NULL },
49
50         { NULL, -1, 0, NULL, NULL }        /* end the list */
51 };
52
53
54 /*
55  *      Attach the EAP-TLS module.
56  */
57 static int eaptls_attach(CONF_SECTION *cs, void **instance)
58 {
59         rlm_eap_tls_t           *inst;
60
61         /*
62          *      Parse the config file & get all the configured values
63          */
64         *instance = inst = talloc_zero(cs, rlm_eap_tls_t);
65         if (!inst) return -1;
66
67         if (cf_section_parse(cs, inst, module_config) < 0) {
68                 return -1;
69         }
70
71         inst->tls_conf = eaptls_conf_parse(cs, "tls");
72
73         if (!inst->tls_conf) {
74                 ERROR("rlm_eap_tls: Failed initializing SSL context");
75                 return -1;
76         }
77
78         return 0;
79 }
80
81
82 /*
83  *      Send an initial eap-tls request to the peer, using the libeap functions.
84  */
85 static int eaptls_initiate(void *type_arg, eap_handler_t *handler)
86 {
87         int             status;
88         tls_session_t   *ssn;
89         rlm_eap_tls_t   *inst;
90         REQUEST         *request = handler->request;
91
92         inst = type_arg;
93
94         handler->tls = true;
95         handler->finished = false;
96
97         /*
98          *      EAP-TLS always requires a client certificate.
99          */
100         ssn = eaptls_session(inst->tls_conf, handler, true);
101         if (!ssn) {
102                 return 0;
103         }
104
105         handler->opaque = ((void *)ssn);
106         handler->free_opaque = session_free;
107
108         /*
109          *      Set up type-specific information.
110          */
111         ssn->prf_label = "client EAP encryption";
112
113         /*
114          *      TLS session initialization is over.  Now handle TLS
115          *      related handshaking or application data.
116          */
117         status = eaptls_start(handler->eap_ds, ssn->peap_flag);
118         RDEBUG2("Start returned %d", status);
119         if (status == 0) {
120                 return 0;
121         }
122
123         /*
124          *      The next stage to process the packet.
125          */
126         handler->stage = AUTHENTICATE;
127
128         return 1;
129 }
130
131 /*
132  *      Do authentication, by letting EAP-TLS do most of the work.
133  */
134 static int mod_authenticate(void *type_arg, eap_handler_t *handler)
135 {
136         fr_tls_status_t status;
137         tls_session_t *tls_session = (tls_session_t *) handler->opaque;
138         REQUEST *request = handler->request;
139         rlm_eap_tls_t *inst;
140
141         inst = type_arg;
142
143         RDEBUG2("Authenticate");
144
145         status = eaptls_process(handler);
146         RDEBUG2("eaptls_process returned %d\n", status);
147         switch (status) {
148                 /*
149                  *      EAP-TLS handshake was successful, return an
150                  *      EAP-TLS-Success packet here.
151                  *
152                  *      If a virtual server was configured, check that
153                  *      it accepts the certificates, too.
154                  */
155         case FR_TLS_SUCCESS:
156                 if (inst->virtual_server) {
157                         VALUE_PAIR *vp;
158                         REQUEST *fake;
159
160                         /* create a fake request */
161                         fake = request_alloc_fake(request);
162                         rad_assert(!fake->packet->vps);
163
164                         fake->packet->vps = paircopy(fake->packet, request->packet->vps);
165
166                         /* set the virtual server to use */
167                         if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
168                                 fake->server = vp->vp_strvalue;
169                         } else {
170                                 fake->server = inst->virtual_server;
171                         }
172
173                         RDEBUG("Processing EAP-TLS Certificate check:");
174                         debug_pair_list(fake->packet->vps);
175
176                         RDEBUG("server %s {", fake->server);
177
178                         rad_virtual_server(fake);
179
180                         RDEBUG("} # server %s", fake->server);
181
182                         /* copy the reply vps back to our reply */
183                         pairfilter(request->reply, &request->reply->vps,
184                                   &fake->reply->vps, 0, 0, TAG_ANY);
185
186                         /* reject if virtual server didn't return accept */
187                         if (fake->reply->code != PW_AUTHENTICATION_ACK) {
188                                 RDEBUG2("Certificates were rejected by the virtual server");
189                                 request_free(&fake);
190                                 eaptls_fail(handler, 0);
191                                 return 0;
192                         }
193
194                         request_free(&fake);
195                         /* success */
196                 }
197                 break;
198
199                 /*
200                  *      The TLS code is still working on the TLS
201                  *      exchange, and it's a valid TLS request.
202                  *      do nothing.
203                  */
204         case FR_TLS_HANDLED:
205                 return 1;
206
207                 /*
208                  *      Handshake is done, proceed with decoding tunneled
209                  *      data.
210                  */
211         case FR_TLS_OK:
212                 RDEBUG2("Received unexpected tunneled data after successful handshake.");
213 #ifndef NDEBUG
214                 if ((debug_flag > 2) && fr_log_fp) {
215                         unsigned int i;
216                         unsigned int data_len;
217                         unsigned char buffer[1024];
218
219                         data_len = (tls_session->record_minus)(&tls_session->dirty_in,
220                                                 buffer, sizeof(buffer));
221                         DEBUG("  Tunneled data (%u bytes)", data_len);
222                         for (i = 0; i < data_len; i++) {
223                                 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, "  %x: ", i);
224                                 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
225
226                                 fprintf(fr_log_fp, "%02x ", buffer[i]);
227                         }
228                         fprintf(fr_log_fp, "\n");
229                 }
230 #endif
231
232                 eaptls_fail(handler, 0);
233                 return 0;
234                 break;
235
236                 /*
237                  *      Anything else: fail.
238                  *
239                  *      Also, remove the session from the cache so that
240                  *      the client can't re-use it.
241                  */
242         default:
243                 tls_fail(tls_session);
244
245                 return 0;
246         }
247
248         /*
249          *      Success: Automatically return MPPE keys.
250          */
251         return eaptls_success(handler, 0);
252 }
253
254 /*
255  *      The module name should be the only globally exported symbol.
256  *      That is, everything else should be 'static'.
257  */
258 rlm_eap_module_t rlm_eap_tls = {
259         "eap_tls",
260         eaptls_attach,                  /* attach */
261         eaptls_initiate,                /* Start the initial request */
262         NULL,                           /* authorization */
263         mod_authenticate,               /* authentication */
264         NULL                            /* detach */
265 };