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