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