2 * SSL/TLS interface functions for OpenSSL
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #ifndef CONFIG_SMARTCARD
12 #ifndef OPENSSL_NO_ENGINE
14 #define OPENSSL_NO_ENGINE
19 #include <openssl/ssl.h>
20 #include <openssl/err.h>
21 #include <openssl/pkcs12.h>
22 #include <openssl/x509v3.h>
23 #ifndef OPENSSL_NO_ENGINE
24 #include <openssl/engine.h>
25 #endif /* OPENSSL_NO_ENGINE */
26 #ifndef OPENSSL_NO_DSA
27 #include <openssl/dsa.h>
30 #include <openssl/dh.h>
39 #if OPENSSL_VERSION_NUMBER < 0x10000000L
40 /* ERR_remove_thread_state replaces ERR_remove_state and the latter is
41 * deprecated. However, OpenSSL 0.9.8 doesn't include
42 * ERR_remove_thread_state. */
43 #define ERR_remove_thread_state(tid) ERR_remove_state(0)
46 #if defined(OPENSSL_IS_BORINGSSL)
47 /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
48 typedef size_t stack_index_t
;
50 typedef int stack_index_t
;
53 #ifdef SSL_set_tlsext_status_type
54 #ifndef OPENSSL_NO_TLSEXT
56 #include <openssl/ocsp.h>
57 #endif /* OPENSSL_NO_TLSEXT */
58 #endif /* SSL_set_tlsext_status_type */
61 #include <openssl/pem.h>
62 #include <keystore/keystore_get.h>
64 static BIO
* BIO_from_keystore(const char *key
)
67 uint8_t *value
= NULL
;
68 int length
= keystore_get(key
, strlen(key
), &value
);
69 if (length
!= -1 && (bio
= BIO_new(BIO_s_mem())) != NULL
)
70 BIO_write(bio
, value
, length
);
76 static int tls_openssl_ref_count
= 0;
79 void (*event_cb
)(void *ctx
, enum tls_event ev
,
80 union tls_event_data
*data
);
83 char *ocsp_stapling_response
;
86 static struct tls_context
*tls_global
= NULL
;
89 struct tls_connection
{
90 struct tls_context
*context
;
93 BIO
*ssl_in
, *ssl_out
;
94 #ifndef OPENSSL_NO_ENGINE
95 ENGINE
*engine
; /* functional reference to the engine */
96 EVP_PKEY
*private_key
; /* the private key if using engine */
97 #endif /* OPENSSL_NO_ENGINE */
98 char *subject_match
, *altsubject_match
, *suffix_match
, *domain_match
;
99 int read_alerts
, write_alerts
, failed
;
101 tls_session_ticket_cb session_ticket_cb
;
102 void *session_ticket_cb_ctx
;
104 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
106 size_t session_ticket_len
;
108 unsigned int ca_cert_verify
:1;
109 unsigned int cert_probe
:1;
110 unsigned int server_cert_only
:1;
111 unsigned int invalid_hb_used
:1;
113 u8 srv_cert_hash
[32];
119 X509
*peer_issuer_issuer
;
121 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
122 unsigned char client_random
[SSL3_RANDOM_SIZE
];
123 unsigned char server_random
[SSL3_RANDOM_SIZE
];
128 static struct tls_context
* tls_context_new(const struct tls_config
*conf
)
130 struct tls_context
*context
= os_zalloc(sizeof(*context
));
134 context
->event_cb
= conf
->event_cb
;
135 context
->cb_ctx
= conf
->cb_ctx
;
136 context
->cert_in_cb
= conf
->cert_in_cb
;
142 #ifdef CONFIG_NO_STDOUT_DEBUG
144 static void _tls_show_errors(void)
148 while ((err
= ERR_get_error())) {
149 /* Just ignore the errors, since stdout is disabled */
152 #define tls_show_errors(l, f, t) _tls_show_errors()
154 #else /* CONFIG_NO_STDOUT_DEBUG */
156 static void tls_show_errors(int level
, const char *func
, const char *txt
)
160 wpa_printf(level
, "OpenSSL: %s - %s %s",
161 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
163 while ((err
= ERR_get_error())) {
164 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
165 ERR_error_string(err
, NULL
));
169 #endif /* CONFIG_NO_STDOUT_DEBUG */
172 #ifdef CONFIG_NATIVE_WINDOWS
174 /* Windows CryptoAPI and access to certificate stores */
175 #include <wincrypt.h>
177 #ifdef __MINGW32_VERSION
179 * MinGW does not yet include all the needed definitions for CryptoAPI, so
180 * define here whatever extra is needed.
182 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
183 #define CERT_STORE_READONLY_FLAG 0x00008000
184 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
186 #endif /* __MINGW32_VERSION */
189 struct cryptoapi_rsa_data
{
190 const CERT_CONTEXT
*cert
;
191 HCRYPTPROV crypt_prov
;
193 BOOL free_crypt_prov
;
197 static void cryptoapi_error(const char *msg
)
199 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
200 msg
, (unsigned int) GetLastError());
204 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
205 unsigned char *to
, RSA
*rsa
, int padding
)
207 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
212 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
213 unsigned char *to
, RSA
*rsa
, int padding
)
215 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
220 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
221 unsigned char *to
, RSA
*rsa
, int padding
)
223 struct cryptoapi_rsa_data
*priv
=
224 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
226 DWORD hash_size
, len
, i
;
227 unsigned char *buf
= NULL
;
231 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
232 ERR_R_PASSED_NULL_PARAMETER
);
236 if (padding
!= RSA_PKCS1_PADDING
) {
237 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
238 RSA_R_UNKNOWN_PADDING_TYPE
);
242 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
243 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
245 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
246 RSA_R_INVALID_MESSAGE_LENGTH
);
250 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
252 cryptoapi_error("CryptCreateHash failed");
256 len
= sizeof(hash_size
);
257 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
259 cryptoapi_error("CryptGetHashParam failed");
263 if ((int) hash_size
!= flen
) {
264 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
265 (unsigned) hash_size
, flen
);
266 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
267 RSA_R_INVALID_MESSAGE_LENGTH
);
270 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
271 cryptoapi_error("CryptSetHashParam failed");
276 buf
= os_malloc(len
);
278 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
282 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
283 cryptoapi_error("CryptSignHash failed");
287 for (i
= 0; i
< len
; i
++)
288 to
[i
] = buf
[len
- i
- 1];
293 CryptDestroyHash(hash
);
299 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
300 unsigned char *to
, RSA
*rsa
, int padding
)
302 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
307 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
311 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
312 CryptReleaseContext(priv
->crypt_prov
, 0);
314 CertFreeCertificateContext(priv
->cert
);
319 static int cryptoapi_finish(RSA
*rsa
)
321 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
322 os_free((void *) rsa
->meth
);
328 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
331 const CERT_CONTEXT
*ret
= NULL
;
333 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
334 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
335 CERT_STORE_READONLY_FLAG
, L
"MY");
337 cryptoapi_error("Failed to open 'My system store'");
341 if (strncmp(name
, "cert://", 7) == 0) {
342 unsigned short wbuf
[255];
343 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
344 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
346 0, CERT_FIND_SUBJECT_STR
,
348 } else if (strncmp(name
, "hash://", 7) == 0) {
349 CRYPT_HASH_BLOB blob
;
351 const char *hash
= name
+ 7;
354 len
= os_strlen(hash
) / 2;
355 buf
= os_malloc(len
);
356 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
359 ret
= CertFindCertificateInStore(cs
,
368 CertCloseStore(cs
, 0);
374 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
377 RSA
*rsa
= NULL
, *pub_rsa
;
378 struct cryptoapi_rsa_data
*priv
;
379 RSA_METHOD
*rsa_meth
;
382 (strncmp(name
, "cert://", 7) != 0 &&
383 strncmp(name
, "hash://", 7) != 0))
386 priv
= os_zalloc(sizeof(*priv
));
387 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
388 if (priv
== NULL
|| rsa_meth
== NULL
) {
389 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
390 "for CryptoAPI RSA method");
396 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
397 if (priv
->cert
== NULL
) {
398 priv
->cert
= cryptoapi_find_cert(
399 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
401 if (priv
->cert
== NULL
) {
402 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
407 cert
= d2i_X509(NULL
,
408 (const unsigned char **) &priv
->cert
->pbCertEncoded
,
409 priv
->cert
->cbCertEncoded
);
411 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
416 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
417 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
418 NULL
, &priv
->crypt_prov
,
420 &priv
->free_crypt_prov
)) {
421 cryptoapi_error("Failed to acquire a private key for the "
426 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
427 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
428 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
429 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
430 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
431 rsa_meth
->finish
= cryptoapi_finish
;
432 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
433 rsa_meth
->app_data
= (char *) priv
;
437 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
438 ERR_R_MALLOC_FAILURE
);
442 if (!SSL_use_certificate(ssl
, cert
)) {
447 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
451 rsa
->n
= BN_dup(pub_rsa
->n
);
452 rsa
->e
= BN_dup(pub_rsa
->e
);
453 if (!RSA_set_method(rsa
, rsa_meth
))
456 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
469 cryptoapi_free_data(priv
);
475 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
478 PCCERT_CONTEXT ctx
= NULL
;
486 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
491 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
494 wsprintf(wstore
, L
"%S", store
);
495 cs
= CertOpenSystemStore(0, wstore
);
498 cs
= CertOpenSystemStore(0, store
);
501 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
502 "'%s': error=%d", __func__
, store
,
503 (int) GetLastError());
507 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
508 cert
= d2i_X509(NULL
,
509 (const unsigned char **) &ctx
->pbCertEncoded
,
512 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
513 "X509 DER encoding for CA cert");
517 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
519 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
520 "system certificate store: subject='%s'", buf
);
522 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
523 tls_show_errors(MSG_WARNING
, __func__
,
524 "Failed to add ca_cert to OpenSSL "
525 "certificate store");
531 if (!CertCloseStore(cs
, 0)) {
532 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
533 "'%s': error=%d", __func__
, name
+ 13,
534 (int) GetLastError());
541 #else /* CONFIG_NATIVE_WINDOWS */
543 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
548 #endif /* CONFIG_NATIVE_WINDOWS */
551 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
556 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
557 w
= where
& ~SSL_ST_MASK
;
558 if (w
& SSL_ST_CONNECT
)
560 else if (w
& SSL_ST_ACCEPT
)
565 if (where
& SSL_CB_LOOP
) {
566 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
567 str
, SSL_state_string_long(ssl
));
568 } else if (where
& SSL_CB_ALERT
) {
569 struct tls_connection
*conn
= SSL_get_app_data((SSL
*) ssl
);
570 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
571 where
& SSL_CB_READ
?
572 "read (remote end reported an error)" :
573 "write (local SSL3 detected an error)",
574 SSL_alert_type_string_long(ret
),
575 SSL_alert_desc_string_long(ret
));
576 if ((ret
>> 8) == SSL3_AL_FATAL
) {
577 if (where
& SSL_CB_READ
)
580 conn
->write_alerts
++;
582 if (conn
->context
->event_cb
!= NULL
) {
583 union tls_event_data ev
;
584 struct tls_context
*context
= conn
->context
;
585 os_memset(&ev
, 0, sizeof(ev
));
586 ev
.alert
.is_local
= !(where
& SSL_CB_READ
);
587 ev
.alert
.type
= SSL_alert_type_string_long(ret
);
588 ev
.alert
.description
= SSL_alert_desc_string_long(ret
);
589 context
->event_cb(context
->cb_ctx
, TLS_ALERT
, &ev
);
591 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
592 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
593 str
, ret
== 0 ? "failed" : "error",
594 SSL_state_string_long(ssl
));
599 #ifndef OPENSSL_NO_ENGINE
601 * tls_engine_load_dynamic_generic - load any openssl engine
602 * @pre: an array of commands and values that load an engine initialized
603 * in the engine specific function
604 * @post: an array of commands and values that initialize an already loaded
605 * engine (or %NULL if not required)
606 * @id: the engine id of the engine to load (only required if post is not %NULL
608 * This function is a generic function that loads any openssl engine.
610 * Returns: 0 on success, -1 on failure
612 static int tls_engine_load_dynamic_generic(const char *pre
[],
613 const char *post
[], const char *id
)
616 const char *dynamic_id
= "dynamic";
618 engine
= ENGINE_by_id(id
);
621 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
627 engine
= ENGINE_by_id(dynamic_id
);
628 if (engine
== NULL
) {
629 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
631 ERR_error_string(ERR_get_error(), NULL
));
635 /* Perform the pre commands. This will load the engine. */
636 while (pre
&& pre
[0]) {
637 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
638 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
639 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
640 "%s %s [%s]", pre
[0], pre
[1],
641 ERR_error_string(ERR_get_error(), NULL
));
649 * Free the reference to the "dynamic" engine. The loaded engine can
650 * now be looked up using ENGINE_by_id().
654 engine
= ENGINE_by_id(id
);
655 if (engine
== NULL
) {
656 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
657 id
, ERR_error_string(ERR_get_error(), NULL
));
661 while (post
&& post
[0]) {
662 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
663 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
664 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
665 " %s %s [%s]", post
[0], post
[1],
666 ERR_error_string(ERR_get_error(), NULL
));
667 ENGINE_remove(engine
);
680 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
681 * @pkcs11_so_path: pksc11_so_path from the configuration
682 * @pcks11_module_path: pkcs11_module_path from the configuration
684 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
685 const char *pkcs11_module_path
)
687 char *engine_id
= "pkcs11";
688 const char *pre_cmd
[] = {
689 "SO_PATH", NULL
/* pkcs11_so_path */,
690 "ID", NULL
/* engine_id */,
692 /* "NO_VCHECK", "1", */
696 const char *post_cmd
[] = {
697 "MODULE_PATH", NULL
/* pkcs11_module_path */,
704 pre_cmd
[1] = pkcs11_so_path
;
705 pre_cmd
[3] = engine_id
;
706 if (pkcs11_module_path
)
707 post_cmd
[1] = pkcs11_module_path
;
711 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
714 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
719 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
720 * @opensc_so_path: opensc_so_path from the configuration
722 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
724 char *engine_id
= "opensc";
725 const char *pre_cmd
[] = {
726 "SO_PATH", NULL
/* opensc_so_path */,
727 "ID", NULL
/* engine_id */,
736 pre_cmd
[1] = opensc_so_path
;
737 pre_cmd
[3] = engine_id
;
739 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
742 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
744 #endif /* OPENSSL_NO_ENGINE */
747 void * tls_init(const struct tls_config
*conf
)
750 struct tls_context
*context
;
753 if (tls_openssl_ref_count
== 0) {
754 tls_global
= context
= tls_context_new(conf
);
759 if (conf
&& conf
->fips_mode
) {
760 if (!FIPS_mode_set(1)) {
761 wpa_printf(MSG_ERROR
, "Failed to enable FIPS "
763 ERR_load_crypto_strings();
764 ERR_print_errors_fp(stderr
);
769 wpa_printf(MSG_INFO
, "Running in FIPS mode");
771 #else /* OPENSSL_FIPS */
772 if (conf
&& conf
->fips_mode
) {
773 wpa_printf(MSG_ERROR
, "FIPS mode requested, but not "
779 #endif /* OPENSSL_FIPS */
780 #endif /* CONFIG_FIPS */
781 SSL_load_error_strings();
783 #ifndef OPENSSL_NO_SHA256
784 EVP_add_digest(EVP_sha256());
785 #endif /* OPENSSL_NO_SHA256 */
786 /* TODO: if /dev/urandom is available, PRNG is seeded
787 * automatically. If this is not the case, random data should
791 #ifndef OPENSSL_NO_RC2
793 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
794 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
795 * versions, but it looks like OpenSSL 1.0.0 does not do that
798 EVP_add_cipher(EVP_rc2_40_cbc());
799 #endif /* OPENSSL_NO_RC2 */
801 #endif /* PKCS12_FUNCS */
803 context
= tls_context_new(conf
);
807 tls_openssl_ref_count
++;
809 ssl
= SSL_CTX_new(SSLv23_method());
811 tls_openssl_ref_count
--;
812 if (context
!= tls_global
)
814 if (tls_openssl_ref_count
== 0) {
821 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv2
);
822 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv3
);
824 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
825 SSL_CTX_set_app_data(ssl
, context
);
827 #ifndef OPENSSL_NO_ENGINE
828 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
829 ERR_load_ENGINE_strings();
830 ENGINE_load_dynamic();
833 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
834 conf
->pkcs11_module_path
)) {
835 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
836 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
837 conf
->pkcs11_module_path
)) {
842 #endif /* OPENSSL_NO_ENGINE */
844 if (conf
&& conf
->openssl_ciphers
)
845 ciphers
= conf
->openssl_ciphers
;
847 ciphers
= "DEFAULT:!EXP:!LOW";
848 if (SSL_CTX_set_cipher_list(ssl
, ciphers
) != 1) {
849 wpa_printf(MSG_ERROR
,
850 "OpenSSL: Failed to set cipher string '%s'",
860 void tls_deinit(void *ssl_ctx
)
862 SSL_CTX
*ssl
= ssl_ctx
;
863 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
864 if (context
!= tls_global
)
868 tls_openssl_ref_count
--;
869 if (tls_openssl_ref_count
== 0) {
870 #ifndef OPENSSL_NO_ENGINE
872 #endif /* OPENSSL_NO_ENGINE */
873 CRYPTO_cleanup_all_ex_data();
874 ERR_remove_thread_state(NULL
);
877 os_free(tls_global
->ocsp_stapling_response
);
878 tls_global
->ocsp_stapling_response
= NULL
;
885 #ifndef OPENSSL_NO_ENGINE
887 /* Cryptoki return values */
888 #define CKR_PIN_INCORRECT 0x000000a0
889 #define CKR_PIN_INVALID 0x000000a1
890 #define CKR_PIN_LEN_RANGE 0x000000a2
893 #define ERR_LIB_PKCS11 ERR_LIB_USER
895 static int tls_is_pin_error(unsigned int err
)
897 return ERR_GET_LIB(err
) == ERR_LIB_PKCS11
&&
898 (ERR_GET_REASON(err
) == CKR_PIN_INCORRECT
||
899 ERR_GET_REASON(err
) == CKR_PIN_INVALID
||
900 ERR_GET_REASON(err
) == CKR_PIN_LEN_RANGE
);
903 #endif /* OPENSSL_NO_ENGINE */
906 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
907 const char *pin
, const char *key_id
,
908 const char *cert_id
, const char *ca_cert_id
)
910 #ifndef OPENSSL_NO_ENGINE
912 if (engine_id
== NULL
) {
913 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
919 ENGINE_load_dynamic();
921 conn
->engine
= ENGINE_by_id(engine_id
);
923 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
924 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
927 if (ENGINE_init(conn
->engine
) != 1) {
928 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
929 "(engine: %s) [%s]", engine_id
,
930 ERR_error_string(ERR_get_error(), NULL
));
933 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
936 if (pin
&& ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
937 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
938 ERR_error_string(ERR_get_error(), NULL
));
944 * Ensure that the ENGINE does not attempt to use the OpenSSL
945 * UI system to obtain a PIN, if we didn't provide one.
948 const void *password
;
949 const char *prompt_info
;
950 } key_cb
= { "", NULL
};
952 /* load private key first in-case PIN is required for cert */
953 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
956 if (!conn
->private_key
) {
957 unsigned long err
= ERR_get_error();
959 wpa_printf(MSG_ERROR
,
960 "ENGINE: cannot load private key with id '%s' [%s]",
962 ERR_error_string(err
, NULL
));
963 if (tls_is_pin_error(err
))
964 ret
= TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
966 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
971 /* handle a certificate and/or CA certificate */
972 if (cert_id
|| ca_cert_id
) {
973 const char *cmd_name
= "LOAD_CERT_CTRL";
975 /* test if the engine supports a LOAD_CERT_CTRL */
976 if (!ENGINE_ctrl(conn
->engine
, ENGINE_CTRL_GET_CMD_FROM_NAME
,
977 0, (void *)cmd_name
, NULL
)) {
978 wpa_printf(MSG_ERROR
, "ENGINE: engine does not support"
979 " loading certificates");
980 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
989 ENGINE_free(conn
->engine
);
993 if (conn
->private_key
) {
994 EVP_PKEY_free(conn
->private_key
);
995 conn
->private_key
= NULL
;
999 #else /* OPENSSL_NO_ENGINE */
1001 #endif /* OPENSSL_NO_ENGINE */
1005 static void tls_engine_deinit(struct tls_connection
*conn
)
1007 #ifndef OPENSSL_NO_ENGINE
1008 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
1009 if (conn
->private_key
) {
1010 EVP_PKEY_free(conn
->private_key
);
1011 conn
->private_key
= NULL
;
1014 ENGINE_finish(conn
->engine
);
1015 conn
->engine
= NULL
;
1017 #endif /* OPENSSL_NO_ENGINE */
1021 int tls_get_errors(void *ssl_ctx
)
1026 while ((err
= ERR_get_error())) {
1027 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
1028 ERR_error_string(err
, NULL
));
1036 static void tls_msg_cb(int write_p
, int version
, int content_type
,
1037 const void *buf
, size_t len
, SSL
*ssl
, void *arg
)
1039 struct tls_connection
*conn
= arg
;
1040 const u8
*pos
= buf
;
1042 wpa_printf(MSG_DEBUG
, "OpenSSL: %s ver=0x%x content_type=%d",
1043 write_p
? "TX" : "RX", version
, content_type
);
1044 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Message", buf
, len
);
1045 if (content_type
== 24 && len
>= 3 && pos
[0] == 1) {
1046 size_t payload_len
= WPA_GET_BE16(pos
+ 1);
1047 if (payload_len
+ 3 > len
) {
1048 wpa_printf(MSG_ERROR
, "OpenSSL: Heartbeat attack detected");
1049 conn
->invalid_hb_used
= 1;
1055 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
1057 SSL_CTX
*ssl
= ssl_ctx
;
1058 struct tls_connection
*conn
;
1060 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1062 conn
= os_zalloc(sizeof(*conn
));
1065 conn
->ssl_ctx
= ssl_ctx
;
1066 conn
->ssl
= SSL_new(ssl
);
1067 if (conn
->ssl
== NULL
) {
1068 tls_show_errors(MSG_INFO
, __func__
,
1069 "Failed to initialize new SSL connection");
1074 conn
->context
= context
;
1075 SSL_set_app_data(conn
->ssl
, conn
);
1076 SSL_set_msg_callback(conn
->ssl
, tls_msg_cb
);
1077 SSL_set_msg_callback_arg(conn
->ssl
, conn
);
1078 options
= SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1079 SSL_OP_SINGLE_DH_USE
;
1080 #ifdef SSL_OP_NO_COMPRESSION
1081 options
|= SSL_OP_NO_COMPRESSION
;
1082 #endif /* SSL_OP_NO_COMPRESSION */
1083 SSL_set_options(conn
->ssl
, options
);
1085 conn
->ssl_in
= BIO_new(BIO_s_mem());
1086 if (!conn
->ssl_in
) {
1087 tls_show_errors(MSG_INFO
, __func__
,
1088 "Failed to create a new BIO for ssl_in");
1089 SSL_free(conn
->ssl
);
1094 conn
->ssl_out
= BIO_new(BIO_s_mem());
1095 if (!conn
->ssl_out
) {
1096 tls_show_errors(MSG_INFO
, __func__
,
1097 "Failed to create a new BIO for ssl_out");
1098 SSL_free(conn
->ssl
);
1099 BIO_free(conn
->ssl_in
);
1104 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
1110 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
1114 SSL_free(conn
->ssl
);
1115 tls_engine_deinit(conn
);
1116 os_free(conn
->subject_match
);
1117 os_free(conn
->altsubject_match
);
1118 os_free(conn
->suffix_match
);
1119 os_free(conn
->domain_match
);
1120 os_free(conn
->session_ticket
);
1125 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
1127 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
1131 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
1136 /* Shutdown previous TLS connection without notifying the peer
1137 * because the connection was already terminated in practice
1138 * and "close notify" shutdown alert would confuse AS. */
1139 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1140 SSL_shutdown(conn
->ssl
);
1145 static int tls_match_altsubject_component(X509
*cert
, int type
,
1146 const char *value
, size_t len
)
1153 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1155 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1156 gen
= sk_GENERAL_NAME_value(ext
, i
);
1157 if (gen
->type
!= type
)
1159 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
1160 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
1168 static int tls_match_altsubject(X509
*cert
, const char *match
)
1171 const char *pos
, *end
;
1176 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
1179 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
1182 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
1186 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
1190 end
= os_strchr(pos
, ';');
1192 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
1193 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
1194 os_strncmp(end
+ 1, "URI:", 4) == 0)
1196 end
= os_strchr(end
+ 1, ';');
1201 len
= os_strlen(pos
);
1202 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1211 #ifndef CONFIG_NATIVE_WINDOWS
1212 static int domain_suffix_match(const u8
*val
, size_t len
, const char *match
,
1215 size_t i
, match_len
;
1217 /* Check for embedded nuls that could mess up suffix matching */
1218 for (i
= 0; i
< len
; i
++) {
1219 if (val
[i
] == '\0') {
1220 wpa_printf(MSG_DEBUG
, "TLS: Embedded null in a string - reject");
1225 match_len
= os_strlen(match
);
1226 if (match_len
> len
|| (full
&& match_len
!= len
))
1229 if (os_strncasecmp((const char *) val
+ len
- match_len
, match
,
1231 return 0; /* no match */
1233 if (match_len
== len
)
1234 return 1; /* exact match */
1236 if (val
[len
- match_len
- 1] == '.')
1237 return 1; /* full label match completes suffix match */
1239 wpa_printf(MSG_DEBUG
, "TLS: Reject due to incomplete label match");
1242 #endif /* CONFIG_NATIVE_WINDOWS */
1245 static int tls_match_suffix(X509
*cert
, const char *match
, int full
)
1247 #ifdef CONFIG_NATIVE_WINDOWS
1248 /* wincrypt.h has conflicting X509_NAME definition */
1250 #else /* CONFIG_NATIVE_WINDOWS */
1258 wpa_printf(MSG_DEBUG
, "TLS: Match domain against %s%s",
1259 full
? "": "suffix ", match
);
1261 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1263 for (j
= 0; ext
&& j
< sk_GENERAL_NAME_num(ext
); j
++) {
1264 gen
= sk_GENERAL_NAME_value(ext
, j
);
1265 if (gen
->type
!= GEN_DNS
)
1268 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate dNSName",
1269 gen
->d
.dNSName
->data
,
1270 gen
->d
.dNSName
->length
);
1271 if (domain_suffix_match(gen
->d
.dNSName
->data
,
1272 gen
->d
.dNSName
->length
, match
, full
) ==
1274 wpa_printf(MSG_DEBUG
, "TLS: %s in dNSName found",
1275 full
? "Match" : "Suffix match");
1281 wpa_printf(MSG_DEBUG
, "TLS: None of the dNSName(s) matched");
1285 name
= X509_get_subject_name(cert
);
1291 i
= X509_NAME_get_index_by_NID(name
, NID_commonName
, i
);
1294 e
= X509_NAME_get_entry(name
, i
);
1297 cn
= X509_NAME_ENTRY_get_data(e
);
1300 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate commonName",
1301 cn
->data
, cn
->length
);
1302 if (domain_suffix_match(cn
->data
, cn
->length
, match
, full
) == 1)
1304 wpa_printf(MSG_DEBUG
, "TLS: %s in commonName found",
1305 full
? "Match" : "Suffix match");
1310 wpa_printf(MSG_DEBUG
, "TLS: No CommonName %smatch found",
1311 full
? "": "suffix ");
1313 #endif /* CONFIG_NATIVE_WINDOWS */
1317 static enum tls_fail_reason
openssl_tls_fail_reason(int err
)
1320 case X509_V_ERR_CERT_REVOKED
:
1321 return TLS_FAIL_REVOKED
;
1322 case X509_V_ERR_CERT_NOT_YET_VALID
:
1323 case X509_V_ERR_CRL_NOT_YET_VALID
:
1324 return TLS_FAIL_NOT_YET_VALID
;
1325 case X509_V_ERR_CERT_HAS_EXPIRED
:
1326 case X509_V_ERR_CRL_HAS_EXPIRED
:
1327 return TLS_FAIL_EXPIRED
;
1328 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1329 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1330 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1331 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1332 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1333 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1334 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1335 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1336 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1337 case X509_V_ERR_INVALID_CA
:
1338 return TLS_FAIL_UNTRUSTED
;
1339 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1340 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1341 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1342 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1343 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1344 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1345 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1346 case X509_V_ERR_CERT_UNTRUSTED
:
1347 case X509_V_ERR_CERT_REJECTED
:
1348 return TLS_FAIL_BAD_CERTIFICATE
;
1350 return TLS_FAIL_UNSPECIFIED
;
1355 static struct wpabuf
* get_x509_cert(X509
*cert
)
1360 int cert_len
= i2d_X509(cert
, NULL
);
1364 buf
= wpabuf_alloc(cert_len
);
1368 tmp
= wpabuf_put(buf
, cert_len
);
1369 i2d_X509(cert
, &tmp
);
1374 static void openssl_tls_fail_event(struct tls_connection
*conn
,
1375 X509
*err_cert
, int err
, int depth
,
1376 const char *subject
, const char *err_str
,
1377 enum tls_fail_reason reason
)
1379 union tls_event_data ev
;
1380 struct wpabuf
*cert
= NULL
;
1381 struct tls_context
*context
= conn
->context
;
1383 if (context
->event_cb
== NULL
)
1386 cert
= get_x509_cert(err_cert
);
1387 os_memset(&ev
, 0, sizeof(ev
));
1388 ev
.cert_fail
.reason
= reason
!= TLS_FAIL_UNSPECIFIED
?
1389 reason
: openssl_tls_fail_reason(err
);
1390 ev
.cert_fail
.depth
= depth
;
1391 ev
.cert_fail
.subject
= subject
;
1392 ev
.cert_fail
.reason_txt
= err_str
;
1393 ev
.cert_fail
.cert
= cert
;
1394 context
->event_cb(context
->cb_ctx
, TLS_CERT_CHAIN_FAILURE
, &ev
);
1399 static void openssl_tls_cert_event(struct tls_connection
*conn
,
1400 X509
*err_cert
, int depth
,
1401 const char *subject
)
1403 struct wpabuf
*cert
= NULL
;
1404 union tls_event_data ev
;
1405 struct tls_context
*context
= conn
->context
;
1406 char *altsubject
[TLS_MAX_ALT_SUBJECT
];
1407 int alt
, num_altsubject
= 0;
1411 #ifdef CONFIG_SHA256
1413 #endif /* CONFIG_SHA256 */
1415 if (context
->event_cb
== NULL
)
1418 os_memset(&ev
, 0, sizeof(ev
));
1419 if (conn
->cert_probe
|| context
->cert_in_cb
) {
1420 cert
= get_x509_cert(err_cert
);
1421 ev
.peer_cert
.cert
= cert
;
1423 #ifdef CONFIG_SHA256
1427 addr
[0] = wpabuf_head(cert
);
1428 len
[0] = wpabuf_len(cert
);
1429 if (sha256_vector(1, addr
, len
, hash
) == 0) {
1430 ev
.peer_cert
.hash
= hash
;
1431 ev
.peer_cert
.hash_len
= sizeof(hash
);
1434 #endif /* CONFIG_SHA256 */
1435 ev
.peer_cert
.depth
= depth
;
1436 ev
.peer_cert
.subject
= subject
;
1438 ext
= X509_get_ext_d2i(err_cert
, NID_subject_alt_name
, NULL
, NULL
);
1439 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1442 if (num_altsubject
== TLS_MAX_ALT_SUBJECT
)
1444 gen
= sk_GENERAL_NAME_value(ext
, i
);
1445 if (gen
->type
!= GEN_EMAIL
&&
1446 gen
->type
!= GEN_DNS
&&
1447 gen
->type
!= GEN_URI
)
1450 pos
= os_malloc(10 + gen
->d
.ia5
->length
+ 1);
1453 altsubject
[num_altsubject
++] = pos
;
1455 switch (gen
->type
) {
1457 os_memcpy(pos
, "EMAIL:", 6);
1461 os_memcpy(pos
, "DNS:", 4);
1465 os_memcpy(pos
, "URI:", 4);
1470 os_memcpy(pos
, gen
->d
.ia5
->data
, gen
->d
.ia5
->length
);
1471 pos
+= gen
->d
.ia5
->length
;
1475 for (alt
= 0; alt
< num_altsubject
; alt
++)
1476 ev
.peer_cert
.altsubject
[alt
] = altsubject
[alt
];
1477 ev
.peer_cert
.num_altsubject
= num_altsubject
;
1479 context
->event_cb(context
->cb_ctx
, TLS_PEER_CERTIFICATE
, &ev
);
1481 for (alt
= 0; alt
< num_altsubject
; alt
++)
1482 os_free(altsubject
[alt
]);
1486 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1492 struct tls_connection
*conn
;
1493 struct tls_context
*context
;
1494 char *match
, *altmatch
, *suffix_match
, *domain_match
;
1495 const char *err_str
;
1497 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1501 err
= X509_STORE_CTX_get_error(x509_ctx
);
1502 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1503 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1504 SSL_get_ex_data_X509_STORE_CTX_idx());
1505 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1507 conn
= SSL_get_app_data(ssl
);
1512 conn
->peer_cert
= err_cert
;
1513 else if (depth
== 1)
1514 conn
->peer_issuer
= err_cert
;
1515 else if (depth
== 2)
1516 conn
->peer_issuer_issuer
= err_cert
;
1518 context
= conn
->context
;
1519 match
= conn
->subject_match
;
1520 altmatch
= conn
->altsubject_match
;
1521 suffix_match
= conn
->suffix_match
;
1522 domain_match
= conn
->domain_match
;
1524 if (!preverify_ok
&& !conn
->ca_cert_verify
)
1526 if (!preverify_ok
&& depth
> 0 && conn
->server_cert_only
)
1528 if (!preverify_ok
&& (conn
->flags
& TLS_CONN_DISABLE_TIME_CHECKS
) &&
1529 (err
== X509_V_ERR_CERT_HAS_EXPIRED
||
1530 err
== X509_V_ERR_CERT_NOT_YET_VALID
)) {
1531 wpa_printf(MSG_DEBUG
, "OpenSSL: Ignore certificate validity "
1536 err_str
= X509_verify_cert_error_string(err
);
1538 #ifdef CONFIG_SHA256
1540 * Do not require preverify_ok so we can explicity allow otherwise
1541 * invalid pinned server certificates.
1543 if (depth
== 0 && conn
->server_cert_only
) {
1544 struct wpabuf
*cert
;
1545 cert
= get_x509_cert(err_cert
);
1547 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not fetch "
1548 "server certificate data");
1554 addr
[0] = wpabuf_head(cert
);
1555 len
[0] = wpabuf_len(cert
);
1556 if (sha256_vector(1, addr
, len
, hash
) < 0 ||
1557 os_memcmp(conn
->srv_cert_hash
, hash
, 32) != 0) {
1558 err_str
= "Server certificate mismatch";
1559 err
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
1561 } else if (!preverify_ok
) {
1563 * Certificate matches pinned certificate, allow
1564 * regardless of other problems.
1566 wpa_printf(MSG_DEBUG
,
1567 "OpenSSL: Ignore validation issues for a pinned server certificate");
1573 #endif /* CONFIG_SHA256 */
1575 if (!preverify_ok
) {
1576 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1577 " error %d (%s) depth %d for '%s'", err
, err_str
,
1579 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1580 err_str
, TLS_FAIL_UNSPECIFIED
);
1581 return preverify_ok
;
1584 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - preverify_ok=%d "
1585 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1586 preverify_ok
, err
, err_str
,
1587 conn
->ca_cert_verify
, depth
, buf
);
1588 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1589 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1590 "match with '%s'", buf
, match
);
1592 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1594 TLS_FAIL_SUBJECT_MISMATCH
);
1595 } else if (depth
== 0 && altmatch
&&
1596 !tls_match_altsubject(err_cert
, altmatch
)) {
1597 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1598 "'%s' not found", altmatch
);
1600 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1601 "AltSubject mismatch",
1602 TLS_FAIL_ALTSUBJECT_MISMATCH
);
1603 } else if (depth
== 0 && suffix_match
&&
1604 !tls_match_suffix(err_cert
, suffix_match
, 0)) {
1605 wpa_printf(MSG_WARNING
, "TLS: Domain suffix match '%s' not found",
1608 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1609 "Domain suffix mismatch",
1610 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH
);
1611 } else if (depth
== 0 && domain_match
&&
1612 !tls_match_suffix(err_cert
, domain_match
, 1)) {
1613 wpa_printf(MSG_WARNING
, "TLS: Domain match '%s' not found",
1616 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1618 TLS_FAIL_DOMAIN_MISMATCH
);
1620 openssl_tls_cert_event(conn
, err_cert
, depth
, buf
);
1622 if (conn
->cert_probe
&& preverify_ok
&& depth
== 0) {
1623 wpa_printf(MSG_DEBUG
, "OpenSSL: Reject server certificate "
1624 "on probe-only run");
1626 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1627 "Server certificate chain probe",
1628 TLS_FAIL_SERVER_CHAIN_PROBE
);
1631 if (preverify_ok
&& context
->event_cb
!= NULL
)
1632 context
->event_cb(context
->cb_ctx
,
1633 TLS_CERT_CHAIN_SUCCESS
, NULL
);
1635 return preverify_ok
;
1639 #ifndef OPENSSL_NO_STDIO
1640 static int tls_load_ca_der(void *_ssl_ctx
, const char *ca_cert
)
1642 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1643 X509_LOOKUP
*lookup
;
1646 lookup
= X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx
),
1647 X509_LOOKUP_file());
1648 if (lookup
== NULL
) {
1649 tls_show_errors(MSG_WARNING
, __func__
,
1650 "Failed add lookup for X509 store");
1654 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
1655 unsigned long err
= ERR_peek_error();
1656 tls_show_errors(MSG_WARNING
, __func__
,
1657 "Failed load CA in DER format");
1658 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1659 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1660 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1661 "cert already in hash table error",
1669 #endif /* OPENSSL_NO_STDIO */
1672 static int tls_connection_ca_cert(void *_ssl_ctx
, struct tls_connection
*conn
,
1673 const char *ca_cert
, const u8
*ca_cert_blob
,
1674 size_t ca_cert_blob_len
, const char *ca_path
)
1676 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1680 * Remove previously configured trusted CA certificates before adding
1683 store
= X509_STORE_new();
1684 if (store
== NULL
) {
1685 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
1686 "certificate store", __func__
);
1689 SSL_CTX_set_cert_store(ssl_ctx
, store
);
1691 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1692 conn
->ca_cert_verify
= 1;
1694 if (ca_cert
&& os_strncmp(ca_cert
, "probe://", 8) == 0) {
1695 wpa_printf(MSG_DEBUG
, "OpenSSL: Probe for server certificate "
1697 conn
->cert_probe
= 1;
1698 conn
->ca_cert_verify
= 0;
1702 if (ca_cert
&& os_strncmp(ca_cert
, "hash://", 7) == 0) {
1703 #ifdef CONFIG_SHA256
1704 const char *pos
= ca_cert
+ 7;
1705 if (os_strncmp(pos
, "server/sha256/", 14) != 0) {
1706 wpa_printf(MSG_DEBUG
, "OpenSSL: Unsupported ca_cert "
1707 "hash value '%s'", ca_cert
);
1711 if (os_strlen(pos
) != 32 * 2) {
1712 wpa_printf(MSG_DEBUG
, "OpenSSL: Unexpected SHA256 "
1713 "hash length in ca_cert '%s'", ca_cert
);
1716 if (hexstr2bin(pos
, conn
->srv_cert_hash
, 32) < 0) {
1717 wpa_printf(MSG_DEBUG
, "OpenSSL: Invalid SHA256 hash "
1718 "value in ca_cert '%s'", ca_cert
);
1721 conn
->server_cert_only
= 1;
1722 wpa_printf(MSG_DEBUG
, "OpenSSL: Checking only server "
1723 "certificate match");
1725 #else /* CONFIG_SHA256 */
1726 wpa_printf(MSG_INFO
, "No SHA256 included in the build - "
1727 "cannot validate server certificate hash");
1729 #endif /* CONFIG_SHA256 */
1733 X509
*cert
= d2i_X509(NULL
,
1734 (const unsigned char **) &ca_cert_blob
,
1737 tls_show_errors(MSG_WARNING
, __func__
,
1738 "Failed to parse ca_cert_blob");
1742 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
1744 unsigned long err
= ERR_peek_error();
1745 tls_show_errors(MSG_WARNING
, __func__
,
1746 "Failed to add ca_cert_blob to "
1747 "certificate store");
1748 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1749 ERR_GET_REASON(err
) ==
1750 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1751 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1752 "cert already in hash table error",
1760 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
1761 "to certificate store", __func__
);
1766 if (ca_cert
&& os_strncmp("keystore://", ca_cert
, 11) == 0) {
1767 BIO
*bio
= BIO_from_keystore(&ca_cert
[11]);
1768 STACK_OF(X509_INFO
) *stack
= NULL
;
1772 stack
= PEM_X509_INFO_read_bio(bio
, NULL
, NULL
, NULL
);
1778 for (i
= 0; i
< sk_X509_INFO_num(stack
); ++i
) {
1779 X509_INFO
*info
= sk_X509_INFO_value(stack
, i
);
1781 X509_STORE_add_cert(ssl_ctx
->cert_store
,
1785 X509_STORE_add_crl(ssl_ctx
->cert_store
,
1789 sk_X509_INFO_pop_free(stack
, X509_INFO_free
);
1790 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1793 #endif /* ANDROID */
1795 #ifdef CONFIG_NATIVE_WINDOWS
1796 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
1798 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
1799 "system certificate store");
1802 #endif /* CONFIG_NATIVE_WINDOWS */
1804 if (ca_cert
|| ca_path
) {
1805 #ifndef OPENSSL_NO_STDIO
1806 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
1808 tls_show_errors(MSG_WARNING
, __func__
,
1809 "Failed to load root certificates");
1811 tls_load_ca_der(ssl_ctx
, ca_cert
) == 0) {
1812 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
1813 "DER format CA certificate",
1818 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1819 "certificate(s) loaded");
1820 tls_get_errors(ssl_ctx
);
1822 #else /* OPENSSL_NO_STDIO */
1823 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
1826 #endif /* OPENSSL_NO_STDIO */
1828 /* No ca_cert configured - do not try to verify server
1830 conn
->ca_cert_verify
= 0;
1837 static int tls_global_ca_cert(SSL_CTX
*ssl_ctx
, const char *ca_cert
)
1840 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
1842 tls_show_errors(MSG_WARNING
, __func__
,
1843 "Failed to load root certificates");
1847 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1848 "certificate(s) loaded");
1850 #ifndef OPENSSL_NO_STDIO
1851 /* Add the same CAs to the client certificate requests */
1852 SSL_CTX_set_client_CA_list(ssl_ctx
,
1853 SSL_load_client_CA_file(ca_cert
));
1854 #endif /* OPENSSL_NO_STDIO */
1861 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
1866 X509_STORE
*cs
= SSL_CTX_get_cert_store(ssl_ctx
);
1868 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
1869 "certificate store when enabling "
1873 flags
= X509_V_FLAG_CRL_CHECK
;
1875 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
1876 X509_STORE_set_flags(cs
, flags
);
1882 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
1883 const char *subject_match
,
1884 const char *altsubject_match
,
1885 const char *suffix_match
,
1886 const char *domain_match
)
1888 os_free(conn
->subject_match
);
1889 conn
->subject_match
= NULL
;
1890 if (subject_match
) {
1891 conn
->subject_match
= os_strdup(subject_match
);
1892 if (conn
->subject_match
== NULL
)
1896 os_free(conn
->altsubject_match
);
1897 conn
->altsubject_match
= NULL
;
1898 if (altsubject_match
) {
1899 conn
->altsubject_match
= os_strdup(altsubject_match
);
1900 if (conn
->altsubject_match
== NULL
)
1904 os_free(conn
->suffix_match
);
1905 conn
->suffix_match
= NULL
;
1907 conn
->suffix_match
= os_strdup(suffix_match
);
1908 if (conn
->suffix_match
== NULL
)
1912 os_free(conn
->domain_match
);
1913 conn
->domain_match
= NULL
;
1915 conn
->domain_match
= os_strdup(domain_match
);
1916 if (conn
->domain_match
== NULL
)
1924 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
1927 static int counter
= 0;
1933 conn
->ca_cert_verify
= 1;
1934 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
1935 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1936 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
1938 conn
->ca_cert_verify
= 0;
1939 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
1942 SSL_set_accept_state(conn
->ssl
);
1945 * Set session id context in order to avoid fatal errors when client
1946 * tries to resume a session. However, set the context to a unique
1947 * value in order to effectively disable session resumption for now
1948 * since not all areas of the server code are ready for it (e.g.,
1949 * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
1953 SSL_set_session_id_context(conn
->ssl
,
1954 (const unsigned char *) &counter
,
1961 static int tls_connection_client_cert(struct tls_connection
*conn
,
1962 const char *client_cert
,
1963 const u8
*client_cert_blob
,
1964 size_t client_cert_blob_len
)
1966 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
1969 if (client_cert_blob
&&
1970 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
1971 client_cert_blob_len
) == 1) {
1972 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
1975 } else if (client_cert_blob
) {
1976 tls_show_errors(MSG_DEBUG
, __func__
,
1977 "SSL_use_certificate_ASN1 failed");
1980 if (client_cert
== NULL
)
1984 if (os_strncmp("keystore://", client_cert
, 11) == 0) {
1985 BIO
*bio
= BIO_from_keystore(&client_cert
[11]);
1989 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
1993 if (SSL_use_certificate(conn
->ssl
, x509
) == 1)
1999 #endif /* ANDROID */
2001 #ifndef OPENSSL_NO_STDIO
2002 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2003 SSL_FILETYPE_ASN1
) == 1) {
2004 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
2009 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2010 SSL_FILETYPE_PEM
) == 1) {
2012 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
2017 tls_show_errors(MSG_DEBUG
, __func__
,
2018 "SSL_use_certificate_file failed");
2019 #else /* OPENSSL_NO_STDIO */
2020 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2021 #endif /* OPENSSL_NO_STDIO */
2027 static int tls_global_client_cert(SSL_CTX
*ssl_ctx
, const char *client_cert
)
2029 #ifndef OPENSSL_NO_STDIO
2030 if (client_cert
== NULL
)
2033 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2034 SSL_FILETYPE_ASN1
) != 1 &&
2035 SSL_CTX_use_certificate_chain_file(ssl_ctx
, client_cert
) != 1 &&
2036 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2037 SSL_FILETYPE_PEM
) != 1) {
2038 tls_show_errors(MSG_INFO
, __func__
,
2039 "Failed to load client certificate");
2043 #else /* OPENSSL_NO_STDIO */
2044 if (client_cert
== NULL
)
2046 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2048 #endif /* OPENSSL_NO_STDIO */
2052 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
2054 if (password
== NULL
) {
2057 os_strlcpy(buf
, (char *) password
, size
);
2058 return os_strlen(buf
);
2063 static int tls_parse_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, PKCS12
*p12
,
2068 STACK_OF(X509
) *certs
;
2075 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
2076 tls_show_errors(MSG_DEBUG
, __func__
,
2077 "Failed to parse PKCS12 file");
2081 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
2084 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2086 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
2087 "subject='%s'", buf
);
2089 if (SSL_use_certificate(ssl
, cert
) != 1)
2092 if (SSL_CTX_use_certificate(ssl_ctx
, cert
) != 1)
2099 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
2101 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
2104 if (SSL_CTX_use_PrivateKey(ssl_ctx
, pkey
) != 1)
2107 EVP_PKEY_free(pkey
);
2111 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2112 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2114 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2115 " from PKCS12: subject='%s'", buf
);
2117 * There is no SSL equivalent for the chain cert - so
2118 * always add it to the context...
2120 if (SSL_CTX_add_extra_chain_cert(ssl_ctx
, cert
) != 1) {
2125 sk_X509_free(certs
);
2131 tls_get_errors(ssl_ctx
);
2135 #endif /* PKCS12_FUNCS */
2138 static int tls_read_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *private_key
,
2145 f
= fopen(private_key
, "rb");
2149 p12
= d2i_PKCS12_fp(f
, NULL
);
2153 tls_show_errors(MSG_INFO
, __func__
,
2154 "Failed to use PKCS#12 file");
2158 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2160 #else /* PKCS12_FUNCS */
2161 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
2164 #endif /* PKCS12_FUNCS */
2168 static int tls_read_pkcs12_blob(SSL_CTX
*ssl_ctx
, SSL
*ssl
,
2169 const u8
*blob
, size_t len
, const char *passwd
)
2174 p12
= d2i_PKCS12(NULL
, (const unsigned char **) &blob
, len
);
2176 tls_show_errors(MSG_INFO
, __func__
,
2177 "Failed to use PKCS#12 blob");
2181 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2183 #else /* PKCS12_FUNCS */
2184 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
2187 #endif /* PKCS12_FUNCS */
2191 #ifndef OPENSSL_NO_ENGINE
2192 static int tls_engine_get_cert(struct tls_connection
*conn
,
2193 const char *cert_id
,
2196 /* this runs after the private key is loaded so no PIN is required */
2198 const char *cert_id
;
2201 params
.cert_id
= cert_id
;
2204 if (!ENGINE_ctrl_cmd(conn
->engine
, "LOAD_CERT_CTRL",
2205 0, ¶ms
, NULL
, 1)) {
2206 unsigned long err
= ERR_get_error();
2208 wpa_printf(MSG_ERROR
, "ENGINE: cannot load client cert with id"
2209 " '%s' [%s]", cert_id
,
2210 ERR_error_string(err
, NULL
));
2211 if (tls_is_pin_error(err
))
2212 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
2213 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2216 wpa_printf(MSG_ERROR
, "ENGINE: did not properly cert with id"
2218 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2220 *cert
= params
.cert
;
2223 #endif /* OPENSSL_NO_ENGINE */
2226 static int tls_connection_engine_client_cert(struct tls_connection
*conn
,
2227 const char *cert_id
)
2229 #ifndef OPENSSL_NO_ENGINE
2232 if (tls_engine_get_cert(conn
, cert_id
, &cert
))
2235 if (!SSL_use_certificate(conn
->ssl
, cert
)) {
2236 tls_show_errors(MSG_ERROR
, __func__
,
2237 "SSL_use_certificate failed");
2242 wpa_printf(MSG_DEBUG
, "ENGINE: SSL_use_certificate --> "
2246 #else /* OPENSSL_NO_ENGINE */
2248 #endif /* OPENSSL_NO_ENGINE */
2252 static int tls_connection_engine_ca_cert(void *_ssl_ctx
,
2253 struct tls_connection
*conn
,
2254 const char *ca_cert_id
)
2256 #ifndef OPENSSL_NO_ENGINE
2258 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2261 if (tls_engine_get_cert(conn
, ca_cert_id
, &cert
))
2264 /* start off the same as tls_connection_ca_cert */
2265 store
= X509_STORE_new();
2266 if (store
== NULL
) {
2267 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2268 "certificate store", __func__
);
2272 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2273 if (!X509_STORE_add_cert(store
, cert
)) {
2274 unsigned long err
= ERR_peek_error();
2275 tls_show_errors(MSG_WARNING
, __func__
,
2276 "Failed to add CA certificate from engine "
2277 "to certificate store");
2278 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2279 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2280 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring cert"
2281 " already in hash table error",
2289 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added CA certificate from engine "
2290 "to certificate store", __func__
);
2291 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2292 conn
->ca_cert_verify
= 1;
2296 #else /* OPENSSL_NO_ENGINE */
2298 #endif /* OPENSSL_NO_ENGINE */
2302 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
2304 #ifndef OPENSSL_NO_ENGINE
2305 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
2306 tls_show_errors(MSG_ERROR
, __func__
,
2307 "ENGINE: cannot use private key for TLS");
2310 if (!SSL_check_private_key(conn
->ssl
)) {
2311 tls_show_errors(MSG_INFO
, __func__
,
2312 "Private key failed verification");
2316 #else /* OPENSSL_NO_ENGINE */
2317 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
2318 "engine support was not compiled in");
2320 #endif /* OPENSSL_NO_ENGINE */
2324 static int tls_connection_private_key(void *_ssl_ctx
,
2325 struct tls_connection
*conn
,
2326 const char *private_key
,
2327 const char *private_key_passwd
,
2328 const u8
*private_key_blob
,
2329 size_t private_key_blob_len
)
2331 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2335 if (private_key
== NULL
&& private_key_blob
== NULL
)
2338 if (private_key_passwd
) {
2339 passwd
= os_strdup(private_key_passwd
);
2345 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2346 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2349 while (private_key_blob
) {
2350 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
2351 (u8
*) private_key_blob
,
2352 private_key_blob_len
) == 1) {
2353 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2354 "ASN1(EVP_PKEY_RSA) --> OK");
2359 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
2360 (u8
*) private_key_blob
,
2361 private_key_blob_len
) == 1) {
2362 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2363 "ASN1(EVP_PKEY_DSA) --> OK");
2368 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
2369 (u8
*) private_key_blob
,
2370 private_key_blob_len
) == 1) {
2371 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2372 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2377 if (tls_read_pkcs12_blob(ssl_ctx
, conn
->ssl
, private_key_blob
,
2378 private_key_blob_len
, passwd
) == 0) {
2379 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
2388 while (!ok
&& private_key
) {
2389 #ifndef OPENSSL_NO_STDIO
2390 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2391 SSL_FILETYPE_ASN1
) == 1) {
2392 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2393 "SSL_use_PrivateKey_File (DER) --> OK");
2398 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2399 SSL_FILETYPE_PEM
) == 1) {
2400 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2401 "SSL_use_PrivateKey_File (PEM) --> OK");
2405 #else /* OPENSSL_NO_STDIO */
2406 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2408 #endif /* OPENSSL_NO_STDIO */
2410 if (tls_read_pkcs12(ssl_ctx
, conn
->ssl
, private_key
, passwd
)
2412 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
2418 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
2419 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
2420 "access certificate store --> OK");
2429 tls_show_errors(MSG_INFO
, __func__
,
2430 "Failed to load private key");
2435 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2438 if (!SSL_check_private_key(conn
->ssl
)) {
2439 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
2444 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
2449 static int tls_global_private_key(SSL_CTX
*ssl_ctx
, const char *private_key
,
2450 const char *private_key_passwd
)
2454 if (private_key
== NULL
)
2457 if (private_key_passwd
) {
2458 passwd
= os_strdup(private_key_passwd
);
2464 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2465 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2467 #ifndef OPENSSL_NO_STDIO
2468 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2469 SSL_FILETYPE_ASN1
) != 1 &&
2470 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2471 SSL_FILETYPE_PEM
) != 1 &&
2472 #endif /* OPENSSL_NO_STDIO */
2473 tls_read_pkcs12(ssl_ctx
, NULL
, private_key
, passwd
)) {
2474 tls_show_errors(MSG_INFO
, __func__
,
2475 "Failed to load private key");
2482 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2484 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
2485 tls_show_errors(MSG_INFO
, __func__
,
2486 "Private key failed verification");
2494 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
2496 #ifdef OPENSSL_NO_DH
2497 if (dh_file
== NULL
)
2499 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2500 "dh_file specified");
2502 #else /* OPENSSL_NO_DH */
2506 /* TODO: add support for dh_blob */
2507 if (dh_file
== NULL
)
2512 bio
= BIO_new_file(dh_file
, "r");
2514 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2515 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2518 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2520 #ifndef OPENSSL_NO_DSA
2521 while (dh
== NULL
) {
2523 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2524 " trying to parse as DSA params", dh_file
,
2525 ERR_error_string(ERR_get_error(), NULL
));
2526 bio
= BIO_new_file(dh_file
, "r");
2529 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2532 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2533 "'%s': %s", dh_file
,
2534 ERR_error_string(ERR_get_error(), NULL
));
2538 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2539 dh
= DSA_dup_DH(dsa
);
2542 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2543 "params into DH params");
2548 #endif /* !OPENSSL_NO_DSA */
2550 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2555 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
2556 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2558 ERR_error_string(ERR_get_error(), NULL
));
2564 #endif /* OPENSSL_NO_DH */
2568 static int tls_global_dh(SSL_CTX
*ssl_ctx
, const char *dh_file
)
2570 #ifdef OPENSSL_NO_DH
2571 if (dh_file
== NULL
)
2573 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2574 "dh_file specified");
2576 #else /* OPENSSL_NO_DH */
2580 /* TODO: add support for dh_blob */
2581 if (dh_file
== NULL
)
2583 if (ssl_ctx
== NULL
)
2586 bio
= BIO_new_file(dh_file
, "r");
2588 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2589 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2592 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2594 #ifndef OPENSSL_NO_DSA
2595 while (dh
== NULL
) {
2597 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2598 " trying to parse as DSA params", dh_file
,
2599 ERR_error_string(ERR_get_error(), NULL
));
2600 bio
= BIO_new_file(dh_file
, "r");
2603 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2606 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2607 "'%s': %s", dh_file
,
2608 ERR_error_string(ERR_get_error(), NULL
));
2612 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2613 dh
= DSA_dup_DH(dsa
);
2616 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2617 "params into DH params");
2622 #endif /* !OPENSSL_NO_DSA */
2624 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2629 if (SSL_CTX_set_tmp_dh(ssl_ctx
, dh
) != 1) {
2630 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2632 ERR_error_string(ERR_get_error(), NULL
));
2638 #endif /* OPENSSL_NO_DH */
2642 int tls_connection_get_keys(void *ssl_ctx
, struct tls_connection
*conn
,
2643 struct tls_keys
*keys
)
2646 wpa_printf(MSG_ERROR
, "OpenSSL: TLS keys cannot be exported in FIPS "
2649 #else /* CONFIG_FIPS */
2652 if (conn
== NULL
|| keys
== NULL
)
2655 #if OPENSSL_VERSION_NUMBER < 0x10100000L
2656 if (ssl
== NULL
|| ssl
->s3
== NULL
|| ssl
->session
== NULL
)
2659 os_memset(keys
, 0, sizeof(*keys
));
2660 keys
->client_random
= ssl
->s3
->client_random
;
2661 keys
->client_random_len
= SSL3_RANDOM_SIZE
;
2662 keys
->server_random
= ssl
->s3
->server_random
;
2663 keys
->server_random_len
= SSL3_RANDOM_SIZE
;
2668 os_memset(keys
, 0, sizeof(*keys
));
2669 keys
->client_random
= conn
->client_random
;
2670 keys
->client_random_len
= SSL_get_client_random(
2671 ssl
, conn
->client_random
, sizeof(conn
->client_random
));
2672 keys
->server_random
= conn
->server_random
;
2673 keys
->server_random_len
= SSL_get_server_random(
2674 ssl
, conn
->server_random
, sizeof(conn
->server_random
));
2678 #endif /* CONFIG_FIPS */
2682 static int openssl_get_keyblock_size(SSL
*ssl
)
2684 #if OPENSSL_VERSION_NUMBER < 0x10100000L
2685 const EVP_CIPHER
*c
;
2689 if (ssl
->enc_read_ctx
== NULL
|| ssl
->enc_read_ctx
->cipher
== NULL
||
2690 ssl
->read_hash
== NULL
)
2693 c
= ssl
->enc_read_ctx
->cipher
;
2694 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
2695 h
= EVP_MD_CTX_md(ssl
->read_hash
);
2700 md_size
= EVP_MD_size(h
);
2701 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
2703 md_size
= ssl
->s3
->tmp
.new_mac_secret_size
;
2708 wpa_printf(MSG_DEBUG
, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
2709 "IV_len=%d", EVP_CIPHER_key_length(c
), md_size
,
2710 EVP_CIPHER_iv_length(c
));
2711 return 2 * (EVP_CIPHER_key_length(c
) +
2713 EVP_CIPHER_iv_length(c
));
2715 const SSL_CIPHER
*ssl_cipher
;
2717 const EVP_CIPHER
*c
;
2720 ssl_cipher
= SSL_get_current_cipher(ssl
);
2723 cipher
= SSL_CIPHER_get_cipher_nid(ssl_cipher
);
2724 digest
= SSL_CIPHER_get_digest_nid(ssl_cipher
);
2725 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher nid %d digest nid %d",
2727 if (cipher
< 0 || digest
< 0)
2729 c
= EVP_get_cipherbynid(cipher
);
2730 h
= EVP_get_digestbynid(digest
);
2734 wpa_printf(MSG_DEBUG
,
2735 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
2736 EVP_CIPHER_key_length(c
), EVP_MD_size(h
),
2737 EVP_CIPHER_iv_length(c
));
2738 return 2 * (EVP_CIPHER_key_length(c
) + EVP_MD_size(h
) +
2739 EVP_CIPHER_iv_length(c
));
2744 static int openssl_tls_prf(void *tls_ctx
, struct tls_connection
*conn
,
2745 const char *label
, int server_random_first
,
2746 int skip_keyblock
, u8
*out
, size_t out_len
)
2749 wpa_printf(MSG_ERROR
, "OpenSSL: TLS keys cannot be exported in FIPS "
2752 #else /* CONFIG_FIPS */
2753 #if OPENSSL_VERSION_NUMBER < 0x10100000L
2763 * TLS library did not support key generation, so get the needed TLS
2764 * session parameters and use an internal implementation of TLS PRF to
2771 if (ssl
== NULL
|| ssl
->s3
== NULL
|| ssl
->session
== NULL
||
2772 ssl
->session
->master_key_length
<= 0)
2774 ver
= SSL_get_version(ssl
);
2776 if (skip_keyblock
) {
2777 skip
= openssl_get_keyblock_size(ssl
);
2780 tmp_out
= os_malloc(skip
+ out_len
);
2786 rnd
= os_malloc(2 * SSL3_RANDOM_SIZE
);
2792 if (server_random_first
) {
2793 os_memcpy(rnd
, ssl
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2794 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, ssl
->s3
->client_random
,
2797 os_memcpy(rnd
, ssl
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2798 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, ssl
->s3
->server_random
,
2802 if (os_strcmp(ver
, "TLSv1.2") == 0) {
2803 tls_prf_sha256(ssl
->session
->master_key
,
2804 ssl
->session
->master_key_length
,
2805 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2806 _out
, skip
+ out_len
);
2808 } else if (tls_prf_sha1_md5(ssl
->session
->master_key
,
2809 ssl
->session
->master_key_length
,
2810 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2811 _out
, skip
+ out_len
) == 0) {
2815 if (ret
== 0 && skip_keyblock
)
2816 os_memcpy(out
, _out
+ skip
, out_len
);
2817 bin_clear_free(tmp_out
, skip
);
2828 unsigned char client_random
[SSL3_RANDOM_SIZE
];
2829 unsigned char server_random
[SSL3_RANDOM_SIZE
];
2830 unsigned char master_key
[64];
2831 size_t master_key_len
;
2835 * TLS library did not support key generation, so get the needed TLS
2836 * session parameters and use an internal implementation of TLS PRF to
2845 ver
= SSL_get_version(ssl
);
2846 sess
= SSL_get_session(ssl
);
2850 if (skip_keyblock
) {
2851 skip
= openssl_get_keyblock_size(ssl
);
2854 tmp_out
= os_malloc(skip
+ out_len
);
2860 rnd
= os_malloc(2 * SSL3_RANDOM_SIZE
);
2866 SSL_get_client_random(ssl
, client_random
, sizeof(client_random
));
2867 SSL_get_server_random(ssl
, server_random
, sizeof(server_random
));
2868 master_key_len
= SSL_SESSION_get_master_key(sess
, master_key
,
2869 sizeof(master_key
));
2871 if (server_random_first
) {
2872 os_memcpy(rnd
, server_random
, SSL3_RANDOM_SIZE
);
2873 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, client_random
,
2876 os_memcpy(rnd
, client_random
, SSL3_RANDOM_SIZE
);
2877 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, server_random
,
2881 if (os_strcmp(ver
, "TLSv1.2") == 0) {
2882 tls_prf_sha256(master_key
, master_key_len
,
2883 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2884 _out
, skip
+ out_len
);
2886 } else if (tls_prf_sha1_md5(master_key
, master_key_len
,
2887 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2888 _out
, skip
+ out_len
) == 0) {
2891 os_memset(master_key
, 0, sizeof(master_key
));
2893 if (ret
== 0 && skip_keyblock
)
2894 os_memcpy(out
, _out
+ skip
, out_len
);
2895 bin_clear_free(tmp_out
, skip
);
2899 #endif /* CONFIG_FIPS */
2903 int tls_connection_prf(void *tls_ctx
, struct tls_connection
*conn
,
2904 const char *label
, int server_random_first
,
2905 int skip_keyblock
, u8
*out
, size_t out_len
)
2907 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
2911 if (server_random_first
|| skip_keyblock
)
2912 return openssl_tls_prf(tls_ctx
, conn
, label
,
2913 server_random_first
, skip_keyblock
,
2916 if (SSL_export_keying_material(ssl
, out
, out_len
, label
,
2917 os_strlen(label
), NULL
, 0, 0) == 1) {
2918 wpa_printf(MSG_DEBUG
, "OpenSSL: Using internal PRF");
2922 return openssl_tls_prf(tls_ctx
, conn
, label
, server_random_first
,
2923 skip_keyblock
, out
, out_len
);
2927 static struct wpabuf
*
2928 openssl_handshake(struct tls_connection
*conn
, const struct wpabuf
*in_data
,
2932 struct wpabuf
*out_data
;
2935 * Give TLS handshake data from the server (if available) to OpenSSL
2938 if (in_data
&& wpabuf_len(in_data
) > 0 &&
2939 BIO_write(conn
->ssl_in
, wpabuf_head(in_data
), wpabuf_len(in_data
))
2941 tls_show_errors(MSG_INFO
, __func__
,
2942 "Handshake failed - BIO_write");
2946 /* Initiate TLS handshake or continue the existing handshake */
2948 res
= SSL_accept(conn
->ssl
);
2950 res
= SSL_connect(conn
->ssl
);
2952 int err
= SSL_get_error(conn
->ssl
, res
);
2953 if (err
== SSL_ERROR_WANT_READ
)
2954 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
2956 else if (err
== SSL_ERROR_WANT_WRITE
)
2957 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
2960 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
2965 /* Get the TLS handshake data to be sent to the server */
2966 res
= BIO_ctrl_pending(conn
->ssl_out
);
2967 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
2968 out_data
= wpabuf_alloc(res
);
2969 if (out_data
== NULL
) {
2970 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
2971 "handshake output (%d bytes)", res
);
2972 if (BIO_reset(conn
->ssl_out
) < 0) {
2973 tls_show_errors(MSG_INFO
, __func__
,
2974 "BIO_reset failed");
2978 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, wpabuf_mhead(out_data
),
2981 tls_show_errors(MSG_INFO
, __func__
,
2982 "Handshake failed - BIO_read");
2983 if (BIO_reset(conn
->ssl_out
) < 0) {
2984 tls_show_errors(MSG_INFO
, __func__
,
2985 "BIO_reset failed");
2987 wpabuf_free(out_data
);
2990 wpabuf_put(out_data
, res
);
2996 static struct wpabuf
*
2997 openssl_get_appl_data(struct tls_connection
*conn
, size_t max_len
)
2999 struct wpabuf
*appl_data
;
3002 appl_data
= wpabuf_alloc(max_len
+ 100);
3003 if (appl_data
== NULL
)
3006 res
= SSL_read(conn
->ssl
, wpabuf_mhead(appl_data
),
3007 wpabuf_size(appl_data
));
3009 int err
= SSL_get_error(conn
->ssl
, res
);
3010 if (err
== SSL_ERROR_WANT_READ
||
3011 err
== SSL_ERROR_WANT_WRITE
) {
3012 wpa_printf(MSG_DEBUG
, "SSL: No Application Data "
3015 tls_show_errors(MSG_INFO
, __func__
,
3016 "Failed to read possible "
3017 "Application Data");
3019 wpabuf_free(appl_data
);
3023 wpabuf_put(appl_data
, res
);
3024 wpa_hexdump_buf_key(MSG_MSGDUMP
, "SSL: Application Data in Finished "
3025 "message", appl_data
);
3031 static struct wpabuf
*
3032 openssl_connection_handshake(struct tls_connection
*conn
,
3033 const struct wpabuf
*in_data
,
3034 struct wpabuf
**appl_data
, int server
)
3036 struct wpabuf
*out_data
;
3041 out_data
= openssl_handshake(conn
, in_data
, server
);
3042 if (out_data
== NULL
)
3044 if (conn
->invalid_hb_used
) {
3045 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3046 wpabuf_free(out_data
);
3050 if (SSL_is_init_finished(conn
->ssl
) && appl_data
&& in_data
)
3051 *appl_data
= openssl_get_appl_data(conn
, wpabuf_len(in_data
));
3053 if (conn
->invalid_hb_used
) {
3054 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3056 wpabuf_free(*appl_data
);
3059 wpabuf_free(out_data
);
3068 tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
3069 const struct wpabuf
*in_data
,
3070 struct wpabuf
**appl_data
)
3072 return openssl_connection_handshake(conn
, in_data
, appl_data
, 0);
3076 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
3077 struct tls_connection
*conn
,
3078 const struct wpabuf
*in_data
,
3079 struct wpabuf
**appl_data
)
3081 return openssl_connection_handshake(conn
, in_data
, appl_data
, 1);
3085 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
3086 struct tls_connection
*conn
,
3087 const struct wpabuf
*in_data
)
3095 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3096 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
3097 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
3098 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3101 res
= SSL_write(conn
->ssl
, wpabuf_head(in_data
), wpabuf_len(in_data
));
3103 tls_show_errors(MSG_INFO
, __func__
,
3104 "Encryption failed - SSL_write");
3108 /* Read encrypted data to be sent to the server */
3109 buf
= wpabuf_alloc(wpabuf_len(in_data
) + 300);
3112 res
= BIO_read(conn
->ssl_out
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3114 tls_show_errors(MSG_INFO
, __func__
,
3115 "Encryption failed - BIO_read");
3119 wpabuf_put(buf
, res
);
3125 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
3126 struct tls_connection
*conn
,
3127 const struct wpabuf
*in_data
)
3132 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3133 res
= BIO_write(conn
->ssl_in
, wpabuf_head(in_data
),
3134 wpabuf_len(in_data
));
3136 tls_show_errors(MSG_INFO
, __func__
,
3137 "Decryption failed - BIO_write");
3140 if (BIO_reset(conn
->ssl_out
) < 0) {
3141 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3145 /* Read decrypted data for further processing */
3147 * Even though we try to disable TLS compression, it is possible that
3148 * this cannot be done with all TLS libraries. Add extra buffer space
3149 * to handle the possibility of the decrypted data being longer than
3152 buf
= wpabuf_alloc((wpabuf_len(in_data
) + 500) * 3);
3155 res
= SSL_read(conn
->ssl
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3157 tls_show_errors(MSG_INFO
, __func__
,
3158 "Decryption failed - SSL_read");
3162 wpabuf_put(buf
, res
);
3164 if (conn
->invalid_hb_used
) {
3165 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3174 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
3176 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
3177 return conn
? SSL_cache_hit(conn
->ssl
) : 0;
3179 return conn
? conn
->ssl
->hit
: 0;
3184 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
3187 char buf
[100], *pos
, *end
;
3191 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
3196 end
= pos
+ sizeof(buf
);
3199 while (*c
!= TLS_CIPHER_NONE
) {
3203 case TLS_CIPHER_RC4_SHA
:
3206 case TLS_CIPHER_AES128_SHA
:
3207 suite
= "AES128-SHA";
3209 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
3210 suite
= "DHE-RSA-AES128-SHA";
3212 case TLS_CIPHER_ANON_DH_AES128_SHA
:
3213 suite
= "ADH-AES128-SHA";
3216 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
3217 "cipher selection: %d", *c
);
3220 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
3221 if (os_snprintf_error(end
- pos
, ret
))
3228 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
3230 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
3231 tls_show_errors(MSG_INFO
, __func__
,
3232 "Cipher suite configuration failed");
3240 int tls_get_version(void *ssl_ctx
, struct tls_connection
*conn
,
3241 char *buf
, size_t buflen
)
3244 if (conn
== NULL
|| conn
->ssl
== NULL
)
3247 name
= SSL_get_version(conn
->ssl
);
3251 os_strlcpy(buf
, name
, buflen
);
3256 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
3257 char *buf
, size_t buflen
)
3260 if (conn
== NULL
|| conn
->ssl
== NULL
)
3263 name
= SSL_get_cipher(conn
->ssl
);
3267 os_strlcpy(buf
, name
, buflen
);
3272 int tls_connection_enable_workaround(void *ssl_ctx
,
3273 struct tls_connection
*conn
)
3275 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3281 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3282 /* ClientHello TLS extensions require a patch to openssl, so this function is
3283 * commented out unless explicitly needed for EAP-FAST in order to be able to
3284 * build this file with unmodified openssl. */
3285 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
3286 int ext_type
, const u8
*data
,
3289 if (conn
== NULL
|| conn
->ssl
== NULL
|| ext_type
!= 35)
3292 if (SSL_set_session_ticket_ext(conn
->ssl
, (void *) data
,
3298 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3301 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
3305 return conn
->failed
;
3309 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3313 return conn
->read_alerts
;
3317 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3321 return conn
->write_alerts
;
3327 static void ocsp_debug_print_resp(OCSP_RESPONSE
*rsp
)
3329 #ifndef CONFIG_NO_STDOUT_DEBUG
3335 if (wpa_debug_level
> MSG_DEBUG
)
3338 out
= BIO_new(BIO_s_mem());
3342 OCSP_RESPONSE_print(out
, rsp
, 0);
3343 rlen
= BIO_ctrl_pending(out
);
3344 txt
= os_malloc(rlen
+ 1);
3350 res
= BIO_read(out
, txt
, rlen
);
3353 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP Response\n%s", txt
);
3357 #endif /* CONFIG_NO_STDOUT_DEBUG */
3361 static void debug_print_cert(X509
*cert
, const char *title
)
3363 #ifndef CONFIG_NO_STDOUT_DEBUG
3369 if (wpa_debug_level
> MSG_DEBUG
)
3372 out
= BIO_new(BIO_s_mem());
3376 X509_print(out
, cert
);
3377 rlen
= BIO_ctrl_pending(out
);
3378 txt
= os_malloc(rlen
+ 1);
3384 res
= BIO_read(out
, txt
, rlen
);
3387 wpa_printf(MSG_DEBUG
, "OpenSSL: %s\n%s", title
, txt
);
3392 #endif /* CONFIG_NO_STDOUT_DEBUG */
3396 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3398 struct tls_connection
*conn
= arg
;
3399 const unsigned char *p
;
3400 int len
, status
, reason
;
3402 OCSP_BASICRESP
*basic
;
3404 ASN1_GENERALIZEDTIME
*produced_at
, *this_update
, *next_update
;
3406 STACK_OF(X509
) *certs
= NULL
;
3408 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3410 wpa_printf(MSG_DEBUG
, "OpenSSL: No OCSP response received");
3411 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3414 wpa_hexdump(MSG_DEBUG
, "OpenSSL: OCSP response", p
, len
);
3416 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3418 wpa_printf(MSG_INFO
, "OpenSSL: Failed to parse OCSP response");
3422 ocsp_debug_print_resp(rsp
);
3424 status
= OCSP_response_status(rsp
);
3425 if (status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
3426 wpa_printf(MSG_INFO
, "OpenSSL: OCSP responder error %d (%s)",
3427 status
, OCSP_response_status_str(status
));
3431 basic
= OCSP_response_get1_basic(rsp
);
3433 wpa_printf(MSG_INFO
, "OpenSSL: Could not find BasicOCSPResponse");
3437 store
= SSL_CTX_get_cert_store(conn
->ssl_ctx
);
3438 if (conn
->peer_issuer
) {
3439 debug_print_cert(conn
->peer_issuer
, "Add OCSP issuer");
3441 if (X509_STORE_add_cert(store
, conn
->peer_issuer
) != 1) {
3442 tls_show_errors(MSG_INFO
, __func__
,
3443 "OpenSSL: Could not add issuer to certificate store");
3445 certs
= sk_X509_new_null();
3448 cert
= X509_dup(conn
->peer_issuer
);
3449 if (cert
&& !sk_X509_push(certs
, cert
)) {
3452 "OpenSSL: Could not add issuer to OCSP responder trust store");
3454 sk_X509_free(certs
);
3457 if (certs
&& conn
->peer_issuer_issuer
) {
3458 cert
= X509_dup(conn
->peer_issuer_issuer
);
3459 if (cert
&& !sk_X509_push(certs
, cert
)) {
3462 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
3469 status
= OCSP_basic_verify(basic
, certs
, store
, OCSP_TRUSTOTHER
);
3470 sk_X509_pop_free(certs
, X509_free
);
3472 tls_show_errors(MSG_INFO
, __func__
,
3473 "OpenSSL: OCSP response failed verification");
3474 OCSP_BASICRESP_free(basic
);
3475 OCSP_RESPONSE_free(rsp
);
3479 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP response verification succeeded");
3481 if (!conn
->peer_cert
) {
3482 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer certificate not available for OCSP status check");
3483 OCSP_BASICRESP_free(basic
);
3484 OCSP_RESPONSE_free(rsp
);
3488 if (!conn
->peer_issuer
) {
3489 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer issuer certificate not available for OCSP status check");
3490 OCSP_BASICRESP_free(basic
);
3491 OCSP_RESPONSE_free(rsp
);
3495 id
= OCSP_cert_to_id(NULL
, conn
->peer_cert
, conn
->peer_issuer
);
3497 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not create OCSP certificate identifier");
3498 OCSP_BASICRESP_free(basic
);
3499 OCSP_RESPONSE_free(rsp
);
3503 if (!OCSP_resp_find_status(basic
, id
, &status
, &reason
, &produced_at
,
3504 &this_update
, &next_update
)) {
3505 wpa_printf(MSG_INFO
, "OpenSSL: Could not find current server certificate from OCSP response%s",
3506 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? "" :
3507 " (OCSP not required)");
3508 OCSP_BASICRESP_free(basic
);
3509 OCSP_RESPONSE_free(rsp
);
3510 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3513 if (!OCSP_check_validity(this_update
, next_update
, 5 * 60, -1)) {
3514 tls_show_errors(MSG_INFO
, __func__
,
3515 "OpenSSL: OCSP status times invalid");
3516 OCSP_BASICRESP_free(basic
);
3517 OCSP_RESPONSE_free(rsp
);
3521 OCSP_BASICRESP_free(basic
);
3522 OCSP_RESPONSE_free(rsp
);
3524 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status for server certificate: %s",
3525 OCSP_cert_status_str(status
));
3527 if (status
== V_OCSP_CERTSTATUS_GOOD
)
3529 if (status
== V_OCSP_CERTSTATUS_REVOKED
)
3531 if (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) {
3532 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP required");
3535 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
3540 static int ocsp_status_cb(SSL
*s
, void *arg
)
3546 if (tls_global
->ocsp_stapling_response
== NULL
) {
3547 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - no response configured");
3548 return SSL_TLSEXT_ERR_OK
;
3551 resp
= os_readfile(tls_global
->ocsp_stapling_response
, &len
);
3553 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - could not read response file");
3554 /* TODO: Build OCSPResponse with responseStatus = internalError
3556 return SSL_TLSEXT_ERR_OK
;
3558 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - send cached response");
3559 tmp
= OPENSSL_malloc(len
);
3562 return SSL_TLSEXT_ERR_ALERT_FATAL
;
3565 os_memcpy(tmp
, resp
, len
);
3567 SSL_set_tlsext_status_ocsp_resp(s
, tmp
, len
);
3569 return SSL_TLSEXT_ERR_OK
;
3572 #endif /* HAVE_OCSP */
3575 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
3576 const struct tls_connection_params
*params
)
3581 const char *key_id
= params
->key_id
;
3582 const char *cert_id
= params
->cert_id
;
3583 const char *ca_cert_id
= params
->ca_cert_id
;
3584 const char *engine_id
= params
->engine
? params
->engine_id
: NULL
;
3590 * If the engine isn't explicitly configured, and any of the
3591 * cert/key fields are actually PKCS#11 URIs, then automatically
3592 * use the PKCS#11 ENGINE.
3594 if (!engine_id
|| os_strcmp(engine_id
, "pkcs11") == 0)
3597 if (!key_id
&& params
->private_key
&& can_pkcs11
&&
3598 os_strncmp(params
->private_key
, "pkcs11:", 7) == 0) {
3600 key_id
= params
->private_key
;
3603 if (!cert_id
&& params
->client_cert
&& can_pkcs11
&&
3604 os_strncmp(params
->client_cert
, "pkcs11:", 7) == 0) {
3606 cert_id
= params
->client_cert
;
3609 if (!ca_cert_id
&& params
->ca_cert
&& can_pkcs11
&&
3610 os_strncmp(params
->ca_cert
, "pkcs11:", 7) == 0) {
3612 ca_cert_id
= params
->ca_cert
;
3615 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3616 if (can_pkcs11
== 2 && !engine_id
)
3617 engine_id
= "pkcs11";
3619 if (params
->flags
& TLS_CONN_EAP_FAST
) {
3620 wpa_printf(MSG_DEBUG
,
3621 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3622 if (SSL_set_ssl_method(conn
->ssl
, TLSv1_method()) != 1) {
3623 tls_show_errors(MSG_INFO
, __func__
,
3624 "Failed to set TLSv1_method() for EAP-FAST");
3629 while ((err
= ERR_get_error())) {
3630 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3631 __func__
, ERR_error_string(err
, NULL
));
3635 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
3636 ret
= tls_engine_init(conn
, engine_id
, params
->pin
,
3637 key_id
, cert_id
, ca_cert_id
);
3641 if (tls_connection_set_subject_match(conn
,
3642 params
->subject_match
,
3643 params
->altsubject_match
,
3644 params
->suffix_match
,
3645 params
->domain_match
))
3648 if (engine_id
&& ca_cert_id
) {
3649 if (tls_connection_engine_ca_cert(tls_ctx
, conn
,
3651 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3652 } else if (tls_connection_ca_cert(tls_ctx
, conn
, params
->ca_cert
,
3653 params
->ca_cert_blob
,
3654 params
->ca_cert_blob_len
,
3658 if (engine_id
&& cert_id
) {
3659 if (tls_connection_engine_client_cert(conn
, cert_id
))
3660 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3661 } else if (tls_connection_client_cert(conn
, params
->client_cert
,
3662 params
->client_cert_blob
,
3663 params
->client_cert_blob_len
))
3666 if (engine_id
&& key_id
) {
3667 wpa_printf(MSG_DEBUG
, "TLS: Using private key from engine");
3668 if (tls_connection_engine_private_key(conn
))
3669 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3670 } else if (tls_connection_private_key(tls_ctx
, conn
,
3671 params
->private_key
,
3672 params
->private_key_passwd
,
3673 params
->private_key_blob
,
3674 params
->private_key_blob_len
)) {
3675 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
3676 params
->private_key
);
3680 if (tls_connection_dh(conn
, params
->dh_file
)) {
3681 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
3686 if (params
->openssl_ciphers
&&
3687 SSL_set_cipher_list(conn
->ssl
, params
->openssl_ciphers
) != 1) {
3688 wpa_printf(MSG_INFO
,
3689 "OpenSSL: Failed to set cipher string '%s'",
3690 params
->openssl_ciphers
);
3694 #ifdef SSL_OP_NO_TICKET
3695 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3696 SSL_set_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3697 #ifdef SSL_clear_options
3699 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3700 #endif /* SSL_clear_options */
3701 #endif /* SSL_OP_NO_TICKET */
3703 #ifdef SSL_OP_NO_TLSv1
3704 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_0
)
3705 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1
);
3707 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1
);
3708 #endif /* SSL_OP_NO_TLSv1 */
3709 #ifdef SSL_OP_NO_TLSv1_1
3710 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_1
)
3711 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3713 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3714 #endif /* SSL_OP_NO_TLSv1_1 */
3715 #ifdef SSL_OP_NO_TLSv1_2
3716 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_2
)
3717 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3719 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3720 #endif /* SSL_OP_NO_TLSv1_2 */
3723 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
3724 SSL_CTX
*ssl_ctx
= tls_ctx
;
3725 SSL_set_tlsext_status_type(conn
->ssl
, TLSEXT_STATUSTYPE_ocsp
);
3726 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_resp_cb
);
3727 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, conn
);
3729 #endif /* HAVE_OCSP */
3731 conn
->flags
= params
->flags
;
3733 tls_get_errors(tls_ctx
);
3739 int tls_global_set_params(void *tls_ctx
,
3740 const struct tls_connection_params
*params
)
3742 SSL_CTX
*ssl_ctx
= tls_ctx
;
3745 while ((err
= ERR_get_error())) {
3746 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3747 __func__
, ERR_error_string(err
, NULL
));
3750 if (tls_global_ca_cert(ssl_ctx
, params
->ca_cert
) ||
3751 tls_global_client_cert(ssl_ctx
, params
->client_cert
) ||
3752 tls_global_private_key(ssl_ctx
, params
->private_key
,
3753 params
->private_key_passwd
) ||
3754 tls_global_dh(ssl_ctx
, params
->dh_file
)) {
3755 wpa_printf(MSG_INFO
, "TLS: Failed to set global parameters");
3759 if (params
->openssl_ciphers
&&
3760 SSL_CTX_set_cipher_list(ssl_ctx
, params
->openssl_ciphers
) != 1) {
3761 wpa_printf(MSG_INFO
,
3762 "OpenSSL: Failed to set cipher string '%s'",
3763 params
->openssl_ciphers
);
3767 #ifdef SSL_OP_NO_TICKET
3768 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3769 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3770 #ifdef SSL_CTX_clear_options
3772 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3773 #endif /* SSL_clear_options */
3774 #endif /* SSL_OP_NO_TICKET */
3777 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_status_cb
);
3778 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, ssl_ctx
);
3779 os_free(tls_global
->ocsp_stapling_response
);
3780 if (params
->ocsp_stapling_response
)
3781 tls_global
->ocsp_stapling_response
=
3782 os_strdup(params
->ocsp_stapling_response
);
3784 tls_global
->ocsp_stapling_response
= NULL
;
3785 #endif /* HAVE_OCSP */
3791 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3792 /* Pre-shared secred requires a patch to openssl, so this function is
3793 * commented out unless explicitly needed for EAP-FAST in order to be able to
3794 * build this file with unmodified openssl. */
3796 #ifdef OPENSSL_IS_BORINGSSL
3797 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3798 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3799 const SSL_CIPHER
**cipher
, void *arg
)
3800 #else /* OPENSSL_IS_BORINGSSL */
3801 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3802 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3803 SSL_CIPHER
**cipher
, void *arg
)
3804 #endif /* OPENSSL_IS_BORINGSSL */
3806 struct tls_connection
*conn
= arg
;
3809 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3810 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3813 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
3814 conn
->session_ticket
,
3815 conn
->session_ticket_len
,
3816 s
->s3
->client_random
,
3817 s
->s3
->server_random
, secret
);
3819 unsigned char client_random
[SSL3_RANDOM_SIZE
];
3820 unsigned char server_random
[SSL3_RANDOM_SIZE
];
3822 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3825 SSL_get_client_random(s
, client_random
, sizeof(client_random
));
3826 SSL_get_server_random(s
, server_random
, sizeof(server_random
));
3828 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
3829 conn
->session_ticket
,
3830 conn
->session_ticket_len
,
3832 server_random
, secret
);
3835 os_free(conn
->session_ticket
);
3836 conn
->session_ticket
= NULL
;
3841 *secret_len
= SSL_MAX_MASTER_KEY_LENGTH
;
3846 static int tls_session_ticket_ext_cb(SSL
*s
, const unsigned char *data
,
3849 struct tls_connection
*conn
= arg
;
3851 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3854 wpa_printf(MSG_DEBUG
, "OpenSSL: %s: length=%d", __func__
, len
);
3856 os_free(conn
->session_ticket
);
3857 conn
->session_ticket
= NULL
;
3859 wpa_hexdump(MSG_DEBUG
, "OpenSSL: ClientHello SessionTicket "
3860 "extension", data
, len
);
3862 conn
->session_ticket
= os_malloc(len
);
3863 if (conn
->session_ticket
== NULL
)
3866 os_memcpy(conn
->session_ticket
, data
, len
);
3867 conn
->session_ticket_len
= len
;
3871 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3874 int tls_connection_set_session_ticket_cb(void *tls_ctx
,
3875 struct tls_connection
*conn
,
3876 tls_session_ticket_cb cb
,
3879 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3880 conn
->session_ticket_cb
= cb
;
3881 conn
->session_ticket_cb_ctx
= ctx
;
3884 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
3887 SSL_set_session_ticket_ext_cb(conn
->ssl
,
3888 tls_session_ticket_ext_cb
, conn
);
3890 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
3892 SSL_set_session_ticket_ext_cb(conn
->ssl
, NULL
, NULL
);
3896 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3898 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3902 int tls_get_library_version(char *buf
, size_t buf_len
)
3904 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
3905 OPENSSL_VERSION_TEXT
,
3906 SSLeay_version(SSLEAY_VERSION
));