blob: 1aee10cc1af4d8edfcbeadb4e0c0925f175904a5 [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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700107#if OPENSSL_VERSION_NUMBER < 0x10100000L
108#ifdef CONFIG_SUITEB
109static int RSA_bits(const RSA *r)
110{
111 return BN_num_bits(r->n);
112}
113#endif /* CONFIG_SUITEB */
114#endif
115
Dmitry Shmidtff079172013-11-08 14:10:30 -0800116#ifdef ANDROID
117#include <openssl/pem.h>
118#include <keystore/keystore_get.h>
119
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000120#include <log/log.h>
121#include <log/log_event_list.h>
122
123#define CERT_VALIDATION_FAILURE 210033
124
125static void log_cert_validation_failure(const char *reason)
126{
127 android_log_context ctx = create_android_logger(CERT_VALIDATION_FAILURE);
128 android_log_write_string8(ctx, reason);
129 android_log_write_list(ctx, LOG_ID_SECURITY);
130 android_log_destroy(&ctx);
131}
132
133
Dmitry Shmidtff079172013-11-08 14:10:30 -0800134static BIO * BIO_from_keystore(const char *key)
135{
136 BIO *bio = NULL;
137 uint8_t *value = NULL;
138 int length = keystore_get(key, strlen(key), &value);
139 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
140 BIO_write(bio, value, length);
141 free(value);
142 return bio;
143}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800144
145
146static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *key_alias)
147{
148 BIO *bio = BIO_from_keystore(key_alias);
149 STACK_OF(X509_INFO) *stack = NULL;
150 stack_index_t i;
151
152 if (bio) {
153 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
154 BIO_free(bio);
155 }
156
157 if (!stack) {
158 wpa_printf(MSG_WARNING, "TLS: Failed to parse certificate: %s",
159 key_alias);
160 return -1;
161 }
162
163 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
164 X509_INFO *info = sk_X509_INFO_value(stack, i);
165
166 if (info->x509)
167 X509_STORE_add_cert(ctx, info->x509);
168 if (info->crl)
169 X509_STORE_add_crl(ctx, info->crl);
170 }
171
172 sk_X509_INFO_pop_free(stack, X509_INFO_free);
173
174 return 0;
175}
176
177
178static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
179 const char *encoded_key_alias)
180{
181 int rc = -1;
182 int len = os_strlen(encoded_key_alias);
183 unsigned char *decoded_alias;
184
185 if (len & 1) {
186 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
187 encoded_key_alias);
188 return rc;
189 }
190
191 decoded_alias = os_malloc(len / 2 + 1);
192 if (decoded_alias) {
193 if (!hexstr2bin(encoded_key_alias, decoded_alias, len / 2)) {
194 decoded_alias[len / 2] = '\0';
195 rc = tls_add_ca_from_keystore(
196 ctx, (const char *) decoded_alias);
197 }
198 os_free(decoded_alias);
199 }
200
201 return rc;
202}
203
Dmitry Shmidtff079172013-11-08 14:10:30 -0800204#endif /* ANDROID */
205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206static int tls_openssl_ref_count = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800207static int tls_ex_idx_session = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700209struct tls_context {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 void (*event_cb)(void *ctx, enum tls_event ev,
211 union tls_event_data *data);
212 void *cb_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213 int cert_in_cb;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700214 char *ocsp_stapling_response;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215};
216
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700217static struct tls_context *tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218
219
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800220struct tls_data {
221 SSL_CTX *ssl;
222 unsigned int tls_session_lifetime;
223};
224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700226 struct tls_context *context;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800227 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 SSL *ssl;
229 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700230#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 ENGINE *engine; /* functional reference to the engine */
232 EVP_PKEY *private_key; /* the private key if using engine */
233#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800234 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235 int read_alerts, write_alerts, failed;
236
237 tls_session_ticket_cb session_ticket_cb;
238 void *session_ticket_cb_ctx;
239
240 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
241 u8 *session_ticket;
242 size_t session_ticket_len;
243
244 unsigned int ca_cert_verify:1;
245 unsigned int cert_probe:1;
246 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300247 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800248 unsigned int success_data:1;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700249 unsigned int client_hello_generated:1;
250 unsigned int server:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251
252 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700253
254 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700255
256 X509 *peer_cert;
257 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800258 X509 *peer_issuer_issuer;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800259
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800260 unsigned char client_random[SSL3_RANDOM_SIZE];
261 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700262
263 u16 cipher_suite;
264 int server_dh_prime_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265};
266
267
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700268static struct tls_context * tls_context_new(const struct tls_config *conf)
269{
270 struct tls_context *context = os_zalloc(sizeof(*context));
271 if (context == NULL)
272 return NULL;
273 if (conf) {
274 context->event_cb = conf->event_cb;
275 context->cb_ctx = conf->cb_ctx;
276 context->cert_in_cb = conf->cert_in_cb;
277 }
278 return context;
279}
280
281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282#ifdef CONFIG_NO_STDOUT_DEBUG
283
284static void _tls_show_errors(void)
285{
286 unsigned long err;
287
288 while ((err = ERR_get_error())) {
289 /* Just ignore the errors, since stdout is disabled */
290 }
291}
292#define tls_show_errors(l, f, t) _tls_show_errors()
293
294#else /* CONFIG_NO_STDOUT_DEBUG */
295
296static void tls_show_errors(int level, const char *func, const char *txt)
297{
298 unsigned long err;
299
300 wpa_printf(level, "OpenSSL: %s - %s %s",
301 func, txt, ERR_error_string(ERR_get_error(), NULL));
302
303 while ((err = ERR_get_error())) {
304 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
305 ERR_error_string(err, NULL));
306 }
307}
308
309#endif /* CONFIG_NO_STDOUT_DEBUG */
310
311
312#ifdef CONFIG_NATIVE_WINDOWS
313
314/* Windows CryptoAPI and access to certificate stores */
315#include <wincrypt.h>
316
317#ifdef __MINGW32_VERSION
318/*
319 * MinGW does not yet include all the needed definitions for CryptoAPI, so
320 * define here whatever extra is needed.
321 */
322#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
323#define CERT_STORE_READONLY_FLAG 0x00008000
324#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
325
326#endif /* __MINGW32_VERSION */
327
328
329struct cryptoapi_rsa_data {
330 const CERT_CONTEXT *cert;
331 HCRYPTPROV crypt_prov;
332 DWORD key_spec;
333 BOOL free_crypt_prov;
334};
335
336
337static void cryptoapi_error(const char *msg)
338{
339 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
340 msg, (unsigned int) GetLastError());
341}
342
343
344static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
345 unsigned char *to, RSA *rsa, int padding)
346{
347 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
348 return 0;
349}
350
351
352static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
353 unsigned char *to, RSA *rsa, int padding)
354{
355 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
356 return 0;
357}
358
359
360static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
361 unsigned char *to, RSA *rsa, int padding)
362{
363 struct cryptoapi_rsa_data *priv =
364 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
365 HCRYPTHASH hash;
366 DWORD hash_size, len, i;
367 unsigned char *buf = NULL;
368 int ret = 0;
369
370 if (priv == NULL) {
371 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
372 ERR_R_PASSED_NULL_PARAMETER);
373 return 0;
374 }
375
376 if (padding != RSA_PKCS1_PADDING) {
377 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
378 RSA_R_UNKNOWN_PADDING_TYPE);
379 return 0;
380 }
381
382 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
383 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
384 __func__);
385 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
386 RSA_R_INVALID_MESSAGE_LENGTH);
387 return 0;
388 }
389
390 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
391 {
392 cryptoapi_error("CryptCreateHash failed");
393 return 0;
394 }
395
396 len = sizeof(hash_size);
397 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
398 0)) {
399 cryptoapi_error("CryptGetHashParam failed");
400 goto err;
401 }
402
403 if ((int) hash_size != flen) {
404 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
405 (unsigned) hash_size, flen);
406 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
407 RSA_R_INVALID_MESSAGE_LENGTH);
408 goto err;
409 }
410 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
411 cryptoapi_error("CryptSetHashParam failed");
412 goto err;
413 }
414
415 len = RSA_size(rsa);
416 buf = os_malloc(len);
417 if (buf == NULL) {
418 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
419 goto err;
420 }
421
422 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
423 cryptoapi_error("CryptSignHash failed");
424 goto err;
425 }
426
427 for (i = 0; i < len; i++)
428 to[i] = buf[len - i - 1];
429 ret = len;
430
431err:
432 os_free(buf);
433 CryptDestroyHash(hash);
434
435 return ret;
436}
437
438
439static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
440 unsigned char *to, RSA *rsa, int padding)
441{
442 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
443 return 0;
444}
445
446
447static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
448{
449 if (priv == NULL)
450 return;
451 if (priv->crypt_prov && priv->free_crypt_prov)
452 CryptReleaseContext(priv->crypt_prov, 0);
453 if (priv->cert)
454 CertFreeCertificateContext(priv->cert);
455 os_free(priv);
456}
457
458
459static int cryptoapi_finish(RSA *rsa)
460{
461 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
462 os_free((void *) rsa->meth);
463 rsa->meth = NULL;
464 return 1;
465}
466
467
468static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
469{
470 HCERTSTORE cs;
471 const CERT_CONTEXT *ret = NULL;
472
473 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
474 store | CERT_STORE_OPEN_EXISTING_FLAG |
475 CERT_STORE_READONLY_FLAG, L"MY");
476 if (cs == NULL) {
477 cryptoapi_error("Failed to open 'My system store'");
478 return NULL;
479 }
480
481 if (strncmp(name, "cert://", 7) == 0) {
482 unsigned short wbuf[255];
483 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
484 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
485 PKCS_7_ASN_ENCODING,
486 0, CERT_FIND_SUBJECT_STR,
487 wbuf, NULL);
488 } else if (strncmp(name, "hash://", 7) == 0) {
489 CRYPT_HASH_BLOB blob;
490 int len;
491 const char *hash = name + 7;
492 unsigned char *buf;
493
494 len = os_strlen(hash) / 2;
495 buf = os_malloc(len);
496 if (buf && hexstr2bin(hash, buf, len) == 0) {
497 blob.cbData = len;
498 blob.pbData = buf;
499 ret = CertFindCertificateInStore(cs,
500 X509_ASN_ENCODING |
501 PKCS_7_ASN_ENCODING,
502 0, CERT_FIND_HASH,
503 &blob, NULL);
504 }
505 os_free(buf);
506 }
507
508 CertCloseStore(cs, 0);
509
510 return ret;
511}
512
513
514static int tls_cryptoapi_cert(SSL *ssl, const char *name)
515{
516 X509 *cert = NULL;
517 RSA *rsa = NULL, *pub_rsa;
518 struct cryptoapi_rsa_data *priv;
519 RSA_METHOD *rsa_meth;
520
521 if (name == NULL ||
522 (strncmp(name, "cert://", 7) != 0 &&
523 strncmp(name, "hash://", 7) != 0))
524 return -1;
525
526 priv = os_zalloc(sizeof(*priv));
527 rsa_meth = os_zalloc(sizeof(*rsa_meth));
528 if (priv == NULL || rsa_meth == NULL) {
529 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
530 "for CryptoAPI RSA method");
531 os_free(priv);
532 os_free(rsa_meth);
533 return -1;
534 }
535
536 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
537 if (priv->cert == NULL) {
538 priv->cert = cryptoapi_find_cert(
539 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
540 }
541 if (priv->cert == NULL) {
542 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
543 "'%s'", name);
544 goto err;
545 }
546
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800547 cert = d2i_X509(NULL,
548 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 priv->cert->cbCertEncoded);
550 if (cert == NULL) {
551 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
552 "encoding");
553 goto err;
554 }
555
556 if (!CryptAcquireCertificatePrivateKey(priv->cert,
557 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
558 NULL, &priv->crypt_prov,
559 &priv->key_spec,
560 &priv->free_crypt_prov)) {
561 cryptoapi_error("Failed to acquire a private key for the "
562 "certificate");
563 goto err;
564 }
565
566 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
567 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
568 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
569 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
570 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
571 rsa_meth->finish = cryptoapi_finish;
572 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
573 rsa_meth->app_data = (char *) priv;
574
575 rsa = RSA_new();
576 if (rsa == NULL) {
577 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
578 ERR_R_MALLOC_FAILURE);
579 goto err;
580 }
581
582 if (!SSL_use_certificate(ssl, cert)) {
583 RSA_free(rsa);
584 rsa = NULL;
585 goto err;
586 }
587 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
588 X509_free(cert);
589 cert = NULL;
590
591 rsa->n = BN_dup(pub_rsa->n);
592 rsa->e = BN_dup(pub_rsa->e);
593 if (!RSA_set_method(rsa, rsa_meth))
594 goto err;
595
596 if (!SSL_use_RSAPrivateKey(ssl, rsa))
597 goto err;
598 RSA_free(rsa);
599
600 return 0;
601
602err:
603 if (cert)
604 X509_free(cert);
605 if (rsa)
606 RSA_free(rsa);
607 else {
608 os_free(rsa_meth);
609 cryptoapi_free_data(priv);
610 }
611 return -1;
612}
613
614
615static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
616{
617 HCERTSTORE cs;
618 PCCERT_CONTEXT ctx = NULL;
619 X509 *cert;
620 char buf[128];
621 const char *store;
622#ifdef UNICODE
623 WCHAR *wstore;
624#endif /* UNICODE */
625
626 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
627 return -1;
628
629 store = name + 13;
630#ifdef UNICODE
631 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
632 if (wstore == NULL)
633 return -1;
634 wsprintf(wstore, L"%S", store);
635 cs = CertOpenSystemStore(0, wstore);
636 os_free(wstore);
637#else /* UNICODE */
638 cs = CertOpenSystemStore(0, store);
639#endif /* UNICODE */
640 if (cs == NULL) {
641 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
642 "'%s': error=%d", __func__, store,
643 (int) GetLastError());
644 return -1;
645 }
646
647 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800648 cert = d2i_X509(NULL,
649 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 ctx->cbCertEncoded);
651 if (cert == NULL) {
652 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
653 "X509 DER encoding for CA cert");
654 continue;
655 }
656
657 X509_NAME_oneline(X509_get_subject_name(cert), buf,
658 sizeof(buf));
659 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
660 "system certificate store: subject='%s'", buf);
661
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700662 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
663 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 tls_show_errors(MSG_WARNING, __func__,
665 "Failed to add ca_cert to OpenSSL "
666 "certificate store");
667 }
668
669 X509_free(cert);
670 }
671
672 if (!CertCloseStore(cs, 0)) {
673 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
674 "'%s': error=%d", __func__, name + 13,
675 (int) GetLastError());
676 }
677
678 return 0;
679}
680
681
682#else /* CONFIG_NATIVE_WINDOWS */
683
684static int tls_cryptoapi_cert(SSL *ssl, const char *name)
685{
686 return -1;
687}
688
689#endif /* CONFIG_NATIVE_WINDOWS */
690
691
692static void ssl_info_cb(const SSL *ssl, int where, int ret)
693{
694 const char *str;
695 int w;
696
697 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
698 w = where & ~SSL_ST_MASK;
699 if (w & SSL_ST_CONNECT)
700 str = "SSL_connect";
701 else if (w & SSL_ST_ACCEPT)
702 str = "SSL_accept";
703 else
704 str = "undefined";
705
706 if (where & SSL_CB_LOOP) {
707 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
708 str, SSL_state_string_long(ssl));
709 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700710 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
712 where & SSL_CB_READ ?
713 "read (remote end reported an error)" :
714 "write (local SSL3 detected an error)",
715 SSL_alert_type_string_long(ret),
716 SSL_alert_desc_string_long(ret));
717 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 if (where & SSL_CB_READ)
719 conn->read_alerts++;
720 else
721 conn->write_alerts++;
722 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700723 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700724 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700725 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700726 os_memset(&ev, 0, sizeof(ev));
727 ev.alert.is_local = !(where & SSL_CB_READ);
728 ev.alert.type = SSL_alert_type_string_long(ret);
729 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700730 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 } else if (where & SSL_CB_EXIT && ret <= 0) {
733 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
734 str, ret == 0 ? "failed" : "error",
735 SSL_state_string_long(ssl));
736 }
737}
738
739
740#ifndef OPENSSL_NO_ENGINE
741/**
742 * tls_engine_load_dynamic_generic - load any openssl engine
743 * @pre: an array of commands and values that load an engine initialized
744 * in the engine specific function
745 * @post: an array of commands and values that initialize an already loaded
746 * engine (or %NULL if not required)
747 * @id: the engine id of the engine to load (only required if post is not %NULL
748 *
749 * This function is a generic function that loads any openssl engine.
750 *
751 * Returns: 0 on success, -1 on failure
752 */
753static int tls_engine_load_dynamic_generic(const char *pre[],
754 const char *post[], const char *id)
755{
756 ENGINE *engine;
757 const char *dynamic_id = "dynamic";
758
759 engine = ENGINE_by_id(id);
760 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
762 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700763 /*
764 * If it was auto-loaded by ENGINE_by_id() we might still
765 * need to tell it which PKCS#11 module to use in legacy
766 * (non-p11-kit) environments. Do so now; even if it was
767 * properly initialised before, setting it again will be
768 * harmless.
769 */
770 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 }
772 ERR_clear_error();
773
774 engine = ENGINE_by_id(dynamic_id);
775 if (engine == NULL) {
776 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
777 dynamic_id,
778 ERR_error_string(ERR_get_error(), NULL));
779 return -1;
780 }
781
782 /* Perform the pre commands. This will load the engine. */
783 while (pre && pre[0]) {
784 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
785 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
786 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
787 "%s %s [%s]", pre[0], pre[1],
788 ERR_error_string(ERR_get_error(), NULL));
789 ENGINE_free(engine);
790 return -1;
791 }
792 pre += 2;
793 }
794
795 /*
796 * Free the reference to the "dynamic" engine. The loaded engine can
797 * now be looked up using ENGINE_by_id().
798 */
799 ENGINE_free(engine);
800
801 engine = ENGINE_by_id(id);
802 if (engine == NULL) {
803 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
804 id, ERR_error_string(ERR_get_error(), NULL));
805 return -1;
806 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700807 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 while (post && post[0]) {
809 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
810 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
811 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
812 " %s %s [%s]", post[0], post[1],
813 ERR_error_string(ERR_get_error(), NULL));
814 ENGINE_remove(engine);
815 ENGINE_free(engine);
816 return -1;
817 }
818 post += 2;
819 }
820 ENGINE_free(engine);
821
822 return 0;
823}
824
825
826/**
827 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
828 * @pkcs11_so_path: pksc11_so_path from the configuration
829 * @pcks11_module_path: pkcs11_module_path from the configuration
830 */
831static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
832 const char *pkcs11_module_path)
833{
834 char *engine_id = "pkcs11";
835 const char *pre_cmd[] = {
836 "SO_PATH", NULL /* pkcs11_so_path */,
837 "ID", NULL /* engine_id */,
838 "LIST_ADD", "1",
839 /* "NO_VCHECK", "1", */
840 "LOAD", NULL,
841 NULL, NULL
842 };
843 const char *post_cmd[] = {
844 "MODULE_PATH", NULL /* pkcs11_module_path */,
845 NULL, NULL
846 };
847
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800848 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 return 0;
850
851 pre_cmd[1] = pkcs11_so_path;
852 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800853 if (pkcs11_module_path)
854 post_cmd[1] = pkcs11_module_path;
855 else
856 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857
858 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
859 pkcs11_so_path);
860
861 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
862}
863
864
865/**
866 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
867 * @opensc_so_path: opensc_so_path from the configuration
868 */
869static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
870{
871 char *engine_id = "opensc";
872 const char *pre_cmd[] = {
873 "SO_PATH", NULL /* opensc_so_path */,
874 "ID", NULL /* engine_id */,
875 "LIST_ADD", "1",
876 "LOAD", NULL,
877 NULL, NULL
878 };
879
880 if (!opensc_so_path)
881 return 0;
882
883 pre_cmd[1] = opensc_so_path;
884 pre_cmd[3] = engine_id;
885
886 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
887 opensc_so_path);
888
889 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
890}
891#endif /* OPENSSL_NO_ENGINE */
892
893
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800894static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
895{
896 struct wpabuf *buf;
897
898 if (tls_ex_idx_session < 0)
899 return;
900 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
901 if (!buf)
902 return;
903 wpa_printf(MSG_DEBUG,
904 "OpenSSL: Free application session data %p (sess %p)",
905 buf, sess);
906 wpabuf_free(buf);
907
908 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
909}
910
911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912void * tls_init(const struct tls_config *conf)
913{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800914 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700916 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800917 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918
919 if (tls_openssl_ref_count == 0) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700920 tls_global = context = tls_context_new(conf);
921 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923#ifdef CONFIG_FIPS
924#ifdef OPENSSL_FIPS
925 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800926 static int fips_enabled = 0;
927
928 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
930 "mode");
931 ERR_load_crypto_strings();
932 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700933 os_free(tls_global);
934 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800936 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800938 fips_enabled = 1;
939 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 }
941#else /* OPENSSL_FIPS */
942 if (conf && conf->fips_mode) {
943 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
944 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700945 os_free(tls_global);
946 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 return NULL;
948 }
949#endif /* OPENSSL_FIPS */
950#endif /* CONFIG_FIPS */
Roshan Pius3a1667e2018-07-03 15:17:14 -0700951#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
952 (defined(LIBRESSL_VERSION_NUMBER) && \
953 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 SSL_load_error_strings();
955 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800956#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 EVP_add_digest(EVP_sha256());
958#endif /* OPENSSL_NO_SHA256 */
959 /* TODO: if /dev/urandom is available, PRNG is seeded
960 * automatically. If this is not the case, random data should
961 * be added here. */
962
963#ifdef PKCS12_FUNCS
964#ifndef OPENSSL_NO_RC2
965 /*
966 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
967 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
968 * versions, but it looks like OpenSSL 1.0.0 does not do that
969 * anymore.
970 */
971 EVP_add_cipher(EVP_rc2_40_cbc());
972#endif /* OPENSSL_NO_RC2 */
973 PKCS12_PBE_add();
974#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800975#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700976 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700977 context = tls_context_new(conf);
978 if (context == NULL)
979 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 }
981 tls_openssl_ref_count++;
982
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800983 data = os_zalloc(sizeof(*data));
984 if (data)
985 ssl = SSL_CTX_new(SSLv23_method());
986 else
987 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700988 if (ssl == NULL) {
989 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700990 if (context != tls_global)
991 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700992 if (tls_openssl_ref_count == 0) {
993 os_free(tls_global);
994 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700995 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800996 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700998 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800999 data->ssl = ssl;
1000 if (conf)
1001 data->tls_session_lifetime = conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001003 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
1004 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
1005
Dmitry Shmidt29333592017-01-09 12:27:11 -08001006#ifdef SSL_MODE_NO_AUTO_CHAIN
1007 /* Number of deployed use cases assume the default OpenSSL behavior of
1008 * auto chaining the local certificate is in use. BoringSSL removed this
1009 * functionality by default, so we need to restore it here to avoid
1010 * breaking existing use cases. */
1011 SSL_CTX_clear_mode(ssl, SSL_MODE_NO_AUTO_CHAIN);
1012#endif /* SSL_MODE_NO_AUTO_CHAIN */
1013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001015 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001016 if (data->tls_session_lifetime > 0) {
1017 SSL_CTX_set_quiet_shutdown(ssl, 1);
1018 /*
1019 * Set default context here. In practice, this will be replaced
1020 * by the per-EAP method context in tls_connection_set_verify().
1021 */
1022 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
1023 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
1024 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
1025 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
1026 } else {
1027 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
1028 }
1029
1030 if (tls_ex_idx_session < 0) {
1031 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
1032 0, NULL, NULL, NULL, NULL);
1033 if (tls_ex_idx_session < 0) {
1034 tls_deinit(data);
1035 return NULL;
1036 }
1037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038
1039#ifndef OPENSSL_NO_ENGINE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
Hai Shalomce48b4a2018-09-05 11:41:35 -07001041#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001042 ERR_load_ENGINE_strings();
1043 ENGINE_load_dynamic();
Hai Shalomce48b4a2018-09-05 11:41:35 -07001044#endif /* OPENSSL_VERSION_NUMBER */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 if (conf &&
1047 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1048 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1050 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1051 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001052 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 return NULL;
1054 }
1055 }
1056#endif /* OPENSSL_NO_ENGINE */
1057
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001058 if (conf && conf->openssl_ciphers)
1059 ciphers = conf->openssl_ciphers;
1060 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001061 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001062 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1063 wpa_printf(MSG_ERROR,
1064 "OpenSSL: Failed to set cipher string '%s'",
1065 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001066 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001067 return NULL;
1068 }
1069
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001070 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071}
1072
1073
1074void tls_deinit(void *ssl_ctx)
1075{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001076 struct tls_data *data = ssl_ctx;
1077 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001078 struct tls_context *context = SSL_CTX_get_app_data(ssl);
1079 if (context != tls_global)
1080 os_free(context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001081 if (data->tls_session_lifetime > 0)
1082 SSL_CTX_flush_sessions(ssl, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 SSL_CTX_free(ssl);
1084
1085 tls_openssl_ref_count--;
1086 if (tls_openssl_ref_count == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001087#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1088 (defined(LIBRESSL_VERSION_NUMBER) && \
1089 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090#ifndef OPENSSL_NO_ENGINE
1091 ENGINE_cleanup();
1092#endif /* OPENSSL_NO_ENGINE */
1093 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001094 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 ERR_free_strings();
1096 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001097#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001098 os_free(tls_global->ocsp_stapling_response);
1099 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 os_free(tls_global);
1101 tls_global = NULL;
1102 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001103
1104 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105}
1106
1107
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001108#ifndef OPENSSL_NO_ENGINE
1109
1110/* Cryptoki return values */
1111#define CKR_PIN_INCORRECT 0x000000a0
1112#define CKR_PIN_INVALID 0x000000a1
1113#define CKR_PIN_LEN_RANGE 0x000000a2
1114
1115/* libp11 */
1116#define ERR_LIB_PKCS11 ERR_LIB_USER
1117
1118static int tls_is_pin_error(unsigned int err)
1119{
1120 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1121 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1122 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1123 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1124}
1125
1126#endif /* OPENSSL_NO_ENGINE */
1127
1128
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001129#ifdef ANDROID
1130/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1131EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1132#endif /* ANDROID */
1133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1135 const char *pin, const char *key_id,
1136 const char *cert_id, const char *ca_cert_id)
1137{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001138#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1139#if !defined(OPENSSL_NO_ENGINE)
1140#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1141#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001142 if (!key_id)
1143 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001144 conn->engine = NULL;
1145 conn->private_key = EVP_PKEY_from_keystore(key_id);
1146 if (!conn->private_key) {
1147 wpa_printf(MSG_ERROR,
1148 "ENGINE: cannot load private key with id '%s' [%s]",
1149 key_id,
1150 ERR_error_string(ERR_get_error(), NULL));
1151 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1152 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001153#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155#ifndef OPENSSL_NO_ENGINE
1156 int ret = -1;
1157 if (engine_id == NULL) {
1158 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1159 return -1;
1160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161
1162 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001163#ifdef ANDROID
1164 ENGINE_load_dynamic();
1165#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 conn->engine = ENGINE_by_id(engine_id);
1167 if (!conn->engine) {
1168 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1169 engine_id, ERR_error_string(ERR_get_error(), NULL));
1170 goto err;
1171 }
1172 if (ENGINE_init(conn->engine) != 1) {
1173 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1174 "(engine: %s) [%s]", engine_id,
1175 ERR_error_string(ERR_get_error(), NULL));
1176 goto err;
1177 }
1178 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1179
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001180#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001181 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1183 ERR_error_string(ERR_get_error(), NULL));
1184 goto err;
1185 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001186#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001187 if (key_id) {
1188 /*
1189 * Ensure that the ENGINE does not attempt to use the OpenSSL
1190 * UI system to obtain a PIN, if we didn't provide one.
1191 */
1192 struct {
1193 const void *password;
1194 const char *prompt_info;
1195 } key_cb = { "", NULL };
1196
1197 /* load private key first in-case PIN is required for cert */
1198 conn->private_key = ENGINE_load_private_key(conn->engine,
1199 key_id, NULL,
1200 &key_cb);
1201 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001202 unsigned long err = ERR_get_error();
1203
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001204 wpa_printf(MSG_ERROR,
1205 "ENGINE: cannot load private key with id '%s' [%s]",
1206 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001207 ERR_error_string(err, NULL));
1208 if (tls_is_pin_error(err))
1209 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1210 else
1211 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001212 goto err;
1213 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 }
1215
1216 /* handle a certificate and/or CA certificate */
1217 if (cert_id || ca_cert_id) {
1218 const char *cmd_name = "LOAD_CERT_CTRL";
1219
1220 /* test if the engine supports a LOAD_CERT_CTRL */
1221 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1222 0, (void *)cmd_name, NULL)) {
1223 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1224 " loading certificates");
1225 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1226 goto err;
1227 }
1228 }
1229
1230 return 0;
1231
1232err:
1233 if (conn->engine) {
1234 ENGINE_free(conn->engine);
1235 conn->engine = NULL;
1236 }
1237
1238 if (conn->private_key) {
1239 EVP_PKEY_free(conn->private_key);
1240 conn->private_key = NULL;
1241 }
1242
1243 return ret;
1244#else /* OPENSSL_NO_ENGINE */
1245 return 0;
1246#endif /* OPENSSL_NO_ENGINE */
1247}
1248
1249
1250static void tls_engine_deinit(struct tls_connection *conn)
1251{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001252#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1254 if (conn->private_key) {
1255 EVP_PKEY_free(conn->private_key);
1256 conn->private_key = NULL;
1257 }
1258 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001259#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001261#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 conn->engine = NULL;
1263 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001264#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265}
1266
1267
1268int tls_get_errors(void *ssl_ctx)
1269{
1270 int count = 0;
1271 unsigned long err;
1272
1273 while ((err = ERR_get_error())) {
1274 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1275 ERR_error_string(err, NULL));
1276 count++;
1277 }
1278
1279 return count;
1280}
1281
Jouni Malinen26af48b2014-04-09 13:02:53 +03001282
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001283static const char * openssl_content_type(int content_type)
1284{
1285 switch (content_type) {
1286 case 20:
1287 return "change cipher spec";
1288 case 21:
1289 return "alert";
1290 case 22:
1291 return "handshake";
1292 case 23:
1293 return "application data";
1294 case 24:
1295 return "heartbeat";
1296 case 256:
1297 return "TLS header info"; /* pseudo content type */
1298 default:
1299 return "?";
1300 }
1301}
1302
1303
1304static const char * openssl_handshake_type(int content_type, const u8 *buf,
1305 size_t len)
1306{
1307 if (content_type != 22 || !buf || len == 0)
1308 return "";
1309 switch (buf[0]) {
1310 case 0:
1311 return "hello request";
1312 case 1:
1313 return "client hello";
1314 case 2:
1315 return "server hello";
1316 case 4:
1317 return "new session ticket";
1318 case 11:
1319 return "certificate";
1320 case 12:
1321 return "server key exchange";
1322 case 13:
1323 return "certificate request";
1324 case 14:
1325 return "server hello done";
1326 case 15:
1327 return "certificate verify";
1328 case 16:
1329 return "client key exchange";
1330 case 20:
1331 return "finished";
1332 case 21:
1333 return "certificate url";
1334 case 22:
1335 return "certificate status";
1336 default:
1337 return "?";
1338 }
1339}
1340
1341
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001342#ifdef CONFIG_SUITEB
1343
1344static void check_server_hello(struct tls_connection *conn,
1345 const u8 *pos, const u8 *end)
1346{
1347 size_t payload_len, id_len;
1348
1349 /*
1350 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1351 * not make it cleanly available during handshake and we need to know
1352 * whether DHE was selected.
1353 */
1354
1355 if (end - pos < 3)
1356 return;
1357 payload_len = WPA_GET_BE24(pos);
1358 pos += 3;
1359
1360 if ((size_t) (end - pos) < payload_len)
1361 return;
1362 end = pos + payload_len;
1363
1364 /* Skip Version and Random */
1365 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1366 return;
1367 pos += 2 + SSL3_RANDOM_SIZE;
1368
1369 /* Skip Session ID */
1370 if (end - pos < 1)
1371 return;
1372 id_len = *pos++;
1373 if ((size_t) (end - pos) < id_len)
1374 return;
1375 pos += id_len;
1376
1377 if (end - pos < 2)
1378 return;
1379 conn->cipher_suite = WPA_GET_BE16(pos);
1380 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1381 conn->cipher_suite);
1382}
1383
1384
1385static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1386 const u8 *pos, const u8 *end)
1387{
1388 size_t payload_len;
1389 u16 dh_len;
1390 BIGNUM *p;
1391 int bits;
1392
1393 if (!(conn->flags & TLS_CONN_SUITEB))
1394 return;
1395
1396 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1397 if (conn->cipher_suite != 0x9f)
1398 return;
1399
1400 if (end - pos < 3)
1401 return;
1402 payload_len = WPA_GET_BE24(pos);
1403 pos += 3;
1404
1405 if ((size_t) (end - pos) < payload_len)
1406 return;
1407 end = pos + payload_len;
1408
1409 if (end - pos < 2)
1410 return;
1411 dh_len = WPA_GET_BE16(pos);
1412 pos += 2;
1413
1414 if ((size_t) (end - pos) < dh_len)
1415 return;
1416 p = BN_bin2bn(pos, dh_len, NULL);
1417 if (!p)
1418 return;
1419
1420 bits = BN_num_bits(p);
1421 BN_free(p);
1422
1423 conn->server_dh_prime_len = bits;
1424 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1425 conn->server_dh_prime_len);
1426}
1427
1428#endif /* CONFIG_SUITEB */
1429
1430
Jouni Malinen26af48b2014-04-09 13:02:53 +03001431static void tls_msg_cb(int write_p, int version, int content_type,
1432 const void *buf, size_t len, SSL *ssl, void *arg)
1433{
1434 struct tls_connection *conn = arg;
1435 const u8 *pos = buf;
1436
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001437 if (write_p == 2) {
1438 wpa_printf(MSG_DEBUG,
1439 "OpenSSL: session ver=0x%x content_type=%d",
1440 version, content_type);
1441 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1442 return;
1443 }
1444
1445 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1446 write_p ? "TX" : "RX", version, content_type,
1447 openssl_content_type(content_type),
1448 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001449 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1450 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1451 size_t payload_len = WPA_GET_BE16(pos + 1);
1452 if (payload_len + 3 > len) {
1453 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1454 conn->invalid_hb_used = 1;
1455 }
1456 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001457
1458#ifdef CONFIG_SUITEB
1459 /*
1460 * Need to parse these handshake messages to be able to check DH prime
1461 * length since OpenSSL does not expose the new cipher suite and DH
1462 * parameters during handshake (e.g., for cert_cb() callback).
1463 */
1464 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1465 check_server_hello(conn, pos + 1, pos + len);
1466 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1467 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1468#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001469}
1470
1471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472struct tls_connection * tls_connection_init(void *ssl_ctx)
1473{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001474 struct tls_data *data = ssl_ctx;
1475 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 struct tls_connection *conn;
1477 long options;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001478 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479
1480 conn = os_zalloc(sizeof(*conn));
1481 if (conn == NULL)
1482 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001483 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 conn->ssl = SSL_new(ssl);
1485 if (conn->ssl == NULL) {
1486 tls_show_errors(MSG_INFO, __func__,
1487 "Failed to initialize new SSL connection");
1488 os_free(conn);
1489 return NULL;
1490 }
1491
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001492 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001494 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1495 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1497 SSL_OP_SINGLE_DH_USE;
1498#ifdef SSL_OP_NO_COMPRESSION
1499 options |= SSL_OP_NO_COMPRESSION;
1500#endif /* SSL_OP_NO_COMPRESSION */
1501 SSL_set_options(conn->ssl, options);
1502
1503 conn->ssl_in = BIO_new(BIO_s_mem());
1504 if (!conn->ssl_in) {
1505 tls_show_errors(MSG_INFO, __func__,
1506 "Failed to create a new BIO for ssl_in");
1507 SSL_free(conn->ssl);
1508 os_free(conn);
1509 return NULL;
1510 }
1511
1512 conn->ssl_out = BIO_new(BIO_s_mem());
1513 if (!conn->ssl_out) {
1514 tls_show_errors(MSG_INFO, __func__,
1515 "Failed to create a new BIO for ssl_out");
1516 SSL_free(conn->ssl);
1517 BIO_free(conn->ssl_in);
1518 os_free(conn);
1519 return NULL;
1520 }
1521
1522 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1523
1524 return conn;
1525}
1526
1527
1528void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1529{
1530 if (conn == NULL)
1531 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001532 if (conn->success_data) {
1533 /*
1534 * Make sure ssl_clear_bad_session() does not remove this
1535 * session.
1536 */
1537 SSL_set_quiet_shutdown(conn->ssl, 1);
1538 SSL_shutdown(conn->ssl);
1539 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540 SSL_free(conn->ssl);
1541 tls_engine_deinit(conn);
1542 os_free(conn->subject_match);
1543 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001544 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001545 os_free(conn->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 os_free(conn->session_ticket);
1547 os_free(conn);
1548}
1549
1550
1551int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1552{
1553 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1554}
1555
1556
1557int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1558{
1559 if (conn == NULL)
1560 return -1;
1561
1562 /* Shutdown previous TLS connection without notifying the peer
1563 * because the connection was already terminated in practice
1564 * and "close notify" shutdown alert would confuse AS. */
1565 SSL_set_quiet_shutdown(conn->ssl, 1);
1566 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001567 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568}
1569
1570
1571static int tls_match_altsubject_component(X509 *cert, int type,
1572 const char *value, size_t len)
1573{
1574 GENERAL_NAME *gen;
1575 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001576 int found = 0;
1577 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578
1579 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1580
1581 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1582 gen = sk_GENERAL_NAME_value(ext, i);
1583 if (gen->type != type)
1584 continue;
1585 if (os_strlen((char *) gen->d.ia5->data) == len &&
1586 os_memcmp(value, gen->d.ia5->data, len) == 0)
1587 found++;
1588 }
1589
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001590 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 return found;
1593}
1594
1595
1596static int tls_match_altsubject(X509 *cert, const char *match)
1597{
1598 int type;
1599 const char *pos, *end;
1600 size_t len;
1601
1602 pos = match;
1603 do {
1604 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1605 type = GEN_EMAIL;
1606 pos += 6;
1607 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1608 type = GEN_DNS;
1609 pos += 4;
1610 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1611 type = GEN_URI;
1612 pos += 4;
1613 } else {
1614 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1615 "match '%s'", pos);
1616 return 0;
1617 }
1618 end = os_strchr(pos, ';');
1619 while (end) {
1620 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1621 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1622 os_strncmp(end + 1, "URI:", 4) == 0)
1623 break;
1624 end = os_strchr(end + 1, ';');
1625 }
1626 if (end)
1627 len = end - pos;
1628 else
1629 len = os_strlen(pos);
1630 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1631 return 1;
1632 pos = end + 1;
1633 } while (end);
1634
1635 return 0;
1636}
1637
1638
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001639#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001640static int domain_suffix_match(const u8 *val, size_t len, const char *match,
1641 int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001642{
1643 size_t i, match_len;
1644
1645 /* Check for embedded nuls that could mess up suffix matching */
1646 for (i = 0; i < len; i++) {
1647 if (val[i] == '\0') {
1648 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1649 return 0;
1650 }
1651 }
1652
1653 match_len = os_strlen(match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001654 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001655 return 0;
1656
1657 if (os_strncasecmp((const char *) val + len - match_len, match,
1658 match_len) != 0)
1659 return 0; /* no match */
1660
1661 if (match_len == len)
1662 return 1; /* exact match */
1663
1664 if (val[len - match_len - 1] == '.')
1665 return 1; /* full label match completes suffix match */
1666
1667 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1668 return 0;
1669}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001670#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001671
1672
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001673static int tls_match_suffix(X509 *cert, const char *match, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001674{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001675#ifdef CONFIG_NATIVE_WINDOWS
1676 /* wincrypt.h has conflicting X509_NAME definition */
1677 return -1;
1678#else /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001679 GENERAL_NAME *gen;
1680 void *ext;
1681 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001682 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001683 int dns_name = 0;
1684 X509_NAME *name;
1685
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001686 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
1687 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001688
1689 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1690
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001691 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
1692 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001693 if (gen->type != GEN_DNS)
1694 continue;
1695 dns_name++;
1696 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
1697 gen->d.dNSName->data,
1698 gen->d.dNSName->length);
1699 if (domain_suffix_match(gen->d.dNSName->data,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001700 gen->d.dNSName->length, match, full) ==
1701 1) {
1702 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
1703 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001704 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001705 return 1;
1706 }
1707 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001708 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001709
1710 if (dns_name) {
1711 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
1712 return 0;
1713 }
1714
1715 name = X509_get_subject_name(cert);
1716 i = -1;
1717 for (;;) {
1718 X509_NAME_ENTRY *e;
1719 ASN1_STRING *cn;
1720
1721 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
1722 if (i == -1)
1723 break;
1724 e = X509_NAME_get_entry(name, i);
1725 if (e == NULL)
1726 continue;
1727 cn = X509_NAME_ENTRY_get_data(e);
1728 if (cn == NULL)
1729 continue;
1730 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
1731 cn->data, cn->length);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001732 if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
1733 {
1734 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
1735 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001736 return 1;
1737 }
1738 }
1739
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001740 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
1741 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001742 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001743#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001744}
1745
1746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747static enum tls_fail_reason openssl_tls_fail_reason(int err)
1748{
1749 switch (err) {
1750 case X509_V_ERR_CERT_REVOKED:
1751 return TLS_FAIL_REVOKED;
1752 case X509_V_ERR_CERT_NOT_YET_VALID:
1753 case X509_V_ERR_CRL_NOT_YET_VALID:
1754 return TLS_FAIL_NOT_YET_VALID;
1755 case X509_V_ERR_CERT_HAS_EXPIRED:
1756 case X509_V_ERR_CRL_HAS_EXPIRED:
1757 return TLS_FAIL_EXPIRED;
1758 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1759 case X509_V_ERR_UNABLE_TO_GET_CRL:
1760 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1761 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1762 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1763 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1764 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1765 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1766 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1767 case X509_V_ERR_INVALID_CA:
1768 return TLS_FAIL_UNTRUSTED;
1769 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1770 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1771 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1772 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1773 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1774 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1775 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1776 case X509_V_ERR_CERT_UNTRUSTED:
1777 case X509_V_ERR_CERT_REJECTED:
1778 return TLS_FAIL_BAD_CERTIFICATE;
1779 default:
1780 return TLS_FAIL_UNSPECIFIED;
1781 }
1782}
1783
1784
1785static struct wpabuf * get_x509_cert(X509 *cert)
1786{
1787 struct wpabuf *buf;
1788 u8 *tmp;
1789
1790 int cert_len = i2d_X509(cert, NULL);
1791 if (cert_len <= 0)
1792 return NULL;
1793
1794 buf = wpabuf_alloc(cert_len);
1795 if (buf == NULL)
1796 return NULL;
1797
1798 tmp = wpabuf_put(buf, cert_len);
1799 i2d_X509(cert, &tmp);
1800 return buf;
1801}
1802
1803
1804static void openssl_tls_fail_event(struct tls_connection *conn,
1805 X509 *err_cert, int err, int depth,
1806 const char *subject, const char *err_str,
1807 enum tls_fail_reason reason)
1808{
1809 union tls_event_data ev;
1810 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001811 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812
Pavel Grafov4d8552e2018-02-06 11:28:29 +00001813#ifdef ANDROID
1814 log_cert_validation_failure(err_str);
1815#endif
1816
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001817 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 return;
1819
1820 cert = get_x509_cert(err_cert);
1821 os_memset(&ev, 0, sizeof(ev));
1822 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
1823 reason : openssl_tls_fail_reason(err);
1824 ev.cert_fail.depth = depth;
1825 ev.cert_fail.subject = subject;
1826 ev.cert_fail.reason_txt = err_str;
1827 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001828 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 wpabuf_free(cert);
1830}
1831
1832
1833static void openssl_tls_cert_event(struct tls_connection *conn,
1834 X509 *err_cert, int depth,
1835 const char *subject)
1836{
1837 struct wpabuf *cert = NULL;
1838 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001839 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001840 char *altsubject[TLS_MAX_ALT_SUBJECT];
1841 int alt, num_altsubject = 0;
1842 GENERAL_NAME *gen;
1843 void *ext;
1844 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845#ifdef CONFIG_SHA256
1846 u8 hash[32];
1847#endif /* CONFIG_SHA256 */
1848
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001849 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 return;
1851
1852 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001853 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
1854 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 cert = get_x509_cert(err_cert);
1856 ev.peer_cert.cert = cert;
1857 }
1858#ifdef CONFIG_SHA256
1859 if (cert) {
1860 const u8 *addr[1];
1861 size_t len[1];
1862 addr[0] = wpabuf_head(cert);
1863 len[0] = wpabuf_len(cert);
1864 if (sha256_vector(1, addr, len, hash) == 0) {
1865 ev.peer_cert.hash = hash;
1866 ev.peer_cert.hash_len = sizeof(hash);
1867 }
1868 }
1869#endif /* CONFIG_SHA256 */
1870 ev.peer_cert.depth = depth;
1871 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001872
1873 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
1874 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1875 char *pos;
1876
1877 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
1878 break;
1879 gen = sk_GENERAL_NAME_value(ext, i);
1880 if (gen->type != GEN_EMAIL &&
1881 gen->type != GEN_DNS &&
1882 gen->type != GEN_URI)
1883 continue;
1884
1885 pos = os_malloc(10 + gen->d.ia5->length + 1);
1886 if (pos == NULL)
1887 break;
1888 altsubject[num_altsubject++] = pos;
1889
1890 switch (gen->type) {
1891 case GEN_EMAIL:
1892 os_memcpy(pos, "EMAIL:", 6);
1893 pos += 6;
1894 break;
1895 case GEN_DNS:
1896 os_memcpy(pos, "DNS:", 4);
1897 pos += 4;
1898 break;
1899 case GEN_URI:
1900 os_memcpy(pos, "URI:", 4);
1901 pos += 4;
1902 break;
1903 }
1904
1905 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
1906 pos += gen->d.ia5->length;
1907 *pos = '\0';
1908 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001909 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001910
1911 for (alt = 0; alt < num_altsubject; alt++)
1912 ev.peer_cert.altsubject[alt] = altsubject[alt];
1913 ev.peer_cert.num_altsubject = num_altsubject;
1914
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001915 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001917 for (alt = 0; alt < num_altsubject; alt++)
1918 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919}
1920
1921
1922static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1923{
1924 char buf[256];
1925 X509 *err_cert;
1926 int err, depth;
1927 SSL *ssl;
1928 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001929 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001930 char *match, *altmatch, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 const char *err_str;
1932
1933 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001934 if (!err_cert)
1935 return 0;
1936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 err = X509_STORE_CTX_get_error(x509_ctx);
1938 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1939 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1940 SSL_get_ex_data_X509_STORE_CTX_idx());
1941 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1942
1943 conn = SSL_get_app_data(ssl);
1944 if (conn == NULL)
1945 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001946
1947 if (depth == 0)
1948 conn->peer_cert = err_cert;
1949 else if (depth == 1)
1950 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001951 else if (depth == 2)
1952 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001953
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001954 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 match = conn->subject_match;
1956 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001957 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001958 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959
1960 if (!preverify_ok && !conn->ca_cert_verify)
1961 preverify_ok = 1;
1962 if (!preverify_ok && depth > 0 && conn->server_cert_only)
1963 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001964 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
1965 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
1966 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
1967 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
1968 "time mismatch");
1969 preverify_ok = 1;
1970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971
1972 err_str = X509_verify_cert_error_string(err);
1973
1974#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001975 /*
1976 * Do not require preverify_ok so we can explicity allow otherwise
1977 * invalid pinned server certificates.
1978 */
1979 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 struct wpabuf *cert;
1981 cert = get_x509_cert(err_cert);
1982 if (!cert) {
1983 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
1984 "server certificate data");
1985 preverify_ok = 0;
1986 } else {
1987 u8 hash[32];
1988 const u8 *addr[1];
1989 size_t len[1];
1990 addr[0] = wpabuf_head(cert);
1991 len[0] = wpabuf_len(cert);
1992 if (sha256_vector(1, addr, len, hash) < 0 ||
1993 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
1994 err_str = "Server certificate mismatch";
1995 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
1996 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001997 } else if (!preverify_ok) {
1998 /*
1999 * Certificate matches pinned certificate, allow
2000 * regardless of other problems.
2001 */
2002 wpa_printf(MSG_DEBUG,
2003 "OpenSSL: Ignore validation issues for a pinned server certificate");
2004 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 }
2006 wpabuf_free(cert);
2007 }
2008 }
2009#endif /* CONFIG_SHA256 */
2010
2011 if (!preverify_ok) {
2012 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2013 " error %d (%s) depth %d for '%s'", err, err_str,
2014 depth, buf);
2015 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2016 err_str, TLS_FAIL_UNSPECIFIED);
2017 return preverify_ok;
2018 }
2019
2020 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2021 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2022 preverify_ok, err, err_str,
2023 conn->ca_cert_verify, depth, buf);
2024 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2025 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2026 "match with '%s'", buf, match);
2027 preverify_ok = 0;
2028 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2029 "Subject mismatch",
2030 TLS_FAIL_SUBJECT_MISMATCH);
2031 } else if (depth == 0 && altmatch &&
2032 !tls_match_altsubject(err_cert, altmatch)) {
2033 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2034 "'%s' not found", altmatch);
2035 preverify_ok = 0;
2036 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2037 "AltSubject mismatch",
2038 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002039 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002040 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002041 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2042 suffix_match);
2043 preverify_ok = 0;
2044 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2045 "Domain suffix mismatch",
2046 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002047 } else if (depth == 0 && domain_match &&
2048 !tls_match_suffix(err_cert, domain_match, 1)) {
2049 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2050 domain_match);
2051 preverify_ok = 0;
2052 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2053 "Domain mismatch",
2054 TLS_FAIL_DOMAIN_MISMATCH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 } else
2056 openssl_tls_cert_event(conn, err_cert, depth, buf);
2057
2058 if (conn->cert_probe && preverify_ok && depth == 0) {
2059 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2060 "on probe-only run");
2061 preverify_ok = 0;
2062 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2063 "Server certificate chain probe",
2064 TLS_FAIL_SERVER_CHAIN_PROBE);
2065 }
2066
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002067#ifdef CONFIG_SUITEB
2068 if (conn->flags & TLS_CONN_SUITEB) {
2069 EVP_PKEY *pk;
2070 RSA *rsa;
2071 int len = -1;
2072
2073 pk = X509_get_pubkey(err_cert);
2074 if (pk) {
2075 rsa = EVP_PKEY_get1_RSA(pk);
2076 if (rsa) {
2077 len = RSA_bits(rsa);
2078 RSA_free(rsa);
2079 }
2080 EVP_PKEY_free(pk);
2081 }
2082
2083 if (len >= 0) {
2084 wpa_printf(MSG_DEBUG,
2085 "OpenSSL: RSA modulus size: %d bits", len);
2086 if (len < 3072) {
2087 preverify_ok = 0;
2088 openssl_tls_fail_event(
2089 conn, err_cert, err,
2090 depth, buf,
2091 "Insufficient RSA modulus size",
2092 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2093 }
2094 }
2095 }
2096#endif /* CONFIG_SUITEB */
2097
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002098#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002099 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2100 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002101 enum ocsp_result res;
2102
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002103 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2104 conn->peer_issuer,
2105 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002106 if (res == OCSP_REVOKED) {
2107 preverify_ok = 0;
2108 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2109 "certificate revoked",
2110 TLS_FAIL_REVOKED);
2111 if (err == X509_V_OK)
2112 X509_STORE_CTX_set_error(
2113 x509_ctx, X509_V_ERR_CERT_REVOKED);
2114 } else if (res != OCSP_GOOD &&
2115 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2116 preverify_ok = 0;
2117 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2118 "bad certificate status response",
2119 TLS_FAIL_UNSPECIFIED);
2120 }
2121 }
2122#endif /* OPENSSL_IS_BORINGSSL */
2123
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002124 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002125 context->event_cb(context->cb_ctx,
2126 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128 return preverify_ok;
2129}
2130
2131
2132#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002133static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002135 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136 X509_LOOKUP *lookup;
2137 int ret = 0;
2138
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002139 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 X509_LOOKUP_file());
2141 if (lookup == NULL) {
2142 tls_show_errors(MSG_WARNING, __func__,
2143 "Failed add lookup for X509 store");
2144 return -1;
2145 }
2146
2147 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2148 unsigned long err = ERR_peek_error();
2149 tls_show_errors(MSG_WARNING, __func__,
2150 "Failed load CA in DER format");
2151 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2152 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2153 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2154 "cert already in hash table error",
2155 __func__);
2156 } else
2157 ret = -1;
2158 }
2159
2160 return ret;
2161}
2162#endif /* OPENSSL_NO_STDIO */
2163
2164
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002165static int tls_connection_ca_cert(struct tls_data *data,
2166 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167 const char *ca_cert, const u8 *ca_cert_blob,
2168 size_t ca_cert_blob_len, const char *ca_path)
2169{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002170 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002171 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172
2173 /*
2174 * Remove previously configured trusted CA certificates before adding
2175 * new ones.
2176 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002177 store = X509_STORE_new();
2178 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2180 "certificate store", __func__);
2181 return -1;
2182 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002183 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184
2185 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2186 conn->ca_cert_verify = 1;
2187
2188 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2189 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2190 "chain");
2191 conn->cert_probe = 1;
2192 conn->ca_cert_verify = 0;
2193 return 0;
2194 }
2195
2196 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2197#ifdef CONFIG_SHA256
2198 const char *pos = ca_cert + 7;
2199 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2200 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2201 "hash value '%s'", ca_cert);
2202 return -1;
2203 }
2204 pos += 14;
2205 if (os_strlen(pos) != 32 * 2) {
2206 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2207 "hash length in ca_cert '%s'", ca_cert);
2208 return -1;
2209 }
2210 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2211 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2212 "value in ca_cert '%s'", ca_cert);
2213 return -1;
2214 }
2215 conn->server_cert_only = 1;
2216 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2217 "certificate match");
2218 return 0;
2219#else /* CONFIG_SHA256 */
2220 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2221 "cannot validate server certificate hash");
2222 return -1;
2223#endif /* CONFIG_SHA256 */
2224 }
2225
2226 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002227 X509 *cert = d2i_X509(NULL,
2228 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229 ca_cert_blob_len);
2230 if (cert == NULL) {
2231 tls_show_errors(MSG_WARNING, __func__,
2232 "Failed to parse ca_cert_blob");
2233 return -1;
2234 }
2235
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002236 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2237 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 unsigned long err = ERR_peek_error();
2239 tls_show_errors(MSG_WARNING, __func__,
2240 "Failed to add ca_cert_blob to "
2241 "certificate store");
2242 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2243 ERR_GET_REASON(err) ==
2244 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2245 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2246 "cert already in hash table error",
2247 __func__);
2248 } else {
2249 X509_free(cert);
2250 return -1;
2251 }
2252 }
2253 X509_free(cert);
2254 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2255 "to certificate store", __func__);
2256 return 0;
2257 }
2258
2259#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002260 /* Single alias */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002262 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002263 &ca_cert[11]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002265 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2266 return 0;
2267 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002269 /* Multiple aliases separated by space */
2270 if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) {
2271 char *aliases = os_strdup(&ca_cert[12]);
2272 const char *delim = " ";
2273 int rc = 0;
2274 char *savedptr;
2275 char *alias;
2276
2277 if (!aliases)
2278 return -1;
2279 alias = strtok_r(aliases, delim, &savedptr);
2280 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2281 if (tls_add_ca_from_keystore_encoded(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002282 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002283 wpa_printf(MSG_WARNING,
2284 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2285 __func__, alias);
2286 rc = -1;
2287 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 }
2289 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002290 os_free(aliases);
2291 if (rc)
2292 return rc;
2293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2295 return 0;
2296 }
2297#endif /* ANDROID */
2298
2299#ifdef CONFIG_NATIVE_WINDOWS
2300 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2301 0) {
2302 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2303 "system certificate store");
2304 return 0;
2305 }
2306#endif /* CONFIG_NATIVE_WINDOWS */
2307
2308 if (ca_cert || ca_path) {
2309#ifndef OPENSSL_NO_STDIO
2310 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2311 1) {
2312 tls_show_errors(MSG_WARNING, __func__,
2313 "Failed to load root certificates");
2314 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002315 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2317 "DER format CA certificate",
2318 __func__);
2319 } else
2320 return -1;
2321 } else {
2322 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2323 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002324 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 }
2326#else /* OPENSSL_NO_STDIO */
2327 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2328 __func__);
2329 return -1;
2330#endif /* OPENSSL_NO_STDIO */
2331 } else {
2332 /* No ca_cert configured - do not try to verify server
2333 * certificate */
2334 conn->ca_cert_verify = 0;
2335 }
2336
2337 return 0;
2338}
2339
2340
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002341static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002343 SSL_CTX *ssl_ctx = data->ssl;
2344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 if (ca_cert) {
2346 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2347 {
2348 tls_show_errors(MSG_WARNING, __func__,
2349 "Failed to load root certificates");
2350 return -1;
2351 }
2352
2353 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2354 "certificate(s) loaded");
2355
2356#ifndef OPENSSL_NO_STDIO
2357 /* Add the same CAs to the client certificate requests */
2358 SSL_CTX_set_client_CA_list(ssl_ctx,
2359 SSL_load_client_CA_file(ca_cert));
2360#endif /* OPENSSL_NO_STDIO */
2361 }
2362
2363 return 0;
2364}
2365
2366
2367int tls_global_set_verify(void *ssl_ctx, int check_crl)
2368{
2369 int flags;
2370
2371 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002372 struct tls_data *data = ssl_ctx;
2373 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 if (cs == NULL) {
2375 tls_show_errors(MSG_INFO, __func__, "Failed to get "
2376 "certificate store when enabling "
2377 "check_crl");
2378 return -1;
2379 }
2380 flags = X509_V_FLAG_CRL_CHECK;
2381 if (check_crl == 2)
2382 flags |= X509_V_FLAG_CRL_CHECK_ALL;
2383 X509_STORE_set_flags(cs, flags);
2384 }
2385 return 0;
2386}
2387
2388
2389static int tls_connection_set_subject_match(struct tls_connection *conn,
2390 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002391 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002392 const char *suffix_match,
2393 const char *domain_match)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394{
2395 os_free(conn->subject_match);
2396 conn->subject_match = NULL;
2397 if (subject_match) {
2398 conn->subject_match = os_strdup(subject_match);
2399 if (conn->subject_match == NULL)
2400 return -1;
2401 }
2402
2403 os_free(conn->altsubject_match);
2404 conn->altsubject_match = NULL;
2405 if (altsubject_match) {
2406 conn->altsubject_match = os_strdup(altsubject_match);
2407 if (conn->altsubject_match == NULL)
2408 return -1;
2409 }
2410
Dmitry Shmidt051af732013-10-22 13:52:46 -07002411 os_free(conn->suffix_match);
2412 conn->suffix_match = NULL;
2413 if (suffix_match) {
2414 conn->suffix_match = os_strdup(suffix_match);
2415 if (conn->suffix_match == NULL)
2416 return -1;
2417 }
2418
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002419 os_free(conn->domain_match);
2420 conn->domain_match = NULL;
2421 if (domain_match) {
2422 conn->domain_match = os_strdup(domain_match);
2423 if (conn->domain_match == NULL)
2424 return -1;
2425 }
2426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 return 0;
2428}
2429
2430
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002431#ifdef CONFIG_SUITEB
2432#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2433static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002434{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002435 struct tls_connection *conn = arg;
2436
2437 /*
2438 * This cert_cb() is not really the best location for doing a
2439 * constraint check for the ServerKeyExchange message, but this seems to
2440 * be the only place where the current OpenSSL sequence can be
2441 * terminated cleanly with an TLS alert going out to the server.
2442 */
2443
2444 if (!(conn->flags & TLS_CONN_SUITEB))
2445 return 1;
2446
2447 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
2448 if (conn->cipher_suite != 0x9f)
2449 return 1;
2450
2451 if (conn->server_dh_prime_len >= 3072)
2452 return 1;
2453
2454 wpa_printf(MSG_DEBUG,
2455 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
2456 conn->server_dh_prime_len);
2457 return 0;
2458}
2459#endif /* OPENSSL_VERSION_NUMBER */
2460#endif /* CONFIG_SUITEB */
2461
2462
Roshan Pius3a1667e2018-07-03 15:17:14 -07002463static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
2464 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002465{
2466 SSL *ssl = conn->ssl;
2467
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002468#ifdef SSL_OP_NO_TICKET
2469 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
2470 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002471 else
2472 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002473#endif /* SSL_OP_NO_TICKET */
2474
2475#ifdef SSL_OP_NO_TLSv1
2476 if (flags & TLS_CONN_DISABLE_TLSv1_0)
2477 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2478 else
2479 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
2480#endif /* SSL_OP_NO_TLSv1 */
2481#ifdef SSL_OP_NO_TLSv1_1
2482 if (flags & TLS_CONN_DISABLE_TLSv1_1)
2483 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2484 else
2485 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
2486#endif /* SSL_OP_NO_TLSv1_1 */
2487#ifdef SSL_OP_NO_TLSv1_2
2488 if (flags & TLS_CONN_DISABLE_TLSv1_2)
2489 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
2490 else
2491 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
2492#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002493#ifdef SSL_OP_NO_TLSv1_3
2494 if (flags & TLS_CONN_DISABLE_TLSv1_3)
2495 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
2496 else
2497 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
2498#endif /* SSL_OP_NO_TLSv1_3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002499#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07002500#ifdef OPENSSL_IS_BORINGSSL
2501 /* Start with defaults from BoringSSL */
2502 SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, NULL, 0);
2503#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002504#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2505 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
2506 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
2507
Roshan Pius3a1667e2018-07-03 15:17:14 -07002508 if (openssl_ciphers) {
2509 wpa_printf(MSG_DEBUG,
2510 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
2511 openssl_ciphers);
2512 ciphers = openssl_ciphers;
2513 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002514 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
2515 wpa_printf(MSG_INFO,
2516 "OpenSSL: Failed to set Suite B ciphers");
2517 return -1;
2518 }
2519 } else if (flags & TLS_CONN_SUITEB) {
2520 EC_KEY *ecdh;
2521 const char *ciphers =
2522 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07002523 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002524
Roshan Pius3a1667e2018-07-03 15:17:14 -07002525 if (openssl_ciphers) {
2526 wpa_printf(MSG_DEBUG,
2527 "OpenSSL: Override ciphers for Suite B: %s",
2528 openssl_ciphers);
2529 ciphers = openssl_ciphers;
2530 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002531 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
2532 wpa_printf(MSG_INFO,
2533 "OpenSSL: Failed to set Suite B ciphers");
2534 return -1;
2535 }
2536
Roshan Pius3a1667e2018-07-03 15:17:14 -07002537 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002538 wpa_printf(MSG_INFO,
2539 "OpenSSL: Failed to set Suite B curves");
2540 return -1;
2541 }
2542
2543 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
2544 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
2545 EC_KEY_free(ecdh);
2546 wpa_printf(MSG_INFO,
2547 "OpenSSL: Failed to set ECDH parameter");
2548 return -1;
2549 }
2550 EC_KEY_free(ecdh);
2551 }
2552 if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002553#ifdef OPENSSL_IS_BORINGSSL
2554 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
2555
2556 if (SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, sigalgs,
2557 1) != 1) {
2558 wpa_printf(MSG_INFO,
2559 "OpenSSL: Failed to set Suite B sigalgs");
2560 return -1;
2561 }
2562#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002563 /* ECDSA+SHA384 if need to add EC support here */
2564 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
2565 wpa_printf(MSG_INFO,
2566 "OpenSSL: Failed to set Suite B sigalgs");
2567 return -1;
2568 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002569#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002570
2571 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2572 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2573 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
2574 }
2575#else /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2576 if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) {
2577 wpa_printf(MSG_ERROR,
2578 "OpenSSL: Suite B RSA case not supported with this OpenSSL version");
2579 return -1;
2580 }
2581#endif /* OPENSSL_VERSION_NUMBER */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002582
2583#ifdef OPENSSL_IS_BORINGSSL
2584 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
2585 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
2586 int nid[1] = { NID_secp384r1 };
2587
2588 if (SSL_set1_curves(ssl, nid, 1) != 1) {
2589 wpa_printf(MSG_INFO,
2590 "OpenSSL: Failed to set Suite B curves");
2591 return -1;
2592 }
2593
2594 if (SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, sigalgs,
2595 1) != 1) {
2596 wpa_printf(MSG_INFO,
2597 "OpenSSL: Failed to set Suite B sigalgs");
2598 return -1;
2599 }
2600 }
2601#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002602#endif /* CONFIG_SUITEB */
2603
2604 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002605}
2606
2607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002609 int verify_peer, unsigned int flags,
2610 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611{
2612 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002613 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614
2615 if (conn == NULL)
2616 return -1;
2617
2618 if (verify_peer) {
2619 conn->ca_cert_verify = 1;
2620 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
2621 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
2622 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
2623 } else {
2624 conn->ca_cert_verify = 0;
2625 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
2626 }
2627
Roshan Pius3a1667e2018-07-03 15:17:14 -07002628 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002629 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002630 conn->flags = flags;
2631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002632 SSL_set_accept_state(conn->ssl);
2633
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002634 if (data->tls_session_lifetime == 0) {
2635 /*
2636 * Set session id context to a unique value to make sure
2637 * session resumption cannot be used either through session
2638 * caching or TLS ticket extension.
2639 */
2640 counter++;
2641 SSL_set_session_id_context(conn->ssl,
2642 (const unsigned char *) &counter,
2643 sizeof(counter));
2644 } else if (session_ctx) {
2645 SSL_set_session_id_context(conn->ssl, session_ctx,
2646 session_ctx_len);
2647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648
2649 return 0;
2650}
2651
2652
2653static int tls_connection_client_cert(struct tls_connection *conn,
2654 const char *client_cert,
2655 const u8 *client_cert_blob,
2656 size_t client_cert_blob_len)
2657{
2658 if (client_cert == NULL && client_cert_blob == NULL)
2659 return 0;
2660
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002661#ifdef PKCS12_FUNCS
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002662#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002663 /*
2664 * Clear previously set extra chain certificates, if any, from PKCS#12
2665 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2666 * chain properly.
2667 */
2668 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
2669#endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2670#endif /* PKCS12_FUNCS */
2671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 if (client_cert_blob &&
2673 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
2674 client_cert_blob_len) == 1) {
2675 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
2676 "OK");
2677 return 0;
2678 } else if (client_cert_blob) {
2679 tls_show_errors(MSG_DEBUG, __func__,
2680 "SSL_use_certificate_ASN1 failed");
2681 }
2682
2683 if (client_cert == NULL)
2684 return -1;
2685
2686#ifdef ANDROID
2687 if (os_strncmp("keystore://", client_cert, 11) == 0) {
2688 BIO *bio = BIO_from_keystore(&client_cert[11]);
2689 X509 *x509 = NULL;
2690 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002691 if (bio) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694 if (x509) {
2695 if (SSL_use_certificate(conn->ssl, x509) == 1)
2696 ret = 0;
2697 X509_free(x509);
2698 }
Paul Stewart50772e82017-01-25 13:59:16 -08002699
2700 /* Read additional certificates into the chain. */
2701 while (bio) {
2702 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2703 if (x509) {
2704 /* Takes ownership of x509 */
2705 SSL_add0_chain_cert(conn->ssl, x509);
2706 } else {
2707 BIO_free(bio);
2708 bio = NULL;
2709 }
2710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 return ret;
2712 }
2713#endif /* ANDROID */
2714
2715#ifndef OPENSSL_NO_STDIO
2716 if (SSL_use_certificate_file(conn->ssl, client_cert,
2717 SSL_FILETYPE_ASN1) == 1) {
2718 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
2719 " --> OK");
2720 return 0;
2721 }
2722
2723 if (SSL_use_certificate_file(conn->ssl, client_cert,
2724 SSL_FILETYPE_PEM) == 1) {
2725 ERR_clear_error();
2726 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
2727 " --> OK");
2728 return 0;
2729 }
2730
2731 tls_show_errors(MSG_DEBUG, __func__,
2732 "SSL_use_certificate_file failed");
2733#else /* OPENSSL_NO_STDIO */
2734 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2735#endif /* OPENSSL_NO_STDIO */
2736
2737 return -1;
2738}
2739
2740
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002741static int tls_global_client_cert(struct tls_data *data,
2742 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743{
2744#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002745 SSL_CTX *ssl_ctx = data->ssl;
2746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747 if (client_cert == NULL)
2748 return 0;
2749
2750 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2751 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002752 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2754 SSL_FILETYPE_PEM) != 1) {
2755 tls_show_errors(MSG_INFO, __func__,
2756 "Failed to load client certificate");
2757 return -1;
2758 }
2759 return 0;
2760#else /* OPENSSL_NO_STDIO */
2761 if (client_cert == NULL)
2762 return 0;
2763 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2764 return -1;
2765#endif /* OPENSSL_NO_STDIO */
2766}
2767
2768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002770static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 const char *passwd)
2772{
2773 EVP_PKEY *pkey;
2774 X509 *cert;
2775 STACK_OF(X509) *certs;
2776 int res = 0;
2777 char buf[256];
2778
2779 pkey = NULL;
2780 cert = NULL;
2781 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002782 if (!passwd)
2783 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
2785 tls_show_errors(MSG_DEBUG, __func__,
2786 "Failed to parse PKCS12 file");
2787 PKCS12_free(p12);
2788 return -1;
2789 }
2790 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
2791
2792 if (cert) {
2793 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2794 sizeof(buf));
2795 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
2796 "subject='%s'", buf);
2797 if (ssl) {
2798 if (SSL_use_certificate(ssl, cert) != 1)
2799 res = -1;
2800 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002801 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002802 res = -1;
2803 }
2804 X509_free(cert);
2805 }
2806
2807 if (pkey) {
2808 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
2809 if (ssl) {
2810 if (SSL_use_PrivateKey(ssl, pkey) != 1)
2811 res = -1;
2812 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002813 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 res = -1;
2815 }
2816 EVP_PKEY_free(pkey);
2817 }
2818
2819 if (certs) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002820#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002821 if (ssl)
2822 SSL_clear_chain_certs(ssl);
2823 else
2824 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002825 while ((cert = sk_X509_pop(certs)) != NULL) {
2826 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2827 sizeof(buf));
2828 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2829 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002830 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
2831 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
2832 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002833 tls_show_errors(MSG_DEBUG, __func__,
2834 "Failed to add additional certificate");
2835 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002836 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002837 break;
2838 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002839 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002840 }
2841 if (!res) {
2842 /* Try to continue anyway */
2843 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002844 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002845#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002846 if (ssl)
2847 res = SSL_build_cert_chain(
2848 ssl,
2849 SSL_BUILD_CHAIN_FLAG_CHECK |
2850 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
2851 else
2852 res = SSL_CTX_build_cert_chain(
2853 data->ssl,
2854 SSL_BUILD_CHAIN_FLAG_CHECK |
2855 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002856 if (!res) {
2857 tls_show_errors(MSG_DEBUG, __func__,
2858 "Failed to build certificate chain");
2859 } else if (res == 2) {
2860 wpa_printf(MSG_DEBUG,
2861 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2862 }
2863#endif /* OPENSSL_IS_BORINGSSL */
2864 /*
2865 * Try to continue regardless of result since it is possible for
2866 * the extra certificates not to be required.
2867 */
2868 res = 0;
2869#else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002870 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871 while ((cert = sk_X509_pop(certs)) != NULL) {
2872 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2873 sizeof(buf));
2874 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2875 " from PKCS12: subject='%s'", buf);
2876 /*
2877 * There is no SSL equivalent for the chain cert - so
2878 * always add it to the context...
2879 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002880 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
2881 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002882 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 res = -1;
2884 break;
2885 }
2886 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002887 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002888#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 }
2890
2891 PKCS12_free(p12);
2892
2893 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002894 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895
2896 return res;
2897}
2898#endif /* PKCS12_FUNCS */
2899
2900
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002901static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
2902 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903{
2904#ifdef PKCS12_FUNCS
2905 FILE *f;
2906 PKCS12 *p12;
2907
2908 f = fopen(private_key, "rb");
2909 if (f == NULL)
2910 return -1;
2911
2912 p12 = d2i_PKCS12_fp(f, NULL);
2913 fclose(f);
2914
2915 if (p12 == NULL) {
2916 tls_show_errors(MSG_INFO, __func__,
2917 "Failed to use PKCS#12 file");
2918 return -1;
2919 }
2920
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002921 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922
2923#else /* PKCS12_FUNCS */
2924 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
2925 "p12/pfx files");
2926 return -1;
2927#endif /* PKCS12_FUNCS */
2928}
2929
2930
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002931static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 const u8 *blob, size_t len, const char *passwd)
2933{
2934#ifdef PKCS12_FUNCS
2935 PKCS12 *p12;
2936
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002937 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 if (p12 == NULL) {
2939 tls_show_errors(MSG_INFO, __func__,
2940 "Failed to use PKCS#12 blob");
2941 return -1;
2942 }
2943
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002944 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945
2946#else /* PKCS12_FUNCS */
2947 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
2948 "p12/pfx blobs");
2949 return -1;
2950#endif /* PKCS12_FUNCS */
2951}
2952
2953
2954#ifndef OPENSSL_NO_ENGINE
2955static int tls_engine_get_cert(struct tls_connection *conn,
2956 const char *cert_id,
2957 X509 **cert)
2958{
2959 /* this runs after the private key is loaded so no PIN is required */
2960 struct {
2961 const char *cert_id;
2962 X509 *cert;
2963 } params;
2964 params.cert_id = cert_id;
2965 params.cert = NULL;
2966
2967 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
2968 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002969 unsigned long err = ERR_get_error();
2970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
2972 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002973 ERR_error_string(err, NULL));
2974 if (tls_is_pin_error(err))
2975 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2977 }
2978 if (!params.cert) {
2979 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
2980 " '%s'", cert_id);
2981 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2982 }
2983 *cert = params.cert;
2984 return 0;
2985}
2986#endif /* OPENSSL_NO_ENGINE */
2987
2988
2989static int tls_connection_engine_client_cert(struct tls_connection *conn,
2990 const char *cert_id)
2991{
2992#ifndef OPENSSL_NO_ENGINE
2993 X509 *cert;
2994
2995 if (tls_engine_get_cert(conn, cert_id, &cert))
2996 return -1;
2997
2998 if (!SSL_use_certificate(conn->ssl, cert)) {
2999 tls_show_errors(MSG_ERROR, __func__,
3000 "SSL_use_certificate failed");
3001 X509_free(cert);
3002 return -1;
3003 }
3004 X509_free(cert);
3005 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3006 "OK");
3007 return 0;
3008
3009#else /* OPENSSL_NO_ENGINE */
3010 return -1;
3011#endif /* OPENSSL_NO_ENGINE */
3012}
3013
3014
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003015static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 struct tls_connection *conn,
3017 const char *ca_cert_id)
3018{
3019#ifndef OPENSSL_NO_ENGINE
3020 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003021 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003022 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023
3024 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3025 return -1;
3026
3027 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003028 store = X509_STORE_new();
3029 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3031 "certificate store", __func__);
3032 X509_free(cert);
3033 return -1;
3034 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003035 SSL_CTX_set_cert_store(ssl_ctx, store);
3036 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 unsigned long err = ERR_peek_error();
3038 tls_show_errors(MSG_WARNING, __func__,
3039 "Failed to add CA certificate from engine "
3040 "to certificate store");
3041 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3042 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3043 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3044 " already in hash table error",
3045 __func__);
3046 } else {
3047 X509_free(cert);
3048 return -1;
3049 }
3050 }
3051 X509_free(cert);
3052 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3053 "to certificate store", __func__);
3054 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003055 conn->ca_cert_verify = 1;
3056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057 return 0;
3058
3059#else /* OPENSSL_NO_ENGINE */
3060 return -1;
3061#endif /* OPENSSL_NO_ENGINE */
3062}
3063
3064
3065static int tls_connection_engine_private_key(struct tls_connection *conn)
3066{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003067#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3069 tls_show_errors(MSG_ERROR, __func__,
3070 "ENGINE: cannot use private key for TLS");
3071 return -1;
3072 }
3073 if (!SSL_check_private_key(conn->ssl)) {
3074 tls_show_errors(MSG_INFO, __func__,
3075 "Private key failed verification");
3076 return -1;
3077 }
3078 return 0;
3079#else /* OPENSSL_NO_ENGINE */
3080 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3081 "engine support was not compiled in");
3082 return -1;
3083#endif /* OPENSSL_NO_ENGINE */
3084}
3085
3086
Roshan Pius3a1667e2018-07-03 15:17:14 -07003087#ifndef OPENSSL_NO_STDIO
3088static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003089{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003090 if (!password)
3091 return 0;
3092 os_strlcpy(buf, (const char *) password, size);
3093 return os_strlen(buf);
3094}
3095#endif /* OPENSSL_NO_STDIO */
3096
3097
3098static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3099 const char *private_key,
3100 const char *private_key_passwd)
3101{
3102#ifndef OPENSSL_NO_STDIO
3103 BIO *bio;
3104 EVP_PKEY *pkey;
3105 int ret;
3106
3107 /* First try ASN.1 (DER). */
3108 bio = BIO_new_file(private_key, "r");
3109 if (!bio)
3110 return -1;
3111 pkey = d2i_PrivateKey_bio(bio, NULL);
3112 BIO_free(bio);
3113
3114 if (pkey) {
3115 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3116 } else {
3117 /* Try PEM with the provided password. */
3118 bio = BIO_new_file(private_key, "r");
3119 if (!bio)
3120 return -1;
3121 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3122 (void *) private_key_passwd);
3123 BIO_free(bio);
3124 if (!pkey)
3125 return -1;
3126 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3127 /* Clear errors from the previous failed load. */
3128 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003129 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003130
3131 if (ssl)
3132 ret = SSL_use_PrivateKey(ssl, pkey);
3133 else
3134 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3135
3136 EVP_PKEY_free(pkey);
3137 return ret == 1 ? 0 : -1;
3138#else /* OPENSSL_NO_STDIO */
3139 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3140 return -1;
3141#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003142}
3143
3144
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003145static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146 struct tls_connection *conn,
3147 const char *private_key,
3148 const char *private_key_passwd,
3149 const u8 *private_key_blob,
3150 size_t private_key_blob_len)
3151{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 int ok;
3153
3154 if (private_key == NULL && private_key_blob == NULL)
3155 return 0;
3156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157 ok = 0;
3158 while (private_key_blob) {
3159 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3160 (u8 *) private_key_blob,
3161 private_key_blob_len) == 1) {
3162 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3163 "ASN1(EVP_PKEY_RSA) --> OK");
3164 ok = 1;
3165 break;
3166 }
3167
3168 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3169 (u8 *) private_key_blob,
3170 private_key_blob_len) == 1) {
3171 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3172 "ASN1(EVP_PKEY_DSA) --> OK");
3173 ok = 1;
3174 break;
3175 }
3176
3177 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
3178 (u8 *) private_key_blob,
3179 private_key_blob_len) == 1) {
3180 wpa_printf(MSG_DEBUG, "OpenSSL: "
3181 "SSL_use_RSAPrivateKey_ASN1 --> OK");
3182 ok = 1;
3183 break;
3184 }
3185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003186 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003187 private_key_blob_len,
3188 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
3190 "OK");
3191 ok = 1;
3192 break;
3193 }
3194
3195 break;
3196 }
3197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003199 if (tls_use_private_key_file(data, conn->ssl, private_key,
3200 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201 ok = 1;
3202 break;
3203 }
3204
Roshan Pius3a1667e2018-07-03 15:17:14 -07003205 if (tls_read_pkcs12(data, conn->ssl, private_key,
3206 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
3208 "--> OK");
3209 ok = 1;
3210 break;
3211 }
3212
3213 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
3214 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
3215 "access certificate store --> OK");
3216 ok = 1;
3217 break;
3218 }
3219
3220 break;
3221 }
3222
3223 if (!ok) {
3224 tls_show_errors(MSG_INFO, __func__,
3225 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 return -1;
3227 }
3228 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230 if (!SSL_check_private_key(conn->ssl)) {
3231 tls_show_errors(MSG_INFO, __func__, "Private key failed "
3232 "verification");
3233 return -1;
3234 }
3235
3236 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
3237 return 0;
3238}
3239
3240
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003241static int tls_global_private_key(struct tls_data *data,
3242 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 const char *private_key_passwd)
3244{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003245 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246
3247 if (private_key == NULL)
3248 return 0;
3249
Roshan Pius3a1667e2018-07-03 15:17:14 -07003250 if (tls_use_private_key_file(data, NULL, private_key,
3251 private_key_passwd) &&
3252 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 tls_show_errors(MSG_INFO, __func__,
3254 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 ERR_clear_error();
3256 return -1;
3257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 if (!SSL_CTX_check_private_key(ssl_ctx)) {
3261 tls_show_errors(MSG_INFO, __func__,
3262 "Private key failed verification");
3263 return -1;
3264 }
3265
3266 return 0;
3267}
3268
3269
3270static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
3271{
3272#ifdef OPENSSL_NO_DH
3273 if (dh_file == NULL)
3274 return 0;
3275 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3276 "dh_file specified");
3277 return -1;
3278#else /* OPENSSL_NO_DH */
3279 DH *dh;
3280 BIO *bio;
3281
3282 /* TODO: add support for dh_blob */
3283 if (dh_file == NULL)
3284 return 0;
3285 if (conn == NULL)
3286 return -1;
3287
3288 bio = BIO_new_file(dh_file, "r");
3289 if (bio == NULL) {
3290 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3291 dh_file, ERR_error_string(ERR_get_error(), NULL));
3292 return -1;
3293 }
3294 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3295 BIO_free(bio);
3296#ifndef OPENSSL_NO_DSA
3297 while (dh == NULL) {
3298 DSA *dsa;
3299 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3300 " trying to parse as DSA params", dh_file,
3301 ERR_error_string(ERR_get_error(), NULL));
3302 bio = BIO_new_file(dh_file, "r");
3303 if (bio == NULL)
3304 break;
3305 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3306 BIO_free(bio);
3307 if (!dsa) {
3308 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3309 "'%s': %s", dh_file,
3310 ERR_error_string(ERR_get_error(), NULL));
3311 break;
3312 }
3313
3314 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3315 dh = DSA_dup_DH(dsa);
3316 DSA_free(dsa);
3317 if (dh == NULL) {
3318 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3319 "params into DH params");
3320 break;
3321 }
3322 break;
3323 }
3324#endif /* !OPENSSL_NO_DSA */
3325 if (dh == NULL) {
3326 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3327 "'%s'", dh_file);
3328 return -1;
3329 }
3330
3331 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
3332 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3333 "%s", dh_file,
3334 ERR_error_string(ERR_get_error(), NULL));
3335 DH_free(dh);
3336 return -1;
3337 }
3338 DH_free(dh);
3339 return 0;
3340#endif /* OPENSSL_NO_DH */
3341}
3342
3343
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003344static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345{
3346#ifdef OPENSSL_NO_DH
3347 if (dh_file == NULL)
3348 return 0;
3349 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3350 "dh_file specified");
3351 return -1;
3352#else /* OPENSSL_NO_DH */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003353 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 DH *dh;
3355 BIO *bio;
3356
3357 /* TODO: add support for dh_blob */
3358 if (dh_file == NULL)
3359 return 0;
3360 if (ssl_ctx == NULL)
3361 return -1;
3362
3363 bio = BIO_new_file(dh_file, "r");
3364 if (bio == NULL) {
3365 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3366 dh_file, ERR_error_string(ERR_get_error(), NULL));
3367 return -1;
3368 }
3369 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3370 BIO_free(bio);
3371#ifndef OPENSSL_NO_DSA
3372 while (dh == NULL) {
3373 DSA *dsa;
3374 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3375 " trying to parse as DSA params", dh_file,
3376 ERR_error_string(ERR_get_error(), NULL));
3377 bio = BIO_new_file(dh_file, "r");
3378 if (bio == NULL)
3379 break;
3380 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3381 BIO_free(bio);
3382 if (!dsa) {
3383 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3384 "'%s': %s", dh_file,
3385 ERR_error_string(ERR_get_error(), NULL));
3386 break;
3387 }
3388
3389 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3390 dh = DSA_dup_DH(dsa);
3391 DSA_free(dsa);
3392 if (dh == NULL) {
3393 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3394 "params into DH params");
3395 break;
3396 }
3397 break;
3398 }
3399#endif /* !OPENSSL_NO_DSA */
3400 if (dh == NULL) {
3401 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3402 "'%s'", dh_file);
3403 return -1;
3404 }
3405
3406 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
3407 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3408 "%s", dh_file,
3409 ERR_error_string(ERR_get_error(), NULL));
3410 DH_free(dh);
3411 return -1;
3412 }
3413 DH_free(dh);
3414 return 0;
3415#endif /* OPENSSL_NO_DH */
3416}
3417
3418
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003419int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
3420 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003421{
3422 SSL *ssl;
3423
3424 if (conn == NULL || keys == NULL)
3425 return -1;
3426 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003427 if (ssl == NULL)
3428 return -1;
3429
3430 os_memset(keys, 0, sizeof(*keys));
3431 keys->client_random = conn->client_random;
3432 keys->client_random_len = SSL_get_client_random(
3433 ssl, conn->client_random, sizeof(conn->client_random));
3434 keys->server_random = conn->server_random;
3435 keys->server_random_len = SSL_get_server_random(
3436 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437
3438 return 0;
3439}
3440
3441
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003442#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003443static int openssl_get_keyblock_size(SSL *ssl)
3444{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003445#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
3446 (defined(LIBRESSL_VERSION_NUMBER) && \
3447 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003448 const EVP_CIPHER *c;
3449 const EVP_MD *h;
3450 int md_size;
3451
3452 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
3453 ssl->read_hash == NULL)
3454 return -1;
3455
3456 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003457 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003458 if (h)
3459 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003460 else if (ssl->s3)
3461 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003462 else
3463 return -1;
3464
3465 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3466 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
3467 EVP_CIPHER_iv_length(c));
3468 return 2 * (EVP_CIPHER_key_length(c) +
3469 md_size +
3470 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003471#else
3472 const SSL_CIPHER *ssl_cipher;
3473 int cipher, digest;
3474 const EVP_CIPHER *c;
3475 const EVP_MD *h;
3476
3477 ssl_cipher = SSL_get_current_cipher(ssl);
3478 if (!ssl_cipher)
3479 return -1;
3480 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
3481 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
3482 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
3483 cipher, digest);
3484 if (cipher < 0 || digest < 0)
3485 return -1;
3486 c = EVP_get_cipherbynid(cipher);
3487 h = EVP_get_digestbynid(digest);
3488 if (!c || !h)
3489 return -1;
3490
3491 wpa_printf(MSG_DEBUG,
3492 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3493 EVP_CIPHER_key_length(c), EVP_MD_size(h),
3494 EVP_CIPHER_iv_length(c));
3495 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
3496 EVP_CIPHER_iv_length(c));
3497#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003498}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003499#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003500
3501
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003502int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
3503 const char *label, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003504{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003505 if (!conn ||
3506 SSL_export_keying_material(conn->ssl, out, out_len, label,
3507 os_strlen(label), NULL, 0, 0) != 1)
3508 return -1;
3509 return 0;
3510}
3511
3512
3513int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
3514 u8 *out, size_t out_len)
3515{
3516#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003517 SSL *ssl;
3518 SSL_SESSION *sess;
3519 u8 *rnd;
3520 int ret = -1;
3521 int skip = 0;
3522 u8 *tmp_out = NULL;
3523 u8 *_out = out;
3524 unsigned char client_random[SSL3_RANDOM_SIZE];
3525 unsigned char server_random[SSL3_RANDOM_SIZE];
3526 unsigned char master_key[64];
3527 size_t master_key_len;
3528 const char *ver;
3529
3530 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003531 * TLS library did not support EAP-FAST key generation, so get the
3532 * needed TLS session parameters and use an internal implementation of
3533 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003534 */
3535
3536 if (conn == NULL)
3537 return -1;
3538 ssl = conn->ssl;
3539 if (ssl == NULL)
3540 return -1;
3541 ver = SSL_get_version(ssl);
3542 sess = SSL_get_session(ssl);
3543 if (!ver || !sess)
3544 return -1;
3545
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003546 skip = openssl_get_keyblock_size(ssl);
3547 if (skip < 0)
3548 return -1;
3549 tmp_out = os_malloc(skip + out_len);
3550 if (!tmp_out)
3551 return -1;
3552 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003553
3554 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
3555 if (!rnd) {
3556 os_free(tmp_out);
3557 return -1;
3558 }
3559
3560 SSL_get_client_random(ssl, client_random, sizeof(client_random));
3561 SSL_get_server_random(ssl, server_random, sizeof(server_random));
3562 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
3563 sizeof(master_key));
3564
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003565 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
3566 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003567
3568 if (os_strcmp(ver, "TLSv1.2") == 0) {
3569 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003570 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003571 _out, skip + out_len);
3572 ret = 0;
3573 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003574 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003575 _out, skip + out_len) == 0) {
3576 ret = 0;
3577 }
3578 os_memset(master_key, 0, sizeof(master_key));
3579 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003580 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003581 os_memcpy(out, _out + skip, out_len);
3582 bin_clear_free(tmp_out, skip);
3583
3584 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003585#else /* OPENSSL_NEED_EAP_FAST_PRF */
3586 wpa_printf(MSG_ERROR,
3587 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3588 return -1;
3589#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590}
3591
3592
3593static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07003594openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595{
3596 int res;
3597 struct wpabuf *out_data;
3598
3599 /*
3600 * Give TLS handshake data from the server (if available) to OpenSSL
3601 * for processing.
3602 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003603 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
3605 < 0) {
3606 tls_show_errors(MSG_INFO, __func__,
3607 "Handshake failed - BIO_write");
3608 return NULL;
3609 }
3610
3611 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003612 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 res = SSL_accept(conn->ssl);
3614 else
3615 res = SSL_connect(conn->ssl);
3616 if (res != 1) {
3617 int err = SSL_get_error(conn->ssl, res);
3618 if (err == SSL_ERROR_WANT_READ)
3619 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
3620 "more data");
3621 else if (err == SSL_ERROR_WANT_WRITE)
3622 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
3623 "write");
3624 else {
3625 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
3626 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003627 if (!conn->server && !conn->client_hello_generated) {
3628 /* The server would not understand TLS Alert
3629 * before ClientHello, so simply terminate
3630 * handshake on this type of error case caused
3631 * by a likely internal error like no ciphers
3632 * available. */
3633 wpa_printf(MSG_DEBUG,
3634 "OpenSSL: Could not generate ClientHello");
3635 conn->write_alerts++;
3636 return NULL;
3637 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 }
3639 }
3640
Roshan Pius3a1667e2018-07-03 15:17:14 -07003641 if (!conn->server && !conn->failed)
3642 conn->client_hello_generated = 1;
3643
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003644#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003645 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003646 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
3647 conn->server_dh_prime_len < 3072) {
3648 struct tls_context *context = conn->context;
3649
3650 /*
3651 * This should not be reached since earlier cert_cb should have
3652 * terminated the handshake. Keep this check here for extra
3653 * protection if anything goes wrong with the more low-level
3654 * checks based on having to parse the TLS handshake messages.
3655 */
3656 wpa_printf(MSG_DEBUG,
3657 "OpenSSL: Server DH prime length: %d bits",
3658 conn->server_dh_prime_len);
3659
3660 if (context->event_cb) {
3661 union tls_event_data ev;
3662
3663 os_memset(&ev, 0, sizeof(ev));
3664 ev.alert.is_local = 1;
3665 ev.alert.type = "fatal";
3666 ev.alert.description = "insufficient security";
3667 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
3668 }
3669 /*
3670 * Could send a TLS Alert to the server, but for now, simply
3671 * terminate handshake.
3672 */
3673 conn->failed++;
3674 conn->write_alerts++;
3675 return NULL;
3676 }
3677#endif /* CONFIG_SUITEB */
3678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 /* Get the TLS handshake data to be sent to the server */
3680 res = BIO_ctrl_pending(conn->ssl_out);
3681 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
3682 out_data = wpabuf_alloc(res);
3683 if (out_data == NULL) {
3684 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
3685 "handshake output (%d bytes)", res);
3686 if (BIO_reset(conn->ssl_out) < 0) {
3687 tls_show_errors(MSG_INFO, __func__,
3688 "BIO_reset failed");
3689 }
3690 return NULL;
3691 }
3692 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
3693 res);
3694 if (res < 0) {
3695 tls_show_errors(MSG_INFO, __func__,
3696 "Handshake failed - BIO_read");
3697 if (BIO_reset(conn->ssl_out) < 0) {
3698 tls_show_errors(MSG_INFO, __func__,
3699 "BIO_reset failed");
3700 }
3701 wpabuf_free(out_data);
3702 return NULL;
3703 }
3704 wpabuf_put(out_data, res);
3705
3706 return out_data;
3707}
3708
3709
3710static struct wpabuf *
3711openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
3712{
3713 struct wpabuf *appl_data;
3714 int res;
3715
3716 appl_data = wpabuf_alloc(max_len + 100);
3717 if (appl_data == NULL)
3718 return NULL;
3719
3720 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
3721 wpabuf_size(appl_data));
3722 if (res < 0) {
3723 int err = SSL_get_error(conn->ssl, res);
3724 if (err == SSL_ERROR_WANT_READ ||
3725 err == SSL_ERROR_WANT_WRITE) {
3726 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
3727 "included");
3728 } else {
3729 tls_show_errors(MSG_INFO, __func__,
3730 "Failed to read possible "
3731 "Application Data");
3732 }
3733 wpabuf_free(appl_data);
3734 return NULL;
3735 }
3736
3737 wpabuf_put(appl_data, res);
3738 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
3739 "message", appl_data);
3740
3741 return appl_data;
3742}
3743
3744
3745static struct wpabuf *
3746openssl_connection_handshake(struct tls_connection *conn,
3747 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003748 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749{
3750 struct wpabuf *out_data;
3751
3752 if (appl_data)
3753 *appl_data = NULL;
3754
Roshan Pius3a1667e2018-07-03 15:17:14 -07003755 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756 if (out_data == NULL)
3757 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03003758 if (conn->invalid_hb_used) {
3759 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3760 wpabuf_free(out_data);
3761 return NULL;
3762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003764 if (SSL_is_init_finished(conn->ssl)) {
3765 wpa_printf(MSG_DEBUG,
3766 "OpenSSL: Handshake finished - resumed=%d",
3767 tls_connection_resumed(conn->ssl_ctx, conn));
3768 if (appl_data && in_data)
3769 *appl_data = openssl_get_appl_data(conn,
3770 wpabuf_len(in_data));
3771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772
Jouni Malinen26af48b2014-04-09 13:02:53 +03003773 if (conn->invalid_hb_used) {
3774 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3775 if (appl_data) {
3776 wpabuf_free(*appl_data);
3777 *appl_data = NULL;
3778 }
3779 wpabuf_free(out_data);
3780 return NULL;
3781 }
3782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 return out_data;
3784}
3785
3786
3787struct wpabuf *
3788tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
3789 const struct wpabuf *in_data,
3790 struct wpabuf **appl_data)
3791{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003792 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793}
3794
3795
3796struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
3797 struct tls_connection *conn,
3798 const struct wpabuf *in_data,
3799 struct wpabuf **appl_data)
3800{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003801 conn->server = 1;
3802 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803}
3804
3805
3806struct wpabuf * tls_connection_encrypt(void *tls_ctx,
3807 struct tls_connection *conn,
3808 const struct wpabuf *in_data)
3809{
3810 int res;
3811 struct wpabuf *buf;
3812
3813 if (conn == NULL)
3814 return NULL;
3815
3816 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3817 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
3818 (res = BIO_reset(conn->ssl_out)) < 0) {
3819 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3820 return NULL;
3821 }
3822 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
3823 if (res < 0) {
3824 tls_show_errors(MSG_INFO, __func__,
3825 "Encryption failed - SSL_write");
3826 return NULL;
3827 }
3828
3829 /* Read encrypted data to be sent to the server */
3830 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
3831 if (buf == NULL)
3832 return NULL;
3833 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
3834 if (res < 0) {
3835 tls_show_errors(MSG_INFO, __func__,
3836 "Encryption failed - BIO_read");
3837 wpabuf_free(buf);
3838 return NULL;
3839 }
3840 wpabuf_put(buf, res);
3841
3842 return buf;
3843}
3844
3845
3846struct wpabuf * tls_connection_decrypt(void *tls_ctx,
3847 struct tls_connection *conn,
3848 const struct wpabuf *in_data)
3849{
3850 int res;
3851 struct wpabuf *buf;
3852
3853 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3854 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
3855 wpabuf_len(in_data));
3856 if (res < 0) {
3857 tls_show_errors(MSG_INFO, __func__,
3858 "Decryption failed - BIO_write");
3859 return NULL;
3860 }
3861 if (BIO_reset(conn->ssl_out) < 0) {
3862 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3863 return NULL;
3864 }
3865
3866 /* Read decrypted data for further processing */
3867 /*
3868 * Even though we try to disable TLS compression, it is possible that
3869 * this cannot be done with all TLS libraries. Add extra buffer space
3870 * to handle the possibility of the decrypted data being longer than
3871 * input data.
3872 */
3873 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
3874 if (buf == NULL)
3875 return NULL;
3876 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
3877 if (res < 0) {
3878 tls_show_errors(MSG_INFO, __func__,
3879 "Decryption failed - SSL_read");
3880 wpabuf_free(buf);
3881 return NULL;
3882 }
3883 wpabuf_put(buf, res);
3884
Jouni Malinen26af48b2014-04-09 13:02:53 +03003885 if (conn->invalid_hb_used) {
3886 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3887 wpabuf_free(buf);
3888 return NULL;
3889 }
3890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891 return buf;
3892}
3893
3894
3895int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
3896{
Hai Shalomce48b4a2018-09-05 11:41:35 -07003897 return conn ? SSL_session_reused(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898}
3899
3900
3901int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
3902 u8 *ciphers)
3903{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003904 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 u8 *c;
3906 int ret;
3907
3908 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
3909 return -1;
3910
3911 buf[0] = '\0';
3912 pos = buf;
3913 end = pos + sizeof(buf);
3914
3915 c = ciphers;
3916 while (*c != TLS_CIPHER_NONE) {
3917 const char *suite;
3918
3919 switch (*c) {
3920 case TLS_CIPHER_RC4_SHA:
3921 suite = "RC4-SHA";
3922 break;
3923 case TLS_CIPHER_AES128_SHA:
3924 suite = "AES128-SHA";
3925 break;
3926 case TLS_CIPHER_RSA_DHE_AES128_SHA:
3927 suite = "DHE-RSA-AES128-SHA";
3928 break;
3929 case TLS_CIPHER_ANON_DH_AES128_SHA:
3930 suite = "ADH-AES128-SHA";
3931 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003932 case TLS_CIPHER_RSA_DHE_AES256_SHA:
3933 suite = "DHE-RSA-AES256-SHA";
3934 break;
3935 case TLS_CIPHER_AES256_SHA:
3936 suite = "AES256-SHA";
3937 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 default:
3939 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
3940 "cipher selection: %d", *c);
3941 return -1;
3942 }
3943 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003944 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003945 break;
3946 pos += ret;
3947
3948 c++;
3949 }
3950
3951 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
3952
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003953#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003954#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3955 if (os_strstr(buf, ":ADH-")) {
3956 /*
3957 * Need to drop to security level 0 to allow anonymous
3958 * cipher suites for EAP-FAST.
3959 */
3960 SSL_set_security_level(conn->ssl, 0);
3961 } else if (SSL_get_security_level(conn->ssl) == 0) {
3962 /* Force at least security level 1 */
3963 SSL_set_security_level(conn->ssl, 1);
3964 }
3965#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3966#endif
3967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
3969 tls_show_errors(MSG_INFO, __func__,
3970 "Cipher suite configuration failed");
3971 return -1;
3972 }
3973
3974 return 0;
3975}
3976
3977
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003978int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
3979 char *buf, size_t buflen)
3980{
3981 const char *name;
3982 if (conn == NULL || conn->ssl == NULL)
3983 return -1;
3984
3985 name = SSL_get_version(conn->ssl);
3986 if (name == NULL)
3987 return -1;
3988
3989 os_strlcpy(buf, name, buflen);
3990 return 0;
3991}
3992
3993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
3995 char *buf, size_t buflen)
3996{
3997 const char *name;
3998 if (conn == NULL || conn->ssl == NULL)
3999 return -1;
4000
4001 name = SSL_get_cipher(conn->ssl);
4002 if (name == NULL)
4003 return -1;
4004
4005 os_strlcpy(buf, name, buflen);
4006 return 0;
4007}
4008
4009
4010int tls_connection_enable_workaround(void *ssl_ctx,
4011 struct tls_connection *conn)
4012{
4013 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4014
4015 return 0;
4016}
4017
4018
4019#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4020/* ClientHello TLS extensions require a patch to openssl, so this function is
4021 * commented out unless explicitly needed for EAP-FAST in order to be able to
4022 * build this file with unmodified openssl. */
4023int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4024 int ext_type, const u8 *data,
4025 size_t data_len)
4026{
4027 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4028 return -1;
4029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4031 data_len) != 1)
4032 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033
4034 return 0;
4035}
4036#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4037
4038
4039int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4040{
4041 if (conn == NULL)
4042 return -1;
4043 return conn->failed;
4044}
4045
4046
4047int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
4048{
4049 if (conn == NULL)
4050 return -1;
4051 return conn->read_alerts;
4052}
4053
4054
4055int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
4056{
4057 if (conn == NULL)
4058 return -1;
4059 return conn->write_alerts;
4060}
4061
4062
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004063#ifdef HAVE_OCSP
4064
4065static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
4066{
4067#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004068 BIO *out;
4069 size_t rlen;
4070 char *txt;
4071 int res;
4072
4073 if (wpa_debug_level > MSG_DEBUG)
4074 return;
4075
4076 out = BIO_new(BIO_s_mem());
4077 if (!out)
4078 return;
4079
4080 OCSP_RESPONSE_print(out, rsp, 0);
4081 rlen = BIO_ctrl_pending(out);
4082 txt = os_malloc(rlen + 1);
4083 if (!txt) {
4084 BIO_free(out);
4085 return;
4086 }
4087
4088 res = BIO_read(out, txt, rlen);
4089 if (res > 0) {
4090 txt[res] = '\0';
4091 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
4092 }
4093 os_free(txt);
4094 BIO_free(out);
4095#endif /* CONFIG_NO_STDOUT_DEBUG */
4096}
4097
4098
Dmitry Shmidt71757432014-06-02 13:50:35 -07004099static void debug_print_cert(X509 *cert, const char *title)
4100{
4101#ifndef CONFIG_NO_STDOUT_DEBUG
4102 BIO *out;
4103 size_t rlen;
4104 char *txt;
4105 int res;
4106
4107 if (wpa_debug_level > MSG_DEBUG)
4108 return;
4109
4110 out = BIO_new(BIO_s_mem());
4111 if (!out)
4112 return;
4113
4114 X509_print(out, cert);
4115 rlen = BIO_ctrl_pending(out);
4116 txt = os_malloc(rlen + 1);
4117 if (!txt) {
4118 BIO_free(out);
4119 return;
4120 }
4121
4122 res = BIO_read(out, txt, rlen);
4123 if (res > 0) {
4124 txt[res] = '\0';
4125 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
4126 }
4127 os_free(txt);
4128
4129 BIO_free(out);
4130#endif /* CONFIG_NO_STDOUT_DEBUG */
4131}
4132
4133
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004134static int ocsp_resp_cb(SSL *s, void *arg)
4135{
4136 struct tls_connection *conn = arg;
4137 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004138 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004139 OCSP_RESPONSE *rsp;
4140 OCSP_BASICRESP *basic;
4141 OCSP_CERTID *id;
4142 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004143 X509_STORE *store;
4144 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004145
4146 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
4147 if (!p) {
4148 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
4149 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
4150 }
4151
4152 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
4153
4154 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
4155 if (!rsp) {
4156 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
4157 return 0;
4158 }
4159
4160 ocsp_debug_print_resp(rsp);
4161
4162 status = OCSP_response_status(rsp);
4163 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
4164 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
4165 status, OCSP_response_status_str(status));
4166 return 0;
4167 }
4168
4169 basic = OCSP_response_get1_basic(rsp);
4170 if (!basic) {
4171 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
4172 return 0;
4173 }
4174
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004175 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004176 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07004177 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004178
4179 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
4180 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004181 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004182 }
4183 certs = sk_X509_new_null();
4184 if (certs) {
4185 X509 *cert;
4186 cert = X509_dup(conn->peer_issuer);
4187 if (cert && !sk_X509_push(certs, cert)) {
4188 tls_show_errors(
4189 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004190 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004191 X509_free(cert);
4192 sk_X509_free(certs);
4193 certs = NULL;
4194 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004195 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004196 cert = X509_dup(conn->peer_issuer_issuer);
4197 if (cert && !sk_X509_push(certs, cert)) {
4198 tls_show_errors(
4199 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004200 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004201 X509_free(cert);
4202 }
4203 }
4204 }
4205 }
4206
4207 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
4208 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004209 if (status <= 0) {
4210 tls_show_errors(MSG_INFO, __func__,
4211 "OpenSSL: OCSP response failed verification");
4212 OCSP_BASICRESP_free(basic);
4213 OCSP_RESPONSE_free(rsp);
4214 return 0;
4215 }
4216
4217 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
4218
Dmitry Shmidt56052862013-10-04 10:23:25 -07004219 if (!conn->peer_cert) {
4220 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
4221 OCSP_BASICRESP_free(basic);
4222 OCSP_RESPONSE_free(rsp);
4223 return 0;
4224 }
4225
4226 if (!conn->peer_issuer) {
4227 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004228 OCSP_BASICRESP_free(basic);
4229 OCSP_RESPONSE_free(rsp);
4230 return 0;
4231 }
4232
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004233 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004234 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004235 wpa_printf(MSG_DEBUG,
4236 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004237 OCSP_BASICRESP_free(basic);
4238 OCSP_RESPONSE_free(rsp);
4239 return 0;
4240 }
4241
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004242 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
4243 &this_update, &next_update);
4244 if (!res) {
4245 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
4246 if (!id) {
4247 wpa_printf(MSG_DEBUG,
4248 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
4249 OCSP_BASICRESP_free(basic);
4250 OCSP_RESPONSE_free(rsp);
4251 return 0;
4252 }
4253
4254 res = OCSP_resp_find_status(basic, id, &status, &reason,
4255 &produced_at, &this_update,
4256 &next_update);
4257 }
4258
4259 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004260 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
4261 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
4262 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004263 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004264 OCSP_BASICRESP_free(basic);
4265 OCSP_RESPONSE_free(rsp);
4266 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
4267 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004268 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004269
4270 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
4271 tls_show_errors(MSG_INFO, __func__,
4272 "OpenSSL: OCSP status times invalid");
4273 OCSP_BASICRESP_free(basic);
4274 OCSP_RESPONSE_free(rsp);
4275 return 0;
4276 }
4277
4278 OCSP_BASICRESP_free(basic);
4279 OCSP_RESPONSE_free(rsp);
4280
4281 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
4282 OCSP_cert_status_str(status));
4283
4284 if (status == V_OCSP_CERTSTATUS_GOOD)
4285 return 1;
4286 if (status == V_OCSP_CERTSTATUS_REVOKED)
4287 return 0;
4288 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
4289 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
4290 return 0;
4291 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07004292 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 -07004293 return 1;
4294}
4295
4296
4297static int ocsp_status_cb(SSL *s, void *arg)
4298{
4299 char *tmp;
4300 char *resp;
4301 size_t len;
4302
4303 if (tls_global->ocsp_stapling_response == NULL) {
4304 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
4305 return SSL_TLSEXT_ERR_OK;
4306 }
4307
4308 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
4309 if (resp == NULL) {
4310 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
4311 /* TODO: Build OCSPResponse with responseStatus = internalError
4312 */
4313 return SSL_TLSEXT_ERR_OK;
4314 }
4315 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
4316 tmp = OPENSSL_malloc(len);
4317 if (tmp == NULL) {
4318 os_free(resp);
4319 return SSL_TLSEXT_ERR_ALERT_FATAL;
4320 }
4321
4322 os_memcpy(tmp, resp, len);
4323 os_free(resp);
4324 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
4325
4326 return SSL_TLSEXT_ERR_OK;
4327}
4328
4329#endif /* HAVE_OCSP */
4330
4331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
4333 const struct tls_connection_params *params)
4334{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004335 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 int ret;
4337 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004338 int can_pkcs11 = 0;
4339 const char *key_id = params->key_id;
4340 const char *cert_id = params->cert_id;
4341 const char *ca_cert_id = params->ca_cert_id;
4342 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004343 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344
4345 if (conn == NULL)
4346 return -1;
4347
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004348 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
4349 wpa_printf(MSG_INFO,
4350 "OpenSSL: ocsp=3 not supported");
4351 return -1;
4352 }
4353
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004354 /*
4355 * If the engine isn't explicitly configured, and any of the
4356 * cert/key fields are actually PKCS#11 URIs, then automatically
4357 * use the PKCS#11 ENGINE.
4358 */
4359 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
4360 can_pkcs11 = 1;
4361
4362 if (!key_id && params->private_key && can_pkcs11 &&
4363 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
4364 can_pkcs11 = 2;
4365 key_id = params->private_key;
4366 }
4367
4368 if (!cert_id && params->client_cert && can_pkcs11 &&
4369 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
4370 can_pkcs11 = 2;
4371 cert_id = params->client_cert;
4372 }
4373
4374 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
4375 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
4376 can_pkcs11 = 2;
4377 ca_cert_id = params->ca_cert;
4378 }
4379
4380 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
4381 if (can_pkcs11 == 2 && !engine_id)
4382 engine_id = "pkcs11";
4383
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004384#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004385#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004386 if (params->flags & TLS_CONN_EAP_FAST) {
4387 wpa_printf(MSG_DEBUG,
4388 "OpenSSL: Use TLSv1_method() for EAP-FAST");
4389 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
4390 tls_show_errors(MSG_INFO, __func__,
4391 "Failed to set TLSv1_method() for EAP-FAST");
4392 return -1;
4393 }
4394 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004395#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07004396#if OPENSSL_VERSION_NUMBER >= 0x10101000L
4397#ifdef SSL_OP_NO_TLSv1_3
4398 if (params->flags & TLS_CONN_EAP_FAST) {
4399 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
4400 * refuses to start the handshake with the modified ciphersuite
4401 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
4402 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
4403 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
4404 }
4405#endif /* SSL_OP_NO_TLSv1_3 */
4406#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004407#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 while ((err = ERR_get_error())) {
4410 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4411 __func__, ERR_error_string(err, NULL));
4412 }
4413
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004414 if (engine_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004416 ret = tls_engine_init(conn, engine_id, params->pin,
4417 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004418 if (ret)
4419 return ret;
4420 }
4421 if (tls_connection_set_subject_match(conn,
4422 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07004423 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004424 params->suffix_match,
4425 params->domain_match))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004426 return -1;
4427
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004428 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004429 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004431 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 params->ca_cert_blob,
4433 params->ca_cert_blob_len,
4434 params->ca_path))
4435 return -1;
4436
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004437 if (engine_id && cert_id) {
4438 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004439 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
4440 } else if (tls_connection_client_cert(conn, params->client_cert,
4441 params->client_cert_blob,
4442 params->client_cert_blob_len))
4443 return -1;
4444
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004445 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
4447 if (tls_connection_engine_private_key(conn))
4448 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004449 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 params->private_key,
4451 params->private_key_passwd,
4452 params->private_key_blob,
4453 params->private_key_blob_len)) {
4454 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
4455 params->private_key);
4456 return -1;
4457 }
4458
4459 if (tls_connection_dh(conn, params->dh_file)) {
4460 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
4461 params->dh_file);
4462 return -1;
4463 }
4464
Roshan Pius3a1667e2018-07-03 15:17:14 -07004465 ciphers = params->openssl_ciphers;
4466#ifdef CONFIG_SUITEB
4467#ifdef OPENSSL_IS_BORINGSSL
4468 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
4469 /* BoringSSL removed support for SUITEB192, so need to handle
4470 * this with hardcoded ciphersuite and additional checks for
4471 * other parameters. */
4472 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
4473 }
4474#endif /* OPENSSL_IS_BORINGSSL */
4475#endif /* CONFIG_SUITEB */
4476 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004477 wpa_printf(MSG_INFO,
4478 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07004479 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004480 return -1;
4481 }
4482
Roshan Pius3a1667e2018-07-03 15:17:14 -07004483 if (tls_set_conn_flags(conn, params->flags,
4484 params->openssl_ciphers) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004485 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004486
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004487#ifdef OPENSSL_IS_BORINGSSL
4488 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4489 SSL_enable_ocsp_stapling(conn->ssl);
4490 }
4491#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004492#ifdef HAVE_OCSP
4493 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004494 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004495 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
4496 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
4497 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
4498 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004499#else /* HAVE_OCSP */
4500 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
4501 wpa_printf(MSG_INFO,
4502 "OpenSSL: No OCSP support included - reject configuration");
4503 return -1;
4504 }
4505 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4506 wpa_printf(MSG_DEBUG,
4507 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4508 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004509#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004510#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004511
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004512 conn->flags = params->flags;
4513
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004514 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515
4516 return 0;
4517}
4518
4519
4520int tls_global_set_params(void *tls_ctx,
4521 const struct tls_connection_params *params)
4522{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004523 struct tls_data *data = tls_ctx;
4524 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 unsigned long err;
4526
4527 while ((err = ERR_get_error())) {
4528 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4529 __func__, ERR_error_string(err, NULL));
4530 }
4531
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004532 if (tls_global_ca_cert(data, params->ca_cert) ||
4533 tls_global_client_cert(data, params->client_cert) ||
4534 tls_global_private_key(data, params->private_key,
4535 params->private_key_passwd) ||
4536 tls_global_dh(data, params->dh_file)) {
4537 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004538 return -1;
4539 }
4540
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004541 if (params->openssl_ciphers &&
4542 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
4543 wpa_printf(MSG_INFO,
4544 "OpenSSL: Failed to set cipher string '%s'",
4545 params->openssl_ciphers);
4546 return -1;
4547 }
4548
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004549#ifdef SSL_OP_NO_TICKET
4550 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
4551 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
4552 else
4553 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
4554#endif /* SSL_OP_NO_TICKET */
4555
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004556#ifdef HAVE_OCSP
4557 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
4558 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
4559 os_free(tls_global->ocsp_stapling_response);
4560 if (params->ocsp_stapling_response)
4561 tls_global->ocsp_stapling_response =
4562 os_strdup(params->ocsp_stapling_response);
4563 else
4564 tls_global->ocsp_stapling_response = NULL;
4565#endif /* HAVE_OCSP */
4566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567 return 0;
4568}
4569
4570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4572/* Pre-shared secred requires a patch to openssl, so this function is
4573 * commented out unless explicitly needed for EAP-FAST in order to be able to
4574 * build this file with unmodified openssl. */
4575
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004576#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004577static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4578 STACK_OF(SSL_CIPHER) *peer_ciphers,
4579 const SSL_CIPHER **cipher, void *arg)
4580#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4582 STACK_OF(SSL_CIPHER) *peer_ciphers,
4583 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004584#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585{
4586 struct tls_connection *conn = arg;
4587 int ret;
4588
Roshan Pius3a1667e2018-07-03 15:17:14 -07004589#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4590 (defined(LIBRESSL_VERSION_NUMBER) && \
4591 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592 if (conn == NULL || conn->session_ticket_cb == NULL)
4593 return 0;
4594
4595 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4596 conn->session_ticket,
4597 conn->session_ticket_len,
4598 s->s3->client_random,
4599 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004600#else
4601 unsigned char client_random[SSL3_RANDOM_SIZE];
4602 unsigned char server_random[SSL3_RANDOM_SIZE];
4603
4604 if (conn == NULL || conn->session_ticket_cb == NULL)
4605 return 0;
4606
4607 SSL_get_client_random(s, client_random, sizeof(client_random));
4608 SSL_get_server_random(s, server_random, sizeof(server_random));
4609
4610 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4611 conn->session_ticket,
4612 conn->session_ticket_len,
4613 client_random,
4614 server_random, secret);
4615#endif
4616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617 os_free(conn->session_ticket);
4618 conn->session_ticket = NULL;
4619
4620 if (ret <= 0)
4621 return 0;
4622
4623 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
4624 return 1;
4625}
4626
4627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
4629 int len, void *arg)
4630{
4631 struct tls_connection *conn = arg;
4632
4633 if (conn == NULL || conn->session_ticket_cb == NULL)
4634 return 0;
4635
4636 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
4637
4638 os_free(conn->session_ticket);
4639 conn->session_ticket = NULL;
4640
4641 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
4642 "extension", data, len);
4643
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004644 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 if (conn->session_ticket == NULL)
4646 return 0;
4647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 conn->session_ticket_len = len;
4649
4650 return 1;
4651}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4653
4654
4655int tls_connection_set_session_ticket_cb(void *tls_ctx,
4656 struct tls_connection *conn,
4657 tls_session_ticket_cb cb,
4658 void *ctx)
4659{
4660#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4661 conn->session_ticket_cb = cb;
4662 conn->session_ticket_cb_ctx = ctx;
4663
4664 if (cb) {
4665 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
4666 conn) != 1)
4667 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668 SSL_set_session_ticket_ext_cb(conn->ssl,
4669 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670 } else {
4671 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
4672 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 }
4675
4676 return 0;
4677#else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4678 return -1;
4679#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4680}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004681
4682
4683int tls_get_library_version(char *buf, size_t buf_len)
4684{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004685#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004686 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4687 OPENSSL_VERSION_TEXT,
4688 OpenSSL_version(OPENSSL_VERSION));
4689#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004690 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4691 OPENSSL_VERSION_TEXT,
4692 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004693#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004694}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004695
4696
4697void tls_connection_set_success_data(struct tls_connection *conn,
4698 struct wpabuf *data)
4699{
4700 SSL_SESSION *sess;
4701 struct wpabuf *old;
4702
4703 if (tls_ex_idx_session < 0)
4704 goto fail;
4705 sess = SSL_get_session(conn->ssl);
4706 if (!sess)
4707 goto fail;
4708 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4709 if (old) {
4710 wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
4711 old);
4712 wpabuf_free(old);
4713 }
4714 if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
4715 goto fail;
4716
4717 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
4718 conn->success_data = 1;
4719 return;
4720
4721fail:
4722 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
4723 wpabuf_free(data);
4724}
4725
4726
4727void tls_connection_set_success_data_resumed(struct tls_connection *conn)
4728{
4729 wpa_printf(MSG_DEBUG,
4730 "OpenSSL: Success data accepted for resumed session");
4731 conn->success_data = 1;
4732}
4733
4734
4735const struct wpabuf *
4736tls_connection_get_success_data(struct tls_connection *conn)
4737{
4738 SSL_SESSION *sess;
4739
4740 if (tls_ex_idx_session < 0 ||
4741 !(sess = SSL_get_session(conn->ssl)))
4742 return NULL;
4743 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4744}
4745
4746
4747void tls_connection_remove_session(struct tls_connection *conn)
4748{
4749 SSL_SESSION *sess;
4750
4751 sess = SSL_get_session(conn->ssl);
4752 if (!sess)
4753 return;
4754
4755 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
4756 wpa_printf(MSG_DEBUG,
4757 "OpenSSL: Session was not cached");
4758 else
4759 wpa_printf(MSG_DEBUG,
4760 "OpenSSL: Removed cached session to disable session resumption");
4761}