blob: 1abc28233c0f1c71de9934776bd9b2c234d47535 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * SSL/TLS interface functions for OpenSSL
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#ifndef CONFIG_SMARTCARD
12#ifndef OPENSSL_NO_ENGINE
Kenny Rootdb3c5a42012-03-20 17:00:47 -070013#ifndef ANDROID
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#define OPENSSL_NO_ENGINE
15#endif
16#endif
Kenny Rootdb3c5a42012-03-20 17:00:47 -070017#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018
19#include <openssl/ssl.h>
20#include <openssl/err.h>
Dmitry Shmidt849734c2016-05-27 09:59:01 -070021#include <openssl/opensslv.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <openssl/pkcs12.h>
23#include <openssl/x509v3.h>
24#ifndef OPENSSL_NO_ENGINE
25#include <openssl/engine.h>
26#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080027#ifndef OPENSSL_NO_DSA
28#include <openssl/dsa.h>
29#endif
30#ifndef OPENSSL_NO_DH
31#include <openssl/dh.h>
32#endif
33
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "common.h"
35#include "crypto.h"
Dmitry Shmidtaf9da312015-04-03 10:03:11 -070036#include "sha1.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080037#include "sha256.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "tls.h"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080039#include "tls_openssl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
Dmitry Shmidt849734c2016-05-27 09:59:01 -070041#if !defined(CONFIG_FIPS) && \
42 (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
43 defined(EAP_SERVER_FAST))
44#define OPENSSL_NEED_EAP_FAST_PRF
45#endif
46
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070047#if defined(OPENSSL_IS_BORINGSSL)
48/* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
49typedef size_t stack_index_t;
50#else
51typedef int stack_index_t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052#endif
53
Dmitry Shmidt34af3062013-07-11 10:46:32 -070054#ifdef SSL_set_tlsext_status_type
55#ifndef OPENSSL_NO_TLSEXT
56#define HAVE_OCSP
57#include <openssl/ocsp.h>
58#endif /* OPENSSL_NO_TLSEXT */
59#endif /* SSL_set_tlsext_status_type */
60
Dmitry Shmidt849734c2016-05-27 09:59:01 -070061#if (OPENSSL_VERSION_NUMBER < 0x10100000L || \
62 defined(LIBRESSL_VERSION_NUMBER)) && \
63 !defined(BORINGSSL_API_VERSION)
Dmitry Shmidtde47be72016-01-07 12:52:55 -080064/*
65 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
Dmitry Shmidt849734c2016-05-27 09:59:01 -070066 * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for
67 * older versions.
Dmitry Shmidtde47be72016-01-07 12:52:55 -080068 */
69
70static size_t SSL_get_client_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->client_random, SSL3_RANDOM_SIZE);
76 return SSL3_RANDOM_SIZE;
77}
78
79
80static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out,
81 size_t outlen)
82{
83 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
84 return 0;
85 os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE);
86 return SSL3_RANDOM_SIZE;
87}
88
89
Dmitry Shmidt849734c2016-05-27 09:59:01 -070090#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtde47be72016-01-07 12:52:55 -080091static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
92 unsigned char *out, size_t outlen)
93{
94 if (!session || session->master_key_length < 0 ||
95 (size_t) session->master_key_length > outlen)
96 return 0;
97 if ((size_t) session->master_key_length < outlen)
98 outlen = session->master_key_length;
99 os_memcpy(out, session->master_key, outlen);
100 return outlen;
101}
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700102#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800103
104#endif
105
Dmitry Shmidtff079172013-11-08 14:10:30 -0800106#ifdef ANDROID
107#include <openssl/pem.h>
108#include <keystore/keystore_get.h>
109
110static BIO * BIO_from_keystore(const char *key)
111{
112 BIO *bio = NULL;
113 uint8_t *value = NULL;
114 int length = keystore_get(key, strlen(key), &value);
115 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
116 BIO_write(bio, value, length);
117 free(value);
118 return bio;
119}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800120
121
122static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *key_alias)
123{
124 BIO *bio = BIO_from_keystore(key_alias);
125 STACK_OF(X509_INFO) *stack = NULL;
126 stack_index_t i;
127
128 if (bio) {
129 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
130 BIO_free(bio);
131 }
132
133 if (!stack) {
134 wpa_printf(MSG_WARNING, "TLS: Failed to parse certificate: %s",
135 key_alias);
136 return -1;
137 }
138
139 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
140 X509_INFO *info = sk_X509_INFO_value(stack, i);
141
142 if (info->x509)
143 X509_STORE_add_cert(ctx, info->x509);
144 if (info->crl)
145 X509_STORE_add_crl(ctx, info->crl);
146 }
147
148 sk_X509_INFO_pop_free(stack, X509_INFO_free);
149
150 return 0;
151}
152
153
154static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
155 const char *encoded_key_alias)
156{
157 int rc = -1;
158 int len = os_strlen(encoded_key_alias);
159 unsigned char *decoded_alias;
160
161 if (len & 1) {
162 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
163 encoded_key_alias);
164 return rc;
165 }
166
167 decoded_alias = os_malloc(len / 2 + 1);
168 if (decoded_alias) {
169 if (!hexstr2bin(encoded_key_alias, decoded_alias, len / 2)) {
170 decoded_alias[len / 2] = '\0';
171 rc = tls_add_ca_from_keystore(
172 ctx, (const char *) decoded_alias);
173 }
174 os_free(decoded_alias);
175 }
176
177 return rc;
178}
179
Dmitry Shmidtff079172013-11-08 14:10:30 -0800180#endif /* ANDROID */
181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182static int tls_openssl_ref_count = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800183static int tls_ex_idx_session = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700185struct tls_context {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186 void (*event_cb)(void *ctx, enum tls_event ev,
187 union tls_event_data *data);
188 void *cb_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189 int cert_in_cb;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700190 char *ocsp_stapling_response;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191};
192
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700193static struct tls_context *tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194
195
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800196struct tls_data {
197 SSL_CTX *ssl;
198 unsigned int tls_session_lifetime;
199};
200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700202 struct tls_context *context;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800203 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 SSL *ssl;
205 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700206#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 ENGINE *engine; /* functional reference to the engine */
208 EVP_PKEY *private_key; /* the private key if using engine */
209#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800210 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 int read_alerts, write_alerts, failed;
212
213 tls_session_ticket_cb session_ticket_cb;
214 void *session_ticket_cb_ctx;
215
216 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
217 u8 *session_ticket;
218 size_t session_ticket_len;
219
220 unsigned int ca_cert_verify:1;
221 unsigned int cert_probe:1;
222 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300223 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800224 unsigned int success_data:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225
226 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700227
228 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700229
230 X509 *peer_cert;
231 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800232 X509 *peer_issuer_issuer;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800233
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800234 unsigned char client_random[SSL3_RANDOM_SIZE];
235 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236};
237
238
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700239static struct tls_context * tls_context_new(const struct tls_config *conf)
240{
241 struct tls_context *context = os_zalloc(sizeof(*context));
242 if (context == NULL)
243 return NULL;
244 if (conf) {
245 context->event_cb = conf->event_cb;
246 context->cb_ctx = conf->cb_ctx;
247 context->cert_in_cb = conf->cert_in_cb;
248 }
249 return context;
250}
251
252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253#ifdef CONFIG_NO_STDOUT_DEBUG
254
255static void _tls_show_errors(void)
256{
257 unsigned long err;
258
259 while ((err = ERR_get_error())) {
260 /* Just ignore the errors, since stdout is disabled */
261 }
262}
263#define tls_show_errors(l, f, t) _tls_show_errors()
264
265#else /* CONFIG_NO_STDOUT_DEBUG */
266
267static void tls_show_errors(int level, const char *func, const char *txt)
268{
269 unsigned long err;
270
271 wpa_printf(level, "OpenSSL: %s - %s %s",
272 func, txt, ERR_error_string(ERR_get_error(), NULL));
273
274 while ((err = ERR_get_error())) {
275 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
276 ERR_error_string(err, NULL));
277 }
278}
279
280#endif /* CONFIG_NO_STDOUT_DEBUG */
281
282
283#ifdef CONFIG_NATIVE_WINDOWS
284
285/* Windows CryptoAPI and access to certificate stores */
286#include <wincrypt.h>
287
288#ifdef __MINGW32_VERSION
289/*
290 * MinGW does not yet include all the needed definitions for CryptoAPI, so
291 * define here whatever extra is needed.
292 */
293#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
294#define CERT_STORE_READONLY_FLAG 0x00008000
295#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
296
297#endif /* __MINGW32_VERSION */
298
299
300struct cryptoapi_rsa_data {
301 const CERT_CONTEXT *cert;
302 HCRYPTPROV crypt_prov;
303 DWORD key_spec;
304 BOOL free_crypt_prov;
305};
306
307
308static void cryptoapi_error(const char *msg)
309{
310 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
311 msg, (unsigned int) GetLastError());
312}
313
314
315static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
316 unsigned char *to, RSA *rsa, int padding)
317{
318 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
319 return 0;
320}
321
322
323static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
324 unsigned char *to, RSA *rsa, int padding)
325{
326 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
327 return 0;
328}
329
330
331static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
332 unsigned char *to, RSA *rsa, int padding)
333{
334 struct cryptoapi_rsa_data *priv =
335 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
336 HCRYPTHASH hash;
337 DWORD hash_size, len, i;
338 unsigned char *buf = NULL;
339 int ret = 0;
340
341 if (priv == NULL) {
342 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
343 ERR_R_PASSED_NULL_PARAMETER);
344 return 0;
345 }
346
347 if (padding != RSA_PKCS1_PADDING) {
348 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
349 RSA_R_UNKNOWN_PADDING_TYPE);
350 return 0;
351 }
352
353 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
354 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
355 __func__);
356 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
357 RSA_R_INVALID_MESSAGE_LENGTH);
358 return 0;
359 }
360
361 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
362 {
363 cryptoapi_error("CryptCreateHash failed");
364 return 0;
365 }
366
367 len = sizeof(hash_size);
368 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
369 0)) {
370 cryptoapi_error("CryptGetHashParam failed");
371 goto err;
372 }
373
374 if ((int) hash_size != flen) {
375 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
376 (unsigned) hash_size, flen);
377 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
378 RSA_R_INVALID_MESSAGE_LENGTH);
379 goto err;
380 }
381 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
382 cryptoapi_error("CryptSetHashParam failed");
383 goto err;
384 }
385
386 len = RSA_size(rsa);
387 buf = os_malloc(len);
388 if (buf == NULL) {
389 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
390 goto err;
391 }
392
393 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
394 cryptoapi_error("CryptSignHash failed");
395 goto err;
396 }
397
398 for (i = 0; i < len; i++)
399 to[i] = buf[len - i - 1];
400 ret = len;
401
402err:
403 os_free(buf);
404 CryptDestroyHash(hash);
405
406 return ret;
407}
408
409
410static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
411 unsigned char *to, RSA *rsa, int padding)
412{
413 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
414 return 0;
415}
416
417
418static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
419{
420 if (priv == NULL)
421 return;
422 if (priv->crypt_prov && priv->free_crypt_prov)
423 CryptReleaseContext(priv->crypt_prov, 0);
424 if (priv->cert)
425 CertFreeCertificateContext(priv->cert);
426 os_free(priv);
427}
428
429
430static int cryptoapi_finish(RSA *rsa)
431{
432 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
433 os_free((void *) rsa->meth);
434 rsa->meth = NULL;
435 return 1;
436}
437
438
439static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
440{
441 HCERTSTORE cs;
442 const CERT_CONTEXT *ret = NULL;
443
444 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
445 store | CERT_STORE_OPEN_EXISTING_FLAG |
446 CERT_STORE_READONLY_FLAG, L"MY");
447 if (cs == NULL) {
448 cryptoapi_error("Failed to open 'My system store'");
449 return NULL;
450 }
451
452 if (strncmp(name, "cert://", 7) == 0) {
453 unsigned short wbuf[255];
454 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
455 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
456 PKCS_7_ASN_ENCODING,
457 0, CERT_FIND_SUBJECT_STR,
458 wbuf, NULL);
459 } else if (strncmp(name, "hash://", 7) == 0) {
460 CRYPT_HASH_BLOB blob;
461 int len;
462 const char *hash = name + 7;
463 unsigned char *buf;
464
465 len = os_strlen(hash) / 2;
466 buf = os_malloc(len);
467 if (buf && hexstr2bin(hash, buf, len) == 0) {
468 blob.cbData = len;
469 blob.pbData = buf;
470 ret = CertFindCertificateInStore(cs,
471 X509_ASN_ENCODING |
472 PKCS_7_ASN_ENCODING,
473 0, CERT_FIND_HASH,
474 &blob, NULL);
475 }
476 os_free(buf);
477 }
478
479 CertCloseStore(cs, 0);
480
481 return ret;
482}
483
484
485static int tls_cryptoapi_cert(SSL *ssl, const char *name)
486{
487 X509 *cert = NULL;
488 RSA *rsa = NULL, *pub_rsa;
489 struct cryptoapi_rsa_data *priv;
490 RSA_METHOD *rsa_meth;
491
492 if (name == NULL ||
493 (strncmp(name, "cert://", 7) != 0 &&
494 strncmp(name, "hash://", 7) != 0))
495 return -1;
496
497 priv = os_zalloc(sizeof(*priv));
498 rsa_meth = os_zalloc(sizeof(*rsa_meth));
499 if (priv == NULL || rsa_meth == NULL) {
500 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
501 "for CryptoAPI RSA method");
502 os_free(priv);
503 os_free(rsa_meth);
504 return -1;
505 }
506
507 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
508 if (priv->cert == NULL) {
509 priv->cert = cryptoapi_find_cert(
510 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
511 }
512 if (priv->cert == NULL) {
513 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
514 "'%s'", name);
515 goto err;
516 }
517
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800518 cert = d2i_X509(NULL,
519 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520 priv->cert->cbCertEncoded);
521 if (cert == NULL) {
522 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
523 "encoding");
524 goto err;
525 }
526
527 if (!CryptAcquireCertificatePrivateKey(priv->cert,
528 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
529 NULL, &priv->crypt_prov,
530 &priv->key_spec,
531 &priv->free_crypt_prov)) {
532 cryptoapi_error("Failed to acquire a private key for the "
533 "certificate");
534 goto err;
535 }
536
537 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
538 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
539 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
540 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
541 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
542 rsa_meth->finish = cryptoapi_finish;
543 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
544 rsa_meth->app_data = (char *) priv;
545
546 rsa = RSA_new();
547 if (rsa == NULL) {
548 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
549 ERR_R_MALLOC_FAILURE);
550 goto err;
551 }
552
553 if (!SSL_use_certificate(ssl, cert)) {
554 RSA_free(rsa);
555 rsa = NULL;
556 goto err;
557 }
558 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
559 X509_free(cert);
560 cert = NULL;
561
562 rsa->n = BN_dup(pub_rsa->n);
563 rsa->e = BN_dup(pub_rsa->e);
564 if (!RSA_set_method(rsa, rsa_meth))
565 goto err;
566
567 if (!SSL_use_RSAPrivateKey(ssl, rsa))
568 goto err;
569 RSA_free(rsa);
570
571 return 0;
572
573err:
574 if (cert)
575 X509_free(cert);
576 if (rsa)
577 RSA_free(rsa);
578 else {
579 os_free(rsa_meth);
580 cryptoapi_free_data(priv);
581 }
582 return -1;
583}
584
585
586static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
587{
588 HCERTSTORE cs;
589 PCCERT_CONTEXT ctx = NULL;
590 X509 *cert;
591 char buf[128];
592 const char *store;
593#ifdef UNICODE
594 WCHAR *wstore;
595#endif /* UNICODE */
596
597 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
598 return -1;
599
600 store = name + 13;
601#ifdef UNICODE
602 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
603 if (wstore == NULL)
604 return -1;
605 wsprintf(wstore, L"%S", store);
606 cs = CertOpenSystemStore(0, wstore);
607 os_free(wstore);
608#else /* UNICODE */
609 cs = CertOpenSystemStore(0, store);
610#endif /* UNICODE */
611 if (cs == NULL) {
612 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
613 "'%s': error=%d", __func__, store,
614 (int) GetLastError());
615 return -1;
616 }
617
618 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800619 cert = d2i_X509(NULL,
620 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 ctx->cbCertEncoded);
622 if (cert == NULL) {
623 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
624 "X509 DER encoding for CA cert");
625 continue;
626 }
627
628 X509_NAME_oneline(X509_get_subject_name(cert), buf,
629 sizeof(buf));
630 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
631 "system certificate store: subject='%s'", buf);
632
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700633 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
634 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 tls_show_errors(MSG_WARNING, __func__,
636 "Failed to add ca_cert to OpenSSL "
637 "certificate store");
638 }
639
640 X509_free(cert);
641 }
642
643 if (!CertCloseStore(cs, 0)) {
644 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
645 "'%s': error=%d", __func__, name + 13,
646 (int) GetLastError());
647 }
648
649 return 0;
650}
651
652
653#else /* CONFIG_NATIVE_WINDOWS */
654
655static int tls_cryptoapi_cert(SSL *ssl, const char *name)
656{
657 return -1;
658}
659
660#endif /* CONFIG_NATIVE_WINDOWS */
661
662
663static void ssl_info_cb(const SSL *ssl, int where, int ret)
664{
665 const char *str;
666 int w;
667
668 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
669 w = where & ~SSL_ST_MASK;
670 if (w & SSL_ST_CONNECT)
671 str = "SSL_connect";
672 else if (w & SSL_ST_ACCEPT)
673 str = "SSL_accept";
674 else
675 str = "undefined";
676
677 if (where & SSL_CB_LOOP) {
678 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
679 str, SSL_state_string_long(ssl));
680 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700681 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
683 where & SSL_CB_READ ?
684 "read (remote end reported an error)" :
685 "write (local SSL3 detected an error)",
686 SSL_alert_type_string_long(ret),
687 SSL_alert_desc_string_long(ret));
688 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 if (where & SSL_CB_READ)
690 conn->read_alerts++;
691 else
692 conn->write_alerts++;
693 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700694 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700695 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700696 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700697 os_memset(&ev, 0, sizeof(ev));
698 ev.alert.is_local = !(where & SSL_CB_READ);
699 ev.alert.type = SSL_alert_type_string_long(ret);
700 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700701 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700702 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 } else if (where & SSL_CB_EXIT && ret <= 0) {
704 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
705 str, ret == 0 ? "failed" : "error",
706 SSL_state_string_long(ssl));
707 }
708}
709
710
711#ifndef OPENSSL_NO_ENGINE
712/**
713 * tls_engine_load_dynamic_generic - load any openssl engine
714 * @pre: an array of commands and values that load an engine initialized
715 * in the engine specific function
716 * @post: an array of commands and values that initialize an already loaded
717 * engine (or %NULL if not required)
718 * @id: the engine id of the engine to load (only required if post is not %NULL
719 *
720 * This function is a generic function that loads any openssl engine.
721 *
722 * Returns: 0 on success, -1 on failure
723 */
724static int tls_engine_load_dynamic_generic(const char *pre[],
725 const char *post[], const char *id)
726{
727 ENGINE *engine;
728 const char *dynamic_id = "dynamic";
729
730 engine = ENGINE_by_id(id);
731 if (engine) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
733 "available", id);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700734 /*
735 * If it was auto-loaded by ENGINE_by_id() we might still
736 * need to tell it which PKCS#11 module to use in legacy
737 * (non-p11-kit) environments. Do so now; even if it was
738 * properly initialised before, setting it again will be
739 * harmless.
740 */
741 goto found;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 }
743 ERR_clear_error();
744
745 engine = ENGINE_by_id(dynamic_id);
746 if (engine == NULL) {
747 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
748 dynamic_id,
749 ERR_error_string(ERR_get_error(), NULL));
750 return -1;
751 }
752
753 /* Perform the pre commands. This will load the engine. */
754 while (pre && pre[0]) {
755 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
756 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
757 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
758 "%s %s [%s]", pre[0], pre[1],
759 ERR_error_string(ERR_get_error(), NULL));
760 ENGINE_free(engine);
761 return -1;
762 }
763 pre += 2;
764 }
765
766 /*
767 * Free the reference to the "dynamic" engine. The loaded engine can
768 * now be looked up using ENGINE_by_id().
769 */
770 ENGINE_free(engine);
771
772 engine = ENGINE_by_id(id);
773 if (engine == NULL) {
774 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
775 id, ERR_error_string(ERR_get_error(), NULL));
776 return -1;
777 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700778 found:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 while (post && post[0]) {
780 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
781 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
782 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
783 " %s %s [%s]", post[0], post[1],
784 ERR_error_string(ERR_get_error(), NULL));
785 ENGINE_remove(engine);
786 ENGINE_free(engine);
787 return -1;
788 }
789 post += 2;
790 }
791 ENGINE_free(engine);
792
793 return 0;
794}
795
796
797/**
798 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
799 * @pkcs11_so_path: pksc11_so_path from the configuration
800 * @pcks11_module_path: pkcs11_module_path from the configuration
801 */
802static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
803 const char *pkcs11_module_path)
804{
805 char *engine_id = "pkcs11";
806 const char *pre_cmd[] = {
807 "SO_PATH", NULL /* pkcs11_so_path */,
808 "ID", NULL /* engine_id */,
809 "LIST_ADD", "1",
810 /* "NO_VCHECK", "1", */
811 "LOAD", NULL,
812 NULL, NULL
813 };
814 const char *post_cmd[] = {
815 "MODULE_PATH", NULL /* pkcs11_module_path */,
816 NULL, NULL
817 };
818
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800819 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 return 0;
821
822 pre_cmd[1] = pkcs11_so_path;
823 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800824 if (pkcs11_module_path)
825 post_cmd[1] = pkcs11_module_path;
826 else
827 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828
829 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
830 pkcs11_so_path);
831
832 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
833}
834
835
836/**
837 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
838 * @opensc_so_path: opensc_so_path from the configuration
839 */
840static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
841{
842 char *engine_id = "opensc";
843 const char *pre_cmd[] = {
844 "SO_PATH", NULL /* opensc_so_path */,
845 "ID", NULL /* engine_id */,
846 "LIST_ADD", "1",
847 "LOAD", NULL,
848 NULL, NULL
849 };
850
851 if (!opensc_so_path)
852 return 0;
853
854 pre_cmd[1] = opensc_so_path;
855 pre_cmd[3] = engine_id;
856
857 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
858 opensc_so_path);
859
860 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
861}
862#endif /* OPENSSL_NO_ENGINE */
863
864
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800865static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
866{
867 struct wpabuf *buf;
868
869 if (tls_ex_idx_session < 0)
870 return;
871 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
872 if (!buf)
873 return;
874 wpa_printf(MSG_DEBUG,
875 "OpenSSL: Free application session data %p (sess %p)",
876 buf, sess);
877 wpabuf_free(buf);
878
879 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
880}
881
882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883void * tls_init(const struct tls_config *conf)
884{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800885 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700887 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889
890 if (tls_openssl_ref_count == 0) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700891 tls_global = context = tls_context_new(conf);
892 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894#ifdef CONFIG_FIPS
895#ifdef OPENSSL_FIPS
896 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800897 static int fips_enabled = 0;
898
899 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
901 "mode");
902 ERR_load_crypto_strings();
903 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700904 os_free(tls_global);
905 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800907 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800909 fips_enabled = 1;
910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 }
912#else /* OPENSSL_FIPS */
913 if (conf && conf->fips_mode) {
914 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
915 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700916 os_free(tls_global);
917 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 return NULL;
919 }
920#endif /* OPENSSL_FIPS */
921#endif /* CONFIG_FIPS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800922#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 SSL_load_error_strings();
924 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800925#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 EVP_add_digest(EVP_sha256());
927#endif /* OPENSSL_NO_SHA256 */
928 /* TODO: if /dev/urandom is available, PRNG is seeded
929 * automatically. If this is not the case, random data should
930 * be added here. */
931
932#ifdef PKCS12_FUNCS
933#ifndef OPENSSL_NO_RC2
934 /*
935 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
936 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
937 * versions, but it looks like OpenSSL 1.0.0 does not do that
938 * anymore.
939 */
940 EVP_add_cipher(EVP_rc2_40_cbc());
941#endif /* OPENSSL_NO_RC2 */
942 PKCS12_PBE_add();
943#endif /* PKCS12_FUNCS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800944#endif /* < 1.1.0 */
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700945 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700946 context = tls_context_new(conf);
947 if (context == NULL)
948 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 }
950 tls_openssl_ref_count++;
951
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800952 data = os_zalloc(sizeof(*data));
953 if (data)
954 ssl = SSL_CTX_new(SSLv23_method());
955 else
956 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700957 if (ssl == NULL) {
958 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700959 if (context != tls_global)
960 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700961 if (tls_openssl_ref_count == 0) {
962 os_free(tls_global);
963 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700964 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800965 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700967 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800968 data->ssl = ssl;
969 if (conf)
970 data->tls_session_lifetime = conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800972 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
973 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700976 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800977 if (data->tls_session_lifetime > 0) {
978 SSL_CTX_set_quiet_shutdown(ssl, 1);
979 /*
980 * Set default context here. In practice, this will be replaced
981 * by the per-EAP method context in tls_connection_set_verify().
982 */
983 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
984 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
985 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
986 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
987 } else {
988 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
989 }
990
991 if (tls_ex_idx_session < 0) {
992 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
993 0, NULL, NULL, NULL, NULL);
994 if (tls_ex_idx_session < 0) {
995 tls_deinit(data);
996 return NULL;
997 }
998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999
1000#ifndef OPENSSL_NO_ENGINE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001001 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
1002 ERR_load_ENGINE_strings();
1003 ENGINE_load_dynamic();
1004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 if (conf &&
1006 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
1007 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
1009 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
1010 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001011 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 return NULL;
1013 }
1014 }
1015#endif /* OPENSSL_NO_ENGINE */
1016
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001017 if (conf && conf->openssl_ciphers)
1018 ciphers = conf->openssl_ciphers;
1019 else
1020 ciphers = "DEFAULT:!EXP:!LOW";
1021 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1022 wpa_printf(MSG_ERROR,
1023 "OpenSSL: Failed to set cipher string '%s'",
1024 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001025 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001026 return NULL;
1027 }
1028
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001029 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030}
1031
1032
1033void tls_deinit(void *ssl_ctx)
1034{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001035 struct tls_data *data = ssl_ctx;
1036 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001037 struct tls_context *context = SSL_CTX_get_app_data(ssl);
1038 if (context != tls_global)
1039 os_free(context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001040 if (data->tls_session_lifetime > 0)
1041 SSL_CTX_flush_sessions(ssl, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 SSL_CTX_free(ssl);
1043
1044 tls_openssl_ref_count--;
1045 if (tls_openssl_ref_count == 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001046#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047#ifndef OPENSSL_NO_ENGINE
1048 ENGINE_cleanup();
1049#endif /* OPENSSL_NO_ENGINE */
1050 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001051 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 ERR_free_strings();
1053 EVP_cleanup();
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001054#endif /* < 1.1.0 */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001055 os_free(tls_global->ocsp_stapling_response);
1056 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 os_free(tls_global);
1058 tls_global = NULL;
1059 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001060
1061 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062}
1063
1064
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001065#ifndef OPENSSL_NO_ENGINE
1066
1067/* Cryptoki return values */
1068#define CKR_PIN_INCORRECT 0x000000a0
1069#define CKR_PIN_INVALID 0x000000a1
1070#define CKR_PIN_LEN_RANGE 0x000000a2
1071
1072/* libp11 */
1073#define ERR_LIB_PKCS11 ERR_LIB_USER
1074
1075static int tls_is_pin_error(unsigned int err)
1076{
1077 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1078 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1079 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1080 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1081}
1082
1083#endif /* OPENSSL_NO_ENGINE */
1084
1085
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001086#ifdef ANDROID
1087/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1088EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1089#endif /* ANDROID */
1090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1092 const char *pin, const char *key_id,
1093 const char *cert_id, const char *ca_cert_id)
1094{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001095#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1096#if !defined(OPENSSL_NO_ENGINE)
1097#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1098#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001099 if (!key_id)
1100 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001101 conn->engine = NULL;
1102 conn->private_key = EVP_PKEY_from_keystore(key_id);
1103 if (!conn->private_key) {
1104 wpa_printf(MSG_ERROR,
1105 "ENGINE: cannot load private key with id '%s' [%s]",
1106 key_id,
1107 ERR_error_string(ERR_get_error(), NULL));
1108 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1109 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001110#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112#ifndef OPENSSL_NO_ENGINE
1113 int ret = -1;
1114 if (engine_id == NULL) {
1115 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1116 return -1;
1117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118
1119 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001120#ifdef ANDROID
1121 ENGINE_load_dynamic();
1122#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 conn->engine = ENGINE_by_id(engine_id);
1124 if (!conn->engine) {
1125 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1126 engine_id, ERR_error_string(ERR_get_error(), NULL));
1127 goto err;
1128 }
1129 if (ENGINE_init(conn->engine) != 1) {
1130 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1131 "(engine: %s) [%s]", engine_id,
1132 ERR_error_string(ERR_get_error(), NULL));
1133 goto err;
1134 }
1135 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1136
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001137#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001138 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1140 ERR_error_string(ERR_get_error(), NULL));
1141 goto err;
1142 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001143#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001144 if (key_id) {
1145 /*
1146 * Ensure that the ENGINE does not attempt to use the OpenSSL
1147 * UI system to obtain a PIN, if we didn't provide one.
1148 */
1149 struct {
1150 const void *password;
1151 const char *prompt_info;
1152 } key_cb = { "", NULL };
1153
1154 /* load private key first in-case PIN is required for cert */
1155 conn->private_key = ENGINE_load_private_key(conn->engine,
1156 key_id, NULL,
1157 &key_cb);
1158 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001159 unsigned long err = ERR_get_error();
1160
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001161 wpa_printf(MSG_ERROR,
1162 "ENGINE: cannot load private key with id '%s' [%s]",
1163 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001164 ERR_error_string(err, NULL));
1165 if (tls_is_pin_error(err))
1166 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1167 else
1168 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001169 goto err;
1170 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 }
1172
1173 /* handle a certificate and/or CA certificate */
1174 if (cert_id || ca_cert_id) {
1175 const char *cmd_name = "LOAD_CERT_CTRL";
1176
1177 /* test if the engine supports a LOAD_CERT_CTRL */
1178 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1179 0, (void *)cmd_name, NULL)) {
1180 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1181 " loading certificates");
1182 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1183 goto err;
1184 }
1185 }
1186
1187 return 0;
1188
1189err:
1190 if (conn->engine) {
1191 ENGINE_free(conn->engine);
1192 conn->engine = NULL;
1193 }
1194
1195 if (conn->private_key) {
1196 EVP_PKEY_free(conn->private_key);
1197 conn->private_key = NULL;
1198 }
1199
1200 return ret;
1201#else /* OPENSSL_NO_ENGINE */
1202 return 0;
1203#endif /* OPENSSL_NO_ENGINE */
1204}
1205
1206
1207static void tls_engine_deinit(struct tls_connection *conn)
1208{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001209#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1211 if (conn->private_key) {
1212 EVP_PKEY_free(conn->private_key);
1213 conn->private_key = NULL;
1214 }
1215 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001216#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001218#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 conn->engine = NULL;
1220 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001221#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222}
1223
1224
1225int tls_get_errors(void *ssl_ctx)
1226{
1227 int count = 0;
1228 unsigned long err;
1229
1230 while ((err = ERR_get_error())) {
1231 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1232 ERR_error_string(err, NULL));
1233 count++;
1234 }
1235
1236 return count;
1237}
1238
Jouni Malinen26af48b2014-04-09 13:02:53 +03001239
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001240static const char * openssl_content_type(int content_type)
1241{
1242 switch (content_type) {
1243 case 20:
1244 return "change cipher spec";
1245 case 21:
1246 return "alert";
1247 case 22:
1248 return "handshake";
1249 case 23:
1250 return "application data";
1251 case 24:
1252 return "heartbeat";
1253 case 256:
1254 return "TLS header info"; /* pseudo content type */
1255 default:
1256 return "?";
1257 }
1258}
1259
1260
1261static const char * openssl_handshake_type(int content_type, const u8 *buf,
1262 size_t len)
1263{
1264 if (content_type != 22 || !buf || len == 0)
1265 return "";
1266 switch (buf[0]) {
1267 case 0:
1268 return "hello request";
1269 case 1:
1270 return "client hello";
1271 case 2:
1272 return "server hello";
1273 case 4:
1274 return "new session ticket";
1275 case 11:
1276 return "certificate";
1277 case 12:
1278 return "server key exchange";
1279 case 13:
1280 return "certificate request";
1281 case 14:
1282 return "server hello done";
1283 case 15:
1284 return "certificate verify";
1285 case 16:
1286 return "client key exchange";
1287 case 20:
1288 return "finished";
1289 case 21:
1290 return "certificate url";
1291 case 22:
1292 return "certificate status";
1293 default:
1294 return "?";
1295 }
1296}
1297
1298
Jouni Malinen26af48b2014-04-09 13:02:53 +03001299static void tls_msg_cb(int write_p, int version, int content_type,
1300 const void *buf, size_t len, SSL *ssl, void *arg)
1301{
1302 struct tls_connection *conn = arg;
1303 const u8 *pos = buf;
1304
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001305 if (write_p == 2) {
1306 wpa_printf(MSG_DEBUG,
1307 "OpenSSL: session ver=0x%x content_type=%d",
1308 version, content_type);
1309 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1310 return;
1311 }
1312
1313 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1314 write_p ? "TX" : "RX", version, content_type,
1315 openssl_content_type(content_type),
1316 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001317 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1318 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1319 size_t payload_len = WPA_GET_BE16(pos + 1);
1320 if (payload_len + 3 > len) {
1321 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1322 conn->invalid_hb_used = 1;
1323 }
1324 }
1325}
1326
1327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328struct tls_connection * tls_connection_init(void *ssl_ctx)
1329{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001330 struct tls_data *data = ssl_ctx;
1331 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 struct tls_connection *conn;
1333 long options;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001334 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335
1336 conn = os_zalloc(sizeof(*conn));
1337 if (conn == NULL)
1338 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001339 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 conn->ssl = SSL_new(ssl);
1341 if (conn->ssl == NULL) {
1342 tls_show_errors(MSG_INFO, __func__,
1343 "Failed to initialize new SSL connection");
1344 os_free(conn);
1345 return NULL;
1346 }
1347
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001348 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001350 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1351 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1353 SSL_OP_SINGLE_DH_USE;
1354#ifdef SSL_OP_NO_COMPRESSION
1355 options |= SSL_OP_NO_COMPRESSION;
1356#endif /* SSL_OP_NO_COMPRESSION */
1357 SSL_set_options(conn->ssl, options);
1358
1359 conn->ssl_in = BIO_new(BIO_s_mem());
1360 if (!conn->ssl_in) {
1361 tls_show_errors(MSG_INFO, __func__,
1362 "Failed to create a new BIO for ssl_in");
1363 SSL_free(conn->ssl);
1364 os_free(conn);
1365 return NULL;
1366 }
1367
1368 conn->ssl_out = BIO_new(BIO_s_mem());
1369 if (!conn->ssl_out) {
1370 tls_show_errors(MSG_INFO, __func__,
1371 "Failed to create a new BIO for ssl_out");
1372 SSL_free(conn->ssl);
1373 BIO_free(conn->ssl_in);
1374 os_free(conn);
1375 return NULL;
1376 }
1377
1378 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1379
1380 return conn;
1381}
1382
1383
1384void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1385{
1386 if (conn == NULL)
1387 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001388 if (conn->success_data) {
1389 /*
1390 * Make sure ssl_clear_bad_session() does not remove this
1391 * session.
1392 */
1393 SSL_set_quiet_shutdown(conn->ssl, 1);
1394 SSL_shutdown(conn->ssl);
1395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 SSL_free(conn->ssl);
1397 tls_engine_deinit(conn);
1398 os_free(conn->subject_match);
1399 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001400 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001401 os_free(conn->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 os_free(conn->session_ticket);
1403 os_free(conn);
1404}
1405
1406
1407int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1408{
1409 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1410}
1411
1412
1413int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1414{
1415 if (conn == NULL)
1416 return -1;
1417
1418 /* Shutdown previous TLS connection without notifying the peer
1419 * because the connection was already terminated in practice
1420 * and "close notify" shutdown alert would confuse AS. */
1421 SSL_set_quiet_shutdown(conn->ssl, 1);
1422 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001423 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424}
1425
1426
1427static int tls_match_altsubject_component(X509 *cert, int type,
1428 const char *value, size_t len)
1429{
1430 GENERAL_NAME *gen;
1431 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001432 int found = 0;
1433 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434
1435 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1436
1437 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1438 gen = sk_GENERAL_NAME_value(ext, i);
1439 if (gen->type != type)
1440 continue;
1441 if (os_strlen((char *) gen->d.ia5->data) == len &&
1442 os_memcmp(value, gen->d.ia5->data, len) == 0)
1443 found++;
1444 }
1445
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001446 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
1447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 return found;
1449}
1450
1451
1452static int tls_match_altsubject(X509 *cert, const char *match)
1453{
1454 int type;
1455 const char *pos, *end;
1456 size_t len;
1457
1458 pos = match;
1459 do {
1460 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1461 type = GEN_EMAIL;
1462 pos += 6;
1463 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1464 type = GEN_DNS;
1465 pos += 4;
1466 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1467 type = GEN_URI;
1468 pos += 4;
1469 } else {
1470 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1471 "match '%s'", pos);
1472 return 0;
1473 }
1474 end = os_strchr(pos, ';');
1475 while (end) {
1476 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1477 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1478 os_strncmp(end + 1, "URI:", 4) == 0)
1479 break;
1480 end = os_strchr(end + 1, ';');
1481 }
1482 if (end)
1483 len = end - pos;
1484 else
1485 len = os_strlen(pos);
1486 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1487 return 1;
1488 pos = end + 1;
1489 } while (end);
1490
1491 return 0;
1492}
1493
1494
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001495#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001496static int domain_suffix_match(const u8 *val, size_t len, const char *match,
1497 int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001498{
1499 size_t i, match_len;
1500
1501 /* Check for embedded nuls that could mess up suffix matching */
1502 for (i = 0; i < len; i++) {
1503 if (val[i] == '\0') {
1504 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1505 return 0;
1506 }
1507 }
1508
1509 match_len = os_strlen(match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001510 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001511 return 0;
1512
1513 if (os_strncasecmp((const char *) val + len - match_len, match,
1514 match_len) != 0)
1515 return 0; /* no match */
1516
1517 if (match_len == len)
1518 return 1; /* exact match */
1519
1520 if (val[len - match_len - 1] == '.')
1521 return 1; /* full label match completes suffix match */
1522
1523 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1524 return 0;
1525}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001526#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001527
1528
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001529static int tls_match_suffix(X509 *cert, const char *match, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001530{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001531#ifdef CONFIG_NATIVE_WINDOWS
1532 /* wincrypt.h has conflicting X509_NAME definition */
1533 return -1;
1534#else /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001535 GENERAL_NAME *gen;
1536 void *ext;
1537 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001538 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001539 int dns_name = 0;
1540 X509_NAME *name;
1541
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001542 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
1543 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001544
1545 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1546
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001547 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
1548 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001549 if (gen->type != GEN_DNS)
1550 continue;
1551 dns_name++;
1552 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
1553 gen->d.dNSName->data,
1554 gen->d.dNSName->length);
1555 if (domain_suffix_match(gen->d.dNSName->data,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001556 gen->d.dNSName->length, match, full) ==
1557 1) {
1558 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
1559 full ? "Match" : "Suffix match");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001560 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001561 return 1;
1562 }
1563 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001564 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001565
1566 if (dns_name) {
1567 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
1568 return 0;
1569 }
1570
1571 name = X509_get_subject_name(cert);
1572 i = -1;
1573 for (;;) {
1574 X509_NAME_ENTRY *e;
1575 ASN1_STRING *cn;
1576
1577 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
1578 if (i == -1)
1579 break;
1580 e = X509_NAME_get_entry(name, i);
1581 if (e == NULL)
1582 continue;
1583 cn = X509_NAME_ENTRY_get_data(e);
1584 if (cn == NULL)
1585 continue;
1586 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
1587 cn->data, cn->length);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001588 if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
1589 {
1590 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
1591 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001592 return 1;
1593 }
1594 }
1595
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001596 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
1597 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001598 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001599#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001600}
1601
1602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603static enum tls_fail_reason openssl_tls_fail_reason(int err)
1604{
1605 switch (err) {
1606 case X509_V_ERR_CERT_REVOKED:
1607 return TLS_FAIL_REVOKED;
1608 case X509_V_ERR_CERT_NOT_YET_VALID:
1609 case X509_V_ERR_CRL_NOT_YET_VALID:
1610 return TLS_FAIL_NOT_YET_VALID;
1611 case X509_V_ERR_CERT_HAS_EXPIRED:
1612 case X509_V_ERR_CRL_HAS_EXPIRED:
1613 return TLS_FAIL_EXPIRED;
1614 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1615 case X509_V_ERR_UNABLE_TO_GET_CRL:
1616 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1617 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1618 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1619 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1620 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1621 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1622 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1623 case X509_V_ERR_INVALID_CA:
1624 return TLS_FAIL_UNTRUSTED;
1625 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1626 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1627 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1628 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1629 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1630 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1631 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1632 case X509_V_ERR_CERT_UNTRUSTED:
1633 case X509_V_ERR_CERT_REJECTED:
1634 return TLS_FAIL_BAD_CERTIFICATE;
1635 default:
1636 return TLS_FAIL_UNSPECIFIED;
1637 }
1638}
1639
1640
1641static struct wpabuf * get_x509_cert(X509 *cert)
1642{
1643 struct wpabuf *buf;
1644 u8 *tmp;
1645
1646 int cert_len = i2d_X509(cert, NULL);
1647 if (cert_len <= 0)
1648 return NULL;
1649
1650 buf = wpabuf_alloc(cert_len);
1651 if (buf == NULL)
1652 return NULL;
1653
1654 tmp = wpabuf_put(buf, cert_len);
1655 i2d_X509(cert, &tmp);
1656 return buf;
1657}
1658
1659
1660static void openssl_tls_fail_event(struct tls_connection *conn,
1661 X509 *err_cert, int err, int depth,
1662 const char *subject, const char *err_str,
1663 enum tls_fail_reason reason)
1664{
1665 union tls_event_data ev;
1666 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001667 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001669 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 return;
1671
1672 cert = get_x509_cert(err_cert);
1673 os_memset(&ev, 0, sizeof(ev));
1674 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
1675 reason : openssl_tls_fail_reason(err);
1676 ev.cert_fail.depth = depth;
1677 ev.cert_fail.subject = subject;
1678 ev.cert_fail.reason_txt = err_str;
1679 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001680 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 wpabuf_free(cert);
1682}
1683
1684
1685static void openssl_tls_cert_event(struct tls_connection *conn,
1686 X509 *err_cert, int depth,
1687 const char *subject)
1688{
1689 struct wpabuf *cert = NULL;
1690 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001691 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001692 char *altsubject[TLS_MAX_ALT_SUBJECT];
1693 int alt, num_altsubject = 0;
1694 GENERAL_NAME *gen;
1695 void *ext;
1696 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697#ifdef CONFIG_SHA256
1698 u8 hash[32];
1699#endif /* CONFIG_SHA256 */
1700
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001701 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 return;
1703
1704 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001705 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
1706 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 cert = get_x509_cert(err_cert);
1708 ev.peer_cert.cert = cert;
1709 }
1710#ifdef CONFIG_SHA256
1711 if (cert) {
1712 const u8 *addr[1];
1713 size_t len[1];
1714 addr[0] = wpabuf_head(cert);
1715 len[0] = wpabuf_len(cert);
1716 if (sha256_vector(1, addr, len, hash) == 0) {
1717 ev.peer_cert.hash = hash;
1718 ev.peer_cert.hash_len = sizeof(hash);
1719 }
1720 }
1721#endif /* CONFIG_SHA256 */
1722 ev.peer_cert.depth = depth;
1723 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001724
1725 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
1726 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1727 char *pos;
1728
1729 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
1730 break;
1731 gen = sk_GENERAL_NAME_value(ext, i);
1732 if (gen->type != GEN_EMAIL &&
1733 gen->type != GEN_DNS &&
1734 gen->type != GEN_URI)
1735 continue;
1736
1737 pos = os_malloc(10 + gen->d.ia5->length + 1);
1738 if (pos == NULL)
1739 break;
1740 altsubject[num_altsubject++] = pos;
1741
1742 switch (gen->type) {
1743 case GEN_EMAIL:
1744 os_memcpy(pos, "EMAIL:", 6);
1745 pos += 6;
1746 break;
1747 case GEN_DNS:
1748 os_memcpy(pos, "DNS:", 4);
1749 pos += 4;
1750 break;
1751 case GEN_URI:
1752 os_memcpy(pos, "URI:", 4);
1753 pos += 4;
1754 break;
1755 }
1756
1757 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
1758 pos += gen->d.ia5->length;
1759 *pos = '\0';
1760 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001761 sk_GENERAL_NAME_pop_free(ext, GENERAL_NAME_free);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001762
1763 for (alt = 0; alt < num_altsubject; alt++)
1764 ev.peer_cert.altsubject[alt] = altsubject[alt];
1765 ev.peer_cert.num_altsubject = num_altsubject;
1766
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001767 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001769 for (alt = 0; alt < num_altsubject; alt++)
1770 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771}
1772
1773
1774static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1775{
1776 char buf[256];
1777 X509 *err_cert;
1778 int err, depth;
1779 SSL *ssl;
1780 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001781 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001782 char *match, *altmatch, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 const char *err_str;
1784
1785 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001786 if (!err_cert)
1787 return 0;
1788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 err = X509_STORE_CTX_get_error(x509_ctx);
1790 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1791 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1792 SSL_get_ex_data_X509_STORE_CTX_idx());
1793 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1794
1795 conn = SSL_get_app_data(ssl);
1796 if (conn == NULL)
1797 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001798
1799 if (depth == 0)
1800 conn->peer_cert = err_cert;
1801 else if (depth == 1)
1802 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001803 else if (depth == 2)
1804 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001805
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001806 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 match = conn->subject_match;
1808 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001809 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001810 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811
1812 if (!preverify_ok && !conn->ca_cert_verify)
1813 preverify_ok = 1;
1814 if (!preverify_ok && depth > 0 && conn->server_cert_only)
1815 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001816 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
1817 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
1818 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
1819 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
1820 "time mismatch");
1821 preverify_ok = 1;
1822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823
1824 err_str = X509_verify_cert_error_string(err);
1825
1826#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001827 /*
1828 * Do not require preverify_ok so we can explicity allow otherwise
1829 * invalid pinned server certificates.
1830 */
1831 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832 struct wpabuf *cert;
1833 cert = get_x509_cert(err_cert);
1834 if (!cert) {
1835 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
1836 "server certificate data");
1837 preverify_ok = 0;
1838 } else {
1839 u8 hash[32];
1840 const u8 *addr[1];
1841 size_t len[1];
1842 addr[0] = wpabuf_head(cert);
1843 len[0] = wpabuf_len(cert);
1844 if (sha256_vector(1, addr, len, hash) < 0 ||
1845 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
1846 err_str = "Server certificate mismatch";
1847 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
1848 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001849 } else if (!preverify_ok) {
1850 /*
1851 * Certificate matches pinned certificate, allow
1852 * regardless of other problems.
1853 */
1854 wpa_printf(MSG_DEBUG,
1855 "OpenSSL: Ignore validation issues for a pinned server certificate");
1856 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 }
1858 wpabuf_free(cert);
1859 }
1860 }
1861#endif /* CONFIG_SHA256 */
1862
1863 if (!preverify_ok) {
1864 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1865 " error %d (%s) depth %d for '%s'", err, err_str,
1866 depth, buf);
1867 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1868 err_str, TLS_FAIL_UNSPECIFIED);
1869 return preverify_ok;
1870 }
1871
1872 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
1873 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1874 preverify_ok, err, err_str,
1875 conn->ca_cert_verify, depth, buf);
1876 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1877 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1878 "match with '%s'", buf, match);
1879 preverify_ok = 0;
1880 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1881 "Subject mismatch",
1882 TLS_FAIL_SUBJECT_MISMATCH);
1883 } else if (depth == 0 && altmatch &&
1884 !tls_match_altsubject(err_cert, altmatch)) {
1885 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1886 "'%s' not found", altmatch);
1887 preverify_ok = 0;
1888 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1889 "AltSubject mismatch",
1890 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001891 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001892 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001893 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
1894 suffix_match);
1895 preverify_ok = 0;
1896 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1897 "Domain suffix mismatch",
1898 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001899 } else if (depth == 0 && domain_match &&
1900 !tls_match_suffix(err_cert, domain_match, 1)) {
1901 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
1902 domain_match);
1903 preverify_ok = 0;
1904 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1905 "Domain mismatch",
1906 TLS_FAIL_DOMAIN_MISMATCH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 } else
1908 openssl_tls_cert_event(conn, err_cert, depth, buf);
1909
1910 if (conn->cert_probe && preverify_ok && depth == 0) {
1911 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
1912 "on probe-only run");
1913 preverify_ok = 0;
1914 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1915 "Server certificate chain probe",
1916 TLS_FAIL_SERVER_CHAIN_PROBE);
1917 }
1918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001919#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001920 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
1921 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001922 enum ocsp_result res;
1923
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001924 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
1925 conn->peer_issuer,
1926 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001927 if (res == OCSP_REVOKED) {
1928 preverify_ok = 0;
1929 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1930 "certificate revoked",
1931 TLS_FAIL_REVOKED);
1932 if (err == X509_V_OK)
1933 X509_STORE_CTX_set_error(
1934 x509_ctx, X509_V_ERR_CERT_REVOKED);
1935 } else if (res != OCSP_GOOD &&
1936 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
1937 preverify_ok = 0;
1938 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1939 "bad certificate status response",
1940 TLS_FAIL_UNSPECIFIED);
1941 }
1942 }
1943#endif /* OPENSSL_IS_BORINGSSL */
1944
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001945 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001946 context->event_cb(context->cb_ctx,
1947 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 return preverify_ok;
1950}
1951
1952
1953#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001954static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001956 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 X509_LOOKUP *lookup;
1958 int ret = 0;
1959
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001960 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 X509_LOOKUP_file());
1962 if (lookup == NULL) {
1963 tls_show_errors(MSG_WARNING, __func__,
1964 "Failed add lookup for X509 store");
1965 return -1;
1966 }
1967
1968 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1969 unsigned long err = ERR_peek_error();
1970 tls_show_errors(MSG_WARNING, __func__,
1971 "Failed load CA in DER format");
1972 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1973 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1974 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1975 "cert already in hash table error",
1976 __func__);
1977 } else
1978 ret = -1;
1979 }
1980
1981 return ret;
1982}
1983#endif /* OPENSSL_NO_STDIO */
1984
1985
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001986static int tls_connection_ca_cert(struct tls_data *data,
1987 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988 const char *ca_cert, const u8 *ca_cert_blob,
1989 size_t ca_cert_blob_len, const char *ca_path)
1990{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001991 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001992 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993
1994 /*
1995 * Remove previously configured trusted CA certificates before adding
1996 * new ones.
1997 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001998 store = X509_STORE_new();
1999 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2001 "certificate store", __func__);
2002 return -1;
2003 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002004 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005
2006 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2007 conn->ca_cert_verify = 1;
2008
2009 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
2010 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
2011 "chain");
2012 conn->cert_probe = 1;
2013 conn->ca_cert_verify = 0;
2014 return 0;
2015 }
2016
2017 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
2018#ifdef CONFIG_SHA256
2019 const char *pos = ca_cert + 7;
2020 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
2021 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
2022 "hash value '%s'", ca_cert);
2023 return -1;
2024 }
2025 pos += 14;
2026 if (os_strlen(pos) != 32 * 2) {
2027 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
2028 "hash length in ca_cert '%s'", ca_cert);
2029 return -1;
2030 }
2031 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2032 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2033 "value in ca_cert '%s'", ca_cert);
2034 return -1;
2035 }
2036 conn->server_cert_only = 1;
2037 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2038 "certificate match");
2039 return 0;
2040#else /* CONFIG_SHA256 */
2041 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2042 "cannot validate server certificate hash");
2043 return -1;
2044#endif /* CONFIG_SHA256 */
2045 }
2046
2047 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002048 X509 *cert = d2i_X509(NULL,
2049 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 ca_cert_blob_len);
2051 if (cert == NULL) {
2052 tls_show_errors(MSG_WARNING, __func__,
2053 "Failed to parse ca_cert_blob");
2054 return -1;
2055 }
2056
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002057 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2058 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 unsigned long err = ERR_peek_error();
2060 tls_show_errors(MSG_WARNING, __func__,
2061 "Failed to add ca_cert_blob to "
2062 "certificate store");
2063 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2064 ERR_GET_REASON(err) ==
2065 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2066 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2067 "cert already in hash table error",
2068 __func__);
2069 } else {
2070 X509_free(cert);
2071 return -1;
2072 }
2073 }
2074 X509_free(cert);
2075 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2076 "to certificate store", __func__);
2077 return 0;
2078 }
2079
2080#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002081 /* Single alias */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002083 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002084 &ca_cert[11]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002086 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2087 return 0;
2088 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002090 /* Multiple aliases separated by space */
2091 if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) {
2092 char *aliases = os_strdup(&ca_cert[12]);
2093 const char *delim = " ";
2094 int rc = 0;
2095 char *savedptr;
2096 char *alias;
2097
2098 if (!aliases)
2099 return -1;
2100 alias = strtok_r(aliases, delim, &savedptr);
2101 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2102 if (tls_add_ca_from_keystore_encoded(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002103 SSL_CTX_get_cert_store(ssl_ctx), alias)) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002104 wpa_printf(MSG_WARNING,
2105 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2106 __func__, alias);
2107 rc = -1;
2108 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 }
2110 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002111 os_free(aliases);
2112 if (rc)
2113 return rc;
2114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2116 return 0;
2117 }
2118#endif /* ANDROID */
2119
2120#ifdef CONFIG_NATIVE_WINDOWS
2121 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2122 0) {
2123 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2124 "system certificate store");
2125 return 0;
2126 }
2127#endif /* CONFIG_NATIVE_WINDOWS */
2128
2129 if (ca_cert || ca_path) {
2130#ifndef OPENSSL_NO_STDIO
2131 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2132 1) {
2133 tls_show_errors(MSG_WARNING, __func__,
2134 "Failed to load root certificates");
2135 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002136 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2138 "DER format CA certificate",
2139 __func__);
2140 } else
2141 return -1;
2142 } else {
2143 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2144 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002145 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 }
2147#else /* OPENSSL_NO_STDIO */
2148 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2149 __func__);
2150 return -1;
2151#endif /* OPENSSL_NO_STDIO */
2152 } else {
2153 /* No ca_cert configured - do not try to verify server
2154 * certificate */
2155 conn->ca_cert_verify = 0;
2156 }
2157
2158 return 0;
2159}
2160
2161
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002162static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002164 SSL_CTX *ssl_ctx = data->ssl;
2165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 if (ca_cert) {
2167 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2168 {
2169 tls_show_errors(MSG_WARNING, __func__,
2170 "Failed to load root certificates");
2171 return -1;
2172 }
2173
2174 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2175 "certificate(s) loaded");
2176
2177#ifndef OPENSSL_NO_STDIO
2178 /* Add the same CAs to the client certificate requests */
2179 SSL_CTX_set_client_CA_list(ssl_ctx,
2180 SSL_load_client_CA_file(ca_cert));
2181#endif /* OPENSSL_NO_STDIO */
2182 }
2183
2184 return 0;
2185}
2186
2187
2188int tls_global_set_verify(void *ssl_ctx, int check_crl)
2189{
2190 int flags;
2191
2192 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002193 struct tls_data *data = ssl_ctx;
2194 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 if (cs == NULL) {
2196 tls_show_errors(MSG_INFO, __func__, "Failed to get "
2197 "certificate store when enabling "
2198 "check_crl");
2199 return -1;
2200 }
2201 flags = X509_V_FLAG_CRL_CHECK;
2202 if (check_crl == 2)
2203 flags |= X509_V_FLAG_CRL_CHECK_ALL;
2204 X509_STORE_set_flags(cs, flags);
2205 }
2206 return 0;
2207}
2208
2209
2210static int tls_connection_set_subject_match(struct tls_connection *conn,
2211 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002212 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002213 const char *suffix_match,
2214 const char *domain_match)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215{
2216 os_free(conn->subject_match);
2217 conn->subject_match = NULL;
2218 if (subject_match) {
2219 conn->subject_match = os_strdup(subject_match);
2220 if (conn->subject_match == NULL)
2221 return -1;
2222 }
2223
2224 os_free(conn->altsubject_match);
2225 conn->altsubject_match = NULL;
2226 if (altsubject_match) {
2227 conn->altsubject_match = os_strdup(altsubject_match);
2228 if (conn->altsubject_match == NULL)
2229 return -1;
2230 }
2231
Dmitry Shmidt051af732013-10-22 13:52:46 -07002232 os_free(conn->suffix_match);
2233 conn->suffix_match = NULL;
2234 if (suffix_match) {
2235 conn->suffix_match = os_strdup(suffix_match);
2236 if (conn->suffix_match == NULL)
2237 return -1;
2238 }
2239
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002240 os_free(conn->domain_match);
2241 conn->domain_match = NULL;
2242 if (domain_match) {
2243 conn->domain_match = os_strdup(domain_match);
2244 if (conn->domain_match == NULL)
2245 return -1;
2246 }
2247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 return 0;
2249}
2250
2251
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002252static void tls_set_conn_flags(SSL *ssl, unsigned int flags)
2253{
2254#ifdef SSL_OP_NO_TICKET
2255 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
2256 SSL_set_options(ssl, SSL_OP_NO_TICKET);
2257#ifdef SSL_clear_options
2258 else
2259 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
2260#endif /* SSL_clear_options */
2261#endif /* SSL_OP_NO_TICKET */
2262
2263#ifdef SSL_OP_NO_TLSv1
2264 if (flags & TLS_CONN_DISABLE_TLSv1_0)
2265 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2266 else
2267 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
2268#endif /* SSL_OP_NO_TLSv1 */
2269#ifdef SSL_OP_NO_TLSv1_1
2270 if (flags & TLS_CONN_DISABLE_TLSv1_1)
2271 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2272 else
2273 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
2274#endif /* SSL_OP_NO_TLSv1_1 */
2275#ifdef SSL_OP_NO_TLSv1_2
2276 if (flags & TLS_CONN_DISABLE_TLSv1_2)
2277 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
2278 else
2279 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
2280#endif /* SSL_OP_NO_TLSv1_2 */
2281}
2282
2283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002285 int verify_peer, unsigned int flags,
2286 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287{
2288 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002289 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290
2291 if (conn == NULL)
2292 return -1;
2293
2294 if (verify_peer) {
2295 conn->ca_cert_verify = 1;
2296 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
2297 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
2298 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
2299 } else {
2300 conn->ca_cert_verify = 0;
2301 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
2302 }
2303
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002304 tls_set_conn_flags(conn->ssl, flags);
2305 conn->flags = flags;
2306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 SSL_set_accept_state(conn->ssl);
2308
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002309 if (data->tls_session_lifetime == 0) {
2310 /*
2311 * Set session id context to a unique value to make sure
2312 * session resumption cannot be used either through session
2313 * caching or TLS ticket extension.
2314 */
2315 counter++;
2316 SSL_set_session_id_context(conn->ssl,
2317 (const unsigned char *) &counter,
2318 sizeof(counter));
2319 } else if (session_ctx) {
2320 SSL_set_session_id_context(conn->ssl, session_ctx,
2321 session_ctx_len);
2322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323
2324 return 0;
2325}
2326
2327
2328static int tls_connection_client_cert(struct tls_connection *conn,
2329 const char *client_cert,
2330 const u8 *client_cert_blob,
2331 size_t client_cert_blob_len)
2332{
2333 if (client_cert == NULL && client_cert_blob == NULL)
2334 return 0;
2335
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002336#ifdef PKCS12_FUNCS
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002337#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002338 /*
2339 * Clear previously set extra chain certificates, if any, from PKCS#12
2340 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2341 * chain properly.
2342 */
2343 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
2344#endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2345#endif /* PKCS12_FUNCS */
2346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 if (client_cert_blob &&
2348 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
2349 client_cert_blob_len) == 1) {
2350 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
2351 "OK");
2352 return 0;
2353 } else if (client_cert_blob) {
2354 tls_show_errors(MSG_DEBUG, __func__,
2355 "SSL_use_certificate_ASN1 failed");
2356 }
2357
2358 if (client_cert == NULL)
2359 return -1;
2360
2361#ifdef ANDROID
2362 if (os_strncmp("keystore://", client_cert, 11) == 0) {
2363 BIO *bio = BIO_from_keystore(&client_cert[11]);
2364 X509 *x509 = NULL;
2365 int ret = -1;
Paul Stewart50772e82017-01-25 13:59:16 -08002366 if (bio)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
Paul Stewart50772e82017-01-25 13:59:16 -08002368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 if (x509) {
2370 if (SSL_use_certificate(conn->ssl, x509) == 1)
2371 ret = 0;
2372 X509_free(x509);
2373 }
Paul Stewart50772e82017-01-25 13:59:16 -08002374
2375 /* Read additional certificates into the chain. */
2376 while (bio) {
2377 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2378 if (x509) {
2379 /* Takes ownership of x509 */
2380 SSL_add0_chain_cert(conn->ssl, x509);
2381 } else {
2382 BIO_free(bio);
2383 bio = NULL;
2384 }
2385 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386 return ret;
2387 }
2388#endif /* ANDROID */
2389
2390#ifndef OPENSSL_NO_STDIO
2391 if (SSL_use_certificate_file(conn->ssl, client_cert,
2392 SSL_FILETYPE_ASN1) == 1) {
2393 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
2394 " --> OK");
2395 return 0;
2396 }
2397
2398 if (SSL_use_certificate_file(conn->ssl, client_cert,
2399 SSL_FILETYPE_PEM) == 1) {
2400 ERR_clear_error();
2401 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
2402 " --> OK");
2403 return 0;
2404 }
2405
2406 tls_show_errors(MSG_DEBUG, __func__,
2407 "SSL_use_certificate_file failed");
2408#else /* OPENSSL_NO_STDIO */
2409 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2410#endif /* OPENSSL_NO_STDIO */
2411
2412 return -1;
2413}
2414
2415
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002416static int tls_global_client_cert(struct tls_data *data,
2417 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418{
2419#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002420 SSL_CTX *ssl_ctx = data->ssl;
2421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422 if (client_cert == NULL)
2423 return 0;
2424
2425 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2426 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2429 SSL_FILETYPE_PEM) != 1) {
2430 tls_show_errors(MSG_INFO, __func__,
2431 "Failed to load client certificate");
2432 return -1;
2433 }
2434 return 0;
2435#else /* OPENSSL_NO_STDIO */
2436 if (client_cert == NULL)
2437 return 0;
2438 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2439 return -1;
2440#endif /* OPENSSL_NO_STDIO */
2441}
2442
2443
2444static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
2445{
2446 if (password == NULL) {
2447 return 0;
2448 }
2449 os_strlcpy(buf, (char *) password, size);
2450 return os_strlen(buf);
2451}
2452
2453
2454#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002455static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 const char *passwd)
2457{
2458 EVP_PKEY *pkey;
2459 X509 *cert;
2460 STACK_OF(X509) *certs;
2461 int res = 0;
2462 char buf[256];
2463
2464 pkey = NULL;
2465 cert = NULL;
2466 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002467 if (!passwd)
2468 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
2470 tls_show_errors(MSG_DEBUG, __func__,
2471 "Failed to parse PKCS12 file");
2472 PKCS12_free(p12);
2473 return -1;
2474 }
2475 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
2476
2477 if (cert) {
2478 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2479 sizeof(buf));
2480 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
2481 "subject='%s'", buf);
2482 if (ssl) {
2483 if (SSL_use_certificate(ssl, cert) != 1)
2484 res = -1;
2485 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002486 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 res = -1;
2488 }
2489 X509_free(cert);
2490 }
2491
2492 if (pkey) {
2493 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
2494 if (ssl) {
2495 if (SSL_use_PrivateKey(ssl, pkey) != 1)
2496 res = -1;
2497 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002498 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 res = -1;
2500 }
2501 EVP_PKEY_free(pkey);
2502 }
2503
2504 if (certs) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002505#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002506 if (ssl)
2507 SSL_clear_chain_certs(ssl);
2508 else
2509 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002510 while ((cert = sk_X509_pop(certs)) != NULL) {
2511 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2512 sizeof(buf));
2513 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2514 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002515 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
2516 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
2517 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002518 tls_show_errors(MSG_DEBUG, __func__,
2519 "Failed to add additional certificate");
2520 res = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002521 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002522 break;
2523 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002524 X509_free(cert);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002525 }
2526 if (!res) {
2527 /* Try to continue anyway */
2528 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002529 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002530#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002531 if (ssl)
2532 res = SSL_build_cert_chain(
2533 ssl,
2534 SSL_BUILD_CHAIN_FLAG_CHECK |
2535 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
2536 else
2537 res = SSL_CTX_build_cert_chain(
2538 data->ssl,
2539 SSL_BUILD_CHAIN_FLAG_CHECK |
2540 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002541 if (!res) {
2542 tls_show_errors(MSG_DEBUG, __func__,
2543 "Failed to build certificate chain");
2544 } else if (res == 2) {
2545 wpa_printf(MSG_DEBUG,
2546 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2547 }
2548#endif /* OPENSSL_IS_BORINGSSL */
2549 /*
2550 * Try to continue regardless of result since it is possible for
2551 * the extra certificates not to be required.
2552 */
2553 res = 0;
2554#else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002555 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 while ((cert = sk_X509_pop(certs)) != NULL) {
2557 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2558 sizeof(buf));
2559 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2560 " from PKCS12: subject='%s'", buf);
2561 /*
2562 * There is no SSL equivalent for the chain cert - so
2563 * always add it to the context...
2564 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002565 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
2566 {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002567 X509_free(cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 res = -1;
2569 break;
2570 }
2571 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002572 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002573#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 }
2575
2576 PKCS12_free(p12);
2577
2578 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002579 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580
2581 return res;
2582}
2583#endif /* PKCS12_FUNCS */
2584
2585
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002586static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
2587 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588{
2589#ifdef PKCS12_FUNCS
2590 FILE *f;
2591 PKCS12 *p12;
2592
2593 f = fopen(private_key, "rb");
2594 if (f == NULL)
2595 return -1;
2596
2597 p12 = d2i_PKCS12_fp(f, NULL);
2598 fclose(f);
2599
2600 if (p12 == NULL) {
2601 tls_show_errors(MSG_INFO, __func__,
2602 "Failed to use PKCS#12 file");
2603 return -1;
2604 }
2605
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002606 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607
2608#else /* PKCS12_FUNCS */
2609 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
2610 "p12/pfx files");
2611 return -1;
2612#endif /* PKCS12_FUNCS */
2613}
2614
2615
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002616static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617 const u8 *blob, size_t len, const char *passwd)
2618{
2619#ifdef PKCS12_FUNCS
2620 PKCS12 *p12;
2621
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002622 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 if (p12 == NULL) {
2624 tls_show_errors(MSG_INFO, __func__,
2625 "Failed to use PKCS#12 blob");
2626 return -1;
2627 }
2628
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002629 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630
2631#else /* PKCS12_FUNCS */
2632 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
2633 "p12/pfx blobs");
2634 return -1;
2635#endif /* PKCS12_FUNCS */
2636}
2637
2638
2639#ifndef OPENSSL_NO_ENGINE
2640static int tls_engine_get_cert(struct tls_connection *conn,
2641 const char *cert_id,
2642 X509 **cert)
2643{
2644 /* this runs after the private key is loaded so no PIN is required */
2645 struct {
2646 const char *cert_id;
2647 X509 *cert;
2648 } params;
2649 params.cert_id = cert_id;
2650 params.cert = NULL;
2651
2652 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
2653 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002654 unsigned long err = ERR_get_error();
2655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
2657 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002658 ERR_error_string(err, NULL));
2659 if (tls_is_pin_error(err))
2660 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2662 }
2663 if (!params.cert) {
2664 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
2665 " '%s'", cert_id);
2666 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2667 }
2668 *cert = params.cert;
2669 return 0;
2670}
2671#endif /* OPENSSL_NO_ENGINE */
2672
2673
2674static int tls_connection_engine_client_cert(struct tls_connection *conn,
2675 const char *cert_id)
2676{
2677#ifndef OPENSSL_NO_ENGINE
2678 X509 *cert;
2679
2680 if (tls_engine_get_cert(conn, cert_id, &cert))
2681 return -1;
2682
2683 if (!SSL_use_certificate(conn->ssl, cert)) {
2684 tls_show_errors(MSG_ERROR, __func__,
2685 "SSL_use_certificate failed");
2686 X509_free(cert);
2687 return -1;
2688 }
2689 X509_free(cert);
2690 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
2691 "OK");
2692 return 0;
2693
2694#else /* OPENSSL_NO_ENGINE */
2695 return -1;
2696#endif /* OPENSSL_NO_ENGINE */
2697}
2698
2699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002700static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 struct tls_connection *conn,
2702 const char *ca_cert_id)
2703{
2704#ifndef OPENSSL_NO_ENGINE
2705 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002706 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002707 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708
2709 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
2710 return -1;
2711
2712 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002713 store = X509_STORE_new();
2714 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2716 "certificate store", __func__);
2717 X509_free(cert);
2718 return -1;
2719 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002720 SSL_CTX_set_cert_store(ssl_ctx, store);
2721 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 unsigned long err = ERR_peek_error();
2723 tls_show_errors(MSG_WARNING, __func__,
2724 "Failed to add CA certificate from engine "
2725 "to certificate store");
2726 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2727 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2728 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
2729 " already in hash table error",
2730 __func__);
2731 } else {
2732 X509_free(cert);
2733 return -1;
2734 }
2735 }
2736 X509_free(cert);
2737 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
2738 "to certificate store", __func__);
2739 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002740 conn->ca_cert_verify = 1;
2741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 return 0;
2743
2744#else /* OPENSSL_NO_ENGINE */
2745 return -1;
2746#endif /* OPENSSL_NO_ENGINE */
2747}
2748
2749
2750static int tls_connection_engine_private_key(struct tls_connection *conn)
2751{
Adam Langley1eb02ed2015-04-21 19:00:05 -07002752#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
2754 tls_show_errors(MSG_ERROR, __func__,
2755 "ENGINE: cannot use private key for TLS");
2756 return -1;
2757 }
2758 if (!SSL_check_private_key(conn->ssl)) {
2759 tls_show_errors(MSG_INFO, __func__,
2760 "Private key failed verification");
2761 return -1;
2762 }
2763 return 0;
2764#else /* OPENSSL_NO_ENGINE */
2765 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
2766 "engine support was not compiled in");
2767 return -1;
2768#endif /* OPENSSL_NO_ENGINE */
2769}
2770
2771
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002772static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 struct tls_connection *conn,
2774 const char *private_key,
2775 const char *private_key_passwd,
2776 const u8 *private_key_blob,
2777 size_t private_key_blob_len)
2778{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002779 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780 char *passwd;
2781 int ok;
2782
2783 if (private_key == NULL && private_key_blob == NULL)
2784 return 0;
2785
2786 if (private_key_passwd) {
2787 passwd = os_strdup(private_key_passwd);
2788 if (passwd == NULL)
2789 return -1;
2790 } else
2791 passwd = NULL;
2792
2793 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2794 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2795
2796 ok = 0;
2797 while (private_key_blob) {
2798 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
2799 (u8 *) private_key_blob,
2800 private_key_blob_len) == 1) {
2801 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2802 "ASN1(EVP_PKEY_RSA) --> OK");
2803 ok = 1;
2804 break;
2805 }
2806
2807 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
2808 (u8 *) private_key_blob,
2809 private_key_blob_len) == 1) {
2810 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2811 "ASN1(EVP_PKEY_DSA) --> OK");
2812 ok = 1;
2813 break;
2814 }
2815
2816 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
2817 (u8 *) private_key_blob,
2818 private_key_blob_len) == 1) {
2819 wpa_printf(MSG_DEBUG, "OpenSSL: "
2820 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2821 ok = 1;
2822 break;
2823 }
2824
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002825 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826 private_key_blob_len, passwd) == 0) {
2827 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
2828 "OK");
2829 ok = 1;
2830 break;
2831 }
2832
2833 break;
2834 }
2835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836 while (!ok && private_key) {
2837#ifndef OPENSSL_NO_STDIO
2838 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2839 SSL_FILETYPE_ASN1) == 1) {
2840 wpa_printf(MSG_DEBUG, "OpenSSL: "
2841 "SSL_use_PrivateKey_File (DER) --> OK");
2842 ok = 1;
2843 break;
2844 }
2845
2846 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2847 SSL_FILETYPE_PEM) == 1) {
2848 wpa_printf(MSG_DEBUG, "OpenSSL: "
2849 "SSL_use_PrivateKey_File (PEM) --> OK");
2850 ok = 1;
2851 break;
2852 }
2853#else /* OPENSSL_NO_STDIO */
2854 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2855 __func__);
2856#endif /* OPENSSL_NO_STDIO */
2857
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002858 if (tls_read_pkcs12(data, conn->ssl, private_key, passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 == 0) {
2860 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
2861 "--> OK");
2862 ok = 1;
2863 break;
2864 }
2865
2866 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
2867 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
2868 "access certificate store --> OK");
2869 ok = 1;
2870 break;
2871 }
2872
2873 break;
2874 }
2875
2876 if (!ok) {
2877 tls_show_errors(MSG_INFO, __func__,
2878 "Failed to load private key");
2879 os_free(passwd);
2880 return -1;
2881 }
2882 ERR_clear_error();
2883 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
2884 os_free(passwd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 if (!SSL_check_private_key(conn->ssl)) {
2887 tls_show_errors(MSG_INFO, __func__, "Private key failed "
2888 "verification");
2889 return -1;
2890 }
2891
2892 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
2893 return 0;
2894}
2895
2896
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002897static int tls_global_private_key(struct tls_data *data,
2898 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 const char *private_key_passwd)
2900{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002901 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 char *passwd;
2903
2904 if (private_key == NULL)
2905 return 0;
2906
2907 if (private_key_passwd) {
2908 passwd = os_strdup(private_key_passwd);
2909 if (passwd == NULL)
2910 return -1;
2911 } else
2912 passwd = NULL;
2913
2914 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2915 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2916 if (
2917#ifndef OPENSSL_NO_STDIO
2918 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2919 SSL_FILETYPE_ASN1) != 1 &&
2920 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2921 SSL_FILETYPE_PEM) != 1 &&
2922#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002923 tls_read_pkcs12(data, NULL, private_key, passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 tls_show_errors(MSG_INFO, __func__,
2925 "Failed to load private key");
2926 os_free(passwd);
2927 ERR_clear_error();
2928 return -1;
2929 }
2930 os_free(passwd);
2931 ERR_clear_error();
2932 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 if (!SSL_CTX_check_private_key(ssl_ctx)) {
2935 tls_show_errors(MSG_INFO, __func__,
2936 "Private key failed verification");
2937 return -1;
2938 }
2939
2940 return 0;
2941}
2942
2943
2944static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
2945{
2946#ifdef OPENSSL_NO_DH
2947 if (dh_file == NULL)
2948 return 0;
2949 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
2950 "dh_file specified");
2951 return -1;
2952#else /* OPENSSL_NO_DH */
2953 DH *dh;
2954 BIO *bio;
2955
2956 /* TODO: add support for dh_blob */
2957 if (dh_file == NULL)
2958 return 0;
2959 if (conn == NULL)
2960 return -1;
2961
2962 bio = BIO_new_file(dh_file, "r");
2963 if (bio == NULL) {
2964 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
2965 dh_file, ERR_error_string(ERR_get_error(), NULL));
2966 return -1;
2967 }
2968 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
2969 BIO_free(bio);
2970#ifndef OPENSSL_NO_DSA
2971 while (dh == NULL) {
2972 DSA *dsa;
2973 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
2974 " trying to parse as DSA params", dh_file,
2975 ERR_error_string(ERR_get_error(), NULL));
2976 bio = BIO_new_file(dh_file, "r");
2977 if (bio == NULL)
2978 break;
2979 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
2980 BIO_free(bio);
2981 if (!dsa) {
2982 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
2983 "'%s': %s", dh_file,
2984 ERR_error_string(ERR_get_error(), NULL));
2985 break;
2986 }
2987
2988 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
2989 dh = DSA_dup_DH(dsa);
2990 DSA_free(dsa);
2991 if (dh == NULL) {
2992 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
2993 "params into DH params");
2994 break;
2995 }
2996 break;
2997 }
2998#endif /* !OPENSSL_NO_DSA */
2999 if (dh == NULL) {
3000 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3001 "'%s'", dh_file);
3002 return -1;
3003 }
3004
3005 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
3006 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3007 "%s", dh_file,
3008 ERR_error_string(ERR_get_error(), NULL));
3009 DH_free(dh);
3010 return -1;
3011 }
3012 DH_free(dh);
3013 return 0;
3014#endif /* OPENSSL_NO_DH */
3015}
3016
3017
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003018static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019{
3020#ifdef OPENSSL_NO_DH
3021 if (dh_file == NULL)
3022 return 0;
3023 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
3024 "dh_file specified");
3025 return -1;
3026#else /* OPENSSL_NO_DH */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003027 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 DH *dh;
3029 BIO *bio;
3030
3031 /* TODO: add support for dh_blob */
3032 if (dh_file == NULL)
3033 return 0;
3034 if (ssl_ctx == NULL)
3035 return -1;
3036
3037 bio = BIO_new_file(dh_file, "r");
3038 if (bio == NULL) {
3039 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
3040 dh_file, ERR_error_string(ERR_get_error(), NULL));
3041 return -1;
3042 }
3043 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3044 BIO_free(bio);
3045#ifndef OPENSSL_NO_DSA
3046 while (dh == NULL) {
3047 DSA *dsa;
3048 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3049 " trying to parse as DSA params", dh_file,
3050 ERR_error_string(ERR_get_error(), NULL));
3051 bio = BIO_new_file(dh_file, "r");
3052 if (bio == NULL)
3053 break;
3054 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3055 BIO_free(bio);
3056 if (!dsa) {
3057 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3058 "'%s': %s", dh_file,
3059 ERR_error_string(ERR_get_error(), NULL));
3060 break;
3061 }
3062
3063 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3064 dh = DSA_dup_DH(dsa);
3065 DSA_free(dsa);
3066 if (dh == NULL) {
3067 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3068 "params into DH params");
3069 break;
3070 }
3071 break;
3072 }
3073#endif /* !OPENSSL_NO_DSA */
3074 if (dh == NULL) {
3075 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3076 "'%s'", dh_file);
3077 return -1;
3078 }
3079
3080 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
3081 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3082 "%s", dh_file,
3083 ERR_error_string(ERR_get_error(), NULL));
3084 DH_free(dh);
3085 return -1;
3086 }
3087 DH_free(dh);
3088 return 0;
3089#endif /* OPENSSL_NO_DH */
3090}
3091
3092
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003093int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
3094 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095{
3096 SSL *ssl;
3097
3098 if (conn == NULL || keys == NULL)
3099 return -1;
3100 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003101 if (ssl == NULL)
3102 return -1;
3103
3104 os_memset(keys, 0, sizeof(*keys));
3105 keys->client_random = conn->client_random;
3106 keys->client_random_len = SSL_get_client_random(
3107 ssl, conn->client_random, sizeof(conn->client_random));
3108 keys->server_random = conn->server_random;
3109 keys->server_random_len = SSL_get_server_random(
3110 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111
3112 return 0;
3113}
3114
3115
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003116#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003117static int openssl_get_keyblock_size(SSL *ssl)
3118{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003119#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003120 const EVP_CIPHER *c;
3121 const EVP_MD *h;
3122 int md_size;
3123
3124 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
3125 ssl->read_hash == NULL)
3126 return -1;
3127
3128 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003129 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003130 if (h)
3131 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003132 else if (ssl->s3)
3133 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003134 else
3135 return -1;
3136
3137 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3138 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
3139 EVP_CIPHER_iv_length(c));
3140 return 2 * (EVP_CIPHER_key_length(c) +
3141 md_size +
3142 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003143#else
3144 const SSL_CIPHER *ssl_cipher;
3145 int cipher, digest;
3146 const EVP_CIPHER *c;
3147 const EVP_MD *h;
3148
3149 ssl_cipher = SSL_get_current_cipher(ssl);
3150 if (!ssl_cipher)
3151 return -1;
3152 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
3153 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
3154 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
3155 cipher, digest);
3156 if (cipher < 0 || digest < 0)
3157 return -1;
3158 c = EVP_get_cipherbynid(cipher);
3159 h = EVP_get_digestbynid(digest);
3160 if (!c || !h)
3161 return -1;
3162
3163 wpa_printf(MSG_DEBUG,
3164 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3165 EVP_CIPHER_key_length(c), EVP_MD_size(h),
3166 EVP_CIPHER_iv_length(c));
3167 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
3168 EVP_CIPHER_iv_length(c));
3169#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003170}
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003171#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003172
3173
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003174int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
3175 const char *label, u8 *out, size_t out_len)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003176{
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003177 if (!conn ||
3178 SSL_export_keying_material(conn->ssl, out, out_len, label,
3179 os_strlen(label), NULL, 0, 0) != 1)
3180 return -1;
3181 return 0;
3182}
3183
3184
3185int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
3186 u8 *out, size_t out_len)
3187{
3188#ifdef OPENSSL_NEED_EAP_FAST_PRF
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003189 SSL *ssl;
3190 SSL_SESSION *sess;
3191 u8 *rnd;
3192 int ret = -1;
3193 int skip = 0;
3194 u8 *tmp_out = NULL;
3195 u8 *_out = out;
3196 unsigned char client_random[SSL3_RANDOM_SIZE];
3197 unsigned char server_random[SSL3_RANDOM_SIZE];
3198 unsigned char master_key[64];
3199 size_t master_key_len;
3200 const char *ver;
3201
3202 /*
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003203 * TLS library did not support EAP-FAST key generation, so get the
3204 * needed TLS session parameters and use an internal implementation of
3205 * TLS PRF to derive the key.
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003206 */
3207
3208 if (conn == NULL)
3209 return -1;
3210 ssl = conn->ssl;
3211 if (ssl == NULL)
3212 return -1;
3213 ver = SSL_get_version(ssl);
3214 sess = SSL_get_session(ssl);
3215 if (!ver || !sess)
3216 return -1;
3217
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003218 skip = openssl_get_keyblock_size(ssl);
3219 if (skip < 0)
3220 return -1;
3221 tmp_out = os_malloc(skip + out_len);
3222 if (!tmp_out)
3223 return -1;
3224 _out = tmp_out;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003225
3226 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
3227 if (!rnd) {
3228 os_free(tmp_out);
3229 return -1;
3230 }
3231
3232 SSL_get_client_random(ssl, client_random, sizeof(client_random));
3233 SSL_get_server_random(ssl, server_random, sizeof(server_random));
3234 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
3235 sizeof(master_key));
3236
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003237 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
3238 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random, SSL3_RANDOM_SIZE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003239
3240 if (os_strcmp(ver, "TLSv1.2") == 0) {
3241 tls_prf_sha256(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003242 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003243 _out, skip + out_len);
3244 ret = 0;
3245 } else if (tls_prf_sha1_md5(master_key, master_key_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003246 "key expansion", rnd, 2 * SSL3_RANDOM_SIZE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003247 _out, skip + out_len) == 0) {
3248 ret = 0;
3249 }
3250 os_memset(master_key, 0, sizeof(master_key));
3251 os_free(rnd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003252 if (ret == 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003253 os_memcpy(out, _out + skip, out_len);
3254 bin_clear_free(tmp_out, skip);
3255
3256 return ret;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003257#else /* OPENSSL_NEED_EAP_FAST_PRF */
3258 wpa_printf(MSG_ERROR,
3259 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3260 return -1;
3261#endif /* OPENSSL_NEED_EAP_FAST_PRF */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262}
3263
3264
3265static struct wpabuf *
3266openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data,
3267 int server)
3268{
3269 int res;
3270 struct wpabuf *out_data;
3271
3272 /*
3273 * Give TLS handshake data from the server (if available) to OpenSSL
3274 * for processing.
3275 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003276 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
3278 < 0) {
3279 tls_show_errors(MSG_INFO, __func__,
3280 "Handshake failed - BIO_write");
3281 return NULL;
3282 }
3283
3284 /* Initiate TLS handshake or continue the existing handshake */
3285 if (server)
3286 res = SSL_accept(conn->ssl);
3287 else
3288 res = SSL_connect(conn->ssl);
3289 if (res != 1) {
3290 int err = SSL_get_error(conn->ssl, res);
3291 if (err == SSL_ERROR_WANT_READ)
3292 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
3293 "more data");
3294 else if (err == SSL_ERROR_WANT_WRITE)
3295 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
3296 "write");
3297 else {
3298 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
3299 conn->failed++;
3300 }
3301 }
3302
3303 /* Get the TLS handshake data to be sent to the server */
3304 res = BIO_ctrl_pending(conn->ssl_out);
3305 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
3306 out_data = wpabuf_alloc(res);
3307 if (out_data == NULL) {
3308 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
3309 "handshake output (%d bytes)", res);
3310 if (BIO_reset(conn->ssl_out) < 0) {
3311 tls_show_errors(MSG_INFO, __func__,
3312 "BIO_reset failed");
3313 }
3314 return NULL;
3315 }
3316 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
3317 res);
3318 if (res < 0) {
3319 tls_show_errors(MSG_INFO, __func__,
3320 "Handshake failed - BIO_read");
3321 if (BIO_reset(conn->ssl_out) < 0) {
3322 tls_show_errors(MSG_INFO, __func__,
3323 "BIO_reset failed");
3324 }
3325 wpabuf_free(out_data);
3326 return NULL;
3327 }
3328 wpabuf_put(out_data, res);
3329
3330 return out_data;
3331}
3332
3333
3334static struct wpabuf *
3335openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
3336{
3337 struct wpabuf *appl_data;
3338 int res;
3339
3340 appl_data = wpabuf_alloc(max_len + 100);
3341 if (appl_data == NULL)
3342 return NULL;
3343
3344 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
3345 wpabuf_size(appl_data));
3346 if (res < 0) {
3347 int err = SSL_get_error(conn->ssl, res);
3348 if (err == SSL_ERROR_WANT_READ ||
3349 err == SSL_ERROR_WANT_WRITE) {
3350 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
3351 "included");
3352 } else {
3353 tls_show_errors(MSG_INFO, __func__,
3354 "Failed to read possible "
3355 "Application Data");
3356 }
3357 wpabuf_free(appl_data);
3358 return NULL;
3359 }
3360
3361 wpabuf_put(appl_data, res);
3362 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
3363 "message", appl_data);
3364
3365 return appl_data;
3366}
3367
3368
3369static struct wpabuf *
3370openssl_connection_handshake(struct tls_connection *conn,
3371 const struct wpabuf *in_data,
3372 struct wpabuf **appl_data, int server)
3373{
3374 struct wpabuf *out_data;
3375
3376 if (appl_data)
3377 *appl_data = NULL;
3378
3379 out_data = openssl_handshake(conn, in_data, server);
3380 if (out_data == NULL)
3381 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03003382 if (conn->invalid_hb_used) {
3383 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3384 wpabuf_free(out_data);
3385 return NULL;
3386 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003388 if (SSL_is_init_finished(conn->ssl)) {
3389 wpa_printf(MSG_DEBUG,
3390 "OpenSSL: Handshake finished - resumed=%d",
3391 tls_connection_resumed(conn->ssl_ctx, conn));
3392 if (appl_data && in_data)
3393 *appl_data = openssl_get_appl_data(conn,
3394 wpabuf_len(in_data));
3395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396
Jouni Malinen26af48b2014-04-09 13:02:53 +03003397 if (conn->invalid_hb_used) {
3398 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3399 if (appl_data) {
3400 wpabuf_free(*appl_data);
3401 *appl_data = NULL;
3402 }
3403 wpabuf_free(out_data);
3404 return NULL;
3405 }
3406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407 return out_data;
3408}
3409
3410
3411struct wpabuf *
3412tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
3413 const struct wpabuf *in_data,
3414 struct wpabuf **appl_data)
3415{
3416 return openssl_connection_handshake(conn, in_data, appl_data, 0);
3417}
3418
3419
3420struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
3421 struct tls_connection *conn,
3422 const struct wpabuf *in_data,
3423 struct wpabuf **appl_data)
3424{
3425 return openssl_connection_handshake(conn, in_data, appl_data, 1);
3426}
3427
3428
3429struct wpabuf * tls_connection_encrypt(void *tls_ctx,
3430 struct tls_connection *conn,
3431 const struct wpabuf *in_data)
3432{
3433 int res;
3434 struct wpabuf *buf;
3435
3436 if (conn == NULL)
3437 return NULL;
3438
3439 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3440 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
3441 (res = BIO_reset(conn->ssl_out)) < 0) {
3442 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3443 return NULL;
3444 }
3445 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
3446 if (res < 0) {
3447 tls_show_errors(MSG_INFO, __func__,
3448 "Encryption failed - SSL_write");
3449 return NULL;
3450 }
3451
3452 /* Read encrypted data to be sent to the server */
3453 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
3454 if (buf == NULL)
3455 return NULL;
3456 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
3457 if (res < 0) {
3458 tls_show_errors(MSG_INFO, __func__,
3459 "Encryption failed - BIO_read");
3460 wpabuf_free(buf);
3461 return NULL;
3462 }
3463 wpabuf_put(buf, res);
3464
3465 return buf;
3466}
3467
3468
3469struct wpabuf * tls_connection_decrypt(void *tls_ctx,
3470 struct tls_connection *conn,
3471 const struct wpabuf *in_data)
3472{
3473 int res;
3474 struct wpabuf *buf;
3475
3476 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3477 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
3478 wpabuf_len(in_data));
3479 if (res < 0) {
3480 tls_show_errors(MSG_INFO, __func__,
3481 "Decryption failed - BIO_write");
3482 return NULL;
3483 }
3484 if (BIO_reset(conn->ssl_out) < 0) {
3485 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3486 return NULL;
3487 }
3488
3489 /* Read decrypted data for further processing */
3490 /*
3491 * Even though we try to disable TLS compression, it is possible that
3492 * this cannot be done with all TLS libraries. Add extra buffer space
3493 * to handle the possibility of the decrypted data being longer than
3494 * input data.
3495 */
3496 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
3497 if (buf == NULL)
3498 return NULL;
3499 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
3500 if (res < 0) {
3501 tls_show_errors(MSG_INFO, __func__,
3502 "Decryption failed - SSL_read");
3503 wpabuf_free(buf);
3504 return NULL;
3505 }
3506 wpabuf_put(buf, res);
3507
Jouni Malinen26af48b2014-04-09 13:02:53 +03003508 if (conn->invalid_hb_used) {
3509 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3510 wpabuf_free(buf);
3511 return NULL;
3512 }
3513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514 return buf;
3515}
3516
3517
3518int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
3519{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003520 return conn ? SSL_cache_hit(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521}
3522
3523
3524int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
3525 u8 *ciphers)
3526{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003527 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 u8 *c;
3529 int ret;
3530
3531 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
3532 return -1;
3533
3534 buf[0] = '\0';
3535 pos = buf;
3536 end = pos + sizeof(buf);
3537
3538 c = ciphers;
3539 while (*c != TLS_CIPHER_NONE) {
3540 const char *suite;
3541
3542 switch (*c) {
3543 case TLS_CIPHER_RC4_SHA:
3544 suite = "RC4-SHA";
3545 break;
3546 case TLS_CIPHER_AES128_SHA:
3547 suite = "AES128-SHA";
3548 break;
3549 case TLS_CIPHER_RSA_DHE_AES128_SHA:
3550 suite = "DHE-RSA-AES128-SHA";
3551 break;
3552 case TLS_CIPHER_ANON_DH_AES128_SHA:
3553 suite = "ADH-AES128-SHA";
3554 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003555 case TLS_CIPHER_RSA_DHE_AES256_SHA:
3556 suite = "DHE-RSA-AES256-SHA";
3557 break;
3558 case TLS_CIPHER_AES256_SHA:
3559 suite = "AES256-SHA";
3560 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 default:
3562 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
3563 "cipher selection: %d", *c);
3564 return -1;
3565 }
3566 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003567 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568 break;
3569 pos += ret;
3570
3571 c++;
3572 }
3573
3574 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
3575
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003576#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003577#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3578 if (os_strstr(buf, ":ADH-")) {
3579 /*
3580 * Need to drop to security level 0 to allow anonymous
3581 * cipher suites for EAP-FAST.
3582 */
3583 SSL_set_security_level(conn->ssl, 0);
3584 } else if (SSL_get_security_level(conn->ssl) == 0) {
3585 /* Force at least security level 1 */
3586 SSL_set_security_level(conn->ssl, 1);
3587 }
3588#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3589#endif
3590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
3592 tls_show_errors(MSG_INFO, __func__,
3593 "Cipher suite configuration failed");
3594 return -1;
3595 }
3596
3597 return 0;
3598}
3599
3600
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003601int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
3602 char *buf, size_t buflen)
3603{
3604 const char *name;
3605 if (conn == NULL || conn->ssl == NULL)
3606 return -1;
3607
3608 name = SSL_get_version(conn->ssl);
3609 if (name == NULL)
3610 return -1;
3611
3612 os_strlcpy(buf, name, buflen);
3613 return 0;
3614}
3615
3616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
3618 char *buf, size_t buflen)
3619{
3620 const char *name;
3621 if (conn == NULL || conn->ssl == NULL)
3622 return -1;
3623
3624 name = SSL_get_cipher(conn->ssl);
3625 if (name == NULL)
3626 return -1;
3627
3628 os_strlcpy(buf, name, buflen);
3629 return 0;
3630}
3631
3632
3633int tls_connection_enable_workaround(void *ssl_ctx,
3634 struct tls_connection *conn)
3635{
3636 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
3637
3638 return 0;
3639}
3640
3641
3642#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3643/* ClientHello TLS extensions require a patch to openssl, so this function is
3644 * commented out unless explicitly needed for EAP-FAST in order to be able to
3645 * build this file with unmodified openssl. */
3646int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
3647 int ext_type, const u8 *data,
3648 size_t data_len)
3649{
3650 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
3651 return -1;
3652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
3654 data_len) != 1)
3655 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656
3657 return 0;
3658}
3659#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3660
3661
3662int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
3663{
3664 if (conn == NULL)
3665 return -1;
3666 return conn->failed;
3667}
3668
3669
3670int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
3671{
3672 if (conn == NULL)
3673 return -1;
3674 return conn->read_alerts;
3675}
3676
3677
3678int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
3679{
3680 if (conn == NULL)
3681 return -1;
3682 return conn->write_alerts;
3683}
3684
3685
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003686#ifdef HAVE_OCSP
3687
3688static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
3689{
3690#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003691 BIO *out;
3692 size_t rlen;
3693 char *txt;
3694 int res;
3695
3696 if (wpa_debug_level > MSG_DEBUG)
3697 return;
3698
3699 out = BIO_new(BIO_s_mem());
3700 if (!out)
3701 return;
3702
3703 OCSP_RESPONSE_print(out, rsp, 0);
3704 rlen = BIO_ctrl_pending(out);
3705 txt = os_malloc(rlen + 1);
3706 if (!txt) {
3707 BIO_free(out);
3708 return;
3709 }
3710
3711 res = BIO_read(out, txt, rlen);
3712 if (res > 0) {
3713 txt[res] = '\0';
3714 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
3715 }
3716 os_free(txt);
3717 BIO_free(out);
3718#endif /* CONFIG_NO_STDOUT_DEBUG */
3719}
3720
3721
Dmitry Shmidt71757432014-06-02 13:50:35 -07003722static void debug_print_cert(X509 *cert, const char *title)
3723{
3724#ifndef CONFIG_NO_STDOUT_DEBUG
3725 BIO *out;
3726 size_t rlen;
3727 char *txt;
3728 int res;
3729
3730 if (wpa_debug_level > MSG_DEBUG)
3731 return;
3732
3733 out = BIO_new(BIO_s_mem());
3734 if (!out)
3735 return;
3736
3737 X509_print(out, cert);
3738 rlen = BIO_ctrl_pending(out);
3739 txt = os_malloc(rlen + 1);
3740 if (!txt) {
3741 BIO_free(out);
3742 return;
3743 }
3744
3745 res = BIO_read(out, txt, rlen);
3746 if (res > 0) {
3747 txt[res] = '\0';
3748 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
3749 }
3750 os_free(txt);
3751
3752 BIO_free(out);
3753#endif /* CONFIG_NO_STDOUT_DEBUG */
3754}
3755
3756
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003757static int ocsp_resp_cb(SSL *s, void *arg)
3758{
3759 struct tls_connection *conn = arg;
3760 const unsigned char *p;
3761 int len, status, reason;
3762 OCSP_RESPONSE *rsp;
3763 OCSP_BASICRESP *basic;
3764 OCSP_CERTID *id;
3765 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003766 X509_STORE *store;
3767 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003768
3769 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
3770 if (!p) {
3771 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
3772 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3773 }
3774
3775 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
3776
3777 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
3778 if (!rsp) {
3779 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
3780 return 0;
3781 }
3782
3783 ocsp_debug_print_resp(rsp);
3784
3785 status = OCSP_response_status(rsp);
3786 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
3787 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
3788 status, OCSP_response_status_str(status));
3789 return 0;
3790 }
3791
3792 basic = OCSP_response_get1_basic(rsp);
3793 if (!basic) {
3794 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
3795 return 0;
3796 }
3797
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003798 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003799 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07003800 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003801
3802 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
3803 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003804 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003805 }
3806 certs = sk_X509_new_null();
3807 if (certs) {
3808 X509 *cert;
3809 cert = X509_dup(conn->peer_issuer);
3810 if (cert && !sk_X509_push(certs, cert)) {
3811 tls_show_errors(
3812 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003813 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003814 X509_free(cert);
3815 sk_X509_free(certs);
3816 certs = NULL;
3817 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003818 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003819 cert = X509_dup(conn->peer_issuer_issuer);
3820 if (cert && !sk_X509_push(certs, cert)) {
3821 tls_show_errors(
3822 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003823 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003824 X509_free(cert);
3825 }
3826 }
3827 }
3828 }
3829
3830 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
3831 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003832 if (status <= 0) {
3833 tls_show_errors(MSG_INFO, __func__,
3834 "OpenSSL: OCSP response failed verification");
3835 OCSP_BASICRESP_free(basic);
3836 OCSP_RESPONSE_free(rsp);
3837 return 0;
3838 }
3839
3840 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
3841
Dmitry Shmidt56052862013-10-04 10:23:25 -07003842 if (!conn->peer_cert) {
3843 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
3844 OCSP_BASICRESP_free(basic);
3845 OCSP_RESPONSE_free(rsp);
3846 return 0;
3847 }
3848
3849 if (!conn->peer_issuer) {
3850 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003851 OCSP_BASICRESP_free(basic);
3852 OCSP_RESPONSE_free(rsp);
3853 return 0;
3854 }
3855
3856 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
3857 if (!id) {
3858 wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier");
3859 OCSP_BASICRESP_free(basic);
3860 OCSP_RESPONSE_free(rsp);
3861 return 0;
3862 }
3863
3864 if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
3865 &this_update, &next_update)) {
3866 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
3867 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
3868 " (OCSP not required)");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003869 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003870 OCSP_BASICRESP_free(basic);
3871 OCSP_RESPONSE_free(rsp);
3872 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3873 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003874 OCSP_CERTID_free(id);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003875
3876 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
3877 tls_show_errors(MSG_INFO, __func__,
3878 "OpenSSL: OCSP status times invalid");
3879 OCSP_BASICRESP_free(basic);
3880 OCSP_RESPONSE_free(rsp);
3881 return 0;
3882 }
3883
3884 OCSP_BASICRESP_free(basic);
3885 OCSP_RESPONSE_free(rsp);
3886
3887 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
3888 OCSP_cert_status_str(status));
3889
3890 if (status == V_OCSP_CERTSTATUS_GOOD)
3891 return 1;
3892 if (status == V_OCSP_CERTSTATUS_REVOKED)
3893 return 0;
3894 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
3895 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
3896 return 0;
3897 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07003898 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003899 return 1;
3900}
3901
3902
3903static int ocsp_status_cb(SSL *s, void *arg)
3904{
3905 char *tmp;
3906 char *resp;
3907 size_t len;
3908
3909 if (tls_global->ocsp_stapling_response == NULL) {
3910 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
3911 return SSL_TLSEXT_ERR_OK;
3912 }
3913
3914 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
3915 if (resp == NULL) {
3916 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
3917 /* TODO: Build OCSPResponse with responseStatus = internalError
3918 */
3919 return SSL_TLSEXT_ERR_OK;
3920 }
3921 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
3922 tmp = OPENSSL_malloc(len);
3923 if (tmp == NULL) {
3924 os_free(resp);
3925 return SSL_TLSEXT_ERR_ALERT_FATAL;
3926 }
3927
3928 os_memcpy(tmp, resp, len);
3929 os_free(resp);
3930 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
3931
3932 return SSL_TLSEXT_ERR_OK;
3933}
3934
3935#endif /* HAVE_OCSP */
3936
3937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
3939 const struct tls_connection_params *params)
3940{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003941 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942 int ret;
3943 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003944 int can_pkcs11 = 0;
3945 const char *key_id = params->key_id;
3946 const char *cert_id = params->cert_id;
3947 const char *ca_cert_id = params->ca_cert_id;
3948 const char *engine_id = params->engine ? params->engine_id : NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949
3950 if (conn == NULL)
3951 return -1;
3952
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08003953 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
3954 wpa_printf(MSG_INFO,
3955 "OpenSSL: ocsp=3 not supported");
3956 return -1;
3957 }
3958
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003959 /*
3960 * If the engine isn't explicitly configured, and any of the
3961 * cert/key fields are actually PKCS#11 URIs, then automatically
3962 * use the PKCS#11 ENGINE.
3963 */
3964 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
3965 can_pkcs11 = 1;
3966
3967 if (!key_id && params->private_key && can_pkcs11 &&
3968 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
3969 can_pkcs11 = 2;
3970 key_id = params->private_key;
3971 }
3972
3973 if (!cert_id && params->client_cert && can_pkcs11 &&
3974 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
3975 can_pkcs11 = 2;
3976 cert_id = params->client_cert;
3977 }
3978
3979 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
3980 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
3981 can_pkcs11 = 2;
3982 ca_cert_id = params->ca_cert;
3983 }
3984
3985 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3986 if (can_pkcs11 == 2 && !engine_id)
3987 engine_id = "pkcs11";
3988
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003989#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003990#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003991 if (params->flags & TLS_CONN_EAP_FAST) {
3992 wpa_printf(MSG_DEBUG,
3993 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3994 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
3995 tls_show_errors(MSG_INFO, __func__,
3996 "Failed to set TLSv1_method() for EAP-FAST");
3997 return -1;
3998 }
3999 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004000#endif
4001#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 while ((err = ERR_get_error())) {
4004 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4005 __func__, ERR_error_string(err, NULL));
4006 }
4007
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004008 if (engine_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004010 ret = tls_engine_init(conn, engine_id, params->pin,
4011 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 if (ret)
4013 return ret;
4014 }
4015 if (tls_connection_set_subject_match(conn,
4016 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07004017 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004018 params->suffix_match,
4019 params->domain_match))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 return -1;
4021
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004022 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004023 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004025 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004026 params->ca_cert_blob,
4027 params->ca_cert_blob_len,
4028 params->ca_path))
4029 return -1;
4030
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004031 if (engine_id && cert_id) {
4032 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
4034 } else if (tls_connection_client_cert(conn, params->client_cert,
4035 params->client_cert_blob,
4036 params->client_cert_blob_len))
4037 return -1;
4038
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004039 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
4041 if (tls_connection_engine_private_key(conn))
4042 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004043 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 params->private_key,
4045 params->private_key_passwd,
4046 params->private_key_blob,
4047 params->private_key_blob_len)) {
4048 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
4049 params->private_key);
4050 return -1;
4051 }
4052
4053 if (tls_connection_dh(conn, params->dh_file)) {
4054 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
4055 params->dh_file);
4056 return -1;
4057 }
4058
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004059 if (params->openssl_ciphers &&
4060 SSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) {
4061 wpa_printf(MSG_INFO,
4062 "OpenSSL: Failed to set cipher string '%s'",
4063 params->openssl_ciphers);
4064 return -1;
4065 }
4066
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004067 tls_set_conn_flags(conn->ssl, params->flags);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004068
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004069#ifdef OPENSSL_IS_BORINGSSL
4070 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4071 SSL_enable_ocsp_stapling(conn->ssl);
4072 }
4073#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004074#ifdef HAVE_OCSP
4075 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004076 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004077 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
4078 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
4079 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
4080 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004081#else /* HAVE_OCSP */
4082 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
4083 wpa_printf(MSG_INFO,
4084 "OpenSSL: No OCSP support included - reject configuration");
4085 return -1;
4086 }
4087 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4088 wpa_printf(MSG_DEBUG,
4089 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4090 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004091#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004092#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004093
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004094 conn->flags = params->flags;
4095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004096 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097
4098 return 0;
4099}
4100
4101
4102int tls_global_set_params(void *tls_ctx,
4103 const struct tls_connection_params *params)
4104{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004105 struct tls_data *data = tls_ctx;
4106 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 unsigned long err;
4108
4109 while ((err = ERR_get_error())) {
4110 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4111 __func__, ERR_error_string(err, NULL));
4112 }
4113
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004114 if (tls_global_ca_cert(data, params->ca_cert) ||
4115 tls_global_client_cert(data, params->client_cert) ||
4116 tls_global_private_key(data, params->private_key,
4117 params->private_key_passwd) ||
4118 tls_global_dh(data, params->dh_file)) {
4119 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 return -1;
4121 }
4122
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004123 if (params->openssl_ciphers &&
4124 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
4125 wpa_printf(MSG_INFO,
4126 "OpenSSL: Failed to set cipher string '%s'",
4127 params->openssl_ciphers);
4128 return -1;
4129 }
4130
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004131#ifdef SSL_OP_NO_TICKET
4132 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
4133 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004134#ifdef SSL_CTX_clear_options
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004135 else
4136 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004137#endif /* SSL_clear_options */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004138#endif /* SSL_OP_NO_TICKET */
4139
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004140#ifdef HAVE_OCSP
4141 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
4142 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
4143 os_free(tls_global->ocsp_stapling_response);
4144 if (params->ocsp_stapling_response)
4145 tls_global->ocsp_stapling_response =
4146 os_strdup(params->ocsp_stapling_response);
4147 else
4148 tls_global->ocsp_stapling_response = NULL;
4149#endif /* HAVE_OCSP */
4150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 return 0;
4152}
4153
4154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4156/* Pre-shared secred requires a patch to openssl, so this function is
4157 * commented out unless explicitly needed for EAP-FAST in order to be able to
4158 * build this file with unmodified openssl. */
4159
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004160#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004161static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4162 STACK_OF(SSL_CIPHER) *peer_ciphers,
4163 const SSL_CIPHER **cipher, void *arg)
4164#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004165static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4166 STACK_OF(SSL_CIPHER) *peer_ciphers,
4167 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004168#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169{
4170 struct tls_connection *conn = arg;
4171 int ret;
4172
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004173#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174 if (conn == NULL || conn->session_ticket_cb == NULL)
4175 return 0;
4176
4177 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4178 conn->session_ticket,
4179 conn->session_ticket_len,
4180 s->s3->client_random,
4181 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004182#else
4183 unsigned char client_random[SSL3_RANDOM_SIZE];
4184 unsigned char server_random[SSL3_RANDOM_SIZE];
4185
4186 if (conn == NULL || conn->session_ticket_cb == NULL)
4187 return 0;
4188
4189 SSL_get_client_random(s, client_random, sizeof(client_random));
4190 SSL_get_server_random(s, server_random, sizeof(server_random));
4191
4192 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4193 conn->session_ticket,
4194 conn->session_ticket_len,
4195 client_random,
4196 server_random, secret);
4197#endif
4198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 os_free(conn->session_ticket);
4200 conn->session_ticket = NULL;
4201
4202 if (ret <= 0)
4203 return 0;
4204
4205 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
4206 return 1;
4207}
4208
4209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004210static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
4211 int len, void *arg)
4212{
4213 struct tls_connection *conn = arg;
4214
4215 if (conn == NULL || conn->session_ticket_cb == NULL)
4216 return 0;
4217
4218 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
4219
4220 os_free(conn->session_ticket);
4221 conn->session_ticket = NULL;
4222
4223 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
4224 "extension", data, len);
4225
4226 conn->session_ticket = os_malloc(len);
4227 if (conn->session_ticket == NULL)
4228 return 0;
4229
4230 os_memcpy(conn->session_ticket, data, len);
4231 conn->session_ticket_len = len;
4232
4233 return 1;
4234}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004235#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4236
4237
4238int tls_connection_set_session_ticket_cb(void *tls_ctx,
4239 struct tls_connection *conn,
4240 tls_session_ticket_cb cb,
4241 void *ctx)
4242{
4243#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4244 conn->session_ticket_cb = cb;
4245 conn->session_ticket_cb_ctx = ctx;
4246
4247 if (cb) {
4248 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
4249 conn) != 1)
4250 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251 SSL_set_session_ticket_ext_cb(conn->ssl,
4252 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 } else {
4254 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
4255 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 }
4258
4259 return 0;
4260#else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4261 return -1;
4262#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4263}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004264
4265
4266int tls_get_library_version(char *buf, size_t buf_len)
4267{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004268#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004269 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4270 OPENSSL_VERSION_TEXT,
4271 OpenSSL_version(OPENSSL_VERSION));
4272#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004273 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4274 OPENSSL_VERSION_TEXT,
4275 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004276#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004277}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004278
4279
4280void tls_connection_set_success_data(struct tls_connection *conn,
4281 struct wpabuf *data)
4282{
4283 SSL_SESSION *sess;
4284 struct wpabuf *old;
4285
4286 if (tls_ex_idx_session < 0)
4287 goto fail;
4288 sess = SSL_get_session(conn->ssl);
4289 if (!sess)
4290 goto fail;
4291 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4292 if (old) {
4293 wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
4294 old);
4295 wpabuf_free(old);
4296 }
4297 if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
4298 goto fail;
4299
4300 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
4301 conn->success_data = 1;
4302 return;
4303
4304fail:
4305 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
4306 wpabuf_free(data);
4307}
4308
4309
4310void tls_connection_set_success_data_resumed(struct tls_connection *conn)
4311{
4312 wpa_printf(MSG_DEBUG,
4313 "OpenSSL: Success data accepted for resumed session");
4314 conn->success_data = 1;
4315}
4316
4317
4318const struct wpabuf *
4319tls_connection_get_success_data(struct tls_connection *conn)
4320{
4321 SSL_SESSION *sess;
4322
4323 if (tls_ex_idx_session < 0 ||
4324 !(sess = SSL_get_session(conn->ssl)))
4325 return NULL;
4326 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4327}
4328
4329
4330void tls_connection_remove_session(struct tls_connection *conn)
4331{
4332 SSL_SESSION *sess;
4333
4334 sess = SSL_get_session(conn->ssl);
4335 if (!sess)
4336 return;
4337
4338 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
4339 wpa_printf(MSG_DEBUG,
4340 "OpenSSL: Session was not cached");
4341 else
4342 wpa_printf(MSG_DEBUG,
4343 "OpenSSL: Removed cached session to disable session resumption");
4344}