2016-05-18 02:18:30 +08:00
|
|
|
/*
|
2021-01-28 03:23:33 +08:00
|
|
|
* Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2017-06-15 22:16:46 +08:00
|
|
|
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
|
2006-06-14 16:55:23 +08:00
|
|
|
*
|
2018-12-06 20:08:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:18:30 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-05-10 04:12:44 +08:00
|
|
|
*/
|
2016-05-18 02:18:30 +08:00
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
#include <stdio.h>
|
2017-08-25 20:51:45 +08:00
|
|
|
#include <sys/types.h>
|
1999-09-12 01:54:18 +08:00
|
|
|
|
2017-08-18 11:52:46 +08:00
|
|
|
#include "internal/nelem.h"
|
2015-05-14 20:54:49 +08:00
|
|
|
#include "internal/o_dir.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/pem.h>
|
2019-03-07 06:34:19 +08:00
|
|
|
#include <openssl/store.h>
|
1999-11-30 06:35:00 +08:00
|
|
|
#include <openssl/x509v3.h>
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/dh.h>
|
2004-05-18 02:53:47 +08:00
|
|
|
#include <openssl/bn.h>
|
2016-05-16 01:02:17 +08:00
|
|
|
#include <openssl/crypto.h>
|
2017-08-22 05:17:35 +08:00
|
|
|
#include "internal/refcount.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "ssl_local.h"
|
2017-07-08 19:11:59 +08:00
|
|
|
#include "ssl_cert_table.h"
|
2016-07-20 01:42:11 +08:00
|
|
|
#include "internal/thread_once.h"
|
2023-02-21 21:20:24 +08:00
|
|
|
#ifndef OPENSSL_NO_POSIX_IO
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# ifdef _WIN32
|
|
|
|
# define stat _stat
|
|
|
|
# endif
|
2023-04-20 17:41:46 +08:00
|
|
|
# ifndef S_ISDIR
|
|
|
|
# define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
|
|
|
|
# endif
|
2023-02-21 21:20:24 +08:00
|
|
|
#endif
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-08-06 01:03:17 +08:00
|
|
|
static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
|
|
|
|
int op, int bits, int nid, void *other,
|
2015-01-22 11:40:55 +08:00
|
|
|
void *ex);
|
2013-12-15 21:32:24 +08:00
|
|
|
|
2016-03-01 01:26:07 +08:00
|
|
|
static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;
|
|
|
|
static volatile int ssl_x509_store_ctx_idx = -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-07-20 01:42:11 +08:00
|
|
|
DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)
|
2016-03-01 01:26:07 +08:00
|
|
|
{
|
|
|
|
ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
|
2016-08-06 01:03:17 +08:00
|
|
|
"SSL for verify callback",
|
|
|
|
NULL, NULL, NULL);
|
2016-07-20 01:42:11 +08:00
|
|
|
return ssl_x509_store_ctx_idx >= 0;
|
2016-03-01 01:26:07 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-03-01 01:26:07 +08:00
|
|
|
int SSL_get_ex_data_X509_STORE_CTX_idx(void)
|
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-07-20 01:42:11 +08:00
|
|
|
if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
|
|
|
|
return -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
return ssl_x509_store_ctx_idx;
|
|
|
|
}
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
CERT *ssl_cert_new(size_t ssl_pkey_num)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-09-26 23:32:05 +08:00
|
|
|
CERT *ret = NULL;
|
|
|
|
|
|
|
|
/* Should never happen */
|
|
|
|
if (!ossl_assert(ssl_pkey_num >= SSL_PKEY_NUM))
|
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
ret = OPENSSL_zalloc(sizeof(*ret));
|
2022-09-29 19:57:34 +08:00
|
|
|
if (ret == NULL)
|
2016-03-01 01:26:07 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
ret->ssl_pkey_num = ssl_pkey_num;
|
|
|
|
ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
|
|
|
|
if (ret->pkeys == NULL) {
|
|
|
|
OPENSSL_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-10 12:23:53 +08:00
|
|
|
ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->sec_cb = ssl_security_default_callback;
|
|
|
|
ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
|
|
|
|
ret->sec_ex = NULL;
|
2023-06-22 07:39:36 +08:00
|
|
|
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
|
2022-09-26 23:32:05 +08:00
|
|
|
OPENSSL_free(ret->pkeys);
|
2016-03-01 01:26:07 +08:00
|
|
|
OPENSSL_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-05-10 04:12:44 +08:00
|
|
|
CERT *ssl_cert_dup(CERT *cert)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-08-26 01:25:58 +08:00
|
|
|
CERT *ret = OPENSSL_zalloc(sizeof(*ret));
|
2022-09-26 23:32:05 +08:00
|
|
|
size_t i;
|
2021-08-10 04:56:50 +08:00
|
|
|
#ifndef OPENSSL_NO_COMP_ALG
|
|
|
|
int j;
|
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2022-09-29 19:57:34 +08:00
|
|
|
if (ret == NULL)
|
2016-03-01 01:26:07 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
ret->ssl_pkey_num = cert->ssl_pkey_num;
|
|
|
|
ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
|
2023-03-14 09:04:11 +08:00
|
|
|
if (ret->pkeys == NULL) {
|
|
|
|
OPENSSL_free(ret);
|
2022-09-26 23:32:05 +08:00
|
|
|
return NULL;
|
2023-03-14 09:04:11 +08:00
|
|
|
}
|
2022-09-26 23:32:05 +08:00
|
|
|
|
2015-05-05 06:00:15 +08:00
|
|
|
ret->key = &ret->pkeys[cert->key - cert->pkeys];
|
2023-06-22 07:39:36 +08:00
|
|
|
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
|
2023-03-14 09:04:11 +08:00
|
|
|
OPENSSL_free(ret->pkeys);
|
2016-03-01 01:26:07 +08:00
|
|
|
OPENSSL_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-16 23:35:44 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (cert->dh_tmp != NULL) {
|
2015-12-17 08:05:26 +08:00
|
|
|
ret->dh_tmp = cert->dh_tmp;
|
|
|
|
EVP_PKEY_up_ref(ret->dh_tmp);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-01-13 20:39:40 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->dh_tmp_cb = cert->dh_tmp_cb;
|
2020-10-16 23:35:44 +08:00
|
|
|
ret->dh_tmp_auto = cert->dh_tmp_auto;
|
1999-05-10 04:12:44 +08:00
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
for (i = 0; i < ret->ssl_pkey_num; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
CERT_PKEY *cpk = cert->pkeys + i;
|
|
|
|
CERT_PKEY *rpk = ret->pkeys + i;
|
2021-08-10 04:56:50 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (cpk->x509 != NULL) {
|
|
|
|
rpk->x509 = cpk->x509;
|
2015-09-01 03:29:57 +08:00
|
|
|
X509_up_ref(rpk->x509);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cpk->privatekey != NULL) {
|
|
|
|
rpk->privatekey = cpk->privatekey;
|
2016-01-19 08:21:12 +08:00
|
|
|
EVP_PKEY_up_ref(cpk->privatekey);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cpk->chain) {
|
|
|
|
rpk->chain = X509_chain_up_ref(cpk->chain);
|
|
|
|
if (!rpk->chain) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 04:56:50 +08:00
|
|
|
if (cpk->serverinfo != NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Just copy everything. */
|
2021-08-10 04:56:50 +08:00
|
|
|
rpk->serverinfo = OPENSSL_memdup(cpk->serverinfo, cpk->serverinfo_length);
|
|
|
|
if (rpk->serverinfo == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2021-08-10 04:56:50 +08:00
|
|
|
rpk->serverinfo_length = cpk->serverinfo_length;
|
|
|
|
}
|
|
|
|
#ifndef OPENSSL_NO_COMP_ALG
|
|
|
|
for (j = TLSEXT_comp_cert_none; j < TLSEXT_comp_cert_limit; j++) {
|
|
|
|
if (cpk->comp_cert[j] != NULL) {
|
|
|
|
if (!OSSL_COMP_CERT_up_ref(cpk->comp_cert[j]))
|
|
|
|
goto err;
|
|
|
|
rpk->comp_cert[j] = cpk->comp_cert[j];
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-08-10 04:56:50 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2015-05-13 00:17:37 +08:00
|
|
|
/* Configured sigalgs copied across */
|
2015-01-22 11:40:55 +08:00
|
|
|
if (cert->conf_sigalgs) {
|
2016-12-14 22:31:21 +08:00
|
|
|
ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
|
|
|
|
* sizeof(*cert->conf_sigalgs));
|
2015-10-30 18:05:53 +08:00
|
|
|
if (ret->conf_sigalgs == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-12-14 22:31:21 +08:00
|
|
|
memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
|
|
|
|
cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->conf_sigalgslen = cert->conf_sigalgslen;
|
|
|
|
} else
|
|
|
|
ret->conf_sigalgs = NULL;
|
|
|
|
|
|
|
|
if (cert->client_sigalgs) {
|
2016-12-14 22:31:21 +08:00
|
|
|
ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
|
|
|
|
* sizeof(*cert->client_sigalgs));
|
2015-10-30 18:05:53 +08:00
|
|
|
if (ret->client_sigalgs == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
memcpy(ret->client_sigalgs, cert->client_sigalgs,
|
2016-12-14 22:31:21 +08:00
|
|
|
cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->client_sigalgslen = cert->client_sigalgslen;
|
|
|
|
} else
|
|
|
|
ret->client_sigalgs = NULL;
|
|
|
|
/* Copy any custom client certificate types */
|
2017-02-24 06:12:28 +08:00
|
|
|
if (cert->ctype) {
|
|
|
|
ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
|
|
|
|
if (ret->ctype == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2017-02-24 06:12:28 +08:00
|
|
|
ret->ctype_len = cert->ctype_len;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret->cert_flags = cert->cert_flags;
|
|
|
|
|
|
|
|
ret->cert_cb = cert->cert_cb;
|
|
|
|
ret->cert_cb_arg = cert->cert_cb_arg;
|
|
|
|
|
|
|
|
if (cert->verify_store) {
|
2016-03-02 02:06:15 +08:00
|
|
|
X509_STORE_up_ref(cert->verify_store);
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->verify_store = cert->verify_store;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cert->chain_store) {
|
2016-03-02 02:06:15 +08:00
|
|
|
X509_STORE_up_ref(cert->chain_store);
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->chain_store = cert->chain_store;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->sec_cb = cert->sec_cb;
|
|
|
|
ret->sec_level = cert->sec_level;
|
|
|
|
ret->sec_ex = cert->sec_ex;
|
2013-12-15 21:32:24 +08:00
|
|
|
|
2017-04-05 18:59:23 +08:00
|
|
|
if (!custom_exts_copy(&ret->custext, &cert->custext))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-09-12 00:08:11 +08:00
|
|
|
#ifndef OPENSSL_NO_PSK
|
2015-09-15 00:58:04 +08:00
|
|
|
if (cert->psk_identity_hint) {
|
Rename some BUF_xxx to OPENSSL_xxx
Rename BUF_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
to OPENSSL_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
Add #define's for the old names.
Add CRYPTO_{memdup,strndup}, called by OPENSSL_{memdup,strndup} macros.
Reviewed-by: Tim Hudson <tjh@openssl.org>
2015-12-17 05:12:24 +08:00
|
|
|
ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
|
2015-09-15 00:58:04 +08:00
|
|
|
if (ret->psk_identity_hint == NULL)
|
|
|
|
goto err;
|
|
|
|
}
|
2015-09-12 00:08:11 +08:00
|
|
|
#endif
|
2016-03-01 01:26:07 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
ssl_cert_free(ret);
|
2012-01-27 22:21:38 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-05-10 04:12:44 +08:00
|
|
|
|
2012-06-18 20:56:59 +08:00
|
|
|
/* Free up and clear all certificates and chains */
|
|
|
|
|
|
|
|
void ssl_cert_clear_certs(CERT *c)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-09-26 23:32:05 +08:00
|
|
|
size_t i;
|
2021-08-10 04:56:50 +08:00
|
|
|
#ifndef OPENSSL_NO_COMP_ALG
|
|
|
|
int j;
|
|
|
|
#endif
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (c == NULL)
|
|
|
|
return;
|
2022-09-26 23:32:05 +08:00
|
|
|
for (i = 0; i < c->ssl_pkey_num; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
CERT_PKEY *cpk = c->pkeys + i;
|
2015-05-01 05:33:59 +08:00
|
|
|
X509_free(cpk->x509);
|
|
|
|
cpk->x509 = NULL;
|
2015-03-28 22:54:15 +08:00
|
|
|
EVP_PKEY_free(cpk->privatekey);
|
|
|
|
cpk->privatekey = NULL;
|
2021-12-18 23:15:49 +08:00
|
|
|
OSSL_STACK_OF_X509_free(cpk->chain);
|
2015-05-01 05:33:59 +08:00
|
|
|
cpk->chain = NULL;
|
2015-05-02 02:37:16 +08:00
|
|
|
OPENSSL_free(cpk->serverinfo);
|
|
|
|
cpk->serverinfo = NULL;
|
|
|
|
cpk->serverinfo_length = 0;
|
2021-08-10 04:56:50 +08:00
|
|
|
#ifndef OPENSSL_NO_COMP_ALG
|
|
|
|
for (j = 0; j < TLSEXT_comp_cert_limit; j++) {
|
|
|
|
OSSL_COMP_CERT_free(cpk->comp_cert[j]);
|
|
|
|
cpk->comp_cert[j] = NULL;
|
|
|
|
cpk->cert_comp_used = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
1999-05-10 04:12:44 +08:00
|
|
|
|
1999-03-01 01:41:55 +08:00
|
|
|
void ssl_cert_free(CERT *c)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2018-03-28 04:25:08 +08:00
|
|
|
if (c == NULL)
|
|
|
|
return;
|
2023-06-22 07:39:36 +08:00
|
|
|
CRYPTO_DOWN_REF(&c->references, &i);
|
2016-01-31 01:04:25 +08:00
|
|
|
REF_PRINT_COUNT("CERT", c);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (i > 0)
|
|
|
|
return;
|
2016-01-31 01:04:25 +08:00
|
|
|
REF_ASSERT_ISNT(i < 0);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-12-17 08:05:26 +08:00
|
|
|
EVP_PKEY_free(c->dh_tmp);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ssl_cert_clear_certs(c);
|
2015-05-02 02:37:16 +08:00
|
|
|
OPENSSL_free(c->conf_sigalgs);
|
|
|
|
OPENSSL_free(c->client_sigalgs);
|
2017-02-24 06:12:28 +08:00
|
|
|
OPENSSL_free(c->ctype);
|
2015-05-01 05:33:59 +08:00
|
|
|
X509_STORE_free(c->verify_store);
|
|
|
|
X509_STORE_free(c->chain_store);
|
2017-04-05 18:59:23 +08:00
|
|
|
custom_exts_free(&c->custext);
|
2015-09-15 00:58:04 +08:00
|
|
|
#ifndef OPENSSL_NO_PSK
|
|
|
|
OPENSSL_free(c->psk_identity_hint);
|
|
|
|
#endif
|
2022-09-26 23:32:05 +08:00
|
|
|
OPENSSL_free(c->pkeys);
|
2023-06-22 07:39:36 +08:00
|
|
|
CRYPTO_FREE_REF(&c->references);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(c);
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i, r;
|
2020-03-20 19:55:19 +08:00
|
|
|
CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!cpk)
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < sk_X509_num(chain); i++) {
|
2020-03-20 19:55:19 +08:00
|
|
|
X509 *x = sk_X509_value(chain, i);
|
|
|
|
|
|
|
|
r = ssl_security_cert(s, ctx, x, 0, 0);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (r != 1) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, r);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 23:15:49 +08:00
|
|
|
OSSL_STACK_OF_X509_free(cpk->chain);
|
2015-01-22 11:40:55 +08:00
|
|
|
cpk->chain = chain;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-01-31 22:00:10 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
STACK_OF(X509) *dchain;
|
2022-09-26 23:32:05 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!chain)
|
|
|
|
return ssl_cert_set0_chain(s, ctx, NULL);
|
|
|
|
dchain = X509_chain_up_ref(chain);
|
|
|
|
if (!dchain)
|
|
|
|
return 0;
|
|
|
|
if (!ssl_cert_set0_chain(s, ctx, dchain)) {
|
2021-12-18 23:15:49 +08:00
|
|
|
OSSL_STACK_OF_X509_free(dchain);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2012-01-31 22:00:10 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
|
2022-09-26 23:32:05 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!cpk)
|
|
|
|
return 0;
|
|
|
|
r = ssl_security_cert(s, ctx, x, 0, 0);
|
|
|
|
if (r != 1) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, r);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!cpk->chain)
|
|
|
|
cpk->chain = sk_X509_new_null();
|
|
|
|
if (!cpk->chain || !sk_X509_push(cpk->chain, x))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-01-31 22:00:10 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (!ssl_cert_add0_chain_cert(s, ctx, x))
|
|
|
|
return 0;
|
2015-09-01 03:29:57 +08:00
|
|
|
X509_up_ref(x);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2013-11-12 01:04:24 +08:00
|
|
|
|
|
|
|
int ssl_cert_select_current(CERT *c, X509 *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-09-26 23:32:05 +08:00
|
|
|
size_t i;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (x == NULL)
|
|
|
|
return 0;
|
2022-09-26 23:32:05 +08:00
|
|
|
for (i = 0; i < c->ssl_pkey_num; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
CERT_PKEY *cpk = c->pkeys + i;
|
|
|
|
if (cpk->x509 == x && cpk->privatekey) {
|
|
|
|
c->key = cpk;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
for (i = 0; i < c->ssl_pkey_num; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
CERT_PKEY *cpk = c->pkeys + i;
|
|
|
|
if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
|
|
|
|
c->key = cpk;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-02 10:51:30 +08:00
|
|
|
|
|
|
|
int ssl_cert_set_current(CERT *c, long op)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-09-26 23:32:05 +08:00
|
|
|
size_t i, idx;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!c)
|
|
|
|
return 0;
|
|
|
|
if (op == SSL_CERT_SET_FIRST)
|
|
|
|
idx = 0;
|
|
|
|
else if (op == SSL_CERT_SET_NEXT) {
|
2022-09-26 23:32:05 +08:00
|
|
|
idx = (size_t)(c->key - c->pkeys + 1);
|
|
|
|
if (idx >= c->ssl_pkey_num)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return 0;
|
2022-09-26 23:32:05 +08:00
|
|
|
for (i = idx; i < c->ssl_pkey_num; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
CERT_PKEY *cpk = c->pkeys + i;
|
|
|
|
if (cpk->x509 && cpk->privatekey) {
|
|
|
|
c->key = cpk;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
|
|
|
|
{
|
|
|
|
c->cert_cb = cb;
|
|
|
|
c->cert_cb_arg = arg;
|
|
|
|
}
|
2012-06-29 22:24:42 +08:00
|
|
|
|
2021-12-03 23:56:58 +08:00
|
|
|
/*
|
2021-01-28 03:23:33 +08:00
|
|
|
* Verify a certificate chain/raw public key
|
2021-12-03 23:56:58 +08:00
|
|
|
* Return codes:
|
|
|
|
* 1: Verify success
|
|
|
|
* 0: Verify failure or error
|
|
|
|
* -1: Retry required
|
|
|
|
*/
|
2021-01-28 03:23:33 +08:00
|
|
|
static int ssl_verify_internal(SSL_CONNECTION *s, STACK_OF(X509) *sk, EVP_PKEY *rpk)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
X509 *x;
|
2016-04-15 11:59:26 +08:00
|
|
|
int i = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_STORE *verify_store;
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX *ctx = NULL;
|
2015-12-30 02:28:28 +08:00
|
|
|
X509_VERIFY_PARAM *param;
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CTX *sctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-01-28 03:23:33 +08:00
|
|
|
/* Something must be passed in */
|
|
|
|
if ((sk == NULL || sk_X509_num(sk) == 0) && rpk == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Only one can be set */
|
|
|
|
if (sk != NULL && rpk != NULL)
|
2016-04-15 11:59:26 +08:00
|
|
|
return 0;
|
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
sctx = SSL_CONNECTION_GET_CTX(s);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (s->cert->verify_store)
|
|
|
|
verify_store = s->cert->verify_store;
|
|
|
|
else
|
2022-06-20 23:11:28 +08:00
|
|
|
verify_store = sctx->cert_store;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
ctx = X509_STORE_CTX_new_ex(sctx->libctx, sctx->propq);
|
2016-04-15 11:59:26 +08:00
|
|
|
if (ctx == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
2016-04-15 11:59:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-01-28 03:23:33 +08:00
|
|
|
if (sk != NULL) {
|
|
|
|
x = sk_X509_value(sk, 0);
|
|
|
|
if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
|
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!X509_STORE_CTX_init_rpk(ctx, verify_store, rpk)) {
|
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
|
|
|
goto end;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-04-15 11:59:26 +08:00
|
|
|
param = X509_STORE_CTX_get0_param(ctx);
|
2016-03-19 10:09:41 +08:00
|
|
|
/*
|
|
|
|
* XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
|
|
|
|
* point, for now a single @SECLEVEL sets the same policy for TLS crypto
|
|
|
|
* and PKI authentication.
|
|
|
|
*/
|
2022-06-20 23:11:28 +08:00
|
|
|
X509_VERIFY_PARAM_set_auth_level(param,
|
|
|
|
SSL_get_security_level(SSL_CONNECTION_GET_SSL(s)));
|
2015-12-30 02:28:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Set suite B flags if needed */
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
|
2022-06-20 23:11:28 +08:00
|
|
|
if (!X509_STORE_CTX_set_ex_data(ctx,
|
|
|
|
SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
|
2016-02-14 02:01:14 +08:00
|
|
|
goto end;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-12-30 02:28:28 +08:00
|
|
|
/* Verify via DANE if enabled */
|
|
|
|
if (DANETLS_ENABLED(&s->dane))
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_set0_dane(ctx, &s->dane);
|
2015-12-30 02:28:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* We need to inherit the verify parameters. These can be determined by
|
|
|
|
* the context: if its a server it will verify SSL client certificates or
|
|
|
|
* vice versa.
|
|
|
|
*/
|
|
|
|
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2015-12-30 02:28:28 +08:00
|
|
|
* Anything non-default in "s->param" should overwrite anything in the ctx.
|
2015-01-22 11:40:55 +08:00
|
|
|
*/
|
2015-12-30 02:28:28 +08:00
|
|
|
X509_VERIFY_PARAM_set1(param, s->param);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (s->verify_callback)
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
if (sctx->app_verify_callback != NULL) {
|
|
|
|
i = sctx->app_verify_callback(ctx, sctx->app_verify_arg);
|
2021-12-03 23:56:58 +08:00
|
|
|
} else {
|
2016-04-15 11:59:26 +08:00
|
|
|
i = X509_verify_cert(ctx);
|
2021-12-03 23:56:58 +08:00
|
|
|
/* We treat an error in the same way as a failure to verify */
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-04-15 11:59:26 +08:00
|
|
|
s->verify_result = X509_STORE_CTX_get_error(ctx);
|
2021-12-18 23:15:49 +08:00
|
|
|
OSSL_STACK_OF_X509_free(s->verified_chain);
|
2016-02-06 11:17:23 +08:00
|
|
|
s->verified_chain = NULL;
|
2021-01-28 03:23:33 +08:00
|
|
|
|
|
|
|
if (sk != NULL && X509_STORE_CTX_get0_chain(ctx) != NULL) {
|
2016-04-15 11:59:26 +08:00
|
|
|
s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
|
2016-02-06 11:17:23 +08:00
|
|
|
if (s->verified_chain == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
2016-02-06 11:17:23 +08:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-12-30 02:28:28 +08:00
|
|
|
|
|
|
|
/* Move peername from the store context params to the SSL handle's */
|
|
|
|
X509_VERIFY_PARAM_move_peername(s->param, param);
|
|
|
|
|
2016-08-06 01:03:17 +08:00
|
|
|
end:
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_free(ctx);
|
|
|
|
return i;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2021-01-28 03:23:33 +08:00
|
|
|
/*
|
|
|
|
* Verify a raw public key
|
|
|
|
* Return codes:
|
|
|
|
* 1: Verify success
|
|
|
|
* 0: Verify failure or error
|
|
|
|
* -1: Retry required
|
|
|
|
*/
|
|
|
|
int ssl_verify_rpk(SSL_CONNECTION *s, EVP_PKEY *rpk)
|
|
|
|
{
|
|
|
|
return ssl_verify_internal(s, NULL, rpk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify a certificate chain
|
|
|
|
* Return codes:
|
|
|
|
* 1: Verify success
|
|
|
|
* 0: Verify failure or error
|
|
|
|
* -1: Retry required
|
|
|
|
*/
|
|
|
|
int ssl_verify_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk)
|
|
|
|
{
|
|
|
|
return ssl_verify_internal(s, sk, NULL);
|
|
|
|
}
|
|
|
|
|
2017-03-13 23:00:36 +08:00
|
|
|
static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
|
|
|
|
STACK_OF(X509_NAME) *name_list)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-05-01 05:33:59 +08:00
|
|
|
sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
|
2015-01-22 11:40:55 +08:00
|
|
|
*ca_list = name_list;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2017-04-01 05:30:58 +08:00
|
|
|
STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
2017-09-19 06:48:14 +08:00
|
|
|
const int num = sk_X509_NAME_num(sk);
|
2015-01-22 11:40:55 +08:00
|
|
|
STACK_OF(X509_NAME) *ret;
|
|
|
|
X509_NAME *name;
|
|
|
|
|
2017-10-27 01:56:14 +08:00
|
|
|
ret = sk_X509_NAME_new_reserve(NULL, num);
|
2016-06-04 06:15:19 +08:00
|
|
|
if (ret == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
|
2016-06-04 06:15:19 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-19 06:48:14 +08:00
|
|
|
for (i = 0; i < num; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
|
2017-09-19 06:48:14 +08:00
|
|
|
if (name == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
sk_X509_NAME_pop_free(ret, X509_NAME_free);
|
2016-06-04 06:15:19 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-09-19 06:48:14 +08:00
|
|
|
sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-10-09 02:10:13 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2017-03-13 23:00:36 +08:00
|
|
|
void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
|
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
set0_CA_list(&sc->ca_names, name_list);
|
2017-03-13 23:00:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-03-13 23:00:36 +08:00
|
|
|
set0_CA_list(&ctx->ca_names, name_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
|
|
|
|
{
|
|
|
|
return ctx->ca_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
|
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return sc->ca_names != NULL ? sc->ca_names : s->ctx->ca_names;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
|
|
|
|
{
|
2018-10-26 18:43:19 +08:00
|
|
|
set0_CA_list(&ctx->client_ca_names, name_list);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2005-03-30 18:26:02 +08:00
|
|
|
STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2018-10-26 18:43:19 +08:00
|
|
|
return ctx->client_ca_names;
|
2017-03-13 23:00:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
|
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
set0_CA_list(&sc->client_ca_names, name_list);
|
2017-03-13 23:00:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
|
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return sc->s3.tmp.peer_ca_names;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2005-03-30 18:26:02 +08:00
|
|
|
STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!sc->server)
|
|
|
|
return sc->s3.tmp.peer_ca_names;
|
|
|
|
return sc->client_ca_names != NULL ? sc->client_ca_names
|
|
|
|
: s->ctx->client_ca_names;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2017-03-13 23:00:36 +08:00
|
|
|
static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
X509_NAME *name;
|
|
|
|
|
|
|
|
if (x == NULL)
|
2017-03-13 23:00:36 +08:00
|
|
|
return 0;
|
|
|
|
if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
|
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
|
2017-03-13 23:00:36 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (!sk_X509_NAME_push(*sk, name)) {
|
|
|
|
X509_NAME_free(name);
|
2017-03-13 23:00:36 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-03-13 23:00:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-13 13:11:47 +08:00
|
|
|
int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
|
2017-03-13 23:00:36 +08:00
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return add_ca_name(&sc->ca_names, x);
|
2017-03-13 23:00:36 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:11:47 +08:00
|
|
|
int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
|
2017-03-13 23:00:36 +08:00
|
|
|
{
|
|
|
|
return add_ca_name(&ctx->ca_names, x);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:11:47 +08:00
|
|
|
/*
|
|
|
|
* The following two are older names are to be replaced with
|
|
|
|
* SSL(_CTX)_add1_to_CA_list
|
|
|
|
*/
|
2015-01-22 11:40:55 +08:00
|
|
|
int SSL_add_client_CA(SSL *ssl, X509 *x)
|
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return add_ca_name(&sc->client_ca_names, x);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
|
|
|
|
{
|
2018-10-26 18:43:19 +08:00
|
|
|
return add_ca_name(&ctx->client_ca_names, x);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 20:09:13 +08:00
|
|
|
static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-12-11 20:09:13 +08:00
|
|
|
unsigned char *abuf = NULL, *bbuf = NULL;
|
|
|
|
int alen, blen, ret;
|
|
|
|
|
|
|
|
/* X509_NAME_cmp() itself casts away constness in this way, so
|
|
|
|
* assume it's safe:
|
|
|
|
*/
|
|
|
|
alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
|
|
|
|
blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
|
|
|
|
|
|
|
|
if (alen < 0 || blen < 0)
|
|
|
|
ret = -2;
|
|
|
|
else if (alen != blen)
|
|
|
|
ret = alen - blen;
|
|
|
|
else /* alen == blen */
|
|
|
|
ret = memcmp(abuf, bbuf, alen);
|
|
|
|
|
|
|
|
OPENSSL_free(abuf);
|
|
|
|
OPENSSL_free(bbuf);
|
|
|
|
|
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2017-12-11 20:09:13 +08:00
|
|
|
static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
|
2016-02-04 02:08:45 +08:00
|
|
|
{
|
2017-12-11 20:09:13 +08:00
|
|
|
return xname_cmp(*a, *b);
|
2016-02-04 02:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long xname_hash(const X509_NAME *a)
|
|
|
|
{
|
2020-12-28 18:27:31 +08:00
|
|
|
/* This returns 0 also if SHA1 is not available */
|
|
|
|
return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);
|
2016-02-04 02:08:45 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx,
|
2020-09-24 17:42:23 +08:00
|
|
|
const char *propq)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-02-04 02:08:45 +08:00
|
|
|
BIO *in = BIO_new(BIO_s_file());
|
2015-01-22 11:40:55 +08:00
|
|
|
X509 *x = NULL;
|
|
|
|
X509_NAME *xn = NULL;
|
2016-02-04 02:08:45 +08:00
|
|
|
STACK_OF(X509_NAME) *ret = NULL;
|
2016-08-06 01:03:17 +08:00
|
|
|
LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *prev_libctx = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2022-09-29 19:57:34 +08:00
|
|
|
if (name_hash == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (in == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
x = X509_new_ex(libctx, propq);
|
2020-07-24 20:53:27 +08:00
|
|
|
if (x == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
2020-07-24 20:53:27 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2021-11-15 00:41:21 +08:00
|
|
|
if (BIO_read_filename(in, file) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2020-07-24 20:53:27 +08:00
|
|
|
/* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
|
2020-10-15 17:55:50 +08:00
|
|
|
prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
for (;;) {
|
|
|
|
if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
|
|
|
|
break;
|
|
|
|
if (ret == NULL) {
|
|
|
|
ret = sk_X509_NAME_new_null();
|
|
|
|
if (ret == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((xn = X509_get_subject_name(x)) == NULL)
|
|
|
|
goto err;
|
|
|
|
/* check for duplicates */
|
|
|
|
xn = X509_NAME_dup(xn);
|
|
|
|
if (xn == NULL)
|
|
|
|
goto err;
|
2016-02-04 02:08:45 +08:00
|
|
|
if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
|
|
|
|
/* Duplicate. */
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_NAME_free(xn);
|
2016-06-04 06:15:19 +08:00
|
|
|
xn = NULL;
|
2016-02-04 02:08:45 +08:00
|
|
|
} else {
|
2016-07-02 07:19:39 +08:00
|
|
|
lh_X509_NAME_insert(name_hash, xn);
|
2016-06-04 06:15:19 +08:00
|
|
|
if (!sk_X509_NAME_push(ret, xn))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-02 02:29:48 +08:00
|
|
|
goto done;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
err:
|
2016-06-04 06:15:19 +08:00
|
|
|
X509_NAME_free(xn);
|
2015-05-02 02:29:48 +08:00
|
|
|
sk_X509_NAME_pop_free(ret, X509_NAME_free);
|
|
|
|
ret = NULL;
|
|
|
|
done:
|
2020-07-24 20:53:27 +08:00
|
|
|
/* restore the old libctx */
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX_set0_default(prev_libctx);
|
2015-03-25 23:31:18 +08:00
|
|
|
BIO_free(in);
|
2015-05-01 05:33:59 +08:00
|
|
|
X509_free(x);
|
2016-02-04 02:08:45 +08:00
|
|
|
lh_X509_NAME_free(name_hash);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ret != NULL)
|
|
|
|
ERR_clear_error();
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-07-24 20:53:27 +08:00
|
|
|
STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return SSL_load_client_CA_file_ex(file, NULL, NULL);
|
2020-07-24 20:53:27 +08:00
|
|
|
}
|
|
|
|
|
1999-05-04 03:55:00 +08:00
|
|
|
int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
|
2015-01-22 11:40:55 +08:00
|
|
|
const char *file)
|
|
|
|
{
|
|
|
|
BIO *in;
|
|
|
|
X509 *x = NULL;
|
|
|
|
X509_NAME *xn = NULL;
|
|
|
|
int ret = 1;
|
|
|
|
int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
|
|
|
|
|
2016-02-04 02:08:45 +08:00
|
|
|
oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-10-01 02:32:49 +08:00
|
|
|
in = BIO_new(BIO_s_file());
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (in == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-11-15 00:41:21 +08:00
|
|
|
if (BIO_read_filename(in, file) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
|
|
|
|
break;
|
|
|
|
if ((xn = X509_get_subject_name(x)) == NULL)
|
|
|
|
goto err;
|
|
|
|
xn = X509_NAME_dup(xn);
|
|
|
|
if (xn == NULL)
|
|
|
|
goto err;
|
2016-06-04 06:15:19 +08:00
|
|
|
if (sk_X509_NAME_find(stack, xn) >= 0) {
|
|
|
|
/* Duplicate. */
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_NAME_free(xn);
|
2016-06-04 06:15:19 +08:00
|
|
|
} else if (!sk_X509_NAME_push(stack, xn)) {
|
|
|
|
X509_NAME_free(xn);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_clear_error();
|
2015-05-02 02:29:48 +08:00
|
|
|
goto done;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
err:
|
2016-06-04 06:15:19 +08:00
|
|
|
ret = 0;
|
2015-05-02 02:29:48 +08:00
|
|
|
done:
|
2015-03-25 23:31:18 +08:00
|
|
|
BIO_free(in);
|
2015-05-02 02:37:16 +08:00
|
|
|
X509_free(x);
|
2015-01-22 11:40:55 +08:00
|
|
|
(void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
1999-05-04 03:55:00 +08:00
|
|
|
int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
|
2015-01-22 11:40:55 +08:00
|
|
|
const char *dir)
|
|
|
|
{
|
|
|
|
OPENSSL_DIR_CTX *d = NULL;
|
|
|
|
const char *filename;
|
|
|
|
int ret = 0;
|
1999-03-01 01:41:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Note that a side effect is that the CAs will be sorted by name */
|
2004-07-10 21:17:16 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
while ((filename = OPENSSL_DIR_read(&d, dir))) {
|
|
|
|
char buf[1024];
|
|
|
|
int r;
|
2023-04-20 17:41:46 +08:00
|
|
|
#ifndef OPENSSL_NO_POSIX_IO
|
2023-02-21 21:20:24 +08:00
|
|
|
struct stat st;
|
2004-07-10 21:17:16 +08:00
|
|
|
|
2023-04-20 17:41:46 +08:00
|
|
|
#else
|
|
|
|
/* Cannot use stat so just skip current and parent directories */
|
|
|
|
if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
|
|
|
|
continue;
|
|
|
|
#endif
|
2017-12-08 02:39:34 +08:00
|
|
|
if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2004-07-10 21:17:16 +08:00
|
|
|
#ifdef OPENSSL_SYS_VMS
|
2017-12-08 02:39:34 +08:00
|
|
|
r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
|
2004-07-10 21:17:16 +08:00
|
|
|
#else
|
2017-12-08 02:39:34 +08:00
|
|
|
r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
|
2004-07-10 21:17:16 +08:00
|
|
|
#endif
|
2023-04-20 17:41:46 +08:00
|
|
|
#ifndef OPENSSL_NO_POSIX_IO
|
2023-02-21 21:20:24 +08:00
|
|
|
/* Skip subdirectories */
|
|
|
|
if (!stat(buf, &st) && S_ISDIR(st.st_mode))
|
|
|
|
continue;
|
2023-04-20 17:41:46 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
if (r <= 0 || r >= (int)sizeof(buf))
|
|
|
|
goto err;
|
|
|
|
if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno) {
|
2019-08-01 03:24:20 +08:00
|
|
|
ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
|
2020-11-04 19:18:33 +08:00
|
|
|
"calling OPENSSL_dir_read(%s)", dir);
|
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (d)
|
|
|
|
OPENSSL_DIR_end(&d);
|
2016-03-01 01:26:07 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2001-10-04 20:27:39 +08:00
|
|
|
|
2019-03-07 06:34:19 +08:00
|
|
|
static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
|
|
|
|
const char *uri, int depth)
|
|
|
|
{
|
|
|
|
int ok = 1;
|
|
|
|
OSSL_STORE_CTX *ctx = NULL;
|
|
|
|
X509 *x = NULL;
|
|
|
|
X509_NAME *xn = NULL;
|
|
|
|
|
|
|
|
if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
|
|
|
|
OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
|
|
|
|
int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
|
|
|
|
|
|
|
|
if (info == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (infotype == OSSL_STORE_INFO_NAME) {
|
|
|
|
/*
|
|
|
|
* This is an entry in the "directory" represented by the current
|
|
|
|
* uri. if |depth| allows, dive into it.
|
|
|
|
*/
|
2019-11-10 22:36:24 +08:00
|
|
|
if (depth > 0)
|
|
|
|
ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
|
|
|
|
depth - 1);
|
2019-03-07 06:34:19 +08:00
|
|
|
} else if (infotype == OSSL_STORE_INFO_CERT) {
|
|
|
|
if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
|
|
|
|
|| (xn = X509_get_subject_name(x)) == NULL
|
|
|
|
|| (xn = X509_NAME_dup(xn)) == NULL)
|
|
|
|
goto err;
|
|
|
|
if (sk_X509_NAME_find(stack, xn) >= 0) {
|
|
|
|
/* Duplicate. */
|
|
|
|
X509_NAME_free(xn);
|
|
|
|
} else if (!sk_X509_NAME_push(stack, xn)) {
|
|
|
|
X509_NAME_free(xn);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OSSL_STORE_INFO_free(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_clear_error();
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ok = 0;
|
|
|
|
done:
|
|
|
|
OSSL_STORE_close(ctx);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
|
|
|
|
const char *store)
|
|
|
|
{
|
|
|
|
int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
|
|
|
|
= sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
|
|
|
|
int ret = add_uris_recursive(stack, store, 1);
|
|
|
|
|
|
|
|
(void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-24 07:34:28 +08:00
|
|
|
/* Build a certificate chain for current certificate */
|
2022-06-20 23:11:28 +08:00
|
|
|
int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
CERT *c = s != NULL ? s->cert : ctx->cert;
|
2015-01-22 11:40:55 +08:00
|
|
|
CERT_PKEY *cpk = c->key;
|
|
|
|
X509_STORE *chain_store = NULL;
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX *xs_ctx = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
STACK_OF(X509) *chain = NULL, *untrusted = NULL;
|
|
|
|
X509 *x;
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CTX *real_ctx = (s == NULL) ? ctx : SSL_CONNECTION_GET_CTX(s);
|
2015-01-22 11:40:55 +08:00
|
|
|
int i, rv = 0;
|
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
if (cpk->x509 == NULL) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* Rearranging and check the chain: add everything to a store */
|
|
|
|
if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
|
|
|
|
chain_store = X509_STORE_new();
|
2015-10-30 18:05:53 +08:00
|
|
|
if (chain_store == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
for (i = 0; i < sk_X509_num(cpk->chain); i++) {
|
|
|
|
x = sk_X509_value(cpk->chain, i);
|
2017-04-21 03:33:42 +08:00
|
|
|
if (!X509_STORE_add_cert(chain_store, x))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2017-04-21 03:33:42 +08:00
|
|
|
/* Add EE cert too: it might be self signed */
|
|
|
|
if (!X509_STORE_add_cert(chain_store, cpk->x509))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
} else {
|
2022-06-20 23:11:28 +08:00
|
|
|
if (c->chain_store != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
chain_store = c->chain_store;
|
|
|
|
else
|
2022-06-20 23:11:28 +08:00
|
|
|
chain_store = real_ctx->cert_store;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
|
|
|
|
untrusted = cpk->chain;
|
|
|
|
}
|
|
|
|
|
2021-02-24 13:59:14 +08:00
|
|
|
xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);
|
2016-04-15 11:59:26 +08:00
|
|
|
if (xs_ctx == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
2016-04-15 11:59:26 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* Set suite B flags if needed */
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_set_flags(xs_ctx,
|
2015-01-22 11:40:55 +08:00
|
|
|
c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
|
|
|
|
|
2016-04-15 11:59:26 +08:00
|
|
|
i = X509_verify_cert(xs_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
|
|
|
|
if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
|
|
|
|
ERR_clear_error();
|
|
|
|
i = 1;
|
|
|
|
rv = 2;
|
|
|
|
}
|
|
|
|
if (i > 0)
|
2016-04-15 11:59:26 +08:00
|
|
|
chain = X509_STORE_CTX_get1_chain(xs_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (i <= 0) {
|
2016-04-15 11:59:26 +08:00
|
|
|
i = X509_STORE_CTX_get_error(xs_ctx);
|
2020-11-04 21:39:57 +08:00
|
|
|
ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
|
|
|
|
"Verify error:%s", X509_verify_cert_error_string(i));
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* Remove EE certificate from chain */
|
|
|
|
x = sk_X509_shift(chain);
|
|
|
|
X509_free(x);
|
|
|
|
if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
|
|
|
|
if (sk_X509_num(chain) > 0) {
|
|
|
|
/* See if last cert is self signed */
|
|
|
|
x = sk_X509_value(chain, sk_X509_num(chain) - 1);
|
2015-09-03 05:01:18 +08:00
|
|
|
if (X509_get_extension_flags(x) & EXFLAG_SS) {
|
2015-01-22 11:40:55 +08:00
|
|
|
x = sk_X509_pop(chain);
|
|
|
|
X509_free(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check security level of all CA certificates: EE will have been checked
|
|
|
|
* already.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < sk_X509_num(chain); i++) {
|
|
|
|
x = sk_X509_value(chain, i);
|
|
|
|
rv = ssl_security_cert(s, ctx, x, 0, 0);
|
|
|
|
if (rv != 1) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, rv);
|
2021-12-18 23:15:49 +08:00
|
|
|
OSSL_STACK_OF_X509_free(chain);
|
2015-01-22 11:40:55 +08:00
|
|
|
rv = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 23:15:49 +08:00
|
|
|
OSSL_STACK_OF_X509_free(cpk->chain);
|
2015-01-22 11:40:55 +08:00
|
|
|
cpk->chain = chain;
|
|
|
|
if (rv == 0)
|
|
|
|
rv = 1;
|
|
|
|
err:
|
|
|
|
if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
|
|
|
|
X509_STORE_free(chain_store);
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_free(xs_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2012-07-24 07:34:28 +08:00
|
|
|
|
|
|
|
int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
X509_STORE **pstore;
|
|
|
|
if (chain)
|
|
|
|
pstore = &c->chain_store;
|
|
|
|
else
|
|
|
|
pstore = &c->verify_store;
|
2015-05-01 05:33:59 +08:00
|
|
|
X509_STORE_free(*pstore);
|
2015-01-22 11:40:55 +08:00
|
|
|
*pstore = store;
|
|
|
|
if (ref && store)
|
2016-03-02 02:06:15 +08:00
|
|
|
X509_STORE_up_ref(store);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:36:20 +08:00
|
|
|
int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain)
|
|
|
|
{
|
|
|
|
*pstore = (chain ? c->chain_store : c->verify_store);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-18 10:54:24 +08:00
|
|
|
int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)
|
|
|
|
{
|
|
|
|
int level;
|
2021-06-21 22:52:14 +08:00
|
|
|
/*
|
|
|
|
* note that there's a corresponding minbits_table
|
|
|
|
* in crypto/x509/x509_vfy.c that's used for checking the security level
|
|
|
|
* of RSA and DSA keys
|
|
|
|
*/
|
2021-06-18 10:54:24 +08:00
|
|
|
static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 };
|
|
|
|
|
|
|
|
if (ctx != NULL)
|
|
|
|
level = SSL_CTX_get_security_level(ctx);
|
|
|
|
else
|
|
|
|
level = SSL_get_security_level(s);
|
|
|
|
|
|
|
|
if (level > 5)
|
|
|
|
level = 5;
|
|
|
|
else if (level < 0)
|
|
|
|
level = 0;
|
|
|
|
|
|
|
|
if (levelp != NULL)
|
|
|
|
*levelp = level;
|
|
|
|
|
|
|
|
return minbits_table[level];
|
|
|
|
}
|
|
|
|
|
2016-08-06 01:03:17 +08:00
|
|
|
static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
|
|
|
|
int op, int bits, int nid, void *other,
|
2015-01-22 11:40:55 +08:00
|
|
|
void *ex)
|
|
|
|
{
|
2022-02-22 22:26:26 +08:00
|
|
|
int level, minbits, pfs_mask;
|
2022-06-20 23:11:28 +08:00
|
|
|
const SSL_CONNECTION *sc;
|
2016-01-14 14:16:16 +08:00
|
|
|
|
2021-06-18 10:54:24 +08:00
|
|
|
minbits = ssl_get_security_level_bits(s, ctx, &level);
|
|
|
|
|
|
|
|
if (level == 0) {
|
2016-01-14 14:16:16 +08:00
|
|
|
/*
|
|
|
|
* No EDH keys weaker than 1024-bits even at level 0, otherwise,
|
|
|
|
* anything goes.
|
|
|
|
*/
|
|
|
|
if (op == SSL_SECOP_TMP_DH && bits < 80)
|
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
2016-01-14 14:16:16 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
switch (op) {
|
|
|
|
case SSL_SECOP_CIPHER_SUPPORTED:
|
|
|
|
case SSL_SECOP_CIPHER_SHARED:
|
|
|
|
case SSL_SECOP_CIPHER_CHECK:
|
|
|
|
{
|
|
|
|
const SSL_CIPHER *c = other;
|
|
|
|
/* No ciphers below security level */
|
|
|
|
if (bits < minbits)
|
|
|
|
return 0;
|
|
|
|
/* No unauthenticated ciphersuites */
|
|
|
|
if (c->algorithm_auth & SSL_aNULL)
|
|
|
|
return 0;
|
|
|
|
/* No MD5 mac ciphersuites */
|
|
|
|
if (c->algorithm_mac & SSL_MD5)
|
|
|
|
return 0;
|
|
|
|
/* SHA1 HMAC is 160 bits of security */
|
|
|
|
if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
|
|
|
|
return 0;
|
|
|
|
/* Level 3: forward secure ciphersuites only */
|
2022-02-22 22:26:26 +08:00
|
|
|
pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK;
|
2018-10-12 23:24:14 +08:00
|
|
|
if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
|
2022-02-22 22:26:26 +08:00
|
|
|
!(c->algorithm_mkey & pfs_mask))
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SSL_SECOP_VERSION:
|
2022-06-20 23:11:28 +08:00
|
|
|
if ((sc = SSL_CONNECTION_FROM_CONST_SSL(s)) == NULL)
|
|
|
|
return 0;
|
|
|
|
if (!SSL_CONNECTION_IS_DTLS(sc)) {
|
2022-05-04 11:01:35 +08:00
|
|
|
/* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
|
|
|
|
if (nid <= TLS1_1_VERSION && level > 0)
|
2015-12-29 16:24:17 +08:00
|
|
|
return 0;
|
|
|
|
} else {
|
2022-05-04 11:01:35 +08:00
|
|
|
/* DTLS v1.0 only allowed at level 0 */
|
|
|
|
if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level > 0)
|
2015-12-29 16:24:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSL_SECOP_COMPRESSION:
|
|
|
|
if (level >= 2)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case SSL_SECOP_TICKET:
|
|
|
|
if (level >= 3)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (bits < minbits)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2013-12-15 21:32:24 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid, void *other)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-06-20 23:11:28 +08:00
|
|
|
return s->cert->sec_cb(SSL_CONNECTION_GET_SSL(s), NULL, op, bits, nid,
|
|
|
|
other, s->cert->sec_ex);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2013-12-15 21:32:24 +08:00
|
|
|
|
2016-02-08 03:44:27 +08:00
|
|
|
int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
|
|
|
|
ctx->cert->sec_ex);
|
|
|
|
}
|
2017-06-29 00:45:10 +08:00
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx)
|
2017-06-29 00:45:10 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
|
|
|
|
if (ssl_cert_info[i].nid == nid) {
|
2018-07-17 23:31:07 +08:00
|
|
|
*pidx = i;
|
|
|
|
return 1;
|
2017-06-29 00:45:10 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 23:32:05 +08:00
|
|
|
for (i = 0; i < ctx->sigalg_list_len; i++) {
|
|
|
|
if (ctx->ssl_cert_info[i].nid == nid) {
|
|
|
|
*pidx = SSL_PKEY_NUM + i;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 23:31:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:51:33 +08:00
|
|
|
const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx, SSL_CTX *ctx)
|
2018-07-17 23:31:07 +08:00
|
|
|
{
|
2020-05-14 20:04:41 +08:00
|
|
|
size_t i;
|
2018-07-17 23:31:07 +08:00
|
|
|
|
2022-09-26 23:32:05 +08:00
|
|
|
/* check classic pk types */
|
2020-05-14 20:04:41 +08:00
|
|
|
for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
|
2023-11-27 15:51:33 +08:00
|
|
|
const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
|
2018-07-17 23:31:07 +08:00
|
|
|
|
2020-05-14 20:04:41 +08:00
|
|
|
if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
|
|
|
|
|| EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
|
|
|
|
if (pidx != NULL)
|
|
|
|
*pidx = i;
|
|
|
|
return tmp_lu;
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 23:32:05 +08:00
|
|
|
/* check provider-loaded pk types */
|
|
|
|
for (i = 0; ctx->sigalg_list_len; i++) {
|
|
|
|
SSL_CERT_LOOKUP *tmp_lu = &(ctx->ssl_cert_info[i]);
|
|
|
|
|
|
|
|
if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
|
|
|
|
|| EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
|
|
|
|
if (pidx != NULL)
|
|
|
|
*pidx = SSL_PKEY_NUM + i;
|
|
|
|
return &ctx->ssl_cert_info[i];
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 23:31:07 +08:00
|
|
|
|
2020-05-14 20:04:41 +08:00
|
|
|
return NULL;
|
2017-06-29 00:45:10 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:51:33 +08:00
|
|
|
const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx)
|
2017-06-29 00:45:10 +08:00
|
|
|
{
|
2022-09-26 23:32:05 +08:00
|
|
|
if (idx >= (OSSL_NELEM(ssl_cert_info) + ctx->sigalg_list_len))
|
2017-10-21 10:58:46 +08:00
|
|
|
return NULL;
|
2022-09-26 23:32:05 +08:00
|
|
|
else if (idx >= (OSSL_NELEM(ssl_cert_info)))
|
|
|
|
return &(ctx->ssl_cert_info[idx - SSL_PKEY_NUM]);
|
2017-06-29 00:45:10 +08:00
|
|
|
return &ssl_cert_info[idx];
|
|
|
|
}
|