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