]> git.ozlabs.org Git - ppp.git/blob - pppd/eap-tls.c
pppd: Support for use of PKCS12 certificates (#264)
[ppp.git] / pppd / eap-tls.c
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 #include <string.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38
39 #include <openssl/conf.h>
40 #include <openssl/engine.h>
41 #include <openssl/hmac.h>
42 #include <openssl/err.h>
43 #include <openssl/ui.h>
44 #include <openssl/x509v3.h>
45 #include <openssl/pkcs12.h>
46
47 #include "pppd.h"
48 #include "eap.h"
49 #include "eap-tls.h"
50 #include "fsm.h"
51 #include "lcp.h"
52 #include "chap_ms.h"
53 #include "mppe.h"
54 #include "pathnames.h"
55
56 typedef struct pw_cb_data
57 {
58     const void *password;
59     const char *prompt_info;
60 } PW_CB_DATA;
61
62 #ifndef OPENSSL_NO_ENGINE
63 /* The openssl configuration file and engines can be loaded only once */
64 static CONF   *ssl_config  = NULL;
65 static ENGINE *cert_engine = NULL;
66 static ENGINE *pkey_engine = NULL;
67 #endif
68
69 /* TLSv1.3 do we have a session ticket ? */
70 static int have_session_ticket = 0;
71
72 int ssl_verify_callback(int, X509_STORE_CTX *);
73 void ssl_msg_callback(int write_p, int version, int ct, const void *buf,
74               size_t len, SSL * ssl, void *arg);
75 int ssl_new_session_cb(SSL *s, SSL_SESSION *sess);
76
77 X509 *get_X509_from_file(char *filename);
78 int ssl_cmp_certs(char *filename, X509 * a); 
79
80 /*
81  *  OpenSSL 1.1+ introduced a generic TLS_method()
82  *  For older releases we substitute the appropriate method
83  */
84
85 #if OPENSSL_VERSION_NUMBER < 0x10100000L
86
87 #define TLS_method SSLv23_method
88
89 #define SSL3_RT_HEADER  0x100
90
91 #ifndef SSL_CTX_set_max_proto_version
92 /** Mimics SSL_CTX_set_max_proto_version for OpenSSL < 1.1 */
93 static inline int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, long tls_ver_max)
94 {
95     long sslopt = 0;
96
97     if (tls_ver_max < TLS1_VERSION)
98     {
99         sslopt |= SSL_OP_NO_TLSv1;
100     }
101 #ifdef SSL_OP_NO_TLSv1_1
102     if (tls_ver_max < TLS1_1_VERSION)
103     {
104         sslopt |= SSL_OP_NO_TLSv1_1;
105     }
106 #endif
107 #ifdef SSL_OP_NO_TLSv1_2
108     if (tls_ver_max < TLS1_2_VERSION)
109     {
110         sslopt |= SSL_OP_NO_TLSv1_2;
111     }
112 #endif
113     SSL_CTX_set_options(ctx, sslopt);
114
115     return 1;
116 }
117 #endif /* SSL_CTX_set_max_proto_version */
118
119 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
120
121 #ifdef MPPE
122 #define EAPTLS_MPPE_KEY_LEN     32
123
124 /*
125  *  Generate keys according to RFC 2716 and add to reply
126  */
127 void eaptls_gen_mppe_keys(struct eaptls_session *ets, int client)
128 {
129     unsigned char  out[4*EAPTLS_MPPE_KEY_LEN];
130     const char    *prf_label;
131     size_t         prf_size;
132     unsigned char  eap_tls13_context[] = { EAPT_TLS };
133     unsigned char *context = NULL;
134     size_t         context_len = 0;
135     unsigned char *p;
136
137     dbglog("EAP-TLS generating MPPE keys");
138     if (ets->tls_v13)
139     {
140         prf_label = "EXPORTER_EAP_TLS_Key_Material";
141         context   = eap_tls13_context;
142         context_len = 1;
143     }
144     else
145     {
146         prf_label = "client EAP encryption";
147     }
148
149     dbglog("EAP-TLS PRF label = %s", prf_label);
150     prf_size = strlen(prf_label);
151     if (SSL_export_keying_material(ets->ssl, out, sizeof(out), prf_label, prf_size, 
152                                    context, context_len, 0) != 1)
153     {
154         warn( "EAP-TLS: Failed generating keying material" );
155         return;
156     }   
157
158     /* 
159      * We now have the master send and receive keys.
160      * From these, generate the session send and receive keys.
161      * (see RFC3079 / draft-ietf-pppext-mppe-keys-03.txt for details)
162      */
163     if (client)
164     {
165         mppe_set_keys(out, out + EAPTLS_MPPE_KEY_LEN, EAPTLS_MPPE_KEY_LEN);
166     }
167     else
168     {
169         mppe_set_keys(out + EAPTLS_MPPE_KEY_LEN, out, EAPTLS_MPPE_KEY_LEN);
170     }
171 }
172
173 #endif /* MPPE */
174
175
176 void log_ssl_errors( void )
177 {
178     unsigned long ssl_err = ERR_get_error();
179
180     if (ssl_err != 0)
181         dbglog("EAP-TLS SSL error stack:");
182     while (ssl_err != 0) {
183         dbglog( ERR_error_string( ssl_err, NULL ) );
184         ssl_err = ERR_get_error();
185     }
186 }
187
188
189 int password_callback (char *buf, int size, int rwflag, void *u)
190 {
191     if (buf)
192     {
193         strlcpy (buf, passwd, size);
194         return strlen (buf);
195     }
196     return 0;
197 }
198
199
200 CONF *eaptls_ssl_load_config( void )
201 {
202     CONF        *config;
203     int          ret_code;
204     long         error_line = 33;
205
206     config = NCONF_new( NULL );
207     dbglog( "Loading OpenSSL config file" );
208     ret_code = NCONF_load( config, _PATH_OPENSSLCONFFILE, &error_line );
209     if (ret_code == 0)
210     {
211         warn( "EAP-TLS: Error in OpenSSL config file %s at line %d", _PATH_OPENSSLCONFFILE, error_line );
212         NCONF_free( config );
213         config = NULL;
214         ERR_clear_error();
215     }
216
217     dbglog( "Loading OpenSSL built-ins" );
218 #ifndef OPENSSL_NO_ENGINE
219     ENGINE_load_builtin_engines();
220 #endif
221     OPENSSL_load_builtin_modules();
222    
223     dbglog( "Loading OpenSSL configured modules" );
224     if (CONF_modules_load( config, NULL, 0 ) <= 0 )
225     {
226         warn( "EAP-TLS: Error loading OpenSSL modules" );
227         log_ssl_errors();
228         config = NULL;
229     }
230
231     return config;
232 }
233
234 #ifndef OPENSSL_NO_ENGINE
235 ENGINE *eaptls_ssl_load_engine( char *engine_name )
236 {
237     ENGINE      *e = NULL;
238
239     dbglog( "Enabling OpenSSL auto engines" );
240     ENGINE_register_all_complete();
241
242     dbglog( "Loading OpenSSL '%s' engine support", engine_name );
243     e = ENGINE_by_id( engine_name );
244     if (!e) 
245     {
246         dbglog( "EAP-TLS: Cannot load '%s' engine support, trying 'dynamic'", engine_name );
247         e = ENGINE_by_id( "dynamic" );
248         if (e)
249         {
250             if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine_name, 0)
251              || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
252             {
253                 warn( "EAP-TLS: Error loading dynamic engine '%s'", engine_name );
254                 log_ssl_errors();
255                 ENGINE_free(e);
256                 e = NULL;
257             }
258         }
259         else
260         {
261             warn( "EAP-TLS: Cannot load dynamic engine support" );
262         }
263     }
264
265     if (e)
266     {
267         dbglog( "Initialising engine" );
268         if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
269         {
270             warn( "EAP-TLS: Cannot use that engine" );
271             log_ssl_errors();
272             ENGINE_free(e);
273             e = NULL;
274         }
275     }
276
277     return e;
278 }
279 #endif
280
281
282 /*
283  * Initialize the SSL stacks and tests if certificates, key and crl
284  * for client or server use can be loaded.
285  */
286 SSL_CTX *eaptls_init_ssl(int init_server, char *cacertfile, char *capath,
287             char *certfile, char *peer_certfile, char *privkeyfile, char *pkcs12)
288 {
289 #ifndef OPENSSL_NO_ENGINE
290     char        *cert_engine_name = NULL;
291     char        *cert_identifier = NULL;
292     char        *pkey_engine_name = NULL;
293     char        *pkey_identifier = NULL;
294 #endif
295     SSL_CTX     *ctx;
296     SSL         *ssl;
297     X509_STORE  *certstore;
298     X509_LOOKUP *lookup;
299     X509        *tmp;
300     X509        *cert = NULL;
301     PKCS12      *p12 = NULL;
302     EVP_PKEY    *pkey = NULL;
303     STACK_OF(X509) *chain = NULL;
304     BIO         *input;
305     int          ret;
306     int          reason;
307 #if defined(TLS1_2_VERSION)
308     long         tls_version = TLS1_2_VERSION; 
309 #elif defined(TLS1_1_VERSION)
310     long         tls_version = TLS1_1_VERSION; 
311 #else
312     long         tls_version = TLS1_VERSION; 
313 #endif
314
315     /*
316      * Without these can't continue 
317      */
318     if (!pkcs12[0]) 
319     {
320         if (!(cacertfile[0] || capath[0]))
321         {
322             error("EAP-TLS: CA certificate file or path missing");
323             return NULL;
324         }
325
326         if (!certfile[0])
327         {
328             error("EAP-TLS: Certificate missing");
329             return NULL;
330         }
331
332         if (!privkeyfile[0])
333         {
334             error("EAP-TLS: Private key missing");
335             return NULL;
336         }
337     }
338
339     SSL_library_init();
340     SSL_load_error_strings();
341
342 #ifndef OPENSSL_NO_ENGINE
343     /* load the openssl config file only once and load it before triggering
344        the loading of a global openssl config file via SSL_CTX_new()
345      */
346     if (!ssl_config)
347         ssl_config = eaptls_ssl_load_config();
348 #endif
349
350     ctx = SSL_CTX_new(TLS_method());
351
352     if (!ctx) {
353         error("EAP-TLS: Cannot initialize SSL CTX context");
354         goto fail;
355     }
356
357 #ifndef OPENSSL_NO_ENGINE
358     /* if the certificate filename is of the form engine:id. e.g.
359         pkcs11:12345
360        then we try to load and use this engine.
361        If the certificate filename starts with a / or . then we
362        ALWAYS assume it is a file and not an engine/pkcs11 identifier
363      */
364     if ( index( certfile, '/' ) == NULL && index( certfile, '.') == NULL )
365     {
366         cert_identifier = index( certfile, ':' );
367
368         if (cert_identifier)
369         {
370             cert_engine_name = certfile;
371             *cert_identifier = '\0';
372             cert_identifier++;
373
374             dbglog( "Found certificate engine '%s'", cert_engine_name );
375             dbglog( "Found certificate identifier '%s'", cert_identifier );
376         }
377     }
378
379     /* if the privatekey filename is of the form engine:id. e.g.
380         pkcs11:12345
381        then we try to load and use this engine.
382        If the privatekey filename starts with a / or . then we
383        ALWAYS assume it is a file and not an engine/pkcs11 identifier
384      */
385     if ( index( privkeyfile, '/' ) == NULL && index( privkeyfile, '.') == NULL )
386     {
387         pkey_identifier = index( privkeyfile, ':' );
388
389         if (pkey_identifier)
390         {
391             pkey_engine_name = privkeyfile;
392             *pkey_identifier = '\0';
393             pkey_identifier++;
394
395             dbglog( "Found privatekey engine '%s'", pkey_engine_name );
396             dbglog( "Found privatekey identifier '%s'", pkey_identifier );
397         }
398     }
399
400     if (cert_identifier && pkey_identifier)
401     {
402         if (strlen( cert_identifier ) == 0)
403         {
404             if (strlen( pkey_identifier ) == 0)
405                 error( "EAP-TLS: both the certificate and privatekey identifiers are missing!" );
406             else
407             {
408                 dbglog( "Substituting privatekey identifier for certificate identifier" );
409                 cert_identifier = pkey_identifier;
410             }
411         }
412         else
413         {
414             if (strlen( pkey_identifier ) == 0)
415             {
416                 dbglog( "Substituting certificate identifier for privatekey identifier" );
417                 pkey_identifier = cert_identifier;
418             }
419         }
420     }
421
422     if (ssl_config && cert_engine_name)
423         cert_engine = eaptls_ssl_load_engine( cert_engine_name );
424
425     if (ssl_config && pkey_engine_name)
426     {
427         /* don't load the same engine twice */
428         if ( cert_engine && strcmp( cert_engine_name, pkey_engine_name) == 0 )
429             pkey_engine = cert_engine;
430         else
431             pkey_engine = eaptls_ssl_load_engine( pkey_engine_name );
432     }
433 #endif
434
435     SSL_CTX_set_default_passwd_cb (ctx, password_callback);
436
437     if (strlen(cacertfile) == 0) cacertfile = NULL;
438     if (strlen(capath) == 0)     capath = NULL;
439
440     if (!SSL_CTX_load_verify_locations(ctx, cacertfile, capath))
441     {
442         error("EAP-TLS: Cannot load verify locations");
443         if (cacertfile) dbglog("CA certificate file = [%s]", cacertfile);
444         if (capath) dbglog("CA certificate path = [%s]", capath);
445         goto fail;
446     }
447
448     if (init_server)
449         SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(cacertfile));
450
451 #ifndef OPENSSL_NO_ENGINE
452     if (cert_engine)
453     {
454         struct
455         {
456             const char *s_slot_cert_id;
457             X509 *cert;
458         } cert_info;
459
460         cert_info.s_slot_cert_id = cert_identifier;
461         cert_info.cert = NULL;
462         
463         if (!ENGINE_ctrl_cmd( cert_engine, "LOAD_CERT_CTRL", 0, &cert_info, NULL, 0 ) )
464         {
465             error( "EAP-TLS: Error loading certificate with id '%s' from engine", cert_identifier );
466             goto fail;
467         }
468
469         if (cert_info.cert)
470         {
471             dbglog( "Got the certificate" );
472             dbglog( "subject = %s", X509_NAME_oneline( X509_get_subject_name( cert_info.cert ), NULL, 0 ) );
473             cert = cert_info.cert;
474         }
475         else
476         {
477             warn("EAP-TLS: Cannot load PKCS11 key %s", cert_identifier);
478             log_ssl_errors();
479         }
480     }
481     else
482 #endif
483     {
484         if (pkcs12[0])
485         {
486             input = BIO_new_file(pkcs12, "r");
487             if (input == NULL)
488             {
489                 error("EAP-TLS: Cannot open `%s' PKCS12 for input", pkcs12);
490                 goto fail;
491             }
492
493             p12 = d2i_PKCS12_bio(input, NULL);
494             BIO_free(input);
495             if (!p12)
496             {
497                 error("EAP-TLS: Cannot load PKCS12 certificate");
498                 goto fail;
499             }
500
501             if (PKCS12_parse(p12, passwd, &pkey, &cert, &chain) != 1)
502             {
503                 error("EAP-TLS: Cannot parse PKCS12 certificate, invalid password");
504                 PKCS12_free(p12);
505                 goto fail;
506             }
507
508             PKCS12_free(p12);
509         }
510         else 
511         {
512             if (!SSL_CTX_use_certificate_chain_file(ctx, certfile))
513             {
514                 error( "EAP-TLS: Cannot load certificate %s", certfile );
515                 goto fail;
516             }
517         }
518     }
519
520     if (cert)
521     {
522         if (!SSL_CTX_use_certificate(ctx, cert))
523         {
524             error("EAP-TLS: Cannot use load certificate");
525             goto fail;
526         }
527
528         if (chain)
529         {
530             int i;
531             for (i = 0; i < sk_X509_num(chain); i++)
532             {
533                 if (!SSL_CTX_add_extra_chain_cert(ctx, sk_X509_value(chain, i)))
534                 {
535                     error("EAP-TLS: Cannot add extra chain certificate");
536                     goto fail;
537                 }
538             }
539         }
540     }
541
542     /*
543      *  Check the Before and After dates of the certificate
544      */
545     ssl = SSL_new(ctx);
546     tmp = SSL_get_certificate(ssl);
547
548     ret = X509_cmp_time(X509_get_notBefore(tmp), NULL);
549     if (ret == 0)
550     {    
551         warn( "EAP-TLS: Failed to read certificate notBefore field.");
552     }    
553     if (ret > 0) 
554     {    
555         warn( "EAP-TLS: Your certificate is not yet valid!");
556     }    
557
558     ret = X509_cmp_time(X509_get_notAfter(tmp), NULL);
559     if (ret == 0)
560     {    
561         warn( "EAP-TLS: Failed to read certificate notAfter field.");
562     }    
563     if (ret < 0)
564     {
565         warn( "EAP-TLS: Your certificate has expired!");
566     }
567     SSL_free(ssl);
568
569 #ifndef OPENSSL_NO_ENGINE
570     if (pkey_engine)
571     {
572         PW_CB_DATA  cb_data;
573
574         cb_data.password = passwd;
575         cb_data.prompt_info = pkey_identifier;
576
577         if (passwd[0] != 0)
578         {
579             UI_METHOD* transfer_pin = UI_create_method("transfer_pin");
580
581             int writer (UI *ui, UI_STRING *uis)
582             {
583                 PW_CB_DATA* cb_data = (PW_CB_DATA*)UI_get0_user_data(ui);
584                 UI_set_result(ui, uis, cb_data->password);
585                 return 1;
586             };
587             int stub (UI* ui) {return 1;};
588             int stub_reader (UI *ui, UI_STRING *uis) {return 1;};
589
590             UI_method_set_writer(transfer_pin,  writer);
591             UI_method_set_opener(transfer_pin,  stub);
592             UI_method_set_closer(transfer_pin,  stub);
593             UI_method_set_flusher(transfer_pin, stub);
594             UI_method_set_reader(transfer_pin,  stub_reader);
595
596             dbglog( "Using our private key '%s' in engine", pkey_identifier );
597             pkey = ENGINE_load_private_key(pkey_engine, pkey_identifier, transfer_pin, &cb_data);
598
599             if (transfer_pin) UI_destroy_method(transfer_pin);
600         }
601         else {
602             dbglog( "Loading private key '%s' from engine", pkey_identifier );
603             pkey = ENGINE_load_private_key(pkey_engine, pkey_identifier, NULL, NULL);
604         }
605     }
606     else 
607 #endif
608     {
609         if (!pkey)
610         {
611             input = BIO_new_file(privkeyfile, "r");
612             if (!input)
613             {
614                 error("EAP-TLS: Could not open private key, %s", privkeyfile);
615                 goto fail;
616             }
617
618             pkey = PEM_read_bio_PrivateKey(input, NULL, password_callback, NULL);
619             BIO_free(input);
620             if (!pkey)
621             {
622                 error("EAP-TLS: Cannot load private key, %s", privkeyfile);
623                 goto fail;
624             }
625         }
626     }
627
628     if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1)
629     {
630         error("EAP-TLS: Cannot use private key");
631         goto fail;
632     }
633
634     if (SSL_CTX_check_private_key(ctx) != 1)
635     {
636         error("EAP-TLS: Private key fails security check");
637         goto fail;
638     }
639
640     /* Explicitly set the NO_TICKETS flag to support Win7/Win8 clients */
641     SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3
642 #ifdef SSL_OP_NO_TICKET
643     | SSL_OP_NO_TICKET
644 #endif
645     );
646
647     /* OpenSSL 1.1.1+ does not include RC4 ciphers by default.
648      * This causes totally obsolete WinXP clients to fail. If you really
649      * need ppp+EAP-TLS+openssl 1.1.1+WinXP then enable RC4 cipers and
650      * make sure that you use an OpenSSL that supports them
651
652     SSL_CTX_set_cipher_list(ctx, "RC4");
653      */
654
655
656     /* Set up a SSL Session cache with a callback. This is needed for TLSv1.3+.
657      * During the initial handshake the server signals to the client early on
658      * that the handshake is finished, even before the client has sent its
659      * credentials to the server. The actual connection (and moment that the
660      * client sends its credentials) only starts after the arrival of the first
661      * session ticket. The 'ssl_new_session_cb' catches this ticket.
662      */
663     SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE);
664     SSL_CTX_sess_set_new_cb(ctx, ssl_new_session_cb);
665
666     /* As EAP-TLS+TLSv1.3 is highly experimental we offer the user a chance to override */
667     if (max_tls_version)
668     {
669         if (strncmp(max_tls_version, "1.0", 3) == 0)
670             tls_version = TLS1_VERSION;
671         else if (strncmp(max_tls_version, "1.1", 3) == 0)
672             tls_version = TLS1_1_VERSION;
673         else if (strncmp(max_tls_version, "1.2", 3) == 0)
674 #ifdef TLS1_2_VERSION
675             tls_version = TLS1_2_VERSION;
676 #else
677         {
678             warn("TLSv1.2 not available. Defaulting to TLSv1.1");
679             tls_version = TLS_1_1_VERSION;
680         }
681 #endif
682         else if (strncmp(max_tls_version, "1.3", 3) == 0)
683 #ifdef TLS1_3_VERSION
684             tls_version = TLS1_3_VERSION;
685 #else
686             warn("TLSv1.3 not available.");
687 #endif
688     }
689
690     dbglog("EAP-TLS: Setting max protocol version to 0x%X", tls_version);
691     SSL_CTX_set_max_proto_version(ctx, tls_version);
692
693     SSL_CTX_set_verify_depth(ctx, 5);
694     SSL_CTX_set_verify(ctx,
695                SSL_VERIFY_PEER |
696                SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
697                &ssl_verify_callback);
698
699     if (crl_dir) {
700         if (!(certstore = SSL_CTX_get_cert_store(ctx))) {
701             error("EAP-TLS: Failed to get certificate store");
702             goto fail;
703         }
704
705         if (!(lookup =
706              X509_STORE_add_lookup(certstore, X509_LOOKUP_hash_dir()))) {
707             error("EAP-TLS: Store lookup for CRL failed");
708
709             goto fail;
710         }
711
712         X509_LOOKUP_add_dir(lookup, crl_dir, X509_FILETYPE_PEM);
713         X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK);
714     }
715
716     if (crl_file) {
717         FILE     *fp  = NULL;
718         X509_CRL *crl = NULL;
719
720         fp = fopen(crl_file, "r");
721         if (!fp) {
722             error("EAP-TLS: Cannot open CRL file '%s'", crl_file);
723             goto fail;
724         }
725
726         crl = PEM_read_X509_CRL(fp, NULL, NULL, NULL);
727         if (!crl) {
728             error("EAP-TLS: Cannot read CRL file '%s'", crl_file);
729             goto fail;
730         }
731
732         if (!(certstore = SSL_CTX_get_cert_store(ctx))) {
733             error("EAP-TLS: Failed to get certificate store");
734             goto fail;
735         }
736         if (!X509_STORE_add_crl(certstore, crl)) {
737             error("EAP-TLS: Cannot add CRL to certificate store");
738             goto fail;
739         }
740         X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK);
741
742     }
743
744     /*
745      * If a peer certificate file was specified, it must be valid, else fail 
746      */
747     if (peer_certfile[0]) {
748         if (!(tmp = get_X509_from_file(peer_certfile))) {
749             error("EAP-TLS: Error loading client certificate from file %s",
750                  peer_certfile);
751             goto fail;
752         }
753         X509_free(tmp);
754     }
755
756     return ctx;
757
758 fail:
759
760     if (cert)
761         X509_free(cert);
762
763     if (pkey)
764         EVP_PKEY_free(pkey);
765
766     if (chain)
767         sk_X509_pop_free(chain, X509_free);
768
769     log_ssl_errors();
770     SSL_CTX_free(ctx);
771     return NULL;
772 }
773
774 /*
775  * Determine the maximum packet size by looking at the LCP handshake
776  */
777
778 int eaptls_get_mtu(int unit)
779 {
780     int mtu, mru;
781
782     lcp_options *wo = &lcp_wantoptions[unit];
783     lcp_options *go = &lcp_gotoptions[unit];
784     lcp_options *ho = &lcp_hisoptions[unit];
785     lcp_options *ao = &lcp_allowoptions[unit];
786
787     mtu = ho->neg_mru? ho->mru: PPP_MRU;
788     mru = go->neg_mru? MAX(wo->mru, go->mru): PPP_MRU;
789     mtu = MIN(MIN(mtu, mru), ao->mru)- PPP_HDRLEN - 10;
790
791     dbglog("MTU = %d", mtu);
792     return mtu;
793 }
794
795
796 /*
797  * Init the ssl handshake (server mode)
798  */
799 int eaptls_init_ssl_server(eap_state * esp)
800 {
801     struct eaptls_session *ets;
802     char servcertfile[MAXWORDLEN];
803     char clicertfile[MAXWORDLEN];
804     char cacertfile[MAXWORDLEN];
805     char capath[MAXWORDLEN];
806     char pkfile[MAXWORDLEN];
807     char pkcs12[MAXWORDLEN];
808
809     /*
810      * Allocate new eaptls session 
811      */
812     esp->es_server.ea_session = malloc(sizeof(struct eaptls_session));
813     if (!esp->es_server.ea_session)
814         fatal("Allocation error");
815     ets = esp->es_server.ea_session;
816     ets->client = 0;
817
818     if (!esp->es_server.ea_peer) {
819         error("EAP-TLS: Error: client name not set (BUG)");
820         return 0;
821     }
822
823     strlcpy(ets->peer, esp->es_server.ea_peer, MAXWORDLEN-1);
824
825     dbglog( "getting eaptls secret" );
826     if (!get_eaptls_secret(esp->es_unit, esp->es_server.ea_peer,
827                    esp->es_server.ea_name, clicertfile,
828                    servcertfile, cacertfile, capath, pkfile, pkcs12, 1)) {
829         error( "EAP-TLS: Cannot get secret/password for client \"%s\", server \"%s\"",
830                 esp->es_server.ea_peer, esp->es_server.ea_name );
831         return 0;
832     }
833
834     ets->mtu = eaptls_get_mtu(esp->es_unit);
835
836     ets->ctx = eaptls_init_ssl(1, cacertfile, capath, servcertfile, clicertfile, pkfile, pkcs12);
837     if (!ets->ctx)
838         goto fail;
839
840     if (!(ets->ssl = SSL_new(ets->ctx)))
841         goto fail;
842
843     /*
844      * Set auto-retry to avoid timeouts on BIO_read
845      */
846     SSL_set_mode(ets->ssl, SSL_MODE_AUTO_RETRY);
847
848     /*
849      * Initialize the BIOs we use to read/write to ssl engine 
850      */
851     ets->into_ssl = BIO_new(BIO_s_mem());
852     ets->from_ssl = BIO_new(BIO_s_mem());
853     SSL_set_bio(ets->ssl, ets->into_ssl, ets->from_ssl);
854
855     SSL_set_msg_callback(ets->ssl, ssl_msg_callback);
856     SSL_set_msg_callback_arg(ets->ssl, ets);
857
858     /*
859      * Attach the session struct to the connection, so we can later
860      * retrieve it when doing certificate verification
861      */
862     SSL_set_ex_data(ets->ssl, 0, ets);
863
864     SSL_set_accept_state(ets->ssl);
865
866     ets->tls_v13 = 0;
867
868     ets->data = NULL;
869     ets->datalen = 0;
870     ets->alert_sent = 0;
871     ets->alert_recv = 0;
872
873     /*
874      * If we specified the client certificate file, store it in ets->peercertfile,
875      * so we can check it later in ssl_verify_callback()
876      */
877     if (clicertfile[0])
878         strlcpy(&ets->peercertfile[0], clicertfile, MAXWORDLEN);
879     else
880         ets->peercertfile[0] = 0;
881
882     return 1;
883
884 fail:
885     SSL_CTX_free(ets->ctx);
886     return 0;
887 }
888
889 /*
890  * Init the ssl handshake (client mode)
891  */
892 int eaptls_init_ssl_client(eap_state * esp)
893 {
894     struct eaptls_session *ets;
895     char servcertfile[MAXWORDLEN];
896     char clicertfile[MAXWORDLEN];
897     char cacertfile[MAXWORDLEN];
898     char capath[MAXWORDLEN];
899     char pkfile[MAXWORDLEN];
900     char pkcs12[MAXWORDLEN];
901
902     /*
903      * Allocate new eaptls session 
904      */
905     esp->es_client.ea_session = malloc(sizeof(struct eaptls_session));
906     if (!esp->es_client.ea_session)
907         fatal("Allocation error");
908     ets = esp->es_client.ea_session;
909     ets->client = 1;
910
911     /*
912      * If available, copy server name in ets; it will be used in cert
913      * verify 
914      */
915     if (esp->es_client.ea_peer)
916         strlcpy(ets->peer, esp->es_client.ea_peer, MAXWORDLEN-1);
917     else
918         ets->peer[0] = 0;
919     
920     ets->mtu = eaptls_get_mtu(esp->es_unit);
921
922     dbglog( "calling get_eaptls_secret" );
923     if (!get_eaptls_secret(esp->es_unit, esp->es_client.ea_name,
924                    ets->peer, clicertfile,
925                    servcertfile, cacertfile, capath, pkfile, pkcs12, 0)) {
926         error( "EAP-TLS: Cannot get secret/password for client \"%s\", server \"%s\"",
927                 esp->es_client.ea_name, ets->peer );
928         return 0;
929     }
930
931     dbglog( "calling eaptls_init_ssl" );
932     ets->ctx = eaptls_init_ssl(0, cacertfile, capath, clicertfile, servcertfile, pkfile, pkcs12);
933     if (!ets->ctx)
934         goto fail;
935
936     ets->ssl = SSL_new(ets->ctx);
937
938     if (!ets->ssl)
939         goto fail;
940
941     /*
942      * Initialize the BIOs we use to read/write to ssl engine 
943      */
944     dbglog( "Initializing SSL BIOs" );
945     ets->into_ssl = BIO_new(BIO_s_mem());
946     ets->from_ssl = BIO_new(BIO_s_mem());
947     SSL_set_bio(ets->ssl, ets->into_ssl, ets->from_ssl);
948
949     SSL_set_msg_callback(ets->ssl, ssl_msg_callback);
950     SSL_set_msg_callback_arg(ets->ssl, ets);
951
952     /*
953      * Attach the session struct to the connection, so we can later
954      * retrieve it when doing certificate verification
955      */
956     SSL_set_ex_data(ets->ssl, 0, ets);
957
958     SSL_set_connect_state(ets->ssl);
959
960     ets->tls_v13 = 0;
961
962     ets->data = NULL;
963     ets->datalen = 0;
964     ets->alert_sent = 0;
965     ets->alert_recv = 0;
966
967     /*
968      * If we specified the server certificate file, store it in
969      * ets->peercertfile, so we can check it later in
970      * ssl_verify_callback() 
971      */
972     if (servcertfile[0])
973         strlcpy(ets->peercertfile, servcertfile, MAXWORDLEN);
974     else
975         ets->peercertfile[0] = 0;
976
977     return 1;
978
979 fail:
980     dbglog( "eaptls_init_ssl_client: fail" );
981     SSL_CTX_free(ets->ctx);
982     return 0;
983
984 }
985
986 void eaptls_free_session(struct eaptls_session *ets)
987 {
988     if (ets->ssl)
989         SSL_free(ets->ssl);
990
991     if (ets->ctx)
992         SSL_CTX_free(ets->ctx);
993
994     free(ets);
995 }
996
997
998 int eaptls_is_init_finished(struct eaptls_session *ets)
999 {
1000     if (ets->ssl && SSL_is_init_finished(ets->ssl))
1001     {
1002         if (ets->tls_v13) 
1003             return have_session_ticket;
1004         else
1005             return 1;
1006     }
1007
1008     return 0;
1009 }
1010
1011 /*
1012  * Handle a received packet, reassembling fragmented messages and
1013  * passing them to the ssl engine
1014  */
1015 int eaptls_receive(struct eaptls_session *ets, u_char * inp, int len)
1016 {
1017     u_char flags;
1018     u_int tlslen = 0;
1019     u_char dummy[65536];
1020
1021     if (len < 1) {
1022         warn("EAP-TLS: received no or invalid data");
1023         return 1;
1024     }
1025         
1026     GETCHAR(flags, inp);
1027     len--;
1028
1029     if (flags & EAP_TLS_FLAGS_LI && len > 4) {
1030         /*
1031          * LenghtIncluded flag set -> this is the first packet of a message
1032         */
1033
1034         /*
1035          * the first 4 octets are the length of the EAP-TLS message
1036          */
1037         GETLONG(tlslen, inp);
1038         len -= 4;
1039
1040         if (!ets->data) {
1041
1042             if (tlslen > EAP_TLS_MAX_LEN) {
1043                 error("EAP-TLS: TLS message length > %d, truncated", EAP_TLS_MAX_LEN);
1044                 tlslen = EAP_TLS_MAX_LEN;
1045             }
1046
1047             /*
1048              * Allocate memory for the whole message
1049             */
1050             ets->data = malloc(tlslen);
1051             if (!ets->data)
1052                 fatal("EAP-TLS: allocation error\n");
1053
1054             ets->datalen = 0;
1055             ets->tlslen = tlslen;
1056         }
1057         else
1058             warn("EAP-TLS: non-first LI packet? that's odd...");
1059     }
1060     else if (!ets->data) {
1061         /*
1062          * A non fragmented message without LI flag
1063         */
1064  
1065         ets->data = malloc(len);
1066         if (!ets->data)
1067             fatal("EAP-TLS: memory allocation error in eaptls_receive\n");
1068  
1069         ets->datalen = 0;
1070         ets->tlslen = len;
1071     }
1072
1073     if (flags & EAP_TLS_FLAGS_MF)
1074         ets->frag = 1;
1075     else
1076         ets->frag = 0;
1077
1078     if (len < 0) {
1079         warn("EAP-TLS: received malformed data");
1080         return 1;
1081     }
1082
1083     if (len + ets->datalen > ets->tlslen) {
1084         warn("EAP-TLS: received data > TLS message length");
1085         return 1;
1086     }
1087
1088     BCOPY(inp, ets->data + ets->datalen, len);
1089     ets->datalen += len;
1090
1091     if (!ets->frag) {
1092
1093         /*
1094          * If we have the whole message, pass it to ssl 
1095          */
1096
1097         if (ets->datalen != ets->tlslen) {
1098             warn("EAP-TLS: received data != TLS message length");
1099             return 1;
1100         }
1101
1102         if (BIO_write(ets->into_ssl, ets->data, ets->datalen) == -1)
1103             log_ssl_errors();
1104
1105         SSL_read(ets->ssl, dummy, 65536);
1106
1107         free(ets->data);
1108         ets->data = NULL;
1109         ets->datalen = 0;
1110     }
1111
1112     return 0;
1113 }
1114
1115 /*
1116  * Return an eap-tls packet in outp.
1117  * A TLS message read from the ssl engine is buffered in ets->data.
1118  * At each call we control if there is buffered data and send a 
1119  * packet of mtu bytes.
1120  */
1121 int eaptls_send(struct eaptls_session *ets, u_char ** outp)
1122 {
1123     bool first = 0;
1124     int size;
1125     u_char fromtls[65536];
1126     int res;
1127     u_char *start;
1128
1129     start = *outp;
1130
1131     if (!ets->data)
1132     {
1133         if(!ets->alert_sent)
1134         {
1135             res = SSL_read(ets->ssl, fromtls, 65536);
1136         }
1137
1138         /*
1139          * Read from ssl 
1140          */
1141         if ((res = BIO_read(ets->from_ssl, fromtls, 65536)) == -1)
1142         {
1143             warn("EAP-TLS send: No data from BIO_read");
1144             return 1;
1145         }
1146
1147         ets->datalen = res;
1148
1149         ets->data = malloc(ets->datalen);
1150         if (!ets->data)
1151             fatal("EAP-TLS: memory allocation error in eaptls_send\n");
1152
1153         BCOPY(fromtls, ets->data, ets->datalen);
1154
1155         ets->offset = 0;
1156         first = 1;
1157     }
1158
1159     size = ets->datalen - ets->offset;
1160     
1161     if (size > ets->mtu) {
1162         size = ets->mtu;
1163         ets->frag = 1;
1164     } else
1165         ets->frag = 0;
1166
1167     PUTCHAR(EAPT_TLS, *outp);
1168
1169     /*
1170      * Set right flags and length if necessary 
1171      */
1172     if (ets->frag && first) {
1173         PUTCHAR(EAP_TLS_FLAGS_LI | EAP_TLS_FLAGS_MF, *outp);
1174         PUTLONG(ets->datalen, *outp);
1175     } else if (ets->frag) {
1176         PUTCHAR(EAP_TLS_FLAGS_MF, *outp);
1177     } else
1178         PUTCHAR(0, *outp);
1179
1180     /*
1181      * Copy the data in outp 
1182      */
1183     BCOPY(ets->data + ets->offset, *outp, size);
1184     INCPTR(size, *outp);
1185
1186     /*
1187      * Copy the packet in retransmission buffer 
1188      */
1189     BCOPY(start, &ets->rtx[0], *outp - start);
1190     ets->rtx_len = *outp - start;
1191
1192     ets->offset += size;
1193
1194     if (ets->offset >= ets->datalen) {
1195
1196         /*
1197          * The whole message has been sent 
1198          */
1199
1200         free(ets->data);
1201         ets->data = NULL;
1202         ets->datalen = 0;
1203         ets->offset = 0;
1204     }
1205
1206     return 0;
1207 }
1208
1209 /*
1210  * Get the sent packet from the retransmission buffer
1211  */
1212 void eaptls_retransmit(struct eaptls_session *ets, u_char ** outp)
1213 {
1214     BCOPY(ets->rtx, *outp, ets->rtx_len);
1215     INCPTR(ets->rtx_len, *outp);
1216 }
1217
1218 /*
1219  * Verify a certificate.
1220  * Most of the work (signatures and issuer attributes checking)
1221  * is done by ssl; we check the CN in the peer certificate 
1222  * against the peer name.
1223  */
1224 int ssl_verify_callback(int ok, X509_STORE_CTX * ctx)
1225 {
1226     char subject[256];
1227     char cn_str[256];
1228     X509 *peer_cert;
1229     int err, depth;
1230     SSL *ssl;
1231     struct eaptls_session *ets;
1232     char *ptr1 = NULL, *ptr2 = NULL;
1233
1234     peer_cert = X509_STORE_CTX_get_current_cert(ctx);
1235     err = X509_STORE_CTX_get_error(ctx);
1236     depth = X509_STORE_CTX_get_error_depth(ctx);
1237
1238     dbglog("certificate verify depth: %d", depth);
1239
1240     if (auth_required && !ok) {
1241         X509_NAME_oneline(X509_get_subject_name(peer_cert),
1242                   subject, 256);
1243
1244         X509_NAME_get_text_by_NID(X509_get_subject_name(peer_cert),
1245                       NID_commonName, cn_str, 256);
1246
1247         dbglog("Certificate verification error:\n depth: %d CN: %s"
1248                "\n err: %d (%s)\n", depth, cn_str, err,
1249                X509_verify_cert_error_string(err));
1250
1251         return 0;
1252     }
1253
1254     ssl = X509_STORE_CTX_get_ex_data(ctx,
1255                        SSL_get_ex_data_X509_STORE_CTX_idx());
1256
1257     ets = (struct eaptls_session *)SSL_get_ex_data(ssl, 0);
1258
1259     if (ets == NULL) {
1260         error("Error: SSL_get_ex_data returned NULL");
1261         return 0;
1262     }
1263
1264     log_ssl_errors();
1265
1266     if (!depth) 
1267     {
1268         /* Verify certificate based on certificate type and extended key usage */
1269         if (tls_verify_key_usage) {
1270             int purpose = ets->client ? X509_PURPOSE_SSL_SERVER : X509_PURPOSE_SSL_CLIENT ;
1271             if (X509_check_purpose(peer_cert, purpose, 0) == 0) {
1272                 error("Certificate verification error: nsCertType mismatch");
1273                 return 0;
1274             }
1275
1276 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
1277             int flags = ets->client ? XKU_SSL_SERVER : XKU_SSL_CLIENT;
1278             if (!(X509_get_extended_key_usage(peer_cert) & flags)) {
1279                 error("Certificate verification error: invalid extended key usage");
1280                 return 0;
1281             }
1282 #endif
1283             info("Certificate key usage: OK");
1284         }
1285
1286         /*
1287          * If acting as client and the name of the server wasn't specified
1288          * explicitely, we can't verify the server authenticity 
1289          */
1290         if (!tls_verify_method)
1291             tls_verify_method = TLS_VERIFY_NONE;
1292
1293         if (!ets->peer[0] || !strcmp(TLS_VERIFY_NONE, tls_verify_method)) {
1294             warn("Certificate verication disabled or no peer name was specified");
1295             return ok;
1296         }
1297
1298         /* This is the peer certificate */
1299         X509_NAME_oneline(X509_get_subject_name(peer_cert),
1300                   subject, 256);
1301
1302         X509_NAME_get_text_by_NID(X509_get_subject_name(peer_cert),
1303                       NID_commonName, cn_str, 256);
1304
1305         /* Verify based on subject name */
1306         ptr1 = ets->peer;
1307         if (!strcmp(TLS_VERIFY_SUBJECT, tls_verify_method)) {
1308             ptr2 = subject;
1309         }
1310
1311         /* Verify based on common name (default) */
1312         if (strlen(tls_verify_method) == 0 ||
1313             !strcmp(TLS_VERIFY_NAME, tls_verify_method)) {
1314             ptr2 = cn_str;
1315         }
1316
1317         /* Match the suffix of common name */
1318         if (!strcmp(TLS_VERIFY_SUFFIX, tls_verify_method)) {
1319             int len = strlen(ptr1);
1320             int off = strlen(cn_str) - len;
1321             ptr2 = cn_str;
1322             if (off > 0) {
1323                 ptr2 = cn_str + off;
1324             }
1325         }
1326
1327         if (strcmp(ptr1, ptr2)) {
1328             error("Certificate verification error: CN (%s) != %s", ptr1, ptr2);
1329             return 0;
1330         }
1331
1332         info("Certificate CN: %s, peer name %s", cn_str, ets->peer);
1333
1334         /*
1335          * If a peer certificate file was specified, here we check it 
1336          */
1337         if (ets->peercertfile[0]) {
1338             if (ssl_cmp_certs(&ets->peercertfile[0], peer_cert)
1339                 != 0) {
1340                 error
1341                     ("Peer certificate doesn't match stored certificate");
1342                 return 0;
1343             }
1344         }
1345     }
1346
1347     return ok;
1348 }
1349
1350 /*
1351  * Compare a certificate with the one stored in a file
1352  */
1353 int ssl_cmp_certs(char *filename, X509 * a)
1354 {
1355     X509 *b;
1356     int ret;
1357
1358     if (!(b = get_X509_from_file(filename)))
1359         return 1;
1360
1361     ret = X509_cmp(a, b);
1362     X509_free(b);
1363
1364     return ret;
1365
1366 }
1367
1368 X509 *get_X509_from_file(char *filename)
1369 {
1370     FILE *fp;
1371     X509 *ret;
1372
1373     if (!(fp = fopen(filename, "r")))
1374         return NULL;
1375
1376     ret = PEM_read_X509(fp, NULL, NULL, NULL);
1377
1378     fclose(fp);
1379
1380     return ret;
1381 }
1382
1383 /*
1384  * Every sent & received message this callback function is invoked,
1385  * so we know when alert messages have arrived or are sent and
1386  * we can print debug information about TLS handshake.
1387  */
1388 void
1389 ssl_msg_callback(int write_p, int version, int content_type,
1390          const void *buf, size_t len, SSL * ssl, void *arg)
1391 {
1392     char string[256];
1393     struct eaptls_session *ets = (struct eaptls_session *)arg;
1394     unsigned char code;
1395     const unsigned char*msg = buf;
1396     int hvers = msg[1] << 8 | msg[2];
1397
1398     if(write_p)
1399         strcpy(string, " -> ");
1400     else
1401         strcpy(string, " <- ");
1402
1403     switch(content_type) {
1404
1405     case SSL3_RT_HEADER:
1406         strcat(string, "SSL/TLS Header: ");
1407         switch(hvers) {
1408         case SSL3_VERSION:
1409                 strcat(string, "SSL 3.0");
1410                 break;
1411         case TLS1_VERSION:
1412                 strcat(string, "TLS 1.0");
1413                 break;
1414         case TLS1_1_VERSION:
1415                 strcat(string, "TLS 1.1");
1416                 break;
1417         case TLS1_2_VERSION:
1418                 strcat(string, "TLS 1.2");
1419                 break;
1420         default:
1421             sprintf(string, "SSL/TLS Header: Unknown version (%d)", hvers);
1422         }
1423         break;
1424
1425     case SSL3_RT_ALERT:
1426         strcat(string, "Alert: ");
1427         code = msg[1];
1428
1429         if (write_p) {
1430             ets->alert_sent = 1;
1431             ets->alert_sent_desc = code;
1432         } else {
1433             ets->alert_recv = 1;
1434             ets->alert_recv_desc = code;
1435         }
1436
1437         strcat(string, SSL_alert_desc_string_long(code));
1438         break;
1439
1440     case SSL3_RT_CHANGE_CIPHER_SPEC:
1441         strcat(string, "ChangeCipherSpec");
1442         break;
1443
1444 #ifdef SSL3_RT_INNER_CONTENT_TYPE
1445     case SSL3_RT_INNER_CONTENT_TYPE:
1446         strcat(string, "InnerContentType (TLS1.3)");
1447         break;
1448 #endif
1449
1450     case SSL3_RT_HANDSHAKE:
1451
1452         strcat(string, "Handshake: ");
1453         code = msg[0];
1454
1455         switch(code) {
1456             case SSL3_MT_HELLO_REQUEST:
1457                 strcat(string,"Hello Request");
1458                 break;
1459             case SSL3_MT_CLIENT_HELLO:
1460                 strcat(string,"Client Hello");
1461                 break;
1462             case SSL3_MT_SERVER_HELLO:
1463                 strcat(string,"Server Hello");
1464                 break;
1465 #ifdef SSL3_MT_NEWSESSION_TICKET
1466             case SSL3_MT_NEWSESSION_TICKET:
1467                 strcat(string,"New Session Ticket");
1468                 break;
1469 #endif
1470 #ifdef SSL3_MT_END_OF_EARLY_DATA
1471             case SSL3_MT_END_OF_EARLY_DATA:
1472                 strcat(string,"End of Early Data");
1473                 break;
1474 #endif
1475 #ifdef SSL3_MT_ENCRYPTED_EXTENSIONS
1476             case SSL3_MT_ENCRYPTED_EXTENSIONS:
1477                 strcat(string,"Encryped Extensions");
1478                 break;
1479 #endif
1480             case SSL3_MT_CERTIFICATE:
1481                 strcat(string,"Certificate");
1482                 break;
1483             case SSL3_MT_SERVER_KEY_EXCHANGE:
1484                 strcat(string,"Server Key Exchange");
1485                 break;
1486             case SSL3_MT_CERTIFICATE_REQUEST:
1487                 strcat(string,"Certificate Request");
1488                 break;
1489             case SSL3_MT_SERVER_DONE:
1490                 strcat(string,"Server Hello Done");
1491                 break;
1492             case SSL3_MT_CERTIFICATE_VERIFY:
1493                 strcat(string,"Certificate Verify");
1494                 break;
1495             case SSL3_MT_CLIENT_KEY_EXCHANGE:
1496                 strcat(string,"Client Key Exchange");
1497                 break;
1498             case SSL3_MT_FINISHED:
1499                 strcat(string,"Finished: ");
1500                 hvers = SSL_version(ssl);
1501                 switch(hvers){
1502                     case SSL3_VERSION:
1503                         strcat(string, "SSL 3.0");
1504                         break;
1505                     case TLS1_VERSION:
1506                         strcat(string, "TLS 1.0");
1507                         break;
1508                     case TLS1_1_VERSION:
1509                         strcat(string, "TLS 1.1");
1510                         break;
1511                     case TLS1_2_VERSION:
1512                         strcat(string, "TLS 1.2");
1513                         break;
1514 #ifdef TLS1_3_VERSION
1515                     case TLS1_3_VERSION:
1516                         strcat(string, "TLS 1.3 (experimental)");
1517                         ets->tls_v13 = 1;
1518                         break;
1519 #endif
1520                     default:
1521                         strcat(string, "Unknown version");
1522                 }
1523                 break;
1524             default:
1525                 sprintf( string, "Handshake: Unknown SSL3 code received: %d", code );
1526         }
1527         break;
1528
1529     default:
1530         sprintf( string, "SSL message contains unknown content type: %d", content_type );
1531     }
1532
1533     /* Alert messages must always be displayed */
1534     if(content_type == SSL3_RT_ALERT)
1535         error("%s", string);
1536     else
1537         dbglog("%s", string);
1538 }
1539
1540 int 
1541 ssl_new_session_cb(SSL *s, SSL_SESSION *sess)
1542 {
1543     dbglog("EAP-TLS: Post-Handshake New Session Ticket arrived:");
1544     have_session_ticket = 1;
1545
1546     /* always return success */
1547     return 1;
1548 }
1549