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