blob: e34a3d075996f5e143f9eaa1b04b84705df010b2 [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>
21#include <openssl/pkcs12.h>
22#include <openssl/x509v3.h>
23#ifndef OPENSSL_NO_ENGINE
24#include <openssl/engine.h>
25#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080026#ifndef OPENSSL_NO_DSA
27#include <openssl/dsa.h>
28#endif
29#ifndef OPENSSL_NO_DH
30#include <openssl/dh.h>
31#endif
32
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "common.h"
34#include "crypto.h"
Dmitry Shmidtaf9da312015-04-03 10:03:11 -070035#include "sha1.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080036#include "sha256.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "tls.h"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080038#include "tls_openssl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070040#if defined(OPENSSL_IS_BORINGSSL)
41/* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
42typedef size_t stack_index_t;
43#else
44typedef int stack_index_t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#endif
46
Dmitry Shmidt34af3062013-07-11 10:46:32 -070047#ifdef SSL_set_tlsext_status_type
48#ifndef OPENSSL_NO_TLSEXT
49#define HAVE_OCSP
50#include <openssl/ocsp.h>
51#endif /* OPENSSL_NO_TLSEXT */
52#endif /* SSL_set_tlsext_status_type */
53
Dmitry Shmidtde47be72016-01-07 12:52:55 -080054#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
55/*
56 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
57 * 1.1.0. Provide compatibility wrappers for older versions.
58 */
59
60static size_t SSL_get_client_random(const SSL *ssl, unsigned char *out,
61 size_t outlen)
62{
63 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
64 return 0;
65 os_memcpy(out, ssl->s3->client_random, SSL3_RANDOM_SIZE);
66 return SSL3_RANDOM_SIZE;
67}
68
69
70static size_t SSL_get_server_random(const SSL *ssl, unsigned char *out,
71 size_t outlen)
72{
73 if (!ssl->s3 || outlen < SSL3_RANDOM_SIZE)
74 return 0;
75 os_memcpy(out, ssl->s3->server_random, SSL3_RANDOM_SIZE);
76 return SSL3_RANDOM_SIZE;
77}
78
79
80static size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
81 unsigned char *out, size_t outlen)
82{
83 if (!session || session->master_key_length < 0 ||
84 (size_t) session->master_key_length > outlen)
85 return 0;
86 if ((size_t) session->master_key_length < outlen)
87 outlen = session->master_key_length;
88 os_memcpy(out, session->master_key, outlen);
89 return outlen;
90}
91
92#endif
93
Dmitry Shmidtff079172013-11-08 14:10:30 -080094#ifdef ANDROID
95#include <openssl/pem.h>
96#include <keystore/keystore_get.h>
97
98static BIO * BIO_from_keystore(const char *key)
99{
100 BIO *bio = NULL;
101 uint8_t *value = NULL;
102 int length = keystore_get(key, strlen(key), &value);
103 if (length != -1 && (bio = BIO_new(BIO_s_mem())) != NULL)
104 BIO_write(bio, value, length);
105 free(value);
106 return bio;
107}
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800108
109
110static int tls_add_ca_from_keystore(X509_STORE *ctx, const char *key_alias)
111{
112 BIO *bio = BIO_from_keystore(key_alias);
113 STACK_OF(X509_INFO) *stack = NULL;
114 stack_index_t i;
115
116 if (bio) {
117 stack = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
118 BIO_free(bio);
119 }
120
121 if (!stack) {
122 wpa_printf(MSG_WARNING, "TLS: Failed to parse certificate: %s",
123 key_alias);
124 return -1;
125 }
126
127 for (i = 0; i < sk_X509_INFO_num(stack); ++i) {
128 X509_INFO *info = sk_X509_INFO_value(stack, i);
129
130 if (info->x509)
131 X509_STORE_add_cert(ctx, info->x509);
132 if (info->crl)
133 X509_STORE_add_crl(ctx, info->crl);
134 }
135
136 sk_X509_INFO_pop_free(stack, X509_INFO_free);
137
138 return 0;
139}
140
141
142static int tls_add_ca_from_keystore_encoded(X509_STORE *ctx,
143 const char *encoded_key_alias)
144{
145 int rc = -1;
146 int len = os_strlen(encoded_key_alias);
147 unsigned char *decoded_alias;
148
149 if (len & 1) {
150 wpa_printf(MSG_WARNING, "Invalid hex-encoded alias: %s",
151 encoded_key_alias);
152 return rc;
153 }
154
155 decoded_alias = os_malloc(len / 2 + 1);
156 if (decoded_alias) {
157 if (!hexstr2bin(encoded_key_alias, decoded_alias, len / 2)) {
158 decoded_alias[len / 2] = '\0';
159 rc = tls_add_ca_from_keystore(
160 ctx, (const char *) decoded_alias);
161 }
162 os_free(decoded_alias);
163 }
164
165 return rc;
166}
167
Dmitry Shmidtff079172013-11-08 14:10:30 -0800168#endif /* ANDROID */
169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170static int tls_openssl_ref_count = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800171static int tls_ex_idx_session = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700173struct tls_context {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174 void (*event_cb)(void *ctx, enum tls_event ev,
175 union tls_event_data *data);
176 void *cb_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177 int cert_in_cb;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700178 char *ocsp_stapling_response;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179};
180
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700181static struct tls_context *tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182
183
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800184struct tls_data {
185 SSL_CTX *ssl;
186 unsigned int tls_session_lifetime;
187};
188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189struct tls_connection {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700190 struct tls_context *context;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800191 SSL_CTX *ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 SSL *ssl;
193 BIO *ssl_in, *ssl_out;
Adam Langley1eb02ed2015-04-21 19:00:05 -0700194#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 ENGINE *engine; /* functional reference to the engine */
196 EVP_PKEY *private_key; /* the private key if using engine */
197#endif /* OPENSSL_NO_ENGINE */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800198 char *subject_match, *altsubject_match, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 int read_alerts, write_alerts, failed;
200
201 tls_session_ticket_cb session_ticket_cb;
202 void *session_ticket_cb_ctx;
203
204 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
205 u8 *session_ticket;
206 size_t session_ticket_len;
207
208 unsigned int ca_cert_verify:1;
209 unsigned int cert_probe:1;
210 unsigned int server_cert_only:1;
Jouni Malinen26af48b2014-04-09 13:02:53 +0300211 unsigned int invalid_hb_used:1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800212 unsigned int success_data:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213
214 u8 srv_cert_hash[32];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700215
216 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700217
218 X509 *peer_cert;
219 X509 *peer_issuer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800220 X509 *peer_issuer_issuer;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800221
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800222 unsigned char client_random[SSL3_RANDOM_SIZE];
223 unsigned char server_random[SSL3_RANDOM_SIZE];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224};
225
226
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700227static struct tls_context * tls_context_new(const struct tls_config *conf)
228{
229 struct tls_context *context = os_zalloc(sizeof(*context));
230 if (context == NULL)
231 return NULL;
232 if (conf) {
233 context->event_cb = conf->event_cb;
234 context->cb_ctx = conf->cb_ctx;
235 context->cert_in_cb = conf->cert_in_cb;
236 }
237 return context;
238}
239
240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241#ifdef CONFIG_NO_STDOUT_DEBUG
242
243static void _tls_show_errors(void)
244{
245 unsigned long err;
246
247 while ((err = ERR_get_error())) {
248 /* Just ignore the errors, since stdout is disabled */
249 }
250}
251#define tls_show_errors(l, f, t) _tls_show_errors()
252
253#else /* CONFIG_NO_STDOUT_DEBUG */
254
255static void tls_show_errors(int level, const char *func, const char *txt)
256{
257 unsigned long err;
258
259 wpa_printf(level, "OpenSSL: %s - %s %s",
260 func, txt, ERR_error_string(ERR_get_error(), NULL));
261
262 while ((err = ERR_get_error())) {
263 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
264 ERR_error_string(err, NULL));
265 }
266}
267
268#endif /* CONFIG_NO_STDOUT_DEBUG */
269
270
271#ifdef CONFIG_NATIVE_WINDOWS
272
273/* Windows CryptoAPI and access to certificate stores */
274#include <wincrypt.h>
275
276#ifdef __MINGW32_VERSION
277/*
278 * MinGW does not yet include all the needed definitions for CryptoAPI, so
279 * define here whatever extra is needed.
280 */
281#define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
282#define CERT_STORE_READONLY_FLAG 0x00008000
283#define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
284
285#endif /* __MINGW32_VERSION */
286
287
288struct cryptoapi_rsa_data {
289 const CERT_CONTEXT *cert;
290 HCRYPTPROV crypt_prov;
291 DWORD key_spec;
292 BOOL free_crypt_prov;
293};
294
295
296static void cryptoapi_error(const char *msg)
297{
298 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
299 msg, (unsigned int) GetLastError());
300}
301
302
303static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
304 unsigned char *to, RSA *rsa, int padding)
305{
306 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
307 return 0;
308}
309
310
311static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
312 unsigned char *to, RSA *rsa, int padding)
313{
314 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
315 return 0;
316}
317
318
319static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
320 unsigned char *to, RSA *rsa, int padding)
321{
322 struct cryptoapi_rsa_data *priv =
323 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
324 HCRYPTHASH hash;
325 DWORD hash_size, len, i;
326 unsigned char *buf = NULL;
327 int ret = 0;
328
329 if (priv == NULL) {
330 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
331 ERR_R_PASSED_NULL_PARAMETER);
332 return 0;
333 }
334
335 if (padding != RSA_PKCS1_PADDING) {
336 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
337 RSA_R_UNKNOWN_PADDING_TYPE);
338 return 0;
339 }
340
341 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
342 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
343 __func__);
344 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
345 RSA_R_INVALID_MESSAGE_LENGTH);
346 return 0;
347 }
348
349 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
350 {
351 cryptoapi_error("CryptCreateHash failed");
352 return 0;
353 }
354
355 len = sizeof(hash_size);
356 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
357 0)) {
358 cryptoapi_error("CryptGetHashParam failed");
359 goto err;
360 }
361
362 if ((int) hash_size != flen) {
363 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
364 (unsigned) hash_size, flen);
365 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
366 RSA_R_INVALID_MESSAGE_LENGTH);
367 goto err;
368 }
369 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
370 cryptoapi_error("CryptSetHashParam failed");
371 goto err;
372 }
373
374 len = RSA_size(rsa);
375 buf = os_malloc(len);
376 if (buf == NULL) {
377 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
378 goto err;
379 }
380
381 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
382 cryptoapi_error("CryptSignHash failed");
383 goto err;
384 }
385
386 for (i = 0; i < len; i++)
387 to[i] = buf[len - i - 1];
388 ret = len;
389
390err:
391 os_free(buf);
392 CryptDestroyHash(hash);
393
394 return ret;
395}
396
397
398static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
399 unsigned char *to, RSA *rsa, int padding)
400{
401 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
402 return 0;
403}
404
405
406static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
407{
408 if (priv == NULL)
409 return;
410 if (priv->crypt_prov && priv->free_crypt_prov)
411 CryptReleaseContext(priv->crypt_prov, 0);
412 if (priv->cert)
413 CertFreeCertificateContext(priv->cert);
414 os_free(priv);
415}
416
417
418static int cryptoapi_finish(RSA *rsa)
419{
420 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
421 os_free((void *) rsa->meth);
422 rsa->meth = NULL;
423 return 1;
424}
425
426
427static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
428{
429 HCERTSTORE cs;
430 const CERT_CONTEXT *ret = NULL;
431
432 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
433 store | CERT_STORE_OPEN_EXISTING_FLAG |
434 CERT_STORE_READONLY_FLAG, L"MY");
435 if (cs == NULL) {
436 cryptoapi_error("Failed to open 'My system store'");
437 return NULL;
438 }
439
440 if (strncmp(name, "cert://", 7) == 0) {
441 unsigned short wbuf[255];
442 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
443 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
444 PKCS_7_ASN_ENCODING,
445 0, CERT_FIND_SUBJECT_STR,
446 wbuf, NULL);
447 } else if (strncmp(name, "hash://", 7) == 0) {
448 CRYPT_HASH_BLOB blob;
449 int len;
450 const char *hash = name + 7;
451 unsigned char *buf;
452
453 len = os_strlen(hash) / 2;
454 buf = os_malloc(len);
455 if (buf && hexstr2bin(hash, buf, len) == 0) {
456 blob.cbData = len;
457 blob.pbData = buf;
458 ret = CertFindCertificateInStore(cs,
459 X509_ASN_ENCODING |
460 PKCS_7_ASN_ENCODING,
461 0, CERT_FIND_HASH,
462 &blob, NULL);
463 }
464 os_free(buf);
465 }
466
467 CertCloseStore(cs, 0);
468
469 return ret;
470}
471
472
473static int tls_cryptoapi_cert(SSL *ssl, const char *name)
474{
475 X509 *cert = NULL;
476 RSA *rsa = NULL, *pub_rsa;
477 struct cryptoapi_rsa_data *priv;
478 RSA_METHOD *rsa_meth;
479
480 if (name == NULL ||
481 (strncmp(name, "cert://", 7) != 0 &&
482 strncmp(name, "hash://", 7) != 0))
483 return -1;
484
485 priv = os_zalloc(sizeof(*priv));
486 rsa_meth = os_zalloc(sizeof(*rsa_meth));
487 if (priv == NULL || rsa_meth == NULL) {
488 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
489 "for CryptoAPI RSA method");
490 os_free(priv);
491 os_free(rsa_meth);
492 return -1;
493 }
494
495 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
496 if (priv->cert == NULL) {
497 priv->cert = cryptoapi_find_cert(
498 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
499 }
500 if (priv->cert == NULL) {
501 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
502 "'%s'", name);
503 goto err;
504 }
505
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800506 cert = d2i_X509(NULL,
507 (const unsigned char **) &priv->cert->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 priv->cert->cbCertEncoded);
509 if (cert == NULL) {
510 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
511 "encoding");
512 goto err;
513 }
514
515 if (!CryptAcquireCertificatePrivateKey(priv->cert,
516 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
517 NULL, &priv->crypt_prov,
518 &priv->key_spec,
519 &priv->free_crypt_prov)) {
520 cryptoapi_error("Failed to acquire a private key for the "
521 "certificate");
522 goto err;
523 }
524
525 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
526 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
527 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
528 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
529 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
530 rsa_meth->finish = cryptoapi_finish;
531 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
532 rsa_meth->app_data = (char *) priv;
533
534 rsa = RSA_new();
535 if (rsa == NULL) {
536 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
537 ERR_R_MALLOC_FAILURE);
538 goto err;
539 }
540
541 if (!SSL_use_certificate(ssl, cert)) {
542 RSA_free(rsa);
543 rsa = NULL;
544 goto err;
545 }
546 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
547 X509_free(cert);
548 cert = NULL;
549
550 rsa->n = BN_dup(pub_rsa->n);
551 rsa->e = BN_dup(pub_rsa->e);
552 if (!RSA_set_method(rsa, rsa_meth))
553 goto err;
554
555 if (!SSL_use_RSAPrivateKey(ssl, rsa))
556 goto err;
557 RSA_free(rsa);
558
559 return 0;
560
561err:
562 if (cert)
563 X509_free(cert);
564 if (rsa)
565 RSA_free(rsa);
566 else {
567 os_free(rsa_meth);
568 cryptoapi_free_data(priv);
569 }
570 return -1;
571}
572
573
574static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
575{
576 HCERTSTORE cs;
577 PCCERT_CONTEXT ctx = NULL;
578 X509 *cert;
579 char buf[128];
580 const char *store;
581#ifdef UNICODE
582 WCHAR *wstore;
583#endif /* UNICODE */
584
585 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
586 return -1;
587
588 store = name + 13;
589#ifdef UNICODE
590 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
591 if (wstore == NULL)
592 return -1;
593 wsprintf(wstore, L"%S", store);
594 cs = CertOpenSystemStore(0, wstore);
595 os_free(wstore);
596#else /* UNICODE */
597 cs = CertOpenSystemStore(0, store);
598#endif /* UNICODE */
599 if (cs == NULL) {
600 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
601 "'%s': error=%d", __func__, store,
602 (int) GetLastError());
603 return -1;
604 }
605
606 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607 cert = d2i_X509(NULL,
608 (const unsigned char **) &ctx->pbCertEncoded,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 ctx->cbCertEncoded);
610 if (cert == NULL) {
611 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
612 "X509 DER encoding for CA cert");
613 continue;
614 }
615
616 X509_NAME_oneline(X509_get_subject_name(cert), buf,
617 sizeof(buf));
618 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
619 "system certificate store: subject='%s'", buf);
620
621 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
622 tls_show_errors(MSG_WARNING, __func__,
623 "Failed to add ca_cert to OpenSSL "
624 "certificate store");
625 }
626
627 X509_free(cert);
628 }
629
630 if (!CertCloseStore(cs, 0)) {
631 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
632 "'%s': error=%d", __func__, name + 13,
633 (int) GetLastError());
634 }
635
636 return 0;
637}
638
639
640#else /* CONFIG_NATIVE_WINDOWS */
641
642static int tls_cryptoapi_cert(SSL *ssl, const char *name)
643{
644 return -1;
645}
646
647#endif /* CONFIG_NATIVE_WINDOWS */
648
649
650static void ssl_info_cb(const SSL *ssl, int where, int ret)
651{
652 const char *str;
653 int w;
654
655 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
656 w = where & ~SSL_ST_MASK;
657 if (w & SSL_ST_CONNECT)
658 str = "SSL_connect";
659 else if (w & SSL_ST_ACCEPT)
660 str = "SSL_accept";
661 else
662 str = "undefined";
663
664 if (where & SSL_CB_LOOP) {
665 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
666 str, SSL_state_string_long(ssl));
667 } else if (where & SSL_CB_ALERT) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700668 struct tls_connection *conn = SSL_get_app_data((SSL *) ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
670 where & SSL_CB_READ ?
671 "read (remote end reported an error)" :
672 "write (local SSL3 detected an error)",
673 SSL_alert_type_string_long(ret),
674 SSL_alert_desc_string_long(ret));
675 if ((ret >> 8) == SSL3_AL_FATAL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 if (where & SSL_CB_READ)
677 conn->read_alerts++;
678 else
679 conn->write_alerts++;
680 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700681 if (conn->context->event_cb != NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700682 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700683 struct tls_context *context = conn->context;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700684 os_memset(&ev, 0, sizeof(ev));
685 ev.alert.is_local = !(where & SSL_CB_READ);
686 ev.alert.type = SSL_alert_type_string_long(ret);
687 ev.alert.description = SSL_alert_desc_string_long(ret);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700688 context->event_cb(context->cb_ctx, TLS_ALERT, &ev);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700689 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 } else if (where & SSL_CB_EXIT && ret <= 0) {
691 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
692 str, ret == 0 ? "failed" : "error",
693 SSL_state_string_long(ssl));
694 }
695}
696
697
698#ifndef OPENSSL_NO_ENGINE
699/**
700 * tls_engine_load_dynamic_generic - load any openssl engine
701 * @pre: an array of commands and values that load an engine initialized
702 * in the engine specific function
703 * @post: an array of commands and values that initialize an already loaded
704 * engine (or %NULL if not required)
705 * @id: the engine id of the engine to load (only required if post is not %NULL
706 *
707 * This function is a generic function that loads any openssl engine.
708 *
709 * Returns: 0 on success, -1 on failure
710 */
711static int tls_engine_load_dynamic_generic(const char *pre[],
712 const char *post[], const char *id)
713{
714 ENGINE *engine;
715 const char *dynamic_id = "dynamic";
716
717 engine = ENGINE_by_id(id);
718 if (engine) {
719 ENGINE_free(engine);
720 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
721 "available", id);
722 return 0;
723 }
724 ERR_clear_error();
725
726 engine = ENGINE_by_id(dynamic_id);
727 if (engine == NULL) {
728 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
729 dynamic_id,
730 ERR_error_string(ERR_get_error(), NULL));
731 return -1;
732 }
733
734 /* Perform the pre commands. This will load the engine. */
735 while (pre && pre[0]) {
736 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
737 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
738 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
739 "%s %s [%s]", pre[0], pre[1],
740 ERR_error_string(ERR_get_error(), NULL));
741 ENGINE_free(engine);
742 return -1;
743 }
744 pre += 2;
745 }
746
747 /*
748 * Free the reference to the "dynamic" engine. The loaded engine can
749 * now be looked up using ENGINE_by_id().
750 */
751 ENGINE_free(engine);
752
753 engine = ENGINE_by_id(id);
754 if (engine == NULL) {
755 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
756 id, ERR_error_string(ERR_get_error(), NULL));
757 return -1;
758 }
759
760 while (post && post[0]) {
761 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
762 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
763 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
764 " %s %s [%s]", post[0], post[1],
765 ERR_error_string(ERR_get_error(), NULL));
766 ENGINE_remove(engine);
767 ENGINE_free(engine);
768 return -1;
769 }
770 post += 2;
771 }
772 ENGINE_free(engine);
773
774 return 0;
775}
776
777
778/**
779 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
780 * @pkcs11_so_path: pksc11_so_path from the configuration
781 * @pcks11_module_path: pkcs11_module_path from the configuration
782 */
783static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
784 const char *pkcs11_module_path)
785{
786 char *engine_id = "pkcs11";
787 const char *pre_cmd[] = {
788 "SO_PATH", NULL /* pkcs11_so_path */,
789 "ID", NULL /* engine_id */,
790 "LIST_ADD", "1",
791 /* "NO_VCHECK", "1", */
792 "LOAD", NULL,
793 NULL, NULL
794 };
795 const char *post_cmd[] = {
796 "MODULE_PATH", NULL /* pkcs11_module_path */,
797 NULL, NULL
798 };
799
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800800 if (!pkcs11_so_path)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801 return 0;
802
803 pre_cmd[1] = pkcs11_so_path;
804 pre_cmd[3] = engine_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800805 if (pkcs11_module_path)
806 post_cmd[1] = pkcs11_module_path;
807 else
808 post_cmd[0] = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809
810 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
811 pkcs11_so_path);
812
813 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
814}
815
816
817/**
818 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
819 * @opensc_so_path: opensc_so_path from the configuration
820 */
821static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
822{
823 char *engine_id = "opensc";
824 const char *pre_cmd[] = {
825 "SO_PATH", NULL /* opensc_so_path */,
826 "ID", NULL /* engine_id */,
827 "LIST_ADD", "1",
828 "LOAD", NULL,
829 NULL, NULL
830 };
831
832 if (!opensc_so_path)
833 return 0;
834
835 pre_cmd[1] = opensc_so_path;
836 pre_cmd[3] = engine_id;
837
838 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
839 opensc_so_path);
840
841 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
842}
843#endif /* OPENSSL_NO_ENGINE */
844
845
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800846static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
847{
848 struct wpabuf *buf;
849
850 if (tls_ex_idx_session < 0)
851 return;
852 buf = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
853 if (!buf)
854 return;
855 wpa_printf(MSG_DEBUG,
856 "OpenSSL: Free application session data %p (sess %p)",
857 buf, sess);
858 wpabuf_free(buf);
859
860 SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
861}
862
863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864void * tls_init(const struct tls_config *conf)
865{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800866 struct tls_data *data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 SSL_CTX *ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700868 struct tls_context *context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800869 const char *ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870
871 if (tls_openssl_ref_count == 0) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700872 tls_global = context = tls_context_new(conf);
873 if (context == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875#ifdef CONFIG_FIPS
876#ifdef OPENSSL_FIPS
877 if (conf && conf->fips_mode) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800878 static int fips_enabled = 0;
879
880 if (!fips_enabled && !FIPS_mode_set(1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
882 "mode");
883 ERR_load_crypto_strings();
884 ERR_print_errors_fp(stderr);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700885 os_free(tls_global);
886 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800888 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889 wpa_printf(MSG_INFO, "Running in FIPS mode");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800890 fips_enabled = 1;
891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
893#else /* OPENSSL_FIPS */
894 if (conf && conf->fips_mode) {
895 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
896 "supported");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897 os_free(tls_global);
898 tls_global = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 return NULL;
900 }
901#endif /* OPENSSL_FIPS */
902#endif /* CONFIG_FIPS */
903 SSL_load_error_strings();
904 SSL_library_init();
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800905#ifndef OPENSSL_NO_SHA256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 EVP_add_digest(EVP_sha256());
907#endif /* OPENSSL_NO_SHA256 */
908 /* TODO: if /dev/urandom is available, PRNG is seeded
909 * automatically. If this is not the case, random data should
910 * be added here. */
911
912#ifdef PKCS12_FUNCS
913#ifndef OPENSSL_NO_RC2
914 /*
915 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
916 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
917 * versions, but it looks like OpenSSL 1.0.0 does not do that
918 * anymore.
919 */
920 EVP_add_cipher(EVP_rc2_40_cbc());
921#endif /* OPENSSL_NO_RC2 */
922 PKCS12_PBE_add();
923#endif /* PKCS12_FUNCS */
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700924 } else {
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700925 context = tls_context_new(conf);
926 if (context == NULL)
927 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 }
929 tls_openssl_ref_count++;
930
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800931 data = os_zalloc(sizeof(*data));
932 if (data)
933 ssl = SSL_CTX_new(SSLv23_method());
934 else
935 ssl = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700936 if (ssl == NULL) {
937 tls_openssl_ref_count--;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700938 if (context != tls_global)
939 os_free(context);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700940 if (tls_openssl_ref_count == 0) {
941 os_free(tls_global);
942 tls_global = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 return NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700945 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800946 data->ssl = ssl;
947 if (conf)
948 data->tls_session_lifetime = conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800950 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv2);
951 SSL_CTX_set_options(ssl, SSL_OP_NO_SSLv3);
952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700954 SSL_CTX_set_app_data(ssl, context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800955 if (data->tls_session_lifetime > 0) {
956 SSL_CTX_set_quiet_shutdown(ssl, 1);
957 /*
958 * Set default context here. In practice, this will be replaced
959 * by the per-EAP method context in tls_connection_set_verify().
960 */
961 SSL_CTX_set_session_id_context(ssl, (u8 *) "hostapd", 7);
962 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_SERVER);
963 SSL_CTX_set_timeout(ssl, data->tls_session_lifetime);
964 SSL_CTX_sess_set_remove_cb(ssl, remove_session_cb);
965 } else {
966 SSL_CTX_set_session_cache_mode(ssl, SSL_SESS_CACHE_OFF);
967 }
968
969 if (tls_ex_idx_session < 0) {
970 tls_ex_idx_session = SSL_SESSION_get_ex_new_index(
971 0, NULL, NULL, NULL, NULL);
972 if (tls_ex_idx_session < 0) {
973 tls_deinit(data);
974 return NULL;
975 }
976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977
978#ifndef OPENSSL_NO_ENGINE
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800979 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
980 ERR_load_ENGINE_strings();
981 ENGINE_load_dynamic();
982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 if (conf &&
984 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
985 conf->pkcs11_module_path)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
987 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
988 conf->pkcs11_module_path)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800989 tls_deinit(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 return NULL;
991 }
992 }
993#endif /* OPENSSL_NO_ENGINE */
994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800995 if (conf && conf->openssl_ciphers)
996 ciphers = conf->openssl_ciphers;
997 else
998 ciphers = "DEFAULT:!EXP:!LOW";
999 if (SSL_CTX_set_cipher_list(ssl, ciphers) != 1) {
1000 wpa_printf(MSG_ERROR,
1001 "OpenSSL: Failed to set cipher string '%s'",
1002 ciphers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001003 tls_deinit(data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001004 return NULL;
1005 }
1006
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001007 return data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008}
1009
1010
1011void tls_deinit(void *ssl_ctx)
1012{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001013 struct tls_data *data = ssl_ctx;
1014 SSL_CTX *ssl = data->ssl;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001015 struct tls_context *context = SSL_CTX_get_app_data(ssl);
1016 if (context != tls_global)
1017 os_free(context);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001018 if (data->tls_session_lifetime > 0)
1019 SSL_CTX_flush_sessions(ssl, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 SSL_CTX_free(ssl);
1021
1022 tls_openssl_ref_count--;
1023 if (tls_openssl_ref_count == 0) {
1024#ifndef OPENSSL_NO_ENGINE
1025 ENGINE_cleanup();
1026#endif /* OPENSSL_NO_ENGINE */
1027 CRYPTO_cleanup_all_ex_data();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001028 ERR_remove_thread_state(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 ERR_free_strings();
1030 EVP_cleanup();
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001031 os_free(tls_global->ocsp_stapling_response);
1032 tls_global->ocsp_stapling_response = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 os_free(tls_global);
1034 tls_global = NULL;
1035 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001036
1037 os_free(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038}
1039
1040
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001041#ifndef OPENSSL_NO_ENGINE
1042
1043/* Cryptoki return values */
1044#define CKR_PIN_INCORRECT 0x000000a0
1045#define CKR_PIN_INVALID 0x000000a1
1046#define CKR_PIN_LEN_RANGE 0x000000a2
1047
1048/* libp11 */
1049#define ERR_LIB_PKCS11 ERR_LIB_USER
1050
1051static int tls_is_pin_error(unsigned int err)
1052{
1053 return ERR_GET_LIB(err) == ERR_LIB_PKCS11 &&
1054 (ERR_GET_REASON(err) == CKR_PIN_INCORRECT ||
1055 ERR_GET_REASON(err) == CKR_PIN_INVALID ||
1056 ERR_GET_REASON(err) == CKR_PIN_LEN_RANGE);
1057}
1058
1059#endif /* OPENSSL_NO_ENGINE */
1060
1061
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001062#ifdef ANDROID
1063/* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1064EVP_PKEY * EVP_PKEY_from_keystore(const char *key_id);
1065#endif /* ANDROID */
1066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
1068 const char *pin, const char *key_id,
1069 const char *cert_id, const char *ca_cert_id)
1070{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001071#if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1072#if !defined(OPENSSL_NO_ENGINE)
1073#error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1074#endif
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001075 if (!key_id)
1076 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Adam Langley1eb02ed2015-04-21 19:00:05 -07001077 conn->engine = NULL;
1078 conn->private_key = EVP_PKEY_from_keystore(key_id);
1079 if (!conn->private_key) {
1080 wpa_printf(MSG_ERROR,
1081 "ENGINE: cannot load private key with id '%s' [%s]",
1082 key_id,
1083 ERR_error_string(ERR_get_error(), NULL));
1084 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1085 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001086#endif /* ANDROID && OPENSSL_IS_BORINGSSL */
Adam Langley1eb02ed2015-04-21 19:00:05 -07001087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088#ifndef OPENSSL_NO_ENGINE
1089 int ret = -1;
1090 if (engine_id == NULL) {
1091 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
1092 return -1;
1093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094
1095 ERR_clear_error();
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001096#ifdef ANDROID
1097 ENGINE_load_dynamic();
1098#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 conn->engine = ENGINE_by_id(engine_id);
1100 if (!conn->engine) {
1101 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
1102 engine_id, ERR_error_string(ERR_get_error(), NULL));
1103 goto err;
1104 }
1105 if (ENGINE_init(conn->engine) != 1) {
1106 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
1107 "(engine: %s) [%s]", engine_id,
1108 ERR_error_string(ERR_get_error(), NULL));
1109 goto err;
1110 }
1111 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
1112
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001113#ifndef ANDROID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001114 if (pin && ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
1116 ERR_error_string(ERR_get_error(), NULL));
1117 goto err;
1118 }
Kenny Rootdb3c5a42012-03-20 17:00:47 -07001119#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001120 if (key_id) {
1121 /*
1122 * Ensure that the ENGINE does not attempt to use the OpenSSL
1123 * UI system to obtain a PIN, if we didn't provide one.
1124 */
1125 struct {
1126 const void *password;
1127 const char *prompt_info;
1128 } key_cb = { "", NULL };
1129
1130 /* load private key first in-case PIN is required for cert */
1131 conn->private_key = ENGINE_load_private_key(conn->engine,
1132 key_id, NULL,
1133 &key_cb);
1134 if (!conn->private_key) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001135 unsigned long err = ERR_get_error();
1136
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001137 wpa_printf(MSG_ERROR,
1138 "ENGINE: cannot load private key with id '%s' [%s]",
1139 key_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001140 ERR_error_string(err, NULL));
1141 if (tls_is_pin_error(err))
1142 ret = TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
1143 else
1144 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001145 goto err;
1146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 }
1148
1149 /* handle a certificate and/or CA certificate */
1150 if (cert_id || ca_cert_id) {
1151 const char *cmd_name = "LOAD_CERT_CTRL";
1152
1153 /* test if the engine supports a LOAD_CERT_CTRL */
1154 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1155 0, (void *)cmd_name, NULL)) {
1156 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
1157 " loading certificates");
1158 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1159 goto err;
1160 }
1161 }
1162
1163 return 0;
1164
1165err:
1166 if (conn->engine) {
1167 ENGINE_free(conn->engine);
1168 conn->engine = NULL;
1169 }
1170
1171 if (conn->private_key) {
1172 EVP_PKEY_free(conn->private_key);
1173 conn->private_key = NULL;
1174 }
1175
1176 return ret;
1177#else /* OPENSSL_NO_ENGINE */
1178 return 0;
1179#endif /* OPENSSL_NO_ENGINE */
1180}
1181
1182
1183static void tls_engine_deinit(struct tls_connection *conn)
1184{
Adam Langley1eb02ed2015-04-21 19:00:05 -07001185#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
1187 if (conn->private_key) {
1188 EVP_PKEY_free(conn->private_key);
1189 conn->private_key = NULL;
1190 }
1191 if (conn->engine) {
Adam Langley1eb02ed2015-04-21 19:00:05 -07001192#if !defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 ENGINE_finish(conn->engine);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001194#endif /* !OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 conn->engine = NULL;
1196 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001197#endif /* ANDROID || !OPENSSL_NO_ENGINE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198}
1199
1200
1201int tls_get_errors(void *ssl_ctx)
1202{
1203 int count = 0;
1204 unsigned long err;
1205
1206 while ((err = ERR_get_error())) {
1207 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
1208 ERR_error_string(err, NULL));
1209 count++;
1210 }
1211
1212 return count;
1213}
1214
Jouni Malinen26af48b2014-04-09 13:02:53 +03001215
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001216static const char * openssl_content_type(int content_type)
1217{
1218 switch (content_type) {
1219 case 20:
1220 return "change cipher spec";
1221 case 21:
1222 return "alert";
1223 case 22:
1224 return "handshake";
1225 case 23:
1226 return "application data";
1227 case 24:
1228 return "heartbeat";
1229 case 256:
1230 return "TLS header info"; /* pseudo content type */
1231 default:
1232 return "?";
1233 }
1234}
1235
1236
1237static const char * openssl_handshake_type(int content_type, const u8 *buf,
1238 size_t len)
1239{
1240 if (content_type != 22 || !buf || len == 0)
1241 return "";
1242 switch (buf[0]) {
1243 case 0:
1244 return "hello request";
1245 case 1:
1246 return "client hello";
1247 case 2:
1248 return "server hello";
1249 case 4:
1250 return "new session ticket";
1251 case 11:
1252 return "certificate";
1253 case 12:
1254 return "server key exchange";
1255 case 13:
1256 return "certificate request";
1257 case 14:
1258 return "server hello done";
1259 case 15:
1260 return "certificate verify";
1261 case 16:
1262 return "client key exchange";
1263 case 20:
1264 return "finished";
1265 case 21:
1266 return "certificate url";
1267 case 22:
1268 return "certificate status";
1269 default:
1270 return "?";
1271 }
1272}
1273
1274
Jouni Malinen26af48b2014-04-09 13:02:53 +03001275static void tls_msg_cb(int write_p, int version, int content_type,
1276 const void *buf, size_t len, SSL *ssl, void *arg)
1277{
1278 struct tls_connection *conn = arg;
1279 const u8 *pos = buf;
1280
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001281 if (write_p == 2) {
1282 wpa_printf(MSG_DEBUG,
1283 "OpenSSL: session ver=0x%x content_type=%d",
1284 version, content_type);
1285 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Data", buf, len);
1286 return;
1287 }
1288
1289 wpa_printf(MSG_DEBUG, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1290 write_p ? "TX" : "RX", version, content_type,
1291 openssl_content_type(content_type),
1292 openssl_handshake_type(content_type, buf, len));
Jouni Malinen26af48b2014-04-09 13:02:53 +03001293 wpa_hexdump_key(MSG_MSGDUMP, "OpenSSL: Message", buf, len);
1294 if (content_type == 24 && len >= 3 && pos[0] == 1) {
1295 size_t payload_len = WPA_GET_BE16(pos + 1);
1296 if (payload_len + 3 > len) {
1297 wpa_printf(MSG_ERROR, "OpenSSL: Heartbeat attack detected");
1298 conn->invalid_hb_used = 1;
1299 }
1300 }
1301}
1302
1303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304struct tls_connection * tls_connection_init(void *ssl_ctx)
1305{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001306 struct tls_data *data = ssl_ctx;
1307 SSL_CTX *ssl = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 struct tls_connection *conn;
1309 long options;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08001310 struct tls_context *context = SSL_CTX_get_app_data(ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311
1312 conn = os_zalloc(sizeof(*conn));
1313 if (conn == NULL)
1314 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001315 conn->ssl_ctx = ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 conn->ssl = SSL_new(ssl);
1317 if (conn->ssl == NULL) {
1318 tls_show_errors(MSG_INFO, __func__,
1319 "Failed to initialize new SSL connection");
1320 os_free(conn);
1321 return NULL;
1322 }
1323
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001324 conn->context = context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 SSL_set_app_data(conn->ssl, conn);
Jouni Malinen26af48b2014-04-09 13:02:53 +03001326 SSL_set_msg_callback(conn->ssl, tls_msg_cb);
1327 SSL_set_msg_callback_arg(conn->ssl, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
1329 SSL_OP_SINGLE_DH_USE;
1330#ifdef SSL_OP_NO_COMPRESSION
1331 options |= SSL_OP_NO_COMPRESSION;
1332#endif /* SSL_OP_NO_COMPRESSION */
1333 SSL_set_options(conn->ssl, options);
1334
1335 conn->ssl_in = BIO_new(BIO_s_mem());
1336 if (!conn->ssl_in) {
1337 tls_show_errors(MSG_INFO, __func__,
1338 "Failed to create a new BIO for ssl_in");
1339 SSL_free(conn->ssl);
1340 os_free(conn);
1341 return NULL;
1342 }
1343
1344 conn->ssl_out = BIO_new(BIO_s_mem());
1345 if (!conn->ssl_out) {
1346 tls_show_errors(MSG_INFO, __func__,
1347 "Failed to create a new BIO for ssl_out");
1348 SSL_free(conn->ssl);
1349 BIO_free(conn->ssl_in);
1350 os_free(conn);
1351 return NULL;
1352 }
1353
1354 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
1355
1356 return conn;
1357}
1358
1359
1360void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
1361{
1362 if (conn == NULL)
1363 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001364 if (conn->success_data) {
1365 /*
1366 * Make sure ssl_clear_bad_session() does not remove this
1367 * session.
1368 */
1369 SSL_set_quiet_shutdown(conn->ssl, 1);
1370 SSL_shutdown(conn->ssl);
1371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372 SSL_free(conn->ssl);
1373 tls_engine_deinit(conn);
1374 os_free(conn->subject_match);
1375 os_free(conn->altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001376 os_free(conn->suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001377 os_free(conn->domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 os_free(conn->session_ticket);
1379 os_free(conn);
1380}
1381
1382
1383int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
1384{
1385 return conn ? SSL_is_init_finished(conn->ssl) : 0;
1386}
1387
1388
1389int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1390{
1391 if (conn == NULL)
1392 return -1;
1393
1394 /* Shutdown previous TLS connection without notifying the peer
1395 * because the connection was already terminated in practice
1396 * and "close notify" shutdown alert would confuse AS. */
1397 SSL_set_quiet_shutdown(conn->ssl, 1);
1398 SSL_shutdown(conn->ssl);
Jouni Malinenf291c682015-08-17 22:50:41 +03001399 return SSL_clear(conn->ssl) == 1 ? 0 : -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400}
1401
1402
1403static int tls_match_altsubject_component(X509 *cert, int type,
1404 const char *value, size_t len)
1405{
1406 GENERAL_NAME *gen;
1407 void *ext;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001408 int found = 0;
1409 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410
1411 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1412
1413 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1414 gen = sk_GENERAL_NAME_value(ext, i);
1415 if (gen->type != type)
1416 continue;
1417 if (os_strlen((char *) gen->d.ia5->data) == len &&
1418 os_memcmp(value, gen->d.ia5->data, len) == 0)
1419 found++;
1420 }
1421
1422 return found;
1423}
1424
1425
1426static int tls_match_altsubject(X509 *cert, const char *match)
1427{
1428 int type;
1429 const char *pos, *end;
1430 size_t len;
1431
1432 pos = match;
1433 do {
1434 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1435 type = GEN_EMAIL;
1436 pos += 6;
1437 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1438 type = GEN_DNS;
1439 pos += 4;
1440 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1441 type = GEN_URI;
1442 pos += 4;
1443 } else {
1444 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1445 "match '%s'", pos);
1446 return 0;
1447 }
1448 end = os_strchr(pos, ';');
1449 while (end) {
1450 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1451 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1452 os_strncmp(end + 1, "URI:", 4) == 0)
1453 break;
1454 end = os_strchr(end + 1, ';');
1455 }
1456 if (end)
1457 len = end - pos;
1458 else
1459 len = os_strlen(pos);
1460 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1461 return 1;
1462 pos = end + 1;
1463 } while (end);
1464
1465 return 0;
1466}
1467
1468
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001469#ifndef CONFIG_NATIVE_WINDOWS
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001470static int domain_suffix_match(const u8 *val, size_t len, const char *match,
1471 int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001472{
1473 size_t i, match_len;
1474
1475 /* Check for embedded nuls that could mess up suffix matching */
1476 for (i = 0; i < len; i++) {
1477 if (val[i] == '\0') {
1478 wpa_printf(MSG_DEBUG, "TLS: Embedded null in a string - reject");
1479 return 0;
1480 }
1481 }
1482
1483 match_len = os_strlen(match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001484 if (match_len > len || (full && match_len != len))
Dmitry Shmidt051af732013-10-22 13:52:46 -07001485 return 0;
1486
1487 if (os_strncasecmp((const char *) val + len - match_len, match,
1488 match_len) != 0)
1489 return 0; /* no match */
1490
1491 if (match_len == len)
1492 return 1; /* exact match */
1493
1494 if (val[len - match_len - 1] == '.')
1495 return 1; /* full label match completes suffix match */
1496
1497 wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
1498 return 0;
1499}
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001500#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001501
1502
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001503static int tls_match_suffix(X509 *cert, const char *match, int full)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001504{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001505#ifdef CONFIG_NATIVE_WINDOWS
1506 /* wincrypt.h has conflicting X509_NAME definition */
1507 return -1;
1508#else /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001509 GENERAL_NAME *gen;
1510 void *ext;
1511 int i;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001512 stack_index_t j;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001513 int dns_name = 0;
1514 X509_NAME *name;
1515
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001516 wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
1517 full ? "": "suffix ", match);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001518
1519 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1520
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001521 for (j = 0; ext && j < sk_GENERAL_NAME_num(ext); j++) {
1522 gen = sk_GENERAL_NAME_value(ext, j);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001523 if (gen->type != GEN_DNS)
1524 continue;
1525 dns_name++;
1526 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
1527 gen->d.dNSName->data,
1528 gen->d.dNSName->length);
1529 if (domain_suffix_match(gen->d.dNSName->data,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001530 gen->d.dNSName->length, match, full) ==
1531 1) {
1532 wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
1533 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001534 return 1;
1535 }
1536 }
1537
1538 if (dns_name) {
1539 wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
1540 return 0;
1541 }
1542
1543 name = X509_get_subject_name(cert);
1544 i = -1;
1545 for (;;) {
1546 X509_NAME_ENTRY *e;
1547 ASN1_STRING *cn;
1548
1549 i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
1550 if (i == -1)
1551 break;
1552 e = X509_NAME_get_entry(name, i);
1553 if (e == NULL)
1554 continue;
1555 cn = X509_NAME_ENTRY_get_data(e);
1556 if (cn == NULL)
1557 continue;
1558 wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
1559 cn->data, cn->length);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001560 if (domain_suffix_match(cn->data, cn->length, match, full) == 1)
1561 {
1562 wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
1563 full ? "Match" : "Suffix match");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001564 return 1;
1565 }
1566 }
1567
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001568 wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
1569 full ? "": "suffix ");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001570 return 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001571#endif /* CONFIG_NATIVE_WINDOWS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001572}
1573
1574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575static enum tls_fail_reason openssl_tls_fail_reason(int err)
1576{
1577 switch (err) {
1578 case X509_V_ERR_CERT_REVOKED:
1579 return TLS_FAIL_REVOKED;
1580 case X509_V_ERR_CERT_NOT_YET_VALID:
1581 case X509_V_ERR_CRL_NOT_YET_VALID:
1582 return TLS_FAIL_NOT_YET_VALID;
1583 case X509_V_ERR_CERT_HAS_EXPIRED:
1584 case X509_V_ERR_CRL_HAS_EXPIRED:
1585 return TLS_FAIL_EXPIRED;
1586 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1587 case X509_V_ERR_UNABLE_TO_GET_CRL:
1588 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1589 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1590 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1591 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1592 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1593 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1594 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1595 case X509_V_ERR_INVALID_CA:
1596 return TLS_FAIL_UNTRUSTED;
1597 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1598 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1599 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1600 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1601 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1602 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1603 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1604 case X509_V_ERR_CERT_UNTRUSTED:
1605 case X509_V_ERR_CERT_REJECTED:
1606 return TLS_FAIL_BAD_CERTIFICATE;
1607 default:
1608 return TLS_FAIL_UNSPECIFIED;
1609 }
1610}
1611
1612
1613static struct wpabuf * get_x509_cert(X509 *cert)
1614{
1615 struct wpabuf *buf;
1616 u8 *tmp;
1617
1618 int cert_len = i2d_X509(cert, NULL);
1619 if (cert_len <= 0)
1620 return NULL;
1621
1622 buf = wpabuf_alloc(cert_len);
1623 if (buf == NULL)
1624 return NULL;
1625
1626 tmp = wpabuf_put(buf, cert_len);
1627 i2d_X509(cert, &tmp);
1628 return buf;
1629}
1630
1631
1632static void openssl_tls_fail_event(struct tls_connection *conn,
1633 X509 *err_cert, int err, int depth,
1634 const char *subject, const char *err_str,
1635 enum tls_fail_reason reason)
1636{
1637 union tls_event_data ev;
1638 struct wpabuf *cert = NULL;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001639 struct tls_context *context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001641 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 return;
1643
1644 cert = get_x509_cert(err_cert);
1645 os_memset(&ev, 0, sizeof(ev));
1646 ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
1647 reason : openssl_tls_fail_reason(err);
1648 ev.cert_fail.depth = depth;
1649 ev.cert_fail.subject = subject;
1650 ev.cert_fail.reason_txt = err_str;
1651 ev.cert_fail.cert = cert;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001652 context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 wpabuf_free(cert);
1654}
1655
1656
1657static void openssl_tls_cert_event(struct tls_connection *conn,
1658 X509 *err_cert, int depth,
1659 const char *subject)
1660{
1661 struct wpabuf *cert = NULL;
1662 union tls_event_data ev;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001663 struct tls_context *context = conn->context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001664 char *altsubject[TLS_MAX_ALT_SUBJECT];
1665 int alt, num_altsubject = 0;
1666 GENERAL_NAME *gen;
1667 void *ext;
1668 stack_index_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669#ifdef CONFIG_SHA256
1670 u8 hash[32];
1671#endif /* CONFIG_SHA256 */
1672
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001673 if (context->event_cb == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 return;
1675
1676 os_memset(&ev, 0, sizeof(ev));
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001677 if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
1678 context->cert_in_cb) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 cert = get_x509_cert(err_cert);
1680 ev.peer_cert.cert = cert;
1681 }
1682#ifdef CONFIG_SHA256
1683 if (cert) {
1684 const u8 *addr[1];
1685 size_t len[1];
1686 addr[0] = wpabuf_head(cert);
1687 len[0] = wpabuf_len(cert);
1688 if (sha256_vector(1, addr, len, hash) == 0) {
1689 ev.peer_cert.hash = hash;
1690 ev.peer_cert.hash_len = sizeof(hash);
1691 }
1692 }
1693#endif /* CONFIG_SHA256 */
1694 ev.peer_cert.depth = depth;
1695 ev.peer_cert.subject = subject;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001696
1697 ext = X509_get_ext_d2i(err_cert, NID_subject_alt_name, NULL, NULL);
1698 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1699 char *pos;
1700
1701 if (num_altsubject == TLS_MAX_ALT_SUBJECT)
1702 break;
1703 gen = sk_GENERAL_NAME_value(ext, i);
1704 if (gen->type != GEN_EMAIL &&
1705 gen->type != GEN_DNS &&
1706 gen->type != GEN_URI)
1707 continue;
1708
1709 pos = os_malloc(10 + gen->d.ia5->length + 1);
1710 if (pos == NULL)
1711 break;
1712 altsubject[num_altsubject++] = pos;
1713
1714 switch (gen->type) {
1715 case GEN_EMAIL:
1716 os_memcpy(pos, "EMAIL:", 6);
1717 pos += 6;
1718 break;
1719 case GEN_DNS:
1720 os_memcpy(pos, "DNS:", 4);
1721 pos += 4;
1722 break;
1723 case GEN_URI:
1724 os_memcpy(pos, "URI:", 4);
1725 pos += 4;
1726 break;
1727 }
1728
1729 os_memcpy(pos, gen->d.ia5->data, gen->d.ia5->length);
1730 pos += gen->d.ia5->length;
1731 *pos = '\0';
1732 }
1733
1734 for (alt = 0; alt < num_altsubject; alt++)
1735 ev.peer_cert.altsubject[alt] = altsubject[alt];
1736 ev.peer_cert.num_altsubject = num_altsubject;
1737
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001738 context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 wpabuf_free(cert);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001740 for (alt = 0; alt < num_altsubject; alt++)
1741 os_free(altsubject[alt]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742}
1743
1744
1745static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1746{
1747 char buf[256];
1748 X509 *err_cert;
1749 int err, depth;
1750 SSL *ssl;
1751 struct tls_connection *conn;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001752 struct tls_context *context;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001753 char *match, *altmatch, *suffix_match, *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 const char *err_str;
1755
1756 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001757 if (!err_cert)
1758 return 0;
1759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760 err = X509_STORE_CTX_get_error(x509_ctx);
1761 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1762 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1763 SSL_get_ex_data_X509_STORE_CTX_idx());
1764 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1765
1766 conn = SSL_get_app_data(ssl);
1767 if (conn == NULL)
1768 return 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001769
1770 if (depth == 0)
1771 conn->peer_cert = err_cert;
1772 else if (depth == 1)
1773 conn->peer_issuer = err_cert;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001774 else if (depth == 2)
1775 conn->peer_issuer_issuer = err_cert;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001776
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001777 context = conn->context;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778 match = conn->subject_match;
1779 altmatch = conn->altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001780 suffix_match = conn->suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001781 domain_match = conn->domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782
1783 if (!preverify_ok && !conn->ca_cert_verify)
1784 preverify_ok = 1;
1785 if (!preverify_ok && depth > 0 && conn->server_cert_only)
1786 preverify_ok = 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001787 if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
1788 (err == X509_V_ERR_CERT_HAS_EXPIRED ||
1789 err == X509_V_ERR_CERT_NOT_YET_VALID)) {
1790 wpa_printf(MSG_DEBUG, "OpenSSL: Ignore certificate validity "
1791 "time mismatch");
1792 preverify_ok = 1;
1793 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794
1795 err_str = X509_verify_cert_error_string(err);
1796
1797#ifdef CONFIG_SHA256
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001798 /*
1799 * Do not require preverify_ok so we can explicity allow otherwise
1800 * invalid pinned server certificates.
1801 */
1802 if (depth == 0 && conn->server_cert_only) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 struct wpabuf *cert;
1804 cert = get_x509_cert(err_cert);
1805 if (!cert) {
1806 wpa_printf(MSG_DEBUG, "OpenSSL: Could not fetch "
1807 "server certificate data");
1808 preverify_ok = 0;
1809 } else {
1810 u8 hash[32];
1811 const u8 *addr[1];
1812 size_t len[1];
1813 addr[0] = wpabuf_head(cert);
1814 len[0] = wpabuf_len(cert);
1815 if (sha256_vector(1, addr, len, hash) < 0 ||
1816 os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
1817 err_str = "Server certificate mismatch";
1818 err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
1819 preverify_ok = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001820 } else if (!preverify_ok) {
1821 /*
1822 * Certificate matches pinned certificate, allow
1823 * regardless of other problems.
1824 */
1825 wpa_printf(MSG_DEBUG,
1826 "OpenSSL: Ignore validation issues for a pinned server certificate");
1827 preverify_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 }
1829 wpabuf_free(cert);
1830 }
1831 }
1832#endif /* CONFIG_SHA256 */
1833
1834 if (!preverify_ok) {
1835 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1836 " error %d (%s) depth %d for '%s'", err, err_str,
1837 depth, buf);
1838 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1839 err_str, TLS_FAIL_UNSPECIFIED);
1840 return preverify_ok;
1841 }
1842
1843 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - preverify_ok=%d "
1844 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1845 preverify_ok, err, err_str,
1846 conn->ca_cert_verify, depth, buf);
1847 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1848 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1849 "match with '%s'", buf, match);
1850 preverify_ok = 0;
1851 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1852 "Subject mismatch",
1853 TLS_FAIL_SUBJECT_MISMATCH);
1854 } else if (depth == 0 && altmatch &&
1855 !tls_match_altsubject(err_cert, altmatch)) {
1856 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1857 "'%s' not found", altmatch);
1858 preverify_ok = 0;
1859 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1860 "AltSubject mismatch",
1861 TLS_FAIL_ALTSUBJECT_MISMATCH);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001862 } else if (depth == 0 && suffix_match &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001863 !tls_match_suffix(err_cert, suffix_match, 0)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001864 wpa_printf(MSG_WARNING, "TLS: Domain suffix match '%s' not found",
1865 suffix_match);
1866 preverify_ok = 0;
1867 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1868 "Domain suffix mismatch",
1869 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001870 } else if (depth == 0 && domain_match &&
1871 !tls_match_suffix(err_cert, domain_match, 1)) {
1872 wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
1873 domain_match);
1874 preverify_ok = 0;
1875 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1876 "Domain mismatch",
1877 TLS_FAIL_DOMAIN_MISMATCH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878 } else
1879 openssl_tls_cert_event(conn, err_cert, depth, buf);
1880
1881 if (conn->cert_probe && preverify_ok && depth == 0) {
1882 wpa_printf(MSG_DEBUG, "OpenSSL: Reject server certificate "
1883 "on probe-only run");
1884 preverify_ok = 0;
1885 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1886 "Server certificate chain probe",
1887 TLS_FAIL_SERVER_CHAIN_PROBE);
1888 }
1889
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001890#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001891 if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
1892 preverify_ok) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001893 enum ocsp_result res;
1894
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001895 res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
1896 conn->peer_issuer,
1897 conn->peer_issuer_issuer);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001898 if (res == OCSP_REVOKED) {
1899 preverify_ok = 0;
1900 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1901 "certificate revoked",
1902 TLS_FAIL_REVOKED);
1903 if (err == X509_V_OK)
1904 X509_STORE_CTX_set_error(
1905 x509_ctx, X509_V_ERR_CERT_REVOKED);
1906 } else if (res != OCSP_GOOD &&
1907 (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
1908 preverify_ok = 0;
1909 openssl_tls_fail_event(conn, err_cert, err, depth, buf,
1910 "bad certificate status response",
1911 TLS_FAIL_UNSPECIFIED);
1912 }
1913 }
1914#endif /* OPENSSL_IS_BORINGSSL */
1915
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001916 if (depth == 0 && preverify_ok && context->event_cb != NULL)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001917 context->event_cb(context->cb_ctx,
1918 TLS_CERT_CHAIN_SUCCESS, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920 return preverify_ok;
1921}
1922
1923
1924#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001925static int tls_load_ca_der(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001927 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 X509_LOOKUP *lookup;
1929 int ret = 0;
1930
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001931 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 X509_LOOKUP_file());
1933 if (lookup == NULL) {
1934 tls_show_errors(MSG_WARNING, __func__,
1935 "Failed add lookup for X509 store");
1936 return -1;
1937 }
1938
1939 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1940 unsigned long err = ERR_peek_error();
1941 tls_show_errors(MSG_WARNING, __func__,
1942 "Failed load CA in DER format");
1943 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1944 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1945 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1946 "cert already in hash table error",
1947 __func__);
1948 } else
1949 ret = -1;
1950 }
1951
1952 return ret;
1953}
1954#endif /* OPENSSL_NO_STDIO */
1955
1956
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001957static int tls_connection_ca_cert(struct tls_data *data,
1958 struct tls_connection *conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 const char *ca_cert, const u8 *ca_cert_blob,
1960 size_t ca_cert_blob_len, const char *ca_path)
1961{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001962 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001963 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964
1965 /*
1966 * Remove previously configured trusted CA certificates before adding
1967 * new ones.
1968 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001969 store = X509_STORE_new();
1970 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1972 "certificate store", __func__);
1973 return -1;
1974 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001975 SSL_CTX_set_cert_store(ssl_ctx, store);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976
1977 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1978 conn->ca_cert_verify = 1;
1979
1980 if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
1981 wpa_printf(MSG_DEBUG, "OpenSSL: Probe for server certificate "
1982 "chain");
1983 conn->cert_probe = 1;
1984 conn->ca_cert_verify = 0;
1985 return 0;
1986 }
1987
1988 if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
1989#ifdef CONFIG_SHA256
1990 const char *pos = ca_cert + 7;
1991 if (os_strncmp(pos, "server/sha256/", 14) != 0) {
1992 wpa_printf(MSG_DEBUG, "OpenSSL: Unsupported ca_cert "
1993 "hash value '%s'", ca_cert);
1994 return -1;
1995 }
1996 pos += 14;
1997 if (os_strlen(pos) != 32 * 2) {
1998 wpa_printf(MSG_DEBUG, "OpenSSL: Unexpected SHA256 "
1999 "hash length in ca_cert '%s'", ca_cert);
2000 return -1;
2001 }
2002 if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
2003 wpa_printf(MSG_DEBUG, "OpenSSL: Invalid SHA256 hash "
2004 "value in ca_cert '%s'", ca_cert);
2005 return -1;
2006 }
2007 conn->server_cert_only = 1;
2008 wpa_printf(MSG_DEBUG, "OpenSSL: Checking only server "
2009 "certificate match");
2010 return 0;
2011#else /* CONFIG_SHA256 */
2012 wpa_printf(MSG_INFO, "No SHA256 included in the build - "
2013 "cannot validate server certificate hash");
2014 return -1;
2015#endif /* CONFIG_SHA256 */
2016 }
2017
2018 if (ca_cert_blob) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002019 X509 *cert = d2i_X509(NULL,
2020 (const unsigned char **) &ca_cert_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 ca_cert_blob_len);
2022 if (cert == NULL) {
2023 tls_show_errors(MSG_WARNING, __func__,
2024 "Failed to parse ca_cert_blob");
2025 return -1;
2026 }
2027
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002028 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx),
2029 cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 unsigned long err = ERR_peek_error();
2031 tls_show_errors(MSG_WARNING, __func__,
2032 "Failed to add ca_cert_blob to "
2033 "certificate store");
2034 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2035 ERR_GET_REASON(err) ==
2036 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2037 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
2038 "cert already in hash table error",
2039 __func__);
2040 } else {
2041 X509_free(cert);
2042 return -1;
2043 }
2044 }
2045 X509_free(cert);
2046 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
2047 "to certificate store", __func__);
2048 return 0;
2049 }
2050
2051#ifdef ANDROID
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002052 /* Single alias */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 if (ca_cert && os_strncmp("keystore://", ca_cert, 11) == 0) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002054 if (tls_add_ca_from_keystore(ssl_ctx->cert_store,
2055 &ca_cert[11]) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 return -1;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002057 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2058 return 0;
2059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002061 /* Multiple aliases separated by space */
2062 if (ca_cert && os_strncmp("keystores://", ca_cert, 12) == 0) {
2063 char *aliases = os_strdup(&ca_cert[12]);
2064 const char *delim = " ";
2065 int rc = 0;
2066 char *savedptr;
2067 char *alias;
2068
2069 if (!aliases)
2070 return -1;
2071 alias = strtok_r(aliases, delim, &savedptr);
2072 for (; alias; alias = strtok_r(NULL, delim, &savedptr)) {
2073 if (tls_add_ca_from_keystore_encoded(
2074 ssl_ctx->cert_store, alias)) {
2075 wpa_printf(MSG_WARNING,
2076 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2077 __func__, alias);
2078 rc = -1;
2079 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 }
2081 }
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002082 os_free(aliases);
2083 if (rc)
2084 return rc;
2085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
2087 return 0;
2088 }
2089#endif /* ANDROID */
2090
2091#ifdef CONFIG_NATIVE_WINDOWS
2092 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
2093 0) {
2094 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
2095 "system certificate store");
2096 return 0;
2097 }
2098#endif /* CONFIG_NATIVE_WINDOWS */
2099
2100 if (ca_cert || ca_path) {
2101#ifndef OPENSSL_NO_STDIO
2102 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
2103 1) {
2104 tls_show_errors(MSG_WARNING, __func__,
2105 "Failed to load root certificates");
2106 if (ca_cert &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002107 tls_load_ca_der(data, ca_cert) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
2109 "DER format CA certificate",
2110 __func__);
2111 } else
2112 return -1;
2113 } else {
2114 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2115 "certificate(s) loaded");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002116 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 }
2118#else /* OPENSSL_NO_STDIO */
2119 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2120 __func__);
2121 return -1;
2122#endif /* OPENSSL_NO_STDIO */
2123 } else {
2124 /* No ca_cert configured - do not try to verify server
2125 * certificate */
2126 conn->ca_cert_verify = 0;
2127 }
2128
2129 return 0;
2130}
2131
2132
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002133static int tls_global_ca_cert(struct tls_data *data, const char *ca_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002135 SSL_CTX *ssl_ctx = data->ssl;
2136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 if (ca_cert) {
2138 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
2139 {
2140 tls_show_errors(MSG_WARNING, __func__,
2141 "Failed to load root certificates");
2142 return -1;
2143 }
2144
2145 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
2146 "certificate(s) loaded");
2147
2148#ifndef OPENSSL_NO_STDIO
2149 /* Add the same CAs to the client certificate requests */
2150 SSL_CTX_set_client_CA_list(ssl_ctx,
2151 SSL_load_client_CA_file(ca_cert));
2152#endif /* OPENSSL_NO_STDIO */
2153 }
2154
2155 return 0;
2156}
2157
2158
2159int tls_global_set_verify(void *ssl_ctx, int check_crl)
2160{
2161 int flags;
2162
2163 if (check_crl) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002164 struct tls_data *data = ssl_ctx;
2165 X509_STORE *cs = SSL_CTX_get_cert_store(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 if (cs == NULL) {
2167 tls_show_errors(MSG_INFO, __func__, "Failed to get "
2168 "certificate store when enabling "
2169 "check_crl");
2170 return -1;
2171 }
2172 flags = X509_V_FLAG_CRL_CHECK;
2173 if (check_crl == 2)
2174 flags |= X509_V_FLAG_CRL_CHECK_ALL;
2175 X509_STORE_set_flags(cs, flags);
2176 }
2177 return 0;
2178}
2179
2180
2181static int tls_connection_set_subject_match(struct tls_connection *conn,
2182 const char *subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002183 const char *altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002184 const char *suffix_match,
2185 const char *domain_match)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186{
2187 os_free(conn->subject_match);
2188 conn->subject_match = NULL;
2189 if (subject_match) {
2190 conn->subject_match = os_strdup(subject_match);
2191 if (conn->subject_match == NULL)
2192 return -1;
2193 }
2194
2195 os_free(conn->altsubject_match);
2196 conn->altsubject_match = NULL;
2197 if (altsubject_match) {
2198 conn->altsubject_match = os_strdup(altsubject_match);
2199 if (conn->altsubject_match == NULL)
2200 return -1;
2201 }
2202
Dmitry Shmidt051af732013-10-22 13:52:46 -07002203 os_free(conn->suffix_match);
2204 conn->suffix_match = NULL;
2205 if (suffix_match) {
2206 conn->suffix_match = os_strdup(suffix_match);
2207 if (conn->suffix_match == NULL)
2208 return -1;
2209 }
2210
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002211 os_free(conn->domain_match);
2212 conn->domain_match = NULL;
2213 if (domain_match) {
2214 conn->domain_match = os_strdup(domain_match);
2215 if (conn->domain_match == NULL)
2216 return -1;
2217 }
2218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 return 0;
2220}
2221
2222
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002223static void tls_set_conn_flags(SSL *ssl, unsigned int flags)
2224{
2225#ifdef SSL_OP_NO_TICKET
2226 if (flags & TLS_CONN_DISABLE_SESSION_TICKET)
2227 SSL_set_options(ssl, SSL_OP_NO_TICKET);
2228#ifdef SSL_clear_options
2229 else
2230 SSL_clear_options(ssl, SSL_OP_NO_TICKET);
2231#endif /* SSL_clear_options */
2232#endif /* SSL_OP_NO_TICKET */
2233
2234#ifdef SSL_OP_NO_TLSv1
2235 if (flags & TLS_CONN_DISABLE_TLSv1_0)
2236 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
2237 else
2238 SSL_clear_options(ssl, SSL_OP_NO_TLSv1);
2239#endif /* SSL_OP_NO_TLSv1 */
2240#ifdef SSL_OP_NO_TLSv1_1
2241 if (flags & TLS_CONN_DISABLE_TLSv1_1)
2242 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
2243 else
2244 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_1);
2245#endif /* SSL_OP_NO_TLSv1_1 */
2246#ifdef SSL_OP_NO_TLSv1_2
2247 if (flags & TLS_CONN_DISABLE_TLSv1_2)
2248 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
2249 else
2250 SSL_clear_options(ssl, SSL_OP_NO_TLSv1_2);
2251#endif /* SSL_OP_NO_TLSv1_2 */
2252}
2253
2254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002256 int verify_peer, unsigned int flags,
2257 const u8 *session_ctx, size_t session_ctx_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258{
2259 static int counter = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002260 struct tls_data *data = ssl_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261
2262 if (conn == NULL)
2263 return -1;
2264
2265 if (verify_peer) {
2266 conn->ca_cert_verify = 1;
2267 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
2268 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
2269 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
2270 } else {
2271 conn->ca_cert_verify = 0;
2272 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
2273 }
2274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002275 tls_set_conn_flags(conn->ssl, flags);
2276 conn->flags = flags;
2277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278 SSL_set_accept_state(conn->ssl);
2279
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002280 if (data->tls_session_lifetime == 0) {
2281 /*
2282 * Set session id context to a unique value to make sure
2283 * session resumption cannot be used either through session
2284 * caching or TLS ticket extension.
2285 */
2286 counter++;
2287 SSL_set_session_id_context(conn->ssl,
2288 (const unsigned char *) &counter,
2289 sizeof(counter));
2290 } else if (session_ctx) {
2291 SSL_set_session_id_context(conn->ssl, session_ctx,
2292 session_ctx_len);
2293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294
2295 return 0;
2296}
2297
2298
2299static int tls_connection_client_cert(struct tls_connection *conn,
2300 const char *client_cert,
2301 const u8 *client_cert_blob,
2302 size_t client_cert_blob_len)
2303{
2304 if (client_cert == NULL && client_cert_blob == NULL)
2305 return 0;
2306
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002307#ifdef PKCS12_FUNCS
2308#if OPENSSL_VERSION_NUMBER < 0x10002000L
2309 /*
2310 * Clear previously set extra chain certificates, if any, from PKCS#12
2311 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2312 * chain properly.
2313 */
2314 SSL_CTX_clear_extra_chain_certs(conn->ssl_ctx);
2315#endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2316#endif /* PKCS12_FUNCS */
2317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 if (client_cert_blob &&
2319 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
2320 client_cert_blob_len) == 1) {
2321 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
2322 "OK");
2323 return 0;
2324 } else if (client_cert_blob) {
2325 tls_show_errors(MSG_DEBUG, __func__,
2326 "SSL_use_certificate_ASN1 failed");
2327 }
2328
2329 if (client_cert == NULL)
2330 return -1;
2331
2332#ifdef ANDROID
2333 if (os_strncmp("keystore://", client_cert, 11) == 0) {
2334 BIO *bio = BIO_from_keystore(&client_cert[11]);
2335 X509 *x509 = NULL;
2336 int ret = -1;
2337 if (bio) {
2338 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
2339 BIO_free(bio);
2340 }
2341 if (x509) {
2342 if (SSL_use_certificate(conn->ssl, x509) == 1)
2343 ret = 0;
2344 X509_free(x509);
2345 }
2346 return ret;
2347 }
2348#endif /* ANDROID */
2349
2350#ifndef OPENSSL_NO_STDIO
2351 if (SSL_use_certificate_file(conn->ssl, client_cert,
2352 SSL_FILETYPE_ASN1) == 1) {
2353 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
2354 " --> OK");
2355 return 0;
2356 }
2357
2358 if (SSL_use_certificate_file(conn->ssl, client_cert,
2359 SSL_FILETYPE_PEM) == 1) {
2360 ERR_clear_error();
2361 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
2362 " --> OK");
2363 return 0;
2364 }
2365
2366 tls_show_errors(MSG_DEBUG, __func__,
2367 "SSL_use_certificate_file failed");
2368#else /* OPENSSL_NO_STDIO */
2369 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2370#endif /* OPENSSL_NO_STDIO */
2371
2372 return -1;
2373}
2374
2375
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002376static int tls_global_client_cert(struct tls_data *data,
2377 const char *client_cert)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378{
2379#ifndef OPENSSL_NO_STDIO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002380 SSL_CTX *ssl_ctx = data->ssl;
2381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 if (client_cert == NULL)
2383 return 0;
2384
2385 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2386 SSL_FILETYPE_ASN1) != 1 &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002387 SSL_CTX_use_certificate_chain_file(ssl_ctx, client_cert) != 1 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
2389 SSL_FILETYPE_PEM) != 1) {
2390 tls_show_errors(MSG_INFO, __func__,
2391 "Failed to load client certificate");
2392 return -1;
2393 }
2394 return 0;
2395#else /* OPENSSL_NO_STDIO */
2396 if (client_cert == NULL)
2397 return 0;
2398 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
2399 return -1;
2400#endif /* OPENSSL_NO_STDIO */
2401}
2402
2403
2404static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
2405{
2406 if (password == NULL) {
2407 return 0;
2408 }
2409 os_strlcpy(buf, (char *) password, size);
2410 return os_strlen(buf);
2411}
2412
2413
2414#ifdef PKCS12_FUNCS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002415static int tls_parse_pkcs12(struct tls_data *data, SSL *ssl, PKCS12 *p12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416 const char *passwd)
2417{
2418 EVP_PKEY *pkey;
2419 X509 *cert;
2420 STACK_OF(X509) *certs;
2421 int res = 0;
2422 char buf[256];
2423
2424 pkey = NULL;
2425 cert = NULL;
2426 certs = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002427 if (!passwd)
2428 passwd = "";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
2430 tls_show_errors(MSG_DEBUG, __func__,
2431 "Failed to parse PKCS12 file");
2432 PKCS12_free(p12);
2433 return -1;
2434 }
2435 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
2436
2437 if (cert) {
2438 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2439 sizeof(buf));
2440 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
2441 "subject='%s'", buf);
2442 if (ssl) {
2443 if (SSL_use_certificate(ssl, cert) != 1)
2444 res = -1;
2445 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002446 if (SSL_CTX_use_certificate(data->ssl, cert) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 res = -1;
2448 }
2449 X509_free(cert);
2450 }
2451
2452 if (pkey) {
2453 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
2454 if (ssl) {
2455 if (SSL_use_PrivateKey(ssl, pkey) != 1)
2456 res = -1;
2457 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002458 if (SSL_CTX_use_PrivateKey(data->ssl, pkey) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002459 res = -1;
2460 }
2461 EVP_PKEY_free(pkey);
2462 }
2463
2464 if (certs) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002465#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002466 if (ssl)
2467 SSL_clear_chain_certs(ssl);
2468 else
2469 SSL_CTX_clear_chain_certs(data->ssl);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002470 while ((cert = sk_X509_pop(certs)) != NULL) {
2471 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2472 sizeof(buf));
2473 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2474 " from PKCS12: subject='%s'", buf);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002475 if ((ssl && SSL_add1_chain_cert(ssl, cert) != 1) ||
2476 (!ssl && SSL_CTX_add1_chain_cert(data->ssl,
2477 cert) != 1)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002478 tls_show_errors(MSG_DEBUG, __func__,
2479 "Failed to add additional certificate");
2480 res = -1;
2481 break;
2482 }
2483 }
2484 if (!res) {
2485 /* Try to continue anyway */
2486 }
2487 sk_X509_free(certs);
2488#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002489 if (ssl)
2490 res = SSL_build_cert_chain(
2491 ssl,
2492 SSL_BUILD_CHAIN_FLAG_CHECK |
2493 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
2494 else
2495 res = SSL_CTX_build_cert_chain(
2496 data->ssl,
2497 SSL_BUILD_CHAIN_FLAG_CHECK |
2498 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002499 if (!res) {
2500 tls_show_errors(MSG_DEBUG, __func__,
2501 "Failed to build certificate chain");
2502 } else if (res == 2) {
2503 wpa_printf(MSG_DEBUG,
2504 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2505 }
2506#endif /* OPENSSL_IS_BORINGSSL */
2507 /*
2508 * Try to continue regardless of result since it is possible for
2509 * the extra certificates not to be required.
2510 */
2511 res = 0;
2512#else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002513 SSL_CTX_clear_extra_chain_certs(data->ssl);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 while ((cert = sk_X509_pop(certs)) != NULL) {
2515 X509_NAME_oneline(X509_get_subject_name(cert), buf,
2516 sizeof(buf));
2517 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
2518 " from PKCS12: subject='%s'", buf);
2519 /*
2520 * There is no SSL equivalent for the chain cert - so
2521 * always add it to the context...
2522 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002523 if (SSL_CTX_add_extra_chain_cert(data->ssl, cert) != 1)
2524 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525 res = -1;
2526 break;
2527 }
2528 }
2529 sk_X509_free(certs);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002530#endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531 }
2532
2533 PKCS12_free(p12);
2534
2535 if (res < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002536 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537
2538 return res;
2539}
2540#endif /* PKCS12_FUNCS */
2541
2542
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002543static int tls_read_pkcs12(struct tls_data *data, SSL *ssl,
2544 const char *private_key, const char *passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545{
2546#ifdef PKCS12_FUNCS
2547 FILE *f;
2548 PKCS12 *p12;
2549
2550 f = fopen(private_key, "rb");
2551 if (f == NULL)
2552 return -1;
2553
2554 p12 = d2i_PKCS12_fp(f, NULL);
2555 fclose(f);
2556
2557 if (p12 == NULL) {
2558 tls_show_errors(MSG_INFO, __func__,
2559 "Failed to use PKCS#12 file");
2560 return -1;
2561 }
2562
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002563 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564
2565#else /* PKCS12_FUNCS */
2566 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
2567 "p12/pfx files");
2568 return -1;
2569#endif /* PKCS12_FUNCS */
2570}
2571
2572
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002573static int tls_read_pkcs12_blob(struct tls_data *data, SSL *ssl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 const u8 *blob, size_t len, const char *passwd)
2575{
2576#ifdef PKCS12_FUNCS
2577 PKCS12 *p12;
2578
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002579 p12 = d2i_PKCS12(NULL, (const unsigned char **) &blob, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 if (p12 == NULL) {
2581 tls_show_errors(MSG_INFO, __func__,
2582 "Failed to use PKCS#12 blob");
2583 return -1;
2584 }
2585
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002586 return tls_parse_pkcs12(data, ssl, p12, passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587
2588#else /* PKCS12_FUNCS */
2589 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
2590 "p12/pfx blobs");
2591 return -1;
2592#endif /* PKCS12_FUNCS */
2593}
2594
2595
2596#ifndef OPENSSL_NO_ENGINE
2597static int tls_engine_get_cert(struct tls_connection *conn,
2598 const char *cert_id,
2599 X509 **cert)
2600{
2601 /* this runs after the private key is loaded so no PIN is required */
2602 struct {
2603 const char *cert_id;
2604 X509 *cert;
2605 } params;
2606 params.cert_id = cert_id;
2607 params.cert = NULL;
2608
2609 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
2610 0, &params, NULL, 1)) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002611 unsigned long err = ERR_get_error();
2612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
2614 " '%s' [%s]", cert_id,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002615 ERR_error_string(err, NULL));
2616 if (tls_is_pin_error(err))
2617 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2619 }
2620 if (!params.cert) {
2621 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
2622 " '%s'", cert_id);
2623 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
2624 }
2625 *cert = params.cert;
2626 return 0;
2627}
2628#endif /* OPENSSL_NO_ENGINE */
2629
2630
2631static int tls_connection_engine_client_cert(struct tls_connection *conn,
2632 const char *cert_id)
2633{
2634#ifndef OPENSSL_NO_ENGINE
2635 X509 *cert;
2636
2637 if (tls_engine_get_cert(conn, cert_id, &cert))
2638 return -1;
2639
2640 if (!SSL_use_certificate(conn->ssl, cert)) {
2641 tls_show_errors(MSG_ERROR, __func__,
2642 "SSL_use_certificate failed");
2643 X509_free(cert);
2644 return -1;
2645 }
2646 X509_free(cert);
2647 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
2648 "OK");
2649 return 0;
2650
2651#else /* OPENSSL_NO_ENGINE */
2652 return -1;
2653#endif /* OPENSSL_NO_ENGINE */
2654}
2655
2656
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002657static int tls_connection_engine_ca_cert(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658 struct tls_connection *conn,
2659 const char *ca_cert_id)
2660{
2661#ifndef OPENSSL_NO_ENGINE
2662 X509 *cert;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002663 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002664 X509_STORE *store;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665
2666 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
2667 return -1;
2668
2669 /* start off the same as tls_connection_ca_cert */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002670 store = X509_STORE_new();
2671 if (store == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
2673 "certificate store", __func__);
2674 X509_free(cert);
2675 return -1;
2676 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002677 SSL_CTX_set_cert_store(ssl_ctx, store);
2678 if (!X509_STORE_add_cert(store, cert)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 unsigned long err = ERR_peek_error();
2680 tls_show_errors(MSG_WARNING, __func__,
2681 "Failed to add CA certificate from engine "
2682 "to certificate store");
2683 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
2684 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
2685 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
2686 " already in hash table error",
2687 __func__);
2688 } else {
2689 X509_free(cert);
2690 return -1;
2691 }
2692 }
2693 X509_free(cert);
2694 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
2695 "to certificate store", __func__);
2696 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002697 conn->ca_cert_verify = 1;
2698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699 return 0;
2700
2701#else /* OPENSSL_NO_ENGINE */
2702 return -1;
2703#endif /* OPENSSL_NO_ENGINE */
2704}
2705
2706
2707static int tls_connection_engine_private_key(struct tls_connection *conn)
2708{
Adam Langley1eb02ed2015-04-21 19:00:05 -07002709#if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
2711 tls_show_errors(MSG_ERROR, __func__,
2712 "ENGINE: cannot use private key for TLS");
2713 return -1;
2714 }
2715 if (!SSL_check_private_key(conn->ssl)) {
2716 tls_show_errors(MSG_INFO, __func__,
2717 "Private key failed verification");
2718 return -1;
2719 }
2720 return 0;
2721#else /* OPENSSL_NO_ENGINE */
2722 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
2723 "engine support was not compiled in");
2724 return -1;
2725#endif /* OPENSSL_NO_ENGINE */
2726}
2727
2728
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002729static int tls_connection_private_key(struct tls_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730 struct tls_connection *conn,
2731 const char *private_key,
2732 const char *private_key_passwd,
2733 const u8 *private_key_blob,
2734 size_t private_key_blob_len)
2735{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002736 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 char *passwd;
2738 int ok;
2739
2740 if (private_key == NULL && private_key_blob == NULL)
2741 return 0;
2742
2743 if (private_key_passwd) {
2744 passwd = os_strdup(private_key_passwd);
2745 if (passwd == NULL)
2746 return -1;
2747 } else
2748 passwd = NULL;
2749
2750 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2751 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2752
2753 ok = 0;
2754 while (private_key_blob) {
2755 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
2756 (u8 *) private_key_blob,
2757 private_key_blob_len) == 1) {
2758 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2759 "ASN1(EVP_PKEY_RSA) --> OK");
2760 ok = 1;
2761 break;
2762 }
2763
2764 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
2765 (u8 *) private_key_blob,
2766 private_key_blob_len) == 1) {
2767 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
2768 "ASN1(EVP_PKEY_DSA) --> OK");
2769 ok = 1;
2770 break;
2771 }
2772
2773 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
2774 (u8 *) private_key_blob,
2775 private_key_blob_len) == 1) {
2776 wpa_printf(MSG_DEBUG, "OpenSSL: "
2777 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2778 ok = 1;
2779 break;
2780 }
2781
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002782 if (tls_read_pkcs12_blob(data, conn->ssl, private_key_blob,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 private_key_blob_len, passwd) == 0) {
2784 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
2785 "OK");
2786 ok = 1;
2787 break;
2788 }
2789
2790 break;
2791 }
2792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 while (!ok && private_key) {
2794#ifndef OPENSSL_NO_STDIO
2795 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2796 SSL_FILETYPE_ASN1) == 1) {
2797 wpa_printf(MSG_DEBUG, "OpenSSL: "
2798 "SSL_use_PrivateKey_File (DER) --> OK");
2799 ok = 1;
2800 break;
2801 }
2802
2803 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
2804 SSL_FILETYPE_PEM) == 1) {
2805 wpa_printf(MSG_DEBUG, "OpenSSL: "
2806 "SSL_use_PrivateKey_File (PEM) --> OK");
2807 ok = 1;
2808 break;
2809 }
2810#else /* OPENSSL_NO_STDIO */
2811 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
2812 __func__);
2813#endif /* OPENSSL_NO_STDIO */
2814
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002815 if (tls_read_pkcs12(data, conn->ssl, private_key, passwd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816 == 0) {
2817 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
2818 "--> OK");
2819 ok = 1;
2820 break;
2821 }
2822
2823 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
2824 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
2825 "access certificate store --> OK");
2826 ok = 1;
2827 break;
2828 }
2829
2830 break;
2831 }
2832
2833 if (!ok) {
2834 tls_show_errors(MSG_INFO, __func__,
2835 "Failed to load private key");
2836 os_free(passwd);
2837 return -1;
2838 }
2839 ERR_clear_error();
2840 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
2841 os_free(passwd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 if (!SSL_check_private_key(conn->ssl)) {
2844 tls_show_errors(MSG_INFO, __func__, "Private key failed "
2845 "verification");
2846 return -1;
2847 }
2848
2849 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
2850 return 0;
2851}
2852
2853
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002854static int tls_global_private_key(struct tls_data *data,
2855 const char *private_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 const char *private_key_passwd)
2857{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002858 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 char *passwd;
2860
2861 if (private_key == NULL)
2862 return 0;
2863
2864 if (private_key_passwd) {
2865 passwd = os_strdup(private_key_passwd);
2866 if (passwd == NULL)
2867 return -1;
2868 } else
2869 passwd = NULL;
2870
2871 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
2872 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
2873 if (
2874#ifndef OPENSSL_NO_STDIO
2875 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2876 SSL_FILETYPE_ASN1) != 1 &&
2877 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
2878 SSL_FILETYPE_PEM) != 1 &&
2879#endif /* OPENSSL_NO_STDIO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002880 tls_read_pkcs12(data, NULL, private_key, passwd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 tls_show_errors(MSG_INFO, __func__,
2882 "Failed to load private key");
2883 os_free(passwd);
2884 ERR_clear_error();
2885 return -1;
2886 }
2887 os_free(passwd);
2888 ERR_clear_error();
2889 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 if (!SSL_CTX_check_private_key(ssl_ctx)) {
2892 tls_show_errors(MSG_INFO, __func__,
2893 "Private key failed verification");
2894 return -1;
2895 }
2896
2897 return 0;
2898}
2899
2900
2901static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
2902{
2903#ifdef OPENSSL_NO_DH
2904 if (dh_file == NULL)
2905 return 0;
2906 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
2907 "dh_file specified");
2908 return -1;
2909#else /* OPENSSL_NO_DH */
2910 DH *dh;
2911 BIO *bio;
2912
2913 /* TODO: add support for dh_blob */
2914 if (dh_file == NULL)
2915 return 0;
2916 if (conn == NULL)
2917 return -1;
2918
2919 bio = BIO_new_file(dh_file, "r");
2920 if (bio == NULL) {
2921 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
2922 dh_file, ERR_error_string(ERR_get_error(), NULL));
2923 return -1;
2924 }
2925 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
2926 BIO_free(bio);
2927#ifndef OPENSSL_NO_DSA
2928 while (dh == NULL) {
2929 DSA *dsa;
2930 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
2931 " trying to parse as DSA params", dh_file,
2932 ERR_error_string(ERR_get_error(), NULL));
2933 bio = BIO_new_file(dh_file, "r");
2934 if (bio == NULL)
2935 break;
2936 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
2937 BIO_free(bio);
2938 if (!dsa) {
2939 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
2940 "'%s': %s", dh_file,
2941 ERR_error_string(ERR_get_error(), NULL));
2942 break;
2943 }
2944
2945 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
2946 dh = DSA_dup_DH(dsa);
2947 DSA_free(dsa);
2948 if (dh == NULL) {
2949 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
2950 "params into DH params");
2951 break;
2952 }
2953 break;
2954 }
2955#endif /* !OPENSSL_NO_DSA */
2956 if (dh == NULL) {
2957 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
2958 "'%s'", dh_file);
2959 return -1;
2960 }
2961
2962 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
2963 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
2964 "%s", dh_file,
2965 ERR_error_string(ERR_get_error(), NULL));
2966 DH_free(dh);
2967 return -1;
2968 }
2969 DH_free(dh);
2970 return 0;
2971#endif /* OPENSSL_NO_DH */
2972}
2973
2974
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002975static int tls_global_dh(struct tls_data *data, const char *dh_file)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976{
2977#ifdef OPENSSL_NO_DH
2978 if (dh_file == NULL)
2979 return 0;
2980 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
2981 "dh_file specified");
2982 return -1;
2983#else /* OPENSSL_NO_DH */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002984 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002985 DH *dh;
2986 BIO *bio;
2987
2988 /* TODO: add support for dh_blob */
2989 if (dh_file == NULL)
2990 return 0;
2991 if (ssl_ctx == NULL)
2992 return -1;
2993
2994 bio = BIO_new_file(dh_file, "r");
2995 if (bio == NULL) {
2996 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
2997 dh_file, ERR_error_string(ERR_get_error(), NULL));
2998 return -1;
2999 }
3000 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3001 BIO_free(bio);
3002#ifndef OPENSSL_NO_DSA
3003 while (dh == NULL) {
3004 DSA *dsa;
3005 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
3006 " trying to parse as DSA params", dh_file,
3007 ERR_error_string(ERR_get_error(), NULL));
3008 bio = BIO_new_file(dh_file, "r");
3009 if (bio == NULL)
3010 break;
3011 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
3012 BIO_free(bio);
3013 if (!dsa) {
3014 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
3015 "'%s': %s", dh_file,
3016 ERR_error_string(ERR_get_error(), NULL));
3017 break;
3018 }
3019
3020 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
3021 dh = DSA_dup_DH(dsa);
3022 DSA_free(dsa);
3023 if (dh == NULL) {
3024 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
3025 "params into DH params");
3026 break;
3027 }
3028 break;
3029 }
3030#endif /* !OPENSSL_NO_DSA */
3031 if (dh == NULL) {
3032 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
3033 "'%s'", dh_file);
3034 return -1;
3035 }
3036
3037 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
3038 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
3039 "%s", dh_file,
3040 ERR_error_string(ERR_get_error(), NULL));
3041 DH_free(dh);
3042 return -1;
3043 }
3044 DH_free(dh);
3045 return 0;
3046#endif /* OPENSSL_NO_DH */
3047}
3048
3049
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003050int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
3051 struct tls_random *keys)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052{
3053 SSL *ssl;
3054
3055 if (conn == NULL || keys == NULL)
3056 return -1;
3057 ssl = conn->ssl;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003058 if (ssl == NULL)
3059 return -1;
3060
3061 os_memset(keys, 0, sizeof(*keys));
3062 keys->client_random = conn->client_random;
3063 keys->client_random_len = SSL_get_client_random(
3064 ssl, conn->client_random, sizeof(conn->client_random));
3065 keys->server_random = conn->server_random;
3066 keys->server_random_len = SSL_get_server_random(
3067 ssl, conn->server_random, sizeof(conn->server_random));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068
3069 return 0;
3070}
3071
3072
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003073#ifndef CONFIG_FIPS
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003074static int openssl_get_keyblock_size(SSL *ssl)
3075{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003076#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003077 const EVP_CIPHER *c;
3078 const EVP_MD *h;
3079 int md_size;
3080
3081 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
3082 ssl->read_hash == NULL)
3083 return -1;
3084
3085 c = ssl->enc_read_ctx->cipher;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003086 h = EVP_MD_CTX_md(ssl->read_hash);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003087 if (h)
3088 md_size = EVP_MD_size(h);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003089 else if (ssl->s3)
3090 md_size = ssl->s3->tmp.new_mac_secret_size;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003091 else
3092 return -1;
3093
3094 wpa_printf(MSG_DEBUG, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3095 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
3096 EVP_CIPHER_iv_length(c));
3097 return 2 * (EVP_CIPHER_key_length(c) +
3098 md_size +
3099 EVP_CIPHER_iv_length(c));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003100#else
3101 const SSL_CIPHER *ssl_cipher;
3102 int cipher, digest;
3103 const EVP_CIPHER *c;
3104 const EVP_MD *h;
3105
3106 ssl_cipher = SSL_get_current_cipher(ssl);
3107 if (!ssl_cipher)
3108 return -1;
3109 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
3110 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
3111 wpa_printf(MSG_DEBUG, "OpenSSL: cipher nid %d digest nid %d",
3112 cipher, digest);
3113 if (cipher < 0 || digest < 0)
3114 return -1;
3115 c = EVP_get_cipherbynid(cipher);
3116 h = EVP_get_digestbynid(digest);
3117 if (!c || !h)
3118 return -1;
3119
3120 wpa_printf(MSG_DEBUG,
3121 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3122 EVP_CIPHER_key_length(c), EVP_MD_size(h),
3123 EVP_CIPHER_iv_length(c));
3124 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
3125 EVP_CIPHER_iv_length(c));
3126#endif
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003127}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003128#endif /* CONFIG_FIPS */
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003129
3130
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003131static int openssl_tls_prf(struct tls_connection *conn,
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003132 const char *label, int server_random_first,
3133 int skip_keyblock, u8 *out, size_t out_len)
3134{
3135#ifdef CONFIG_FIPS
3136 wpa_printf(MSG_ERROR, "OpenSSL: TLS keys cannot be exported in FIPS "
3137 "mode");
3138 return -1;
3139#else /* CONFIG_FIPS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003140 SSL *ssl;
3141 SSL_SESSION *sess;
3142 u8 *rnd;
3143 int ret = -1;
3144 int skip = 0;
3145 u8 *tmp_out = NULL;
3146 u8 *_out = out;
3147 unsigned char client_random[SSL3_RANDOM_SIZE];
3148 unsigned char server_random[SSL3_RANDOM_SIZE];
3149 unsigned char master_key[64];
3150 size_t master_key_len;
3151 const char *ver;
3152
3153 /*
3154 * TLS library did not support key generation, so get the needed TLS
3155 * session parameters and use an internal implementation of TLS PRF to
3156 * derive the key.
3157 */
3158
3159 if (conn == NULL)
3160 return -1;
3161 ssl = conn->ssl;
3162 if (ssl == NULL)
3163 return -1;
3164 ver = SSL_get_version(ssl);
3165 sess = SSL_get_session(ssl);
3166 if (!ver || !sess)
3167 return -1;
3168
3169 if (skip_keyblock) {
3170 skip = openssl_get_keyblock_size(ssl);
3171 if (skip < 0)
3172 return -1;
3173 tmp_out = os_malloc(skip + out_len);
3174 if (!tmp_out)
3175 return -1;
3176 _out = tmp_out;
3177 }
3178
3179 rnd = os_malloc(2 * SSL3_RANDOM_SIZE);
3180 if (!rnd) {
3181 os_free(tmp_out);
3182 return -1;
3183 }
3184
3185 SSL_get_client_random(ssl, client_random, sizeof(client_random));
3186 SSL_get_server_random(ssl, server_random, sizeof(server_random));
3187 master_key_len = SSL_SESSION_get_master_key(sess, master_key,
3188 sizeof(master_key));
3189
3190 if (server_random_first) {
3191 os_memcpy(rnd, server_random, SSL3_RANDOM_SIZE);
3192 os_memcpy(rnd + SSL3_RANDOM_SIZE, client_random,
3193 SSL3_RANDOM_SIZE);
3194 } else {
3195 os_memcpy(rnd, client_random, SSL3_RANDOM_SIZE);
3196 os_memcpy(rnd + SSL3_RANDOM_SIZE, server_random,
3197 SSL3_RANDOM_SIZE);
3198 }
3199
3200 if (os_strcmp(ver, "TLSv1.2") == 0) {
3201 tls_prf_sha256(master_key, master_key_len,
3202 label, rnd, 2 * SSL3_RANDOM_SIZE,
3203 _out, skip + out_len);
3204 ret = 0;
3205 } else if (tls_prf_sha1_md5(master_key, master_key_len,
3206 label, rnd, 2 * SSL3_RANDOM_SIZE,
3207 _out, skip + out_len) == 0) {
3208 ret = 0;
3209 }
3210 os_memset(master_key, 0, sizeof(master_key));
3211 os_free(rnd);
3212 if (ret == 0 && skip_keyblock)
3213 os_memcpy(out, _out + skip, out_len);
3214 bin_clear_free(tmp_out, skip);
3215
3216 return ret;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003217#endif /* CONFIG_FIPS */
3218}
3219
3220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
3222 const char *label, int server_random_first,
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003223 int skip_keyblock, u8 *out, size_t out_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003225 if (conn == NULL)
3226 return -1;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003227 if (server_random_first || skip_keyblock)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003228 return openssl_tls_prf(conn, label,
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003229 server_random_first, skip_keyblock,
3230 out, out_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003231 if (SSL_export_keying_material(conn->ssl, out, out_len, label,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003232 os_strlen(label), NULL, 0, 0) == 1) {
3233 wpa_printf(MSG_DEBUG, "OpenSSL: Using internal PRF");
3234 return 0;
3235 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003236 return openssl_tls_prf(conn, label, server_random_first,
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003237 skip_keyblock, out, out_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238}
3239
3240
3241static struct wpabuf *
3242openssl_handshake(struct tls_connection *conn, const struct wpabuf *in_data,
3243 int server)
3244{
3245 int res;
3246 struct wpabuf *out_data;
3247
3248 /*
3249 * Give TLS handshake data from the server (if available) to OpenSSL
3250 * for processing.
3251 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003252 if (in_data && wpabuf_len(in_data) > 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 BIO_write(conn->ssl_in, wpabuf_head(in_data), wpabuf_len(in_data))
3254 < 0) {
3255 tls_show_errors(MSG_INFO, __func__,
3256 "Handshake failed - BIO_write");
3257 return NULL;
3258 }
3259
3260 /* Initiate TLS handshake or continue the existing handshake */
3261 if (server)
3262 res = SSL_accept(conn->ssl);
3263 else
3264 res = SSL_connect(conn->ssl);
3265 if (res != 1) {
3266 int err = SSL_get_error(conn->ssl, res);
3267 if (err == SSL_ERROR_WANT_READ)
3268 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
3269 "more data");
3270 else if (err == SSL_ERROR_WANT_WRITE)
3271 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
3272 "write");
3273 else {
3274 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
3275 conn->failed++;
3276 }
3277 }
3278
3279 /* Get the TLS handshake data to be sent to the server */
3280 res = BIO_ctrl_pending(conn->ssl_out);
3281 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
3282 out_data = wpabuf_alloc(res);
3283 if (out_data == NULL) {
3284 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
3285 "handshake output (%d bytes)", res);
3286 if (BIO_reset(conn->ssl_out) < 0) {
3287 tls_show_errors(MSG_INFO, __func__,
3288 "BIO_reset failed");
3289 }
3290 return NULL;
3291 }
3292 res = res == 0 ? 0 : BIO_read(conn->ssl_out, wpabuf_mhead(out_data),
3293 res);
3294 if (res < 0) {
3295 tls_show_errors(MSG_INFO, __func__,
3296 "Handshake failed - BIO_read");
3297 if (BIO_reset(conn->ssl_out) < 0) {
3298 tls_show_errors(MSG_INFO, __func__,
3299 "BIO_reset failed");
3300 }
3301 wpabuf_free(out_data);
3302 return NULL;
3303 }
3304 wpabuf_put(out_data, res);
3305
3306 return out_data;
3307}
3308
3309
3310static struct wpabuf *
3311openssl_get_appl_data(struct tls_connection *conn, size_t max_len)
3312{
3313 struct wpabuf *appl_data;
3314 int res;
3315
3316 appl_data = wpabuf_alloc(max_len + 100);
3317 if (appl_data == NULL)
3318 return NULL;
3319
3320 res = SSL_read(conn->ssl, wpabuf_mhead(appl_data),
3321 wpabuf_size(appl_data));
3322 if (res < 0) {
3323 int err = SSL_get_error(conn->ssl, res);
3324 if (err == SSL_ERROR_WANT_READ ||
3325 err == SSL_ERROR_WANT_WRITE) {
3326 wpa_printf(MSG_DEBUG, "SSL: No Application Data "
3327 "included");
3328 } else {
3329 tls_show_errors(MSG_INFO, __func__,
3330 "Failed to read possible "
3331 "Application Data");
3332 }
3333 wpabuf_free(appl_data);
3334 return NULL;
3335 }
3336
3337 wpabuf_put(appl_data, res);
3338 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application Data in Finished "
3339 "message", appl_data);
3340
3341 return appl_data;
3342}
3343
3344
3345static struct wpabuf *
3346openssl_connection_handshake(struct tls_connection *conn,
3347 const struct wpabuf *in_data,
3348 struct wpabuf **appl_data, int server)
3349{
3350 struct wpabuf *out_data;
3351
3352 if (appl_data)
3353 *appl_data = NULL;
3354
3355 out_data = openssl_handshake(conn, in_data, server);
3356 if (out_data == NULL)
3357 return NULL;
Jouni Malinen26af48b2014-04-09 13:02:53 +03003358 if (conn->invalid_hb_used) {
3359 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3360 wpabuf_free(out_data);
3361 return NULL;
3362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003364 if (SSL_is_init_finished(conn->ssl)) {
3365 wpa_printf(MSG_DEBUG,
3366 "OpenSSL: Handshake finished - resumed=%d",
3367 tls_connection_resumed(conn->ssl_ctx, conn));
3368 if (appl_data && in_data)
3369 *appl_data = openssl_get_appl_data(conn,
3370 wpabuf_len(in_data));
3371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372
Jouni Malinen26af48b2014-04-09 13:02:53 +03003373 if (conn->invalid_hb_used) {
3374 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3375 if (appl_data) {
3376 wpabuf_free(*appl_data);
3377 *appl_data = NULL;
3378 }
3379 wpabuf_free(out_data);
3380 return NULL;
3381 }
3382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383 return out_data;
3384}
3385
3386
3387struct wpabuf *
3388tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
3389 const struct wpabuf *in_data,
3390 struct wpabuf **appl_data)
3391{
3392 return openssl_connection_handshake(conn, in_data, appl_data, 0);
3393}
3394
3395
3396struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
3397 struct tls_connection *conn,
3398 const struct wpabuf *in_data,
3399 struct wpabuf **appl_data)
3400{
3401 return openssl_connection_handshake(conn, in_data, appl_data, 1);
3402}
3403
3404
3405struct wpabuf * tls_connection_encrypt(void *tls_ctx,
3406 struct tls_connection *conn,
3407 const struct wpabuf *in_data)
3408{
3409 int res;
3410 struct wpabuf *buf;
3411
3412 if (conn == NULL)
3413 return NULL;
3414
3415 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3416 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
3417 (res = BIO_reset(conn->ssl_out)) < 0) {
3418 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3419 return NULL;
3420 }
3421 res = SSL_write(conn->ssl, wpabuf_head(in_data), wpabuf_len(in_data));
3422 if (res < 0) {
3423 tls_show_errors(MSG_INFO, __func__,
3424 "Encryption failed - SSL_write");
3425 return NULL;
3426 }
3427
3428 /* Read encrypted data to be sent to the server */
3429 buf = wpabuf_alloc(wpabuf_len(in_data) + 300);
3430 if (buf == NULL)
3431 return NULL;
3432 res = BIO_read(conn->ssl_out, wpabuf_mhead(buf), wpabuf_size(buf));
3433 if (res < 0) {
3434 tls_show_errors(MSG_INFO, __func__,
3435 "Encryption failed - BIO_read");
3436 wpabuf_free(buf);
3437 return NULL;
3438 }
3439 wpabuf_put(buf, res);
3440
3441 return buf;
3442}
3443
3444
3445struct wpabuf * tls_connection_decrypt(void *tls_ctx,
3446 struct tls_connection *conn,
3447 const struct wpabuf *in_data)
3448{
3449 int res;
3450 struct wpabuf *buf;
3451
3452 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3453 res = BIO_write(conn->ssl_in, wpabuf_head(in_data),
3454 wpabuf_len(in_data));
3455 if (res < 0) {
3456 tls_show_errors(MSG_INFO, __func__,
3457 "Decryption failed - BIO_write");
3458 return NULL;
3459 }
3460 if (BIO_reset(conn->ssl_out) < 0) {
3461 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
3462 return NULL;
3463 }
3464
3465 /* Read decrypted data for further processing */
3466 /*
3467 * Even though we try to disable TLS compression, it is possible that
3468 * this cannot be done with all TLS libraries. Add extra buffer space
3469 * to handle the possibility of the decrypted data being longer than
3470 * input data.
3471 */
3472 buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
3473 if (buf == NULL)
3474 return NULL;
3475 res = SSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
3476 if (res < 0) {
3477 tls_show_errors(MSG_INFO, __func__,
3478 "Decryption failed - SSL_read");
3479 wpabuf_free(buf);
3480 return NULL;
3481 }
3482 wpabuf_put(buf, res);
3483
Jouni Malinen26af48b2014-04-09 13:02:53 +03003484 if (conn->invalid_hb_used) {
3485 wpa_printf(MSG_INFO, "TLS: Heartbeat attack detected - do not send response");
3486 wpabuf_free(buf);
3487 return NULL;
3488 }
3489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490 return buf;
3491}
3492
3493
3494int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
3495{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003496 return conn ? SSL_cache_hit(conn->ssl) : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003497}
3498
3499
3500int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
3501 u8 *ciphers)
3502{
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003503 char buf[500], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 u8 *c;
3505 int ret;
3506
3507 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
3508 return -1;
3509
3510 buf[0] = '\0';
3511 pos = buf;
3512 end = pos + sizeof(buf);
3513
3514 c = ciphers;
3515 while (*c != TLS_CIPHER_NONE) {
3516 const char *suite;
3517
3518 switch (*c) {
3519 case TLS_CIPHER_RC4_SHA:
3520 suite = "RC4-SHA";
3521 break;
3522 case TLS_CIPHER_AES128_SHA:
3523 suite = "AES128-SHA";
3524 break;
3525 case TLS_CIPHER_RSA_DHE_AES128_SHA:
3526 suite = "DHE-RSA-AES128-SHA";
3527 break;
3528 case TLS_CIPHER_ANON_DH_AES128_SHA:
3529 suite = "ADH-AES128-SHA";
3530 break;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003531 case TLS_CIPHER_RSA_DHE_AES256_SHA:
3532 suite = "DHE-RSA-AES256-SHA";
3533 break;
3534 case TLS_CIPHER_AES256_SHA:
3535 suite = "AES256-SHA";
3536 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537 default:
3538 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
3539 "cipher selection: %d", *c);
3540 return -1;
3541 }
3542 ret = os_snprintf(pos, end - pos, ":%s", suite);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003543 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544 break;
3545 pos += ret;
3546
3547 c++;
3548 }
3549
3550 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
3551
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003552#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003553#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3554 if (os_strstr(buf, ":ADH-")) {
3555 /*
3556 * Need to drop to security level 0 to allow anonymous
3557 * cipher suites for EAP-FAST.
3558 */
3559 SSL_set_security_level(conn->ssl, 0);
3560 } else if (SSL_get_security_level(conn->ssl) == 0) {
3561 /* Force at least security level 1 */
3562 SSL_set_security_level(conn->ssl, 1);
3563 }
3564#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3565#endif
3566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
3568 tls_show_errors(MSG_INFO, __func__,
3569 "Cipher suite configuration failed");
3570 return -1;
3571 }
3572
3573 return 0;
3574}
3575
3576
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003577int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
3578 char *buf, size_t buflen)
3579{
3580 const char *name;
3581 if (conn == NULL || conn->ssl == NULL)
3582 return -1;
3583
3584 name = SSL_get_version(conn->ssl);
3585 if (name == NULL)
3586 return -1;
3587
3588 os_strlcpy(buf, name, buflen);
3589 return 0;
3590}
3591
3592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
3594 char *buf, size_t buflen)
3595{
3596 const char *name;
3597 if (conn == NULL || conn->ssl == NULL)
3598 return -1;
3599
3600 name = SSL_get_cipher(conn->ssl);
3601 if (name == NULL)
3602 return -1;
3603
3604 os_strlcpy(buf, name, buflen);
3605 return 0;
3606}
3607
3608
3609int tls_connection_enable_workaround(void *ssl_ctx,
3610 struct tls_connection *conn)
3611{
3612 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
3613
3614 return 0;
3615}
3616
3617
3618#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3619/* ClientHello TLS extensions require a patch to openssl, so this function is
3620 * commented out unless explicitly needed for EAP-FAST in order to be able to
3621 * build this file with unmodified openssl. */
3622int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
3623 int ext_type, const u8 *data,
3624 size_t data_len)
3625{
3626 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
3627 return -1;
3628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
3630 data_len) != 1)
3631 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632
3633 return 0;
3634}
3635#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3636
3637
3638int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
3639{
3640 if (conn == NULL)
3641 return -1;
3642 return conn->failed;
3643}
3644
3645
3646int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
3647{
3648 if (conn == NULL)
3649 return -1;
3650 return conn->read_alerts;
3651}
3652
3653
3654int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
3655{
3656 if (conn == NULL)
3657 return -1;
3658 return conn->write_alerts;
3659}
3660
3661
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003662#ifdef HAVE_OCSP
3663
3664static void ocsp_debug_print_resp(OCSP_RESPONSE *rsp)
3665{
3666#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003667 BIO *out;
3668 size_t rlen;
3669 char *txt;
3670 int res;
3671
3672 if (wpa_debug_level > MSG_DEBUG)
3673 return;
3674
3675 out = BIO_new(BIO_s_mem());
3676 if (!out)
3677 return;
3678
3679 OCSP_RESPONSE_print(out, rsp, 0);
3680 rlen = BIO_ctrl_pending(out);
3681 txt = os_malloc(rlen + 1);
3682 if (!txt) {
3683 BIO_free(out);
3684 return;
3685 }
3686
3687 res = BIO_read(out, txt, rlen);
3688 if (res > 0) {
3689 txt[res] = '\0';
3690 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP Response\n%s", txt);
3691 }
3692 os_free(txt);
3693 BIO_free(out);
3694#endif /* CONFIG_NO_STDOUT_DEBUG */
3695}
3696
3697
Dmitry Shmidt71757432014-06-02 13:50:35 -07003698static void debug_print_cert(X509 *cert, const char *title)
3699{
3700#ifndef CONFIG_NO_STDOUT_DEBUG
3701 BIO *out;
3702 size_t rlen;
3703 char *txt;
3704 int res;
3705
3706 if (wpa_debug_level > MSG_DEBUG)
3707 return;
3708
3709 out = BIO_new(BIO_s_mem());
3710 if (!out)
3711 return;
3712
3713 X509_print(out, cert);
3714 rlen = BIO_ctrl_pending(out);
3715 txt = os_malloc(rlen + 1);
3716 if (!txt) {
3717 BIO_free(out);
3718 return;
3719 }
3720
3721 res = BIO_read(out, txt, rlen);
3722 if (res > 0) {
3723 txt[res] = '\0';
3724 wpa_printf(MSG_DEBUG, "OpenSSL: %s\n%s", title, txt);
3725 }
3726 os_free(txt);
3727
3728 BIO_free(out);
3729#endif /* CONFIG_NO_STDOUT_DEBUG */
3730}
3731
3732
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003733static int ocsp_resp_cb(SSL *s, void *arg)
3734{
3735 struct tls_connection *conn = arg;
3736 const unsigned char *p;
3737 int len, status, reason;
3738 OCSP_RESPONSE *rsp;
3739 OCSP_BASICRESP *basic;
3740 OCSP_CERTID *id;
3741 ASN1_GENERALIZEDTIME *produced_at, *this_update, *next_update;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003742 X509_STORE *store;
3743 STACK_OF(X509) *certs = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003744
3745 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
3746 if (!p) {
3747 wpa_printf(MSG_DEBUG, "OpenSSL: No OCSP response received");
3748 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3749 }
3750
3751 wpa_hexdump(MSG_DEBUG, "OpenSSL: OCSP response", p, len);
3752
3753 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
3754 if (!rsp) {
3755 wpa_printf(MSG_INFO, "OpenSSL: Failed to parse OCSP response");
3756 return 0;
3757 }
3758
3759 ocsp_debug_print_resp(rsp);
3760
3761 status = OCSP_response_status(rsp);
3762 if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
3763 wpa_printf(MSG_INFO, "OpenSSL: OCSP responder error %d (%s)",
3764 status, OCSP_response_status_str(status));
3765 return 0;
3766 }
3767
3768 basic = OCSP_response_get1_basic(rsp);
3769 if (!basic) {
3770 wpa_printf(MSG_INFO, "OpenSSL: Could not find BasicOCSPResponse");
3771 return 0;
3772 }
3773
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003774 store = SSL_CTX_get_cert_store(conn->ssl_ctx);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003775 if (conn->peer_issuer) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07003776 debug_print_cert(conn->peer_issuer, "Add OCSP issuer");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003777
3778 if (X509_STORE_add_cert(store, conn->peer_issuer) != 1) {
3779 tls_show_errors(MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003780 "OpenSSL: Could not add issuer to certificate store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003781 }
3782 certs = sk_X509_new_null();
3783 if (certs) {
3784 X509 *cert;
3785 cert = X509_dup(conn->peer_issuer);
3786 if (cert && !sk_X509_push(certs, cert)) {
3787 tls_show_errors(
3788 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003789 "OpenSSL: Could not add issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003790 X509_free(cert);
3791 sk_X509_free(certs);
3792 certs = NULL;
3793 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003794 if (certs && conn->peer_issuer_issuer) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003795 cert = X509_dup(conn->peer_issuer_issuer);
3796 if (cert && !sk_X509_push(certs, cert)) {
3797 tls_show_errors(
3798 MSG_INFO, __func__,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003799 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003800 X509_free(cert);
3801 }
3802 }
3803 }
3804 }
3805
3806 status = OCSP_basic_verify(basic, certs, store, OCSP_TRUSTOTHER);
3807 sk_X509_pop_free(certs, X509_free);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003808 if (status <= 0) {
3809 tls_show_errors(MSG_INFO, __func__,
3810 "OpenSSL: OCSP response failed verification");
3811 OCSP_BASICRESP_free(basic);
3812 OCSP_RESPONSE_free(rsp);
3813 return 0;
3814 }
3815
3816 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP response verification succeeded");
3817
Dmitry Shmidt56052862013-10-04 10:23:25 -07003818 if (!conn->peer_cert) {
3819 wpa_printf(MSG_DEBUG, "OpenSSL: Peer certificate not available for OCSP status check");
3820 OCSP_BASICRESP_free(basic);
3821 OCSP_RESPONSE_free(rsp);
3822 return 0;
3823 }
3824
3825 if (!conn->peer_issuer) {
3826 wpa_printf(MSG_DEBUG, "OpenSSL: Peer issuer certificate not available for OCSP status check");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003827 OCSP_BASICRESP_free(basic);
3828 OCSP_RESPONSE_free(rsp);
3829 return 0;
3830 }
3831
3832 id = OCSP_cert_to_id(NULL, conn->peer_cert, conn->peer_issuer);
3833 if (!id) {
3834 wpa_printf(MSG_DEBUG, "OpenSSL: Could not create OCSP certificate identifier");
3835 OCSP_BASICRESP_free(basic);
3836 OCSP_RESPONSE_free(rsp);
3837 return 0;
3838 }
3839
3840 if (!OCSP_resp_find_status(basic, id, &status, &reason, &produced_at,
3841 &this_update, &next_update)) {
3842 wpa_printf(MSG_INFO, "OpenSSL: Could not find current server certificate from OCSP response%s",
3843 (conn->flags & TLS_CONN_REQUIRE_OCSP) ? "" :
3844 " (OCSP not required)");
3845 OCSP_BASICRESP_free(basic);
3846 OCSP_RESPONSE_free(rsp);
3847 return (conn->flags & TLS_CONN_REQUIRE_OCSP) ? 0 : 1;
3848 }
3849
3850 if (!OCSP_check_validity(this_update, next_update, 5 * 60, -1)) {
3851 tls_show_errors(MSG_INFO, __func__,
3852 "OpenSSL: OCSP status times invalid");
3853 OCSP_BASICRESP_free(basic);
3854 OCSP_RESPONSE_free(rsp);
3855 return 0;
3856 }
3857
3858 OCSP_BASICRESP_free(basic);
3859 OCSP_RESPONSE_free(rsp);
3860
3861 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status for server certificate: %s",
3862 OCSP_cert_status_str(status));
3863
3864 if (status == V_OCSP_CERTSTATUS_GOOD)
3865 return 1;
3866 if (status == V_OCSP_CERTSTATUS_REVOKED)
3867 return 0;
3868 if (conn->flags & TLS_CONN_REQUIRE_OCSP) {
3869 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status unknown, but OCSP required");
3870 return 0;
3871 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07003872 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 -07003873 return 1;
3874}
3875
3876
3877static int ocsp_status_cb(SSL *s, void *arg)
3878{
3879 char *tmp;
3880 char *resp;
3881 size_t len;
3882
3883 if (tls_global->ocsp_stapling_response == NULL) {
3884 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - no response configured");
3885 return SSL_TLSEXT_ERR_OK;
3886 }
3887
3888 resp = os_readfile(tls_global->ocsp_stapling_response, &len);
3889 if (resp == NULL) {
3890 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - could not read response file");
3891 /* TODO: Build OCSPResponse with responseStatus = internalError
3892 */
3893 return SSL_TLSEXT_ERR_OK;
3894 }
3895 wpa_printf(MSG_DEBUG, "OpenSSL: OCSP status callback - send cached response");
3896 tmp = OPENSSL_malloc(len);
3897 if (tmp == NULL) {
3898 os_free(resp);
3899 return SSL_TLSEXT_ERR_ALERT_FATAL;
3900 }
3901
3902 os_memcpy(tmp, resp, len);
3903 os_free(resp);
3904 SSL_set_tlsext_status_ocsp_resp(s, tmp, len);
3905
3906 return SSL_TLSEXT_ERR_OK;
3907}
3908
3909#endif /* HAVE_OCSP */
3910
3911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
3913 const struct tls_connection_params *params)
3914{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003915 struct tls_data *data = tls_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 int ret;
3917 unsigned long err;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003918 int can_pkcs11 = 0;
3919 const char *key_id = params->key_id;
3920 const char *cert_id = params->cert_id;
3921 const char *ca_cert_id = params->ca_cert_id;
3922 const char *engine_id = params->engine ? params->engine_id : NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923
3924 if (conn == NULL)
3925 return -1;
3926
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08003927 if (params->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
3928 wpa_printf(MSG_INFO,
3929 "OpenSSL: ocsp=3 not supported");
3930 return -1;
3931 }
3932
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003933 /*
3934 * If the engine isn't explicitly configured, and any of the
3935 * cert/key fields are actually PKCS#11 URIs, then automatically
3936 * use the PKCS#11 ENGINE.
3937 */
3938 if (!engine_id || os_strcmp(engine_id, "pkcs11") == 0)
3939 can_pkcs11 = 1;
3940
3941 if (!key_id && params->private_key && can_pkcs11 &&
3942 os_strncmp(params->private_key, "pkcs11:", 7) == 0) {
3943 can_pkcs11 = 2;
3944 key_id = params->private_key;
3945 }
3946
3947 if (!cert_id && params->client_cert && can_pkcs11 &&
3948 os_strncmp(params->client_cert, "pkcs11:", 7) == 0) {
3949 can_pkcs11 = 2;
3950 cert_id = params->client_cert;
3951 }
3952
3953 if (!ca_cert_id && params->ca_cert && can_pkcs11 &&
3954 os_strncmp(params->ca_cert, "pkcs11:", 7) == 0) {
3955 can_pkcs11 = 2;
3956 ca_cert_id = params->ca_cert;
3957 }
3958
3959 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3960 if (can_pkcs11 == 2 && !engine_id)
3961 engine_id = "pkcs11";
3962
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003963#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3964#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003965 if (params->flags & TLS_CONN_EAP_FAST) {
3966 wpa_printf(MSG_DEBUG,
3967 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3968 if (SSL_set_ssl_method(conn->ssl, TLSv1_method()) != 1) {
3969 tls_show_errors(MSG_INFO, __func__,
3970 "Failed to set TLSv1_method() for EAP-FAST");
3971 return -1;
3972 }
3973 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003974#endif
3975#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977 while ((err = ERR_get_error())) {
3978 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
3979 __func__, ERR_error_string(err, NULL));
3980 }
3981
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003982 if (engine_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003984 ret = tls_engine_init(conn, engine_id, params->pin,
3985 key_id, cert_id, ca_cert_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986 if (ret)
3987 return ret;
3988 }
3989 if (tls_connection_set_subject_match(conn,
3990 params->subject_match,
Dmitry Shmidt051af732013-10-22 13:52:46 -07003991 params->altsubject_match,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003992 params->suffix_match,
3993 params->domain_match))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994 return -1;
3995
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003996 if (engine_id && ca_cert_id) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003997 if (tls_connection_engine_ca_cert(data, conn, ca_cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003999 } else if (tls_connection_ca_cert(data, conn, params->ca_cert,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000 params->ca_cert_blob,
4001 params->ca_cert_blob_len,
4002 params->ca_path))
4003 return -1;
4004
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004005 if (engine_id && cert_id) {
4006 if (tls_connection_engine_client_cert(conn, cert_id))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
4008 } else if (tls_connection_client_cert(conn, params->client_cert,
4009 params->client_cert_blob,
4010 params->client_cert_blob_len))
4011 return -1;
4012
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004013 if (engine_id && key_id) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
4015 if (tls_connection_engine_private_key(conn))
4016 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004017 } else if (tls_connection_private_key(data, conn,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018 params->private_key,
4019 params->private_key_passwd,
4020 params->private_key_blob,
4021 params->private_key_blob_len)) {
4022 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
4023 params->private_key);
4024 return -1;
4025 }
4026
4027 if (tls_connection_dh(conn, params->dh_file)) {
4028 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
4029 params->dh_file);
4030 return -1;
4031 }
4032
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004033 if (params->openssl_ciphers &&
4034 SSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) {
4035 wpa_printf(MSG_INFO,
4036 "OpenSSL: Failed to set cipher string '%s'",
4037 params->openssl_ciphers);
4038 return -1;
4039 }
4040
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004041 tls_set_conn_flags(conn->ssl, params->flags);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004042
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004043#ifdef OPENSSL_IS_BORINGSSL
4044 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4045 SSL_enable_ocsp_stapling(conn->ssl);
4046 }
4047#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004048#ifdef HAVE_OCSP
4049 if (params->flags & TLS_CONN_REQUEST_OCSP) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004050 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004051 SSL_set_tlsext_status_type(conn->ssl, TLSEXT_STATUSTYPE_ocsp);
4052 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_resp_cb);
4053 SSL_CTX_set_tlsext_status_arg(ssl_ctx, conn);
4054 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004055#else /* HAVE_OCSP */
4056 if (params->flags & TLS_CONN_REQUIRE_OCSP) {
4057 wpa_printf(MSG_INFO,
4058 "OpenSSL: No OCSP support included - reject configuration");
4059 return -1;
4060 }
4061 if (params->flags & TLS_CONN_REQUEST_OCSP) {
4062 wpa_printf(MSG_DEBUG,
4063 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4064 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004065#endif /* HAVE_OCSP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004066#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004067
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004068 conn->flags = params->flags;
4069
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004070 tls_get_errors(data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071
4072 return 0;
4073}
4074
4075
4076int tls_global_set_params(void *tls_ctx,
4077 const struct tls_connection_params *params)
4078{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004079 struct tls_data *data = tls_ctx;
4080 SSL_CTX *ssl_ctx = data->ssl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 unsigned long err;
4082
4083 while ((err = ERR_get_error())) {
4084 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
4085 __func__, ERR_error_string(err, NULL));
4086 }
4087
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004088 if (tls_global_ca_cert(data, params->ca_cert) ||
4089 tls_global_client_cert(data, params->client_cert) ||
4090 tls_global_private_key(data, params->private_key,
4091 params->private_key_passwd) ||
4092 tls_global_dh(data, params->dh_file)) {
4093 wpa_printf(MSG_INFO, "TLS: Failed to set global parameters");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 return -1;
4095 }
4096
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004097 if (params->openssl_ciphers &&
4098 SSL_CTX_set_cipher_list(ssl_ctx, params->openssl_ciphers) != 1) {
4099 wpa_printf(MSG_INFO,
4100 "OpenSSL: Failed to set cipher string '%s'",
4101 params->openssl_ciphers);
4102 return -1;
4103 }
4104
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004105#ifdef SSL_OP_NO_TICKET
4106 if (params->flags & TLS_CONN_DISABLE_SESSION_TICKET)
4107 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004108#ifdef SSL_CTX_clear_options
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004109 else
4110 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TICKET);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004111#endif /* SSL_clear_options */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004112#endif /* SSL_OP_NO_TICKET */
4113
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004114#ifdef HAVE_OCSP
4115 SSL_CTX_set_tlsext_status_cb(ssl_ctx, ocsp_status_cb);
4116 SSL_CTX_set_tlsext_status_arg(ssl_ctx, ssl_ctx);
4117 os_free(tls_global->ocsp_stapling_response);
4118 if (params->ocsp_stapling_response)
4119 tls_global->ocsp_stapling_response =
4120 os_strdup(params->ocsp_stapling_response);
4121 else
4122 tls_global->ocsp_stapling_response = NULL;
4123#endif /* HAVE_OCSP */
4124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 return 0;
4126}
4127
4128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4130/* Pre-shared secred requires a patch to openssl, so this function is
4131 * commented out unless explicitly needed for EAP-FAST in order to be able to
4132 * build this file with unmodified openssl. */
4133
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004134#if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004135static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4136 STACK_OF(SSL_CIPHER) *peer_ciphers,
4137 const SSL_CIPHER **cipher, void *arg)
4138#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
4140 STACK_OF(SSL_CIPHER) *peer_ciphers,
4141 SSL_CIPHER **cipher, void *arg)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004142#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143{
4144 struct tls_connection *conn = arg;
4145 int ret;
4146
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004147#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148 if (conn == NULL || conn->session_ticket_cb == NULL)
4149 return 0;
4150
4151 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4152 conn->session_ticket,
4153 conn->session_ticket_len,
4154 s->s3->client_random,
4155 s->s3->server_random, secret);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004156#else
4157 unsigned char client_random[SSL3_RANDOM_SIZE];
4158 unsigned char server_random[SSL3_RANDOM_SIZE];
4159
4160 if (conn == NULL || conn->session_ticket_cb == NULL)
4161 return 0;
4162
4163 SSL_get_client_random(s, client_random, sizeof(client_random));
4164 SSL_get_server_random(s, server_random, sizeof(server_random));
4165
4166 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
4167 conn->session_ticket,
4168 conn->session_ticket_len,
4169 client_random,
4170 server_random, secret);
4171#endif
4172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 os_free(conn->session_ticket);
4174 conn->session_ticket = NULL;
4175
4176 if (ret <= 0)
4177 return 0;
4178
4179 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
4180 return 1;
4181}
4182
4183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
4185 int len, void *arg)
4186{
4187 struct tls_connection *conn = arg;
4188
4189 if (conn == NULL || conn->session_ticket_cb == NULL)
4190 return 0;
4191
4192 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
4193
4194 os_free(conn->session_ticket);
4195 conn->session_ticket = NULL;
4196
4197 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
4198 "extension", data, len);
4199
4200 conn->session_ticket = os_malloc(len);
4201 if (conn->session_ticket == NULL)
4202 return 0;
4203
4204 os_memcpy(conn->session_ticket, data, len);
4205 conn->session_ticket_len = len;
4206
4207 return 1;
4208}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4210
4211
4212int tls_connection_set_session_ticket_cb(void *tls_ctx,
4213 struct tls_connection *conn,
4214 tls_session_ticket_cb cb,
4215 void *ctx)
4216{
4217#if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4218 conn->session_ticket_cb = cb;
4219 conn->session_ticket_cb_ctx = ctx;
4220
4221 if (cb) {
4222 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
4223 conn) != 1)
4224 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 SSL_set_session_ticket_ext_cb(conn->ssl,
4226 tls_session_ticket_ext_cb, conn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227 } else {
4228 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
4229 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231 }
4232
4233 return 0;
4234#else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4235 return -1;
4236#endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4237}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004238
4239
4240int tls_get_library_version(char *buf, size_t buf_len)
4241{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08004242#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004243 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4244 OPENSSL_VERSION_TEXT,
4245 OpenSSL_version(OPENSSL_VERSION));
4246#else
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004247 return os_snprintf(buf, buf_len, "OpenSSL build=%s run=%s",
4248 OPENSSL_VERSION_TEXT,
4249 SSLeay_version(SSLEAY_VERSION));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004250#endif
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004251}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004252
4253
4254void tls_connection_set_success_data(struct tls_connection *conn,
4255 struct wpabuf *data)
4256{
4257 SSL_SESSION *sess;
4258 struct wpabuf *old;
4259
4260 if (tls_ex_idx_session < 0)
4261 goto fail;
4262 sess = SSL_get_session(conn->ssl);
4263 if (!sess)
4264 goto fail;
4265 old = SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4266 if (old) {
4267 wpa_printf(MSG_DEBUG, "OpenSSL: Replacing old success data %p",
4268 old);
4269 wpabuf_free(old);
4270 }
4271 if (SSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
4272 goto fail;
4273
4274 wpa_printf(MSG_DEBUG, "OpenSSL: Stored success data %p", data);
4275 conn->success_data = 1;
4276 return;
4277
4278fail:
4279 wpa_printf(MSG_INFO, "OpenSSL: Failed to store success data");
4280 wpabuf_free(data);
4281}
4282
4283
4284void tls_connection_set_success_data_resumed(struct tls_connection *conn)
4285{
4286 wpa_printf(MSG_DEBUG,
4287 "OpenSSL: Success data accepted for resumed session");
4288 conn->success_data = 1;
4289}
4290
4291
4292const struct wpabuf *
4293tls_connection_get_success_data(struct tls_connection *conn)
4294{
4295 SSL_SESSION *sess;
4296
4297 if (tls_ex_idx_session < 0 ||
4298 !(sess = SSL_get_session(conn->ssl)))
4299 return NULL;
4300 return SSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
4301}
4302
4303
4304void tls_connection_remove_session(struct tls_connection *conn)
4305{
4306 SSL_SESSION *sess;
4307
4308 sess = SSL_get_session(conn->ssl);
4309 if (!sess)
4310 return;
4311
4312 if (SSL_CTX_remove_session(conn->ssl_ctx, sess) != 1)
4313 wpa_printf(MSG_DEBUG,
4314 "OpenSSL: Session was not cached");
4315 else
4316 wpa_printf(MSG_DEBUG,
4317 "OpenSSL: Removed cached session to disable session resumption");
4318}