2 * SSL/TLS interface functions for OpenSSL
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
17 #ifndef CONFIG_SMARTCARD
18 #ifndef OPENSSL_NO_ENGINE
19 #define OPENSSL_NO_ENGINE
23 #include <openssl/ssl.h>
24 #include <openssl/err.h>
25 #include <openssl/pkcs12.h>
26 #include <openssl/x509v3.h>
27 #ifndef OPENSSL_NO_ENGINE
28 #include <openssl/engine.h>
29 #endif /* OPENSSL_NO_ENGINE */
34 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
35 #define OPENSSL_d2i_TYPE const unsigned char **
37 #define OPENSSL_d2i_TYPE unsigned char **
40 #ifdef SSL_F_SSL_SET_SESSION_TICKET_EXT
41 #ifdef SSL_OP_NO_TICKET
43 * Session ticket override patch was merged into OpenSSL 0.9.9 tree on
44 * 2008-11-15. This version uses a bit different API compared to the old patch.
46 #define CONFIG_OPENSSL_TICKET_OVERRIDE
50 static int tls_openssl_ref_count = 0;
52 struct tls_connection {
54 BIO *ssl_in, *ssl_out;
55 #ifndef OPENSSL_NO_ENGINE
56 ENGINE *engine; /* functional reference to the engine */
57 EVP_PKEY *private_key; /* the private key if using engine */
58 #endif /* OPENSSL_NO_ENGINE */
59 char *subject_match, *altsubject_match;
60 int read_alerts, write_alerts, failed;
62 tls_session_ticket_cb session_ticket_cb;
63 void *session_ticket_cb_ctx;
65 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
67 size_t session_ticket_len;
71 #ifdef CONFIG_NO_STDOUT_DEBUG
73 static void _tls_show_errors(void)
77 while ((err = ERR_get_error())) {
78 /* Just ignore the errors, since stdout is disabled */
81 #define tls_show_errors(l, f, t) _tls_show_errors()
83 #else /* CONFIG_NO_STDOUT_DEBUG */
85 static void tls_show_errors(int level, const char *func, const char *txt)
89 wpa_printf(level, "OpenSSL: %s - %s %s",
90 func, txt, ERR_error_string(ERR_get_error(), NULL));
92 while ((err = ERR_get_error())) {
93 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
94 ERR_error_string(err, NULL));
98 #endif /* CONFIG_NO_STDOUT_DEBUG */
101 #ifdef CONFIG_NATIVE_WINDOWS
103 /* Windows CryptoAPI and access to certificate stores */
104 #include <wincrypt.h>
106 #ifdef __MINGW32_VERSION
108 * MinGW does not yet include all the needed definitions for CryptoAPI, so
109 * define here whatever extra is needed.
111 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
112 #define CERT_STORE_READONLY_FLAG 0x00008000
113 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
115 #endif /* __MINGW32_VERSION */
118 struct cryptoapi_rsa_data {
119 const CERT_CONTEXT *cert;
120 HCRYPTPROV crypt_prov;
122 BOOL free_crypt_prov;
126 static void cryptoapi_error(const char *msg)
128 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
129 msg, (unsigned int) GetLastError());
133 static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
134 unsigned char *to, RSA *rsa, int padding)
136 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
141 static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
142 unsigned char *to, RSA *rsa, int padding)
144 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
149 static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
150 unsigned char *to, RSA *rsa, int padding)
152 struct cryptoapi_rsa_data *priv =
153 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
155 DWORD hash_size, len, i;
156 unsigned char *buf = NULL;
160 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
161 ERR_R_PASSED_NULL_PARAMETER);
165 if (padding != RSA_PKCS1_PADDING) {
166 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
167 RSA_R_UNKNOWN_PADDING_TYPE);
171 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
172 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
174 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
175 RSA_R_INVALID_MESSAGE_LENGTH);
179 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
181 cryptoapi_error("CryptCreateHash failed");
185 len = sizeof(hash_size);
186 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
188 cryptoapi_error("CryptGetHashParam failed");
192 if ((int) hash_size != flen) {
193 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
194 (unsigned) hash_size, flen);
195 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
196 RSA_R_INVALID_MESSAGE_LENGTH);
199 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
200 cryptoapi_error("CryptSetHashParam failed");
205 buf = os_malloc(len);
207 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
211 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
212 cryptoapi_error("CryptSignHash failed");
216 for (i = 0; i < len; i++)
217 to[i] = buf[len - i - 1];
222 CryptDestroyHash(hash);
228 static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
229 unsigned char *to, RSA *rsa, int padding)
231 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
236 static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
240 if (priv->crypt_prov && priv->free_crypt_prov)
241 CryptReleaseContext(priv->crypt_prov, 0);
243 CertFreeCertificateContext(priv->cert);
248 static int cryptoapi_finish(RSA *rsa)
250 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
251 os_free((void *) rsa->meth);
257 static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
260 const CERT_CONTEXT *ret = NULL;
262 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
263 store | CERT_STORE_OPEN_EXISTING_FLAG |
264 CERT_STORE_READONLY_FLAG, L"MY");
266 cryptoapi_error("Failed to open 'My system store'");
270 if (strncmp(name, "cert://", 7) == 0) {
271 unsigned short wbuf[255];
272 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
273 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
275 0, CERT_FIND_SUBJECT_STR,
277 } else if (strncmp(name, "hash://", 7) == 0) {
278 CRYPT_HASH_BLOB blob;
280 const char *hash = name + 7;
283 len = os_strlen(hash) / 2;
284 buf = os_malloc(len);
285 if (buf && hexstr2bin(hash, buf, len) == 0) {
288 ret = CertFindCertificateInStore(cs,
297 CertCloseStore(cs, 0);
303 static int tls_cryptoapi_cert(SSL *ssl, const char *name)
306 RSA *rsa = NULL, *pub_rsa;
307 struct cryptoapi_rsa_data *priv;
308 RSA_METHOD *rsa_meth;
311 (strncmp(name, "cert://", 7) != 0 &&
312 strncmp(name, "hash://", 7) != 0))
315 priv = os_zalloc(sizeof(*priv));
316 rsa_meth = os_zalloc(sizeof(*rsa_meth));
317 if (priv == NULL || rsa_meth == NULL) {
318 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
319 "for CryptoAPI RSA method");
325 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
326 if (priv->cert == NULL) {
327 priv->cert = cryptoapi_find_cert(
328 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
330 if (priv->cert == NULL) {
331 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
336 cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &priv->cert->pbCertEncoded,
337 priv->cert->cbCertEncoded);
339 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
344 if (!CryptAcquireCertificatePrivateKey(priv->cert,
345 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
346 NULL, &priv->crypt_prov,
348 &priv->free_crypt_prov)) {
349 cryptoapi_error("Failed to acquire a private key for the "
354 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
355 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
356 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
357 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
358 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
359 rsa_meth->finish = cryptoapi_finish;
360 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
361 rsa_meth->app_data = (char *) priv;
365 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
366 ERR_R_MALLOC_FAILURE);
370 if (!SSL_use_certificate(ssl, cert)) {
375 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
379 rsa->n = BN_dup(pub_rsa->n);
380 rsa->e = BN_dup(pub_rsa->e);
381 if (!RSA_set_method(rsa, rsa_meth))
384 if (!SSL_use_RSAPrivateKey(ssl, rsa))
397 cryptoapi_free_data(priv);
403 static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
406 PCCERT_CONTEXT ctx = NULL;
414 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
419 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
422 wsprintf(wstore, L"%S", store);
423 cs = CertOpenSystemStore(0, wstore);
426 cs = CertOpenSystemStore(0, store);
429 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
430 "'%s': error=%d", __func__, store,
431 (int) GetLastError());
435 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
436 cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ctx->pbCertEncoded,
439 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
440 "X509 DER encoding for CA cert");
444 X509_NAME_oneline(X509_get_subject_name(cert), buf,
446 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
447 "system certificate store: subject='%s'", buf);
449 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
450 tls_show_errors(MSG_WARNING, __func__,
451 "Failed to add ca_cert to OpenSSL "
452 "certificate store");
458 if (!CertCloseStore(cs, 0)) {
459 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
460 "'%s': error=%d", __func__, name + 13,
461 (int) GetLastError());
468 #else /* CONFIG_NATIVE_WINDOWS */
470 static int tls_cryptoapi_cert(SSL *ssl, const char *name)
475 #endif /* CONFIG_NATIVE_WINDOWS */
478 static void ssl_info_cb(const SSL *ssl, int where, int ret)
483 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
484 w = where & ~SSL_ST_MASK;
485 if (w & SSL_ST_CONNECT)
487 else if (w & SSL_ST_ACCEPT)
492 if (where & SSL_CB_LOOP) {
493 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
494 str, SSL_state_string_long(ssl));
495 } else if (where & SSL_CB_ALERT) {
496 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
497 where & SSL_CB_READ ?
498 "read (remote end reported an error)" :
499 "write (local SSL3 detected an error)",
500 SSL_alert_type_string_long(ret),
501 SSL_alert_desc_string_long(ret));
502 if ((ret >> 8) == SSL3_AL_FATAL) {
503 struct tls_connection *conn =
504 SSL_get_app_data((SSL *) ssl);
505 if (where & SSL_CB_READ)
508 conn->write_alerts++;
510 } else if (where & SSL_CB_EXIT && ret <= 0) {
511 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
512 str, ret == 0 ? "failed" : "error",
513 SSL_state_string_long(ssl));
518 #ifndef OPENSSL_NO_ENGINE
520 * tls_engine_load_dynamic_generic - load any openssl engine
521 * @pre: an array of commands and values that load an engine initialized
522 * in the engine specific function
523 * @post: an array of commands and values that initialize an already loaded
524 * engine (or %NULL if not required)
525 * @id: the engine id of the engine to load (only required if post is not %NULL
527 * This function is a generic function that loads any openssl engine.
529 * Returns: 0 on success, -1 on failure
531 static int tls_engine_load_dynamic_generic(const char *pre[],
532 const char *post[], const char *id)
535 const char *dynamic_id = "dynamic";
537 engine = ENGINE_by_id(id);
540 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
546 engine = ENGINE_by_id(dynamic_id);
547 if (engine == NULL) {
548 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
550 ERR_error_string(ERR_get_error(), NULL));
554 /* Perform the pre commands. This will load the engine. */
555 while (pre && pre[0]) {
556 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
557 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
558 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
559 "%s %s [%s]", pre[0], pre[1],
560 ERR_error_string(ERR_get_error(), NULL));
568 * Free the reference to the "dynamic" engine. The loaded engine can
569 * now be looked up using ENGINE_by_id().
573 engine = ENGINE_by_id(id);
574 if (engine == NULL) {
575 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
576 id, ERR_error_string(ERR_get_error(), NULL));
580 while (post && post[0]) {
581 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
582 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
583 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
584 " %s %s [%s]", post[0], post[1],
585 ERR_error_string(ERR_get_error(), NULL));
586 ENGINE_remove(engine);
599 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
600 * @pkcs11_so_path: pksc11_so_path from the configuration
601 * @pcks11_module_path: pkcs11_module_path from the configuration
603 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
604 const char *pkcs11_module_path)
606 char *engine_id = "pkcs11";
607 const char *pre_cmd[] = {
608 "SO_PATH", NULL /* pkcs11_so_path */,
609 "ID", NULL /* engine_id */,
611 /* "NO_VCHECK", "1", */
615 const char *post_cmd[] = {
616 "MODULE_PATH", NULL /* pkcs11_module_path */,
620 if (!pkcs11_so_path || !pkcs11_module_path)
623 pre_cmd[1] = pkcs11_so_path;
624 pre_cmd[3] = engine_id;
625 post_cmd[1] = pkcs11_module_path;
627 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
630 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
635 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
636 * @opensc_so_path: opensc_so_path from the configuration
638 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
640 char *engine_id = "opensc";
641 const char *pre_cmd[] = {
642 "SO_PATH", NULL /* opensc_so_path */,
643 "ID", NULL /* engine_id */,
652 pre_cmd[1] = opensc_so_path;
653 pre_cmd[3] = engine_id;
655 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
658 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
660 #endif /* OPENSSL_NO_ENGINE */
663 void * tls_init(const struct tls_config *conf)
667 if (tls_openssl_ref_count == 0) {
670 if (conf && conf->fips_mode) {
671 if (!FIPS_mode_set(1)) {
672 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
674 ERR_load_crypto_strings();
675 ERR_print_errors_fp(stderr);
678 wpa_printf(MSG_INFO, "Running in FIPS mode");
680 #else /* OPENSSL_FIPS */
681 if (conf && conf->fips_mode) {
682 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
686 #endif /* OPENSSL_FIPS */
687 #endif /* CONFIG_FIPS */
688 SSL_load_error_strings();
690 #ifndef OPENSSL_NO_SHA256
691 EVP_add_digest(EVP_sha256());
692 #endif /* OPENSSL_NO_SHA256 */
693 /* TODO: if /dev/urandom is available, PRNG is seeded
694 * automatically. If this is not the case, random data should
698 #ifndef OPENSSL_NO_RC2
700 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
701 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
702 * versions, but it looks like OpenSSL 1.0.0 does not do that
705 EVP_add_cipher(EVP_rc2_40_cbc());
706 #endif /* OPENSSL_NO_RC2 */
708 #endif /* PKCS12_FUNCS */
710 tls_openssl_ref_count++;
712 ssl = SSL_CTX_new(TLSv1_method());
716 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
718 #ifndef OPENSSL_NO_ENGINE
720 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
721 conf->pkcs11_module_path)) {
722 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
723 ERR_load_ENGINE_strings();
724 ENGINE_load_dynamic();
726 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
727 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
728 conf->pkcs11_module_path)) {
733 #endif /* OPENSSL_NO_ENGINE */
739 void tls_deinit(void *ssl_ctx)
741 SSL_CTX *ssl = ssl_ctx;
744 tls_openssl_ref_count--;
745 if (tls_openssl_ref_count == 0) {
746 #ifndef OPENSSL_NO_ENGINE
748 #endif /* OPENSSL_NO_ENGINE */
749 CRYPTO_cleanup_all_ex_data();
757 static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
758 const char *pin, const char *key_id,
759 const char *cert_id, const char *ca_cert_id)
761 #ifndef OPENSSL_NO_ENGINE
763 if (engine_id == NULL) {
764 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
768 wpa_printf(MSG_ERROR, "ENGINE: Smartcard PIN not set");
771 if (key_id == NULL) {
772 wpa_printf(MSG_ERROR, "ENGINE: Key Id not set");
777 conn->engine = ENGINE_by_id(engine_id);
779 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
780 engine_id, ERR_error_string(ERR_get_error(), NULL));
783 if (ENGINE_init(conn->engine) != 1) {
784 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
785 "(engine: %s) [%s]", engine_id,
786 ERR_error_string(ERR_get_error(), NULL));
789 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
791 if (ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
792 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
793 ERR_error_string(ERR_get_error(), NULL));
796 /* load private key first in-case PIN is required for cert */
797 conn->private_key = ENGINE_load_private_key(conn->engine,
799 if (!conn->private_key) {
800 wpa_printf(MSG_ERROR, "ENGINE: cannot load private key with id"
801 " '%s' [%s]", key_id,
802 ERR_error_string(ERR_get_error(), NULL));
803 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
807 /* handle a certificate and/or CA certificate */
808 if (cert_id || ca_cert_id) {
809 const char *cmd_name = "LOAD_CERT_CTRL";
811 /* test if the engine supports a LOAD_CERT_CTRL */
812 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
813 0, (void *)cmd_name, NULL)) {
814 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
815 " loading certificates");
816 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
825 ENGINE_free(conn->engine);
829 if (conn->private_key) {
830 EVP_PKEY_free(conn->private_key);
831 conn->private_key = NULL;
835 #else /* OPENSSL_NO_ENGINE */
837 #endif /* OPENSSL_NO_ENGINE */
841 static void tls_engine_deinit(struct tls_connection *conn)
843 #ifndef OPENSSL_NO_ENGINE
844 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
845 if (conn->private_key) {
846 EVP_PKEY_free(conn->private_key);
847 conn->private_key = NULL;
850 ENGINE_finish(conn->engine);
853 #endif /* OPENSSL_NO_ENGINE */
857 int tls_get_errors(void *ssl_ctx)
862 while ((err = ERR_get_error())) {
863 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
864 ERR_error_string(err, NULL));
871 struct tls_connection * tls_connection_init(void *ssl_ctx)
873 SSL_CTX *ssl = ssl_ctx;
874 struct tls_connection *conn;
877 conn = os_zalloc(sizeof(*conn));
880 conn->ssl = SSL_new(ssl);
881 if (conn->ssl == NULL) {
882 tls_show_errors(MSG_INFO, __func__,
883 "Failed to initialize new SSL connection");
888 SSL_set_app_data(conn->ssl, conn);
889 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
890 SSL_OP_SINGLE_DH_USE;
891 #ifdef SSL_OP_NO_COMPRESSION
892 options |= SSL_OP_NO_COMPRESSION;
893 #endif /* SSL_OP_NO_COMPRESSION */
894 SSL_set_options(conn->ssl, options);
896 conn->ssl_in = BIO_new(BIO_s_mem());
898 tls_show_errors(MSG_INFO, __func__,
899 "Failed to create a new BIO for ssl_in");
905 conn->ssl_out = BIO_new(BIO_s_mem());
906 if (!conn->ssl_out) {
907 tls_show_errors(MSG_INFO, __func__,
908 "Failed to create a new BIO for ssl_out");
910 BIO_free(conn->ssl_in);
915 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
921 void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
926 tls_engine_deinit(conn);
927 os_free(conn->subject_match);
928 os_free(conn->altsubject_match);
929 os_free(conn->session_ticket);
934 int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
936 return conn ? SSL_is_init_finished(conn->ssl) : 0;
940 int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
945 /* Shutdown previous TLS connection without notifying the peer
946 * because the connection was already terminated in practice
947 * and "close notify" shutdown alert would confuse AS. */
948 SSL_set_quiet_shutdown(conn->ssl, 1);
949 SSL_shutdown(conn->ssl);
954 static int tls_match_altsubject_component(X509 *cert, int type,
955 const char *value, size_t len)
961 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
963 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
964 gen = sk_GENERAL_NAME_value(ext, i);
965 if (gen->type != type)
967 if (os_strlen((char *) gen->d.ia5->data) == len &&
968 os_memcmp(value, gen->d.ia5->data, len) == 0)
976 static int tls_match_altsubject(X509 *cert, const char *match)
979 const char *pos, *end;
984 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
987 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
990 } else if (os_strncmp(pos, "URI:", 4) == 0) {
994 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
998 end = os_strchr(pos, ';');
1000 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1001 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1002 os_strncmp(end + 1, "URI:", 4) == 0)
1004 end = os_strchr(end + 1, ';');
1009 len = os_strlen(pos);
1010 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1019 static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1025 struct tls_connection *conn;
1026 char *match, *altmatch;
1028 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
1029 err = X509_STORE_CTX_get_error(x509_ctx);
1030 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1031 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1032 SSL_get_ex_data_X509_STORE_CTX_idx());
1033 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1035 conn = SSL_get_app_data(ssl);
1036 match = conn ? conn->subject_match : NULL;
1037 altmatch = conn ? conn->altsubject_match : NULL;
1039 if (!preverify_ok) {
1040 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1041 " error %d (%s) depth %d for '%s'", err,
1042 X509_verify_cert_error_string(err), depth, buf);
1044 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - "
1045 "preverify_ok=%d err=%d (%s) depth=%d buf='%s'",
1047 X509_verify_cert_error_string(err), depth, buf);
1048 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1049 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1050 "match with '%s'", buf, match);
1052 } else if (depth == 0 && altmatch &&
1053 !tls_match_altsubject(err_cert, altmatch)) {
1054 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1055 "'%s' not found", altmatch);
1060 return preverify_ok;
1064 #ifndef OPENSSL_NO_STDIO
1065 static int tls_load_ca_der(void *_ssl_ctx, const char *ca_cert)
1067 SSL_CTX *ssl_ctx = _ssl_ctx;
1068 X509_LOOKUP *lookup;
1071 lookup = X509_STORE_add_lookup(ssl_ctx->cert_store,
1072 X509_LOOKUP_file());
1073 if (lookup == NULL) {
1074 tls_show_errors(MSG_WARNING, __func__,
1075 "Failed add lookup for X509 store");
1079 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1080 unsigned long err = ERR_peek_error();
1081 tls_show_errors(MSG_WARNING, __func__,
1082 "Failed load CA in DER format");
1083 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1084 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1085 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1086 "cert already in hash table error",
1094 #endif /* OPENSSL_NO_STDIO */
1097 static int tls_connection_ca_cert(void *_ssl_ctx, struct tls_connection *conn,
1098 const char *ca_cert, const u8 *ca_cert_blob,
1099 size_t ca_cert_blob_len, const char *ca_path)
1101 SSL_CTX *ssl_ctx = _ssl_ctx;
1104 * Remove previously configured trusted CA certificates before adding
1107 X509_STORE_free(ssl_ctx->cert_store);
1108 ssl_ctx->cert_store = X509_STORE_new();
1109 if (ssl_ctx->cert_store == NULL) {
1110 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1111 "certificate store", __func__);
1116 X509 *cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ca_cert_blob,
1119 tls_show_errors(MSG_WARNING, __func__,
1120 "Failed to parse ca_cert_blob");
1124 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
1125 unsigned long err = ERR_peek_error();
1126 tls_show_errors(MSG_WARNING, __func__,
1127 "Failed to add ca_cert_blob to "
1128 "certificate store");
1129 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1130 ERR_GET_REASON(err) ==
1131 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1132 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1133 "cert already in hash table error",
1141 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
1142 "to certificate store", __func__);
1143 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1147 #ifdef CONFIG_NATIVE_WINDOWS
1148 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
1150 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
1151 "system certificate store");
1152 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1155 #endif /* CONFIG_NATIVE_WINDOWS */
1157 if (ca_cert || ca_path) {
1158 #ifndef OPENSSL_NO_STDIO
1159 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
1161 tls_show_errors(MSG_WARNING, __func__,
1162 "Failed to load root certificates");
1164 tls_load_ca_der(ssl_ctx, ca_cert) == 0) {
1165 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
1166 "DER format CA certificate",
1171 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1172 "certificate(s) loaded");
1173 tls_get_errors(ssl_ctx);
1175 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1176 #else /* OPENSSL_NO_STDIO */
1177 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1180 #endif /* OPENSSL_NO_STDIO */
1182 /* No ca_cert configured - do not try to verify server
1184 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1191 static int tls_global_ca_cert(SSL_CTX *ssl_ctx, const char *ca_cert)
1194 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
1196 tls_show_errors(MSG_WARNING, __func__,
1197 "Failed to load root certificates");
1201 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1202 "certificate(s) loaded");
1204 #ifndef OPENSSL_NO_STDIO
1205 /* Add the same CAs to the client certificate requests */
1206 SSL_CTX_set_client_CA_list(ssl_ctx,
1207 SSL_load_client_CA_file(ca_cert));
1208 #endif /* OPENSSL_NO_STDIO */
1215 int tls_global_set_verify(void *ssl_ctx, int check_crl)
1220 X509_STORE *cs = SSL_CTX_get_cert_store(ssl_ctx);
1222 tls_show_errors(MSG_INFO, __func__, "Failed to get "
1223 "certificate store when enabling "
1227 flags = X509_V_FLAG_CRL_CHECK;
1229 flags |= X509_V_FLAG_CRL_CHECK_ALL;
1230 X509_STORE_set_flags(cs, flags);
1236 static int tls_connection_set_subject_match(struct tls_connection *conn,
1237 const char *subject_match,
1238 const char *altsubject_match)
1240 os_free(conn->subject_match);
1241 conn->subject_match = NULL;
1242 if (subject_match) {
1243 conn->subject_match = os_strdup(subject_match);
1244 if (conn->subject_match == NULL)
1248 os_free(conn->altsubject_match);
1249 conn->altsubject_match = NULL;
1250 if (altsubject_match) {
1251 conn->altsubject_match = os_strdup(altsubject_match);
1252 if (conn->altsubject_match == NULL)
1260 int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
1263 static int counter = 0;
1269 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
1270 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
1271 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
1273 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1276 SSL_set_accept_state(conn->ssl);
1279 * Set session id context in order to avoid fatal errors when client
1280 * tries to resume a session. However, set the context to a unique
1281 * value in order to effectively disable session resumption for now
1282 * since not all areas of the server code are ready for it (e.g.,
1283 * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
1287 SSL_set_session_id_context(conn->ssl,
1288 (const unsigned char *) &counter,
1295 static int tls_connection_client_cert(struct tls_connection *conn,
1296 const char *client_cert,
1297 const u8 *client_cert_blob,
1298 size_t client_cert_blob_len)
1300 if (client_cert == NULL && client_cert_blob == NULL)
1303 if (client_cert_blob &&
1304 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
1305 client_cert_blob_len) == 1) {
1306 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
1309 } else if (client_cert_blob) {
1310 tls_show_errors(MSG_DEBUG, __func__,
1311 "SSL_use_certificate_ASN1 failed");
1314 if (client_cert == NULL)
1317 #ifndef OPENSSL_NO_STDIO
1318 if (SSL_use_certificate_file(conn->ssl, client_cert,
1319 SSL_FILETYPE_ASN1) == 1) {
1320 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
1324 tls_show_errors(MSG_DEBUG, __func__,
1325 "SSL_use_certificate_file (DER) failed");
1328 if (SSL_use_certificate_file(conn->ssl, client_cert,
1329 SSL_FILETYPE_PEM) == 1) {
1330 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
1334 tls_show_errors(MSG_DEBUG, __func__,
1335 "SSL_use_certificate_file (PEM) failed");
1337 #else /* OPENSSL_NO_STDIO */
1338 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1339 #endif /* OPENSSL_NO_STDIO */
1345 static int tls_global_client_cert(SSL_CTX *ssl_ctx, const char *client_cert)
1347 #ifndef OPENSSL_NO_STDIO
1348 if (client_cert == NULL)
1351 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1352 SSL_FILETYPE_ASN1) != 1 &&
1353 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1354 SSL_FILETYPE_PEM) != 1) {
1355 tls_show_errors(MSG_INFO, __func__,
1356 "Failed to load client certificate");
1360 #else /* OPENSSL_NO_STDIO */
1361 if (client_cert == NULL)
1363 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1365 #endif /* OPENSSL_NO_STDIO */
1369 static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
1371 if (password == NULL) {
1374 os_strlcpy(buf, (char *) password, size);
1375 return os_strlen(buf);
1380 static int tls_parse_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, PKCS12 *p12,
1385 STACK_OF(X509) *certs;
1392 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
1393 tls_show_errors(MSG_DEBUG, __func__,
1394 "Failed to parse PKCS12 file");
1398 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
1401 X509_NAME_oneline(X509_get_subject_name(cert), buf,
1403 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
1404 "subject='%s'", buf);
1406 if (SSL_use_certificate(ssl, cert) != 1)
1409 if (SSL_CTX_use_certificate(ssl_ctx, cert) != 1)
1416 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
1418 if (SSL_use_PrivateKey(ssl, pkey) != 1)
1421 if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1)
1424 EVP_PKEY_free(pkey);
1428 while ((cert = sk_X509_pop(certs)) != NULL) {
1429 X509_NAME_oneline(X509_get_subject_name(cert), buf,
1431 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
1432 " from PKCS12: subject='%s'", buf);
1434 * There is no SSL equivalent for the chain cert - so
1435 * always add it to the context...
1437 if (SSL_CTX_add_extra_chain_cert(ssl_ctx, cert) != 1) {
1442 sk_X509_free(certs);
1448 tls_get_errors(ssl_ctx);
1452 #endif /* PKCS12_FUNCS */
1455 static int tls_read_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, const char *private_key,
1462 f = fopen(private_key, "rb");
1466 p12 = d2i_PKCS12_fp(f, NULL);
1470 tls_show_errors(MSG_INFO, __func__,
1471 "Failed to use PKCS#12 file");
1475 return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1477 #else /* PKCS12_FUNCS */
1478 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
1481 #endif /* PKCS12_FUNCS */
1485 static int tls_read_pkcs12_blob(SSL_CTX *ssl_ctx, SSL *ssl,
1486 const u8 *blob, size_t len, const char *passwd)
1491 p12 = d2i_PKCS12(NULL, (OPENSSL_d2i_TYPE) &blob, len);
1493 tls_show_errors(MSG_INFO, __func__,
1494 "Failed to use PKCS#12 blob");
1498 return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1500 #else /* PKCS12_FUNCS */
1501 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
1504 #endif /* PKCS12_FUNCS */
1508 #ifndef OPENSSL_NO_ENGINE
1509 static int tls_engine_get_cert(struct tls_connection *conn,
1510 const char *cert_id,
1513 /* this runs after the private key is loaded so no PIN is required */
1515 const char *cert_id;
1518 params.cert_id = cert_id;
1521 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
1522 0, ¶ms, NULL, 1)) {
1523 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
1524 " '%s' [%s]", cert_id,
1525 ERR_error_string(ERR_get_error(), NULL));
1526 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1529 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
1531 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1533 *cert = params.cert;
1536 #endif /* OPENSSL_NO_ENGINE */
1539 static int tls_connection_engine_client_cert(struct tls_connection *conn,
1540 const char *cert_id)
1542 #ifndef OPENSSL_NO_ENGINE
1545 if (tls_engine_get_cert(conn, cert_id, &cert))
1548 if (!SSL_use_certificate(conn->ssl, cert)) {
1549 tls_show_errors(MSG_ERROR, __func__,
1550 "SSL_use_certificate failed");
1555 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
1559 #else /* OPENSSL_NO_ENGINE */
1561 #endif /* OPENSSL_NO_ENGINE */
1565 static int tls_connection_engine_ca_cert(void *_ssl_ctx,
1566 struct tls_connection *conn,
1567 const char *ca_cert_id)
1569 #ifndef OPENSSL_NO_ENGINE
1571 SSL_CTX *ssl_ctx = _ssl_ctx;
1573 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
1576 /* start off the same as tls_connection_ca_cert */
1577 X509_STORE_free(ssl_ctx->cert_store);
1578 ssl_ctx->cert_store = X509_STORE_new();
1579 if (ssl_ctx->cert_store == NULL) {
1580 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1581 "certificate store", __func__);
1585 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
1586 unsigned long err = ERR_peek_error();
1587 tls_show_errors(MSG_WARNING, __func__,
1588 "Failed to add CA certificate from engine "
1589 "to certificate store");
1590 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1591 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1592 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
1593 " already in hash table error",
1601 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
1602 "to certificate store", __func__);
1603 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1606 #else /* OPENSSL_NO_ENGINE */
1608 #endif /* OPENSSL_NO_ENGINE */
1612 static int tls_connection_engine_private_key(struct tls_connection *conn)
1614 #ifndef OPENSSL_NO_ENGINE
1615 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
1616 tls_show_errors(MSG_ERROR, __func__,
1617 "ENGINE: cannot use private key for TLS");
1620 if (!SSL_check_private_key(conn->ssl)) {
1621 tls_show_errors(MSG_INFO, __func__,
1622 "Private key failed verification");
1626 #else /* OPENSSL_NO_ENGINE */
1627 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
1628 "engine support was not compiled in");
1630 #endif /* OPENSSL_NO_ENGINE */
1634 static int tls_connection_private_key(void *_ssl_ctx,
1635 struct tls_connection *conn,
1636 const char *private_key,
1637 const char *private_key_passwd,
1638 const u8 *private_key_blob,
1639 size_t private_key_blob_len)
1641 SSL_CTX *ssl_ctx = _ssl_ctx;
1645 if (private_key == NULL && private_key_blob == NULL)
1648 if (private_key_passwd) {
1649 passwd = os_strdup(private_key_passwd);
1655 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1656 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1659 while (private_key_blob) {
1660 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
1661 (u8 *) private_key_blob,
1662 private_key_blob_len) == 1) {
1663 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1664 "ASN1(EVP_PKEY_RSA) --> OK");
1668 tls_show_errors(MSG_DEBUG, __func__,
1669 "SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA)"
1673 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
1674 (u8 *) private_key_blob,
1675 private_key_blob_len) == 1) {
1676 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1677 "ASN1(EVP_PKEY_DSA) --> OK");
1681 tls_show_errors(MSG_DEBUG, __func__,
1682 "SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA)"
1686 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
1687 (u8 *) private_key_blob,
1688 private_key_blob_len) == 1) {
1689 wpa_printf(MSG_DEBUG, "OpenSSL: "
1690 "SSL_use_RSAPrivateKey_ASN1 --> OK");
1694 tls_show_errors(MSG_DEBUG, __func__,
1695 "SSL_use_RSAPrivateKey_ASN1 failed");
1698 if (tls_read_pkcs12_blob(ssl_ctx, conn->ssl, private_key_blob,
1699 private_key_blob_len, passwd) == 0) {
1700 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
1709 while (!ok && private_key) {
1710 #ifndef OPENSSL_NO_STDIO
1711 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1712 SSL_FILETYPE_ASN1) == 1) {
1713 wpa_printf(MSG_DEBUG, "OpenSSL: "
1714 "SSL_use_PrivateKey_File (DER) --> OK");
1718 tls_show_errors(MSG_DEBUG, __func__,
1719 "SSL_use_PrivateKey_File (DER) "
1723 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1724 SSL_FILETYPE_PEM) == 1) {
1725 wpa_printf(MSG_DEBUG, "OpenSSL: "
1726 "SSL_use_PrivateKey_File (PEM) --> OK");
1730 tls_show_errors(MSG_DEBUG, __func__,
1731 "SSL_use_PrivateKey_File (PEM) "
1734 #else /* OPENSSL_NO_STDIO */
1735 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1737 #endif /* OPENSSL_NO_STDIO */
1739 if (tls_read_pkcs12(ssl_ctx, conn->ssl, private_key, passwd)
1741 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
1747 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
1748 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
1749 "access certificate store --> OK");
1758 wpa_printf(MSG_INFO, "OpenSSL: Failed to load private key");
1764 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1767 if (!SSL_check_private_key(conn->ssl)) {
1768 tls_show_errors(MSG_INFO, __func__, "Private key failed "
1773 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
1778 static int tls_global_private_key(SSL_CTX *ssl_ctx, const char *private_key,
1779 const char *private_key_passwd)
1783 if (private_key == NULL)
1786 if (private_key_passwd) {
1787 passwd = os_strdup(private_key_passwd);
1793 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1794 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1796 #ifndef OPENSSL_NO_STDIO
1797 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1798 SSL_FILETYPE_ASN1) != 1 &&
1799 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1800 SSL_FILETYPE_PEM) != 1 &&
1801 #endif /* OPENSSL_NO_STDIO */
1802 tls_read_pkcs12(ssl_ctx, NULL, private_key, passwd)) {
1803 tls_show_errors(MSG_INFO, __func__,
1804 "Failed to load private key");
1811 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1813 if (!SSL_CTX_check_private_key(ssl_ctx)) {
1814 tls_show_errors(MSG_INFO, __func__,
1815 "Private key failed verification");
1823 static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
1825 #ifdef OPENSSL_NO_DH
1826 if (dh_file == NULL)
1828 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
1829 "dh_file specified");
1831 #else /* OPENSSL_NO_DH */
1835 /* TODO: add support for dh_blob */
1836 if (dh_file == NULL)
1841 bio = BIO_new_file(dh_file, "r");
1843 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
1844 dh_file, ERR_error_string(ERR_get_error(), NULL));
1847 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1849 #ifndef OPENSSL_NO_DSA
1850 while (dh == NULL) {
1852 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
1853 " trying to parse as DSA params", dh_file,
1854 ERR_error_string(ERR_get_error(), NULL));
1855 bio = BIO_new_file(dh_file, "r");
1858 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
1861 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
1862 "'%s': %s", dh_file,
1863 ERR_error_string(ERR_get_error(), NULL));
1867 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
1868 dh = DSA_dup_DH(dsa);
1871 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
1872 "params into DH params");
1877 #endif /* !OPENSSL_NO_DSA */
1879 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
1884 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
1885 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
1887 ERR_error_string(ERR_get_error(), NULL));
1893 #endif /* OPENSSL_NO_DH */
1897 static int tls_global_dh(SSL_CTX *ssl_ctx, const char *dh_file)
1899 #ifdef OPENSSL_NO_DH
1900 if (dh_file == NULL)
1902 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
1903 "dh_file specified");
1905 #else /* OPENSSL_NO_DH */
1909 /* TODO: add support for dh_blob */
1910 if (dh_file == NULL)
1912 if (ssl_ctx == NULL)
1915 bio = BIO_new_file(dh_file, "r");
1917 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
1918 dh_file, ERR_error_string(ERR_get_error(), NULL));
1921 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1923 #ifndef OPENSSL_NO_DSA
1924 while (dh == NULL) {
1926 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
1927 " trying to parse as DSA params", dh_file,
1928 ERR_error_string(ERR_get_error(), NULL));
1929 bio = BIO_new_file(dh_file, "r");
1932 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
1935 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
1936 "'%s': %s", dh_file,
1937 ERR_error_string(ERR_get_error(), NULL));
1941 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
1942 dh = DSA_dup_DH(dsa);
1945 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
1946 "params into DH params");
1951 #endif /* !OPENSSL_NO_DSA */
1953 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
1958 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
1959 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
1961 ERR_error_string(ERR_get_error(), NULL));
1967 #endif /* OPENSSL_NO_DH */
1971 int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
1972 struct tls_keys *keys)
1976 if (conn == NULL || keys == NULL)
1979 if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL)
1982 os_memset(keys, 0, sizeof(*keys));
1983 keys->master_key = ssl->session->master_key;
1984 keys->master_key_len = ssl->session->master_key_length;
1985 keys->client_random = ssl->s3->client_random;
1986 keys->client_random_len = SSL3_RANDOM_SIZE;
1987 keys->server_random = ssl->s3->server_random;
1988 keys->server_random_len = SSL3_RANDOM_SIZE;
1994 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
1995 const char *label, int server_random_first,
1996 u8 *out, size_t out_len)
2002 static struct wpabuf *
2003 openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data,
2007 struct wpabuf *out_data;
2010 * Give TLS handshake data from the server (if available) to OpenSSL
2014 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
2016 tls_show_errors(MSG_INFO, __func__,
2017 "Handshake failed - BIO_write");
2021 /* Initiate TLS handshake or continue the existing handshake */
2023 res = SSL_accept(conn->ssl);
2025 res = SSL_connect(conn->ssl);
2027 int err = SSL_get_error(conn->ssl, res);
2028 if (err == SSL_ERROR_WANT_READ)
2029 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
2031 else if (err == SSL_ERROR_WANT_WRITE)
2032 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
2035 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
2040 /* Get the TLS handshake data to be sent to the server */
2041 res = BIO_ctrl_pending(conn->ssl_out);
2042 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
2043 out_data = wpabuf_alloc(res);
2044 if (out_data == NULL) {
2045 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
2046 "handshake output (%d bytes)", res);
2047 if (BIO_reset(conn->ssl_out) < 0) {
2048 tls_show_errors(MSG_INFO, __func__,
2049 "BIO_reset failed");
2053 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
2056 tls_show_errors(MSG_INFO, __func__,
2057 "Handshake failed - BIO_read");
2058 if (BIO_reset(conn->ssl_out) < 0) {
2059 tls_show_errors(MSG_INFO, __func__,
2060 "BIO_reset failed");
2062 wpabuf_free(out_data);
2065 wpabuf_put(out_data, res);
2071 static struct wpabuf *
2072 openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
2074 struct wpabuf *appl_data;
2077 appl_data = wpabuf_alloc(max_len + 100);
2078 if (appl_data == NULL)
2081 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
2082 wpabuf_size(appl_data));
2084 int err = SSL_get_error(conn->ssl, res);
2085 if (err == SSL_ERROR_WANT_READ ||
2086 err == SSL_ERROR_WANT_WRITE) {
2087 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
2090 tls_show_errors(MSG_INFO, __func__,
2091 "Failed to read possible "
2092 "Application Data");
2094 wpabuf_free(appl_data);
2098 wpabuf_put(appl_data, res);
2099 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
2100 "message", appl_data);
2106 static struct wpabuf *
2107 openssl_connection_handshake(struct tls_connection *conn,
2108 const struct wpabuf *in_data,
2109 struct wpabuf **appl_data, int server)
2111 struct wpabuf *out_data;
2116 out_data = openssl_handshake(conn, in_data, server);
2117 if (out_data == NULL)
2120 if (SSL_is_init_finished(conn->ssl) && appl_data && in_data)
2121 *appl_data = openssl_get_appl_data(conn, wpabuf_len(in_data));
2128 tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
2129 const struct wpabuf *in_data,
2130 struct wpabuf **appl_data)
2132 return openssl_connection_handshake(conn, in_data, appl_data, 0);
2136 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
2137 struct tls_connection *conn,
2138 const struct wpabuf *in_data,
2139 struct wpabuf **appl_data)
2141 return openssl_connection_handshake(conn, in_data, appl_data, 1);
2145 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
2146 struct tls_connection *conn,
2147 const struct wpabuf *in_data)
2155 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
2156 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
2157 (res = BIO_reset(conn->ssl_out)) < 0) {
2158 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
2161 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
2163 tls_show_errors(MSG_INFO, __func__,
2164 "Encryption failed - SSL_write");
2168 /* Read encrypted data to be sent to the server */
2169 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
2172 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
2174 tls_show_errors(MSG_INFO, __func__,
2175 "Encryption failed - BIO_read");
2179 wpabuf_put(buf, res);
2185 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
2186 struct tls_connection *conn,
2187 const struct wpabuf *in_data)
2192 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
2193 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
2194 wpabuf_len(in_data));
2196 tls_show_errors(MSG_INFO, __func__,
2197 "Decryption failed - BIO_write");
2200 if (BIO_reset(conn->ssl_out) < 0) {
2201 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
2205 /* Read decrypted data for further processing */
2207 * Even though we try to disable TLS compression, it is possible that
2208 * this cannot be done with all TLS libraries. Add extra buffer space
2209 * to handle the possibility of the decrypted data being longer than
2212 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
2215 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
2217 tls_show_errors(MSG_INFO, __func__,
2218 "Decryption failed - SSL_read");
2222 wpabuf_put(buf, res);
2228 int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
2230 return conn ? conn->ssl->hit : 0;
2234 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
2237 char buf[100], *pos, *end;
2241 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
2246 end = pos + sizeof(buf);
2249 while (*c != TLS_CIPHER_NONE) {
2253 case TLS_CIPHER_RC4_SHA:
2256 case TLS_CIPHER_AES128_SHA:
2257 suite = "AES128-SHA";
2259 case TLS_CIPHER_RSA_DHE_AES128_SHA:
2260 suite = "DHE-RSA-AES128-SHA";
2262 case TLS_CIPHER_ANON_DH_AES128_SHA:
2263 suite = "ADH-AES128-SHA";
2266 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
2267 "cipher selection: %d", *c);
2270 ret = os_snprintf(pos, end - pos, ":%s", suite);
2271 if (ret < 0 || ret >= end - pos)
2278 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
2280 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
2281 tls_show_errors(MSG_INFO, __func__,
2282 "Cipher suite configuration failed");
2290 int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
2291 char *buf, size_t buflen)
2294 if (conn == NULL || conn->ssl == NULL)
2297 name = SSL_get_cipher(conn->ssl);
2301 os_strlcpy(buf, name, buflen);
2306 int tls_connection_enable_workaround(void *ssl_ctx,
2307 struct tls_connection *conn)
2309 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
2315 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2316 /* ClientHello TLS extensions require a patch to openssl, so this function is
2317 * commented out unless explicitly needed for EAP-FAST in order to be able to
2318 * build this file with unmodified openssl. */
2319 int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
2320 int ext_type, const u8 *data,
2323 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
2326 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2327 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
2330 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2331 if (SSL_set_hello_extension(conn->ssl, ext_type, (void *) data,
2334 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2338 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2341 int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
2345 return conn->failed;
2349 int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
2353 return conn->read_alerts;
2357 int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
2361 return conn->write_alerts;
2365 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
2366 const struct tls_connection_params *params)
2374 while ((err = ERR_get_error())) {
2375 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2376 __func__, ERR_error_string(err, NULL));
2379 if (params->engine) {
2380 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
2381 ret = tls_engine_init(conn, params->engine_id, params->pin,
2382 params->key_id, params->cert_id,
2383 params->ca_cert_id);
2387 if (tls_connection_set_subject_match(conn,
2388 params->subject_match,
2389 params->altsubject_match))
2392 if (params->engine && params->ca_cert_id) {
2393 if (tls_connection_engine_ca_cert(tls_ctx, conn,
2394 params->ca_cert_id))
2395 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2396 } else if (tls_connection_ca_cert(tls_ctx, conn, params->ca_cert,
2397 params->ca_cert_blob,
2398 params->ca_cert_blob_len,
2402 if (params->engine && params->cert_id) {
2403 if (tls_connection_engine_client_cert(conn, params->cert_id))
2404 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2405 } else if (tls_connection_client_cert(conn, params->client_cert,
2406 params->client_cert_blob,
2407 params->client_cert_blob_len))
2410 if (params->engine && params->key_id) {
2411 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
2412 if (tls_connection_engine_private_key(conn))
2413 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2414 } else if (tls_connection_private_key(tls_ctx, conn,
2415 params->private_key,
2416 params->private_key_passwd,
2417 params->private_key_blob,
2418 params->private_key_blob_len)) {
2419 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
2420 params->private_key);
2424 if (tls_connection_dh(conn, params->dh_file)) {
2425 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
2430 tls_get_errors(tls_ctx);
2436 int tls_global_set_params(void *tls_ctx,
2437 const struct tls_connection_params *params)
2439 SSL_CTX *ssl_ctx = tls_ctx;
2442 while ((err = ERR_get_error())) {
2443 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2444 __func__, ERR_error_string(err, NULL));
2447 if (tls_global_ca_cert(ssl_ctx, params->ca_cert))
2450 if (tls_global_client_cert(ssl_ctx, params->client_cert))
2453 if (tls_global_private_key(ssl_ctx, params->private_key,
2454 params->private_key_passwd))
2457 if (tls_global_dh(ssl_ctx, params->dh_file)) {
2458 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
2467 int tls_connection_get_keyblock_size(void *tls_ctx,
2468 struct tls_connection *conn)
2470 const EVP_CIPHER *c;
2473 if (conn == NULL || conn->ssl == NULL ||
2474 conn->ssl->enc_read_ctx == NULL ||
2475 conn->ssl->enc_read_ctx->cipher == NULL ||
2476 conn->ssl->read_hash == NULL)
2479 c = conn->ssl->enc_read_ctx->cipher;
2480 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
2481 h = EVP_MD_CTX_md(conn->ssl->read_hash);
2483 h = conn->ssl->read_hash;
2486 return 2 * (EVP_CIPHER_key_length(c) +
2488 EVP_CIPHER_iv_length(c));
2492 unsigned int tls_capabilities(void *tls_ctx)
2498 int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
2505 struct wpabuf * tls_connection_ia_send_phase_finished(
2506 void *tls_ctx, struct tls_connection *conn, int final)
2512 int tls_connection_ia_final_phase_finished(void *tls_ctx,
2513 struct tls_connection *conn)
2519 int tls_connection_ia_permute_inner_secret(void *tls_ctx,
2520 struct tls_connection *conn,
2521 const u8 *key, size_t key_len)
2527 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2528 /* Pre-shared secred requires a patch to openssl, so this function is
2529 * commented out unless explicitly needed for EAP-FAST in order to be able to
2530 * build this file with unmodified openssl. */
2532 static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
2533 STACK_OF(SSL_CIPHER) *peer_ciphers,
2534 SSL_CIPHER **cipher, void *arg)
2536 struct tls_connection *conn = arg;
2539 if (conn == NULL || conn->session_ticket_cb == NULL)
2542 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
2543 conn->session_ticket,
2544 conn->session_ticket_len,
2545 s->s3->client_random,
2546 s->s3->server_random, secret);
2547 os_free(conn->session_ticket);
2548 conn->session_ticket = NULL;
2553 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
2558 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2559 static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
2562 struct tls_connection *conn = arg;
2564 if (conn == NULL || conn->session_ticket_cb == NULL)
2567 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
2569 os_free(conn->session_ticket);
2570 conn->session_ticket = NULL;
2572 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2573 "extension", data, len);
2575 conn->session_ticket = os_malloc(len);
2576 if (conn->session_ticket == NULL)
2579 os_memcpy(conn->session_ticket, data, len);
2580 conn->session_ticket_len = len;
2584 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2585 #ifdef SSL_OP_NO_TICKET
2586 static void tls_hello_ext_cb(SSL *s, int client_server, int type,
2587 unsigned char *data, int len, void *arg)
2589 struct tls_connection *conn = arg;
2591 if (conn == NULL || conn->session_ticket_cb == NULL)
2594 wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
2597 if (type == TLSEXT_TYPE_session_ticket && !client_server) {
2598 os_free(conn->session_ticket);
2599 conn->session_ticket = NULL;
2601 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2602 "extension", data, len);
2603 conn->session_ticket = os_malloc(len);
2604 if (conn->session_ticket == NULL)
2607 os_memcpy(conn->session_ticket, data, len);
2608 conn->session_ticket_len = len;
2611 #else /* SSL_OP_NO_TICKET */
2612 static int tls_hello_ext_cb(SSL *s, TLS_EXTENSION *ext, void *arg)
2614 struct tls_connection *conn = arg;
2616 if (conn == NULL || conn->session_ticket_cb == NULL)
2619 wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
2620 ext->type, ext->length);
2622 os_free(conn->session_ticket);
2623 conn->session_ticket = NULL;
2625 if (ext->type == 35) {
2626 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2627 "extension", ext->data, ext->length);
2628 conn->session_ticket = os_malloc(ext->length);
2629 if (conn->session_ticket == NULL)
2630 return SSL_AD_INTERNAL_ERROR;
2632 os_memcpy(conn->session_ticket, ext->data, ext->length);
2633 conn->session_ticket_len = ext->length;
2638 #endif /* SSL_OP_NO_TICKET */
2639 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2640 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2643 int tls_connection_set_session_ticket_cb(void *tls_ctx,
2644 struct tls_connection *conn,
2645 tls_session_ticket_cb cb,
2648 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2649 conn->session_ticket_cb = cb;
2650 conn->session_ticket_cb_ctx = ctx;
2653 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
2656 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2657 SSL_set_session_ticket_ext_cb(conn->ssl,
2658 tls_session_ticket_ext_cb, conn);
2659 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2660 #ifdef SSL_OP_NO_TICKET
2661 SSL_set_tlsext_debug_callback(conn->ssl, tls_hello_ext_cb);
2662 SSL_set_tlsext_debug_arg(conn->ssl, conn);
2663 #else /* SSL_OP_NO_TICKET */
2664 if (SSL_set_hello_extension_cb(conn->ssl, tls_hello_ext_cb,
2667 #endif /* SSL_OP_NO_TICKET */
2668 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2670 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
2672 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2673 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
2674 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2675 #ifdef SSL_OP_NO_TICKET
2676 SSL_set_tlsext_debug_callback(conn->ssl, NULL);
2677 SSL_set_tlsext_debug_arg(conn->ssl, conn);
2678 #else /* SSL_OP_NO_TICKET */
2679 if (SSL_set_hello_extension_cb(conn->ssl, NULL, NULL) != 1)
2681 #endif /* SSL_OP_NO_TICKET */
2682 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2686 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2688 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */