blob: 095c09631e4ef6c5b4ab043c245672b928b0ae15 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * SSL/TLS interface functions for OpenSSL
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#ifndef CONFIG_SMARTCARD
12#ifndef OPENSSL_NO_ENGINE
Kenny Rootdb3c5a42012-03-20 17:00:47 -070013#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#define OPENSSL_NO_ENGINE
15#endif
16#endif
Kenny Rootdb3c5a42012-03-20 17:00:47 -070017#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018
19#include <openssl/ssl.h>
20#include <openssl/err.h>
Dmitry Shmidt849734c2016-05-27 09:59:01 -070021#include <openssl/opensslv.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <openssl/pkcs12.h>
23#include <openssl/x509v3.h>
24#ifndef OPENSSL_NO_ENGINE
25#include <openssl/engine.h>
26#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080027#ifndef OPENSSL_NO_DSA
28#include <openssl/dsa.h>
29#endif
30#ifndef OPENSSL_NO_DH
31#include <openssl/dh.h>
32#endif
33
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "common.h"
35#include "crypto.h"
Dmitry Shmidtaf9da312015-04-03 10:03:11 -070036#include "sha1.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080037#include "sha256.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "tls.h"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080039#include "tls_openssl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
Dmitry Shmidt849734c2016-05-27 09:59:01 -070041#if !defined(CONFIG_FIPS) && \
42 (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
43 defined(EAP_SERVER_FAST))
44#define OPENSSL_NEED_EAP_FAST_PRF
45#endif
46
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070047#if defined(OPENSSL_IS_BORINGSSL)
48/* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
49typedef size_t stack_index_t;
50#else
51typedef int stack_index_t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052#endif
53
Dmitry Shmidt34af3062013-07-11 10:46:32 -070054#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 Shmidt849734c2016-05-27 09:59:01 -070061#if (OPENSSL_VERSION_NUMBER < 0x10100000L || \
Roshan Pius3a1667e2018-07-03 15:17:14 -070062 (defined(LIBRESSL_VERSION_NUMBER) && \
63 LIBRESSL_VERSION_NUMBER < 0x20700000L)) && \
Dmitry Shmidt849734c2016-05-27 09:59:01 -070064 !defined(BORINGSSL_API_VERSION)
Dmitry Shmidtde47be72016-01-07 12:52:55 -080065/*
66 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
Dmitry Shmidt849734c2016-05-27 09:59:01 -070067 * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for
68 * older versions.
Dmitry Shmidtde47be72016-01-07 12:52:55 -080069 */
70
71static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out,
72 size_t outlen)
73{
74 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
75 return 0;
76 os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE);
77 return SSL3_RANDOM_SIZE;
78}
79
80
81static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out,
82 size_t outlen)
83{
84 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
85 return 0;
86 os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE);
87 return SSL3_RANDOM_SIZE;
88}
89
90
Dmitry Shmidt849734c2016-05-27 09:59:01 -070091#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtde47be72016-01-07 12:52:55 -080092static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
93 unsigned char *out, size_t outlen)
94{
95 if (!session || session->master_key_length < 0 ||
96 (size_t) session->master_key_length > outlen)
97 return 0;
98 if ((size_t) session->master_key_length < outlen)
99 outlen = session->master_key_length;
100 os_memcpy(out, session->master_key, outlen);
101 return outlen;
102}
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700103#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800104
105#endif
106
Hai Shalom74f70d42019-02-11 14:42:39 -0800107#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
108 (defined(LIBRESSL_VERSION_NUMBER) && \
109 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700110#ifdef CONFIG_SUITEB
111static int RSA_bits(const RSA *r)
112{
113 return BN_num_bits(r->n);
114}
115#endif /* CONFIG_SUITEB */
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800116
117
118static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
119{
120 return ASN1_STRING_data((ASN1_STRING *) x);
121}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700122#endif
123
Dmitry Shmidtff079172013-11-08 14:10:30 -0800124#ifdef ANDROID
125#include <openssl/pem.h>
126#include <keystore/keystore_get.h>
127
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000128#include <log/log.h>
129#include <log/log_event_list.h>
130
131#define CERT_VALIDATION_FAILURE 210033
132
133static void log_cert_validation_failure(const char *reason)
134{
135 android_log_context ctx = create_android_logger(CERT_VALIDATION_FAILURE);
136 android_log_write_string8(ctx, reason);
137 android_log_write_list(ctx, LOG_ID_SECURITY);
138 android_log_destroy(&ctx);
139}
140
141
Dmitry Shmidtff079172013-11-08 14:10:30 -0800142static BIO * BIO_from_keystore(const char *key)
143{
144 BIO *bio = NULL;
145 uint8_t *value = NULL;
146 int length = keystore_get(key, strlen(key), &value);
147 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
148 BIO_write(bio, value, length);
149 free(value);
150 return bio;
151}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800152
153
154static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *key_alias)
155{
156 BIO *bio = BIO_from_keystore(key_alias);
157 STACK_OF(X509_INFO) *stack = NULL;
158 stack_index_t i;
159
160 if (bio) {
161 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
162 BIO_free(bio);
163 }
164
165 if (!stack) {
166 wpa_printf(MSG_WARNING, "TLS: Failed to parse certificate: %s",
167 key_alias);
168 return -1;
169 }
170
171 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
172 X509_INFO *info = sk_X509_INFO_value(stack, i);
173
174 if (info->x509)
175 X509_STORE_add_cert(ctx, info->x509);
176 if (info->crl)
177 X509_STORE_add_crl(ctx, info->crl);
178 }
179
180 sk_X509_INFO_pop_free(stack, X509_INFO_free);
181
182 return 0;
183}
184
185
186static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
187 const char *encoded_key_alias)
188{
189 int rc = -1;
190 int len = os_strlen(encoded_key_alias);
191 unsigned char *decoded_alias;
192
193 if (len & 1) {
194 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
195 encoded_key_alias);
196 return rc;
197 }
198
199 decoded_alias = os_malloc(len / 2 + 1);
200 if (decoded_alias) {
201 if (!hexstr2bin(encoded_key_alias, decoded_alias, len / 2)) {
202 decoded_alias[len / 2] = '\0';
203 rc = tls_add_ca_from_keystore(
204 ctx, (const char *) decoded_alias);
205 }
206 os_free(decoded_alias);
207 }
208
209 return rc;
210}
211
Dmitry Shmidtff079172013-11-08 14:10:30 -0800212#endif /* ANDROID */
213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214static int tls_openssl_ref_count = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800215static int tls_ex_idx_session = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700217struct tls_context {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218 void (*event_cb)(void *ctx, enum tls_event ev,
219 union tls_event_data *data);
220 void *cb_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 int cert_in_cb;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700222 char *ocsp_stapling_response;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223};
224
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700225static struct tls_context *tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226
227
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800228struct tls_data {
229 SSL_CTX *ssl;
230 unsigned int tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -0800231 int check_crl;
232 int check_crl_strict;
233 char *ca_cert;
234 unsigned int crl_reload_interval;
235 struct os_reltime crl_last_reload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800236};
237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700239 struct tls_context *context;
Hai Shalom74f70d42019-02-11 14:42:39 -0800240 struct tls_data *data;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800241 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 SSL *ssl;
243 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700244#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 ENGINE *engine; /* functional reference to the engine */
246 EVP_PKEY *private_key; /* the private key if using engine */
247#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800248 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 int read_alerts, write_alerts, failed;
250
251 tls_session_ticket_cb session_ticket_cb;
252 void *session_ticket_cb_ctx;
253
254 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
255 u8 *session_ticket;
256 size_t session_ticket_len;
257
258 unsigned int ca_cert_verify:1;
259 unsigned int cert_probe:1;
260 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300261 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800262 unsigned int success_data:1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700263 unsigned int client_hello_generated:1;
264 unsigned int server:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265
266 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700267
268 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700269
270 X509 *peer_cert;
271 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800272 X509 *peer_issuer_issuer;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800273
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800274 unsigned char client_random[SSL3_RANDOM_SIZE];
275 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700276
277 u16 cipher_suite;
278 int server_dh_prime_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279};
280
281
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700282static struct tls_context * tls_context_new(const struct tls_config *conf)
283{
284 struct tls_context *context = os_zalloc(sizeof(*context));
285 if (context == NULL)
286 return NULL;
287 if (conf) {
288 context->event_cb = conf->event_cb;
289 context->cb_ctx = conf->cb_ctx;
290 context->cert_in_cb = conf->cert_in_cb;
291 }
292 return context;
293}
294
295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296#ifdef CONFIG_NO_STDOUT_DEBUG
297
298static void _tls_show_errors(void)
299{
300 unsigned long err;
301
302 while ((err = ERR_get_error())) {
303 /* Just ignore the errors, since stdout is disabled */
304 }
305}
306#define tls_show_errors(l, f, t) _tls_show_errors()
307
308#else /* CONFIG_NO_STDOUT_DEBUG */
309
310static void tls_show_errors(int level, const char *func, const char *txt)
311{
312 unsigned long err;
313
314 wpa_printf(level, "OpenSSL: %s - %s %s",
315 func, txt, ERR_error_string(ERR_get_error(), NULL));
316
317 while ((err = ERR_get_error())) {
318 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
319 ERR_error_string(err, NULL));
320 }
321}
322
323#endif /* CONFIG_NO_STDOUT_DEBUG */
324
325
Hai Shalom74f70d42019-02-11 14:42:39 -0800326static X509_STORE * tls_crl_cert_reload(const char *ca_cert, int check_crl)
327{
328 int flags;
329 X509_STORE *store;
330
331 store = X509_STORE_new();
332 if (!store) {
333 wpa_printf(MSG_DEBUG,
334 "OpenSSL: %s - failed to allocate new certificate store",
335 __func__);
336 return NULL;
337 }
338
339 if (ca_cert && X509_STORE_load_locations(store, ca_cert, NULL) != 1) {
340 tls_show_errors(MSG_WARNING, __func__,
341 "Failed to load root certificates");
342 X509_STORE_free(store);
343 return NULL;
344 }
345
346 flags = check_crl ? X509_V_FLAG_CRL_CHECK : 0;
347 if (check_crl == 2)
348 flags |= X509_V_FLAG_CRL_CHECK_ALL;
349
350 X509_STORE_set_flags(store, flags);
351
352 return store;
353}
354
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356#ifdef CONFIG_NATIVE_WINDOWS
357
358/* Windows CryptoAPI and access to certificate stores */
359#include <wincrypt.h>
360
361#ifdef __MINGW32_VERSION
362/*
363 * MinGW does not yet include all the needed definitions for CryptoAPI, so
364 * define here whatever extra is needed.
365 */
366#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
367#define CERT_STORE_READONLY_FLAG 0x00008000
368#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
369
370#endif /* __MINGW32_VERSION */
371
372
373struct cryptoapi_rsa_data {
374 const CERT_CONTEXT *cert;
375 HCRYPTPROV crypt_prov;
376 DWORD key_spec;
377 BOOL free_crypt_prov;
378};
379
380
381static void cryptoapi_error(const char *msg)
382{
383 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
384 msg, (unsigned int) GetLastError());
385}
386
387
388static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
389 unsigned char *to, RSA *rsa, int padding)
390{
391 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
392 return 0;
393}
394
395
396static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
397 unsigned char *to, RSA *rsa, int padding)
398{
399 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
400 return 0;
401}
402
403
404static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
405 unsigned char *to, RSA *rsa, int padding)
406{
407 struct cryptoapi_rsa_data *priv =
408 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
409 HCRYPTHASH hash;
410 DWORD hash_size, len, i;
411 unsigned char *buf = NULL;
412 int ret = 0;
413
414 if (priv == NULL) {
415 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
416 ERR_R_PASSED_NULL_PARAMETER);
417 return 0;
418 }
419
420 if (padding != RSA_PKCS1_PADDING) {
421 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
422 RSA_R_UNKNOWN_PADDING_TYPE);
423 return 0;
424 }
425
426 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
427 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
428 __func__);
429 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
430 RSA_R_INVALID_MESSAGE_LENGTH);
431 return 0;
432 }
433
434 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
435 {
436 cryptoapi_error("CryptCreateHash failed");
437 return 0;
438 }
439
440 len = sizeof(hash_size);
441 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
442 0)) {
443 cryptoapi_error("CryptGetHashParam failed");
444 goto err;
445 }
446
447 if ((int) hash_size != flen) {
448 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
449 (unsigned) hash_size, flen);
450 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
451 RSA_R_INVALID_MESSAGE_LENGTH);
452 goto err;
453 }
454 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
455 cryptoapi_error("CryptSetHashParam failed");
456 goto err;
457 }
458
459 len = RSA_size(rsa);
460 buf = os_malloc(len);
461 if (buf == NULL) {
462 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
463 goto err;
464 }
465
466 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
467 cryptoapi_error("CryptSignHash failed");
468 goto err;
469 }
470
471 for (i = 0; i < len; i++)
472 to[i] = buf[len - i - 1];
473 ret = len;
474
475err:
476 os_free(buf);
477 CryptDestroyHash(hash);
478
479 return ret;
480}
481
482
483static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
484 unsigned char *to, RSA *rsa, int padding)
485{
486 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
487 return 0;
488}
489
490
491static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
492{
493 if (priv == NULL)
494 return;
495 if (priv->crypt_prov && priv->free_crypt_prov)
496 CryptReleaseContext(priv->crypt_prov, 0);
497 if (priv->cert)
498 CertFreeCertificateContext(priv->cert);
499 os_free(priv);
500}
501
502
503static int cryptoapi_finish(RSA *rsa)
504{
505 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
506 os_free((void *) rsa->meth);
507 rsa->meth = NULL;
508 return 1;
509}
510
511
512static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
513{
514 HCERTSTORE cs;
515 const CERT_CONTEXT *ret = NULL;
516
517 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
518 store | CERT_STORE_OPEN_EXISTING_FLAG |
519 CERT_STORE_READONLY_FLAG, L"MY");
520 if (cs == NULL) {
521 cryptoapi_error("Failed to open 'My system store'");
522 return NULL;
523 }
524
525 if (strncmp(name, "cert://", 7) == 0) {
526 unsigned short wbuf[255];
527 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
528 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
529 PKCS_7_ASN_ENCODING,
530 0, CERT_FIND_SUBJECT_STR,
531 wbuf, NULL);
532 } else if (strncmp(name, "hash://", 7) == 0) {
533 CRYPT_HASH_BLOB blob;
534 int len;
535 const char *hash = name + 7;
536 unsigned char *buf;
537
538 len = os_strlen(hash) / 2;
539 buf = os_malloc(len);
540 if (buf && hexstr2bin(hash, buf, len) == 0) {
541 blob.cbData = len;
542 blob.pbData = buf;
543 ret = CertFindCertificateInStore(cs,
544 X509_ASN_ENCODING |
545 PKCS_7_ASN_ENCODING,
546 0, CERT_FIND_HASH,
547 &blob, NULL);
548 }
549 os_free(buf);
550 }
551
552 CertCloseStore(cs, 0);
553
554 return ret;
555}
556
557
558static int tls_cryptoapi_cert(SSL *ssl, const char *name)
559{
560 X509 *cert = NULL;
561 RSA *rsa = NULL, *pub_rsa;
562 struct cryptoapi_rsa_data *priv;
563 RSA_METHOD *rsa_meth;
564
565 if (name == NULL ||
566 (strncmp(name, "cert://", 7) != 0 &&
567 strncmp(name, "hash://", 7) != 0))
568 return -1;
569
570 priv = os_zalloc(sizeof(*priv));
571 rsa_meth = os_zalloc(sizeof(*rsa_meth));
572 if (priv == NULL || rsa_meth == NULL) {
573 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
574 "for CryptoAPI RSA method");
575 os_free(priv);
576 os_free(rsa_meth);
577 return -1;
578 }
579
580 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
581 if (priv->cert == NULL) {
582 priv->cert = cryptoapi_find_cert(
583 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
584 }
585 if (priv->cert == NULL) {
586 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
587 "'%s'", name);
588 goto err;
589 }
590
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800591 cert = d2i_X509(NULL,
592 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 priv->cert->cbCertEncoded);
594 if (cert == NULL) {
595 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
596 "encoding");
597 goto err;
598 }
599
600 if (!CryptAcquireCertificatePrivateKey(priv->cert,
601 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
602 NULL, &priv->crypt_prov,
603 &priv->key_spec,
604 &priv->free_crypt_prov)) {
605 cryptoapi_error("Failed to acquire a private key for the "
606 "certificate");
607 goto err;
608 }
609
610 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
611 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
612 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
613 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
614 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
615 rsa_meth->finish = cryptoapi_finish;
616 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
617 rsa_meth->app_data = (char *) priv;
618
619 rsa = RSA_new();
620 if (rsa == NULL) {
621 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
622 ERR_R_MALLOC_FAILURE);
623 goto err;
624 }
625
626 if (!SSL_use_certificate(ssl, cert)) {
627 RSA_free(rsa);
628 rsa = NULL;
629 goto err;
630 }
631 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
632 X509_free(cert);
633 cert = NULL;
634
635 rsa->n = BN_dup(pub_rsa->n);
636 rsa->e = BN_dup(pub_rsa->e);
637 if (!RSA_set_method(rsa, rsa_meth))
638 goto err;
639
640 if (!SSL_use_RSAPrivateKey(ssl, rsa))
641 goto err;
642 RSA_free(rsa);
643
644 return 0;
645
646err:
647 if (cert)
648 X509_free(cert);
649 if (rsa)
650 RSA_free(rsa);
651 else {
652 os_free(rsa_meth);
653 cryptoapi_free_data(priv);
654 }
655 return -1;
656}
657
658
659static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
660{
661 HCERTSTORE cs;
662 PCCERT_CONTEXT ctx = NULL;
663 X509 *cert;
664 char buf[128];
665 const char *store;
666#ifdef UNICODE
667 WCHAR *wstore;
668#endif /* UNICODE */
669
670 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
671 return -1;
672
673 store = name + 13;
674#ifdef UNICODE
675 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
676 if (wstore == NULL)
677 return -1;
678 wsprintf(wstore, L"%S", store);
679 cs = CertOpenSystemStore(0, wstore);
680 os_free(wstore);
681#else /* UNICODE */
682 cs = CertOpenSystemStore(0, store);
683#endif /* UNICODE */
684 if (cs == NULL) {
685 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
686 "'%s': error=%d", __func__, store,
687 (int) GetLastError());
688 return -1;
689 }
690
691 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800692 cert = d2i_X509(NULL,
693 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 ctx->cbCertEncoded);
695 if (cert == NULL) {
696 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
697 "X509 DER encoding for CA cert");
698 continue;
699 }
700
701 X509_NAME_oneline(X509_get_subject_name(cert), buf,
702 sizeof(buf));
703 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
704 "system certificate store: subject='%s'", buf);
705
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700706 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
707 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 tls_show_errors(MSG_WARNING, __func__,
709 "Failed to add ca_cert to OpenSSL "
710 "certificate store");
711 }
712
713 X509_free(cert);
714 }
715
716 if (!CertCloseStore(cs, 0)) {
717 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
718 "'%s': error=%d", __func__, name + 13,
719 (int) GetLastError());
720 }
721
722 return 0;
723}
724
725
726#else /* CONFIG_NATIVE_WINDOWS */
727
728static int tls_cryptoapi_cert(SSL *ssl, const char *name)
729{
730 return -1;
731}
732
733#endif /* CONFIG_NATIVE_WINDOWS */
734
735
736static void ssl_info_cb(const SSL *ssl, int where, int ret)
737{
738 const char *str;
739 int w;
740
741 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
742 w = where & ~SSL_ST_MASK;
743 if (w & SSL_ST_CONNECT)
744 str = "SSL_connect";
745 else if (w & SSL_ST_ACCEPT)
746 str = "SSL_accept";
747 else
748 str = "undefined";
749
750 if (where & SSL_CB_LOOP) {
751 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
752 str, SSL_state_string_long(ssl));
753 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700754 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
756 where & SSL_CB_READ ?
757 "read (remote end reported an error)" :
758 "write (local SSL3 detected an error)",
759 SSL_alert_type_string_long(ret),
760 SSL_alert_desc_string_long(ret));
761 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 if (where & SSL_CB_READ)
763 conn->read_alerts++;
764 else
765 conn->write_alerts++;
766 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700767 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700768 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700769 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700770 os_memset(&ev, 0, sizeof(ev));
771 ev.alert.is_local = !(where & SSL_CB_READ);
772 ev.alert.type = SSL_alert_type_string_long(ret);
773 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700774 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700775 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 } else if (where & SSL_CB_EXIT && ret <= 0) {
777 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
778 str, ret == 0 ? "failed" : "error",
779 SSL_state_string_long(ssl));
780 }
781}
782
783
784#ifndef OPENSSL_NO_ENGINE
785/**
786 * tls_engine_load_dynamic_generic - load any openssl engine
787 * @pre: an array of commands and values that load an engine initialized
788 * in the engine specific function
789 * @post: an array of commands and values that initialize an already loaded
790 * engine (or %NULL if not required)
791 * @id: the engine id of the engine to load (only required if post is not %NULL
792 *
793 * This function is a generic function that loads any openssl engine.
794 *
795 * Returns: 0 on success, -1 on failure
796 */
797static int tls_engine_load_dynamic_generic(const char *pre[],
798 const char *post[], const char *id)
799{
800 ENGINE *engine;
801 const char *dynamic_id = "dynamic";
802
803 engine = ENGINE_by_id(id);
804 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
806 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700807 /*
808 * If it was auto-loaded by ENGINE_by_id() we might still
809 * need to tell it which PKCS#11 module to use in legacy
810 * (non-p11-kit) environments. Do so now; even if it was
811 * properly initialised before, setting it again will be
812 * harmless.
813 */
814 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 }
816 ERR_clear_error();
817
818 engine = ENGINE_by_id(dynamic_id);
819 if (engine == NULL) {
820 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
821 dynamic_id,
822 ERR_error_string(ERR_get_error(), NULL));
823 return -1;
824 }
825
826 /* Perform the pre commands. This will load the engine. */
827 while (pre && pre[0]) {
828 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
829 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
830 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
831 "%s %s [%s]", pre[0], pre[1],
832 ERR_error_string(ERR_get_error(), NULL));
833 ENGINE_free(engine);
834 return -1;
835 }
836 pre += 2;
837 }
838
839 /*
840 * Free the reference to the "dynamic" engine. The loaded engine can
841 * now be looked up using ENGINE_by_id().
842 */
843 ENGINE_free(engine);
844
845 engine = ENGINE_by_id(id);
846 if (engine == NULL) {
847 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
848 id, ERR_error_string(ERR_get_error(), NULL));
849 return -1;
850 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700851 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 while (post && post[0]) {
853 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
854 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
855 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
856 " %s %s [%s]", post[0], post[1],
857 ERR_error_string(ERR_get_error(), NULL));
858 ENGINE_remove(engine);
859 ENGINE_free(engine);
860 return -1;
861 }
862 post += 2;
863 }
864 ENGINE_free(engine);
865
866 return 0;
867}
868
869
870/**
871 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
872 * @pkcs11_so_path: pksc11_so_path from the configuration
873 * @pcks11_module_path: pkcs11_module_path from the configuration
874 */
875static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
876 const char *pkcs11_module_path)
877{
878 char *engine_id = "pkcs11";
879 const char *pre_cmd[] = {
880 "SO_PATH", NULL /* pkcs11_so_path */,
881 "ID", NULL /* engine_id */,
882 "LIST_ADD", "1",
883 /* "NO_VCHECK", "1", */
884 "LOAD", NULL,
885 NULL, NULL
886 };
887 const char *post_cmd[] = {
888 "MODULE_PATH", NULL /* pkcs11_module_path */,
889 NULL, NULL
890 };
891
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800892 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 return 0;
894
895 pre_cmd[1] = pkcs11_so_path;
896 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800897 if (pkcs11_module_path)
898 post_cmd[1] = pkcs11_module_path;
899 else
900 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901
902 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
903 pkcs11_so_path);
904
905 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
906}
907
908
909/**
910 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
911 * @opensc_so_path: opensc_so_path from the configuration
912 */
913static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
914{
915 char *engine_id = "opensc";
916 const char *pre_cmd[] = {
917 "SO_PATH", NULL /* opensc_so_path */,
918 "ID", NULL /* engine_id */,
919 "LIST_ADD", "1",
920 "LOAD", NULL,
921 NULL, NULL
922 };
923
924 if (!opensc_so_path)
925 return 0;
926
927 pre_cmd[1] = opensc_so_path;
928 pre_cmd[3] = engine_id;
929
930 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
931 opensc_so_path);
932
933 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
934}
935#endif /* OPENSSL_NO_ENGINE */
936
937
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800938static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
939{
940 struct wpabuf *buf;
941
942 if (tls_ex_idx_session < 0)
943 return;
944 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
945 if (!buf)
946 return;
947 wpa_printf(MSG_DEBUG,
948 "OpenSSL: Free application session data %p (sess %p)",
949 buf, sess);
950 wpabuf_free(buf);
951
952 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
953}
954
955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956void * tls_init(const struct tls_config *conf)
957{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800958 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700960 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800961 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962
963 if (tls_openssl_ref_count == 0) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700964 tls_global = context = tls_context_new(conf);
965 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967#ifdef CONFIG_FIPS
968#ifdef OPENSSL_FIPS
969 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800970 static int fips_enabled = 0;
971
972 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
974 "mode");
975 ERR_load_crypto_strings();
976 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700977 os_free(tls_global);
978 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800980 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800982 fips_enabled = 1;
983 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 }
985#else /* OPENSSL_FIPS */
986 if (conf && conf->fips_mode) {
987 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
988 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700989 os_free(tls_global);
990 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 return NULL;
992 }
993#endif /* OPENSSL_FIPS */
994#endif /* CONFIG_FIPS */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700995#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
996 (defined(LIBRESSL_VERSION_NUMBER) && \
997 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 SSL_load_error_strings();
999 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001000#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 EVP_add_digest(EVP_sha256());
1002#endif /* OPENSSL_NO_SHA256 */
1003 /* TODO: if /dev/urandom is available, PRNG is seeded
1004 * automatically. If this is not the case, random data should
1005 * be added here. */
1006
1007#ifdef PKCS12_FUNCS
1008#ifndef OPENSSL_NO_RC2
1009 /*
1010 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
1011 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
1012 * versions, but it looks like OpenSSL 1.0.0 does not do that
1013 * anymore.
1014 */
1015 EVP_add_cipher(EVP_rc2_40_cbc());
1016#endif /* OPENSSL_NO_RC2 */
1017 PKCS12_PBE_add();
1018#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001019#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001020 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001021 context = tls_context_new(conf);
1022 if (context == NULL)
1023 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 }
1025 tls_openssl_ref_count++;
1026
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001027 data = os_zalloc(sizeof(*data));
1028 if (data)
1029 ssl = SSL_CTX_new(SSLv23_method());
1030 else
1031 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001032 if (ssl == NULL) {
1033 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001034 if (context != tls_global)
1035 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001036 if (tls_openssl_ref_count == 0) {
1037 os_free(tls_global);
1038 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001039 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001040 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001042 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001043 data->ssl = ssl;
Hai Shalom74f70d42019-02-11 14:42:39 -08001044 if (conf) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001045 data->tls_session_lifetime = conf->tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -08001046 data->crl_reload_interval = conf->crl_reload_interval;
1047 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001049 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
1050 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
1051
Dmitry Shmidt29333592017-01-09 12:27:11 -08001052#ifdef SSL_MODE_NO_AUTO_CHAIN
1053 /* Number of deployed use cases assume the default OpenSSL behavior of
1054 * auto chaining the local certificate is in use. BoringSSL removed this
1055 * functionality by default, so we need to restore it here to avoid
1056 * breaking existing use cases. */
1057 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
1058#endif /* SSL_MODE_NO_AUTO_CHAIN */
1059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001061 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001062 if (data->tls_session_lifetime > 0) {
1063 SSL_CTX_set_quiet_shutdown(ssl, 1);
1064 /*
1065 * Set default context here. In practice, this will be replaced
1066 * by the per-EAP method context in tls_connection_set_verify().
1067 */
1068 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
1069 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
1070 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
1071 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
1072 } else {
1073 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
1074 }
1075
1076 if (tls_ex_idx_session < 0) {
1077 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1078 0, NULL, NULL, NULL, NULL);
1079 if (tls_ex_idx_session < 0) {
1080 tls_deinit(data);
1081 return NULL;
1082 }
1083 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084
1085#ifndef OPENSSL_NO_ENGINE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001086 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
Hai Shalomce48b4a2018-09-05 11:41:35 -07001087#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001088 ERR_load_ENGINE_strings();
1089 ENGINE_load_dynamic();
Hai Shalomce48b4a2018-09-05 11:41:35 -07001090#endif /* OPENSSL_VERSION_NUMBER */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 if (conf &&
1093 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1094 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1096 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1097 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001098 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 return NULL;
1100 }
1101 }
1102#endif /* OPENSSL_NO_ENGINE */
1103
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001104 if (conf && conf->openssl_ciphers)
1105 ciphers = conf->openssl_ciphers;
1106 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001107 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001108 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1109 wpa_printf(MSG_ERROR,
1110 "OpenSSL: Failed to set cipher string '%s'",
1111 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001112 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001113 return NULL;
1114 }
1115
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001116 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117}
1118
1119
1120void tls_deinit(void *ssl_ctx)
1121{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001122 struct tls_data *data = ssl_ctx;
1123 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001124 struct tls_context *context = SSL_CTX_get_app_data(ssl);
1125 if (context != tls_global)
1126 os_free(context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001127 if (data->tls_session_lifetime > 0)
1128 SSL_CTX_flush_sessions(ssl, 0);
Hai Shalom74f70d42019-02-11 14:42:39 -08001129 os_free(data->ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 SSL_CTX_free(ssl);
1131
1132 tls_openssl_ref_count--;
1133 if (tls_openssl_ref_count == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001134#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1135 (defined(LIBRESSL_VERSION_NUMBER) && \
1136 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137#ifndef OPENSSL_NO_ENGINE
1138 ENGINE_cleanup();
1139#endif /* OPENSSL_NO_ENGINE */
1140 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001141 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 ERR_free_strings();
1143 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001144#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001145 os_free(tls_global->ocsp_stapling_response);
1146 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 os_free(tls_global);
1148 tls_global = NULL;
1149 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001150
1151 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152}
1153
1154
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001155#ifndef OPENSSL_NO_ENGINE
1156
1157/* Cryptoki return values */
1158#define CKR_PIN_INCORRECT 0x000000a0
1159#define CKR_PIN_INVALID 0x000000a1
1160#define CKR_PIN_LEN_RANGE 0x000000a2
1161
1162/* libp11 */
1163#define ERR_LIB_PKCS11 ERR_LIB_USER
1164
1165static int tls_is_pin_error(unsigned int err)
1166{
1167 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1168 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1169 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1170 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1171}
1172
1173#endif /* OPENSSL_NO_ENGINE */
1174
1175
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001176#ifdef ANDROID
1177/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1178EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1179#endif /* ANDROID */
1180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1182 const char *pin, const char *key_id,
1183 const char *cert_id, const char *ca_cert_id)
1184{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001185#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1186#if !defined(OPENSSL_NO_ENGINE)
1187#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1188#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001189 if (!key_id)
1190 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001191 conn->engine = NULL;
1192 conn->private_key = EVP_PKEY_from_keystore(key_id);
1193 if (!conn->private_key) {
1194 wpa_printf(MSG_ERROR,
1195 "ENGINE: cannot load private key with id '%s' [%s]",
1196 key_id,
1197 ERR_error_string(ERR_get_error(), NULL));
1198 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1199 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001200#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202#ifndef OPENSSL_NO_ENGINE
1203 int ret = -1;
1204 if (engine_id == NULL) {
1205 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1206 return -1;
1207 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208
1209 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001210#ifdef ANDROID
1211 ENGINE_load_dynamic();
1212#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 conn->engine = ENGINE_by_id(engine_id);
1214 if (!conn->engine) {
1215 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1216 engine_id, ERR_error_string(ERR_get_error(), NULL));
1217 goto err;
1218 }
1219 if (ENGINE_init(conn->engine) != 1) {
1220 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1221 "(engine: %s) [%s]", engine_id,
1222 ERR_error_string(ERR_get_error(), NULL));
1223 goto err;
1224 }
1225 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1226
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001227#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001228 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1230 ERR_error_string(ERR_get_error(), NULL));
1231 goto err;
1232 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001233#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001234 if (key_id) {
1235 /*
1236 * Ensure that the ENGINE does not attempt to use the OpenSSL
1237 * UI system to obtain a PIN, if we didn't provide one.
1238 */
1239 struct {
1240 const void *password;
1241 const char *prompt_info;
1242 } key_cb = { "", NULL };
1243
1244 /* load private key first in-case PIN is required for cert */
1245 conn->private_key = ENGINE_load_private_key(conn->engine,
1246 key_id, NULL,
1247 &key_cb);
1248 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001249 unsigned long err = ERR_get_error();
1250
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001251 wpa_printf(MSG_ERROR,
1252 "ENGINE: cannot load private key with id '%s' [%s]",
1253 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001254 ERR_error_string(err, NULL));
1255 if (tls_is_pin_error(err))
1256 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1257 else
1258 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001259 goto err;
1260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 }
1262
1263 /* handle a certificate and/or CA certificate */
1264 if (cert_id || ca_cert_id) {
1265 const char *cmd_name = "LOAD_CERT_CTRL";
1266
1267 /* test if the engine supports a LOAD_CERT_CTRL */
1268 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1269 0, (void *)cmd_name, NULL)) {
1270 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1271 " loading certificates");
1272 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1273 goto err;
1274 }
1275 }
1276
1277 return 0;
1278
1279err:
1280 if (conn->engine) {
1281 ENGINE_free(conn->engine);
1282 conn->engine = NULL;
1283 }
1284
1285 if (conn->private_key) {
1286 EVP_PKEY_free(conn->private_key);
1287 conn->private_key = NULL;
1288 }
1289
1290 return ret;
1291#else /* OPENSSL_NO_ENGINE */
1292 return 0;
1293#endif /* OPENSSL_NO_ENGINE */
1294}
1295
1296
1297static void tls_engine_deinit(struct tls_connection *conn)
1298{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001299#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1301 if (conn->private_key) {
1302 EVP_PKEY_free(conn->private_key);
1303 conn->private_key = NULL;
1304 }
1305 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001306#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001308#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 conn->engine = NULL;
1310 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001311#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312}
1313
1314
1315int tls_get_errors(void *ssl_ctx)
1316{
1317 int count = 0;
1318 unsigned long err;
1319
1320 while ((err = ERR_get_error())) {
1321 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1322 ERR_error_string(err, NULL));
1323 count++;
1324 }
1325
1326 return count;
1327}
1328
Jouni Malinen26af48b2014-04-09 13:02:53 +03001329
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001330static const char * openssl_content_type(int content_type)
1331{
1332 switch (content_type) {
1333 case 20:
1334 return "change cipher spec";
1335 case 21:
1336 return "alert";
1337 case 22:
1338 return "handshake";
1339 case 23:
1340 return "application data";
1341 case 24:
1342 return "heartbeat";
1343 case 256:
1344 return "TLS header info"; /* pseudo content type */
1345 default:
1346 return "?";
1347 }
1348}
1349
1350
1351static const char * openssl_handshake_type(int content_type, const u8 *buf,
1352 size_t len)
1353{
1354 if (content_type != 22 || !buf || len == 0)
1355 return "";
1356 switch (buf[0]) {
1357 case 0:
1358 return "hello request";
1359 case 1:
1360 return "client hello";
1361 case 2:
1362 return "server hello";
Hai Shalom74f70d42019-02-11 14:42:39 -08001363 case 3:
1364 return "hello verify request";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001365 case 4:
1366 return "new session ticket";
Hai Shalom74f70d42019-02-11 14:42:39 -08001367 case 5:
1368 return "end of early data";
1369 case 6:
1370 return "hello retry request";
1371 case 8:
1372 return "encrypted extensions";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001373 case 11:
1374 return "certificate";
1375 case 12:
1376 return "server key exchange";
1377 case 13:
1378 return "certificate request";
1379 case 14:
1380 return "server hello done";
1381 case 15:
1382 return "certificate verify";
1383 case 16:
1384 return "client key exchange";
1385 case 20:
1386 return "finished";
1387 case 21:
1388 return "certificate url";
1389 case 22:
1390 return "certificate status";
Hai Shalom74f70d42019-02-11 14:42:39 -08001391 case 23:
1392 return "supplemental data";
1393 case 24:
1394 return "key update";
1395 case 254:
1396 return "message hash";
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001397 default:
1398 return "?";
1399 }
1400}
1401
1402
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001403#ifdef CONFIG_SUITEB
1404
1405static void check_server_hello(struct tls_connection *conn,
1406 const u8 *pos, const u8 *end)
1407{
1408 size_t payload_len, id_len;
1409
1410 /*
1411 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1412 * not make it cleanly available during handshake and we need to know
1413 * whether DHE was selected.
1414 */
1415
1416 if (end - pos < 3)
1417 return;
1418 payload_len = WPA_GET_BE24(pos);
1419 pos += 3;
1420
1421 if ((size_t) (end - pos) < payload_len)
1422 return;
1423 end = pos + payload_len;
1424
1425 /* Skip Version and Random */
1426 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1427 return;
1428 pos += 2 + SSL3_RANDOM_SIZE;
1429
1430 /* Skip Session ID */
1431 if (end - pos < 1)
1432 return;
1433 id_len = *pos++;
1434 if ((size_t) (end - pos) < id_len)
1435 return;
1436 pos += id_len;
1437
1438 if (end - pos < 2)
1439 return;
1440 conn->cipher_suite = WPA_GET_BE16(pos);
1441 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1442 conn->cipher_suite);
1443}
1444
1445
1446static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1447 const u8 *pos, const u8 *end)
1448{
1449 size_t payload_len;
1450 u16 dh_len;
1451 BIGNUM *p;
1452 int bits;
1453
1454 if (!(conn->flags & TLS_CONN_SUITEB))
1455 return;
1456
1457 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1458 if (conn->cipher_suite != 0x9f)
1459 return;
1460
1461 if (end - pos < 3)
1462 return;
1463 payload_len = WPA_GET_BE24(pos);
1464 pos += 3;
1465
1466 if ((size_t) (end - pos) < payload_len)
1467 return;
1468 end = pos + payload_len;
1469
1470 if (end - pos < 2)
1471 return;
1472 dh_len = WPA_GET_BE16(pos);
1473 pos += 2;
1474
1475 if ((size_t) (end - pos) < dh_len)
1476 return;
1477 p = BN_bin2bn(pos, dh_len, NULL);
1478 if (!p)
1479 return;
1480
1481 bits = BN_num_bits(p);
1482 BN_free(p);
1483
1484 conn->server_dh_prime_len = bits;
1485 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1486 conn->server_dh_prime_len);
1487}
1488
1489#endif /* CONFIG_SUITEB */
1490
1491
Jouni Malinen26af48b2014-04-09 13:02:53 +03001492static void tls_msg_cb(int write_p, int version, int content_type,
1493 const void *buf, size_t len, SSL *ssl, void *arg)
1494{
1495 struct tls_connection *conn = arg;
1496 const u8 *pos = buf;
1497
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001498 if (write_p == 2) {
1499 wpa_printf(MSG_DEBUG,
1500 "OpenSSL: session ver=0x%x content_type=%d",
1501 version, content_type);
1502 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1503 return;
1504 }
1505
1506 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1507 write_p ? "TX" : "RX", version, content_type,
1508 openssl_content_type(content_type),
1509 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001510 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1511 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1512 size_t payload_len = WPA_GET_BE16(pos + 1);
1513 if (payload_len + 3 > len) {
1514 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1515 conn->invalid_hb_used = 1;
1516 }
1517 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001518
1519#ifdef CONFIG_SUITEB
1520 /*
1521 * Need to parse these handshake messages to be able to check DH prime
1522 * length since OpenSSL does not expose the new cipher suite and DH
1523 * parameters during handshake (e.g., for cert_cb() callback).
1524 */
1525 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1526 check_server_hello(conn, pos + 1, pos + len);
1527 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1528 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1529#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001530}
1531
1532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533struct tls_connection * tls_connection_init(void *ssl_ctx)
1534{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001535 struct tls_data *data = ssl_ctx;
1536 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 struct tls_connection *conn;
1538 long options;
Hai Shalom74f70d42019-02-11 14:42:39 -08001539 X509_STORE *new_cert_store;
1540 struct os_reltime now;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001541 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542
Hai Shalom74f70d42019-02-11 14:42:39 -08001543 /* Replace X509 store if it is time to update CRL. */
1544 if (data->crl_reload_interval > 0 && os_get_reltime(&now) == 0 &&
1545 os_reltime_expired(&now, &data->crl_last_reload,
1546 data->crl_reload_interval)) {
1547 wpa_printf(MSG_INFO,
1548 "OpenSSL: Flushing X509 store with ca_cert file");
1549 new_cert_store = tls_crl_cert_reload(data->ca_cert,
1550 data->check_crl);
1551 if (!new_cert_store) {
1552 wpa_printf(MSG_ERROR,
1553 "OpenSSL: Error replacing X509 store with ca_cert file");
1554 } else {
1555 /* Replace old store */
1556 SSL_CTX_set_cert_store(ssl, new_cert_store);
1557 data->crl_last_reload = now;
1558 }
1559 }
1560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561 conn = os_zalloc(sizeof(*conn));
1562 if (conn == NULL)
1563 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08001564 conn->data = data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001565 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 conn->ssl = SSL_new(ssl);
1567 if (conn->ssl == NULL) {
1568 tls_show_errors(MSG_INFO, __func__,
1569 "Failed to initialize new SSL connection");
1570 os_free(conn);
1571 return NULL;
1572 }
1573
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001574 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001576 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1577 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1579 SSL_OP_SINGLE_DH_USE;
1580#ifdef SSL_OP_NO_COMPRESSION
1581 options |= SSL_OP_NO_COMPRESSION;
1582#endif /* SSL_OP_NO_COMPRESSION */
1583 SSL_set_options(conn->ssl, options);
1584
1585 conn->ssl_in = BIO_new(BIO_s_mem());
1586 if (!conn->ssl_in) {
1587 tls_show_errors(MSG_INFO, __func__,
1588 "Failed to create a new BIO for ssl_in");
1589 SSL_free(conn->ssl);
1590 os_free(conn);
1591 return NULL;
1592 }
1593
1594 conn->ssl_out = BIO_new(BIO_s_mem());
1595 if (!conn->ssl_out) {
1596 tls_show_errors(MSG_INFO, __func__,
1597 "Failed to create a new BIO for ssl_out");
1598 SSL_free(conn->ssl);
1599 BIO_free(conn->ssl_in);
1600 os_free(conn);
1601 return NULL;
1602 }
1603
1604 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1605
1606 return conn;
1607}
1608
1609
1610void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1611{
1612 if (conn == NULL)
1613 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001614 if (conn->success_data) {
1615 /*
1616 * Make sure ssl_clear_bad_session() does not remove this
1617 * session.
1618 */
1619 SSL_set_quiet_shutdown(conn->ssl, 1);
1620 SSL_shutdown(conn->ssl);
1621 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 SSL_free(conn->ssl);
1623 tls_engine_deinit(conn);
1624 os_free(conn->subject_match);
1625 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001626 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001627 os_free(conn->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 os_free(conn->session_ticket);
1629 os_free(conn);
1630}
1631
1632
1633int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1634{
1635 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1636}
1637
1638
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001639char * tls_connection_peer_serial_num(void *tls_ctx,
1640 struct tls_connection *conn)
1641{
1642 ASN1_INTEGER *ser;
1643 char *serial_num;
1644 size_t len;
1645
1646 if (!conn->peer_cert)
1647 return NULL;
1648
1649 ser = X509_get_serialNumber(conn->peer_cert);
1650 if (!ser)
1651 return NULL;
1652
1653 len = ASN1_STRING_length(ser) * 2 + 1;
1654 serial_num = os_malloc(len);
1655 if (!serial_num)
1656 return NULL;
1657 wpa_snprintf_hex_uppercase(serial_num, len,
1658 ASN1_STRING_get0_data(ser),
1659 ASN1_STRING_length(ser));
1660 return serial_num;
1661}
1662
1663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1665{
1666 if (conn == NULL)
1667 return -1;
1668
1669 /* Shutdown previous TLS connection without notifying the peer
1670 * because the connection was already terminated in practice
1671 * and "close notify" shutdown alert would confuse AS. */
1672 SSL_set_quiet_shutdown(conn->ssl, 1);
1673 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001674 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675}
1676
1677
1678static int tls_match_altsubject_component(X509 *cert, int type,
1679 const char *value, size_t len)
1680{
1681 GENERAL_NAME *gen;
1682 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001683 int found = 0;
1684 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685
1686 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1687
1688 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1689 gen = sk_GENERAL_NAME_value(ext, i);
1690 if (gen->type != type)
1691 continue;
1692 if (os_strlen((char *) gen->d.ia5->data) == len &&
1693 os_memcmp(value, gen->d.ia5->data, len) == 0)
1694 found++;
1695 }
1696
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001697 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 return found;
1700}
1701
1702
1703static int tls_match_altsubject(X509 *cert, const char *match)
1704{
1705 int type;
1706 const char *pos, *end;
1707 size_t len;
1708
1709 pos = match;
1710 do {
1711 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1712 type = GEN_EMAIL;
1713 pos += 6;
1714 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1715 type = GEN_DNS;
1716 pos += 4;
1717 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1718 type = GEN_URI;
1719 pos += 4;
1720 } else {
1721 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1722 "match '%s'", pos);
1723 return 0;
1724 }
1725 end = os_strchr(pos, ';');
1726 while (end) {
1727 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1728 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1729 os_strncmp(end + 1, "URI:", 4) == 0)
1730 break;
1731 end = os_strchr(end + 1, ';');
1732 }
1733 if (end)
1734 len = end - pos;
1735 else
1736 len = os_strlen(pos);
1737 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1738 return 1;
1739 pos = end + 1;
1740 } while (end);
1741
1742 return 0;
1743}
1744
1745
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001746#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001747static int domain_suffix_match(const u8 *val, size_t len, const char *match,
1748 int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001749{
1750 size_t i, match_len;
1751
1752 /* Check for embedded nuls that could mess up suffix matching */
1753 for (i = 0; i < len; i++) {
1754 if (val[i] == '\0') {
1755 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1756 return 0;
1757 }
1758 }
1759
1760 match_len = os_strlen(match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001761 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001762 return 0;
1763
1764 if (os_strncasecmp((const char *) val + len - match_len, match,
1765 match_len) != 0)
1766 return 0; /* no match */
1767
1768 if (match_len == len)
1769 return 1; /* exact match */
1770
1771 if (val[len - match_len - 1] == '.')
1772 return 1; /* full label match completes suffix match */
1773
1774 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1775 return 0;
1776}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001777#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001778
1779
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001780static int tls_match_suffix(X509 *cert, const char *match, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001781{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001782#ifdef CONFIG_NATIVE_WINDOWS
1783 /* wincrypt.h has conflicting X509_NAME definition */
1784 return -1;
1785#else /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001786 GENERAL_NAME *gen;
1787 void *ext;
1788 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001789 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001790 int dns_name = 0;
1791 X509_NAME *name;
1792
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001793 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
1794 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001795
1796 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1797
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001798 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
1799 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001800 if (gen->type != GEN_DNS)
1801 continue;
1802 dns_name++;
1803 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
1804 gen->d.dNSName->data,
1805 gen->d.dNSName->length);
1806 if (domain_suffix_match(gen->d.dNSName->data,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001807 gen->d.dNSName->length, match, full) ==
1808 1) {
1809 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
1810 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001811 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001812 return 1;
1813 }
1814 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001815 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001816
1817 if (dns_name) {
1818 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
1819 return 0;
1820 }
1821
1822 name = X509_get_subject_name(cert);
1823 i = -1;
1824 for (;;) {
1825 X509_NAME_ENTRY *e;
1826 ASN1_STRING *cn;
1827
1828 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
1829 if (i == -1)
1830 break;
1831 e = X509_NAME_get_entry(name, i);
1832 if (e == NULL)
1833 continue;
1834 cn = X509_NAME_ENTRY_get_data(e);
1835 if (cn == NULL)
1836 continue;
1837 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
1838 cn->data, cn->length);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001839 if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
1840 {
1841 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
1842 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001843 return 1;
1844 }
1845 }
1846
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001847 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
1848 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001849 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001850#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001851}
1852
1853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854static enum tls_fail_reason openssl_tls_fail_reason(int err)
1855{
1856 switch (err) {
1857 case X509_V_ERR_CERT_REVOKED:
1858 return TLS_FAIL_REVOKED;
1859 case X509_V_ERR_CERT_NOT_YET_VALID:
1860 case X509_V_ERR_CRL_NOT_YET_VALID:
1861 return TLS_FAIL_NOT_YET_VALID;
1862 case X509_V_ERR_CERT_HAS_EXPIRED:
1863 case X509_V_ERR_CRL_HAS_EXPIRED:
1864 return TLS_FAIL_EXPIRED;
1865 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1866 case X509_V_ERR_UNABLE_TO_GET_CRL:
1867 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1868 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1869 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1870 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1871 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1872 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1873 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1874 case X509_V_ERR_INVALID_CA:
1875 return TLS_FAIL_UNTRUSTED;
1876 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1877 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1878 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1879 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1880 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1881 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1882 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1883 case X509_V_ERR_CERT_UNTRUSTED:
1884 case X509_V_ERR_CERT_REJECTED:
1885 return TLS_FAIL_BAD_CERTIFICATE;
1886 default:
1887 return TLS_FAIL_UNSPECIFIED;
1888 }
1889}
1890
1891
1892static struct wpabuf * get_x509_cert(X509 *cert)
1893{
1894 struct wpabuf *buf;
1895 u8 *tmp;
1896
1897 int cert_len = i2d_X509(cert, NULL);
1898 if (cert_len <= 0)
1899 return NULL;
1900
1901 buf = wpabuf_alloc(cert_len);
1902 if (buf == NULL)
1903 return NULL;
1904
1905 tmp = wpabuf_put(buf, cert_len);
1906 i2d_X509(cert, &tmp);
1907 return buf;
1908}
1909
1910
1911static void openssl_tls_fail_event(struct tls_connection *conn,
1912 X509 *err_cert, int err, int depth,
1913 const char *subject, const char *err_str,
1914 enum tls_fail_reason reason)
1915{
1916 union tls_event_data ev;
1917 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001918 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919
Pavel Grafov4d8552e2018-02-06 11:28:29 +00001920#ifdef ANDROID
1921 log_cert_validation_failure(err_str);
1922#endif
1923
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001924 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 return;
1926
1927 cert = get_x509_cert(err_cert);
1928 os_memset(&ev, 0, sizeof(ev));
1929 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
1930 reason : openssl_tls_fail_reason(err);
1931 ev.cert_fail.depth = depth;
1932 ev.cert_fail.subject = subject;
1933 ev.cert_fail.reason_txt = err_str;
1934 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001935 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936 wpabuf_free(cert);
1937}
1938
1939
1940static void openssl_tls_cert_event(struct tls_connection *conn,
1941 X509 *err_cert, int depth,
1942 const char *subject)
1943{
1944 struct wpabuf *cert = NULL;
1945 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001946 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001947 char *altsubject[TLS_MAX_ALT_SUBJECT];
1948 int alt, num_altsubject = 0;
1949 GENERAL_NAME *gen;
1950 void *ext;
1951 stack_index_t i;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001952 ASN1_INTEGER *ser;
1953 char serial_num[128];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954#ifdef CONFIG_SHA256
1955 u8 hash[32];
1956#endif /* CONFIG_SHA256 */
1957
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001958 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 return;
1960
1961 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001962 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
1963 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 cert = get_x509_cert(err_cert);
1965 ev.peer_cert.cert = cert;
1966 }
1967#ifdef CONFIG_SHA256
1968 if (cert) {
1969 const u8 *addr[1];
1970 size_t len[1];
1971 addr[0] = wpabuf_head(cert);
1972 len[0] = wpabuf_len(cert);
1973 if (sha256_vector(1, addr, len, hash) == 0) {
1974 ev.peer_cert.hash = hash;
1975 ev.peer_cert.hash_len = sizeof(hash);
1976 }
1977 }
1978#endif /* CONFIG_SHA256 */
1979 ev.peer_cert.depth = depth;
1980 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001981
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001982 ser = X509_get_serialNumber(err_cert);
1983 if (ser) {
1984 wpa_snprintf_hex_uppercase(serial_num, sizeof(serial_num),
1985 ASN1_STRING_get0_data(ser),
1986 ASN1_STRING_length(ser));
1987 ev.peer_cert.serial_num = serial_num;
1988 }
1989
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001990 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
1991 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1992 char *pos;
1993
1994 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
1995 break;
1996 gen = sk_GENERAL_NAME_value(ext, i);
1997 if (gen->type != GEN_EMAIL &&
1998 gen->type != GEN_DNS &&
1999 gen->type != GEN_URI)
2000 continue;
2001
2002 pos = os_malloc(10 + gen->d.ia5->length + 1);
2003 if (pos == NULL)
2004 break;
2005 altsubject[num_altsubject++] = pos;
2006
2007 switch (gen->type) {
2008 case GEN_EMAIL:
2009 os_memcpy(pos, "EMAIL:", 6);
2010 pos += 6;
2011 break;
2012 case GEN_DNS:
2013 os_memcpy(pos, "DNS:", 4);
2014 pos += 4;
2015 break;
2016 case GEN_URI:
2017 os_memcpy(pos, "URI:", 4);
2018 pos += 4;
2019 break;
2020 }
2021
2022 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
2023 pos += gen->d.ia5->length;
2024 *pos = '\0';
2025 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002026 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002027
2028 for (alt = 0; alt < num_altsubject; alt++)
2029 ev.peer_cert.altsubject[alt] = altsubject[alt];
2030 ev.peer_cert.num_altsubject = num_altsubject;
2031
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002032 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002034 for (alt = 0; alt < num_altsubject; alt++)
2035 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036}
2037
2038
2039static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
2040{
2041 char buf[256];
2042 X509 *err_cert;
2043 int err, depth;
2044 SSL *ssl;
2045 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002046 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002047 char *match, *altmatch, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 const char *err_str;
2049
2050 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002051 if (!err_cert)
2052 return 0;
2053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 err = X509_STORE_CTX_get_error(x509_ctx);
2055 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
2056 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
2057 SSL_get_ex_data_X509_STORE_CTX_idx());
2058 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
2059
2060 conn = SSL_get_app_data(ssl);
2061 if (conn == NULL)
2062 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002063
2064 if (depth == 0)
2065 conn->peer_cert = err_cert;
2066 else if (depth == 1)
2067 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002068 else if (depth == 2)
2069 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002070
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002071 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 match = conn->subject_match;
2073 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002074 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002075 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076
2077 if (!preverify_ok && !conn->ca_cert_verify)
2078 preverify_ok = 1;
2079 if (!preverify_ok && depth > 0 && conn->server_cert_only)
2080 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002081 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
2082 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
2083 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
2084 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
2085 "time mismatch");
2086 preverify_ok = 1;
2087 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002088 if (!preverify_ok && !conn->data->check_crl_strict &&
2089 (err == X509_V_ERR_CRL_HAS_EXPIRED ||
2090 err == X509_V_ERR_CRL_NOT_YET_VALID)) {
2091 wpa_printf(MSG_DEBUG,
2092 "OpenSSL: Ignore certificate validity CRL time mismatch");
2093 preverify_ok = 1;
2094 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095
2096 err_str = X509_verify_cert_error_string(err);
2097
2098#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002099 /*
2100 * Do not require preverify_ok so we can explicity allow otherwise
2101 * invalid pinned server certificates.
2102 */
2103 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 struct wpabuf *cert;
2105 cert = get_x509_cert(err_cert);
2106 if (!cert) {
2107 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
2108 "server certificate data");
2109 preverify_ok = 0;
2110 } else {
2111 u8 hash[32];
2112 const u8 *addr[1];
2113 size_t len[1];
2114 addr[0] = wpabuf_head(cert);
2115 len[0] = wpabuf_len(cert);
2116 if (sha256_vector(1, addr, len, hash) < 0 ||
2117 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
2118 err_str = "Server certificate mismatch";
2119 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2120 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002121 } else if (!preverify_ok) {
2122 /*
2123 * Certificate matches pinned certificate, allow
2124 * regardless of other problems.
2125 */
2126 wpa_printf(MSG_DEBUG,
2127 "OpenSSL: Ignore validation issues for a pinned server certificate");
2128 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 }
2130 wpabuf_free(cert);
2131 }
2132 }
2133#endif /* CONFIG_SHA256 */
2134
2135 if (!preverify_ok) {
2136 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2137 " error %d (%s) depth %d for '%s'", err, err_str,
2138 depth, buf);
2139 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2140 err_str, TLS_FAIL_UNSPECIFIED);
2141 return preverify_ok;
2142 }
2143
2144 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2145 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2146 preverify_ok, err, err_str,
2147 conn->ca_cert_verify, depth, buf);
2148 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2149 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2150 "match with '%s'", buf, match);
2151 preverify_ok = 0;
2152 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2153 "Subject mismatch",
2154 TLS_FAIL_SUBJECT_MISMATCH);
2155 } else if (depth == 0 && altmatch &&
2156 !tls_match_altsubject(err_cert, altmatch)) {
2157 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2158 "'%s' not found", altmatch);
2159 preverify_ok = 0;
2160 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2161 "AltSubject mismatch",
2162 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002163 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002164 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002165 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2166 suffix_match);
2167 preverify_ok = 0;
2168 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2169 "Domain suffix mismatch",
2170 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002171 } else if (depth == 0 && domain_match &&
2172 !tls_match_suffix(err_cert, domain_match, 1)) {
2173 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2174 domain_match);
2175 preverify_ok = 0;
2176 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2177 "Domain mismatch",
2178 TLS_FAIL_DOMAIN_MISMATCH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 } else
2180 openssl_tls_cert_event(conn, err_cert, depth, buf);
2181
2182 if (conn->cert_probe && preverify_ok && depth == 0) {
2183 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2184 "on probe-only run");
2185 preverify_ok = 0;
2186 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2187 "Server certificate chain probe",
2188 TLS_FAIL_SERVER_CHAIN_PROBE);
2189 }
2190
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002191#ifdef CONFIG_SUITEB
2192 if (conn->flags & TLS_CONN_SUITEB) {
2193 EVP_PKEY *pk;
2194 RSA *rsa;
2195 int len = -1;
2196
2197 pk = X509_get_pubkey(err_cert);
2198 if (pk) {
2199 rsa = EVP_PKEY_get1_RSA(pk);
2200 if (rsa) {
2201 len = RSA_bits(rsa);
2202 RSA_free(rsa);
2203 }
2204 EVP_PKEY_free(pk);
2205 }
2206
2207 if (len >= 0) {
2208 wpa_printf(MSG_DEBUG,
2209 "OpenSSL: RSA modulus size: %d bits", len);
2210 if (len < 3072) {
2211 preverify_ok = 0;
2212 openssl_tls_fail_event(
2213 conn, err_cert, err,
2214 depth, buf,
2215 "Insufficient RSA modulus size",
2216 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2217 }
2218 }
2219 }
2220#endif /* CONFIG_SUITEB */
2221
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002222#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002223 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2224 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002225 enum ocsp_result res;
2226
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002227 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2228 conn->peer_issuer,
2229 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002230 if (res == OCSP_REVOKED) {
2231 preverify_ok = 0;
2232 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2233 "certificate revoked",
2234 TLS_FAIL_REVOKED);
2235 if (err == X509_V_OK)
2236 X509_STORE_CTX_set_error(
2237 x509_ctx, X509_V_ERR_CERT_REVOKED);
2238 } else if (res != OCSP_GOOD &&
2239 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2240 preverify_ok = 0;
2241 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2242 "bad certificate status response",
2243 TLS_FAIL_UNSPECIFIED);
2244 }
2245 }
2246#endif /* OPENSSL_IS_BORINGSSL */
2247
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002248 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002249 context->event_cb(context->cb_ctx,
2250 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 return preverify_ok;
2253}
2254
2255
2256#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002257static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002259 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 X509_LOOKUP *lookup;
2261 int ret = 0;
2262
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002263 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 X509_LOOKUP_file());
2265 if (lookup == NULL) {
2266 tls_show_errors(MSG_WARNING, __func__,
2267 "Failed add lookup for X509 store");
2268 return -1;
2269 }
2270
2271 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2272 unsigned long err = ERR_peek_error();
2273 tls_show_errors(MSG_WARNING, __func__,
2274 "Failed load CA in DER format");
2275 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2276 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2277 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2278 "cert already in hash table error",
2279 __func__);
2280 } else
2281 ret = -1;
2282 }
2283
2284 return ret;
2285}
2286#endif /* OPENSSL_NO_STDIO */
2287
2288
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002289static int tls_connection_ca_cert(struct tls_data *data,
2290 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291 const char *ca_cert, const u8 *ca_cert_blob,
2292 size_t ca_cert_blob_len, const char *ca_path)
2293{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002294 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002295 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296
2297 /*
2298 * Remove previously configured trusted CA certificates before adding
2299 * new ones.
2300 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002301 store = X509_STORE_new();
2302 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2304 "certificate store", __func__);
2305 return -1;
2306 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002307 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308
2309 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2310 conn->ca_cert_verify = 1;
2311
2312 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2313 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2314 "chain");
2315 conn->cert_probe = 1;
2316 conn->ca_cert_verify = 0;
2317 return 0;
2318 }
2319
2320 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2321#ifdef CONFIG_SHA256
2322 const char *pos = ca_cert + 7;
2323 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2324 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2325 "hash value '%s'", ca_cert);
2326 return -1;
2327 }
2328 pos += 14;
2329 if (os_strlen(pos) != 32 * 2) {
2330 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2331 "hash length in ca_cert '%s'", ca_cert);
2332 return -1;
2333 }
2334 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2335 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2336 "value in ca_cert '%s'", ca_cert);
2337 return -1;
2338 }
2339 conn->server_cert_only = 1;
2340 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2341 "certificate match");
2342 return 0;
2343#else /* CONFIG_SHA256 */
2344 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2345 "cannot validate server certificate hash");
2346 return -1;
2347#endif /* CONFIG_SHA256 */
2348 }
2349
2350 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002351 X509 *cert = d2i_X509(NULL,
2352 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 ca_cert_blob_len);
2354 if (cert == NULL) {
2355 tls_show_errors(MSG_WARNING, __func__,
2356 "Failed to parse ca_cert_blob");
2357 return -1;
2358 }
2359
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002360 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2361 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 unsigned long err = ERR_peek_error();
2363 tls_show_errors(MSG_WARNING, __func__,
2364 "Failed to add ca_cert_blob to "
2365 "certificate store");
2366 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2367 ERR_GET_REASON(err) ==
2368 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2369 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2370 "cert already in hash table error",
2371 __func__);
2372 } else {
2373 X509_free(cert);
2374 return -1;
2375 }
2376 }
2377 X509_free(cert);
2378 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2379 "to certificate store", __func__);
2380 return 0;
2381 }
2382
2383#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002384 /* Single alias */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002386 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002387 &ca_cert[11]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002389 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2390 return 0;
2391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002393 /* Multiple aliases separated by space */
2394 if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) {
2395 char *aliases = os_strdup(&ca_cert[12]);
2396 const char *delim = " ";
2397 int rc = 0;
2398 char *savedptr;
2399 char *alias;
2400
2401 if (!aliases)
2402 return -1;
2403 alias = strtok_r(aliases, delim, &savedptr);
2404 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2405 if (tls_add_ca_from_keystore_encoded(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002406 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002407 wpa_printf(MSG_WARNING,
2408 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2409 __func__, alias);
2410 rc = -1;
2411 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 }
2413 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002414 os_free(aliases);
2415 if (rc)
2416 return rc;
2417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2419 return 0;
2420 }
2421#endif /* ANDROID */
2422
2423#ifdef CONFIG_NATIVE_WINDOWS
2424 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2425 0) {
2426 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2427 "system certificate store");
2428 return 0;
2429 }
2430#endif /* CONFIG_NATIVE_WINDOWS */
2431
2432 if (ca_cert || ca_path) {
2433#ifndef OPENSSL_NO_STDIO
2434 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2435 1) {
2436 tls_show_errors(MSG_WARNING, __func__,
2437 "Failed to load root certificates");
2438 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002439 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2441 "DER format CA certificate",
2442 __func__);
2443 } else
2444 return -1;
2445 } else {
2446 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2447 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002448 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 }
2450#else /* OPENSSL_NO_STDIO */
2451 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2452 __func__);
2453 return -1;
2454#endif /* OPENSSL_NO_STDIO */
2455 } else {
2456 /* No ca_cert configured - do not try to verify server
2457 * certificate */
2458 conn->ca_cert_verify = 0;
2459 }
2460
2461 return 0;
2462}
2463
2464
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002465static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002467 SSL_CTX *ssl_ctx = data->ssl;
2468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 if (ca_cert) {
2470 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2471 {
2472 tls_show_errors(MSG_WARNING, __func__,
2473 "Failed to load root certificates");
2474 return -1;
2475 }
2476
2477 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2478 "certificate(s) loaded");
2479
2480#ifndef OPENSSL_NO_STDIO
2481 /* Add the same CAs to the client certificate requests */
2482 SSL_CTX_set_client_CA_list(ssl_ctx,
2483 SSL_load_client_CA_file(ca_cert));
2484#endif /* OPENSSL_NO_STDIO */
Hai Shalom74f70d42019-02-11 14:42:39 -08002485
2486 os_free(data->ca_cert);
2487 data->ca_cert = os_strdup(ca_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 }
2489
2490 return 0;
2491}
2492
2493
Hai Shalom74f70d42019-02-11 14:42:39 -08002494int tls_global_set_verify(void *ssl_ctx, int check_crl, int strict)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495{
2496 int flags;
2497
2498 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002499 struct tls_data *data = ssl_ctx;
2500 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 if (cs == NULL) {
2502 tls_show_errors(MSG_INFO, __func__, "Failed to get "
2503 "certificate store when enabling "
2504 "check_crl");
2505 return -1;
2506 }
2507 flags = X509_V_FLAG_CRL_CHECK;
2508 if (check_crl == 2)
2509 flags |= X509_V_FLAG_CRL_CHECK_ALL;
2510 X509_STORE_set_flags(cs, flags);
Hai Shalom74f70d42019-02-11 14:42:39 -08002511
2512 data->check_crl = check_crl;
2513 data->check_crl_strict = strict;
2514 os_get_reltime(&data->crl_last_reload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 }
2516 return 0;
2517}
2518
2519
2520static int tls_connection_set_subject_match(struct tls_connection *conn,
2521 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002522 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002523 const char *suffix_match,
2524 const char *domain_match)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525{
2526 os_free(conn->subject_match);
2527 conn->subject_match = NULL;
2528 if (subject_match) {
2529 conn->subject_match = os_strdup(subject_match);
2530 if (conn->subject_match == NULL)
2531 return -1;
2532 }
2533
2534 os_free(conn->altsubject_match);
2535 conn->altsubject_match = NULL;
2536 if (altsubject_match) {
2537 conn->altsubject_match = os_strdup(altsubject_match);
2538 if (conn->altsubject_match == NULL)
2539 return -1;
2540 }
2541
Dmitry Shmidt051af732013-10-22 13:52:46 -07002542 os_free(conn->suffix_match);
2543 conn->suffix_match = NULL;
2544 if (suffix_match) {
2545 conn->suffix_match = os_strdup(suffix_match);
2546 if (conn->suffix_match == NULL)
2547 return -1;
2548 }
2549
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002550 os_free(conn->domain_match);
2551 conn->domain_match = NULL;
2552 if (domain_match) {
2553 conn->domain_match = os_strdup(domain_match);
2554 if (conn->domain_match == NULL)
2555 return -1;
2556 }
2557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 return 0;
2559}
2560
2561
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002562#ifdef CONFIG_SUITEB
2563#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2564static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002565{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002566 struct tls_connection *conn = arg;
2567
2568 /*
2569 * This cert_cb() is not really the best location for doing a
2570 * constraint check for the ServerKeyExchange message, but this seems to
2571 * be the only place where the current OpenSSL sequence can be
2572 * terminated cleanly with an TLS alert going out to the server.
2573 */
2574
2575 if (!(conn->flags & TLS_CONN_SUITEB))
2576 return 1;
2577
2578 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
2579 if (conn->cipher_suite != 0x9f)
2580 return 1;
2581
2582 if (conn->server_dh_prime_len >= 3072)
2583 return 1;
2584
2585 wpa_printf(MSG_DEBUG,
2586 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
2587 conn->server_dh_prime_len);
2588 return 0;
2589}
2590#endif /* OPENSSL_VERSION_NUMBER */
2591#endif /* CONFIG_SUITEB */
2592
2593
Roshan Pius3a1667e2018-07-03 15:17:14 -07002594static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
2595 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002596{
2597 SSL *ssl = conn->ssl;
2598
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002599#ifdef SSL_OP_NO_TICKET
2600 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
2601 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002602 else
2603 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002604#endif /* SSL_OP_NO_TICKET */
2605
2606#ifdef SSL_OP_NO_TLSv1
2607 if (flags & TLS_CONN_DISABLE_TLSv1_0)
2608 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2609 else
2610 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
2611#endif /* SSL_OP_NO_TLSv1 */
2612#ifdef SSL_OP_NO_TLSv1_1
2613 if (flags & TLS_CONN_DISABLE_TLSv1_1)
2614 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2615 else
2616 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
2617#endif /* SSL_OP_NO_TLSv1_1 */
2618#ifdef SSL_OP_NO_TLSv1_2
2619 if (flags & TLS_CONN_DISABLE_TLSv1_2)
2620 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
2621 else
2622 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
2623#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002624#ifdef SSL_OP_NO_TLSv1_3
2625 if (flags & TLS_CONN_DISABLE_TLSv1_3)
2626 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
2627 else
2628 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
2629#endif /* SSL_OP_NO_TLSv1_3 */
Hai Shalom74f70d42019-02-11 14:42:39 -08002630#if OPENSSL_VERSION_NUMBER >= 0x10100000L
2631 if (flags & (TLS_CONN_ENABLE_TLSv1_0 |
2632 TLS_CONN_ENABLE_TLSv1_1 |
2633 TLS_CONN_ENABLE_TLSv1_2)) {
2634 int version = 0;
2635
2636 /* Explicit request to enable TLS versions even if needing to
2637 * override systemwide policies. */
2638 if (flags & TLS_CONN_ENABLE_TLSv1_0) {
2639 version = TLS1_VERSION;
2640 } else if (flags & TLS_CONN_ENABLE_TLSv1_1) {
2641 if (!(flags & TLS_CONN_DISABLE_TLSv1_0))
2642 version = TLS1_1_VERSION;
2643 } else if (flags & TLS_CONN_ENABLE_TLSv1_2) {
2644 if (!(flags & (TLS_CONN_DISABLE_TLSv1_0 |
2645 TLS_CONN_DISABLE_TLSv1_1)))
2646 version = TLS1_2_VERSION;
2647 }
2648 if (!version) {
2649 wpa_printf(MSG_DEBUG,
2650 "OpenSSL: Invalid TLS version configuration");
2651 return -1;
2652 }
2653
2654 if (SSL_set_min_proto_version(ssl, version) != 1) {
2655 wpa_printf(MSG_DEBUG,
2656 "OpenSSL: Failed to set minimum TLS version");
2657 return -1;
2658 }
2659 }
2660#endif /* >= 1.1.0 */
2661
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002662#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07002663#ifdef OPENSSL_IS_BORINGSSL
2664 /* Start with defaults from BoringSSL */
2665 SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, NULL, 0);
2666#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002667#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2668 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
2669 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
2670
Roshan Pius3a1667e2018-07-03 15:17:14 -07002671 if (openssl_ciphers) {
2672 wpa_printf(MSG_DEBUG,
2673 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
2674 openssl_ciphers);
2675 ciphers = openssl_ciphers;
2676 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002677 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
2678 wpa_printf(MSG_INFO,
2679 "OpenSSL: Failed to set Suite B ciphers");
2680 return -1;
2681 }
2682 } else if (flags & TLS_CONN_SUITEB) {
2683 EC_KEY *ecdh;
2684 const char *ciphers =
2685 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07002686 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002687
Roshan Pius3a1667e2018-07-03 15:17:14 -07002688 if (openssl_ciphers) {
2689 wpa_printf(MSG_DEBUG,
2690 "OpenSSL: Override ciphers for Suite B: %s",
2691 openssl_ciphers);
2692 ciphers = openssl_ciphers;
2693 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002694 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
2695 wpa_printf(MSG_INFO,
2696 "OpenSSL: Failed to set Suite B ciphers");
2697 return -1;
2698 }
2699
Roshan Pius3a1667e2018-07-03 15:17:14 -07002700 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002701 wpa_printf(MSG_INFO,
2702 "OpenSSL: Failed to set Suite B curves");
2703 return -1;
2704 }
2705
2706 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
2707 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
2708 EC_KEY_free(ecdh);
2709 wpa_printf(MSG_INFO,
2710 "OpenSSL: Failed to set ECDH parameter");
2711 return -1;
2712 }
2713 EC_KEY_free(ecdh);
2714 }
2715 if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002716#ifdef OPENSSL_IS_BORINGSSL
2717 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
2718
2719 if (SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, sigalgs,
2720 1) != 1) {
2721 wpa_printf(MSG_INFO,
2722 "OpenSSL: Failed to set Suite B sigalgs");
2723 return -1;
2724 }
2725#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002726 /* ECDSA+SHA384 if need to add EC support here */
2727 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
2728 wpa_printf(MSG_INFO,
2729 "OpenSSL: Failed to set Suite B sigalgs");
2730 return -1;
2731 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002732#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002733
2734 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2735 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2736 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
2737 }
2738#else /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2739 if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) {
2740 wpa_printf(MSG_ERROR,
2741 "OpenSSL: Suite B RSA case not supported with this OpenSSL version");
2742 return -1;
2743 }
2744#endif /* OPENSSL_VERSION_NUMBER */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002745
2746#ifdef OPENSSL_IS_BORINGSSL
2747 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
2748 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
2749 int nid[1] = { NID_secp384r1 };
2750
2751 if (SSL_set1_curves(ssl, nid, 1) != 1) {
2752 wpa_printf(MSG_INFO,
2753 "OpenSSL: Failed to set Suite B curves");
2754 return -1;
2755 }
2756
2757 if (SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, sigalgs,
2758 1) != 1) {
2759 wpa_printf(MSG_INFO,
2760 "OpenSSL: Failed to set Suite B sigalgs");
2761 return -1;
2762 }
2763 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002764#else /* OPENSSL_IS_BORINGSSL */
2765 if (!(flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) &&
2766 openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
2767 wpa_printf(MSG_INFO,
2768 "OpenSSL: Failed to set openssl_ciphers '%s'",
2769 openssl_ciphers);
2770 return -1;
2771 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002772#endif /* OPENSSL_IS_BORINGSSL */
Hai Shalom74f70d42019-02-11 14:42:39 -08002773#else /* CONFIG_SUITEB */
2774 if (openssl_ciphers && SSL_set_cipher_list(ssl, openssl_ciphers) != 1) {
2775 wpa_printf(MSG_INFO,
2776 "OpenSSL: Failed to set openssl_ciphers '%s'",
2777 openssl_ciphers);
2778 return -1;
2779 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002780#endif /* CONFIG_SUITEB */
2781
2782 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002783}
2784
2785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002787 int verify_peer, unsigned int flags,
2788 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789{
2790 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002791 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792
2793 if (conn == NULL)
2794 return -1;
2795
2796 if (verify_peer) {
2797 conn->ca_cert_verify = 1;
2798 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
2799 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
2800 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
2801 } else {
2802 conn->ca_cert_verify = 0;
2803 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
2804 }
2805
Roshan Pius3a1667e2018-07-03 15:17:14 -07002806 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002807 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002808 conn->flags = flags;
2809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 SSL_set_accept_state(conn->ssl);
2811
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002812 if (data->tls_session_lifetime == 0) {
2813 /*
2814 * Set session id context to a unique value to make sure
2815 * session resumption cannot be used either through session
2816 * caching or TLS ticket extension.
2817 */
2818 counter++;
2819 SSL_set_session_id_context(conn->ssl,
2820 (const unsigned char *) &counter,
2821 sizeof(counter));
2822 } else if (session_ctx) {
2823 SSL_set_session_id_context(conn->ssl, session_ctx,
2824 session_ctx_len);
2825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826
2827 return 0;
2828}
2829
2830
2831static int tls_connection_client_cert(struct tls_connection *conn,
2832 const char *client_cert,
2833 const u8 *client_cert_blob,
2834 size_t client_cert_blob_len)
2835{
2836 if (client_cert == NULL && client_cert_blob == NULL)
2837 return 0;
2838
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002839#ifdef PKCS12_FUNCS
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002840#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002841 /*
2842 * Clear previously set extra chain certificates, if any, from PKCS#12
2843 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2844 * chain properly.
2845 */
2846 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
2847#endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2848#endif /* PKCS12_FUNCS */
2849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 if (client_cert_blob &&
2851 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
2852 client_cert_blob_len) == 1) {
2853 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
2854 "OK");
2855 return 0;
2856 } else if (client_cert_blob) {
2857 tls_show_errors(MSG_DEBUG, __func__,
2858 "SSL_use_certificate_ASN1 failed");
2859 }
2860
2861 if (client_cert == NULL)
2862 return -1;
2863
2864#ifdef ANDROID
2865 if (os_strncmp("keystore://", client_cert, 11) == 0) {
2866 BIO *bio = BIO_from_keystore(&client_cert[11]);
2867 X509 *x509 = NULL;
2868 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002869 if (bio) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002871 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 if (x509) {
2873 if (SSL_use_certificate(conn->ssl, x509) == 1)
2874 ret = 0;
2875 X509_free(x509);
2876 }
Paul Stewart50772e82017-01-25 13:59:16 -08002877
2878 /* Read additional certificates into the chain. */
2879 while (bio) {
2880 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2881 if (x509) {
2882 /* Takes ownership of x509 */
2883 SSL_add0_chain_cert(conn->ssl, x509);
2884 } else {
2885 BIO_free(bio);
2886 bio = NULL;
2887 }
2888 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 return ret;
2890 }
2891#endif /* ANDROID */
2892
2893#ifndef OPENSSL_NO_STDIO
2894 if (SSL_use_certificate_file(conn->ssl, client_cert,
2895 SSL_FILETYPE_ASN1) == 1) {
2896 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
2897 " --> OK");
2898 return 0;
2899 }
2900
Hai Shalom74f70d42019-02-11 14:42:39 -08002901#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)\
2902 && !defined(ANDROID)
2903 if (SSL_use_certificate_chain_file(conn->ssl, client_cert) == 1) {
2904 ERR_clear_error();
2905 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_chain_file"
2906 " --> OK");
2907 return 0;
2908 }
2909#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 if (SSL_use_certificate_file(conn->ssl, client_cert,
2911 SSL_FILETYPE_PEM) == 1) {
2912 ERR_clear_error();
2913 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
2914 " --> OK");
2915 return 0;
2916 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002917#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918
2919 tls_show_errors(MSG_DEBUG, __func__,
2920 "SSL_use_certificate_file failed");
2921#else /* OPENSSL_NO_STDIO */
2922 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2923#endif /* OPENSSL_NO_STDIO */
2924
2925 return -1;
2926}
2927
2928
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002929static int tls_global_client_cert(struct tls_data *data,
2930 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931{
2932#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002933 SSL_CTX *ssl_ctx = data->ssl;
2934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002935 if (client_cert == NULL)
2936 return 0;
2937
2938 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2939 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002940 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2942 SSL_FILETYPE_PEM) != 1) {
2943 tls_show_errors(MSG_INFO, __func__,
2944 "Failed to load client certificate");
2945 return -1;
2946 }
2947 return 0;
2948#else /* OPENSSL_NO_STDIO */
2949 if (client_cert == NULL)
2950 return 0;
2951 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2952 return -1;
2953#endif /* OPENSSL_NO_STDIO */
2954}
2955
2956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002958static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 const char *passwd)
2960{
2961 EVP_PKEY *pkey;
2962 X509 *cert;
2963 STACK_OF(X509) *certs;
2964 int res = 0;
2965 char buf[256];
2966
2967 pkey = NULL;
2968 cert = NULL;
2969 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002970 if (!passwd)
2971 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
2973 tls_show_errors(MSG_DEBUG, __func__,
2974 "Failed to parse PKCS12 file");
2975 PKCS12_free(p12);
2976 return -1;
2977 }
2978 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
2979
2980 if (cert) {
2981 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2982 sizeof(buf));
2983 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
2984 "subject='%s'", buf);
2985 if (ssl) {
2986 if (SSL_use_certificate(ssl, cert) != 1)
2987 res = -1;
2988 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002989 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990 res = -1;
2991 }
2992 X509_free(cert);
2993 }
2994
2995 if (pkey) {
2996 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
2997 if (ssl) {
2998 if (SSL_use_PrivateKey(ssl, pkey) != 1)
2999 res = -1;
3000 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003001 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 res = -1;
3003 }
3004 EVP_PKEY_free(pkey);
3005 }
3006
3007 if (certs) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003008#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003009 if (ssl)
3010 SSL_clear_chain_certs(ssl);
3011 else
3012 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003013 while ((cert = sk_X509_pop(certs)) != NULL) {
3014 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3015 sizeof(buf));
3016 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3017 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003018 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
3019 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
3020 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003021 tls_show_errors(MSG_DEBUG, __func__,
3022 "Failed to add additional certificate");
3023 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003024 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003025 break;
3026 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003027 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003028 }
3029 if (!res) {
3030 /* Try to continue anyway */
3031 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003032 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003033#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003034 if (ssl)
3035 res = SSL_build_cert_chain(
3036 ssl,
3037 SSL_BUILD_CHAIN_FLAG_CHECK |
3038 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
3039 else
3040 res = SSL_CTX_build_cert_chain(
3041 data->ssl,
3042 SSL_BUILD_CHAIN_FLAG_CHECK |
3043 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003044 if (!res) {
3045 tls_show_errors(MSG_DEBUG, __func__,
3046 "Failed to build certificate chain");
3047 } else if (res == 2) {
3048 wpa_printf(MSG_DEBUG,
3049 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
3050 }
3051#endif /* OPENSSL_IS_BORINGSSL */
3052 /*
3053 * Try to continue regardless of result since it is possible for
3054 * the extra certificates not to be required.
3055 */
3056 res = 0;
3057#else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003058 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 while ((cert = sk_X509_pop(certs)) != NULL) {
3060 X509_NAME_oneline(X509_get_subject_name(cert), buf,
3061 sizeof(buf));
3062 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
3063 " from PKCS12: subject='%s'", buf);
3064 /*
3065 * There is no SSL equivalent for the chain cert - so
3066 * always add it to the context...
3067 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003068 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
3069 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003070 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 res = -1;
3072 break;
3073 }
3074 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003075 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003076#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 }
3078
3079 PKCS12_free(p12);
3080
3081 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003082 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083
3084 return res;
3085}
3086#endif /* PKCS12_FUNCS */
3087
3088
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003089static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
3090 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091{
3092#ifdef PKCS12_FUNCS
3093 FILE *f;
3094 PKCS12 *p12;
3095
3096 f = fopen(private_key, "rb");
3097 if (f == NULL)
3098 return -1;
3099
3100 p12 = d2i_PKCS12_fp(f, NULL);
3101 fclose(f);
3102
3103 if (p12 == NULL) {
3104 tls_show_errors(MSG_INFO, __func__,
3105 "Failed to use PKCS#12 file");
3106 return -1;
3107 }
3108
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003109 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110
3111#else /* PKCS12_FUNCS */
3112 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
3113 "p12/pfx files");
3114 return -1;
3115#endif /* PKCS12_FUNCS */
3116}
3117
3118
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003119static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 const u8 *blob, size_t len, const char *passwd)
3121{
3122#ifdef PKCS12_FUNCS
3123 PKCS12 *p12;
3124
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003125 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126 if (p12 == NULL) {
3127 tls_show_errors(MSG_INFO, __func__,
3128 "Failed to use PKCS#12 blob");
3129 return -1;
3130 }
3131
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003132 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133
3134#else /* PKCS12_FUNCS */
3135 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
3136 "p12/pfx blobs");
3137 return -1;
3138#endif /* PKCS12_FUNCS */
3139}
3140
3141
3142#ifndef OPENSSL_NO_ENGINE
3143static int tls_engine_get_cert(struct tls_connection *conn,
3144 const char *cert_id,
3145 X509 **cert)
3146{
3147 /* this runs after the private key is loaded so no PIN is required */
3148 struct {
3149 const char *cert_id;
3150 X509 *cert;
3151 } params;
3152 params.cert_id = cert_id;
3153 params.cert = NULL;
3154
3155 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
3156 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003157 unsigned long err = ERR_get_error();
3158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003159 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
3160 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003161 ERR_error_string(err, NULL));
3162 if (tls_is_pin_error(err))
3163 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3165 }
3166 if (!params.cert) {
3167 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
3168 " '%s'", cert_id);
3169 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
3170 }
3171 *cert = params.cert;
3172 return 0;
3173}
3174#endif /* OPENSSL_NO_ENGINE */
3175
3176
3177static int tls_connection_engine_client_cert(struct tls_connection *conn,
3178 const char *cert_id)
3179{
3180#ifndef OPENSSL_NO_ENGINE
3181 X509 *cert;
3182
3183 if (tls_engine_get_cert(conn, cert_id, &cert))
3184 return -1;
3185
3186 if (!SSL_use_certificate(conn->ssl, cert)) {
3187 tls_show_errors(MSG_ERROR, __func__,
3188 "SSL_use_certificate failed");
3189 X509_free(cert);
3190 return -1;
3191 }
3192 X509_free(cert);
3193 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3194 "OK");
3195 return 0;
3196
3197#else /* OPENSSL_NO_ENGINE */
3198 return -1;
3199#endif /* OPENSSL_NO_ENGINE */
3200}
3201
3202
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003203static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204 struct tls_connection *conn,
3205 const char *ca_cert_id)
3206{
3207#ifndef OPENSSL_NO_ENGINE
3208 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003209 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003210 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211
3212 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3213 return -1;
3214
3215 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003216 store = X509_STORE_new();
3217 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3219 "certificate store", __func__);
3220 X509_free(cert);
3221 return -1;
3222 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003223 SSL_CTX_set_cert_store(ssl_ctx, store);
3224 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 unsigned long err = ERR_peek_error();
3226 tls_show_errors(MSG_WARNING, __func__,
3227 "Failed to add CA certificate from engine "
3228 "to certificate store");
3229 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3230 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3231 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3232 " already in hash table error",
3233 __func__);
3234 } else {
3235 X509_free(cert);
3236 return -1;
3237 }
3238 }
3239 X509_free(cert);
3240 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3241 "to certificate store", __func__);
3242 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003243 conn->ca_cert_verify = 1;
3244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 return 0;
3246
3247#else /* OPENSSL_NO_ENGINE */
3248 return -1;
3249#endif /* OPENSSL_NO_ENGINE */
3250}
3251
3252
3253static int tls_connection_engine_private_key(struct tls_connection *conn)
3254{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003255#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3257 tls_show_errors(MSG_ERROR, __func__,
3258 "ENGINE: cannot use private key for TLS");
3259 return -1;
3260 }
3261 if (!SSL_check_private_key(conn->ssl)) {
3262 tls_show_errors(MSG_INFO, __func__,
3263 "Private key failed verification");
3264 return -1;
3265 }
3266 return 0;
3267#else /* OPENSSL_NO_ENGINE */
3268 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3269 "engine support was not compiled in");
3270 return -1;
3271#endif /* OPENSSL_NO_ENGINE */
3272}
3273
3274
Roshan Pius3a1667e2018-07-03 15:17:14 -07003275#ifndef OPENSSL_NO_STDIO
3276static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003277{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003278 if (!password)
3279 return 0;
3280 os_strlcpy(buf, (const char *) password, size);
3281 return os_strlen(buf);
3282}
3283#endif /* OPENSSL_NO_STDIO */
3284
3285
3286static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3287 const char *private_key,
3288 const char *private_key_passwd)
3289{
3290#ifndef OPENSSL_NO_STDIO
3291 BIO *bio;
3292 EVP_PKEY *pkey;
3293 int ret;
3294
3295 /* First try ASN.1 (DER). */
3296 bio = BIO_new_file(private_key, "r");
3297 if (!bio)
3298 return -1;
3299 pkey = d2i_PrivateKey_bio(bio, NULL);
3300 BIO_free(bio);
3301
3302 if (pkey) {
3303 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3304 } else {
3305 /* Try PEM with the provided password. */
3306 bio = BIO_new_file(private_key, "r");
3307 if (!bio)
3308 return -1;
3309 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3310 (void *) private_key_passwd);
3311 BIO_free(bio);
3312 if (!pkey)
3313 return -1;
3314 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3315 /* Clear errors from the previous failed load. */
3316 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003317 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003318
3319 if (ssl)
3320 ret = SSL_use_PrivateKey(ssl, pkey);
3321 else
3322 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3323
3324 EVP_PKEY_free(pkey);
3325 return ret == 1 ? 0 : -1;
3326#else /* OPENSSL_NO_STDIO */
3327 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3328 return -1;
3329#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003330}
3331
3332
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003333static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 struct tls_connection *conn,
3335 const char *private_key,
3336 const char *private_key_passwd,
3337 const u8 *private_key_blob,
3338 size_t private_key_blob_len)
3339{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340 int ok;
3341
3342 if (private_key == NULL && private_key_blob == NULL)
3343 return 0;
3344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 ok = 0;
3346 while (private_key_blob) {
3347 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3348 (u8 *) private_key_blob,
3349 private_key_blob_len) == 1) {
3350 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3351 "ASN1(EVP_PKEY_RSA) --> OK");
3352 ok = 1;
3353 break;
3354 }
3355
3356 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3357 (u8 *) private_key_blob,
3358 private_key_blob_len) == 1) {
3359 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3360 "ASN1(EVP_PKEY_DSA) --> OK");
3361 ok = 1;
3362 break;
3363 }
3364
3365 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
3366 (u8 *) private_key_blob,
3367 private_key_blob_len) == 1) {
3368 wpa_printf(MSG_DEBUG, "OpenSSL: "
3369 "SSL_use_RSAPrivateKey_ASN1 --> OK");
3370 ok = 1;
3371 break;
3372 }
3373
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003374 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003375 private_key_blob_len,
3376 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
3378 "OK");
3379 ok = 1;
3380 break;
3381 }
3382
3383 break;
3384 }
3385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003387 if (tls_use_private_key_file(data, conn->ssl, private_key,
3388 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389 ok = 1;
3390 break;
3391 }
3392
Roshan Pius3a1667e2018-07-03 15:17:14 -07003393 if (tls_read_pkcs12(data, conn->ssl, private_key,
3394 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
3396 "--> OK");
3397 ok = 1;
3398 break;
3399 }
3400
3401 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
3402 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
3403 "access certificate store --> OK");
3404 ok = 1;
3405 break;
3406 }
3407
3408 break;
3409 }
3410
3411 if (!ok) {
3412 tls_show_errors(MSG_INFO, __func__,
3413 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414 return -1;
3415 }
3416 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 if (!SSL_check_private_key(conn->ssl)) {
3419 tls_show_errors(MSG_INFO, __func__, "Private key failed "
3420 "verification");
3421 return -1;
3422 }
3423
3424 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
3425 return 0;
3426}
3427
3428
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003429static int tls_global_private_key(struct tls_data *data,
3430 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431 const char *private_key_passwd)
3432{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003433 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434
3435 if (private_key == NULL)
3436 return 0;
3437
Roshan Pius3a1667e2018-07-03 15:17:14 -07003438 if (tls_use_private_key_file(data, NULL, private_key,
3439 private_key_passwd) &&
3440 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 tls_show_errors(MSG_INFO, __func__,
3442 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 ERR_clear_error();
3444 return -1;
3445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 if (!SSL_CTX_check_private_key(ssl_ctx)) {
3449 tls_show_errors(MSG_INFO, __func__,
3450 "Private key failed verification");
3451 return -1;
3452 }
3453
3454 return 0;
3455}
3456
3457
3458static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
3459{
3460#ifdef OPENSSL_NO_DH
3461 if (dh_file == NULL)
3462 return 0;
3463 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3464 "dh_file specified");
3465 return -1;
3466#else /* OPENSSL_NO_DH */
3467 DH *dh;
3468 BIO *bio;
3469
3470 /* TODO: add support for dh_blob */
3471 if (dh_file == NULL)
3472 return 0;
3473 if (conn == NULL)
3474 return -1;
3475
3476 bio = BIO_new_file(dh_file, "r");
3477 if (bio == NULL) {
3478 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3479 dh_file, ERR_error_string(ERR_get_error(), NULL));
3480 return -1;
3481 }
3482 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3483 BIO_free(bio);
3484#ifndef OPENSSL_NO_DSA
3485 while (dh == NULL) {
3486 DSA *dsa;
3487 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3488 " trying to parse as DSA params", dh_file,
3489 ERR_error_string(ERR_get_error(), NULL));
3490 bio = BIO_new_file(dh_file, "r");
3491 if (bio == NULL)
3492 break;
3493 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3494 BIO_free(bio);
3495 if (!dsa) {
3496 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3497 "'%s': %s", dh_file,
3498 ERR_error_string(ERR_get_error(), NULL));
3499 break;
3500 }
3501
3502 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3503 dh = DSA_dup_DH(dsa);
3504 DSA_free(dsa);
3505 if (dh == NULL) {
3506 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3507 "params into DH params");
3508 break;
3509 }
3510 break;
3511 }
3512#endif /* !OPENSSL_NO_DSA */
3513 if (dh == NULL) {
3514 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3515 "'%s'", dh_file);
3516 return -1;
3517 }
3518
3519 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
3520 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3521 "%s", dh_file,
3522 ERR_error_string(ERR_get_error(), NULL));
3523 DH_free(dh);
3524 return -1;
3525 }
3526 DH_free(dh);
3527 return 0;
3528#endif /* OPENSSL_NO_DH */
3529}
3530
3531
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003532static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533{
3534#ifdef OPENSSL_NO_DH
3535 if (dh_file == NULL)
3536 return 0;
3537 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3538 "dh_file specified");
3539 return -1;
3540#else /* OPENSSL_NO_DH */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003541 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 DH *dh;
3543 BIO *bio;
3544
3545 /* TODO: add support for dh_blob */
3546 if (dh_file == NULL)
3547 return 0;
3548 if (ssl_ctx == NULL)
3549 return -1;
3550
3551 bio = BIO_new_file(dh_file, "r");
3552 if (bio == NULL) {
3553 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3554 dh_file, ERR_error_string(ERR_get_error(), NULL));
3555 return -1;
3556 }
3557 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3558 BIO_free(bio);
3559#ifndef OPENSSL_NO_DSA
3560 while (dh == NULL) {
3561 DSA *dsa;
3562 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3563 " trying to parse as DSA params", dh_file,
3564 ERR_error_string(ERR_get_error(), NULL));
3565 bio = BIO_new_file(dh_file, "r");
3566 if (bio == NULL)
3567 break;
3568 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3569 BIO_free(bio);
3570 if (!dsa) {
3571 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3572 "'%s': %s", dh_file,
3573 ERR_error_string(ERR_get_error(), NULL));
3574 break;
3575 }
3576
3577 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3578 dh = DSA_dup_DH(dsa);
3579 DSA_free(dsa);
3580 if (dh == NULL) {
3581 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3582 "params into DH params");
3583 break;
3584 }
3585 break;
3586 }
3587#endif /* !OPENSSL_NO_DSA */
3588 if (dh == NULL) {
3589 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3590 "'%s'", dh_file);
3591 return -1;
3592 }
3593
3594 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
3595 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3596 "%s", dh_file,
3597 ERR_error_string(ERR_get_error(), NULL));
3598 DH_free(dh);
3599 return -1;
3600 }
3601 DH_free(dh);
3602 return 0;
3603#endif /* OPENSSL_NO_DH */
3604}
3605
3606
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003607int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
3608 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609{
3610 SSL *ssl;
3611
3612 if (conn == NULL || keys == NULL)
3613 return -1;
3614 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003615 if (ssl == NULL)
3616 return -1;
3617
3618 os_memset(keys, 0, sizeof(*keys));
3619 keys->client_random = conn->client_random;
3620 keys->client_random_len = SSL_get_client_random(
3621 ssl, conn->client_random, sizeof(conn->client_random));
3622 keys->server_random = conn->server_random;
3623 keys->server_random_len = SSL_get_server_random(
3624 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625
3626 return 0;
3627}
3628
3629
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003630#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003631static int openssl_get_keyblock_size(SSL *ssl)
3632{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003633#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
3634 (defined(LIBRESSL_VERSION_NUMBER) && \
3635 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003636 const EVP_CIPHER *c;
3637 const EVP_MD *h;
3638 int md_size;
3639
3640 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
3641 ssl->read_hash == NULL)
3642 return -1;
3643
3644 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003645 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003646 if (h)
3647 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003648 else if (ssl->s3)
3649 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003650 else
3651 return -1;
3652
3653 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3654 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
3655 EVP_CIPHER_iv_length(c));
3656 return 2 * (EVP_CIPHER_key_length(c) +
3657 md_size +
3658 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003659#else
3660 const SSL_CIPHER *ssl_cipher;
3661 int cipher, digest;
3662 const EVP_CIPHER *c;
3663 const EVP_MD *h;
3664
3665 ssl_cipher = SSL_get_current_cipher(ssl);
3666 if (!ssl_cipher)
3667 return -1;
3668 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
3669 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
3670 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
3671 cipher, digest);
3672 if (cipher < 0 || digest < 0)
3673 return -1;
3674 c = EVP_get_cipherbynid(cipher);
3675 h = EVP_get_digestbynid(digest);
3676 if (!c || !h)
3677 return -1;
3678
3679 wpa_printf(MSG_DEBUG,
3680 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3681 EVP_CIPHER_key_length(c), EVP_MD_size(h),
3682 EVP_CIPHER_iv_length(c));
3683 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
3684 EVP_CIPHER_iv_length(c));
3685#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003686}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003687#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003688
3689
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003690int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
3691 const char *label, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003692{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003693 if (!conn ||
3694 SSL_export_keying_material(conn->ssl, out, out_len, label,
3695 os_strlen(label), NULL, 0, 0) != 1)
3696 return -1;
3697 return 0;
3698}
3699
3700
3701int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
3702 u8 *out, size_t out_len)
3703{
3704#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003705 SSL *ssl;
3706 SSL_SESSION *sess;
3707 u8 *rnd;
3708 int ret = -1;
3709 int skip = 0;
3710 u8 *tmp_out = NULL;
3711 u8 *_out = out;
3712 unsigned char client_random[SSL3_RANDOM_SIZE];
3713 unsigned char server_random[SSL3_RANDOM_SIZE];
3714 unsigned char master_key[64];
3715 size_t master_key_len;
3716 const char *ver;
3717
3718 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003719 * TLS library did not support EAP-FAST key generation, so get the
3720 * needed TLS session parameters and use an internal implementation of
3721 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003722 */
3723
3724 if (conn == NULL)
3725 return -1;
3726 ssl = conn->ssl;
3727 if (ssl == NULL)
3728 return -1;
3729 ver = SSL_get_version(ssl);
3730 sess = SSL_get_session(ssl);
3731 if (!ver || !sess)
3732 return -1;
3733
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003734 skip = openssl_get_keyblock_size(ssl);
3735 if (skip < 0)
3736 return -1;
3737 tmp_out = os_malloc(skip + out_len);
3738 if (!tmp_out)
3739 return -1;
3740 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003741
3742 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
3743 if (!rnd) {
3744 os_free(tmp_out);
3745 return -1;
3746 }
3747
3748 SSL_get_client_random(ssl, client_random, sizeof(client_random));
3749 SSL_get_server_random(ssl, server_random, sizeof(server_random));
3750 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
3751 sizeof(master_key));
3752
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003753 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
3754 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003755
3756 if (os_strcmp(ver, "TLSv1.2") == 0) {
3757 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003758 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003759 _out, skip + out_len);
3760 ret = 0;
3761 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003762 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003763 _out, skip + out_len) == 0) {
3764 ret = 0;
3765 }
3766 os_memset(master_key, 0, sizeof(master_key));
3767 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003768 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003769 os_memcpy(out, _out + skip, out_len);
3770 bin_clear_free(tmp_out, skip);
3771
3772 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003773#else /* OPENSSL_NEED_EAP_FAST_PRF */
3774 wpa_printf(MSG_ERROR,
3775 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3776 return -1;
3777#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778}
3779
3780
3781static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07003782openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783{
3784 int res;
3785 struct wpabuf *out_data;
3786
3787 /*
3788 * Give TLS handshake data from the server (if available) to OpenSSL
3789 * for processing.
3790 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003791 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
3793 < 0) {
3794 tls_show_errors(MSG_INFO, __func__,
3795 "Handshake failed - BIO_write");
3796 return NULL;
3797 }
3798
3799 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003800 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 res = SSL_accept(conn->ssl);
3802 else
3803 res = SSL_connect(conn->ssl);
3804 if (res != 1) {
3805 int err = SSL_get_error(conn->ssl, res);
3806 if (err == SSL_ERROR_WANT_READ)
3807 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
3808 "more data");
3809 else if (err == SSL_ERROR_WANT_WRITE)
3810 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
3811 "write");
3812 else {
3813 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
3814 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003815 if (!conn->server && !conn->client_hello_generated) {
3816 /* The server would not understand TLS Alert
3817 * before ClientHello, so simply terminate
3818 * handshake on this type of error case caused
3819 * by a likely internal error like no ciphers
3820 * available. */
3821 wpa_printf(MSG_DEBUG,
3822 "OpenSSL: Could not generate ClientHello");
3823 conn->write_alerts++;
3824 return NULL;
3825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 }
3827 }
3828
Roshan Pius3a1667e2018-07-03 15:17:14 -07003829 if (!conn->server && !conn->failed)
3830 conn->client_hello_generated = 1;
3831
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003832#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003833 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003834 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
3835 conn->server_dh_prime_len < 3072) {
3836 struct tls_context *context = conn->context;
3837
3838 /*
3839 * This should not be reached since earlier cert_cb should have
3840 * terminated the handshake. Keep this check here for extra
3841 * protection if anything goes wrong with the more low-level
3842 * checks based on having to parse the TLS handshake messages.
3843 */
3844 wpa_printf(MSG_DEBUG,
3845 "OpenSSL: Server DH prime length: %d bits",
3846 conn->server_dh_prime_len);
3847
3848 if (context->event_cb) {
3849 union tls_event_data ev;
3850
3851 os_memset(&ev, 0, sizeof(ev));
3852 ev.alert.is_local = 1;
3853 ev.alert.type = "fatal";
3854 ev.alert.description = "insufficient security";
3855 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
3856 }
3857 /*
3858 * Could send a TLS Alert to the server, but for now, simply
3859 * terminate handshake.
3860 */
3861 conn->failed++;
3862 conn->write_alerts++;
3863 return NULL;
3864 }
3865#endif /* CONFIG_SUITEB */
3866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 /* Get the TLS handshake data to be sent to the server */
3868 res = BIO_ctrl_pending(conn->ssl_out);
3869 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
3870 out_data = wpabuf_alloc(res);
3871 if (out_data == NULL) {
3872 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
3873 "handshake output (%d bytes)", res);
3874 if (BIO_reset(conn->ssl_out) < 0) {
3875 tls_show_errors(MSG_INFO, __func__,
3876 "BIO_reset failed");
3877 }
3878 return NULL;
3879 }
3880 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
3881 res);
3882 if (res < 0) {
3883 tls_show_errors(MSG_INFO, __func__,
3884 "Handshake failed - BIO_read");
3885 if (BIO_reset(conn->ssl_out) < 0) {
3886 tls_show_errors(MSG_INFO, __func__,
3887 "BIO_reset failed");
3888 }
3889 wpabuf_free(out_data);
3890 return NULL;
3891 }
3892 wpabuf_put(out_data, res);
3893
3894 return out_data;
3895}
3896
3897
3898static struct wpabuf *
3899openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
3900{
3901 struct wpabuf *appl_data;
3902 int res;
3903
3904 appl_data = wpabuf_alloc(max_len + 100);
3905 if (appl_data == NULL)
3906 return NULL;
3907
3908 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
3909 wpabuf_size(appl_data));
3910 if (res < 0) {
3911 int err = SSL_get_error(conn->ssl, res);
3912 if (err == SSL_ERROR_WANT_READ ||
3913 err == SSL_ERROR_WANT_WRITE) {
3914 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
3915 "included");
3916 } else {
3917 tls_show_errors(MSG_INFO, __func__,
3918 "Failed to read possible "
3919 "Application Data");
3920 }
3921 wpabuf_free(appl_data);
3922 return NULL;
3923 }
3924
3925 wpabuf_put(appl_data, res);
3926 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
3927 "message", appl_data);
3928
3929 return appl_data;
3930}
3931
3932
3933static struct wpabuf *
3934openssl_connection_handshake(struct tls_connection *conn,
3935 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003936 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937{
3938 struct wpabuf *out_data;
3939
3940 if (appl_data)
3941 *appl_data = NULL;
3942
Roshan Pius3a1667e2018-07-03 15:17:14 -07003943 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 if (out_data == NULL)
3945 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03003946 if (conn->invalid_hb_used) {
3947 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3948 wpabuf_free(out_data);
3949 return NULL;
3950 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003952 if (SSL_is_init_finished(conn->ssl)) {
3953 wpa_printf(MSG_DEBUG,
3954 "OpenSSL: Handshake finished - resumed=%d",
3955 tls_connection_resumed(conn->ssl_ctx, conn));
3956 if (appl_data && in_data)
3957 *appl_data = openssl_get_appl_data(conn,
3958 wpabuf_len(in_data));
3959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960
Jouni Malinen26af48b2014-04-09 13:02:53 +03003961 if (conn->invalid_hb_used) {
3962 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3963 if (appl_data) {
3964 wpabuf_free(*appl_data);
3965 *appl_data = NULL;
3966 }
3967 wpabuf_free(out_data);
3968 return NULL;
3969 }
3970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 return out_data;
3972}
3973
3974
3975struct wpabuf *
3976tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
3977 const struct wpabuf *in_data,
3978 struct wpabuf **appl_data)
3979{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003980 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981}
3982
3983
3984struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
3985 struct tls_connection *conn,
3986 const struct wpabuf *in_data,
3987 struct wpabuf **appl_data)
3988{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003989 conn->server = 1;
3990 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991}
3992
3993
3994struct wpabuf * tls_connection_encrypt(void *tls_ctx,
3995 struct tls_connection *conn,
3996 const struct wpabuf *in_data)
3997{
3998 int res;
3999 struct wpabuf *buf;
4000
4001 if (conn == NULL)
4002 return NULL;
4003
4004 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
4005 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
4006 (res = BIO_reset(conn->ssl_out)) < 0) {
4007 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4008 return NULL;
4009 }
4010 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
4011 if (res < 0) {
4012 tls_show_errors(MSG_INFO, __func__,
4013 "Encryption failed - SSL_write");
4014 return NULL;
4015 }
4016
4017 /* Read encrypted data to be sent to the server */
4018 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
4019 if (buf == NULL)
4020 return NULL;
4021 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
4022 if (res < 0) {
4023 tls_show_errors(MSG_INFO, __func__,
4024 "Encryption failed - BIO_read");
4025 wpabuf_free(buf);
4026 return NULL;
4027 }
4028 wpabuf_put(buf, res);
4029
4030 return buf;
4031}
4032
4033
4034struct wpabuf * tls_connection_decrypt(void *tls_ctx,
4035 struct tls_connection *conn,
4036 const struct wpabuf *in_data)
4037{
4038 int res;
4039 struct wpabuf *buf;
4040
4041 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
4042 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
4043 wpabuf_len(in_data));
4044 if (res < 0) {
4045 tls_show_errors(MSG_INFO, __func__,
4046 "Decryption failed - BIO_write");
4047 return NULL;
4048 }
4049 if (BIO_reset(conn->ssl_out) < 0) {
4050 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
4051 return NULL;
4052 }
4053
4054 /* Read decrypted data for further processing */
4055 /*
4056 * Even though we try to disable TLS compression, it is possible that
4057 * this cannot be done with all TLS libraries. Add extra buffer space
4058 * to handle the possibility of the decrypted data being longer than
4059 * input data.
4060 */
4061 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
4062 if (buf == NULL)
4063 return NULL;
4064 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
4065 if (res < 0) {
4066 tls_show_errors(MSG_INFO, __func__,
4067 "Decryption failed - SSL_read");
4068 wpabuf_free(buf);
4069 return NULL;
4070 }
4071 wpabuf_put(buf, res);
4072
Jouni Malinen26af48b2014-04-09 13:02:53 +03004073 if (conn->invalid_hb_used) {
4074 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
4075 wpabuf_free(buf);
4076 return NULL;
4077 }
4078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 return buf;
4080}
4081
4082
4083int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
4084{
Hai Shalomce48b4a2018-09-05 11:41:35 -07004085 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086}
4087
4088
4089int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
4090 u8 *ciphers)
4091{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004092 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 u8 *c;
4094 int ret;
4095
4096 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
4097 return -1;
4098
4099 buf[0] = '\0';
4100 pos = buf;
4101 end = pos + sizeof(buf);
4102
4103 c = ciphers;
4104 while (*c != TLS_CIPHER_NONE) {
4105 const char *suite;
4106
4107 switch (*c) {
4108 case TLS_CIPHER_RC4_SHA:
4109 suite = "RC4-SHA";
4110 break;
4111 case TLS_CIPHER_AES128_SHA:
4112 suite = "AES128-SHA";
4113 break;
4114 case TLS_CIPHER_RSA_DHE_AES128_SHA:
4115 suite = "DHE-RSA-AES128-SHA";
4116 break;
4117 case TLS_CIPHER_ANON_DH_AES128_SHA:
4118 suite = "ADH-AES128-SHA";
4119 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004120 case TLS_CIPHER_RSA_DHE_AES256_SHA:
4121 suite = "DHE-RSA-AES256-SHA";
4122 break;
4123 case TLS_CIPHER_AES256_SHA:
4124 suite = "AES256-SHA";
4125 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 default:
4127 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
4128 "cipher selection: %d", *c);
4129 return -1;
4130 }
4131 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004132 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 break;
4134 pos += ret;
4135
4136 c++;
4137 }
4138
4139 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
4140
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004141#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004142#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4143 if (os_strstr(buf, ":ADH-")) {
4144 /*
4145 * Need to drop to security level 0 to allow anonymous
4146 * cipher suites for EAP-FAST.
4147 */
4148 SSL_set_security_level(conn->ssl, 0);
4149 } else if (SSL_get_security_level(conn->ssl) == 0) {
4150 /* Force at least security level 1 */
4151 SSL_set_security_level(conn->ssl, 1);
4152 }
4153#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4154#endif
4155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
4157 tls_show_errors(MSG_INFO, __func__,
4158 "Cipher suite configuration failed");
4159 return -1;
4160 }
4161
4162 return 0;
4163}
4164
4165
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004166int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
4167 char *buf, size_t buflen)
4168{
4169 const char *name;
4170 if (conn == NULL || conn->ssl == NULL)
4171 return -1;
4172
4173 name = SSL_get_version(conn->ssl);
4174 if (name == NULL)
4175 return -1;
4176
4177 os_strlcpy(buf, name, buflen);
4178 return 0;
4179}
4180
4181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
4183 char *buf, size_t buflen)
4184{
4185 const char *name;
4186 if (conn == NULL || conn->ssl == NULL)
4187 return -1;
4188
4189 name = SSL_get_cipher(conn->ssl);
4190 if (name == NULL)
4191 return -1;
4192
4193 os_strlcpy(buf, name, buflen);
4194 return 0;
4195}
4196
4197
4198int tls_connection_enable_workaround(void *ssl_ctx,
4199 struct tls_connection *conn)
4200{
4201 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4202
4203 return 0;
4204}
4205
4206
4207#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4208/* ClientHello TLS extensions require a patch to openssl, so this function is
4209 * commented out unless explicitly needed for EAP-FAST in order to be able to
4210 * build this file with unmodified openssl. */
4211int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4212 int ext_type, const u8 *data,
4213 size_t data_len)
4214{
4215 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4216 return -1;
4217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4219 data_len) != 1)
4220 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004221
4222 return 0;
4223}
4224#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4225
4226
4227int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4228{
4229 if (conn == NULL)
4230 return -1;
4231 return conn->failed;
4232}
4233
4234
4235int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
4236{
4237 if (conn == NULL)
4238 return -1;
4239 return conn->read_alerts;
4240}
4241
4242
4243int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
4244{
4245 if (conn == NULL)
4246 return -1;
4247 return conn->write_alerts;
4248}
4249
4250
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004251#ifdef HAVE_OCSP
4252
4253static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
4254{
4255#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004256 BIO *out;
4257 size_t rlen;
4258 char *txt;
4259 int res;
4260
4261 if (wpa_debug_level > MSG_DEBUG)
4262 return;
4263
4264 out = BIO_new(BIO_s_mem());
4265 if (!out)
4266 return;
4267
4268 OCSP_RESPONSE_print(out, rsp, 0);
4269 rlen = BIO_ctrl_pending(out);
4270 txt = os_malloc(rlen + 1);
4271 if (!txt) {
4272 BIO_free(out);
4273 return;
4274 }
4275
4276 res = BIO_read(out, txt, rlen);
4277 if (res > 0) {
4278 txt[res] = '\0';
4279 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
4280 }
4281 os_free(txt);
4282 BIO_free(out);
4283#endif /* CONFIG_NO_STDOUT_DEBUG */
4284}
4285
4286
Dmitry Shmidt71757432014-06-02 13:50:35 -07004287static void debug_print_cert(X509 *cert, const char *title)
4288{
4289#ifndef CONFIG_NO_STDOUT_DEBUG
4290 BIO *out;
4291 size_t rlen;
4292 char *txt;
4293 int res;
4294
4295 if (wpa_debug_level > MSG_DEBUG)
4296 return;
4297
4298 out = BIO_new(BIO_s_mem());
4299 if (!out)
4300 return;
4301
4302 X509_print(out, cert);
4303 rlen = BIO_ctrl_pending(out);
4304 txt = os_malloc(rlen + 1);
4305 if (!txt) {
4306 BIO_free(out);
4307 return;
4308 }
4309
4310 res = BIO_read(out, txt, rlen);
4311 if (res > 0) {
4312 txt[res] = '\0';
4313 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
4314 }
4315 os_free(txt);
4316
4317 BIO_free(out);
4318#endif /* CONFIG_NO_STDOUT_DEBUG */
4319}
4320
4321
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004322static int ocsp_resp_cb(SSL *s, void *arg)
4323{
4324 struct tls_connection *conn = arg;
4325 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004326 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004327 OCSP_RESPONSE *rsp;
4328 OCSP_BASICRESP *basic;
4329 OCSP_CERTID *id;
4330 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004331 X509_STORE *store;
4332 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004333
4334 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
4335 if (!p) {
4336 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
4337 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
4338 }
4339
4340 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
4341
4342 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
4343 if (!rsp) {
4344 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
4345 return 0;
4346 }
4347
4348 ocsp_debug_print_resp(rsp);
4349
4350 status = OCSP_response_status(rsp);
4351 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
4352 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
4353 status, OCSP_response_status_str(status));
4354 return 0;
4355 }
4356
4357 basic = OCSP_response_get1_basic(rsp);
4358 if (!basic) {
4359 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
4360 return 0;
4361 }
4362
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004363 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004364 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07004365 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004366
4367 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
4368 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004369 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004370 }
4371 certs = sk_X509_new_null();
4372 if (certs) {
4373 X509 *cert;
4374 cert = X509_dup(conn->peer_issuer);
4375 if (cert && !sk_X509_push(certs, cert)) {
4376 tls_show_errors(
4377 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004378 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004379 X509_free(cert);
4380 sk_X509_free(certs);
4381 certs = NULL;
4382 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004383 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004384 cert = X509_dup(conn->peer_issuer_issuer);
4385 if (cert && !sk_X509_push(certs, cert)) {
4386 tls_show_errors(
4387 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004388 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004389 X509_free(cert);
4390 }
4391 }
4392 }
4393 }
4394
4395 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
4396 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004397 if (status <= 0) {
4398 tls_show_errors(MSG_INFO, __func__,
4399 "OpenSSL: OCSP response failed verification");
4400 OCSP_BASICRESP_free(basic);
4401 OCSP_RESPONSE_free(rsp);
4402 return 0;
4403 }
4404
4405 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
4406
Dmitry Shmidt56052862013-10-04 10:23:25 -07004407 if (!conn->peer_cert) {
4408 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
4409 OCSP_BASICRESP_free(basic);
4410 OCSP_RESPONSE_free(rsp);
4411 return 0;
4412 }
4413
4414 if (!conn->peer_issuer) {
4415 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004416 OCSP_BASICRESP_free(basic);
4417 OCSP_RESPONSE_free(rsp);
4418 return 0;
4419 }
4420
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004421 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004422 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004423 wpa_printf(MSG_DEBUG,
4424 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004425 OCSP_BASICRESP_free(basic);
4426 OCSP_RESPONSE_free(rsp);
4427 return 0;
4428 }
4429
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004430 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
4431 &this_update, &next_update);
4432 if (!res) {
4433 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
4434 if (!id) {
4435 wpa_printf(MSG_DEBUG,
4436 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
4437 OCSP_BASICRESP_free(basic);
4438 OCSP_RESPONSE_free(rsp);
4439 return 0;
4440 }
4441
4442 res = OCSP_resp_find_status(basic, id, &status, &reason,
4443 &produced_at, &this_update,
4444 &next_update);
4445 }
4446
4447 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004448 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
4449 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
4450 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004451 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004452 OCSP_BASICRESP_free(basic);
4453 OCSP_RESPONSE_free(rsp);
4454 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
4455 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004456 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004457
4458 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
4459 tls_show_errors(MSG_INFO, __func__,
4460 "OpenSSL: OCSP status times invalid");
4461 OCSP_BASICRESP_free(basic);
4462 OCSP_RESPONSE_free(rsp);
4463 return 0;
4464 }
4465
4466 OCSP_BASICRESP_free(basic);
4467 OCSP_RESPONSE_free(rsp);
4468
4469 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
4470 OCSP_cert_status_str(status));
4471
4472 if (status == V_OCSP_CERTSTATUS_GOOD)
4473 return 1;
4474 if (status == V_OCSP_CERTSTATUS_REVOKED)
4475 return 0;
4476 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
4477 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
4478 return 0;
4479 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07004480 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004481 return 1;
4482}
4483
4484
4485static int ocsp_status_cb(SSL *s, void *arg)
4486{
4487 char *tmp;
4488 char *resp;
4489 size_t len;
4490
4491 if (tls_global->ocsp_stapling_response == NULL) {
4492 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
4493 return SSL_TLSEXT_ERR_OK;
4494 }
4495
4496 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
4497 if (resp == NULL) {
4498 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
4499 /* TODO: Build OCSPResponse with responseStatus = internalError
4500 */
4501 return SSL_TLSEXT_ERR_OK;
4502 }
4503 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
4504 tmp = OPENSSL_malloc(len);
4505 if (tmp == NULL) {
4506 os_free(resp);
4507 return SSL_TLSEXT_ERR_ALERT_FATAL;
4508 }
4509
4510 os_memcpy(tmp, resp, len);
4511 os_free(resp);
4512 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
4513
4514 return SSL_TLSEXT_ERR_OK;
4515}
4516
4517#endif /* HAVE_OCSP */
4518
4519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004520int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
4521 const struct tls_connection_params *params)
4522{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004523 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524 int ret;
4525 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004526 int can_pkcs11 = 0;
4527 const char *key_id = params->key_id;
4528 const char *cert_id = params->cert_id;
4529 const char *ca_cert_id = params->ca_cert_id;
4530 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004531 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532
4533 if (conn == NULL)
4534 return -1;
4535
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004536 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
4537 wpa_printf(MSG_INFO,
4538 "OpenSSL: ocsp=3 not supported");
4539 return -1;
4540 }
4541
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004542 /*
4543 * If the engine isn't explicitly configured, and any of the
4544 * cert/key fields are actually PKCS#11 URIs, then automatically
4545 * use the PKCS#11 ENGINE.
4546 */
4547 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
4548 can_pkcs11 = 1;
4549
4550 if (!key_id && params->private_key && can_pkcs11 &&
4551 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
4552 can_pkcs11 = 2;
4553 key_id = params->private_key;
4554 }
4555
4556 if (!cert_id && params->client_cert && can_pkcs11 &&
4557 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
4558 can_pkcs11 = 2;
4559 cert_id = params->client_cert;
4560 }
4561
4562 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
4563 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
4564 can_pkcs11 = 2;
4565 ca_cert_id = params->ca_cert;
4566 }
4567
4568 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
4569 if (can_pkcs11 == 2 && !engine_id)
4570 engine_id = "pkcs11";
4571
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004572#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004573#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004574 if (params->flags & TLS_CONN_EAP_FAST) {
4575 wpa_printf(MSG_DEBUG,
4576 "OpenSSL: Use TLSv1_method() for EAP-FAST");
4577 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
4578 tls_show_errors(MSG_INFO, __func__,
4579 "Failed to set TLSv1_method() for EAP-FAST");
4580 return -1;
4581 }
4582 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004583#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07004584#if OPENSSL_VERSION_NUMBER >= 0x10101000L
4585#ifdef SSL_OP_NO_TLSv1_3
4586 if (params->flags & TLS_CONN_EAP_FAST) {
4587 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
4588 * refuses to start the handshake with the modified ciphersuite
4589 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
4590 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
4591 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
4592 }
4593#endif /* SSL_OP_NO_TLSv1_3 */
4594#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004595#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597 while ((err = ERR_get_error())) {
4598 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4599 __func__, ERR_error_string(err, NULL));
4600 }
4601
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004602 if (engine_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004604 ret = tls_engine_init(conn, engine_id, params->pin,
4605 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606 if (ret)
4607 return ret;
4608 }
4609 if (tls_connection_set_subject_match(conn,
4610 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07004611 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004612 params->suffix_match,
4613 params->domain_match))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004614 return -1;
4615
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004616 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004617 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004618 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004619 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 params->ca_cert_blob,
4621 params->ca_cert_blob_len,
4622 params->ca_path))
4623 return -1;
4624
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004625 if (engine_id && cert_id) {
4626 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
4628 } else if (tls_connection_client_cert(conn, params->client_cert,
4629 params->client_cert_blob,
4630 params->client_cert_blob_len))
4631 return -1;
4632
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004633 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
4635 if (tls_connection_engine_private_key(conn))
4636 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004637 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004638 params->private_key,
4639 params->private_key_passwd,
4640 params->private_key_blob,
4641 params->private_key_blob_len)) {
4642 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
4643 params->private_key);
4644 return -1;
4645 }
4646
4647 if (tls_connection_dh(conn, params->dh_file)) {
4648 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
4649 params->dh_file);
4650 return -1;
4651 }
4652
Roshan Pius3a1667e2018-07-03 15:17:14 -07004653 ciphers = params->openssl_ciphers;
4654#ifdef CONFIG_SUITEB
4655#ifdef OPENSSL_IS_BORINGSSL
4656 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
4657 /* BoringSSL removed support for SUITEB192, so need to handle
4658 * this with hardcoded ciphersuite and additional checks for
4659 * other parameters. */
4660 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
4661 }
4662#endif /* OPENSSL_IS_BORINGSSL */
4663#endif /* CONFIG_SUITEB */
4664 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004665 wpa_printf(MSG_INFO,
4666 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07004667 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004668 return -1;
4669 }
4670
Hai Shalom74f70d42019-02-11 14:42:39 -08004671 if (!params->openssl_ecdh_curves) {
4672#ifndef OPENSSL_IS_BORINGSSL
4673#ifndef OPENSSL_NO_EC
4674#if (OPENSSL_VERSION_NUMBER >= 0x10002000L) && \
4675 (OPENSSL_VERSION_NUMBER < 0x10100000L)
4676 if (SSL_set_ecdh_auto(conn->ssl, 1) != 1) {
4677 wpa_printf(MSG_INFO,
4678 "OpenSSL: Failed to set ECDH curves to auto");
4679 return -1;
4680 }
4681#endif /* >= 1.0.2 && < 1.1.0 */
4682#endif /* OPENSSL_NO_EC */
4683#endif /* OPENSSL_IS_BORINGSSL */
4684 } else if (params->openssl_ecdh_curves[0]) {
4685#if defined(OPENSSL_IS_BORINGSSL) || (OPENSSL_VERSION_NUMBER < 0x10002000L)
4686 wpa_printf(MSG_INFO,
4687 "OpenSSL: ECDH configuration nnot supported");
4688 return -1;
4689#else /* OPENSSL_IS_BORINGSSL || < 1.0.2 */
4690#ifndef OPENSSL_NO_EC
4691 if (SSL_set1_curves_list(conn->ssl,
4692 params->openssl_ecdh_curves) != 1) {
4693 wpa_printf(MSG_INFO,
4694 "OpenSSL: Failed to set ECDH curves '%s'",
4695 params->openssl_ecdh_curves);
4696 return -1;
4697 }
4698#else /* OPENSSL_NO_EC */
4699 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
4700 return -1;
4701#endif /* OPENSSL_NO_EC */
4702#endif /* OPENSSL_IS_BORINGSSL */
4703 }
4704
Roshan Pius3a1667e2018-07-03 15:17:14 -07004705 if (tls_set_conn_flags(conn, params->flags,
4706 params->openssl_ciphers) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004707 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004708
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004709#ifdef OPENSSL_IS_BORINGSSL
4710 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4711 SSL_enable_ocsp_stapling(conn->ssl);
4712 }
4713#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004714#ifdef HAVE_OCSP
4715 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004716 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004717 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
4718 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
4719 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
4720 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004721#else /* HAVE_OCSP */
4722 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
4723 wpa_printf(MSG_INFO,
4724 "OpenSSL: No OCSP support included - reject configuration");
4725 return -1;
4726 }
4727 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4728 wpa_printf(MSG_DEBUG,
4729 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4730 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004731#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004732#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004733
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004734 conn->flags = params->flags;
4735
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004736 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737
4738 return 0;
4739}
4740
4741
4742int tls_global_set_params(void *tls_ctx,
4743 const struct tls_connection_params *params)
4744{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004745 struct tls_data *data = tls_ctx;
4746 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747 unsigned long err;
4748
4749 while ((err = ERR_get_error())) {
4750 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4751 __func__, ERR_error_string(err, NULL));
4752 }
4753
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004754 if (tls_global_ca_cert(data, params->ca_cert) ||
4755 tls_global_client_cert(data, params->client_cert) ||
4756 tls_global_private_key(data, params->private_key,
4757 params->private_key_passwd) ||
4758 tls_global_dh(data, params->dh_file)) {
4759 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 return -1;
4761 }
4762
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004763 if (params->openssl_ciphers &&
4764 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
4765 wpa_printf(MSG_INFO,
4766 "OpenSSL: Failed to set cipher string '%s'",
4767 params->openssl_ciphers);
4768 return -1;
4769 }
4770
Hai Shalom74f70d42019-02-11 14:42:39 -08004771 if (!params->openssl_ecdh_curves) {
4772#ifndef OPENSSL_IS_BORINGSSL
4773#ifndef OPENSSL_NO_EC
4774#if (OPENSSL_VERSION_NUMBER >= 0x10002000L) && \
4775 (OPENSSL_VERSION_NUMBER < 0x10100000L)
4776 if (SSL_CTX_set_ecdh_auto(ssl_ctx, 1) != 1) {
4777 wpa_printf(MSG_INFO,
4778 "OpenSSL: Failed to set ECDH curves to auto");
4779 return -1;
4780 }
4781#endif /* >= 1.0.2 && < 1.1.0 */
4782#endif /* OPENSSL_NO_EC */
4783#endif /* OPENSSL_IS_BORINGSSL */
4784 } else if (params->openssl_ecdh_curves[0]) {
4785#if defined(OPENSSL_IS_BORINGSSL) || (OPENSSL_VERSION_NUMBER < 0x10002000L)
4786 wpa_printf(MSG_INFO,
4787 "OpenSSL: ECDH configuration nnot supported");
4788 return -1;
4789#else /* OPENSSL_IS_BORINGSSL || < 1.0.2 */
4790#ifndef OPENSSL_NO_EC
4791 if (SSL_CTX_set1_curves_list(ssl_ctx,
4792 params->openssl_ecdh_curves) !=
4793 1) {
4794 wpa_printf(MSG_INFO,
4795 "OpenSSL: Failed to set ECDH curves '%s'",
4796 params->openssl_ecdh_curves);
4797 return -1;
4798 }
4799#else /* OPENSSL_NO_EC */
4800 wpa_printf(MSG_INFO, "OpenSSL: ECDH not supported");
4801 return -1;
4802#endif /* OPENSSL_NO_EC */
4803#endif /* OPENSSL_IS_BORINGSSL */
4804 }
4805
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004806#ifdef SSL_OP_NO_TICKET
4807 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
4808 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
4809 else
4810 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
4811#endif /* SSL_OP_NO_TICKET */
4812
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004813#ifdef HAVE_OCSP
4814 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
4815 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
4816 os_free(tls_global->ocsp_stapling_response);
4817 if (params->ocsp_stapling_response)
4818 tls_global->ocsp_stapling_response =
4819 os_strdup(params->ocsp_stapling_response);
4820 else
4821 tls_global->ocsp_stapling_response = NULL;
4822#endif /* HAVE_OCSP */
4823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 return 0;
4825}
4826
4827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4829/* Pre-shared secred requires a patch to openssl, so this function is
4830 * commented out unless explicitly needed for EAP-FAST in order to be able to
4831 * build this file with unmodified openssl. */
4832
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004833#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004834static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4835 STACK_OF(SSL_CIPHER) *peer_ciphers,
4836 const SSL_CIPHER **cipher, void *arg)
4837#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4839 STACK_OF(SSL_CIPHER) *peer_ciphers,
4840 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004841#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842{
4843 struct tls_connection *conn = arg;
4844 int ret;
4845
Roshan Pius3a1667e2018-07-03 15:17:14 -07004846#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4847 (defined(LIBRESSL_VERSION_NUMBER) && \
4848 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849 if (conn == NULL || conn->session_ticket_cb == NULL)
4850 return 0;
4851
4852 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4853 conn->session_ticket,
4854 conn->session_ticket_len,
4855 s->s3->client_random,
4856 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004857#else
4858 unsigned char client_random[SSL3_RANDOM_SIZE];
4859 unsigned char server_random[SSL3_RANDOM_SIZE];
4860
4861 if (conn == NULL || conn->session_ticket_cb == NULL)
4862 return 0;
4863
4864 SSL_get_client_random(s, client_random, sizeof(client_random));
4865 SSL_get_server_random(s, server_random, sizeof(server_random));
4866
4867 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4868 conn->session_ticket,
4869 conn->session_ticket_len,
4870 client_random,
4871 server_random, secret);
4872#endif
4873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 os_free(conn->session_ticket);
4875 conn->session_ticket = NULL;
4876
4877 if (ret <= 0)
4878 return 0;
4879
4880 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
4881 return 1;
4882}
4883
4884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
4886 int len, void *arg)
4887{
4888 struct tls_connection *conn = arg;
4889
4890 if (conn == NULL || conn->session_ticket_cb == NULL)
4891 return 0;
4892
4893 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
4894
4895 os_free(conn->session_ticket);
4896 conn->session_ticket = NULL;
4897
4898 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
4899 "extension", data, len);
4900
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004901 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 if (conn->session_ticket == NULL)
4903 return 0;
4904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 conn->session_ticket_len = len;
4906
4907 return 1;
4908}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4910
4911
4912int tls_connection_set_session_ticket_cb(void *tls_ctx,
4913 struct tls_connection *conn,
4914 tls_session_ticket_cb cb,
4915 void *ctx)
4916{
4917#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4918 conn->session_ticket_cb = cb;
4919 conn->session_ticket_cb_ctx = ctx;
4920
4921 if (cb) {
4922 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
4923 conn) != 1)
4924 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925 SSL_set_session_ticket_ext_cb(conn->ssl,
4926 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927 } else {
4928 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
4929 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 }
4932
4933 return 0;
4934#else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4935 return -1;
4936#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4937}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004938
4939
4940int tls_get_library_version(char *buf, size_t buf_len)
4941{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004942#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004943 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4944 OPENSSL_VERSION_TEXT,
4945 OpenSSL_version(OPENSSL_VERSION));
4946#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004947 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4948 OPENSSL_VERSION_TEXT,
4949 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004950#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004951}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004952
4953
4954void tls_connection_set_success_data(struct tls_connection *conn,
4955 struct wpabuf *data)
4956{
4957 SSL_SESSION *sess;
4958 struct wpabuf *old;
4959
4960 if (tls_ex_idx_session < 0)
4961 goto fail;
4962 sess = SSL_get_session(conn->ssl);
4963 if (!sess)
4964 goto fail;
4965 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4966 if (old) {
4967 wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
4968 old);
4969 wpabuf_free(old);
4970 }
4971 if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
4972 goto fail;
4973
4974 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
4975 conn->success_data = 1;
4976 return;
4977
4978fail:
4979 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
4980 wpabuf_free(data);
4981}
4982
4983
4984void tls_connection_set_success_data_resumed(struct tls_connection *conn)
4985{
4986 wpa_printf(MSG_DEBUG,
4987 "OpenSSL: Success data accepted for resumed session");
4988 conn->success_data = 1;
4989}
4990
4991
4992const struct wpabuf *
4993tls_connection_get_success_data(struct tls_connection *conn)
4994{
4995 SSL_SESSION *sess;
4996
4997 if (tls_ex_idx_session < 0 ||
4998 !(sess = SSL_get_session(conn->ssl)))
4999 return NULL;
5000 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
5001}
5002
5003
5004void tls_connection_remove_session(struct tls_connection *conn)
5005{
5006 SSL_SESSION *sess;
5007
5008 sess = SSL_get_session(conn->ssl);
5009 if (!sess)
5010 return;
5011
5012 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
5013 wpa_printf(MSG_DEBUG,
5014 "OpenSSL: Session was not cached");
5015 else
5016 wpa_printf(MSG_DEBUG,
5017 "OpenSSL: Removed cached session to disable session resumption");
5018}