Debian Codegraph @

pppd/eap-tls.c

raw
1 /* * eap-tls.c - EAP-TLS implementation for PPP
2 *
3 * Copyright (c) Beniamino Galvani 2005 All rights reserved.
4 * Jan Just Keijser 2006-2019 All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
21 *
22 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 *
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <string.h>
37 #include <strings.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42
43 #include <openssl/conf.h>
44 #ifndef OPENSSL_NO_ENGINE
45 #include <openssl/engine.h>
46 #endif
47 #include <openssl/ssl.h>
48 #include <openssl/hmac.h>
49 #include <openssl/err.h>
50 #include <openssl/ui.h>
51 #include <openssl/x509v3.h>
52 #include <openssl/pkcs12.h>
53
54 #include "pppd-private.h"
55 #include "tls.h"
56 #include "eap.h"
57 #include "eap-tls.h"
58 #include "fsm.h"
59 #include "lcp.h"
60 #include "chap_ms.h"
61 #include "mppe.h"
62 #include "pathnames.h"
63
64 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
65 #define SSL3_RT_HEADER 0x100
66 #endif
67
68 typedef struct pw_cb_data
69 {
70 const void *password;
71 const char *prompt_info;
72 } PW_CB_DATA;
73
74 #ifndef OPENSSL_NO_ENGINE
75 /* The openssl configuration file and engines can be loaded only once */
76 static CONF *ssl_config = NULL;
77 static ENGINE *cert_engine = NULL;
78 static ENGINE *pkey_engine = NULL;
79 #endif
80
81 /* TLSv1.3 do we have a session ticket ? */
82 static int have_session_ticket = 0;
83
84 void ssl_msg_callback(int write_p, int version, int ct, const void *buf,
85 size_t len, SSL * ssl, void *arg);
86 int ssl_new_session_cb(SSL *s, SSL_SESSION *sess);
87
88 #ifdef PPP_WITH_MPPE
89 #define EAPTLS_MPPE_KEY_LEN 32
90
91 /*
92 * Generate keys according to RFC 2716 and add to reply
93 */
94 void eaptls_gen_mppe_keys(struct eaptls_session *ets, int client)
95 {
96 unsigned char out[4*EAPTLS_MPPE_KEY_LEN];
97 const char *prf_label;
98 size_t prf_size;
99 unsigned char eap_tls13_context[] = { EAPT_TLS };
100 unsigned char *context = NULL;
101 size_t context_len = 0;
102 unsigned char *p;
103
104 dbglog("EAP-TLS generating MPPE keys");
105 if (ets->tls_v13)
106 {
107 prf_label = "EXPORTER_EAP_TLS_Key_Material";
108 context = eap_tls13_context;
109 context_len = 1;
110 }
111 else
112 {
113 prf_label = "client EAP encryption";
114 }
115
116 dbglog("EAP-TLS PRF label = %s", prf_label);
117 prf_size = strlen(prf_label);
118 if (SSL_export_keying_material(ets->ssl, out, sizeof(out), prf_label, prf_size,
119 context, context_len, 0) != 1)
120 {
121 warn( "EAP-TLS: Failed generating keying material" );
122 return;
123 }
124
125 /*
126 * We now have the master send and receive keys.
127 * From these, generate the session send and receive keys.
128 * (see RFC3079 / draft-ietf-pppext-mppe-keys-03.txt for details)
129 */
130 if (client)
131 {
132 mppe_set_keys(out, out + EAPTLS_MPPE_KEY_LEN, EAPTLS_MPPE_KEY_LEN);
133 }
134 else
135 {
136 mppe_set_keys(out + EAPTLS_MPPE_KEY_LEN, out, EAPTLS_MPPE_KEY_LEN);
137 }
138 }
139
140 #endif /* PPP_WITH_MPPE */
141
142 int password_callback (char *buf, int size, int rwflag, void *u)
143 {
144 if (buf)
145 {
146 strlcpy (buf, passwd, size);
147 return strlen (buf);
148 }
149 return 0;
150 }
151
152
153 CONF *eaptls_ssl_load_config( void )
154 {
155 CONF *config;
156 int ret_code;
157 long error_line = 33;
158
159 config = NCONF_new( NULL );
160 dbglog( "Loading OpenSSL config file" );
161 ret_code = NCONF_load( config, PPP_PATH_OPENSSLCONFFILE, &error_line );
162 if (ret_code == 0)
163 {
164 warn( "EAP-TLS: Error in OpenSSL config file %s at line %d", PPP_PATH_OPENSSLCONFFILE, error_line );
165 NCONF_free( config );
166 config = NULL;
167 ERR_clear_error();
168 }
169
170 dbglog( "Loading OpenSSL built-ins" );
171 #ifndef OPENSSL_NO_ENGINE
172 ENGINE_load_builtin_engines();
173 #endif
174 #if !defined(LIBRESSL_VERSION_NUMBER) || (LIBRESSL_VERSION_NUMBER < 0x4000000fL)
175 OPENSSL_load_builtin_modules();
176 #endif
177
178 dbglog( "Loading OpenSSL configured modules" );
179 if (CONF_modules_load( config, NULL, 0 ) <= 0 )
180 {
181 warn( "EAP-TLS: Error loading OpenSSL modules" );
182 tls_log_sslerr();
183 config = NULL;
184 }
185
186 return config;
187 }
188
189 #ifndef OPENSSL_NO_ENGINE
190 ENGINE *eaptls_ssl_load_engine( char *engine_name )
191 {
192 ENGINE *e = NULL;
193
194 dbglog( "Enabling OpenSSL auto engines" );
195 ENGINE_register_all_complete();
196
197 dbglog( "Loading OpenSSL '%s' engine support", engine_name );
198 e = ENGINE_by_id( engine_name );
199 if (!e)
200 {
201 dbglog( "EAP-TLS: Cannot load '%s' engine support, trying 'dynamic'", engine_name );
202 e = ENGINE_by_id( "dynamic" );
203 if (e)
204 {
205 if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine_name, 0)
206 || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
207 {
208 warn( "EAP-TLS: Error loading dynamic engine '%s'", engine_name );
209 tls_log_sslerr();
210 ENGINE_free(e);
211 e = NULL;
212 }
213 }
214 else
215 {
216 warn( "EAP-TLS: Cannot load dynamic engine support" );
217 }
218 }
219
220 if (e)
221 {
222 dbglog( "Initialising engine" );
223 if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
224 {
225 warn( "EAP-TLS: Cannot use that engine" );
226 tls_log_sslerr();
227 ENGINE_free(e);
228 e = NULL;
229 }
230 }
231
232 return e;
233 }
234 #endif
235
236
237 #ifndef OPENSSL_NO_ENGINE
238 static int eaptls_UI_writer(UI *ui, UI_STRING *uis)
239 {
240 PW_CB_DATA* cb_data = (PW_CB_DATA*)UI_get0_user_data(ui);
241 UI_set_result(ui, uis, cb_data->password);
242 return 1;
243 }
244
245 static int eaptls_UI_stub(UI* ui) {
246 return 1;
247 }
248
249 static int eaptls_UI_reader(UI *ui, UI_STRING *uis) {
250 return 1;
251 }
252 #endif
253
254 /*
255 * Initialize the SSL stacks and tests if certificates, key and crl
256 * for client or server use can be loaded.
257 */
258 SSL_CTX *eaptls_init_ssl(int init_server, char *cacertfile, char *capath,
259 char *certfile, char *privkeyfile, char *pkcs12)
260 {
261 #ifndef OPENSSL_NO_ENGINE
262 char *cert_engine_name = NULL;
263 char *pkey_engine_name = NULL;
264 char *idx;
265 #endif
266 SSL_CTX *ctx;
267 SSL *ssl;
268 X509 *tmp;
269 X509 *cert = NULL;
270 PKCS12 *p12 = NULL;
271 EVP_PKEY *pkey = NULL;
272 STACK_OF(X509) *chain = NULL;
273 BIO *input;
274 int ret;
275 int reason;
276
277 /*
278 * Without these can't continue
279 */
280 if (!pkcs12[0])
281 {
282 if (!(cacertfile[0] || capath[0]))
283 {
284 error("EAP-TLS: CA certificate file or path missing");
285 return NULL;
286 }
287
288 if (!certfile[0])
289 {
290 error("EAP-TLS: Certificate missing");
291 return NULL;
292 }
293
294 if (!privkeyfile[0])
295 {
296 error("EAP-TLS: Private key missing");
297 return NULL;
298 }
299 }
300
301 tls_init();
302
303 #ifndef OPENSSL_NO_ENGINE
304 /* load the openssl config file only once and load it before triggering
305 the loading of a global openssl config file via SSL_CTX_new()
306 */
307 if (!ssl_config)
308 ssl_config = eaptls_ssl_load_config();
309 #endif
310
311 ctx = SSL_CTX_new(tls_method());
312 if (!ctx) {
313 error("EAP-TLS: Cannot initialize SSL CTX context");
314 goto fail;
315 }
316
317 #ifndef OPENSSL_NO_ENGINE
318 /* if the certificate filename is of the form engine:id. e.g.
319 pkcs11:12345
320 then we try to load and use this engine.
321 If the certificate filename starts with a / or . then we
322 ALWAYS assume it is a file and not an engine/pkcs11 identifier
323 */
324 if ( (idx = index( certfile, ':' )) != NULL )
325 {
326 cert_engine_name = strdup( certfile );
327 cert_engine_name[idx - certfile] = 0;
328
329 dbglog( "Using engine '%s' for certificate, URI: '%s'",
330 cert_engine_name, certfile );
331 }
332
333 /* if the privatekey filename is of the form engine:id. e.g.
334 pkcs11:12345
335 then we try to load and use this engine.
336 If the privatekey filename starts with a / or . then we
337 ALWAYS assume it is a file and not an engine/pkcs11 identifier
338 */
339 if ( (idx = index( privkeyfile, ':' )) != NULL )
340 {
341 pkey_engine_name = strdup( privkeyfile );
342 pkey_engine_name[idx - privkeyfile] = 0;
343
344 dbglog( "Using engine '%s' for private key, URI: '%s'",
345 pkey_engine_name, privkeyfile );
346 }
347
348 if (cert_engine_name && pkey_engine_name)
349 {
350 if (strlen( certfile ) - strlen( cert_engine_name ) == 1)
351 {
352 if (strlen( privkeyfile ) - strlen( pkey_engine_name ) == 1)
353 error( "EAP-TLS: both the certificate and privatekey identifiers are missing!" );
354 else
355 {
356 dbglog( "Substituting privatekey identifier for certificate identifier" );
357 certfile = privkeyfile;
358 }
359 }
360 else
361 {
362 if (strlen( privkeyfile ) - strlen( pkey_engine_name ) == 1)
363 {
364 dbglog( "Substituting certificate identifier for privatekey identifier" );
365 privkeyfile = certfile;
366 }
367 }
368 }
369
370 if (ssl_config && cert_engine_name)
371 cert_engine = eaptls_ssl_load_engine( cert_engine_name );
372
373 if (ssl_config && pkey_engine_name)
374 {
375 /* don't load the same engine twice */
376 if ( cert_engine && strcmp( cert_engine_name, pkey_engine_name) == 0 )
377 pkey_engine = cert_engine;
378 else
379 pkey_engine = eaptls_ssl_load_engine( pkey_engine_name );
380 }
381
382 if (cert_engine_name)
383 free(cert_engine_name);
384
385 if (pkey_engine_name)
386 free(pkey_engine_name);
387
388 #endif
389
390 SSL_CTX_set_default_passwd_cb (ctx, password_callback);
391
392 if (tls_set_ca(ctx, capath, cacertfile) != 0) {
393 goto fail;
394 }
395
396 if (init_server)
397 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(cacertfile));
398
399 #ifndef OPENSSL_NO_ENGINE
400 if (cert_engine)
401 {
402 struct
403 {
404 const char *s_slot_cert_id;
405 X509 *cert;
406 } cert_info;
407
408 cert_info.s_slot_cert_id = certfile;
409 cert_info.cert = NULL;
410
411 if (!ENGINE_ctrl_cmd( cert_engine, "LOAD_CERT_CTRL", 0, &cert_info, NULL, 0 ) )
412 {
413 error( "EAP-TLS: Error loading certificate with URI '%s' from engine", certfile );
414 goto fail;
415 }
416
417 if (cert_info.cert)
418 {
419 dbglog( "Got the certificate" );
420 dbglog( "subject = %s", X509_NAME_oneline( X509_get_subject_name( cert_info.cert ), NULL, 0 ) );
421 cert = cert_info.cert;
422 }
423 else
424 {
425 warn("EAP-TLS: Cannot load key with URI: '%s'", certfile );
426 tls_log_sslerr();
427 }
428 }
429 else
430 #endif
431 {
432 if (pkcs12[0])
433 {
434 input = BIO_new_file(pkcs12, "r");
435 if (input == NULL)
436 {
437 error("EAP-TLS: Cannot open `%s' PKCS12 for input", pkcs12);
438 goto fail;
439 }
440
441 p12 = d2i_PKCS12_bio(input, NULL);
442 BIO_free(input);
443 if (!p12)
444 {
445 error("EAP-TLS: Cannot load PKCS12 certificate");
446 goto fail;
447 }
448
449 if (PKCS12_parse(p12, passwd, &pkey, &cert, &chain) != 1)
450 {
451 error("EAP-TLS: Cannot parse PKCS12 certificate, invalid password");
452 PKCS12_free(p12);
453 goto fail;
454 }
455
456 PKCS12_free(p12);
457 }
458 else
459 {
460 if (!SSL_CTX_use_certificate_chain_file(ctx, certfile))
461 {
462 error( "EAP-TLS: Cannot load certificate %s", certfile );
463 goto fail;
464 }
465 }
466 }
467
468 if (cert)
469 {
470 if (!SSL_CTX_use_certificate(ctx, cert))
471 {
472 error("EAP-TLS: Cannot use load certificate");
473 goto fail;
474 }
475
476 if (chain)
477 {
478 int i;
479 for (i = 0; i < sk_X509_num(chain); i++)
480 {
481 if (!SSL_CTX_add_extra_chain_cert(ctx, sk_X509_value(chain, i)))
482 {
483 error("EAP-TLS: Cannot add extra chain certificate");
484 goto fail;
485 }
486 }
487 }
488 }
489
490 /*
491 * Check the Before and After dates of the certificate
492 */
493 ssl = SSL_new(ctx);
494 tmp = SSL_get_certificate(ssl);
495
496 ret = X509_cmp_time(X509_get_notBefore(tmp), NULL);
497 if (ret == 0)
498 {
499 warn( "EAP-TLS: Failed to read certificate notBefore field.");
500 }
501 if (ret > 0)
502 {
503 warn( "EAP-TLS: Your certificate is not yet valid!");
504 }
505
506 ret = X509_cmp_time(X509_get_notAfter(tmp), NULL);
507 if (ret == 0)
508 {
509 warn( "EAP-TLS: Failed to read certificate notAfter field.");
510 }
511 if (ret < 0)
512 {
513 warn( "EAP-TLS: Your certificate has expired!");
514 }
515 SSL_free(ssl);
516
517 #ifndef OPENSSL_NO_ENGINE
518 if (pkey_engine)
519 {
520 PW_CB_DATA cb_data;
521
522 cb_data.password = passwd;
523 cb_data.prompt_info = privkeyfile;
524
525 if (passwd[0] != 0)
526 {
527 UI_METHOD* transfer_pin = UI_create_method("transfer_pin");
528
529 UI_method_set_writer(transfer_pin, eaptls_UI_writer);
530 UI_method_set_opener(transfer_pin, eaptls_UI_stub);
531 UI_method_set_closer(transfer_pin, eaptls_UI_stub);
532 UI_method_set_flusher(transfer_pin, eaptls_UI_stub);
533 UI_method_set_reader(transfer_pin, eaptls_UI_reader);
534
535 dbglog( "Using our private key URI: '%s' in engine", privkeyfile );
536 pkey = ENGINE_load_private_key(pkey_engine, privkeyfile, transfer_pin, &cb_data);
537
538 if (transfer_pin) UI_destroy_method(transfer_pin);
539 }
540 else {
541 dbglog( "Loading private key URI: '%s' from engine", privkeyfile );
542 pkey = ENGINE_load_private_key(pkey_engine, privkeyfile, NULL, NULL);
543 }
544 }
545 else
546 #endif
547 {
548 if (!pkey)
549 {
550 input = BIO_new_file(privkeyfile, "r");
551 if (!input)
552 {
553 error("EAP-TLS: Could not open private key, %s", privkeyfile);
554 goto fail;
555 }
556
557 pkey = PEM_read_bio_PrivateKey(input, NULL, password_callback, NULL);
558 BIO_free(input);
559 if (!pkey)
560 {
561 error("EAP-TLS: Cannot load private key, %s", privkeyfile);
562 goto fail;
563 }
564 }
565 }
566
567 if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1)
568 {
569 error("EAP-TLS: Cannot use private key");
570 goto fail;
571 }
572
573 if (SSL_CTX_check_private_key(ctx) != 1)
574 {
575 error("EAP-TLS: Private key fails security check");
576 goto fail;
577 }
578
579 /* Configure the default options */
580 tls_set_opts(ctx);
581
582 /* Set up a SSL Session cache with a callback. This is needed for TLSv1.3+.
583 * During the initial handshake the server signals to the client early on
584 * that the handshake is finished, even before the client has sent its
585 * credentials to the server. The actual connection (and moment that the
586 * client sends its credentials) only starts after the arrival of the first
587 * session ticket. The 'ssl_new_session_cb' catches this ticket.
588 */
589 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE);
590 SSL_CTX_sess_set_new_cb(ctx, ssl_new_session_cb);
591
592 /* Configure the maximum SSL version */
593 tls_set_version(ctx, max_tls_version);
594
595 /* Configure the callback */
596 if (tls_set_verify(ctx, 5)) {
597 goto fail;
598 }
599
600 /* Configure CRL check (if any) */
601 if (tls_set_crl(ctx, crl_dir, crl_file)) {
602 goto fail;
603 }
604
605 return ctx;
606
607 fail:
608
609 if (cert)
610 X509_free(cert);
611
612 if (pkey)
613 EVP_PKEY_free(pkey);
614
615 if (chain)
616 sk_X509_pop_free(chain, X509_free);
617
618 tls_log_sslerr();
619 SSL_CTX_free(ctx);
620 return NULL;
621 }
622
623 /*
624 * Determine the maximum packet size by looking at the LCP handshake
625 */
626
627 int eaptls_get_mtu(int unit)
628 {
629 int mtu, mru;
630
631 lcp_options *wo = &lcp_wantoptions[unit];
632 lcp_options *go = &lcp_gotoptions[unit];
633 lcp_options *ho = &lcp_hisoptions[unit];
634 lcp_options *ao = &lcp_allowoptions[unit];
635
636 mtu = ho->neg_mru? ho->mru: PPP_MRU;
637 mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
638 mtu = MIN(MIN(mtu, mru), ao->mru)- PPP_HDRLEN - 10;
639
640 dbglog("MTU = %d", mtu);
641 return mtu;
642 }
643
644
645 /*
646 * Init the ssl handshake (server mode)
647 */
648 int eaptls_init_ssl_server(eap_state * esp)
649 {
650 struct eaptls_session *ets;
651 char servcertfile[MAXWORDLEN];
652 char clicertfile[MAXWORDLEN];
653 char cacertfile[MAXWORDLEN];
654 char capath[MAXWORDLEN];
655 char pkfile[MAXWORDLEN];
656 char pkcs12[MAXWORDLEN];
657
658 /*
659 * Allocate new eaptls session
660 */
661 esp->es_server.ea_session = malloc(sizeof(struct eaptls_session));
662 if (!esp->es_server.ea_session)
663 fatal("Allocation error");
664 ets = esp->es_server.ea_session;
665
666 if (!esp->es_server.ea_peer) {
667 error("EAP-TLS: Error: client name not set (BUG)");
668 return 0;
669 }
670
671 dbglog( "getting eaptls secret" );
672 if (!get_eaptls_secret(esp->es_unit, esp->es_server.ea_peer,
673 esp->es_server.ea_name, clicertfile,
674 servcertfile, cacertfile, capath, pkfile, pkcs12, 1)) {
675 error( "EAP-TLS: Cannot get secret/password for client \"%s\", server \"%s\"",
676 esp->es_server.ea_peer, esp->es_server.ea_name );
677 return 0;
678 }
679
680 ets->mtu = eaptls_get_mtu(esp->es_unit);
681
682 ets->ctx = eaptls_init_ssl(1, cacertfile, capath, servcertfile, pkfile, pkcs12);
683 if (!ets->ctx)
684 goto fail;
685
686 if (!(ets->ssl = SSL_new(ets->ctx)))
687 goto fail;
688
689 if (tls_set_verify_info(ets->ssl, esp->es_server.ea_peer,
690 clicertfile, 0, &ets->info))
691 goto fail;
692
693 /*
694 * Set auto-retry to avoid timeouts on BIO_read
695 */
696 SSL_set_mode(ets->ssl, SSL_MODE_AUTO_RETRY);
697
698 /*
699 * Initialize the BIOs we use to read/write to ssl engine
700 */
701 ets->into_ssl = BIO_new(BIO_s_mem());
702 ets->from_ssl = BIO_new(BIO_s_mem());
703 SSL_set_bio(ets->ssl, ets->into_ssl, ets->from_ssl);
704
705 SSL_set_msg_callback(ets->ssl, ssl_msg_callback);
706 SSL_set_msg_callback_arg(ets->ssl, ets);
707
708 SSL_set_accept_state(ets->ssl);
709
710 ets->tls_v13 = 0;
711
712 ets->data = NULL;
713 ets->datalen = 0;
714 ets->alert_sent = 0;
715 ets->alert_recv = 0;
716 return 1;
717
718 fail:
719 SSL_CTX_free(ets->ctx);
720 return 0;
721 }
722
723 /*
724 * Init the ssl handshake (client mode)
725 */
726 int eaptls_init_ssl_client(eap_state * esp)
727 {
728 struct eaptls_session *ets;
729 char servcertfile[MAXWORDLEN];
730 char clicertfile[MAXWORDLEN];
731 char cacertfile[MAXWORDLEN];
732 char capath[MAXWORDLEN];
733 char pkfile[MAXWORDLEN];
734 char pkcs12[MAXWORDLEN];
735
736 /*
737 * Allocate new eaptls session
738 */
739 esp->es_client.ea_session = malloc(sizeof(struct eaptls_session));
740 if (!esp->es_client.ea_session)
741 fatal("Allocation error");
742 ets = esp->es_client.ea_session;
743 ets->mtu = eaptls_get_mtu(esp->es_unit);
744
745 dbglog( "calling get_eaptls_secret" );
746 if (!get_eaptls_secret(esp->es_unit, esp->es_client.ea_name,
747 esp->es_client.ea_peer, clicertfile,
748 servcertfile, cacertfile, capath, pkfile, pkcs12, 0)) {
749 error( "EAP-TLS: Cannot get secret/password for client \"%s\", server \"%s\"",
750 esp->es_client.ea_name, esp->es_client.ea_peer);
751 return 0;
752 }
753
754 dbglog( "calling eaptls_init_ssl" );
755 ets->ctx = eaptls_init_ssl(0, cacertfile, capath, clicertfile, pkfile, pkcs12);
756 if (!ets->ctx)
757 goto fail;
758
759 ets->ssl = SSL_new(ets->ctx);
760 if (!ets->ssl)
761 goto fail;
762
763 if (tls_set_verify_info(ets->ssl, esp->es_client.ea_peer,
764 servcertfile, 0, &ets->info))
765 goto fail;
766
767 /*
768 * Initialize the BIOs we use to read/write to ssl engine
769 */
770 dbglog( "Initializing SSL BIOs" );
771 ets->into_ssl = BIO_new(BIO_s_mem());
772 ets->from_ssl = BIO_new(BIO_s_mem());
773 SSL_set_bio(ets->ssl, ets->into_ssl, ets->from_ssl);
774
775 SSL_set_msg_callback(ets->ssl, ssl_msg_callback);
776 SSL_set_msg_callback_arg(ets->ssl, ets);
777 SSL_set_connect_state(ets->ssl);
778
779 ets->tls_v13 = 0;
780
781 ets->data = NULL;
782 ets->datalen = 0;
783 ets->alert_sent = 0;
784 ets->alert_recv = 0;
785 return 1;
786
787 fail:
788 dbglog( "eaptls_init_ssl_client: fail" );
789 SSL_CTX_free(ets->ctx);
790 return 0;
791
792 }
793
794 void eaptls_free_session(struct eaptls_session *ets)
795 {
796 if (ets->ssl)
797 SSL_free(ets->ssl);
798
799 if (ets->ctx)
800 SSL_CTX_free(ets->ctx);
801
802 if (ets->info)
803 tls_free_verify_info(&ets->info);
804
805 free(ets);
806 }
807
808
809 int eaptls_is_init_finished(struct eaptls_session *ets)
810 {
811 if (ets->ssl && SSL_is_init_finished(ets->ssl))
812 {
813 if (ets->tls_v13)
814 return have_session_ticket;
815 else
816 return 1;
817 }
818
819 return 0;
820 }
821
822 /*
823 * Handle a received packet, reassembling fragmented messages and
824 * passing them to the ssl engine
825 */
826 int eaptls_receive(struct eaptls_session *ets, u_char * inp, int len)
827 {
828 u_char flags;
829 u_int tlslen = 0;
830 u_char dummy[65536];
831
832 if (len < 1) {
833 warn("EAP-TLS: received no or invalid data");
834 return 1;
835 }
836
837 GETCHAR(flags, inp);
838 len--;
839
840 if (flags & EAP_TLS_FLAGS_LI && len > 4) {
841 /*
842 * LenghtIncluded flag set -> this is the first packet of a message
843 */
844
845 /*
846 * the first 4 octets are the length of the EAP-TLS message
847 */
848 GETLONG(tlslen, inp);
849 len -= 4;
850
851 if (!ets->data) {
852
853 if (tlslen > EAP_TLS_MAX_LEN) {
854 error("EAP-TLS: TLS message length > %d, truncated", EAP_TLS_MAX_LEN);
855 tlslen = EAP_TLS_MAX_LEN;
856 }
857
858 /*
859 * Allocate memory for the whole message
860 */
861 ets->data = malloc(tlslen);
862 if (!ets->data)
863 fatal("EAP-TLS: allocation error\n");
864
865 ets->datalen = 0;
866 ets->tlslen = tlslen;
867 }
868 else
869 warn("EAP-TLS: non-first LI packet? that's odd...");
870 }
871 else if (!ets->data) {
872 /*
873 * A non fragmented message without LI flag
874 */
875
876 ets->data = malloc(len);
877 if (!ets->data)
878 fatal("EAP-TLS: memory allocation error in eaptls_receive\n");
879
880 ets->datalen = 0;
881 ets->tlslen = len;
882 }
883
884 if (flags & EAP_TLS_FLAGS_MF)
885 ets->frag = 1;
886 else
887 ets->frag = 0;
888
889 if (len < 0) {
890 warn("EAP-TLS: received malformed data");
891 return 1;
892 }
893
894 if (len + ets->datalen > ets->tlslen) {
895 warn("EAP-TLS: received data > TLS message length");
896 return 1;
897 }
898
899 BCOPY(inp, ets->data + ets->datalen, len);
900 ets->datalen += len;
901
902 if (!ets->frag) {
903
904 /*
905 * If we have the whole message, pass it to ssl
906 */
907
908 if (ets->datalen != ets->tlslen) {
909 warn("EAP-TLS: received data != TLS message length");
910 return 1;
911 }
912
913 if (BIO_write(ets->into_ssl, ets->data, ets->datalen) == -1)
914 tls_log_sslerr();
915
916 SSL_read(ets->ssl, dummy, 65536);
917
918 free(ets->data);
919 ets->data = NULL;
920 ets->datalen = 0;
921 }
922
923 return 0;
924 }
925
926 /*
927 * Return an eap-tls packet in outp.
928 * A TLS message read from the ssl engine is buffered in ets->data.
929 * At each call we control if there is buffered data and send a
930 * packet of mtu bytes.
931 */
932 int eaptls_send(struct eaptls_session *ets, u_char ** outp)
933 {
934 bool first = 0;
935 int size;
936 u_char fromtls[65536];
937 int res;
938 u_char *start;
939
940 start = *outp;
941
942 if (!ets->data)
943 {
944 if(!ets->alert_sent)
945 {
946 res = SSL_read(ets->ssl, fromtls, 65536);
947 }
948
949 /*
950 * Read from ssl
951 */
952 if ((res = BIO_read(ets->from_ssl, fromtls, 65536)) == -1)
953 {
954 warn("EAP-TLS send: No data from BIO_read");
955 return 1;
956 }
957
958 ets->datalen = res;
959
960 ets->data = malloc(ets->datalen);
961 if (!ets->data)
962 fatal("EAP-TLS: memory allocation error in eaptls_send\n");
963
964 BCOPY(fromtls, ets->data, ets->datalen);
965
966 ets->offset = 0;
967 first = 1;
968 }
969
970 size = ets->datalen - ets->offset;
971
972 if (size > ets->mtu) {
973 size = ets->mtu;
974 ets->frag = 1;
975 } else
976 ets->frag = 0;
977
978 PUTCHAR(EAPT_TLS, *outp);
979
980 /*
981 * Set right flags and length if necessary
982 */
983 if (ets->frag && first) {
984 PUTCHAR(EAP_TLS_FLAGS_LI | EAP_TLS_FLAGS_MF, *outp);
985 PUTLONG(ets->datalen, *outp);
986 } else if (ets->frag) {
987 PUTCHAR(EAP_TLS_FLAGS_MF, *outp);
988 } else
989 PUTCHAR(0, *outp);
990
991 /*
992 * Copy the data in outp
993 */
994 BCOPY(ets->data + ets->offset, *outp, size);
995 INCPTR(size, *outp);
996
997 /*
998 * Copy the packet in retransmission buffer
999 */
1000 BCOPY(start, &ets->rtx[0], *outp - start);
1001 ets->rtx_len = *outp - start;
1002
1003 ets->offset += size;
1004
1005 if (ets->offset >= ets->datalen) {
1006
1007 /*
1008 * The whole message has been sent
1009 */
1010
1011 free(ets->data);
1012 ets->data = NULL;
1013 ets->datalen = 0;
1014 ets->offset = 0;
1015 }
1016
1017 return 0;
1018 }
1019
1020 /*
1021 * Get the sent packet from the retransmission buffer
1022 */
1023 void eaptls_retransmit(struct eaptls_session *ets, u_char ** outp)
1024 {
1025 BCOPY(ets->rtx, *outp, ets->rtx_len);
1026 INCPTR(ets->rtx_len, *outp);
1027 }
1028
1029 /*
1030 * Every sent & received message this callback function is invoked,
1031 * so we know when alert messages have arrived or are sent and
1032 * we can print debug information about TLS handshake.
1033 */
1034 void
1035 ssl_msg_callback(int write_p, int version, int content_type,
1036 const void *buf, size_t len, SSL * ssl, void *arg)
1037 {
1038 char string[256];
1039 struct eaptls_session *ets = (struct eaptls_session *)arg;
1040 unsigned char code;
1041 const unsigned char*msg = buf;
1042 int hvers = msg[1] << 8 | msg[2];
1043
1044 if(write_p)
1045 strcpy(string, " -> ");
1046 else
1047 strcpy(string, " <- ");
1048
1049 switch(content_type) {
1050
1051 case SSL3_RT_HEADER:
1052 strcat(string, "SSL/TLS Header: ");
1053 switch(hvers) {
1054 case SSL3_VERSION:
1055 strcat(string, "SSL 3.0");
1056 break;
1057 case TLS1_VERSION:
1058 strcat(string, "TLS 1.0");
1059 break;
1060 case TLS1_1_VERSION:
1061 strcat(string, "TLS 1.1");
1062 break;
1063 case TLS1_2_VERSION:
1064 strcat(string, "TLS 1.2");
1065 break;
1066 default:
1067 sprintf(string, "SSL/TLS Header: Unknown version (%d)", hvers);
1068 }
1069 break;
1070
1071 case SSL3_RT_ALERT:
1072 strcat(string, "Alert: ");
1073 code = msg[1];
1074
1075 if (write_p) {
1076 ets->alert_sent = 1;
1077 ets->alert_sent_desc = code;
1078 } else {
1079 ets->alert_recv = 1;
1080 ets->alert_recv_desc = code;
1081 }
1082
1083 strcat(string, SSL_alert_desc_string_long(code));
1084 break;
1085
1086 case SSL3_RT_CHANGE_CIPHER_SPEC:
1087 strcat(string, "ChangeCipherSpec");
1088 break;
1089
1090 #ifdef SSL3_RT_INNER_CONTENT_TYPE
1091 case SSL3_RT_INNER_CONTENT_TYPE:
1092 strcat(string, "InnerContentType (TLS1.3)");
1093 break;
1094 #endif
1095
1096 case SSL3_RT_HANDSHAKE:
1097
1098 strcat(string, "Handshake: ");
1099 code = msg[0];
1100
1101 switch(code) {
1102 case SSL3_MT_HELLO_REQUEST:
1103 strcat(string,"Hello Request");
1104 break;
1105 case SSL3_MT_CLIENT_HELLO:
1106 strcat(string,"Client Hello");
1107 break;
1108 case SSL3_MT_SERVER_HELLO:
1109 strcat(string,"Server Hello");
1110 break;
1111 #ifdef SSL3_MT_NEWSESSION_TICKET
1112 case SSL3_MT_NEWSESSION_TICKET:
1113 strcat(string,"New Session Ticket");
1114 break;
1115 #endif
1116 #ifdef SSL3_MT_END_OF_EARLY_DATA
1117 case SSL3_MT_END_OF_EARLY_DATA:
1118 strcat(string,"End of Early Data");
1119 break;
1120 #endif
1121 #ifdef SSL3_MT_ENCRYPTED_EXTENSIONS
1122 case SSL3_MT_ENCRYPTED_EXTENSIONS:
1123 strcat(string,"Encryped Extensions");
1124 break;
1125 #endif
1126 case SSL3_MT_CERTIFICATE:
1127 strcat(string,"Certificate");
1128 break;
1129 case SSL3_MT_SERVER_KEY_EXCHANGE:
1130 strcat(string,"Server Key Exchange");
1131 break;
1132 case SSL3_MT_CERTIFICATE_REQUEST:
1133 strcat(string,"Certificate Request");
1134 break;
1135 case SSL3_MT_SERVER_DONE:
1136 strcat(string,"Server Hello Done");
1137 break;
1138 case SSL3_MT_CERTIFICATE_VERIFY:
1139 strcat(string,"Certificate Verify");
1140 break;
1141 case SSL3_MT_CLIENT_KEY_EXCHANGE:
1142 strcat(string,"Client Key Exchange");
1143 break;
1144 case SSL3_MT_FINISHED:
1145 strcat(string,"Finished: ");
1146 hvers = SSL_version(ssl);
1147 switch(hvers){
1148 case SSL3_VERSION:
1149 strcat(string, "SSL 3.0");
1150 break;
1151 case TLS1_VERSION:
1152 strcat(string, "TLS 1.0");
1153 break;
1154 case TLS1_1_VERSION:
1155 strcat(string, "TLS 1.1");
1156 break;
1157 case TLS1_2_VERSION:
1158 strcat(string, "TLS 1.2");
1159 break;
1160 #ifdef TLS1_3_VERSION
1161 case TLS1_3_VERSION:
1162 strcat(string, "TLS 1.3 (experimental)");
1163 ets->tls_v13 = 1;
1164 break;
1165 #endif
1166 default:
1167 strcat(string, "Unknown version");
1168 }
1169 break;
1170 default:
1171 sprintf( string, "Handshake: Unknown SSL3 code received: %d", code );
1172 }
1173 break;
1174
1175 default:
1176 sprintf( string, "SSL message contains unknown content type: %d", content_type );
1177 }
1178
1179 /* Alert messages must always be displayed */
1180 if(content_type == SSL3_RT_ALERT)
1181 error("%s", string);
1182 else
1183 dbglog("%s", string);
1184 }
1185
1186 int
1187 ssl_new_session_cb(SSL *s, SSL_SESSION *sess)
1188 {
1189 dbglog("EAP-TLS: Post-Handshake New Session Ticket arrived:");
1190 have_session_ticket = 1;
1191
1192 /* always return success */
1193 return 1;
1194 }
1195