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