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