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