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