Free more entries in the config.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
21  * Copyright 2003  Alan DeKok <aland@freeradius.org>
22  */
23
24 #include "autoconf.h"
25
26 #ifdef HAVE_OPENSSL_RAND_H
27 #include <openssl/rand.h>
28 #endif
29
30 #include "rlm_eap_tls.h"
31
32 static CONF_PARSER module_config[] = {
33         { "rsa_key_exchange", PW_TYPE_BOOLEAN,
34           offsetof(EAP_TLS_CONF, rsa_key), NULL, "no" },
35         { "dh_key_exchange", PW_TYPE_BOOLEAN,
36           offsetof(EAP_TLS_CONF, dh_key), NULL, "yes" },
37         { "rsa_key_length", PW_TYPE_INTEGER,
38           offsetof(EAP_TLS_CONF, rsa_key_length), NULL, "512" },
39         { "dh_key_length", PW_TYPE_INTEGER,
40           offsetof(EAP_TLS_CONF, dh_key_length), NULL, "512" },
41         { "verify_depth", PW_TYPE_INTEGER,
42           offsetof(EAP_TLS_CONF, verify_depth), NULL, "0" },
43         { "CA_path", PW_TYPE_STRING_PTR,
44           offsetof(EAP_TLS_CONF, ca_path), NULL, NULL },
45         { "pem_file_type", PW_TYPE_BOOLEAN,
46           offsetof(EAP_TLS_CONF, file_type), NULL, "yes" },
47         { "private_key_file", PW_TYPE_STRING_PTR,
48           offsetof(EAP_TLS_CONF, private_key_file), NULL, NULL },
49         { "certificate_file", PW_TYPE_STRING_PTR,
50           offsetof(EAP_TLS_CONF, certificate_file), NULL, NULL },
51         { "CA_file", PW_TYPE_STRING_PTR,
52           offsetof(EAP_TLS_CONF, ca_file), NULL, NULL },
53         { "private_key_password", PW_TYPE_STRING_PTR,
54           offsetof(EAP_TLS_CONF, private_key_password), NULL, NULL },
55         { "dh_file", PW_TYPE_STRING_PTR,
56           offsetof(EAP_TLS_CONF, dh_file), NULL, NULL },
57         { "random_file", PW_TYPE_STRING_PTR,
58           offsetof(EAP_TLS_CONF, random_file), NULL, NULL },
59         { "fragment_size", PW_TYPE_INTEGER,
60           offsetof(EAP_TLS_CONF, fragment_size), NULL, "1024" },
61         { "include_length", PW_TYPE_BOOLEAN,
62           offsetof(EAP_TLS_CONF, include_length), NULL, "yes" },
63         { "check_crl", PW_TYPE_BOOLEAN,
64           offsetof(EAP_TLS_CONF, check_crl), NULL, "no"},
65         { "check_cert_cn", PW_TYPE_STRING_PTR,
66           offsetof(EAP_TLS_CONF, check_cert_cn), NULL, NULL},
67         { "cipher_list", PW_TYPE_STRING_PTR,
68           offsetof(EAP_TLS_CONF, cipher_list), NULL, NULL},
69         { "check_cert_issuer", PW_TYPE_STRING_PTR,
70           offsetof(EAP_TLS_CONF, check_cert_issuer), NULL, NULL},
71
72         { NULL, -1, 0, NULL, NULL }           /* end the list */
73 };
74
75
76 /*
77  *      TODO: Check for the type of key exchange * like conf->dh_key
78  */
79 static int load_dh_params(SSL_CTX *ctx, char *file)
80 {
81         DH *dh = NULL;
82         BIO *bio;
83
84         if ((bio = BIO_new_file(file, "r")) == NULL) {
85                 radlog(L_ERR, "rlm_eap_tls: Unable to open DH file - %s", file);
86                 return -1;
87         }
88
89         dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
90         BIO_free(bio);
91         if (SSL_CTX_set_tmp_dh(ctx, dh) < 0) {
92                 radlog(L_ERR, "rlm_eap_tls: Unable to set DH parameters");
93                 DH_free(dh);
94                 return -1;
95         }
96
97         DH_free(dh);
98         return 0;
99 }
100
101 /*
102  *      Generte ephemeral RSA keys.
103  */
104 static int generate_eph_rsa_key(SSL_CTX *ctx)
105 {
106         RSA *rsa;
107
108         rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
109
110         if (!SSL_CTX_set_tmp_rsa(ctx, rsa)) {
111                 radlog(L_ERR, "rlm_eap_tls: Couldn't set RSA key");
112                 return -1;
113         }
114
115         RSA_free(rsa);
116         return 0;
117 }
118
119
120 /*
121  *      Before trusting a certificate, you must make sure that the
122  *      certificate is 'valid'. There are several steps that your
123  *      application can take in determining if a certificate is
124  *      valid. Commonly used steps are:
125  *
126  *      1.Verifying the certificate's signature, and verifying that
127  *      the certificate has been issued by a trusted Certificate
128  *      Authority.
129  *
130  *      2.Verifying that the certificate is valid for the present date
131  *      (i.e. it is being presented within its validity dates).
132  *
133  *      3.Verifying that the certificate has not been revoked by its
134  *      issuing Certificate Authority, by checking with respect to a
135  *      Certificate Revocation List (CRL).
136  *
137  *      4.Verifying that the credentials presented by the certificate
138  *      fulfill additional requirements specific to the application,
139  *      such as with respect to access control lists or with respect
140  *      to OCSP (Online Certificate Status Processing).
141  *
142  *      NOTE: This callback will be called multiple times based on the
143  *      depth of the root certificate chain
144  */
145 static int cbtls_verify(int ok, X509_STORE_CTX *ctx)
146 {
147         char subject[1024]; /* Used for the subject name */
148         char issuer[1024]; /* Used for the issuer name */
149         char common_name[1024];
150         char cn_str[1024];
151         EAP_HANDLER *handler = NULL;
152         X509 *client_cert;
153         SSL *ssl;
154         int err, depth;
155         EAP_TLS_CONF *conf;
156         int my_ok = ok;
157
158         client_cert = X509_STORE_CTX_get_current_cert(ctx);
159         err = X509_STORE_CTX_get_error(ctx);
160         depth = X509_STORE_CTX_get_error_depth(ctx);
161
162         if (!my_ok) {
163                 radlog(L_ERR,"--> verify error:num=%d:%s\n",err,
164                         X509_verify_cert_error_string(err));
165                 return my_ok;
166         }
167
168         /*
169          * Retrieve the pointer to the SSL of the connection currently treated
170          * and the application specific data stored into the SSL object.
171          */
172         ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
173         handler = (EAP_HANDLER *)SSL_get_ex_data(ssl, 0);
174         conf = (EAP_TLS_CONF *)SSL_get_ex_data(ssl, 1);
175
176         /*
177          *      Get the Subject & Issuer
178          */
179         subject[0] = issuer[0] = '\0';
180         X509_NAME_oneline(X509_get_subject_name(client_cert), subject,
181                           sizeof(subject));
182         X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer,
183                           sizeof(issuer));
184
185         subject[sizeof(subject) - 1] = '\0';
186         issuer[sizeof(issuer) - 1] = '\0';
187
188         /*
189          *      Get the Common Name
190          */
191         X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
192                                   NID_commonName, common_name, sizeof(common_name));
193         common_name[sizeof(common_name) - 1] = '\0';
194
195         switch (ctx->error) {
196
197         case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
198                 radlog(L_ERR, "issuer= %s\n", issuer);
199                 break;
200         case X509_V_ERR_CERT_NOT_YET_VALID:
201         case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
202                 radlog(L_ERR, "notBefore=");
203 #if 0
204                 ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
205 #endif
206                 break;
207         case X509_V_ERR_CERT_HAS_EXPIRED:
208         case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
209                 radlog(L_ERR, "notAfter=");
210 #if 0
211                 ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
212 #endif
213                 break;
214         }
215
216         /*
217          *      If we're at the actual client cert, apply additional
218          *      checks.
219          */
220         if (depth == 0) {
221                 /*
222                  *      If the conf tells us to, check cert issuer
223                  *      against the specified value and fail
224                  *      verification if they don't match.
225                  */
226                 if (conf->check_cert_issuer && 
227                     (strcmp(issuer, conf->check_cert_issuer) != 0)) {
228                         radlog(L_AUTH, "rlm_eap_tls: Certificate issuer (%s) does not match specified value (%s)!", issuer, conf->check_cert_issuer);
229                         my_ok = 0;
230                 }
231
232                 /*
233                  *      If the conf tells us to, check the CN in the
234                  *      cert against xlat'ed value, but only if the
235                  *      previous checks passed.
236                  */
237                 if (my_ok && conf->check_cert_cn) {
238                         if (!radius_xlat(cn_str, sizeof(cn_str), conf->check_cert_cn, handler->request, NULL)) {
239                                 radlog(L_ERR, "rlm_eap_tls (%s): xlat failed.",
240                                        conf->check_cert_cn);
241                                 /* if this fails, fail the verification */
242                                 my_ok = 0;
243                         } else {
244                                 DEBUG2("    rlm_eap_tls: checking certificate CN (%s) with xlat'ed value (%s)", common_name, cn_str);
245                                 if (strcmp(cn_str, common_name) != 0) {
246                                         radlog(L_AUTH, "rlm_eap_tls: Certificate CN (%s) does not match specified value (%s)!", common_name, cn_str);
247                                         my_ok = 0;
248                                 }
249                         }
250                 } /* check_cert_cn */
251         } /* depth == 0 */
252
253         if (debug_flag > 0) {
254                 radlog(L_INFO, "chain-depth=%d, ", depth);
255                 radlog(L_INFO, "error=%d", err);
256
257                 radlog(L_INFO, "--> User-Name = %s", handler->identity);
258                 radlog(L_INFO, "--> BUF-Name = %s", common_name);
259                 radlog(L_INFO, "--> subject = %s", subject);
260                 radlog(L_INFO, "--> issuer  = %s", issuer);
261                 radlog(L_INFO, "--> verify return:%d", my_ok);
262         }
263         return my_ok;
264 }
265
266
267 /*
268  *      Create Global context SSL and use it in every new session
269  *
270  *      - Load the trusted CAs
271  *      - Load the Private key & the certificate
272  *      - Set the Context options & Verify options
273  */
274 static SSL_CTX *init_tls_ctx(EAP_TLS_CONF *conf)
275 {
276         SSL_METHOD *meth;
277         SSL_CTX *ctx;
278         X509_STORE *certstore;
279         int verify_mode = SSL_VERIFY_NONE;
280         int ctx_options = 0;
281         int type;
282
283         /*
284          *      Add all the default ciphers and message digests
285          *      Create our context.
286          */
287         SSL_library_init();
288         SSL_load_error_strings();
289
290         meth = TLSv1_method();
291         ctx = SSL_CTX_new(meth);
292
293         /*
294          * Identify the type of certificates that needs to be loaded
295          */
296         if (conf->file_type) {
297                 type = SSL_FILETYPE_PEM;
298         } else {
299                 type = SSL_FILETYPE_ASN1;
300         }
301
302         /*
303          * Set the password to load private key
304          */
305         if (conf->private_key_password) {
306                 SSL_CTX_set_default_passwd_cb_userdata(ctx, conf->private_key_password);
307                 SSL_CTX_set_default_passwd_cb(ctx, cbtls_password);
308         }
309
310         /*
311          *      Load our keys and certificates
312          *
313          *      If certificates are of type PEM then we can make use
314          *      of cert chain authentication using openssl api call
315          *      SSL_CTX_use_certificate_chain_file.  Please see how
316          *      the cert chain needs to be given in PEM from
317          *      openSSL.org
318          */
319         if (type == SSL_FILETYPE_PEM) {
320                 radlog(L_INFO, "rlm_eap_tls: Loading the certificate file as a chain");
321                 if (!(SSL_CTX_use_certificate_chain_file(ctx, conf->certificate_file))) {
322                         radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
323                         radlog(L_ERR, "rlm_eap_tls: Error reading certificate file");
324                         return NULL;
325                 }
326
327         } else if (!(SSL_CTX_use_certificate_file(ctx, conf->certificate_file, type))) {
328                 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
329                 radlog(L_ERR, "rlm_eap_tls: Error reading certificate file");
330                 return NULL;
331         }
332
333
334         /* Load the CAs we trust */
335         if (!SSL_CTX_load_verify_locations(ctx, conf->ca_file, conf->ca_path)) {
336                 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
337                 radlog(L_ERR, "rlm_eap_tls: Error reading Trusted root CA list");
338                 return NULL;
339         }
340         SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(conf->ca_file));
341
342         if (!(SSL_CTX_use_PrivateKey_file(ctx, conf->private_key_file, type))) {
343                 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
344                 radlog(L_ERR, "rlm_eap_tls: Error reading private key file");
345                 return NULL;
346         }
347
348         /*
349          * Check if the loaded private key is the right one
350          */
351         if (!SSL_CTX_check_private_key(ctx)) {
352                 radlog(L_ERR, "rlm_eap_tls: Private key does not match the certificate public key");
353                 return NULL;
354         }
355
356         /*
357          *      Set ctx_options
358          */
359         ctx_options |= SSL_OP_NO_SSLv2;
360         ctx_options |= SSL_OP_NO_SSLv3;
361
362         /*
363          *      SSL_OP_SINGLE_DH_USE must be used in order to prevent
364          *      small subgroup attacks and forward secrecy. Always
365          *      using
366          *
367          *      SSL_OP_SINGLE_DH_USE has an impact on the computer
368          *      time needed during negotiation, but it is not very
369          *      large.
370          */
371         ctx_options |= SSL_OP_SINGLE_DH_USE;
372         SSL_CTX_set_options(ctx, ctx_options);
373
374         /*
375          *      TODO: Set the RSA & DH
376          *      SSL_CTX_set_tmp_rsa_callback(ctx, cbtls_rsa);
377          *      SSL_CTX_set_tmp_dh_callback(ctx, cbtls_dh);
378          */
379
380         /*
381          *      set the message callback to identify the type of
382          *      message.  For every new session, there can be a
383          *      different callback argument.
384          *
385          *      SSL_CTX_set_msg_callback(ctx, cbtls_msg);
386          */
387
388         /* Set Info callback */
389         SSL_CTX_set_info_callback(ctx, cbtls_info);
390
391         /*
392          *      Check the certificates for revocation.
393          */
394 #ifdef X509_V_FLAG_CRL_CHECK
395         if (conf->check_crl) {
396           certstore = SSL_CTX_get_cert_store(ctx);
397           if (certstore == NULL) {
398             radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
399             radlog(L_ERR, "rlm_eap_tls: Error reading Certificate Store");
400             return NULL;
401           }
402           X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK);
403         }
404 #endif
405
406         /*
407          *      Set verify modes
408          *      Always verify the peer certificate
409          */
410         verify_mode |= SSL_VERIFY_PEER;
411         verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
412         verify_mode |= SSL_VERIFY_CLIENT_ONCE;
413         SSL_CTX_set_verify(ctx, verify_mode, cbtls_verify);
414
415         if (conf->verify_depth) {
416                 SSL_CTX_set_verify_depth(ctx, conf->verify_depth);
417         }
418
419         /* Load randomness */
420         if (!(RAND_load_file(conf->random_file, 1024*1024))) {
421                 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
422                 radlog(L_ERR, "rlm_eap_tls: Error loading randomness");
423                 return NULL;
424         }
425
426         /*
427          * Set the cipher list if we were told to
428          */
429         if (conf->cipher_list) {
430                 if (!SSL_CTX_set_cipher_list(ctx, conf->cipher_list)) {
431                         radlog(L_ERR, "rlm_eap_tls: Error setting cipher list");
432                         return NULL;
433                 }
434         }
435
436         return ctx;
437 }
438
439
440 /*
441  *      Detach the EAP-TLS module.
442  */
443 static int eaptls_detach(void *arg)
444 {
445         EAP_TLS_CONF     *conf;
446         eap_tls_t        *inst;
447
448         inst = (eap_tls_t *) arg;
449         conf = inst->conf;
450
451         if (conf) {
452                 free(conf->dh_file);
453                 free(conf->ca_path);
454                 free(conf->certificate_file);
455                 free(conf->private_key_file);
456                 free(conf->private_key_password);
457                 free(conf->ca_file);
458                 free(conf->random_file);
459
460                 free(conf->check_cert_cn);
461                 free(conf->check_cert_cn);
462                 free(conf->cipher_list);
463                 free(conf->check_cert_issuer);
464
465                 memset(conf, 0, sizeof(*conf));
466                 free(inst->conf);
467                 inst->conf = NULL;
468         }
469
470         if (inst->ctx) SSL_CTX_free(inst->ctx);
471         inst->ctx = NULL;
472
473         free(inst);
474
475         return 0;
476 }
477
478
479 /*
480  *      Attach the EAP-TLS module.
481  */
482 static int eaptls_attach(CONF_SECTION *cs, void **instance)
483 {
484         EAP_TLS_CONF     *conf;
485         eap_tls_t        *inst;
486
487         /* Store all these values in the data structure for later references */
488         inst = (eap_tls_t *)malloc(sizeof(*inst));
489         if (!inst) {
490                 radlog(L_ERR, "rlm_eap_tls: out of memory");
491                 return -1;
492         }
493         memset(inst, 0, sizeof(*inst));
494
495         /*
496          *      Parse the config file & get all the configured values
497          */
498         conf = (EAP_TLS_CONF *)malloc(sizeof(*conf));
499         if (conf == NULL) {
500                 radlog(L_ERR, "rlm_eap_tls: out of memory");
501                 return -1;
502         }
503         memset(conf, 0, sizeof(*conf));
504
505         inst->conf = conf;
506         if (cf_section_parse(cs, conf, module_config) < 0) {
507                 eaptls_detach(inst);
508                 return -1;
509         }
510
511
512         /*
513          *      Initialize TLS
514          */
515         inst->ctx = init_tls_ctx(conf);
516         if (inst->ctx == NULL) {
517                 eaptls_detach(inst);
518                 return -1;
519         }
520
521         if (load_dh_params(inst->ctx, conf->dh_file) < 0) {
522                 eaptls_detach(inst);
523                 return -1;
524         }
525         if (generate_eph_rsa_key(inst->ctx) < 0) {
526                 eaptls_detach(inst);
527                 return -1;
528         }
529
530         *instance = inst;
531
532         return 0;
533 }
534
535
536 /*
537  *      Send an initial eap-tls request to the peer.
538  *
539  *      Frame eap reply packet.
540  *      len = header + type + tls_typedata
541  *      tls_typedata = flags(Start (S) bit set, and no data)
542  *
543  *      Once having received the peer's Identity, the EAP server MUST
544  *      respond with an EAP-TLS/Start packet, which is an
545  *      EAP-Request packet with EAP-Type=EAP-TLS, the Start (S) bit
546  *      set, and no data.  The EAP-TLS conversation will then begin,
547  *      with the peer sending an EAP-Response packet with
548  *      EAP-Type = EAP-TLS.  The data field of that packet will
549  *      be the TLS data.
550  *
551  *      Fragment length is Framed-MTU - 4.
552  *
553  *      http://mail.frascone.com/pipermail/public/eap/2003-July/001426.html
554  */
555 static int eaptls_initiate(void *type_arg, EAP_HANDLER *handler)
556 {
557         int             status;
558         tls_session_t   *ssn;
559         eap_tls_t       *inst;
560         VALUE_PAIR      *vp;
561         int             client_cert = TRUE;
562         int             verify_mode = SSL_VERIFY_NONE;
563
564         inst = (eap_tls_t *)type_arg;
565
566         /*
567          *      If we're TTLS or PEAP, then do NOT require a client
568          *      certificate.
569          *
570          *      FIXME: This should be more configurable.
571          */
572         if (handler->eap_type != PW_EAP_TLS) {
573                 vp = pairfind(handler->request->config_items,
574                               PW_EAP_TLS_REQUIRE_CLIENT_CERT);
575                 if (!vp) {
576                         client_cert = FALSE;
577                 } else {
578                         client_cert = vp->lvalue;
579                 }
580         }
581
582         /*
583          *      Every new session is started only from EAP-TLS-START.
584          *      Before Sending EAP-TLS-START, open a new SSL session.
585          *      Create all the required data structures & store them
586          *      in Opaque.  So that we can use these data structures
587          *      when we get the response
588          */
589         ssn = eaptls_new_session(inst->ctx, client_cert);
590         if (!ssn) {
591                 return 0;
592         }
593
594         /*
595          *      Verify the peer certificate, if asked.
596          */
597         if (client_cert) {
598                 DEBUG2(" rlm_eap_tls: Requiring client certificate");
599                 verify_mode = SSL_VERIFY_PEER;
600                 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
601                 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
602         }
603         SSL_set_verify(ssn->ssl, verify_mode, cbtls_verify);
604
605         /*
606          *      Create a structure for all the items required to be
607          *      verified for each client and set that as opaque data
608          *      structure.
609          *
610          *      NOTE: If we want to set each item sepearately then
611          *      this index should be global.
612          */
613         SSL_set_ex_data(ssn->ssl, 0, (void *)handler);
614         SSL_set_ex_data(ssn->ssl, 1, (void *)inst->conf);
615
616         ssn->length_flag = inst->conf->include_length;
617
618         /*
619          *      We set a default fragment size, unless the Framed-MTU
620          *      tells us it's too big.
621          */
622         ssn->offset = inst->conf->fragment_size;
623         vp = pairfind(handler->request->packet->vps, PW_FRAMED_MTU);
624         if (vp && ((vp->lvalue - 4) < ssn->offset)) {
625                 ssn->offset = vp->lvalue - 4;
626         }
627
628         handler->opaque = ((void *)ssn);
629         handler->free_opaque = session_free;
630
631         DEBUG2("  rlm_eap_tls: Initiate");
632
633         /*
634          *      PEAP-specific breakage.
635          */
636         if (handler->eap_type == PW_EAP_PEAP) {
637                 /*
638                  *      As it is a poorly designed protocol, PEAP uses
639                  *      bits in the TLS header to indicate PEAP
640                  *      version numbers.  For now, we only support
641                  *      PEAP version 0, so it doesn't matter too much.
642                  *      However, if we support later versions of PEAP,
643                  *      we will need this flag to indicate which
644                  *      version we're currently dealing with.
645                  */
646                 ssn->peap_flag = 0x00;
647
648                 /*
649                  *      PEAP version 0 requires 'include_length = no',
650                  *      so rather than hoping the user figures it out,
651                  *      we force it here.
652                  */
653                 ssn->length_flag = 0;
654         }
655
656         /*
657          *      TLS session initialization is over.  Now handle TLS
658          *      related handshaking or application data.
659          */
660         status = eaptls_start(handler->eap_ds, ssn->peap_flag);
661         DEBUG2("  rlm_eap_tls: Start returned %d", status);
662         if (status == 0)
663                 return 0;
664
665         /*
666          *      The next stage to process the packet.
667          */
668         handler->stage = AUTHENTICATE;
669
670         return 1;
671 }
672
673 /*
674  *      Do authentication, by letting EAP-TLS do most of the work.
675  */
676 static int eaptls_authenticate(void *arg UNUSED, EAP_HANDLER *handler)
677 {
678         eaptls_status_t status;
679         tls_session_t *tls_session = (tls_session_t *) handler->opaque;
680
681         DEBUG2("  rlm_eap_tls: Authenticate");
682
683         status = eaptls_process(handler);
684         DEBUG2("  eaptls_process returned %d\n", status);
685         switch (status) {
686                 /*
687                  *      EAP-TLS handshake was successful, return an
688                  *      EAP-TLS-Success packet here.
689                  */
690         case EAPTLS_SUCCESS:
691                 break;
692
693                 /*
694                  *      The TLS code is still working on the TLS
695                  *      exchange, and it's a valid TLS request.
696                  *      do nothing.
697                  */
698         case EAPTLS_HANDLED:
699                 return 1;
700
701                 /*
702                  *      Handshake is done, proceed with decoding tunneled
703                  *      data.
704                  */
705         case EAPTLS_OK:
706                 DEBUG2("  rlm_eap_tls: Received unexpected tunneled data after successful handshake.");
707 #ifndef NDEBUG
708                 if (debug_flag > 2) {
709                         unsigned int i;
710                         unsigned int data_len;
711                         unsigned char buffer[1024];
712
713                         data_len = (tls_session->record_minus)(&tls_session->dirty_in,
714                                                 buffer, sizeof(buffer));
715                         log_debug("  Tunneled data (%u bytes)\n", data_len);
716                         for (i = 0; i < data_len; i++) {
717                                 if ((i & 0x0f) == 0x00) printf("  %x: ", i);
718                                 if ((i & 0x0f) == 0x0f) printf("\n");
719
720                                 printf("%02x ", buffer[i]);
721                         }
722                         printf("\n");
723                 }
724 #endif
725
726                 eaptls_fail(handler->eap_ds, 0);
727                 return 0;
728                 break;
729
730                 /*
731                  *      Anything else: fail.
732                  */
733         default:
734                 return 0;
735         }
736
737         /*
738          *      Success: Return MPPE keys.
739          */
740         eaptls_success(handler->eap_ds, 0);
741         eaptls_gen_mppe_keys(&handler->request->reply->vps,
742                              tls_session->ssl,
743                              "client EAP encryption");
744         return 1;
745 }
746
747 /*
748  *      The module name should be the only globally exported symbol.
749  *      That is, everything else should be 'static'.
750  */
751 EAP_TYPE rlm_eap_tls = {
752         "eap_tls",
753         eaptls_attach,                  /* attach */
754         eaptls_initiate,                /* Start the initial request */
755         NULL,                           /* authorization */
756         eaptls_authenticate,            /* authentication */
757         eaptls_detach                   /* detach */
758 };