Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SSL/TLS interface functions for OpenSSL |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3 | * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 10 | #ifdef CONFIG_TESTING_OPTIONS |
| 11 | #include <fcntl.h> |
| 12 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 13 | |
| 14 | #ifndef CONFIG_SMARTCARD |
| 15 | #ifndef OPENSSL_NO_ENGINE |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 16 | #ifndef ANDROID |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 17 | #define OPENSSL_NO_ENGINE |
| 18 | #endif |
| 19 | #endif |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 20 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 21 | |
| 22 | #include <openssl/ssl.h> |
| 23 | #include <openssl/err.h> |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 24 | #include <openssl/opensslv.h> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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 */ |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 30 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 31 | #include <openssl/core_names.h> |
| 32 | #include <openssl/decoder.h> |
| 33 | #include <openssl/param_build.h> |
| 34 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 35 | #ifndef OPENSSL_NO_DSA |
| 36 | #include <openssl/dsa.h> |
| 37 | #endif |
| 38 | #ifndef OPENSSL_NO_DH |
| 39 | #include <openssl/dh.h> |
| 40 | #endif |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 41 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 42 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 43 | #include "common.h" |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 44 | #include "utils/list.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 45 | #include "crypto.h" |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 46 | #include "sha1.h" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 47 | #include "sha256.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 48 | #include "tls.h" |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 49 | #include "tls_openssl.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 50 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 51 | #if !defined(CONFIG_FIPS) && \ |
| 52 | (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \ |
| 53 | defined(EAP_SERVER_FAST)) |
| 54 | #define OPENSSL_NEED_EAP_FAST_PRF |
| 55 | #endif |
| 56 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 57 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \ |
| 58 | defined(EAP_SERVER_FAST) || defined(EAP_TEAP) || \ |
| 59 | defined(EAP_SERVER_TEAP) |
| 60 | #define EAP_FAST_OR_TEAP |
| 61 | #endif |
| 62 | |
| 63 | |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 64 | #if defined(OPENSSL_IS_BORINGSSL) |
| 65 | /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */ |
| 66 | typedef size_t stack_index_t; |
| 67 | #else |
| 68 | typedef int stack_index_t; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 69 | #endif |
| 70 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 71 | #ifdef SSL_set_tlsext_status_type |
| 72 | #ifndef OPENSSL_NO_TLSEXT |
| 73 | #define HAVE_OCSP |
| 74 | #include <openssl/ocsp.h> |
| 75 | #endif /* OPENSSL_NO_TLSEXT */ |
| 76 | #endif /* SSL_set_tlsext_status_type */ |
| 77 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 78 | #if OPENSSL_VERSION_NUMBER < 0x10100000L && \ |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 79 | !defined(BORINGSSL_API_VERSION) |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 80 | /* |
| 81 | * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 82 | * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for |
| 83 | * older versions. |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 84 | */ |
| 85 | |
| 86 | static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, |
| 87 | size_t outlen) |
| 88 | { |
| 89 | if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE) |
| 90 | return 0; |
| 91 | os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 92 | return SSL3_RANDOM_SIZE; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, |
| 97 | size_t outlen) |
| 98 | { |
| 99 | if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE) |
| 100 | return 0; |
| 101 | os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE); |
| 102 | return SSL3_RANDOM_SIZE; |
| 103 | } |
| 104 | |
| 105 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 106 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 107 | static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, |
| 108 | unsigned char *out, size_t outlen) |
| 109 | { |
| 110 | if (!session || session->master_key_length < 0 || |
| 111 | (size_t) session->master_key_length > outlen) |
| 112 | return 0; |
| 113 | if ((size_t) session->master_key_length < outlen) |
| 114 | outlen = session->master_key_length; |
| 115 | os_memcpy(out, session->master_key, outlen); |
| 116 | return outlen; |
| 117 | } |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 118 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 119 | |
| 120 | #endif |
| 121 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 122 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 123 | static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x) |
| 124 | { |
| 125 | return ASN1_STRING_data((ASN1_STRING *) x); |
| 126 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 127 | #endif |
| 128 | |
Gabriel Biren | a5bdf37 | 2022-12-15 20:54:33 +0000 | [diff] [blame] | 129 | static int tls_openssl_ref_count = 0; |
| 130 | static int tls_ex_idx_session = -1; |
| 131 | |
| 132 | struct tls_session_data { |
| 133 | struct dl_list list; |
| 134 | struct wpabuf *buf; |
| 135 | }; |
| 136 | |
| 137 | struct tls_context { |
| 138 | void (*event_cb)(void *ctx, enum tls_event ev, |
| 139 | union tls_event_data *data); |
| 140 | void *cb_ctx; |
| 141 | int cert_in_cb; |
| 142 | char *ocsp_stapling_response; |
| 143 | struct dl_list sessions; /* struct tls_session_data */ |
| 144 | }; |
| 145 | |
Gabriel Biren | cd0cb1c | 2023-04-17 18:16:23 +0000 | [diff] [blame] | 146 | struct tls_data { |
| 147 | SSL_CTX *ssl; |
| 148 | unsigned int tls_session_lifetime; |
| 149 | int check_crl; |
| 150 | int check_crl_strict; |
| 151 | char *ca_cert; |
| 152 | unsigned int crl_reload_interval; |
| 153 | struct os_reltime crl_last_reload; |
| 154 | char *check_cert_subject; |
| 155 | }; |
| 156 | |
| 157 | struct tls_connection { |
| 158 | struct tls_context *context; |
| 159 | struct tls_data *data; |
| 160 | SSL_CTX *ssl_ctx; |
| 161 | SSL *ssl; |
| 162 | BIO *ssl_in, *ssl_out; |
| 163 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
| 164 | ENGINE *engine; /* functional reference to the engine */ |
| 165 | EVP_PKEY *private_key; /* the private key if using engine */ |
| 166 | #endif /* OPENSSL_NO_ENGINE */ |
| 167 | char *subject_match, *altsubject_match, *suffix_match, *domain_match; |
| 168 | char *check_cert_subject; |
| 169 | int read_alerts, write_alerts, failed; |
| 170 | |
| 171 | tls_session_ticket_cb session_ticket_cb; |
| 172 | void *session_ticket_cb_ctx; |
| 173 | |
| 174 | /* SessionTicket received from OpenSSL hello_extension_cb (server) */ |
| 175 | u8 *session_ticket; |
| 176 | size_t session_ticket_len; |
| 177 | |
| 178 | unsigned int ca_cert_verify:1; |
| 179 | unsigned int cert_probe:1; |
| 180 | unsigned int server_cert_only:1; |
| 181 | unsigned int invalid_hb_used:1; |
| 182 | unsigned int success_data:1; |
| 183 | unsigned int client_hello_generated:1; |
| 184 | unsigned int server:1; |
| 185 | |
| 186 | u8 srv_cert_hash[32]; |
| 187 | |
| 188 | unsigned int flags; |
| 189 | |
| 190 | X509 *peer_cert; |
| 191 | X509 *peer_issuer; |
| 192 | X509 *peer_issuer_issuer; |
| 193 | char *peer_subject; /* peer subject info for authenticated peer */ |
| 194 | |
| 195 | unsigned char client_random[SSL3_RANDOM_SIZE]; |
| 196 | unsigned char server_random[SSL3_RANDOM_SIZE]; |
| 197 | |
| 198 | u16 cipher_suite; |
| 199 | int server_dh_prime_len; |
| 200 | }; |
| 201 | |
Gabriel Biren | a5bdf37 | 2022-12-15 20:54:33 +0000 | [diff] [blame] | 202 | static struct tls_context *tls_global = NULL; |
| 203 | static tls_get_certificate_cb certificate_callback_global = NULL; |
Gabriel Biren | 60ae068 | 2023-11-01 22:04:12 +0000 | [diff] [blame] | 204 | static tls_openssl_failure_cb openssl_failure_callback_global = NULL; |
Gabriel Biren | a5bdf37 | 2022-12-15 20:54:33 +0000 | [diff] [blame] | 205 | |
Dmitry Shmidt | ff07917 | 2013-11-08 14:10:30 -0800 | [diff] [blame] | 206 | #ifdef ANDROID |
| 207 | #include <openssl/pem.h> |
Dmitry Shmidt | ff07917 | 2013-11-08 14:10:30 -0800 | [diff] [blame] | 208 | |
Pavel Grafov | 4d8552e | 2018-02-06 11:28:29 +0000 | [diff] [blame] | 209 | #include <log/log.h> |
| 210 | #include <log/log_event_list.h> |
| 211 | |
| 212 | #define CERT_VALIDATION_FAILURE 210033 |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 213 | #define ANDROID_KEYSTORE_PREFIX "keystore://" |
| 214 | #define ANDROID_KEYSTORE_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_PREFIX) |
| 215 | #define ANDROID_KEYSTORE_ENCODED_PREFIX "keystores://" |
| 216 | #define ANDROID_KEYSTORE_ENCODED_PREFIX_LEN os_strlen(ANDROID_KEYSTORE_ENCODED_PREFIX) |
Pavel Grafov | 4d8552e | 2018-02-06 11:28:29 +0000 | [diff] [blame] | 217 | |
| 218 | static void log_cert_validation_failure(const char *reason) |
| 219 | { |
| 220 | android_log_context ctx = create_android_logger(CERT_VALIDATION_FAILURE); |
| 221 | android_log_write_string8(ctx, reason); |
| 222 | android_log_write_list(ctx, LOG_ID_SECURITY); |
| 223 | android_log_destroy(&ctx); |
| 224 | } |
| 225 | |
| 226 | |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 227 | static BIO* BIO_from_keystore(const char *alias, struct tls_connection *conn) |
Dmitry Shmidt | ff07917 | 2013-11-08 14:10:30 -0800 | [diff] [blame] | 228 | { |
| 229 | BIO *bio = NULL; |
| 230 | uint8_t *value = NULL; |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 231 | |
| 232 | void *cb_ctx = NULL; |
| 233 | if (conn != NULL && conn->context != NULL) { |
| 234 | cb_ctx = conn->context->cb_ctx; |
| 235 | } |
| 236 | |
| 237 | if (cb_ctx != NULL && certificate_callback_global != NULL) { |
Gabriel Biren | 980c48a | 2023-03-27 21:49:21 +0000 | [diff] [blame] | 238 | wpa_printf(MSG_INFO, "Retrieving certificate using callback"); |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 239 | int length = (*certificate_callback_global)(cb_ctx, alias, &value); |
Gabriel Biren | a5bdf37 | 2022-12-15 20:54:33 +0000 | [diff] [blame] | 240 | if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL) |
| 241 | BIO_write(bio, value, length); |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 242 | free(value); |
Gabriel Biren | a5bdf37 | 2022-12-15 20:54:33 +0000 | [diff] [blame] | 243 | } |
Dmitry Shmidt | ff07917 | 2013-11-08 14:10:30 -0800 | [diff] [blame] | 244 | return bio; |
| 245 | } |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 246 | |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 247 | static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *alias, struct tls_connection *conn) |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 248 | { |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 249 | BIO *bio = BIO_from_keystore(alias, conn); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 250 | STACK_OF(X509_INFO) *stack = NULL; |
| 251 | stack_index_t i; |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 252 | int ret = 0; |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 253 | |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 254 | if (!bio) { |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 255 | wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s", |
| 256 | alias); |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 257 | return -1; |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 260 | // Keystore returns X.509 certificates in PEM encoding |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 261 | stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL); |
| 262 | BIO_free(bio); |
| 263 | |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 264 | if (!stack) { |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 265 | wpa_printf(MSG_ERROR, "OpenSSL: Failed to parse certificate: %s", |
| 266 | alias); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | for (i = 0; i < sk_X509_INFO_num(stack); ++i) { |
| 271 | X509_INFO *info = sk_X509_INFO_value(stack, i); |
| 272 | |
| 273 | if (info->x509) |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 274 | if (!X509_STORE_add_cert(ctx, info->x509)) { |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 275 | wpa_printf(MSG_ERROR, |
| 276 | "OpenSSL: Failed to add Root CA certificate"); |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 277 | ret = -1; |
| 278 | break; |
| 279 | } |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 280 | if (info->crl) |
| 281 | X509_STORE_add_crl(ctx, info->crl); |
| 282 | } |
| 283 | |
| 284 | sk_X509_INFO_pop_free(stack, X509_INFO_free); |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 285 | return ret; |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
| 289 | static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx, |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 290 | const char *encoded_alias, |
| 291 | struct tls_connection *conn) |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 292 | { |
| 293 | int rc = -1; |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 294 | int len = os_strlen(encoded_alias); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 295 | unsigned char *decoded_alias; |
| 296 | |
| 297 | if (len & 1) { |
| 298 | wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s", |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 299 | encoded_alias); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | decoded_alias = os_malloc(len / 2 + 1); |
| 304 | if (decoded_alias) { |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 305 | if (!hexstr2bin(encoded_alias, decoded_alias, len / 2)) { |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 306 | decoded_alias[len / 2] = '\0'; |
| 307 | rc = tls_add_ca_from_keystore( |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 308 | ctx, (const char *) decoded_alias, conn); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 309 | } |
| 310 | os_free(decoded_alias); |
| 311 | } |
| 312 | |
| 313 | return rc; |
| 314 | } |
| 315 | |
Dmitry Shmidt | ff07917 | 2013-11-08 14:10:30 -0800 | [diff] [blame] | 316 | #endif /* ANDROID */ |
| 317 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 318 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 319 | static struct tls_context * tls_context_new(const struct tls_config *conf) |
| 320 | { |
| 321 | struct tls_context *context = os_zalloc(sizeof(*context)); |
| 322 | if (context == NULL) |
| 323 | return NULL; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 324 | dl_list_init(&context->sessions); |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 325 | if (conf) { |
| 326 | context->event_cb = conf->event_cb; |
| 327 | context->cb_ctx = conf->cb_ctx; |
| 328 | context->cert_in_cb = conf->cert_in_cb; |
| 329 | } |
| 330 | return context; |
| 331 | } |
| 332 | |
| 333 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 334 | #ifdef CONFIG_NO_STDOUT_DEBUG |
| 335 | |
| 336 | static void _tls_show_errors(void) |
| 337 | { |
| 338 | unsigned long err; |
| 339 | |
| 340 | while ((err = ERR_get_error())) { |
| 341 | /* Just ignore the errors, since stdout is disabled */ |
| 342 | } |
| 343 | } |
| 344 | #define tls_show_errors(l, f, t) _tls_show_errors() |
| 345 | |
| 346 | #else /* CONFIG_NO_STDOUT_DEBUG */ |
| 347 | |
| 348 | static void tls_show_errors(int level, const char *func, const char *txt) |
| 349 | { |
| 350 | unsigned long err; |
| 351 | |
| 352 | wpa_printf(level, "OpenSSL: %s - %s %s", |
| 353 | func, txt, ERR_error_string(ERR_get_error(), NULL)); |
| 354 | |
| 355 | while ((err = ERR_get_error())) { |
| 356 | wpa_printf(MSG_INFO, "OpenSSL: pending error: %s", |
| 357 | ERR_error_string(err, NULL)); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 362 | |
| 363 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 364 | static X509_STORE * tls_crl_cert_reload(const char *ca_cert, int check_crl) |
| 365 | { |
| 366 | int flags; |
| 367 | X509_STORE *store; |
| 368 | |
| 369 | store = X509_STORE_new(); |
| 370 | if (!store) { |
| 371 | wpa_printf(MSG_DEBUG, |
| 372 | "OpenSSL: %s - failed to allocate new certificate store", |
| 373 | __func__); |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | if (ca_cert && X509_STORE_load_locations(store, ca_cert, NULL) != 1) { |
| 378 | tls_show_errors(MSG_WARNING, __func__, |
| 379 | "Failed to load root certificates"); |
| 380 | X509_STORE_free(store); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | flags = check_crl ? X509_V_FLAG_CRL_CHECK : 0; |
| 385 | if (check_crl == 2) |
| 386 | flags |= X509_V_FLAG_CRL_CHECK_ALL; |
| 387 | |
| 388 | X509_STORE_set_flags(store, flags); |
| 389 | |
| 390 | return store; |
| 391 | } |
| 392 | |
| 393 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 394 | #ifdef CONFIG_NATIVE_WINDOWS |
| 395 | |
| 396 | /* Windows CryptoAPI and access to certificate stores */ |
| 397 | #include <wincrypt.h> |
| 398 | |
| 399 | #ifdef __MINGW32_VERSION |
| 400 | /* |
| 401 | * MinGW does not yet include all the needed definitions for CryptoAPI, so |
| 402 | * define here whatever extra is needed. |
| 403 | */ |
| 404 | #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16) |
| 405 | #define CERT_STORE_READONLY_FLAG 0x00008000 |
| 406 | #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000 |
| 407 | |
| 408 | #endif /* __MINGW32_VERSION */ |
| 409 | |
| 410 | |
| 411 | struct cryptoapi_rsa_data { |
| 412 | const CERT_CONTEXT *cert; |
| 413 | HCRYPTPROV crypt_prov; |
| 414 | DWORD key_spec; |
| 415 | BOOL free_crypt_prov; |
| 416 | }; |
| 417 | |
| 418 | |
| 419 | static void cryptoapi_error(const char *msg) |
| 420 | { |
| 421 | wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u", |
| 422 | msg, (unsigned int) GetLastError()); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from, |
| 427 | unsigned char *to, RSA *rsa, int padding) |
| 428 | { |
| 429 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from, |
| 435 | unsigned char *to, RSA *rsa, int padding) |
| 436 | { |
| 437 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from, |
| 443 | unsigned char *to, RSA *rsa, int padding) |
| 444 | { |
| 445 | struct cryptoapi_rsa_data *priv = |
| 446 | (struct cryptoapi_rsa_data *) rsa->meth->app_data; |
| 447 | HCRYPTHASH hash; |
| 448 | DWORD hash_size, len, i; |
| 449 | unsigned char *buf = NULL; |
| 450 | int ret = 0; |
| 451 | |
| 452 | if (priv == NULL) { |
| 453 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 454 | ERR_R_PASSED_NULL_PARAMETER); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | if (padding != RSA_PKCS1_PADDING) { |
| 459 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 460 | RSA_R_UNKNOWN_PADDING_TYPE); |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) { |
| 465 | wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported", |
| 466 | __func__); |
| 467 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 468 | RSA_R_INVALID_MESSAGE_LENGTH); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash)) |
| 473 | { |
| 474 | cryptoapi_error("CryptCreateHash failed"); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | len = sizeof(hash_size); |
| 479 | if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len, |
| 480 | 0)) { |
| 481 | cryptoapi_error("CryptGetHashParam failed"); |
| 482 | goto err; |
| 483 | } |
| 484 | |
| 485 | if ((int) hash_size != flen) { |
| 486 | wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)", |
| 487 | (unsigned) hash_size, flen); |
| 488 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 489 | RSA_R_INVALID_MESSAGE_LENGTH); |
| 490 | goto err; |
| 491 | } |
| 492 | if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) { |
| 493 | cryptoapi_error("CryptSetHashParam failed"); |
| 494 | goto err; |
| 495 | } |
| 496 | |
| 497 | len = RSA_size(rsa); |
| 498 | buf = os_malloc(len); |
| 499 | if (buf == NULL) { |
| 500 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE); |
| 501 | goto err; |
| 502 | } |
| 503 | |
| 504 | if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) { |
| 505 | cryptoapi_error("CryptSignHash failed"); |
| 506 | goto err; |
| 507 | } |
| 508 | |
| 509 | for (i = 0; i < len; i++) |
| 510 | to[i] = buf[len - i - 1]; |
| 511 | ret = len; |
| 512 | |
| 513 | err: |
| 514 | os_free(buf); |
| 515 | CryptDestroyHash(hash); |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from, |
| 522 | unsigned char *to, RSA *rsa, int padding) |
| 523 | { |
| 524 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv) |
| 530 | { |
| 531 | if (priv == NULL) |
| 532 | return; |
| 533 | if (priv->crypt_prov && priv->free_crypt_prov) |
| 534 | CryptReleaseContext(priv->crypt_prov, 0); |
| 535 | if (priv->cert) |
| 536 | CertFreeCertificateContext(priv->cert); |
| 537 | os_free(priv); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | static int cryptoapi_finish(RSA *rsa) |
| 542 | { |
| 543 | cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data); |
| 544 | os_free((void *) rsa->meth); |
| 545 | rsa->meth = NULL; |
| 546 | return 1; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store) |
| 551 | { |
| 552 | HCERTSTORE cs; |
| 553 | const CERT_CONTEXT *ret = NULL; |
| 554 | |
| 555 | cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0, |
| 556 | store | CERT_STORE_OPEN_EXISTING_FLAG | |
| 557 | CERT_STORE_READONLY_FLAG, L"MY"); |
| 558 | if (cs == NULL) { |
| 559 | cryptoapi_error("Failed to open 'My system store'"); |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | if (strncmp(name, "cert://", 7) == 0) { |
| 564 | unsigned short wbuf[255]; |
| 565 | MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255); |
| 566 | ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING | |
| 567 | PKCS_7_ASN_ENCODING, |
| 568 | 0, CERT_FIND_SUBJECT_STR, |
| 569 | wbuf, NULL); |
| 570 | } else if (strncmp(name, "hash://", 7) == 0) { |
| 571 | CRYPT_HASH_BLOB blob; |
| 572 | int len; |
| 573 | const char *hash = name + 7; |
| 574 | unsigned char *buf; |
| 575 | |
| 576 | len = os_strlen(hash) / 2; |
| 577 | buf = os_malloc(len); |
| 578 | if (buf && hexstr2bin(hash, buf, len) == 0) { |
| 579 | blob.cbData = len; |
| 580 | blob.pbData = buf; |
| 581 | ret = CertFindCertificateInStore(cs, |
| 582 | X509_ASN_ENCODING | |
| 583 | PKCS_7_ASN_ENCODING, |
| 584 | 0, CERT_FIND_HASH, |
| 585 | &blob, NULL); |
| 586 | } |
| 587 | os_free(buf); |
| 588 | } |
| 589 | |
| 590 | CertCloseStore(cs, 0); |
| 591 | |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) |
| 597 | { |
| 598 | X509 *cert = NULL; |
| 599 | RSA *rsa = NULL, *pub_rsa; |
| 600 | struct cryptoapi_rsa_data *priv; |
| 601 | RSA_METHOD *rsa_meth; |
| 602 | |
| 603 | if (name == NULL || |
| 604 | (strncmp(name, "cert://", 7) != 0 && |
| 605 | strncmp(name, "hash://", 7) != 0)) |
| 606 | return -1; |
| 607 | |
| 608 | priv = os_zalloc(sizeof(*priv)); |
| 609 | rsa_meth = os_zalloc(sizeof(*rsa_meth)); |
| 610 | if (priv == NULL || rsa_meth == NULL) { |
| 611 | wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory " |
| 612 | "for CryptoAPI RSA method"); |
| 613 | os_free(priv); |
| 614 | os_free(rsa_meth); |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER); |
| 619 | if (priv->cert == NULL) { |
| 620 | priv->cert = cryptoapi_find_cert( |
| 621 | name, CERT_SYSTEM_STORE_LOCAL_MACHINE); |
| 622 | } |
| 623 | if (priv->cert == NULL) { |
| 624 | wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate " |
| 625 | "'%s'", name); |
| 626 | goto err; |
| 627 | } |
| 628 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 629 | cert = d2i_X509(NULL, |
| 630 | (const unsigned char **) &priv->cert->pbCertEncoded, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 631 | priv->cert->cbCertEncoded); |
| 632 | if (cert == NULL) { |
| 633 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER " |
| 634 | "encoding"); |
| 635 | goto err; |
| 636 | } |
| 637 | |
| 638 | if (!CryptAcquireCertificatePrivateKey(priv->cert, |
| 639 | CRYPT_ACQUIRE_COMPARE_KEY_FLAG, |
| 640 | NULL, &priv->crypt_prov, |
| 641 | &priv->key_spec, |
| 642 | &priv->free_crypt_prov)) { |
| 643 | cryptoapi_error("Failed to acquire a private key for the " |
| 644 | "certificate"); |
| 645 | goto err; |
| 646 | } |
| 647 | |
| 648 | rsa_meth->name = "Microsoft CryptoAPI RSA Method"; |
| 649 | rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc; |
| 650 | rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec; |
| 651 | rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc; |
| 652 | rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec; |
| 653 | rsa_meth->finish = cryptoapi_finish; |
| 654 | rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK; |
| 655 | rsa_meth->app_data = (char *) priv; |
| 656 | |
| 657 | rsa = RSA_new(); |
| 658 | if (rsa == NULL) { |
| 659 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, |
| 660 | ERR_R_MALLOC_FAILURE); |
| 661 | goto err; |
| 662 | } |
| 663 | |
| 664 | if (!SSL_use_certificate(ssl, cert)) { |
| 665 | RSA_free(rsa); |
| 666 | rsa = NULL; |
| 667 | goto err; |
| 668 | } |
| 669 | pub_rsa = cert->cert_info->key->pkey->pkey.rsa; |
| 670 | X509_free(cert); |
| 671 | cert = NULL; |
| 672 | |
| 673 | rsa->n = BN_dup(pub_rsa->n); |
| 674 | rsa->e = BN_dup(pub_rsa->e); |
| 675 | if (!RSA_set_method(rsa, rsa_meth)) |
| 676 | goto err; |
| 677 | |
| 678 | if (!SSL_use_RSAPrivateKey(ssl, rsa)) |
| 679 | goto err; |
| 680 | RSA_free(rsa); |
| 681 | |
| 682 | return 0; |
| 683 | |
| 684 | err: |
| 685 | if (cert) |
| 686 | X509_free(cert); |
| 687 | if (rsa) |
| 688 | RSA_free(rsa); |
| 689 | else { |
| 690 | os_free(rsa_meth); |
| 691 | cryptoapi_free_data(priv); |
| 692 | } |
| 693 | return -1; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name) |
| 698 | { |
| 699 | HCERTSTORE cs; |
| 700 | PCCERT_CONTEXT ctx = NULL; |
| 701 | X509 *cert; |
| 702 | char buf[128]; |
| 703 | const char *store; |
| 704 | #ifdef UNICODE |
| 705 | WCHAR *wstore; |
| 706 | #endif /* UNICODE */ |
| 707 | |
| 708 | if (name == NULL || strncmp(name, "cert_store://", 13) != 0) |
| 709 | return -1; |
| 710 | |
| 711 | store = name + 13; |
| 712 | #ifdef UNICODE |
| 713 | wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR)); |
| 714 | if (wstore == NULL) |
| 715 | return -1; |
| 716 | wsprintf(wstore, L"%S", store); |
| 717 | cs = CertOpenSystemStore(0, wstore); |
| 718 | os_free(wstore); |
| 719 | #else /* UNICODE */ |
| 720 | cs = CertOpenSystemStore(0, store); |
| 721 | #endif /* UNICODE */ |
| 722 | if (cs == NULL) { |
| 723 | wpa_printf(MSG_DEBUG, "%s: failed to open system cert store " |
| 724 | "'%s': error=%d", __func__, store, |
| 725 | (int) GetLastError()); |
| 726 | return -1; |
| 727 | } |
| 728 | |
| 729 | while ((ctx = CertEnumCertificatesInStore(cs, ctx))) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 730 | cert = d2i_X509(NULL, |
| 731 | (const unsigned char **) &ctx->pbCertEncoded, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 732 | ctx->cbCertEncoded); |
| 733 | if (cert == NULL) { |
| 734 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process " |
| 735 | "X509 DER encoding for CA cert"); |
| 736 | continue; |
| 737 | } |
| 738 | |
| 739 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 740 | sizeof(buf)); |
| 741 | wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for " |
| 742 | "system certificate store: subject='%s'", buf); |
| 743 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 744 | if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx), |
| 745 | cert)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 746 | tls_show_errors(MSG_WARNING, __func__, |
| 747 | "Failed to add ca_cert to OpenSSL " |
| 748 | "certificate store"); |
| 749 | } |
| 750 | |
| 751 | X509_free(cert); |
| 752 | } |
| 753 | |
| 754 | if (!CertCloseStore(cs, 0)) { |
| 755 | wpa_printf(MSG_DEBUG, "%s: failed to close system cert store " |
| 756 | "'%s': error=%d", __func__, name + 13, |
| 757 | (int) GetLastError()); |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | |
| 764 | #else /* CONFIG_NATIVE_WINDOWS */ |
| 765 | |
| 766 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) |
| 767 | { |
| 768 | return -1; |
| 769 | } |
| 770 | |
| 771 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 772 | |
| 773 | |
| 774 | static void ssl_info_cb(const SSL *ssl, int where, int ret) |
| 775 | { |
| 776 | const char *str; |
| 777 | int w; |
| 778 | |
| 779 | wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret); |
| 780 | w = where & ~SSL_ST_MASK; |
| 781 | if (w & SSL_ST_CONNECT) |
| 782 | str = "SSL_connect"; |
| 783 | else if (w & SSL_ST_ACCEPT) |
| 784 | str = "SSL_accept"; |
| 785 | else |
| 786 | str = "undefined"; |
| 787 | |
| 788 | if (where & SSL_CB_LOOP) { |
| 789 | wpa_printf(MSG_DEBUG, "SSL: %s:%s", |
| 790 | str, SSL_state_string_long(ssl)); |
| 791 | } else if (where & SSL_CB_ALERT) { |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 792 | struct tls_connection *conn = SSL_get_app_data((SSL *) ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 793 | wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s", |
| 794 | where & SSL_CB_READ ? |
| 795 | "read (remote end reported an error)" : |
| 796 | "write (local SSL3 detected an error)", |
| 797 | SSL_alert_type_string_long(ret), |
| 798 | SSL_alert_desc_string_long(ret)); |
| 799 | if ((ret >> 8) == SSL3_AL_FATAL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 800 | if (where & SSL_CB_READ) |
| 801 | conn->read_alerts++; |
| 802 | else |
| 803 | conn->write_alerts++; |
| 804 | } |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 805 | if (conn->context->event_cb != NULL) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 806 | union tls_event_data ev; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 807 | struct tls_context *context = conn->context; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 808 | os_memset(&ev, 0, sizeof(ev)); |
| 809 | ev.alert.is_local = !(where & SSL_CB_READ); |
| 810 | ev.alert.type = SSL_alert_type_string_long(ret); |
| 811 | ev.alert.description = SSL_alert_desc_string_long(ret); |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 812 | context->event_cb(context->cb_ctx, TLS_ALERT, &ev); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 813 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 814 | } else if (where & SSL_CB_EXIT && ret <= 0) { |
| 815 | wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s", |
| 816 | str, ret == 0 ? "failed" : "error", |
| 817 | SSL_state_string_long(ssl)); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
| 822 | #ifndef OPENSSL_NO_ENGINE |
| 823 | /** |
| 824 | * tls_engine_load_dynamic_generic - load any openssl engine |
| 825 | * @pre: an array of commands and values that load an engine initialized |
| 826 | * in the engine specific function |
| 827 | * @post: an array of commands and values that initialize an already loaded |
| 828 | * engine (or %NULL if not required) |
| 829 | * @id: the engine id of the engine to load (only required if post is not %NULL |
| 830 | * |
| 831 | * This function is a generic function that loads any openssl engine. |
| 832 | * |
| 833 | * Returns: 0 on success, -1 on failure |
| 834 | */ |
| 835 | static int tls_engine_load_dynamic_generic(const char *pre[], |
| 836 | const char *post[], const char *id) |
| 837 | { |
| 838 | ENGINE *engine; |
| 839 | const char *dynamic_id = "dynamic"; |
| 840 | |
| 841 | engine = ENGINE_by_id(id); |
| 842 | if (engine) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 843 | wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already " |
| 844 | "available", id); |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 845 | /* |
| 846 | * If it was auto-loaded by ENGINE_by_id() we might still |
| 847 | * need to tell it which PKCS#11 module to use in legacy |
| 848 | * (non-p11-kit) environments. Do so now; even if it was |
| 849 | * properly initialised before, setting it again will be |
| 850 | * harmless. |
| 851 | */ |
| 852 | goto found; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 853 | } |
| 854 | ERR_clear_error(); |
| 855 | |
| 856 | engine = ENGINE_by_id(dynamic_id); |
| 857 | if (engine == NULL) { |
| 858 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", |
| 859 | dynamic_id, |
| 860 | ERR_error_string(ERR_get_error(), NULL)); |
| 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | /* Perform the pre commands. This will load the engine. */ |
| 865 | while (pre && pre[0]) { |
| 866 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]); |
| 867 | if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) { |
| 868 | wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: " |
| 869 | "%s %s [%s]", pre[0], pre[1], |
| 870 | ERR_error_string(ERR_get_error(), NULL)); |
| 871 | ENGINE_free(engine); |
| 872 | return -1; |
| 873 | } |
| 874 | pre += 2; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * Free the reference to the "dynamic" engine. The loaded engine can |
| 879 | * now be looked up using ENGINE_by_id(). |
| 880 | */ |
| 881 | ENGINE_free(engine); |
| 882 | |
| 883 | engine = ENGINE_by_id(id); |
| 884 | if (engine == NULL) { |
| 885 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", |
| 886 | id, ERR_error_string(ERR_get_error(), NULL)); |
| 887 | return -1; |
| 888 | } |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 889 | found: |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 890 | while (post && post[0]) { |
| 891 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]); |
| 892 | if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) { |
| 893 | wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:" |
| 894 | " %s %s [%s]", post[0], post[1], |
| 895 | ERR_error_string(ERR_get_error(), NULL)); |
| 896 | ENGINE_remove(engine); |
| 897 | ENGINE_free(engine); |
| 898 | return -1; |
| 899 | } |
| 900 | post += 2; |
| 901 | } |
| 902 | ENGINE_free(engine); |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | |
| 908 | /** |
| 909 | * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc |
| 910 | * @pkcs11_so_path: pksc11_so_path from the configuration |
| 911 | * @pcks11_module_path: pkcs11_module_path from the configuration |
| 912 | */ |
| 913 | static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path, |
| 914 | const char *pkcs11_module_path) |
| 915 | { |
| 916 | char *engine_id = "pkcs11"; |
| 917 | const char *pre_cmd[] = { |
| 918 | "SO_PATH", NULL /* pkcs11_so_path */, |
| 919 | "ID", NULL /* engine_id */, |
| 920 | "LIST_ADD", "1", |
| 921 | /* "NO_VCHECK", "1", */ |
| 922 | "LOAD", NULL, |
| 923 | NULL, NULL |
| 924 | }; |
| 925 | const char *post_cmd[] = { |
| 926 | "MODULE_PATH", NULL /* pkcs11_module_path */, |
| 927 | NULL, NULL |
| 928 | }; |
| 929 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 930 | if (!pkcs11_so_path) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 931 | return 0; |
| 932 | |
| 933 | pre_cmd[1] = pkcs11_so_path; |
| 934 | pre_cmd[3] = engine_id; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 935 | if (pkcs11_module_path) |
| 936 | post_cmd[1] = pkcs11_module_path; |
| 937 | else |
| 938 | post_cmd[0] = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 939 | |
| 940 | wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s", |
| 941 | pkcs11_so_path); |
| 942 | |
| 943 | return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id); |
| 944 | } |
| 945 | |
| 946 | |
| 947 | /** |
| 948 | * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc |
| 949 | * @opensc_so_path: opensc_so_path from the configuration |
| 950 | */ |
| 951 | static int tls_engine_load_dynamic_opensc(const char *opensc_so_path) |
| 952 | { |
| 953 | char *engine_id = "opensc"; |
| 954 | const char *pre_cmd[] = { |
| 955 | "SO_PATH", NULL /* opensc_so_path */, |
| 956 | "ID", NULL /* engine_id */, |
| 957 | "LIST_ADD", "1", |
| 958 | "LOAD", NULL, |
| 959 | NULL, NULL |
| 960 | }; |
| 961 | |
| 962 | if (!opensc_so_path) |
| 963 | return 0; |
| 964 | |
| 965 | pre_cmd[1] = opensc_so_path; |
| 966 | pre_cmd[3] = engine_id; |
| 967 | |
| 968 | wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s", |
| 969 | opensc_so_path); |
| 970 | |
| 971 | return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id); |
| 972 | } |
| 973 | #endif /* OPENSSL_NO_ENGINE */ |
| 974 | |
| 975 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 976 | static struct tls_session_data * get_session_data(struct tls_context *context, |
| 977 | const struct wpabuf *buf) |
| 978 | { |
| 979 | struct tls_session_data *data; |
| 980 | |
| 981 | dl_list_for_each(data, &context->sessions, struct tls_session_data, |
| 982 | list) { |
| 983 | if (data->buf == buf) |
| 984 | return data; |
| 985 | } |
| 986 | |
| 987 | return NULL; |
| 988 | } |
| 989 | |
| 990 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 991 | static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
| 992 | { |
| 993 | struct wpabuf *buf; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 994 | struct tls_context *context; |
| 995 | struct tls_session_data *found; |
| 996 | |
| 997 | wpa_printf(MSG_DEBUG, |
| 998 | "OpenSSL: Remove session %p (tls_ex_idx_session=%d)", sess, |
| 999 | tls_ex_idx_session); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1000 | |
| 1001 | if (tls_ex_idx_session < 0) |
| 1002 | return; |
| 1003 | buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); |
| 1004 | if (!buf) |
| 1005 | return; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1006 | |
| 1007 | context = SSL_CTX_get_app_data(ctx); |
| 1008 | SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL); |
| 1009 | found = get_session_data(context, buf); |
| 1010 | if (!found) { |
| 1011 | wpa_printf(MSG_DEBUG, |
| 1012 | "OpenSSL: Do not free application session data %p (sess %p)", |
| 1013 | buf, sess); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | dl_list_del(&found->list); |
| 1018 | os_free(found); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1019 | wpa_printf(MSG_DEBUG, |
| 1020 | "OpenSSL: Free application session data %p (sess %p)", |
| 1021 | buf, sess); |
| 1022 | wpabuf_free(buf); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1026 | void * tls_init(const struct tls_config *conf) |
| 1027 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1028 | struct tls_data *data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1029 | SSL_CTX *ssl; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1030 | struct tls_context *context; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1031 | const char *ciphers; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1032 | |
| 1033 | if (tls_openssl_ref_count == 0) { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1034 | void openssl_load_legacy_provider(void); |
| 1035 | |
| 1036 | openssl_load_legacy_provider(); |
| 1037 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1038 | tls_global = context = tls_context_new(conf); |
| 1039 | if (context == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1040 | return NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1041 | #ifdef CONFIG_FIPS |
| 1042 | #ifdef OPENSSL_FIPS |
| 1043 | if (conf && conf->fips_mode) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1044 | static int fips_enabled = 0; |
| 1045 | |
| 1046 | if (!fips_enabled && !FIPS_mode_set(1)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1047 | wpa_printf(MSG_ERROR, "Failed to enable FIPS " |
| 1048 | "mode"); |
| 1049 | ERR_load_crypto_strings(); |
| 1050 | ERR_print_errors_fp(stderr); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1051 | os_free(tls_global); |
| 1052 | tls_global = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1053 | return NULL; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1054 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1055 | wpa_printf(MSG_INFO, "Running in FIPS mode"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1056 | fips_enabled = 1; |
| 1057 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1058 | } |
| 1059 | #else /* OPENSSL_FIPS */ |
| 1060 | if (conf && conf->fips_mode) { |
| 1061 | wpa_printf(MSG_ERROR, "FIPS mode requested, but not " |
| 1062 | "supported"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1063 | os_free(tls_global); |
| 1064 | tls_global = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1065 | return NULL; |
| 1066 | } |
| 1067 | #endif /* OPENSSL_FIPS */ |
| 1068 | #endif /* CONFIG_FIPS */ |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1069 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1070 | SSL_load_error_strings(); |
| 1071 | SSL_library_init(); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1072 | #ifndef OPENSSL_NO_SHA256 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1073 | EVP_add_digest(EVP_sha256()); |
| 1074 | #endif /* OPENSSL_NO_SHA256 */ |
| 1075 | /* TODO: if /dev/urandom is available, PRNG is seeded |
| 1076 | * automatically. If this is not the case, random data should |
| 1077 | * be added here. */ |
| 1078 | |
| 1079 | #ifdef PKCS12_FUNCS |
| 1080 | #ifndef OPENSSL_NO_RC2 |
| 1081 | /* |
| 1082 | * 40-bit RC2 is commonly used in PKCS#12 files, so enable it. |
| 1083 | * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8 |
| 1084 | * versions, but it looks like OpenSSL 1.0.0 does not do that |
| 1085 | * anymore. |
| 1086 | */ |
| 1087 | EVP_add_cipher(EVP_rc2_40_cbc()); |
| 1088 | #endif /* OPENSSL_NO_RC2 */ |
| 1089 | PKCS12_PBE_add(); |
| 1090 | #endif /* PKCS12_FUNCS */ |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1091 | #endif /* < 1.1.0 */ |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1092 | } else { |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1093 | context = tls_context_new(conf); |
| 1094 | if (context == NULL) |
| 1095 | return NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1096 | } |
| 1097 | tls_openssl_ref_count++; |
| 1098 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1099 | data = os_zalloc(sizeof(*data)); |
| 1100 | if (data) |
| 1101 | ssl = SSL_CTX_new(SSLv23_method()); |
| 1102 | else |
| 1103 | ssl = NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1104 | if (ssl == NULL) { |
| 1105 | tls_openssl_ref_count--; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1106 | if (context != tls_global) |
| 1107 | os_free(context); |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1108 | if (tls_openssl_ref_count == 0) { |
| 1109 | os_free(tls_global); |
| 1110 | tls_global = NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1111 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1112 | os_free(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1113 | return NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1114 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1115 | data->ssl = ssl; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1116 | if (conf) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1117 | data->tls_session_lifetime = conf->tls_session_lifetime; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1118 | data->crl_reload_interval = conf->crl_reload_interval; |
| 1119 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1120 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1121 | SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2); |
| 1122 | SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3); |
| 1123 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1124 | SSL_CTX_set_mode(ssl, SSL_MODE_AUTO_RETRY); |
| 1125 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 1126 | #ifdef SSL_MODE_NO_AUTO_CHAIN |
| 1127 | /* Number of deployed use cases assume the default OpenSSL behavior of |
| 1128 | * auto chaining the local certificate is in use. BoringSSL removed this |
| 1129 | * functionality by default, so we need to restore it here to avoid |
| 1130 | * breaking existing use cases. */ |
| 1131 | SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN); |
| 1132 | #endif /* SSL_MODE_NO_AUTO_CHAIN */ |
| 1133 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1134 | SSL_CTX_set_info_callback(ssl, ssl_info_cb); |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1135 | SSL_CTX_set_app_data(ssl, context); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1136 | if (data->tls_session_lifetime > 0) { |
| 1137 | SSL_CTX_set_quiet_shutdown(ssl, 1); |
| 1138 | /* |
| 1139 | * Set default context here. In practice, this will be replaced |
| 1140 | * by the per-EAP method context in tls_connection_set_verify(). |
| 1141 | */ |
| 1142 | SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7); |
| 1143 | SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER); |
| 1144 | SSL_CTX_set_timeout(ssl, data->tls_session_lifetime); |
| 1145 | SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1146 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L && \ |
| 1147 | !defined(LIBRESSL_VERSION_NUMBER) && \ |
| 1148 | !defined(OPENSSL_IS_BORINGSSL) |
| 1149 | /* One session ticket is sufficient for EAP-TLS */ |
| 1150 | SSL_CTX_set_num_tickets(ssl, 1); |
| 1151 | #endif |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1152 | } else { |
| 1153 | SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1154 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L && \ |
| 1155 | !defined(LIBRESSL_VERSION_NUMBER) && \ |
| 1156 | !defined(OPENSSL_IS_BORINGSSL) |
| 1157 | SSL_CTX_set_num_tickets(ssl, 0); |
| 1158 | #endif |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | if (tls_ex_idx_session < 0) { |
| 1162 | tls_ex_idx_session = SSL_SESSION_get_ex_new_index( |
| 1163 | 0, NULL, NULL, NULL, NULL); |
| 1164 | if (tls_ex_idx_session < 0) { |
| 1165 | tls_deinit(data); |
| 1166 | return NULL; |
| 1167 | } |
| 1168 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1169 | |
| 1170 | #ifndef OPENSSL_NO_ENGINE |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1171 | wpa_printf(MSG_DEBUG, "ENGINE: Loading builtin engines"); |
| 1172 | ENGINE_load_builtin_engines(); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1173 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1174 | if (conf && |
| 1175 | (conf->opensc_engine_path || conf->pkcs11_engine_path || |
| 1176 | conf->pkcs11_module_path)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1177 | if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) || |
| 1178 | tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path, |
| 1179 | conf->pkcs11_module_path)) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1180 | tls_deinit(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1181 | return NULL; |
| 1182 | } |
| 1183 | } |
| 1184 | #endif /* OPENSSL_NO_ENGINE */ |
| 1185 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1186 | if (conf && conf->openssl_ciphers) |
| 1187 | ciphers = conf->openssl_ciphers; |
| 1188 | else |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1189 | ciphers = TLS_DEFAULT_CIPHERS; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1190 | if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) { |
| 1191 | wpa_printf(MSG_ERROR, |
| 1192 | "OpenSSL: Failed to set cipher string '%s'", |
| 1193 | ciphers); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1194 | tls_deinit(data); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1195 | return NULL; |
| 1196 | } |
| 1197 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1198 | return data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | void tls_deinit(void *ssl_ctx) |
| 1203 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1204 | struct tls_data *data = ssl_ctx; |
| 1205 | SSL_CTX *ssl = data->ssl; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1206 | struct tls_context *context = SSL_CTX_get_app_data(ssl); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1207 | struct tls_session_data *sess_data; |
| 1208 | |
| 1209 | if (data->tls_session_lifetime > 0) { |
| 1210 | wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions"); |
| 1211 | SSL_CTX_flush_sessions(ssl, 0); |
| 1212 | wpa_printf(MSG_DEBUG, "OpenSSL: Flush sessions - done"); |
| 1213 | } |
| 1214 | while ((sess_data = dl_list_first(&context->sessions, |
| 1215 | struct tls_session_data, list))) { |
| 1216 | wpa_printf(MSG_DEBUG, |
| 1217 | "OpenSSL: Freeing not-flushed session data %p", |
| 1218 | sess_data->buf); |
| 1219 | wpabuf_free(sess_data->buf); |
| 1220 | dl_list_del(&sess_data->list); |
| 1221 | os_free(sess_data); |
| 1222 | } |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1223 | if (context != tls_global) |
| 1224 | os_free(context); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1225 | os_free(data->ca_cert); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1226 | SSL_CTX_free(ssl); |
| 1227 | |
| 1228 | tls_openssl_ref_count--; |
| 1229 | if (tls_openssl_ref_count == 0) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1230 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1231 | #ifndef OPENSSL_NO_ENGINE |
| 1232 | ENGINE_cleanup(); |
| 1233 | #endif /* OPENSSL_NO_ENGINE */ |
| 1234 | CRYPTO_cleanup_all_ex_data(); |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 1235 | ERR_remove_thread_state(NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1236 | ERR_free_strings(); |
| 1237 | EVP_cleanup(); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1238 | #endif /* < 1.1.0 */ |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1239 | os_free(tls_global->ocsp_stapling_response); |
| 1240 | tls_global->ocsp_stapling_response = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1241 | os_free(tls_global); |
| 1242 | tls_global = NULL; |
| 1243 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1244 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1245 | os_free(data->check_cert_subject); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1246 | os_free(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1250 | #ifndef OPENSSL_NO_ENGINE |
| 1251 | |
| 1252 | /* Cryptoki return values */ |
| 1253 | #define CKR_PIN_INCORRECT 0x000000a0 |
| 1254 | #define CKR_PIN_INVALID 0x000000a1 |
| 1255 | #define CKR_PIN_LEN_RANGE 0x000000a2 |
| 1256 | |
| 1257 | /* libp11 */ |
| 1258 | #define ERR_LIB_PKCS11 ERR_LIB_USER |
| 1259 | |
| 1260 | static int tls_is_pin_error(unsigned int err) |
| 1261 | { |
| 1262 | return ERR_GET_LIB(err) == ERR_LIB_PKCS11 && |
| 1263 | (ERR_GET_REASON(err) == CKR_PIN_INCORRECT || |
| 1264 | ERR_GET_REASON(err) == CKR_PIN_INVALID || |
| 1265 | ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE); |
| 1266 | } |
| 1267 | |
| 1268 | #endif /* OPENSSL_NO_ENGINE */ |
| 1269 | |
| 1270 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1271 | #ifdef ANDROID |
| 1272 | /* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */ |
| 1273 | EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id); |
| 1274 | #endif /* ANDROID */ |
| 1275 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1276 | static int tls_engine_init(struct tls_connection *conn, const char *engine_id, |
| 1277 | const char *pin, const char *key_id, |
| 1278 | const char *cert_id, const char *ca_cert_id) |
| 1279 | { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1280 | #if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL) |
| 1281 | #if !defined(OPENSSL_NO_ENGINE) |
| 1282 | #error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL." |
| 1283 | #endif |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1284 | if (!key_id) |
| 1285 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1286 | conn->engine = NULL; |
| 1287 | conn->private_key = EVP_PKEY_from_keystore(key_id); |
| 1288 | if (!conn->private_key) { |
| 1289 | wpa_printf(MSG_ERROR, |
| 1290 | "ENGINE: cannot load private key with id '%s' [%s]", |
| 1291 | key_id, |
| 1292 | ERR_error_string(ERR_get_error(), NULL)); |
| 1293 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 1294 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1295 | #endif /* ANDROID && OPENSSL_IS_BORINGSSL */ |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1296 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1297 | #ifndef OPENSSL_NO_ENGINE |
| 1298 | int ret = -1; |
| 1299 | if (engine_id == NULL) { |
| 1300 | wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set"); |
| 1301 | return -1; |
| 1302 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1303 | |
| 1304 | ERR_clear_error(); |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 1305 | #ifdef ANDROID |
| 1306 | ENGINE_load_dynamic(); |
| 1307 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1308 | conn->engine = ENGINE_by_id(engine_id); |
| 1309 | if (!conn->engine) { |
| 1310 | wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]", |
| 1311 | engine_id, ERR_error_string(ERR_get_error(), NULL)); |
| 1312 | goto err; |
| 1313 | } |
| 1314 | if (ENGINE_init(conn->engine) != 1) { |
| 1315 | wpa_printf(MSG_ERROR, "ENGINE: engine init failed " |
| 1316 | "(engine: %s) [%s]", engine_id, |
| 1317 | ERR_error_string(ERR_get_error(), NULL)); |
| 1318 | goto err; |
| 1319 | } |
| 1320 | wpa_printf(MSG_DEBUG, "ENGINE: engine initialized"); |
| 1321 | |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 1322 | #ifndef ANDROID |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1323 | if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1324 | wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]", |
| 1325 | ERR_error_string(ERR_get_error(), NULL)); |
| 1326 | goto err; |
| 1327 | } |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 1328 | #endif |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1329 | if (key_id) { |
| 1330 | /* |
| 1331 | * Ensure that the ENGINE does not attempt to use the OpenSSL |
| 1332 | * UI system to obtain a PIN, if we didn't provide one. |
| 1333 | */ |
| 1334 | struct { |
| 1335 | const void *password; |
| 1336 | const char *prompt_info; |
| 1337 | } key_cb = { "", NULL }; |
| 1338 | |
| 1339 | /* load private key first in-case PIN is required for cert */ |
| 1340 | conn->private_key = ENGINE_load_private_key(conn->engine, |
| 1341 | key_id, NULL, |
| 1342 | &key_cb); |
| 1343 | if (!conn->private_key) { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1344 | unsigned long err = ERR_get_error(); |
| 1345 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1346 | wpa_printf(MSG_ERROR, |
| 1347 | "ENGINE: cannot load private key with id '%s' [%s]", |
| 1348 | key_id, |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1349 | ERR_error_string(err, NULL)); |
| 1350 | if (tls_is_pin_error(err)) |
| 1351 | ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN; |
| 1352 | else |
| 1353 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1354 | goto err; |
| 1355 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /* handle a certificate and/or CA certificate */ |
| 1359 | if (cert_id || ca_cert_id) { |
| 1360 | const char *cmd_name = "LOAD_CERT_CTRL"; |
| 1361 | |
| 1362 | /* test if the engine supports a LOAD_CERT_CTRL */ |
| 1363 | if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME, |
| 1364 | 0, (void *)cmd_name, NULL)) { |
| 1365 | wpa_printf(MSG_ERROR, "ENGINE: engine does not support" |
| 1366 | " loading certificates"); |
| 1367 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 1368 | goto err; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | return 0; |
| 1373 | |
| 1374 | err: |
| 1375 | if (conn->engine) { |
| 1376 | ENGINE_free(conn->engine); |
| 1377 | conn->engine = NULL; |
| 1378 | } |
| 1379 | |
| 1380 | if (conn->private_key) { |
| 1381 | EVP_PKEY_free(conn->private_key); |
| 1382 | conn->private_key = NULL; |
| 1383 | } |
| 1384 | |
| 1385 | return ret; |
| 1386 | #else /* OPENSSL_NO_ENGINE */ |
| 1387 | return 0; |
| 1388 | #endif /* OPENSSL_NO_ENGINE */ |
| 1389 | } |
| 1390 | |
| 1391 | |
| 1392 | static void tls_engine_deinit(struct tls_connection *conn) |
| 1393 | { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1394 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1395 | wpa_printf(MSG_DEBUG, "ENGINE: engine deinit"); |
| 1396 | if (conn->private_key) { |
| 1397 | EVP_PKEY_free(conn->private_key); |
| 1398 | conn->private_key = NULL; |
| 1399 | } |
| 1400 | if (conn->engine) { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1401 | #if !defined(OPENSSL_IS_BORINGSSL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1402 | ENGINE_finish(conn->engine); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1403 | #endif /* !OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1404 | conn->engine = NULL; |
| 1405 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1406 | #endif /* ANDROID || !OPENSSL_NO_ENGINE */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | int tls_get_errors(void *ssl_ctx) |
| 1411 | { |
| 1412 | int count = 0; |
| 1413 | unsigned long err; |
| 1414 | |
| 1415 | while ((err = ERR_get_error())) { |
| 1416 | wpa_printf(MSG_INFO, "TLS - SSL error: %s", |
| 1417 | ERR_error_string(err, NULL)); |
| 1418 | count++; |
| 1419 | } |
| 1420 | |
| 1421 | return count; |
| 1422 | } |
| 1423 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1424 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1425 | static const char * openssl_content_type(int content_type) |
| 1426 | { |
| 1427 | switch (content_type) { |
| 1428 | case 20: |
| 1429 | return "change cipher spec"; |
| 1430 | case 21: |
| 1431 | return "alert"; |
| 1432 | case 22: |
| 1433 | return "handshake"; |
| 1434 | case 23: |
| 1435 | return "application data"; |
| 1436 | case 24: |
| 1437 | return "heartbeat"; |
| 1438 | case 256: |
| 1439 | return "TLS header info"; /* pseudo content type */ |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1440 | case 257: |
| 1441 | return "inner content type"; /* pseudo content type */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1442 | default: |
| 1443 | return "?"; |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | static const char * openssl_handshake_type(int content_type, const u8 *buf, |
| 1449 | size_t len) |
| 1450 | { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1451 | if (content_type == 257 && buf && len == 1) |
| 1452 | return openssl_content_type(buf[0]); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1453 | if (content_type != 22 || !buf || len == 0) |
| 1454 | return ""; |
| 1455 | switch (buf[0]) { |
| 1456 | case 0: |
| 1457 | return "hello request"; |
| 1458 | case 1: |
| 1459 | return "client hello"; |
| 1460 | case 2: |
| 1461 | return "server hello"; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1462 | case 3: |
| 1463 | return "hello verify request"; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1464 | case 4: |
| 1465 | return "new session ticket"; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1466 | case 5: |
| 1467 | return "end of early data"; |
| 1468 | case 6: |
| 1469 | return "hello retry request"; |
| 1470 | case 8: |
| 1471 | return "encrypted extensions"; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1472 | case 11: |
| 1473 | return "certificate"; |
| 1474 | case 12: |
| 1475 | return "server key exchange"; |
| 1476 | case 13: |
| 1477 | return "certificate request"; |
| 1478 | case 14: |
| 1479 | return "server hello done"; |
| 1480 | case 15: |
| 1481 | return "certificate verify"; |
| 1482 | case 16: |
| 1483 | return "client key exchange"; |
| 1484 | case 20: |
| 1485 | return "finished"; |
| 1486 | case 21: |
| 1487 | return "certificate url"; |
| 1488 | case 22: |
| 1489 | return "certificate status"; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1490 | case 23: |
| 1491 | return "supplemental data"; |
| 1492 | case 24: |
| 1493 | return "key update"; |
| 1494 | case 254: |
| 1495 | return "message hash"; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1496 | default: |
| 1497 | return "?"; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1502 | #ifdef CONFIG_SUITEB |
| 1503 | |
| 1504 | static void check_server_hello(struct tls_connection *conn, |
| 1505 | const u8 *pos, const u8 *end) |
| 1506 | { |
| 1507 | size_t payload_len, id_len; |
| 1508 | |
| 1509 | /* |
| 1510 | * Parse ServerHello to get the selected cipher suite since OpenSSL does |
| 1511 | * not make it cleanly available during handshake and we need to know |
| 1512 | * whether DHE was selected. |
| 1513 | */ |
| 1514 | |
| 1515 | if (end - pos < 3) |
| 1516 | return; |
| 1517 | payload_len = WPA_GET_BE24(pos); |
| 1518 | pos += 3; |
| 1519 | |
| 1520 | if ((size_t) (end - pos) < payload_len) |
| 1521 | return; |
| 1522 | end = pos + payload_len; |
| 1523 | |
| 1524 | /* Skip Version and Random */ |
| 1525 | if (end - pos < 2 + SSL3_RANDOM_SIZE) |
| 1526 | return; |
| 1527 | pos += 2 + SSL3_RANDOM_SIZE; |
| 1528 | |
| 1529 | /* Skip Session ID */ |
| 1530 | if (end - pos < 1) |
| 1531 | return; |
| 1532 | id_len = *pos++; |
| 1533 | if ((size_t) (end - pos) < id_len) |
| 1534 | return; |
| 1535 | pos += id_len; |
| 1536 | |
| 1537 | if (end - pos < 2) |
| 1538 | return; |
| 1539 | conn->cipher_suite = WPA_GET_BE16(pos); |
| 1540 | wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x", |
| 1541 | conn->cipher_suite); |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn, |
| 1546 | const u8 *pos, const u8 *end) |
| 1547 | { |
| 1548 | size_t payload_len; |
| 1549 | u16 dh_len; |
| 1550 | BIGNUM *p; |
| 1551 | int bits; |
| 1552 | |
| 1553 | if (!(conn->flags & TLS_CONN_SUITEB)) |
| 1554 | return; |
| 1555 | |
| 1556 | /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */ |
| 1557 | if (conn->cipher_suite != 0x9f) |
| 1558 | return; |
| 1559 | |
| 1560 | if (end - pos < 3) |
| 1561 | return; |
| 1562 | payload_len = WPA_GET_BE24(pos); |
| 1563 | pos += 3; |
| 1564 | |
| 1565 | if ((size_t) (end - pos) < payload_len) |
| 1566 | return; |
| 1567 | end = pos + payload_len; |
| 1568 | |
| 1569 | if (end - pos < 2) |
| 1570 | return; |
| 1571 | dh_len = WPA_GET_BE16(pos); |
| 1572 | pos += 2; |
| 1573 | |
| 1574 | if ((size_t) (end - pos) < dh_len) |
| 1575 | return; |
| 1576 | p = BN_bin2bn(pos, dh_len, NULL); |
| 1577 | if (!p) |
| 1578 | return; |
| 1579 | |
| 1580 | bits = BN_num_bits(p); |
| 1581 | BN_free(p); |
| 1582 | |
| 1583 | conn->server_dh_prime_len = bits; |
| 1584 | wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits", |
| 1585 | conn->server_dh_prime_len); |
| 1586 | } |
| 1587 | |
| 1588 | #endif /* CONFIG_SUITEB */ |
| 1589 | |
| 1590 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1591 | static void tls_msg_cb(int write_p, int version, int content_type, |
| 1592 | const void *buf, size_t len, SSL *ssl, void *arg) |
| 1593 | { |
| 1594 | struct tls_connection *conn = arg; |
| 1595 | const u8 *pos = buf; |
| 1596 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1597 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1598 | if ((SSL_version(ssl) == TLS1_VERSION || |
| 1599 | SSL_version(ssl) == TLS1_1_VERSION) && |
| 1600 | SSL_get_security_level(ssl) > 0) { |
| 1601 | wpa_printf(MSG_DEBUG, |
| 1602 | "OpenSSL: Drop security level to 0 to allow TLS 1.0/1.1 use of MD5-SHA1 signature algorithm"); |
| 1603 | SSL_set_security_level(ssl, 0); |
| 1604 | } |
| 1605 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1606 | if (write_p == 2) { |
| 1607 | wpa_printf(MSG_DEBUG, |
| 1608 | "OpenSSL: session ver=0x%x content_type=%d", |
| 1609 | version, content_type); |
| 1610 | wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len); |
| 1611 | return; |
| 1612 | } |
| 1613 | |
| 1614 | wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)", |
| 1615 | write_p ? "TX" : "RX", version, content_type, |
| 1616 | openssl_content_type(content_type), |
| 1617 | openssl_handshake_type(content_type, buf, len)); |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1618 | wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len); |
| 1619 | if (content_type == 24 && len >= 3 && pos[0] == 1) { |
| 1620 | size_t payload_len = WPA_GET_BE16(pos + 1); |
| 1621 | if (payload_len + 3 > len) { |
| 1622 | wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected"); |
| 1623 | conn->invalid_hb_used = 1; |
| 1624 | } |
| 1625 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1626 | |
| 1627 | #ifdef CONFIG_SUITEB |
| 1628 | /* |
| 1629 | * Need to parse these handshake messages to be able to check DH prime |
| 1630 | * length since OpenSSL does not expose the new cipher suite and DH |
| 1631 | * parameters during handshake (e.g., for cert_cb() callback). |
| 1632 | */ |
| 1633 | if (content_type == 22 && pos && len > 0 && pos[0] == 2) |
| 1634 | check_server_hello(conn, pos + 1, pos + len); |
| 1635 | if (content_type == 22 && pos && len > 0 && pos[0] == 12) |
| 1636 | check_server_key_exchange(ssl, conn, pos + 1, pos + len); |
| 1637 | #endif /* CONFIG_SUITEB */ |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1641 | #ifdef CONFIG_TESTING_OPTIONS |
| 1642 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER) |
| 1643 | /* |
| 1644 | * By setting the environment variable SSLKEYLOGFILE to a filename keying |
| 1645 | * material will be exported that you may use with Wireshark to decode any |
| 1646 | * TLS flows. Please see the following for more details: |
| 1647 | * |
| 1648 | * https://gitlab.com/wireshark/wireshark/-/wikis/TLS#tls-decryption |
| 1649 | * |
| 1650 | * Example logging sessions are (you should delete the file on each run): |
| 1651 | * |
| 1652 | * rm -f /tmp/sslkey.log |
| 1653 | * env SSLKEYLOGFILE=/tmp/sslkey.log hostapd ... |
| 1654 | * |
| 1655 | * rm -f /tmp/sslkey.log |
| 1656 | * env SSLKEYLOGFILE=/tmp/sslkey.log wpa_supplicant ... |
| 1657 | * |
| 1658 | * rm -f /tmp/sslkey.log |
| 1659 | * env SSLKEYLOGFILE=/tmp/sslkey.log eapol_test ... |
| 1660 | */ |
| 1661 | static void tls_keylog_cb(const SSL *ssl, const char *line) |
| 1662 | { |
| 1663 | int fd; |
| 1664 | const char *filename; |
| 1665 | struct iovec iov[2]; |
| 1666 | |
| 1667 | filename = getenv("SSLKEYLOGFILE"); |
| 1668 | if (!filename) |
| 1669 | return; |
| 1670 | |
| 1671 | fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR); |
| 1672 | if (fd < 0) { |
| 1673 | wpa_printf(MSG_ERROR, |
| 1674 | "OpenSSL: Failed to open keylog file %s: %s", |
| 1675 | filename, strerror(errno)); |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | /* Assume less than _POSIX_PIPE_BUF (512) where writes are guaranteed |
| 1680 | * to be atomic for O_APPEND. */ |
| 1681 | iov[0].iov_base = (void *) line; |
| 1682 | iov[0].iov_len = os_strlen(line); |
| 1683 | iov[1].iov_base = "\n"; |
| 1684 | iov[1].iov_len = 1; |
| 1685 | |
| 1686 | if (writev(fd, iov, ARRAY_SIZE(iov)) < 01) { |
| 1687 | wpa_printf(MSG_DEBUG, |
| 1688 | "OpenSSL: Failed to write to keylog file %s: %s", |
| 1689 | filename, strerror(errno)); |
| 1690 | } |
| 1691 | |
| 1692 | close(fd); |
| 1693 | } |
| 1694 | #endif |
| 1695 | #endif /* CONFIG_TESTING_OPTIONS */ |
| 1696 | |
| 1697 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1698 | struct tls_connection * tls_connection_init(void *ssl_ctx) |
| 1699 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1700 | struct tls_data *data = ssl_ctx; |
| 1701 | SSL_CTX *ssl = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1702 | struct tls_connection *conn; |
| 1703 | long options; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1704 | X509_STORE *new_cert_store; |
| 1705 | struct os_reltime now; |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1706 | struct tls_context *context = SSL_CTX_get_app_data(ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1707 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1708 | /* Replace X509 store if it is time to update CRL. */ |
| 1709 | if (data->crl_reload_interval > 0 && os_get_reltime(&now) == 0 && |
| 1710 | os_reltime_expired(&now, &data->crl_last_reload, |
| 1711 | data->crl_reload_interval)) { |
| 1712 | wpa_printf(MSG_INFO, |
| 1713 | "OpenSSL: Flushing X509 store with ca_cert file"); |
| 1714 | new_cert_store = tls_crl_cert_reload(data->ca_cert, |
| 1715 | data->check_crl); |
| 1716 | if (!new_cert_store) { |
| 1717 | wpa_printf(MSG_ERROR, |
| 1718 | "OpenSSL: Error replacing X509 store with ca_cert file"); |
| 1719 | } else { |
| 1720 | /* Replace old store */ |
| 1721 | SSL_CTX_set_cert_store(ssl, new_cert_store); |
| 1722 | data->crl_last_reload = now; |
| 1723 | } |
| 1724 | } |
| 1725 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1726 | conn = os_zalloc(sizeof(*conn)); |
| 1727 | if (conn == NULL) |
| 1728 | return NULL; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1729 | conn->data = data; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1730 | conn->ssl_ctx = ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1731 | conn->ssl = SSL_new(ssl); |
| 1732 | if (conn->ssl == NULL) { |
| 1733 | tls_show_errors(MSG_INFO, __func__, |
| 1734 | "Failed to initialize new SSL connection"); |
| 1735 | os_free(conn); |
| 1736 | return NULL; |
| 1737 | } |
| 1738 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1739 | conn->context = context; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1740 | SSL_set_app_data(conn->ssl, conn); |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1741 | SSL_set_msg_callback(conn->ssl, tls_msg_cb); |
| 1742 | SSL_set_msg_callback_arg(conn->ssl, conn); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1743 | options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | |
| 1744 | SSL_OP_SINGLE_DH_USE; |
| 1745 | #ifdef SSL_OP_NO_COMPRESSION |
| 1746 | options |= SSL_OP_NO_COMPRESSION; |
| 1747 | #endif /* SSL_OP_NO_COMPRESSION */ |
| 1748 | SSL_set_options(conn->ssl, options); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1749 | #ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT |
| 1750 | /* Hopefully there is no need for middlebox compatibility mechanisms |
| 1751 | * when going through EAP authentication. */ |
| 1752 | SSL_clear_options(conn->ssl, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); |
| 1753 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1754 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1755 | #ifdef CONFIG_TESTING_OPTIONS |
| 1756 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER) |
| 1757 | /* Set the keylog file if the admin requested it. */ |
| 1758 | if (getenv("SSLKEYLOGFILE")) |
| 1759 | SSL_CTX_set_keylog_callback(conn->ssl_ctx, tls_keylog_cb); |
| 1760 | #endif |
| 1761 | #endif /* CONFIG_TESTING_OPTIONS */ |
| 1762 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1763 | conn->ssl_in = BIO_new(BIO_s_mem()); |
| 1764 | if (!conn->ssl_in) { |
| 1765 | tls_show_errors(MSG_INFO, __func__, |
| 1766 | "Failed to create a new BIO for ssl_in"); |
| 1767 | SSL_free(conn->ssl); |
| 1768 | os_free(conn); |
| 1769 | return NULL; |
| 1770 | } |
| 1771 | |
| 1772 | conn->ssl_out = BIO_new(BIO_s_mem()); |
| 1773 | if (!conn->ssl_out) { |
| 1774 | tls_show_errors(MSG_INFO, __func__, |
| 1775 | "Failed to create a new BIO for ssl_out"); |
| 1776 | SSL_free(conn->ssl); |
| 1777 | BIO_free(conn->ssl_in); |
| 1778 | os_free(conn); |
| 1779 | return NULL; |
| 1780 | } |
| 1781 | |
| 1782 | SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out); |
| 1783 | |
| 1784 | return conn; |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn) |
| 1789 | { |
| 1790 | if (conn == NULL) |
| 1791 | return; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1792 | if (conn->success_data) { |
| 1793 | /* |
| 1794 | * Make sure ssl_clear_bad_session() does not remove this |
| 1795 | * session. |
| 1796 | */ |
| 1797 | SSL_set_quiet_shutdown(conn->ssl, 1); |
| 1798 | SSL_shutdown(conn->ssl); |
| 1799 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1800 | SSL_free(conn->ssl); |
| 1801 | tls_engine_deinit(conn); |
| 1802 | os_free(conn->subject_match); |
| 1803 | os_free(conn->altsubject_match); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1804 | os_free(conn->suffix_match); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1805 | os_free(conn->domain_match); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1806 | os_free(conn->check_cert_subject); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1807 | os_free(conn->session_ticket); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1808 | os_free(conn->peer_subject); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1809 | os_free(conn); |
| 1810 | } |
| 1811 | |
| 1812 | |
| 1813 | int tls_connection_established(void *ssl_ctx, struct tls_connection *conn) |
| 1814 | { |
| 1815 | return conn ? SSL_is_init_finished(conn->ssl) : 0; |
| 1816 | } |
| 1817 | |
| 1818 | |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 1819 | char * tls_connection_peer_serial_num(void *tls_ctx, |
| 1820 | struct tls_connection *conn) |
| 1821 | { |
| 1822 | ASN1_INTEGER *ser; |
| 1823 | char *serial_num; |
| 1824 | size_t len; |
| 1825 | |
| 1826 | if (!conn->peer_cert) |
| 1827 | return NULL; |
| 1828 | |
| 1829 | ser = X509_get_serialNumber(conn->peer_cert); |
| 1830 | if (!ser) |
| 1831 | return NULL; |
| 1832 | |
| 1833 | len = ASN1_STRING_length(ser) * 2 + 1; |
| 1834 | serial_num = os_malloc(len); |
| 1835 | if (!serial_num) |
| 1836 | return NULL; |
| 1837 | wpa_snprintf_hex_uppercase(serial_num, len, |
| 1838 | ASN1_STRING_get0_data(ser), |
| 1839 | ASN1_STRING_length(ser)); |
| 1840 | return serial_num; |
| 1841 | } |
| 1842 | |
| 1843 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1844 | int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn) |
| 1845 | { |
| 1846 | if (conn == NULL) |
| 1847 | return -1; |
| 1848 | |
| 1849 | /* Shutdown previous TLS connection without notifying the peer |
| 1850 | * because the connection was already terminated in practice |
| 1851 | * and "close notify" shutdown alert would confuse AS. */ |
| 1852 | SSL_set_quiet_shutdown(conn->ssl, 1); |
| 1853 | SSL_shutdown(conn->ssl); |
Jouni Malinen | f291c68 | 2015-08-17 22:50:41 +0300 | [diff] [blame] | 1854 | return SSL_clear(conn->ssl) == 1 ? 0 : -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | |
| 1858 | static int tls_match_altsubject_component(X509 *cert, int type, |
| 1859 | const char *value, size_t len) |
| 1860 | { |
| 1861 | GENERAL_NAME *gen; |
| 1862 | void *ext; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 1863 | int found = 0; |
| 1864 | stack_index_t i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1865 | |
| 1866 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 1867 | |
| 1868 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { |
| 1869 | gen = sk_GENERAL_NAME_value(ext, i); |
| 1870 | if (gen->type != type) |
| 1871 | continue; |
| 1872 | if (os_strlen((char *) gen->d.ia5->data) == len && |
| 1873 | os_memcmp(value, gen->d.ia5->data, len) == 0) |
| 1874 | found++; |
| 1875 | } |
| 1876 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1877 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
| 1878 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1879 | return found; |
| 1880 | } |
| 1881 | |
| 1882 | |
| 1883 | static int tls_match_altsubject(X509 *cert, const char *match) |
| 1884 | { |
| 1885 | int type; |
| 1886 | const char *pos, *end; |
| 1887 | size_t len; |
| 1888 | |
| 1889 | pos = match; |
| 1890 | do { |
| 1891 | if (os_strncmp(pos, "EMAIL:", 6) == 0) { |
| 1892 | type = GEN_EMAIL; |
| 1893 | pos += 6; |
| 1894 | } else if (os_strncmp(pos, "DNS:", 4) == 0) { |
| 1895 | type = GEN_DNS; |
| 1896 | pos += 4; |
| 1897 | } else if (os_strncmp(pos, "URI:", 4) == 0) { |
| 1898 | type = GEN_URI; |
| 1899 | pos += 4; |
| 1900 | } else { |
| 1901 | wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName " |
| 1902 | "match '%s'", pos); |
| 1903 | return 0; |
| 1904 | } |
| 1905 | end = os_strchr(pos, ';'); |
| 1906 | while (end) { |
| 1907 | if (os_strncmp(end + 1, "EMAIL:", 6) == 0 || |
| 1908 | os_strncmp(end + 1, "DNS:", 4) == 0 || |
| 1909 | os_strncmp(end + 1, "URI:", 4) == 0) |
| 1910 | break; |
| 1911 | end = os_strchr(end + 1, ';'); |
| 1912 | } |
| 1913 | if (end) |
| 1914 | len = end - pos; |
| 1915 | else |
| 1916 | len = os_strlen(pos); |
| 1917 | if (tls_match_altsubject_component(cert, type, pos, len) > 0) |
| 1918 | return 1; |
| 1919 | pos = end + 1; |
| 1920 | } while (end); |
| 1921 | |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1926 | #ifndef CONFIG_NATIVE_WINDOWS |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1927 | static int domain_suffix_match(const u8 *val, size_t len, const char *match, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1928 | size_t match_len, int full) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1929 | { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1930 | size_t i; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1931 | |
| 1932 | /* Check for embedded nuls that could mess up suffix matching */ |
| 1933 | for (i = 0; i < len; i++) { |
| 1934 | if (val[i] == '\0') { |
| 1935 | wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject"); |
| 1936 | return 0; |
| 1937 | } |
| 1938 | } |
| 1939 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1940 | if (match_len > len || (full && match_len != len)) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1941 | return 0; |
| 1942 | |
| 1943 | if (os_strncasecmp((const char *) val + len - match_len, match, |
| 1944 | match_len) != 0) |
| 1945 | return 0; /* no match */ |
| 1946 | |
| 1947 | if (match_len == len) |
| 1948 | return 1; /* exact match */ |
| 1949 | |
| 1950 | if (val[len - match_len - 1] == '.') |
| 1951 | return 1; /* full label match completes suffix match */ |
| 1952 | |
| 1953 | wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match"); |
| 1954 | return 0; |
| 1955 | } |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1956 | #endif /* CONFIG_NATIVE_WINDOWS */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1957 | |
| 1958 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1959 | struct tls_dn_field_order_cnt { |
| 1960 | u8 cn; |
| 1961 | u8 c; |
| 1962 | u8 l; |
| 1963 | u8 st; |
| 1964 | u8 o; |
| 1965 | u8 ou; |
| 1966 | u8 email; |
| 1967 | }; |
| 1968 | |
| 1969 | |
| 1970 | static int get_dn_field_index(const struct tls_dn_field_order_cnt *dn_cnt, |
| 1971 | int nid) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1972 | { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1973 | switch (nid) { |
| 1974 | case NID_commonName: |
| 1975 | return dn_cnt->cn; |
| 1976 | case NID_countryName: |
| 1977 | return dn_cnt->c; |
| 1978 | case NID_localityName: |
| 1979 | return dn_cnt->l; |
| 1980 | case NID_stateOrProvinceName: |
| 1981 | return dn_cnt->st; |
| 1982 | case NID_organizationName: |
| 1983 | return dn_cnt->o; |
| 1984 | case NID_organizationalUnitName: |
| 1985 | return dn_cnt->ou; |
| 1986 | case NID_pkcs9_emailAddress: |
| 1987 | return dn_cnt->email; |
| 1988 | default: |
| 1989 | wpa_printf(MSG_ERROR, |
| 1990 | "TLS: Unknown NID '%d' in check_cert_subject", |
| 1991 | nid); |
| 1992 | return -1; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | |
| 1997 | /** |
| 1998 | * match_dn_field - Match configuration DN field against Certificate DN field |
| 1999 | * @cert: Certificate |
| 2000 | * @nid: NID of DN field |
| 2001 | * @field: Field name |
| 2002 | * @value DN field value which is passed from configuration |
| 2003 | * e.g., if configuration have C=US and this argument will point to US. |
| 2004 | * @dn_cnt: DN matching context |
| 2005 | * Returns: 1 on success and 0 on failure |
| 2006 | */ |
| 2007 | static int match_dn_field(const X509 *cert, int nid, const char *field, |
| 2008 | const char *value, |
| 2009 | const struct tls_dn_field_order_cnt *dn_cnt) |
| 2010 | { |
| 2011 | int i, ret = 0, len, config_dn_field_index, match_index = 0; |
| 2012 | X509_NAME *name; |
| 2013 | |
| 2014 | len = os_strlen(value); |
| 2015 | name = X509_get_subject_name((X509 *) cert); |
| 2016 | |
| 2017 | /* Assign incremented cnt for every field of DN to check DN field in |
| 2018 | * right order */ |
| 2019 | config_dn_field_index = get_dn_field_index(dn_cnt, nid); |
| 2020 | if (config_dn_field_index < 0) |
| 2021 | return 0; |
| 2022 | |
| 2023 | /* Fetch value based on NID */ |
| 2024 | for (i = -1; (i = X509_NAME_get_index_by_NID(name, nid, i)) > -1;) { |
| 2025 | X509_NAME_ENTRY *e; |
| 2026 | ASN1_STRING *cn; |
| 2027 | |
| 2028 | e = X509_NAME_get_entry(name, i); |
| 2029 | if (!e) |
| 2030 | continue; |
| 2031 | |
| 2032 | cn = X509_NAME_ENTRY_get_data(e); |
| 2033 | if (!cn) |
| 2034 | continue; |
| 2035 | |
| 2036 | match_index++; |
| 2037 | |
| 2038 | /* check for more than one DN field with same name */ |
| 2039 | if (match_index != config_dn_field_index) |
| 2040 | continue; |
| 2041 | |
| 2042 | /* Check wildcard at the right end side */ |
| 2043 | /* E.g., if OU=develop* mentioned in configuration, allow 'OU' |
| 2044 | * of the subject in the client certificate to start with |
| 2045 | * 'develop' */ |
| 2046 | if (len > 0 && value[len - 1] == '*') { |
| 2047 | /* Compare actual certificate DN field value with |
| 2048 | * configuration DN field value up to the specified |
| 2049 | * length. */ |
| 2050 | ret = ASN1_STRING_length(cn) >= len - 1 && |
| 2051 | os_memcmp(ASN1_STRING_get0_data(cn), value, |
| 2052 | len - 1) == 0; |
| 2053 | } else { |
| 2054 | /* Compare actual certificate DN field value with |
| 2055 | * configuration DN field value */ |
| 2056 | ret = ASN1_STRING_length(cn) == len && |
| 2057 | os_memcmp(ASN1_STRING_get0_data(cn), value, |
| 2058 | len) == 0; |
| 2059 | } |
| 2060 | if (!ret) { |
| 2061 | wpa_printf(MSG_ERROR, |
| 2062 | "OpenSSL: Failed to match %s '%s' with certificate DN field value '%s'", |
| 2063 | field, value, ASN1_STRING_get0_data(cn)); |
| 2064 | } |
| 2065 | break; |
| 2066 | } |
| 2067 | |
| 2068 | return ret; |
| 2069 | } |
| 2070 | |
| 2071 | |
| 2072 | /** |
| 2073 | * get_value_from_field - Get value from DN field |
| 2074 | * @cert: Certificate |
| 2075 | * @field_str: DN field string which is passed from configuration file (e.g., |
| 2076 | * C=US) |
| 2077 | * @dn_cnt: DN matching context |
| 2078 | * Returns: 1 on success and 0 on failure |
| 2079 | */ |
| 2080 | static int get_value_from_field(const X509 *cert, char *field_str, |
| 2081 | struct tls_dn_field_order_cnt *dn_cnt) |
| 2082 | { |
| 2083 | int nid; |
| 2084 | char *context = NULL, *name, *value; |
| 2085 | |
| 2086 | if (os_strcmp(field_str, "*") == 0) |
| 2087 | return 1; /* wildcard matches everything */ |
| 2088 | |
| 2089 | name = str_token(field_str, "=", &context); |
| 2090 | if (!name) |
| 2091 | return 0; |
| 2092 | |
| 2093 | /* Compare all configured DN fields and assign nid based on that to |
| 2094 | * fetch correct value from certificate subject */ |
| 2095 | if (os_strcmp(name, "CN") == 0) { |
| 2096 | nid = NID_commonName; |
| 2097 | dn_cnt->cn++; |
| 2098 | } else if(os_strcmp(name, "C") == 0) { |
| 2099 | nid = NID_countryName; |
| 2100 | dn_cnt->c++; |
| 2101 | } else if (os_strcmp(name, "L") == 0) { |
| 2102 | nid = NID_localityName; |
| 2103 | dn_cnt->l++; |
| 2104 | } else if (os_strcmp(name, "ST") == 0) { |
| 2105 | nid = NID_stateOrProvinceName; |
| 2106 | dn_cnt->st++; |
| 2107 | } else if (os_strcmp(name, "O") == 0) { |
| 2108 | nid = NID_organizationName; |
| 2109 | dn_cnt->o++; |
| 2110 | } else if (os_strcmp(name, "OU") == 0) { |
| 2111 | nid = NID_organizationalUnitName; |
| 2112 | dn_cnt->ou++; |
| 2113 | } else if (os_strcmp(name, "emailAddress") == 0) { |
| 2114 | nid = NID_pkcs9_emailAddress; |
| 2115 | dn_cnt->email++; |
| 2116 | } else { |
| 2117 | wpa_printf(MSG_ERROR, |
| 2118 | "TLS: Unknown field '%s' in check_cert_subject", name); |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
| 2122 | value = str_token(field_str, "=", &context); |
| 2123 | if (!value) { |
| 2124 | wpa_printf(MSG_ERROR, |
| 2125 | "TLS: Distinguished Name field '%s' value is not defined in check_cert_subject", |
| 2126 | name); |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
| 2130 | return match_dn_field(cert, nid, name, value, dn_cnt); |
| 2131 | } |
| 2132 | |
| 2133 | |
| 2134 | /** |
| 2135 | * tls_match_dn_field - Match subject DN field with check_cert_subject |
| 2136 | * @cert: Certificate |
| 2137 | * @match: check_cert_subject string |
| 2138 | * Returns: Return 1 on success and 0 on failure |
| 2139 | */ |
| 2140 | static int tls_match_dn_field(X509 *cert, const char *match) |
| 2141 | { |
| 2142 | const char *token, *last = NULL; |
| 2143 | char field[256]; |
| 2144 | struct tls_dn_field_order_cnt dn_cnt; |
| 2145 | |
| 2146 | os_memset(&dn_cnt, 0, sizeof(dn_cnt)); |
| 2147 | |
| 2148 | /* Maximum length of each DN field is 255 characters */ |
| 2149 | |
| 2150 | /* Process each '/' delimited field */ |
| 2151 | while ((token = cstr_token(match, "/", &last))) { |
| 2152 | if (last - token >= (int) sizeof(field)) { |
| 2153 | wpa_printf(MSG_ERROR, |
| 2154 | "OpenSSL: Too long DN matching field value in '%s'", |
| 2155 | match); |
| 2156 | return 0; |
| 2157 | } |
| 2158 | os_memcpy(field, token, last - token); |
| 2159 | field[last - token] = '\0'; |
| 2160 | |
| 2161 | if (!get_value_from_field(cert, field, &dn_cnt)) { |
| 2162 | wpa_printf(MSG_DEBUG, "OpenSSL: No match for DN '%s'", |
| 2163 | field); |
| 2164 | return 0; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | return 1; |
| 2169 | } |
| 2170 | |
| 2171 | |
| 2172 | #ifndef CONFIG_NATIVE_WINDOWS |
| 2173 | static int tls_match_suffix_helper(X509 *cert, const char *match, |
| 2174 | size_t match_len, int full) |
| 2175 | { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2176 | GENERAL_NAME *gen; |
| 2177 | void *ext; |
| 2178 | int i; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 2179 | stack_index_t j; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2180 | int dns_name = 0; |
| 2181 | X509_NAME *name; |
| 2182 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2183 | wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s", |
| 2184 | full ? "": "suffix ", match); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2185 | |
| 2186 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 2187 | |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 2188 | for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) { |
| 2189 | gen = sk_GENERAL_NAME_value(ext, j); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2190 | if (gen->type != GEN_DNS) |
| 2191 | continue; |
| 2192 | dns_name++; |
| 2193 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName", |
| 2194 | gen->d.dNSName->data, |
| 2195 | gen->d.dNSName->length); |
| 2196 | if (domain_suffix_match(gen->d.dNSName->data, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2197 | gen->d.dNSName->length, |
| 2198 | match, match_len, full) == 1) { |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2199 | wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found", |
| 2200 | full ? "Match" : "Suffix match"); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2201 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2202 | return 1; |
| 2203 | } |
| 2204 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2205 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2206 | |
| 2207 | if (dns_name) { |
| 2208 | wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched"); |
| 2209 | return 0; |
| 2210 | } |
| 2211 | |
| 2212 | name = X509_get_subject_name(cert); |
| 2213 | i = -1; |
| 2214 | for (;;) { |
| 2215 | X509_NAME_ENTRY *e; |
| 2216 | ASN1_STRING *cn; |
| 2217 | |
| 2218 | i = X509_NAME_get_index_by_NID(name, NID_commonName, i); |
| 2219 | if (i == -1) |
| 2220 | break; |
| 2221 | e = X509_NAME_get_entry(name, i); |
| 2222 | if (e == NULL) |
| 2223 | continue; |
| 2224 | cn = X509_NAME_ENTRY_get_data(e); |
| 2225 | if (cn == NULL) |
| 2226 | continue; |
| 2227 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName", |
| 2228 | cn->data, cn->length); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2229 | if (domain_suffix_match(cn->data, cn->length, |
| 2230 | match, match_len, full) == 1) { |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2231 | wpa_printf(MSG_DEBUG, "TLS: %s in commonName found", |
| 2232 | full ? "Match" : "Suffix match"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2233 | return 1; |
| 2234 | } |
| 2235 | } |
| 2236 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2237 | wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found", |
| 2238 | full ? "": "suffix "); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2239 | return 0; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2240 | } |
| 2241 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 2242 | |
| 2243 | |
| 2244 | static int tls_match_suffix(X509 *cert, const char *match, int full) |
| 2245 | { |
| 2246 | #ifdef CONFIG_NATIVE_WINDOWS |
| 2247 | /* wincrypt.h has conflicting X509_NAME definition */ |
| 2248 | return -1; |
| 2249 | #else /* CONFIG_NATIVE_WINDOWS */ |
| 2250 | const char *token, *last = NULL; |
| 2251 | |
| 2252 | /* Process each match alternative separately until a match is found */ |
| 2253 | while ((token = cstr_token(match, ";", &last))) { |
| 2254 | if (tls_match_suffix_helper(cert, token, last - token, full)) |
| 2255 | return 1; |
| 2256 | } |
| 2257 | |
| 2258 | return 0; |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 2259 | #endif /* CONFIG_NATIVE_WINDOWS */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2263 | static enum tls_fail_reason openssl_tls_fail_reason(int err) |
| 2264 | { |
| 2265 | switch (err) { |
| 2266 | case X509_V_ERR_CERT_REVOKED: |
| 2267 | return TLS_FAIL_REVOKED; |
| 2268 | case X509_V_ERR_CERT_NOT_YET_VALID: |
| 2269 | case X509_V_ERR_CRL_NOT_YET_VALID: |
| 2270 | return TLS_FAIL_NOT_YET_VALID; |
| 2271 | case X509_V_ERR_CERT_HAS_EXPIRED: |
| 2272 | case X509_V_ERR_CRL_HAS_EXPIRED: |
| 2273 | return TLS_FAIL_EXPIRED; |
| 2274 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
| 2275 | case X509_V_ERR_UNABLE_TO_GET_CRL: |
| 2276 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: |
| 2277 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 2278 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: |
| 2279 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 2280 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: |
| 2281 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: |
| 2282 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 2283 | case X509_V_ERR_INVALID_CA: |
| 2284 | return TLS_FAIL_UNTRUSTED; |
| 2285 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: |
| 2286 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: |
| 2287 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: |
| 2288 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
| 2289 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
| 2290 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: |
| 2291 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: |
| 2292 | case X509_V_ERR_CERT_UNTRUSTED: |
| 2293 | case X509_V_ERR_CERT_REJECTED: |
| 2294 | return TLS_FAIL_BAD_CERTIFICATE; |
| 2295 | default: |
| 2296 | return TLS_FAIL_UNSPECIFIED; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | |
| 2301 | static struct wpabuf * get_x509_cert(X509 *cert) |
| 2302 | { |
| 2303 | struct wpabuf *buf; |
| 2304 | u8 *tmp; |
| 2305 | |
| 2306 | int cert_len = i2d_X509(cert, NULL); |
| 2307 | if (cert_len <= 0) |
| 2308 | return NULL; |
| 2309 | |
| 2310 | buf = wpabuf_alloc(cert_len); |
| 2311 | if (buf == NULL) |
| 2312 | return NULL; |
| 2313 | |
| 2314 | tmp = wpabuf_put(buf, cert_len); |
| 2315 | i2d_X509(cert, &tmp); |
| 2316 | return buf; |
| 2317 | } |
| 2318 | |
| 2319 | |
| 2320 | static void openssl_tls_fail_event(struct tls_connection *conn, |
| 2321 | X509 *err_cert, int err, int depth, |
| 2322 | const char *subject, const char *err_str, |
| 2323 | enum tls_fail_reason reason) |
| 2324 | { |
| 2325 | union tls_event_data ev; |
| 2326 | struct wpabuf *cert = NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2327 | struct tls_context *context = conn->context; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2328 | |
Pavel Grafov | 4d8552e | 2018-02-06 11:28:29 +0000 | [diff] [blame] | 2329 | #ifdef ANDROID |
| 2330 | log_cert_validation_failure(err_str); |
| 2331 | #endif |
| 2332 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2333 | if (context->event_cb == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2334 | return; |
| 2335 | |
| 2336 | cert = get_x509_cert(err_cert); |
| 2337 | os_memset(&ev, 0, sizeof(ev)); |
| 2338 | ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ? |
| 2339 | reason : openssl_tls_fail_reason(err); |
| 2340 | ev.cert_fail.depth = depth; |
| 2341 | ev.cert_fail.subject = subject; |
| 2342 | ev.cert_fail.reason_txt = err_str; |
| 2343 | ev.cert_fail.cert = cert; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2344 | context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2345 | wpabuf_free(cert); |
| 2346 | } |
| 2347 | |
| 2348 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2349 | static int openssl_cert_tod(X509 *cert) |
| 2350 | { |
| 2351 | CERTIFICATEPOLICIES *ext; |
| 2352 | stack_index_t i; |
| 2353 | char buf[100]; |
| 2354 | int res; |
| 2355 | int tod = 0; |
| 2356 | |
| 2357 | ext = X509_get_ext_d2i(cert, NID_certificate_policies, NULL, NULL); |
| 2358 | if (!ext) |
| 2359 | return 0; |
| 2360 | |
| 2361 | for (i = 0; i < sk_POLICYINFO_num(ext); i++) { |
| 2362 | POLICYINFO *policy; |
| 2363 | |
| 2364 | policy = sk_POLICYINFO_value(ext, i); |
| 2365 | res = OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 0); |
| 2366 | if (res < 0 || (size_t) res >= sizeof(buf)) |
| 2367 | continue; |
| 2368 | wpa_printf(MSG_DEBUG, "OpenSSL: Certificate Policy %s", buf); |
| 2369 | if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.1") == 0) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2370 | tod = 1; /* TOD-STRICT */ |
| 2371 | else if (os_strcmp(buf, "1.3.6.1.4.1.40808.1.3.2") == 0 && !tod) |
| 2372 | tod = 2; /* TOD-TOFU */ |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2373 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2374 | sk_POLICYINFO_pop_free(ext, POLICYINFO_free); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2375 | |
| 2376 | return tod; |
| 2377 | } |
| 2378 | |
| 2379 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2380 | static void openssl_tls_cert_event(struct tls_connection *conn, |
| 2381 | X509 *err_cert, int depth, |
| 2382 | const char *subject) |
| 2383 | { |
| 2384 | struct wpabuf *cert = NULL; |
| 2385 | union tls_event_data ev; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2386 | struct tls_context *context = conn->context; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2387 | char *altsubject[TLS_MAX_ALT_SUBJECT]; |
| 2388 | int alt, num_altsubject = 0; |
| 2389 | GENERAL_NAME *gen; |
| 2390 | void *ext; |
| 2391 | stack_index_t i; |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 2392 | ASN1_INTEGER *ser; |
| 2393 | char serial_num[128]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2394 | #ifdef CONFIG_SHA256 |
| 2395 | u8 hash[32]; |
| 2396 | #endif /* CONFIG_SHA256 */ |
| 2397 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2398 | if (context->event_cb == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2399 | return; |
| 2400 | |
| 2401 | os_memset(&ev, 0, sizeof(ev)); |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 2402 | if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) || |
| 2403 | context->cert_in_cb) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2404 | cert = get_x509_cert(err_cert); |
| 2405 | ev.peer_cert.cert = cert; |
| 2406 | } |
| 2407 | #ifdef CONFIG_SHA256 |
| 2408 | if (cert) { |
| 2409 | const u8 *addr[1]; |
| 2410 | size_t len[1]; |
| 2411 | addr[0] = wpabuf_head(cert); |
| 2412 | len[0] = wpabuf_len(cert); |
| 2413 | if (sha256_vector(1, addr, len, hash) == 0) { |
| 2414 | ev.peer_cert.hash = hash; |
| 2415 | ev.peer_cert.hash_len = sizeof(hash); |
| 2416 | } |
| 2417 | } |
| 2418 | #endif /* CONFIG_SHA256 */ |
| 2419 | ev.peer_cert.depth = depth; |
| 2420 | ev.peer_cert.subject = subject; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2421 | |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 2422 | ser = X509_get_serialNumber(err_cert); |
| 2423 | if (ser) { |
| 2424 | wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num), |
| 2425 | ASN1_STRING_get0_data(ser), |
| 2426 | ASN1_STRING_length(ser)); |
| 2427 | ev.peer_cert.serial_num = serial_num; |
| 2428 | } |
| 2429 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2430 | ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL); |
| 2431 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { |
| 2432 | char *pos; |
| 2433 | |
| 2434 | if (num_altsubject == TLS_MAX_ALT_SUBJECT) |
| 2435 | break; |
| 2436 | gen = sk_GENERAL_NAME_value(ext, i); |
| 2437 | if (gen->type != GEN_EMAIL && |
| 2438 | gen->type != GEN_DNS && |
| 2439 | gen->type != GEN_URI) |
| 2440 | continue; |
| 2441 | |
| 2442 | pos = os_malloc(10 + gen->d.ia5->length + 1); |
| 2443 | if (pos == NULL) |
| 2444 | break; |
| 2445 | altsubject[num_altsubject++] = pos; |
| 2446 | |
| 2447 | switch (gen->type) { |
| 2448 | case GEN_EMAIL: |
| 2449 | os_memcpy(pos, "EMAIL:", 6); |
| 2450 | pos += 6; |
| 2451 | break; |
| 2452 | case GEN_DNS: |
| 2453 | os_memcpy(pos, "DNS:", 4); |
| 2454 | pos += 4; |
| 2455 | break; |
| 2456 | case GEN_URI: |
| 2457 | os_memcpy(pos, "URI:", 4); |
| 2458 | pos += 4; |
| 2459 | break; |
| 2460 | } |
| 2461 | |
| 2462 | os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length); |
| 2463 | pos += gen->d.ia5->length; |
| 2464 | *pos = '\0'; |
| 2465 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2466 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2467 | |
| 2468 | for (alt = 0; alt < num_altsubject; alt++) |
| 2469 | ev.peer_cert.altsubject[alt] = altsubject[alt]; |
| 2470 | ev.peer_cert.num_altsubject = num_altsubject; |
| 2471 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2472 | ev.peer_cert.tod = openssl_cert_tod(err_cert); |
| 2473 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2474 | context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2475 | wpabuf_free(cert); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2476 | for (alt = 0; alt < num_altsubject; alt++) |
| 2477 | os_free(altsubject[alt]); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2481 | static void debug_print_cert(X509 *cert, const char *title) |
| 2482 | { |
| 2483 | #ifndef CONFIG_NO_STDOUT_DEBUG |
| 2484 | BIO *out; |
| 2485 | size_t rlen; |
| 2486 | char *txt; |
| 2487 | int res; |
| 2488 | |
| 2489 | if (wpa_debug_level > MSG_DEBUG) |
| 2490 | return; |
| 2491 | |
| 2492 | out = BIO_new(BIO_s_mem()); |
| 2493 | if (!out) |
| 2494 | return; |
| 2495 | |
| 2496 | X509_print(out, cert); |
| 2497 | rlen = BIO_ctrl_pending(out); |
| 2498 | txt = os_malloc(rlen + 1); |
| 2499 | if (txt) { |
| 2500 | res = BIO_read(out, txt, rlen); |
| 2501 | if (res > 0) { |
| 2502 | txt[res] = '\0'; |
| 2503 | wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt); |
| 2504 | } |
| 2505 | os_free(txt); |
| 2506 | } |
| 2507 | |
| 2508 | BIO_free(out); |
| 2509 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 2510 | } |
| 2511 | |
| 2512 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2513 | static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 2514 | { |
| 2515 | char buf[256]; |
| 2516 | X509 *err_cert; |
| 2517 | int err, depth; |
| 2518 | SSL *ssl; |
| 2519 | struct tls_connection *conn; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2520 | struct tls_context *context; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2521 | char *match, *altmatch, *suffix_match, *domain_match; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2522 | const char *check_cert_subject; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2523 | const char *err_str; |
| 2524 | |
| 2525 | err_cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 2526 | if (!err_cert) |
| 2527 | return 0; |
| 2528 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2529 | err = X509_STORE_CTX_get_error(x509_ctx); |
| 2530 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); |
| 2531 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
| 2532 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2533 | os_snprintf(buf, sizeof(buf), "Peer certificate - depth %d", depth); |
| 2534 | debug_print_cert(err_cert, buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2535 | X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf)); |
| 2536 | |
| 2537 | conn = SSL_get_app_data(ssl); |
| 2538 | if (conn == NULL) |
| 2539 | return 0; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2540 | |
| 2541 | if (depth == 0) |
| 2542 | conn->peer_cert = err_cert; |
| 2543 | else if (depth == 1) |
| 2544 | conn->peer_issuer = err_cert; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2545 | else if (depth == 2) |
| 2546 | conn->peer_issuer_issuer = err_cert; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2547 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2548 | context = conn->context; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2549 | match = conn->subject_match; |
| 2550 | altmatch = conn->altsubject_match; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2551 | suffix_match = conn->suffix_match; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2552 | domain_match = conn->domain_match; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2553 | |
| 2554 | if (!preverify_ok && !conn->ca_cert_verify) |
| 2555 | preverify_ok = 1; |
| 2556 | if (!preverify_ok && depth > 0 && conn->server_cert_only) |
| 2557 | preverify_ok = 1; |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 2558 | if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) && |
| 2559 | (err == X509_V_ERR_CERT_HAS_EXPIRED || |
| 2560 | err == X509_V_ERR_CERT_NOT_YET_VALID)) { |
| 2561 | wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity " |
| 2562 | "time mismatch"); |
| 2563 | preverify_ok = 1; |
| 2564 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2565 | if (!preverify_ok && !conn->data->check_crl_strict && |
| 2566 | (err == X509_V_ERR_CRL_HAS_EXPIRED || |
| 2567 | err == X509_V_ERR_CRL_NOT_YET_VALID)) { |
| 2568 | wpa_printf(MSG_DEBUG, |
| 2569 | "OpenSSL: Ignore certificate validity CRL time mismatch"); |
| 2570 | preverify_ok = 1; |
| 2571 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2572 | |
| 2573 | err_str = X509_verify_cert_error_string(err); |
| 2574 | |
| 2575 | #ifdef CONFIG_SHA256 |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 2576 | /* |
| 2577 | * Do not require preverify_ok so we can explicity allow otherwise |
| 2578 | * invalid pinned server certificates. |
| 2579 | */ |
| 2580 | if (depth == 0 && conn->server_cert_only) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2581 | struct wpabuf *cert; |
| 2582 | cert = get_x509_cert(err_cert); |
| 2583 | if (!cert) { |
| 2584 | wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch " |
| 2585 | "server certificate data"); |
| 2586 | preverify_ok = 0; |
| 2587 | } else { |
| 2588 | u8 hash[32]; |
| 2589 | const u8 *addr[1]; |
| 2590 | size_t len[1]; |
Hai Shalom | ae89c83 | 2023-04-19 15:42:17 -0700 | [diff] [blame] | 2591 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2592 | addr[0] = wpabuf_head(cert); |
| 2593 | len[0] = wpabuf_len(cert); |
| 2594 | if (sha256_vector(1, addr, len, hash) < 0 || |
| 2595 | os_memcmp(conn->srv_cert_hash, hash, 32) != 0) { |
| 2596 | err_str = "Server certificate mismatch"; |
| 2597 | err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; |
| 2598 | preverify_ok = 0; |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 2599 | } else if (!preverify_ok) { |
| 2600 | /* |
| 2601 | * Certificate matches pinned certificate, allow |
| 2602 | * regardless of other problems. |
| 2603 | */ |
| 2604 | wpa_printf(MSG_DEBUG, |
| 2605 | "OpenSSL: Ignore validation issues for a pinned server certificate"); |
| 2606 | preverify_ok = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2607 | } |
| 2608 | wpabuf_free(cert); |
| 2609 | } |
| 2610 | } |
| 2611 | #endif /* CONFIG_SHA256 */ |
| 2612 | |
| 2613 | if (!preverify_ok) { |
Hai Shalom | ae89c83 | 2023-04-19 15:42:17 -0700 | [diff] [blame] | 2614 | /* Send cert events for the peer certificate chain so that |
| 2615 | * the upper layers get information about it even if |
| 2616 | * validation of a CA certificate fails. */ |
| 2617 | STACK_OF(X509) *chain; |
| 2618 | int num_of_certs; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2619 | |
Hai Shalom | ae89c83 | 2023-04-19 15:42:17 -0700 | [diff] [blame] | 2620 | chain = X509_STORE_CTX_get1_chain(x509_ctx); |
| 2621 | num_of_certs = sk_X509_num(chain); |
| 2622 | if (chain && num_of_certs > 0) { |
| 2623 | char buf2[256]; |
| 2624 | X509 *cert; |
| 2625 | int cur_depth; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2626 | |
Hai Shalom | ae89c83 | 2023-04-19 15:42:17 -0700 | [diff] [blame] | 2627 | for (cur_depth = num_of_certs - 1; cur_depth >= 0; cur_depth--) { |
| 2628 | cert = sk_X509_value(chain, cur_depth); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2629 | X509_NAME_oneline(X509_get_subject_name(cert), |
| 2630 | buf2, sizeof(buf2)); |
| 2631 | |
Hai Shalom | ae89c83 | 2023-04-19 15:42:17 -0700 | [diff] [blame] | 2632 | openssl_tls_cert_event(conn, cert, cur_depth, buf2); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2633 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2634 | } |
Hai Shalom | ae89c83 | 2023-04-19 15:42:17 -0700 | [diff] [blame] | 2635 | if (chain) |
| 2636 | sk_X509_pop_free(chain, X509_free); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2637 | |
Gabriel Biren | 60ae068 | 2023-11-01 22:04:12 +0000 | [diff] [blame] | 2638 | char *format_str = "TLS: Certificate verification failed," |
| 2639 | " error %d (%s) depth %d for '%s'"; |
| 2640 | int msg_len = snprintf(NULL, 0, format_str, err, err_str, depth, buf) + 1; |
| 2641 | char *msg = os_malloc(msg_len); |
| 2642 | snprintf(msg, msg_len, format_str, err, err_str, depth, buf); |
| 2643 | |
| 2644 | wpa_printf(MSG_WARNING, "%s", msg); |
| 2645 | if (conn != NULL && conn->context != NULL |
| 2646 | && openssl_failure_callback_global != NULL) { |
| 2647 | (*openssl_failure_callback_global)(conn->context->cb_ctx, msg); |
| 2648 | } |
| 2649 | os_free(msg); |
| 2650 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2651 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2652 | err_str, TLS_FAIL_UNSPECIFIED); |
| 2653 | return preverify_ok; |
| 2654 | } |
| 2655 | |
Hai Shalom | ae89c83 | 2023-04-19 15:42:17 -0700 | [diff] [blame] | 2656 | openssl_tls_cert_event(conn, err_cert, depth, buf); |
| 2657 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2658 | wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d " |
| 2659 | "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'", |
| 2660 | preverify_ok, err, err_str, |
| 2661 | conn->ca_cert_verify, depth, buf); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2662 | check_cert_subject = conn->check_cert_subject; |
| 2663 | if (!check_cert_subject) |
| 2664 | check_cert_subject = conn->data->check_cert_subject; |
| 2665 | if (check_cert_subject) { |
| 2666 | if (depth == 0 && |
| 2667 | !tls_match_dn_field(err_cert, check_cert_subject)) { |
| 2668 | preverify_ok = 0; |
| 2669 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2670 | "Distinguished Name", |
| 2671 | TLS_FAIL_DN_MISMATCH); |
| 2672 | } |
| 2673 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2674 | if (depth == 0 && match && os_strstr(buf, match) == NULL) { |
| 2675 | wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not " |
| 2676 | "match with '%s'", buf, match); |
| 2677 | preverify_ok = 0; |
| 2678 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2679 | "Subject mismatch", |
| 2680 | TLS_FAIL_SUBJECT_MISMATCH); |
| 2681 | } else if (depth == 0 && altmatch && |
| 2682 | !tls_match_altsubject(err_cert, altmatch)) { |
| 2683 | wpa_printf(MSG_WARNING, "TLS: altSubjectName match " |
| 2684 | "'%s' not found", altmatch); |
| 2685 | preverify_ok = 0; |
| 2686 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2687 | "AltSubject mismatch", |
| 2688 | TLS_FAIL_ALTSUBJECT_MISMATCH); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2689 | } else if (depth == 0 && suffix_match && |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2690 | !tls_match_suffix(err_cert, suffix_match, 0)) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2691 | wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found", |
| 2692 | suffix_match); |
| 2693 | preverify_ok = 0; |
| 2694 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2695 | "Domain suffix mismatch", |
| 2696 | TLS_FAIL_DOMAIN_SUFFIX_MISMATCH); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2697 | } else if (depth == 0 && domain_match && |
| 2698 | !tls_match_suffix(err_cert, domain_match, 1)) { |
| 2699 | wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found", |
| 2700 | domain_match); |
| 2701 | preverify_ok = 0; |
| 2702 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2703 | "Domain mismatch", |
| 2704 | TLS_FAIL_DOMAIN_MISMATCH); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2705 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2706 | |
| 2707 | if (conn->cert_probe && preverify_ok && depth == 0) { |
| 2708 | wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate " |
| 2709 | "on probe-only run"); |
| 2710 | preverify_ok = 0; |
| 2711 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2712 | "Server certificate chain probe", |
| 2713 | TLS_FAIL_SERVER_CHAIN_PROBE); |
| 2714 | } |
| 2715 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2716 | #ifdef CONFIG_SUITEB |
| 2717 | if (conn->flags & TLS_CONN_SUITEB) { |
| 2718 | EVP_PKEY *pk; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2719 | int len = -1; |
| 2720 | |
| 2721 | pk = X509_get_pubkey(err_cert); |
| 2722 | if (pk) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2723 | len = EVP_PKEY_bits(pk); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2724 | EVP_PKEY_free(pk); |
| 2725 | } |
| 2726 | |
| 2727 | if (len >= 0) { |
| 2728 | wpa_printf(MSG_DEBUG, |
| 2729 | "OpenSSL: RSA modulus size: %d bits", len); |
| 2730 | if (len < 3072) { |
| 2731 | preverify_ok = 0; |
| 2732 | openssl_tls_fail_event( |
| 2733 | conn, err_cert, err, |
| 2734 | depth, buf, |
| 2735 | "Insufficient RSA modulus size", |
| 2736 | TLS_FAIL_INSUFFICIENT_KEY_LEN); |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | #endif /* CONFIG_SUITEB */ |
| 2741 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2742 | #ifdef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2743 | if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) && |
| 2744 | preverify_ok) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2745 | enum ocsp_result res; |
| 2746 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2747 | res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert, |
| 2748 | conn->peer_issuer, |
| 2749 | conn->peer_issuer_issuer); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2750 | if (res == OCSP_REVOKED) { |
| 2751 | preverify_ok = 0; |
| 2752 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2753 | "certificate revoked", |
| 2754 | TLS_FAIL_REVOKED); |
| 2755 | if (err == X509_V_OK) |
| 2756 | X509_STORE_CTX_set_error( |
| 2757 | x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 2758 | } else if (res != OCSP_GOOD && |
| 2759 | (conn->flags & TLS_CONN_REQUIRE_OCSP)) { |
| 2760 | preverify_ok = 0; |
| 2761 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2762 | "bad certificate status response", |
| 2763 | TLS_FAIL_UNSPECIFIED); |
| 2764 | } |
| 2765 | } |
| 2766 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 2767 | |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 2768 | if (depth == 0 && preverify_ok && context->event_cb != NULL) |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2769 | context->event_cb(context->cb_ctx, |
| 2770 | TLS_CERT_CHAIN_SUCCESS, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2771 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2772 | if (depth == 0 && preverify_ok) { |
| 2773 | os_free(conn->peer_subject); |
| 2774 | conn->peer_subject = os_strdup(buf); |
| 2775 | } |
| 2776 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2777 | return preverify_ok; |
| 2778 | } |
| 2779 | |
| 2780 | |
| 2781 | #ifndef OPENSSL_NO_STDIO |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2782 | static int tls_load_ca_der(struct tls_data *data, const char *ca_cert) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2783 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2784 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2785 | X509_LOOKUP *lookup; |
| 2786 | int ret = 0; |
| 2787 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2788 | lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx), |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2789 | X509_LOOKUP_file()); |
| 2790 | if (lookup == NULL) { |
| 2791 | tls_show_errors(MSG_WARNING, __func__, |
| 2792 | "Failed add lookup for X509 store"); |
| 2793 | return -1; |
| 2794 | } |
| 2795 | |
| 2796 | if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) { |
| 2797 | unsigned long err = ERR_peek_error(); |
| 2798 | tls_show_errors(MSG_WARNING, __func__, |
| 2799 | "Failed load CA in DER format"); |
| 2800 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && |
| 2801 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 2802 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " |
| 2803 | "cert already in hash table error", |
| 2804 | __func__); |
| 2805 | } else |
| 2806 | ret = -1; |
| 2807 | } |
| 2808 | |
| 2809 | return ret; |
| 2810 | } |
| 2811 | #endif /* OPENSSL_NO_STDIO */ |
| 2812 | |
| 2813 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2814 | static int tls_connection_ca_cert(struct tls_data *data, |
| 2815 | struct tls_connection *conn, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2816 | const char *ca_cert, const u8 *ca_cert_blob, |
| 2817 | size_t ca_cert_blob_len, const char *ca_path) |
| 2818 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2819 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2820 | X509_STORE *store; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2821 | |
| 2822 | /* |
| 2823 | * Remove previously configured trusted CA certificates before adding |
| 2824 | * new ones. |
| 2825 | */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2826 | store = X509_STORE_new(); |
| 2827 | if (store == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2828 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " |
| 2829 | "certificate store", __func__); |
| 2830 | return -1; |
| 2831 | } |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2832 | SSL_CTX_set_cert_store(ssl_ctx, store); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2833 | |
| 2834 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
| 2835 | conn->ca_cert_verify = 1; |
| 2836 | |
| 2837 | if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) { |
| 2838 | wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate " |
| 2839 | "chain"); |
| 2840 | conn->cert_probe = 1; |
| 2841 | conn->ca_cert_verify = 0; |
| 2842 | return 0; |
| 2843 | } |
| 2844 | |
| 2845 | if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) { |
| 2846 | #ifdef CONFIG_SHA256 |
| 2847 | const char *pos = ca_cert + 7; |
| 2848 | if (os_strncmp(pos, "server/sha256/", 14) != 0) { |
| 2849 | wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert " |
| 2850 | "hash value '%s'", ca_cert); |
| 2851 | return -1; |
| 2852 | } |
| 2853 | pos += 14; |
| 2854 | if (os_strlen(pos) != 32 * 2) { |
| 2855 | wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 " |
| 2856 | "hash length in ca_cert '%s'", ca_cert); |
| 2857 | return -1; |
| 2858 | } |
| 2859 | if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) { |
| 2860 | wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash " |
| 2861 | "value in ca_cert '%s'", ca_cert); |
| 2862 | return -1; |
| 2863 | } |
| 2864 | conn->server_cert_only = 1; |
| 2865 | wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server " |
| 2866 | "certificate match"); |
| 2867 | return 0; |
| 2868 | #else /* CONFIG_SHA256 */ |
| 2869 | wpa_printf(MSG_INFO, "No SHA256 included in the build - " |
| 2870 | "cannot validate server certificate hash"); |
| 2871 | return -1; |
| 2872 | #endif /* CONFIG_SHA256 */ |
| 2873 | } |
| 2874 | |
| 2875 | if (ca_cert_blob) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2876 | X509 *cert = d2i_X509(NULL, |
| 2877 | (const unsigned char **) &ca_cert_blob, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2878 | ca_cert_blob_len); |
| 2879 | if (cert == NULL) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2880 | BIO *bio = BIO_new_mem_buf(ca_cert_blob, |
| 2881 | ca_cert_blob_len); |
| 2882 | |
| 2883 | if (bio) { |
| 2884 | cert = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 2885 | BIO_free(bio); |
| 2886 | } |
| 2887 | |
| 2888 | if (!cert) { |
| 2889 | tls_show_errors(MSG_WARNING, __func__, |
| 2890 | "Failed to parse ca_cert_blob"); |
| 2891 | return -1; |
| 2892 | } |
| 2893 | |
| 2894 | while (ERR_get_error()) { |
| 2895 | /* Ignore errors from DER conversion. */ |
| 2896 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2897 | } |
| 2898 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2899 | if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx), |
| 2900 | cert)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2901 | unsigned long err = ERR_peek_error(); |
| 2902 | tls_show_errors(MSG_WARNING, __func__, |
| 2903 | "Failed to add ca_cert_blob to " |
| 2904 | "certificate store"); |
| 2905 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && |
| 2906 | ERR_GET_REASON(err) == |
| 2907 | X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 2908 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " |
| 2909 | "cert already in hash table error", |
| 2910 | __func__); |
| 2911 | } else { |
| 2912 | X509_free(cert); |
| 2913 | return -1; |
| 2914 | } |
| 2915 | } |
| 2916 | X509_free(cert); |
| 2917 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob " |
| 2918 | "to certificate store", __func__); |
| 2919 | return 0; |
| 2920 | } |
| 2921 | |
| 2922 | #ifdef ANDROID |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2923 | /* Single alias */ |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 2924 | if (ca_cert && os_strncmp(ANDROID_KEYSTORE_PREFIX, ca_cert, |
| 2925 | ANDROID_KEYSTORE_PREFIX_LEN) == 0) { |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2926 | if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx), |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 2927 | &ca_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2928 | return -1; |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2929 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
| 2930 | return 0; |
| 2931 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2932 | |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2933 | /* Multiple aliases separated by space */ |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 2934 | if (ca_cert && os_strncmp(ANDROID_KEYSTORE_ENCODED_PREFIX, ca_cert, |
| 2935 | ANDROID_KEYSTORE_ENCODED_PREFIX_LEN) == 0) { |
| 2936 | char *aliases = os_strdup( |
| 2937 | &ca_cert[ANDROID_KEYSTORE_ENCODED_PREFIX_LEN]); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2938 | const char *delim = " "; |
| 2939 | int rc = 0; |
| 2940 | char *savedptr; |
| 2941 | char *alias; |
| 2942 | |
| 2943 | if (!aliases) |
| 2944 | return -1; |
| 2945 | alias = strtok_r(aliases, delim, &savedptr); |
| 2946 | for (; alias; alias = strtok_r(NULL, delim, &savedptr)) { |
| 2947 | if (tls_add_ca_from_keystore_encoded( |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 2948 | SSL_CTX_get_cert_store(ssl_ctx), alias, conn)) { |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 2949 | wpa_printf(MSG_ERROR, |
| 2950 | "OpenSSL: Failed to add ca_cert %s from keystore", |
| 2951 | alias); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2952 | rc = -1; |
| 2953 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2954 | } |
| 2955 | } |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2956 | os_free(aliases); |
| 2957 | if (rc) |
| 2958 | return rc; |
| 2959 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2960 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
| 2961 | return 0; |
| 2962 | } |
| 2963 | #endif /* ANDROID */ |
| 2964 | |
| 2965 | #ifdef CONFIG_NATIVE_WINDOWS |
| 2966 | if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) == |
| 2967 | 0) { |
| 2968 | wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from " |
| 2969 | "system certificate store"); |
| 2970 | return 0; |
| 2971 | } |
| 2972 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 2973 | |
| 2974 | if (ca_cert || ca_path) { |
| 2975 | #ifndef OPENSSL_NO_STDIO |
| 2976 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) != |
| 2977 | 1) { |
| 2978 | tls_show_errors(MSG_WARNING, __func__, |
| 2979 | "Failed to load root certificates"); |
| 2980 | if (ca_cert && |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2981 | tls_load_ca_der(data, ca_cert) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2982 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded " |
| 2983 | "DER format CA certificate", |
| 2984 | __func__); |
| 2985 | } else |
| 2986 | return -1; |
| 2987 | } else { |
| 2988 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " |
| 2989 | "certificate(s) loaded"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2990 | tls_get_errors(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2991 | } |
| 2992 | #else /* OPENSSL_NO_STDIO */ |
| 2993 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", |
| 2994 | __func__); |
| 2995 | return -1; |
| 2996 | #endif /* OPENSSL_NO_STDIO */ |
| 2997 | } else { |
| 2998 | /* No ca_cert configured - do not try to verify server |
| 2999 | * certificate */ |
| 3000 | conn->ca_cert_verify = 0; |
| 3001 | } |
| 3002 | |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
| 3006 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3007 | static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3008 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3009 | SSL_CTX *ssl_ctx = data->ssl; |
| 3010 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3011 | if (ca_cert) { |
| 3012 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1) |
| 3013 | { |
| 3014 | tls_show_errors(MSG_WARNING, __func__, |
| 3015 | "Failed to load root certificates"); |
| 3016 | return -1; |
| 3017 | } |
| 3018 | |
| 3019 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " |
| 3020 | "certificate(s) loaded"); |
| 3021 | |
| 3022 | #ifndef OPENSSL_NO_STDIO |
| 3023 | /* Add the same CAs to the client certificate requests */ |
| 3024 | SSL_CTX_set_client_CA_list(ssl_ctx, |
| 3025 | SSL_load_client_CA_file(ca_cert)); |
| 3026 | #endif /* OPENSSL_NO_STDIO */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3027 | |
| 3028 | os_free(data->ca_cert); |
| 3029 | data->ca_cert = os_strdup(ca_cert); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3036 | int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3037 | { |
| 3038 | int flags; |
| 3039 | |
| 3040 | if (check_crl) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3041 | struct tls_data *data = ssl_ctx; |
| 3042 | X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3043 | if (cs == NULL) { |
| 3044 | tls_show_errors(MSG_INFO, __func__, "Failed to get " |
| 3045 | "certificate store when enabling " |
| 3046 | "check_crl"); |
| 3047 | return -1; |
| 3048 | } |
| 3049 | flags = X509_V_FLAG_CRL_CHECK; |
| 3050 | if (check_crl == 2) |
| 3051 | flags |= X509_V_FLAG_CRL_CHECK_ALL; |
| 3052 | X509_STORE_set_flags(cs, flags); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3053 | |
| 3054 | data->check_crl = check_crl; |
| 3055 | data->check_crl_strict = strict; |
| 3056 | os_get_reltime(&data->crl_last_reload); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3057 | } |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
| 3061 | |
| 3062 | static int tls_connection_set_subject_match(struct tls_connection *conn, |
| 3063 | const char *subject_match, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 3064 | const char *altsubject_match, |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 3065 | const char *suffix_match, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3066 | const char *domain_match, |
| 3067 | const char *check_cert_subject) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3068 | { |
| 3069 | os_free(conn->subject_match); |
| 3070 | conn->subject_match = NULL; |
| 3071 | if (subject_match) { |
| 3072 | conn->subject_match = os_strdup(subject_match); |
| 3073 | if (conn->subject_match == NULL) |
| 3074 | return -1; |
| 3075 | } |
| 3076 | |
| 3077 | os_free(conn->altsubject_match); |
| 3078 | conn->altsubject_match = NULL; |
| 3079 | if (altsubject_match) { |
| 3080 | conn->altsubject_match = os_strdup(altsubject_match); |
| 3081 | if (conn->altsubject_match == NULL) |
| 3082 | return -1; |
| 3083 | } |
| 3084 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 3085 | os_free(conn->suffix_match); |
| 3086 | conn->suffix_match = NULL; |
| 3087 | if (suffix_match) { |
| 3088 | conn->suffix_match = os_strdup(suffix_match); |
| 3089 | if (conn->suffix_match == NULL) |
| 3090 | return -1; |
| 3091 | } |
| 3092 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 3093 | os_free(conn->domain_match); |
| 3094 | conn->domain_match = NULL; |
| 3095 | if (domain_match) { |
| 3096 | conn->domain_match = os_strdup(domain_match); |
| 3097 | if (conn->domain_match == NULL) |
| 3098 | return -1; |
| 3099 | } |
| 3100 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3101 | os_free(conn->check_cert_subject); |
| 3102 | conn->check_cert_subject = NULL; |
| 3103 | if (check_cert_subject) { |
| 3104 | conn->check_cert_subject = os_strdup(check_cert_subject); |
| 3105 | if (!conn->check_cert_subject) |
| 3106 | return -1; |
| 3107 | } |
| 3108 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3109 | return 0; |
| 3110 | } |
| 3111 | |
| 3112 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3113 | #ifdef CONFIG_SUITEB |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3114 | static int suiteb_cert_cb(SSL *ssl, void *arg) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3115 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3116 | struct tls_connection *conn = arg; |
| 3117 | |
| 3118 | /* |
| 3119 | * This cert_cb() is not really the best location for doing a |
| 3120 | * constraint check for the ServerKeyExchange message, but this seems to |
| 3121 | * be the only place where the current OpenSSL sequence can be |
| 3122 | * terminated cleanly with an TLS alert going out to the server. |
| 3123 | */ |
| 3124 | |
| 3125 | if (!(conn->flags & TLS_CONN_SUITEB)) |
| 3126 | return 1; |
| 3127 | |
| 3128 | /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */ |
| 3129 | if (conn->cipher_suite != 0x9f) |
| 3130 | return 1; |
| 3131 | |
| 3132 | if (conn->server_dh_prime_len >= 3072) |
| 3133 | return 1; |
| 3134 | |
| 3135 | wpa_printf(MSG_DEBUG, |
| 3136 | "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake", |
| 3137 | conn->server_dh_prime_len); |
| 3138 | return 0; |
| 3139 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3140 | #endif /* CONFIG_SUITEB */ |
| 3141 | |
| 3142 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3143 | static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags, |
| 3144 | const char *openssl_ciphers) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3145 | { |
| 3146 | SSL *ssl = conn->ssl; |
| 3147 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3148 | #ifdef SSL_OP_NO_TICKET |
| 3149 | if (flags & TLS_CONN_DISABLE_SESSION_TICKET) |
| 3150 | SSL_set_options(ssl, SSL_OP_NO_TICKET); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3151 | else |
| 3152 | SSL_clear_options(ssl, SSL_OP_NO_TICKET); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3153 | #endif /* SSL_OP_NO_TICKET */ |
| 3154 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3155 | #ifdef SSL_OP_LEGACY_SERVER_CONNECT |
| 3156 | if (flags & TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION) |
| 3157 | SSL_set_options(ssl, SSL_OP_LEGACY_SERVER_CONNECT); |
| 3158 | #endif /* SSL_OP_LEGACY_SERVER_CONNECT */ |
| 3159 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3160 | #ifdef SSL_OP_NO_TLSv1 |
| 3161 | if (flags & TLS_CONN_DISABLE_TLSv1_0) |
| 3162 | SSL_set_options(ssl, SSL_OP_NO_TLSv1); |
| 3163 | else |
| 3164 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1); |
| 3165 | #endif /* SSL_OP_NO_TLSv1 */ |
| 3166 | #ifdef SSL_OP_NO_TLSv1_1 |
| 3167 | if (flags & TLS_CONN_DISABLE_TLSv1_1) |
| 3168 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_1); |
| 3169 | else |
| 3170 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1); |
| 3171 | #endif /* SSL_OP_NO_TLSv1_1 */ |
| 3172 | #ifdef SSL_OP_NO_TLSv1_2 |
| 3173 | if (flags & TLS_CONN_DISABLE_TLSv1_2) |
| 3174 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_2); |
| 3175 | else |
| 3176 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2); |
| 3177 | #endif /* SSL_OP_NO_TLSv1_2 */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3178 | #ifdef SSL_OP_NO_TLSv1_3 |
| 3179 | if (flags & TLS_CONN_DISABLE_TLSv1_3) |
| 3180 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_3); |
| 3181 | else |
| 3182 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3); |
| 3183 | #endif /* SSL_OP_NO_TLSv1_3 */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3184 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 3185 | if (flags & (TLS_CONN_ENABLE_TLSv1_0 | |
| 3186 | TLS_CONN_ENABLE_TLSv1_1 | |
| 3187 | TLS_CONN_ENABLE_TLSv1_2)) { |
| 3188 | int version = 0; |
| 3189 | |
| 3190 | /* Explicit request to enable TLS versions even if needing to |
| 3191 | * override systemwide policies. */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3192 | if (flags & TLS_CONN_ENABLE_TLSv1_0) |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3193 | version = TLS1_VERSION; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3194 | else if (flags & TLS_CONN_ENABLE_TLSv1_1) |
| 3195 | version = TLS1_1_VERSION; |
| 3196 | else if (flags & TLS_CONN_ENABLE_TLSv1_2) |
| 3197 | version = TLS1_2_VERSION; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3198 | if (!version) { |
| 3199 | wpa_printf(MSG_DEBUG, |
| 3200 | "OpenSSL: Invalid TLS version configuration"); |
| 3201 | return -1; |
| 3202 | } |
| 3203 | |
| 3204 | if (SSL_set_min_proto_version(ssl, version) != 1) { |
| 3205 | wpa_printf(MSG_DEBUG, |
| 3206 | "OpenSSL: Failed to set minimum TLS version"); |
| 3207 | return -1; |
| 3208 | } |
| 3209 | } |
| 3210 | #endif /* >= 1.1.0 */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3211 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \ |
| 3212 | !defined(LIBRESSL_VERSION_NUMBER) && \ |
| 3213 | !defined(OPENSSL_IS_BORINGSSL) |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3214 | { |
| 3215 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3216 | int need_level = 0; |
| 3217 | #else |
| 3218 | int need_level = 1; |
| 3219 | #endif |
| 3220 | |
| 3221 | if ((flags & |
| 3222 | (TLS_CONN_ENABLE_TLSv1_0 | TLS_CONN_ENABLE_TLSv1_1)) && |
| 3223 | SSL_get_security_level(ssl) > need_level) { |
| 3224 | /* |
| 3225 | * Need to drop to security level 1 (or 0 with OpenSSL |
| 3226 | * 3.0) to allow TLS versions older than 1.2 to be used |
| 3227 | * when explicitly enabled in configuration. |
| 3228 | */ |
| 3229 | SSL_set_security_level(conn->ssl, need_level); |
| 3230 | } |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3231 | } |
| 3232 | #endif |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3233 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3234 | #ifdef CONFIG_SUITEB |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3235 | #ifdef OPENSSL_IS_BORINGSSL |
| 3236 | /* Start with defaults from BoringSSL */ |
Jimmy Chen | 916e0a7 | 2022-01-11 15:19:46 +0800 | [diff] [blame] | 3237 | SSL_set_verify_algorithm_prefs(conn->ssl, NULL, 0); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3238 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3239 | if (flags & TLS_CONN_SUITEB_NO_ECDH) { |
| 3240 | const char *ciphers = "DHE-RSA-AES256-GCM-SHA384"; |
| 3241 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3242 | if (openssl_ciphers) { |
| 3243 | wpa_printf(MSG_DEBUG, |
| 3244 | "OpenSSL: Override ciphers for Suite B (no ECDH): %s", |
| 3245 | openssl_ciphers); |
| 3246 | ciphers = openssl_ciphers; |
| 3247 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3248 | if (SSL_set_cipher_list(ssl, ciphers) != 1) { |
| 3249 | wpa_printf(MSG_INFO, |
| 3250 | "OpenSSL: Failed to set Suite B ciphers"); |
| 3251 | return -1; |
| 3252 | } |
| 3253 | } else if (flags & TLS_CONN_SUITEB) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3254 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3255 | EC_KEY *ecdh; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3256 | #endif |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3257 | const char *ciphers = |
| 3258 | "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384"; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3259 | int nid[1] = { NID_secp384r1 }; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3260 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3261 | if (openssl_ciphers) { |
| 3262 | wpa_printf(MSG_DEBUG, |
| 3263 | "OpenSSL: Override ciphers for Suite B: %s", |
| 3264 | openssl_ciphers); |
| 3265 | ciphers = openssl_ciphers; |
| 3266 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3267 | if (SSL_set_cipher_list(ssl, ciphers) != 1) { |
| 3268 | wpa_printf(MSG_INFO, |
| 3269 | "OpenSSL: Failed to set Suite B ciphers"); |
| 3270 | return -1; |
| 3271 | } |
| 3272 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3273 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3274 | if (SSL_set1_groups(ssl, nid, 1) != 1) { |
| 3275 | wpa_printf(MSG_INFO, |
| 3276 | "OpenSSL: Failed to set Suite B groups"); |
| 3277 | return -1; |
| 3278 | } |
| 3279 | |
| 3280 | #else |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3281 | if (SSL_set1_curves(ssl, nid, 1) != 1) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3282 | wpa_printf(MSG_INFO, |
| 3283 | "OpenSSL: Failed to set Suite B curves"); |
| 3284 | return -1; |
| 3285 | } |
| 3286 | |
| 3287 | ecdh = EC_KEY_new_by_curve_name(NID_secp384r1); |
| 3288 | if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) { |
| 3289 | EC_KEY_free(ecdh); |
| 3290 | wpa_printf(MSG_INFO, |
| 3291 | "OpenSSL: Failed to set ECDH parameter"); |
| 3292 | return -1; |
| 3293 | } |
| 3294 | EC_KEY_free(ecdh); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3295 | #endif |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3296 | } |
Hai Shalom | 0f94b7a | 2023-03-13 13:22:35 -0700 | [diff] [blame] | 3297 | if ((flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) |
| 3298 | #ifdef EAP_TLSV1_3 |
| 3299 | && (flags & TLS_CONN_DISABLE_TLSv1_3) |
| 3300 | #endif |
| 3301 | ) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3302 | #ifdef OPENSSL_IS_BORINGSSL |
| 3303 | uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 }; |
| 3304 | |
Jimmy Chen | 916e0a7 | 2022-01-11 15:19:46 +0800 | [diff] [blame] | 3305 | if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3306 | 1) != 1) { |
| 3307 | wpa_printf(MSG_INFO, |
| 3308 | "OpenSSL: Failed to set Suite B sigalgs"); |
| 3309 | return -1; |
| 3310 | } |
| 3311 | #else /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3312 | /* ECDSA+SHA384 if need to add EC support here */ |
| 3313 | if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) { |
| 3314 | wpa_printf(MSG_INFO, |
| 3315 | "OpenSSL: Failed to set Suite B sigalgs"); |
| 3316 | return -1; |
| 3317 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3318 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3319 | |
| 3320 | SSL_set_options(ssl, SSL_OP_NO_TLSv1); |
| 3321 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_1); |
| 3322 | SSL_set_cert_cb(ssl, suiteb_cert_cb, conn); |
| 3323 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3324 | |
| 3325 | #ifdef OPENSSL_IS_BORINGSSL |
| 3326 | if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) { |
| 3327 | uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 }; |
| 3328 | int nid[1] = { NID_secp384r1 }; |
| 3329 | |
| 3330 | if (SSL_set1_curves(ssl, nid, 1) != 1) { |
| 3331 | wpa_printf(MSG_INFO, |
| 3332 | "OpenSSL: Failed to set Suite B curves"); |
| 3333 | return -1; |
| 3334 | } |
| 3335 | |
Jimmy Chen | 916e0a7 | 2022-01-11 15:19:46 +0800 | [diff] [blame] | 3336 | if (SSL_set_verify_algorithm_prefs(conn->ssl, sigalgs, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3337 | 1) != 1) { |
| 3338 | wpa_printf(MSG_INFO, |
| 3339 | "OpenSSL: Failed to set Suite B sigalgs"); |
| 3340 | return -1; |
| 3341 | } |
| 3342 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3343 | #else /* OPENSSL_IS_BORINGSSL */ |
| 3344 | if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) && |
| 3345 | openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) { |
| 3346 | wpa_printf(MSG_INFO, |
| 3347 | "OpenSSL: Failed to set openssl_ciphers '%s'", |
| 3348 | openssl_ciphers); |
| 3349 | return -1; |
| 3350 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3351 | #endif /* OPENSSL_IS_BORINGSSL */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3352 | #else /* CONFIG_SUITEB */ |
| 3353 | if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) { |
| 3354 | wpa_printf(MSG_INFO, |
| 3355 | "OpenSSL: Failed to set openssl_ciphers '%s'", |
| 3356 | openssl_ciphers); |
| 3357 | return -1; |
| 3358 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3359 | #endif /* CONFIG_SUITEB */ |
| 3360 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 3361 | if (flags & TLS_CONN_TEAP_ANON_DH) { |
| 3362 | #ifndef TEAP_DH_ANON_CS |
| 3363 | #define TEAP_DH_ANON_CS \ |
| 3364 | "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:" \ |
| 3365 | "ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:" \ |
| 3366 | "ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:" \ |
| 3367 | "DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \ |
| 3368 | "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:" \ |
| 3369 | "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" \ |
| 3370 | "ADH-AES256-GCM-SHA384:ADH-AES128-GCM-SHA256:" \ |
| 3371 | "ADH-AES256-SHA256:ADH-AES128-SHA256:ADH-AES256-SHA:ADH-AES128-SHA" |
| 3372 | #endif |
| 3373 | static const char *cs = TEAP_DH_ANON_CS; |
| 3374 | |
| 3375 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \ |
| 3376 | !defined(LIBRESSL_VERSION_NUMBER) && \ |
| 3377 | !defined(OPENSSL_IS_BORINGSSL) |
| 3378 | /* |
| 3379 | * Need to drop to security level 0 to allow anonymous |
| 3380 | * cipher suites for EAP-TEAP. |
| 3381 | */ |
| 3382 | SSL_set_security_level(conn->ssl, 0); |
| 3383 | #endif |
| 3384 | |
| 3385 | wpa_printf(MSG_DEBUG, |
| 3386 | "OpenSSL: Enable cipher suites for anonymous EAP-TEAP provisioning: %s", |
| 3387 | cs); |
| 3388 | if (SSL_set_cipher_list(conn->ssl, cs) != 1) { |
| 3389 | tls_show_errors(MSG_INFO, __func__, |
| 3390 | "Cipher suite configuration failed"); |
| 3391 | return -1; |
| 3392 | } |
| 3393 | } |
| 3394 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3395 | return 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3399 | int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3400 | int verify_peer, unsigned int flags, |
| 3401 | const u8 *session_ctx, size_t session_ctx_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3402 | { |
| 3403 | static int counter = 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3404 | struct tls_data *data = ssl_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3405 | |
| 3406 | if (conn == NULL) |
| 3407 | return -1; |
| 3408 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3409 | if (verify_peer == 2) { |
| 3410 | conn->ca_cert_verify = 1; |
| 3411 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER | |
| 3412 | SSL_VERIFY_CLIENT_ONCE, tls_verify_cb); |
| 3413 | } else if (verify_peer) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3414 | conn->ca_cert_verify = 1; |
| 3415 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER | |
| 3416 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | |
| 3417 | SSL_VERIFY_CLIENT_ONCE, tls_verify_cb); |
| 3418 | } else { |
| 3419 | conn->ca_cert_verify = 0; |
| 3420 | SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL); |
| 3421 | } |
| 3422 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3423 | if (tls_set_conn_flags(conn, flags, NULL) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3424 | return -1; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3425 | conn->flags = flags; |
| 3426 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3427 | SSL_set_accept_state(conn->ssl); |
| 3428 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3429 | if (data->tls_session_lifetime == 0) { |
| 3430 | /* |
| 3431 | * Set session id context to a unique value to make sure |
| 3432 | * session resumption cannot be used either through session |
| 3433 | * caching or TLS ticket extension. |
| 3434 | */ |
| 3435 | counter++; |
| 3436 | SSL_set_session_id_context(conn->ssl, |
| 3437 | (const unsigned char *) &counter, |
| 3438 | sizeof(counter)); |
| 3439 | } else if (session_ctx) { |
| 3440 | SSL_set_session_id_context(conn->ssl, session_ctx, |
| 3441 | session_ctx_len); |
| 3442 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3443 | |
| 3444 | return 0; |
| 3445 | } |
| 3446 | |
| 3447 | |
| 3448 | static int tls_connection_client_cert(struct tls_connection *conn, |
| 3449 | const char *client_cert, |
| 3450 | const u8 *client_cert_blob, |
| 3451 | size_t client_cert_blob_len) |
| 3452 | { |
| 3453 | if (client_cert == NULL && client_cert_blob == NULL) |
| 3454 | return 0; |
| 3455 | |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 3456 | #ifdef PKCS12_FUNCS |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3457 | #ifdef LIBRESSL_VERSION_NUMBER |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 3458 | /* |
| 3459 | * Clear previously set extra chain certificates, if any, from PKCS#12 |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3460 | * processing in tls_parse_pkcs12() to allow LibreSSL to build a new |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 3461 | * chain properly. |
| 3462 | */ |
| 3463 | SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3464 | #endif /* LIBRESSL_VERSION_NUMBER */ |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 3465 | #endif /* PKCS12_FUNCS */ |
| 3466 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3467 | if (client_cert_blob && |
| 3468 | SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob, |
| 3469 | client_cert_blob_len) == 1) { |
| 3470 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> " |
| 3471 | "OK"); |
| 3472 | return 0; |
| 3473 | } else if (client_cert_blob) { |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3474 | #if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20901000L |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3475 | tls_show_errors(MSG_DEBUG, __func__, |
| 3476 | "SSL_use_certificate_ASN1 failed"); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3477 | #else |
| 3478 | BIO *bio; |
| 3479 | X509 *x509; |
| 3480 | |
| 3481 | tls_show_errors(MSG_DEBUG, __func__, |
| 3482 | "SSL_use_certificate_ASN1 failed"); |
| 3483 | bio = BIO_new(BIO_s_mem()); |
| 3484 | if (!bio) |
| 3485 | return -1; |
| 3486 | BIO_write(bio, client_cert_blob, client_cert_blob_len); |
| 3487 | x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 3488 | if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) { |
| 3489 | X509_free(x509); |
| 3490 | BIO_free(bio); |
| 3491 | return -1; |
| 3492 | } |
| 3493 | X509_free(x509); |
| 3494 | wpa_printf(MSG_DEBUG, |
| 3495 | "OpenSSL: Found PEM encoded certificate from blob"); |
| 3496 | while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) { |
| 3497 | wpa_printf(MSG_DEBUG, |
| 3498 | "OpenSSL: Added an additional certificate into the chain"); |
| 3499 | SSL_add0_chain_cert(conn->ssl, x509); |
| 3500 | } |
| 3501 | BIO_free(bio); |
| 3502 | return 0; |
| 3503 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | if (client_cert == NULL) |
| 3507 | return -1; |
| 3508 | |
| 3509 | #ifdef ANDROID |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 3510 | if (os_strncmp(ANDROID_KEYSTORE_PREFIX, client_cert, |
| 3511 | ANDROID_KEYSTORE_PREFIX_LEN) == 0) { |
Gabriel Biren | ff4f838 | 2023-04-06 20:14:39 +0000 | [diff] [blame] | 3512 | BIO *bio = BIO_from_keystore(&client_cert[ANDROID_KEYSTORE_PREFIX_LEN], conn); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3513 | X509 *x509 = NULL; |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 3514 | if (!bio) { |
| 3515 | return -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3516 | } |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 3517 | // Keystore returns X.509 certificates in PEM encoding |
| 3518 | x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 3519 | if (!x509 || SSL_use_certificate(conn->ssl, x509) != 1) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3520 | X509_free(x509); |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 3521 | BIO_free(bio); |
| 3522 | wpa_printf(MSG_ERROR, "OpenSSL: Unknown certificate encoding"); |
| 3523 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3524 | } |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 3525 | X509_free(x509); |
| 3526 | wpa_printf(MSG_DEBUG, |
| 3527 | "OpenSSL: Found PEM encoded certificate from keystore: %s", |
| 3528 | client_cert); |
Paul Stewart | 50772e8 | 2017-01-25 13:59:16 -0800 | [diff] [blame] | 3529 | |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 3530 | // Read additional certificates into the chain |
| 3531 | while ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL))) { |
| 3532 | wpa_printf(MSG_DEBUG, |
| 3533 | "OpenSSL: Added an additional certificate into the chain"); |
| 3534 | // Takes ownership of x509, no need to free it here |
| 3535 | SSL_add0_chain_cert(conn->ssl, x509); |
Paul Stewart | 50772e8 | 2017-01-25 13:59:16 -0800 | [diff] [blame] | 3536 | } |
Hai Shalom | 7ad2a87 | 2021-08-02 18:56:55 -0700 | [diff] [blame] | 3537 | BIO_free(bio); |
| 3538 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3539 | } |
| 3540 | #endif /* ANDROID */ |
| 3541 | |
| 3542 | #ifndef OPENSSL_NO_STDIO |
| 3543 | if (SSL_use_certificate_file(conn->ssl, client_cert, |
| 3544 | SSL_FILETYPE_ASN1) == 1) { |
| 3545 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)" |
| 3546 | " --> OK"); |
| 3547 | return 0; |
| 3548 | } |
| 3549 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 3550 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \ |
| 3551 | !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL) |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3552 | if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) { |
| 3553 | ERR_clear_error(); |
| 3554 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file" |
| 3555 | " --> OK"); |
| 3556 | return 0; |
| 3557 | } |
| 3558 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3559 | if (SSL_use_certificate_file(conn->ssl, client_cert, |
| 3560 | SSL_FILETYPE_PEM) == 1) { |
| 3561 | ERR_clear_error(); |
| 3562 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)" |
| 3563 | " --> OK"); |
| 3564 | return 0; |
| 3565 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3566 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3567 | |
| 3568 | tls_show_errors(MSG_DEBUG, __func__, |
| 3569 | "SSL_use_certificate_file failed"); |
| 3570 | #else /* OPENSSL_NO_STDIO */ |
| 3571 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); |
| 3572 | #endif /* OPENSSL_NO_STDIO */ |
| 3573 | |
| 3574 | return -1; |
| 3575 | } |
| 3576 | |
| 3577 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3578 | static int tls_global_client_cert(struct tls_data *data, |
| 3579 | const char *client_cert) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3580 | { |
| 3581 | #ifndef OPENSSL_NO_STDIO |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3582 | SSL_CTX *ssl_ctx = data->ssl; |
| 3583 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3584 | if (client_cert == NULL) |
| 3585 | return 0; |
| 3586 | |
| 3587 | if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert, |
| 3588 | SSL_FILETYPE_ASN1) != 1 && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3589 | SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3590 | SSL_CTX_use_certificate_file(ssl_ctx, client_cert, |
| 3591 | SSL_FILETYPE_PEM) != 1) { |
| 3592 | tls_show_errors(MSG_INFO, __func__, |
| 3593 | "Failed to load client certificate"); |
| 3594 | return -1; |
| 3595 | } |
| 3596 | return 0; |
| 3597 | #else /* OPENSSL_NO_STDIO */ |
| 3598 | if (client_cert == NULL) |
| 3599 | return 0; |
| 3600 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); |
| 3601 | return -1; |
| 3602 | #endif /* OPENSSL_NO_STDIO */ |
| 3603 | } |
| 3604 | |
| 3605 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3606 | #ifdef PKCS12_FUNCS |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3607 | static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3608 | const char *passwd) |
| 3609 | { |
| 3610 | EVP_PKEY *pkey; |
| 3611 | X509 *cert; |
| 3612 | STACK_OF(X509) *certs; |
| 3613 | int res = 0; |
| 3614 | char buf[256]; |
| 3615 | |
| 3616 | pkey = NULL; |
| 3617 | cert = NULL; |
| 3618 | certs = NULL; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3619 | if (!passwd) |
| 3620 | passwd = ""; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3621 | if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) { |
| 3622 | tls_show_errors(MSG_DEBUG, __func__, |
| 3623 | "Failed to parse PKCS12 file"); |
| 3624 | PKCS12_free(p12); |
| 3625 | return -1; |
| 3626 | } |
| 3627 | wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data"); |
| 3628 | |
| 3629 | if (cert) { |
| 3630 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 3631 | sizeof(buf)); |
| 3632 | wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: " |
| 3633 | "subject='%s'", buf); |
| 3634 | if (ssl) { |
| 3635 | if (SSL_use_certificate(ssl, cert) != 1) |
| 3636 | res = -1; |
| 3637 | } else { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3638 | if (SSL_CTX_use_certificate(data->ssl, cert) != 1) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3639 | res = -1; |
| 3640 | } |
| 3641 | X509_free(cert); |
| 3642 | } |
| 3643 | |
| 3644 | if (pkey) { |
| 3645 | wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12"); |
| 3646 | if (ssl) { |
| 3647 | if (SSL_use_PrivateKey(ssl, pkey) != 1) |
| 3648 | res = -1; |
| 3649 | } else { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3650 | if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3651 | res = -1; |
| 3652 | } |
| 3653 | EVP_PKEY_free(pkey); |
| 3654 | } |
| 3655 | |
| 3656 | if (certs) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3657 | #ifndef LIBRESSL_VERSION_NUMBER |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 3658 | if (ssl) |
| 3659 | SSL_clear_chain_certs(ssl); |
| 3660 | else |
| 3661 | SSL_CTX_clear_chain_certs(data->ssl); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3662 | while ((cert = sk_X509_pop(certs)) != NULL) { |
| 3663 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 3664 | sizeof(buf)); |
| 3665 | wpa_printf(MSG_DEBUG, "TLS: additional certificate" |
| 3666 | " from PKCS12: subject='%s'", buf); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 3667 | if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) || |
| 3668 | (!ssl && SSL_CTX_add1_chain_cert(data->ssl, |
| 3669 | cert) != 1)) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3670 | tls_show_errors(MSG_DEBUG, __func__, |
| 3671 | "Failed to add additional certificate"); |
| 3672 | res = -1; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3673 | X509_free(cert); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3674 | break; |
| 3675 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3676 | X509_free(cert); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3677 | } |
| 3678 | if (!res) { |
| 3679 | /* Try to continue anyway */ |
| 3680 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3681 | sk_X509_pop_free(certs, X509_free); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3682 | #ifndef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 3683 | if (ssl) |
| 3684 | res = SSL_build_cert_chain( |
| 3685 | ssl, |
| 3686 | SSL_BUILD_CHAIN_FLAG_CHECK | |
| 3687 | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR); |
| 3688 | else |
| 3689 | res = SSL_CTX_build_cert_chain( |
| 3690 | data->ssl, |
| 3691 | SSL_BUILD_CHAIN_FLAG_CHECK | |
| 3692 | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3693 | if (!res) { |
| 3694 | tls_show_errors(MSG_DEBUG, __func__, |
| 3695 | "Failed to build certificate chain"); |
| 3696 | } else if (res == 2) { |
| 3697 | wpa_printf(MSG_DEBUG, |
| 3698 | "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates"); |
| 3699 | } |
| 3700 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 3701 | /* |
| 3702 | * Try to continue regardless of result since it is possible for |
| 3703 | * the extra certificates not to be required. |
| 3704 | */ |
| 3705 | res = 0; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3706 | #else /* LIBRESSL_VERSION_NUMBER */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3707 | SSL_CTX_clear_extra_chain_certs(data->ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3708 | while ((cert = sk_X509_pop(certs)) != NULL) { |
| 3709 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 3710 | sizeof(buf)); |
| 3711 | wpa_printf(MSG_DEBUG, "TLS: additional certificate" |
| 3712 | " from PKCS12: subject='%s'", buf); |
| 3713 | /* |
| 3714 | * There is no SSL equivalent for the chain cert - so |
| 3715 | * always add it to the context... |
| 3716 | */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3717 | if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1) |
| 3718 | { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3719 | X509_free(cert); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3720 | res = -1; |
| 3721 | break; |
| 3722 | } |
| 3723 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3724 | sk_X509_pop_free(certs, X509_free); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3725 | #endif /* LIBRSESSL_VERSION_NUMBER */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | PKCS12_free(p12); |
| 3729 | |
| 3730 | if (res < 0) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3731 | tls_get_errors(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3732 | |
| 3733 | return res; |
| 3734 | } |
| 3735 | #endif /* PKCS12_FUNCS */ |
| 3736 | |
| 3737 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3738 | static int tls_read_pkcs12(struct tls_data *data, SSL *ssl, |
| 3739 | const char *private_key, const char *passwd) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3740 | { |
| 3741 | #ifdef PKCS12_FUNCS |
| 3742 | FILE *f; |
| 3743 | PKCS12 *p12; |
| 3744 | |
| 3745 | f = fopen(private_key, "rb"); |
| 3746 | if (f == NULL) |
| 3747 | return -1; |
| 3748 | |
| 3749 | p12 = d2i_PKCS12_fp(f, NULL); |
| 3750 | fclose(f); |
| 3751 | |
| 3752 | if (p12 == NULL) { |
| 3753 | tls_show_errors(MSG_INFO, __func__, |
| 3754 | "Failed to use PKCS#12 file"); |
| 3755 | return -1; |
| 3756 | } |
| 3757 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3758 | return tls_parse_pkcs12(data, ssl, p12, passwd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3759 | |
| 3760 | #else /* PKCS12_FUNCS */ |
| 3761 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read " |
| 3762 | "p12/pfx files"); |
| 3763 | return -1; |
| 3764 | #endif /* PKCS12_FUNCS */ |
| 3765 | } |
| 3766 | |
| 3767 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3768 | static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3769 | const u8 *blob, size_t len, const char *passwd) |
| 3770 | { |
| 3771 | #ifdef PKCS12_FUNCS |
| 3772 | PKCS12 *p12; |
| 3773 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3774 | p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3775 | if (p12 == NULL) { |
| 3776 | tls_show_errors(MSG_INFO, __func__, |
| 3777 | "Failed to use PKCS#12 blob"); |
| 3778 | return -1; |
| 3779 | } |
| 3780 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3781 | return tls_parse_pkcs12(data, ssl, p12, passwd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3782 | |
| 3783 | #else /* PKCS12_FUNCS */ |
| 3784 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse " |
| 3785 | "p12/pfx blobs"); |
| 3786 | return -1; |
| 3787 | #endif /* PKCS12_FUNCS */ |
| 3788 | } |
| 3789 | |
| 3790 | |
| 3791 | #ifndef OPENSSL_NO_ENGINE |
| 3792 | static int tls_engine_get_cert(struct tls_connection *conn, |
| 3793 | const char *cert_id, |
| 3794 | X509 **cert) |
| 3795 | { |
| 3796 | /* this runs after the private key is loaded so no PIN is required */ |
| 3797 | struct { |
| 3798 | const char *cert_id; |
| 3799 | X509 *cert; |
| 3800 | } params; |
| 3801 | params.cert_id = cert_id; |
| 3802 | params.cert = NULL; |
| 3803 | |
| 3804 | if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL", |
| 3805 | 0, ¶ms, NULL, 1)) { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 3806 | unsigned long err = ERR_get_error(); |
| 3807 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3808 | wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id" |
| 3809 | " '%s' [%s]", cert_id, |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 3810 | ERR_error_string(err, NULL)); |
| 3811 | if (tls_is_pin_error(err)) |
| 3812 | return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3813 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 3814 | } |
| 3815 | if (!params.cert) { |
| 3816 | wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id" |
| 3817 | " '%s'", cert_id); |
| 3818 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 3819 | } |
| 3820 | *cert = params.cert; |
| 3821 | return 0; |
| 3822 | } |
| 3823 | #endif /* OPENSSL_NO_ENGINE */ |
| 3824 | |
| 3825 | |
| 3826 | static int tls_connection_engine_client_cert(struct tls_connection *conn, |
| 3827 | const char *cert_id) |
| 3828 | { |
| 3829 | #ifndef OPENSSL_NO_ENGINE |
| 3830 | X509 *cert; |
| 3831 | |
| 3832 | if (tls_engine_get_cert(conn, cert_id, &cert)) |
| 3833 | return -1; |
| 3834 | |
| 3835 | if (!SSL_use_certificate(conn->ssl, cert)) { |
| 3836 | tls_show_errors(MSG_ERROR, __func__, |
| 3837 | "SSL_use_certificate failed"); |
| 3838 | X509_free(cert); |
| 3839 | return -1; |
| 3840 | } |
| 3841 | X509_free(cert); |
| 3842 | wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> " |
| 3843 | "OK"); |
| 3844 | return 0; |
| 3845 | |
| 3846 | #else /* OPENSSL_NO_ENGINE */ |
| 3847 | return -1; |
| 3848 | #endif /* OPENSSL_NO_ENGINE */ |
| 3849 | } |
| 3850 | |
| 3851 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3852 | static int tls_connection_engine_ca_cert(struct tls_data *data, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3853 | struct tls_connection *conn, |
| 3854 | const char *ca_cert_id) |
| 3855 | { |
| 3856 | #ifndef OPENSSL_NO_ENGINE |
| 3857 | X509 *cert; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3858 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3859 | X509_STORE *store; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3860 | |
| 3861 | if (tls_engine_get_cert(conn, ca_cert_id, &cert)) |
| 3862 | return -1; |
| 3863 | |
| 3864 | /* start off the same as tls_connection_ca_cert */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3865 | store = X509_STORE_new(); |
| 3866 | if (store == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3867 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " |
| 3868 | "certificate store", __func__); |
| 3869 | X509_free(cert); |
| 3870 | return -1; |
| 3871 | } |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3872 | SSL_CTX_set_cert_store(ssl_ctx, store); |
| 3873 | if (!X509_STORE_add_cert(store, cert)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3874 | unsigned long err = ERR_peek_error(); |
| 3875 | tls_show_errors(MSG_WARNING, __func__, |
| 3876 | "Failed to add CA certificate from engine " |
| 3877 | "to certificate store"); |
| 3878 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && |
| 3879 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 3880 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert" |
| 3881 | " already in hash table error", |
| 3882 | __func__); |
| 3883 | } else { |
| 3884 | X509_free(cert); |
| 3885 | return -1; |
| 3886 | } |
| 3887 | } |
| 3888 | X509_free(cert); |
| 3889 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine " |
| 3890 | "to certificate store", __func__); |
| 3891 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3892 | conn->ca_cert_verify = 1; |
| 3893 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3894 | return 0; |
| 3895 | |
| 3896 | #else /* OPENSSL_NO_ENGINE */ |
| 3897 | return -1; |
| 3898 | #endif /* OPENSSL_NO_ENGINE */ |
| 3899 | } |
| 3900 | |
| 3901 | |
| 3902 | static int tls_connection_engine_private_key(struct tls_connection *conn) |
| 3903 | { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 3904 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3905 | if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) { |
| 3906 | tls_show_errors(MSG_ERROR, __func__, |
| 3907 | "ENGINE: cannot use private key for TLS"); |
| 3908 | return -1; |
| 3909 | } |
| 3910 | if (!SSL_check_private_key(conn->ssl)) { |
| 3911 | tls_show_errors(MSG_INFO, __func__, |
| 3912 | "Private key failed verification"); |
| 3913 | return -1; |
| 3914 | } |
| 3915 | return 0; |
| 3916 | #else /* OPENSSL_NO_ENGINE */ |
| 3917 | wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but " |
| 3918 | "engine support was not compiled in"); |
| 3919 | return -1; |
| 3920 | #endif /* OPENSSL_NO_ENGINE */ |
| 3921 | } |
| 3922 | |
| 3923 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3924 | #ifndef OPENSSL_NO_STDIO |
| 3925 | static int tls_passwd_cb(char *buf, int size, int rwflag, void *password) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3926 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3927 | if (!password) |
| 3928 | return 0; |
| 3929 | os_strlcpy(buf, (const char *) password, size); |
| 3930 | return os_strlen(buf); |
| 3931 | } |
| 3932 | #endif /* OPENSSL_NO_STDIO */ |
| 3933 | |
| 3934 | |
| 3935 | static int tls_use_private_key_file(struct tls_data *data, SSL *ssl, |
| 3936 | const char *private_key, |
| 3937 | const char *private_key_passwd) |
| 3938 | { |
| 3939 | #ifndef OPENSSL_NO_STDIO |
| 3940 | BIO *bio; |
| 3941 | EVP_PKEY *pkey; |
| 3942 | int ret; |
| 3943 | |
| 3944 | /* First try ASN.1 (DER). */ |
| 3945 | bio = BIO_new_file(private_key, "r"); |
| 3946 | if (!bio) |
| 3947 | return -1; |
| 3948 | pkey = d2i_PrivateKey_bio(bio, NULL); |
| 3949 | BIO_free(bio); |
| 3950 | |
| 3951 | if (pkey) { |
| 3952 | wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__); |
| 3953 | } else { |
| 3954 | /* Try PEM with the provided password. */ |
| 3955 | bio = BIO_new_file(private_key, "r"); |
| 3956 | if (!bio) |
| 3957 | return -1; |
| 3958 | pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb, |
| 3959 | (void *) private_key_passwd); |
| 3960 | BIO_free(bio); |
| 3961 | if (!pkey) |
| 3962 | return -1; |
| 3963 | wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__); |
| 3964 | /* Clear errors from the previous failed load. */ |
| 3965 | ERR_clear_error(); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3966 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3967 | |
| 3968 | if (ssl) |
| 3969 | ret = SSL_use_PrivateKey(ssl, pkey); |
| 3970 | else |
| 3971 | ret = SSL_CTX_use_PrivateKey(data->ssl, pkey); |
| 3972 | |
| 3973 | EVP_PKEY_free(pkey); |
| 3974 | return ret == 1 ? 0 : -1; |
| 3975 | #else /* OPENSSL_NO_STDIO */ |
| 3976 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); |
| 3977 | return -1; |
| 3978 | #endif /* OPENSSL_NO_STDIO */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3982 | static int tls_connection_private_key(struct tls_data *data, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3983 | struct tls_connection *conn, |
| 3984 | const char *private_key, |
| 3985 | const char *private_key_passwd, |
| 3986 | const u8 *private_key_blob, |
| 3987 | size_t private_key_blob_len) |
| 3988 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3989 | BIO *bio; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3990 | int ok; |
| 3991 | |
| 3992 | if (private_key == NULL && private_key_blob == NULL) |
| 3993 | return 0; |
| 3994 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3995 | ok = 0; |
| 3996 | while (private_key_blob) { |
| 3997 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl, |
| 3998 | (u8 *) private_key_blob, |
| 3999 | private_key_blob_len) == 1) { |
| 4000 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" |
| 4001 | "ASN1(EVP_PKEY_RSA) --> OK"); |
| 4002 | ok = 1; |
| 4003 | break; |
| 4004 | } |
| 4005 | |
| 4006 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl, |
| 4007 | (u8 *) private_key_blob, |
| 4008 | private_key_blob_len) == 1) { |
| 4009 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" |
| 4010 | "ASN1(EVP_PKEY_DSA) --> OK"); |
| 4011 | ok = 1; |
| 4012 | break; |
| 4013 | } |
| 4014 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 4015 | #ifndef OPENSSL_NO_EC |
| 4016 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_EC, conn->ssl, |
| 4017 | (u8 *) private_key_blob, |
| 4018 | private_key_blob_len) == 1) { |
| 4019 | wpa_printf(MSG_DEBUG, |
| 4020 | "OpenSSL: SSL_use_PrivateKey_ASN1(EVP_PKEY_EC) --> OK"); |
| 4021 | ok = 1; |
| 4022 | break; |
| 4023 | } |
| 4024 | #endif /* OPENSSL_NO_EC */ |
| 4025 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4026 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4027 | if (SSL_use_RSAPrivateKey_ASN1(conn->ssl, |
| 4028 | (u8 *) private_key_blob, |
| 4029 | private_key_blob_len) == 1) { |
| 4030 | wpa_printf(MSG_DEBUG, "OpenSSL: " |
| 4031 | "SSL_use_RSAPrivateKey_ASN1 --> OK"); |
| 4032 | ok = 1; |
| 4033 | break; |
| 4034 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4035 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4036 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 4037 | bio = BIO_new_mem_buf((u8 *) private_key_blob, |
| 4038 | private_key_blob_len); |
| 4039 | if (bio) { |
| 4040 | EVP_PKEY *pkey; |
| 4041 | |
| 4042 | pkey = PEM_read_bio_PrivateKey( |
| 4043 | bio, NULL, tls_passwd_cb, |
| 4044 | (void *) private_key_passwd); |
| 4045 | if (pkey) { |
| 4046 | if (SSL_use_PrivateKey(conn->ssl, pkey) == 1) { |
| 4047 | wpa_printf(MSG_DEBUG, |
| 4048 | "OpenSSL: SSL_use_PrivateKey --> OK"); |
| 4049 | ok = 1; |
| 4050 | EVP_PKEY_free(pkey); |
| 4051 | BIO_free(bio); |
| 4052 | break; |
| 4053 | } |
| 4054 | EVP_PKEY_free(pkey); |
| 4055 | } |
| 4056 | BIO_free(bio); |
| 4057 | } |
| 4058 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4059 | if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4060 | private_key_blob_len, |
| 4061 | private_key_passwd) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4062 | wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> " |
| 4063 | "OK"); |
| 4064 | ok = 1; |
| 4065 | break; |
| 4066 | } |
| 4067 | |
| 4068 | break; |
| 4069 | } |
| 4070 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4071 | while (!ok && private_key) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4072 | if (tls_use_private_key_file(data, conn->ssl, private_key, |
| 4073 | private_key_passwd) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4074 | ok = 1; |
| 4075 | break; |
| 4076 | } |
| 4077 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4078 | if (tls_read_pkcs12(data, conn->ssl, private_key, |
| 4079 | private_key_passwd) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4080 | wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file " |
| 4081 | "--> OK"); |
| 4082 | ok = 1; |
| 4083 | break; |
| 4084 | } |
| 4085 | |
| 4086 | if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) { |
| 4087 | wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to " |
| 4088 | "access certificate store --> OK"); |
| 4089 | ok = 1; |
| 4090 | break; |
| 4091 | } |
| 4092 | |
| 4093 | break; |
| 4094 | } |
| 4095 | |
| 4096 | if (!ok) { |
| 4097 | tls_show_errors(MSG_INFO, __func__, |
| 4098 | "Failed to load private key"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4099 | return -1; |
| 4100 | } |
| 4101 | ERR_clear_error(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4102 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4103 | if (!SSL_check_private_key(conn->ssl)) { |
| 4104 | tls_show_errors(MSG_INFO, __func__, "Private key failed " |
| 4105 | "verification"); |
| 4106 | return -1; |
| 4107 | } |
| 4108 | |
| 4109 | wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully"); |
| 4110 | return 0; |
| 4111 | } |
| 4112 | |
| 4113 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4114 | static int tls_global_private_key(struct tls_data *data, |
| 4115 | const char *private_key, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4116 | const char *private_key_passwd) |
| 4117 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4118 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4119 | |
| 4120 | if (private_key == NULL) |
| 4121 | return 0; |
| 4122 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4123 | if (tls_use_private_key_file(data, NULL, private_key, |
| 4124 | private_key_passwd) && |
| 4125 | tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4126 | tls_show_errors(MSG_INFO, __func__, |
| 4127 | "Failed to load private key"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4128 | ERR_clear_error(); |
| 4129 | return -1; |
| 4130 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4131 | ERR_clear_error(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4132 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4133 | if (!SSL_CTX_check_private_key(ssl_ctx)) { |
| 4134 | tls_show_errors(MSG_INFO, __func__, |
| 4135 | "Private key failed verification"); |
| 4136 | return -1; |
| 4137 | } |
| 4138 | |
| 4139 | return 0; |
| 4140 | } |
| 4141 | |
| 4142 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4143 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 4144 | #ifndef OPENSSL_NO_DH |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4145 | #ifndef OPENSSL_NO_DSA |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4146 | /* This is needed to replace the deprecated DSA_dup_DH() function */ |
| 4147 | static EVP_PKEY * openssl_dsa_to_dh(EVP_PKEY *dsa) |
| 4148 | { |
| 4149 | OSSL_PARAM_BLD *bld = NULL; |
| 4150 | OSSL_PARAM *params = NULL; |
| 4151 | BIGNUM *p = NULL, *q = NULL, *g = NULL; |
| 4152 | EVP_PKEY_CTX *ctx = NULL; |
| 4153 | EVP_PKEY *pkey = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4154 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4155 | if (!EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_P, &p) || |
| 4156 | !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_Q, &q) || |
| 4157 | !EVP_PKEY_get_bn_param(dsa, OSSL_PKEY_PARAM_FFC_G, &g) || |
| 4158 | !(bld = OSSL_PARAM_BLD_new()) || |
| 4159 | !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p) || |
| 4160 | !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q) || |
| 4161 | !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g) || |
| 4162 | !(params = OSSL_PARAM_BLD_to_param(bld)) || |
| 4163 | !(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL)) || |
| 4164 | EVP_PKEY_fromdata_init(ctx) != 1 || |
| 4165 | EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, |
| 4166 | params) != 1) |
| 4167 | wpa_printf(MSG_INFO, |
| 4168 | "TLS: Failed to convert DSA parameters to DH parameters"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4169 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4170 | EVP_PKEY_CTX_free(ctx); |
| 4171 | OSSL_PARAM_free(params); |
| 4172 | OSSL_PARAM_BLD_free(bld); |
| 4173 | BN_free(p); |
| 4174 | BN_free(q); |
| 4175 | BN_free(g); |
| 4176 | return pkey; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4177 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4178 | #endif /* !OPENSSL_NO_DSA */ |
| 4179 | #endif /* OPENSSL_NO_DH */ |
| 4180 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4181 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4182 | static int tls_global_dh(struct tls_data *data, const char *dh_file) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4183 | { |
| 4184 | #ifdef OPENSSL_NO_DH |
| 4185 | if (dh_file == NULL) |
| 4186 | return 0; |
| 4187 | wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but " |
| 4188 | "dh_file specified"); |
| 4189 | return -1; |
| 4190 | #else /* OPENSSL_NO_DH */ |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4191 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 4192 | SSL_CTX *ssl_ctx = data->ssl; |
| 4193 | BIO *bio; |
| 4194 | OSSL_DECODER_CTX *ctx = NULL; |
| 4195 | EVP_PKEY *pkey = NULL, *tmpkey = NULL; |
| 4196 | bool dsa = false; |
| 4197 | |
| 4198 | if (!ssl_ctx) |
| 4199 | return -1; |
| 4200 | if (!dh_file) { |
| 4201 | SSL_CTX_set_dh_auto(ssl_ctx, 1); |
| 4202 | return 0; |
| 4203 | } |
| 4204 | |
| 4205 | bio = BIO_new_file(dh_file, "r"); |
| 4206 | if (!bio) { |
| 4207 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", |
| 4208 | dh_file, ERR_error_string(ERR_get_error(), NULL)); |
| 4209 | return -1; |
| 4210 | } |
| 4211 | ctx = OSSL_DECODER_CTX_new_for_pkey( |
| 4212 | &tmpkey, "PEM", NULL, NULL, |
| 4213 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL); |
| 4214 | if (!ctx || |
| 4215 | OSSL_DECODER_from_bio(ctx, bio) != 1) { |
| 4216 | wpa_printf(MSG_INFO, |
| 4217 | "TLS: Failed to decode domain parameters from '%s': %s", |
| 4218 | dh_file, ERR_error_string(ERR_get_error(), NULL)); |
| 4219 | BIO_free(bio); |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 4220 | OSSL_DECODER_CTX_free(ctx); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4221 | return -1; |
| 4222 | } |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 4223 | OSSL_DECODER_CTX_free(ctx); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4224 | BIO_free(bio); |
| 4225 | |
| 4226 | if (!tmpkey) { |
| 4227 | wpa_printf(MSG_INFO, "TLS: Failed to load domain parameters"); |
| 4228 | return -1; |
| 4229 | } |
| 4230 | |
| 4231 | #ifndef OPENSSL_NO_DSA |
| 4232 | if (EVP_PKEY_is_a(tmpkey, "DSA")) { |
| 4233 | pkey = openssl_dsa_to_dh(tmpkey); |
| 4234 | EVP_PKEY_free(tmpkey); |
| 4235 | if (!pkey) |
| 4236 | return -1; |
| 4237 | dsa = true; |
| 4238 | } |
| 4239 | #endif /* !OPENSSL_NO_DSA */ |
| 4240 | if (!dsa) { |
| 4241 | if (EVP_PKEY_is_a(tmpkey, "DH") || |
| 4242 | EVP_PKEY_is_a(tmpkey, "DHX")) { |
| 4243 | } else { |
| 4244 | wpa_printf(MSG_INFO, |
| 4245 | "TLS: No DH parameters found in %s", |
| 4246 | dh_file); |
| 4247 | EVP_PKEY_free(tmpkey); |
| 4248 | return -1; |
| 4249 | } |
| 4250 | pkey = tmpkey; |
| 4251 | tmpkey = NULL; |
| 4252 | } |
| 4253 | |
| 4254 | if (SSL_CTX_set0_tmp_dh_pkey(ssl_ctx, pkey) != 1) { |
| 4255 | wpa_printf(MSG_INFO, |
| 4256 | "TLS: Failed to set DH params from '%s': %s", |
| 4257 | dh_file, ERR_error_string(ERR_get_error(), NULL)); |
| 4258 | EVP_PKEY_free(pkey); |
| 4259 | return -1; |
| 4260 | } |
| 4261 | return 0; |
| 4262 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4263 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4264 | DH *dh; |
| 4265 | BIO *bio; |
| 4266 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4267 | if (!ssl_ctx) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4268 | return -1; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4269 | if (!dh_file) { |
| 4270 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_BORINGSSL) |
| 4271 | SSL_CTX_set_dh_auto(ssl_ctx, 1); |
| 4272 | #endif |
| 4273 | return 0; |
| 4274 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4275 | |
| 4276 | bio = BIO_new_file(dh_file, "r"); |
| 4277 | if (bio == NULL) { |
| 4278 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", |
| 4279 | dh_file, ERR_error_string(ERR_get_error(), NULL)); |
| 4280 | return -1; |
| 4281 | } |
| 4282 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 4283 | BIO_free(bio); |
| 4284 | #ifndef OPENSSL_NO_DSA |
| 4285 | while (dh == NULL) { |
| 4286 | DSA *dsa; |
| 4287 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -" |
| 4288 | " trying to parse as DSA params", dh_file, |
| 4289 | ERR_error_string(ERR_get_error(), NULL)); |
| 4290 | bio = BIO_new_file(dh_file, "r"); |
| 4291 | if (bio == NULL) |
| 4292 | break; |
| 4293 | dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL); |
| 4294 | BIO_free(bio); |
| 4295 | if (!dsa) { |
| 4296 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file " |
| 4297 | "'%s': %s", dh_file, |
| 4298 | ERR_error_string(ERR_get_error(), NULL)); |
| 4299 | break; |
| 4300 | } |
| 4301 | |
| 4302 | wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format"); |
| 4303 | dh = DSA_dup_DH(dsa); |
| 4304 | DSA_free(dsa); |
| 4305 | if (dh == NULL) { |
| 4306 | wpa_printf(MSG_INFO, "TLS: Failed to convert DSA " |
| 4307 | "params into DH params"); |
| 4308 | break; |
| 4309 | } |
| 4310 | break; |
| 4311 | } |
| 4312 | #endif /* !OPENSSL_NO_DSA */ |
| 4313 | if (dh == NULL) { |
| 4314 | wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file " |
| 4315 | "'%s'", dh_file); |
| 4316 | return -1; |
| 4317 | } |
| 4318 | |
| 4319 | if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) { |
| 4320 | wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': " |
| 4321 | "%s", dh_file, |
| 4322 | ERR_error_string(ERR_get_error(), NULL)); |
| 4323 | DH_free(dh); |
| 4324 | return -1; |
| 4325 | } |
| 4326 | DH_free(dh); |
| 4327 | return 0; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4328 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4329 | #endif /* OPENSSL_NO_DH */ |
| 4330 | } |
| 4331 | |
| 4332 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4333 | int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn, |
| 4334 | struct tls_random *keys) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4335 | { |
| 4336 | SSL *ssl; |
| 4337 | |
| 4338 | if (conn == NULL || keys == NULL) |
| 4339 | return -1; |
| 4340 | ssl = conn->ssl; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4341 | if (ssl == NULL) |
| 4342 | return -1; |
| 4343 | |
| 4344 | os_memset(keys, 0, sizeof(*keys)); |
| 4345 | keys->client_random = conn->client_random; |
| 4346 | keys->client_random_len = SSL_get_client_random( |
| 4347 | ssl, conn->client_random, sizeof(conn->client_random)); |
| 4348 | keys->server_random = conn->server_random; |
| 4349 | keys->server_random_len = SSL_get_server_random( |
| 4350 | ssl, conn->server_random, sizeof(conn->server_random)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4351 | |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
| 4355 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4356 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4357 | static int openssl_get_keyblock_size(SSL *ssl) |
| 4358 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4359 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4360 | const EVP_CIPHER *c; |
| 4361 | const EVP_MD *h; |
| 4362 | int md_size; |
| 4363 | |
| 4364 | if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL || |
| 4365 | ssl->read_hash == NULL) |
| 4366 | return -1; |
| 4367 | |
| 4368 | c = ssl->enc_read_ctx->cipher; |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4369 | h = EVP_MD_CTX_md(ssl->read_hash); |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4370 | if (h) |
| 4371 | md_size = EVP_MD_size(h); |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4372 | else if (ssl->s3) |
| 4373 | md_size = ssl->s3->tmp.new_mac_secret_size; |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4374 | else |
| 4375 | return -1; |
| 4376 | |
| 4377 | wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d " |
| 4378 | "IV_len=%d", EVP_CIPHER_key_length(c), md_size, |
| 4379 | EVP_CIPHER_iv_length(c)); |
| 4380 | return 2 * (EVP_CIPHER_key_length(c) + |
| 4381 | md_size + |
| 4382 | EVP_CIPHER_iv_length(c)); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4383 | #else |
| 4384 | const SSL_CIPHER *ssl_cipher; |
| 4385 | int cipher, digest; |
| 4386 | const EVP_CIPHER *c; |
| 4387 | const EVP_MD *h; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4388 | int mac_key_len, enc_key_len, fixed_iv_len; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4389 | |
| 4390 | ssl_cipher = SSL_get_current_cipher(ssl); |
| 4391 | if (!ssl_cipher) |
| 4392 | return -1; |
| 4393 | cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher); |
| 4394 | digest = SSL_CIPHER_get_digest_nid(ssl_cipher); |
| 4395 | wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d", |
| 4396 | cipher, digest); |
| 4397 | if (cipher < 0 || digest < 0) |
| 4398 | return -1; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4399 | if (cipher == NID_undef) { |
| 4400 | wpa_printf(MSG_DEBUG, "OpenSSL: no cipher in use?!"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4401 | return -1; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4402 | } |
| 4403 | c = EVP_get_cipherbynid(cipher); |
| 4404 | if (!c) |
| 4405 | return -1; |
| 4406 | enc_key_len = EVP_CIPHER_key_length(c); |
| 4407 | if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE || |
| 4408 | EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) |
| 4409 | fixed_iv_len = 4; /* only part of IV from PRF */ |
| 4410 | else |
| 4411 | fixed_iv_len = EVP_CIPHER_iv_length(c); |
| 4412 | if (digest == NID_undef) { |
| 4413 | wpa_printf(MSG_DEBUG, "OpenSSL: no digest in use (e.g., AEAD)"); |
| 4414 | mac_key_len = 0; |
| 4415 | } else { |
| 4416 | h = EVP_get_digestbynid(digest); |
| 4417 | if (!h) |
| 4418 | return -1; |
| 4419 | mac_key_len = EVP_MD_size(h); |
| 4420 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4421 | |
| 4422 | wpa_printf(MSG_DEBUG, |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 4423 | "OpenSSL: keyblock size: mac_key_len=%d enc_key_len=%d fixed_iv_len=%d", |
| 4424 | mac_key_len, enc_key_len, fixed_iv_len); |
| 4425 | return 2 * (mac_key_len + enc_key_len + fixed_iv_len); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4426 | #endif |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4427 | } |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4428 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4429 | |
| 4430 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4431 | int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 4432 | const char *label, const u8 *context, |
| 4433 | size_t context_len, u8 *out, size_t out_len) |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 4434 | { |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4435 | if (!conn || |
| 4436 | SSL_export_keying_material(conn->ssl, out, out_len, label, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 4437 | os_strlen(label), context, context_len, |
| 4438 | context != NULL) != 1) |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4439 | return -1; |
| 4440 | return 0; |
| 4441 | } |
| 4442 | |
| 4443 | |
| 4444 | int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn, |
| 4445 | u8 *out, size_t out_len) |
| 4446 | { |
| 4447 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4448 | SSL *ssl; |
| 4449 | SSL_SESSION *sess; |
| 4450 | u8 *rnd; |
| 4451 | int ret = -1; |
| 4452 | int skip = 0; |
| 4453 | u8 *tmp_out = NULL; |
| 4454 | u8 *_out = out; |
| 4455 | unsigned char client_random[SSL3_RANDOM_SIZE]; |
| 4456 | unsigned char server_random[SSL3_RANDOM_SIZE]; |
| 4457 | unsigned char master_key[64]; |
| 4458 | size_t master_key_len; |
| 4459 | const char *ver; |
| 4460 | |
| 4461 | /* |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4462 | * TLS library did not support EAP-FAST key generation, so get the |
| 4463 | * needed TLS session parameters and use an internal implementation of |
| 4464 | * TLS PRF to derive the key. |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4465 | */ |
| 4466 | |
| 4467 | if (conn == NULL) |
| 4468 | return -1; |
| 4469 | ssl = conn->ssl; |
| 4470 | if (ssl == NULL) |
| 4471 | return -1; |
| 4472 | ver = SSL_get_version(ssl); |
| 4473 | sess = SSL_get_session(ssl); |
| 4474 | if (!ver || !sess) |
| 4475 | return -1; |
| 4476 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4477 | skip = openssl_get_keyblock_size(ssl); |
| 4478 | if (skip < 0) |
| 4479 | return -1; |
| 4480 | tmp_out = os_malloc(skip + out_len); |
| 4481 | if (!tmp_out) |
| 4482 | return -1; |
| 4483 | _out = tmp_out; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4484 | |
| 4485 | rnd = os_malloc(2 * SSL3_RANDOM_SIZE); |
| 4486 | if (!rnd) { |
| 4487 | os_free(tmp_out); |
| 4488 | return -1; |
| 4489 | } |
| 4490 | |
| 4491 | SSL_get_client_random(ssl, client_random, sizeof(client_random)); |
| 4492 | SSL_get_server_random(ssl, server_random, sizeof(server_random)); |
| 4493 | master_key_len = SSL_SESSION_get_master_key(sess, master_key, |
| 4494 | sizeof(master_key)); |
| 4495 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4496 | os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE); |
| 4497 | os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4498 | |
| 4499 | if (os_strcmp(ver, "TLSv1.2") == 0) { |
| 4500 | tls_prf_sha256(master_key, master_key_len, |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4501 | "key expansion", rnd, 2 * SSL3_RANDOM_SIZE, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4502 | _out, skip + out_len); |
| 4503 | ret = 0; |
| 4504 | } else if (tls_prf_sha1_md5(master_key, master_key_len, |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4505 | "key expansion", rnd, 2 * SSL3_RANDOM_SIZE, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4506 | _out, skip + out_len) == 0) { |
| 4507 | ret = 0; |
| 4508 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4509 | forced_memzero(master_key, sizeof(master_key)); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4510 | os_free(rnd); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4511 | if (ret == 0) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4512 | os_memcpy(out, _out + skip, out_len); |
| 4513 | bin_clear_free(tmp_out, skip); |
| 4514 | |
| 4515 | return ret; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 4516 | #else /* OPENSSL_NEED_EAP_FAST_PRF */ |
| 4517 | wpa_printf(MSG_ERROR, |
| 4518 | "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode"); |
| 4519 | return -1; |
| 4520 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | |
| 4524 | static struct wpabuf * |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4525 | openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4526 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4527 | struct tls_context *context = conn->context; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4528 | int res; |
| 4529 | struct wpabuf *out_data; |
| 4530 | |
| 4531 | /* |
| 4532 | * Give TLS handshake data from the server (if available) to OpenSSL |
| 4533 | * for processing. |
| 4534 | */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4535 | if (in_data && wpabuf_len(in_data) > 0 && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4536 | BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data)) |
| 4537 | < 0) { |
| 4538 | tls_show_errors(MSG_INFO, __func__, |
| 4539 | "Handshake failed - BIO_write"); |
| 4540 | return NULL; |
| 4541 | } |
| 4542 | |
| 4543 | /* Initiate TLS handshake or continue the existing handshake */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4544 | if (conn->server) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4545 | res = SSL_accept(conn->ssl); |
| 4546 | else |
| 4547 | res = SSL_connect(conn->ssl); |
| 4548 | if (res != 1) { |
| 4549 | int err = SSL_get_error(conn->ssl, res); |
| 4550 | if (err == SSL_ERROR_WANT_READ) |
| 4551 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want " |
| 4552 | "more data"); |
| 4553 | else if (err == SSL_ERROR_WANT_WRITE) |
| 4554 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to " |
| 4555 | "write"); |
| 4556 | else { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4557 | unsigned long error = ERR_peek_last_error(); |
| 4558 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4559 | tls_show_errors(MSG_INFO, __func__, "SSL_connect"); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4560 | |
| 4561 | if (context->event_cb && |
| 4562 | ERR_GET_LIB(error) == ERR_LIB_SSL && |
| 4563 | ERR_GET_REASON(error) == |
| 4564 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED) { |
| 4565 | context->event_cb( |
| 4566 | context->cb_ctx, |
| 4567 | TLS_UNSAFE_RENEGOTIATION_DISABLED, |
| 4568 | NULL); |
| 4569 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4570 | conn->failed++; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4571 | if (!conn->server && !conn->client_hello_generated) { |
| 4572 | /* The server would not understand TLS Alert |
| 4573 | * before ClientHello, so simply terminate |
| 4574 | * handshake on this type of error case caused |
| 4575 | * by a likely internal error like no ciphers |
| 4576 | * available. */ |
| 4577 | wpa_printf(MSG_DEBUG, |
| 4578 | "OpenSSL: Could not generate ClientHello"); |
| 4579 | conn->write_alerts++; |
| 4580 | return NULL; |
| 4581 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4585 | if (!conn->server && !conn->failed) |
| 4586 | conn->client_hello_generated = 1; |
| 4587 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4588 | #ifdef CONFIG_SUITEB |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4589 | if ((conn->flags & TLS_CONN_SUITEB) && !conn->server && |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4590 | os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 && |
| 4591 | conn->server_dh_prime_len < 3072) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4592 | /* |
| 4593 | * This should not be reached since earlier cert_cb should have |
| 4594 | * terminated the handshake. Keep this check here for extra |
| 4595 | * protection if anything goes wrong with the more low-level |
| 4596 | * checks based on having to parse the TLS handshake messages. |
| 4597 | */ |
| 4598 | wpa_printf(MSG_DEBUG, |
| 4599 | "OpenSSL: Server DH prime length: %d bits", |
| 4600 | conn->server_dh_prime_len); |
| 4601 | |
| 4602 | if (context->event_cb) { |
| 4603 | union tls_event_data ev; |
| 4604 | |
| 4605 | os_memset(&ev, 0, sizeof(ev)); |
| 4606 | ev.alert.is_local = 1; |
| 4607 | ev.alert.type = "fatal"; |
| 4608 | ev.alert.description = "insufficient security"; |
| 4609 | context->event_cb(context->cb_ctx, TLS_ALERT, &ev); |
| 4610 | } |
| 4611 | /* |
| 4612 | * Could send a TLS Alert to the server, but for now, simply |
| 4613 | * terminate handshake. |
| 4614 | */ |
| 4615 | conn->failed++; |
| 4616 | conn->write_alerts++; |
| 4617 | return NULL; |
| 4618 | } |
| 4619 | #endif /* CONFIG_SUITEB */ |
| 4620 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4621 | /* Get the TLS handshake data to be sent to the server */ |
| 4622 | res = BIO_ctrl_pending(conn->ssl_out); |
| 4623 | wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res); |
| 4624 | out_data = wpabuf_alloc(res); |
| 4625 | if (out_data == NULL) { |
| 4626 | wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for " |
| 4627 | "handshake output (%d bytes)", res); |
| 4628 | if (BIO_reset(conn->ssl_out) < 0) { |
| 4629 | tls_show_errors(MSG_INFO, __func__, |
| 4630 | "BIO_reset failed"); |
| 4631 | } |
| 4632 | return NULL; |
| 4633 | } |
| 4634 | res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data), |
| 4635 | res); |
| 4636 | if (res < 0) { |
| 4637 | tls_show_errors(MSG_INFO, __func__, |
| 4638 | "Handshake failed - BIO_read"); |
| 4639 | if (BIO_reset(conn->ssl_out) < 0) { |
| 4640 | tls_show_errors(MSG_INFO, __func__, |
| 4641 | "BIO_reset failed"); |
| 4642 | } |
| 4643 | wpabuf_free(out_data); |
| 4644 | return NULL; |
| 4645 | } |
| 4646 | wpabuf_put(out_data, res); |
| 4647 | |
| 4648 | return out_data; |
| 4649 | } |
| 4650 | |
| 4651 | |
| 4652 | static struct wpabuf * |
| 4653 | openssl_get_appl_data(struct tls_connection *conn, size_t max_len) |
| 4654 | { |
| 4655 | struct wpabuf *appl_data; |
| 4656 | int res; |
| 4657 | |
| 4658 | appl_data = wpabuf_alloc(max_len + 100); |
| 4659 | if (appl_data == NULL) |
| 4660 | return NULL; |
| 4661 | |
| 4662 | res = SSL_read(conn->ssl, wpabuf_mhead(appl_data), |
| 4663 | wpabuf_size(appl_data)); |
| 4664 | if (res < 0) { |
| 4665 | int err = SSL_get_error(conn->ssl, res); |
| 4666 | if (err == SSL_ERROR_WANT_READ || |
| 4667 | err == SSL_ERROR_WANT_WRITE) { |
| 4668 | wpa_printf(MSG_DEBUG, "SSL: No Application Data " |
| 4669 | "included"); |
| 4670 | } else { |
| 4671 | tls_show_errors(MSG_INFO, __func__, |
| 4672 | "Failed to read possible " |
| 4673 | "Application Data"); |
| 4674 | } |
| 4675 | wpabuf_free(appl_data); |
| 4676 | return NULL; |
| 4677 | } |
| 4678 | |
| 4679 | wpabuf_put(appl_data, res); |
| 4680 | wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished " |
| 4681 | "message", appl_data); |
| 4682 | |
| 4683 | return appl_data; |
| 4684 | } |
| 4685 | |
| 4686 | |
| 4687 | static struct wpabuf * |
| 4688 | openssl_connection_handshake(struct tls_connection *conn, |
| 4689 | const struct wpabuf *in_data, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4690 | struct wpabuf **appl_data) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4691 | { |
| 4692 | struct wpabuf *out_data; |
| 4693 | |
| 4694 | if (appl_data) |
| 4695 | *appl_data = NULL; |
| 4696 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4697 | out_data = openssl_handshake(conn, in_data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4698 | if (out_data == NULL) |
| 4699 | return NULL; |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 4700 | if (conn->invalid_hb_used) { |
| 4701 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); |
| 4702 | wpabuf_free(out_data); |
| 4703 | return NULL; |
| 4704 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4705 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4706 | if (SSL_is_init_finished(conn->ssl)) { |
| 4707 | wpa_printf(MSG_DEBUG, |
| 4708 | "OpenSSL: Handshake finished - resumed=%d", |
| 4709 | tls_connection_resumed(conn->ssl_ctx, conn)); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4710 | if (conn->server) { |
| 4711 | char *buf; |
| 4712 | size_t buflen = 2000; |
| 4713 | |
| 4714 | buf = os_malloc(buflen); |
| 4715 | if (buf) { |
| 4716 | if (SSL_get_shared_ciphers(conn->ssl, buf, |
| 4717 | buflen)) { |
| 4718 | buf[buflen - 1] = '\0'; |
| 4719 | wpa_printf(MSG_DEBUG, |
| 4720 | "OpenSSL: Shared ciphers: %s", |
| 4721 | buf); |
| 4722 | } |
| 4723 | os_free(buf); |
| 4724 | } |
| 4725 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4726 | if (appl_data && in_data) |
| 4727 | *appl_data = openssl_get_appl_data(conn, |
| 4728 | wpabuf_len(in_data)); |
| 4729 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4730 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 4731 | if (conn->invalid_hb_used) { |
| 4732 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); |
| 4733 | if (appl_data) { |
| 4734 | wpabuf_free(*appl_data); |
| 4735 | *appl_data = NULL; |
| 4736 | } |
| 4737 | wpabuf_free(out_data); |
| 4738 | return NULL; |
| 4739 | } |
| 4740 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4741 | return out_data; |
| 4742 | } |
| 4743 | |
| 4744 | |
| 4745 | struct wpabuf * |
| 4746 | tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn, |
| 4747 | const struct wpabuf *in_data, |
| 4748 | struct wpabuf **appl_data) |
| 4749 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4750 | return openssl_connection_handshake(conn, in_data, appl_data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | |
| 4754 | struct wpabuf * tls_connection_server_handshake(void *tls_ctx, |
| 4755 | struct tls_connection *conn, |
| 4756 | const struct wpabuf *in_data, |
| 4757 | struct wpabuf **appl_data) |
| 4758 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4759 | conn->server = 1; |
| 4760 | return openssl_connection_handshake(conn, in_data, appl_data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | |
| 4764 | struct wpabuf * tls_connection_encrypt(void *tls_ctx, |
| 4765 | struct tls_connection *conn, |
| 4766 | const struct wpabuf *in_data) |
| 4767 | { |
| 4768 | int res; |
| 4769 | struct wpabuf *buf; |
| 4770 | |
| 4771 | if (conn == NULL) |
| 4772 | return NULL; |
| 4773 | |
| 4774 | /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */ |
| 4775 | if ((res = BIO_reset(conn->ssl_in)) < 0 || |
| 4776 | (res = BIO_reset(conn->ssl_out)) < 0) { |
| 4777 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); |
| 4778 | return NULL; |
| 4779 | } |
| 4780 | res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data)); |
| 4781 | if (res < 0) { |
| 4782 | tls_show_errors(MSG_INFO, __func__, |
| 4783 | "Encryption failed - SSL_write"); |
| 4784 | return NULL; |
| 4785 | } |
| 4786 | |
| 4787 | /* Read encrypted data to be sent to the server */ |
| 4788 | buf = wpabuf_alloc(wpabuf_len(in_data) + 300); |
| 4789 | if (buf == NULL) |
| 4790 | return NULL; |
| 4791 | res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf)); |
| 4792 | if (res < 0) { |
| 4793 | tls_show_errors(MSG_INFO, __func__, |
| 4794 | "Encryption failed - BIO_read"); |
| 4795 | wpabuf_free(buf); |
| 4796 | return NULL; |
| 4797 | } |
| 4798 | wpabuf_put(buf, res); |
| 4799 | |
| 4800 | return buf; |
| 4801 | } |
| 4802 | |
| 4803 | |
| 4804 | struct wpabuf * tls_connection_decrypt(void *tls_ctx, |
| 4805 | struct tls_connection *conn, |
| 4806 | const struct wpabuf *in_data) |
| 4807 | { |
| 4808 | int res; |
| 4809 | struct wpabuf *buf; |
| 4810 | |
| 4811 | /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */ |
| 4812 | res = BIO_write(conn->ssl_in, wpabuf_head(in_data), |
| 4813 | wpabuf_len(in_data)); |
| 4814 | if (res < 0) { |
| 4815 | tls_show_errors(MSG_INFO, __func__, |
| 4816 | "Decryption failed - BIO_write"); |
| 4817 | return NULL; |
| 4818 | } |
| 4819 | if (BIO_reset(conn->ssl_out) < 0) { |
| 4820 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); |
| 4821 | return NULL; |
| 4822 | } |
| 4823 | |
| 4824 | /* Read decrypted data for further processing */ |
| 4825 | /* |
| 4826 | * Even though we try to disable TLS compression, it is possible that |
| 4827 | * this cannot be done with all TLS libraries. Add extra buffer space |
| 4828 | * to handle the possibility of the decrypted data being longer than |
| 4829 | * input data. |
| 4830 | */ |
| 4831 | buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3); |
| 4832 | if (buf == NULL) |
| 4833 | return NULL; |
| 4834 | res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf)); |
| 4835 | if (res < 0) { |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 4836 | int err = SSL_get_error(conn->ssl, res); |
| 4837 | |
| 4838 | if (err == SSL_ERROR_WANT_READ) { |
| 4839 | wpa_printf(MSG_DEBUG, |
| 4840 | "SSL: SSL_connect - want more data"); |
| 4841 | res = 0; |
| 4842 | } else { |
| 4843 | tls_show_errors(MSG_INFO, __func__, |
| 4844 | "Decryption failed - SSL_read"); |
| 4845 | wpabuf_free(buf); |
| 4846 | return NULL; |
| 4847 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4848 | } |
| 4849 | wpabuf_put(buf, res); |
| 4850 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 4851 | if (conn->invalid_hb_used) { |
| 4852 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); |
| 4853 | wpabuf_free(buf); |
| 4854 | return NULL; |
| 4855 | } |
| 4856 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4857 | return buf; |
| 4858 | } |
| 4859 | |
| 4860 | |
| 4861 | int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn) |
| 4862 | { |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 4863 | return conn ? SSL_session_reused(conn->ssl) : 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | |
| 4867 | int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn, |
| 4868 | u8 *ciphers) |
| 4869 | { |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4870 | char buf[500], *pos, *end; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4871 | u8 *c; |
| 4872 | int ret; |
| 4873 | |
| 4874 | if (conn == NULL || conn->ssl == NULL || ciphers == NULL) |
| 4875 | return -1; |
| 4876 | |
| 4877 | buf[0] = '\0'; |
| 4878 | pos = buf; |
| 4879 | end = pos + sizeof(buf); |
| 4880 | |
| 4881 | c = ciphers; |
| 4882 | while (*c != TLS_CIPHER_NONE) { |
| 4883 | const char *suite; |
| 4884 | |
| 4885 | switch (*c) { |
| 4886 | case TLS_CIPHER_RC4_SHA: |
| 4887 | suite = "RC4-SHA"; |
| 4888 | break; |
| 4889 | case TLS_CIPHER_AES128_SHA: |
| 4890 | suite = "AES128-SHA"; |
| 4891 | break; |
| 4892 | case TLS_CIPHER_RSA_DHE_AES128_SHA: |
| 4893 | suite = "DHE-RSA-AES128-SHA"; |
| 4894 | break; |
| 4895 | case TLS_CIPHER_ANON_DH_AES128_SHA: |
| 4896 | suite = "ADH-AES128-SHA"; |
| 4897 | break; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 4898 | case TLS_CIPHER_RSA_DHE_AES256_SHA: |
| 4899 | suite = "DHE-RSA-AES256-SHA"; |
| 4900 | break; |
| 4901 | case TLS_CIPHER_AES256_SHA: |
| 4902 | suite = "AES256-SHA"; |
| 4903 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4904 | default: |
| 4905 | wpa_printf(MSG_DEBUG, "TLS: Unsupported " |
| 4906 | "cipher selection: %d", *c); |
| 4907 | return -1; |
| 4908 | } |
| 4909 | ret = os_snprintf(pos, end - pos, ":%s", suite); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4910 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4911 | break; |
| 4912 | pos += ret; |
| 4913 | |
| 4914 | c++; |
| 4915 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4916 | if (!buf[0]) { |
| 4917 | wpa_printf(MSG_DEBUG, "OpenSSL: No ciphers listed"); |
| 4918 | return -1; |
| 4919 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4920 | |
| 4921 | wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1); |
| 4922 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4923 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4924 | #ifdef EAP_FAST_OR_TEAP |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4925 | if (os_strstr(buf, ":ADH-")) { |
| 4926 | /* |
| 4927 | * Need to drop to security level 0 to allow anonymous |
| 4928 | * cipher suites for EAP-FAST. |
| 4929 | */ |
| 4930 | SSL_set_security_level(conn->ssl, 0); |
| 4931 | } else if (SSL_get_security_level(conn->ssl) == 0) { |
| 4932 | /* Force at least security level 1 */ |
| 4933 | SSL_set_security_level(conn->ssl, 1); |
| 4934 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4935 | #endif /* EAP_FAST_OR_TEAP */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4936 | #endif |
| 4937 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4938 | if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) { |
| 4939 | tls_show_errors(MSG_INFO, __func__, |
| 4940 | "Cipher suite configuration failed"); |
| 4941 | return -1; |
| 4942 | } |
| 4943 | |
| 4944 | return 0; |
| 4945 | } |
| 4946 | |
| 4947 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4948 | int tls_get_version(void *ssl_ctx, struct tls_connection *conn, |
| 4949 | char *buf, size_t buflen) |
| 4950 | { |
| 4951 | const char *name; |
| 4952 | if (conn == NULL || conn->ssl == NULL) |
| 4953 | return -1; |
| 4954 | |
| 4955 | name = SSL_get_version(conn->ssl); |
| 4956 | if (name == NULL) |
| 4957 | return -1; |
| 4958 | |
| 4959 | os_strlcpy(buf, name, buflen); |
| 4960 | return 0; |
| 4961 | } |
| 4962 | |
| 4963 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4964 | int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn, |
| 4965 | char *buf, size_t buflen) |
| 4966 | { |
| 4967 | const char *name; |
| 4968 | if (conn == NULL || conn->ssl == NULL) |
| 4969 | return -1; |
| 4970 | |
| 4971 | name = SSL_get_cipher(conn->ssl); |
| 4972 | if (name == NULL) |
| 4973 | return -1; |
| 4974 | |
| 4975 | os_strlcpy(buf, name, buflen); |
| 4976 | return 0; |
| 4977 | } |
| 4978 | |
| 4979 | |
| 4980 | int tls_connection_enable_workaround(void *ssl_ctx, |
| 4981 | struct tls_connection *conn) |
| 4982 | { |
| 4983 | SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
| 4984 | |
| 4985 | return 0; |
| 4986 | } |
| 4987 | |
| 4988 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4989 | #ifdef EAP_FAST_OR_TEAP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4990 | /* ClientHello TLS extensions require a patch to openssl, so this function is |
| 4991 | * commented out unless explicitly needed for EAP-FAST in order to be able to |
| 4992 | * build this file with unmodified openssl. */ |
| 4993 | int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn, |
| 4994 | int ext_type, const u8 *data, |
| 4995 | size_t data_len) |
| 4996 | { |
| 4997 | if (conn == NULL || conn->ssl == NULL || ext_type != 35) |
| 4998 | return -1; |
| 4999 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5000 | if (SSL_set_session_ticket_ext(conn->ssl, (void *) data, |
| 5001 | data_len) != 1) |
| 5002 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5003 | |
| 5004 | return 0; |
| 5005 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5006 | #endif /* EAP_FAST_OR_TEAP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5007 | |
| 5008 | |
| 5009 | int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn) |
| 5010 | { |
| 5011 | if (conn == NULL) |
| 5012 | return -1; |
| 5013 | return conn->failed; |
| 5014 | } |
| 5015 | |
| 5016 | |
| 5017 | int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn) |
| 5018 | { |
| 5019 | if (conn == NULL) |
| 5020 | return -1; |
| 5021 | return conn->read_alerts; |
| 5022 | } |
| 5023 | |
| 5024 | |
| 5025 | int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn) |
| 5026 | { |
| 5027 | if (conn == NULL) |
| 5028 | return -1; |
| 5029 | return conn->write_alerts; |
| 5030 | } |
| 5031 | |
| 5032 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5033 | #ifdef HAVE_OCSP |
| 5034 | |
| 5035 | static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp) |
| 5036 | { |
| 5037 | #ifndef CONFIG_NO_STDOUT_DEBUG |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5038 | BIO *out; |
| 5039 | size_t rlen; |
| 5040 | char *txt; |
| 5041 | int res; |
| 5042 | |
| 5043 | if (wpa_debug_level > MSG_DEBUG) |
| 5044 | return; |
| 5045 | |
| 5046 | out = BIO_new(BIO_s_mem()); |
| 5047 | if (!out) |
| 5048 | return; |
| 5049 | |
| 5050 | OCSP_RESPONSE_print(out, rsp, 0); |
| 5051 | rlen = BIO_ctrl_pending(out); |
| 5052 | txt = os_malloc(rlen + 1); |
| 5053 | if (!txt) { |
| 5054 | BIO_free(out); |
| 5055 | return; |
| 5056 | } |
| 5057 | |
| 5058 | res = BIO_read(out, txt, rlen); |
| 5059 | if (res > 0) { |
| 5060 | txt[res] = '\0'; |
| 5061 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt); |
| 5062 | } |
| 5063 | os_free(txt); |
| 5064 | BIO_free(out); |
| 5065 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 5066 | } |
| 5067 | |
| 5068 | |
| 5069 | static int ocsp_resp_cb(SSL *s, void *arg) |
| 5070 | { |
| 5071 | struct tls_connection *conn = arg; |
| 5072 | const unsigned char *p; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5073 | int len, status, reason, res; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5074 | OCSP_RESPONSE *rsp; |
| 5075 | OCSP_BASICRESP *basic; |
| 5076 | OCSP_CERTID *id; |
| 5077 | ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5078 | X509_STORE *store; |
| 5079 | STACK_OF(X509) *certs = NULL; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5080 | |
| 5081 | len = SSL_get_tlsext_status_ocsp_resp(s, &p); |
| 5082 | if (!p) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5083 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
| 5084 | #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L |
| 5085 | if (SSL_version(s) == TLS1_3_VERSION && SSL_session_reused(s)) { |
| 5086 | /* TLS 1.3 sends the OCSP response with the server |
| 5087 | * Certificate message. Since that Certificate message |
| 5088 | * is not sent when resuming a session, there can be no |
| 5089 | * new OCSP response. Allow this since the OCSP response |
| 5090 | * was validated when checking the initial certificate |
| 5091 | * exchange. */ |
| 5092 | wpa_printf(MSG_DEBUG, |
| 5093 | "OpenSSL: Allow no OCSP response when using TLS 1.3 and a resumed session"); |
| 5094 | return 1; |
| 5095 | } |
| 5096 | #endif |
| 5097 | #endif |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5098 | wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received"); |
| 5099 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; |
| 5100 | } |
| 5101 | |
| 5102 | wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len); |
| 5103 | |
| 5104 | rsp = d2i_OCSP_RESPONSE(NULL, &p, len); |
| 5105 | if (!rsp) { |
| 5106 | wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response"); |
| 5107 | return 0; |
| 5108 | } |
| 5109 | |
| 5110 | ocsp_debug_print_resp(rsp); |
| 5111 | |
| 5112 | status = OCSP_response_status(rsp); |
| 5113 | if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 5114 | wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)", |
| 5115 | status, OCSP_response_status_str(status)); |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
| 5119 | basic = OCSP_response_get1_basic(rsp); |
| 5120 | if (!basic) { |
| 5121 | wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse"); |
| 5122 | return 0; |
| 5123 | } |
| 5124 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5125 | store = SSL_CTX_get_cert_store(conn->ssl_ctx); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5126 | if (conn->peer_issuer) { |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 5127 | debug_print_cert(conn->peer_issuer, "Add OCSP issuer"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5128 | |
| 5129 | if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) { |
| 5130 | tls_show_errors(MSG_INFO, __func__, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 5131 | "OpenSSL: Could not add issuer to certificate store"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5132 | } |
| 5133 | certs = sk_X509_new_null(); |
| 5134 | if (certs) { |
| 5135 | X509 *cert; |
| 5136 | cert = X509_dup(conn->peer_issuer); |
| 5137 | if (cert && !sk_X509_push(certs, cert)) { |
| 5138 | tls_show_errors( |
| 5139 | MSG_INFO, __func__, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 5140 | "OpenSSL: Could not add issuer to OCSP responder trust store"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5141 | X509_free(cert); |
| 5142 | sk_X509_free(certs); |
| 5143 | certs = NULL; |
| 5144 | } |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 5145 | if (certs && conn->peer_issuer_issuer) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5146 | cert = X509_dup(conn->peer_issuer_issuer); |
| 5147 | if (cert && !sk_X509_push(certs, cert)) { |
| 5148 | tls_show_errors( |
| 5149 | MSG_INFO, __func__, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 5150 | "OpenSSL: Could not add issuer's issuer to OCSP responder trust store"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5151 | X509_free(cert); |
| 5152 | } |
| 5153 | } |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER); |
| 5158 | sk_X509_pop_free(certs, X509_free); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5159 | if (status <= 0) { |
| 5160 | tls_show_errors(MSG_INFO, __func__, |
| 5161 | "OpenSSL: OCSP response failed verification"); |
| 5162 | OCSP_BASICRESP_free(basic); |
| 5163 | OCSP_RESPONSE_free(rsp); |
| 5164 | return 0; |
| 5165 | } |
| 5166 | |
| 5167 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded"); |
| 5168 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5169 | if (!conn->peer_cert) { |
| 5170 | wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check"); |
| 5171 | OCSP_BASICRESP_free(basic); |
| 5172 | OCSP_RESPONSE_free(rsp); |
| 5173 | return 0; |
| 5174 | } |
| 5175 | |
| 5176 | if (!conn->peer_issuer) { |
| 5177 | wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5178 | OCSP_BASICRESP_free(basic); |
| 5179 | OCSP_RESPONSE_free(rsp); |
| 5180 | return 0; |
| 5181 | } |
| 5182 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5183 | id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5184 | if (!id) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5185 | wpa_printf(MSG_DEBUG, |
| 5186 | "OpenSSL: Could not create OCSP certificate identifier (SHA256)"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5187 | OCSP_BASICRESP_free(basic); |
| 5188 | OCSP_RESPONSE_free(rsp); |
| 5189 | return 0; |
| 5190 | } |
| 5191 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5192 | res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at, |
| 5193 | &this_update, &next_update); |
| 5194 | if (!res) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5195 | OCSP_CERTID_free(id); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5196 | id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer); |
| 5197 | if (!id) { |
| 5198 | wpa_printf(MSG_DEBUG, |
| 5199 | "OpenSSL: Could not create OCSP certificate identifier (SHA1)"); |
| 5200 | OCSP_BASICRESP_free(basic); |
| 5201 | OCSP_RESPONSE_free(rsp); |
| 5202 | return 0; |
| 5203 | } |
| 5204 | |
| 5205 | res = OCSP_resp_find_status(basic, id, &status, &reason, |
| 5206 | &produced_at, &this_update, |
| 5207 | &next_update); |
| 5208 | } |
| 5209 | |
| 5210 | if (!res) { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5211 | wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s", |
| 5212 | (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" : |
| 5213 | " (OCSP not required)"); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 5214 | OCSP_CERTID_free(id); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5215 | OCSP_BASICRESP_free(basic); |
| 5216 | OCSP_RESPONSE_free(rsp); |
| 5217 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; |
| 5218 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 5219 | OCSP_CERTID_free(id); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5220 | |
| 5221 | if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) { |
| 5222 | tls_show_errors(MSG_INFO, __func__, |
| 5223 | "OpenSSL: OCSP status times invalid"); |
| 5224 | OCSP_BASICRESP_free(basic); |
| 5225 | OCSP_RESPONSE_free(rsp); |
| 5226 | return 0; |
| 5227 | } |
| 5228 | |
| 5229 | OCSP_BASICRESP_free(basic); |
| 5230 | OCSP_RESPONSE_free(rsp); |
| 5231 | |
| 5232 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s", |
| 5233 | OCSP_cert_status_str(status)); |
| 5234 | |
| 5235 | if (status == V_OCSP_CERTSTATUS_GOOD) |
| 5236 | return 1; |
| 5237 | if (status == V_OCSP_CERTSTATUS_REVOKED) |
| 5238 | return 0; |
| 5239 | if (conn->flags & TLS_CONN_REQUIRE_OCSP) { |
| 5240 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required"); |
| 5241 | return 0; |
| 5242 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5243 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5244 | return 1; |
| 5245 | } |
| 5246 | |
| 5247 | |
| 5248 | static int ocsp_status_cb(SSL *s, void *arg) |
| 5249 | { |
| 5250 | char *tmp; |
| 5251 | char *resp; |
| 5252 | size_t len; |
| 5253 | |
| 5254 | if (tls_global->ocsp_stapling_response == NULL) { |
| 5255 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured"); |
| 5256 | return SSL_TLSEXT_ERR_OK; |
| 5257 | } |
| 5258 | |
| 5259 | resp = os_readfile(tls_global->ocsp_stapling_response, &len); |
| 5260 | if (resp == NULL) { |
| 5261 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file"); |
| 5262 | /* TODO: Build OCSPResponse with responseStatus = internalError |
| 5263 | */ |
| 5264 | return SSL_TLSEXT_ERR_OK; |
| 5265 | } |
| 5266 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response"); |
| 5267 | tmp = OPENSSL_malloc(len); |
| 5268 | if (tmp == NULL) { |
| 5269 | os_free(resp); |
| 5270 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 5271 | } |
| 5272 | |
| 5273 | os_memcpy(tmp, resp, len); |
| 5274 | os_free(resp); |
| 5275 | SSL_set_tlsext_status_ocsp_resp(s, tmp, len); |
| 5276 | |
| 5277 | return SSL_TLSEXT_ERR_OK; |
| 5278 | } |
| 5279 | |
| 5280 | #endif /* HAVE_OCSP */ |
| 5281 | |
| 5282 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 5283 | static size_t max_str_len(const char **lines) |
| 5284 | { |
| 5285 | const char **p; |
| 5286 | size_t max_len = 0; |
| 5287 | |
| 5288 | for (p = lines; *p; p++) { |
| 5289 | size_t len = os_strlen(*p); |
| 5290 | |
| 5291 | if (len > max_len) |
| 5292 | max_len = len; |
| 5293 | } |
| 5294 | |
| 5295 | return max_len; |
| 5296 | } |
| 5297 | |
| 5298 | |
| 5299 | static int match_lines_in_file(const char *path, const char **lines) |
| 5300 | { |
| 5301 | FILE *f; |
| 5302 | char *buf; |
| 5303 | size_t bufsize; |
| 5304 | int found = 0, is_linestart = 1; |
| 5305 | |
| 5306 | bufsize = max_str_len(lines) + sizeof("\r\n"); |
| 5307 | buf = os_malloc(bufsize); |
| 5308 | if (!buf) |
| 5309 | return 0; |
| 5310 | |
| 5311 | f = fopen(path, "r"); |
| 5312 | if (!f) { |
| 5313 | os_free(buf); |
| 5314 | return 0; |
| 5315 | } |
| 5316 | |
| 5317 | while (!found && fgets(buf, bufsize, f)) { |
| 5318 | int is_lineend; |
| 5319 | size_t len; |
| 5320 | const char **p; |
| 5321 | |
| 5322 | len = strcspn(buf, "\r\n"); |
| 5323 | is_lineend = buf[len] != '\0'; |
| 5324 | buf[len] = '\0'; |
| 5325 | |
| 5326 | if (is_linestart && is_lineend) { |
| 5327 | for (p = lines; !found && *p; p++) |
| 5328 | found = os_strcmp(buf, *p) == 0; |
| 5329 | } |
| 5330 | is_linestart = is_lineend; |
| 5331 | } |
| 5332 | |
| 5333 | fclose(f); |
| 5334 | bin_clear_free(buf, bufsize); |
| 5335 | |
| 5336 | return found; |
| 5337 | } |
| 5338 | |
| 5339 | |
| 5340 | static int is_tpm2_key(const char *path) |
| 5341 | { |
| 5342 | /* Check both new and old format of TPM2 PEM guard tag */ |
| 5343 | static const char *tpm2_tags[] = { |
| 5344 | "-----BEGIN TSS2 PRIVATE KEY-----", |
| 5345 | "-----BEGIN TSS2 KEY BLOB-----", |
| 5346 | NULL |
| 5347 | }; |
| 5348 | |
| 5349 | return match_lines_in_file(path, tpm2_tags); |
| 5350 | } |
| 5351 | |
| 5352 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5353 | int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, |
| 5354 | const struct tls_connection_params *params) |
| 5355 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5356 | struct tls_data *data = tls_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5357 | int ret; |
| 5358 | unsigned long err; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5359 | int can_pkcs11 = 0; |
| 5360 | const char *key_id = params->key_id; |
| 5361 | const char *cert_id = params->cert_id; |
| 5362 | const char *ca_cert_id = params->ca_cert_id; |
| 5363 | const char *engine_id = params->engine ? params->engine_id : NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5364 | const char *ciphers; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5365 | |
| 5366 | if (conn == NULL) |
| 5367 | return -1; |
| 5368 | |
Dmitry Shmidt | 014a3ff | 2015-12-28 13:27:49 -0800 | [diff] [blame] | 5369 | if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) { |
| 5370 | wpa_printf(MSG_INFO, |
| 5371 | "OpenSSL: ocsp=3 not supported"); |
| 5372 | return -1; |
| 5373 | } |
| 5374 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5375 | /* |
| 5376 | * If the engine isn't explicitly configured, and any of the |
| 5377 | * cert/key fields are actually PKCS#11 URIs, then automatically |
| 5378 | * use the PKCS#11 ENGINE. |
| 5379 | */ |
| 5380 | if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0) |
| 5381 | can_pkcs11 = 1; |
| 5382 | |
| 5383 | if (!key_id && params->private_key && can_pkcs11 && |
| 5384 | os_strncmp(params->private_key, "pkcs11:", 7) == 0) { |
| 5385 | can_pkcs11 = 2; |
| 5386 | key_id = params->private_key; |
| 5387 | } |
| 5388 | |
| 5389 | if (!cert_id && params->client_cert && can_pkcs11 && |
| 5390 | os_strncmp(params->client_cert, "pkcs11:", 7) == 0) { |
| 5391 | can_pkcs11 = 2; |
| 5392 | cert_id = params->client_cert; |
| 5393 | } |
| 5394 | |
| 5395 | if (!ca_cert_id && params->ca_cert && can_pkcs11 && |
| 5396 | os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) { |
| 5397 | can_pkcs11 = 2; |
| 5398 | ca_cert_id = params->ca_cert; |
| 5399 | } |
| 5400 | |
| 5401 | /* If we need to automatically enable the PKCS#11 ENGINE, do so. */ |
| 5402 | if (can_pkcs11 == 2 && !engine_id) |
| 5403 | engine_id = "pkcs11"; |
| 5404 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 5405 | /* If private_key points to a TPM2-wrapped key, automatically enable |
| 5406 | * tpm2 engine and use it to unwrap the key. */ |
| 5407 | if (params->private_key && |
| 5408 | (!engine_id || os_strcmp(engine_id, "tpm2") == 0) && |
| 5409 | is_tpm2_key(params->private_key)) { |
| 5410 | wpa_printf(MSG_DEBUG, "OpenSSL: Found TPM2 wrapped key %s", |
| 5411 | params->private_key); |
| 5412 | key_id = key_id ? key_id : params->private_key; |
| 5413 | engine_id = engine_id ? engine_id : "tpm2"; |
| 5414 | } |
| 5415 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5416 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5417 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5418 | if (params->flags & TLS_CONN_EAP_FAST) { |
| 5419 | wpa_printf(MSG_DEBUG, |
| 5420 | "OpenSSL: Use TLSv1_method() for EAP-FAST"); |
| 5421 | if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) { |
| 5422 | tls_show_errors(MSG_INFO, __func__, |
| 5423 | "Failed to set TLSv1_method() for EAP-FAST"); |
| 5424 | return -1; |
| 5425 | } |
| 5426 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5427 | #endif |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5428 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L |
| 5429 | #ifdef SSL_OP_NO_TLSv1_3 |
| 5430 | if (params->flags & TLS_CONN_EAP_FAST) { |
| 5431 | /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1 |
| 5432 | * refuses to start the handshake with the modified ciphersuite |
| 5433 | * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */ |
| 5434 | wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST"); |
| 5435 | SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3); |
| 5436 | } |
| 5437 | #endif /* SSL_OP_NO_TLSv1_3 */ |
| 5438 | #endif |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5439 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5440 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5441 | while ((err = ERR_get_error())) { |
| 5442 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", |
| 5443 | __func__, ERR_error_string(err, NULL)); |
| 5444 | } |
| 5445 | |
Sunil Ravi | 77d572f | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 5446 | if (tls_set_conn_flags(conn, params->flags, |
| 5447 | params->openssl_ciphers) < 0) { |
| 5448 | wpa_printf(MSG_ERROR, "TLS: Failed to set connection flags"); |
| 5449 | return -1; |
| 5450 | } |
| 5451 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5452 | if (engine_id) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 5453 | wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine %s", |
| 5454 | engine_id); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5455 | ret = tls_engine_init(conn, engine_id, params->pin, |
| 5456 | key_id, cert_id, ca_cert_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5457 | if (ret) |
| 5458 | return ret; |
| 5459 | } |
| 5460 | if (tls_connection_set_subject_match(conn, |
| 5461 | params->subject_match, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5462 | params->altsubject_match, |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 5463 | params->suffix_match, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 5464 | params->domain_match, |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 5465 | params->check_cert_subject)) { |
| 5466 | wpa_printf(MSG_ERROR, "TLS: Failed to set subject match"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5467 | return -1; |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 5468 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5469 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5470 | if (engine_id && ca_cert_id) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5471 | if (tls_connection_engine_ca_cert(data, conn, ca_cert_id)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5472 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5473 | } else if (tls_connection_ca_cert(data, conn, params->ca_cert, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5474 | params->ca_cert_blob, |
| 5475 | params->ca_cert_blob_len, |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 5476 | params->ca_path)) { |
| 5477 | wpa_printf(MSG_ERROR, "TLS: Failed to parse Root CA certificate"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5478 | return -1; |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 5479 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5480 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5481 | if (engine_id && cert_id) { |
| 5482 | if (tls_connection_engine_client_cert(conn, cert_id)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5483 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
| 5484 | } else if (tls_connection_client_cert(conn, params->client_cert, |
| 5485 | params->client_cert_blob, |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 5486 | params->client_cert_blob_len)) { |
| 5487 | wpa_printf(MSG_ERROR, "TLS: Failed to parse client certificate"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5488 | return -1; |
Hai Shalom | 2217159 | 2021-04-02 16:05:23 -0700 | [diff] [blame] | 5489 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5490 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5491 | if (engine_id && key_id) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5492 | wpa_printf(MSG_DEBUG, "TLS: Using private key from engine"); |
| 5493 | if (tls_connection_engine_private_key(conn)) |
| 5494 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5495 | } else if (tls_connection_private_key(data, conn, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5496 | params->private_key, |
| 5497 | params->private_key_passwd, |
| 5498 | params->private_key_blob, |
| 5499 | params->private_key_blob_len)) { |
| 5500 | wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'", |
| 5501 | params->private_key); |
| 5502 | return -1; |
| 5503 | } |
| 5504 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5505 | ciphers = params->openssl_ciphers; |
| 5506 | #ifdef CONFIG_SUITEB |
| 5507 | #ifdef OPENSSL_IS_BORINGSSL |
| 5508 | if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) { |
| 5509 | /* BoringSSL removed support for SUITEB192, so need to handle |
| 5510 | * this with hardcoded ciphersuite and additional checks for |
| 5511 | * other parameters. */ |
| 5512 | ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384"; |
| 5513 | } |
| 5514 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 5515 | #endif /* CONFIG_SUITEB */ |
| 5516 | if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5517 | wpa_printf(MSG_INFO, |
| 5518 | "OpenSSL: Failed to set cipher string '%s'", |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5519 | ciphers); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5520 | return -1; |
| 5521 | } |
| 5522 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5523 | if (!params->openssl_ecdh_curves) { |
| 5524 | #ifndef OPENSSL_IS_BORINGSSL |
| 5525 | #ifndef OPENSSL_NO_EC |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5526 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5527 | if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) { |
| 5528 | wpa_printf(MSG_INFO, |
| 5529 | "OpenSSL: Failed to set ECDH curves to auto"); |
| 5530 | return -1; |
| 5531 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5532 | #endif /* < 1.1.0 */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5533 | #endif /* OPENSSL_NO_EC */ |
| 5534 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 5535 | } else if (params->openssl_ecdh_curves[0]) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5536 | #ifdef OPENSSL_IS_BORINGSSL |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5537 | wpa_printf(MSG_INFO, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5538 | "OpenSSL: ECDH configuration not supported"); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5539 | return -1; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5540 | #else /* !OPENSSL_IS_BORINGSSL */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5541 | #ifndef OPENSSL_NO_EC |
| 5542 | if (SSL_set1_curves_list(conn->ssl, |
| 5543 | params->openssl_ecdh_curves) != 1) { |
| 5544 | wpa_printf(MSG_INFO, |
| 5545 | "OpenSSL: Failed to set ECDH curves '%s'", |
| 5546 | params->openssl_ecdh_curves); |
| 5547 | return -1; |
| 5548 | } |
| 5549 | #else /* OPENSSL_NO_EC */ |
| 5550 | wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported"); |
| 5551 | return -1; |
| 5552 | #endif /* OPENSSL_NO_EC */ |
| 5553 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 5554 | } |
| 5555 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5556 | #ifdef OPENSSL_IS_BORINGSSL |
| 5557 | if (params->flags & TLS_CONN_REQUEST_OCSP) { |
| 5558 | SSL_enable_ocsp_stapling(conn->ssl); |
| 5559 | } |
| 5560 | #else /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5561 | #ifdef HAVE_OCSP |
| 5562 | if (params->flags & TLS_CONN_REQUEST_OCSP) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5563 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5564 | SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp); |
| 5565 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb); |
| 5566 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn); |
| 5567 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5568 | #else /* HAVE_OCSP */ |
| 5569 | if (params->flags & TLS_CONN_REQUIRE_OCSP) { |
| 5570 | wpa_printf(MSG_INFO, |
| 5571 | "OpenSSL: No OCSP support included - reject configuration"); |
| 5572 | return -1; |
| 5573 | } |
| 5574 | if (params->flags & TLS_CONN_REQUEST_OCSP) { |
| 5575 | wpa_printf(MSG_DEBUG, |
| 5576 | "OpenSSL: No OCSP support included - allow optional OCSP case to continue"); |
| 5577 | } |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5578 | #endif /* HAVE_OCSP */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5579 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5580 | |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 5581 | conn->flags = params->flags; |
| 5582 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5583 | tls_get_errors(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5584 | |
| 5585 | return 0; |
| 5586 | } |
| 5587 | |
| 5588 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5589 | static void openssl_debug_dump_cipher_list(SSL_CTX *ssl_ctx) |
| 5590 | { |
| 5591 | SSL *ssl; |
| 5592 | int i; |
| 5593 | |
| 5594 | ssl = SSL_new(ssl_ctx); |
| 5595 | if (!ssl) |
| 5596 | return; |
| 5597 | |
| 5598 | wpa_printf(MSG_DEBUG, |
| 5599 | "OpenSSL: Enabled cipher suites in priority order"); |
| 5600 | for (i = 0; ; i++) { |
| 5601 | const char *cipher; |
| 5602 | |
| 5603 | cipher = SSL_get_cipher_list(ssl, i); |
| 5604 | if (!cipher) |
| 5605 | break; |
| 5606 | wpa_printf(MSG_DEBUG, "Cipher %d: %s", i, cipher); |
| 5607 | } |
| 5608 | |
| 5609 | SSL_free(ssl); |
| 5610 | } |
| 5611 | |
| 5612 | |
| 5613 | #if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION) |
| 5614 | |
| 5615 | static const char * openssl_pkey_type_str(const EVP_PKEY *pkey) |
| 5616 | { |
| 5617 | if (!pkey) |
| 5618 | return "NULL"; |
| 5619 | switch (EVP_PKEY_type(EVP_PKEY_id(pkey))) { |
| 5620 | case EVP_PKEY_RSA: |
| 5621 | return "RSA"; |
| 5622 | case EVP_PKEY_DSA: |
| 5623 | return "DSA"; |
| 5624 | case EVP_PKEY_DH: |
| 5625 | return "DH"; |
| 5626 | case EVP_PKEY_EC: |
| 5627 | return "EC"; |
Sunil Ravi | 77d572f | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 5628 | default: |
| 5629 | return "?"; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5630 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | |
| 5634 | static void openssl_debug_dump_certificate(int i, X509 *cert) |
| 5635 | { |
| 5636 | char buf[256]; |
| 5637 | EVP_PKEY *pkey; |
| 5638 | ASN1_INTEGER *ser; |
| 5639 | char serial_num[128]; |
| 5640 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 5641 | if (!cert) |
| 5642 | return; |
| 5643 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5644 | X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf)); |
| 5645 | |
| 5646 | ser = X509_get_serialNumber(cert); |
| 5647 | if (ser) |
| 5648 | wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num), |
| 5649 | ASN1_STRING_get0_data(ser), |
| 5650 | ASN1_STRING_length(ser)); |
| 5651 | else |
| 5652 | serial_num[0] = '\0'; |
| 5653 | |
| 5654 | pkey = X509_get_pubkey(cert); |
| 5655 | wpa_printf(MSG_DEBUG, "%d: %s (%s) %s", i, buf, |
| 5656 | openssl_pkey_type_str(pkey), serial_num); |
| 5657 | EVP_PKEY_free(pkey); |
| 5658 | } |
| 5659 | |
| 5660 | |
| 5661 | static void openssl_debug_dump_certificates(SSL_CTX *ssl_ctx) |
| 5662 | { |
| 5663 | STACK_OF(X509) *certs; |
| 5664 | |
| 5665 | wpa_printf(MSG_DEBUG, "OpenSSL: Configured certificate chain"); |
| 5666 | if (SSL_CTX_get0_chain_certs(ssl_ctx, &certs) == 1) { |
| 5667 | int i; |
| 5668 | |
| 5669 | for (i = sk_X509_num(certs); i > 0; i--) |
| 5670 | openssl_debug_dump_certificate(i, sk_X509_value(certs, |
| 5671 | i - 1)); |
| 5672 | } |
| 5673 | openssl_debug_dump_certificate(0, SSL_CTX_get0_certificate(ssl_ctx)); |
| 5674 | } |
| 5675 | |
| 5676 | #endif |
| 5677 | |
| 5678 | |
| 5679 | static void openssl_debug_dump_certificate_chains(SSL_CTX *ssl_ctx) |
| 5680 | { |
| 5681 | #if !defined(LIBRESSL_VERSION_NUMBER) && !defined(BORINGSSL_API_VERSION) |
| 5682 | int res; |
| 5683 | |
| 5684 | for (res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST); |
| 5685 | res == 1; |
| 5686 | res = SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_NEXT)) |
| 5687 | openssl_debug_dump_certificates(ssl_ctx); |
| 5688 | |
| 5689 | SSL_CTX_set_current_cert(ssl_ctx, SSL_CERT_SET_FIRST); |
| 5690 | #endif |
| 5691 | } |
| 5692 | |
| 5693 | |
| 5694 | static void openssl_debug_dump_ctx(SSL_CTX *ssl_ctx) |
| 5695 | { |
| 5696 | openssl_debug_dump_cipher_list(ssl_ctx); |
| 5697 | openssl_debug_dump_certificate_chains(ssl_ctx); |
| 5698 | } |
| 5699 | |
| 5700 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5701 | int tls_global_set_params(void *tls_ctx, |
| 5702 | const struct tls_connection_params *params) |
| 5703 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5704 | struct tls_data *data = tls_ctx; |
| 5705 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5706 | unsigned long err; |
| 5707 | |
| 5708 | while ((err = ERR_get_error())) { |
| 5709 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", |
| 5710 | __func__, ERR_error_string(err, NULL)); |
| 5711 | } |
| 5712 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 5713 | os_free(data->check_cert_subject); |
| 5714 | data->check_cert_subject = NULL; |
| 5715 | if (params->check_cert_subject) { |
| 5716 | data->check_cert_subject = |
| 5717 | os_strdup(params->check_cert_subject); |
| 5718 | if (!data->check_cert_subject) |
| 5719 | return -1; |
| 5720 | } |
| 5721 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5722 | if (tls_global_ca_cert(data, params->ca_cert) || |
| 5723 | tls_global_client_cert(data, params->client_cert) || |
| 5724 | tls_global_private_key(data, params->private_key, |
| 5725 | params->private_key_passwd) || |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5726 | tls_global_client_cert(data, params->client_cert2) || |
| 5727 | tls_global_private_key(data, params->private_key2, |
| 5728 | params->private_key_passwd2) || |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5729 | tls_global_dh(data, params->dh_file)) { |
| 5730 | wpa_printf(MSG_INFO, "TLS: Failed to set global parameters"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5731 | return -1; |
| 5732 | } |
| 5733 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5734 | if (params->openssl_ciphers && |
| 5735 | SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) { |
| 5736 | wpa_printf(MSG_INFO, |
| 5737 | "OpenSSL: Failed to set cipher string '%s'", |
| 5738 | params->openssl_ciphers); |
| 5739 | return -1; |
| 5740 | } |
| 5741 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5742 | if (!params->openssl_ecdh_curves) { |
| 5743 | #ifndef OPENSSL_IS_BORINGSSL |
| 5744 | #ifndef OPENSSL_NO_EC |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5745 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5746 | if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) { |
| 5747 | wpa_printf(MSG_INFO, |
| 5748 | "OpenSSL: Failed to set ECDH curves to auto"); |
| 5749 | return -1; |
| 5750 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5751 | #endif /* < 1.1.0 */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5752 | #endif /* OPENSSL_NO_EC */ |
| 5753 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 5754 | } else if (params->openssl_ecdh_curves[0]) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5755 | #ifdef OPENSSL_IS_BORINGSSL |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5756 | wpa_printf(MSG_INFO, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5757 | "OpenSSL: ECDH configuration not supported"); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5758 | return -1; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5759 | #else /* !OPENSSL_IS_BORINGSSL */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5760 | #ifndef OPENSSL_NO_EC |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 5761 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 5762 | SSL_CTX_set_ecdh_auto(ssl_ctx, 1); |
| 5763 | #endif |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 5764 | if (SSL_CTX_set1_curves_list(ssl_ctx, |
| 5765 | params->openssl_ecdh_curves) != |
| 5766 | 1) { |
| 5767 | wpa_printf(MSG_INFO, |
| 5768 | "OpenSSL: Failed to set ECDH curves '%s'", |
| 5769 | params->openssl_ecdh_curves); |
| 5770 | return -1; |
| 5771 | } |
| 5772 | #else /* OPENSSL_NO_EC */ |
| 5773 | wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported"); |
| 5774 | return -1; |
| 5775 | #endif /* OPENSSL_NO_EC */ |
| 5776 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 5777 | } |
| 5778 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5779 | #ifdef SSL_OP_NO_TICKET |
| 5780 | if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET) |
| 5781 | SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET); |
| 5782 | else |
| 5783 | SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET); |
| 5784 | #endif /* SSL_OP_NO_TICKET */ |
| 5785 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 5786 | #ifdef HAVE_OCSP |
| 5787 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb); |
| 5788 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx); |
| 5789 | os_free(tls_global->ocsp_stapling_response); |
| 5790 | if (params->ocsp_stapling_response) |
| 5791 | tls_global->ocsp_stapling_response = |
| 5792 | os_strdup(params->ocsp_stapling_response); |
| 5793 | else |
| 5794 | tls_global->ocsp_stapling_response = NULL; |
| 5795 | #endif /* HAVE_OCSP */ |
| 5796 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5797 | openssl_debug_dump_ctx(ssl_ctx); |
| 5798 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5799 | return 0; |
| 5800 | } |
| 5801 | |
| 5802 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5803 | #ifdef EAP_FAST_OR_TEAP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5804 | /* Pre-shared secred requires a patch to openssl, so this function is |
| 5805 | * commented out unless explicitly needed for EAP-FAST in order to be able to |
| 5806 | * build this file with unmodified openssl. */ |
| 5807 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 5808 | #if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 5809 | static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len, |
| 5810 | STACK_OF(SSL_CIPHER) *peer_ciphers, |
| 5811 | const SSL_CIPHER **cipher, void *arg) |
| 5812 | #else /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5813 | static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len, |
| 5814 | STACK_OF(SSL_CIPHER) *peer_ciphers, |
| 5815 | SSL_CIPHER **cipher, void *arg) |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 5816 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5817 | { |
| 5818 | struct tls_connection *conn = arg; |
| 5819 | int ret; |
| 5820 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5821 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5822 | if (conn == NULL || conn->session_ticket_cb == NULL) |
| 5823 | return 0; |
| 5824 | |
| 5825 | ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx, |
| 5826 | conn->session_ticket, |
| 5827 | conn->session_ticket_len, |
| 5828 | s->s3->client_random, |
| 5829 | s->s3->server_random, secret); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5830 | #else |
| 5831 | unsigned char client_random[SSL3_RANDOM_SIZE]; |
| 5832 | unsigned char server_random[SSL3_RANDOM_SIZE]; |
| 5833 | |
| 5834 | if (conn == NULL || conn->session_ticket_cb == NULL) |
| 5835 | return 0; |
| 5836 | |
| 5837 | SSL_get_client_random(s, client_random, sizeof(client_random)); |
| 5838 | SSL_get_server_random(s, server_random, sizeof(server_random)); |
| 5839 | |
| 5840 | ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx, |
| 5841 | conn->session_ticket, |
| 5842 | conn->session_ticket_len, |
| 5843 | client_random, |
| 5844 | server_random, secret); |
| 5845 | #endif |
| 5846 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5847 | os_free(conn->session_ticket); |
| 5848 | conn->session_ticket = NULL; |
| 5849 | |
| 5850 | if (ret <= 0) |
| 5851 | return 0; |
| 5852 | |
| 5853 | *secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 5854 | return 1; |
| 5855 | } |
| 5856 | |
| 5857 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5858 | static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data, |
| 5859 | int len, void *arg) |
| 5860 | { |
| 5861 | struct tls_connection *conn = arg; |
| 5862 | |
| 5863 | if (conn == NULL || conn->session_ticket_cb == NULL) |
| 5864 | return 0; |
| 5865 | |
| 5866 | wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len); |
| 5867 | |
| 5868 | os_free(conn->session_ticket); |
| 5869 | conn->session_ticket = NULL; |
| 5870 | |
| 5871 | wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket " |
| 5872 | "extension", data, len); |
| 5873 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5874 | conn->session_ticket = os_memdup(data, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5875 | if (conn->session_ticket == NULL) |
| 5876 | return 0; |
| 5877 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5878 | conn->session_ticket_len = len; |
| 5879 | |
| 5880 | return 1; |
| 5881 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5882 | #endif /* EAP_FAST_OR_TEAP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5883 | |
| 5884 | |
| 5885 | int tls_connection_set_session_ticket_cb(void *tls_ctx, |
| 5886 | struct tls_connection *conn, |
| 5887 | tls_session_ticket_cb cb, |
| 5888 | void *ctx) |
| 5889 | { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5890 | #ifdef EAP_FAST_OR_TEAP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5891 | conn->session_ticket_cb = cb; |
| 5892 | conn->session_ticket_cb_ctx = ctx; |
| 5893 | |
| 5894 | if (cb) { |
| 5895 | if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb, |
| 5896 | conn) != 1) |
| 5897 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5898 | SSL_set_session_ticket_ext_cb(conn->ssl, |
| 5899 | tls_session_ticket_ext_cb, conn); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5900 | } else { |
| 5901 | if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1) |
| 5902 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5903 | SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5904 | } |
| 5905 | |
| 5906 | return 0; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5907 | #else /* EAP_FAST_OR_TEAP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5908 | return -1; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5909 | #endif /* EAP_FAST_OR_TEAP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5910 | } |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 5911 | |
| 5912 | |
| 5913 | int tls_get_library_version(char *buf, size_t buf_len) |
| 5914 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 5915 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5916 | return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s", |
| 5917 | OPENSSL_VERSION_TEXT, |
| 5918 | OpenSSL_version(OPENSSL_VERSION)); |
| 5919 | #else |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 5920 | return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s", |
| 5921 | OPENSSL_VERSION_TEXT, |
| 5922 | SSLeay_version(SSLEAY_VERSION)); |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 5923 | #endif |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 5924 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5925 | |
| 5926 | |
| 5927 | void tls_connection_set_success_data(struct tls_connection *conn, |
| 5928 | struct wpabuf *data) |
| 5929 | { |
| 5930 | SSL_SESSION *sess; |
| 5931 | struct wpabuf *old; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5932 | struct tls_session_data *sess_data = NULL; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5933 | |
| 5934 | if (tls_ex_idx_session < 0) |
| 5935 | goto fail; |
| 5936 | sess = SSL_get_session(conn->ssl); |
| 5937 | if (!sess) |
| 5938 | goto fail; |
| 5939 | old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); |
| 5940 | if (old) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5941 | struct tls_session_data *found; |
| 5942 | |
| 5943 | found = get_session_data(conn->context, old); |
| 5944 | wpa_printf(MSG_DEBUG, |
| 5945 | "OpenSSL: Replacing old success data %p (sess %p)%s", |
| 5946 | old, sess, found ? "" : " (not freeing)"); |
| 5947 | if (found) { |
| 5948 | dl_list_del(&found->list); |
| 5949 | os_free(found); |
| 5950 | wpabuf_free(old); |
| 5951 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5952 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5953 | |
| 5954 | sess_data = os_zalloc(sizeof(*sess_data)); |
| 5955 | if (!sess_data || |
| 5956 | SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5957 | goto fail; |
| 5958 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5959 | sess_data->buf = data; |
| 5960 | dl_list_add(&conn->context->sessions, &sess_data->list); |
| 5961 | wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p (sess %p)", |
| 5962 | data, sess); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5963 | conn->success_data = 1; |
| 5964 | return; |
| 5965 | |
| 5966 | fail: |
| 5967 | wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data"); |
| 5968 | wpabuf_free(data); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 5969 | os_free(sess_data); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | |
| 5973 | void tls_connection_set_success_data_resumed(struct tls_connection *conn) |
| 5974 | { |
| 5975 | wpa_printf(MSG_DEBUG, |
| 5976 | "OpenSSL: Success data accepted for resumed session"); |
| 5977 | conn->success_data = 1; |
| 5978 | } |
| 5979 | |
| 5980 | |
| 5981 | const struct wpabuf * |
| 5982 | tls_connection_get_success_data(struct tls_connection *conn) |
| 5983 | { |
| 5984 | SSL_SESSION *sess; |
| 5985 | |
| 5986 | if (tls_ex_idx_session < 0 || |
| 5987 | !(sess = SSL_get_session(conn->ssl))) |
| 5988 | return NULL; |
| 5989 | return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); |
| 5990 | } |
| 5991 | |
| 5992 | |
| 5993 | void tls_connection_remove_session(struct tls_connection *conn) |
| 5994 | { |
| 5995 | SSL_SESSION *sess; |
| 5996 | |
| 5997 | sess = SSL_get_session(conn->ssl); |
| 5998 | if (!sess) |
| 5999 | return; |
| 6000 | |
| 6001 | if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1) |
| 6002 | wpa_printf(MSG_DEBUG, |
| 6003 | "OpenSSL: Session was not cached"); |
| 6004 | else |
| 6005 | wpa_printf(MSG_DEBUG, |
| 6006 | "OpenSSL: Removed cached session to disable session resumption"); |
| 6007 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 6008 | |
| 6009 | |
| 6010 | int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len) |
| 6011 | { |
| 6012 | size_t len; |
| 6013 | int reused; |
| 6014 | |
| 6015 | reused = SSL_session_reused(conn->ssl); |
| 6016 | if ((conn->server && !reused) || (!conn->server && reused)) |
| 6017 | len = SSL_get_peer_finished(conn->ssl, buf, max_len); |
| 6018 | else |
| 6019 | len = SSL_get_finished(conn->ssl, buf, max_len); |
| 6020 | |
| 6021 | if (len == 0 || len > max_len) |
| 6022 | return -1; |
| 6023 | |
| 6024 | return len; |
| 6025 | } |
| 6026 | |
| 6027 | |
| 6028 | u16 tls_connection_get_cipher_suite(struct tls_connection *conn) |
| 6029 | { |
| 6030 | const SSL_CIPHER *cipher; |
| 6031 | |
| 6032 | cipher = SSL_get_current_cipher(conn->ssl); |
| 6033 | if (!cipher) |
| 6034 | return 0; |
| 6035 | #if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER) |
| 6036 | return SSL_CIPHER_get_protocol_id(cipher); |
| 6037 | #else |
| 6038 | return SSL_CIPHER_get_id(cipher) & 0xFFFF; |
| 6039 | #endif |
| 6040 | } |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 6041 | |
| 6042 | |
| 6043 | const char * tls_connection_get_peer_subject(struct tls_connection *conn) |
| 6044 | { |
| 6045 | if (conn) |
| 6046 | return conn->peer_subject; |
| 6047 | return NULL; |
| 6048 | } |
| 6049 | |
| 6050 | |
| 6051 | bool tls_connection_get_own_cert_used(struct tls_connection *conn) |
| 6052 | { |
| 6053 | if (conn) |
| 6054 | return SSL_get_certificate(conn->ssl) != NULL; |
| 6055 | return false; |
| 6056 | } |
Gabriel Biren | a5bdf37 | 2022-12-15 20:54:33 +0000 | [diff] [blame] | 6057 | |
| 6058 | void tls_register_cert_callback(tls_get_certificate_cb cb) |
| 6059 | { |
| 6060 | certificate_callback_global = cb; |
| 6061 | } |
Gabriel Biren | 60ae068 | 2023-11-01 22:04:12 +0000 | [diff] [blame] | 6062 | |
| 6063 | void tls_register_openssl_failure_callback(tls_openssl_failure_cb cb) |
| 6064 | { |
| 6065 | openssl_failure_callback_global = cb; |
| 6066 | } |