blob: 47ef642355163df3580d06d7e4ce926d34bc05c5 [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");
1041 ERR_load_ENGINE_strings();
1042 ENGINE_load_dynamic();
1043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 if (conf &&
1045 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1046 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1048 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1049 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001050 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 return NULL;
1052 }
1053 }
1054#endif /* OPENSSL_NO_ENGINE */
1055
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001056 if (conf && conf->openssl_ciphers)
1057 ciphers = conf->openssl_ciphers;
1058 else
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001059 ciphers = TLS_DEFAULT_CIPHERS;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001060 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1061 wpa_printf(MSG_ERROR,
1062 "OpenSSL: Failed to set cipher string '%s'",
1063 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001064 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001065 return NULL;
1066 }
1067
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001068 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069}
1070
1071
1072void tls_deinit(void *ssl_ctx)
1073{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001074 struct tls_data *data = ssl_ctx;
1075 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001076 struct tls_context *context = SSL_CTX_get_app_data(ssl);
1077 if (context != tls_global)
1078 os_free(context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001079 if (data->tls_session_lifetime > 0)
1080 SSL_CTX_flush_sessions(ssl, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 SSL_CTX_free(ssl);
1082
1083 tls_openssl_ref_count--;
1084 if (tls_openssl_ref_count == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001085#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1086 (defined(LIBRESSL_VERSION_NUMBER) && \
1087 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088#ifndef OPENSSL_NO_ENGINE
1089 ENGINE_cleanup();
1090#endif /* OPENSSL_NO_ENGINE */
1091 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001092 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 ERR_free_strings();
1094 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001095#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001096 os_free(tls_global->ocsp_stapling_response);
1097 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 os_free(tls_global);
1099 tls_global = NULL;
1100 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001101
1102 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103}
1104
1105
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001106#ifndef OPENSSL_NO_ENGINE
1107
1108/* Cryptoki return values */
1109#define CKR_PIN_INCORRECT 0x000000a0
1110#define CKR_PIN_INVALID 0x000000a1
1111#define CKR_PIN_LEN_RANGE 0x000000a2
1112
1113/* libp11 */
1114#define ERR_LIB_PKCS11 ERR_LIB_USER
1115
1116static int tls_is_pin_error(unsigned int err)
1117{
1118 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1119 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1120 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1121 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1122}
1123
1124#endif /* OPENSSL_NO_ENGINE */
1125
1126
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001127#ifdef ANDROID
1128/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1129EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1130#endif /* ANDROID */
1131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1133 const char *pin, const char *key_id,
1134 const char *cert_id, const char *ca_cert_id)
1135{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001136#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1137#if !defined(OPENSSL_NO_ENGINE)
1138#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1139#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001140 if (!key_id)
1141 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001142 conn->engine = NULL;
1143 conn->private_key = EVP_PKEY_from_keystore(key_id);
1144 if (!conn->private_key) {
1145 wpa_printf(MSG_ERROR,
1146 "ENGINE: cannot load private key with id '%s' [%s]",
1147 key_id,
1148 ERR_error_string(ERR_get_error(), NULL));
1149 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1150 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001151#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153#ifndef OPENSSL_NO_ENGINE
1154 int ret = -1;
1155 if (engine_id == NULL) {
1156 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1157 return -1;
1158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159
1160 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001161#ifdef ANDROID
1162 ENGINE_load_dynamic();
1163#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 conn->engine = ENGINE_by_id(engine_id);
1165 if (!conn->engine) {
1166 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1167 engine_id, ERR_error_string(ERR_get_error(), NULL));
1168 goto err;
1169 }
1170 if (ENGINE_init(conn->engine) != 1) {
1171 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1172 "(engine: %s) [%s]", engine_id,
1173 ERR_error_string(ERR_get_error(), NULL));
1174 goto err;
1175 }
1176 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1177
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001178#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001179 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1181 ERR_error_string(ERR_get_error(), NULL));
1182 goto err;
1183 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001184#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001185 if (key_id) {
1186 /*
1187 * Ensure that the ENGINE does not attempt to use the OpenSSL
1188 * UI system to obtain a PIN, if we didn't provide one.
1189 */
1190 struct {
1191 const void *password;
1192 const char *prompt_info;
1193 } key_cb = { "", NULL };
1194
1195 /* load private key first in-case PIN is required for cert */
1196 conn->private_key = ENGINE_load_private_key(conn->engine,
1197 key_id, NULL,
1198 &key_cb);
1199 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001200 unsigned long err = ERR_get_error();
1201
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001202 wpa_printf(MSG_ERROR,
1203 "ENGINE: cannot load private key with id '%s' [%s]",
1204 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001205 ERR_error_string(err, NULL));
1206 if (tls_is_pin_error(err))
1207 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1208 else
1209 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001210 goto err;
1211 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 }
1213
1214 /* handle a certificate and/or CA certificate */
1215 if (cert_id || ca_cert_id) {
1216 const char *cmd_name = "LOAD_CERT_CTRL";
1217
1218 /* test if the engine supports a LOAD_CERT_CTRL */
1219 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1220 0, (void *)cmd_name, NULL)) {
1221 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1222 " loading certificates");
1223 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1224 goto err;
1225 }
1226 }
1227
1228 return 0;
1229
1230err:
1231 if (conn->engine) {
1232 ENGINE_free(conn->engine);
1233 conn->engine = NULL;
1234 }
1235
1236 if (conn->private_key) {
1237 EVP_PKEY_free(conn->private_key);
1238 conn->private_key = NULL;
1239 }
1240
1241 return ret;
1242#else /* OPENSSL_NO_ENGINE */
1243 return 0;
1244#endif /* OPENSSL_NO_ENGINE */
1245}
1246
1247
1248static void tls_engine_deinit(struct tls_connection *conn)
1249{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001250#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1252 if (conn->private_key) {
1253 EVP_PKEY_free(conn->private_key);
1254 conn->private_key = NULL;
1255 }
1256 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001257#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001259#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 conn->engine = NULL;
1261 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001262#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263}
1264
1265
1266int tls_get_errors(void *ssl_ctx)
1267{
1268 int count = 0;
1269 unsigned long err;
1270
1271 while ((err = ERR_get_error())) {
1272 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1273 ERR_error_string(err, NULL));
1274 count++;
1275 }
1276
1277 return count;
1278}
1279
Jouni Malinen26af48b2014-04-09 13:02:53 +03001280
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001281static const char * openssl_content_type(int content_type)
1282{
1283 switch (content_type) {
1284 case 20:
1285 return "change cipher spec";
1286 case 21:
1287 return "alert";
1288 case 22:
1289 return "handshake";
1290 case 23:
1291 return "application data";
1292 case 24:
1293 return "heartbeat";
1294 case 256:
1295 return "TLS header info"; /* pseudo content type */
1296 default:
1297 return "?";
1298 }
1299}
1300
1301
1302static const char * openssl_handshake_type(int content_type, const u8 *buf,
1303 size_t len)
1304{
1305 if (content_type != 22 || !buf || len == 0)
1306 return "";
1307 switch (buf[0]) {
1308 case 0:
1309 return "hello request";
1310 case 1:
1311 return "client hello";
1312 case 2:
1313 return "server hello";
1314 case 4:
1315 return "new session ticket";
1316 case 11:
1317 return "certificate";
1318 case 12:
1319 return "server key exchange";
1320 case 13:
1321 return "certificate request";
1322 case 14:
1323 return "server hello done";
1324 case 15:
1325 return "certificate verify";
1326 case 16:
1327 return "client key exchange";
1328 case 20:
1329 return "finished";
1330 case 21:
1331 return "certificate url";
1332 case 22:
1333 return "certificate status";
1334 default:
1335 return "?";
1336 }
1337}
1338
1339
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001340#ifdef CONFIG_SUITEB
1341
1342static void check_server_hello(struct tls_connection *conn,
1343 const u8 *pos, const u8 *end)
1344{
1345 size_t payload_len, id_len;
1346
1347 /*
1348 * Parse ServerHello to get the selected cipher suite since OpenSSL does
1349 * not make it cleanly available during handshake and we need to know
1350 * whether DHE was selected.
1351 */
1352
1353 if (end - pos < 3)
1354 return;
1355 payload_len = WPA_GET_BE24(pos);
1356 pos += 3;
1357
1358 if ((size_t) (end - pos) < payload_len)
1359 return;
1360 end = pos + payload_len;
1361
1362 /* Skip Version and Random */
1363 if (end - pos < 2 + SSL3_RANDOM_SIZE)
1364 return;
1365 pos += 2 + SSL3_RANDOM_SIZE;
1366
1367 /* Skip Session ID */
1368 if (end - pos < 1)
1369 return;
1370 id_len = *pos++;
1371 if ((size_t) (end - pos) < id_len)
1372 return;
1373 pos += id_len;
1374
1375 if (end - pos < 2)
1376 return;
1377 conn->cipher_suite = WPA_GET_BE16(pos);
1378 wpa_printf(MSG_DEBUG, "OpenSSL: Server selected cipher suite 0x%x",
1379 conn->cipher_suite);
1380}
1381
1382
1383static void check_server_key_exchange(SSL *ssl, struct tls_connection *conn,
1384 const u8 *pos, const u8 *end)
1385{
1386 size_t payload_len;
1387 u16 dh_len;
1388 BIGNUM *p;
1389 int bits;
1390
1391 if (!(conn->flags & TLS_CONN_SUITEB))
1392 return;
1393
1394 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
1395 if (conn->cipher_suite != 0x9f)
1396 return;
1397
1398 if (end - pos < 3)
1399 return;
1400 payload_len = WPA_GET_BE24(pos);
1401 pos += 3;
1402
1403 if ((size_t) (end - pos) < payload_len)
1404 return;
1405 end = pos + payload_len;
1406
1407 if (end - pos < 2)
1408 return;
1409 dh_len = WPA_GET_BE16(pos);
1410 pos += 2;
1411
1412 if ((size_t) (end - pos) < dh_len)
1413 return;
1414 p = BN_bin2bn(pos, dh_len, NULL);
1415 if (!p)
1416 return;
1417
1418 bits = BN_num_bits(p);
1419 BN_free(p);
1420
1421 conn->server_dh_prime_len = bits;
1422 wpa_printf(MSG_DEBUG, "OpenSSL: Server DH prime length: %d bits",
1423 conn->server_dh_prime_len);
1424}
1425
1426#endif /* CONFIG_SUITEB */
1427
1428
Jouni Malinen26af48b2014-04-09 13:02:53 +03001429static void tls_msg_cb(int write_p, int version, int content_type,
1430 const void *buf, size_t len, SSL *ssl, void *arg)
1431{
1432 struct tls_connection *conn = arg;
1433 const u8 *pos = buf;
1434
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001435 if (write_p == 2) {
1436 wpa_printf(MSG_DEBUG,
1437 "OpenSSL: session ver=0x%x content_type=%d",
1438 version, content_type);
1439 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1440 return;
1441 }
1442
1443 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1444 write_p ? "TX" : "RX", version, content_type,
1445 openssl_content_type(content_type),
1446 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001447 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1448 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1449 size_t payload_len = WPA_GET_BE16(pos + 1);
1450 if (payload_len + 3 > len) {
1451 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1452 conn->invalid_hb_used = 1;
1453 }
1454 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001455
1456#ifdef CONFIG_SUITEB
1457 /*
1458 * Need to parse these handshake messages to be able to check DH prime
1459 * length since OpenSSL does not expose the new cipher suite and DH
1460 * parameters during handshake (e.g., for cert_cb() callback).
1461 */
1462 if (content_type == 22 && pos && len > 0 && pos[0] == 2)
1463 check_server_hello(conn, pos + 1, pos + len);
1464 if (content_type == 22 && pos && len > 0 && pos[0] == 12)
1465 check_server_key_exchange(ssl, conn, pos + 1, pos + len);
1466#endif /* CONFIG_SUITEB */
Jouni Malinen26af48b2014-04-09 13:02:53 +03001467}
1468
1469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470struct tls_connection * tls_connection_init(void *ssl_ctx)
1471{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001472 struct tls_data *data = ssl_ctx;
1473 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 struct tls_connection *conn;
1475 long options;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001476 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477
1478 conn = os_zalloc(sizeof(*conn));
1479 if (conn == NULL)
1480 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001481 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 conn->ssl = SSL_new(ssl);
1483 if (conn->ssl == NULL) {
1484 tls_show_errors(MSG_INFO, __func__,
1485 "Failed to initialize new SSL connection");
1486 os_free(conn);
1487 return NULL;
1488 }
1489
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001490 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001492 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1493 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1495 SSL_OP_SINGLE_DH_USE;
1496#ifdef SSL_OP_NO_COMPRESSION
1497 options |= SSL_OP_NO_COMPRESSION;
1498#endif /* SSL_OP_NO_COMPRESSION */
1499 SSL_set_options(conn->ssl, options);
1500
1501 conn->ssl_in = BIO_new(BIO_s_mem());
1502 if (!conn->ssl_in) {
1503 tls_show_errors(MSG_INFO, __func__,
1504 "Failed to create a new BIO for ssl_in");
1505 SSL_free(conn->ssl);
1506 os_free(conn);
1507 return NULL;
1508 }
1509
1510 conn->ssl_out = BIO_new(BIO_s_mem());
1511 if (!conn->ssl_out) {
1512 tls_show_errors(MSG_INFO, __func__,
1513 "Failed to create a new BIO for ssl_out");
1514 SSL_free(conn->ssl);
1515 BIO_free(conn->ssl_in);
1516 os_free(conn);
1517 return NULL;
1518 }
1519
1520 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1521
1522 return conn;
1523}
1524
1525
1526void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1527{
1528 if (conn == NULL)
1529 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001530 if (conn->success_data) {
1531 /*
1532 * Make sure ssl_clear_bad_session() does not remove this
1533 * session.
1534 */
1535 SSL_set_quiet_shutdown(conn->ssl, 1);
1536 SSL_shutdown(conn->ssl);
1537 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 SSL_free(conn->ssl);
1539 tls_engine_deinit(conn);
1540 os_free(conn->subject_match);
1541 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001542 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001543 os_free(conn->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 os_free(conn->session_ticket);
1545 os_free(conn);
1546}
1547
1548
1549int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1550{
1551 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1552}
1553
1554
1555int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1556{
1557 if (conn == NULL)
1558 return -1;
1559
1560 /* Shutdown previous TLS connection without notifying the peer
1561 * because the connection was already terminated in practice
1562 * and "close notify" shutdown alert would confuse AS. */
1563 SSL_set_quiet_shutdown(conn->ssl, 1);
1564 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001565 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566}
1567
1568
1569static int tls_match_altsubject_component(X509 *cert, int type,
1570 const char *value, size_t len)
1571{
1572 GENERAL_NAME *gen;
1573 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001574 int found = 0;
1575 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576
1577 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1578
1579 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1580 gen = sk_GENERAL_NAME_value(ext, i);
1581 if (gen->type != type)
1582 continue;
1583 if (os_strlen((char *) gen->d.ia5->data) == len &&
1584 os_memcmp(value, gen->d.ia5->data, len) == 0)
1585 found++;
1586 }
1587
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001588 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 return found;
1591}
1592
1593
1594static int tls_match_altsubject(X509 *cert, const char *match)
1595{
1596 int type;
1597 const char *pos, *end;
1598 size_t len;
1599
1600 pos = match;
1601 do {
1602 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1603 type = GEN_EMAIL;
1604 pos += 6;
1605 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1606 type = GEN_DNS;
1607 pos += 4;
1608 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1609 type = GEN_URI;
1610 pos += 4;
1611 } else {
1612 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1613 "match '%s'", pos);
1614 return 0;
1615 }
1616 end = os_strchr(pos, ';');
1617 while (end) {
1618 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1619 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1620 os_strncmp(end + 1, "URI:", 4) == 0)
1621 break;
1622 end = os_strchr(end + 1, ';');
1623 }
1624 if (end)
1625 len = end - pos;
1626 else
1627 len = os_strlen(pos);
1628 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1629 return 1;
1630 pos = end + 1;
1631 } while (end);
1632
1633 return 0;
1634}
1635
1636
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001637#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001638static int domain_suffix_match(const u8 *val, size_t len, const char *match,
1639 int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001640{
1641 size_t i, match_len;
1642
1643 /* Check for embedded nuls that could mess up suffix matching */
1644 for (i = 0; i < len; i++) {
1645 if (val[i] == '\0') {
1646 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1647 return 0;
1648 }
1649 }
1650
1651 match_len = os_strlen(match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001652 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001653 return 0;
1654
1655 if (os_strncasecmp((const char *) val + len - match_len, match,
1656 match_len) != 0)
1657 return 0; /* no match */
1658
1659 if (match_len == len)
1660 return 1; /* exact match */
1661
1662 if (val[len - match_len - 1] == '.')
1663 return 1; /* full label match completes suffix match */
1664
1665 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1666 return 0;
1667}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001668#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001669
1670
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001671static int tls_match_suffix(X509 *cert, const char *match, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001672{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001673#ifdef CONFIG_NATIVE_WINDOWS
1674 /* wincrypt.h has conflicting X509_NAME definition */
1675 return -1;
1676#else /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001677 GENERAL_NAME *gen;
1678 void *ext;
1679 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001680 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001681 int dns_name = 0;
1682 X509_NAME *name;
1683
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001684 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
1685 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001686
1687 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1688
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001689 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
1690 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001691 if (gen->type != GEN_DNS)
1692 continue;
1693 dns_name++;
1694 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
1695 gen->d.dNSName->data,
1696 gen->d.dNSName->length);
1697 if (domain_suffix_match(gen->d.dNSName->data,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001698 gen->d.dNSName->length, match, full) ==
1699 1) {
1700 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
1701 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001702 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001703 return 1;
1704 }
1705 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001706 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001707
1708 if (dns_name) {
1709 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
1710 return 0;
1711 }
1712
1713 name = X509_get_subject_name(cert);
1714 i = -1;
1715 for (;;) {
1716 X509_NAME_ENTRY *e;
1717 ASN1_STRING *cn;
1718
1719 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
1720 if (i == -1)
1721 break;
1722 e = X509_NAME_get_entry(name, i);
1723 if (e == NULL)
1724 continue;
1725 cn = X509_NAME_ENTRY_get_data(e);
1726 if (cn == NULL)
1727 continue;
1728 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
1729 cn->data, cn->length);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001730 if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
1731 {
1732 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
1733 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001734 return 1;
1735 }
1736 }
1737
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001738 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
1739 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001740 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001741#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001742}
1743
1744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745static enum tls_fail_reason openssl_tls_fail_reason(int err)
1746{
1747 switch (err) {
1748 case X509_V_ERR_CERT_REVOKED:
1749 return TLS_FAIL_REVOKED;
1750 case X509_V_ERR_CERT_NOT_YET_VALID:
1751 case X509_V_ERR_CRL_NOT_YET_VALID:
1752 return TLS_FAIL_NOT_YET_VALID;
1753 case X509_V_ERR_CERT_HAS_EXPIRED:
1754 case X509_V_ERR_CRL_HAS_EXPIRED:
1755 return TLS_FAIL_EXPIRED;
1756 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1757 case X509_V_ERR_UNABLE_TO_GET_CRL:
1758 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1759 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1760 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1761 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1762 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1763 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1764 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1765 case X509_V_ERR_INVALID_CA:
1766 return TLS_FAIL_UNTRUSTED;
1767 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1768 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1769 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1770 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1771 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1772 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1773 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1774 case X509_V_ERR_CERT_UNTRUSTED:
1775 case X509_V_ERR_CERT_REJECTED:
1776 return TLS_FAIL_BAD_CERTIFICATE;
1777 default:
1778 return TLS_FAIL_UNSPECIFIED;
1779 }
1780}
1781
1782
1783static struct wpabuf * get_x509_cert(X509 *cert)
1784{
1785 struct wpabuf *buf;
1786 u8 *tmp;
1787
1788 int cert_len = i2d_X509(cert, NULL);
1789 if (cert_len <= 0)
1790 return NULL;
1791
1792 buf = wpabuf_alloc(cert_len);
1793 if (buf == NULL)
1794 return NULL;
1795
1796 tmp = wpabuf_put(buf, cert_len);
1797 i2d_X509(cert, &tmp);
1798 return buf;
1799}
1800
1801
1802static void openssl_tls_fail_event(struct tls_connection *conn,
1803 X509 *err_cert, int err, int depth,
1804 const char *subject, const char *err_str,
1805 enum tls_fail_reason reason)
1806{
1807 union tls_event_data ev;
1808 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001809 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810
Pavel Grafov4d8552e2018-02-06 11:28:29 +00001811#ifdef ANDROID
1812 log_cert_validation_failure(err_str);
1813#endif
1814
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001815 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 return;
1817
1818 cert = get_x509_cert(err_cert);
1819 os_memset(&ev, 0, sizeof(ev));
1820 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
1821 reason : openssl_tls_fail_reason(err);
1822 ev.cert_fail.depth = depth;
1823 ev.cert_fail.subject = subject;
1824 ev.cert_fail.reason_txt = err_str;
1825 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001826 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 wpabuf_free(cert);
1828}
1829
1830
1831static void openssl_tls_cert_event(struct tls_connection *conn,
1832 X509 *err_cert, int depth,
1833 const char *subject)
1834{
1835 struct wpabuf *cert = NULL;
1836 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001837 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001838 char *altsubject[TLS_MAX_ALT_SUBJECT];
1839 int alt, num_altsubject = 0;
1840 GENERAL_NAME *gen;
1841 void *ext;
1842 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843#ifdef CONFIG_SHA256
1844 u8 hash[32];
1845#endif /* CONFIG_SHA256 */
1846
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001847 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 return;
1849
1850 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001851 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
1852 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 cert = get_x509_cert(err_cert);
1854 ev.peer_cert.cert = cert;
1855 }
1856#ifdef CONFIG_SHA256
1857 if (cert) {
1858 const u8 *addr[1];
1859 size_t len[1];
1860 addr[0] = wpabuf_head(cert);
1861 len[0] = wpabuf_len(cert);
1862 if (sha256_vector(1, addr, len, hash) == 0) {
1863 ev.peer_cert.hash = hash;
1864 ev.peer_cert.hash_len = sizeof(hash);
1865 }
1866 }
1867#endif /* CONFIG_SHA256 */
1868 ev.peer_cert.depth = depth;
1869 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001870
1871 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
1872 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1873 char *pos;
1874
1875 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
1876 break;
1877 gen = sk_GENERAL_NAME_value(ext, i);
1878 if (gen->type != GEN_EMAIL &&
1879 gen->type != GEN_DNS &&
1880 gen->type != GEN_URI)
1881 continue;
1882
1883 pos = os_malloc(10 + gen->d.ia5->length + 1);
1884 if (pos == NULL)
1885 break;
1886 altsubject[num_altsubject++] = pos;
1887
1888 switch (gen->type) {
1889 case GEN_EMAIL:
1890 os_memcpy(pos, "EMAIL:", 6);
1891 pos += 6;
1892 break;
1893 case GEN_DNS:
1894 os_memcpy(pos, "DNS:", 4);
1895 pos += 4;
1896 break;
1897 case GEN_URI:
1898 os_memcpy(pos, "URI:", 4);
1899 pos += 4;
1900 break;
1901 }
1902
1903 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
1904 pos += gen->d.ia5->length;
1905 *pos = '\0';
1906 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001907 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001908
1909 for (alt = 0; alt < num_altsubject; alt++)
1910 ev.peer_cert.altsubject[alt] = altsubject[alt];
1911 ev.peer_cert.num_altsubject = num_altsubject;
1912
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001913 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001915 for (alt = 0; alt < num_altsubject; alt++)
1916 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917}
1918
1919
1920static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1921{
1922 char buf[256];
1923 X509 *err_cert;
1924 int err, depth;
1925 SSL *ssl;
1926 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001927 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001928 char *match, *altmatch, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929 const char *err_str;
1930
1931 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001932 if (!err_cert)
1933 return 0;
1934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935 err = X509_STORE_CTX_get_error(x509_ctx);
1936 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1937 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1938 SSL_get_ex_data_X509_STORE_CTX_idx());
1939 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1940
1941 conn = SSL_get_app_data(ssl);
1942 if (conn == NULL)
1943 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001944
1945 if (depth == 0)
1946 conn->peer_cert = err_cert;
1947 else if (depth == 1)
1948 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001949 else if (depth == 2)
1950 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001951
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001952 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 match = conn->subject_match;
1954 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001955 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001956 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957
1958 if (!preverify_ok && !conn->ca_cert_verify)
1959 preverify_ok = 1;
1960 if (!preverify_ok && depth > 0 && conn->server_cert_only)
1961 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001962 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
1963 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
1964 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
1965 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
1966 "time mismatch");
1967 preverify_ok = 1;
1968 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969
1970 err_str = X509_verify_cert_error_string(err);
1971
1972#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001973 /*
1974 * Do not require preverify_ok so we can explicity allow otherwise
1975 * invalid pinned server certificates.
1976 */
1977 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 struct wpabuf *cert;
1979 cert = get_x509_cert(err_cert);
1980 if (!cert) {
1981 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
1982 "server certificate data");
1983 preverify_ok = 0;
1984 } else {
1985 u8 hash[32];
1986 const u8 *addr[1];
1987 size_t len[1];
1988 addr[0] = wpabuf_head(cert);
1989 len[0] = wpabuf_len(cert);
1990 if (sha256_vector(1, addr, len, hash) < 0 ||
1991 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
1992 err_str = "Server certificate mismatch";
1993 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
1994 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001995 } else if (!preverify_ok) {
1996 /*
1997 * Certificate matches pinned certificate, allow
1998 * regardless of other problems.
1999 */
2000 wpa_printf(MSG_DEBUG,
2001 "OpenSSL: Ignore validation issues for a pinned server certificate");
2002 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 }
2004 wpabuf_free(cert);
2005 }
2006 }
2007#endif /* CONFIG_SHA256 */
2008
2009 if (!preverify_ok) {
2010 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
2011 " error %d (%s) depth %d for '%s'", err, err_str,
2012 depth, buf);
2013 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2014 err_str, TLS_FAIL_UNSPECIFIED);
2015 return preverify_ok;
2016 }
2017
2018 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
2019 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
2020 preverify_ok, err, err_str,
2021 conn->ca_cert_verify, depth, buf);
2022 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
2023 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
2024 "match with '%s'", buf, match);
2025 preverify_ok = 0;
2026 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2027 "Subject mismatch",
2028 TLS_FAIL_SUBJECT_MISMATCH);
2029 } else if (depth == 0 && altmatch &&
2030 !tls_match_altsubject(err_cert, altmatch)) {
2031 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
2032 "'%s' not found", altmatch);
2033 preverify_ok = 0;
2034 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2035 "AltSubject mismatch",
2036 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002037 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002038 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07002039 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
2040 suffix_match);
2041 preverify_ok = 0;
2042 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2043 "Domain suffix mismatch",
2044 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002045 } else if (depth == 0 && domain_match &&
2046 !tls_match_suffix(err_cert, domain_match, 1)) {
2047 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
2048 domain_match);
2049 preverify_ok = 0;
2050 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2051 "Domain mismatch",
2052 TLS_FAIL_DOMAIN_MISMATCH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 } else
2054 openssl_tls_cert_event(conn, err_cert, depth, buf);
2055
2056 if (conn->cert_probe && preverify_ok && depth == 0) {
2057 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
2058 "on probe-only run");
2059 preverify_ok = 0;
2060 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2061 "Server certificate chain probe",
2062 TLS_FAIL_SERVER_CHAIN_PROBE);
2063 }
2064
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002065#ifdef CONFIG_SUITEB
2066 if (conn->flags & TLS_CONN_SUITEB) {
2067 EVP_PKEY *pk;
2068 RSA *rsa;
2069 int len = -1;
2070
2071 pk = X509_get_pubkey(err_cert);
2072 if (pk) {
2073 rsa = EVP_PKEY_get1_RSA(pk);
2074 if (rsa) {
2075 len = RSA_bits(rsa);
2076 RSA_free(rsa);
2077 }
2078 EVP_PKEY_free(pk);
2079 }
2080
2081 if (len >= 0) {
2082 wpa_printf(MSG_DEBUG,
2083 "OpenSSL: RSA modulus size: %d bits", len);
2084 if (len < 3072) {
2085 preverify_ok = 0;
2086 openssl_tls_fail_event(
2087 conn, err_cert, err,
2088 depth, buf,
2089 "Insufficient RSA modulus size",
2090 TLS_FAIL_INSUFFICIENT_KEY_LEN);
2091 }
2092 }
2093 }
2094#endif /* CONFIG_SUITEB */
2095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002096#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002097 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
2098 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002099 enum ocsp_result res;
2100
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002101 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
2102 conn->peer_issuer,
2103 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002104 if (res == OCSP_REVOKED) {
2105 preverify_ok = 0;
2106 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2107 "certificate revoked",
2108 TLS_FAIL_REVOKED);
2109 if (err == X509_V_OK)
2110 X509_STORE_CTX_set_error(
2111 x509_ctx, X509_V_ERR_CERT_REVOKED);
2112 } else if (res != OCSP_GOOD &&
2113 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
2114 preverify_ok = 0;
2115 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
2116 "bad certificate status response",
2117 TLS_FAIL_UNSPECIFIED);
2118 }
2119 }
2120#endif /* OPENSSL_IS_BORINGSSL */
2121
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002122 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002123 context->event_cb(context->cb_ctx,
2124 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 return preverify_ok;
2127}
2128
2129
2130#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002131static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002133 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 X509_LOOKUP *lookup;
2135 int ret = 0;
2136
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002137 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 X509_LOOKUP_file());
2139 if (lookup == NULL) {
2140 tls_show_errors(MSG_WARNING, __func__,
2141 "Failed add lookup for X509 store");
2142 return -1;
2143 }
2144
2145 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
2146 unsigned long err = ERR_peek_error();
2147 tls_show_errors(MSG_WARNING, __func__,
2148 "Failed load CA in DER format");
2149 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2150 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2151 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2152 "cert already in hash table error",
2153 __func__);
2154 } else
2155 ret = -1;
2156 }
2157
2158 return ret;
2159}
2160#endif /* OPENSSL_NO_STDIO */
2161
2162
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002163static int tls_connection_ca_cert(struct tls_data *data,
2164 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165 const char *ca_cert, const u8 *ca_cert_blob,
2166 size_t ca_cert_blob_len, const char *ca_path)
2167{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002168 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002169 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170
2171 /*
2172 * Remove previously configured trusted CA certificates before adding
2173 * new ones.
2174 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002175 store = X509_STORE_new();
2176 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2178 "certificate store", __func__);
2179 return -1;
2180 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002181 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182
2183 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2184 conn->ca_cert_verify = 1;
2185
2186 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2187 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2188 "chain");
2189 conn->cert_probe = 1;
2190 conn->ca_cert_verify = 0;
2191 return 0;
2192 }
2193
2194 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2195#ifdef CONFIG_SHA256
2196 const char *pos = ca_cert + 7;
2197 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2198 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2199 "hash value '%s'", ca_cert);
2200 return -1;
2201 }
2202 pos += 14;
2203 if (os_strlen(pos) != 32 * 2) {
2204 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2205 "hash length in ca_cert '%s'", ca_cert);
2206 return -1;
2207 }
2208 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2209 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2210 "value in ca_cert '%s'", ca_cert);
2211 return -1;
2212 }
2213 conn->server_cert_only = 1;
2214 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2215 "certificate match");
2216 return 0;
2217#else /* CONFIG_SHA256 */
2218 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2219 "cannot validate server certificate hash");
2220 return -1;
2221#endif /* CONFIG_SHA256 */
2222 }
2223
2224 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002225 X509 *cert = d2i_X509(NULL,
2226 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 ca_cert_blob_len);
2228 if (cert == NULL) {
2229 tls_show_errors(MSG_WARNING, __func__,
2230 "Failed to parse ca_cert_blob");
2231 return -1;
2232 }
2233
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002234 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2235 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 unsigned long err = ERR_peek_error();
2237 tls_show_errors(MSG_WARNING, __func__,
2238 "Failed to add ca_cert_blob to "
2239 "certificate store");
2240 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2241 ERR_GET_REASON(err) ==
2242 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2243 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2244 "cert already in hash table error",
2245 __func__);
2246 } else {
2247 X509_free(cert);
2248 return -1;
2249 }
2250 }
2251 X509_free(cert);
2252 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2253 "to certificate store", __func__);
2254 return 0;
2255 }
2256
2257#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002258 /* Single alias */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002260 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002261 &ca_cert[11]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002263 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2264 return 0;
2265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002267 /* Multiple aliases separated by space */
2268 if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) {
2269 char *aliases = os_strdup(&ca_cert[12]);
2270 const char *delim = " ";
2271 int rc = 0;
2272 char *savedptr;
2273 char *alias;
2274
2275 if (!aliases)
2276 return -1;
2277 alias = strtok_r(aliases, delim, &savedptr);
2278 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2279 if (tls_add_ca_from_keystore_encoded(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002280 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002281 wpa_printf(MSG_WARNING,
2282 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2283 __func__, alias);
2284 rc = -1;
2285 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 }
2287 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002288 os_free(aliases);
2289 if (rc)
2290 return rc;
2291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2293 return 0;
2294 }
2295#endif /* ANDROID */
2296
2297#ifdef CONFIG_NATIVE_WINDOWS
2298 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2299 0) {
2300 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2301 "system certificate store");
2302 return 0;
2303 }
2304#endif /* CONFIG_NATIVE_WINDOWS */
2305
2306 if (ca_cert || ca_path) {
2307#ifndef OPENSSL_NO_STDIO
2308 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2309 1) {
2310 tls_show_errors(MSG_WARNING, __func__,
2311 "Failed to load root certificates");
2312 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002313 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2315 "DER format CA certificate",
2316 __func__);
2317 } else
2318 return -1;
2319 } else {
2320 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2321 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002322 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 }
2324#else /* OPENSSL_NO_STDIO */
2325 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2326 __func__);
2327 return -1;
2328#endif /* OPENSSL_NO_STDIO */
2329 } else {
2330 /* No ca_cert configured - do not try to verify server
2331 * certificate */
2332 conn->ca_cert_verify = 0;
2333 }
2334
2335 return 0;
2336}
2337
2338
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002339static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002341 SSL_CTX *ssl_ctx = data->ssl;
2342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 if (ca_cert) {
2344 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2345 {
2346 tls_show_errors(MSG_WARNING, __func__,
2347 "Failed to load root certificates");
2348 return -1;
2349 }
2350
2351 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2352 "certificate(s) loaded");
2353
2354#ifndef OPENSSL_NO_STDIO
2355 /* Add the same CAs to the client certificate requests */
2356 SSL_CTX_set_client_CA_list(ssl_ctx,
2357 SSL_load_client_CA_file(ca_cert));
2358#endif /* OPENSSL_NO_STDIO */
2359 }
2360
2361 return 0;
2362}
2363
2364
2365int tls_global_set_verify(void *ssl_ctx, int check_crl)
2366{
2367 int flags;
2368
2369 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002370 struct tls_data *data = ssl_ctx;
2371 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 if (cs == NULL) {
2373 tls_show_errors(MSG_INFO, __func__, "Failed to get "
2374 "certificate store when enabling "
2375 "check_crl");
2376 return -1;
2377 }
2378 flags = X509_V_FLAG_CRL_CHECK;
2379 if (check_crl == 2)
2380 flags |= X509_V_FLAG_CRL_CHECK_ALL;
2381 X509_STORE_set_flags(cs, flags);
2382 }
2383 return 0;
2384}
2385
2386
2387static int tls_connection_set_subject_match(struct tls_connection *conn,
2388 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002389 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002390 const char *suffix_match,
2391 const char *domain_match)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392{
2393 os_free(conn->subject_match);
2394 conn->subject_match = NULL;
2395 if (subject_match) {
2396 conn->subject_match = os_strdup(subject_match);
2397 if (conn->subject_match == NULL)
2398 return -1;
2399 }
2400
2401 os_free(conn->altsubject_match);
2402 conn->altsubject_match = NULL;
2403 if (altsubject_match) {
2404 conn->altsubject_match = os_strdup(altsubject_match);
2405 if (conn->altsubject_match == NULL)
2406 return -1;
2407 }
2408
Dmitry Shmidt051af732013-10-22 13:52:46 -07002409 os_free(conn->suffix_match);
2410 conn->suffix_match = NULL;
2411 if (suffix_match) {
2412 conn->suffix_match = os_strdup(suffix_match);
2413 if (conn->suffix_match == NULL)
2414 return -1;
2415 }
2416
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002417 os_free(conn->domain_match);
2418 conn->domain_match = NULL;
2419 if (domain_match) {
2420 conn->domain_match = os_strdup(domain_match);
2421 if (conn->domain_match == NULL)
2422 return -1;
2423 }
2424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 return 0;
2426}
2427
2428
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002429#ifdef CONFIG_SUITEB
2430#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2431static int suiteb_cert_cb(SSL *ssl, void *arg)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002432{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002433 struct tls_connection *conn = arg;
2434
2435 /*
2436 * This cert_cb() is not really the best location for doing a
2437 * constraint check for the ServerKeyExchange message, but this seems to
2438 * be the only place where the current OpenSSL sequence can be
2439 * terminated cleanly with an TLS alert going out to the server.
2440 */
2441
2442 if (!(conn->flags & TLS_CONN_SUITEB))
2443 return 1;
2444
2445 /* DHE is enabled only with DHE-RSA-AES256-GCM-SHA384 */
2446 if (conn->cipher_suite != 0x9f)
2447 return 1;
2448
2449 if (conn->server_dh_prime_len >= 3072)
2450 return 1;
2451
2452 wpa_printf(MSG_DEBUG,
2453 "OpenSSL: Server DH prime length (%d bits) not sufficient for Suite B RSA - reject handshake",
2454 conn->server_dh_prime_len);
2455 return 0;
2456}
2457#endif /* OPENSSL_VERSION_NUMBER */
2458#endif /* CONFIG_SUITEB */
2459
2460
Roshan Pius3a1667e2018-07-03 15:17:14 -07002461static int tls_set_conn_flags(struct tls_connection *conn, unsigned int flags,
2462 const char *openssl_ciphers)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002463{
2464 SSL *ssl = conn->ssl;
2465
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002466#ifdef SSL_OP_NO_TICKET
2467 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
2468 SSL_set_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002469 else
2470 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002471#endif /* SSL_OP_NO_TICKET */
2472
2473#ifdef SSL_OP_NO_TLSv1
2474 if (flags & TLS_CONN_DISABLE_TLSv1_0)
2475 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2476 else
2477 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
2478#endif /* SSL_OP_NO_TLSv1 */
2479#ifdef SSL_OP_NO_TLSv1_1
2480 if (flags & TLS_CONN_DISABLE_TLSv1_1)
2481 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2482 else
2483 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
2484#endif /* SSL_OP_NO_TLSv1_1 */
2485#ifdef SSL_OP_NO_TLSv1_2
2486 if (flags & TLS_CONN_DISABLE_TLSv1_2)
2487 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
2488 else
2489 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
2490#endif /* SSL_OP_NO_TLSv1_2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002491#ifdef SSL_OP_NO_TLSv1_3
2492 if (flags & TLS_CONN_DISABLE_TLSv1_3)
2493 SSL_set_options(ssl, SSL_OP_NO_TLSv1_3);
2494 else
2495 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_3);
2496#endif /* SSL_OP_NO_TLSv1_3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002497#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07002498#ifdef OPENSSL_IS_BORINGSSL
2499 /* Start with defaults from BoringSSL */
2500 SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, NULL, 0);
2501#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002502#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2503 if (flags & TLS_CONN_SUITEB_NO_ECDH) {
2504 const char *ciphers = "DHE-RSA-AES256-GCM-SHA384";
2505
Roshan Pius3a1667e2018-07-03 15:17:14 -07002506 if (openssl_ciphers) {
2507 wpa_printf(MSG_DEBUG,
2508 "OpenSSL: Override ciphers for Suite B (no ECDH): %s",
2509 openssl_ciphers);
2510 ciphers = openssl_ciphers;
2511 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002512 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
2513 wpa_printf(MSG_INFO,
2514 "OpenSSL: Failed to set Suite B ciphers");
2515 return -1;
2516 }
2517 } else if (flags & TLS_CONN_SUITEB) {
2518 EC_KEY *ecdh;
2519 const char *ciphers =
2520 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
Roshan Pius3a1667e2018-07-03 15:17:14 -07002521 int nid[1] = { NID_secp384r1 };
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002522
Roshan Pius3a1667e2018-07-03 15:17:14 -07002523 if (openssl_ciphers) {
2524 wpa_printf(MSG_DEBUG,
2525 "OpenSSL: Override ciphers for Suite B: %s",
2526 openssl_ciphers);
2527 ciphers = openssl_ciphers;
2528 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002529 if (SSL_set_cipher_list(ssl, ciphers) != 1) {
2530 wpa_printf(MSG_INFO,
2531 "OpenSSL: Failed to set Suite B ciphers");
2532 return -1;
2533 }
2534
Roshan Pius3a1667e2018-07-03 15:17:14 -07002535 if (SSL_set1_curves(ssl, nid, 1) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002536 wpa_printf(MSG_INFO,
2537 "OpenSSL: Failed to set Suite B curves");
2538 return -1;
2539 }
2540
2541 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
2542 if (!ecdh || SSL_set_tmp_ecdh(ssl, ecdh) != 1) {
2543 EC_KEY_free(ecdh);
2544 wpa_printf(MSG_INFO,
2545 "OpenSSL: Failed to set ECDH parameter");
2546 return -1;
2547 }
2548 EC_KEY_free(ecdh);
2549 }
2550 if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002551#ifdef OPENSSL_IS_BORINGSSL
2552 uint16_t sigalgs[1] = { SSL_SIGN_RSA_PKCS1_SHA384 };
2553
2554 if (SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, sigalgs,
2555 1) != 1) {
2556 wpa_printf(MSG_INFO,
2557 "OpenSSL: Failed to set Suite B sigalgs");
2558 return -1;
2559 }
2560#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002561 /* ECDSA+SHA384 if need to add EC support here */
2562 if (SSL_set1_sigalgs_list(ssl, "RSA+SHA384") != 1) {
2563 wpa_printf(MSG_INFO,
2564 "OpenSSL: Failed to set Suite B sigalgs");
2565 return -1;
2566 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002567#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002568
2569 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2570 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2571 SSL_set_cert_cb(ssl, suiteb_cert_cb, conn);
2572 }
2573#else /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2574 if (flags & (TLS_CONN_SUITEB | TLS_CONN_SUITEB_NO_ECDH)) {
2575 wpa_printf(MSG_ERROR,
2576 "OpenSSL: Suite B RSA case not supported with this OpenSSL version");
2577 return -1;
2578 }
2579#endif /* OPENSSL_VERSION_NUMBER */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002580
2581#ifdef OPENSSL_IS_BORINGSSL
2582 if (openssl_ciphers && os_strcmp(openssl_ciphers, "SUITEB192") == 0) {
2583 uint16_t sigalgs[1] = { SSL_SIGN_ECDSA_SECP384R1_SHA384 };
2584 int nid[1] = { NID_secp384r1 };
2585
2586 if (SSL_set1_curves(ssl, nid, 1) != 1) {
2587 wpa_printf(MSG_INFO,
2588 "OpenSSL: Failed to set Suite B curves");
2589 return -1;
2590 }
2591
2592 if (SSL_CTX_set_verify_algorithm_prefs(conn->ssl_ctx, sigalgs,
2593 1) != 1) {
2594 wpa_printf(MSG_INFO,
2595 "OpenSSL: Failed to set Suite B sigalgs");
2596 return -1;
2597 }
2598 }
2599#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002600#endif /* CONFIG_SUITEB */
2601
2602 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002603}
2604
2605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002607 int verify_peer, unsigned int flags,
2608 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609{
2610 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002611 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612
2613 if (conn == NULL)
2614 return -1;
2615
2616 if (verify_peer) {
2617 conn->ca_cert_verify = 1;
2618 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
2619 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
2620 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
2621 } else {
2622 conn->ca_cert_verify = 0;
2623 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
2624 }
2625
Roshan Pius3a1667e2018-07-03 15:17:14 -07002626 if (tls_set_conn_flags(conn, flags, NULL) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002627 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002628 conn->flags = flags;
2629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 SSL_set_accept_state(conn->ssl);
2631
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002632 if (data->tls_session_lifetime == 0) {
2633 /*
2634 * Set session id context to a unique value to make sure
2635 * session resumption cannot be used either through session
2636 * caching or TLS ticket extension.
2637 */
2638 counter++;
2639 SSL_set_session_id_context(conn->ssl,
2640 (const unsigned char *) &counter,
2641 sizeof(counter));
2642 } else if (session_ctx) {
2643 SSL_set_session_id_context(conn->ssl, session_ctx,
2644 session_ctx_len);
2645 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646
2647 return 0;
2648}
2649
2650
2651static int tls_connection_client_cert(struct tls_connection *conn,
2652 const char *client_cert,
2653 const u8 *client_cert_blob,
2654 size_t client_cert_blob_len)
2655{
2656 if (client_cert == NULL && client_cert_blob == NULL)
2657 return 0;
2658
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002659#ifdef PKCS12_FUNCS
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002660#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002661 /*
2662 * Clear previously set extra chain certificates, if any, from PKCS#12
2663 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2664 * chain properly.
2665 */
2666 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
2667#endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2668#endif /* PKCS12_FUNCS */
2669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670 if (client_cert_blob &&
2671 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
2672 client_cert_blob_len) == 1) {
2673 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
2674 "OK");
2675 return 0;
2676 } else if (client_cert_blob) {
2677 tls_show_errors(MSG_DEBUG, __func__,
2678 "SSL_use_certificate_ASN1 failed");
2679 }
2680
2681 if (client_cert == NULL)
2682 return -1;
2683
2684#ifdef ANDROID
2685 if (os_strncmp("keystore://", client_cert, 11) == 0) {
2686 BIO *bio = BIO_from_keystore(&client_cert[11]);
2687 X509 *x509 = NULL;
2688 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002689 if (bio) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002691 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 if (x509) {
2693 if (SSL_use_certificate(conn->ssl, x509) == 1)
2694 ret = 0;
2695 X509_free(x509);
2696 }
Paul Stewart50772e82017-01-25 13:59:16 -08002697
2698 /* Read additional certificates into the chain. */
2699 while (bio) {
2700 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2701 if (x509) {
2702 /* Takes ownership of x509 */
2703 SSL_add0_chain_cert(conn->ssl, x509);
2704 } else {
2705 BIO_free(bio);
2706 bio = NULL;
2707 }
2708 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002709 return ret;
2710 }
2711#endif /* ANDROID */
2712
2713#ifndef OPENSSL_NO_STDIO
2714 if (SSL_use_certificate_file(conn->ssl, client_cert,
2715 SSL_FILETYPE_ASN1) == 1) {
2716 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
2717 " --> OK");
2718 return 0;
2719 }
2720
2721 if (SSL_use_certificate_file(conn->ssl, client_cert,
2722 SSL_FILETYPE_PEM) == 1) {
2723 ERR_clear_error();
2724 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
2725 " --> OK");
2726 return 0;
2727 }
2728
2729 tls_show_errors(MSG_DEBUG, __func__,
2730 "SSL_use_certificate_file failed");
2731#else /* OPENSSL_NO_STDIO */
2732 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2733#endif /* OPENSSL_NO_STDIO */
2734
2735 return -1;
2736}
2737
2738
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002739static int tls_global_client_cert(struct tls_data *data,
2740 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741{
2742#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002743 SSL_CTX *ssl_ctx = data->ssl;
2744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745 if (client_cert == NULL)
2746 return 0;
2747
2748 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2749 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002750 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2752 SSL_FILETYPE_PEM) != 1) {
2753 tls_show_errors(MSG_INFO, __func__,
2754 "Failed to load client certificate");
2755 return -1;
2756 }
2757 return 0;
2758#else /* OPENSSL_NO_STDIO */
2759 if (client_cert == NULL)
2760 return 0;
2761 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2762 return -1;
2763#endif /* OPENSSL_NO_STDIO */
2764}
2765
2766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002768static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 const char *passwd)
2770{
2771 EVP_PKEY *pkey;
2772 X509 *cert;
2773 STACK_OF(X509) *certs;
2774 int res = 0;
2775 char buf[256];
2776
2777 pkey = NULL;
2778 cert = NULL;
2779 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002780 if (!passwd)
2781 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
2783 tls_show_errors(MSG_DEBUG, __func__,
2784 "Failed to parse PKCS12 file");
2785 PKCS12_free(p12);
2786 return -1;
2787 }
2788 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
2789
2790 if (cert) {
2791 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2792 sizeof(buf));
2793 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
2794 "subject='%s'", buf);
2795 if (ssl) {
2796 if (SSL_use_certificate(ssl, cert) != 1)
2797 res = -1;
2798 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002799 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800 res = -1;
2801 }
2802 X509_free(cert);
2803 }
2804
2805 if (pkey) {
2806 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
2807 if (ssl) {
2808 if (SSL_use_PrivateKey(ssl, pkey) != 1)
2809 res = -1;
2810 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002811 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 res = -1;
2813 }
2814 EVP_PKEY_free(pkey);
2815 }
2816
2817 if (certs) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002818#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002819 if (ssl)
2820 SSL_clear_chain_certs(ssl);
2821 else
2822 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002823 while ((cert = sk_X509_pop(certs)) != NULL) {
2824 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2825 sizeof(buf));
2826 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2827 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002828 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
2829 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
2830 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002831 tls_show_errors(MSG_DEBUG, __func__,
2832 "Failed to add additional certificate");
2833 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002834 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002835 break;
2836 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002837 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002838 }
2839 if (!res) {
2840 /* Try to continue anyway */
2841 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002842 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002843#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002844 if (ssl)
2845 res = SSL_build_cert_chain(
2846 ssl,
2847 SSL_BUILD_CHAIN_FLAG_CHECK |
2848 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
2849 else
2850 res = SSL_CTX_build_cert_chain(
2851 data->ssl,
2852 SSL_BUILD_CHAIN_FLAG_CHECK |
2853 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002854 if (!res) {
2855 tls_show_errors(MSG_DEBUG, __func__,
2856 "Failed to build certificate chain");
2857 } else if (res == 2) {
2858 wpa_printf(MSG_DEBUG,
2859 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2860 }
2861#endif /* OPENSSL_IS_BORINGSSL */
2862 /*
2863 * Try to continue regardless of result since it is possible for
2864 * the extra certificates not to be required.
2865 */
2866 res = 0;
2867#else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002868 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 while ((cert = sk_X509_pop(certs)) != NULL) {
2870 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2871 sizeof(buf));
2872 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2873 " from PKCS12: subject='%s'", buf);
2874 /*
2875 * There is no SSL equivalent for the chain cert - so
2876 * always add it to the context...
2877 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002878 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
2879 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002880 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 res = -1;
2882 break;
2883 }
2884 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002885 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002886#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887 }
2888
2889 PKCS12_free(p12);
2890
2891 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002892 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893
2894 return res;
2895}
2896#endif /* PKCS12_FUNCS */
2897
2898
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002899static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
2900 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901{
2902#ifdef PKCS12_FUNCS
2903 FILE *f;
2904 PKCS12 *p12;
2905
2906 f = fopen(private_key, "rb");
2907 if (f == NULL)
2908 return -1;
2909
2910 p12 = d2i_PKCS12_fp(f, NULL);
2911 fclose(f);
2912
2913 if (p12 == NULL) {
2914 tls_show_errors(MSG_INFO, __func__,
2915 "Failed to use PKCS#12 file");
2916 return -1;
2917 }
2918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002919 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920
2921#else /* PKCS12_FUNCS */
2922 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
2923 "p12/pfx files");
2924 return -1;
2925#endif /* PKCS12_FUNCS */
2926}
2927
2928
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002929static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930 const u8 *blob, size_t len, const char *passwd)
2931{
2932#ifdef PKCS12_FUNCS
2933 PKCS12 *p12;
2934
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002935 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 if (p12 == NULL) {
2937 tls_show_errors(MSG_INFO, __func__,
2938 "Failed to use PKCS#12 blob");
2939 return -1;
2940 }
2941
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002942 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943
2944#else /* PKCS12_FUNCS */
2945 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
2946 "p12/pfx blobs");
2947 return -1;
2948#endif /* PKCS12_FUNCS */
2949}
2950
2951
2952#ifndef OPENSSL_NO_ENGINE
2953static int tls_engine_get_cert(struct tls_connection *conn,
2954 const char *cert_id,
2955 X509 **cert)
2956{
2957 /* this runs after the private key is loaded so no PIN is required */
2958 struct {
2959 const char *cert_id;
2960 X509 *cert;
2961 } params;
2962 params.cert_id = cert_id;
2963 params.cert = NULL;
2964
2965 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
2966 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002967 unsigned long err = ERR_get_error();
2968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
2970 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002971 ERR_error_string(err, NULL));
2972 if (tls_is_pin_error(err))
2973 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2975 }
2976 if (!params.cert) {
2977 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
2978 " '%s'", cert_id);
2979 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2980 }
2981 *cert = params.cert;
2982 return 0;
2983}
2984#endif /* OPENSSL_NO_ENGINE */
2985
2986
2987static int tls_connection_engine_client_cert(struct tls_connection *conn,
2988 const char *cert_id)
2989{
2990#ifndef OPENSSL_NO_ENGINE
2991 X509 *cert;
2992
2993 if (tls_engine_get_cert(conn, cert_id, &cert))
2994 return -1;
2995
2996 if (!SSL_use_certificate(conn->ssl, cert)) {
2997 tls_show_errors(MSG_ERROR, __func__,
2998 "SSL_use_certificate failed");
2999 X509_free(cert);
3000 return -1;
3001 }
3002 X509_free(cert);
3003 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
3004 "OK");
3005 return 0;
3006
3007#else /* OPENSSL_NO_ENGINE */
3008 return -1;
3009#endif /* OPENSSL_NO_ENGINE */
3010}
3011
3012
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003013static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014 struct tls_connection *conn,
3015 const char *ca_cert_id)
3016{
3017#ifndef OPENSSL_NO_ENGINE
3018 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003019 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003020 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021
3022 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
3023 return -1;
3024
3025 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003026 store = X509_STORE_new();
3027 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
3029 "certificate store", __func__);
3030 X509_free(cert);
3031 return -1;
3032 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003033 SSL_CTX_set_cert_store(ssl_ctx, store);
3034 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 unsigned long err = ERR_peek_error();
3036 tls_show_errors(MSG_WARNING, __func__,
3037 "Failed to add CA certificate from engine "
3038 "to certificate store");
3039 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
3040 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
3041 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
3042 " already in hash table error",
3043 __func__);
3044 } else {
3045 X509_free(cert);
3046 return -1;
3047 }
3048 }
3049 X509_free(cert);
3050 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
3051 "to certificate store", __func__);
3052 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003053 conn->ca_cert_verify = 1;
3054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055 return 0;
3056
3057#else /* OPENSSL_NO_ENGINE */
3058 return -1;
3059#endif /* OPENSSL_NO_ENGINE */
3060}
3061
3062
3063static int tls_connection_engine_private_key(struct tls_connection *conn)
3064{
Adam Langley1eb02ed2015-04-21 19:00:05 -07003065#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
3067 tls_show_errors(MSG_ERROR, __func__,
3068 "ENGINE: cannot use private key for TLS");
3069 return -1;
3070 }
3071 if (!SSL_check_private_key(conn->ssl)) {
3072 tls_show_errors(MSG_INFO, __func__,
3073 "Private key failed verification");
3074 return -1;
3075 }
3076 return 0;
3077#else /* OPENSSL_NO_ENGINE */
3078 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
3079 "engine support was not compiled in");
3080 return -1;
3081#endif /* OPENSSL_NO_ENGINE */
3082}
3083
3084
Roshan Pius3a1667e2018-07-03 15:17:14 -07003085#ifndef OPENSSL_NO_STDIO
3086static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003087{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003088 if (!password)
3089 return 0;
3090 os_strlcpy(buf, (const char *) password, size);
3091 return os_strlen(buf);
3092}
3093#endif /* OPENSSL_NO_STDIO */
3094
3095
3096static int tls_use_private_key_file(struct tls_data *data, SSL *ssl,
3097 const char *private_key,
3098 const char *private_key_passwd)
3099{
3100#ifndef OPENSSL_NO_STDIO
3101 BIO *bio;
3102 EVP_PKEY *pkey;
3103 int ret;
3104
3105 /* First try ASN.1 (DER). */
3106 bio = BIO_new_file(private_key, "r");
3107 if (!bio)
3108 return -1;
3109 pkey = d2i_PrivateKey_bio(bio, NULL);
3110 BIO_free(bio);
3111
3112 if (pkey) {
3113 wpa_printf(MSG_DEBUG, "OpenSSL: %s (DER) --> loaded", __func__);
3114 } else {
3115 /* Try PEM with the provided password. */
3116 bio = BIO_new_file(private_key, "r");
3117 if (!bio)
3118 return -1;
3119 pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_passwd_cb,
3120 (void *) private_key_passwd);
3121 BIO_free(bio);
3122 if (!pkey)
3123 return -1;
3124 wpa_printf(MSG_DEBUG, "OpenSSL: %s (PEM) --> loaded", __func__);
3125 /* Clear errors from the previous failed load. */
3126 ERR_clear_error();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003127 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003128
3129 if (ssl)
3130 ret = SSL_use_PrivateKey(ssl, pkey);
3131 else
3132 ret = SSL_CTX_use_PrivateKey(data->ssl, pkey);
3133
3134 EVP_PKEY_free(pkey);
3135 return ret == 1 ? 0 : -1;
3136#else /* OPENSSL_NO_STDIO */
3137 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
3138 return -1;
3139#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003140}
3141
3142
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003143static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 struct tls_connection *conn,
3145 const char *private_key,
3146 const char *private_key_passwd,
3147 const u8 *private_key_blob,
3148 size_t private_key_blob_len)
3149{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 int ok;
3151
3152 if (private_key == NULL && private_key_blob == NULL)
3153 return 0;
3154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 ok = 0;
3156 while (private_key_blob) {
3157 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
3158 (u8 *) private_key_blob,
3159 private_key_blob_len) == 1) {
3160 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3161 "ASN1(EVP_PKEY_RSA) --> OK");
3162 ok = 1;
3163 break;
3164 }
3165
3166 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
3167 (u8 *) private_key_blob,
3168 private_key_blob_len) == 1) {
3169 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
3170 "ASN1(EVP_PKEY_DSA) --> OK");
3171 ok = 1;
3172 break;
3173 }
3174
3175 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
3176 (u8 *) private_key_blob,
3177 private_key_blob_len) == 1) {
3178 wpa_printf(MSG_DEBUG, "OpenSSL: "
3179 "SSL_use_RSAPrivateKey_ASN1 --> OK");
3180 ok = 1;
3181 break;
3182 }
3183
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003184 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003185 private_key_blob_len,
3186 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
3188 "OK");
3189 ok = 1;
3190 break;
3191 }
3192
3193 break;
3194 }
3195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196 while (!ok && private_key) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003197 if (tls_use_private_key_file(data, conn->ssl, private_key,
3198 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 ok = 1;
3200 break;
3201 }
3202
Roshan Pius3a1667e2018-07-03 15:17:14 -07003203 if (tls_read_pkcs12(data, conn->ssl, private_key,
3204 private_key_passwd) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
3206 "--> OK");
3207 ok = 1;
3208 break;
3209 }
3210
3211 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
3212 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
3213 "access certificate store --> OK");
3214 ok = 1;
3215 break;
3216 }
3217
3218 break;
3219 }
3220
3221 if (!ok) {
3222 tls_show_errors(MSG_INFO, __func__,
3223 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 return -1;
3225 }
3226 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 if (!SSL_check_private_key(conn->ssl)) {
3229 tls_show_errors(MSG_INFO, __func__, "Private key failed "
3230 "verification");
3231 return -1;
3232 }
3233
3234 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
3235 return 0;
3236}
3237
3238
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003239static int tls_global_private_key(struct tls_data *data,
3240 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241 const char *private_key_passwd)
3242{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003243 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244
3245 if (private_key == NULL)
3246 return 0;
3247
Roshan Pius3a1667e2018-07-03 15:17:14 -07003248 if (tls_use_private_key_file(data, NULL, private_key,
3249 private_key_passwd) &&
3250 tls_read_pkcs12(data, NULL, private_key, private_key_passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 tls_show_errors(MSG_INFO, __func__,
3252 "Failed to load private key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 ERR_clear_error();
3254 return -1;
3255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 ERR_clear_error();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 if (!SSL_CTX_check_private_key(ssl_ctx)) {
3259 tls_show_errors(MSG_INFO, __func__,
3260 "Private key failed verification");
3261 return -1;
3262 }
3263
3264 return 0;
3265}
3266
3267
3268static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
3269{
3270#ifdef OPENSSL_NO_DH
3271 if (dh_file == NULL)
3272 return 0;
3273 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3274 "dh_file specified");
3275 return -1;
3276#else /* OPENSSL_NO_DH */
3277 DH *dh;
3278 BIO *bio;
3279
3280 /* TODO: add support for dh_blob */
3281 if (dh_file == NULL)
3282 return 0;
3283 if (conn == NULL)
3284 return -1;
3285
3286 bio = BIO_new_file(dh_file, "r");
3287 if (bio == NULL) {
3288 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3289 dh_file, ERR_error_string(ERR_get_error(), NULL));
3290 return -1;
3291 }
3292 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3293 BIO_free(bio);
3294#ifndef OPENSSL_NO_DSA
3295 while (dh == NULL) {
3296 DSA *dsa;
3297 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3298 " trying to parse as DSA params", dh_file,
3299 ERR_error_string(ERR_get_error(), NULL));
3300 bio = BIO_new_file(dh_file, "r");
3301 if (bio == NULL)
3302 break;
3303 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3304 BIO_free(bio);
3305 if (!dsa) {
3306 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3307 "'%s': %s", dh_file,
3308 ERR_error_string(ERR_get_error(), NULL));
3309 break;
3310 }
3311
3312 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3313 dh = DSA_dup_DH(dsa);
3314 DSA_free(dsa);
3315 if (dh == NULL) {
3316 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3317 "params into DH params");
3318 break;
3319 }
3320 break;
3321 }
3322#endif /* !OPENSSL_NO_DSA */
3323 if (dh == NULL) {
3324 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3325 "'%s'", dh_file);
3326 return -1;
3327 }
3328
3329 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
3330 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3331 "%s", dh_file,
3332 ERR_error_string(ERR_get_error(), NULL));
3333 DH_free(dh);
3334 return -1;
3335 }
3336 DH_free(dh);
3337 return 0;
3338#endif /* OPENSSL_NO_DH */
3339}
3340
3341
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003342static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343{
3344#ifdef OPENSSL_NO_DH
3345 if (dh_file == NULL)
3346 return 0;
3347 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3348 "dh_file specified");
3349 return -1;
3350#else /* OPENSSL_NO_DH */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003351 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003352 DH *dh;
3353 BIO *bio;
3354
3355 /* TODO: add support for dh_blob */
3356 if (dh_file == NULL)
3357 return 0;
3358 if (ssl_ctx == NULL)
3359 return -1;
3360
3361 bio = BIO_new_file(dh_file, "r");
3362 if (bio == NULL) {
3363 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3364 dh_file, ERR_error_string(ERR_get_error(), NULL));
3365 return -1;
3366 }
3367 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3368 BIO_free(bio);
3369#ifndef OPENSSL_NO_DSA
3370 while (dh == NULL) {
3371 DSA *dsa;
3372 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3373 " trying to parse as DSA params", dh_file,
3374 ERR_error_string(ERR_get_error(), NULL));
3375 bio = BIO_new_file(dh_file, "r");
3376 if (bio == NULL)
3377 break;
3378 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3379 BIO_free(bio);
3380 if (!dsa) {
3381 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3382 "'%s': %s", dh_file,
3383 ERR_error_string(ERR_get_error(), NULL));
3384 break;
3385 }
3386
3387 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3388 dh = DSA_dup_DH(dsa);
3389 DSA_free(dsa);
3390 if (dh == NULL) {
3391 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3392 "params into DH params");
3393 break;
3394 }
3395 break;
3396 }
3397#endif /* !OPENSSL_NO_DSA */
3398 if (dh == NULL) {
3399 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3400 "'%s'", dh_file);
3401 return -1;
3402 }
3403
3404 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
3405 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3406 "%s", dh_file,
3407 ERR_error_string(ERR_get_error(), NULL));
3408 DH_free(dh);
3409 return -1;
3410 }
3411 DH_free(dh);
3412 return 0;
3413#endif /* OPENSSL_NO_DH */
3414}
3415
3416
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003417int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
3418 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419{
3420 SSL *ssl;
3421
3422 if (conn == NULL || keys == NULL)
3423 return -1;
3424 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003425 if (ssl == NULL)
3426 return -1;
3427
3428 os_memset(keys, 0, sizeof(*keys));
3429 keys->client_random = conn->client_random;
3430 keys->client_random_len = SSL_get_client_random(
3431 ssl, conn->client_random, sizeof(conn->client_random));
3432 keys->server_random = conn->server_random;
3433 keys->server_random_len = SSL_get_server_random(
3434 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435
3436 return 0;
3437}
3438
3439
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003440#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003441static int openssl_get_keyblock_size(SSL *ssl)
3442{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003443#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
3444 (defined(LIBRESSL_VERSION_NUMBER) && \
3445 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003446 const EVP_CIPHER *c;
3447 const EVP_MD *h;
3448 int md_size;
3449
3450 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
3451 ssl->read_hash == NULL)
3452 return -1;
3453
3454 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003455 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003456 if (h)
3457 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003458 else if (ssl->s3)
3459 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003460 else
3461 return -1;
3462
3463 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3464 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
3465 EVP_CIPHER_iv_length(c));
3466 return 2 * (EVP_CIPHER_key_length(c) +
3467 md_size +
3468 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003469#else
3470 const SSL_CIPHER *ssl_cipher;
3471 int cipher, digest;
3472 const EVP_CIPHER *c;
3473 const EVP_MD *h;
3474
3475 ssl_cipher = SSL_get_current_cipher(ssl);
3476 if (!ssl_cipher)
3477 return -1;
3478 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
3479 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
3480 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
3481 cipher, digest);
3482 if (cipher < 0 || digest < 0)
3483 return -1;
3484 c = EVP_get_cipherbynid(cipher);
3485 h = EVP_get_digestbynid(digest);
3486 if (!c || !h)
3487 return -1;
3488
3489 wpa_printf(MSG_DEBUG,
3490 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3491 EVP_CIPHER_key_length(c), EVP_MD_size(h),
3492 EVP_CIPHER_iv_length(c));
3493 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
3494 EVP_CIPHER_iv_length(c));
3495#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003496}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003497#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003498
3499
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003500int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
3501 const char *label, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003502{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003503 if (!conn ||
3504 SSL_export_keying_material(conn->ssl, out, out_len, label,
3505 os_strlen(label), NULL, 0, 0) != 1)
3506 return -1;
3507 return 0;
3508}
3509
3510
3511int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
3512 u8 *out, size_t out_len)
3513{
3514#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003515 SSL *ssl;
3516 SSL_SESSION *sess;
3517 u8 *rnd;
3518 int ret = -1;
3519 int skip = 0;
3520 u8 *tmp_out = NULL;
3521 u8 *_out = out;
3522 unsigned char client_random[SSL3_RANDOM_SIZE];
3523 unsigned char server_random[SSL3_RANDOM_SIZE];
3524 unsigned char master_key[64];
3525 size_t master_key_len;
3526 const char *ver;
3527
3528 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003529 * TLS library did not support EAP-FAST key generation, so get the
3530 * needed TLS session parameters and use an internal implementation of
3531 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003532 */
3533
3534 if (conn == NULL)
3535 return -1;
3536 ssl = conn->ssl;
3537 if (ssl == NULL)
3538 return -1;
3539 ver = SSL_get_version(ssl);
3540 sess = SSL_get_session(ssl);
3541 if (!ver || !sess)
3542 return -1;
3543
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003544 skip = openssl_get_keyblock_size(ssl);
3545 if (skip < 0)
3546 return -1;
3547 tmp_out = os_malloc(skip + out_len);
3548 if (!tmp_out)
3549 return -1;
3550 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003551
3552 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
3553 if (!rnd) {
3554 os_free(tmp_out);
3555 return -1;
3556 }
3557
3558 SSL_get_client_random(ssl, client_random, sizeof(client_random));
3559 SSL_get_server_random(ssl, server_random, sizeof(server_random));
3560 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
3561 sizeof(master_key));
3562
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003563 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
3564 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003565
3566 if (os_strcmp(ver, "TLSv1.2") == 0) {
3567 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003568 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003569 _out, skip + out_len);
3570 ret = 0;
3571 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003572 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003573 _out, skip + out_len) == 0) {
3574 ret = 0;
3575 }
3576 os_memset(master_key, 0, sizeof(master_key));
3577 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003578 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003579 os_memcpy(out, _out + skip, out_len);
3580 bin_clear_free(tmp_out, skip);
3581
3582 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003583#else /* OPENSSL_NEED_EAP_FAST_PRF */
3584 wpa_printf(MSG_ERROR,
3585 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3586 return -1;
3587#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003588}
3589
3590
3591static struct wpabuf *
Roshan Pius3a1667e2018-07-03 15:17:14 -07003592openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593{
3594 int res;
3595 struct wpabuf *out_data;
3596
3597 /*
3598 * Give TLS handshake data from the server (if available) to OpenSSL
3599 * for processing.
3600 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003601 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
3603 < 0) {
3604 tls_show_errors(MSG_INFO, __func__,
3605 "Handshake failed - BIO_write");
3606 return NULL;
3607 }
3608
3609 /* Initiate TLS handshake or continue the existing handshake */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003610 if (conn->server)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 res = SSL_accept(conn->ssl);
3612 else
3613 res = SSL_connect(conn->ssl);
3614 if (res != 1) {
3615 int err = SSL_get_error(conn->ssl, res);
3616 if (err == SSL_ERROR_WANT_READ)
3617 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
3618 "more data");
3619 else if (err == SSL_ERROR_WANT_WRITE)
3620 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
3621 "write");
3622 else {
3623 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
3624 conn->failed++;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003625 if (!conn->server && !conn->client_hello_generated) {
3626 /* The server would not understand TLS Alert
3627 * before ClientHello, so simply terminate
3628 * handshake on this type of error case caused
3629 * by a likely internal error like no ciphers
3630 * available. */
3631 wpa_printf(MSG_DEBUG,
3632 "OpenSSL: Could not generate ClientHello");
3633 conn->write_alerts++;
3634 return NULL;
3635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 }
3637 }
3638
Roshan Pius3a1667e2018-07-03 15:17:14 -07003639 if (!conn->server && !conn->failed)
3640 conn->client_hello_generated = 1;
3641
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003642#ifdef CONFIG_SUITEB
Roshan Pius3a1667e2018-07-03 15:17:14 -07003643 if ((conn->flags & TLS_CONN_SUITEB) && !conn->server &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003644 os_strncmp(SSL_get_cipher(conn->ssl), "DHE-", 4) == 0 &&
3645 conn->server_dh_prime_len < 3072) {
3646 struct tls_context *context = conn->context;
3647
3648 /*
3649 * This should not be reached since earlier cert_cb should have
3650 * terminated the handshake. Keep this check here for extra
3651 * protection if anything goes wrong with the more low-level
3652 * checks based on having to parse the TLS handshake messages.
3653 */
3654 wpa_printf(MSG_DEBUG,
3655 "OpenSSL: Server DH prime length: %d bits",
3656 conn->server_dh_prime_len);
3657
3658 if (context->event_cb) {
3659 union tls_event_data ev;
3660
3661 os_memset(&ev, 0, sizeof(ev));
3662 ev.alert.is_local = 1;
3663 ev.alert.type = "fatal";
3664 ev.alert.description = "insufficient security";
3665 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
3666 }
3667 /*
3668 * Could send a TLS Alert to the server, but for now, simply
3669 * terminate handshake.
3670 */
3671 conn->failed++;
3672 conn->write_alerts++;
3673 return NULL;
3674 }
3675#endif /* CONFIG_SUITEB */
3676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677 /* Get the TLS handshake data to be sent to the server */
3678 res = BIO_ctrl_pending(conn->ssl_out);
3679 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
3680 out_data = wpabuf_alloc(res);
3681 if (out_data == NULL) {
3682 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
3683 "handshake output (%d bytes)", res);
3684 if (BIO_reset(conn->ssl_out) < 0) {
3685 tls_show_errors(MSG_INFO, __func__,
3686 "BIO_reset failed");
3687 }
3688 return NULL;
3689 }
3690 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
3691 res);
3692 if (res < 0) {
3693 tls_show_errors(MSG_INFO, __func__,
3694 "Handshake failed - BIO_read");
3695 if (BIO_reset(conn->ssl_out) < 0) {
3696 tls_show_errors(MSG_INFO, __func__,
3697 "BIO_reset failed");
3698 }
3699 wpabuf_free(out_data);
3700 return NULL;
3701 }
3702 wpabuf_put(out_data, res);
3703
3704 return out_data;
3705}
3706
3707
3708static struct wpabuf *
3709openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
3710{
3711 struct wpabuf *appl_data;
3712 int res;
3713
3714 appl_data = wpabuf_alloc(max_len + 100);
3715 if (appl_data == NULL)
3716 return NULL;
3717
3718 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
3719 wpabuf_size(appl_data));
3720 if (res < 0) {
3721 int err = SSL_get_error(conn->ssl, res);
3722 if (err == SSL_ERROR_WANT_READ ||
3723 err == SSL_ERROR_WANT_WRITE) {
3724 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
3725 "included");
3726 } else {
3727 tls_show_errors(MSG_INFO, __func__,
3728 "Failed to read possible "
3729 "Application Data");
3730 }
3731 wpabuf_free(appl_data);
3732 return NULL;
3733 }
3734
3735 wpabuf_put(appl_data, res);
3736 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
3737 "message", appl_data);
3738
3739 return appl_data;
3740}
3741
3742
3743static struct wpabuf *
3744openssl_connection_handshake(struct tls_connection *conn,
3745 const struct wpabuf *in_data,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003746 struct wpabuf **appl_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747{
3748 struct wpabuf *out_data;
3749
3750 if (appl_data)
3751 *appl_data = NULL;
3752
Roshan Pius3a1667e2018-07-03 15:17:14 -07003753 out_data = openssl_handshake(conn, in_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 if (out_data == NULL)
3755 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03003756 if (conn->invalid_hb_used) {
3757 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3758 wpabuf_free(out_data);
3759 return NULL;
3760 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003762 if (SSL_is_init_finished(conn->ssl)) {
3763 wpa_printf(MSG_DEBUG,
3764 "OpenSSL: Handshake finished - resumed=%d",
3765 tls_connection_resumed(conn->ssl_ctx, conn));
3766 if (appl_data && in_data)
3767 *appl_data = openssl_get_appl_data(conn,
3768 wpabuf_len(in_data));
3769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770
Jouni Malinen26af48b2014-04-09 13:02:53 +03003771 if (conn->invalid_hb_used) {
3772 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3773 if (appl_data) {
3774 wpabuf_free(*appl_data);
3775 *appl_data = NULL;
3776 }
3777 wpabuf_free(out_data);
3778 return NULL;
3779 }
3780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 return out_data;
3782}
3783
3784
3785struct wpabuf *
3786tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
3787 const struct wpabuf *in_data,
3788 struct wpabuf **appl_data)
3789{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003790 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791}
3792
3793
3794struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
3795 struct tls_connection *conn,
3796 const struct wpabuf *in_data,
3797 struct wpabuf **appl_data)
3798{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003799 conn->server = 1;
3800 return openssl_connection_handshake(conn, in_data, appl_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801}
3802
3803
3804struct wpabuf * tls_connection_encrypt(void *tls_ctx,
3805 struct tls_connection *conn,
3806 const struct wpabuf *in_data)
3807{
3808 int res;
3809 struct wpabuf *buf;
3810
3811 if (conn == NULL)
3812 return NULL;
3813
3814 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3815 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
3816 (res = BIO_reset(conn->ssl_out)) < 0) {
3817 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3818 return NULL;
3819 }
3820 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
3821 if (res < 0) {
3822 tls_show_errors(MSG_INFO, __func__,
3823 "Encryption failed - SSL_write");
3824 return NULL;
3825 }
3826
3827 /* Read encrypted data to be sent to the server */
3828 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
3829 if (buf == NULL)
3830 return NULL;
3831 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
3832 if (res < 0) {
3833 tls_show_errors(MSG_INFO, __func__,
3834 "Encryption failed - BIO_read");
3835 wpabuf_free(buf);
3836 return NULL;
3837 }
3838 wpabuf_put(buf, res);
3839
3840 return buf;
3841}
3842
3843
3844struct wpabuf * tls_connection_decrypt(void *tls_ctx,
3845 struct tls_connection *conn,
3846 const struct wpabuf *in_data)
3847{
3848 int res;
3849 struct wpabuf *buf;
3850
3851 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3852 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
3853 wpabuf_len(in_data));
3854 if (res < 0) {
3855 tls_show_errors(MSG_INFO, __func__,
3856 "Decryption failed - BIO_write");
3857 return NULL;
3858 }
3859 if (BIO_reset(conn->ssl_out) < 0) {
3860 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3861 return NULL;
3862 }
3863
3864 /* Read decrypted data for further processing */
3865 /*
3866 * Even though we try to disable TLS compression, it is possible that
3867 * this cannot be done with all TLS libraries. Add extra buffer space
3868 * to handle the possibility of the decrypted data being longer than
3869 * input data.
3870 */
3871 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
3872 if (buf == NULL)
3873 return NULL;
3874 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
3875 if (res < 0) {
3876 tls_show_errors(MSG_INFO, __func__,
3877 "Decryption failed - SSL_read");
3878 wpabuf_free(buf);
3879 return NULL;
3880 }
3881 wpabuf_put(buf, res);
3882
Jouni Malinen26af48b2014-04-09 13:02:53 +03003883 if (conn->invalid_hb_used) {
3884 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3885 wpabuf_free(buf);
3886 return NULL;
3887 }
3888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 return buf;
3890}
3891
3892
3893int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
3894{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003895 return conn ? SSL_cache_hit(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896}
3897
3898
3899int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
3900 u8 *ciphers)
3901{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003902 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903 u8 *c;
3904 int ret;
3905
3906 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
3907 return -1;
3908
3909 buf[0] = '\0';
3910 pos = buf;
3911 end = pos + sizeof(buf);
3912
3913 c = ciphers;
3914 while (*c != TLS_CIPHER_NONE) {
3915 const char *suite;
3916
3917 switch (*c) {
3918 case TLS_CIPHER_RC4_SHA:
3919 suite = "RC4-SHA";
3920 break;
3921 case TLS_CIPHER_AES128_SHA:
3922 suite = "AES128-SHA";
3923 break;
3924 case TLS_CIPHER_RSA_DHE_AES128_SHA:
3925 suite = "DHE-RSA-AES128-SHA";
3926 break;
3927 case TLS_CIPHER_ANON_DH_AES128_SHA:
3928 suite = "ADH-AES128-SHA";
3929 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003930 case TLS_CIPHER_RSA_DHE_AES256_SHA:
3931 suite = "DHE-RSA-AES256-SHA";
3932 break;
3933 case TLS_CIPHER_AES256_SHA:
3934 suite = "AES256-SHA";
3935 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 default:
3937 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
3938 "cipher selection: %d", *c);
3939 return -1;
3940 }
3941 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003942 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943 break;
3944 pos += ret;
3945
3946 c++;
3947 }
3948
3949 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
3950
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003951#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003952#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3953 if (os_strstr(buf, ":ADH-")) {
3954 /*
3955 * Need to drop to security level 0 to allow anonymous
3956 * cipher suites for EAP-FAST.
3957 */
3958 SSL_set_security_level(conn->ssl, 0);
3959 } else if (SSL_get_security_level(conn->ssl) == 0) {
3960 /* Force at least security level 1 */
3961 SSL_set_security_level(conn->ssl, 1);
3962 }
3963#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3964#endif
3965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
3967 tls_show_errors(MSG_INFO, __func__,
3968 "Cipher suite configuration failed");
3969 return -1;
3970 }
3971
3972 return 0;
3973}
3974
3975
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003976int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
3977 char *buf, size_t buflen)
3978{
3979 const char *name;
3980 if (conn == NULL || conn->ssl == NULL)
3981 return -1;
3982
3983 name = SSL_get_version(conn->ssl);
3984 if (name == NULL)
3985 return -1;
3986
3987 os_strlcpy(buf, name, buflen);
3988 return 0;
3989}
3990
3991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
3993 char *buf, size_t buflen)
3994{
3995 const char *name;
3996 if (conn == NULL || conn->ssl == NULL)
3997 return -1;
3998
3999 name = SSL_get_cipher(conn->ssl);
4000 if (name == NULL)
4001 return -1;
4002
4003 os_strlcpy(buf, name, buflen);
4004 return 0;
4005}
4006
4007
4008int tls_connection_enable_workaround(void *ssl_ctx,
4009 struct tls_connection *conn)
4010{
4011 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
4012
4013 return 0;
4014}
4015
4016
4017#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4018/* ClientHello TLS extensions require a patch to openssl, so this function is
4019 * commented out unless explicitly needed for EAP-FAST in order to be able to
4020 * build this file with unmodified openssl. */
4021int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
4022 int ext_type, const u8 *data,
4023 size_t data_len)
4024{
4025 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
4026 return -1;
4027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
4029 data_len) != 1)
4030 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031
4032 return 0;
4033}
4034#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4035
4036
4037int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
4038{
4039 if (conn == NULL)
4040 return -1;
4041 return conn->failed;
4042}
4043
4044
4045int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
4046{
4047 if (conn == NULL)
4048 return -1;
4049 return conn->read_alerts;
4050}
4051
4052
4053int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
4054{
4055 if (conn == NULL)
4056 return -1;
4057 return conn->write_alerts;
4058}
4059
4060
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004061#ifdef HAVE_OCSP
4062
4063static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
4064{
4065#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004066 BIO *out;
4067 size_t rlen;
4068 char *txt;
4069 int res;
4070
4071 if (wpa_debug_level > MSG_DEBUG)
4072 return;
4073
4074 out = BIO_new(BIO_s_mem());
4075 if (!out)
4076 return;
4077
4078 OCSP_RESPONSE_print(out, rsp, 0);
4079 rlen = BIO_ctrl_pending(out);
4080 txt = os_malloc(rlen + 1);
4081 if (!txt) {
4082 BIO_free(out);
4083 return;
4084 }
4085
4086 res = BIO_read(out, txt, rlen);
4087 if (res > 0) {
4088 txt[res] = '\0';
4089 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
4090 }
4091 os_free(txt);
4092 BIO_free(out);
4093#endif /* CONFIG_NO_STDOUT_DEBUG */
4094}
4095
4096
Dmitry Shmidt71757432014-06-02 13:50:35 -07004097static void debug_print_cert(X509 *cert, const char *title)
4098{
4099#ifndef CONFIG_NO_STDOUT_DEBUG
4100 BIO *out;
4101 size_t rlen;
4102 char *txt;
4103 int res;
4104
4105 if (wpa_debug_level > MSG_DEBUG)
4106 return;
4107
4108 out = BIO_new(BIO_s_mem());
4109 if (!out)
4110 return;
4111
4112 X509_print(out, cert);
4113 rlen = BIO_ctrl_pending(out);
4114 txt = os_malloc(rlen + 1);
4115 if (!txt) {
4116 BIO_free(out);
4117 return;
4118 }
4119
4120 res = BIO_read(out, txt, rlen);
4121 if (res > 0) {
4122 txt[res] = '\0';
4123 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
4124 }
4125 os_free(txt);
4126
4127 BIO_free(out);
4128#endif /* CONFIG_NO_STDOUT_DEBUG */
4129}
4130
4131
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004132static int ocsp_resp_cb(SSL *s, void *arg)
4133{
4134 struct tls_connection *conn = arg;
4135 const unsigned char *p;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004136 int len, status, reason, res;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004137 OCSP_RESPONSE *rsp;
4138 OCSP_BASICRESP *basic;
4139 OCSP_CERTID *id;
4140 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004141 X509_STORE *store;
4142 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004143
4144 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
4145 if (!p) {
4146 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
4147 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
4148 }
4149
4150 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
4151
4152 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
4153 if (!rsp) {
4154 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
4155 return 0;
4156 }
4157
4158 ocsp_debug_print_resp(rsp);
4159
4160 status = OCSP_response_status(rsp);
4161 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
4162 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
4163 status, OCSP_response_status_str(status));
4164 return 0;
4165 }
4166
4167 basic = OCSP_response_get1_basic(rsp);
4168 if (!basic) {
4169 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
4170 return 0;
4171 }
4172
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004173 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004174 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07004175 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004176
4177 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
4178 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004179 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004180 }
4181 certs = sk_X509_new_null();
4182 if (certs) {
4183 X509 *cert;
4184 cert = X509_dup(conn->peer_issuer);
4185 if (cert && !sk_X509_push(certs, cert)) {
4186 tls_show_errors(
4187 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004188 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004189 X509_free(cert);
4190 sk_X509_free(certs);
4191 certs = NULL;
4192 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004193 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004194 cert = X509_dup(conn->peer_issuer_issuer);
4195 if (cert && !sk_X509_push(certs, cert)) {
4196 tls_show_errors(
4197 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004198 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004199 X509_free(cert);
4200 }
4201 }
4202 }
4203 }
4204
4205 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
4206 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004207 if (status <= 0) {
4208 tls_show_errors(MSG_INFO, __func__,
4209 "OpenSSL: OCSP response failed verification");
4210 OCSP_BASICRESP_free(basic);
4211 OCSP_RESPONSE_free(rsp);
4212 return 0;
4213 }
4214
4215 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
4216
Dmitry Shmidt56052862013-10-04 10:23:25 -07004217 if (!conn->peer_cert) {
4218 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
4219 OCSP_BASICRESP_free(basic);
4220 OCSP_RESPONSE_free(rsp);
4221 return 0;
4222 }
4223
4224 if (!conn->peer_issuer) {
4225 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004226 OCSP_BASICRESP_free(basic);
4227 OCSP_RESPONSE_free(rsp);
4228 return 0;
4229 }
4230
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004231 id = OCSP_cert_to_id(EVP_sha256(), conn->peer_cert, conn->peer_issuer);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004232 if (!id) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004233 wpa_printf(MSG_DEBUG,
4234 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004235 OCSP_BASICRESP_free(basic);
4236 OCSP_RESPONSE_free(rsp);
4237 return 0;
4238 }
4239
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004240 res = OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
4241 &this_update, &next_update);
4242 if (!res) {
4243 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
4244 if (!id) {
4245 wpa_printf(MSG_DEBUG,
4246 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
4247 OCSP_BASICRESP_free(basic);
4248 OCSP_RESPONSE_free(rsp);
4249 return 0;
4250 }
4251
4252 res = OCSP_resp_find_status(basic, id, &status, &reason,
4253 &produced_at, &this_update,
4254 &next_update);
4255 }
4256
4257 if (!res) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004258 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
4259 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
4260 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004261 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004262 OCSP_BASICRESP_free(basic);
4263 OCSP_RESPONSE_free(rsp);
4264 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
4265 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004266 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004267
4268 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
4269 tls_show_errors(MSG_INFO, __func__,
4270 "OpenSSL: OCSP status times invalid");
4271 OCSP_BASICRESP_free(basic);
4272 OCSP_RESPONSE_free(rsp);
4273 return 0;
4274 }
4275
4276 OCSP_BASICRESP_free(basic);
4277 OCSP_RESPONSE_free(rsp);
4278
4279 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
4280 OCSP_cert_status_str(status));
4281
4282 if (status == V_OCSP_CERTSTATUS_GOOD)
4283 return 1;
4284 if (status == V_OCSP_CERTSTATUS_REVOKED)
4285 return 0;
4286 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
4287 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
4288 return 0;
4289 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07004290 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 -07004291 return 1;
4292}
4293
4294
4295static int ocsp_status_cb(SSL *s, void *arg)
4296{
4297 char *tmp;
4298 char *resp;
4299 size_t len;
4300
4301 if (tls_global->ocsp_stapling_response == NULL) {
4302 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
4303 return SSL_TLSEXT_ERR_OK;
4304 }
4305
4306 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
4307 if (resp == NULL) {
4308 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
4309 /* TODO: Build OCSPResponse with responseStatus = internalError
4310 */
4311 return SSL_TLSEXT_ERR_OK;
4312 }
4313 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
4314 tmp = OPENSSL_malloc(len);
4315 if (tmp == NULL) {
4316 os_free(resp);
4317 return SSL_TLSEXT_ERR_ALERT_FATAL;
4318 }
4319
4320 os_memcpy(tmp, resp, len);
4321 os_free(resp);
4322 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
4323
4324 return SSL_TLSEXT_ERR_OK;
4325}
4326
4327#endif /* HAVE_OCSP */
4328
4329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
4331 const struct tls_connection_params *params)
4332{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004333 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 int ret;
4335 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004336 int can_pkcs11 = 0;
4337 const char *key_id = params->key_id;
4338 const char *cert_id = params->cert_id;
4339 const char *ca_cert_id = params->ca_cert_id;
4340 const char *engine_id = params->engine ? params->engine_id : NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004341 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342
4343 if (conn == NULL)
4344 return -1;
4345
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004346 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
4347 wpa_printf(MSG_INFO,
4348 "OpenSSL: ocsp=3 not supported");
4349 return -1;
4350 }
4351
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004352 /*
4353 * If the engine isn't explicitly configured, and any of the
4354 * cert/key fields are actually PKCS#11 URIs, then automatically
4355 * use the PKCS#11 ENGINE.
4356 */
4357 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
4358 can_pkcs11 = 1;
4359
4360 if (!key_id && params->private_key && can_pkcs11 &&
4361 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
4362 can_pkcs11 = 2;
4363 key_id = params->private_key;
4364 }
4365
4366 if (!cert_id && params->client_cert && can_pkcs11 &&
4367 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
4368 can_pkcs11 = 2;
4369 cert_id = params->client_cert;
4370 }
4371
4372 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
4373 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
4374 can_pkcs11 = 2;
4375 ca_cert_id = params->ca_cert;
4376 }
4377
4378 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
4379 if (can_pkcs11 == 2 && !engine_id)
4380 engine_id = "pkcs11";
4381
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004382#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004383#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004384 if (params->flags & TLS_CONN_EAP_FAST) {
4385 wpa_printf(MSG_DEBUG,
4386 "OpenSSL: Use TLSv1_method() for EAP-FAST");
4387 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
4388 tls_show_errors(MSG_INFO, __func__,
4389 "Failed to set TLSv1_method() for EAP-FAST");
4390 return -1;
4391 }
4392 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004393#endif
Roshan Pius3a1667e2018-07-03 15:17:14 -07004394#if OPENSSL_VERSION_NUMBER >= 0x10101000L
4395#ifdef SSL_OP_NO_TLSv1_3
4396 if (params->flags & TLS_CONN_EAP_FAST) {
4397 /* Need to disable TLS v1.3 at least for now since OpenSSL 1.1.1
4398 * refuses to start the handshake with the modified ciphersuite
4399 * list (no TLS v1.3 ciphersuites included) for EAP-FAST. */
4400 wpa_printf(MSG_DEBUG, "OpenSSL: Disable TLSv1.3 for EAP-FAST");
4401 SSL_set_options(conn->ssl, SSL_OP_NO_TLSv1_3);
4402 }
4403#endif /* SSL_OP_NO_TLSv1_3 */
4404#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004405#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 while ((err = ERR_get_error())) {
4408 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4409 __func__, ERR_error_string(err, NULL));
4410 }
4411
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004412 if (engine_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004414 ret = tls_engine_init(conn, engine_id, params->pin,
4415 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 if (ret)
4417 return ret;
4418 }
4419 if (tls_connection_set_subject_match(conn,
4420 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07004421 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004422 params->suffix_match,
4423 params->domain_match))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 return -1;
4425
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004426 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004427 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004429 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 params->ca_cert_blob,
4431 params->ca_cert_blob_len,
4432 params->ca_path))
4433 return -1;
4434
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004435 if (engine_id && cert_id) {
4436 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
4438 } else if (tls_connection_client_cert(conn, params->client_cert,
4439 params->client_cert_blob,
4440 params->client_cert_blob_len))
4441 return -1;
4442
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004443 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
4445 if (tls_connection_engine_private_key(conn))
4446 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004447 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 params->private_key,
4449 params->private_key_passwd,
4450 params->private_key_blob,
4451 params->private_key_blob_len)) {
4452 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
4453 params->private_key);
4454 return -1;
4455 }
4456
4457 if (tls_connection_dh(conn, params->dh_file)) {
4458 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
4459 params->dh_file);
4460 return -1;
4461 }
4462
Roshan Pius3a1667e2018-07-03 15:17:14 -07004463 ciphers = params->openssl_ciphers;
4464#ifdef CONFIG_SUITEB
4465#ifdef OPENSSL_IS_BORINGSSL
4466 if (ciphers && os_strcmp(ciphers, "SUITEB192") == 0) {
4467 /* BoringSSL removed support for SUITEB192, so need to handle
4468 * this with hardcoded ciphersuite and additional checks for
4469 * other parameters. */
4470 ciphers = "ECDHE-ECDSA-AES256-GCM-SHA384";
4471 }
4472#endif /* OPENSSL_IS_BORINGSSL */
4473#endif /* CONFIG_SUITEB */
4474 if (ciphers && SSL_set_cipher_list(conn->ssl, ciphers) != 1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004475 wpa_printf(MSG_INFO,
4476 "OpenSSL: Failed to set cipher string '%s'",
Roshan Pius3a1667e2018-07-03 15:17:14 -07004477 ciphers);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004478 return -1;
4479 }
4480
Roshan Pius3a1667e2018-07-03 15:17:14 -07004481 if (tls_set_conn_flags(conn, params->flags,
4482 params->openssl_ciphers) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004483 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004484
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004485#ifdef OPENSSL_IS_BORINGSSL
4486 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4487 SSL_enable_ocsp_stapling(conn->ssl);
4488 }
4489#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004490#ifdef HAVE_OCSP
4491 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004492 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004493 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
4494 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
4495 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
4496 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004497#else /* HAVE_OCSP */
4498 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
4499 wpa_printf(MSG_INFO,
4500 "OpenSSL: No OCSP support included - reject configuration");
4501 return -1;
4502 }
4503 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4504 wpa_printf(MSG_DEBUG,
4505 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4506 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004507#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004508#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004509
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004510 conn->flags = params->flags;
4511
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004512 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513
4514 return 0;
4515}
4516
4517
4518int tls_global_set_params(void *tls_ctx,
4519 const struct tls_connection_params *params)
4520{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004521 struct tls_data *data = tls_ctx;
4522 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523 unsigned long err;
4524
4525 while ((err = ERR_get_error())) {
4526 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4527 __func__, ERR_error_string(err, NULL));
4528 }
4529
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004530 if (tls_global_ca_cert(data, params->ca_cert) ||
4531 tls_global_client_cert(data, params->client_cert) ||
4532 tls_global_private_key(data, params->private_key,
4533 params->private_key_passwd) ||
4534 tls_global_dh(data, params->dh_file)) {
4535 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536 return -1;
4537 }
4538
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004539 if (params->openssl_ciphers &&
4540 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
4541 wpa_printf(MSG_INFO,
4542 "OpenSSL: Failed to set cipher string '%s'",
4543 params->openssl_ciphers);
4544 return -1;
4545 }
4546
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004547#ifdef SSL_OP_NO_TICKET
4548 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
4549 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
4550 else
4551 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
4552#endif /* SSL_OP_NO_TICKET */
4553
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004554#ifdef HAVE_OCSP
4555 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
4556 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
4557 os_free(tls_global->ocsp_stapling_response);
4558 if (params->ocsp_stapling_response)
4559 tls_global->ocsp_stapling_response =
4560 os_strdup(params->ocsp_stapling_response);
4561 else
4562 tls_global->ocsp_stapling_response = NULL;
4563#endif /* HAVE_OCSP */
4564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565 return 0;
4566}
4567
4568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4570/* Pre-shared secred requires a patch to openssl, so this function is
4571 * commented out unless explicitly needed for EAP-FAST in order to be able to
4572 * build this file with unmodified openssl. */
4573
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004574#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004575static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4576 STACK_OF(SSL_CIPHER) *peer_ciphers,
4577 const SSL_CIPHER **cipher, void *arg)
4578#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4580 STACK_OF(SSL_CIPHER) *peer_ciphers,
4581 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004582#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583{
4584 struct tls_connection *conn = arg;
4585 int ret;
4586
Roshan Pius3a1667e2018-07-03 15:17:14 -07004587#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4588 (defined(LIBRESSL_VERSION_NUMBER) && \
4589 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590 if (conn == NULL || conn->session_ticket_cb == NULL)
4591 return 0;
4592
4593 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4594 conn->session_ticket,
4595 conn->session_ticket_len,
4596 s->s3->client_random,
4597 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004598#else
4599 unsigned char client_random[SSL3_RANDOM_SIZE];
4600 unsigned char server_random[SSL3_RANDOM_SIZE];
4601
4602 if (conn == NULL || conn->session_ticket_cb == NULL)
4603 return 0;
4604
4605 SSL_get_client_random(s, client_random, sizeof(client_random));
4606 SSL_get_server_random(s, server_random, sizeof(server_random));
4607
4608 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4609 conn->session_ticket,
4610 conn->session_ticket_len,
4611 client_random,
4612 server_random, secret);
4613#endif
4614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 os_free(conn->session_ticket);
4616 conn->session_ticket = NULL;
4617
4618 if (ret <= 0)
4619 return 0;
4620
4621 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
4622 return 1;
4623}
4624
4625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
4627 int len, void *arg)
4628{
4629 struct tls_connection *conn = arg;
4630
4631 if (conn == NULL || conn->session_ticket_cb == NULL)
4632 return 0;
4633
4634 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
4635
4636 os_free(conn->session_ticket);
4637 conn->session_ticket = NULL;
4638
4639 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
4640 "extension", data, len);
4641
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004642 conn->session_ticket = os_memdup(data, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 if (conn->session_ticket == NULL)
4644 return 0;
4645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 conn->session_ticket_len = len;
4647
4648 return 1;
4649}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4651
4652
4653int tls_connection_set_session_ticket_cb(void *tls_ctx,
4654 struct tls_connection *conn,
4655 tls_session_ticket_cb cb,
4656 void *ctx)
4657{
4658#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4659 conn->session_ticket_cb = cb;
4660 conn->session_ticket_cb_ctx = ctx;
4661
4662 if (cb) {
4663 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
4664 conn) != 1)
4665 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 SSL_set_session_ticket_ext_cb(conn->ssl,
4667 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668 } else {
4669 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
4670 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 }
4673
4674 return 0;
4675#else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4676 return -1;
4677#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4678}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004679
4680
4681int tls_get_library_version(char *buf, size_t buf_len)
4682{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004683#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004684 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4685 OPENSSL_VERSION_TEXT,
4686 OpenSSL_version(OPENSSL_VERSION));
4687#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004688 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4689 OPENSSL_VERSION_TEXT,
4690 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004691#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004692}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004693
4694
4695void tls_connection_set_success_data(struct tls_connection *conn,
4696 struct wpabuf *data)
4697{
4698 SSL_SESSION *sess;
4699 struct wpabuf *old;
4700
4701 if (tls_ex_idx_session < 0)
4702 goto fail;
4703 sess = SSL_get_session(conn->ssl);
4704 if (!sess)
4705 goto fail;
4706 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4707 if (old) {
4708 wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
4709 old);
4710 wpabuf_free(old);
4711 }
4712 if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
4713 goto fail;
4714
4715 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
4716 conn->success_data = 1;
4717 return;
4718
4719fail:
4720 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
4721 wpabuf_free(data);
4722}
4723
4724
4725void tls_connection_set_success_data_resumed(struct tls_connection *conn)
4726{
4727 wpa_printf(MSG_DEBUG,
4728 "OpenSSL: Success data accepted for resumed session");
4729 conn->success_data = 1;
4730}
4731
4732
4733const struct wpabuf *
4734tls_connection_get_success_data(struct tls_connection *conn)
4735{
4736 SSL_SESSION *sess;
4737
4738 if (tls_ex_idx_session < 0 ||
4739 !(sess = SSL_get_session(conn->ssl)))
4740 return NULL;
4741 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4742}
4743
4744
4745void tls_connection_remove_session(struct tls_connection *conn)
4746{
4747 SSL_SESSION *sess;
4748
4749 sess = SSL_get_session(conn->ssl);
4750 if (!sess)
4751 return;
4752
4753 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
4754 wpa_printf(MSG_DEBUG,
4755 "OpenSSL: Session was not cached");
4756 else
4757 wpa_printf(MSG_DEBUG,
4758 "OpenSSL: Removed cached session to disable session resumption");
4759}