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