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" |
| 10 | |
| 11 | #ifndef CONFIG_SMARTCARD |
| 12 | #ifndef OPENSSL_NO_ENGINE |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 13 | #ifndef ANDROID |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 14 | #define OPENSSL_NO_ENGINE |
| 15 | #endif |
| 16 | #endif |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 17 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 18 | |
| 19 | #include <openssl/ssl.h> |
| 20 | #include <openssl/err.h> |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 21 | #include <openssl/opensslv.h> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 22 | #include <openssl/pkcs12.h> |
| 23 | #include <openssl/x509v3.h> |
| 24 | #ifndef OPENSSL_NO_ENGINE |
| 25 | #include <openssl/engine.h> |
| 26 | #endif /* OPENSSL_NO_ENGINE */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 27 | #ifndef OPENSSL_NO_DSA |
| 28 | #include <openssl/dsa.h> |
| 29 | #endif |
| 30 | #ifndef OPENSSL_NO_DH |
| 31 | #include <openssl/dh.h> |
| 32 | #endif |
| 33 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 34 | #include "common.h" |
| 35 | #include "crypto.h" |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 36 | #include "sha1.h" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 37 | #include "sha256.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 38 | #include "tls.h" |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 39 | #include "tls_openssl.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 40 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 41 | #if !defined(CONFIG_FIPS) && \ |
| 42 | (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \ |
| 43 | defined(EAP_SERVER_FAST)) |
| 44 | #define OPENSSL_NEED_EAP_FAST_PRF |
| 45 | #endif |
| 46 | |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 47 | #if defined(OPENSSL_IS_BORINGSSL) |
| 48 | /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */ |
| 49 | typedef size_t stack_index_t; |
| 50 | #else |
| 51 | typedef int stack_index_t; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 52 | #endif |
| 53 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 54 | #ifdef SSL_set_tlsext_status_type |
| 55 | #ifndef OPENSSL_NO_TLSEXT |
| 56 | #define HAVE_OCSP |
| 57 | #include <openssl/ocsp.h> |
| 58 | #endif /* OPENSSL_NO_TLSEXT */ |
| 59 | #endif /* SSL_set_tlsext_status_type */ |
| 60 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 61 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 62 | defined(LIBRESSL_VERSION_NUMBER)) && \ |
| 63 | !defined(BORINGSSL_API_VERSION) |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 64 | /* |
| 65 | * 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] | 66 | * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for |
| 67 | * older versions. |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 68 | */ |
| 69 | |
| 70 | static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, |
| 71 | size_t outlen) |
| 72 | { |
| 73 | if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE) |
| 74 | return 0; |
| 75 | os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE); |
| 76 | return SSL3_RANDOM_SIZE; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, |
| 81 | size_t outlen) |
| 82 | { |
| 83 | if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE) |
| 84 | return 0; |
| 85 | os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE); |
| 86 | return SSL3_RANDOM_SIZE; |
| 87 | } |
| 88 | |
| 89 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 90 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 91 | static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, |
| 92 | unsigned char *out, size_t outlen) |
| 93 | { |
| 94 | if (!session || session->master_key_length < 0 || |
| 95 | (size_t) session->master_key_length > outlen) |
| 96 | return 0; |
| 97 | if ((size_t) session->master_key_length < outlen) |
| 98 | outlen = session->master_key_length; |
| 99 | os_memcpy(out, session->master_key, outlen); |
| 100 | return outlen; |
| 101 | } |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 102 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 103 | |
| 104 | #endif |
| 105 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 106 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 107 | #ifdef CONFIG_SUITEB |
| 108 | static int RSA_bits(const RSA *r) |
| 109 | { |
| 110 | return BN_num_bits(r->n); |
| 111 | } |
| 112 | #endif /* CONFIG_SUITEB */ |
| 113 | #endif |
| 114 | |
Dmitry Shmidt | ff07917 | 2013-11-08 14:10:30 -0800 | [diff] [blame] | 115 | #ifdef ANDROID |
| 116 | #include <openssl/pem.h> |
| 117 | #include <keystore/keystore_get.h> |
| 118 | |
| 119 | static BIO * BIO_from_keystore(const char *key) |
| 120 | { |
| 121 | BIO *bio = NULL; |
| 122 | uint8_t *value = NULL; |
| 123 | int length = keystore_get(key, strlen(key), &value); |
| 124 | if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL) |
| 125 | BIO_write(bio, value, length); |
| 126 | free(value); |
| 127 | return bio; |
| 128 | } |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 129 | |
| 130 | |
| 131 | static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *key_alias) |
| 132 | { |
| 133 | BIO *bio = BIO_from_keystore(key_alias); |
| 134 | STACK_OF(X509_INFO) *stack = NULL; |
| 135 | stack_index_t i; |
| 136 | |
| 137 | if (bio) { |
| 138 | stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL); |
| 139 | BIO_free(bio); |
| 140 | } |
| 141 | |
| 142 | if (!stack) { |
| 143 | wpa_printf(MSG_WARNING, "TLS: Failed to parse certificate: %s", |
| 144 | key_alias); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | for (i = 0; i < sk_X509_INFO_num(stack); ++i) { |
| 149 | X509_INFO *info = sk_X509_INFO_value(stack, i); |
| 150 | |
| 151 | if (info->x509) |
| 152 | X509_STORE_add_cert(ctx, info->x509); |
| 153 | if (info->crl) |
| 154 | X509_STORE_add_crl(ctx, info->crl); |
| 155 | } |
| 156 | |
| 157 | sk_X509_INFO_pop_free(stack, X509_INFO_free); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx, |
| 164 | const char *encoded_key_alias) |
| 165 | { |
| 166 | int rc = -1; |
| 167 | int len = os_strlen(encoded_key_alias); |
| 168 | unsigned char *decoded_alias; |
| 169 | |
| 170 | if (len & 1) { |
| 171 | wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s", |
| 172 | encoded_key_alias); |
| 173 | return rc; |
| 174 | } |
| 175 | |
| 176 | decoded_alias = os_malloc(len / 2 + 1); |
| 177 | if (decoded_alias) { |
| 178 | if (!hexstr2bin(encoded_key_alias, decoded_alias, len / 2)) { |
| 179 | decoded_alias[len / 2] = '\0'; |
| 180 | rc = tls_add_ca_from_keystore( |
| 181 | ctx, (const char *) decoded_alias); |
| 182 | } |
| 183 | os_free(decoded_alias); |
| 184 | } |
| 185 | |
| 186 | return rc; |
| 187 | } |
| 188 | |
Dmitry Shmidt | ff07917 | 2013-11-08 14:10:30 -0800 | [diff] [blame] | 189 | #endif /* ANDROID */ |
| 190 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 191 | static int tls_openssl_ref_count = 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 192 | static int tls_ex_idx_session = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 193 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 194 | struct tls_context { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 195 | void (*event_cb)(void *ctx, enum tls_event ev, |
| 196 | union tls_event_data *data); |
| 197 | void *cb_ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 198 | int cert_in_cb; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 199 | char *ocsp_stapling_response; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 202 | static struct tls_context *tls_global = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 203 | |
| 204 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 205 | struct tls_data { |
| 206 | SSL_CTX *ssl; |
| 207 | unsigned int tls_session_lifetime; |
| 208 | }; |
| 209 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 210 | struct tls_connection { |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 211 | struct tls_context *context; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 212 | SSL_CTX *ssl_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 213 | SSL *ssl; |
| 214 | BIO *ssl_in, *ssl_out; |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 215 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 216 | ENGINE *engine; /* functional reference to the engine */ |
| 217 | EVP_PKEY *private_key; /* the private key if using engine */ |
| 218 | #endif /* OPENSSL_NO_ENGINE */ |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 219 | char *subject_match, *altsubject_match, *suffix_match, *domain_match; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 220 | int read_alerts, write_alerts, failed; |
| 221 | |
| 222 | tls_session_ticket_cb session_ticket_cb; |
| 223 | void *session_ticket_cb_ctx; |
| 224 | |
| 225 | /* SessionTicket received from OpenSSL hello_extension_cb (server) */ |
| 226 | u8 *session_ticket; |
| 227 | size_t session_ticket_len; |
| 228 | |
| 229 | unsigned int ca_cert_verify:1; |
| 230 | unsigned int cert_probe:1; |
| 231 | unsigned int server_cert_only:1; |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 232 | unsigned int invalid_hb_used:1; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 233 | unsigned int success_data:1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 234 | |
| 235 | u8 srv_cert_hash[32]; |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 236 | |
| 237 | unsigned int flags; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 238 | |
| 239 | X509 *peer_cert; |
| 240 | X509 *peer_issuer; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 241 | X509 *peer_issuer_issuer; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 242 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 243 | unsigned char client_random[SSL3_RANDOM_SIZE]; |
| 244 | unsigned char server_random[SSL3_RANDOM_SIZE]; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 245 | |
| 246 | u16 cipher_suite; |
| 247 | int server_dh_prime_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 251 | static struct tls_context * tls_context_new(const struct tls_config *conf) |
| 252 | { |
| 253 | struct tls_context *context = os_zalloc(sizeof(*context)); |
| 254 | if (context == NULL) |
| 255 | return NULL; |
| 256 | if (conf) { |
| 257 | context->event_cb = conf->event_cb; |
| 258 | context->cb_ctx = conf->cb_ctx; |
| 259 | context->cert_in_cb = conf->cert_in_cb; |
| 260 | } |
| 261 | return context; |
| 262 | } |
| 263 | |
| 264 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 265 | #ifdef CONFIG_NO_STDOUT_DEBUG |
| 266 | |
| 267 | static void _tls_show_errors(void) |
| 268 | { |
| 269 | unsigned long err; |
| 270 | |
| 271 | while ((err = ERR_get_error())) { |
| 272 | /* Just ignore the errors, since stdout is disabled */ |
| 273 | } |
| 274 | } |
| 275 | #define tls_show_errors(l, f, t) _tls_show_errors() |
| 276 | |
| 277 | #else /* CONFIG_NO_STDOUT_DEBUG */ |
| 278 | |
| 279 | static void tls_show_errors(int level, const char *func, const char *txt) |
| 280 | { |
| 281 | unsigned long err; |
| 282 | |
| 283 | wpa_printf(level, "OpenSSL: %s - %s %s", |
| 284 | func, txt, ERR_error_string(ERR_get_error(), NULL)); |
| 285 | |
| 286 | while ((err = ERR_get_error())) { |
| 287 | wpa_printf(MSG_INFO, "OpenSSL: pending error: %s", |
| 288 | ERR_error_string(err, NULL)); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 293 | |
| 294 | |
| 295 | #ifdef CONFIG_NATIVE_WINDOWS |
| 296 | |
| 297 | /* Windows CryptoAPI and access to certificate stores */ |
| 298 | #include <wincrypt.h> |
| 299 | |
| 300 | #ifdef __MINGW32_VERSION |
| 301 | /* |
| 302 | * MinGW does not yet include all the needed definitions for CryptoAPI, so |
| 303 | * define here whatever extra is needed. |
| 304 | */ |
| 305 | #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16) |
| 306 | #define CERT_STORE_READONLY_FLAG 0x00008000 |
| 307 | #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000 |
| 308 | |
| 309 | #endif /* __MINGW32_VERSION */ |
| 310 | |
| 311 | |
| 312 | struct cryptoapi_rsa_data { |
| 313 | const CERT_CONTEXT *cert; |
| 314 | HCRYPTPROV crypt_prov; |
| 315 | DWORD key_spec; |
| 316 | BOOL free_crypt_prov; |
| 317 | }; |
| 318 | |
| 319 | |
| 320 | static void cryptoapi_error(const char *msg) |
| 321 | { |
| 322 | wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u", |
| 323 | msg, (unsigned int) GetLastError()); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from, |
| 328 | unsigned char *to, RSA *rsa, int padding) |
| 329 | { |
| 330 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from, |
| 336 | unsigned char *to, RSA *rsa, int padding) |
| 337 | { |
| 338 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from, |
| 344 | unsigned char *to, RSA *rsa, int padding) |
| 345 | { |
| 346 | struct cryptoapi_rsa_data *priv = |
| 347 | (struct cryptoapi_rsa_data *) rsa->meth->app_data; |
| 348 | HCRYPTHASH hash; |
| 349 | DWORD hash_size, len, i; |
| 350 | unsigned char *buf = NULL; |
| 351 | int ret = 0; |
| 352 | |
| 353 | if (priv == NULL) { |
| 354 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 355 | ERR_R_PASSED_NULL_PARAMETER); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | if (padding != RSA_PKCS1_PADDING) { |
| 360 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 361 | RSA_R_UNKNOWN_PADDING_TYPE); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) { |
| 366 | wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported", |
| 367 | __func__); |
| 368 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 369 | RSA_R_INVALID_MESSAGE_LENGTH); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash)) |
| 374 | { |
| 375 | cryptoapi_error("CryptCreateHash failed"); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | len = sizeof(hash_size); |
| 380 | if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len, |
| 381 | 0)) { |
| 382 | cryptoapi_error("CryptGetHashParam failed"); |
| 383 | goto err; |
| 384 | } |
| 385 | |
| 386 | if ((int) hash_size != flen) { |
| 387 | wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)", |
| 388 | (unsigned) hash_size, flen); |
| 389 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, |
| 390 | RSA_R_INVALID_MESSAGE_LENGTH); |
| 391 | goto err; |
| 392 | } |
| 393 | if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) { |
| 394 | cryptoapi_error("CryptSetHashParam failed"); |
| 395 | goto err; |
| 396 | } |
| 397 | |
| 398 | len = RSA_size(rsa); |
| 399 | buf = os_malloc(len); |
| 400 | if (buf == NULL) { |
| 401 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE); |
| 402 | goto err; |
| 403 | } |
| 404 | |
| 405 | if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) { |
| 406 | cryptoapi_error("CryptSignHash failed"); |
| 407 | goto err; |
| 408 | } |
| 409 | |
| 410 | for (i = 0; i < len; i++) |
| 411 | to[i] = buf[len - i - 1]; |
| 412 | ret = len; |
| 413 | |
| 414 | err: |
| 415 | os_free(buf); |
| 416 | CryptDestroyHash(hash); |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from, |
| 423 | unsigned char *to, RSA *rsa, int padding) |
| 424 | { |
| 425 | wpa_printf(MSG_DEBUG, "%s - not implemented", __func__); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv) |
| 431 | { |
| 432 | if (priv == NULL) |
| 433 | return; |
| 434 | if (priv->crypt_prov && priv->free_crypt_prov) |
| 435 | CryptReleaseContext(priv->crypt_prov, 0); |
| 436 | if (priv->cert) |
| 437 | CertFreeCertificateContext(priv->cert); |
| 438 | os_free(priv); |
| 439 | } |
| 440 | |
| 441 | |
| 442 | static int cryptoapi_finish(RSA *rsa) |
| 443 | { |
| 444 | cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data); |
| 445 | os_free((void *) rsa->meth); |
| 446 | rsa->meth = NULL; |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store) |
| 452 | { |
| 453 | HCERTSTORE cs; |
| 454 | const CERT_CONTEXT *ret = NULL; |
| 455 | |
| 456 | cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0, |
| 457 | store | CERT_STORE_OPEN_EXISTING_FLAG | |
| 458 | CERT_STORE_READONLY_FLAG, L"MY"); |
| 459 | if (cs == NULL) { |
| 460 | cryptoapi_error("Failed to open 'My system store'"); |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | if (strncmp(name, "cert://", 7) == 0) { |
| 465 | unsigned short wbuf[255]; |
| 466 | MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255); |
| 467 | ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING | |
| 468 | PKCS_7_ASN_ENCODING, |
| 469 | 0, CERT_FIND_SUBJECT_STR, |
| 470 | wbuf, NULL); |
| 471 | } else if (strncmp(name, "hash://", 7) == 0) { |
| 472 | CRYPT_HASH_BLOB blob; |
| 473 | int len; |
| 474 | const char *hash = name + 7; |
| 475 | unsigned char *buf; |
| 476 | |
| 477 | len = os_strlen(hash) / 2; |
| 478 | buf = os_malloc(len); |
| 479 | if (buf && hexstr2bin(hash, buf, len) == 0) { |
| 480 | blob.cbData = len; |
| 481 | blob.pbData = buf; |
| 482 | ret = CertFindCertificateInStore(cs, |
| 483 | X509_ASN_ENCODING | |
| 484 | PKCS_7_ASN_ENCODING, |
| 485 | 0, CERT_FIND_HASH, |
| 486 | &blob, NULL); |
| 487 | } |
| 488 | os_free(buf); |
| 489 | } |
| 490 | |
| 491 | CertCloseStore(cs, 0); |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) |
| 498 | { |
| 499 | X509 *cert = NULL; |
| 500 | RSA *rsa = NULL, *pub_rsa; |
| 501 | struct cryptoapi_rsa_data *priv; |
| 502 | RSA_METHOD *rsa_meth; |
| 503 | |
| 504 | if (name == NULL || |
| 505 | (strncmp(name, "cert://", 7) != 0 && |
| 506 | strncmp(name, "hash://", 7) != 0)) |
| 507 | return -1; |
| 508 | |
| 509 | priv = os_zalloc(sizeof(*priv)); |
| 510 | rsa_meth = os_zalloc(sizeof(*rsa_meth)); |
| 511 | if (priv == NULL || rsa_meth == NULL) { |
| 512 | wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory " |
| 513 | "for CryptoAPI RSA method"); |
| 514 | os_free(priv); |
| 515 | os_free(rsa_meth); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER); |
| 520 | if (priv->cert == NULL) { |
| 521 | priv->cert = cryptoapi_find_cert( |
| 522 | name, CERT_SYSTEM_STORE_LOCAL_MACHINE); |
| 523 | } |
| 524 | if (priv->cert == NULL) { |
| 525 | wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate " |
| 526 | "'%s'", name); |
| 527 | goto err; |
| 528 | } |
| 529 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 530 | cert = d2i_X509(NULL, |
| 531 | (const unsigned char **) &priv->cert->pbCertEncoded, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | priv->cert->cbCertEncoded); |
| 533 | if (cert == NULL) { |
| 534 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER " |
| 535 | "encoding"); |
| 536 | goto err; |
| 537 | } |
| 538 | |
| 539 | if (!CryptAcquireCertificatePrivateKey(priv->cert, |
| 540 | CRYPT_ACQUIRE_COMPARE_KEY_FLAG, |
| 541 | NULL, &priv->crypt_prov, |
| 542 | &priv->key_spec, |
| 543 | &priv->free_crypt_prov)) { |
| 544 | cryptoapi_error("Failed to acquire a private key for the " |
| 545 | "certificate"); |
| 546 | goto err; |
| 547 | } |
| 548 | |
| 549 | rsa_meth->name = "Microsoft CryptoAPI RSA Method"; |
| 550 | rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc; |
| 551 | rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec; |
| 552 | rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc; |
| 553 | rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec; |
| 554 | rsa_meth->finish = cryptoapi_finish; |
| 555 | rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK; |
| 556 | rsa_meth->app_data = (char *) priv; |
| 557 | |
| 558 | rsa = RSA_new(); |
| 559 | if (rsa == NULL) { |
| 560 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, |
| 561 | ERR_R_MALLOC_FAILURE); |
| 562 | goto err; |
| 563 | } |
| 564 | |
| 565 | if (!SSL_use_certificate(ssl, cert)) { |
| 566 | RSA_free(rsa); |
| 567 | rsa = NULL; |
| 568 | goto err; |
| 569 | } |
| 570 | pub_rsa = cert->cert_info->key->pkey->pkey.rsa; |
| 571 | X509_free(cert); |
| 572 | cert = NULL; |
| 573 | |
| 574 | rsa->n = BN_dup(pub_rsa->n); |
| 575 | rsa->e = BN_dup(pub_rsa->e); |
| 576 | if (!RSA_set_method(rsa, rsa_meth)) |
| 577 | goto err; |
| 578 | |
| 579 | if (!SSL_use_RSAPrivateKey(ssl, rsa)) |
| 580 | goto err; |
| 581 | RSA_free(rsa); |
| 582 | |
| 583 | return 0; |
| 584 | |
| 585 | err: |
| 586 | if (cert) |
| 587 | X509_free(cert); |
| 588 | if (rsa) |
| 589 | RSA_free(rsa); |
| 590 | else { |
| 591 | os_free(rsa_meth); |
| 592 | cryptoapi_free_data(priv); |
| 593 | } |
| 594 | return -1; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name) |
| 599 | { |
| 600 | HCERTSTORE cs; |
| 601 | PCCERT_CONTEXT ctx = NULL; |
| 602 | X509 *cert; |
| 603 | char buf[128]; |
| 604 | const char *store; |
| 605 | #ifdef UNICODE |
| 606 | WCHAR *wstore; |
| 607 | #endif /* UNICODE */ |
| 608 | |
| 609 | if (name == NULL || strncmp(name, "cert_store://", 13) != 0) |
| 610 | return -1; |
| 611 | |
| 612 | store = name + 13; |
| 613 | #ifdef UNICODE |
| 614 | wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR)); |
| 615 | if (wstore == NULL) |
| 616 | return -1; |
| 617 | wsprintf(wstore, L"%S", store); |
| 618 | cs = CertOpenSystemStore(0, wstore); |
| 619 | os_free(wstore); |
| 620 | #else /* UNICODE */ |
| 621 | cs = CertOpenSystemStore(0, store); |
| 622 | #endif /* UNICODE */ |
| 623 | if (cs == NULL) { |
| 624 | wpa_printf(MSG_DEBUG, "%s: failed to open system cert store " |
| 625 | "'%s': error=%d", __func__, store, |
| 626 | (int) GetLastError()); |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | while ((ctx = CertEnumCertificatesInStore(cs, ctx))) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 631 | cert = d2i_X509(NULL, |
| 632 | (const unsigned char **) &ctx->pbCertEncoded, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 633 | ctx->cbCertEncoded); |
| 634 | if (cert == NULL) { |
| 635 | wpa_printf(MSG_INFO, "CryptoAPI: Could not process " |
| 636 | "X509 DER encoding for CA cert"); |
| 637 | continue; |
| 638 | } |
| 639 | |
| 640 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 641 | sizeof(buf)); |
| 642 | wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for " |
| 643 | "system certificate store: subject='%s'", buf); |
| 644 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 645 | if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx), |
| 646 | cert)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 647 | tls_show_errors(MSG_WARNING, __func__, |
| 648 | "Failed to add ca_cert to OpenSSL " |
| 649 | "certificate store"); |
| 650 | } |
| 651 | |
| 652 | X509_free(cert); |
| 653 | } |
| 654 | |
| 655 | if (!CertCloseStore(cs, 0)) { |
| 656 | wpa_printf(MSG_DEBUG, "%s: failed to close system cert store " |
| 657 | "'%s': error=%d", __func__, name + 13, |
| 658 | (int) GetLastError()); |
| 659 | } |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | #else /* CONFIG_NATIVE_WINDOWS */ |
| 666 | |
| 667 | static int tls_cryptoapi_cert(SSL *ssl, const char *name) |
| 668 | { |
| 669 | return -1; |
| 670 | } |
| 671 | |
| 672 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 673 | |
| 674 | |
| 675 | static void ssl_info_cb(const SSL *ssl, int where, int ret) |
| 676 | { |
| 677 | const char *str; |
| 678 | int w; |
| 679 | |
| 680 | wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret); |
| 681 | w = where & ~SSL_ST_MASK; |
| 682 | if (w & SSL_ST_CONNECT) |
| 683 | str = "SSL_connect"; |
| 684 | else if (w & SSL_ST_ACCEPT) |
| 685 | str = "SSL_accept"; |
| 686 | else |
| 687 | str = "undefined"; |
| 688 | |
| 689 | if (where & SSL_CB_LOOP) { |
| 690 | wpa_printf(MSG_DEBUG, "SSL: %s:%s", |
| 691 | str, SSL_state_string_long(ssl)); |
| 692 | } else if (where & SSL_CB_ALERT) { |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 693 | struct tls_connection *conn = SSL_get_app_data((SSL *) ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 694 | wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s", |
| 695 | where & SSL_CB_READ ? |
| 696 | "read (remote end reported an error)" : |
| 697 | "write (local SSL3 detected an error)", |
| 698 | SSL_alert_type_string_long(ret), |
| 699 | SSL_alert_desc_string_long(ret)); |
| 700 | if ((ret >> 8) == SSL3_AL_FATAL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 701 | if (where & SSL_CB_READ) |
| 702 | conn->read_alerts++; |
| 703 | else |
| 704 | conn->write_alerts++; |
| 705 | } |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 706 | if (conn->context->event_cb != NULL) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 707 | union tls_event_data ev; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 708 | struct tls_context *context = conn->context; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 709 | os_memset(&ev, 0, sizeof(ev)); |
| 710 | ev.alert.is_local = !(where & SSL_CB_READ); |
| 711 | ev.alert.type = SSL_alert_type_string_long(ret); |
| 712 | ev.alert.description = SSL_alert_desc_string_long(ret); |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 713 | context->event_cb(context->cb_ctx, TLS_ALERT, &ev); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 714 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 715 | } else if (where & SSL_CB_EXIT && ret <= 0) { |
| 716 | wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s", |
| 717 | str, ret == 0 ? "failed" : "error", |
| 718 | SSL_state_string_long(ssl)); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | |
| 723 | #ifndef OPENSSL_NO_ENGINE |
| 724 | /** |
| 725 | * tls_engine_load_dynamic_generic - load any openssl engine |
| 726 | * @pre: an array of commands and values that load an engine initialized |
| 727 | * in the engine specific function |
| 728 | * @post: an array of commands and values that initialize an already loaded |
| 729 | * engine (or %NULL if not required) |
| 730 | * @id: the engine id of the engine to load (only required if post is not %NULL |
| 731 | * |
| 732 | * This function is a generic function that loads any openssl engine. |
| 733 | * |
| 734 | * Returns: 0 on success, -1 on failure |
| 735 | */ |
| 736 | static int tls_engine_load_dynamic_generic(const char *pre[], |
| 737 | const char *post[], const char *id) |
| 738 | { |
| 739 | ENGINE *engine; |
| 740 | const char *dynamic_id = "dynamic"; |
| 741 | |
| 742 | engine = ENGINE_by_id(id); |
| 743 | if (engine) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 744 | wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already " |
| 745 | "available", id); |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 746 | /* |
| 747 | * If it was auto-loaded by ENGINE_by_id() we might still |
| 748 | * need to tell it which PKCS#11 module to use in legacy |
| 749 | * (non-p11-kit) environments. Do so now; even if it was |
| 750 | * properly initialised before, setting it again will be |
| 751 | * harmless. |
| 752 | */ |
| 753 | goto found; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 754 | } |
| 755 | ERR_clear_error(); |
| 756 | |
| 757 | engine = ENGINE_by_id(dynamic_id); |
| 758 | if (engine == NULL) { |
| 759 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", |
| 760 | dynamic_id, |
| 761 | ERR_error_string(ERR_get_error(), NULL)); |
| 762 | return -1; |
| 763 | } |
| 764 | |
| 765 | /* Perform the pre commands. This will load the engine. */ |
| 766 | while (pre && pre[0]) { |
| 767 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]); |
| 768 | if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) { |
| 769 | wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: " |
| 770 | "%s %s [%s]", pre[0], pre[1], |
| 771 | ERR_error_string(ERR_get_error(), NULL)); |
| 772 | ENGINE_free(engine); |
| 773 | return -1; |
| 774 | } |
| 775 | pre += 2; |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * Free the reference to the "dynamic" engine. The loaded engine can |
| 780 | * now be looked up using ENGINE_by_id(). |
| 781 | */ |
| 782 | ENGINE_free(engine); |
| 783 | |
| 784 | engine = ENGINE_by_id(id); |
| 785 | if (engine == NULL) { |
| 786 | wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]", |
| 787 | id, ERR_error_string(ERR_get_error(), NULL)); |
| 788 | return -1; |
| 789 | } |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 790 | found: |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 791 | while (post && post[0]) { |
| 792 | wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]); |
| 793 | if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) { |
| 794 | wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:" |
| 795 | " %s %s [%s]", post[0], post[1], |
| 796 | ERR_error_string(ERR_get_error(), NULL)); |
| 797 | ENGINE_remove(engine); |
| 798 | ENGINE_free(engine); |
| 799 | return -1; |
| 800 | } |
| 801 | post += 2; |
| 802 | } |
| 803 | ENGINE_free(engine); |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | |
| 809 | /** |
| 810 | * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc |
| 811 | * @pkcs11_so_path: pksc11_so_path from the configuration |
| 812 | * @pcks11_module_path: pkcs11_module_path from the configuration |
| 813 | */ |
| 814 | static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path, |
| 815 | const char *pkcs11_module_path) |
| 816 | { |
| 817 | char *engine_id = "pkcs11"; |
| 818 | const char *pre_cmd[] = { |
| 819 | "SO_PATH", NULL /* pkcs11_so_path */, |
| 820 | "ID", NULL /* engine_id */, |
| 821 | "LIST_ADD", "1", |
| 822 | /* "NO_VCHECK", "1", */ |
| 823 | "LOAD", NULL, |
| 824 | NULL, NULL |
| 825 | }; |
| 826 | const char *post_cmd[] = { |
| 827 | "MODULE_PATH", NULL /* pkcs11_module_path */, |
| 828 | NULL, NULL |
| 829 | }; |
| 830 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 831 | if (!pkcs11_so_path) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 832 | return 0; |
| 833 | |
| 834 | pre_cmd[1] = pkcs11_so_path; |
| 835 | pre_cmd[3] = engine_id; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 836 | if (pkcs11_module_path) |
| 837 | post_cmd[1] = pkcs11_module_path; |
| 838 | else |
| 839 | post_cmd[0] = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 840 | |
| 841 | wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s", |
| 842 | pkcs11_so_path); |
| 843 | |
| 844 | return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | /** |
| 849 | * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc |
| 850 | * @opensc_so_path: opensc_so_path from the configuration |
| 851 | */ |
| 852 | static int tls_engine_load_dynamic_opensc(const char *opensc_so_path) |
| 853 | { |
| 854 | char *engine_id = "opensc"; |
| 855 | const char *pre_cmd[] = { |
| 856 | "SO_PATH", NULL /* opensc_so_path */, |
| 857 | "ID", NULL /* engine_id */, |
| 858 | "LIST_ADD", "1", |
| 859 | "LOAD", NULL, |
| 860 | NULL, NULL |
| 861 | }; |
| 862 | |
| 863 | if (!opensc_so_path) |
| 864 | return 0; |
| 865 | |
| 866 | pre_cmd[1] = opensc_so_path; |
| 867 | pre_cmd[3] = engine_id; |
| 868 | |
| 869 | wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s", |
| 870 | opensc_so_path); |
| 871 | |
| 872 | return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id); |
| 873 | } |
| 874 | #endif /* OPENSSL_NO_ENGINE */ |
| 875 | |
| 876 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 877 | static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
| 878 | { |
| 879 | struct wpabuf *buf; |
| 880 | |
| 881 | if (tls_ex_idx_session < 0) |
| 882 | return; |
| 883 | buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); |
| 884 | if (!buf) |
| 885 | return; |
| 886 | wpa_printf(MSG_DEBUG, |
| 887 | "OpenSSL: Free application session data %p (sess %p)", |
| 888 | buf, sess); |
| 889 | wpabuf_free(buf); |
| 890 | |
| 891 | SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL); |
| 892 | } |
| 893 | |
| 894 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 895 | void * tls_init(const struct tls_config *conf) |
| 896 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 897 | struct tls_data *data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 898 | SSL_CTX *ssl; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 899 | struct tls_context *context; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 900 | const char *ciphers; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 901 | |
| 902 | if (tls_openssl_ref_count == 0) { |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 903 | tls_global = context = tls_context_new(conf); |
| 904 | if (context == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 905 | return NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 906 | #ifdef CONFIG_FIPS |
| 907 | #ifdef OPENSSL_FIPS |
| 908 | if (conf && conf->fips_mode) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 909 | static int fips_enabled = 0; |
| 910 | |
| 911 | if (!fips_enabled && !FIPS_mode_set(1)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 912 | wpa_printf(MSG_ERROR, "Failed to enable FIPS " |
| 913 | "mode"); |
| 914 | ERR_load_crypto_strings(); |
| 915 | ERR_print_errors_fp(stderr); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 916 | os_free(tls_global); |
| 917 | tls_global = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 918 | return NULL; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 919 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 920 | wpa_printf(MSG_INFO, "Running in FIPS mode"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 921 | fips_enabled = 1; |
| 922 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 923 | } |
| 924 | #else /* OPENSSL_FIPS */ |
| 925 | if (conf && conf->fips_mode) { |
| 926 | wpa_printf(MSG_ERROR, "FIPS mode requested, but not " |
| 927 | "supported"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 928 | os_free(tls_global); |
| 929 | tls_global = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 930 | return NULL; |
| 931 | } |
| 932 | #endif /* OPENSSL_FIPS */ |
| 933 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 934 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 935 | SSL_load_error_strings(); |
| 936 | SSL_library_init(); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 937 | #ifndef OPENSSL_NO_SHA256 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 938 | EVP_add_digest(EVP_sha256()); |
| 939 | #endif /* OPENSSL_NO_SHA256 */ |
| 940 | /* TODO: if /dev/urandom is available, PRNG is seeded |
| 941 | * automatically. If this is not the case, random data should |
| 942 | * be added here. */ |
| 943 | |
| 944 | #ifdef PKCS12_FUNCS |
| 945 | #ifndef OPENSSL_NO_RC2 |
| 946 | /* |
| 947 | * 40-bit RC2 is commonly used in PKCS#12 files, so enable it. |
| 948 | * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8 |
| 949 | * versions, but it looks like OpenSSL 1.0.0 does not do that |
| 950 | * anymore. |
| 951 | */ |
| 952 | EVP_add_cipher(EVP_rc2_40_cbc()); |
| 953 | #endif /* OPENSSL_NO_RC2 */ |
| 954 | PKCS12_PBE_add(); |
| 955 | #endif /* PKCS12_FUNCS */ |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 956 | #endif /* < 1.1.0 */ |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 957 | } else { |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 958 | context = tls_context_new(conf); |
| 959 | if (context == NULL) |
| 960 | return NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 961 | } |
| 962 | tls_openssl_ref_count++; |
| 963 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 964 | data = os_zalloc(sizeof(*data)); |
| 965 | if (data) |
| 966 | ssl = SSL_CTX_new(SSLv23_method()); |
| 967 | else |
| 968 | ssl = NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 969 | if (ssl == NULL) { |
| 970 | tls_openssl_ref_count--; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 971 | if (context != tls_global) |
| 972 | os_free(context); |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 973 | if (tls_openssl_ref_count == 0) { |
| 974 | os_free(tls_global); |
| 975 | tls_global = NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 976 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 977 | os_free(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 978 | return NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 979 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 980 | data->ssl = ssl; |
| 981 | if (conf) |
| 982 | data->tls_session_lifetime = conf->tls_session_lifetime; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 983 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 984 | SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2); |
| 985 | SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3); |
| 986 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 987 | #ifdef SSL_MODE_NO_AUTO_CHAIN |
| 988 | /* Number of deployed use cases assume the default OpenSSL behavior of |
| 989 | * auto chaining the local certificate is in use. BoringSSL removed this |
| 990 | * functionality by default, so we need to restore it here to avoid |
| 991 | * breaking existing use cases. */ |
| 992 | SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN); |
| 993 | #endif /* SSL_MODE_NO_AUTO_CHAIN */ |
| 994 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 995 | SSL_CTX_set_info_callback(ssl, ssl_info_cb); |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 996 | SSL_CTX_set_app_data(ssl, context); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 997 | if (data->tls_session_lifetime > 0) { |
| 998 | SSL_CTX_set_quiet_shutdown(ssl, 1); |
| 999 | /* |
| 1000 | * Set default context here. In practice, this will be replaced |
| 1001 | * by the per-EAP method context in tls_connection_set_verify(). |
| 1002 | */ |
| 1003 | SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7); |
| 1004 | SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER); |
| 1005 | SSL_CTX_set_timeout(ssl, data->tls_session_lifetime); |
| 1006 | SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb); |
| 1007 | } else { |
| 1008 | SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF); |
| 1009 | } |
| 1010 | |
| 1011 | if (tls_ex_idx_session < 0) { |
| 1012 | tls_ex_idx_session = SSL_SESSION_get_ex_new_index( |
| 1013 | 0, NULL, NULL, NULL, NULL); |
| 1014 | if (tls_ex_idx_session < 0) { |
| 1015 | tls_deinit(data); |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1019 | |
| 1020 | #ifndef OPENSSL_NO_ENGINE |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1021 | wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine"); |
| 1022 | ERR_load_ENGINE_strings(); |
| 1023 | ENGINE_load_dynamic(); |
| 1024 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1025 | if (conf && |
| 1026 | (conf->opensc_engine_path || conf->pkcs11_engine_path || |
| 1027 | conf->pkcs11_module_path)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1028 | if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) || |
| 1029 | tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path, |
| 1030 | conf->pkcs11_module_path)) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1031 | tls_deinit(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1032 | return NULL; |
| 1033 | } |
| 1034 | } |
| 1035 | #endif /* OPENSSL_NO_ENGINE */ |
| 1036 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1037 | if (conf && conf->openssl_ciphers) |
| 1038 | ciphers = conf->openssl_ciphers; |
| 1039 | else |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 1040 | ciphers = TLS_DEFAULT_CIPHERS; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1041 | if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) { |
| 1042 | wpa_printf(MSG_ERROR, |
| 1043 | "OpenSSL: Failed to set cipher string '%s'", |
| 1044 | ciphers); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1045 | tls_deinit(data); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1046 | return NULL; |
| 1047 | } |
| 1048 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1049 | return data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | void tls_deinit(void *ssl_ctx) |
| 1054 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1055 | struct tls_data *data = ssl_ctx; |
| 1056 | SSL_CTX *ssl = data->ssl; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1057 | struct tls_context *context = SSL_CTX_get_app_data(ssl); |
| 1058 | if (context != tls_global) |
| 1059 | os_free(context); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1060 | if (data->tls_session_lifetime > 0) |
| 1061 | SSL_CTX_flush_sessions(ssl, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1062 | SSL_CTX_free(ssl); |
| 1063 | |
| 1064 | tls_openssl_ref_count--; |
| 1065 | if (tls_openssl_ref_count == 0) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1066 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1067 | #ifndef OPENSSL_NO_ENGINE |
| 1068 | ENGINE_cleanup(); |
| 1069 | #endif /* OPENSSL_NO_ENGINE */ |
| 1070 | CRYPTO_cleanup_all_ex_data(); |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 1071 | ERR_remove_thread_state(NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1072 | ERR_free_strings(); |
| 1073 | EVP_cleanup(); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1074 | #endif /* < 1.1.0 */ |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1075 | os_free(tls_global->ocsp_stapling_response); |
| 1076 | tls_global->ocsp_stapling_response = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1077 | os_free(tls_global); |
| 1078 | tls_global = NULL; |
| 1079 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1080 | |
| 1081 | os_free(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1085 | #ifndef OPENSSL_NO_ENGINE |
| 1086 | |
| 1087 | /* Cryptoki return values */ |
| 1088 | #define CKR_PIN_INCORRECT 0x000000a0 |
| 1089 | #define CKR_PIN_INVALID 0x000000a1 |
| 1090 | #define CKR_PIN_LEN_RANGE 0x000000a2 |
| 1091 | |
| 1092 | /* libp11 */ |
| 1093 | #define ERR_LIB_PKCS11 ERR_LIB_USER |
| 1094 | |
| 1095 | static int tls_is_pin_error(unsigned int err) |
| 1096 | { |
| 1097 | return ERR_GET_LIB(err) == ERR_LIB_PKCS11 && |
| 1098 | (ERR_GET_REASON(err) == CKR_PIN_INCORRECT || |
| 1099 | ERR_GET_REASON(err) == CKR_PIN_INVALID || |
| 1100 | ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE); |
| 1101 | } |
| 1102 | |
| 1103 | #endif /* OPENSSL_NO_ENGINE */ |
| 1104 | |
| 1105 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1106 | #ifdef ANDROID |
| 1107 | /* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */ |
| 1108 | EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id); |
| 1109 | #endif /* ANDROID */ |
| 1110 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1111 | static int tls_engine_init(struct tls_connection *conn, const char *engine_id, |
| 1112 | const char *pin, const char *key_id, |
| 1113 | const char *cert_id, const char *ca_cert_id) |
| 1114 | { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1115 | #if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL) |
| 1116 | #if !defined(OPENSSL_NO_ENGINE) |
| 1117 | #error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL." |
| 1118 | #endif |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1119 | if (!key_id) |
| 1120 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1121 | conn->engine = NULL; |
| 1122 | conn->private_key = EVP_PKEY_from_keystore(key_id); |
| 1123 | if (!conn->private_key) { |
| 1124 | wpa_printf(MSG_ERROR, |
| 1125 | "ENGINE: cannot load private key with id '%s' [%s]", |
| 1126 | key_id, |
| 1127 | ERR_error_string(ERR_get_error(), NULL)); |
| 1128 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 1129 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1130 | #endif /* ANDROID && OPENSSL_IS_BORINGSSL */ |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1131 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1132 | #ifndef OPENSSL_NO_ENGINE |
| 1133 | int ret = -1; |
| 1134 | if (engine_id == NULL) { |
| 1135 | wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set"); |
| 1136 | return -1; |
| 1137 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1138 | |
| 1139 | ERR_clear_error(); |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 1140 | #ifdef ANDROID |
| 1141 | ENGINE_load_dynamic(); |
| 1142 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1143 | conn->engine = ENGINE_by_id(engine_id); |
| 1144 | if (!conn->engine) { |
| 1145 | wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]", |
| 1146 | engine_id, ERR_error_string(ERR_get_error(), NULL)); |
| 1147 | goto err; |
| 1148 | } |
| 1149 | if (ENGINE_init(conn->engine) != 1) { |
| 1150 | wpa_printf(MSG_ERROR, "ENGINE: engine init failed " |
| 1151 | "(engine: %s) [%s]", engine_id, |
| 1152 | ERR_error_string(ERR_get_error(), NULL)); |
| 1153 | goto err; |
| 1154 | } |
| 1155 | wpa_printf(MSG_DEBUG, "ENGINE: engine initialized"); |
| 1156 | |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 1157 | #ifndef ANDROID |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1158 | if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1159 | wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]", |
| 1160 | ERR_error_string(ERR_get_error(), NULL)); |
| 1161 | goto err; |
| 1162 | } |
Kenny Root | db3c5a4 | 2012-03-20 17:00:47 -0700 | [diff] [blame] | 1163 | #endif |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1164 | if (key_id) { |
| 1165 | /* |
| 1166 | * Ensure that the ENGINE does not attempt to use the OpenSSL |
| 1167 | * UI system to obtain a PIN, if we didn't provide one. |
| 1168 | */ |
| 1169 | struct { |
| 1170 | const void *password; |
| 1171 | const char *prompt_info; |
| 1172 | } key_cb = { "", NULL }; |
| 1173 | |
| 1174 | /* load private key first in-case PIN is required for cert */ |
| 1175 | conn->private_key = ENGINE_load_private_key(conn->engine, |
| 1176 | key_id, NULL, |
| 1177 | &key_cb); |
| 1178 | if (!conn->private_key) { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1179 | unsigned long err = ERR_get_error(); |
| 1180 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1181 | wpa_printf(MSG_ERROR, |
| 1182 | "ENGINE: cannot load private key with id '%s' [%s]", |
| 1183 | key_id, |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 1184 | ERR_error_string(err, NULL)); |
| 1185 | if (tls_is_pin_error(err)) |
| 1186 | ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN; |
| 1187 | else |
| 1188 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1189 | goto err; |
| 1190 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /* handle a certificate and/or CA certificate */ |
| 1194 | if (cert_id || ca_cert_id) { |
| 1195 | const char *cmd_name = "LOAD_CERT_CTRL"; |
| 1196 | |
| 1197 | /* test if the engine supports a LOAD_CERT_CTRL */ |
| 1198 | if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME, |
| 1199 | 0, (void *)cmd_name, NULL)) { |
| 1200 | wpa_printf(MSG_ERROR, "ENGINE: engine does not support" |
| 1201 | " loading certificates"); |
| 1202 | ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 1203 | goto err; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | return 0; |
| 1208 | |
| 1209 | err: |
| 1210 | if (conn->engine) { |
| 1211 | ENGINE_free(conn->engine); |
| 1212 | conn->engine = NULL; |
| 1213 | } |
| 1214 | |
| 1215 | if (conn->private_key) { |
| 1216 | EVP_PKEY_free(conn->private_key); |
| 1217 | conn->private_key = NULL; |
| 1218 | } |
| 1219 | |
| 1220 | return ret; |
| 1221 | #else /* OPENSSL_NO_ENGINE */ |
| 1222 | return 0; |
| 1223 | #endif /* OPENSSL_NO_ENGINE */ |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | static void tls_engine_deinit(struct tls_connection *conn) |
| 1228 | { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1229 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1230 | wpa_printf(MSG_DEBUG, "ENGINE: engine deinit"); |
| 1231 | if (conn->private_key) { |
| 1232 | EVP_PKEY_free(conn->private_key); |
| 1233 | conn->private_key = NULL; |
| 1234 | } |
| 1235 | if (conn->engine) { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 1236 | #if !defined(OPENSSL_IS_BORINGSSL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1237 | ENGINE_finish(conn->engine); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1238 | #endif /* !OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1239 | conn->engine = NULL; |
| 1240 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1241 | #endif /* ANDROID || !OPENSSL_NO_ENGINE */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | int tls_get_errors(void *ssl_ctx) |
| 1246 | { |
| 1247 | int count = 0; |
| 1248 | unsigned long err; |
| 1249 | |
| 1250 | while ((err = ERR_get_error())) { |
| 1251 | wpa_printf(MSG_INFO, "TLS - SSL error: %s", |
| 1252 | ERR_error_string(err, NULL)); |
| 1253 | count++; |
| 1254 | } |
| 1255 | |
| 1256 | return count; |
| 1257 | } |
| 1258 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1259 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1260 | static const char * openssl_content_type(int content_type) |
| 1261 | { |
| 1262 | switch (content_type) { |
| 1263 | case 20: |
| 1264 | return "change cipher spec"; |
| 1265 | case 21: |
| 1266 | return "alert"; |
| 1267 | case 22: |
| 1268 | return "handshake"; |
| 1269 | case 23: |
| 1270 | return "application data"; |
| 1271 | case 24: |
| 1272 | return "heartbeat"; |
| 1273 | case 256: |
| 1274 | return "TLS header info"; /* pseudo content type */ |
| 1275 | default: |
| 1276 | return "?"; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | |
| 1281 | static const char * openssl_handshake_type(int content_type, const u8 *buf, |
| 1282 | size_t len) |
| 1283 | { |
| 1284 | if (content_type != 22 || !buf || len == 0) |
| 1285 | return ""; |
| 1286 | switch (buf[0]) { |
| 1287 | case 0: |
| 1288 | return "hello request"; |
| 1289 | case 1: |
| 1290 | return "client hello"; |
| 1291 | case 2: |
| 1292 | return "server hello"; |
| 1293 | case 4: |
| 1294 | return "new session ticket"; |
| 1295 | case 11: |
| 1296 | return "certificate"; |
| 1297 | case 12: |
| 1298 | return "server key exchange"; |
| 1299 | case 13: |
| 1300 | return "certificate request"; |
| 1301 | case 14: |
| 1302 | return "server hello done"; |
| 1303 | case 15: |
| 1304 | return "certificate verify"; |
| 1305 | case 16: |
| 1306 | return "client key exchange"; |
| 1307 | case 20: |
| 1308 | return "finished"; |
| 1309 | case 21: |
| 1310 | return "certificate url"; |
| 1311 | case 22: |
| 1312 | return "certificate status"; |
| 1313 | default: |
| 1314 | return "?"; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 1319 | #ifdef CONFIG_SUITEB |
| 1320 | |
| 1321 | static void check_server_hello(struct tls_connection *conn, |
| 1322 | const u8 *pos, const u8 *end) |
| 1323 | { |
| 1324 | size_t payload_len, id_len; |
| 1325 | |
| 1326 | /* |
| 1327 | * Parse ServerHello to get the selected cipher suite since OpenSSL does |
| 1328 | * not make it cleanly available during handshake and we need to know |
| 1329 | * whether DHE was selected. |
| 1330 | */ |
| 1331 | |
| 1332 | if (end - pos < 3) |
| 1333 | return; |
| 1334 | payload_len = WPA_GET_BE24(pos); |
| 1335 | pos += 3; |
| 1336 | |
| 1337 | if ((size_t) (end - pos) < payload_len) |
| 1338 | return; |
| 1339 | end = pos + payload_len; |
| 1340 | |
| 1341 | /* Skip Version and Random */ |
| 1342 | if (end - pos < 2 + SSL3_RANDOM_SIZE) |
| 1343 | return; |
| 1344 | pos += 2 + SSL3_RANDOM_SIZE; |
| 1345 | |
| 1346 | /* Skip Session ID */ |
| 1347 | if (end - pos < 1) |
| 1348 | return; |
| 1349 | id_len = *pos++; |
| 1350 | if ((size_t) (end - pos) < id_len) |
| 1351 | return; |
| 1352 | pos += id_len; |
| 1353 | |
| 1354 | if (end - pos < 2) |
| 1355 | return; |
| 1356 | conn->cipher_suite = WPA_GET_BE16(pos); |
| 1357 | wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x", |
| 1358 | conn->cipher_suite); |
| 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn, |
| 1363 | const u8 *pos, const u8 *end) |
| 1364 | { |
| 1365 | size_t payload_len; |
| 1366 | u16 dh_len; |
| 1367 | BIGNUM *p; |
| 1368 | int bits; |
| 1369 | |
| 1370 | if (!(conn->flags & TLS_CONN_SUITEB)) |
| 1371 | return; |
| 1372 | |
| 1373 | /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */ |
| 1374 | if (conn->cipher_suite != 0x9f) |
| 1375 | return; |
| 1376 | |
| 1377 | if (end - pos < 3) |
| 1378 | return; |
| 1379 | payload_len = WPA_GET_BE24(pos); |
| 1380 | pos += 3; |
| 1381 | |
| 1382 | if ((size_t) (end - pos) < payload_len) |
| 1383 | return; |
| 1384 | end = pos + payload_len; |
| 1385 | |
| 1386 | if (end - pos < 2) |
| 1387 | return; |
| 1388 | dh_len = WPA_GET_BE16(pos); |
| 1389 | pos += 2; |
| 1390 | |
| 1391 | if ((size_t) (end - pos) < dh_len) |
| 1392 | return; |
| 1393 | p = BN_bin2bn(pos, dh_len, NULL); |
| 1394 | if (!p) |
| 1395 | return; |
| 1396 | |
| 1397 | bits = BN_num_bits(p); |
| 1398 | BN_free(p); |
| 1399 | |
| 1400 | conn->server_dh_prime_len = bits; |
| 1401 | wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits", |
| 1402 | conn->server_dh_prime_len); |
| 1403 | } |
| 1404 | |
| 1405 | #endif /* CONFIG_SUITEB */ |
| 1406 | |
| 1407 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1408 | static void tls_msg_cb(int write_p, int version, int content_type, |
| 1409 | const void *buf, size_t len, SSL *ssl, void *arg) |
| 1410 | { |
| 1411 | struct tls_connection *conn = arg; |
| 1412 | const u8 *pos = buf; |
| 1413 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1414 | if (write_p == 2) { |
| 1415 | wpa_printf(MSG_DEBUG, |
| 1416 | "OpenSSL: session ver=0x%x content_type=%d", |
| 1417 | version, content_type); |
| 1418 | wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)", |
| 1423 | write_p ? "TX" : "RX", version, content_type, |
| 1424 | openssl_content_type(content_type), |
| 1425 | openssl_handshake_type(content_type, buf, len)); |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1426 | wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len); |
| 1427 | if (content_type == 24 && len >= 3 && pos[0] == 1) { |
| 1428 | size_t payload_len = WPA_GET_BE16(pos + 1); |
| 1429 | if (payload_len + 3 > len) { |
| 1430 | wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected"); |
| 1431 | conn->invalid_hb_used = 1; |
| 1432 | } |
| 1433 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 1434 | |
| 1435 | #ifdef CONFIG_SUITEB |
| 1436 | /* |
| 1437 | * Need to parse these handshake messages to be able to check DH prime |
| 1438 | * length since OpenSSL does not expose the new cipher suite and DH |
| 1439 | * parameters during handshake (e.g., for cert_cb() callback). |
| 1440 | */ |
| 1441 | if (content_type == 22 && pos && len > 0 && pos[0] == 2) |
| 1442 | check_server_hello(conn, pos + 1, pos + len); |
| 1443 | if (content_type == 22 && pos && len > 0 && pos[0] == 12) |
| 1444 | check_server_key_exchange(ssl, conn, pos + 1, pos + len); |
| 1445 | #endif /* CONFIG_SUITEB */ |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1449 | struct tls_connection * tls_connection_init(void *ssl_ctx) |
| 1450 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1451 | struct tls_data *data = ssl_ctx; |
| 1452 | SSL_CTX *ssl = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1453 | struct tls_connection *conn; |
| 1454 | long options; |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1455 | struct tls_context *context = SSL_CTX_get_app_data(ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1456 | |
| 1457 | conn = os_zalloc(sizeof(*conn)); |
| 1458 | if (conn == NULL) |
| 1459 | return NULL; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1460 | conn->ssl_ctx = ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1461 | conn->ssl = SSL_new(ssl); |
| 1462 | if (conn->ssl == NULL) { |
| 1463 | tls_show_errors(MSG_INFO, __func__, |
| 1464 | "Failed to initialize new SSL connection"); |
| 1465 | os_free(conn); |
| 1466 | return NULL; |
| 1467 | } |
| 1468 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1469 | conn->context = context; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1470 | SSL_set_app_data(conn->ssl, conn); |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 1471 | SSL_set_msg_callback(conn->ssl, tls_msg_cb); |
| 1472 | SSL_set_msg_callback_arg(conn->ssl, conn); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1473 | options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | |
| 1474 | SSL_OP_SINGLE_DH_USE; |
| 1475 | #ifdef SSL_OP_NO_COMPRESSION |
| 1476 | options |= SSL_OP_NO_COMPRESSION; |
| 1477 | #endif /* SSL_OP_NO_COMPRESSION */ |
| 1478 | SSL_set_options(conn->ssl, options); |
| 1479 | |
| 1480 | conn->ssl_in = BIO_new(BIO_s_mem()); |
| 1481 | if (!conn->ssl_in) { |
| 1482 | tls_show_errors(MSG_INFO, __func__, |
| 1483 | "Failed to create a new BIO for ssl_in"); |
| 1484 | SSL_free(conn->ssl); |
| 1485 | os_free(conn); |
| 1486 | return NULL; |
| 1487 | } |
| 1488 | |
| 1489 | conn->ssl_out = BIO_new(BIO_s_mem()); |
| 1490 | if (!conn->ssl_out) { |
| 1491 | tls_show_errors(MSG_INFO, __func__, |
| 1492 | "Failed to create a new BIO for ssl_out"); |
| 1493 | SSL_free(conn->ssl); |
| 1494 | BIO_free(conn->ssl_in); |
| 1495 | os_free(conn); |
| 1496 | return NULL; |
| 1497 | } |
| 1498 | |
| 1499 | SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out); |
| 1500 | |
| 1501 | return conn; |
| 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn) |
| 1506 | { |
| 1507 | if (conn == NULL) |
| 1508 | return; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1509 | if (conn->success_data) { |
| 1510 | /* |
| 1511 | * Make sure ssl_clear_bad_session() does not remove this |
| 1512 | * session. |
| 1513 | */ |
| 1514 | SSL_set_quiet_shutdown(conn->ssl, 1); |
| 1515 | SSL_shutdown(conn->ssl); |
| 1516 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1517 | SSL_free(conn->ssl); |
| 1518 | tls_engine_deinit(conn); |
| 1519 | os_free(conn->subject_match); |
| 1520 | os_free(conn->altsubject_match); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1521 | os_free(conn->suffix_match); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1522 | os_free(conn->domain_match); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1523 | os_free(conn->session_ticket); |
| 1524 | os_free(conn); |
| 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | int tls_connection_established(void *ssl_ctx, struct tls_connection *conn) |
| 1529 | { |
| 1530 | return conn ? SSL_is_init_finished(conn->ssl) : 0; |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn) |
| 1535 | { |
| 1536 | if (conn == NULL) |
| 1537 | return -1; |
| 1538 | |
| 1539 | /* Shutdown previous TLS connection without notifying the peer |
| 1540 | * because the connection was already terminated in practice |
| 1541 | * and "close notify" shutdown alert would confuse AS. */ |
| 1542 | SSL_set_quiet_shutdown(conn->ssl, 1); |
| 1543 | SSL_shutdown(conn->ssl); |
Jouni Malinen | f291c68 | 2015-08-17 22:50:41 +0300 | [diff] [blame] | 1544 | return SSL_clear(conn->ssl) == 1 ? 0 : -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | static int tls_match_altsubject_component(X509 *cert, int type, |
| 1549 | const char *value, size_t len) |
| 1550 | { |
| 1551 | GENERAL_NAME *gen; |
| 1552 | void *ext; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 1553 | int found = 0; |
| 1554 | stack_index_t i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1555 | |
| 1556 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 1557 | |
| 1558 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { |
| 1559 | gen = sk_GENERAL_NAME_value(ext, i); |
| 1560 | if (gen->type != type) |
| 1561 | continue; |
| 1562 | if (os_strlen((char *) gen->d.ia5->data) == len && |
| 1563 | os_memcmp(value, gen->d.ia5->data, len) == 0) |
| 1564 | found++; |
| 1565 | } |
| 1566 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1567 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
| 1568 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1569 | return found; |
| 1570 | } |
| 1571 | |
| 1572 | |
| 1573 | static int tls_match_altsubject(X509 *cert, const char *match) |
| 1574 | { |
| 1575 | int type; |
| 1576 | const char *pos, *end; |
| 1577 | size_t len; |
| 1578 | |
| 1579 | pos = match; |
| 1580 | do { |
| 1581 | if (os_strncmp(pos, "EMAIL:", 6) == 0) { |
| 1582 | type = GEN_EMAIL; |
| 1583 | pos += 6; |
| 1584 | } else if (os_strncmp(pos, "DNS:", 4) == 0) { |
| 1585 | type = GEN_DNS; |
| 1586 | pos += 4; |
| 1587 | } else if (os_strncmp(pos, "URI:", 4) == 0) { |
| 1588 | type = GEN_URI; |
| 1589 | pos += 4; |
| 1590 | } else { |
| 1591 | wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName " |
| 1592 | "match '%s'", pos); |
| 1593 | return 0; |
| 1594 | } |
| 1595 | end = os_strchr(pos, ';'); |
| 1596 | while (end) { |
| 1597 | if (os_strncmp(end + 1, "EMAIL:", 6) == 0 || |
| 1598 | os_strncmp(end + 1, "DNS:", 4) == 0 || |
| 1599 | os_strncmp(end + 1, "URI:", 4) == 0) |
| 1600 | break; |
| 1601 | end = os_strchr(end + 1, ';'); |
| 1602 | } |
| 1603 | if (end) |
| 1604 | len = end - pos; |
| 1605 | else |
| 1606 | len = os_strlen(pos); |
| 1607 | if (tls_match_altsubject_component(cert, type, pos, len) > 0) |
| 1608 | return 1; |
| 1609 | pos = end + 1; |
| 1610 | } while (end); |
| 1611 | |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1616 | #ifndef CONFIG_NATIVE_WINDOWS |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1617 | static int domain_suffix_match(const u8 *val, size_t len, const char *match, |
| 1618 | int full) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1619 | { |
| 1620 | size_t i, match_len; |
| 1621 | |
| 1622 | /* Check for embedded nuls that could mess up suffix matching */ |
| 1623 | for (i = 0; i < len; i++) { |
| 1624 | if (val[i] == '\0') { |
| 1625 | wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject"); |
| 1626 | return 0; |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | match_len = os_strlen(match); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1631 | if (match_len > len || (full && match_len != len)) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1632 | return 0; |
| 1633 | |
| 1634 | if (os_strncasecmp((const char *) val + len - match_len, match, |
| 1635 | match_len) != 0) |
| 1636 | return 0; /* no match */ |
| 1637 | |
| 1638 | if (match_len == len) |
| 1639 | return 1; /* exact match */ |
| 1640 | |
| 1641 | if (val[len - match_len - 1] == '.') |
| 1642 | return 1; /* full label match completes suffix match */ |
| 1643 | |
| 1644 | wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match"); |
| 1645 | return 0; |
| 1646 | } |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1647 | #endif /* CONFIG_NATIVE_WINDOWS */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1648 | |
| 1649 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1650 | static int tls_match_suffix(X509 *cert, const char *match, int full) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1651 | { |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1652 | #ifdef CONFIG_NATIVE_WINDOWS |
| 1653 | /* wincrypt.h has conflicting X509_NAME definition */ |
| 1654 | return -1; |
| 1655 | #else /* CONFIG_NATIVE_WINDOWS */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1656 | GENERAL_NAME *gen; |
| 1657 | void *ext; |
| 1658 | int i; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 1659 | stack_index_t j; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1660 | int dns_name = 0; |
| 1661 | X509_NAME *name; |
| 1662 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1663 | wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s", |
| 1664 | full ? "": "suffix ", match); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1665 | |
| 1666 | ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 1667 | |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 1668 | for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) { |
| 1669 | gen = sk_GENERAL_NAME_value(ext, j); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1670 | if (gen->type != GEN_DNS) |
| 1671 | continue; |
| 1672 | dns_name++; |
| 1673 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName", |
| 1674 | gen->d.dNSName->data, |
| 1675 | gen->d.dNSName->length); |
| 1676 | if (domain_suffix_match(gen->d.dNSName->data, |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1677 | gen->d.dNSName->length, match, full) == |
| 1678 | 1) { |
| 1679 | wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found", |
| 1680 | full ? "Match" : "Suffix match"); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1681 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1682 | return 1; |
| 1683 | } |
| 1684 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1685 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1686 | |
| 1687 | if (dns_name) { |
| 1688 | wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched"); |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | name = X509_get_subject_name(cert); |
| 1693 | i = -1; |
| 1694 | for (;;) { |
| 1695 | X509_NAME_ENTRY *e; |
| 1696 | ASN1_STRING *cn; |
| 1697 | |
| 1698 | i = X509_NAME_get_index_by_NID(name, NID_commonName, i); |
| 1699 | if (i == -1) |
| 1700 | break; |
| 1701 | e = X509_NAME_get_entry(name, i); |
| 1702 | if (e == NULL) |
| 1703 | continue; |
| 1704 | cn = X509_NAME_ENTRY_get_data(e); |
| 1705 | if (cn == NULL) |
| 1706 | continue; |
| 1707 | wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName", |
| 1708 | cn->data, cn->length); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1709 | if (domain_suffix_match(cn->data, cn->length, match, full) == 1) |
| 1710 | { |
| 1711 | wpa_printf(MSG_DEBUG, "TLS: %s in commonName found", |
| 1712 | full ? "Match" : "Suffix match"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1713 | return 1; |
| 1714 | } |
| 1715 | } |
| 1716 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1717 | wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found", |
| 1718 | full ? "": "suffix "); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1719 | return 0; |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1720 | #endif /* CONFIG_NATIVE_WINDOWS */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1724 | static enum tls_fail_reason openssl_tls_fail_reason(int err) |
| 1725 | { |
| 1726 | switch (err) { |
| 1727 | case X509_V_ERR_CERT_REVOKED: |
| 1728 | return TLS_FAIL_REVOKED; |
| 1729 | case X509_V_ERR_CERT_NOT_YET_VALID: |
| 1730 | case X509_V_ERR_CRL_NOT_YET_VALID: |
| 1731 | return TLS_FAIL_NOT_YET_VALID; |
| 1732 | case X509_V_ERR_CERT_HAS_EXPIRED: |
| 1733 | case X509_V_ERR_CRL_HAS_EXPIRED: |
| 1734 | return TLS_FAIL_EXPIRED; |
| 1735 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
| 1736 | case X509_V_ERR_UNABLE_TO_GET_CRL: |
| 1737 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: |
| 1738 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 1739 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: |
| 1740 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 1741 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: |
| 1742 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: |
| 1743 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 1744 | case X509_V_ERR_INVALID_CA: |
| 1745 | return TLS_FAIL_UNTRUSTED; |
| 1746 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: |
| 1747 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: |
| 1748 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: |
| 1749 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
| 1750 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
| 1751 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: |
| 1752 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: |
| 1753 | case X509_V_ERR_CERT_UNTRUSTED: |
| 1754 | case X509_V_ERR_CERT_REJECTED: |
| 1755 | return TLS_FAIL_BAD_CERTIFICATE; |
| 1756 | default: |
| 1757 | return TLS_FAIL_UNSPECIFIED; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | static struct wpabuf * get_x509_cert(X509 *cert) |
| 1763 | { |
| 1764 | struct wpabuf *buf; |
| 1765 | u8 *tmp; |
| 1766 | |
| 1767 | int cert_len = i2d_X509(cert, NULL); |
| 1768 | if (cert_len <= 0) |
| 1769 | return NULL; |
| 1770 | |
| 1771 | buf = wpabuf_alloc(cert_len); |
| 1772 | if (buf == NULL) |
| 1773 | return NULL; |
| 1774 | |
| 1775 | tmp = wpabuf_put(buf, cert_len); |
| 1776 | i2d_X509(cert, &tmp); |
| 1777 | return buf; |
| 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | static void openssl_tls_fail_event(struct tls_connection *conn, |
| 1782 | X509 *err_cert, int err, int depth, |
| 1783 | const char *subject, const char *err_str, |
| 1784 | enum tls_fail_reason reason) |
| 1785 | { |
| 1786 | union tls_event_data ev; |
| 1787 | struct wpabuf *cert = NULL; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1788 | struct tls_context *context = conn->context; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1789 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1790 | if (context->event_cb == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1791 | return; |
| 1792 | |
| 1793 | cert = get_x509_cert(err_cert); |
| 1794 | os_memset(&ev, 0, sizeof(ev)); |
| 1795 | ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ? |
| 1796 | reason : openssl_tls_fail_reason(err); |
| 1797 | ev.cert_fail.depth = depth; |
| 1798 | ev.cert_fail.subject = subject; |
| 1799 | ev.cert_fail.reason_txt = err_str; |
| 1800 | ev.cert_fail.cert = cert; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1801 | context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1802 | wpabuf_free(cert); |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | static void openssl_tls_cert_event(struct tls_connection *conn, |
| 1807 | X509 *err_cert, int depth, |
| 1808 | const char *subject) |
| 1809 | { |
| 1810 | struct wpabuf *cert = NULL; |
| 1811 | union tls_event_data ev; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1812 | struct tls_context *context = conn->context; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1813 | char *altsubject[TLS_MAX_ALT_SUBJECT]; |
| 1814 | int alt, num_altsubject = 0; |
| 1815 | GENERAL_NAME *gen; |
| 1816 | void *ext; |
| 1817 | stack_index_t i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1818 | #ifdef CONFIG_SHA256 |
| 1819 | u8 hash[32]; |
| 1820 | #endif /* CONFIG_SHA256 */ |
| 1821 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1822 | if (context->event_cb == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1823 | return; |
| 1824 | |
| 1825 | os_memset(&ev, 0, sizeof(ev)); |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1826 | if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) || |
| 1827 | context->cert_in_cb) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1828 | cert = get_x509_cert(err_cert); |
| 1829 | ev.peer_cert.cert = cert; |
| 1830 | } |
| 1831 | #ifdef CONFIG_SHA256 |
| 1832 | if (cert) { |
| 1833 | const u8 *addr[1]; |
| 1834 | size_t len[1]; |
| 1835 | addr[0] = wpabuf_head(cert); |
| 1836 | len[0] = wpabuf_len(cert); |
| 1837 | if (sha256_vector(1, addr, len, hash) == 0) { |
| 1838 | ev.peer_cert.hash = hash; |
| 1839 | ev.peer_cert.hash_len = sizeof(hash); |
| 1840 | } |
| 1841 | } |
| 1842 | #endif /* CONFIG_SHA256 */ |
| 1843 | ev.peer_cert.depth = depth; |
| 1844 | ev.peer_cert.subject = subject; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1845 | |
| 1846 | ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL); |
| 1847 | for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) { |
| 1848 | char *pos; |
| 1849 | |
| 1850 | if (num_altsubject == TLS_MAX_ALT_SUBJECT) |
| 1851 | break; |
| 1852 | gen = sk_GENERAL_NAME_value(ext, i); |
| 1853 | if (gen->type != GEN_EMAIL && |
| 1854 | gen->type != GEN_DNS && |
| 1855 | gen->type != GEN_URI) |
| 1856 | continue; |
| 1857 | |
| 1858 | pos = os_malloc(10 + gen->d.ia5->length + 1); |
| 1859 | if (pos == NULL) |
| 1860 | break; |
| 1861 | altsubject[num_altsubject++] = pos; |
| 1862 | |
| 1863 | switch (gen->type) { |
| 1864 | case GEN_EMAIL: |
| 1865 | os_memcpy(pos, "EMAIL:", 6); |
| 1866 | pos += 6; |
| 1867 | break; |
| 1868 | case GEN_DNS: |
| 1869 | os_memcpy(pos, "DNS:", 4); |
| 1870 | pos += 4; |
| 1871 | break; |
| 1872 | case GEN_URI: |
| 1873 | os_memcpy(pos, "URI:", 4); |
| 1874 | pos += 4; |
| 1875 | break; |
| 1876 | } |
| 1877 | |
| 1878 | os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length); |
| 1879 | pos += gen->d.ia5->length; |
| 1880 | *pos = '\0'; |
| 1881 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1882 | sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1883 | |
| 1884 | for (alt = 0; alt < num_altsubject; alt++) |
| 1885 | ev.peer_cert.altsubject[alt] = altsubject[alt]; |
| 1886 | ev.peer_cert.num_altsubject = num_altsubject; |
| 1887 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1888 | context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1889 | wpabuf_free(cert); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1890 | for (alt = 0; alt < num_altsubject; alt++) |
| 1891 | os_free(altsubject[alt]); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | |
| 1895 | static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 1896 | { |
| 1897 | char buf[256]; |
| 1898 | X509 *err_cert; |
| 1899 | int err, depth; |
| 1900 | SSL *ssl; |
| 1901 | struct tls_connection *conn; |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1902 | struct tls_context *context; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1903 | char *match, *altmatch, *suffix_match, *domain_match; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1904 | const char *err_str; |
| 1905 | |
| 1906 | err_cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 1907 | if (!err_cert) |
| 1908 | return 0; |
| 1909 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1910 | err = X509_STORE_CTX_get_error(x509_ctx); |
| 1911 | depth = X509_STORE_CTX_get_error_depth(x509_ctx); |
| 1912 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
| 1913 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 1914 | X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf)); |
| 1915 | |
| 1916 | conn = SSL_get_app_data(ssl); |
| 1917 | if (conn == NULL) |
| 1918 | return 0; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1919 | |
| 1920 | if (depth == 0) |
| 1921 | conn->peer_cert = err_cert; |
| 1922 | else if (depth == 1) |
| 1923 | conn->peer_issuer = err_cert; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1924 | else if (depth == 2) |
| 1925 | conn->peer_issuer_issuer = err_cert; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1926 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1927 | context = conn->context; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1928 | match = conn->subject_match; |
| 1929 | altmatch = conn->altsubject_match; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1930 | suffix_match = conn->suffix_match; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1931 | domain_match = conn->domain_match; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1932 | |
| 1933 | if (!preverify_ok && !conn->ca_cert_verify) |
| 1934 | preverify_ok = 1; |
| 1935 | if (!preverify_ok && depth > 0 && conn->server_cert_only) |
| 1936 | preverify_ok = 1; |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 1937 | if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) && |
| 1938 | (err == X509_V_ERR_CERT_HAS_EXPIRED || |
| 1939 | err == X509_V_ERR_CERT_NOT_YET_VALID)) { |
| 1940 | wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity " |
| 1941 | "time mismatch"); |
| 1942 | preverify_ok = 1; |
| 1943 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1944 | |
| 1945 | err_str = X509_verify_cert_error_string(err); |
| 1946 | |
| 1947 | #ifdef CONFIG_SHA256 |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 1948 | /* |
| 1949 | * Do not require preverify_ok so we can explicity allow otherwise |
| 1950 | * invalid pinned server certificates. |
| 1951 | */ |
| 1952 | if (depth == 0 && conn->server_cert_only) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1953 | struct wpabuf *cert; |
| 1954 | cert = get_x509_cert(err_cert); |
| 1955 | if (!cert) { |
| 1956 | wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch " |
| 1957 | "server certificate data"); |
| 1958 | preverify_ok = 0; |
| 1959 | } else { |
| 1960 | u8 hash[32]; |
| 1961 | const u8 *addr[1]; |
| 1962 | size_t len[1]; |
| 1963 | addr[0] = wpabuf_head(cert); |
| 1964 | len[0] = wpabuf_len(cert); |
| 1965 | if (sha256_vector(1, addr, len, hash) < 0 || |
| 1966 | os_memcmp(conn->srv_cert_hash, hash, 32) != 0) { |
| 1967 | err_str = "Server certificate mismatch"; |
| 1968 | err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; |
| 1969 | preverify_ok = 0; |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 1970 | } else if (!preverify_ok) { |
| 1971 | /* |
| 1972 | * Certificate matches pinned certificate, allow |
| 1973 | * regardless of other problems. |
| 1974 | */ |
| 1975 | wpa_printf(MSG_DEBUG, |
| 1976 | "OpenSSL: Ignore validation issues for a pinned server certificate"); |
| 1977 | preverify_ok = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1978 | } |
| 1979 | wpabuf_free(cert); |
| 1980 | } |
| 1981 | } |
| 1982 | #endif /* CONFIG_SHA256 */ |
| 1983 | |
| 1984 | if (!preverify_ok) { |
| 1985 | wpa_printf(MSG_WARNING, "TLS: Certificate verification failed," |
| 1986 | " error %d (%s) depth %d for '%s'", err, err_str, |
| 1987 | depth, buf); |
| 1988 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 1989 | err_str, TLS_FAIL_UNSPECIFIED); |
| 1990 | return preverify_ok; |
| 1991 | } |
| 1992 | |
| 1993 | wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d " |
| 1994 | "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'", |
| 1995 | preverify_ok, err, err_str, |
| 1996 | conn->ca_cert_verify, depth, buf); |
| 1997 | if (depth == 0 && match && os_strstr(buf, match) == NULL) { |
| 1998 | wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not " |
| 1999 | "match with '%s'", buf, match); |
| 2000 | preverify_ok = 0; |
| 2001 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2002 | "Subject mismatch", |
| 2003 | TLS_FAIL_SUBJECT_MISMATCH); |
| 2004 | } else if (depth == 0 && altmatch && |
| 2005 | !tls_match_altsubject(err_cert, altmatch)) { |
| 2006 | wpa_printf(MSG_WARNING, "TLS: altSubjectName match " |
| 2007 | "'%s' not found", altmatch); |
| 2008 | preverify_ok = 0; |
| 2009 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2010 | "AltSubject mismatch", |
| 2011 | TLS_FAIL_ALTSUBJECT_MISMATCH); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2012 | } else if (depth == 0 && suffix_match && |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2013 | !tls_match_suffix(err_cert, suffix_match, 0)) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2014 | wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found", |
| 2015 | suffix_match); |
| 2016 | preverify_ok = 0; |
| 2017 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2018 | "Domain suffix mismatch", |
| 2019 | TLS_FAIL_DOMAIN_SUFFIX_MISMATCH); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2020 | } else if (depth == 0 && domain_match && |
| 2021 | !tls_match_suffix(err_cert, domain_match, 1)) { |
| 2022 | wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found", |
| 2023 | domain_match); |
| 2024 | preverify_ok = 0; |
| 2025 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2026 | "Domain mismatch", |
| 2027 | TLS_FAIL_DOMAIN_MISMATCH); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2028 | } else |
| 2029 | openssl_tls_cert_event(conn, err_cert, depth, buf); |
| 2030 | |
| 2031 | if (conn->cert_probe && preverify_ok && depth == 0) { |
| 2032 | wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate " |
| 2033 | "on probe-only run"); |
| 2034 | preverify_ok = 0; |
| 2035 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2036 | "Server certificate chain probe", |
| 2037 | TLS_FAIL_SERVER_CHAIN_PROBE); |
| 2038 | } |
| 2039 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 2040 | #ifdef CONFIG_SUITEB |
| 2041 | if (conn->flags & TLS_CONN_SUITEB) { |
| 2042 | EVP_PKEY *pk; |
| 2043 | RSA *rsa; |
| 2044 | int len = -1; |
| 2045 | |
| 2046 | pk = X509_get_pubkey(err_cert); |
| 2047 | if (pk) { |
| 2048 | rsa = EVP_PKEY_get1_RSA(pk); |
| 2049 | if (rsa) { |
| 2050 | len = RSA_bits(rsa); |
| 2051 | RSA_free(rsa); |
| 2052 | } |
| 2053 | EVP_PKEY_free(pk); |
| 2054 | } |
| 2055 | |
| 2056 | if (len >= 0) { |
| 2057 | wpa_printf(MSG_DEBUG, |
| 2058 | "OpenSSL: RSA modulus size: %d bits", len); |
| 2059 | if (len < 3072) { |
| 2060 | preverify_ok = 0; |
| 2061 | openssl_tls_fail_event( |
| 2062 | conn, err_cert, err, |
| 2063 | depth, buf, |
| 2064 | "Insufficient RSA modulus size", |
| 2065 | TLS_FAIL_INSUFFICIENT_KEY_LEN); |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | #endif /* CONFIG_SUITEB */ |
| 2070 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2071 | #ifdef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2072 | if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) && |
| 2073 | preverify_ok) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2074 | enum ocsp_result res; |
| 2075 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2076 | res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert, |
| 2077 | conn->peer_issuer, |
| 2078 | conn->peer_issuer_issuer); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2079 | if (res == OCSP_REVOKED) { |
| 2080 | preverify_ok = 0; |
| 2081 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2082 | "certificate revoked", |
| 2083 | TLS_FAIL_REVOKED); |
| 2084 | if (err == X509_V_OK) |
| 2085 | X509_STORE_CTX_set_error( |
| 2086 | x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 2087 | } else if (res != OCSP_GOOD && |
| 2088 | (conn->flags & TLS_CONN_REQUIRE_OCSP)) { |
| 2089 | preverify_ok = 0; |
| 2090 | openssl_tls_fail_event(conn, err_cert, err, depth, buf, |
| 2091 | "bad certificate status response", |
| 2092 | TLS_FAIL_UNSPECIFIED); |
| 2093 | } |
| 2094 | } |
| 2095 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 2096 | |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 2097 | if (depth == 0 && preverify_ok && context->event_cb != NULL) |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 2098 | context->event_cb(context->cb_ctx, |
| 2099 | TLS_CERT_CHAIN_SUCCESS, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2100 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2101 | return preverify_ok; |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | #ifndef OPENSSL_NO_STDIO |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2106 | 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] | 2107 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2108 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2109 | X509_LOOKUP *lookup; |
| 2110 | int ret = 0; |
| 2111 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2112 | lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx), |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2113 | X509_LOOKUP_file()); |
| 2114 | if (lookup == NULL) { |
| 2115 | tls_show_errors(MSG_WARNING, __func__, |
| 2116 | "Failed add lookup for X509 store"); |
| 2117 | return -1; |
| 2118 | } |
| 2119 | |
| 2120 | if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) { |
| 2121 | unsigned long err = ERR_peek_error(); |
| 2122 | tls_show_errors(MSG_WARNING, __func__, |
| 2123 | "Failed load CA in DER format"); |
| 2124 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && |
| 2125 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 2126 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " |
| 2127 | "cert already in hash table error", |
| 2128 | __func__); |
| 2129 | } else |
| 2130 | ret = -1; |
| 2131 | } |
| 2132 | |
| 2133 | return ret; |
| 2134 | } |
| 2135 | #endif /* OPENSSL_NO_STDIO */ |
| 2136 | |
| 2137 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2138 | static int tls_connection_ca_cert(struct tls_data *data, |
| 2139 | struct tls_connection *conn, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2140 | const char *ca_cert, const u8 *ca_cert_blob, |
| 2141 | size_t ca_cert_blob_len, const char *ca_path) |
| 2142 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2143 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2144 | X509_STORE *store; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2145 | |
| 2146 | /* |
| 2147 | * Remove previously configured trusted CA certificates before adding |
| 2148 | * new ones. |
| 2149 | */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2150 | store = X509_STORE_new(); |
| 2151 | if (store == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2152 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " |
| 2153 | "certificate store", __func__); |
| 2154 | return -1; |
| 2155 | } |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2156 | SSL_CTX_set_cert_store(ssl_ctx, store); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2157 | |
| 2158 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
| 2159 | conn->ca_cert_verify = 1; |
| 2160 | |
| 2161 | if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) { |
| 2162 | wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate " |
| 2163 | "chain"); |
| 2164 | conn->cert_probe = 1; |
| 2165 | conn->ca_cert_verify = 0; |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) { |
| 2170 | #ifdef CONFIG_SHA256 |
| 2171 | const char *pos = ca_cert + 7; |
| 2172 | if (os_strncmp(pos, "server/sha256/", 14) != 0) { |
| 2173 | wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert " |
| 2174 | "hash value '%s'", ca_cert); |
| 2175 | return -1; |
| 2176 | } |
| 2177 | pos += 14; |
| 2178 | if (os_strlen(pos) != 32 * 2) { |
| 2179 | wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 " |
| 2180 | "hash length in ca_cert '%s'", ca_cert); |
| 2181 | return -1; |
| 2182 | } |
| 2183 | if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) { |
| 2184 | wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash " |
| 2185 | "value in ca_cert '%s'", ca_cert); |
| 2186 | return -1; |
| 2187 | } |
| 2188 | conn->server_cert_only = 1; |
| 2189 | wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server " |
| 2190 | "certificate match"); |
| 2191 | return 0; |
| 2192 | #else /* CONFIG_SHA256 */ |
| 2193 | wpa_printf(MSG_INFO, "No SHA256 included in the build - " |
| 2194 | "cannot validate server certificate hash"); |
| 2195 | return -1; |
| 2196 | #endif /* CONFIG_SHA256 */ |
| 2197 | } |
| 2198 | |
| 2199 | if (ca_cert_blob) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2200 | X509 *cert = d2i_X509(NULL, |
| 2201 | (const unsigned char **) &ca_cert_blob, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2202 | ca_cert_blob_len); |
| 2203 | if (cert == NULL) { |
| 2204 | tls_show_errors(MSG_WARNING, __func__, |
| 2205 | "Failed to parse ca_cert_blob"); |
| 2206 | return -1; |
| 2207 | } |
| 2208 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2209 | if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx), |
| 2210 | cert)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2211 | unsigned long err = ERR_peek_error(); |
| 2212 | tls_show_errors(MSG_WARNING, __func__, |
| 2213 | "Failed to add ca_cert_blob to " |
| 2214 | "certificate store"); |
| 2215 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && |
| 2216 | ERR_GET_REASON(err) == |
| 2217 | X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 2218 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring " |
| 2219 | "cert already in hash table error", |
| 2220 | __func__); |
| 2221 | } else { |
| 2222 | X509_free(cert); |
| 2223 | return -1; |
| 2224 | } |
| 2225 | } |
| 2226 | X509_free(cert); |
| 2227 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob " |
| 2228 | "to certificate store", __func__); |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
| 2232 | #ifdef ANDROID |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2233 | /* Single alias */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2234 | if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) { |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2235 | if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx), |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2236 | &ca_cert[11]) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2237 | return -1; |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2238 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
| 2239 | return 0; |
| 2240 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2241 | |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2242 | /* Multiple aliases separated by space */ |
| 2243 | if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) { |
| 2244 | char *aliases = os_strdup(&ca_cert[12]); |
| 2245 | const char *delim = " "; |
| 2246 | int rc = 0; |
| 2247 | char *savedptr; |
| 2248 | char *alias; |
| 2249 | |
| 2250 | if (!aliases) |
| 2251 | return -1; |
| 2252 | alias = strtok_r(aliases, delim, &savedptr); |
| 2253 | for (; alias; alias = strtok_r(NULL, delim, &savedptr)) { |
| 2254 | if (tls_add_ca_from_keystore_encoded( |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2255 | SSL_CTX_get_cert_store(ssl_ctx), alias)) { |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2256 | wpa_printf(MSG_WARNING, |
| 2257 | "OpenSSL: %s - Failed to add ca_cert %s from keystore", |
| 2258 | __func__, alias); |
| 2259 | rc = -1; |
| 2260 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2261 | } |
| 2262 | } |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2263 | os_free(aliases); |
| 2264 | if (rc) |
| 2265 | return rc; |
| 2266 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2267 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
| 2268 | return 0; |
| 2269 | } |
| 2270 | #endif /* ANDROID */ |
| 2271 | |
| 2272 | #ifdef CONFIG_NATIVE_WINDOWS |
| 2273 | if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) == |
| 2274 | 0) { |
| 2275 | wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from " |
| 2276 | "system certificate store"); |
| 2277 | return 0; |
| 2278 | } |
| 2279 | #endif /* CONFIG_NATIVE_WINDOWS */ |
| 2280 | |
| 2281 | if (ca_cert || ca_path) { |
| 2282 | #ifndef OPENSSL_NO_STDIO |
| 2283 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) != |
| 2284 | 1) { |
| 2285 | tls_show_errors(MSG_WARNING, __func__, |
| 2286 | "Failed to load root certificates"); |
| 2287 | if (ca_cert && |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2288 | tls_load_ca_der(data, ca_cert) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2289 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded " |
| 2290 | "DER format CA certificate", |
| 2291 | __func__); |
| 2292 | } else |
| 2293 | return -1; |
| 2294 | } else { |
| 2295 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " |
| 2296 | "certificate(s) loaded"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2297 | tls_get_errors(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2298 | } |
| 2299 | #else /* OPENSSL_NO_STDIO */ |
| 2300 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", |
| 2301 | __func__); |
| 2302 | return -1; |
| 2303 | #endif /* OPENSSL_NO_STDIO */ |
| 2304 | } else { |
| 2305 | /* No ca_cert configured - do not try to verify server |
| 2306 | * certificate */ |
| 2307 | conn->ca_cert_verify = 0; |
| 2308 | } |
| 2309 | |
| 2310 | return 0; |
| 2311 | } |
| 2312 | |
| 2313 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2314 | 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] | 2315 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2316 | SSL_CTX *ssl_ctx = data->ssl; |
| 2317 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2318 | if (ca_cert) { |
| 2319 | if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1) |
| 2320 | { |
| 2321 | tls_show_errors(MSG_WARNING, __func__, |
| 2322 | "Failed to load root certificates"); |
| 2323 | return -1; |
| 2324 | } |
| 2325 | |
| 2326 | wpa_printf(MSG_DEBUG, "TLS: Trusted root " |
| 2327 | "certificate(s) loaded"); |
| 2328 | |
| 2329 | #ifndef OPENSSL_NO_STDIO |
| 2330 | /* Add the same CAs to the client certificate requests */ |
| 2331 | SSL_CTX_set_client_CA_list(ssl_ctx, |
| 2332 | SSL_load_client_CA_file(ca_cert)); |
| 2333 | #endif /* OPENSSL_NO_STDIO */ |
| 2334 | } |
| 2335 | |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
| 2339 | |
| 2340 | int tls_global_set_verify(void *ssl_ctx, int check_crl) |
| 2341 | { |
| 2342 | int flags; |
| 2343 | |
| 2344 | if (check_crl) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2345 | struct tls_data *data = ssl_ctx; |
| 2346 | X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2347 | if (cs == NULL) { |
| 2348 | tls_show_errors(MSG_INFO, __func__, "Failed to get " |
| 2349 | "certificate store when enabling " |
| 2350 | "check_crl"); |
| 2351 | return -1; |
| 2352 | } |
| 2353 | flags = X509_V_FLAG_CRL_CHECK; |
| 2354 | if (check_crl == 2) |
| 2355 | flags |= X509_V_FLAG_CRL_CHECK_ALL; |
| 2356 | X509_STORE_set_flags(cs, flags); |
| 2357 | } |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | static int tls_connection_set_subject_match(struct tls_connection *conn, |
| 2363 | const char *subject_match, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2364 | const char *altsubject_match, |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2365 | const char *suffix_match, |
| 2366 | const char *domain_match) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2367 | { |
| 2368 | os_free(conn->subject_match); |
| 2369 | conn->subject_match = NULL; |
| 2370 | if (subject_match) { |
| 2371 | conn->subject_match = os_strdup(subject_match); |
| 2372 | if (conn->subject_match == NULL) |
| 2373 | return -1; |
| 2374 | } |
| 2375 | |
| 2376 | os_free(conn->altsubject_match); |
| 2377 | conn->altsubject_match = NULL; |
| 2378 | if (altsubject_match) { |
| 2379 | conn->altsubject_match = os_strdup(altsubject_match); |
| 2380 | if (conn->altsubject_match == NULL) |
| 2381 | return -1; |
| 2382 | } |
| 2383 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 2384 | os_free(conn->suffix_match); |
| 2385 | conn->suffix_match = NULL; |
| 2386 | if (suffix_match) { |
| 2387 | conn->suffix_match = os_strdup(suffix_match); |
| 2388 | if (conn->suffix_match == NULL) |
| 2389 | return -1; |
| 2390 | } |
| 2391 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2392 | os_free(conn->domain_match); |
| 2393 | conn->domain_match = NULL; |
| 2394 | if (domain_match) { |
| 2395 | conn->domain_match = os_strdup(domain_match); |
| 2396 | if (conn->domain_match == NULL) |
| 2397 | return -1; |
| 2398 | } |
| 2399 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2400 | return 0; |
| 2401 | } |
| 2402 | |
| 2403 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 2404 | #ifdef CONFIG_SUITEB |
| 2405 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L |
| 2406 | static int suiteb_cert_cb(SSL *ssl, void *arg) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2407 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 2408 | struct tls_connection *conn = arg; |
| 2409 | |
| 2410 | /* |
| 2411 | * This cert_cb() is not really the best location for doing a |
| 2412 | * constraint check for the ServerKeyExchange message, but this seems to |
| 2413 | * be the only place where the current OpenSSL sequence can be |
| 2414 | * terminated cleanly with an TLS alert going out to the server. |
| 2415 | */ |
| 2416 | |
| 2417 | if (!(conn->flags & TLS_CONN_SUITEB)) |
| 2418 | return 1; |
| 2419 | |
| 2420 | /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */ |
| 2421 | if (conn->cipher_suite != 0x9f) |
| 2422 | return 1; |
| 2423 | |
| 2424 | if (conn->server_dh_prime_len >= 3072) |
| 2425 | return 1; |
| 2426 | |
| 2427 | wpa_printf(MSG_DEBUG, |
| 2428 | "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake", |
| 2429 | conn->server_dh_prime_len); |
| 2430 | return 0; |
| 2431 | } |
| 2432 | #endif /* OPENSSL_VERSION_NUMBER */ |
| 2433 | #endif /* CONFIG_SUITEB */ |
| 2434 | |
| 2435 | |
| 2436 | static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags) |
| 2437 | { |
| 2438 | SSL *ssl = conn->ssl; |
| 2439 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2440 | #ifdef SSL_OP_NO_TICKET |
| 2441 | if (flags & TLS_CONN_DISABLE_SESSION_TICKET) |
| 2442 | SSL_set_options(ssl, SSL_OP_NO_TICKET); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2443 | else |
| 2444 | SSL_clear_options(ssl, SSL_OP_NO_TICKET); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2445 | #endif /* SSL_OP_NO_TICKET */ |
| 2446 | |
| 2447 | #ifdef SSL_OP_NO_TLSv1 |
| 2448 | if (flags & TLS_CONN_DISABLE_TLSv1_0) |
| 2449 | SSL_set_options(ssl, SSL_OP_NO_TLSv1); |
| 2450 | else |
| 2451 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1); |
| 2452 | #endif /* SSL_OP_NO_TLSv1 */ |
| 2453 | #ifdef SSL_OP_NO_TLSv1_1 |
| 2454 | if (flags & TLS_CONN_DISABLE_TLSv1_1) |
| 2455 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_1); |
| 2456 | else |
| 2457 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1); |
| 2458 | #endif /* SSL_OP_NO_TLSv1_1 */ |
| 2459 | #ifdef SSL_OP_NO_TLSv1_2 |
| 2460 | if (flags & TLS_CONN_DISABLE_TLSv1_2) |
| 2461 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_2); |
| 2462 | else |
| 2463 | SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2); |
| 2464 | #endif /* SSL_OP_NO_TLSv1_2 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 2465 | #ifdef CONFIG_SUITEB |
| 2466 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L |
| 2467 | if (flags & TLS_CONN_SUITEB_NO_ECDH) { |
| 2468 | const char *ciphers = "DHE-RSA-AES256-GCM-SHA384"; |
| 2469 | |
| 2470 | if (SSL_set_cipher_list(ssl, ciphers) != 1) { |
| 2471 | wpa_printf(MSG_INFO, |
| 2472 | "OpenSSL: Failed to set Suite B ciphers"); |
| 2473 | return -1; |
| 2474 | } |
| 2475 | } else if (flags & TLS_CONN_SUITEB) { |
| 2476 | EC_KEY *ecdh; |
| 2477 | const char *ciphers = |
| 2478 | "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384"; |
| 2479 | |
| 2480 | if (SSL_set_cipher_list(ssl, ciphers) != 1) { |
| 2481 | wpa_printf(MSG_INFO, |
| 2482 | "OpenSSL: Failed to set Suite B ciphers"); |
| 2483 | return -1; |
| 2484 | } |
| 2485 | |
| 2486 | if (SSL_set1_curves_list(ssl, "P-384") != 1) { |
| 2487 | wpa_printf(MSG_INFO, |
| 2488 | "OpenSSL: Failed to set Suite B curves"); |
| 2489 | return -1; |
| 2490 | } |
| 2491 | |
| 2492 | ecdh = EC_KEY_new_by_curve_name(NID_secp384r1); |
| 2493 | if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) { |
| 2494 | EC_KEY_free(ecdh); |
| 2495 | wpa_printf(MSG_INFO, |
| 2496 | "OpenSSL: Failed to set ECDH parameter"); |
| 2497 | return -1; |
| 2498 | } |
| 2499 | EC_KEY_free(ecdh); |
| 2500 | } |
| 2501 | if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) { |
| 2502 | /* ECDSA+SHA384 if need to add EC support here */ |
| 2503 | if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) { |
| 2504 | wpa_printf(MSG_INFO, |
| 2505 | "OpenSSL: Failed to set Suite B sigalgs"); |
| 2506 | return -1; |
| 2507 | } |
| 2508 | |
| 2509 | SSL_set_options(ssl, SSL_OP_NO_TLSv1); |
| 2510 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_1); |
| 2511 | SSL_set_cert_cb(ssl, suiteb_cert_cb, conn); |
| 2512 | } |
| 2513 | #else /* OPENSSL_VERSION_NUMBER < 0x10002000L */ |
| 2514 | if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) { |
| 2515 | wpa_printf(MSG_ERROR, |
| 2516 | "OpenSSL: Suite B RSA case not supported with this OpenSSL version"); |
| 2517 | return -1; |
| 2518 | } |
| 2519 | #endif /* OPENSSL_VERSION_NUMBER */ |
| 2520 | #endif /* CONFIG_SUITEB */ |
| 2521 | |
| 2522 | return 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2526 | int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2527 | int verify_peer, unsigned int flags, |
| 2528 | const u8 *session_ctx, size_t session_ctx_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2529 | { |
| 2530 | static int counter = 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2531 | struct tls_data *data = ssl_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2532 | |
| 2533 | if (conn == NULL) |
| 2534 | return -1; |
| 2535 | |
| 2536 | if (verify_peer) { |
| 2537 | conn->ca_cert_verify = 1; |
| 2538 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER | |
| 2539 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | |
| 2540 | SSL_VERIFY_CLIENT_ONCE, tls_verify_cb); |
| 2541 | } else { |
| 2542 | conn->ca_cert_verify = 0; |
| 2543 | SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL); |
| 2544 | } |
| 2545 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 2546 | if (tls_set_conn_flags(conn, flags) < 0) |
| 2547 | return -1; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2548 | conn->flags = flags; |
| 2549 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2550 | SSL_set_accept_state(conn->ssl); |
| 2551 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2552 | if (data->tls_session_lifetime == 0) { |
| 2553 | /* |
| 2554 | * Set session id context to a unique value to make sure |
| 2555 | * session resumption cannot be used either through session |
| 2556 | * caching or TLS ticket extension. |
| 2557 | */ |
| 2558 | counter++; |
| 2559 | SSL_set_session_id_context(conn->ssl, |
| 2560 | (const unsigned char *) &counter, |
| 2561 | sizeof(counter)); |
| 2562 | } else if (session_ctx) { |
| 2563 | SSL_set_session_id_context(conn->ssl, session_ctx, |
| 2564 | session_ctx_len); |
| 2565 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2566 | |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
| 2570 | |
| 2571 | static int tls_connection_client_cert(struct tls_connection *conn, |
| 2572 | const char *client_cert, |
| 2573 | const u8 *client_cert_blob, |
| 2574 | size_t client_cert_blob_len) |
| 2575 | { |
| 2576 | if (client_cert == NULL && client_cert_blob == NULL) |
| 2577 | return 0; |
| 2578 | |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 2579 | #ifdef PKCS12_FUNCS |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2580 | #if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 2581 | /* |
| 2582 | * Clear previously set extra chain certificates, if any, from PKCS#12 |
| 2583 | * processing in tls_parse_pkcs12() to allow OpenSSL to build a new |
| 2584 | * chain properly. |
| 2585 | */ |
| 2586 | SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx); |
| 2587 | #endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */ |
| 2588 | #endif /* PKCS12_FUNCS */ |
| 2589 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2590 | if (client_cert_blob && |
| 2591 | SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob, |
| 2592 | client_cert_blob_len) == 1) { |
| 2593 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> " |
| 2594 | "OK"); |
| 2595 | return 0; |
| 2596 | } else if (client_cert_blob) { |
| 2597 | tls_show_errors(MSG_DEBUG, __func__, |
| 2598 | "SSL_use_certificate_ASN1 failed"); |
| 2599 | } |
| 2600 | |
| 2601 | if (client_cert == NULL) |
| 2602 | return -1; |
| 2603 | |
| 2604 | #ifdef ANDROID |
| 2605 | if (os_strncmp("keystore://", client_cert, 11) == 0) { |
| 2606 | BIO *bio = BIO_from_keystore(&client_cert[11]); |
| 2607 | X509 *x509 = NULL; |
| 2608 | int ret = -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 2609 | if (bio) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2610 | x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 2611 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2612 | if (x509) { |
| 2613 | if (SSL_use_certificate(conn->ssl, x509) == 1) |
| 2614 | ret = 0; |
| 2615 | X509_free(x509); |
| 2616 | } |
Paul Stewart | 50772e8 | 2017-01-25 13:59:16 -0800 | [diff] [blame] | 2617 | |
| 2618 | /* Read additional certificates into the chain. */ |
| 2619 | while (bio) { |
| 2620 | x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 2621 | if (x509) { |
| 2622 | /* Takes ownership of x509 */ |
| 2623 | SSL_add0_chain_cert(conn->ssl, x509); |
| 2624 | } else { |
| 2625 | BIO_free(bio); |
| 2626 | bio = NULL; |
| 2627 | } |
| 2628 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2629 | return ret; |
| 2630 | } |
| 2631 | #endif /* ANDROID */ |
| 2632 | |
| 2633 | #ifndef OPENSSL_NO_STDIO |
| 2634 | if (SSL_use_certificate_file(conn->ssl, client_cert, |
| 2635 | SSL_FILETYPE_ASN1) == 1) { |
| 2636 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)" |
| 2637 | " --> OK"); |
| 2638 | return 0; |
| 2639 | } |
| 2640 | |
| 2641 | if (SSL_use_certificate_file(conn->ssl, client_cert, |
| 2642 | SSL_FILETYPE_PEM) == 1) { |
| 2643 | ERR_clear_error(); |
| 2644 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)" |
| 2645 | " --> OK"); |
| 2646 | return 0; |
| 2647 | } |
| 2648 | |
| 2649 | tls_show_errors(MSG_DEBUG, __func__, |
| 2650 | "SSL_use_certificate_file failed"); |
| 2651 | #else /* OPENSSL_NO_STDIO */ |
| 2652 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); |
| 2653 | #endif /* OPENSSL_NO_STDIO */ |
| 2654 | |
| 2655 | return -1; |
| 2656 | } |
| 2657 | |
| 2658 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2659 | static int tls_global_client_cert(struct tls_data *data, |
| 2660 | const char *client_cert) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2661 | { |
| 2662 | #ifndef OPENSSL_NO_STDIO |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2663 | SSL_CTX *ssl_ctx = data->ssl; |
| 2664 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2665 | if (client_cert == NULL) |
| 2666 | return 0; |
| 2667 | |
| 2668 | if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert, |
| 2669 | SSL_FILETYPE_ASN1) != 1 && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2670 | SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2671 | SSL_CTX_use_certificate_file(ssl_ctx, client_cert, |
| 2672 | SSL_FILETYPE_PEM) != 1) { |
| 2673 | tls_show_errors(MSG_INFO, __func__, |
| 2674 | "Failed to load client certificate"); |
| 2675 | return -1; |
| 2676 | } |
| 2677 | return 0; |
| 2678 | #else /* OPENSSL_NO_STDIO */ |
| 2679 | if (client_cert == NULL) |
| 2680 | return 0; |
| 2681 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__); |
| 2682 | return -1; |
| 2683 | #endif /* OPENSSL_NO_STDIO */ |
| 2684 | } |
| 2685 | |
| 2686 | |
| 2687 | static int tls_passwd_cb(char *buf, int size, int rwflag, void *password) |
| 2688 | { |
| 2689 | if (password == NULL) { |
| 2690 | return 0; |
| 2691 | } |
| 2692 | os_strlcpy(buf, (char *) password, size); |
| 2693 | return os_strlen(buf); |
| 2694 | } |
| 2695 | |
| 2696 | |
| 2697 | #ifdef PKCS12_FUNCS |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2698 | 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] | 2699 | const char *passwd) |
| 2700 | { |
| 2701 | EVP_PKEY *pkey; |
| 2702 | X509 *cert; |
| 2703 | STACK_OF(X509) *certs; |
| 2704 | int res = 0; |
| 2705 | char buf[256]; |
| 2706 | |
| 2707 | pkey = NULL; |
| 2708 | cert = NULL; |
| 2709 | certs = NULL; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2710 | if (!passwd) |
| 2711 | passwd = ""; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2712 | if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) { |
| 2713 | tls_show_errors(MSG_DEBUG, __func__, |
| 2714 | "Failed to parse PKCS12 file"); |
| 2715 | PKCS12_free(p12); |
| 2716 | return -1; |
| 2717 | } |
| 2718 | wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data"); |
| 2719 | |
| 2720 | if (cert) { |
| 2721 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 2722 | sizeof(buf)); |
| 2723 | wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: " |
| 2724 | "subject='%s'", buf); |
| 2725 | if (ssl) { |
| 2726 | if (SSL_use_certificate(ssl, cert) != 1) |
| 2727 | res = -1; |
| 2728 | } else { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2729 | if (SSL_CTX_use_certificate(data->ssl, cert) != 1) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2730 | res = -1; |
| 2731 | } |
| 2732 | X509_free(cert); |
| 2733 | } |
| 2734 | |
| 2735 | if (pkey) { |
| 2736 | wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12"); |
| 2737 | if (ssl) { |
| 2738 | if (SSL_use_PrivateKey(ssl, pkey) != 1) |
| 2739 | res = -1; |
| 2740 | } else { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2741 | if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2742 | res = -1; |
| 2743 | } |
| 2744 | EVP_PKEY_free(pkey); |
| 2745 | } |
| 2746 | |
| 2747 | if (certs) { |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2748 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2749 | if (ssl) |
| 2750 | SSL_clear_chain_certs(ssl); |
| 2751 | else |
| 2752 | SSL_CTX_clear_chain_certs(data->ssl); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2753 | while ((cert = sk_X509_pop(certs)) != NULL) { |
| 2754 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 2755 | sizeof(buf)); |
| 2756 | wpa_printf(MSG_DEBUG, "TLS: additional certificate" |
| 2757 | " from PKCS12: subject='%s'", buf); |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2758 | if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) || |
| 2759 | (!ssl && SSL_CTX_add1_chain_cert(data->ssl, |
| 2760 | cert) != 1)) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2761 | tls_show_errors(MSG_DEBUG, __func__, |
| 2762 | "Failed to add additional certificate"); |
| 2763 | res = -1; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2764 | X509_free(cert); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2765 | break; |
| 2766 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2767 | X509_free(cert); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2768 | } |
| 2769 | if (!res) { |
| 2770 | /* Try to continue anyway */ |
| 2771 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2772 | sk_X509_pop_free(certs, X509_free); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2773 | #ifndef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 2774 | if (ssl) |
| 2775 | res = SSL_build_cert_chain( |
| 2776 | ssl, |
| 2777 | SSL_BUILD_CHAIN_FLAG_CHECK | |
| 2778 | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR); |
| 2779 | else |
| 2780 | res = SSL_CTX_build_cert_chain( |
| 2781 | data->ssl, |
| 2782 | SSL_BUILD_CHAIN_FLAG_CHECK | |
| 2783 | SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2784 | if (!res) { |
| 2785 | tls_show_errors(MSG_DEBUG, __func__, |
| 2786 | "Failed to build certificate chain"); |
| 2787 | } else if (res == 2) { |
| 2788 | wpa_printf(MSG_DEBUG, |
| 2789 | "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates"); |
| 2790 | } |
| 2791 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 2792 | /* |
| 2793 | * Try to continue regardless of result since it is possible for |
| 2794 | * the extra certificates not to be required. |
| 2795 | */ |
| 2796 | res = 0; |
| 2797 | #else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2798 | SSL_CTX_clear_extra_chain_certs(data->ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2799 | while ((cert = sk_X509_pop(certs)) != NULL) { |
| 2800 | X509_NAME_oneline(X509_get_subject_name(cert), buf, |
| 2801 | sizeof(buf)); |
| 2802 | wpa_printf(MSG_DEBUG, "TLS: additional certificate" |
| 2803 | " from PKCS12: subject='%s'", buf); |
| 2804 | /* |
| 2805 | * There is no SSL equivalent for the chain cert - so |
| 2806 | * always add it to the context... |
| 2807 | */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2808 | if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1) |
| 2809 | { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2810 | X509_free(cert); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2811 | res = -1; |
| 2812 | break; |
| 2813 | } |
| 2814 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 2815 | sk_X509_pop_free(certs, X509_free); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2816 | #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | PKCS12_free(p12); |
| 2820 | |
| 2821 | if (res < 0) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2822 | tls_get_errors(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2823 | |
| 2824 | return res; |
| 2825 | } |
| 2826 | #endif /* PKCS12_FUNCS */ |
| 2827 | |
| 2828 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2829 | static int tls_read_pkcs12(struct tls_data *data, SSL *ssl, |
| 2830 | const char *private_key, const char *passwd) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2831 | { |
| 2832 | #ifdef PKCS12_FUNCS |
| 2833 | FILE *f; |
| 2834 | PKCS12 *p12; |
| 2835 | |
| 2836 | f = fopen(private_key, "rb"); |
| 2837 | if (f == NULL) |
| 2838 | return -1; |
| 2839 | |
| 2840 | p12 = d2i_PKCS12_fp(f, NULL); |
| 2841 | fclose(f); |
| 2842 | |
| 2843 | if (p12 == NULL) { |
| 2844 | tls_show_errors(MSG_INFO, __func__, |
| 2845 | "Failed to use PKCS#12 file"); |
| 2846 | return -1; |
| 2847 | } |
| 2848 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2849 | return tls_parse_pkcs12(data, ssl, p12, passwd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2850 | |
| 2851 | #else /* PKCS12_FUNCS */ |
| 2852 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read " |
| 2853 | "p12/pfx files"); |
| 2854 | return -1; |
| 2855 | #endif /* PKCS12_FUNCS */ |
| 2856 | } |
| 2857 | |
| 2858 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2859 | static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2860 | const u8 *blob, size_t len, const char *passwd) |
| 2861 | { |
| 2862 | #ifdef PKCS12_FUNCS |
| 2863 | PKCS12 *p12; |
| 2864 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2865 | p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2866 | if (p12 == NULL) { |
| 2867 | tls_show_errors(MSG_INFO, __func__, |
| 2868 | "Failed to use PKCS#12 blob"); |
| 2869 | return -1; |
| 2870 | } |
| 2871 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2872 | return tls_parse_pkcs12(data, ssl, p12, passwd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2873 | |
| 2874 | #else /* PKCS12_FUNCS */ |
| 2875 | wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse " |
| 2876 | "p12/pfx blobs"); |
| 2877 | return -1; |
| 2878 | #endif /* PKCS12_FUNCS */ |
| 2879 | } |
| 2880 | |
| 2881 | |
| 2882 | #ifndef OPENSSL_NO_ENGINE |
| 2883 | static int tls_engine_get_cert(struct tls_connection *conn, |
| 2884 | const char *cert_id, |
| 2885 | X509 **cert) |
| 2886 | { |
| 2887 | /* this runs after the private key is loaded so no PIN is required */ |
| 2888 | struct { |
| 2889 | const char *cert_id; |
| 2890 | X509 *cert; |
| 2891 | } params; |
| 2892 | params.cert_id = cert_id; |
| 2893 | params.cert = NULL; |
| 2894 | |
| 2895 | if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL", |
| 2896 | 0, ¶ms, NULL, 1)) { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 2897 | unsigned long err = ERR_get_error(); |
| 2898 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2899 | wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id" |
| 2900 | " '%s' [%s]", cert_id, |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 2901 | ERR_error_string(err, NULL)); |
| 2902 | if (tls_is_pin_error(err)) |
| 2903 | return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2904 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 2905 | } |
| 2906 | if (!params.cert) { |
| 2907 | wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id" |
| 2908 | " '%s'", cert_id); |
| 2909 | return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED; |
| 2910 | } |
| 2911 | *cert = params.cert; |
| 2912 | return 0; |
| 2913 | } |
| 2914 | #endif /* OPENSSL_NO_ENGINE */ |
| 2915 | |
| 2916 | |
| 2917 | static int tls_connection_engine_client_cert(struct tls_connection *conn, |
| 2918 | const char *cert_id) |
| 2919 | { |
| 2920 | #ifndef OPENSSL_NO_ENGINE |
| 2921 | X509 *cert; |
| 2922 | |
| 2923 | if (tls_engine_get_cert(conn, cert_id, &cert)) |
| 2924 | return -1; |
| 2925 | |
| 2926 | if (!SSL_use_certificate(conn->ssl, cert)) { |
| 2927 | tls_show_errors(MSG_ERROR, __func__, |
| 2928 | "SSL_use_certificate failed"); |
| 2929 | X509_free(cert); |
| 2930 | return -1; |
| 2931 | } |
| 2932 | X509_free(cert); |
| 2933 | wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> " |
| 2934 | "OK"); |
| 2935 | return 0; |
| 2936 | |
| 2937 | #else /* OPENSSL_NO_ENGINE */ |
| 2938 | return -1; |
| 2939 | #endif /* OPENSSL_NO_ENGINE */ |
| 2940 | } |
| 2941 | |
| 2942 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2943 | static int tls_connection_engine_ca_cert(struct tls_data *data, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2944 | struct tls_connection *conn, |
| 2945 | const char *ca_cert_id) |
| 2946 | { |
| 2947 | #ifndef OPENSSL_NO_ENGINE |
| 2948 | X509 *cert; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2949 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2950 | X509_STORE *store; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2951 | |
| 2952 | if (tls_engine_get_cert(conn, ca_cert_id, &cert)) |
| 2953 | return -1; |
| 2954 | |
| 2955 | /* start off the same as tls_connection_ca_cert */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2956 | store = X509_STORE_new(); |
| 2957 | if (store == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2958 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new " |
| 2959 | "certificate store", __func__); |
| 2960 | X509_free(cert); |
| 2961 | return -1; |
| 2962 | } |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2963 | SSL_CTX_set_cert_store(ssl_ctx, store); |
| 2964 | if (!X509_STORE_add_cert(store, cert)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2965 | unsigned long err = ERR_peek_error(); |
| 2966 | tls_show_errors(MSG_WARNING, __func__, |
| 2967 | "Failed to add CA certificate from engine " |
| 2968 | "to certificate store"); |
| 2969 | if (ERR_GET_LIB(err) == ERR_LIB_X509 && |
| 2970 | ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) { |
| 2971 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert" |
| 2972 | " already in hash table error", |
| 2973 | __func__); |
| 2974 | } else { |
| 2975 | X509_free(cert); |
| 2976 | return -1; |
| 2977 | } |
| 2978 | } |
| 2979 | X509_free(cert); |
| 2980 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine " |
| 2981 | "to certificate store", __func__); |
| 2982 | SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2983 | conn->ca_cert_verify = 1; |
| 2984 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2985 | return 0; |
| 2986 | |
| 2987 | #else /* OPENSSL_NO_ENGINE */ |
| 2988 | return -1; |
| 2989 | #endif /* OPENSSL_NO_ENGINE */ |
| 2990 | } |
| 2991 | |
| 2992 | |
| 2993 | static int tls_connection_engine_private_key(struct tls_connection *conn) |
| 2994 | { |
Adam Langley | 1eb02ed | 2015-04-21 19:00:05 -0700 | [diff] [blame] | 2995 | #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2996 | if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) { |
| 2997 | tls_show_errors(MSG_ERROR, __func__, |
| 2998 | "ENGINE: cannot use private key for TLS"); |
| 2999 | return -1; |
| 3000 | } |
| 3001 | if (!SSL_check_private_key(conn->ssl)) { |
| 3002 | tls_show_errors(MSG_INFO, __func__, |
| 3003 | "Private key failed verification"); |
| 3004 | return -1; |
| 3005 | } |
| 3006 | return 0; |
| 3007 | #else /* OPENSSL_NO_ENGINE */ |
| 3008 | wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but " |
| 3009 | "engine support was not compiled in"); |
| 3010 | return -1; |
| 3011 | #endif /* OPENSSL_NO_ENGINE */ |
| 3012 | } |
| 3013 | |
| 3014 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 3015 | static void tls_clear_default_passwd_cb(SSL_CTX *ssl_ctx, SSL *ssl) |
| 3016 | { |
| 3017 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL) |
| 3018 | if (ssl) { |
| 3019 | SSL_set_default_passwd_cb(ssl, NULL); |
| 3020 | SSL_set_default_passwd_cb_userdata(ssl, NULL); |
| 3021 | } |
| 3022 | #endif /* >= 1.1.0f && !LibreSSL && !BoringSSL */ |
| 3023 | SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL); |
| 3024 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, NULL); |
| 3025 | } |
| 3026 | |
| 3027 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3028 | static int tls_connection_private_key(struct tls_data *data, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3029 | struct tls_connection *conn, |
| 3030 | const char *private_key, |
| 3031 | const char *private_key_passwd, |
| 3032 | const u8 *private_key_blob, |
| 3033 | size_t private_key_blob_len) |
| 3034 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3035 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3036 | char *passwd; |
| 3037 | int ok; |
| 3038 | |
| 3039 | if (private_key == NULL && private_key_blob == NULL) |
| 3040 | return 0; |
| 3041 | |
| 3042 | if (private_key_passwd) { |
| 3043 | passwd = os_strdup(private_key_passwd); |
| 3044 | if (passwd == NULL) |
| 3045 | return -1; |
| 3046 | } else |
| 3047 | passwd = NULL; |
| 3048 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 3049 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL) |
| 3050 | /* |
| 3051 | * In OpenSSL >= 1.1.0f SSL_use_PrivateKey_file() uses the callback |
| 3052 | * from the SSL object. See OpenSSL commit d61461a75253. |
| 3053 | */ |
| 3054 | SSL_set_default_passwd_cb(conn->ssl, tls_passwd_cb); |
| 3055 | SSL_set_default_passwd_cb_userdata(conn->ssl, passwd); |
| 3056 | #endif /* >= 1.1.0f && !LibreSSL && !BoringSSL */ |
| 3057 | /* Keep these for OpenSSL < 1.1.0f */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3058 | SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb); |
| 3059 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd); |
| 3060 | |
| 3061 | ok = 0; |
| 3062 | while (private_key_blob) { |
| 3063 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl, |
| 3064 | (u8 *) private_key_blob, |
| 3065 | private_key_blob_len) == 1) { |
| 3066 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" |
| 3067 | "ASN1(EVP_PKEY_RSA) --> OK"); |
| 3068 | ok = 1; |
| 3069 | break; |
| 3070 | } |
| 3071 | |
| 3072 | if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl, |
| 3073 | (u8 *) private_key_blob, |
| 3074 | private_key_blob_len) == 1) { |
| 3075 | wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_" |
| 3076 | "ASN1(EVP_PKEY_DSA) --> OK"); |
| 3077 | ok = 1; |
| 3078 | break; |
| 3079 | } |
| 3080 | |
| 3081 | if (SSL_use_RSAPrivateKey_ASN1(conn->ssl, |
| 3082 | (u8 *) private_key_blob, |
| 3083 | private_key_blob_len) == 1) { |
| 3084 | wpa_printf(MSG_DEBUG, "OpenSSL: " |
| 3085 | "SSL_use_RSAPrivateKey_ASN1 --> OK"); |
| 3086 | ok = 1; |
| 3087 | break; |
| 3088 | } |
| 3089 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3090 | if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3091 | private_key_blob_len, passwd) == 0) { |
| 3092 | wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> " |
| 3093 | "OK"); |
| 3094 | ok = 1; |
| 3095 | break; |
| 3096 | } |
| 3097 | |
| 3098 | break; |
| 3099 | } |
| 3100 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3101 | while (!ok && private_key) { |
| 3102 | #ifndef OPENSSL_NO_STDIO |
| 3103 | if (SSL_use_PrivateKey_file(conn->ssl, private_key, |
| 3104 | SSL_FILETYPE_ASN1) == 1) { |
| 3105 | wpa_printf(MSG_DEBUG, "OpenSSL: " |
| 3106 | "SSL_use_PrivateKey_File (DER) --> OK"); |
| 3107 | ok = 1; |
| 3108 | break; |
| 3109 | } |
| 3110 | |
| 3111 | if (SSL_use_PrivateKey_file(conn->ssl, private_key, |
| 3112 | SSL_FILETYPE_PEM) == 1) { |
| 3113 | wpa_printf(MSG_DEBUG, "OpenSSL: " |
| 3114 | "SSL_use_PrivateKey_File (PEM) --> OK"); |
| 3115 | ok = 1; |
| 3116 | break; |
| 3117 | } |
| 3118 | #else /* OPENSSL_NO_STDIO */ |
| 3119 | wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", |
| 3120 | __func__); |
| 3121 | #endif /* OPENSSL_NO_STDIO */ |
| 3122 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3123 | if (tls_read_pkcs12(data, conn->ssl, private_key, passwd) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3124 | == 0) { |
| 3125 | wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file " |
| 3126 | "--> OK"); |
| 3127 | ok = 1; |
| 3128 | break; |
| 3129 | } |
| 3130 | |
| 3131 | if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) { |
| 3132 | wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to " |
| 3133 | "access certificate store --> OK"); |
| 3134 | ok = 1; |
| 3135 | break; |
| 3136 | } |
| 3137 | |
| 3138 | break; |
| 3139 | } |
| 3140 | |
| 3141 | if (!ok) { |
| 3142 | tls_show_errors(MSG_INFO, __func__, |
| 3143 | "Failed to load private key"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 3144 | tls_clear_default_passwd_cb(ssl_ctx, conn->ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3145 | os_free(passwd); |
| 3146 | return -1; |
| 3147 | } |
| 3148 | ERR_clear_error(); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 3149 | tls_clear_default_passwd_cb(ssl_ctx, conn->ssl); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3150 | os_free(passwd); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3151 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3152 | if (!SSL_check_private_key(conn->ssl)) { |
| 3153 | tls_show_errors(MSG_INFO, __func__, "Private key failed " |
| 3154 | "verification"); |
| 3155 | return -1; |
| 3156 | } |
| 3157 | |
| 3158 | wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully"); |
| 3159 | return 0; |
| 3160 | } |
| 3161 | |
| 3162 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3163 | static int tls_global_private_key(struct tls_data *data, |
| 3164 | const char *private_key, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3165 | const char *private_key_passwd) |
| 3166 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3167 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3168 | char *passwd; |
| 3169 | |
| 3170 | if (private_key == NULL) |
| 3171 | return 0; |
| 3172 | |
| 3173 | if (private_key_passwd) { |
| 3174 | passwd = os_strdup(private_key_passwd); |
| 3175 | if (passwd == NULL) |
| 3176 | return -1; |
| 3177 | } else |
| 3178 | passwd = NULL; |
| 3179 | |
| 3180 | SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb); |
| 3181 | SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd); |
| 3182 | if ( |
| 3183 | #ifndef OPENSSL_NO_STDIO |
| 3184 | SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key, |
| 3185 | SSL_FILETYPE_ASN1) != 1 && |
| 3186 | SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key, |
| 3187 | SSL_FILETYPE_PEM) != 1 && |
| 3188 | #endif /* OPENSSL_NO_STDIO */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3189 | tls_read_pkcs12(data, NULL, private_key, passwd)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3190 | tls_show_errors(MSG_INFO, __func__, |
| 3191 | "Failed to load private key"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 3192 | tls_clear_default_passwd_cb(ssl_ctx, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3193 | os_free(passwd); |
| 3194 | ERR_clear_error(); |
| 3195 | return -1; |
| 3196 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 3197 | tls_clear_default_passwd_cb(ssl_ctx, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3198 | os_free(passwd); |
| 3199 | ERR_clear_error(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3200 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3201 | if (!SSL_CTX_check_private_key(ssl_ctx)) { |
| 3202 | tls_show_errors(MSG_INFO, __func__, |
| 3203 | "Private key failed verification"); |
| 3204 | return -1; |
| 3205 | } |
| 3206 | |
| 3207 | return 0; |
| 3208 | } |
| 3209 | |
| 3210 | |
| 3211 | static int tls_connection_dh(struct tls_connection *conn, const char *dh_file) |
| 3212 | { |
| 3213 | #ifdef OPENSSL_NO_DH |
| 3214 | if (dh_file == NULL) |
| 3215 | return 0; |
| 3216 | wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but " |
| 3217 | "dh_file specified"); |
| 3218 | return -1; |
| 3219 | #else /* OPENSSL_NO_DH */ |
| 3220 | DH *dh; |
| 3221 | BIO *bio; |
| 3222 | |
| 3223 | /* TODO: add support for dh_blob */ |
| 3224 | if (dh_file == NULL) |
| 3225 | return 0; |
| 3226 | if (conn == NULL) |
| 3227 | return -1; |
| 3228 | |
| 3229 | bio = BIO_new_file(dh_file, "r"); |
| 3230 | if (bio == NULL) { |
| 3231 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", |
| 3232 | dh_file, ERR_error_string(ERR_get_error(), NULL)); |
| 3233 | return -1; |
| 3234 | } |
| 3235 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 3236 | BIO_free(bio); |
| 3237 | #ifndef OPENSSL_NO_DSA |
| 3238 | while (dh == NULL) { |
| 3239 | DSA *dsa; |
| 3240 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -" |
| 3241 | " trying to parse as DSA params", dh_file, |
| 3242 | ERR_error_string(ERR_get_error(), NULL)); |
| 3243 | bio = BIO_new_file(dh_file, "r"); |
| 3244 | if (bio == NULL) |
| 3245 | break; |
| 3246 | dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL); |
| 3247 | BIO_free(bio); |
| 3248 | if (!dsa) { |
| 3249 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file " |
| 3250 | "'%s': %s", dh_file, |
| 3251 | ERR_error_string(ERR_get_error(), NULL)); |
| 3252 | break; |
| 3253 | } |
| 3254 | |
| 3255 | wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format"); |
| 3256 | dh = DSA_dup_DH(dsa); |
| 3257 | DSA_free(dsa); |
| 3258 | if (dh == NULL) { |
| 3259 | wpa_printf(MSG_INFO, "TLS: Failed to convert DSA " |
| 3260 | "params into DH params"); |
| 3261 | break; |
| 3262 | } |
| 3263 | break; |
| 3264 | } |
| 3265 | #endif /* !OPENSSL_NO_DSA */ |
| 3266 | if (dh == NULL) { |
| 3267 | wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file " |
| 3268 | "'%s'", dh_file); |
| 3269 | return -1; |
| 3270 | } |
| 3271 | |
| 3272 | if (SSL_set_tmp_dh(conn->ssl, dh) != 1) { |
| 3273 | wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': " |
| 3274 | "%s", dh_file, |
| 3275 | ERR_error_string(ERR_get_error(), NULL)); |
| 3276 | DH_free(dh); |
| 3277 | return -1; |
| 3278 | } |
| 3279 | DH_free(dh); |
| 3280 | return 0; |
| 3281 | #endif /* OPENSSL_NO_DH */ |
| 3282 | } |
| 3283 | |
| 3284 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3285 | 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] | 3286 | { |
| 3287 | #ifdef OPENSSL_NO_DH |
| 3288 | if (dh_file == NULL) |
| 3289 | return 0; |
| 3290 | wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but " |
| 3291 | "dh_file specified"); |
| 3292 | return -1; |
| 3293 | #else /* OPENSSL_NO_DH */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3294 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3295 | DH *dh; |
| 3296 | BIO *bio; |
| 3297 | |
| 3298 | /* TODO: add support for dh_blob */ |
| 3299 | if (dh_file == NULL) |
| 3300 | return 0; |
| 3301 | if (ssl_ctx == NULL) |
| 3302 | return -1; |
| 3303 | |
| 3304 | bio = BIO_new_file(dh_file, "r"); |
| 3305 | if (bio == NULL) { |
| 3306 | wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s", |
| 3307 | dh_file, ERR_error_string(ERR_get_error(), NULL)); |
| 3308 | return -1; |
| 3309 | } |
| 3310 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 3311 | BIO_free(bio); |
| 3312 | #ifndef OPENSSL_NO_DSA |
| 3313 | while (dh == NULL) { |
| 3314 | DSA *dsa; |
| 3315 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -" |
| 3316 | " trying to parse as DSA params", dh_file, |
| 3317 | ERR_error_string(ERR_get_error(), NULL)); |
| 3318 | bio = BIO_new_file(dh_file, "r"); |
| 3319 | if (bio == NULL) |
| 3320 | break; |
| 3321 | dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL); |
| 3322 | BIO_free(bio); |
| 3323 | if (!dsa) { |
| 3324 | wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file " |
| 3325 | "'%s': %s", dh_file, |
| 3326 | ERR_error_string(ERR_get_error(), NULL)); |
| 3327 | break; |
| 3328 | } |
| 3329 | |
| 3330 | wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format"); |
| 3331 | dh = DSA_dup_DH(dsa); |
| 3332 | DSA_free(dsa); |
| 3333 | if (dh == NULL) { |
| 3334 | wpa_printf(MSG_INFO, "TLS: Failed to convert DSA " |
| 3335 | "params into DH params"); |
| 3336 | break; |
| 3337 | } |
| 3338 | break; |
| 3339 | } |
| 3340 | #endif /* !OPENSSL_NO_DSA */ |
| 3341 | if (dh == NULL) { |
| 3342 | wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file " |
| 3343 | "'%s'", dh_file); |
| 3344 | return -1; |
| 3345 | } |
| 3346 | |
| 3347 | if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) { |
| 3348 | wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': " |
| 3349 | "%s", dh_file, |
| 3350 | ERR_error_string(ERR_get_error(), NULL)); |
| 3351 | DH_free(dh); |
| 3352 | return -1; |
| 3353 | } |
| 3354 | DH_free(dh); |
| 3355 | return 0; |
| 3356 | #endif /* OPENSSL_NO_DH */ |
| 3357 | } |
| 3358 | |
| 3359 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3360 | int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn, |
| 3361 | struct tls_random *keys) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3362 | { |
| 3363 | SSL *ssl; |
| 3364 | |
| 3365 | if (conn == NULL || keys == NULL) |
| 3366 | return -1; |
| 3367 | ssl = conn->ssl; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3368 | if (ssl == NULL) |
| 3369 | return -1; |
| 3370 | |
| 3371 | os_memset(keys, 0, sizeof(*keys)); |
| 3372 | keys->client_random = conn->client_random; |
| 3373 | keys->client_random_len = SSL_get_client_random( |
| 3374 | ssl, conn->client_random, sizeof(conn->client_random)); |
| 3375 | keys->server_random = conn->server_random; |
| 3376 | keys->server_random_len = SSL_get_server_random( |
| 3377 | ssl, conn->server_random, sizeof(conn->server_random)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3378 | |
| 3379 | return 0; |
| 3380 | } |
| 3381 | |
| 3382 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3383 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3384 | static int openssl_get_keyblock_size(SSL *ssl) |
| 3385 | { |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3386 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3387 | const EVP_CIPHER *c; |
| 3388 | const EVP_MD *h; |
| 3389 | int md_size; |
| 3390 | |
| 3391 | if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL || |
| 3392 | ssl->read_hash == NULL) |
| 3393 | return -1; |
| 3394 | |
| 3395 | c = ssl->enc_read_ctx->cipher; |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3396 | h = EVP_MD_CTX_md(ssl->read_hash); |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3397 | if (h) |
| 3398 | md_size = EVP_MD_size(h); |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3399 | else if (ssl->s3) |
| 3400 | md_size = ssl->s3->tmp.new_mac_secret_size; |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3401 | else |
| 3402 | return -1; |
| 3403 | |
| 3404 | wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d " |
| 3405 | "IV_len=%d", EVP_CIPHER_key_length(c), md_size, |
| 3406 | EVP_CIPHER_iv_length(c)); |
| 3407 | return 2 * (EVP_CIPHER_key_length(c) + |
| 3408 | md_size + |
| 3409 | EVP_CIPHER_iv_length(c)); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3410 | #else |
| 3411 | const SSL_CIPHER *ssl_cipher; |
| 3412 | int cipher, digest; |
| 3413 | const EVP_CIPHER *c; |
| 3414 | const EVP_MD *h; |
| 3415 | |
| 3416 | ssl_cipher = SSL_get_current_cipher(ssl); |
| 3417 | if (!ssl_cipher) |
| 3418 | return -1; |
| 3419 | cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher); |
| 3420 | digest = SSL_CIPHER_get_digest_nid(ssl_cipher); |
| 3421 | wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d", |
| 3422 | cipher, digest); |
| 3423 | if (cipher < 0 || digest < 0) |
| 3424 | return -1; |
| 3425 | c = EVP_get_cipherbynid(cipher); |
| 3426 | h = EVP_get_digestbynid(digest); |
| 3427 | if (!c || !h) |
| 3428 | return -1; |
| 3429 | |
| 3430 | wpa_printf(MSG_DEBUG, |
| 3431 | "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d", |
| 3432 | EVP_CIPHER_key_length(c), EVP_MD_size(h), |
| 3433 | EVP_CIPHER_iv_length(c)); |
| 3434 | return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) + |
| 3435 | EVP_CIPHER_iv_length(c)); |
| 3436 | #endif |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3437 | } |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3438 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3439 | |
| 3440 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3441 | int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn, |
| 3442 | const char *label, u8 *out, size_t out_len) |
Dmitry Shmidt | af9da31 | 2015-04-03 10:03:11 -0700 | [diff] [blame] | 3443 | { |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3444 | if (!conn || |
| 3445 | SSL_export_keying_material(conn->ssl, out, out_len, label, |
| 3446 | os_strlen(label), NULL, 0, 0) != 1) |
| 3447 | return -1; |
| 3448 | return 0; |
| 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn, |
| 3453 | u8 *out, size_t out_len) |
| 3454 | { |
| 3455 | #ifdef OPENSSL_NEED_EAP_FAST_PRF |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3456 | SSL *ssl; |
| 3457 | SSL_SESSION *sess; |
| 3458 | u8 *rnd; |
| 3459 | int ret = -1; |
| 3460 | int skip = 0; |
| 3461 | u8 *tmp_out = NULL; |
| 3462 | u8 *_out = out; |
| 3463 | unsigned char client_random[SSL3_RANDOM_SIZE]; |
| 3464 | unsigned char server_random[SSL3_RANDOM_SIZE]; |
| 3465 | unsigned char master_key[64]; |
| 3466 | size_t master_key_len; |
| 3467 | const char *ver; |
| 3468 | |
| 3469 | /* |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3470 | * TLS library did not support EAP-FAST key generation, so get the |
| 3471 | * needed TLS session parameters and use an internal implementation of |
| 3472 | * TLS PRF to derive the key. |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3473 | */ |
| 3474 | |
| 3475 | if (conn == NULL) |
| 3476 | return -1; |
| 3477 | ssl = conn->ssl; |
| 3478 | if (ssl == NULL) |
| 3479 | return -1; |
| 3480 | ver = SSL_get_version(ssl); |
| 3481 | sess = SSL_get_session(ssl); |
| 3482 | if (!ver || !sess) |
| 3483 | return -1; |
| 3484 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3485 | skip = openssl_get_keyblock_size(ssl); |
| 3486 | if (skip < 0) |
| 3487 | return -1; |
| 3488 | tmp_out = os_malloc(skip + out_len); |
| 3489 | if (!tmp_out) |
| 3490 | return -1; |
| 3491 | _out = tmp_out; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3492 | |
| 3493 | rnd = os_malloc(2 * SSL3_RANDOM_SIZE); |
| 3494 | if (!rnd) { |
| 3495 | os_free(tmp_out); |
| 3496 | return -1; |
| 3497 | } |
| 3498 | |
| 3499 | SSL_get_client_random(ssl, client_random, sizeof(client_random)); |
| 3500 | SSL_get_server_random(ssl, server_random, sizeof(server_random)); |
| 3501 | master_key_len = SSL_SESSION_get_master_key(sess, master_key, |
| 3502 | sizeof(master_key)); |
| 3503 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3504 | os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE); |
| 3505 | os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3506 | |
| 3507 | if (os_strcmp(ver, "TLSv1.2") == 0) { |
| 3508 | tls_prf_sha256(master_key, master_key_len, |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3509 | "key expansion", rnd, 2 * SSL3_RANDOM_SIZE, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3510 | _out, skip + out_len); |
| 3511 | ret = 0; |
| 3512 | } else if (tls_prf_sha1_md5(master_key, master_key_len, |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3513 | "key expansion", rnd, 2 * SSL3_RANDOM_SIZE, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3514 | _out, skip + out_len) == 0) { |
| 3515 | ret = 0; |
| 3516 | } |
| 3517 | os_memset(master_key, 0, sizeof(master_key)); |
| 3518 | os_free(rnd); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3519 | if (ret == 0) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3520 | os_memcpy(out, _out + skip, out_len); |
| 3521 | bin_clear_free(tmp_out, skip); |
| 3522 | |
| 3523 | return ret; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 3524 | #else /* OPENSSL_NEED_EAP_FAST_PRF */ |
| 3525 | wpa_printf(MSG_ERROR, |
| 3526 | "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode"); |
| 3527 | return -1; |
| 3528 | #endif /* OPENSSL_NEED_EAP_FAST_PRF */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | |
| 3532 | static struct wpabuf * |
| 3533 | openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data, |
| 3534 | int server) |
| 3535 | { |
| 3536 | int res; |
| 3537 | struct wpabuf *out_data; |
| 3538 | |
| 3539 | /* |
| 3540 | * Give TLS handshake data from the server (if available) to OpenSSL |
| 3541 | * for processing. |
| 3542 | */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3543 | if (in_data && wpabuf_len(in_data) > 0 && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3544 | BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data)) |
| 3545 | < 0) { |
| 3546 | tls_show_errors(MSG_INFO, __func__, |
| 3547 | "Handshake failed - BIO_write"); |
| 3548 | return NULL; |
| 3549 | } |
| 3550 | |
| 3551 | /* Initiate TLS handshake or continue the existing handshake */ |
| 3552 | if (server) |
| 3553 | res = SSL_accept(conn->ssl); |
| 3554 | else |
| 3555 | res = SSL_connect(conn->ssl); |
| 3556 | if (res != 1) { |
| 3557 | int err = SSL_get_error(conn->ssl, res); |
| 3558 | if (err == SSL_ERROR_WANT_READ) |
| 3559 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want " |
| 3560 | "more data"); |
| 3561 | else if (err == SSL_ERROR_WANT_WRITE) |
| 3562 | wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to " |
| 3563 | "write"); |
| 3564 | else { |
| 3565 | tls_show_errors(MSG_INFO, __func__, "SSL_connect"); |
| 3566 | conn->failed++; |
| 3567 | } |
| 3568 | } |
| 3569 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 3570 | #ifdef CONFIG_SUITEB |
| 3571 | if ((conn->flags & TLS_CONN_SUITEB) && !server && |
| 3572 | os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 && |
| 3573 | conn->server_dh_prime_len < 3072) { |
| 3574 | struct tls_context *context = conn->context; |
| 3575 | |
| 3576 | /* |
| 3577 | * This should not be reached since earlier cert_cb should have |
| 3578 | * terminated the handshake. Keep this check here for extra |
| 3579 | * protection if anything goes wrong with the more low-level |
| 3580 | * checks based on having to parse the TLS handshake messages. |
| 3581 | */ |
| 3582 | wpa_printf(MSG_DEBUG, |
| 3583 | "OpenSSL: Server DH prime length: %d bits", |
| 3584 | conn->server_dh_prime_len); |
| 3585 | |
| 3586 | if (context->event_cb) { |
| 3587 | union tls_event_data ev; |
| 3588 | |
| 3589 | os_memset(&ev, 0, sizeof(ev)); |
| 3590 | ev.alert.is_local = 1; |
| 3591 | ev.alert.type = "fatal"; |
| 3592 | ev.alert.description = "insufficient security"; |
| 3593 | context->event_cb(context->cb_ctx, TLS_ALERT, &ev); |
| 3594 | } |
| 3595 | /* |
| 3596 | * Could send a TLS Alert to the server, but for now, simply |
| 3597 | * terminate handshake. |
| 3598 | */ |
| 3599 | conn->failed++; |
| 3600 | conn->write_alerts++; |
| 3601 | return NULL; |
| 3602 | } |
| 3603 | #endif /* CONFIG_SUITEB */ |
| 3604 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3605 | /* Get the TLS handshake data to be sent to the server */ |
| 3606 | res = BIO_ctrl_pending(conn->ssl_out); |
| 3607 | wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res); |
| 3608 | out_data = wpabuf_alloc(res); |
| 3609 | if (out_data == NULL) { |
| 3610 | wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for " |
| 3611 | "handshake output (%d bytes)", res); |
| 3612 | if (BIO_reset(conn->ssl_out) < 0) { |
| 3613 | tls_show_errors(MSG_INFO, __func__, |
| 3614 | "BIO_reset failed"); |
| 3615 | } |
| 3616 | return NULL; |
| 3617 | } |
| 3618 | res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data), |
| 3619 | res); |
| 3620 | if (res < 0) { |
| 3621 | tls_show_errors(MSG_INFO, __func__, |
| 3622 | "Handshake failed - BIO_read"); |
| 3623 | if (BIO_reset(conn->ssl_out) < 0) { |
| 3624 | tls_show_errors(MSG_INFO, __func__, |
| 3625 | "BIO_reset failed"); |
| 3626 | } |
| 3627 | wpabuf_free(out_data); |
| 3628 | return NULL; |
| 3629 | } |
| 3630 | wpabuf_put(out_data, res); |
| 3631 | |
| 3632 | return out_data; |
| 3633 | } |
| 3634 | |
| 3635 | |
| 3636 | static struct wpabuf * |
| 3637 | openssl_get_appl_data(struct tls_connection *conn, size_t max_len) |
| 3638 | { |
| 3639 | struct wpabuf *appl_data; |
| 3640 | int res; |
| 3641 | |
| 3642 | appl_data = wpabuf_alloc(max_len + 100); |
| 3643 | if (appl_data == NULL) |
| 3644 | return NULL; |
| 3645 | |
| 3646 | res = SSL_read(conn->ssl, wpabuf_mhead(appl_data), |
| 3647 | wpabuf_size(appl_data)); |
| 3648 | if (res < 0) { |
| 3649 | int err = SSL_get_error(conn->ssl, res); |
| 3650 | if (err == SSL_ERROR_WANT_READ || |
| 3651 | err == SSL_ERROR_WANT_WRITE) { |
| 3652 | wpa_printf(MSG_DEBUG, "SSL: No Application Data " |
| 3653 | "included"); |
| 3654 | } else { |
| 3655 | tls_show_errors(MSG_INFO, __func__, |
| 3656 | "Failed to read possible " |
| 3657 | "Application Data"); |
| 3658 | } |
| 3659 | wpabuf_free(appl_data); |
| 3660 | return NULL; |
| 3661 | } |
| 3662 | |
| 3663 | wpabuf_put(appl_data, res); |
| 3664 | wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished " |
| 3665 | "message", appl_data); |
| 3666 | |
| 3667 | return appl_data; |
| 3668 | } |
| 3669 | |
| 3670 | |
| 3671 | static struct wpabuf * |
| 3672 | openssl_connection_handshake(struct tls_connection *conn, |
| 3673 | const struct wpabuf *in_data, |
| 3674 | struct wpabuf **appl_data, int server) |
| 3675 | { |
| 3676 | struct wpabuf *out_data; |
| 3677 | |
| 3678 | if (appl_data) |
| 3679 | *appl_data = NULL; |
| 3680 | |
| 3681 | out_data = openssl_handshake(conn, in_data, server); |
| 3682 | if (out_data == NULL) |
| 3683 | return NULL; |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 3684 | if (conn->invalid_hb_used) { |
| 3685 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); |
| 3686 | wpabuf_free(out_data); |
| 3687 | return NULL; |
| 3688 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3689 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3690 | if (SSL_is_init_finished(conn->ssl)) { |
| 3691 | wpa_printf(MSG_DEBUG, |
| 3692 | "OpenSSL: Handshake finished - resumed=%d", |
| 3693 | tls_connection_resumed(conn->ssl_ctx, conn)); |
| 3694 | if (appl_data && in_data) |
| 3695 | *appl_data = openssl_get_appl_data(conn, |
| 3696 | wpabuf_len(in_data)); |
| 3697 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3698 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 3699 | if (conn->invalid_hb_used) { |
| 3700 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); |
| 3701 | if (appl_data) { |
| 3702 | wpabuf_free(*appl_data); |
| 3703 | *appl_data = NULL; |
| 3704 | } |
| 3705 | wpabuf_free(out_data); |
| 3706 | return NULL; |
| 3707 | } |
| 3708 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3709 | return out_data; |
| 3710 | } |
| 3711 | |
| 3712 | |
| 3713 | struct wpabuf * |
| 3714 | tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn, |
| 3715 | const struct wpabuf *in_data, |
| 3716 | struct wpabuf **appl_data) |
| 3717 | { |
| 3718 | return openssl_connection_handshake(conn, in_data, appl_data, 0); |
| 3719 | } |
| 3720 | |
| 3721 | |
| 3722 | struct wpabuf * tls_connection_server_handshake(void *tls_ctx, |
| 3723 | struct tls_connection *conn, |
| 3724 | const struct wpabuf *in_data, |
| 3725 | struct wpabuf **appl_data) |
| 3726 | { |
| 3727 | return openssl_connection_handshake(conn, in_data, appl_data, 1); |
| 3728 | } |
| 3729 | |
| 3730 | |
| 3731 | struct wpabuf * tls_connection_encrypt(void *tls_ctx, |
| 3732 | struct tls_connection *conn, |
| 3733 | const struct wpabuf *in_data) |
| 3734 | { |
| 3735 | int res; |
| 3736 | struct wpabuf *buf; |
| 3737 | |
| 3738 | if (conn == NULL) |
| 3739 | return NULL; |
| 3740 | |
| 3741 | /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */ |
| 3742 | if ((res = BIO_reset(conn->ssl_in)) < 0 || |
| 3743 | (res = BIO_reset(conn->ssl_out)) < 0) { |
| 3744 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); |
| 3745 | return NULL; |
| 3746 | } |
| 3747 | res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data)); |
| 3748 | if (res < 0) { |
| 3749 | tls_show_errors(MSG_INFO, __func__, |
| 3750 | "Encryption failed - SSL_write"); |
| 3751 | return NULL; |
| 3752 | } |
| 3753 | |
| 3754 | /* Read encrypted data to be sent to the server */ |
| 3755 | buf = wpabuf_alloc(wpabuf_len(in_data) + 300); |
| 3756 | if (buf == NULL) |
| 3757 | return NULL; |
| 3758 | res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf)); |
| 3759 | if (res < 0) { |
| 3760 | tls_show_errors(MSG_INFO, __func__, |
| 3761 | "Encryption failed - BIO_read"); |
| 3762 | wpabuf_free(buf); |
| 3763 | return NULL; |
| 3764 | } |
| 3765 | wpabuf_put(buf, res); |
| 3766 | |
| 3767 | return buf; |
| 3768 | } |
| 3769 | |
| 3770 | |
| 3771 | struct wpabuf * tls_connection_decrypt(void *tls_ctx, |
| 3772 | struct tls_connection *conn, |
| 3773 | const struct wpabuf *in_data) |
| 3774 | { |
| 3775 | int res; |
| 3776 | struct wpabuf *buf; |
| 3777 | |
| 3778 | /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */ |
| 3779 | res = BIO_write(conn->ssl_in, wpabuf_head(in_data), |
| 3780 | wpabuf_len(in_data)); |
| 3781 | if (res < 0) { |
| 3782 | tls_show_errors(MSG_INFO, __func__, |
| 3783 | "Decryption failed - BIO_write"); |
| 3784 | return NULL; |
| 3785 | } |
| 3786 | if (BIO_reset(conn->ssl_out) < 0) { |
| 3787 | tls_show_errors(MSG_INFO, __func__, "BIO_reset failed"); |
| 3788 | return NULL; |
| 3789 | } |
| 3790 | |
| 3791 | /* Read decrypted data for further processing */ |
| 3792 | /* |
| 3793 | * Even though we try to disable TLS compression, it is possible that |
| 3794 | * this cannot be done with all TLS libraries. Add extra buffer space |
| 3795 | * to handle the possibility of the decrypted data being longer than |
| 3796 | * input data. |
| 3797 | */ |
| 3798 | buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3); |
| 3799 | if (buf == NULL) |
| 3800 | return NULL; |
| 3801 | res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf)); |
| 3802 | if (res < 0) { |
| 3803 | tls_show_errors(MSG_INFO, __func__, |
| 3804 | "Decryption failed - SSL_read"); |
| 3805 | wpabuf_free(buf); |
| 3806 | return NULL; |
| 3807 | } |
| 3808 | wpabuf_put(buf, res); |
| 3809 | |
Jouni Malinen | 26af48b | 2014-04-09 13:02:53 +0300 | [diff] [blame] | 3810 | if (conn->invalid_hb_used) { |
| 3811 | wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response"); |
| 3812 | wpabuf_free(buf); |
| 3813 | return NULL; |
| 3814 | } |
| 3815 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3816 | return buf; |
| 3817 | } |
| 3818 | |
| 3819 | |
| 3820 | int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn) |
| 3821 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3822 | return conn ? SSL_cache_hit(conn->ssl) : 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | |
| 3826 | int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn, |
| 3827 | u8 *ciphers) |
| 3828 | { |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 3829 | char buf[500], *pos, *end; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3830 | u8 *c; |
| 3831 | int ret; |
| 3832 | |
| 3833 | if (conn == NULL || conn->ssl == NULL || ciphers == NULL) |
| 3834 | return -1; |
| 3835 | |
| 3836 | buf[0] = '\0'; |
| 3837 | pos = buf; |
| 3838 | end = pos + sizeof(buf); |
| 3839 | |
| 3840 | c = ciphers; |
| 3841 | while (*c != TLS_CIPHER_NONE) { |
| 3842 | const char *suite; |
| 3843 | |
| 3844 | switch (*c) { |
| 3845 | case TLS_CIPHER_RC4_SHA: |
| 3846 | suite = "RC4-SHA"; |
| 3847 | break; |
| 3848 | case TLS_CIPHER_AES128_SHA: |
| 3849 | suite = "AES128-SHA"; |
| 3850 | break; |
| 3851 | case TLS_CIPHER_RSA_DHE_AES128_SHA: |
| 3852 | suite = "DHE-RSA-AES128-SHA"; |
| 3853 | break; |
| 3854 | case TLS_CIPHER_ANON_DH_AES128_SHA: |
| 3855 | suite = "ADH-AES128-SHA"; |
| 3856 | break; |
Dmitry Shmidt | de47be7 | 2016-01-07 12:52:55 -0800 | [diff] [blame] | 3857 | case TLS_CIPHER_RSA_DHE_AES256_SHA: |
| 3858 | suite = "DHE-RSA-AES256-SHA"; |
| 3859 | break; |
| 3860 | case TLS_CIPHER_AES256_SHA: |
| 3861 | suite = "AES256-SHA"; |
| 3862 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3863 | default: |
| 3864 | wpa_printf(MSG_DEBUG, "TLS: Unsupported " |
| 3865 | "cipher selection: %d", *c); |
| 3866 | return -1; |
| 3867 | } |
| 3868 | ret = os_snprintf(pos, end - pos, ":%s", suite); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3869 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3870 | break; |
| 3871 | pos += ret; |
| 3872 | |
| 3873 | c++; |
| 3874 | } |
| 3875 | |
| 3876 | wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1); |
| 3877 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 3878 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3879 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
| 3880 | if (os_strstr(buf, ":ADH-")) { |
| 3881 | /* |
| 3882 | * Need to drop to security level 0 to allow anonymous |
| 3883 | * cipher suites for EAP-FAST. |
| 3884 | */ |
| 3885 | SSL_set_security_level(conn->ssl, 0); |
| 3886 | } else if (SSL_get_security_level(conn->ssl) == 0) { |
| 3887 | /* Force at least security level 1 */ |
| 3888 | SSL_set_security_level(conn->ssl, 1); |
| 3889 | } |
| 3890 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
| 3891 | #endif |
| 3892 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3893 | if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) { |
| 3894 | tls_show_errors(MSG_INFO, __func__, |
| 3895 | "Cipher suite configuration failed"); |
| 3896 | return -1; |
| 3897 | } |
| 3898 | |
| 3899 | return 0; |
| 3900 | } |
| 3901 | |
| 3902 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3903 | int tls_get_version(void *ssl_ctx, struct tls_connection *conn, |
| 3904 | char *buf, size_t buflen) |
| 3905 | { |
| 3906 | const char *name; |
| 3907 | if (conn == NULL || conn->ssl == NULL) |
| 3908 | return -1; |
| 3909 | |
| 3910 | name = SSL_get_version(conn->ssl); |
| 3911 | if (name == NULL) |
| 3912 | return -1; |
| 3913 | |
| 3914 | os_strlcpy(buf, name, buflen); |
| 3915 | return 0; |
| 3916 | } |
| 3917 | |
| 3918 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3919 | int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn, |
| 3920 | char *buf, size_t buflen) |
| 3921 | { |
| 3922 | const char *name; |
| 3923 | if (conn == NULL || conn->ssl == NULL) |
| 3924 | return -1; |
| 3925 | |
| 3926 | name = SSL_get_cipher(conn->ssl); |
| 3927 | if (name == NULL) |
| 3928 | return -1; |
| 3929 | |
| 3930 | os_strlcpy(buf, name, buflen); |
| 3931 | return 0; |
| 3932 | } |
| 3933 | |
| 3934 | |
| 3935 | int tls_connection_enable_workaround(void *ssl_ctx, |
| 3936 | struct tls_connection *conn) |
| 3937 | { |
| 3938 | SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); |
| 3939 | |
| 3940 | return 0; |
| 3941 | } |
| 3942 | |
| 3943 | |
| 3944 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
| 3945 | /* ClientHello TLS extensions require a patch to openssl, so this function is |
| 3946 | * commented out unless explicitly needed for EAP-FAST in order to be able to |
| 3947 | * build this file with unmodified openssl. */ |
| 3948 | int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn, |
| 3949 | int ext_type, const u8 *data, |
| 3950 | size_t data_len) |
| 3951 | { |
| 3952 | if (conn == NULL || conn->ssl == NULL || ext_type != 35) |
| 3953 | return -1; |
| 3954 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3955 | if (SSL_set_session_ticket_ext(conn->ssl, (void *) data, |
| 3956 | data_len) != 1) |
| 3957 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3958 | |
| 3959 | return 0; |
| 3960 | } |
| 3961 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
| 3962 | |
| 3963 | |
| 3964 | int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn) |
| 3965 | { |
| 3966 | if (conn == NULL) |
| 3967 | return -1; |
| 3968 | return conn->failed; |
| 3969 | } |
| 3970 | |
| 3971 | |
| 3972 | int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn) |
| 3973 | { |
| 3974 | if (conn == NULL) |
| 3975 | return -1; |
| 3976 | return conn->read_alerts; |
| 3977 | } |
| 3978 | |
| 3979 | |
| 3980 | int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn) |
| 3981 | { |
| 3982 | if (conn == NULL) |
| 3983 | return -1; |
| 3984 | return conn->write_alerts; |
| 3985 | } |
| 3986 | |
| 3987 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3988 | #ifdef HAVE_OCSP |
| 3989 | |
| 3990 | static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp) |
| 3991 | { |
| 3992 | #ifndef CONFIG_NO_STDOUT_DEBUG |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3993 | BIO *out; |
| 3994 | size_t rlen; |
| 3995 | char *txt; |
| 3996 | int res; |
| 3997 | |
| 3998 | if (wpa_debug_level > MSG_DEBUG) |
| 3999 | return; |
| 4000 | |
| 4001 | out = BIO_new(BIO_s_mem()); |
| 4002 | if (!out) |
| 4003 | return; |
| 4004 | |
| 4005 | OCSP_RESPONSE_print(out, rsp, 0); |
| 4006 | rlen = BIO_ctrl_pending(out); |
| 4007 | txt = os_malloc(rlen + 1); |
| 4008 | if (!txt) { |
| 4009 | BIO_free(out); |
| 4010 | return; |
| 4011 | } |
| 4012 | |
| 4013 | res = BIO_read(out, txt, rlen); |
| 4014 | if (res > 0) { |
| 4015 | txt[res] = '\0'; |
| 4016 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt); |
| 4017 | } |
| 4018 | os_free(txt); |
| 4019 | BIO_free(out); |
| 4020 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 4021 | } |
| 4022 | |
| 4023 | |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 4024 | static void debug_print_cert(X509 *cert, const char *title) |
| 4025 | { |
| 4026 | #ifndef CONFIG_NO_STDOUT_DEBUG |
| 4027 | BIO *out; |
| 4028 | size_t rlen; |
| 4029 | char *txt; |
| 4030 | int res; |
| 4031 | |
| 4032 | if (wpa_debug_level > MSG_DEBUG) |
| 4033 | return; |
| 4034 | |
| 4035 | out = BIO_new(BIO_s_mem()); |
| 4036 | if (!out) |
| 4037 | return; |
| 4038 | |
| 4039 | X509_print(out, cert); |
| 4040 | rlen = BIO_ctrl_pending(out); |
| 4041 | txt = os_malloc(rlen + 1); |
| 4042 | if (!txt) { |
| 4043 | BIO_free(out); |
| 4044 | return; |
| 4045 | } |
| 4046 | |
| 4047 | res = BIO_read(out, txt, rlen); |
| 4048 | if (res > 0) { |
| 4049 | txt[res] = '\0'; |
| 4050 | wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt); |
| 4051 | } |
| 4052 | os_free(txt); |
| 4053 | |
| 4054 | BIO_free(out); |
| 4055 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 4056 | } |
| 4057 | |
| 4058 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4059 | static int ocsp_resp_cb(SSL *s, void *arg) |
| 4060 | { |
| 4061 | struct tls_connection *conn = arg; |
| 4062 | const unsigned char *p; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 4063 | int len, status, reason, res; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4064 | OCSP_RESPONSE *rsp; |
| 4065 | OCSP_BASICRESP *basic; |
| 4066 | OCSP_CERTID *id; |
| 4067 | ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4068 | X509_STORE *store; |
| 4069 | STACK_OF(X509) *certs = NULL; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4070 | |
| 4071 | len = SSL_get_tlsext_status_ocsp_resp(s, &p); |
| 4072 | if (!p) { |
| 4073 | wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received"); |
| 4074 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; |
| 4075 | } |
| 4076 | |
| 4077 | wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len); |
| 4078 | |
| 4079 | rsp = d2i_OCSP_RESPONSE(NULL, &p, len); |
| 4080 | if (!rsp) { |
| 4081 | wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response"); |
| 4082 | return 0; |
| 4083 | } |
| 4084 | |
| 4085 | ocsp_debug_print_resp(rsp); |
| 4086 | |
| 4087 | status = OCSP_response_status(rsp); |
| 4088 | if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 4089 | wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)", |
| 4090 | status, OCSP_response_status_str(status)); |
| 4091 | return 0; |
| 4092 | } |
| 4093 | |
| 4094 | basic = OCSP_response_get1_basic(rsp); |
| 4095 | if (!basic) { |
| 4096 | wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse"); |
| 4097 | return 0; |
| 4098 | } |
| 4099 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4100 | store = SSL_CTX_get_cert_store(conn->ssl_ctx); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4101 | if (conn->peer_issuer) { |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 4102 | debug_print_cert(conn->peer_issuer, "Add OCSP issuer"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4103 | |
| 4104 | if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) { |
| 4105 | tls_show_errors(MSG_INFO, __func__, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4106 | "OpenSSL: Could not add issuer to certificate store"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4107 | } |
| 4108 | certs = sk_X509_new_null(); |
| 4109 | if (certs) { |
| 4110 | X509 *cert; |
| 4111 | cert = X509_dup(conn->peer_issuer); |
| 4112 | if (cert && !sk_X509_push(certs, cert)) { |
| 4113 | tls_show_errors( |
| 4114 | MSG_INFO, __func__, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4115 | "OpenSSL: Could not add issuer to OCSP responder trust store"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4116 | X509_free(cert); |
| 4117 | sk_X509_free(certs); |
| 4118 | certs = NULL; |
| 4119 | } |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4120 | if (certs && conn->peer_issuer_issuer) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4121 | cert = X509_dup(conn->peer_issuer_issuer); |
| 4122 | if (cert && !sk_X509_push(certs, cert)) { |
| 4123 | tls_show_errors( |
| 4124 | MSG_INFO, __func__, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4125 | "OpenSSL: Could not add issuer's issuer to OCSP responder trust store"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4126 | X509_free(cert); |
| 4127 | } |
| 4128 | } |
| 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER); |
| 4133 | sk_X509_pop_free(certs, X509_free); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4134 | if (status <= 0) { |
| 4135 | tls_show_errors(MSG_INFO, __func__, |
| 4136 | "OpenSSL: OCSP response failed verification"); |
| 4137 | OCSP_BASICRESP_free(basic); |
| 4138 | OCSP_RESPONSE_free(rsp); |
| 4139 | return 0; |
| 4140 | } |
| 4141 | |
| 4142 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded"); |
| 4143 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4144 | if (!conn->peer_cert) { |
| 4145 | wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check"); |
| 4146 | OCSP_BASICRESP_free(basic); |
| 4147 | OCSP_RESPONSE_free(rsp); |
| 4148 | return 0; |
| 4149 | } |
| 4150 | |
| 4151 | if (!conn->peer_issuer) { |
| 4152 | 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] | 4153 | OCSP_BASICRESP_free(basic); |
| 4154 | OCSP_RESPONSE_free(rsp); |
| 4155 | return 0; |
| 4156 | } |
| 4157 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 4158 | 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] | 4159 | if (!id) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 4160 | wpa_printf(MSG_DEBUG, |
| 4161 | "OpenSSL: Could not create OCSP certificate identifier (SHA256)"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4162 | OCSP_BASICRESP_free(basic); |
| 4163 | OCSP_RESPONSE_free(rsp); |
| 4164 | return 0; |
| 4165 | } |
| 4166 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 4167 | res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at, |
| 4168 | &this_update, &next_update); |
| 4169 | if (!res) { |
| 4170 | id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer); |
| 4171 | if (!id) { |
| 4172 | wpa_printf(MSG_DEBUG, |
| 4173 | "OpenSSL: Could not create OCSP certificate identifier (SHA1)"); |
| 4174 | OCSP_BASICRESP_free(basic); |
| 4175 | OCSP_RESPONSE_free(rsp); |
| 4176 | return 0; |
| 4177 | } |
| 4178 | |
| 4179 | res = OCSP_resp_find_status(basic, id, &status, &reason, |
| 4180 | &produced_at, &this_update, |
| 4181 | &next_update); |
| 4182 | } |
| 4183 | |
| 4184 | if (!res) { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4185 | wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s", |
| 4186 | (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" : |
| 4187 | " (OCSP not required)"); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 4188 | OCSP_CERTID_free(id); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4189 | OCSP_BASICRESP_free(basic); |
| 4190 | OCSP_RESPONSE_free(rsp); |
| 4191 | return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1; |
| 4192 | } |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 4193 | OCSP_CERTID_free(id); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4194 | |
| 4195 | if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) { |
| 4196 | tls_show_errors(MSG_INFO, __func__, |
| 4197 | "OpenSSL: OCSP status times invalid"); |
| 4198 | OCSP_BASICRESP_free(basic); |
| 4199 | OCSP_RESPONSE_free(rsp); |
| 4200 | return 0; |
| 4201 | } |
| 4202 | |
| 4203 | OCSP_BASICRESP_free(basic); |
| 4204 | OCSP_RESPONSE_free(rsp); |
| 4205 | |
| 4206 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s", |
| 4207 | OCSP_cert_status_str(status)); |
| 4208 | |
| 4209 | if (status == V_OCSP_CERTSTATUS_GOOD) |
| 4210 | return 1; |
| 4211 | if (status == V_OCSP_CERTSTATUS_REVOKED) |
| 4212 | return 0; |
| 4213 | if (conn->flags & TLS_CONN_REQUIRE_OCSP) { |
| 4214 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required"); |
| 4215 | return 0; |
| 4216 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4217 | 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] | 4218 | return 1; |
| 4219 | } |
| 4220 | |
| 4221 | |
| 4222 | static int ocsp_status_cb(SSL *s, void *arg) |
| 4223 | { |
| 4224 | char *tmp; |
| 4225 | char *resp; |
| 4226 | size_t len; |
| 4227 | |
| 4228 | if (tls_global->ocsp_stapling_response == NULL) { |
| 4229 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured"); |
| 4230 | return SSL_TLSEXT_ERR_OK; |
| 4231 | } |
| 4232 | |
| 4233 | resp = os_readfile(tls_global->ocsp_stapling_response, &len); |
| 4234 | if (resp == NULL) { |
| 4235 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file"); |
| 4236 | /* TODO: Build OCSPResponse with responseStatus = internalError |
| 4237 | */ |
| 4238 | return SSL_TLSEXT_ERR_OK; |
| 4239 | } |
| 4240 | wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response"); |
| 4241 | tmp = OPENSSL_malloc(len); |
| 4242 | if (tmp == NULL) { |
| 4243 | os_free(resp); |
| 4244 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 4245 | } |
| 4246 | |
| 4247 | os_memcpy(tmp, resp, len); |
| 4248 | os_free(resp); |
| 4249 | SSL_set_tlsext_status_ocsp_resp(s, tmp, len); |
| 4250 | |
| 4251 | return SSL_TLSEXT_ERR_OK; |
| 4252 | } |
| 4253 | |
| 4254 | #endif /* HAVE_OCSP */ |
| 4255 | |
| 4256 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4257 | int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, |
| 4258 | const struct tls_connection_params *params) |
| 4259 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4260 | struct tls_data *data = tls_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4261 | int ret; |
| 4262 | unsigned long err; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4263 | int can_pkcs11 = 0; |
| 4264 | const char *key_id = params->key_id; |
| 4265 | const char *cert_id = params->cert_id; |
| 4266 | const char *ca_cert_id = params->ca_cert_id; |
| 4267 | const char *engine_id = params->engine ? params->engine_id : NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4268 | |
| 4269 | if (conn == NULL) |
| 4270 | return -1; |
| 4271 | |
Dmitry Shmidt | 014a3ff | 2015-12-28 13:27:49 -0800 | [diff] [blame] | 4272 | if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) { |
| 4273 | wpa_printf(MSG_INFO, |
| 4274 | "OpenSSL: ocsp=3 not supported"); |
| 4275 | return -1; |
| 4276 | } |
| 4277 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4278 | /* |
| 4279 | * If the engine isn't explicitly configured, and any of the |
| 4280 | * cert/key fields are actually PKCS#11 URIs, then automatically |
| 4281 | * use the PKCS#11 ENGINE. |
| 4282 | */ |
| 4283 | if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0) |
| 4284 | can_pkcs11 = 1; |
| 4285 | |
| 4286 | if (!key_id && params->private_key && can_pkcs11 && |
| 4287 | os_strncmp(params->private_key, "pkcs11:", 7) == 0) { |
| 4288 | can_pkcs11 = 2; |
| 4289 | key_id = params->private_key; |
| 4290 | } |
| 4291 | |
| 4292 | if (!cert_id && params->client_cert && can_pkcs11 && |
| 4293 | os_strncmp(params->client_cert, "pkcs11:", 7) == 0) { |
| 4294 | can_pkcs11 = 2; |
| 4295 | cert_id = params->client_cert; |
| 4296 | } |
| 4297 | |
| 4298 | if (!ca_cert_id && params->ca_cert && can_pkcs11 && |
| 4299 | os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) { |
| 4300 | can_pkcs11 = 2; |
| 4301 | ca_cert_id = params->ca_cert; |
| 4302 | } |
| 4303 | |
| 4304 | /* If we need to automatically enable the PKCS#11 ENGINE, do so. */ |
| 4305 | if (can_pkcs11 == 2 && !engine_id) |
| 4306 | engine_id = "pkcs11"; |
| 4307 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4308 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4309 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4310 | if (params->flags & TLS_CONN_EAP_FAST) { |
| 4311 | wpa_printf(MSG_DEBUG, |
| 4312 | "OpenSSL: Use TLSv1_method() for EAP-FAST"); |
| 4313 | if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) { |
| 4314 | tls_show_errors(MSG_INFO, __func__, |
| 4315 | "Failed to set TLSv1_method() for EAP-FAST"); |
| 4316 | return -1; |
| 4317 | } |
| 4318 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4319 | #endif |
| 4320 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4321 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4322 | while ((err = ERR_get_error())) { |
| 4323 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", |
| 4324 | __func__, ERR_error_string(err, NULL)); |
| 4325 | } |
| 4326 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4327 | if (engine_id) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4328 | wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4329 | ret = tls_engine_init(conn, engine_id, params->pin, |
| 4330 | key_id, cert_id, ca_cert_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4331 | if (ret) |
| 4332 | return ret; |
| 4333 | } |
| 4334 | if (tls_connection_set_subject_match(conn, |
| 4335 | params->subject_match, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4336 | params->altsubject_match, |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 4337 | params->suffix_match, |
| 4338 | params->domain_match)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4339 | return -1; |
| 4340 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4341 | if (engine_id && ca_cert_id) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4342 | if (tls_connection_engine_ca_cert(data, conn, ca_cert_id)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4343 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4344 | } else if (tls_connection_ca_cert(data, conn, params->ca_cert, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4345 | params->ca_cert_blob, |
| 4346 | params->ca_cert_blob_len, |
| 4347 | params->ca_path)) |
| 4348 | return -1; |
| 4349 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4350 | if (engine_id && cert_id) { |
| 4351 | if (tls_connection_engine_client_cert(conn, cert_id)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4352 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
| 4353 | } else if (tls_connection_client_cert(conn, params->client_cert, |
| 4354 | params->client_cert_blob, |
| 4355 | params->client_cert_blob_len)) |
| 4356 | return -1; |
| 4357 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4358 | if (engine_id && key_id) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4359 | wpa_printf(MSG_DEBUG, "TLS: Using private key from engine"); |
| 4360 | if (tls_connection_engine_private_key(conn)) |
| 4361 | return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4362 | } else if (tls_connection_private_key(data, conn, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4363 | params->private_key, |
| 4364 | params->private_key_passwd, |
| 4365 | params->private_key_blob, |
| 4366 | params->private_key_blob_len)) { |
| 4367 | wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'", |
| 4368 | params->private_key); |
| 4369 | return -1; |
| 4370 | } |
| 4371 | |
| 4372 | if (tls_connection_dh(conn, params->dh_file)) { |
| 4373 | wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'", |
| 4374 | params->dh_file); |
| 4375 | return -1; |
| 4376 | } |
| 4377 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4378 | if (params->openssl_ciphers && |
| 4379 | SSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) { |
| 4380 | wpa_printf(MSG_INFO, |
| 4381 | "OpenSSL: Failed to set cipher string '%s'", |
| 4382 | params->openssl_ciphers); |
| 4383 | return -1; |
| 4384 | } |
| 4385 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 4386 | if (tls_set_conn_flags(conn, params->flags) < 0) |
| 4387 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4388 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4389 | #ifdef OPENSSL_IS_BORINGSSL |
| 4390 | if (params->flags & TLS_CONN_REQUEST_OCSP) { |
| 4391 | SSL_enable_ocsp_stapling(conn->ssl); |
| 4392 | } |
| 4393 | #else /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4394 | #ifdef HAVE_OCSP |
| 4395 | if (params->flags & TLS_CONN_REQUEST_OCSP) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4396 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4397 | SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp); |
| 4398 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb); |
| 4399 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn); |
| 4400 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4401 | #else /* HAVE_OCSP */ |
| 4402 | if (params->flags & TLS_CONN_REQUIRE_OCSP) { |
| 4403 | wpa_printf(MSG_INFO, |
| 4404 | "OpenSSL: No OCSP support included - reject configuration"); |
| 4405 | return -1; |
| 4406 | } |
| 4407 | if (params->flags & TLS_CONN_REQUEST_OCSP) { |
| 4408 | wpa_printf(MSG_DEBUG, |
| 4409 | "OpenSSL: No OCSP support included - allow optional OCSP case to continue"); |
| 4410 | } |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4411 | #endif /* HAVE_OCSP */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4412 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4413 | |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 4414 | conn->flags = params->flags; |
| 4415 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4416 | tls_get_errors(data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4417 | |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | |
| 4422 | int tls_global_set_params(void *tls_ctx, |
| 4423 | const struct tls_connection_params *params) |
| 4424 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4425 | struct tls_data *data = tls_ctx; |
| 4426 | SSL_CTX *ssl_ctx = data->ssl; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4427 | unsigned long err; |
| 4428 | |
| 4429 | while ((err = ERR_get_error())) { |
| 4430 | wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s", |
| 4431 | __func__, ERR_error_string(err, NULL)); |
| 4432 | } |
| 4433 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4434 | if (tls_global_ca_cert(data, params->ca_cert) || |
| 4435 | tls_global_client_cert(data, params->client_cert) || |
| 4436 | tls_global_private_key(data, params->private_key, |
| 4437 | params->private_key_passwd) || |
| 4438 | tls_global_dh(data, params->dh_file)) { |
| 4439 | wpa_printf(MSG_INFO, "TLS: Failed to set global parameters"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4440 | return -1; |
| 4441 | } |
| 4442 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4443 | if (params->openssl_ciphers && |
| 4444 | SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) { |
| 4445 | wpa_printf(MSG_INFO, |
| 4446 | "OpenSSL: Failed to set cipher string '%s'", |
| 4447 | params->openssl_ciphers); |
| 4448 | return -1; |
| 4449 | } |
| 4450 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4451 | #ifdef SSL_OP_NO_TICKET |
| 4452 | if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET) |
| 4453 | SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET); |
| 4454 | else |
| 4455 | SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET); |
| 4456 | #endif /* SSL_OP_NO_TICKET */ |
| 4457 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4458 | #ifdef HAVE_OCSP |
| 4459 | SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb); |
| 4460 | SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx); |
| 4461 | os_free(tls_global->ocsp_stapling_response); |
| 4462 | if (params->ocsp_stapling_response) |
| 4463 | tls_global->ocsp_stapling_response = |
| 4464 | os_strdup(params->ocsp_stapling_response); |
| 4465 | else |
| 4466 | tls_global->ocsp_stapling_response = NULL; |
| 4467 | #endif /* HAVE_OCSP */ |
| 4468 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4469 | return 0; |
| 4470 | } |
| 4471 | |
| 4472 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4473 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
| 4474 | /* Pre-shared secred requires a patch to openssl, so this function is |
| 4475 | * commented out unless explicitly needed for EAP-FAST in order to be able to |
| 4476 | * build this file with unmodified openssl. */ |
| 4477 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 4478 | #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] | 4479 | static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len, |
| 4480 | STACK_OF(SSL_CIPHER) *peer_ciphers, |
| 4481 | const SSL_CIPHER **cipher, void *arg) |
| 4482 | #else /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4483 | static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len, |
| 4484 | STACK_OF(SSL_CIPHER) *peer_ciphers, |
| 4485 | SSL_CIPHER **cipher, void *arg) |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 4486 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4487 | { |
| 4488 | struct tls_connection *conn = arg; |
| 4489 | int ret; |
| 4490 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4491 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4492 | if (conn == NULL || conn->session_ticket_cb == NULL) |
| 4493 | return 0; |
| 4494 | |
| 4495 | ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx, |
| 4496 | conn->session_ticket, |
| 4497 | conn->session_ticket_len, |
| 4498 | s->s3->client_random, |
| 4499 | s->s3->server_random, secret); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4500 | #else |
| 4501 | unsigned char client_random[SSL3_RANDOM_SIZE]; |
| 4502 | unsigned char server_random[SSL3_RANDOM_SIZE]; |
| 4503 | |
| 4504 | if (conn == NULL || conn->session_ticket_cb == NULL) |
| 4505 | return 0; |
| 4506 | |
| 4507 | SSL_get_client_random(s, client_random, sizeof(client_random)); |
| 4508 | SSL_get_server_random(s, server_random, sizeof(server_random)); |
| 4509 | |
| 4510 | ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx, |
| 4511 | conn->session_ticket, |
| 4512 | conn->session_ticket_len, |
| 4513 | client_random, |
| 4514 | server_random, secret); |
| 4515 | #endif |
| 4516 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4517 | os_free(conn->session_ticket); |
| 4518 | conn->session_ticket = NULL; |
| 4519 | |
| 4520 | if (ret <= 0) |
| 4521 | return 0; |
| 4522 | |
| 4523 | *secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 4524 | return 1; |
| 4525 | } |
| 4526 | |
| 4527 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4528 | static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data, |
| 4529 | int len, void *arg) |
| 4530 | { |
| 4531 | struct tls_connection *conn = arg; |
| 4532 | |
| 4533 | if (conn == NULL || conn->session_ticket_cb == NULL) |
| 4534 | return 0; |
| 4535 | |
| 4536 | wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len); |
| 4537 | |
| 4538 | os_free(conn->session_ticket); |
| 4539 | conn->session_ticket = NULL; |
| 4540 | |
| 4541 | wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket " |
| 4542 | "extension", data, len); |
| 4543 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 4544 | conn->session_ticket = os_memdup(data, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4545 | if (conn->session_ticket == NULL) |
| 4546 | return 0; |
| 4547 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4548 | conn->session_ticket_len = len; |
| 4549 | |
| 4550 | return 1; |
| 4551 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4552 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
| 4553 | |
| 4554 | |
| 4555 | int tls_connection_set_session_ticket_cb(void *tls_ctx, |
| 4556 | struct tls_connection *conn, |
| 4557 | tls_session_ticket_cb cb, |
| 4558 | void *ctx) |
| 4559 | { |
| 4560 | #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) |
| 4561 | conn->session_ticket_cb = cb; |
| 4562 | conn->session_ticket_cb_ctx = ctx; |
| 4563 | |
| 4564 | if (cb) { |
| 4565 | if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb, |
| 4566 | conn) != 1) |
| 4567 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4568 | SSL_set_session_ticket_ext_cb(conn->ssl, |
| 4569 | tls_session_ticket_ext_cb, conn); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4570 | } else { |
| 4571 | if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1) |
| 4572 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4573 | SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | return 0; |
| 4577 | #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
| 4578 | return -1; |
| 4579 | #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */ |
| 4580 | } |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 4581 | |
| 4582 | |
| 4583 | int tls_get_library_version(char *buf, size_t buf_len) |
| 4584 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 4585 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4586 | return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s", |
| 4587 | OPENSSL_VERSION_TEXT, |
| 4588 | OpenSSL_version(OPENSSL_VERSION)); |
| 4589 | #else |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 4590 | return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s", |
| 4591 | OPENSSL_VERSION_TEXT, |
| 4592 | SSLeay_version(SSLEAY_VERSION)); |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 4593 | #endif |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 4594 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4595 | |
| 4596 | |
| 4597 | void tls_connection_set_success_data(struct tls_connection *conn, |
| 4598 | struct wpabuf *data) |
| 4599 | { |
| 4600 | SSL_SESSION *sess; |
| 4601 | struct wpabuf *old; |
| 4602 | |
| 4603 | if (tls_ex_idx_session < 0) |
| 4604 | goto fail; |
| 4605 | sess = SSL_get_session(conn->ssl); |
| 4606 | if (!sess) |
| 4607 | goto fail; |
| 4608 | old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); |
| 4609 | if (old) { |
| 4610 | wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p", |
| 4611 | old); |
| 4612 | wpabuf_free(old); |
| 4613 | } |
| 4614 | if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1) |
| 4615 | goto fail; |
| 4616 | |
| 4617 | wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data); |
| 4618 | conn->success_data = 1; |
| 4619 | return; |
| 4620 | |
| 4621 | fail: |
| 4622 | wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data"); |
| 4623 | wpabuf_free(data); |
| 4624 | } |
| 4625 | |
| 4626 | |
| 4627 | void tls_connection_set_success_data_resumed(struct tls_connection *conn) |
| 4628 | { |
| 4629 | wpa_printf(MSG_DEBUG, |
| 4630 | "OpenSSL: Success data accepted for resumed session"); |
| 4631 | conn->success_data = 1; |
| 4632 | } |
| 4633 | |
| 4634 | |
| 4635 | const struct wpabuf * |
| 4636 | tls_connection_get_success_data(struct tls_connection *conn) |
| 4637 | { |
| 4638 | SSL_SESSION *sess; |
| 4639 | |
| 4640 | if (tls_ex_idx_session < 0 || |
| 4641 | !(sess = SSL_get_session(conn->ssl))) |
| 4642 | return NULL; |
| 4643 | return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session); |
| 4644 | } |
| 4645 | |
| 4646 | |
| 4647 | void tls_connection_remove_session(struct tls_connection *conn) |
| 4648 | { |
| 4649 | SSL_SESSION *sess; |
| 4650 | |
| 4651 | sess = SSL_get_session(conn->ssl); |
| 4652 | if (!sess) |
| 4653 | return; |
| 4654 | |
| 4655 | if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1) |
| 4656 | wpa_printf(MSG_DEBUG, |
| 4657 | "OpenSSL: Session was not cached"); |
| 4658 | else |
| 4659 | wpa_printf(MSG_DEBUG, |
| 4660 | "OpenSSL: Removed cached session to disable session resumption"); |
| 4661 | } |