Latest update.
This commit is contained in:
@@ -300,6 +300,12 @@ int ssl3_read_n(SSL *s, size_t n, size_t max, int extend, int clearold,
|
||||
ret = BIO_read(s->rbio, pkt + len + left, max - left);
|
||||
if (ret >= 0)
|
||||
bioread = ret;
|
||||
if (ret <= 0
|
||||
&& !BIO_should_retry(s->rbio)
|
||||
&& BIO_eof(s->rbio)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_READ_N,
|
||||
SSL_R_UNEXPECTED_EOF_WHILE_READING);
|
||||
}
|
||||
} else {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_N,
|
||||
SSL_R_READ_BIO_NOT_SET);
|
||||
|
||||
@@ -1015,7 +1015,8 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
} else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
|
||||
} else if (RAND_bytes_ex(s->ctx->libctx, recs[ctr].input,
|
||||
ivlen) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
|
||||
+25
-5
@@ -4062,12 +4062,14 @@ long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
|
||||
ctx->ext.status_cb = (int (*)(SSL *, void *))fp;
|
||||
break;
|
||||
|
||||
# ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
case SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB:
|
||||
ctx->ext.ticket_key_cb = (int (*)(SSL *, unsigned char *,
|
||||
unsigned char *,
|
||||
EVP_CIPHER_CTX *,
|
||||
HMAC_CTX *, int))fp;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
case SSL_CTRL_SET_SRP_VERIFY_PARAM_CB:
|
||||
@@ -4096,6 +4098,14 @@ long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_tlsext_ticket_key_evp_cb
|
||||
(SSL_CTX *ctx, int (*fp)(SSL *, unsigned char *, unsigned char *,
|
||||
EVP_CIPHER_CTX *, EVP_MAC_CTX *, int))
|
||||
{
|
||||
ctx->ext.ticket_key_evp_cb = fp;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const SSL_CIPHER *ssl3_get_cipher_by_id(uint32_t id)
|
||||
{
|
||||
SSL_CIPHER c;
|
||||
@@ -4630,9 +4640,9 @@ int ssl_fill_hello_random(SSL *s, int server, unsigned char *result, size_t len,
|
||||
unsigned char *p = result;
|
||||
|
||||
l2n(Time, p);
|
||||
ret = RAND_bytes(p, len - 4);
|
||||
ret = RAND_bytes_ex(s->ctx->libctx, p, len - 4);
|
||||
} else {
|
||||
ret = RAND_bytes(result, len);
|
||||
ret = RAND_bytes_ex(s->ctx->libctx, result, len);
|
||||
}
|
||||
|
||||
if (ret > 0) {
|
||||
@@ -4719,14 +4729,14 @@ int ssl_generate_master_secret(SSL *s, unsigned char *pms, size_t pmslen,
|
||||
}
|
||||
|
||||
/* Generate a private key from parameters */
|
||||
EVP_PKEY *ssl_generate_pkey(EVP_PKEY *pm)
|
||||
EVP_PKEY *ssl_generate_pkey(SSL *s, EVP_PKEY *pm)
|
||||
{
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
EVP_PKEY *pkey = NULL;
|
||||
|
||||
if (pm == NULL)
|
||||
return NULL;
|
||||
pctx = EVP_PKEY_CTX_new(pm, NULL);
|
||||
pctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pm, s->ctx->propq);
|
||||
if (pctx == NULL)
|
||||
goto err;
|
||||
if (EVP_PKEY_keygen_init(pctx) <= 0)
|
||||
@@ -4759,6 +4769,11 @@ EVP_PKEY *ssl_generate_pkey_group(SSL *s, uint16_t id)
|
||||
goto err;
|
||||
}
|
||||
gtype = ginf->flags & TLS_GROUP_TYPE;
|
||||
/*
|
||||
* TODO(3.0): Convert these EVP_PKEY_CTX_new_id calls to ones that take
|
||||
* s->ctx->libctx and s->ctx->propq when keygen has been updated to be
|
||||
* provider aware.
|
||||
*/
|
||||
# ifndef OPENSSL_NO_DH
|
||||
if (gtype == TLS_GROUP_FFDHE)
|
||||
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
|
||||
@@ -4852,6 +4867,11 @@ EVP_PKEY *ssl_generate_param_group(uint16_t id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO(3.0): Convert this EVP_PKEY_CTX_new_id call to one that takes
|
||||
* s->ctx->libctx and s->ctx->propq when paramgen has been updated to be
|
||||
* provider aware.
|
||||
*/
|
||||
pkey_ctx_id = (ginf->flags & TLS_GROUP_FFDHE)
|
||||
? EVP_PKEY_DH : EVP_PKEY_EC;
|
||||
pctx = EVP_PKEY_CTX_new_id(pkey_ctx_id, NULL);
|
||||
@@ -4898,7 +4918,7 @@ int ssl_derive(SSL *s, EVP_PKEY *privkey, EVP_PKEY *pubkey, int gensecret)
|
||||
return 0;
|
||||
}
|
||||
|
||||
pctx = EVP_PKEY_CTX_new(privkey, NULL);
|
||||
pctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, privkey, s->ctx->propq);
|
||||
|
||||
if (EVP_PKEY_derive_init(pctx) <= 0
|
||||
|| EVP_PKEY_derive_set_peer(pctx, pubkey) <= 0
|
||||
|
||||
@@ -492,6 +492,8 @@ static const ERR_STRING_DATA SSL_str_reasons[] = {
|
||||
"unexpected ccs message"},
|
||||
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_END_OF_EARLY_DATA),
|
||||
"unexpected end of early data"},
|
||||
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_EOF_WHILE_READING),
|
||||
"unexpected eof while reading"},
|
||||
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_GROUP_CLOSE),
|
||||
"unexpected group close"},
|
||||
{ERR_PACK(ERR_LIB_SSL, 0, SSL_R_UNEXPECTED_MESSAGE), "unexpected message"},
|
||||
|
||||
+99
-30
@@ -2812,31 +2812,85 @@ char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
|
||||
return buf;
|
||||
}
|
||||
|
||||
/** return a servername extension value if provided in Client Hello, or NULL.
|
||||
* So far, only host_name types are defined (RFC 3546).
|
||||
/**
|
||||
* Return the requested servername (SNI) value. Note that the behaviour varies
|
||||
* depending on:
|
||||
* - whether this is called by the client or the server,
|
||||
* - if we are before or during/after the handshake,
|
||||
* - if a resumption or normal handshake is being attempted/has occurred
|
||||
* - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
|
||||
*
|
||||
* Note that only the host_name type is defined (RFC 3546).
|
||||
*/
|
||||
|
||||
const char *SSL_get_servername(const SSL *s, const int type)
|
||||
{
|
||||
/*
|
||||
* If we don't know if we are the client or the server yet then we assume
|
||||
* client.
|
||||
*/
|
||||
int server = s->handshake_func == NULL ? 0 : s->server;
|
||||
if (type != TLSEXT_NAMETYPE_host_name)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* SNI is not negotiated in pre-TLS-1.3 resumption flows, so fake up an
|
||||
* SNI value to return if we are resuming/resumed. N.B. that we still
|
||||
* call the relevant callbacks for such resumption flows, and callbacks
|
||||
* might error out if there is not a SNI value available.
|
||||
*/
|
||||
if (s->hit)
|
||||
return s->session->ext.hostname;
|
||||
if (server) {
|
||||
/**
|
||||
* Server side
|
||||
* In TLSv1.3 on the server SNI is not associated with the session
|
||||
* but in TLSv1.2 or below it is.
|
||||
*
|
||||
* Before the handshake:
|
||||
* - return NULL
|
||||
*
|
||||
* During/after the handshake (TLSv1.2 or below resumption occurred):
|
||||
* - If a servername was accepted by the server in the original
|
||||
* handshake then it will return that servername, or NULL otherwise.
|
||||
*
|
||||
* During/after the handshake (TLSv1.2 or below resumption did not occur):
|
||||
* - The function will return the servername requested by the client in
|
||||
* this handshake or NULL if none was requested.
|
||||
*/
|
||||
if (s->hit && !SSL_IS_TLS13(s))
|
||||
return s->session->ext.hostname;
|
||||
} else {
|
||||
/**
|
||||
* Client side
|
||||
*
|
||||
* Before the handshake:
|
||||
* - If a servername has been set via a call to
|
||||
* SSL_set_tlsext_host_name() then it will return that servername
|
||||
* - If one has not been set, but a TLSv1.2 resumption is being
|
||||
* attempted and the session from the original handshake had a
|
||||
* servername accepted by the server then it will return that
|
||||
* servername
|
||||
* - Otherwise it returns NULL
|
||||
*
|
||||
* During/after the handshake (TLSv1.2 or below resumption occurred):
|
||||
* - If the session from the orignal handshake had a servername accepted
|
||||
* by the server then it will return that servername.
|
||||
* - Otherwise it returns the servername set via
|
||||
* SSL_set_tlsext_host_name() (or NULL if it was not called).
|
||||
*
|
||||
* During/after the handshake (TLSv1.2 or below resumption did not occur):
|
||||
* - It will return the servername set via SSL_set_tlsext_host_name()
|
||||
* (or NULL if it was not called).
|
||||
*/
|
||||
if (SSL_in_before(s)) {
|
||||
if (s->ext.hostname == NULL
|
||||
&& s->session != NULL
|
||||
&& s->session->ssl_version != TLS1_3_VERSION)
|
||||
return s->session->ext.hostname;
|
||||
} else {
|
||||
if (!SSL_IS_TLS13(s) && s->hit && s->session->ext.hostname != NULL)
|
||||
return s->session->ext.hostname;
|
||||
}
|
||||
}
|
||||
|
||||
return s->ext.hostname;
|
||||
}
|
||||
|
||||
int SSL_get_servername_type(const SSL *s)
|
||||
{
|
||||
if (s->session
|
||||
&& (!s->ext.hostname ? s->session->
|
||||
ext.hostname : s->ext.hostname))
|
||||
if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
|
||||
return TLSEXT_NAMETYPE_host_name;
|
||||
return -1;
|
||||
}
|
||||
@@ -3091,12 +3145,13 @@ static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
|
||||
* via ssl.h.
|
||||
*/
|
||||
|
||||
SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
SSL_CTX *SSL_CTX_new_with_libctx(OPENSSL_CTX *libctx, const char *propq,
|
||||
const SSL_METHOD *meth)
|
||||
{
|
||||
SSL_CTX *ret = NULL;
|
||||
|
||||
if (meth == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);
|
||||
SSLerr(0, SSL_R_NULL_SSL_METHOD_PASSED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -3104,13 +3159,20 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
return NULL;
|
||||
|
||||
if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
|
||||
SSLerr(0, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
|
||||
goto err;
|
||||
}
|
||||
ret = OPENSSL_zalloc(sizeof(*ret));
|
||||
if (ret == NULL)
|
||||
goto err;
|
||||
|
||||
ret->libctx = libctx;
|
||||
if (propq != NULL) {
|
||||
ret->propq = OPENSSL_strdup(propq);
|
||||
if (ret->propq == NULL)
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret->method = meth;
|
||||
ret->min_proto_version = 0;
|
||||
ret->max_proto_version = 0;
|
||||
@@ -3122,7 +3184,7 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
ret->references = 1;
|
||||
ret->lock = CRYPTO_THREAD_lock_new();
|
||||
if (ret->lock == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
||||
SSLerr(0, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
@@ -3151,7 +3213,7 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
&ret->cipher_list, &ret->cipher_list_by_id,
|
||||
OSSL_default_cipher_list(), ret->cert)
|
||||
|| sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
|
||||
SSLerr(0, SSL_R_LIBRARY_HAS_NO_CIPHERS);
|
||||
goto err2;
|
||||
}
|
||||
|
||||
@@ -3160,11 +3222,11 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
goto err;
|
||||
|
||||
if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
|
||||
SSLerr(0, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
|
||||
goto err2;
|
||||
}
|
||||
if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
|
||||
SSLerr(0, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
|
||||
goto err2;
|
||||
}
|
||||
|
||||
@@ -3188,16 +3250,16 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
|
||||
|
||||
/* Setup RFC5077 ticket keys */
|
||||
if ((RAND_bytes(ret->ext.tick_key_name,
|
||||
sizeof(ret->ext.tick_key_name)) <= 0)
|
||||
|| (RAND_priv_bytes(ret->ext.secure->tick_hmac_key,
|
||||
sizeof(ret->ext.secure->tick_hmac_key)) <= 0)
|
||||
|| (RAND_priv_bytes(ret->ext.secure->tick_aes_key,
|
||||
sizeof(ret->ext.secure->tick_aes_key)) <= 0))
|
||||
if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
|
||||
sizeof(ret->ext.tick_key_name)) <= 0)
|
||||
|| (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
|
||||
sizeof(ret->ext.secure->tick_hmac_key)) <= 0)
|
||||
|| (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
|
||||
sizeof(ret->ext.secure->tick_aes_key)) <= 0))
|
||||
ret->options |= SSL_OP_NO_TICKET;
|
||||
|
||||
if (RAND_priv_bytes(ret->ext.cookie_hmac_key,
|
||||
sizeof(ret->ext.cookie_hmac_key)) <= 0)
|
||||
if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
|
||||
sizeof(ret->ext.cookie_hmac_key)) <= 0)
|
||||
goto err;
|
||||
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
@@ -3274,12 +3336,17 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
|
||||
return ret;
|
||||
err:
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
||||
SSLerr(0, ERR_R_MALLOC_FAILURE);
|
||||
err2:
|
||||
SSL_CTX_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
{
|
||||
return SSL_CTX_new_with_libctx(NULL, NULL, meth);
|
||||
}
|
||||
|
||||
int SSL_CTX_up_ref(SSL_CTX *ctx)
|
||||
{
|
||||
int i;
|
||||
@@ -3353,6 +3420,8 @@ void SSL_CTX_free(SSL_CTX *a)
|
||||
|
||||
CRYPTO_THREAD_lock_free(a->lock);
|
||||
|
||||
OPENSSL_free(a->propq);
|
||||
|
||||
OPENSSL_free(a);
|
||||
}
|
||||
|
||||
|
||||
-5766
@@ -1,5766 +0,0 @@
|
||||
/*
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
|
||||
* Copyright 2005 Nokia. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "ssl_local.h"
|
||||
#include "e_os.h"
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/x509v3.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/rand_drbg.h>
|
||||
#include <openssl/ocsp.h>
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/engine.h>
|
||||
#include <openssl/async.h>
|
||||
#include <openssl/ct.h>
|
||||
#include <openssl/trace.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/refcount.h"
|
||||
#include "internal/ktls.h"
|
||||
|
||||
static int ssl_undefined_function_1(SSL *ssl, SSL3_RECORD *r, size_t s, int t)
|
||||
{
|
||||
(void)r;
|
||||
(void)s;
|
||||
(void)t;
|
||||
return ssl_undefined_function(ssl);
|
||||
}
|
||||
|
||||
static int ssl_undefined_function_2(SSL *ssl, SSL3_RECORD *r, unsigned char *s,
|
||||
int t)
|
||||
{
|
||||
(void)r;
|
||||
(void)s;
|
||||
(void)t;
|
||||
return ssl_undefined_function(ssl);
|
||||
}
|
||||
|
||||
static int ssl_undefined_function_3(SSL *ssl, unsigned char *r,
|
||||
unsigned char *s, size_t t, size_t *u)
|
||||
{
|
||||
(void)r;
|
||||
(void)s;
|
||||
(void)t;
|
||||
(void)u;
|
||||
return ssl_undefined_function(ssl);
|
||||
}
|
||||
|
||||
static int ssl_undefined_function_4(SSL *ssl, int r)
|
||||
{
|
||||
(void)r;
|
||||
return ssl_undefined_function(ssl);
|
||||
}
|
||||
|
||||
static size_t ssl_undefined_function_5(SSL *ssl, const char *r, size_t s,
|
||||
unsigned char *t)
|
||||
{
|
||||
(void)r;
|
||||
(void)s;
|
||||
(void)t;
|
||||
return ssl_undefined_function(ssl);
|
||||
}
|
||||
|
||||
static int ssl_undefined_function_6(int r)
|
||||
{
|
||||
(void)r;
|
||||
return ssl_undefined_function(NULL);
|
||||
}
|
||||
|
||||
static int ssl_undefined_function_7(SSL *ssl, unsigned char *r, size_t s,
|
||||
const char *t, size_t u,
|
||||
const unsigned char *v, size_t w, int x)
|
||||
{
|
||||
(void)r;
|
||||
(void)s;
|
||||
(void)t;
|
||||
(void)u;
|
||||
(void)v;
|
||||
(void)w;
|
||||
(void)x;
|
||||
return ssl_undefined_function(ssl);
|
||||
}
|
||||
|
||||
SSL3_ENC_METHOD ssl3_undef_enc_method = {
|
||||
ssl_undefined_function_1,
|
||||
ssl_undefined_function_2,
|
||||
ssl_undefined_function,
|
||||
ssl_undefined_function_3,
|
||||
ssl_undefined_function_4,
|
||||
ssl_undefined_function_5,
|
||||
NULL, /* client_finished_label */
|
||||
0, /* client_finished_label_len */
|
||||
NULL, /* server_finished_label */
|
||||
0, /* server_finished_label_len */
|
||||
ssl_undefined_function_6,
|
||||
ssl_undefined_function_7,
|
||||
};
|
||||
|
||||
struct ssl_async_args {
|
||||
SSL *s;
|
||||
void *buf;
|
||||
size_t num;
|
||||
enum { READFUNC, WRITEFUNC, OTHERFUNC } type;
|
||||
union {
|
||||
int (*func_read) (SSL *, void *, size_t, size_t *);
|
||||
int (*func_write) (SSL *, const void *, size_t, size_t *);
|
||||
int (*func_other) (SSL *);
|
||||
} f;
|
||||
};
|
||||
|
||||
static const struct {
|
||||
uint8_t mtype;
|
||||
uint8_t ord;
|
||||
int nid;
|
||||
} dane_mds[] = {
|
||||
{
|
||||
DANETLS_MATCHING_FULL, 0, NID_undef
|
||||
},
|
||||
{
|
||||
DANETLS_MATCHING_2256, 1, NID_sha256
|
||||
},
|
||||
{
|
||||
DANETLS_MATCHING_2512, 2, NID_sha512
|
||||
},
|
||||
};
|
||||
|
||||
static int dane_ctx_enable(struct dane_ctx_st *dctx)
|
||||
{
|
||||
const EVP_MD **mdevp;
|
||||
uint8_t *mdord;
|
||||
uint8_t mdmax = DANETLS_MATCHING_LAST;
|
||||
int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
|
||||
size_t i;
|
||||
|
||||
if (dctx->mdevp != NULL)
|
||||
return 1;
|
||||
|
||||
mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
|
||||
mdord = OPENSSL_zalloc(n * sizeof(*mdord));
|
||||
|
||||
if (mdord == NULL || mdevp == NULL) {
|
||||
OPENSSL_free(mdord);
|
||||
OPENSSL_free(mdevp);
|
||||
SSLerr(SSL_F_DANE_CTX_ENABLE, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Install default entries */
|
||||
for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
|
||||
const EVP_MD *md;
|
||||
|
||||
if (dane_mds[i].nid == NID_undef ||
|
||||
(md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
|
||||
continue;
|
||||
mdevp[dane_mds[i].mtype] = md;
|
||||
mdord[dane_mds[i].mtype] = dane_mds[i].ord;
|
||||
}
|
||||
|
||||
dctx->mdevp = mdevp;
|
||||
dctx->mdord = mdord;
|
||||
dctx->mdmax = mdmax;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void dane_ctx_final(struct dane_ctx_st *dctx)
|
||||
{
|
||||
OPENSSL_free(dctx->mdevp);
|
||||
dctx->mdevp = NULL;
|
||||
|
||||
OPENSSL_free(dctx->mdord);
|
||||
dctx->mdord = NULL;
|
||||
dctx->mdmax = 0;
|
||||
}
|
||||
|
||||
static void tlsa_free(danetls_record *t)
|
||||
{
|
||||
if (t == NULL)
|
||||
return;
|
||||
OPENSSL_free(t->data);
|
||||
EVP_PKEY_free(t->spki);
|
||||
OPENSSL_free(t);
|
||||
}
|
||||
|
||||
static void dane_final(SSL_DANE *dane)
|
||||
{
|
||||
sk_danetls_record_pop_free(dane->trecs, tlsa_free);
|
||||
dane->trecs = NULL;
|
||||
|
||||
sk_X509_pop_free(dane->certs, X509_free);
|
||||
dane->certs = NULL;
|
||||
|
||||
X509_free(dane->mcert);
|
||||
dane->mcert = NULL;
|
||||
dane->mtlsa = NULL;
|
||||
dane->mdpth = -1;
|
||||
dane->pdpth = -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* dane_copy - Copy dane configuration, sans verification state.
|
||||
*/
|
||||
static int ssl_dane_dup(SSL *to, SSL *from)
|
||||
{
|
||||
int num;
|
||||
int i;
|
||||
|
||||
if (!DANETLS_ENABLED(&from->dane))
|
||||
return 1;
|
||||
|
||||
num = sk_danetls_record_num(from->dane.trecs);
|
||||
dane_final(&to->dane);
|
||||
to->dane.flags = from->dane.flags;
|
||||
to->dane.dctx = &to->ctx->dane;
|
||||
to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
|
||||
|
||||
if (to->dane.trecs == NULL) {
|
||||
SSLerr(SSL_F_SSL_DANE_DUP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < num; ++i) {
|
||||
danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
|
||||
|
||||
if (SSL_dane_tlsa_add(to, t->usage, t->selector, t->mtype,
|
||||
t->data, t->dlen) <= 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dane_mtype_set(struct dane_ctx_st *dctx,
|
||||
const EVP_MD *md, uint8_t mtype, uint8_t ord)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
|
||||
SSLerr(SSL_F_DANE_MTYPE_SET, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mtype > dctx->mdmax) {
|
||||
const EVP_MD **mdevp;
|
||||
uint8_t *mdord;
|
||||
int n = ((int)mtype) + 1;
|
||||
|
||||
mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
|
||||
if (mdevp == NULL) {
|
||||
SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
dctx->mdevp = mdevp;
|
||||
|
||||
mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
|
||||
if (mdord == NULL) {
|
||||
SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
dctx->mdord = mdord;
|
||||
|
||||
/* Zero-fill any gaps */
|
||||
for (i = dctx->mdmax + 1; i < mtype; ++i) {
|
||||
mdevp[i] = NULL;
|
||||
mdord[i] = 0;
|
||||
}
|
||||
|
||||
dctx->mdmax = mtype;
|
||||
}
|
||||
|
||||
dctx->mdevp[mtype] = md;
|
||||
/* Coerce ordinal of disabled matching types to 0 */
|
||||
dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
|
||||
{
|
||||
if (mtype > dane->dctx->mdmax)
|
||||
return NULL;
|
||||
return dane->dctx->mdevp[mtype];
|
||||
}
|
||||
|
||||
static int dane_tlsa_add(SSL_DANE *dane,
|
||||
uint8_t usage,
|
||||
uint8_t selector,
|
||||
uint8_t mtype, unsigned const char *data, size_t dlen)
|
||||
{
|
||||
danetls_record *t;
|
||||
const EVP_MD *md = NULL;
|
||||
int ilen = (int)dlen;
|
||||
int i;
|
||||
int num;
|
||||
|
||||
if (dane->trecs == NULL) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_NOT_ENABLED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ilen < 0 || dlen != (size_t)ilen) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (usage > DANETLS_USAGE_LAST) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (selector > DANETLS_SELECTOR_LAST) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_SELECTOR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mtype != DANETLS_MATCHING_FULL) {
|
||||
md = tlsa_md_get(dane, mtype);
|
||||
if (md == NULL) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (md != NULL && dlen != (size_t)EVP_MD_size(md)) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
if (!data) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_NULL_DATA);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
t->usage = usage;
|
||||
t->selector = selector;
|
||||
t->mtype = mtype;
|
||||
t->data = OPENSSL_malloc(dlen);
|
||||
if (t->data == NULL) {
|
||||
tlsa_free(t);
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
memcpy(t->data, data, dlen);
|
||||
t->dlen = dlen;
|
||||
|
||||
/* Validate and cache full certificate or public key */
|
||||
if (mtype == DANETLS_MATCHING_FULL) {
|
||||
const unsigned char *p = data;
|
||||
X509 *cert = NULL;
|
||||
EVP_PKEY *pkey = NULL;
|
||||
|
||||
switch (selector) {
|
||||
case DANETLS_SELECTOR_CERT:
|
||||
if (!d2i_X509(&cert, &p, ilen) || p < data ||
|
||||
dlen != (size_t)(p - data)) {
|
||||
tlsa_free(t);
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
|
||||
return 0;
|
||||
}
|
||||
if (X509_get0_pubkey(cert) == NULL) {
|
||||
tlsa_free(t);
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
|
||||
X509_free(cert);
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
|
||||
* records that contain full certificates of trust-anchors that are
|
||||
* not present in the wire chain. For usage PKIX-TA(0), we augment
|
||||
* the chain with untrusted Full(0) certificates from DNS, in case
|
||||
* they are missing from the chain.
|
||||
*/
|
||||
if ((dane->certs == NULL &&
|
||||
(dane->certs = sk_X509_new_null()) == NULL) ||
|
||||
!sk_X509_push(dane->certs, cert)) {
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
|
||||
X509_free(cert);
|
||||
tlsa_free(t);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
|
||||
case DANETLS_SELECTOR_SPKI:
|
||||
if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data ||
|
||||
dlen != (size_t)(p - data)) {
|
||||
tlsa_free(t);
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
|
||||
* records that contain full bare keys of trust-anchors that are
|
||||
* not present in the wire chain.
|
||||
*/
|
||||
if (usage == DANETLS_USAGE_DANE_TA)
|
||||
t->spki = pkey;
|
||||
else
|
||||
EVP_PKEY_free(pkey);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* Find the right insertion point for the new record.
|
||||
*
|
||||
* See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
|
||||
* they can be processed first, as they require no chain building, and no
|
||||
* expiration or hostname checks. Because DANE-EE(3) is numerically
|
||||
* largest, this is accomplished via descending sort by "usage".
|
||||
*
|
||||
* We also sort in descending order by matching ordinal to simplify
|
||||
* the implementation of digest agility in the verification code.
|
||||
*
|
||||
* The choice of order for the selector is not significant, so we
|
||||
* use the same descending order for consistency.
|
||||
*/
|
||||
num = sk_danetls_record_num(dane->trecs);
|
||||
for (i = 0; i < num; ++i) {
|
||||
danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
|
||||
|
||||
if (rec->usage > usage)
|
||||
continue;
|
||||
if (rec->usage < usage)
|
||||
break;
|
||||
if (rec->selector > selector)
|
||||
continue;
|
||||
if (rec->selector < selector)
|
||||
break;
|
||||
if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sk_danetls_record_insert(dane->trecs, t, i)) {
|
||||
tlsa_free(t);
|
||||
SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
dane->umask |= DANETLS_USAGE_BIT(usage);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return 0 if there is only one version configured and it was disabled
|
||||
* at configure time. Return 1 otherwise.
|
||||
*/
|
||||
static int ssl_check_allowed_versions(int min_version, int max_version)
|
||||
{
|
||||
int minisdtls = 0, maxisdtls = 0;
|
||||
|
||||
/* Figure out if we're doing DTLS versions or TLS versions */
|
||||
if (min_version == DTLS1_BAD_VER
|
||||
|| min_version >> 8 == DTLS1_VERSION_MAJOR)
|
||||
minisdtls = 1;
|
||||
if (max_version == DTLS1_BAD_VER
|
||||
|| max_version >> 8 == DTLS1_VERSION_MAJOR)
|
||||
maxisdtls = 1;
|
||||
/* A wildcard version of 0 could be DTLS or TLS. */
|
||||
if ((minisdtls && !maxisdtls && max_version != 0)
|
||||
|| (maxisdtls && !minisdtls && min_version != 0)) {
|
||||
/* Mixing DTLS and TLS versions will lead to sadness; deny it. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (minisdtls || maxisdtls) {
|
||||
/* Do DTLS version checks. */
|
||||
if (min_version == 0)
|
||||
/* Ignore DTLS1_BAD_VER */
|
||||
min_version = DTLS1_VERSION;
|
||||
if (max_version == 0)
|
||||
max_version = DTLS1_2_VERSION;
|
||||
#ifdef OPENSSL_NO_DTLS1_2
|
||||
if (max_version == DTLS1_2_VERSION)
|
||||
max_version = DTLS1_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_DTLS1
|
||||
if (min_version == DTLS1_VERSION)
|
||||
min_version = DTLS1_2_VERSION;
|
||||
#endif
|
||||
/* Done massaging versions; do the check. */
|
||||
if (0
|
||||
#ifdef OPENSSL_NO_DTLS1
|
||||
|| (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
|
||||
&& DTLS_VERSION_GE(DTLS1_VERSION, max_version))
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_DTLS1_2
|
||||
|| (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
|
||||
&& DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
|
||||
#endif
|
||||
)
|
||||
return 0;
|
||||
} else {
|
||||
/* Regular TLS version checks. */
|
||||
if (min_version == 0)
|
||||
min_version = SSL3_VERSION;
|
||||
if (max_version == 0)
|
||||
max_version = TLS1_3_VERSION;
|
||||
#ifdef OPENSSL_NO_TLS1_3
|
||||
if (max_version == TLS1_3_VERSION)
|
||||
max_version = TLS1_2_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1_2
|
||||
if (max_version == TLS1_2_VERSION)
|
||||
max_version = TLS1_1_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1_1
|
||||
if (max_version == TLS1_1_VERSION)
|
||||
max_version = TLS1_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1
|
||||
if (max_version == TLS1_VERSION)
|
||||
max_version = SSL3_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_SSL3
|
||||
if (min_version == SSL3_VERSION)
|
||||
min_version = TLS1_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1
|
||||
if (min_version == TLS1_VERSION)
|
||||
min_version = TLS1_1_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1_1
|
||||
if (min_version == TLS1_1_VERSION)
|
||||
min_version = TLS1_2_VERSION;
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1_2
|
||||
if (min_version == TLS1_2_VERSION)
|
||||
min_version = TLS1_3_VERSION;
|
||||
#endif
|
||||
/* Done massaging versions; do the check. */
|
||||
if (0
|
||||
#ifdef OPENSSL_NO_SSL3
|
||||
|| (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version)
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1
|
||||
|| (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1_1
|
||||
|| (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1_2
|
||||
|| (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
|
||||
#endif
|
||||
#ifdef OPENSSL_NO_TLS1_3
|
||||
|| (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
|
||||
#endif
|
||||
)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void clear_ciphers(SSL *s)
|
||||
{
|
||||
/* clear the current cipher */
|
||||
ssl_clear_cipher_ctx(s);
|
||||
ssl_clear_hash_ctx(&s->read_hash);
|
||||
ssl_clear_hash_ctx(&s->write_hash);
|
||||
}
|
||||
|
||||
int SSL_clear(SSL *s)
|
||||
{
|
||||
if (s->method == NULL) {
|
||||
SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ssl_clear_bad_session(s)) {
|
||||
SSL_SESSION_free(s->session);
|
||||
s->session = NULL;
|
||||
}
|
||||
SSL_SESSION_free(s->psksession);
|
||||
s->psksession = NULL;
|
||||
OPENSSL_free(s->psksession_id);
|
||||
s->psksession_id = NULL;
|
||||
s->psksession_id_len = 0;
|
||||
s->hello_retry_request = 0;
|
||||
s->sent_tickets = 0;
|
||||
|
||||
s->error = 0;
|
||||
s->hit = 0;
|
||||
s->shutdown = 0;
|
||||
|
||||
if (s->renegotiate) {
|
||||
SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ossl_statem_clear(s);
|
||||
|
||||
s->version = s->method->version;
|
||||
s->client_version = s->version;
|
||||
s->rwstate = SSL_NOTHING;
|
||||
|
||||
BUF_MEM_free(s->init_buf);
|
||||
s->init_buf = NULL;
|
||||
clear_ciphers(s);
|
||||
s->first_packet = 0;
|
||||
|
||||
s->key_update = SSL_KEY_UPDATE_NONE;
|
||||
|
||||
EVP_MD_CTX_free(s->pha_dgst);
|
||||
s->pha_dgst = NULL;
|
||||
|
||||
/* Reset DANE verification result state */
|
||||
s->dane.mdpth = -1;
|
||||
s->dane.pdpth = -1;
|
||||
X509_free(s->dane.mcert);
|
||||
s->dane.mcert = NULL;
|
||||
s->dane.mtlsa = NULL;
|
||||
|
||||
/* Clear the verification result peername */
|
||||
X509_VERIFY_PARAM_move_peername(s->param, NULL);
|
||||
|
||||
/* Clear any shared connection state */
|
||||
OPENSSL_free(s->shared_sigalgs);
|
||||
s->shared_sigalgs = NULL;
|
||||
s->shared_sigalgslen = 0;
|
||||
|
||||
/*
|
||||
* Check to see if we were changed into a different method, if so, revert
|
||||
* back.
|
||||
*/
|
||||
if (s->method != s->ctx->method) {
|
||||
s->method->ssl_free(s);
|
||||
s->method = s->ctx->method;
|
||||
if (!s->method->ssl_new(s))
|
||||
return 0;
|
||||
} else {
|
||||
if (!s->method->ssl_clear(s))
|
||||
return 0;
|
||||
}
|
||||
|
||||
RECORD_LAYER_clear(&s->rlayer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/** Used to change an SSL_CTXs default SSL method type */
|
||||
int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
|
||||
{
|
||||
STACK_OF(SSL_CIPHER) *sk;
|
||||
|
||||
ctx->method = meth;
|
||||
|
||||
if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
|
||||
SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
|
||||
return 0;
|
||||
}
|
||||
sk = ssl_create_cipher_list(ctx->method,
|
||||
ctx->tls13_ciphersuites,
|
||||
&(ctx->cipher_list),
|
||||
&(ctx->cipher_list_by_id),
|
||||
OSSL_default_cipher_list(), ctx->cert);
|
||||
if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
|
||||
SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
SSL *SSL_new(SSL_CTX *ctx)
|
||||
{
|
||||
SSL *s;
|
||||
|
||||
if (ctx == NULL) {
|
||||
SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
|
||||
return NULL;
|
||||
}
|
||||
if (ctx->method == NULL) {
|
||||
SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
s = OPENSSL_zalloc(sizeof(*s));
|
||||
if (s == NULL)
|
||||
goto err;
|
||||
|
||||
s->references = 1;
|
||||
s->lock = CRYPTO_THREAD_lock_new();
|
||||
if (s->lock == NULL) {
|
||||
OPENSSL_free(s);
|
||||
s = NULL;
|
||||
goto err;
|
||||
}
|
||||
|
||||
RECORD_LAYER_init(&s->rlayer, s);
|
||||
|
||||
s->options = ctx->options;
|
||||
s->dane.flags = ctx->dane.flags;
|
||||
s->min_proto_version = ctx->min_proto_version;
|
||||
s->max_proto_version = ctx->max_proto_version;
|
||||
s->mode = ctx->mode;
|
||||
s->max_cert_list = ctx->max_cert_list;
|
||||
s->max_early_data = ctx->max_early_data;
|
||||
s->recv_max_early_data = ctx->recv_max_early_data;
|
||||
s->num_tickets = ctx->num_tickets;
|
||||
s->pha_enabled = ctx->pha_enabled;
|
||||
|
||||
/* Shallow copy of the ciphersuites stack */
|
||||
s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
|
||||
if (s->tls13_ciphersuites == NULL)
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* Earlier library versions used to copy the pointer to the CERT, not
|
||||
* its contents; only when setting new parameters for the per-SSL
|
||||
* copy, ssl_cert_new would be called (and the direct reference to
|
||||
* the per-SSL_CTX settings would be lost, but those still were
|
||||
* indirectly accessed for various purposes, and for that reason they
|
||||
* used to be known as s->ctx->default_cert). Now we don't look at the
|
||||
* SSL_CTX's CERT after having duplicated it once.
|
||||
*/
|
||||
s->cert = ssl_cert_dup(ctx->cert);
|
||||
if (s->cert == NULL)
|
||||
goto err;
|
||||
|
||||
RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
|
||||
s->msg_callback = ctx->msg_callback;
|
||||
s->msg_callback_arg = ctx->msg_callback_arg;
|
||||
s->verify_mode = ctx->verify_mode;
|
||||
s->not_resumable_session_cb = ctx->not_resumable_session_cb;
|
||||
s->record_padding_cb = ctx->record_padding_cb;
|
||||
s->record_padding_arg = ctx->record_padding_arg;
|
||||
s->block_padding = ctx->block_padding;
|
||||
s->sid_ctx_length = ctx->sid_ctx_length;
|
||||
if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
|
||||
goto err;
|
||||
memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
|
||||
s->verify_callback = ctx->default_verify_callback;
|
||||
s->generate_session_id = ctx->generate_session_id;
|
||||
|
||||
s->param = X509_VERIFY_PARAM_new();
|
||||
if (s->param == NULL)
|
||||
goto err;
|
||||
X509_VERIFY_PARAM_inherit(s->param, ctx->param);
|
||||
s->quiet_shutdown = ctx->quiet_shutdown;
|
||||
|
||||
s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
|
||||
s->max_send_fragment = ctx->max_send_fragment;
|
||||
s->split_send_fragment = ctx->split_send_fragment;
|
||||
s->max_pipelines = ctx->max_pipelines;
|
||||
if (s->max_pipelines > 1)
|
||||
RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
|
||||
if (ctx->default_read_buf_len > 0)
|
||||
SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
|
||||
|
||||
SSL_CTX_up_ref(ctx);
|
||||
s->ctx = ctx;
|
||||
s->ext.debug_cb = 0;
|
||||
s->ext.debug_arg = NULL;
|
||||
s->ext.ticket_expected = 0;
|
||||
s->ext.status_type = ctx->ext.status_type;
|
||||
s->ext.status_expected = 0;
|
||||
s->ext.ocsp.ids = NULL;
|
||||
s->ext.ocsp.exts = NULL;
|
||||
s->ext.ocsp.resp = NULL;
|
||||
s->ext.ocsp.resp_len = 0;
|
||||
SSL_CTX_up_ref(ctx);
|
||||
s->session_ctx = ctx;
|
||||
#ifndef OPENSSL_NO_EC
|
||||
if (ctx->ext.ecpointformats) {
|
||||
s->ext.ecpointformats =
|
||||
OPENSSL_memdup(ctx->ext.ecpointformats,
|
||||
ctx->ext.ecpointformats_len);
|
||||
if (!s->ext.ecpointformats)
|
||||
goto err;
|
||||
s->ext.ecpointformats_len =
|
||||
ctx->ext.ecpointformats_len;
|
||||
}
|
||||
#endif
|
||||
if (ctx->ext.supportedgroups) {
|
||||
s->ext.supportedgroups =
|
||||
OPENSSL_memdup(ctx->ext.supportedgroups,
|
||||
ctx->ext.supportedgroups_len
|
||||
* sizeof(*ctx->ext.supportedgroups));
|
||||
if (!s->ext.supportedgroups)
|
||||
goto err;
|
||||
s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
s->ext.npn = NULL;
|
||||
#endif
|
||||
|
||||
if (s->ctx->ext.alpn) {
|
||||
s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);
|
||||
if (s->ext.alpn == NULL)
|
||||
goto err;
|
||||
memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);
|
||||
s->ext.alpn_len = s->ctx->ext.alpn_len;
|
||||
}
|
||||
|
||||
s->verified_chain = NULL;
|
||||
s->verify_result = X509_V_OK;
|
||||
|
||||
s->default_passwd_callback = ctx->default_passwd_callback;
|
||||
s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
|
||||
|
||||
s->method = ctx->method;
|
||||
|
||||
s->key_update = SSL_KEY_UPDATE_NONE;
|
||||
|
||||
s->allow_early_data_cb = ctx->allow_early_data_cb;
|
||||
s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
|
||||
|
||||
if (!s->method->ssl_new(s))
|
||||
goto err;
|
||||
|
||||
s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
|
||||
|
||||
if (!SSL_clear(s))
|
||||
goto err;
|
||||
|
||||
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))
|
||||
goto err;
|
||||
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
s->psk_client_callback = ctx->psk_client_callback;
|
||||
s->psk_server_callback = ctx->psk_server_callback;
|
||||
#endif
|
||||
s->psk_find_session_cb = ctx->psk_find_session_cb;
|
||||
s->psk_use_session_cb = ctx->psk_use_session_cb;
|
||||
|
||||
s->async_cb = ctx->async_cb;
|
||||
s->async_cb_arg = ctx->async_cb_arg;
|
||||
|
||||
s->job = NULL;
|
||||
|
||||
#ifndef OPENSSL_NO_CT
|
||||
if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
|
||||
ctx->ct_validation_callback_arg))
|
||||
goto err;
|
||||
#endif
|
||||
|
||||
return s;
|
||||
err:
|
||||
SSL_free(s);
|
||||
SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int SSL_is_dtls(const SSL *s)
|
||||
{
|
||||
return SSL_IS_DTLS(s) ? 1 : 0;
|
||||
}
|
||||
|
||||
int SSL_up_ref(SSL *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (CRYPTO_UP_REF(&s->references, &i, s->lock) <= 0)
|
||||
return 0;
|
||||
|
||||
REF_PRINT_COUNT("SSL", s);
|
||||
REF_ASSERT_ISNT(i < 2);
|
||||
return ((i > 1) ? 1 : 0);
|
||||
}
|
||||
|
||||
int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
|
||||
unsigned int sid_ctx_len)
|
||||
{
|
||||
if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
|
||||
SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,
|
||||
SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
ctx->sid_ctx_length = sid_ctx_len;
|
||||
memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
|
||||
unsigned int sid_ctx_len)
|
||||
{
|
||||
if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
|
||||
SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,
|
||||
SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
ssl->sid_ctx_length = sid_ctx_len;
|
||||
memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
|
||||
{
|
||||
CRYPTO_THREAD_write_lock(ctx->lock);
|
||||
ctx->generate_session_id = cb;
|
||||
CRYPTO_THREAD_unlock(ctx->lock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
|
||||
{
|
||||
CRYPTO_THREAD_write_lock(ssl->lock);
|
||||
ssl->generate_session_id = cb;
|
||||
CRYPTO_THREAD_unlock(ssl->lock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
|
||||
unsigned int id_len)
|
||||
{
|
||||
/*
|
||||
* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
|
||||
* we can "construct" a session to give us the desired check - i.e. to
|
||||
* find if there's a session in the hash table that would conflict with
|
||||
* any new session built out of this id/id_len and the ssl_version in use
|
||||
* by this SSL.
|
||||
*/
|
||||
SSL_SESSION r, *p;
|
||||
|
||||
if (id_len > sizeof(r.session_id))
|
||||
return 0;
|
||||
|
||||
r.ssl_version = ssl->version;
|
||||
r.session_id_length = id_len;
|
||||
memcpy(r.session_id, id, id_len);
|
||||
|
||||
CRYPTO_THREAD_read_lock(ssl->session_ctx->lock);
|
||||
p = lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &r);
|
||||
CRYPTO_THREAD_unlock(ssl->session_ctx->lock);
|
||||
return (p != NULL);
|
||||
}
|
||||
|
||||
int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
|
||||
{
|
||||
return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
|
||||
}
|
||||
|
||||
int SSL_set_purpose(SSL *s, int purpose)
|
||||
{
|
||||
return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
|
||||
}
|
||||
|
||||
int SSL_CTX_set_trust(SSL_CTX *s, int trust)
|
||||
{
|
||||
return X509_VERIFY_PARAM_set_trust(s->param, trust);
|
||||
}
|
||||
|
||||
int SSL_set_trust(SSL *s, int trust)
|
||||
{
|
||||
return X509_VERIFY_PARAM_set_trust(s->param, trust);
|
||||
}
|
||||
|
||||
int SSL_set1_host(SSL *s, const char *hostname)
|
||||
{
|
||||
return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
|
||||
}
|
||||
|
||||
int SSL_add1_host(SSL *s, const char *hostname)
|
||||
{
|
||||
return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0);
|
||||
}
|
||||
|
||||
void SSL_set_hostflags(SSL *s, unsigned int flags)
|
||||
{
|
||||
X509_VERIFY_PARAM_set_hostflags(s->param, flags);
|
||||
}
|
||||
|
||||
const char *SSL_get0_peername(SSL *s)
|
||||
{
|
||||
return X509_VERIFY_PARAM_get0_peername(s->param);
|
||||
}
|
||||
|
||||
int SSL_CTX_dane_enable(SSL_CTX *ctx)
|
||||
{
|
||||
return dane_ctx_enable(&ctx->dane);
|
||||
}
|
||||
|
||||
unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
|
||||
{
|
||||
unsigned long orig = ctx->dane.flags;
|
||||
|
||||
ctx->dane.flags |= flags;
|
||||
return orig;
|
||||
}
|
||||
|
||||
unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
|
||||
{
|
||||
unsigned long orig = ctx->dane.flags;
|
||||
|
||||
ctx->dane.flags &= ~flags;
|
||||
return orig;
|
||||
}
|
||||
|
||||
int SSL_dane_enable(SSL *s, const char *basedomain)
|
||||
{
|
||||
SSL_DANE *dane = &s->dane;
|
||||
|
||||
if (s->ctx->dane.mdmax == 0) {
|
||||
SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_CONTEXT_NOT_DANE_ENABLED);
|
||||
return 0;
|
||||
}
|
||||
if (dane->trecs != NULL) {
|
||||
SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_DANE_ALREADY_ENABLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Default SNI name. This rejects empty names, while set1_host below
|
||||
* accepts them and disables host name checks. To avoid side-effects with
|
||||
* invalid input, set the SNI name first.
|
||||
*/
|
||||
if (s->ext.hostname == NULL) {
|
||||
if (!SSL_set_tlsext_host_name(s, basedomain)) {
|
||||
SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Primary RFC6125 reference identifier */
|
||||
if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) {
|
||||
SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
|
||||
return -1;
|
||||
}
|
||||
|
||||
dane->mdpth = -1;
|
||||
dane->pdpth = -1;
|
||||
dane->dctx = &s->ctx->dane;
|
||||
dane->trecs = sk_danetls_record_new_null();
|
||||
|
||||
if (dane->trecs == NULL) {
|
||||
SSLerr(SSL_F_SSL_DANE_ENABLE, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
|
||||
{
|
||||
unsigned long orig = ssl->dane.flags;
|
||||
|
||||
ssl->dane.flags |= flags;
|
||||
return orig;
|
||||
}
|
||||
|
||||
unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
|
||||
{
|
||||
unsigned long orig = ssl->dane.flags;
|
||||
|
||||
ssl->dane.flags &= ~flags;
|
||||
return orig;
|
||||
}
|
||||
|
||||
int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
|
||||
{
|
||||
SSL_DANE *dane = &s->dane;
|
||||
|
||||
if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
|
||||
return -1;
|
||||
if (dane->mtlsa) {
|
||||
if (mcert)
|
||||
*mcert = dane->mcert;
|
||||
if (mspki)
|
||||
*mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
|
||||
}
|
||||
return dane->mdpth;
|
||||
}
|
||||
|
||||
int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
|
||||
uint8_t *mtype, unsigned const char **data, size_t *dlen)
|
||||
{
|
||||
SSL_DANE *dane = &s->dane;
|
||||
|
||||
if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
|
||||
return -1;
|
||||
if (dane->mtlsa) {
|
||||
if (usage)
|
||||
*usage = dane->mtlsa->usage;
|
||||
if (selector)
|
||||
*selector = dane->mtlsa->selector;
|
||||
if (mtype)
|
||||
*mtype = dane->mtlsa->mtype;
|
||||
if (data)
|
||||
*data = dane->mtlsa->data;
|
||||
if (dlen)
|
||||
*dlen = dane->mtlsa->dlen;
|
||||
}
|
||||
return dane->mdpth;
|
||||
}
|
||||
|
||||
SSL_DANE *SSL_get0_dane(SSL *s)
|
||||
{
|
||||
return &s->dane;
|
||||
}
|
||||
|
||||
int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
|
||||
uint8_t mtype, unsigned const char *data, size_t dlen)
|
||||
{
|
||||
return dane_tlsa_add(&s->dane, usage, selector, mtype, data, dlen);
|
||||
}
|
||||
|
||||
int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
|
||||
uint8_t ord)
|
||||
{
|
||||
return dane_mtype_set(&ctx->dane, md, mtype, ord);
|
||||
}
|
||||
|
||||
int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
|
||||
{
|
||||
return X509_VERIFY_PARAM_set1(ctx->param, vpm);
|
||||
}
|
||||
|
||||
int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
|
||||
{
|
||||
return X509_VERIFY_PARAM_set1(ssl->param, vpm);
|
||||
}
|
||||
|
||||
X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->param;
|
||||
}
|
||||
|
||||
X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
|
||||
{
|
||||
return ssl->param;
|
||||
}
|
||||
|
||||
void SSL_certs_clear(SSL *s)
|
||||
{
|
||||
ssl_cert_clear_certs(s->cert);
|
||||
}
|
||||
|
||||
void SSL_free(SSL *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (s == NULL)
|
||||
return;
|
||||
CRYPTO_DOWN_REF(&s->references, &i, s->lock);
|
||||
REF_PRINT_COUNT("SSL", s);
|
||||
if (i > 0)
|
||||
return;
|
||||
REF_ASSERT_ISNT(i < 0);
|
||||
|
||||
X509_VERIFY_PARAM_free(s->param);
|
||||
dane_final(&s->dane);
|
||||
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
|
||||
|
||||
RECORD_LAYER_release(&s->rlayer);
|
||||
|
||||
/* Ignore return value */
|
||||
ssl_free_wbio_buffer(s);
|
||||
|
||||
BIO_free_all(s->wbio);
|
||||
s->wbio = NULL;
|
||||
BIO_free_all(s->rbio);
|
||||
s->rbio = NULL;
|
||||
|
||||
BUF_MEM_free(s->init_buf);
|
||||
|
||||
/* add extra stuff */
|
||||
sk_SSL_CIPHER_free(s->cipher_list);
|
||||
sk_SSL_CIPHER_free(s->cipher_list_by_id);
|
||||
sk_SSL_CIPHER_free(s->tls13_ciphersuites);
|
||||
sk_SSL_CIPHER_free(s->peer_ciphers);
|
||||
|
||||
/* Make the next call work :-) */
|
||||
if (s->session != NULL) {
|
||||
ssl_clear_bad_session(s);
|
||||
SSL_SESSION_free(s->session);
|
||||
}
|
||||
SSL_SESSION_free(s->psksession);
|
||||
OPENSSL_free(s->psksession_id);
|
||||
|
||||
clear_ciphers(s);
|
||||
|
||||
ssl_cert_free(s->cert);
|
||||
OPENSSL_free(s->shared_sigalgs);
|
||||
/* Free up if allocated */
|
||||
|
||||
OPENSSL_free(s->ext.hostname);
|
||||
SSL_CTX_free(s->session_ctx);
|
||||
#ifndef OPENSSL_NO_EC
|
||||
OPENSSL_free(s->ext.ecpointformats);
|
||||
OPENSSL_free(s->ext.peer_ecpointformats);
|
||||
#endif /* OPENSSL_NO_EC */
|
||||
OPENSSL_free(s->ext.supportedgroups);
|
||||
OPENSSL_free(s->ext.peer_supportedgroups);
|
||||
sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
|
||||
#ifndef OPENSSL_NO_OCSP
|
||||
sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_CT
|
||||
SCT_LIST_free(s->scts);
|
||||
OPENSSL_free(s->ext.scts);
|
||||
#endif
|
||||
OPENSSL_free(s->ext.ocsp.resp);
|
||||
OPENSSL_free(s->ext.alpn);
|
||||
OPENSSL_free(s->ext.tls13_cookie);
|
||||
OPENSSL_free(s->clienthello);
|
||||
OPENSSL_free(s->pha_context);
|
||||
EVP_MD_CTX_free(s->pha_dgst);
|
||||
|
||||
sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
|
||||
sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
|
||||
|
||||
sk_X509_pop_free(s->verified_chain, X509_free);
|
||||
|
||||
if (s->method != NULL)
|
||||
s->method->ssl_free(s);
|
||||
|
||||
SSL_CTX_free(s->ctx);
|
||||
|
||||
ASYNC_WAIT_CTX_free(s->waitctx);
|
||||
|
||||
#if !defined(OPENSSL_NO_NEXTPROTONEG)
|
||||
OPENSSL_free(s->ext.npn);
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_SRTP
|
||||
sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
|
||||
#endif
|
||||
|
||||
CRYPTO_THREAD_lock_free(s->lock);
|
||||
|
||||
OPENSSL_free(s);
|
||||
}
|
||||
|
||||
void SSL_set0_rbio(SSL *s, BIO *rbio)
|
||||
{
|
||||
BIO_free_all(s->rbio);
|
||||
s->rbio = rbio;
|
||||
}
|
||||
|
||||
void SSL_set0_wbio(SSL *s, BIO *wbio)
|
||||
{
|
||||
/*
|
||||
* If the output buffering BIO is still in place, remove it
|
||||
*/
|
||||
if (s->bbio != NULL)
|
||||
s->wbio = BIO_pop(s->wbio);
|
||||
|
||||
BIO_free_all(s->wbio);
|
||||
s->wbio = wbio;
|
||||
|
||||
/* Re-attach |bbio| to the new |wbio|. */
|
||||
if (s->bbio != NULL)
|
||||
s->wbio = BIO_push(s->bbio, s->wbio);
|
||||
}
|
||||
|
||||
void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
|
||||
{
|
||||
/*
|
||||
* For historical reasons, this function has many different cases in
|
||||
* ownership handling.
|
||||
*/
|
||||
|
||||
/* If nothing has changed, do nothing */
|
||||
if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
|
||||
return;
|
||||
|
||||
/*
|
||||
* If the two arguments are equal then one fewer reference is granted by the
|
||||
* caller than we want to take
|
||||
*/
|
||||
if (rbio != NULL && rbio == wbio)
|
||||
BIO_up_ref(rbio);
|
||||
|
||||
/*
|
||||
* If only the wbio is changed only adopt one reference.
|
||||
*/
|
||||
if (rbio == SSL_get_rbio(s)) {
|
||||
SSL_set0_wbio(s, wbio);
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* There is an asymmetry here for historical reasons. If only the rbio is
|
||||
* changed AND the rbio and wbio were originally different, then we only
|
||||
* adopt one reference.
|
||||
*/
|
||||
if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
|
||||
SSL_set0_rbio(s, rbio);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Otherwise, adopt both references. */
|
||||
SSL_set0_rbio(s, rbio);
|
||||
SSL_set0_wbio(s, wbio);
|
||||
}
|
||||
|
||||
BIO *SSL_get_rbio(const SSL *s)
|
||||
{
|
||||
return s->rbio;
|
||||
}
|
||||
|
||||
BIO *SSL_get_wbio(const SSL *s)
|
||||
{
|
||||
if (s->bbio != NULL) {
|
||||
/*
|
||||
* If |bbio| is active, the true caller-configured BIO is its
|
||||
* |next_bio|.
|
||||
*/
|
||||
return BIO_next(s->bbio);
|
||||
}
|
||||
return s->wbio;
|
||||
}
|
||||
|
||||
int SSL_get_fd(const SSL *s)
|
||||
{
|
||||
return SSL_get_rfd(s);
|
||||
}
|
||||
|
||||
int SSL_get_rfd(const SSL *s)
|
||||
{
|
||||
int ret = -1;
|
||||
BIO *b, *r;
|
||||
|
||||
b = SSL_get_rbio(s);
|
||||
r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
|
||||
if (r != NULL)
|
||||
BIO_get_fd(r, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_get_wfd(const SSL *s)
|
||||
{
|
||||
int ret = -1;
|
||||
BIO *b, *r;
|
||||
|
||||
b = SSL_get_wbio(s);
|
||||
r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
|
||||
if (r != NULL)
|
||||
BIO_get_fd(r, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_SOCK
|
||||
int SSL_set_fd(SSL *s, int fd)
|
||||
{
|
||||
int ret = 0;
|
||||
BIO *bio = NULL;
|
||||
|
||||
bio = BIO_new(BIO_s_socket());
|
||||
|
||||
if (bio == NULL) {
|
||||
SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
|
||||
goto err;
|
||||
}
|
||||
BIO_set_fd(bio, fd, BIO_NOCLOSE);
|
||||
SSL_set_bio(s, bio, bio);
|
||||
#ifndef OPENSSL_NO_KTLS
|
||||
/*
|
||||
* The new socket is created successfully regardless of ktls_enable.
|
||||
* ktls_enable doesn't change any functionality of the socket, except
|
||||
* changing the setsockopt to enable the processing of ktls_start.
|
||||
* Thus, it is not a problem to call it for non-TLS sockets.
|
||||
*/
|
||||
ktls_enable(fd);
|
||||
#endif /* OPENSSL_NO_KTLS */
|
||||
ret = 1;
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_set_wfd(SSL *s, int fd)
|
||||
{
|
||||
BIO *rbio = SSL_get_rbio(s);
|
||||
|
||||
if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET
|
||||
|| (int)BIO_get_fd(rbio, NULL) != fd) {
|
||||
BIO *bio = BIO_new(BIO_s_socket());
|
||||
|
||||
if (bio == NULL) {
|
||||
SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fd(bio, fd, BIO_NOCLOSE);
|
||||
SSL_set0_wbio(s, bio);
|
||||
#ifndef OPENSSL_NO_KTLS
|
||||
/*
|
||||
* The new socket is created successfully regardless of ktls_enable.
|
||||
* ktls_enable doesn't change any functionality of the socket, except
|
||||
* changing the setsockopt to enable the processing of ktls_start.
|
||||
* Thus, it is not a problem to call it for non-TLS sockets.
|
||||
*/
|
||||
ktls_enable(fd);
|
||||
#endif /* OPENSSL_NO_KTLS */
|
||||
} else {
|
||||
BIO_up_ref(rbio);
|
||||
SSL_set0_wbio(s, rbio);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_set_rfd(SSL *s, int fd)
|
||||
{
|
||||
BIO *wbio = SSL_get_wbio(s);
|
||||
|
||||
if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET
|
||||
|| ((int)BIO_get_fd(wbio, NULL) != fd)) {
|
||||
BIO *bio = BIO_new(BIO_s_socket());
|
||||
|
||||
if (bio == NULL) {
|
||||
SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB);
|
||||
return 0;
|
||||
}
|
||||
BIO_set_fd(bio, fd, BIO_NOCLOSE);
|
||||
SSL_set0_rbio(s, bio);
|
||||
} else {
|
||||
BIO_up_ref(wbio);
|
||||
SSL_set0_rbio(s, wbio);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* return length of latest Finished message we sent, copy to 'buf' */
|
||||
size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
|
||||
{
|
||||
size_t ret = 0;
|
||||
|
||||
ret = s->s3.tmp.finish_md_len;
|
||||
if (count > ret)
|
||||
count = ret;
|
||||
memcpy(buf, s->s3.tmp.finish_md, count);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* return length of latest Finished message we expected, copy to 'buf' */
|
||||
size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
|
||||
{
|
||||
size_t ret = 0;
|
||||
|
||||
ret = s->s3.tmp.peer_finish_md_len;
|
||||
if (count > ret)
|
||||
count = ret;
|
||||
memcpy(buf, s->s3.tmp.peer_finish_md, count);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_get_verify_mode(const SSL *s)
|
||||
{
|
||||
return s->verify_mode;
|
||||
}
|
||||
|
||||
int SSL_get_verify_depth(const SSL *s)
|
||||
{
|
||||
return X509_VERIFY_PARAM_get_depth(s->param);
|
||||
}
|
||||
|
||||
int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
|
||||
return s->verify_callback;
|
||||
}
|
||||
|
||||
int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->verify_mode;
|
||||
}
|
||||
|
||||
int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
|
||||
{
|
||||
return X509_VERIFY_PARAM_get_depth(ctx->param);
|
||||
}
|
||||
|
||||
int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
|
||||
return ctx->default_verify_callback;
|
||||
}
|
||||
|
||||
void SSL_set_verify(SSL *s, int mode,
|
||||
int (*callback) (int ok, X509_STORE_CTX *ctx))
|
||||
{
|
||||
s->verify_mode = mode;
|
||||
if (callback != NULL)
|
||||
s->verify_callback = callback;
|
||||
}
|
||||
|
||||
void SSL_set_verify_depth(SSL *s, int depth)
|
||||
{
|
||||
X509_VERIFY_PARAM_set_depth(s->param, depth);
|
||||
}
|
||||
|
||||
void SSL_set_read_ahead(SSL *s, int yes)
|
||||
{
|
||||
RECORD_LAYER_set_read_ahead(&s->rlayer, yes);
|
||||
}
|
||||
|
||||
int SSL_get_read_ahead(const SSL *s)
|
||||
{
|
||||
return RECORD_LAYER_get_read_ahead(&s->rlayer);
|
||||
}
|
||||
|
||||
int SSL_pending(const SSL *s)
|
||||
{
|
||||
size_t pending = s->method->ssl_pending(s);
|
||||
|
||||
/*
|
||||
* SSL_pending cannot work properly if read-ahead is enabled
|
||||
* (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
|
||||
* impossible to fix since SSL_pending cannot report errors that may be
|
||||
* observed while scanning the new data. (Note that SSL_pending() is
|
||||
* often used as a boolean value, so we'd better not return -1.)
|
||||
*
|
||||
* SSL_pending also cannot work properly if the value >INT_MAX. In that case
|
||||
* we just return INT_MAX.
|
||||
*/
|
||||
return pending < INT_MAX ? (int)pending : INT_MAX;
|
||||
}
|
||||
|
||||
int SSL_has_pending(const SSL *s)
|
||||
{
|
||||
/*
|
||||
* Similar to SSL_pending() but returns a 1 to indicate that we have
|
||||
* unprocessed data available or 0 otherwise (as opposed to the number of
|
||||
* bytes available). Unlike SSL_pending() this will take into account
|
||||
* read_ahead data. A 1 return simply indicates that we have unprocessed
|
||||
* data. That data may not result in any application data, or we may fail
|
||||
* to parse the records for some reason.
|
||||
*/
|
||||
if (RECORD_LAYER_processed_read_pending(&s->rlayer))
|
||||
return 1;
|
||||
|
||||
return RECORD_LAYER_read_pending(&s->rlayer);
|
||||
}
|
||||
|
||||
X509 *SSL_get_peer_certificate(const SSL *s)
|
||||
{
|
||||
X509 *r;
|
||||
|
||||
if ((s == NULL) || (s->session == NULL))
|
||||
r = NULL;
|
||||
else
|
||||
r = s->session->peer;
|
||||
|
||||
if (r == NULL)
|
||||
return r;
|
||||
|
||||
X509_up_ref(r);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
|
||||
{
|
||||
STACK_OF(X509) *r;
|
||||
|
||||
if ((s == NULL) || (s->session == NULL))
|
||||
r = NULL;
|
||||
else
|
||||
r = s->session->peer_chain;
|
||||
|
||||
/*
|
||||
* If we are a client, cert_chain includes the peer's own certificate; if
|
||||
* we are a server, it does not.
|
||||
*/
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/*
|
||||
* Now in theory, since the calling process own 't' it should be safe to
|
||||
* modify. We need to be able to read f without being hassled
|
||||
*/
|
||||
int SSL_copy_session_id(SSL *t, const SSL *f)
|
||||
{
|
||||
int i;
|
||||
/* Do we need to to SSL locking? */
|
||||
if (!SSL_set_session(t, SSL_get_session(f))) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* what if we are setup for one protocol version but want to talk another
|
||||
*/
|
||||
if (t->method != f->method) {
|
||||
t->method->ssl_free(t);
|
||||
t->method = f->method;
|
||||
if (t->method->ssl_new(t) == 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
CRYPTO_UP_REF(&f->cert->references, &i, f->cert->lock);
|
||||
ssl_cert_free(t->cert);
|
||||
t->cert = f->cert;
|
||||
if (!SSL_set_session_id_context(t, f->sid_ctx, (int)f->sid_ctx_length)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Fix this so it checks all the valid key/cert options */
|
||||
int SSL_CTX_check_private_key(const SSL_CTX *ctx)
|
||||
{
|
||||
if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
|
||||
SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
|
||||
return 0;
|
||||
}
|
||||
if (ctx->cert->key->privatekey == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
|
||||
return 0;
|
||||
}
|
||||
return X509_check_private_key
|
||||
(ctx->cert->key->x509, ctx->cert->key->privatekey);
|
||||
}
|
||||
|
||||
/* Fix this function so that it takes an optional type parameter */
|
||||
int SSL_check_private_key(const SSL *ssl)
|
||||
{
|
||||
if (ssl == NULL) {
|
||||
SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (ssl->cert->key->x509 == NULL) {
|
||||
SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
|
||||
return 0;
|
||||
}
|
||||
if (ssl->cert->key->privatekey == NULL) {
|
||||
SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
|
||||
return 0;
|
||||
}
|
||||
return X509_check_private_key(ssl->cert->key->x509,
|
||||
ssl->cert->key->privatekey);
|
||||
}
|
||||
|
||||
int SSL_waiting_for_async(SSL *s)
|
||||
{
|
||||
if (s->job)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
|
||||
{
|
||||
ASYNC_WAIT_CTX *ctx = s->waitctx;
|
||||
|
||||
if (ctx == NULL)
|
||||
return 0;
|
||||
return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
|
||||
}
|
||||
|
||||
int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
|
||||
OSSL_ASYNC_FD *delfd, size_t *numdelfds)
|
||||
{
|
||||
ASYNC_WAIT_CTX *ctx = s->waitctx;
|
||||
|
||||
if (ctx == NULL)
|
||||
return 0;
|
||||
return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
|
||||
numdelfds);
|
||||
}
|
||||
|
||||
int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
|
||||
{
|
||||
ctx->async_cb = callback;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
|
||||
{
|
||||
ctx->async_cb_arg = arg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
|
||||
{
|
||||
s->async_cb = callback;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_set_async_callback_arg(SSL *s, void *arg)
|
||||
{
|
||||
s->async_cb_arg = arg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_get_async_status(SSL *s, int *status)
|
||||
{
|
||||
ASYNC_WAIT_CTX *ctx = s->waitctx;
|
||||
|
||||
if (ctx == NULL)
|
||||
return 0;
|
||||
*status = ASYNC_WAIT_CTX_get_status(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_accept(SSL *s)
|
||||
{
|
||||
if (s->handshake_func == NULL) {
|
||||
/* Not properly initialized yet */
|
||||
SSL_set_accept_state(s);
|
||||
}
|
||||
|
||||
return SSL_do_handshake(s);
|
||||
}
|
||||
|
||||
int SSL_connect(SSL *s)
|
||||
{
|
||||
if (s->handshake_func == NULL) {
|
||||
/* Not properly initialized yet */
|
||||
SSL_set_connect_state(s);
|
||||
}
|
||||
|
||||
return SSL_do_handshake(s);
|
||||
}
|
||||
|
||||
long SSL_get_default_timeout(const SSL *s)
|
||||
{
|
||||
return s->method->get_timeout();
|
||||
}
|
||||
|
||||
static int ssl_async_wait_ctx_cb(void *arg)
|
||||
{
|
||||
SSL *s = (SSL *)arg;
|
||||
|
||||
return s->async_cb(s, s->async_cb_arg);
|
||||
}
|
||||
|
||||
static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
|
||||
int (*func) (void *))
|
||||
{
|
||||
int ret;
|
||||
if (s->waitctx == NULL) {
|
||||
s->waitctx = ASYNC_WAIT_CTX_new();
|
||||
if (s->waitctx == NULL)
|
||||
return -1;
|
||||
if (s->async_cb != NULL
|
||||
&& !ASYNC_WAIT_CTX_set_callback
|
||||
(s->waitctx, ssl_async_wait_ctx_cb, s))
|
||||
return -1;
|
||||
}
|
||||
switch (ASYNC_start_job(&s->job, s->waitctx, &ret, func, args,
|
||||
sizeof(struct ssl_async_args))) {
|
||||
case ASYNC_ERR:
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSLerr(SSL_F_SSL_START_ASYNC_JOB, SSL_R_FAILED_TO_INIT_ASYNC);
|
||||
return -1;
|
||||
case ASYNC_PAUSE:
|
||||
s->rwstate = SSL_ASYNC_PAUSED;
|
||||
return -1;
|
||||
case ASYNC_NO_JOBS:
|
||||
s->rwstate = SSL_ASYNC_NO_JOBS;
|
||||
return -1;
|
||||
case ASYNC_FINISH:
|
||||
s->job = NULL;
|
||||
return ret;
|
||||
default:
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSLerr(SSL_F_SSL_START_ASYNC_JOB, ERR_R_INTERNAL_ERROR);
|
||||
/* Shouldn't happen */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int ssl_io_intern(void *vargs)
|
||||
{
|
||||
struct ssl_async_args *args;
|
||||
SSL *s;
|
||||
void *buf;
|
||||
size_t num;
|
||||
|
||||
args = (struct ssl_async_args *)vargs;
|
||||
s = args->s;
|
||||
buf = args->buf;
|
||||
num = args->num;
|
||||
switch (args->type) {
|
||||
case READFUNC:
|
||||
return args->f.func_read(s, buf, num, &s->asyncrw);
|
||||
case WRITEFUNC:
|
||||
return args->f.func_write(s, buf, num, &s->asyncrw);
|
||||
case OTHERFUNC:
|
||||
return args->f.func_other(s);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
|
||||
{
|
||||
if (s->handshake_func == NULL) {
|
||||
SSLerr(SSL_F_SSL_READ_INTERNAL, SSL_R_UNINITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
|
||||
|| s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
|
||||
SSLerr(SSL_F_SSL_READ_INTERNAL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* If we are a client and haven't received the ServerHello etc then we
|
||||
* better do that
|
||||
*/
|
||||
ossl_statem_check_finish_init(s, 0);
|
||||
|
||||
if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
|
||||
struct ssl_async_args args;
|
||||
int ret;
|
||||
|
||||
args.s = s;
|
||||
args.buf = buf;
|
||||
args.num = num;
|
||||
args.type = READFUNC;
|
||||
args.f.func_read = s->method->ssl_read;
|
||||
|
||||
ret = ssl_start_async_job(s, &args, ssl_io_intern);
|
||||
*readbytes = s->asyncrw;
|
||||
return ret;
|
||||
} else {
|
||||
return s->method->ssl_read(s, buf, num, readbytes);
|
||||
}
|
||||
}
|
||||
|
||||
int SSL_read(SSL *s, void *buf, int num)
|
||||
{
|
||||
int ret;
|
||||
size_t readbytes;
|
||||
|
||||
if (num < 0) {
|
||||
SSLerr(SSL_F_SSL_READ, SSL_R_BAD_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
|
||||
|
||||
/*
|
||||
* The cast is safe here because ret should be <= INT_MAX because num is
|
||||
* <= INT_MAX
|
||||
*/
|
||||
if (ret > 0)
|
||||
ret = (int)readbytes;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
|
||||
{
|
||||
int ret = ssl_read_internal(s, buf, num, readbytes);
|
||||
|
||||
if (ret < 0)
|
||||
ret = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!s->server) {
|
||||
SSLerr(SSL_F_SSL_READ_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return SSL_READ_EARLY_DATA_ERROR;
|
||||
}
|
||||
|
||||
switch (s->early_data_state) {
|
||||
case SSL_EARLY_DATA_NONE:
|
||||
if (!SSL_in_before(s)) {
|
||||
SSLerr(SSL_F_SSL_READ_EARLY_DATA,
|
||||
ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return SSL_READ_EARLY_DATA_ERROR;
|
||||
}
|
||||
/* fall through */
|
||||
|
||||
case SSL_EARLY_DATA_ACCEPT_RETRY:
|
||||
s->early_data_state = SSL_EARLY_DATA_ACCEPTING;
|
||||
ret = SSL_accept(s);
|
||||
if (ret <= 0) {
|
||||
/* NBIO or error */
|
||||
s->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
|
||||
return SSL_READ_EARLY_DATA_ERROR;
|
||||
}
|
||||
/* fall through */
|
||||
|
||||
case SSL_EARLY_DATA_READ_RETRY:
|
||||
if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
|
||||
s->early_data_state = SSL_EARLY_DATA_READING;
|
||||
ret = SSL_read_ex(s, buf, num, readbytes);
|
||||
/*
|
||||
* State machine will update early_data_state to
|
||||
* SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
|
||||
* message
|
||||
*/
|
||||
if (ret > 0 || (ret <= 0 && s->early_data_state
|
||||
!= SSL_EARLY_DATA_FINISHED_READING)) {
|
||||
s->early_data_state = SSL_EARLY_DATA_READ_RETRY;
|
||||
return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
|
||||
: SSL_READ_EARLY_DATA_ERROR;
|
||||
}
|
||||
} else {
|
||||
s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
|
||||
}
|
||||
*readbytes = 0;
|
||||
return SSL_READ_EARLY_DATA_FINISH;
|
||||
|
||||
default:
|
||||
SSLerr(SSL_F_SSL_READ_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return SSL_READ_EARLY_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
int SSL_get_early_data_status(const SSL *s)
|
||||
{
|
||||
return s->ext.early_data;
|
||||
}
|
||||
|
||||
static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
|
||||
{
|
||||
if (s->handshake_func == NULL) {
|
||||
SSLerr(SSL_F_SSL_PEEK_INTERNAL, SSL_R_UNINITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
||||
return 0;
|
||||
}
|
||||
if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
|
||||
struct ssl_async_args args;
|
||||
int ret;
|
||||
|
||||
args.s = s;
|
||||
args.buf = buf;
|
||||
args.num = num;
|
||||
args.type = READFUNC;
|
||||
args.f.func_read = s->method->ssl_peek;
|
||||
|
||||
ret = ssl_start_async_job(s, &args, ssl_io_intern);
|
||||
*readbytes = s->asyncrw;
|
||||
return ret;
|
||||
} else {
|
||||
return s->method->ssl_peek(s, buf, num, readbytes);
|
||||
}
|
||||
}
|
||||
|
||||
int SSL_peek(SSL *s, void *buf, int num)
|
||||
{
|
||||
int ret;
|
||||
size_t readbytes;
|
||||
|
||||
if (num < 0) {
|
||||
SSLerr(SSL_F_SSL_PEEK, SSL_R_BAD_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
|
||||
|
||||
/*
|
||||
* The cast is safe here because ret should be <= INT_MAX because num is
|
||||
* <= INT_MAX
|
||||
*/
|
||||
if (ret > 0)
|
||||
ret = (int)readbytes;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
|
||||
{
|
||||
int ret = ssl_peek_internal(s, buf, num, readbytes);
|
||||
|
||||
if (ret < 0)
|
||||
ret = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written)
|
||||
{
|
||||
if (s->handshake_func == NULL) {
|
||||
SSLerr(SSL_F_SSL_WRITE_INTERNAL, SSL_R_UNINITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (s->shutdown & SSL_SENT_SHUTDOWN) {
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSLerr(SSL_F_SSL_WRITE_INTERNAL, SSL_R_PROTOCOL_IS_SHUTDOWN);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
|
||||
|| s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
|
||||
|| s->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
|
||||
SSLerr(SSL_F_SSL_WRITE_INTERNAL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
/* If we are a client and haven't sent the Finished we better do that */
|
||||
ossl_statem_check_finish_init(s, 1);
|
||||
|
||||
if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
|
||||
int ret;
|
||||
struct ssl_async_args args;
|
||||
|
||||
args.s = s;
|
||||
args.buf = (void *)buf;
|
||||
args.num = num;
|
||||
args.type = WRITEFUNC;
|
||||
args.f.func_write = s->method->ssl_write;
|
||||
|
||||
ret = ssl_start_async_job(s, &args, ssl_io_intern);
|
||||
*written = s->asyncrw;
|
||||
return ret;
|
||||
} else {
|
||||
return s->method->ssl_write(s, buf, num, written);
|
||||
}
|
||||
}
|
||||
|
||||
ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
|
||||
{
|
||||
ossl_ssize_t ret;
|
||||
|
||||
if (s->handshake_func == NULL) {
|
||||
SSLerr(SSL_F_SSL_SENDFILE, SSL_R_UNINITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (s->shutdown & SSL_SENT_SHUTDOWN) {
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSLerr(SSL_F_SSL_SENDFILE, SSL_R_PROTOCOL_IS_SHUTDOWN);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!BIO_get_ktls_send(s->wbio)) {
|
||||
SSLerr(SSL_F_SSL_SENDFILE, SSL_R_UNINITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If we have an alert to send, lets send it */
|
||||
if (s->s3.alert_dispatch) {
|
||||
ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
|
||||
if (ret <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
return ret;
|
||||
}
|
||||
/* if it went, fall through and send more stuff */
|
||||
}
|
||||
|
||||
s->rwstate = SSL_WRITING;
|
||||
if (BIO_flush(s->wbio) <= 0) {
|
||||
if (!BIO_should_retry(s->wbio)) {
|
||||
s->rwstate = SSL_NOTHING;
|
||||
} else {
|
||||
#ifdef EAGAIN
|
||||
set_sys_error(EAGAIN);
|
||||
#endif
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef OPENSSL_NO_KTLS
|
||||
ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR, "calling sendfile()");
|
||||
return -1;
|
||||
#else
|
||||
ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
|
||||
if (ret < 0) {
|
||||
#if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
|
||||
if ((get_last_sys_error() == EAGAIN) ||
|
||||
(get_last_sys_error() == EINTR) ||
|
||||
(get_last_sys_error() == EBUSY))
|
||||
BIO_set_retry_write(s->wbio);
|
||||
else
|
||||
#endif
|
||||
SSLerr(SSL_F_SSL_SENDFILE, SSL_R_UNINITIALIZED);
|
||||
return ret;
|
||||
}
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
int SSL_write(SSL *s, const void *buf, int num)
|
||||
{
|
||||
int ret;
|
||||
size_t written;
|
||||
|
||||
if (num < 0) {
|
||||
SSLerr(SSL_F_SSL_WRITE, SSL_R_BAD_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssl_write_internal(s, buf, (size_t)num, &written);
|
||||
|
||||
/*
|
||||
* The cast is safe here because ret should be <= INT_MAX because num is
|
||||
* <= INT_MAX
|
||||
*/
|
||||
if (ret > 0)
|
||||
ret = (int)written;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
|
||||
{
|
||||
int ret = ssl_write_internal(s, buf, num, written);
|
||||
|
||||
if (ret < 0)
|
||||
ret = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
|
||||
{
|
||||
int ret, early_data_state;
|
||||
size_t writtmp;
|
||||
uint32_t partialwrite;
|
||||
|
||||
switch (s->early_data_state) {
|
||||
case SSL_EARLY_DATA_NONE:
|
||||
if (s->server
|
||||
|| !SSL_in_before(s)
|
||||
|| ((s->session == NULL || s->session->ext.max_early_data == 0)
|
||||
&& (s->psk_use_session_cb == NULL))) {
|
||||
SSLerr(SSL_F_SSL_WRITE_EARLY_DATA,
|
||||
ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
/* fall through */
|
||||
|
||||
case SSL_EARLY_DATA_CONNECT_RETRY:
|
||||
s->early_data_state = SSL_EARLY_DATA_CONNECTING;
|
||||
ret = SSL_connect(s);
|
||||
if (ret <= 0) {
|
||||
/* NBIO or error */
|
||||
s->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
|
||||
return 0;
|
||||
}
|
||||
/* fall through */
|
||||
|
||||
case SSL_EARLY_DATA_WRITE_RETRY:
|
||||
s->early_data_state = SSL_EARLY_DATA_WRITING;
|
||||
/*
|
||||
* We disable partial write for early data because we don't keep track
|
||||
* of how many bytes we've written between the SSL_write_ex() call and
|
||||
* the flush if the flush needs to be retried)
|
||||
*/
|
||||
partialwrite = s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
|
||||
s->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
|
||||
ret = SSL_write_ex(s, buf, num, &writtmp);
|
||||
s->mode |= partialwrite;
|
||||
if (!ret) {
|
||||
s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
|
||||
return ret;
|
||||
}
|
||||
s->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
|
||||
/* fall through */
|
||||
|
||||
case SSL_EARLY_DATA_WRITE_FLUSH:
|
||||
/* The buffering BIO is still in place so we need to flush it */
|
||||
if (statem_flush(s) != 1)
|
||||
return 0;
|
||||
*written = num;
|
||||
s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
|
||||
return 1;
|
||||
|
||||
case SSL_EARLY_DATA_FINISHED_READING:
|
||||
case SSL_EARLY_DATA_READ_RETRY:
|
||||
early_data_state = s->early_data_state;
|
||||
/* We are a server writing to an unauthenticated client */
|
||||
s->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
|
||||
ret = SSL_write_ex(s, buf, num, written);
|
||||
/* The buffering BIO is still in place */
|
||||
if (ret)
|
||||
(void)BIO_flush(s->wbio);
|
||||
s->early_data_state = early_data_state;
|
||||
return ret;
|
||||
|
||||
default:
|
||||
SSLerr(SSL_F_SSL_WRITE_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int SSL_shutdown(SSL *s)
|
||||
{
|
||||
/*
|
||||
* Note that this function behaves differently from what one might
|
||||
* expect. Return values are 0 for no success (yet), 1 for success; but
|
||||
* calling it once is usually not enough, even if blocking I/O is used
|
||||
* (see ssl3_shutdown).
|
||||
*/
|
||||
|
||||
if (s->handshake_func == NULL) {
|
||||
SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!SSL_in_init(s)) {
|
||||
if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
|
||||
struct ssl_async_args args;
|
||||
|
||||
args.s = s;
|
||||
args.type = OTHERFUNC;
|
||||
args.f.func_other = s->method->ssl_shutdown;
|
||||
|
||||
return ssl_start_async_job(s, &args, ssl_io_intern);
|
||||
} else {
|
||||
return s->method->ssl_shutdown(s);
|
||||
}
|
||||
} else {
|
||||
SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_SHUTDOWN_WHILE_IN_INIT);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int SSL_key_update(SSL *s, int updatetype)
|
||||
{
|
||||
/*
|
||||
* TODO(TLS1.3): How will applications know whether TLSv1.3 has been
|
||||
* negotiated, and that it is appropriate to call SSL_key_update() instead
|
||||
* of SSL_renegotiate().
|
||||
*/
|
||||
if (!SSL_IS_TLS13(s)) {
|
||||
SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_WRONG_SSL_VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
|
||||
&& updatetype != SSL_KEY_UPDATE_REQUESTED) {
|
||||
SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_INVALID_KEY_UPDATE_TYPE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!SSL_is_init_finished(s)) {
|
||||
SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_STILL_IN_INIT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ossl_statem_set_in_init(s, 1);
|
||||
s->key_update = updatetype;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_get_key_update_type(const SSL *s)
|
||||
{
|
||||
return s->key_update;
|
||||
}
|
||||
|
||||
int SSL_renegotiate(SSL *s)
|
||||
{
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
SSLerr(SSL_F_SSL_RENEGOTIATE, SSL_R_WRONG_SSL_VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
|
||||
SSLerr(SSL_F_SSL_RENEGOTIATE, SSL_R_NO_RENEGOTIATION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
s->renegotiate = 1;
|
||||
s->new_session = 1;
|
||||
|
||||
return s->method->ssl_renegotiate(s);
|
||||
}
|
||||
|
||||
int SSL_renegotiate_abbreviated(SSL *s)
|
||||
{
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED, SSL_R_WRONG_SSL_VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
|
||||
SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED, SSL_R_NO_RENEGOTIATION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
s->renegotiate = 1;
|
||||
s->new_session = 0;
|
||||
|
||||
return s->method->ssl_renegotiate(s);
|
||||
}
|
||||
|
||||
int SSL_renegotiate_pending(const SSL *s)
|
||||
{
|
||||
/*
|
||||
* becomes true when negotiation is requested; false again once a
|
||||
* handshake has finished
|
||||
*/
|
||||
return (s->renegotiate != 0);
|
||||
}
|
||||
|
||||
long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
|
||||
{
|
||||
long l;
|
||||
|
||||
switch (cmd) {
|
||||
case SSL_CTRL_GET_READ_AHEAD:
|
||||
return RECORD_LAYER_get_read_ahead(&s->rlayer);
|
||||
case SSL_CTRL_SET_READ_AHEAD:
|
||||
l = RECORD_LAYER_get_read_ahead(&s->rlayer);
|
||||
RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
|
||||
return l;
|
||||
|
||||
case SSL_CTRL_SET_MSG_CALLBACK_ARG:
|
||||
s->msg_callback_arg = parg;
|
||||
return 1;
|
||||
|
||||
case SSL_CTRL_MODE:
|
||||
return (s->mode |= larg);
|
||||
case SSL_CTRL_CLEAR_MODE:
|
||||
return (s->mode &= ~larg);
|
||||
case SSL_CTRL_GET_MAX_CERT_LIST:
|
||||
return (long)s->max_cert_list;
|
||||
case SSL_CTRL_SET_MAX_CERT_LIST:
|
||||
if (larg < 0)
|
||||
return 0;
|
||||
l = (long)s->max_cert_list;
|
||||
s->max_cert_list = (size_t)larg;
|
||||
return l;
|
||||
case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
|
||||
if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
|
||||
return 0;
|
||||
#ifndef OPENSSL_NO_KTLS
|
||||
if (s->wbio != NULL && BIO_get_ktls_send(s->wbio))
|
||||
return 0;
|
||||
#endif /* OPENSSL_NO_KTLS */
|
||||
s->max_send_fragment = larg;
|
||||
if (s->max_send_fragment < s->split_send_fragment)
|
||||
s->split_send_fragment = s->max_send_fragment;
|
||||
return 1;
|
||||
case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
|
||||
if ((size_t)larg > s->max_send_fragment || larg == 0)
|
||||
return 0;
|
||||
s->split_send_fragment = larg;
|
||||
return 1;
|
||||
case SSL_CTRL_SET_MAX_PIPELINES:
|
||||
if (larg < 1 || larg > SSL_MAX_PIPELINES)
|
||||
return 0;
|
||||
s->max_pipelines = larg;
|
||||
if (larg > 1)
|
||||
RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
|
||||
return 1;
|
||||
case SSL_CTRL_GET_RI_SUPPORT:
|
||||
return s->s3.send_connection_binding;
|
||||
case SSL_CTRL_CERT_FLAGS:
|
||||
return (s->cert->cert_flags |= larg);
|
||||
case SSL_CTRL_CLEAR_CERT_FLAGS:
|
||||
return (s->cert->cert_flags &= ~larg);
|
||||
|
||||
case SSL_CTRL_GET_RAW_CIPHERLIST:
|
||||
if (parg) {
|
||||
if (s->s3.tmp.ciphers_raw == NULL)
|
||||
return 0;
|
||||
*(unsigned char **)parg = s->s3.tmp.ciphers_raw;
|
||||
return (int)s->s3.tmp.ciphers_rawlen;
|
||||
} else {
|
||||
return TLS_CIPHER_LEN;
|
||||
}
|
||||
case SSL_CTRL_GET_EXTMS_SUPPORT:
|
||||
if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s))
|
||||
return -1;
|
||||
if (s->session->flags & SSL_SESS_FLAG_EXTMS)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
case SSL_CTRL_SET_MIN_PROTO_VERSION:
|
||||
return ssl_check_allowed_versions(larg, s->max_proto_version)
|
||||
&& ssl_set_version_bound(s->ctx->method->version, (int)larg,
|
||||
&s->min_proto_version);
|
||||
case SSL_CTRL_GET_MIN_PROTO_VERSION:
|
||||
return s->min_proto_version;
|
||||
case SSL_CTRL_SET_MAX_PROTO_VERSION:
|
||||
return ssl_check_allowed_versions(s->min_proto_version, larg)
|
||||
&& ssl_set_version_bound(s->ctx->method->version, (int)larg,
|
||||
&s->max_proto_version);
|
||||
case SSL_CTRL_GET_MAX_PROTO_VERSION:
|
||||
return s->max_proto_version;
|
||||
default:
|
||||
return s->method->ssl_ctrl(s, cmd, larg, parg);
|
||||
}
|
||||
}
|
||||
|
||||
long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
|
||||
{
|
||||
switch (cmd) {
|
||||
case SSL_CTRL_SET_MSG_CALLBACK:
|
||||
s->msg_callback = (void (*)
|
||||
(int write_p, int version, int content_type,
|
||||
const void *buf, size_t len, SSL *ssl,
|
||||
void *arg))(fp);
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return s->method->ssl_callback_ctrl(s, cmd, fp);
|
||||
}
|
||||
}
|
||||
|
||||
LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->sessions;
|
||||
}
|
||||
|
||||
long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
|
||||
{
|
||||
long l;
|
||||
/* For some cases with ctx == NULL perform syntax checks */
|
||||
if (ctx == NULL) {
|
||||
switch (cmd) {
|
||||
#ifndef OPENSSL_NO_EC
|
||||
case SSL_CTRL_SET_GROUPS_LIST:
|
||||
return tls1_set_groups_list(NULL, NULL, parg);
|
||||
#endif
|
||||
case SSL_CTRL_SET_SIGALGS_LIST:
|
||||
case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
|
||||
return tls1_set_sigalgs_list(NULL, parg, 0);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
switch (cmd) {
|
||||
case SSL_CTRL_GET_READ_AHEAD:
|
||||
return ctx->read_ahead;
|
||||
case SSL_CTRL_SET_READ_AHEAD:
|
||||
l = ctx->read_ahead;
|
||||
ctx->read_ahead = larg;
|
||||
return l;
|
||||
|
||||
case SSL_CTRL_SET_MSG_CALLBACK_ARG:
|
||||
ctx->msg_callback_arg = parg;
|
||||
return 1;
|
||||
|
||||
case SSL_CTRL_GET_MAX_CERT_LIST:
|
||||
return (long)ctx->max_cert_list;
|
||||
case SSL_CTRL_SET_MAX_CERT_LIST:
|
||||
if (larg < 0)
|
||||
return 0;
|
||||
l = (long)ctx->max_cert_list;
|
||||
ctx->max_cert_list = (size_t)larg;
|
||||
return l;
|
||||
|
||||
case SSL_CTRL_SET_SESS_CACHE_SIZE:
|
||||
if (larg < 0)
|
||||
return 0;
|
||||
l = (long)ctx->session_cache_size;
|
||||
ctx->session_cache_size = (size_t)larg;
|
||||
return l;
|
||||
case SSL_CTRL_GET_SESS_CACHE_SIZE:
|
||||
return (long)ctx->session_cache_size;
|
||||
case SSL_CTRL_SET_SESS_CACHE_MODE:
|
||||
l = ctx->session_cache_mode;
|
||||
ctx->session_cache_mode = larg;
|
||||
return l;
|
||||
case SSL_CTRL_GET_SESS_CACHE_MODE:
|
||||
return ctx->session_cache_mode;
|
||||
|
||||
case SSL_CTRL_SESS_NUMBER:
|
||||
return lh_SSL_SESSION_num_items(ctx->sessions);
|
||||
case SSL_CTRL_SESS_CONNECT:
|
||||
return tsan_load(&ctx->stats.sess_connect);
|
||||
case SSL_CTRL_SESS_CONNECT_GOOD:
|
||||
return tsan_load(&ctx->stats.sess_connect_good);
|
||||
case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
|
||||
return tsan_load(&ctx->stats.sess_connect_renegotiate);
|
||||
case SSL_CTRL_SESS_ACCEPT:
|
||||
return tsan_load(&ctx->stats.sess_accept);
|
||||
case SSL_CTRL_SESS_ACCEPT_GOOD:
|
||||
return tsan_load(&ctx->stats.sess_accept_good);
|
||||
case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
|
||||
return tsan_load(&ctx->stats.sess_accept_renegotiate);
|
||||
case SSL_CTRL_SESS_HIT:
|
||||
return tsan_load(&ctx->stats.sess_hit);
|
||||
case SSL_CTRL_SESS_CB_HIT:
|
||||
return tsan_load(&ctx->stats.sess_cb_hit);
|
||||
case SSL_CTRL_SESS_MISSES:
|
||||
return tsan_load(&ctx->stats.sess_miss);
|
||||
case SSL_CTRL_SESS_TIMEOUTS:
|
||||
return tsan_load(&ctx->stats.sess_timeout);
|
||||
case SSL_CTRL_SESS_CACHE_FULL:
|
||||
return tsan_load(&ctx->stats.sess_cache_full);
|
||||
case SSL_CTRL_MODE:
|
||||
return (ctx->mode |= larg);
|
||||
case SSL_CTRL_CLEAR_MODE:
|
||||
return (ctx->mode &= ~larg);
|
||||
case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
|
||||
if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
|
||||
return 0;
|
||||
ctx->max_send_fragment = larg;
|
||||
if (ctx->max_send_fragment < ctx->split_send_fragment)
|
||||
ctx->split_send_fragment = ctx->max_send_fragment;
|
||||
return 1;
|
||||
case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
|
||||
if ((size_t)larg > ctx->max_send_fragment || larg == 0)
|
||||
return 0;
|
||||
ctx->split_send_fragment = larg;
|
||||
return 1;
|
||||
case SSL_CTRL_SET_MAX_PIPELINES:
|
||||
if (larg < 1 || larg > SSL_MAX_PIPELINES)
|
||||
return 0;
|
||||
ctx->max_pipelines = larg;
|
||||
return 1;
|
||||
case SSL_CTRL_CERT_FLAGS:
|
||||
return (ctx->cert->cert_flags |= larg);
|
||||
case SSL_CTRL_CLEAR_CERT_FLAGS:
|
||||
return (ctx->cert->cert_flags &= ~larg);
|
||||
case SSL_CTRL_SET_MIN_PROTO_VERSION:
|
||||
return ssl_check_allowed_versions(larg, ctx->max_proto_version)
|
||||
&& ssl_set_version_bound(ctx->method->version, (int)larg,
|
||||
&ctx->min_proto_version);
|
||||
case SSL_CTRL_GET_MIN_PROTO_VERSION:
|
||||
return ctx->min_proto_version;
|
||||
case SSL_CTRL_SET_MAX_PROTO_VERSION:
|
||||
return ssl_check_allowed_versions(ctx->min_proto_version, larg)
|
||||
&& ssl_set_version_bound(ctx->method->version, (int)larg,
|
||||
&ctx->max_proto_version);
|
||||
case SSL_CTRL_GET_MAX_PROTO_VERSION:
|
||||
return ctx->max_proto_version;
|
||||
default:
|
||||
return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
|
||||
}
|
||||
}
|
||||
|
||||
long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
|
||||
{
|
||||
switch (cmd) {
|
||||
case SSL_CTRL_SET_MSG_CALLBACK:
|
||||
ctx->msg_callback = (void (*)
|
||||
(int write_p, int version, int content_type,
|
||||
const void *buf, size_t len, SSL *ssl,
|
||||
void *arg))(fp);
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
|
||||
}
|
||||
}
|
||||
|
||||
int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
|
||||
{
|
||||
if (a->id > b->id)
|
||||
return 1;
|
||||
if (a->id < b->id)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
|
||||
const SSL_CIPHER *const *bp)
|
||||
{
|
||||
if ((*ap)->id > (*bp)->id)
|
||||
return 1;
|
||||
if ((*ap)->id < (*bp)->id)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** return a STACK of the ciphers available for the SSL and in order of
|
||||
* preference */
|
||||
STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
|
||||
{
|
||||
if (s != NULL) {
|
||||
if (s->cipher_list != NULL) {
|
||||
return s->cipher_list;
|
||||
} else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
|
||||
return s->ctx->cipher_list;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
|
||||
{
|
||||
if ((s == NULL) || !s->server)
|
||||
return NULL;
|
||||
return s->peer_ciphers;
|
||||
}
|
||||
|
||||
STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
|
||||
{
|
||||
STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
|
||||
int i;
|
||||
|
||||
ciphers = SSL_get_ciphers(s);
|
||||
if (!ciphers)
|
||||
return NULL;
|
||||
if (!ssl_set_client_disabled(s))
|
||||
return NULL;
|
||||
for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
|
||||
const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
|
||||
if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
|
||||
if (!sk)
|
||||
sk = sk_SSL_CIPHER_new_null();
|
||||
if (!sk)
|
||||
return NULL;
|
||||
if (!sk_SSL_CIPHER_push(sk, c)) {
|
||||
sk_SSL_CIPHER_free(sk);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sk;
|
||||
}
|
||||
|
||||
/** return a STACK of the ciphers available for the SSL and in order of
|
||||
* algorithm id */
|
||||
STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
|
||||
{
|
||||
if (s != NULL) {
|
||||
if (s->cipher_list_by_id != NULL) {
|
||||
return s->cipher_list_by_id;
|
||||
} else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
|
||||
return s->ctx->cipher_list_by_id;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** The old interface to get the same thing as SSL_get_ciphers() */
|
||||
const char *SSL_get_cipher_list(const SSL *s, int n)
|
||||
{
|
||||
const SSL_CIPHER *c;
|
||||
STACK_OF(SSL_CIPHER) *sk;
|
||||
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
sk = SSL_get_ciphers(s);
|
||||
if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
|
||||
return NULL;
|
||||
c = sk_SSL_CIPHER_value(sk, n);
|
||||
if (c == NULL)
|
||||
return NULL;
|
||||
return c->name;
|
||||
}
|
||||
|
||||
/** return a STACK of the ciphers available for the SSL_CTX and in order of
|
||||
* preference */
|
||||
STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
|
||||
{
|
||||
if (ctx != NULL)
|
||||
return ctx->cipher_list;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Distinguish between ciphers controlled by set_ciphersuite() and
|
||||
* set_cipher_list() when counting.
|
||||
*/
|
||||
static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
|
||||
{
|
||||
int i, num = 0;
|
||||
const SSL_CIPHER *c;
|
||||
|
||||
if (sk == NULL)
|
||||
return 0;
|
||||
for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
|
||||
c = sk_SSL_CIPHER_value(sk, i);
|
||||
if (c->min_tls >= TLS1_3_VERSION)
|
||||
continue;
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/** specify the ciphers to be used by default by the SSL_CTX */
|
||||
int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
|
||||
{
|
||||
STACK_OF(SSL_CIPHER) *sk;
|
||||
|
||||
sk = ssl_create_cipher_list(ctx->method, ctx->tls13_ciphersuites,
|
||||
&ctx->cipher_list, &ctx->cipher_list_by_id, str,
|
||||
ctx->cert);
|
||||
/*
|
||||
* ssl_create_cipher_list may return an empty stack if it was unable to
|
||||
* find a cipher matching the given rule string (for example if the rule
|
||||
* string specifies a cipher which has been disabled). This is not an
|
||||
* error as far as ssl_create_cipher_list is concerned, and hence
|
||||
* ctx->cipher_list and ctx->cipher_list_by_id has been updated.
|
||||
*/
|
||||
if (sk == NULL)
|
||||
return 0;
|
||||
else if (cipher_list_tls12_num(sk) == 0) {
|
||||
SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/** specify the ciphers to be used by the SSL */
|
||||
int SSL_set_cipher_list(SSL *s, const char *str)
|
||||
{
|
||||
STACK_OF(SSL_CIPHER) *sk;
|
||||
|
||||
sk = ssl_create_cipher_list(s->ctx->method, s->tls13_ciphersuites,
|
||||
&s->cipher_list, &s->cipher_list_by_id, str,
|
||||
s->cert);
|
||||
/* see comment in SSL_CTX_set_cipher_list */
|
||||
if (sk == NULL)
|
||||
return 0;
|
||||
else if (cipher_list_tls12_num(sk) == 0) {
|
||||
SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
|
||||
{
|
||||
char *p;
|
||||
STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
|
||||
const SSL_CIPHER *c;
|
||||
int i;
|
||||
|
||||
if (!s->server
|
||||
|| s->peer_ciphers == NULL
|
||||
|| size < 2)
|
||||
return NULL;
|
||||
|
||||
p = buf;
|
||||
clntsk = s->peer_ciphers;
|
||||
srvrsk = SSL_get_ciphers(s);
|
||||
if (clntsk == NULL || srvrsk == NULL)
|
||||
return NULL;
|
||||
|
||||
if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
|
||||
int n;
|
||||
|
||||
c = sk_SSL_CIPHER_value(clntsk, i);
|
||||
if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
|
||||
continue;
|
||||
|
||||
n = strlen(c->name);
|
||||
if (n + 1 > size) {
|
||||
if (p != buf)
|
||||
--p;
|
||||
*p = '\0';
|
||||
return buf;
|
||||
}
|
||||
strcpy(p, c->name);
|
||||
p += n;
|
||||
*(p++) = ':';
|
||||
size -= n + 1;
|
||||
}
|
||||
p[-1] = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/** return a servername extension value if provided in Client Hello, or NULL.
|
||||
* So far, only host_name types are defined (RFC 3546).
|
||||
*/
|
||||
|
||||
const char *SSL_get_servername(const SSL *s, const int type)
|
||||
{
|
||||
if (type != TLSEXT_NAMETYPE_host_name)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* SNI is not negotiated in pre-TLS-1.3 resumption flows, so fake up an
|
||||
* SNI value to return if we are resuming/resumed. N.B. that we still
|
||||
* call the relevant callbacks for such resumption flows, and callbacks
|
||||
* might error out if there is not a SNI value available.
|
||||
*/
|
||||
if (s->hit)
|
||||
return s->session->ext.hostname;
|
||||
return s->ext.hostname;
|
||||
}
|
||||
|
||||
int SSL_get_servername_type(const SSL *s)
|
||||
{
|
||||
if (s->session
|
||||
&& (!s->ext.hostname ? s->session->
|
||||
ext.hostname : s->ext.hostname))
|
||||
return TLSEXT_NAMETYPE_host_name;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* SSL_select_next_proto implements the standard protocol selection. It is
|
||||
* expected that this function is called from the callback set by
|
||||
* SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
|
||||
* vector of 8-bit, length prefixed byte strings. The length byte itself is
|
||||
* not included in the length. A byte string of length 0 is invalid. No byte
|
||||
* string may be truncated. The current, but experimental algorithm for
|
||||
* selecting the protocol is: 1) If the server doesn't support NPN then this
|
||||
* is indicated to the callback. In this case, the client application has to
|
||||
* abort the connection or have a default application level protocol. 2) If
|
||||
* the server supports NPN, but advertises an empty list then the client
|
||||
* selects the first protocol in its list, but indicates via the API that this
|
||||
* fallback case was enacted. 3) Otherwise, the client finds the first
|
||||
* protocol in the server's list that it supports and selects this protocol.
|
||||
* This is because it's assumed that the server has better information about
|
||||
* which protocol a client should use. 4) If the client doesn't support any
|
||||
* of the server's advertised protocols, then this is treated the same as
|
||||
* case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
|
||||
* found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
|
||||
*/
|
||||
int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
|
||||
const unsigned char *server,
|
||||
unsigned int server_len,
|
||||
const unsigned char *client, unsigned int client_len)
|
||||
{
|
||||
unsigned int i, j;
|
||||
const unsigned char *result;
|
||||
int status = OPENSSL_NPN_UNSUPPORTED;
|
||||
|
||||
/*
|
||||
* For each protocol in server preference order, see if we support it.
|
||||
*/
|
||||
for (i = 0; i < server_len;) {
|
||||
for (j = 0; j < client_len;) {
|
||||
if (server[i] == client[j] &&
|
||||
memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
|
||||
/* We found a match */
|
||||
result = &server[i];
|
||||
status = OPENSSL_NPN_NEGOTIATED;
|
||||
goto found;
|
||||
}
|
||||
j += client[j];
|
||||
j++;
|
||||
}
|
||||
i += server[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
/* There's no overlap between our protocols and the server's list. */
|
||||
result = client;
|
||||
status = OPENSSL_NPN_NO_OVERLAP;
|
||||
|
||||
found:
|
||||
*out = (unsigned char *)result + 1;
|
||||
*outlen = result[0];
|
||||
return status;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
/*
|
||||
* SSL_get0_next_proto_negotiated sets *data and *len to point to the
|
||||
* client's requested protocol for this connection and returns 0. If the
|
||||
* client didn't request any protocol, then *data is set to NULL. Note that
|
||||
* the client can request any protocol it chooses. The value returned from
|
||||
* this function need not be a member of the list of supported protocols
|
||||
* provided by the callback.
|
||||
*/
|
||||
void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
|
||||
unsigned *len)
|
||||
{
|
||||
*data = s->ext.npn;
|
||||
if (*data == NULL) {
|
||||
*len = 0;
|
||||
} else {
|
||||
*len = (unsigned int)s->ext.npn_len;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* SSL_CTX_set_npn_advertised_cb sets a callback that is called when
|
||||
* a TLS server needs a list of supported protocols for Next Protocol
|
||||
* Negotiation. The returned list must be in wire format. The list is
|
||||
* returned by setting |out| to point to it and |outlen| to its length. This
|
||||
* memory will not be modified, but one should assume that the SSL* keeps a
|
||||
* reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
|
||||
* wishes to advertise. Otherwise, no such extension will be included in the
|
||||
* ServerHello.
|
||||
*/
|
||||
void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
|
||||
SSL_CTX_npn_advertised_cb_func cb,
|
||||
void *arg)
|
||||
{
|
||||
ctx->ext.npn_advertised_cb = cb;
|
||||
ctx->ext.npn_advertised_cb_arg = arg;
|
||||
}
|
||||
|
||||
/*
|
||||
* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
|
||||
* client needs to select a protocol from the server's provided list. |out|
|
||||
* must be set to point to the selected protocol (which may be within |in|).
|
||||
* The length of the protocol name must be written into |outlen|. The
|
||||
* server's advertised protocols are provided in |in| and |inlen|. The
|
||||
* callback can assume that |in| is syntactically valid. The client must
|
||||
* select a protocol. It is fatal to the connection if this callback returns
|
||||
* a value other than SSL_TLSEXT_ERR_OK.
|
||||
*/
|
||||
void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
|
||||
SSL_CTX_npn_select_cb_func cb,
|
||||
void *arg)
|
||||
{
|
||||
ctx->ext.npn_select_cb = cb;
|
||||
ctx->ext.npn_select_cb_arg = arg;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
|
||||
* |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
|
||||
* length-prefixed strings). Returns 0 on success.
|
||||
*/
|
||||
int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
|
||||
unsigned int protos_len)
|
||||
{
|
||||
OPENSSL_free(ctx->ext.alpn);
|
||||
ctx->ext.alpn = OPENSSL_memdup(protos, protos_len);
|
||||
if (ctx->ext.alpn == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE);
|
||||
return 1;
|
||||
}
|
||||
ctx->ext.alpn_len = protos_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
|
||||
* |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
|
||||
* length-prefixed strings). Returns 0 on success.
|
||||
*/
|
||||
int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
|
||||
unsigned int protos_len)
|
||||
{
|
||||
OPENSSL_free(ssl->ext.alpn);
|
||||
ssl->ext.alpn = OPENSSL_memdup(protos, protos_len);
|
||||
if (ssl->ext.alpn == NULL) {
|
||||
SSLerr(SSL_F_SSL_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE);
|
||||
return 1;
|
||||
}
|
||||
ssl->ext.alpn_len = protos_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
|
||||
* called during ClientHello processing in order to select an ALPN protocol
|
||||
* from the client's list of offered protocols.
|
||||
*/
|
||||
void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
|
||||
SSL_CTX_alpn_select_cb_func cb,
|
||||
void *arg)
|
||||
{
|
||||
ctx->ext.alpn_select_cb = cb;
|
||||
ctx->ext.alpn_select_cb_arg = arg;
|
||||
}
|
||||
|
||||
/*
|
||||
* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
|
||||
* On return it sets |*data| to point to |*len| bytes of protocol name
|
||||
* (not including the leading length-prefix byte). If the server didn't
|
||||
* respond with a negotiated protocol then |*len| will be zero.
|
||||
*/
|
||||
void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
|
||||
unsigned int *len)
|
||||
{
|
||||
*data = ssl->s3.alpn_selected;
|
||||
if (*data == NULL)
|
||||
*len = 0;
|
||||
else
|
||||
*len = (unsigned int)ssl->s3.alpn_selected_len;
|
||||
}
|
||||
|
||||
int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
|
||||
const char *label, size_t llen,
|
||||
const unsigned char *context, size_t contextlen,
|
||||
int use_context)
|
||||
{
|
||||
if (s->version < TLS1_VERSION && s->version != DTLS1_BAD_VER)
|
||||
return -1;
|
||||
|
||||
return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
|
||||
llen, context,
|
||||
contextlen, use_context);
|
||||
}
|
||||
|
||||
int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
|
||||
const char *label, size_t llen,
|
||||
const unsigned char *context,
|
||||
size_t contextlen)
|
||||
{
|
||||
if (s->version != TLS1_3_VERSION)
|
||||
return 0;
|
||||
|
||||
return tls13_export_keying_material_early(s, out, olen, label, llen,
|
||||
context, contextlen);
|
||||
}
|
||||
|
||||
static unsigned long ssl_session_hash(const SSL_SESSION *a)
|
||||
{
|
||||
const unsigned char *session_id = a->session_id;
|
||||
unsigned long l;
|
||||
unsigned char tmp_storage[4];
|
||||
|
||||
if (a->session_id_length < sizeof(tmp_storage)) {
|
||||
memset(tmp_storage, 0, sizeof(tmp_storage));
|
||||
memcpy(tmp_storage, a->session_id, a->session_id_length);
|
||||
session_id = tmp_storage;
|
||||
}
|
||||
|
||||
l = (unsigned long)
|
||||
((unsigned long)session_id[0]) |
|
||||
((unsigned long)session_id[1] << 8L) |
|
||||
((unsigned long)session_id[2] << 16L) |
|
||||
((unsigned long)session_id[3] << 24L);
|
||||
return l;
|
||||
}
|
||||
|
||||
/*
|
||||
* NB: If this function (or indeed the hash function which uses a sort of
|
||||
* coarser function than this one) is changed, ensure
|
||||
* SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
|
||||
* being able to construct an SSL_SESSION that will collide with any existing
|
||||
* session with a matching session ID.
|
||||
*/
|
||||
static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
|
||||
{
|
||||
if (a->ssl_version != b->ssl_version)
|
||||
return 1;
|
||||
if (a->session_id_length != b->session_id_length)
|
||||
return 1;
|
||||
return memcmp(a->session_id, b->session_id, a->session_id_length);
|
||||
}
|
||||
|
||||
/*
|
||||
* These wrapper functions should remain rather than redeclaring
|
||||
* SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
|
||||
* variable. The reason is that the functions aren't static, they're exposed
|
||||
* via ssl.h.
|
||||
*/
|
||||
|
||||
SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
|
||||
{
|
||||
SSL_CTX *ret = NULL;
|
||||
|
||||
if (meth == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
|
||||
return NULL;
|
||||
|
||||
if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
|
||||
goto err;
|
||||
}
|
||||
ret = OPENSSL_zalloc(sizeof(*ret));
|
||||
if (ret == NULL)
|
||||
goto err;
|
||||
|
||||
ret->method = meth;
|
||||
ret->min_proto_version = 0;
|
||||
ret->max_proto_version = 0;
|
||||
ret->mode = SSL_MODE_AUTO_RETRY;
|
||||
ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
|
||||
ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
|
||||
/* We take the system default. */
|
||||
ret->session_timeout = meth->get_timeout();
|
||||
ret->references = 1;
|
||||
ret->lock = CRYPTO_THREAD_lock_new();
|
||||
if (ret->lock == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
|
||||
ret->verify_mode = SSL_VERIFY_NONE;
|
||||
if ((ret->cert = ssl_cert_new()) == NULL)
|
||||
goto err;
|
||||
|
||||
ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
|
||||
if (ret->sessions == NULL)
|
||||
goto err;
|
||||
ret->cert_store = X509_STORE_new();
|
||||
if (ret->cert_store == NULL)
|
||||
goto err;
|
||||
#ifndef OPENSSL_NO_CT
|
||||
ret->ctlog_store = CTLOG_STORE_new();
|
||||
if (ret->ctlog_store == NULL)
|
||||
goto err;
|
||||
#endif
|
||||
|
||||
if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites()))
|
||||
goto err;
|
||||
|
||||
if (!ssl_create_cipher_list(ret->method,
|
||||
ret->tls13_ciphersuites,
|
||||
&ret->cipher_list, &ret->cipher_list_by_id,
|
||||
OSSL_default_cipher_list(), ret->cert)
|
||||
|| sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
|
||||
goto err2;
|
||||
}
|
||||
|
||||
ret->param = X509_VERIFY_PARAM_new();
|
||||
if (ret->param == NULL)
|
||||
goto err;
|
||||
|
||||
if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
|
||||
goto err2;
|
||||
}
|
||||
if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
|
||||
goto err2;
|
||||
}
|
||||
|
||||
if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL)
|
||||
goto err;
|
||||
|
||||
if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL)
|
||||
goto err;
|
||||
|
||||
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data))
|
||||
goto err;
|
||||
|
||||
if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
|
||||
goto err;
|
||||
|
||||
/* No compression for DTLS */
|
||||
if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
|
||||
ret->comp_methods = SSL_COMP_get_compression_methods();
|
||||
|
||||
ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
|
||||
ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
|
||||
|
||||
/* Setup RFC5077 ticket keys */
|
||||
if ((RAND_bytes(ret->ext.tick_key_name,
|
||||
sizeof(ret->ext.tick_key_name)) <= 0)
|
||||
|| (RAND_priv_bytes(ret->ext.secure->tick_hmac_key,
|
||||
sizeof(ret->ext.secure->tick_hmac_key)) <= 0)
|
||||
|| (RAND_priv_bytes(ret->ext.secure->tick_aes_key,
|
||||
sizeof(ret->ext.secure->tick_aes_key)) <= 0))
|
||||
ret->options |= SSL_OP_NO_TICKET;
|
||||
|
||||
if (RAND_priv_bytes(ret->ext.cookie_hmac_key,
|
||||
sizeof(ret->ext.cookie_hmac_key)) <= 0)
|
||||
goto err;
|
||||
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
if (!SSL_CTX_SRP_CTX_init(ret))
|
||||
goto err;
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_ENGINE
|
||||
# ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
|
||||
# define eng_strx(x) #x
|
||||
# define eng_str(x) eng_strx(x)
|
||||
/* Use specific client engine automatically... ignore errors */
|
||||
{
|
||||
ENGINE *eng;
|
||||
eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
|
||||
if (!eng) {
|
||||
ERR_clear_error();
|
||||
ENGINE_load_builtin_engines();
|
||||
eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
|
||||
}
|
||||
if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
|
||||
ERR_clear_error();
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
/*
|
||||
* Default is to connect to non-RI servers. When RI is more widely
|
||||
* deployed might change this.
|
||||
*/
|
||||
ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
|
||||
/*
|
||||
* Disable compression by default to prevent CRIME. Applications can
|
||||
* re-enable compression by configuring
|
||||
* SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
|
||||
* or by using the SSL_CONF library. Similarly we also enable TLSv1.3
|
||||
* middlebox compatibility by default. This may be disabled by default in
|
||||
* a later OpenSSL version.
|
||||
*/
|
||||
ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
|
||||
|
||||
ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
|
||||
|
||||
/*
|
||||
* We cannot usefully set a default max_early_data here (which gets
|
||||
* propagated in SSL_new(), for the following reason: setting the
|
||||
* SSL field causes tls_construct_stoc_early_data() to tell the
|
||||
* client that early data will be accepted when constructing a TLS 1.3
|
||||
* session ticket, and the client will accordingly send us early data
|
||||
* when using that ticket (if the client has early data to send).
|
||||
* However, in order for the early data to actually be consumed by
|
||||
* the application, the application must also have calls to
|
||||
* SSL_read_early_data(); otherwise we'll just skip past the early data
|
||||
* and ignore it. So, since the application must add calls to
|
||||
* SSL_read_early_data(), we also require them to add
|
||||
* calls to SSL_CTX_set_max_early_data() in order to use early data,
|
||||
* eliminating the bandwidth-wasting early data in the case described
|
||||
* above.
|
||||
*/
|
||||
ret->max_early_data = 0;
|
||||
|
||||
/*
|
||||
* Default recv_max_early_data is a fully loaded single record. Could be
|
||||
* split across multiple records in practice. We set this differently to
|
||||
* max_early_data so that, in the default case, we do not advertise any
|
||||
* support for early_data, but if a client were to send us some (e.g.
|
||||
* because of an old, stale ticket) then we will tolerate it and skip over
|
||||
* it.
|
||||
*/
|
||||
ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
|
||||
|
||||
/* By default we send two session tickets automatically in TLSv1.3 */
|
||||
ret->num_tickets = 2;
|
||||
|
||||
ssl_ctx_system_config(ret);
|
||||
|
||||
return ret;
|
||||
err:
|
||||
SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
||||
err2:
|
||||
SSL_CTX_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int SSL_CTX_up_ref(SSL_CTX *ctx)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (CRYPTO_UP_REF(&ctx->references, &i, ctx->lock) <= 0)
|
||||
return 0;
|
||||
|
||||
REF_PRINT_COUNT("SSL_CTX", ctx);
|
||||
REF_ASSERT_ISNT(i < 2);
|
||||
return ((i > 1) ? 1 : 0);
|
||||
}
|
||||
|
||||
void SSL_CTX_free(SSL_CTX *a)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (a == NULL)
|
||||
return;
|
||||
|
||||
CRYPTO_DOWN_REF(&a->references, &i, a->lock);
|
||||
REF_PRINT_COUNT("SSL_CTX", a);
|
||||
if (i > 0)
|
||||
return;
|
||||
REF_ASSERT_ISNT(i < 0);
|
||||
|
||||
X509_VERIFY_PARAM_free(a->param);
|
||||
dane_ctx_final(&a->dane);
|
||||
|
||||
/*
|
||||
* Free internal session cache. However: the remove_cb() may reference
|
||||
* the ex_data of SSL_CTX, thus the ex_data store can only be removed
|
||||
* after the sessions were flushed.
|
||||
* As the ex_data handling routines might also touch the session cache,
|
||||
* the most secure solution seems to be: empty (flush) the cache, then
|
||||
* free ex_data, then finally free the cache.
|
||||
* (See ticket [openssl.org #212].)
|
||||
*/
|
||||
if (a->sessions != NULL)
|
||||
SSL_CTX_flush_sessions(a, 0);
|
||||
|
||||
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
|
||||
lh_SSL_SESSION_free(a->sessions);
|
||||
X509_STORE_free(a->cert_store);
|
||||
#ifndef OPENSSL_NO_CT
|
||||
CTLOG_STORE_free(a->ctlog_store);
|
||||
#endif
|
||||
sk_SSL_CIPHER_free(a->cipher_list);
|
||||
sk_SSL_CIPHER_free(a->cipher_list_by_id);
|
||||
sk_SSL_CIPHER_free(a->tls13_ciphersuites);
|
||||
ssl_cert_free(a->cert);
|
||||
sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
|
||||
sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
|
||||
sk_X509_pop_free(a->extra_certs, X509_free);
|
||||
a->comp_methods = NULL;
|
||||
#ifndef OPENSSL_NO_SRTP
|
||||
sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
SSL_CTX_SRP_CTX_free(a);
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_ENGINE
|
||||
ENGINE_finish(a->client_cert_engine);
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
OPENSSL_free(a->ext.ecpointformats);
|
||||
#endif
|
||||
OPENSSL_free(a->ext.supportedgroups);
|
||||
OPENSSL_free(a->ext.alpn);
|
||||
OPENSSL_secure_free(a->ext.secure);
|
||||
|
||||
CRYPTO_THREAD_lock_free(a->lock);
|
||||
|
||||
OPENSSL_free(a);
|
||||
}
|
||||
|
||||
void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
|
||||
{
|
||||
ctx->default_passwd_callback = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
|
||||
{
|
||||
ctx->default_passwd_callback_userdata = u;
|
||||
}
|
||||
|
||||
pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->default_passwd_callback;
|
||||
}
|
||||
|
||||
void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->default_passwd_callback_userdata;
|
||||
}
|
||||
|
||||
void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
|
||||
{
|
||||
s->default_passwd_callback = cb;
|
||||
}
|
||||
|
||||
void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
|
||||
{
|
||||
s->default_passwd_callback_userdata = u;
|
||||
}
|
||||
|
||||
pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
|
||||
{
|
||||
return s->default_passwd_callback;
|
||||
}
|
||||
|
||||
void *SSL_get_default_passwd_cb_userdata(SSL *s)
|
||||
{
|
||||
return s->default_passwd_callback_userdata;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
|
||||
int (*cb) (X509_STORE_CTX *, void *),
|
||||
void *arg)
|
||||
{
|
||||
ctx->app_verify_callback = cb;
|
||||
ctx->app_verify_arg = arg;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
|
||||
int (*cb) (int, X509_STORE_CTX *))
|
||||
{
|
||||
ctx->verify_mode = mode;
|
||||
ctx->default_verify_callback = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
|
||||
{
|
||||
X509_VERIFY_PARAM_set_depth(ctx->param, depth);
|
||||
}
|
||||
|
||||
void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
|
||||
{
|
||||
ssl_cert_set_cert_cb(c->cert, cb, arg);
|
||||
}
|
||||
|
||||
void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
|
||||
{
|
||||
ssl_cert_set_cert_cb(s->cert, cb, arg);
|
||||
}
|
||||
|
||||
void ssl_set_masks(SSL *s)
|
||||
{
|
||||
CERT *c = s->cert;
|
||||
uint32_t *pvalid = s->s3.tmp.valid_flags;
|
||||
int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
|
||||
unsigned long mask_k, mask_a;
|
||||
#ifndef OPENSSL_NO_EC
|
||||
int have_ecc_cert, ecdsa_ok;
|
||||
#endif
|
||||
if (c == NULL)
|
||||
return;
|
||||
|
||||
#ifndef OPENSSL_NO_DH
|
||||
dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto);
|
||||
#else
|
||||
dh_tmp = 0;
|
||||
#endif
|
||||
|
||||
rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
|
||||
rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
|
||||
dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
|
||||
#ifndef OPENSSL_NO_EC
|
||||
have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
|
||||
#endif
|
||||
mask_k = 0;
|
||||
mask_a = 0;
|
||||
|
||||
OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
|
||||
dh_tmp, rsa_enc, rsa_sign, dsa_sign);
|
||||
|
||||
#ifndef OPENSSL_NO_GOST
|
||||
if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
|
||||
mask_k |= SSL_kGOST;
|
||||
mask_a |= SSL_aGOST12;
|
||||
}
|
||||
if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
|
||||
mask_k |= SSL_kGOST;
|
||||
mask_a |= SSL_aGOST12;
|
||||
}
|
||||
if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
|
||||
mask_k |= SSL_kGOST;
|
||||
mask_a |= SSL_aGOST01;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (rsa_enc)
|
||||
mask_k |= SSL_kRSA;
|
||||
|
||||
if (dh_tmp)
|
||||
mask_k |= SSL_kDHE;
|
||||
|
||||
/*
|
||||
* If we only have an RSA-PSS certificate allow RSA authentication
|
||||
* if TLS 1.2 and peer supports it.
|
||||
*/
|
||||
|
||||
if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
|
||||
&& pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
|
||||
&& TLS1_get_version(s) == TLS1_2_VERSION))
|
||||
mask_a |= SSL_aRSA;
|
||||
|
||||
if (dsa_sign) {
|
||||
mask_a |= SSL_aDSS;
|
||||
}
|
||||
|
||||
mask_a |= SSL_aNULL;
|
||||
|
||||
/*
|
||||
* An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
|
||||
* depending on the key usage extension.
|
||||
*/
|
||||
#ifndef OPENSSL_NO_EC
|
||||
if (have_ecc_cert) {
|
||||
uint32_t ex_kusage;
|
||||
ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
|
||||
ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
|
||||
if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
|
||||
ecdsa_ok = 0;
|
||||
if (ecdsa_ok)
|
||||
mask_a |= SSL_aECDSA;
|
||||
}
|
||||
/* Allow Ed25519 for TLS 1.2 if peer supports it */
|
||||
if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
|
||||
&& pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
|
||||
&& TLS1_get_version(s) == TLS1_2_VERSION)
|
||||
mask_a |= SSL_aECDSA;
|
||||
|
||||
/* Allow Ed448 for TLS 1.2 if peer supports it */
|
||||
if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
|
||||
&& pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
|
||||
&& TLS1_get_version(s) == TLS1_2_VERSION)
|
||||
mask_a |= SSL_aECDSA;
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
mask_k |= SSL_kECDHE;
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
mask_k |= SSL_kPSK;
|
||||
mask_a |= SSL_aPSK;
|
||||
if (mask_k & SSL_kRSA)
|
||||
mask_k |= SSL_kRSAPSK;
|
||||
if (mask_k & SSL_kDHE)
|
||||
mask_k |= SSL_kDHEPSK;
|
||||
if (mask_k & SSL_kECDHE)
|
||||
mask_k |= SSL_kECDHEPSK;
|
||||
#endif
|
||||
|
||||
s->s3.tmp.mask_k = mask_k;
|
||||
s->s3.tmp.mask_a = mask_a;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
|
||||
int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
|
||||
{
|
||||
if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
|
||||
/* key usage, if present, must allow signing */
|
||||
if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
|
||||
SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
|
||||
SSL_R_ECC_CERT_NOT_FOR_SIGNING);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1; /* all checks are ok */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
|
||||
size_t *serverinfo_length)
|
||||
{
|
||||
CERT_PKEY *cpk = s->s3.tmp.cert;
|
||||
*serverinfo_length = 0;
|
||||
|
||||
if (cpk == NULL || cpk->serverinfo == NULL)
|
||||
return 0;
|
||||
|
||||
*serverinfo = cpk->serverinfo;
|
||||
*serverinfo_length = cpk->serverinfo_length;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ssl_update_cache(SSL *s, int mode)
|
||||
{
|
||||
int i;
|
||||
|
||||
/*
|
||||
* If the session_id_length is 0, we are not supposed to cache it, and it
|
||||
* would be rather hard to do anyway :-)
|
||||
*/
|
||||
if (s->session->session_id_length == 0)
|
||||
return;
|
||||
|
||||
/*
|
||||
* If sid_ctx_length is 0 there is no specific application context
|
||||
* associated with this session, so when we try to resume it and
|
||||
* SSL_VERIFY_PEER is requested to verify the client identity, we have no
|
||||
* indication that this is actually a session for the proper application
|
||||
* context, and the *handshake* will fail, not just the resumption attempt.
|
||||
* Do not cache (on the server) these sessions that are not resumable
|
||||
* (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
|
||||
*/
|
||||
if (s->server && s->session->sid_ctx_length == 0
|
||||
&& (s->verify_mode & SSL_VERIFY_PEER) != 0)
|
||||
return;
|
||||
|
||||
i = s->session_ctx->session_cache_mode;
|
||||
if ((i & mode) != 0
|
||||
&& (!s->hit || SSL_IS_TLS13(s))) {
|
||||
/*
|
||||
* Add the session to the internal cache. In server side TLSv1.3 we
|
||||
* normally don't do this because by default it's a full stateless ticket
|
||||
* with only a dummy session id so there is no reason to cache it,
|
||||
* unless:
|
||||
* - we are doing early_data, in which case we cache so that we can
|
||||
* detect replays
|
||||
* - the application has set a remove_session_cb so needs to know about
|
||||
* session timeout events
|
||||
* - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
|
||||
*/
|
||||
if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
|
||||
&& (!SSL_IS_TLS13(s)
|
||||
|| !s->server
|
||||
|| (s->max_early_data > 0
|
||||
&& (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
|
||||
|| s->session_ctx->remove_session_cb != NULL
|
||||
|| (s->options & SSL_OP_NO_TICKET) != 0))
|
||||
SSL_CTX_add_session(s->session_ctx, s->session);
|
||||
|
||||
/*
|
||||
* Add the session to the external cache. We do this even in server side
|
||||
* TLSv1.3 without early data because some applications just want to
|
||||
* know about the creation of a session and aren't doing a full cache.
|
||||
*/
|
||||
if (s->session_ctx->new_session_cb != NULL) {
|
||||
SSL_SESSION_up_ref(s->session);
|
||||
if (!s->session_ctx->new_session_cb(s, s->session))
|
||||
SSL_SESSION_free(s->session);
|
||||
}
|
||||
}
|
||||
|
||||
/* auto flush every 255 connections */
|
||||
if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
|
||||
TSAN_QUALIFIER int *stat;
|
||||
if (mode & SSL_SESS_CACHE_CLIENT)
|
||||
stat = &s->session_ctx->stats.sess_connect_good;
|
||||
else
|
||||
stat = &s->session_ctx->stats.sess_accept_good;
|
||||
if ((tsan_load(stat) & 0xff) == 0xff)
|
||||
SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
|
||||
}
|
||||
}
|
||||
|
||||
const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->method;
|
||||
}
|
||||
|
||||
const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
|
||||
{
|
||||
return s->method;
|
||||
}
|
||||
|
||||
int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
if (s->method != meth) {
|
||||
const SSL_METHOD *sm = s->method;
|
||||
int (*hf) (SSL *) = s->handshake_func;
|
||||
|
||||
if (sm->version == meth->version)
|
||||
s->method = meth;
|
||||
else {
|
||||
sm->ssl_free(s);
|
||||
s->method = meth;
|
||||
ret = s->method->ssl_new(s);
|
||||
}
|
||||
|
||||
if (hf == sm->ssl_connect)
|
||||
s->handshake_func = meth->ssl_connect;
|
||||
else if (hf == sm->ssl_accept)
|
||||
s->handshake_func = meth->ssl_accept;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_get_error(const SSL *s, int i)
|
||||
{
|
||||
int reason;
|
||||
unsigned long l;
|
||||
BIO *bio;
|
||||
|
||||
if (i > 0)
|
||||
return SSL_ERROR_NONE;
|
||||
|
||||
/*
|
||||
* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
|
||||
* where we do encode the error
|
||||
*/
|
||||
if ((l = ERR_peek_error()) != 0) {
|
||||
if (ERR_GET_LIB(l) == ERR_LIB_SYS)
|
||||
return SSL_ERROR_SYSCALL;
|
||||
else
|
||||
return SSL_ERROR_SSL;
|
||||
}
|
||||
|
||||
if (SSL_want_read(s)) {
|
||||
bio = SSL_get_rbio(s);
|
||||
if (BIO_should_read(bio))
|
||||
return SSL_ERROR_WANT_READ;
|
||||
else if (BIO_should_write(bio))
|
||||
/*
|
||||
* This one doesn't make too much sense ... We never try to write
|
||||
* to the rbio, and an application program where rbio and wbio
|
||||
* are separate couldn't even know what it should wait for.
|
||||
* However if we ever set s->rwstate incorrectly (so that we have
|
||||
* SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
|
||||
* wbio *are* the same, this test works around that bug; so it
|
||||
* might be safer to keep it.
|
||||
*/
|
||||
return SSL_ERROR_WANT_WRITE;
|
||||
else if (BIO_should_io_special(bio)) {
|
||||
reason = BIO_get_retry_reason(bio);
|
||||
if (reason == BIO_RR_CONNECT)
|
||||
return SSL_ERROR_WANT_CONNECT;
|
||||
else if (reason == BIO_RR_ACCEPT)
|
||||
return SSL_ERROR_WANT_ACCEPT;
|
||||
else
|
||||
return SSL_ERROR_SYSCALL; /* unknown */
|
||||
}
|
||||
}
|
||||
|
||||
if (SSL_want_write(s)) {
|
||||
/* Access wbio directly - in order to use the buffered bio if present */
|
||||
bio = s->wbio;
|
||||
if (BIO_should_write(bio))
|
||||
return SSL_ERROR_WANT_WRITE;
|
||||
else if (BIO_should_read(bio))
|
||||
/*
|
||||
* See above (SSL_want_read(s) with BIO_should_write(bio))
|
||||
*/
|
||||
return SSL_ERROR_WANT_READ;
|
||||
else if (BIO_should_io_special(bio)) {
|
||||
reason = BIO_get_retry_reason(bio);
|
||||
if (reason == BIO_RR_CONNECT)
|
||||
return SSL_ERROR_WANT_CONNECT;
|
||||
else if (reason == BIO_RR_ACCEPT)
|
||||
return SSL_ERROR_WANT_ACCEPT;
|
||||
else
|
||||
return SSL_ERROR_SYSCALL;
|
||||
}
|
||||
}
|
||||
if (SSL_want_x509_lookup(s))
|
||||
return SSL_ERROR_WANT_X509_LOOKUP;
|
||||
if (SSL_want_async(s))
|
||||
return SSL_ERROR_WANT_ASYNC;
|
||||
if (SSL_want_async_job(s))
|
||||
return SSL_ERROR_WANT_ASYNC_JOB;
|
||||
if (SSL_want_client_hello_cb(s))
|
||||
return SSL_ERROR_WANT_CLIENT_HELLO_CB;
|
||||
|
||||
if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
|
||||
(s->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
|
||||
return SSL_ERROR_ZERO_RETURN;
|
||||
|
||||
return SSL_ERROR_SYSCALL;
|
||||
}
|
||||
|
||||
static int ssl_do_handshake_intern(void *vargs)
|
||||
{
|
||||
struct ssl_async_args *args;
|
||||
SSL *s;
|
||||
|
||||
args = (struct ssl_async_args *)vargs;
|
||||
s = args->s;
|
||||
|
||||
return s->handshake_func(s);
|
||||
}
|
||||
|
||||
int SSL_do_handshake(SSL *s)
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
if (s->handshake_func == NULL) {
|
||||
SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ossl_statem_check_finish_init(s, -1);
|
||||
|
||||
s->method->ssl_renegotiate_check(s, 0);
|
||||
|
||||
if (SSL_in_init(s) || SSL_in_before(s)) {
|
||||
if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
|
||||
struct ssl_async_args args;
|
||||
|
||||
args.s = s;
|
||||
|
||||
ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
|
||||
} else {
|
||||
ret = s->handshake_func(s);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SSL_set_accept_state(SSL *s)
|
||||
{
|
||||
s->server = 1;
|
||||
s->shutdown = 0;
|
||||
ossl_statem_clear(s);
|
||||
s->handshake_func = s->method->ssl_accept;
|
||||
clear_ciphers(s);
|
||||
}
|
||||
|
||||
void SSL_set_connect_state(SSL *s)
|
||||
{
|
||||
s->server = 0;
|
||||
s->shutdown = 0;
|
||||
ossl_statem_clear(s);
|
||||
s->handshake_func = s->method->ssl_connect;
|
||||
clear_ciphers(s);
|
||||
}
|
||||
|
||||
int ssl_undefined_function(SSL *s)
|
||||
{
|
||||
SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl_undefined_void_function(void)
|
||||
{
|
||||
SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,
|
||||
ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl_undefined_const_function(const SSL *s)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const SSL_METHOD *ssl_bad_method(int ver)
|
||||
{
|
||||
SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *ssl_protocol_to_string(int version)
|
||||
{
|
||||
switch(version)
|
||||
{
|
||||
case TLS1_3_VERSION:
|
||||
return "TLSv1.3";
|
||||
|
||||
case TLS1_2_VERSION:
|
||||
return "TLSv1.2";
|
||||
|
||||
case TLS1_1_VERSION:
|
||||
return "TLSv1.1";
|
||||
|
||||
case TLS1_VERSION:
|
||||
return "TLSv1";
|
||||
|
||||
case SSL3_VERSION:
|
||||
return "SSLv3";
|
||||
|
||||
case DTLS1_BAD_VER:
|
||||
return "DTLSv0.9";
|
||||
|
||||
case DTLS1_VERSION:
|
||||
return "DTLSv1";
|
||||
|
||||
case DTLS1_2_VERSION:
|
||||
return "DTLSv1.2";
|
||||
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const char *SSL_get_version(const SSL *s)
|
||||
{
|
||||
return ssl_protocol_to_string(s->version);
|
||||
}
|
||||
|
||||
static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
|
||||
{
|
||||
STACK_OF(X509_NAME) *sk;
|
||||
X509_NAME *xn;
|
||||
int i;
|
||||
|
||||
if (src == NULL) {
|
||||
*dst = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((sk = sk_X509_NAME_new_null()) == NULL)
|
||||
return 0;
|
||||
for (i = 0; i < sk_X509_NAME_num(src); i++) {
|
||||
xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
|
||||
if (xn == NULL) {
|
||||
sk_X509_NAME_pop_free(sk, X509_NAME_free);
|
||||
return 0;
|
||||
}
|
||||
if (sk_X509_NAME_insert(sk, xn, i) == 0) {
|
||||
X509_NAME_free(xn);
|
||||
sk_X509_NAME_pop_free(sk, X509_NAME_free);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
*dst = sk;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
SSL *SSL_dup(SSL *s)
|
||||
{
|
||||
SSL *ret;
|
||||
int i;
|
||||
|
||||
/* If we're not quiescent, just up_ref! */
|
||||
if (!SSL_in_init(s) || !SSL_in_before(s)) {
|
||||
CRYPTO_UP_REF(&s->references, &i, s->lock);
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
* Otherwise, copy configuration state, and session if set.
|
||||
*/
|
||||
if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
|
||||
return NULL;
|
||||
|
||||
if (s->session != NULL) {
|
||||
/*
|
||||
* Arranges to share the same session via up_ref. This "copies"
|
||||
* session-id, SSL_METHOD, sid_ctx, and 'cert'
|
||||
*/
|
||||
if (!SSL_copy_session_id(ret, s))
|
||||
goto err;
|
||||
} else {
|
||||
/*
|
||||
* No session has been established yet, so we have to expect that
|
||||
* s->cert or ret->cert will be changed later -- they should not both
|
||||
* point to the same object, and thus we can't use
|
||||
* SSL_copy_session_id.
|
||||
*/
|
||||
if (!SSL_set_ssl_method(ret, s->method))
|
||||
goto err;
|
||||
|
||||
if (s->cert != NULL) {
|
||||
ssl_cert_free(ret->cert);
|
||||
ret->cert = ssl_cert_dup(s->cert);
|
||||
if (ret->cert == NULL)
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!SSL_set_session_id_context(ret, s->sid_ctx,
|
||||
(int)s->sid_ctx_length))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!ssl_dane_dup(ret, s))
|
||||
goto err;
|
||||
ret->version = s->version;
|
||||
ret->options = s->options;
|
||||
ret->mode = s->mode;
|
||||
SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
|
||||
SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
|
||||
ret->msg_callback = s->msg_callback;
|
||||
ret->msg_callback_arg = s->msg_callback_arg;
|
||||
SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
|
||||
SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
|
||||
ret->generate_session_id = s->generate_session_id;
|
||||
|
||||
SSL_set_info_callback(ret, SSL_get_info_callback(s));
|
||||
|
||||
/* copy app data, a little dangerous perhaps */
|
||||
if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
|
||||
goto err;
|
||||
|
||||
/* setup rbio, and wbio */
|
||||
if (s->rbio != NULL) {
|
||||
if (!BIO_dup_state(s->rbio, (char *)&ret->rbio))
|
||||
goto err;
|
||||
}
|
||||
if (s->wbio != NULL) {
|
||||
if (s->wbio != s->rbio) {
|
||||
if (!BIO_dup_state(s->wbio, (char *)&ret->wbio))
|
||||
goto err;
|
||||
} else {
|
||||
BIO_up_ref(ret->rbio);
|
||||
ret->wbio = ret->rbio;
|
||||
}
|
||||
}
|
||||
|
||||
ret->server = s->server;
|
||||
if (s->handshake_func) {
|
||||
if (s->server)
|
||||
SSL_set_accept_state(ret);
|
||||
else
|
||||
SSL_set_connect_state(ret);
|
||||
}
|
||||
ret->shutdown = s->shutdown;
|
||||
ret->hit = s->hit;
|
||||
|
||||
ret->default_passwd_callback = s->default_passwd_callback;
|
||||
ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;
|
||||
|
||||
X509_VERIFY_PARAM_inherit(ret->param, s->param);
|
||||
|
||||
/* dup the cipher_list and cipher_list_by_id stacks */
|
||||
if (s->cipher_list != NULL) {
|
||||
if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
|
||||
goto err;
|
||||
}
|
||||
if (s->cipher_list_by_id != NULL)
|
||||
if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
|
||||
== NULL)
|
||||
goto err;
|
||||
|
||||
/* Dup the client_CA list */
|
||||
if (!dup_ca_names(&ret->ca_names, s->ca_names)
|
||||
|| !dup_ca_names(&ret->client_ca_names, s->client_ca_names))
|
||||
goto err;
|
||||
|
||||
return ret;
|
||||
|
||||
err:
|
||||
SSL_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ssl_clear_cipher_ctx(SSL *s)
|
||||
{
|
||||
if (s->enc_read_ctx != NULL) {
|
||||
EVP_CIPHER_CTX_free(s->enc_read_ctx);
|
||||
s->enc_read_ctx = NULL;
|
||||
}
|
||||
if (s->enc_write_ctx != NULL) {
|
||||
EVP_CIPHER_CTX_free(s->enc_write_ctx);
|
||||
s->enc_write_ctx = NULL;
|
||||
}
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
COMP_CTX_free(s->expand);
|
||||
s->expand = NULL;
|
||||
COMP_CTX_free(s->compress);
|
||||
s->compress = NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
X509 *SSL_get_certificate(const SSL *s)
|
||||
{
|
||||
if (s->cert != NULL)
|
||||
return s->cert->key->x509;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EVP_PKEY *SSL_get_privatekey(const SSL *s)
|
||||
{
|
||||
if (s->cert != NULL)
|
||||
return s->cert->key->privatekey;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
|
||||
{
|
||||
if (ctx->cert != NULL)
|
||||
return ctx->cert->key->x509;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
|
||||
{
|
||||
if (ctx->cert != NULL)
|
||||
return ctx->cert->key->privatekey;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
|
||||
{
|
||||
if ((s->session != NULL) && (s->session->cipher != NULL))
|
||||
return s->session->cipher;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
|
||||
{
|
||||
return s->s3.tmp.new_cipher;
|
||||
}
|
||||
|
||||
const COMP_METHOD *SSL_get_current_compression(const SSL *s)
|
||||
{
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
return s->compress ? COMP_CTX_get_method(s->compress) : NULL;
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
|
||||
{
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
return s->expand ? COMP_CTX_get_method(s->expand) : NULL;
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ssl_init_wbio_buffer(SSL *s)
|
||||
{
|
||||
BIO *bbio;
|
||||
|
||||
if (s->bbio != NULL) {
|
||||
/* Already buffered. */
|
||||
return 1;
|
||||
}
|
||||
|
||||
bbio = BIO_new(BIO_f_buffer());
|
||||
if (bbio == NULL || !BIO_set_read_buffer_size(bbio, 1)) {
|
||||
BIO_free(bbio);
|
||||
SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB);
|
||||
return 0;
|
||||
}
|
||||
s->bbio = bbio;
|
||||
s->wbio = BIO_push(bbio, s->wbio);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ssl_free_wbio_buffer(SSL *s)
|
||||
{
|
||||
/* callers ensure s is never null */
|
||||
if (s->bbio == NULL)
|
||||
return 1;
|
||||
|
||||
s->wbio = BIO_pop(s->wbio);
|
||||
BIO_free(s->bbio);
|
||||
s->bbio = NULL;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
|
||||
{
|
||||
ctx->quiet_shutdown = mode;
|
||||
}
|
||||
|
||||
int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->quiet_shutdown;
|
||||
}
|
||||
|
||||
void SSL_set_quiet_shutdown(SSL *s, int mode)
|
||||
{
|
||||
s->quiet_shutdown = mode;
|
||||
}
|
||||
|
||||
int SSL_get_quiet_shutdown(const SSL *s)
|
||||
{
|
||||
return s->quiet_shutdown;
|
||||
}
|
||||
|
||||
void SSL_set_shutdown(SSL *s, int mode)
|
||||
{
|
||||
s->shutdown = mode;
|
||||
}
|
||||
|
||||
int SSL_get_shutdown(const SSL *s)
|
||||
{
|
||||
return s->shutdown;
|
||||
}
|
||||
|
||||
int SSL_version(const SSL *s)
|
||||
{
|
||||
return s->version;
|
||||
}
|
||||
|
||||
int SSL_client_version(const SSL *s)
|
||||
{
|
||||
return s->client_version;
|
||||
}
|
||||
|
||||
SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
|
||||
{
|
||||
return ssl->ctx;
|
||||
}
|
||||
|
||||
SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
|
||||
{
|
||||
CERT *new_cert;
|
||||
if (ssl->ctx == ctx)
|
||||
return ssl->ctx;
|
||||
if (ctx == NULL)
|
||||
ctx = ssl->session_ctx;
|
||||
new_cert = ssl_cert_dup(ctx->cert);
|
||||
if (new_cert == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!custom_exts_copy_flags(&new_cert->custext, &ssl->cert->custext)) {
|
||||
ssl_cert_free(new_cert);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssl_cert_free(ssl->cert);
|
||||
ssl->cert = new_cert;
|
||||
|
||||
/*
|
||||
* Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
|
||||
* so setter APIs must prevent invalid lengths from entering the system.
|
||||
*/
|
||||
if (!ossl_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)))
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* If the session ID context matches that of the parent SSL_CTX,
|
||||
* inherit it from the new SSL_CTX as well. If however the context does
|
||||
* not match (i.e., it was set per-ssl with SSL_set_session_id_context),
|
||||
* leave it unchanged.
|
||||
*/
|
||||
if ((ssl->ctx != NULL) &&
|
||||
(ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
|
||||
(memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
|
||||
ssl->sid_ctx_length = ctx->sid_ctx_length;
|
||||
memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
|
||||
}
|
||||
|
||||
SSL_CTX_up_ref(ctx);
|
||||
SSL_CTX_free(ssl->ctx); /* decrement reference count */
|
||||
ssl->ctx = ctx;
|
||||
|
||||
return ssl->ctx;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
|
||||
{
|
||||
return X509_STORE_set_default_paths(ctx->cert_store);
|
||||
}
|
||||
|
||||
int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
|
||||
{
|
||||
X509_LOOKUP *lookup;
|
||||
|
||||
lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
|
||||
if (lookup == NULL)
|
||||
return 0;
|
||||
|
||||
/* We ignore errors, in case the directory doesn't exist */
|
||||
ERR_set_mark();
|
||||
|
||||
X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
|
||||
|
||||
ERR_pop_to_mark();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
|
||||
{
|
||||
X509_LOOKUP *lookup;
|
||||
|
||||
lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
|
||||
if (lookup == NULL)
|
||||
return 0;
|
||||
|
||||
/* We ignore errors, in case the directory doesn't exist */
|
||||
ERR_set_mark();
|
||||
|
||||
X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
|
||||
|
||||
ERR_pop_to_mark();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
|
||||
{
|
||||
X509_LOOKUP *lookup;
|
||||
|
||||
lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
|
||||
if (lookup == NULL)
|
||||
return 0;
|
||||
|
||||
/* We ignore errors, in case the directory doesn't exist */
|
||||
ERR_set_mark();
|
||||
|
||||
X509_LOOKUP_add_store(lookup, NULL);
|
||||
|
||||
ERR_pop_to_mark();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
|
||||
{
|
||||
return X509_STORE_load_file(ctx->cert_store, CAfile);
|
||||
}
|
||||
|
||||
int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
|
||||
{
|
||||
return X509_STORE_load_path(ctx->cert_store, CApath);
|
||||
}
|
||||
|
||||
int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
|
||||
{
|
||||
return X509_STORE_load_store(ctx->cert_store, CAstore);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
|
||||
const char *CApath)
|
||||
{
|
||||
if (CAfile == NULL && CApath == NULL)
|
||||
return 0;
|
||||
if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
|
||||
return 0;
|
||||
if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
void SSL_set_info_callback(SSL *ssl,
|
||||
void (*cb) (const SSL *ssl, int type, int val))
|
||||
{
|
||||
ssl->info_callback = cb;
|
||||
}
|
||||
|
||||
/*
|
||||
* One compiler (Diab DCC) doesn't like argument names in returned function
|
||||
* pointer.
|
||||
*/
|
||||
void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
|
||||
int /* type */ ,
|
||||
int /* val */ ) {
|
||||
return ssl->info_callback;
|
||||
}
|
||||
|
||||
void SSL_set_verify_result(SSL *ssl, long arg)
|
||||
{
|
||||
ssl->verify_result = arg;
|
||||
}
|
||||
|
||||
long SSL_get_verify_result(const SSL *ssl)
|
||||
{
|
||||
return ssl->verify_result;
|
||||
}
|
||||
|
||||
size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
|
||||
{
|
||||
if (outlen == 0)
|
||||
return sizeof(ssl->s3.client_random);
|
||||
if (outlen > sizeof(ssl->s3.client_random))
|
||||
outlen = sizeof(ssl->s3.client_random);
|
||||
memcpy(out, ssl->s3.client_random, outlen);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
|
||||
{
|
||||
if (outlen == 0)
|
||||
return sizeof(ssl->s3.server_random);
|
||||
if (outlen > sizeof(ssl->s3.server_random))
|
||||
outlen = sizeof(ssl->s3.server_random);
|
||||
memcpy(out, ssl->s3.server_random, outlen);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
|
||||
unsigned char *out, size_t outlen)
|
||||
{
|
||||
if (outlen == 0)
|
||||
return session->master_key_length;
|
||||
if (outlen > session->master_key_length)
|
||||
outlen = session->master_key_length;
|
||||
memcpy(out, session->master_key, outlen);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
|
||||
size_t len)
|
||||
{
|
||||
if (len > sizeof(sess->master_key))
|
||||
return 0;
|
||||
|
||||
memcpy(sess->master_key, in, len);
|
||||
sess->master_key_length = len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int SSL_set_ex_data(SSL *s, int idx, void *arg)
|
||||
{
|
||||
return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
|
||||
}
|
||||
|
||||
void *SSL_get_ex_data(const SSL *s, int idx)
|
||||
{
|
||||
return CRYPTO_get_ex_data(&s->ex_data, idx);
|
||||
}
|
||||
|
||||
int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
|
||||
{
|
||||
return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
|
||||
}
|
||||
|
||||
void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
|
||||
{
|
||||
return CRYPTO_get_ex_data(&s->ex_data, idx);
|
||||
}
|
||||
|
||||
X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->cert_store;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
|
||||
{
|
||||
X509_STORE_free(ctx->cert_store);
|
||||
ctx->cert_store = store;
|
||||
}
|
||||
|
||||
void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
|
||||
{
|
||||
if (store != NULL)
|
||||
X509_STORE_up_ref(store);
|
||||
SSL_CTX_set_cert_store(ctx, store);
|
||||
}
|
||||
|
||||
int SSL_want(const SSL *s)
|
||||
{
|
||||
return s->rwstate;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Set the callback for generating temporary DH keys.
|
||||
* \param ctx the SSL context.
|
||||
* \param dh the callback
|
||||
*/
|
||||
|
||||
#ifndef OPENSSL_NO_DH
|
||||
void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
|
||||
DH *(*dh) (SSL *ssl, int is_export,
|
||||
int keylength))
|
||||
{
|
||||
SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
|
||||
}
|
||||
|
||||
void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export,
|
||||
int keylength))
|
||||
{
|
||||
SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
|
||||
{
|
||||
if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
|
||||
SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_free(ctx->cert->psk_identity_hint);
|
||||
if (identity_hint != NULL) {
|
||||
ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
|
||||
if (ctx->cert->psk_identity_hint == NULL)
|
||||
return 0;
|
||||
} else
|
||||
ctx->cert->psk_identity_hint = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
|
||||
{
|
||||
if (s == NULL)
|
||||
return 0;
|
||||
|
||||
if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
|
||||
SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_free(s->cert->psk_identity_hint);
|
||||
if (identity_hint != NULL) {
|
||||
s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
|
||||
if (s->cert->psk_identity_hint == NULL)
|
||||
return 0;
|
||||
} else
|
||||
s->cert->psk_identity_hint = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *SSL_get_psk_identity_hint(const SSL *s)
|
||||
{
|
||||
if (s == NULL || s->session == NULL)
|
||||
return NULL;
|
||||
return s->session->psk_identity_hint;
|
||||
}
|
||||
|
||||
const char *SSL_get_psk_identity(const SSL *s)
|
||||
{
|
||||
if (s == NULL || s->session == NULL)
|
||||
return NULL;
|
||||
return s->session->psk_identity;
|
||||
}
|
||||
|
||||
void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
|
||||
{
|
||||
s->psk_client_callback = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
|
||||
{
|
||||
ctx->psk_client_callback = cb;
|
||||
}
|
||||
|
||||
void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
|
||||
{
|
||||
s->psk_server_callback = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
|
||||
{
|
||||
ctx->psk_server_callback = cb;
|
||||
}
|
||||
#endif
|
||||
|
||||
void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
|
||||
{
|
||||
s->psk_find_session_cb = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
|
||||
SSL_psk_find_session_cb_func cb)
|
||||
{
|
||||
ctx->psk_find_session_cb = cb;
|
||||
}
|
||||
|
||||
void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
|
||||
{
|
||||
s->psk_use_session_cb = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
|
||||
SSL_psk_use_session_cb_func cb)
|
||||
{
|
||||
ctx->psk_use_session_cb = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
|
||||
void (*cb) (int write_p, int version,
|
||||
int content_type, const void *buf,
|
||||
size_t len, SSL *ssl, void *arg))
|
||||
{
|
||||
SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
|
||||
}
|
||||
|
||||
void SSL_set_msg_callback(SSL *ssl,
|
||||
void (*cb) (int write_p, int version,
|
||||
int content_type, const void *buf,
|
||||
size_t len, SSL *ssl, void *arg))
|
||||
{
|
||||
SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
|
||||
}
|
||||
|
||||
void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
|
||||
int (*cb) (SSL *ssl,
|
||||
int
|
||||
is_forward_secure))
|
||||
{
|
||||
SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
|
||||
(void (*)(void))cb);
|
||||
}
|
||||
|
||||
void SSL_set_not_resumable_session_callback(SSL *ssl,
|
||||
int (*cb) (SSL *ssl,
|
||||
int is_forward_secure))
|
||||
{
|
||||
SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
|
||||
(void (*)(void))cb);
|
||||
}
|
||||
|
||||
void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
|
||||
size_t (*cb) (SSL *ssl, int type,
|
||||
size_t len, void *arg))
|
||||
{
|
||||
ctx->record_padding_cb = cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
|
||||
{
|
||||
ctx->record_padding_arg = arg;
|
||||
}
|
||||
|
||||
void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->record_padding_arg;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
|
||||
{
|
||||
/* block size of 0 or 1 is basically no padding */
|
||||
if (block_size == 1)
|
||||
ctx->block_padding = 0;
|
||||
else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
|
||||
ctx->block_padding = block_size;
|
||||
else
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void SSL_set_record_padding_callback(SSL *ssl,
|
||||
size_t (*cb) (SSL *ssl, int type,
|
||||
size_t len, void *arg))
|
||||
{
|
||||
ssl->record_padding_cb = cb;
|
||||
}
|
||||
|
||||
void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
|
||||
{
|
||||
ssl->record_padding_arg = arg;
|
||||
}
|
||||
|
||||
void *SSL_get_record_padding_callback_arg(const SSL *ssl)
|
||||
{
|
||||
return ssl->record_padding_arg;
|
||||
}
|
||||
|
||||
int SSL_set_block_padding(SSL *ssl, size_t block_size)
|
||||
{
|
||||
/* block size of 0 or 1 is basically no padding */
|
||||
if (block_size == 1)
|
||||
ssl->block_padding = 0;
|
||||
else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
|
||||
ssl->block_padding = block_size;
|
||||
else
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_set_num_tickets(SSL *s, size_t num_tickets)
|
||||
{
|
||||
s->num_tickets = num_tickets;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t SSL_get_num_tickets(const SSL *s)
|
||||
{
|
||||
return s->num_tickets;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
|
||||
{
|
||||
ctx->num_tickets = num_tickets;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->num_tickets;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
|
||||
* variable, freeing EVP_MD_CTX previously stored in that variable, if any.
|
||||
* If EVP_MD pointer is passed, initializes ctx with this |md|.
|
||||
* Returns the newly allocated ctx;
|
||||
*/
|
||||
|
||||
EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
|
||||
{
|
||||
ssl_clear_hash_ctx(hash);
|
||||
*hash = EVP_MD_CTX_new();
|
||||
if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
|
||||
EVP_MD_CTX_free(*hash);
|
||||
*hash = NULL;
|
||||
return NULL;
|
||||
}
|
||||
return *hash;
|
||||
}
|
||||
|
||||
void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
|
||||
{
|
||||
|
||||
EVP_MD_CTX_free(*hash);
|
||||
*hash = NULL;
|
||||
}
|
||||
|
||||
/* Retrieve handshake hashes */
|
||||
int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
|
||||
size_t *hashlen)
|
||||
{
|
||||
EVP_MD_CTX *ctx = NULL;
|
||||
EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
|
||||
int hashleni = EVP_MD_CTX_size(hdgst);
|
||||
int ret = 0;
|
||||
|
||||
if (hashleni < 0 || (size_t)hashleni > outlen) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_HANDSHAKE_HASH,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ctx = EVP_MD_CTX_new();
|
||||
if (ctx == NULL)
|
||||
goto err;
|
||||
|
||||
if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
|
||||
|| EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_HANDSHAKE_HASH,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
*hashlen = hashleni;
|
||||
|
||||
ret = 1;
|
||||
err:
|
||||
EVP_MD_CTX_free(ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_session_reused(const SSL *s)
|
||||
{
|
||||
return s->hit;
|
||||
}
|
||||
|
||||
int SSL_is_server(const SSL *s)
|
||||
{
|
||||
return s->server;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
|
||||
void SSL_set_debug(SSL *s, int debug)
|
||||
{
|
||||
/* Old function was do-nothing anyway... */
|
||||
(void)s;
|
||||
(void)debug;
|
||||
}
|
||||
#endif
|
||||
|
||||
void SSL_set_security_level(SSL *s, int level)
|
||||
{
|
||||
s->cert->sec_level = level;
|
||||
}
|
||||
|
||||
int SSL_get_security_level(const SSL *s)
|
||||
{
|
||||
return s->cert->sec_level;
|
||||
}
|
||||
|
||||
void SSL_set_security_callback(SSL *s,
|
||||
int (*cb) (const SSL *s, const SSL_CTX *ctx,
|
||||
int op, int bits, int nid,
|
||||
void *other, void *ex))
|
||||
{
|
||||
s->cert->sec_cb = cb;
|
||||
}
|
||||
|
||||
int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
|
||||
const SSL_CTX *ctx, int op,
|
||||
int bits, int nid, void *other,
|
||||
void *ex) {
|
||||
return s->cert->sec_cb;
|
||||
}
|
||||
|
||||
void SSL_set0_security_ex_data(SSL *s, void *ex)
|
||||
{
|
||||
s->cert->sec_ex = ex;
|
||||
}
|
||||
|
||||
void *SSL_get0_security_ex_data(const SSL *s)
|
||||
{
|
||||
return s->cert->sec_ex;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
|
||||
{
|
||||
ctx->cert->sec_level = level;
|
||||
}
|
||||
|
||||
int SSL_CTX_get_security_level(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->cert->sec_level;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_security_callback(SSL_CTX *ctx,
|
||||
int (*cb) (const SSL *s, const SSL_CTX *ctx,
|
||||
int op, int bits, int nid,
|
||||
void *other, void *ex))
|
||||
{
|
||||
ctx->cert->sec_cb = cb;
|
||||
}
|
||||
|
||||
int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
|
||||
const SSL_CTX *ctx,
|
||||
int op, int bits,
|
||||
int nid,
|
||||
void *other,
|
||||
void *ex) {
|
||||
return ctx->cert->sec_cb;
|
||||
}
|
||||
|
||||
void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
|
||||
{
|
||||
ctx->cert->sec_ex = ex;
|
||||
}
|
||||
|
||||
void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->cert->sec_ex;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get/Set/Clear options in SSL_CTX or SSL, formerly macros, now functions that
|
||||
* can return unsigned long, instead of the generic long return value from the
|
||||
* control interface.
|
||||
*/
|
||||
unsigned long SSL_CTX_get_options(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->options;
|
||||
}
|
||||
|
||||
unsigned long SSL_get_options(const SSL *s)
|
||||
{
|
||||
return s->options;
|
||||
}
|
||||
|
||||
unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long op)
|
||||
{
|
||||
return ctx->options |= op;
|
||||
}
|
||||
|
||||
unsigned long SSL_set_options(SSL *s, unsigned long op)
|
||||
{
|
||||
return s->options |= op;
|
||||
}
|
||||
|
||||
unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op)
|
||||
{
|
||||
return ctx->options &= ~op;
|
||||
}
|
||||
|
||||
unsigned long SSL_clear_options(SSL *s, unsigned long op)
|
||||
{
|
||||
return s->options &= ~op;
|
||||
}
|
||||
|
||||
STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
|
||||
{
|
||||
return s->verified_chain;
|
||||
}
|
||||
|
||||
IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
|
||||
|
||||
#ifndef OPENSSL_NO_CT
|
||||
|
||||
/*
|
||||
* Moves SCTs from the |src| stack to the |dst| stack.
|
||||
* The source of each SCT will be set to |origin|.
|
||||
* If |dst| points to a NULL pointer, a new stack will be created and owned by
|
||||
* the caller.
|
||||
* Returns the number of SCTs moved, or a negative integer if an error occurs.
|
||||
*/
|
||||
static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
|
||||
sct_source_t origin)
|
||||
{
|
||||
int scts_moved = 0;
|
||||
SCT *sct = NULL;
|
||||
|
||||
if (*dst == NULL) {
|
||||
*dst = sk_SCT_new_null();
|
||||
if (*dst == NULL) {
|
||||
SSLerr(SSL_F_CT_MOVE_SCTS, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
while ((sct = sk_SCT_pop(src)) != NULL) {
|
||||
if (SCT_set_source(sct, origin) != 1)
|
||||
goto err;
|
||||
|
||||
if (sk_SCT_push(*dst, sct) <= 0)
|
||||
goto err;
|
||||
scts_moved += 1;
|
||||
}
|
||||
|
||||
return scts_moved;
|
||||
err:
|
||||
if (sct != NULL)
|
||||
sk_SCT_push(src, sct); /* Put the SCT back */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Look for data collected during ServerHello and parse if found.
|
||||
* Returns the number of SCTs extracted.
|
||||
*/
|
||||
static int ct_extract_tls_extension_scts(SSL *s)
|
||||
{
|
||||
int scts_extracted = 0;
|
||||
|
||||
if (s->ext.scts != NULL) {
|
||||
const unsigned char *p = s->ext.scts;
|
||||
STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
|
||||
|
||||
scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
|
||||
|
||||
SCT_LIST_free(scts);
|
||||
}
|
||||
|
||||
return scts_extracted;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks for an OCSP response and then attempts to extract any SCTs found if it
|
||||
* contains an SCT X509 extension. They will be stored in |s->scts|.
|
||||
* Returns:
|
||||
* - The number of SCTs extracted, assuming an OCSP response exists.
|
||||
* - 0 if no OCSP response exists or it contains no SCTs.
|
||||
* - A negative integer if an error occurs.
|
||||
*/
|
||||
static int ct_extract_ocsp_response_scts(SSL *s)
|
||||
{
|
||||
# ifndef OPENSSL_NO_OCSP
|
||||
int scts_extracted = 0;
|
||||
const unsigned char *p;
|
||||
OCSP_BASICRESP *br = NULL;
|
||||
OCSP_RESPONSE *rsp = NULL;
|
||||
STACK_OF(SCT) *scts = NULL;
|
||||
int i;
|
||||
|
||||
if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
|
||||
goto err;
|
||||
|
||||
p = s->ext.ocsp.resp;
|
||||
rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
|
||||
if (rsp == NULL)
|
||||
goto err;
|
||||
|
||||
br = OCSP_response_get1_basic(rsp);
|
||||
if (br == NULL)
|
||||
goto err;
|
||||
|
||||
for (i = 0; i < OCSP_resp_count(br); ++i) {
|
||||
OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
|
||||
|
||||
if (single == NULL)
|
||||
continue;
|
||||
|
||||
scts =
|
||||
OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
|
||||
scts_extracted =
|
||||
ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
|
||||
if (scts_extracted < 0)
|
||||
goto err;
|
||||
}
|
||||
err:
|
||||
SCT_LIST_free(scts);
|
||||
OCSP_BASICRESP_free(br);
|
||||
OCSP_RESPONSE_free(rsp);
|
||||
return scts_extracted;
|
||||
# else
|
||||
/* Behave as if no OCSP response exists */
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Attempts to extract SCTs from the peer certificate.
|
||||
* Return the number of SCTs extracted, or a negative integer if an error
|
||||
* occurs.
|
||||
*/
|
||||
static int ct_extract_x509v3_extension_scts(SSL *s)
|
||||
{
|
||||
int scts_extracted = 0;
|
||||
X509 *cert = s->session != NULL ? s->session->peer : NULL;
|
||||
|
||||
if (cert != NULL) {
|
||||
STACK_OF(SCT) *scts =
|
||||
X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
|
||||
|
||||
scts_extracted =
|
||||
ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
|
||||
|
||||
SCT_LIST_free(scts);
|
||||
}
|
||||
|
||||
return scts_extracted;
|
||||
}
|
||||
|
||||
/*
|
||||
* Attempts to find all received SCTs by checking TLS extensions, the OCSP
|
||||
* response (if it exists) and X509v3 extensions in the certificate.
|
||||
* Returns NULL if an error occurs.
|
||||
*/
|
||||
const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
|
||||
{
|
||||
if (!s->scts_parsed) {
|
||||
if (ct_extract_tls_extension_scts(s) < 0 ||
|
||||
ct_extract_ocsp_response_scts(s) < 0 ||
|
||||
ct_extract_x509v3_extension_scts(s) < 0)
|
||||
goto err;
|
||||
|
||||
s->scts_parsed = 1;
|
||||
}
|
||||
return s->scts;
|
||||
err:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int ct_permissive(const CT_POLICY_EVAL_CTX * ctx,
|
||||
const STACK_OF(SCT) *scts, void *unused_arg)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ct_strict(const CT_POLICY_EVAL_CTX * ctx,
|
||||
const STACK_OF(SCT) *scts, void *unused_arg)
|
||||
{
|
||||
int count = scts != NULL ? sk_SCT_num(scts) : 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < count; ++i) {
|
||||
SCT *sct = sk_SCT_value(scts, i);
|
||||
int status = SCT_get_validation_status(sct);
|
||||
|
||||
if (status == SCT_VALIDATION_STATUS_VALID)
|
||||
return 1;
|
||||
}
|
||||
SSLerr(SSL_F_CT_STRICT, SSL_R_NO_VALID_SCTS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
|
||||
void *arg)
|
||||
{
|
||||
/*
|
||||
* Since code exists that uses the custom extension handler for CT, look
|
||||
* for this and throw an error if they have already registered to use CT.
|
||||
*/
|
||||
if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
|
||||
TLSEXT_TYPE_signed_certificate_timestamp))
|
||||
{
|
||||
SSLerr(SSL_F_SSL_SET_CT_VALIDATION_CALLBACK,
|
||||
SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (callback != NULL) {
|
||||
/*
|
||||
* If we are validating CT, then we MUST accept SCTs served via OCSP
|
||||
*/
|
||||
if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
|
||||
return 0;
|
||||
}
|
||||
|
||||
s->ct_validation_callback = callback;
|
||||
s->ct_validation_callback_arg = arg;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
|
||||
ssl_ct_validation_cb callback, void *arg)
|
||||
{
|
||||
/*
|
||||
* Since code exists that uses the custom extension handler for CT, look for
|
||||
* this and throw an error if they have already registered to use CT.
|
||||
*/
|
||||
if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
|
||||
TLSEXT_TYPE_signed_certificate_timestamp))
|
||||
{
|
||||
SSLerr(SSL_F_SSL_CTX_SET_CT_VALIDATION_CALLBACK,
|
||||
SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ctx->ct_validation_callback = callback;
|
||||
ctx->ct_validation_callback_arg = arg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_ct_is_enabled(const SSL *s)
|
||||
{
|
||||
return s->ct_validation_callback != NULL;
|
||||
}
|
||||
|
||||
int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->ct_validation_callback != NULL;
|
||||
}
|
||||
|
||||
int ssl_validate_ct(SSL *s)
|
||||
{
|
||||
int ret = 0;
|
||||
X509 *cert = s->session != NULL ? s->session->peer : NULL;
|
||||
X509 *issuer;
|
||||
SSL_DANE *dane = &s->dane;
|
||||
CT_POLICY_EVAL_CTX *ctx = NULL;
|
||||
const STACK_OF(SCT) *scts;
|
||||
|
||||
/*
|
||||
* If no callback is set, the peer is anonymous, or its chain is invalid,
|
||||
* skip SCT validation - just return success. Applications that continue
|
||||
* handshakes without certificates, with unverified chains, or pinned leaf
|
||||
* certificates are outside the scope of the WebPKI and CT.
|
||||
*
|
||||
* The above exclusions notwithstanding the vast majority of peers will
|
||||
* have rather ordinary certificate chains validated by typical
|
||||
* applications that perform certificate verification and therefore will
|
||||
* process SCTs when enabled.
|
||||
*/
|
||||
if (s->ct_validation_callback == NULL || cert == NULL ||
|
||||
s->verify_result != X509_V_OK ||
|
||||
s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
|
||||
return 1;
|
||||
|
||||
/*
|
||||
* CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
|
||||
* trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
|
||||
*/
|
||||
if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
|
||||
switch (dane->mtlsa->usage) {
|
||||
case DANETLS_USAGE_DANE_TA:
|
||||
case DANETLS_USAGE_DANE_EE:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
ctx = CT_POLICY_EVAL_CTX_new();
|
||||
if (ctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_VALIDATE_CT,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
issuer = sk_X509_value(s->verified_chain, 1);
|
||||
CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
|
||||
CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
|
||||
CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx, s->ctx->ctlog_store);
|
||||
CT_POLICY_EVAL_CTX_set_time(
|
||||
ctx, (uint64_t)SSL_SESSION_get_time(SSL_get0_session(s)) * 1000);
|
||||
|
||||
scts = SSL_get0_peer_scts(s);
|
||||
|
||||
/*
|
||||
* This function returns success (> 0) only when all the SCTs are valid, 0
|
||||
* when some are invalid, and < 0 on various internal errors (out of
|
||||
* memory, etc.). Having some, or even all, invalid SCTs is not sufficient
|
||||
* reason to abort the handshake, that decision is up to the callback.
|
||||
* Therefore, we error out only in the unexpected case that the return
|
||||
* value is negative.
|
||||
*
|
||||
* XXX: One might well argue that the return value of this function is an
|
||||
* unfortunate design choice. Its job is only to determine the validation
|
||||
* status of each of the provided SCTs. So long as it correctly separates
|
||||
* the wheat from the chaff it should return success. Failure in this case
|
||||
* ought to correspond to an inability to carry out its duties.
|
||||
*/
|
||||
if (SCT_LIST_validate(scts, ctx) < 0) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL_VALIDATE_CT,
|
||||
SSL_R_SCT_VERIFICATION_FAILED);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
|
||||
if (ret < 0)
|
||||
ret = 0; /* This function returns 0 on failure */
|
||||
if (!ret)
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL_VALIDATE_CT,
|
||||
SSL_R_CALLBACK_FAILED);
|
||||
|
||||
end:
|
||||
CT_POLICY_EVAL_CTX_free(ctx);
|
||||
/*
|
||||
* With SSL_VERIFY_NONE the session may be cached and re-used despite a
|
||||
* failure return code here. Also the application may wish the complete
|
||||
* the handshake, and then disconnect cleanly at a higher layer, after
|
||||
* checking the verification status of the completed connection.
|
||||
*
|
||||
* We therefore force a certificate verification failure which will be
|
||||
* visible via SSL_get_verify_result() and cached as part of any resumed
|
||||
* session.
|
||||
*
|
||||
* Note: the permissive callback is for information gathering only, always
|
||||
* returns success, and does not affect verification status. Only the
|
||||
* strict callback or a custom application-specified callback can trigger
|
||||
* connection failure or record a verification error.
|
||||
*/
|
||||
if (ret <= 0)
|
||||
s->verify_result = X509_V_ERR_NO_VALID_SCTS;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
|
||||
{
|
||||
switch (validation_mode) {
|
||||
default:
|
||||
SSLerr(SSL_F_SSL_CTX_ENABLE_CT, SSL_R_INVALID_CT_VALIDATION_TYPE);
|
||||
return 0;
|
||||
case SSL_CT_VALIDATION_PERMISSIVE:
|
||||
return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
|
||||
case SSL_CT_VALIDATION_STRICT:
|
||||
return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int SSL_enable_ct(SSL *s, int validation_mode)
|
||||
{
|
||||
switch (validation_mode) {
|
||||
default:
|
||||
SSLerr(SSL_F_SSL_ENABLE_CT, SSL_R_INVALID_CT_VALIDATION_TYPE);
|
||||
return 0;
|
||||
case SSL_CT_VALIDATION_PERMISSIVE:
|
||||
return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
|
||||
case SSL_CT_VALIDATION_STRICT:
|
||||
return SSL_set_ct_validation_callback(s, ct_strict, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
|
||||
{
|
||||
return CTLOG_STORE_load_default_file(ctx->ctlog_store);
|
||||
}
|
||||
|
||||
int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
|
||||
{
|
||||
return CTLOG_STORE_load_file(ctx->ctlog_store, path);
|
||||
}
|
||||
|
||||
void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE * logs)
|
||||
{
|
||||
CTLOG_STORE_free(ctx->ctlog_store);
|
||||
ctx->ctlog_store = logs;
|
||||
}
|
||||
|
||||
const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->ctlog_store;
|
||||
}
|
||||
|
||||
#endif /* OPENSSL_NO_CT */
|
||||
|
||||
void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
|
||||
void *arg)
|
||||
{
|
||||
c->client_hello_cb = cb;
|
||||
c->client_hello_cb_arg = arg;
|
||||
}
|
||||
|
||||
int SSL_client_hello_isv2(SSL *s)
|
||||
{
|
||||
if (s->clienthello == NULL)
|
||||
return 0;
|
||||
return s->clienthello->isv2;
|
||||
}
|
||||
|
||||
unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
|
||||
{
|
||||
if (s->clienthello == NULL)
|
||||
return 0;
|
||||
return s->clienthello->legacy_version;
|
||||
}
|
||||
|
||||
size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
|
||||
{
|
||||
if (s->clienthello == NULL)
|
||||
return 0;
|
||||
if (out != NULL)
|
||||
*out = s->clienthello->random;
|
||||
return SSL3_RANDOM_SIZE;
|
||||
}
|
||||
|
||||
size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
|
||||
{
|
||||
if (s->clienthello == NULL)
|
||||
return 0;
|
||||
if (out != NULL)
|
||||
*out = s->clienthello->session_id;
|
||||
return s->clienthello->session_id_len;
|
||||
}
|
||||
|
||||
size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
|
||||
{
|
||||
if (s->clienthello == NULL)
|
||||
return 0;
|
||||
if (out != NULL)
|
||||
*out = PACKET_data(&s->clienthello->ciphersuites);
|
||||
return PACKET_remaining(&s->clienthello->ciphersuites);
|
||||
}
|
||||
|
||||
size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
|
||||
{
|
||||
if (s->clienthello == NULL)
|
||||
return 0;
|
||||
if (out != NULL)
|
||||
*out = s->clienthello->compressions;
|
||||
return s->clienthello->compressions_len;
|
||||
}
|
||||
|
||||
int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
|
||||
{
|
||||
RAW_EXTENSION *ext;
|
||||
int *present;
|
||||
size_t num = 0, i;
|
||||
|
||||
if (s->clienthello == NULL || out == NULL || outlen == NULL)
|
||||
return 0;
|
||||
for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
|
||||
ext = s->clienthello->pre_proc_exts + i;
|
||||
if (ext->present)
|
||||
num++;
|
||||
}
|
||||
if (num == 0) {
|
||||
*out = NULL;
|
||||
*outlen = 0;
|
||||
return 1;
|
||||
}
|
||||
if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL) {
|
||||
SSLerr(SSL_F_SSL_CLIENT_HELLO_GET1_EXTENSIONS_PRESENT,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
|
||||
ext = s->clienthello->pre_proc_exts + i;
|
||||
if (ext->present) {
|
||||
if (ext->received_order >= num)
|
||||
goto err;
|
||||
present[ext->received_order] = ext->type;
|
||||
}
|
||||
}
|
||||
*out = present;
|
||||
*outlen = num;
|
||||
return 1;
|
||||
err:
|
||||
OPENSSL_free(present);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
|
||||
size_t *outlen)
|
||||
{
|
||||
size_t i;
|
||||
RAW_EXTENSION *r;
|
||||
|
||||
if (s->clienthello == NULL)
|
||||
return 0;
|
||||
for (i = 0; i < s->clienthello->pre_proc_exts_len; ++i) {
|
||||
r = s->clienthello->pre_proc_exts + i;
|
||||
if (r->present && r->type == type) {
|
||||
if (out != NULL)
|
||||
*out = PACKET_data(&r->data);
|
||||
if (outlen != NULL)
|
||||
*outlen = PACKET_remaining(&r->data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SSL_free_buffers(SSL *ssl)
|
||||
{
|
||||
RECORD_LAYER *rl = &ssl->rlayer;
|
||||
|
||||
if (RECORD_LAYER_read_pending(rl) || RECORD_LAYER_write_pending(rl))
|
||||
return 0;
|
||||
|
||||
RECORD_LAYER_release(rl);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_alloc_buffers(SSL *ssl)
|
||||
{
|
||||
return ssl3_setup_buffers(ssl);
|
||||
}
|
||||
|
||||
void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
|
||||
{
|
||||
ctx->keylog_callback = cb;
|
||||
}
|
||||
|
||||
SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->keylog_callback;
|
||||
}
|
||||
|
||||
static int nss_keylog_int(const char *prefix,
|
||||
SSL *ssl,
|
||||
const uint8_t *parameter_1,
|
||||
size_t parameter_1_len,
|
||||
const uint8_t *parameter_2,
|
||||
size_t parameter_2_len)
|
||||
{
|
||||
char *out = NULL;
|
||||
char *cursor = NULL;
|
||||
size_t out_len = 0;
|
||||
size_t i;
|
||||
size_t prefix_len;
|
||||
|
||||
if (ssl->ctx->keylog_callback == NULL)
|
||||
return 1;
|
||||
|
||||
/*
|
||||
* Our output buffer will contain the following strings, rendered with
|
||||
* space characters in between, terminated by a NULL character: first the
|
||||
* prefix, then the first parameter, then the second parameter. The
|
||||
* meaning of each parameter depends on the specific key material being
|
||||
* logged. Note that the first and second parameters are encoded in
|
||||
* hexadecimal, so we need a buffer that is twice their lengths.
|
||||
*/
|
||||
prefix_len = strlen(prefix);
|
||||
out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
|
||||
if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) {
|
||||
SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, SSL_F_NSS_KEYLOG_INT,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
strcpy(cursor, prefix);
|
||||
cursor += prefix_len;
|
||||
*cursor++ = ' ';
|
||||
|
||||
for (i = 0; i < parameter_1_len; i++) {
|
||||
sprintf(cursor, "%02x", parameter_1[i]);
|
||||
cursor += 2;
|
||||
}
|
||||
*cursor++ = ' ';
|
||||
|
||||
for (i = 0; i < parameter_2_len; i++) {
|
||||
sprintf(cursor, "%02x", parameter_2[i]);
|
||||
cursor += 2;
|
||||
}
|
||||
*cursor = '\0';
|
||||
|
||||
ssl->ctx->keylog_callback(ssl, (const char *)out);
|
||||
OPENSSL_clear_free(out, out_len);
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int ssl_log_rsa_client_key_exchange(SSL *ssl,
|
||||
const uint8_t *encrypted_premaster,
|
||||
size_t encrypted_premaster_len,
|
||||
const uint8_t *premaster,
|
||||
size_t premaster_len)
|
||||
{
|
||||
if (encrypted_premaster_len < 8) {
|
||||
SSLfatal(ssl, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_SSL_LOG_RSA_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* We only want the first 8 bytes of the encrypted premaster as a tag. */
|
||||
return nss_keylog_int("RSA",
|
||||
ssl,
|
||||
encrypted_premaster,
|
||||
8,
|
||||
premaster,
|
||||
premaster_len);
|
||||
}
|
||||
|
||||
int ssl_log_secret(SSL *ssl,
|
||||
const char *label,
|
||||
const uint8_t *secret,
|
||||
size_t secret_len)
|
||||
{
|
||||
return nss_keylog_int(label,
|
||||
ssl,
|
||||
ssl->s3.client_random,
|
||||
SSL3_RANDOM_SIZE,
|
||||
secret,
|
||||
secret_len);
|
||||
}
|
||||
|
||||
#define SSLV2_CIPHER_LEN 3
|
||||
|
||||
int ssl_cache_cipherlist(SSL *s, PACKET *cipher_suites, int sslv2format)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
|
||||
|
||||
if (PACKET_remaining(cipher_suites) == 0) {
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL_CACHE_CIPHERLIST,
|
||||
SSL_R_NO_CIPHERS_SPECIFIED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (PACKET_remaining(cipher_suites) % n != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
|
||||
SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_free(s->s3.tmp.ciphers_raw);
|
||||
s->s3.tmp.ciphers_raw = NULL;
|
||||
s->s3.tmp.ciphers_rawlen = 0;
|
||||
|
||||
if (sslv2format) {
|
||||
size_t numciphers = PACKET_remaining(cipher_suites) / n;
|
||||
PACKET sslv2ciphers = *cipher_suites;
|
||||
unsigned int leadbyte;
|
||||
unsigned char *raw;
|
||||
|
||||
/*
|
||||
* We store the raw ciphers list in SSLv3+ format so we need to do some
|
||||
* preprocessing to convert the list first. If there are any SSLv2 only
|
||||
* ciphersuites with a non-zero leading byte then we are going to
|
||||
* slightly over allocate because we won't store those. But that isn't a
|
||||
* problem.
|
||||
*/
|
||||
raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
|
||||
s->s3.tmp.ciphers_raw = raw;
|
||||
if (raw == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
for (s->s3.tmp.ciphers_rawlen = 0;
|
||||
PACKET_remaining(&sslv2ciphers) > 0;
|
||||
raw += TLS_CIPHER_LEN) {
|
||||
if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
|
||||
|| (leadbyte == 0
|
||||
&& !PACKET_copy_bytes(&sslv2ciphers, raw,
|
||||
TLS_CIPHER_LEN))
|
||||
|| (leadbyte != 0
|
||||
&& !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
|
||||
SSL_R_BAD_PACKET);
|
||||
OPENSSL_free(s->s3.tmp.ciphers_raw);
|
||||
s->s3.tmp.ciphers_raw = NULL;
|
||||
s->s3.tmp.ciphers_rawlen = 0;
|
||||
return 0;
|
||||
}
|
||||
if (leadbyte == 0)
|
||||
s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
|
||||
}
|
||||
} else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
|
||||
&s->s3.tmp.ciphers_rawlen)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
|
||||
int isv2format, STACK_OF(SSL_CIPHER) **sk,
|
||||
STACK_OF(SSL_CIPHER) **scsvs)
|
||||
{
|
||||
PACKET pkt;
|
||||
|
||||
if (!PACKET_buf_init(&pkt, bytes, len))
|
||||
return 0;
|
||||
return bytes_to_cipher_list(s, &pkt, sk, scsvs, isv2format, 0);
|
||||
}
|
||||
|
||||
int bytes_to_cipher_list(SSL *s, PACKET *cipher_suites,
|
||||
STACK_OF(SSL_CIPHER) **skp,
|
||||
STACK_OF(SSL_CIPHER) **scsvs_out,
|
||||
int sslv2format, int fatal)
|
||||
{
|
||||
const SSL_CIPHER *c;
|
||||
STACK_OF(SSL_CIPHER) *sk = NULL;
|
||||
STACK_OF(SSL_CIPHER) *scsvs = NULL;
|
||||
int n;
|
||||
/* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
|
||||
unsigned char cipher[SSLV2_CIPHER_LEN];
|
||||
|
||||
n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
|
||||
|
||||
if (PACKET_remaining(cipher_suites) == 0) {
|
||||
if (fatal)
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_BYTES_TO_CIPHER_LIST,
|
||||
SSL_R_NO_CIPHERS_SPECIFIED);
|
||||
else
|
||||
SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (PACKET_remaining(cipher_suites) % n != 0) {
|
||||
if (fatal)
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
|
||||
SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
|
||||
else
|
||||
SSLerr(SSL_F_BYTES_TO_CIPHER_LIST,
|
||||
SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
|
||||
return 0;
|
||||
}
|
||||
|
||||
sk = sk_SSL_CIPHER_new_null();
|
||||
scsvs = sk_SSL_CIPHER_new_null();
|
||||
if (sk == NULL || scsvs == NULL) {
|
||||
if (fatal)
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
else
|
||||
SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
|
||||
/*
|
||||
* SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
|
||||
* first byte set to zero, while true SSLv2 ciphers have a non-zero
|
||||
* first byte. We don't support any true SSLv2 ciphers, so skip them.
|
||||
*/
|
||||
if (sslv2format && cipher[0] != '\0')
|
||||
continue;
|
||||
|
||||
/* For SSLv2-compat, ignore leading 0-byte. */
|
||||
c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
|
||||
if (c != NULL) {
|
||||
if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
|
||||
(!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
|
||||
if (fatal)
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
|
||||
else
|
||||
SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (PACKET_remaining(cipher_suites) > 0) {
|
||||
if (fatal)
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
|
||||
SSL_R_BAD_LENGTH);
|
||||
else
|
||||
SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, SSL_R_BAD_LENGTH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (skp != NULL)
|
||||
*skp = sk;
|
||||
else
|
||||
sk_SSL_CIPHER_free(sk);
|
||||
if (scsvs_out != NULL)
|
||||
*scsvs_out = scsvs;
|
||||
else
|
||||
sk_SSL_CIPHER_free(scsvs);
|
||||
return 1;
|
||||
err:
|
||||
sk_SSL_CIPHER_free(sk);
|
||||
sk_SSL_CIPHER_free(scsvs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
|
||||
{
|
||||
ctx->max_early_data = max_early_data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->max_early_data;
|
||||
}
|
||||
|
||||
int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
|
||||
{
|
||||
s->max_early_data = max_early_data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t SSL_get_max_early_data(const SSL *s)
|
||||
{
|
||||
return s->max_early_data;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
|
||||
{
|
||||
ctx->recv_max_early_data = recv_max_early_data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
|
||||
{
|
||||
return ctx->recv_max_early_data;
|
||||
}
|
||||
|
||||
int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
|
||||
{
|
||||
s->recv_max_early_data = recv_max_early_data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t SSL_get_recv_max_early_data(const SSL *s)
|
||||
{
|
||||
return s->recv_max_early_data;
|
||||
}
|
||||
|
||||
__owur unsigned int ssl_get_max_send_fragment(const SSL *ssl)
|
||||
{
|
||||
/* Return any active Max Fragment Len extension */
|
||||
if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session))
|
||||
return GET_MAX_FRAGMENT_LENGTH(ssl->session);
|
||||
|
||||
/* return current SSL connection setting */
|
||||
return ssl->max_send_fragment;
|
||||
}
|
||||
|
||||
__owur unsigned int ssl_get_split_send_fragment(const SSL *ssl)
|
||||
{
|
||||
/* Return a value regarding an active Max Fragment Len extension */
|
||||
if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session)
|
||||
&& ssl->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(ssl->session))
|
||||
return GET_MAX_FRAGMENT_LENGTH(ssl->session);
|
||||
|
||||
/* else limit |split_send_fragment| to current |max_send_fragment| */
|
||||
if (ssl->split_send_fragment > ssl->max_send_fragment)
|
||||
return ssl->max_send_fragment;
|
||||
|
||||
/* return current SSL connection setting */
|
||||
return ssl->split_send_fragment;
|
||||
}
|
||||
|
||||
int SSL_stateless(SSL *s)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Ensure there is no state left over from a previous invocation */
|
||||
if (!SSL_clear(s))
|
||||
return 0;
|
||||
|
||||
ERR_clear_error();
|
||||
|
||||
s->s3.flags |= TLS1_FLAGS_STATELESS;
|
||||
ret = SSL_accept(s);
|
||||
s->s3.flags &= ~TLS1_FLAGS_STATELESS;
|
||||
|
||||
if (ret > 0 && s->ext.cookieok)
|
||||
return 1;
|
||||
|
||||
if (s->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(s))
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
|
||||
{
|
||||
ctx->pha_enabled = val;
|
||||
}
|
||||
|
||||
void SSL_set_post_handshake_auth(SSL *ssl, int val)
|
||||
{
|
||||
ssl->pha_enabled = val;
|
||||
}
|
||||
|
||||
int SSL_verify_client_post_handshake(SSL *ssl)
|
||||
{
|
||||
if (!SSL_IS_TLS13(ssl)) {
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_WRONG_SSL_VERSION);
|
||||
return 0;
|
||||
}
|
||||
if (!ssl->server) {
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_NOT_SERVER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!SSL_is_init_finished(ssl)) {
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_STILL_IN_INIT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (ssl->post_handshake_auth) {
|
||||
case SSL_PHA_NONE:
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_EXTENSION_NOT_RECEIVED);
|
||||
return 0;
|
||||
default:
|
||||
case SSL_PHA_EXT_SENT:
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
case SSL_PHA_EXT_RECEIVED:
|
||||
break;
|
||||
case SSL_PHA_REQUEST_PENDING:
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_REQUEST_PENDING);
|
||||
return 0;
|
||||
case SSL_PHA_REQUESTED:
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_REQUEST_SENT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssl->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
|
||||
|
||||
/* checks verify_mode and algorithm_auth */
|
||||
if (!send_certificate_request(ssl)) {
|
||||
ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
|
||||
SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_INVALID_CONFIG);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ossl_statem_set_in_init(ssl, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
|
||||
SSL_CTX_generate_session_ticket_fn gen_cb,
|
||||
SSL_CTX_decrypt_session_ticket_fn dec_cb,
|
||||
void *arg)
|
||||
{
|
||||
ctx->generate_ticket_cb = gen_cb;
|
||||
ctx->decrypt_ticket_cb = dec_cb;
|
||||
ctx->ticket_cb_data = arg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
|
||||
SSL_allow_early_data_cb_fn cb,
|
||||
void *arg)
|
||||
{
|
||||
ctx->allow_early_data_cb = cb;
|
||||
ctx->allow_early_data_cb_data = arg;
|
||||
}
|
||||
|
||||
void SSL_set_allow_early_data_cb(SSL *s,
|
||||
SSL_allow_early_data_cb_fn cb,
|
||||
void *arg)
|
||||
{
|
||||
s->allow_early_data_cb = cb;
|
||||
s->allow_early_data_cb_data = arg;
|
||||
}
|
||||
+38
-2
@@ -738,6 +738,32 @@ typedef struct ssl_ctx_ext_secure_st {
|
||||
unsigned char tick_aes_key[TLSEXT_TICK_KEY_LENGTH];
|
||||
} SSL_CTX_EXT_SECURE;
|
||||
|
||||
/*
|
||||
* Helper function for HMAC
|
||||
* The structure should be considered opaque, it will change once the low
|
||||
* level deprecated calls are removed. At that point it can be replaced
|
||||
* by EVP_MAC_CTX and most of the functions converted to macros or inlined
|
||||
* directly.
|
||||
*/
|
||||
typedef struct ssl_hmac_st {
|
||||
EVP_MAC_CTX *ctx;
|
||||
# ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
HMAC_CTX *old_ctx;
|
||||
# endif
|
||||
} SSL_HMAC;
|
||||
|
||||
SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx);
|
||||
void ssl_hmac_free(SSL_HMAC *ctx);
|
||||
# ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
HMAC_CTX *ssl_hmac_get0_HMAC_CTX(SSL_HMAC *ctx);
|
||||
# endif
|
||||
EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx);
|
||||
int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md);
|
||||
int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len);
|
||||
int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len,
|
||||
size_t max_size);
|
||||
size_t ssl_hmac_size(const SSL_HMAC *ctx);
|
||||
|
||||
/* ssl_cipher_preference_list_st contains a list of SSL_CIPHERs with
|
||||
* equal-preference groups. For TLS clients, the groups are moot because the
|
||||
* server picks the cipher and groups cannot be expressed on the wire. However,
|
||||
@@ -776,6 +802,8 @@ struct ssl_cipher_preference_list_st {
|
||||
|
||||
|
||||
struct ssl_ctx_st {
|
||||
OPENSSL_CTX *libctx;
|
||||
|
||||
const SSL_METHOD *method;
|
||||
struct ssl_cipher_preference_list_st *cipher_list;
|
||||
/* same as above but sorted for lookup */
|
||||
@@ -973,10 +1001,16 @@ struct ssl_ctx_st {
|
||||
/* RFC 4507 session ticket keys */
|
||||
unsigned char tick_key_name[TLSEXT_KEYNAME_LENGTH];
|
||||
SSL_CTX_EXT_SECURE *secure;
|
||||
# ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
/* Callback to support customisation of ticket key setting */
|
||||
int (*ticket_key_cb) (SSL *ssl,
|
||||
unsigned char *name, unsigned char *iv,
|
||||
EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc);
|
||||
#endif
|
||||
int (*ticket_key_evp_cb) (SSL *ssl,
|
||||
unsigned char *name, unsigned char *iv,
|
||||
EVP_CIPHER_CTX *ectx, EVP_MAC_CTX *hctx,
|
||||
int enc);
|
||||
|
||||
/* certificate status request info */
|
||||
/* Callback for status request */
|
||||
@@ -1112,6 +1146,8 @@ struct ssl_ctx_st {
|
||||
/* Callback for SSL async handling */
|
||||
SSL_async_callback_fn async_cb;
|
||||
void *async_cb_arg;
|
||||
|
||||
char *propq;
|
||||
};
|
||||
|
||||
typedef struct cert_pkey_st CERT_PKEY;
|
||||
@@ -2391,7 +2427,7 @@ __owur int ssl_fill_hello_random(SSL *s, int server, unsigned char *field,
|
||||
size_t len, DOWNGRADE dgrd);
|
||||
__owur int ssl_generate_master_secret(SSL *s, unsigned char *pms, size_t pmslen,
|
||||
int free_pms);
|
||||
__owur EVP_PKEY *ssl_generate_pkey(EVP_PKEY *pm);
|
||||
__owur EVP_PKEY *ssl_generate_pkey(SSL *s, EVP_PKEY *pm);
|
||||
__owur int ssl_derive(SSL *s, EVP_PKEY *privkey, EVP_PKEY *pubkey,
|
||||
int genmaster);
|
||||
__owur EVP_PKEY *ssl_dh_to_pkey(DH *dh);
|
||||
@@ -2648,7 +2684,7 @@ __owur int tls_check_sigalg_curve(const SSL *s, int curve);
|
||||
# endif
|
||||
__owur int tls12_check_peer_sigalg(SSL *s, uint16_t, EVP_PKEY *pkey);
|
||||
__owur int ssl_set_client_disabled(SSL *s);
|
||||
__owur int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int echde);
|
||||
__owur int ssl_cipher_disabled(const SSL *s, const SSL_CIPHER *c, int op, int echde);
|
||||
|
||||
__owur int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
|
||||
size_t *hashlen);
|
||||
|
||||
+1
-1
@@ -259,7 +259,7 @@ static int def_generate_session_id(SSL *ssl, unsigned char *id,
|
||||
{
|
||||
unsigned int retry = 0;
|
||||
do
|
||||
if (RAND_bytes(id, *id_len) <= 0)
|
||||
if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len) <= 0)
|
||||
return 0;
|
||||
while (SSL_has_matching_session_id(ssl, id, *id_len) &&
|
||||
(++retry < MAX_SESS_ID_ATTEMPTS)) ;
|
||||
|
||||
@@ -948,7 +948,6 @@ static int final_server_name(SSL *s, unsigned int context, int sent)
|
||||
* was successful.
|
||||
*/
|
||||
if (s->server) {
|
||||
/* TODO(OpenSSL1.2) revisit !sent case */
|
||||
if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
|
||||
/* Only store the hostname in the session if we accepted it. */
|
||||
OPENSSL_free(s->session->ext.hostname);
|
||||
@@ -1010,6 +1009,7 @@ static int final_server_name(SSL *s, unsigned int context, int sent)
|
||||
/* TLSv1.3 doesn't have warning alerts so we suppress this */
|
||||
if (!SSL_IS_TLS13(s))
|
||||
ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
|
||||
s->servername_done = 0;
|
||||
return 1;
|
||||
|
||||
case SSL_TLSEXT_ERR_NOACK:
|
||||
|
||||
@@ -127,6 +127,10 @@ int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3
|
||||
* we always use the SNI value from the handshake.
|
||||
*/
|
||||
if (!s->hit || SSL_IS_TLS13(s)) {
|
||||
if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
|
||||
SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME,
|
||||
@@ -155,8 +159,12 @@ int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
|
||||
}
|
||||
|
||||
s->servername_done = 1;
|
||||
}
|
||||
if (s->hit) {
|
||||
} else {
|
||||
/*
|
||||
* In TLSv1.2 and below we should check if the SNI is consistent between
|
||||
* the initial handshake and the resumption. In TLSv1.3 SNI is not
|
||||
* associated with the session.
|
||||
*/
|
||||
/*
|
||||
* TODO(openssl-team): if the SNI doesn't match, we MUST
|
||||
* fall back to a full handshake.
|
||||
@@ -164,9 +172,6 @@ int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
|
||||
s->servername_done = (s->session->ext.hostname != NULL)
|
||||
&& PACKET_equal(&hostname, s->session->ext.hostname,
|
||||
strlen(s->session->ext.hostname));
|
||||
|
||||
if (!s->servername_done && s->session->ext.hostname != NULL)
|
||||
s->ext.early_data_ok = 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
@@ -1333,8 +1338,14 @@ EXT_RETURN tls_construct_stoc_server_name(SSL *s, WPACKET *pkt,
|
||||
unsigned int context, X509 *x,
|
||||
size_t chainidx)
|
||||
{
|
||||
if (s->hit || s->servername_done != 1
|
||||
|| s->ext.hostname == NULL)
|
||||
if (s->servername_done != 1)
|
||||
return EXT_RETURN_NOT_SENT;
|
||||
|
||||
/*
|
||||
* Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
|
||||
* We just use the servername from the initial handshake.
|
||||
*/
|
||||
if (s->hit && !SSL_IS_TLS13(s))
|
||||
return EXT_RETURN_NOT_SENT;
|
||||
|
||||
if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
|
||||
@@ -1716,7 +1727,7 @@ EXT_RETURN tls_construct_stoc_key_share(SSL *s, WPACKET *pkt,
|
||||
return EXT_RETURN_FAIL;
|
||||
}
|
||||
|
||||
skey = ssl_generate_pkey(ckey);
|
||||
skey = ssl_generate_pkey(s, ckey);
|
||||
if (skey == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
|
||||
@@ -1201,7 +1201,8 @@ int tls_construct_client_hello(SSL *s, WPACKET *pkt)
|
||||
s->tmp_session_id_len = sess_id_len;
|
||||
session_id = s->tmp_session_id;
|
||||
if (s->hello_retry_request == SSL_HRR_NONE
|
||||
&& RAND_bytes(s->tmp_session_id, sess_id_len) <= 0) {
|
||||
&& RAND_bytes_ex(s->ctx->libctx, s->tmp_session_id,
|
||||
sess_id_len) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
@@ -2976,7 +2977,7 @@ static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt)
|
||||
pms[0] = s->client_version >> 8;
|
||||
pms[1] = s->client_version & 0xff;
|
||||
/* TODO(size_t): Convert this function */
|
||||
if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
|
||||
if (RAND_bytes_ex(s->ctx->libctx, pms + 2, (int)(pmslen - 2)) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_RSA,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
@@ -3048,7 +3049,7 @@ static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt)
|
||||
goto err;
|
||||
}
|
||||
|
||||
ckey = ssl_generate_pkey(skey);
|
||||
ckey = ssl_generate_pkey(s, skey);
|
||||
if (ckey == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_DHE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
@@ -3106,7 +3107,7 @@ static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt)
|
||||
return 0;
|
||||
}
|
||||
|
||||
ckey = ssl_generate_pkey(skey);
|
||||
ckey = ssl_generate_pkey(s, skey);
|
||||
if (ckey == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_ECDHE,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
@@ -3172,7 +3173,9 @@ static int tls_construct_cke_gost(SSL *s, WPACKET *pkt)
|
||||
return 0;
|
||||
}
|
||||
|
||||
pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
|
||||
pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx,
|
||||
X509_get0_pubkey(peer_cert),
|
||||
s->ctx->propq);
|
||||
if (pkey_ctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
@@ -3197,7 +3200,7 @@ static int tls_construct_cke_gost(SSL *s, WPACKET *pkt)
|
||||
/* Generate session key
|
||||
* TODO(size_t): Convert this function
|
||||
*/
|
||||
|| RAND_bytes(pms, (int)pmslen) <= 0) {
|
||||
|| RAND_bytes_ex(s->ctx->libctx, pms, (int)pmslen) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
|
||||
+38
-23
@@ -18,7 +18,6 @@
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/bn.h>
|
||||
@@ -2570,7 +2569,7 @@ int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
|
||||
goto err;
|
||||
}
|
||||
|
||||
s->s3.tmp.pkey = ssl_generate_pkey(pkdhp);
|
||||
s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
|
||||
if (s->s3.tmp.pkey == NULL) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
@@ -2845,7 +2844,8 @@ int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
|
||||
OPENSSL_free(s->pha_context);
|
||||
s->pha_context_len = 32;
|
||||
if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL
|
||||
|| RAND_bytes(s->pha_context, s->pha_context_len) <= 0
|
||||
|| RAND_bytes_ex(s->ctx->libctx, s->pha_context,
|
||||
s->pha_context_len) <= 0
|
||||
|| !WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
|
||||
@@ -3014,7 +3014,7 @@ static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
|
||||
return 0;
|
||||
}
|
||||
|
||||
ctx = EVP_PKEY_CTX_new(rsa, NULL);
|
||||
ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, rsa, s->ctx->propq);
|
||||
if (ctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
@@ -3297,7 +3297,7 @@ static int tls_process_cke_gost(SSL *s, PACKET *pkt)
|
||||
pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
|
||||
}
|
||||
|
||||
pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
|
||||
pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pk, s->ctx->propq);
|
||||
if (pkey_ctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
@@ -3779,12 +3779,12 @@ static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
{
|
||||
unsigned char *senc = NULL;
|
||||
EVP_CIPHER_CTX *ctx = NULL;
|
||||
HMAC_CTX *hctx = NULL;
|
||||
SSL_HMAC *hctx = NULL;
|
||||
unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
|
||||
const unsigned char *const_p;
|
||||
int len, slen_full, slen, lenfinal;
|
||||
SSL_SESSION *sess;
|
||||
unsigned int hlen;
|
||||
size_t hlen;
|
||||
SSL_CTX *tctx = s->session_ctx;
|
||||
unsigned char iv[EVP_MAX_IV_LENGTH];
|
||||
unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
|
||||
@@ -3810,7 +3810,7 @@ static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
}
|
||||
|
||||
ctx = EVP_CIPHER_CTX_new();
|
||||
hctx = HMAC_CTX_new();
|
||||
hctx = ssl_hmac_new(tctx);
|
||||
if (ctx == NULL || hctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
@@ -3856,10 +3856,24 @@ static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
* Initialize HMAC and cipher contexts. If callback present it does
|
||||
* all the work otherwise use generated values from parent ctx.
|
||||
*/
|
||||
if (tctx->ext.ticket_key_cb) {
|
||||
/* if 0 is returned, write an empty ticket */
|
||||
int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
|
||||
hctx, 1);
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
|
||||
#else
|
||||
if (tctx->ext.ticket_key_evp_cb != NULL)
|
||||
#endif
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (tctx->ext.ticket_key_evp_cb != NULL)
|
||||
ret = tctx->ext.ticket_key_evp_cb(s, key_name, iv, ctx,
|
||||
ssl_hmac_get0_EVP_MAC_CTX(hctx),
|
||||
1);
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
else if (tctx->ext.ticket_key_cb != NULL)
|
||||
/* if 0 is returned, write an empty ticket */
|
||||
ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
|
||||
ssl_hmac_get0_HMAC_CTX(hctx), 1);
|
||||
#endif
|
||||
|
||||
if (ret == 0) {
|
||||
|
||||
@@ -3873,7 +3887,7 @@ static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
}
|
||||
OPENSSL_free(senc);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
HMAC_CTX_free(hctx);
|
||||
ssl_hmac_free(hctx);
|
||||
return 1;
|
||||
}
|
||||
if (ret < 0) {
|
||||
@@ -3886,12 +3900,12 @@ static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
const EVP_CIPHER *cipher = EVP_aes_256_cbc();
|
||||
|
||||
iv_len = EVP_CIPHER_iv_length(cipher);
|
||||
if (RAND_bytes(iv, iv_len) <= 0
|
||||
if (RAND_bytes_ex(s->ctx->libctx, iv, iv_len) <= 0
|
||||
|| !EVP_EncryptInit_ex(ctx, cipher, NULL,
|
||||
tctx->ext.secure->tick_aes_key, iv)
|
||||
|| !HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
|
||||
sizeof(tctx->ext.secure->tick_hmac_key),
|
||||
EVP_sha256(), NULL)) {
|
||||
|| !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
|
||||
sizeof(tctx->ext.secure->tick_hmac_key),
|
||||
"SHA256")) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
@@ -3921,11 +3935,11 @@ static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
|| encdata1 + len != encdata2
|
||||
|| len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
|
||||
|| !WPACKET_get_total_written(pkt, &macendoffset)
|
||||
|| !HMAC_Update(hctx,
|
||||
(unsigned char *)s->init_buf->data + macoffset,
|
||||
macendoffset - macoffset)
|
||||
|| !ssl_hmac_update(hctx,
|
||||
(unsigned char *)s->init_buf->data + macoffset,
|
||||
macendoffset - macoffset)
|
||||
|| !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
|
||||
|| !HMAC_Final(hctx, macdata1, &hlen)
|
||||
|| !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
|
||||
|| hlen > EVP_MAX_MD_SIZE
|
||||
|| !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
|
||||
|| macdata1 != macdata2) {
|
||||
@@ -3945,7 +3959,7 @@ static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
err:
|
||||
OPENSSL_free(senc);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
HMAC_CTX_free(hctx);
|
||||
ssl_hmac_free(hctx);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -4016,7 +4030,8 @@ int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0) {
|
||||
if (RAND_bytes_ex(s->ctx->libctx, age_add_u.age_add_c,
|
||||
sizeof(age_add_u)) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
|
||||
@@ -1,4215 +0,0 @@
|
||||
/*
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
|
||||
* Copyright 2005 Nokia. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "../ssl_local.h"
|
||||
#include "statem_local.h"
|
||||
#include "internal/constant_time.h"
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/buffer.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/trace.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/asn1t.h>
|
||||
|
||||
#define TICKET_NONCE_SIZE 8
|
||||
|
||||
typedef struct {
|
||||
ASN1_TYPE *kxBlob;
|
||||
ASN1_TYPE *opaqueBlob;
|
||||
} GOST_KX_MESSAGE;
|
||||
|
||||
DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
|
||||
|
||||
ASN1_SEQUENCE(GOST_KX_MESSAGE) = {
|
||||
ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY),
|
||||
ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
|
||||
} ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
|
||||
|
||||
IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
|
||||
|
||||
static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
|
||||
|
||||
/*
|
||||
* ossl_statem_server13_read_transition() encapsulates the logic for the allowed
|
||||
* handshake state transitions when a TLSv1.3 server is reading messages from
|
||||
* the client. The message type that the client has sent is provided in |mt|.
|
||||
* The current state is in |s->statem.hand_state|.
|
||||
*
|
||||
* Return values are 1 for success (transition allowed) and 0 on error
|
||||
* (transition not allowed)
|
||||
*/
|
||||
static int ossl_statem_server13_read_transition(SSL *s, int mt)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
/*
|
||||
* Note: There is no case for TLS_ST_BEFORE because at that stage we have
|
||||
* not negotiated TLSv1.3 yet, so that case is handled by
|
||||
* ossl_statem_server_read_transition()
|
||||
*/
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
break;
|
||||
|
||||
case TLS_ST_EARLY_DATA:
|
||||
if (s->hello_retry_request == SSL_HRR_PENDING) {
|
||||
if (mt == SSL3_MT_CLIENT_HELLO) {
|
||||
st->hand_state = TLS_ST_SR_CLNT_HELLO;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
} else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
|
||||
if (mt == SSL3_MT_END_OF_EARLY_DATA) {
|
||||
st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* Fall through */
|
||||
|
||||
case TLS_ST_SR_END_OF_EARLY_DATA:
|
||||
case TLS_ST_SW_FINISHED:
|
||||
if (s->s3.tmp.cert_request) {
|
||||
if (mt == SSL3_MT_CERTIFICATE) {
|
||||
st->hand_state = TLS_ST_SR_CERT;
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (mt == SSL3_MT_FINISHED) {
|
||||
st->hand_state = TLS_ST_SR_FINISHED;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SR_CERT:
|
||||
if (s->session->peer == NULL) {
|
||||
if (mt == SSL3_MT_FINISHED) {
|
||||
st->hand_state = TLS_ST_SR_FINISHED;
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
|
||||
st->hand_state = TLS_ST_SR_CERT_VRFY;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SR_CERT_VRFY:
|
||||
if (mt == SSL3_MT_FINISHED) {
|
||||
st->hand_state = TLS_ST_SR_FINISHED;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_OK:
|
||||
/*
|
||||
* Its never ok to start processing handshake messages in the middle of
|
||||
* early data (i.e. before we've received the end of early data alert)
|
||||
*/
|
||||
if (s->early_data_state == SSL_EARLY_DATA_READING)
|
||||
break;
|
||||
|
||||
if (mt == SSL3_MT_CERTIFICATE
|
||||
&& s->post_handshake_auth == SSL_PHA_REQUESTED) {
|
||||
st->hand_state = TLS_ST_SR_CERT;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mt == SSL3_MT_KEY_UPDATE) {
|
||||
st->hand_state = TLS_ST_SR_KEY_UPDATE;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* No valid transition found */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* ossl_statem_server_read_transition() encapsulates the logic for the allowed
|
||||
* handshake state transitions when the server is reading messages from the
|
||||
* client. The message type that the client has sent is provided in |mt|. The
|
||||
* current state is in |s->statem.hand_state|.
|
||||
*
|
||||
* Return values are 1 for success (transition allowed) and 0 on error
|
||||
* (transition not allowed)
|
||||
*/
|
||||
int ossl_statem_server_read_transition(SSL *s, int mt)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
if (!ossl_statem_server13_read_transition(s, mt))
|
||||
goto err;
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
break;
|
||||
|
||||
case TLS_ST_BEFORE:
|
||||
case TLS_ST_OK:
|
||||
case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
|
||||
if (mt == SSL3_MT_CLIENT_HELLO) {
|
||||
st->hand_state = TLS_ST_SR_CLNT_HELLO;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SRVR_DONE:
|
||||
/*
|
||||
* If we get a CKE message after a ServerDone then either
|
||||
* 1) We didn't request a Certificate
|
||||
* OR
|
||||
* 2) If we did request one then
|
||||
* a) We allow no Certificate to be returned
|
||||
* AND
|
||||
* b) We are running SSL3 (in TLS1.0+ the client must return a 0
|
||||
* list if we requested a certificate)
|
||||
*/
|
||||
if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
|
||||
if (s->s3.tmp.cert_request) {
|
||||
if (s->version == SSL3_VERSION) {
|
||||
if ((s->verify_mode & SSL_VERIFY_PEER)
|
||||
&& (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
|
||||
/*
|
||||
* This isn't an unexpected message as such - we're just
|
||||
* not going to accept it because we require a client
|
||||
* cert.
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
|
||||
SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
|
||||
return 0;
|
||||
}
|
||||
st->hand_state = TLS_ST_SR_KEY_EXCH;
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
st->hand_state = TLS_ST_SR_KEY_EXCH;
|
||||
return 1;
|
||||
}
|
||||
} else if (s->s3.tmp.cert_request) {
|
||||
if (mt == SSL3_MT_CERTIFICATE) {
|
||||
st->hand_state = TLS_ST_SR_CERT;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SR_CERT:
|
||||
if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
|
||||
st->hand_state = TLS_ST_SR_KEY_EXCH;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SR_KEY_EXCH:
|
||||
/*
|
||||
* We should only process a CertificateVerify message if we have
|
||||
* received a Certificate from the client. If so then |s->session->peer|
|
||||
* will be non NULL. In some instances a CertificateVerify message is
|
||||
* not required even if the peer has sent a Certificate (e.g. such as in
|
||||
* the case of static DH). In that case |st->no_cert_verify| should be
|
||||
* set.
|
||||
*/
|
||||
if (s->session->peer == NULL || st->no_cert_verify) {
|
||||
if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
|
||||
/*
|
||||
* For the ECDH ciphersuites when the client sends its ECDH
|
||||
* pub key in a certificate, the CertificateVerify message is
|
||||
* not sent. Also for GOST ciphersuites when the client uses
|
||||
* its key from the certificate for key exchange.
|
||||
*/
|
||||
st->hand_state = TLS_ST_SR_CHANGE;
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
|
||||
st->hand_state = TLS_ST_SR_CERT_VRFY;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SR_CERT_VRFY:
|
||||
if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
|
||||
st->hand_state = TLS_ST_SR_CHANGE;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SR_CHANGE:
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
if (s->s3.npn_seen) {
|
||||
if (mt == SSL3_MT_NEXT_PROTO) {
|
||||
st->hand_state = TLS_ST_SR_NEXT_PROTO;
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
#endif
|
||||
if (mt == SSL3_MT_FINISHED) {
|
||||
st->hand_state = TLS_ST_SR_FINISHED;
|
||||
return 1;
|
||||
}
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
case TLS_ST_SR_NEXT_PROTO:
|
||||
if (mt == SSL3_MT_FINISHED) {
|
||||
st->hand_state = TLS_ST_SR_FINISHED;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
case TLS_ST_SW_FINISHED:
|
||||
if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
|
||||
st->hand_state = TLS_ST_SR_CHANGE;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
err:
|
||||
/* No valid transition found */
|
||||
if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
|
||||
BIO *rbio;
|
||||
|
||||
/*
|
||||
* CCS messages don't have a message sequence number so this is probably
|
||||
* because of an out-of-order CCS. We'll just drop it.
|
||||
*/
|
||||
s->init_num = 0;
|
||||
s->rwstate = SSL_READING;
|
||||
rbio = SSL_get_rbio(s);
|
||||
BIO_clear_retry_flags(rbio);
|
||||
BIO_set_retry_read(rbio);
|
||||
return 0;
|
||||
}
|
||||
SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
|
||||
SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
|
||||
SSL_R_UNEXPECTED_MESSAGE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Should we send a ServerKeyExchange message?
|
||||
*
|
||||
* Valid return values are:
|
||||
* 1: Yes
|
||||
* 0: No
|
||||
*/
|
||||
static int send_server_key_exchange(SSL *s)
|
||||
{
|
||||
unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
|
||||
|
||||
/*
|
||||
* only send a ServerKeyExchange if DH or fortezza but we have a
|
||||
* sign only certificate PSK: may send PSK identity hints For
|
||||
* ECC ciphersuites, we send a serverKeyExchange message only if
|
||||
* the cipher suite is either ECDH-anon or ECDHE. In other cases,
|
||||
* the server certificate contains the server's public key for
|
||||
* key exchange.
|
||||
*/
|
||||
if (alg_k & (SSL_kDHE | SSL_kECDHE)
|
||||
/*
|
||||
* PSK: send ServerKeyExchange if PSK identity hint if
|
||||
* provided
|
||||
*/
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
/* Only send SKE if we have identity hint for plain PSK */
|
||||
|| ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
|
||||
&& s->cert->psk_identity_hint)
|
||||
/* For other PSK always send SKE */
|
||||
|| (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
/* SRP: send ServerKeyExchange */
|
||||
|| (alg_k & SSL_kSRP)
|
||||
#endif
|
||||
) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Should we send a CertificateRequest message?
|
||||
*
|
||||
* Valid return values are:
|
||||
* 1: Yes
|
||||
* 0: No
|
||||
*/
|
||||
int send_certificate_request(SSL *s)
|
||||
{
|
||||
if (
|
||||
/* don't request cert unless asked for it: */
|
||||
s->verify_mode & SSL_VERIFY_PEER
|
||||
/*
|
||||
* don't request if post-handshake-only unless doing
|
||||
* post-handshake in TLSv1.3:
|
||||
*/
|
||||
&& (!SSL_IS_TLS13(s) || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
|
||||
|| s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
|
||||
/*
|
||||
* if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
|
||||
* a second time:
|
||||
*/
|
||||
&& (s->certreqs_sent < 1 ||
|
||||
!(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
|
||||
/*
|
||||
* never request cert in anonymous ciphersuites (see
|
||||
* section "Certificate request" in SSL 3 drafts and in
|
||||
* RFC 2246):
|
||||
*/
|
||||
&& (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)
|
||||
/*
|
||||
* ... except when the application insists on
|
||||
* verification (against the specs, but statem_clnt.c accepts
|
||||
* this for SSL 3)
|
||||
*/
|
||||
|| (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
|
||||
/* don't request certificate for SRP auth */
|
||||
&& !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)
|
||||
/*
|
||||
* With normal PSK Certificates and Certificate Requests
|
||||
* are omitted
|
||||
*/
|
||||
&& !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* ossl_statem_server13_write_transition() works out what handshake state to
|
||||
* move to next when a TLSv1.3 server is writing messages to be sent to the
|
||||
* client.
|
||||
*/
|
||||
static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
/*
|
||||
* No case for TLS_ST_BEFORE, because at that stage we have not negotiated
|
||||
* TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
|
||||
*/
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* Shouldn't happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return WRITE_TRAN_ERROR;
|
||||
|
||||
case TLS_ST_OK:
|
||||
if (s->key_update != SSL_KEY_UPDATE_NONE) {
|
||||
st->hand_state = TLS_ST_SW_KEY_UPDATE;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
|
||||
st->hand_state = TLS_ST_SW_CERT_REQ;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
/* Try to read from the client instead */
|
||||
return WRITE_TRAN_FINISHED;
|
||||
|
||||
case TLS_ST_SR_CLNT_HELLO:
|
||||
st->hand_state = TLS_ST_SW_SRVR_HELLO;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_SRVR_HELLO:
|
||||
if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
|
||||
&& s->hello_retry_request != SSL_HRR_COMPLETE)
|
||||
st->hand_state = TLS_ST_SW_CHANGE;
|
||||
else if (s->hello_retry_request == SSL_HRR_PENDING)
|
||||
st->hand_state = TLS_ST_EARLY_DATA;
|
||||
else
|
||||
st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_CHANGE:
|
||||
if (s->hello_retry_request == SSL_HRR_PENDING)
|
||||
st->hand_state = TLS_ST_EARLY_DATA;
|
||||
else
|
||||
st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
|
||||
if (s->hit)
|
||||
st->hand_state = TLS_ST_SW_FINISHED;
|
||||
else if (send_certificate_request(s))
|
||||
st->hand_state = TLS_ST_SW_CERT_REQ;
|
||||
else
|
||||
st->hand_state = TLS_ST_SW_CERT;
|
||||
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_CERT_REQ:
|
||||
if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
|
||||
s->post_handshake_auth = SSL_PHA_REQUESTED;
|
||||
st->hand_state = TLS_ST_OK;
|
||||
} else {
|
||||
st->hand_state = TLS_ST_SW_CERT;
|
||||
}
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_CERT:
|
||||
st->hand_state = TLS_ST_SW_CERT_VRFY;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_CERT_VRFY:
|
||||
st->hand_state = TLS_ST_SW_FINISHED;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_FINISHED:
|
||||
st->hand_state = TLS_ST_EARLY_DATA;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_EARLY_DATA:
|
||||
return WRITE_TRAN_FINISHED;
|
||||
|
||||
case TLS_ST_SR_FINISHED:
|
||||
/*
|
||||
* Technically we have finished the handshake at this point, but we're
|
||||
* going to remain "in_init" for now and write out any session tickets
|
||||
* immediately.
|
||||
*/
|
||||
if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
|
||||
s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
|
||||
} else if (!s->ext.ticket_expected) {
|
||||
/*
|
||||
* If we're not going to renew the ticket then we just finish the
|
||||
* handshake at this point.
|
||||
*/
|
||||
st->hand_state = TLS_ST_OK;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
if (s->num_tickets > s->sent_tickets)
|
||||
st->hand_state = TLS_ST_SW_SESSION_TICKET;
|
||||
else
|
||||
st->hand_state = TLS_ST_OK;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SR_KEY_UPDATE:
|
||||
case TLS_ST_SW_KEY_UPDATE:
|
||||
st->hand_state = TLS_ST_OK;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_SESSION_TICKET:
|
||||
/* In a resumption we only ever send a maximum of one new ticket.
|
||||
* Following an initial handshake we send the number of tickets we have
|
||||
* been configured for.
|
||||
*/
|
||||
if (s->hit || s->num_tickets <= s->sent_tickets) {
|
||||
/* We've written enough tickets out. */
|
||||
st->hand_state = TLS_ST_OK;
|
||||
}
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ossl_statem_server_write_transition() works out what handshake state to move
|
||||
* to next when the server is writing messages to be sent to the client.
|
||||
*/
|
||||
WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
/*
|
||||
* Note that before the ClientHello we don't know what version we are going
|
||||
* to negotiate yet, so we don't take this branch until later
|
||||
*/
|
||||
|
||||
if (SSL_IS_TLS13(s))
|
||||
return ossl_statem_server13_write_transition(s);
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* Shouldn't happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return WRITE_TRAN_ERROR;
|
||||
|
||||
case TLS_ST_OK:
|
||||
if (st->request_state == TLS_ST_SW_HELLO_REQ) {
|
||||
/* We must be trying to renegotiate */
|
||||
st->hand_state = TLS_ST_SW_HELLO_REQ;
|
||||
st->request_state = TLS_ST_BEFORE;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
/* Must be an incoming ClientHello */
|
||||
if (!tls_setup_handshake(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return WRITE_TRAN_ERROR;
|
||||
}
|
||||
/* Fall through */
|
||||
|
||||
case TLS_ST_BEFORE:
|
||||
/* Just go straight to trying to read from the client */
|
||||
return WRITE_TRAN_FINISHED;
|
||||
|
||||
case TLS_ST_SW_HELLO_REQ:
|
||||
st->hand_state = TLS_ST_OK;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SR_CLNT_HELLO:
|
||||
if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
|
||||
&& (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
|
||||
st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
|
||||
} else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
|
||||
/* We must have rejected the renegotiation */
|
||||
st->hand_state = TLS_ST_OK;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
} else {
|
||||
st->hand_state = TLS_ST_SW_SRVR_HELLO;
|
||||
}
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
|
||||
return WRITE_TRAN_FINISHED;
|
||||
|
||||
case TLS_ST_SW_SRVR_HELLO:
|
||||
if (s->hit) {
|
||||
if (s->ext.ticket_expected)
|
||||
st->hand_state = TLS_ST_SW_SESSION_TICKET;
|
||||
else
|
||||
st->hand_state = TLS_ST_SW_CHANGE;
|
||||
} else {
|
||||
/* Check if it is anon DH or anon ECDH, */
|
||||
/* normal PSK or SRP */
|
||||
if (!(s->s3.tmp.new_cipher->algorithm_auth &
|
||||
(SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
|
||||
st->hand_state = TLS_ST_SW_CERT;
|
||||
} else if (send_server_key_exchange(s)) {
|
||||
st->hand_state = TLS_ST_SW_KEY_EXCH;
|
||||
} else if (send_certificate_request(s)) {
|
||||
st->hand_state = TLS_ST_SW_CERT_REQ;
|
||||
} else {
|
||||
st->hand_state = TLS_ST_SW_SRVR_DONE;
|
||||
}
|
||||
}
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_CERT:
|
||||
if (s->ext.status_expected) {
|
||||
st->hand_state = TLS_ST_SW_CERT_STATUS;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
/* Fall through */
|
||||
|
||||
case TLS_ST_SW_CERT_STATUS:
|
||||
if (send_server_key_exchange(s)) {
|
||||
st->hand_state = TLS_ST_SW_KEY_EXCH;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
/* Fall through */
|
||||
|
||||
case TLS_ST_SW_KEY_EXCH:
|
||||
if (send_certificate_request(s)) {
|
||||
st->hand_state = TLS_ST_SW_CERT_REQ;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
/* Fall through */
|
||||
|
||||
case TLS_ST_SW_CERT_REQ:
|
||||
st->hand_state = TLS_ST_SW_SRVR_DONE;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_SRVR_DONE:
|
||||
return WRITE_TRAN_FINISHED;
|
||||
|
||||
case TLS_ST_SR_FINISHED:
|
||||
if (s->hit) {
|
||||
st->hand_state = TLS_ST_OK;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
} else if (s->ext.ticket_expected) {
|
||||
st->hand_state = TLS_ST_SW_SESSION_TICKET;
|
||||
} else {
|
||||
st->hand_state = TLS_ST_SW_CHANGE;
|
||||
}
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_SESSION_TICKET:
|
||||
st->hand_state = TLS_ST_SW_CHANGE;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_CHANGE:
|
||||
st->hand_state = TLS_ST_SW_FINISHED;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_FINISHED:
|
||||
if (s->hit) {
|
||||
return WRITE_TRAN_FINISHED;
|
||||
}
|
||||
st->hand_state = TLS_ST_OK;
|
||||
return WRITE_TRAN_CONTINUE;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform any pre work that needs to be done prior to sending a message from
|
||||
* the server to the client.
|
||||
*/
|
||||
WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* No pre work to be done */
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_HELLO_REQ:
|
||||
s->shutdown = 0;
|
||||
if (SSL_IS_DTLS(s))
|
||||
dtls1_clear_sent_buffer(s);
|
||||
break;
|
||||
|
||||
case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
|
||||
s->shutdown = 0;
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
dtls1_clear_sent_buffer(s);
|
||||
/* We don't buffer this message so don't use the timer */
|
||||
st->use_timer = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SRVR_HELLO:
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
/*
|
||||
* Messages we write from now on should be buffered and
|
||||
* retransmitted if necessary, so we need to use the timer now
|
||||
*/
|
||||
st->use_timer = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SRVR_DONE:
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
|
||||
/* Calls SSLfatal() as required */
|
||||
return dtls_wait_for_dry(s);
|
||||
}
|
||||
#endif
|
||||
return WORK_FINISHED_CONTINUE;
|
||||
|
||||
case TLS_ST_SW_SESSION_TICKET:
|
||||
if (SSL_IS_TLS13(s) && s->sent_tickets == 0) {
|
||||
/*
|
||||
* Actually this is the end of the handshake, but we're going
|
||||
* straight into writing the session ticket out. So we finish off
|
||||
* the handshake, but keep the various buffers active.
|
||||
*
|
||||
* Calls SSLfatal as required.
|
||||
*/
|
||||
return tls_finish_handshake(s, wst, 0, 0);
|
||||
} if (SSL_IS_DTLS(s)) {
|
||||
/*
|
||||
* We're into the last flight. We don't retransmit the last flight
|
||||
* unless we need to, so we don't use the timer
|
||||
*/
|
||||
st->use_timer = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_CHANGE:
|
||||
if (SSL_IS_TLS13(s))
|
||||
break;
|
||||
s->session->cipher = s->s3.tmp.new_cipher;
|
||||
if (!s->method->ssl3_enc->setup_key_block(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
/*
|
||||
* We're into the last flight. We don't retransmit the last flight
|
||||
* unless we need to, so we don't use the timer. This might have
|
||||
* already been set to 0 if we sent a NewSessionTicket message,
|
||||
* but we'll set it again here in case we didn't.
|
||||
*/
|
||||
st->use_timer = 0;
|
||||
}
|
||||
return WORK_FINISHED_CONTINUE;
|
||||
|
||||
case TLS_ST_EARLY_DATA:
|
||||
if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
|
||||
&& (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
|
||||
return WORK_FINISHED_CONTINUE;
|
||||
/* Fall through */
|
||||
|
||||
case TLS_ST_OK:
|
||||
/* Calls SSLfatal() as required */
|
||||
return tls_finish_handshake(s, wst, 1, 1);
|
||||
}
|
||||
|
||||
return WORK_FINISHED_CONTINUE;
|
||||
}
|
||||
|
||||
static ossl_inline int conn_is_closed(void)
|
||||
{
|
||||
switch (get_last_sys_error()) {
|
||||
#if defined(EPIPE)
|
||||
case EPIPE:
|
||||
return 1;
|
||||
#endif
|
||||
#if defined(ECONNRESET)
|
||||
case ECONNRESET:
|
||||
return 1;
|
||||
#endif
|
||||
#if defined(WSAECONNRESET)
|
||||
case WSAECONNRESET:
|
||||
return 1;
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform any work that needs to be done after sending a message from the
|
||||
* server to the client.
|
||||
*/
|
||||
WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
s->init_num = 0;
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* No post work to be done */
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_HELLO_REQ:
|
||||
if (statem_flush(s) != 1)
|
||||
return WORK_MORE_A;
|
||||
if (!ssl3_init_finished_mac(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
|
||||
if (statem_flush(s) != 1)
|
||||
return WORK_MORE_A;
|
||||
/* HelloVerifyRequest resets Finished MAC */
|
||||
if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
/*
|
||||
* The next message should be another ClientHello which we need to
|
||||
* treat like it was the first packet
|
||||
*/
|
||||
s->first_packet = 1;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SRVR_HELLO:
|
||||
if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
|
||||
if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
|
||||
&& statem_flush(s) != 1)
|
||||
return WORK_MORE_A;
|
||||
break;
|
||||
}
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
if (SSL_IS_DTLS(s) && s->hit) {
|
||||
unsigned char sctpauthkey[64];
|
||||
char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
|
||||
size_t labellen;
|
||||
|
||||
/*
|
||||
* Add new shared key for SCTP-Auth, will be ignored if no
|
||||
* SCTP used.
|
||||
*/
|
||||
memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
|
||||
sizeof(DTLS1_SCTP_AUTH_LABEL));
|
||||
|
||||
/* Don't include the terminating zero. */
|
||||
labellen = sizeof(labelbuffer) - 1;
|
||||
if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
|
||||
labellen += 1;
|
||||
|
||||
if (SSL_export_keying_material(s, sctpauthkey,
|
||||
sizeof(sctpauthkey), labelbuffer,
|
||||
labellen, NULL, 0,
|
||||
0) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_OSSL_STATEM_SERVER_POST_WORK,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return WORK_ERROR;
|
||||
}
|
||||
|
||||
BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
|
||||
sizeof(sctpauthkey), sctpauthkey);
|
||||
}
|
||||
#endif
|
||||
if (!SSL_IS_TLS13(s)
|
||||
|| ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
|
||||
&& s->hello_retry_request != SSL_HRR_COMPLETE))
|
||||
break;
|
||||
/* Fall through */
|
||||
|
||||
case TLS_ST_SW_CHANGE:
|
||||
if (s->hello_retry_request == SSL_HRR_PENDING) {
|
||||
if (!statem_flush(s))
|
||||
return WORK_MORE_A;
|
||||
break;
|
||||
}
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
if (!s->method->ssl3_enc->setup_key_block(s)
|
||||
|| !s->method->ssl3_enc->change_cipher_state(s,
|
||||
SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
|
||||
if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
|
||||
&& !s->method->ssl3_enc->change_cipher_state(s,
|
||||
SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
/*
|
||||
* We don't yet know whether the next record we are going to receive
|
||||
* is an unencrypted alert, an encrypted alert, or an encrypted
|
||||
* handshake message. We temporarily tolerate unencrypted alerts.
|
||||
*/
|
||||
s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
|
||||
break;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
if (SSL_IS_DTLS(s) && !s->hit) {
|
||||
/*
|
||||
* Change to new shared key of SCTP-Auth, will be ignored if
|
||||
* no SCTP used.
|
||||
*/
|
||||
BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
|
||||
0, NULL);
|
||||
}
|
||||
#endif
|
||||
if (!s->method->ssl3_enc->change_cipher_state(s,
|
||||
SSL3_CHANGE_CIPHER_SERVER_WRITE))
|
||||
{
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
|
||||
if (SSL_IS_DTLS(s))
|
||||
dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SRVR_DONE:
|
||||
if (statem_flush(s) != 1)
|
||||
return WORK_MORE_A;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_FINISHED:
|
||||
if (statem_flush(s) != 1)
|
||||
return WORK_MORE_A;
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
if (SSL_IS_DTLS(s) && s->hit) {
|
||||
/*
|
||||
* Change to new shared key of SCTP-Auth, will be ignored if
|
||||
* no SCTP used.
|
||||
*/
|
||||
BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
|
||||
0, NULL);
|
||||
}
|
||||
#endif
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
if (!s->method->ssl3_enc->generate_master_secret(s,
|
||||
s->master_secret, s->handshake_secret, 0,
|
||||
&s->session->master_key_length)
|
||||
|| !s->method->ssl3_enc->change_cipher_state(s,
|
||||
SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_CERT_REQ:
|
||||
if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
|
||||
if (statem_flush(s) != 1)
|
||||
return WORK_MORE_A;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_KEY_UPDATE:
|
||||
if (statem_flush(s) != 1)
|
||||
return WORK_MORE_A;
|
||||
if (!tls13_update_key(s, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SESSION_TICKET:
|
||||
clear_sys_error();
|
||||
if (SSL_IS_TLS13(s) && statem_flush(s) != 1) {
|
||||
if (SSL_get_error(s, 0) == SSL_ERROR_SYSCALL
|
||||
&& conn_is_closed()) {
|
||||
/*
|
||||
* We ignore connection closed errors in TLSv1.3 when sending a
|
||||
* NewSessionTicket and behave as if we were successful. This is
|
||||
* so that we are still able to read data sent to us by a client
|
||||
* that closes soon after the end of the handshake without
|
||||
* waiting to read our post-handshake NewSessionTickets.
|
||||
*/
|
||||
s->rwstate = SSL_NOTHING;
|
||||
break;
|
||||
}
|
||||
|
||||
return WORK_MORE_A;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return WORK_FINISHED_CONTINUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the message construction function and message type for sending from the
|
||||
* server
|
||||
*
|
||||
* Valid return values are:
|
||||
* 1: Success
|
||||
* 0: Error
|
||||
*/
|
||||
int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
|
||||
confunc_f *confunc, int *mt)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* Shouldn't happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE,
|
||||
SSL_R_BAD_HANDSHAKE_STATE);
|
||||
return 0;
|
||||
|
||||
case TLS_ST_SW_CHANGE:
|
||||
if (SSL_IS_DTLS(s))
|
||||
*confunc = dtls_construct_change_cipher_spec;
|
||||
else
|
||||
*confunc = tls_construct_change_cipher_spec;
|
||||
*mt = SSL3_MT_CHANGE_CIPHER_SPEC;
|
||||
break;
|
||||
|
||||
case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
|
||||
*confunc = dtls_construct_hello_verify_request;
|
||||
*mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_HELLO_REQ:
|
||||
/* No construction function needed */
|
||||
*confunc = NULL;
|
||||
*mt = SSL3_MT_HELLO_REQUEST;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SRVR_HELLO:
|
||||
*confunc = tls_construct_server_hello;
|
||||
*mt = SSL3_MT_SERVER_HELLO;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_CERT:
|
||||
*confunc = tls_construct_server_certificate;
|
||||
*mt = SSL3_MT_CERTIFICATE;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_CERT_VRFY:
|
||||
*confunc = tls_construct_cert_verify;
|
||||
*mt = SSL3_MT_CERTIFICATE_VERIFY;
|
||||
break;
|
||||
|
||||
|
||||
case TLS_ST_SW_KEY_EXCH:
|
||||
*confunc = tls_construct_server_key_exchange;
|
||||
*mt = SSL3_MT_SERVER_KEY_EXCHANGE;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_CERT_REQ:
|
||||
*confunc = tls_construct_certificate_request;
|
||||
*mt = SSL3_MT_CERTIFICATE_REQUEST;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SRVR_DONE:
|
||||
*confunc = tls_construct_server_done;
|
||||
*mt = SSL3_MT_SERVER_DONE;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_SESSION_TICKET:
|
||||
*confunc = tls_construct_new_session_ticket;
|
||||
*mt = SSL3_MT_NEWSESSION_TICKET;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_CERT_STATUS:
|
||||
*confunc = tls_construct_cert_status;
|
||||
*mt = SSL3_MT_CERTIFICATE_STATUS;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_FINISHED:
|
||||
*confunc = tls_construct_finished;
|
||||
*mt = SSL3_MT_FINISHED;
|
||||
break;
|
||||
|
||||
case TLS_ST_EARLY_DATA:
|
||||
*confunc = NULL;
|
||||
*mt = SSL3_MT_DUMMY;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
|
||||
*confunc = tls_construct_encrypted_extensions;
|
||||
*mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
|
||||
break;
|
||||
|
||||
case TLS_ST_SW_KEY_UPDATE:
|
||||
*confunc = tls_construct_key_update;
|
||||
*mt = SSL3_MT_KEY_UPDATE;
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Maximum size (excluding the Handshake header) of a ClientHello message,
|
||||
* calculated as follows:
|
||||
*
|
||||
* 2 + # client_version
|
||||
* 32 + # only valid length for random
|
||||
* 1 + # length of session_id
|
||||
* 32 + # maximum size for session_id
|
||||
* 2 + # length of cipher suites
|
||||
* 2^16-2 + # maximum length of cipher suites array
|
||||
* 1 + # length of compression_methods
|
||||
* 2^8-1 + # maximum length of compression methods
|
||||
* 2 + # length of extensions
|
||||
* 2^16-1 # maximum length of extensions
|
||||
*/
|
||||
#define CLIENT_HELLO_MAX_LENGTH 131396
|
||||
|
||||
#define CLIENT_KEY_EXCH_MAX_LENGTH 2048
|
||||
#define NEXT_PROTO_MAX_LENGTH 514
|
||||
|
||||
/*
|
||||
* Returns the maximum allowed length for the current message that we are
|
||||
* reading. Excludes the message header.
|
||||
*/
|
||||
size_t ossl_statem_server_max_message_size(SSL *s)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* Shouldn't happen */
|
||||
return 0;
|
||||
|
||||
case TLS_ST_SR_CLNT_HELLO:
|
||||
return CLIENT_HELLO_MAX_LENGTH;
|
||||
|
||||
case TLS_ST_SR_END_OF_EARLY_DATA:
|
||||
return END_OF_EARLY_DATA_MAX_LENGTH;
|
||||
|
||||
case TLS_ST_SR_CERT:
|
||||
return s->max_cert_list;
|
||||
|
||||
case TLS_ST_SR_KEY_EXCH:
|
||||
return CLIENT_KEY_EXCH_MAX_LENGTH;
|
||||
|
||||
case TLS_ST_SR_CERT_VRFY:
|
||||
return SSL3_RT_MAX_PLAIN_LENGTH;
|
||||
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
case TLS_ST_SR_NEXT_PROTO:
|
||||
return NEXT_PROTO_MAX_LENGTH;
|
||||
#endif
|
||||
|
||||
case TLS_ST_SR_CHANGE:
|
||||
return CCS_MAX_LENGTH;
|
||||
|
||||
case TLS_ST_SR_FINISHED:
|
||||
return FINISHED_MAX_LENGTH;
|
||||
|
||||
case TLS_ST_SR_KEY_UPDATE:
|
||||
return KEY_UPDATE_MAX_LENGTH;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Process a message that the server has received from the client.
|
||||
*/
|
||||
MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* Shouldn't happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return MSG_PROCESS_ERROR;
|
||||
|
||||
case TLS_ST_SR_CLNT_HELLO:
|
||||
return tls_process_client_hello(s, pkt);
|
||||
|
||||
case TLS_ST_SR_END_OF_EARLY_DATA:
|
||||
return tls_process_end_of_early_data(s, pkt);
|
||||
|
||||
case TLS_ST_SR_CERT:
|
||||
return tls_process_client_certificate(s, pkt);
|
||||
|
||||
case TLS_ST_SR_KEY_EXCH:
|
||||
return tls_process_client_key_exchange(s, pkt);
|
||||
|
||||
case TLS_ST_SR_CERT_VRFY:
|
||||
return tls_process_cert_verify(s, pkt);
|
||||
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
case TLS_ST_SR_NEXT_PROTO:
|
||||
return tls_process_next_proto(s, pkt);
|
||||
#endif
|
||||
|
||||
case TLS_ST_SR_CHANGE:
|
||||
return tls_process_change_cipher_spec(s, pkt);
|
||||
|
||||
case TLS_ST_SR_FINISHED:
|
||||
return tls_process_finished(s, pkt);
|
||||
|
||||
case TLS_ST_SR_KEY_UPDATE:
|
||||
return tls_process_key_update(s, pkt);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform any further processing required following the receipt of a message
|
||||
* from the client
|
||||
*/
|
||||
WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
|
||||
{
|
||||
OSSL_STATEM *st = &s->statem;
|
||||
|
||||
switch (st->hand_state) {
|
||||
default:
|
||||
/* Shouldn't happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return WORK_ERROR;
|
||||
|
||||
case TLS_ST_SR_CLNT_HELLO:
|
||||
return tls_post_process_client_hello(s, wst);
|
||||
|
||||
case TLS_ST_SR_KEY_EXCH:
|
||||
return tls_post_process_client_key_exchange(s, wst);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
/* Returns 1 on success, 0 for retryable error, -1 for fatal error */
|
||||
static int ssl_check_srp_ext_ClientHello(SSL *s)
|
||||
{
|
||||
int ret;
|
||||
int al = SSL_AD_UNRECOGNIZED_NAME;
|
||||
|
||||
if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
|
||||
(s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
|
||||
if (s->srp_ctx.login == NULL) {
|
||||
/*
|
||||
* RFC 5054 says SHOULD reject, we do so if There is no srp
|
||||
* login name
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
|
||||
SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
|
||||
SSL_R_PSK_IDENTITY_NOT_FOUND);
|
||||
return -1;
|
||||
} else {
|
||||
ret = SSL_srp_server_param_with_username(s, &al);
|
||||
if (ret < 0)
|
||||
return 0;
|
||||
if (ret == SSL3_AL_FATAL) {
|
||||
SSLfatal(s, al, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
|
||||
al == SSL_AD_UNKNOWN_PSK_IDENTITY
|
||||
? SSL_R_PSK_IDENTITY_NOT_FOUND
|
||||
: SSL_R_CLIENTHELLO_TLSEXT);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
|
||||
size_t cookie_len)
|
||||
{
|
||||
/* Always use DTLS 1.0 version: see RFC 6347 */
|
||||
if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
|
||||
|| !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
unsigned int cookie_leni;
|
||||
if (s->ctx->app_gen_cookie_cb == NULL ||
|
||||
s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
|
||||
&cookie_leni) == 0 ||
|
||||
cookie_leni > 255) {
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
|
||||
SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
s->d1->cookie_len = cookie_leni;
|
||||
|
||||
if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
|
||||
s->d1->cookie_len)) {
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
/*-
|
||||
* ssl_check_for_safari attempts to fingerprint Safari using OS X
|
||||
* SecureTransport using the TLS extension block in |hello|.
|
||||
* Safari, since 10.6, sends exactly these extensions, in this order:
|
||||
* SNI,
|
||||
* elliptic_curves
|
||||
* ec_point_formats
|
||||
* signature_algorithms (for TLSv1.2 only)
|
||||
*
|
||||
* We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
|
||||
* but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
|
||||
* Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
|
||||
* 10.8..10.8.3 (which don't work).
|
||||
*/
|
||||
static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
|
||||
{
|
||||
static const unsigned char kSafariExtensionsBlock[] = {
|
||||
0x00, 0x0a, /* elliptic_curves extension */
|
||||
0x00, 0x08, /* 8 bytes */
|
||||
0x00, 0x06, /* 6 bytes of curve ids */
|
||||
0x00, 0x17, /* P-256 */
|
||||
0x00, 0x18, /* P-384 */
|
||||
0x00, 0x19, /* P-521 */
|
||||
|
||||
0x00, 0x0b, /* ec_point_formats */
|
||||
0x00, 0x02, /* 2 bytes */
|
||||
0x01, /* 1 point format */
|
||||
0x00, /* uncompressed */
|
||||
/* The following is only present in TLS 1.2 */
|
||||
0x00, 0x0d, /* signature_algorithms */
|
||||
0x00, 0x0c, /* 12 bytes */
|
||||
0x00, 0x0a, /* 10 bytes */
|
||||
0x05, 0x01, /* SHA-384/RSA */
|
||||
0x04, 0x01, /* SHA-256/RSA */
|
||||
0x02, 0x01, /* SHA-1/RSA */
|
||||
0x04, 0x03, /* SHA-256/ECDSA */
|
||||
0x02, 0x03, /* SHA-1/ECDSA */
|
||||
};
|
||||
/* Length of the common prefix (first two extensions). */
|
||||
static const size_t kSafariCommonExtensionsLength = 18;
|
||||
unsigned int type;
|
||||
PACKET sni, tmppkt;
|
||||
size_t ext_len;
|
||||
|
||||
tmppkt = hello->extensions;
|
||||
|
||||
if (!PACKET_forward(&tmppkt, 2)
|
||||
|| !PACKET_get_net_2(&tmppkt, &type)
|
||||
|| !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type != TLSEXT_TYPE_server_name)
|
||||
return;
|
||||
|
||||
ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
|
||||
sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
|
||||
|
||||
s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
|
||||
ext_len);
|
||||
}
|
||||
#endif /* !OPENSSL_NO_EC */
|
||||
|
||||
MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
|
||||
{
|
||||
/* |cookie| will only be initialized for DTLS. */
|
||||
PACKET session_id, compression, extensions, cookie;
|
||||
static const unsigned char null_compression = 0;
|
||||
CLIENTHELLO_MSG *clienthello = NULL;
|
||||
|
||||
/* Check if this is actually an unexpected renegotiation ClientHello */
|
||||
if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
|
||||
if (!ossl_assert(!SSL_IS_TLS13(s))) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0
|
||||
|| (!s->s3.send_connection_binding
|
||||
&& (s->options
|
||||
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
|
||||
ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
|
||||
return MSG_PROCESS_FINISHED_READING;
|
||||
}
|
||||
s->renegotiate = 1;
|
||||
s->new_session = 1;
|
||||
}
|
||||
|
||||
clienthello = OPENSSL_zalloc(sizeof(*clienthello));
|
||||
if (clienthello == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
|
||||
*/
|
||||
clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
|
||||
PACKET_null_init(&cookie);
|
||||
|
||||
if (clienthello->isv2) {
|
||||
unsigned int mt;
|
||||
|
||||
if (!SSL_IS_FIRST_HANDSHAKE(s)
|
||||
|| s->hello_retry_request != SSL_HRR_NONE) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
|
||||
SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*-
|
||||
* An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
|
||||
* header is sent directly on the wire, not wrapped as a TLS
|
||||
* record. Our record layer just processes the message length and passes
|
||||
* the rest right through. Its format is:
|
||||
* Byte Content
|
||||
* 0-1 msg_length - decoded by the record layer
|
||||
* 2 msg_type - s->init_msg points here
|
||||
* 3-4 version
|
||||
* 5-6 cipher_spec_length
|
||||
* 7-8 session_id_length
|
||||
* 9-10 challenge_length
|
||||
* ... ...
|
||||
*/
|
||||
|
||||
if (!PACKET_get_1(pkt, &mt)
|
||||
|| mt != SSL2_MT_CLIENT_HELLO) {
|
||||
/*
|
||||
* Should never happen. We should have tested this in the record
|
||||
* layer in order to have determined that this is a SSLv2 record
|
||||
* in the first place
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_LENGTH_TOO_SHORT);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Parse the message and load client random. */
|
||||
if (clienthello->isv2) {
|
||||
/*
|
||||
* Handle an SSLv2 backwards compatible ClientHello
|
||||
* Note, this is only for SSLv3+ using the backward compatible format.
|
||||
* Real SSLv2 is not supported, and is rejected below.
|
||||
*/
|
||||
unsigned int ciphersuite_len, session_id_len, challenge_len;
|
||||
PACKET challenge;
|
||||
|
||||
if (!PACKET_get_net_2(pkt, &ciphersuite_len)
|
||||
|| !PACKET_get_net_2(pkt, &session_id_len)
|
||||
|| !PACKET_get_net_2(pkt, &challenge_len)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_RECORD_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
|
||||
SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
|
||||
ciphersuite_len)
|
||||
|| !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
|
||||
|| !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
|
||||
/* No extensions. */
|
||||
|| PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_RECORD_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
clienthello->session_id_len = session_id_len;
|
||||
|
||||
/* Load the client random and compression list. We use SSL3_RANDOM_SIZE
|
||||
* here rather than sizeof(clienthello->random) because that is the limit
|
||||
* for SSLv3 and it is fixed. It won't change even if
|
||||
* sizeof(clienthello->random) does.
|
||||
*/
|
||||
challenge_len = challenge_len > SSL3_RANDOM_SIZE
|
||||
? SSL3_RANDOM_SIZE : challenge_len;
|
||||
memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
|
||||
if (!PACKET_copy_bytes(&challenge,
|
||||
clienthello->random + SSL3_RANDOM_SIZE -
|
||||
challenge_len, challenge_len)
|
||||
/* Advertise only null compression. */
|
||||
|| !PACKET_buf_init(&compression, &null_compression, 1)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
PACKET_null_init(&clienthello->extensions);
|
||||
} else {
|
||||
/* Regular ClientHello. */
|
||||
if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
|
||||
|| !PACKET_get_length_prefixed_1(pkt, &session_id)
|
||||
|| !PACKET_copy_all(&session_id, clienthello->session_id,
|
||||
SSL_MAX_SSL_SESSION_ID_LENGTH,
|
||||
&clienthello->session_id_len)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
|
||||
DTLS1_COOKIE_LENGTH,
|
||||
&clienthello->dtls_cookie_len)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
/*
|
||||
* If we require cookies and this ClientHello doesn't contain one,
|
||||
* just return since we do not want to allocate any memory yet.
|
||||
* So check cookie length...
|
||||
*/
|
||||
if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
|
||||
if (clienthello->dtls_cookie_len == 0) {
|
||||
OPENSSL_free(clienthello);
|
||||
return MSG_PROCESS_FINISHED_READING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Could be empty. */
|
||||
if (PACKET_remaining(pkt) == 0) {
|
||||
PACKET_null_init(&clienthello->extensions);
|
||||
} else {
|
||||
if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
|
||||
|| PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!PACKET_copy_all(&compression, clienthello->compressions,
|
||||
MAX_COMPRESSIONS_SIZE,
|
||||
&clienthello->compressions_len)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Preserve the raw extensions PACKET for later use */
|
||||
extensions = clienthello->extensions;
|
||||
if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
|
||||
&clienthello->pre_proc_exts,
|
||||
&clienthello->pre_proc_exts_len, 1)) {
|
||||
/* SSLfatal already been called */
|
||||
goto err;
|
||||
}
|
||||
s->clienthello = clienthello;
|
||||
|
||||
return MSG_PROCESS_CONTINUE_PROCESSING;
|
||||
|
||||
err:
|
||||
if (clienthello != NULL)
|
||||
OPENSSL_free(clienthello->pre_proc_exts);
|
||||
OPENSSL_free(clienthello);
|
||||
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
static int tls_early_post_process_client_hello(SSL *s)
|
||||
{
|
||||
unsigned int j;
|
||||
int i, al = SSL_AD_INTERNAL_ERROR;
|
||||
int protverr;
|
||||
size_t loop;
|
||||
unsigned long id;
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
SSL_COMP *comp = NULL;
|
||||
#endif
|
||||
const SSL_CIPHER *c;
|
||||
STACK_OF(SSL_CIPHER) *ciphers = NULL;
|
||||
STACK_OF(SSL_CIPHER) *scsvs = NULL;
|
||||
CLIENTHELLO_MSG *clienthello = s->clienthello;
|
||||
DOWNGRADE dgrd = DOWNGRADE_NONE;
|
||||
|
||||
/* Finished parsing the ClientHello, now we can start processing it */
|
||||
/* Give the ClientHello callback a crack at things */
|
||||
if (s->ctx->client_hello_cb != NULL) {
|
||||
/* A failure in the ClientHello callback terminates the connection. */
|
||||
switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
|
||||
case SSL_CLIENT_HELLO_SUCCESS:
|
||||
break;
|
||||
case SSL_CLIENT_HELLO_RETRY:
|
||||
s->rwstate = SSL_CLIENT_HELLO_CB;
|
||||
return -1;
|
||||
case SSL_CLIENT_HELLO_ERROR:
|
||||
default:
|
||||
SSLfatal(s, al,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_CALLBACK_FAILED);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set up the client_random */
|
||||
memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);
|
||||
|
||||
/* Choose the version */
|
||||
|
||||
if (clienthello->isv2) {
|
||||
if (clienthello->legacy_version == SSL2_VERSION
|
||||
|| (clienthello->legacy_version & 0xff00)
|
||||
!= (SSL3_VERSION_MAJOR << 8)) {
|
||||
/*
|
||||
* This is real SSLv2 or something completely unknown. We don't
|
||||
* support it.
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_UNKNOWN_PROTOCOL);
|
||||
goto err;
|
||||
}
|
||||
/* SSLv3/TLS */
|
||||
s->client_version = clienthello->legacy_version;
|
||||
}
|
||||
/*
|
||||
* Do SSL/TLS version negotiation if applicable. For DTLS we just check
|
||||
* versions are potentially compatible. Version negotiation comes later.
|
||||
*/
|
||||
if (!SSL_IS_DTLS(s)) {
|
||||
protverr = ssl_choose_server_version(s, clienthello, &dgrd);
|
||||
} else if (s->method->version != DTLS_ANY_VERSION &&
|
||||
DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
|
||||
protverr = SSL_R_VERSION_TOO_LOW;
|
||||
} else {
|
||||
protverr = 0;
|
||||
}
|
||||
|
||||
if (protverr) {
|
||||
if (SSL_IS_FIRST_HANDSHAKE(s)) {
|
||||
/* like ssl3_get_record, send alert using remote version number */
|
||||
s->version = s->client_version = clienthello->legacy_version;
|
||||
}
|
||||
SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* TLSv1.3 specifies that a ClientHello must end on a record boundary */
|
||||
if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_NOT_ON_RECORD_BOUNDARY);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
/* Empty cookie was already handled above by returning early. */
|
||||
if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
|
||||
if (s->ctx->app_verify_cookie_cb != NULL) {
|
||||
if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
|
||||
clienthello->dtls_cookie_len) == 0) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_COOKIE_MISMATCH);
|
||||
goto err;
|
||||
/* else cookie verification succeeded */
|
||||
}
|
||||
/* default verification */
|
||||
} else if (s->d1->cookie_len != clienthello->dtls_cookie_len
|
||||
|| memcmp(clienthello->dtls_cookie, s->d1->cookie,
|
||||
s->d1->cookie_len) != 0) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_COOKIE_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
s->d1->cookie_verified = 1;
|
||||
}
|
||||
if (s->method->version == DTLS_ANY_VERSION) {
|
||||
protverr = ssl_choose_server_version(s, clienthello, &dgrd);
|
||||
if (protverr != 0) {
|
||||
s->version = s->client_version;
|
||||
SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s->hit = 0;
|
||||
|
||||
if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
|
||||
clienthello->isv2) ||
|
||||
!bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
|
||||
clienthello->isv2, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
s->s3.send_connection_binding = 0;
|
||||
/* Check what signalling cipher-suite values were received. */
|
||||
if (scsvs != NULL) {
|
||||
for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
|
||||
c = sk_SSL_CIPHER_value(scsvs, i);
|
||||
if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
|
||||
if (s->renegotiate) {
|
||||
/* SCSV is fatal if renegotiating */
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
|
||||
goto err;
|
||||
}
|
||||
s->s3.send_connection_binding = 1;
|
||||
} else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
|
||||
!ssl_check_version_downgrade(s)) {
|
||||
/*
|
||||
* This SCSV indicates that the client previously tried
|
||||
* a higher version. We should fail if the current version
|
||||
* is an unexpected downgrade, as that indicates that the first
|
||||
* connection may have been tampered with in order to trigger
|
||||
* an insecure downgrade.
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_INAPPROPRIATE_FALLBACK);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* For TLSv1.3 we must select the ciphersuite *before* session resumption */
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
const SSL_CIPHER *cipher =
|
||||
ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
|
||||
|
||||
if (cipher == NULL) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_NO_SHARED_CIPHER);
|
||||
goto err;
|
||||
}
|
||||
if (s->hello_retry_request == SSL_HRR_PENDING
|
||||
&& (s->s3.tmp.new_cipher == NULL
|
||||
|| s->s3.tmp.new_cipher->id != cipher->id)) {
|
||||
/*
|
||||
* A previous HRR picked a different ciphersuite to the one we
|
||||
* just selected. Something must have changed.
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_BAD_CIPHER);
|
||||
goto err;
|
||||
}
|
||||
s->s3.tmp.new_cipher = cipher;
|
||||
}
|
||||
|
||||
/* We need to do this before getting the session */
|
||||
if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
|
||||
SSL_EXT_CLIENT_HELLO,
|
||||
clienthello->pre_proc_exts, NULL, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* We don't allow resumption in a backwards compatible ClientHello.
|
||||
* TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
|
||||
*
|
||||
* Versions before 0.9.7 always allow clients to resume sessions in
|
||||
* renegotiation. 0.9.7 and later allow this by default, but optionally
|
||||
* ignore resumption requests with flag
|
||||
* SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
|
||||
* than a change to default behavior so that applications relying on
|
||||
* this for security won't even compile against older library versions).
|
||||
* 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
|
||||
* request renegotiation but not a new session (s->new_session remains
|
||||
* unset): for servers, this essentially just means that the
|
||||
* SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
|
||||
* ignored.
|
||||
*/
|
||||
if (clienthello->isv2 ||
|
||||
(s->new_session &&
|
||||
(s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
|
||||
if (!ssl_get_new_session(s, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
i = ssl_get_prev_session(s, clienthello);
|
||||
if (i == 1) {
|
||||
/* previous session */
|
||||
s->hit = 1;
|
||||
} else if (i == -1) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
} else {
|
||||
/* i == 0 */
|
||||
if (!ssl_get_new_session(s, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
memcpy(s->tmp_session_id, s->clienthello->session_id,
|
||||
s->clienthello->session_id_len);
|
||||
s->tmp_session_id_len = s->clienthello->session_id_len;
|
||||
}
|
||||
|
||||
/*
|
||||
* If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
|
||||
* ciphersuite compatibility with the session as part of resumption.
|
||||
*/
|
||||
if (!SSL_IS_TLS13(s) && s->hit) {
|
||||
j = 0;
|
||||
id = s->session->cipher->id;
|
||||
|
||||
OSSL_TRACE_BEGIN(TLS_CIPHER) {
|
||||
BIO_printf(trc_out, "client sent %d ciphers\n",
|
||||
sk_SSL_CIPHER_num(ciphers));
|
||||
}
|
||||
for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
|
||||
c = sk_SSL_CIPHER_value(ciphers, i);
|
||||
if (trc_out != NULL)
|
||||
BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
|
||||
sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
|
||||
if (c->id == id) {
|
||||
j = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == 0) {
|
||||
/*
|
||||
* we need to have the cipher in the cipher list if we are asked
|
||||
* to reuse it
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_REQUIRED_CIPHER_MISSING);
|
||||
OSSL_TRACE_CANCEL(TLS_CIPHER);
|
||||
goto err;
|
||||
}
|
||||
OSSL_TRACE_END(TLS_CIPHER);
|
||||
}
|
||||
|
||||
for (loop = 0; loop < clienthello->compressions_len; loop++) {
|
||||
if (clienthello->compressions[loop] == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (loop >= clienthello->compressions_len) {
|
||||
/* no compress */
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_NO_COMPRESSION_SPECIFIED);
|
||||
goto err;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
|
||||
ssl_check_for_safari(s, clienthello);
|
||||
#endif /* !OPENSSL_NO_EC */
|
||||
|
||||
/* TLS extensions */
|
||||
if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
|
||||
clienthello->pre_proc_exts, NULL, 0, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if we want to use external pre-shared secret for this handshake
|
||||
* for not reused session only. We need to generate server_random before
|
||||
* calling tls_session_secret_cb in order to allow SessionTicket
|
||||
* processing to use it in key derivation.
|
||||
*/
|
||||
{
|
||||
unsigned char *pos;
|
||||
pos = s->s3.server_random;
|
||||
if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!s->hit
|
||||
&& s->version >= TLS1_VERSION
|
||||
&& !SSL_IS_TLS13(s)
|
||||
&& !SSL_IS_DTLS(s)
|
||||
&& s->ext.session_secret_cb) {
|
||||
const SSL_CIPHER *pref_cipher = NULL;
|
||||
/*
|
||||
* s->session->master_key_length is a size_t, but this is an int for
|
||||
* backwards compat reasons
|
||||
*/
|
||||
int master_key_length;
|
||||
|
||||
master_key_length = sizeof(s->session->master_key);
|
||||
if (s->ext.session_secret_cb(s, s->session->master_key,
|
||||
&master_key_length, ciphers,
|
||||
&pref_cipher,
|
||||
s->ext.session_secret_cb_arg)
|
||||
&& master_key_length > 0) {
|
||||
s->session->master_key_length = master_key_length;
|
||||
s->hit = 1;
|
||||
s->peer_ciphers = ciphers;
|
||||
s->session->verify_result = X509_V_OK;
|
||||
|
||||
ciphers = NULL;
|
||||
|
||||
/* check if some cipher was preferred by call back */
|
||||
if (pref_cipher == NULL)
|
||||
pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
|
||||
SSL_get_ciphers(s));
|
||||
if (pref_cipher == NULL) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_NO_SHARED_CIPHER);
|
||||
goto err;
|
||||
}
|
||||
|
||||
s->session->cipher = pref_cipher;
|
||||
sk_SSL_CIPHER_free(s->cipher_list);
|
||||
s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
|
||||
sk_SSL_CIPHER_free(s->cipher_list_by_id);
|
||||
s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Worst case, we will use the NULL compression, but if we have other
|
||||
* options, we will now look for them. We have complen-1 compression
|
||||
* algorithms from the client, starting at q.
|
||||
*/
|
||||
s->s3.tmp.new_compression = NULL;
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
/*
|
||||
* We already checked above that the NULL compression method appears in
|
||||
* the list. Now we check there aren't any others (which is illegal in
|
||||
* a TLSv1.3 ClientHello.
|
||||
*/
|
||||
if (clienthello->compressions_len != 1) {
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_INVALID_COMPRESSION_ALGORITHM);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
/* This only happens if we have a cache hit */
|
||||
else if (s->session->compress_meth != 0) {
|
||||
int m, comp_id = s->session->compress_meth;
|
||||
unsigned int k;
|
||||
/* Perform sanity checks on resumed compression algorithm */
|
||||
/* Can't disable compression */
|
||||
if (!ssl_allow_compression(s)) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_INCONSISTENT_COMPRESSION);
|
||||
goto err;
|
||||
}
|
||||
/* Look for resumed compression method */
|
||||
for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
|
||||
comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
|
||||
if (comp_id == comp->id) {
|
||||
s->s3.tmp.new_compression = comp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (s->s3.tmp.new_compression == NULL) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_INVALID_COMPRESSION_ALGORITHM);
|
||||
goto err;
|
||||
}
|
||||
/* Look for resumed method in compression list */
|
||||
for (k = 0; k < clienthello->compressions_len; k++) {
|
||||
if (clienthello->compressions[k] == comp_id)
|
||||
break;
|
||||
}
|
||||
if (k >= clienthello->compressions_len) {
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
|
||||
goto err;
|
||||
}
|
||||
} else if (s->hit) {
|
||||
comp = NULL;
|
||||
} else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
|
||||
/* See if we have a match */
|
||||
int m, nn, v, done = 0;
|
||||
unsigned int o;
|
||||
|
||||
nn = sk_SSL_COMP_num(s->ctx->comp_methods);
|
||||
for (m = 0; m < nn; m++) {
|
||||
comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
|
||||
v = comp->id;
|
||||
for (o = 0; o < clienthello->compressions_len; o++) {
|
||||
if (v == clienthello->compressions[o]) {
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (done)
|
||||
break;
|
||||
}
|
||||
if (done)
|
||||
s->s3.tmp.new_compression = comp;
|
||||
else
|
||||
comp = NULL;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
* If compression is disabled we'd better not try to resume a session
|
||||
* using compression.
|
||||
*/
|
||||
if (s->session->compress_meth != 0) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_INCONSISTENT_COMPRESSION);
|
||||
goto err;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
|
||||
*/
|
||||
|
||||
if (!s->hit || SSL_IS_TLS13(s)) {
|
||||
sk_SSL_CIPHER_free(s->peer_ciphers);
|
||||
s->peer_ciphers = ciphers;
|
||||
if (ciphers == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
ciphers = NULL;
|
||||
}
|
||||
|
||||
if (!s->hit) {
|
||||
#ifdef OPENSSL_NO_COMP
|
||||
s->session->compress_meth = 0;
|
||||
#else
|
||||
s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
|
||||
#endif
|
||||
if (!tls1_set_server_sigalgs(s)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
sk_SSL_CIPHER_free(ciphers);
|
||||
sk_SSL_CIPHER_free(scsvs);
|
||||
OPENSSL_free(clienthello->pre_proc_exts);
|
||||
OPENSSL_free(s->clienthello);
|
||||
s->clienthello = NULL;
|
||||
return 1;
|
||||
err:
|
||||
sk_SSL_CIPHER_free(ciphers);
|
||||
sk_SSL_CIPHER_free(scsvs);
|
||||
OPENSSL_free(clienthello->pre_proc_exts);
|
||||
OPENSSL_free(s->clienthello);
|
||||
s->clienthello = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Call the status request callback if needed. Upon success, returns 1.
|
||||
* Upon failure, returns 0.
|
||||
*/
|
||||
static int tls_handle_status_request(SSL *s)
|
||||
{
|
||||
s->ext.status_expected = 0;
|
||||
|
||||
/*
|
||||
* If status request then ask callback what to do. Note: this must be
|
||||
* called after servername callbacks in case the certificate has changed,
|
||||
* and must be called after the cipher has been chosen because this may
|
||||
* influence which certificate is sent
|
||||
*/
|
||||
if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
|
||||
&& s->ctx->ext.status_cb != NULL) {
|
||||
int ret;
|
||||
|
||||
/* If no certificate can't return certificate status */
|
||||
if (s->s3.tmp.cert != NULL) {
|
||||
/*
|
||||
* Set current certificate to one we will use so SSL_get_certificate
|
||||
* et al can pick it up.
|
||||
*/
|
||||
s->cert->key = s->s3.tmp.cert;
|
||||
ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
|
||||
switch (ret) {
|
||||
/* We don't want to send a status request response */
|
||||
case SSL_TLSEXT_ERR_NOACK:
|
||||
s->ext.status_expected = 0;
|
||||
break;
|
||||
/* status request response should be sent */
|
||||
case SSL_TLSEXT_ERR_OK:
|
||||
if (s->ext.ocsp.resp)
|
||||
s->ext.status_expected = 1;
|
||||
break;
|
||||
/* something bad happened */
|
||||
case SSL_TLSEXT_ERR_ALERT_FATAL:
|
||||
default:
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_HANDLE_STATUS_REQUEST,
|
||||
SSL_R_CLIENTHELLO_TLSEXT);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Call the alpn_select callback if needed. Upon success, returns 1.
|
||||
* Upon failure, returns 0.
|
||||
*/
|
||||
int tls_handle_alpn(SSL *s)
|
||||
{
|
||||
const unsigned char *selected = NULL;
|
||||
unsigned char selected_len = 0;
|
||||
|
||||
if (s->ctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) {
|
||||
int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
|
||||
s->s3.alpn_proposed,
|
||||
(unsigned int)s->s3.alpn_proposed_len,
|
||||
s->ctx->ext.alpn_select_cb_arg);
|
||||
|
||||
if (r == SSL_TLSEXT_ERR_OK) {
|
||||
OPENSSL_free(s->s3.alpn_selected);
|
||||
s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);
|
||||
if (s->s3.alpn_selected == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_HANDLE_ALPN,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
s->s3.alpn_selected_len = selected_len;
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
/* ALPN takes precedence over NPN. */
|
||||
s->s3.npn_seen = 0;
|
||||
#endif
|
||||
|
||||
/* Check ALPN is consistent with session */
|
||||
if (s->session->ext.alpn_selected == NULL
|
||||
|| selected_len != s->session->ext.alpn_selected_len
|
||||
|| memcmp(selected, s->session->ext.alpn_selected,
|
||||
selected_len) != 0) {
|
||||
/* Not consistent so can't be used for early_data */
|
||||
s->ext.early_data_ok = 0;
|
||||
|
||||
if (!s->hit) {
|
||||
/*
|
||||
* This is a new session and so alpn_selected should have
|
||||
* been initialised to NULL. We should update it with the
|
||||
* selected ALPN.
|
||||
*/
|
||||
if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_HANDLE_ALPN,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
s->session->ext.alpn_selected = OPENSSL_memdup(selected,
|
||||
selected_len);
|
||||
if (s->session->ext.alpn_selected == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_HANDLE_ALPN,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
s->session->ext.alpn_selected_len = selected_len;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
} else if (r != SSL_TLSEXT_ERR_NOACK) {
|
||||
SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, SSL_F_TLS_HANDLE_ALPN,
|
||||
SSL_R_NO_APPLICATION_PROTOCOL);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
|
||||
* present.
|
||||
*/
|
||||
}
|
||||
|
||||
/* Check ALPN is consistent with session */
|
||||
if (s->session->ext.alpn_selected != NULL) {
|
||||
/* Not consistent so can't be used for early_data */
|
||||
s->ext.early_data_ok = 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
|
||||
{
|
||||
const SSL_CIPHER *cipher;
|
||||
|
||||
if (wst == WORK_MORE_A) {
|
||||
int rv = tls_early_post_process_client_hello(s);
|
||||
if (rv == 0) {
|
||||
/* SSLfatal() was already called */
|
||||
goto err;
|
||||
}
|
||||
if (rv < 0)
|
||||
return WORK_MORE_A;
|
||||
wst = WORK_MORE_B;
|
||||
}
|
||||
if (wst == WORK_MORE_B) {
|
||||
if (!s->hit || SSL_IS_TLS13(s)) {
|
||||
/* Let cert callback update server certificates if required */
|
||||
if (!s->hit && s->cert->cert_cb != NULL) {
|
||||
int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
|
||||
if (rv == 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_CERT_CB_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (rv < 0) {
|
||||
s->rwstate = SSL_X509_LOOKUP;
|
||||
return WORK_MORE_B;
|
||||
}
|
||||
s->rwstate = SSL_NOTHING;
|
||||
}
|
||||
|
||||
/* In TLSv1.3 we selected the ciphersuite before resumption */
|
||||
if (!SSL_IS_TLS13(s)) {
|
||||
cipher =
|
||||
ssl3_choose_cipher(s, s->peer_ciphers, SSL_get_ciphers(s));
|
||||
|
||||
if (cipher == NULL) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
|
||||
SSL_R_NO_SHARED_CIPHER);
|
||||
goto err;
|
||||
}
|
||||
s->s3.tmp.new_cipher = cipher;
|
||||
}
|
||||
if (!s->hit) {
|
||||
if (!tls_choose_sigalg(s, 1)) {
|
||||
/* SSLfatal already called */
|
||||
goto err;
|
||||
}
|
||||
/* check whether we should disable session resumption */
|
||||
if (s->not_resumable_session_cb != NULL)
|
||||
s->session->not_resumable =
|
||||
s->not_resumable_session_cb(s,
|
||||
((s->s3.tmp.new_cipher->algorithm_mkey
|
||||
& (SSL_kDHE | SSL_kECDHE)) != 0));
|
||||
if (s->session->not_resumable)
|
||||
/* do not send a session ticket */
|
||||
s->ext.ticket_expected = 0;
|
||||
}
|
||||
} else {
|
||||
/* Session-id reuse */
|
||||
s->s3.tmp.new_cipher = s->session->cipher;
|
||||
}
|
||||
|
||||
/*-
|
||||
* we now have the following setup.
|
||||
* client_random
|
||||
* cipher_list - our preferred list of ciphers
|
||||
* ciphers - the clients preferred list of ciphers
|
||||
* compression - basically ignored right now
|
||||
* ssl version is set - sslv3
|
||||
* s->session - The ssl session has been setup.
|
||||
* s->hit - session reuse flag
|
||||
* s->s3.tmp.new_cipher - the new cipher to use.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Call status_request callback if needed. Has to be done after the
|
||||
* certificate callbacks etc above.
|
||||
*/
|
||||
if (!tls_handle_status_request(s)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
/*
|
||||
* Call alpn_select callback if needed. Has to be done after SNI and
|
||||
* cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
|
||||
* we already did this because cipher negotiation happens earlier, and
|
||||
* we must handle ALPN before we decide whether to accept early_data.
|
||||
*/
|
||||
if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
wst = WORK_MORE_C;
|
||||
}
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
if (wst == WORK_MORE_C) {
|
||||
int ret;
|
||||
if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
|
||||
/*
|
||||
* callback indicates further work to be done
|
||||
*/
|
||||
s->rwstate = SSL_X509_LOOKUP;
|
||||
return WORK_MORE_C;
|
||||
}
|
||||
if (ret < 0) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return WORK_FINISHED_STOP;
|
||||
err:
|
||||
return WORK_ERROR;
|
||||
}
|
||||
|
||||
int tls_construct_server_hello(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
int compm;
|
||||
size_t sl, len;
|
||||
int version;
|
||||
unsigned char *session_id;
|
||||
int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
|
||||
|
||||
version = usetls13 ? TLS1_2_VERSION : s->version;
|
||||
if (!WPACKET_put_bytes_u16(pkt, version)
|
||||
/*
|
||||
* Random stuff. Filling of the server_random takes place in
|
||||
* tls_process_client_hello()
|
||||
*/
|
||||
|| !WPACKET_memcpy(pkt,
|
||||
s->hello_retry_request == SSL_HRR_PENDING
|
||||
? hrrrandom : s->s3.server_random,
|
||||
SSL3_RANDOM_SIZE)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
* There are several cases for the session ID to send
|
||||
* back in the server hello:
|
||||
* - For session reuse from the session cache,
|
||||
* we send back the old session ID.
|
||||
* - If stateless session reuse (using a session ticket)
|
||||
* is successful, we send back the client's "session ID"
|
||||
* (which doesn't actually identify the session).
|
||||
* - If it is a new session, we send back the new
|
||||
* session ID.
|
||||
* - However, if we want the new session to be single-use,
|
||||
* we send back a 0-length session ID.
|
||||
* - In TLSv1.3 we echo back the session id sent to us by the client
|
||||
* regardless
|
||||
* s->hit is non-zero in either case of session reuse,
|
||||
* so the following won't overwrite an ID that we're supposed
|
||||
* to send back.
|
||||
*/
|
||||
if (s->session->not_resumable ||
|
||||
(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
|
||||
&& !s->hit))
|
||||
s->session->session_id_length = 0;
|
||||
|
||||
if (usetls13) {
|
||||
sl = s->tmp_session_id_len;
|
||||
session_id = s->tmp_session_id;
|
||||
} else {
|
||||
sl = s->session->session_id_length;
|
||||
session_id = s->session->session_id;
|
||||
}
|
||||
|
||||
if (sl > sizeof(s->session->session_id)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* set up the compression method */
|
||||
#ifdef OPENSSL_NO_COMP
|
||||
compm = 0;
|
||||
#else
|
||||
if (usetls13 || s->s3.tmp.new_compression == NULL)
|
||||
compm = 0;
|
||||
else
|
||||
compm = s->s3.tmp.new_compression->id;
|
||||
#endif
|
||||
|
||||
if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
|
||||
|| !s->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt, &len)
|
||||
|| !WPACKET_put_bytes_u8(pkt, compm)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!tls_construct_extensions(s, pkt,
|
||||
s->hello_retry_request == SSL_HRR_PENDING
|
||||
? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
|
||||
: (SSL_IS_TLS13(s)
|
||||
? SSL_EXT_TLS1_3_SERVER_HELLO
|
||||
: SSL_EXT_TLS1_2_SERVER_HELLO),
|
||||
NULL, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s->hello_retry_request == SSL_HRR_PENDING) {
|
||||
/* Ditch the session. We'll create a new one next time around */
|
||||
SSL_SESSION_free(s->session);
|
||||
s->session = NULL;
|
||||
s->hit = 0;
|
||||
|
||||
/*
|
||||
* Re-initialise the Transcript Hash. We're going to prepopulate it with
|
||||
* a synthetic message_hash in place of ClientHello1.
|
||||
*/
|
||||
if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
} else if (!(s->verify_mode & SSL_VERIFY_PEER)
|
||||
&& !ssl3_digest_cached_records(s, 0)) {
|
||||
/* SSLfatal() already called */;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_construct_server_done(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
if (!s->s3.tmp.cert_request) {
|
||||
if (!ssl3_digest_cached_records(s, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
#ifndef OPENSSL_NO_DH
|
||||
EVP_PKEY *pkdh = NULL;
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_EC
|
||||
unsigned char *encodedPoint = NULL;
|
||||
size_t encodedlen = 0;
|
||||
int curve_id = 0;
|
||||
#endif
|
||||
const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
|
||||
int i;
|
||||
unsigned long type;
|
||||
const BIGNUM *r[4];
|
||||
EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
size_t paramlen, paramoffset;
|
||||
|
||||
if (!WPACKET_get_total_written(pkt, ¶moffset)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (md_ctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
type = s->s3.tmp.new_cipher->algorithm_mkey;
|
||||
|
||||
r[0] = r[1] = r[2] = r[3] = NULL;
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
/* Plain PSK or RSAPSK nothing to do */
|
||||
if (type & (SSL_kPSK | SSL_kRSAPSK)) {
|
||||
} else
|
||||
#endif /* !OPENSSL_NO_PSK */
|
||||
#ifndef OPENSSL_NO_DH
|
||||
if (type & (SSL_kDHE | SSL_kDHEPSK)) {
|
||||
CERT *cert = s->cert;
|
||||
|
||||
EVP_PKEY *pkdhp = NULL;
|
||||
DH *dh;
|
||||
|
||||
if (s->cert->dh_tmp_auto) {
|
||||
DH *dhp = ssl_get_auto_dh(s);
|
||||
pkdh = EVP_PKEY_new();
|
||||
if (pkdh == NULL || dhp == NULL) {
|
||||
DH_free(dhp);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
EVP_PKEY_assign_DH(pkdh, dhp);
|
||||
pkdhp = pkdh;
|
||||
} else {
|
||||
pkdhp = cert->dh_tmp;
|
||||
}
|
||||
if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
|
||||
DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
|
||||
pkdh = ssl_dh_to_pkey(dhp);
|
||||
if (pkdh == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
pkdhp = pkdh;
|
||||
}
|
||||
if (pkdhp == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
SSL_R_MISSING_TMP_DH_KEY);
|
||||
goto err;
|
||||
}
|
||||
if (!ssl_security(s, SSL_SECOP_TMP_DH,
|
||||
EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
SSL_R_DH_KEY_TOO_SMALL);
|
||||
goto err;
|
||||
}
|
||||
if (s->s3.tmp.pkey != NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
s->s3.tmp.pkey = ssl_generate_pkey(pkdhp);
|
||||
if (s->s3.tmp.pkey == NULL) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
dh = EVP_PKEY_get0_DH(s->s3.tmp.pkey);
|
||||
if (dh == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
EVP_PKEY_free(pkdh);
|
||||
pkdh = NULL;
|
||||
|
||||
DH_get0_pqg(dh, &r[0], NULL, &r[1]);
|
||||
DH_get0_key(dh, &r[2], NULL);
|
||||
} else
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_EC
|
||||
if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
|
||||
|
||||
if (s->s3.tmp.pkey != NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Get NID of appropriate shared curve */
|
||||
curve_id = tls1_shared_group(s, -2);
|
||||
if (curve_id == 0) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
|
||||
goto err;
|
||||
}
|
||||
s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);
|
||||
/* Generate a new key for this curve */
|
||||
if (s->s3.tmp.pkey == NULL) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Encode the public key. */
|
||||
encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3.tmp.pkey,
|
||||
&encodedPoint);
|
||||
if (encodedlen == 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* We'll generate the serverKeyExchange message explicitly so we
|
||||
* can set these to NULLs
|
||||
*/
|
||||
r[0] = NULL;
|
||||
r[1] = NULL;
|
||||
r[2] = NULL;
|
||||
r[3] = NULL;
|
||||
} else
|
||||
#endif /* !OPENSSL_NO_EC */
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
if (type & SSL_kSRP) {
|
||||
if ((s->srp_ctx.N == NULL) ||
|
||||
(s->srp_ctx.g == NULL) ||
|
||||
(s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
SSL_R_MISSING_SRP_PARAM);
|
||||
goto err;
|
||||
}
|
||||
r[0] = s->srp_ctx.N;
|
||||
r[1] = s->srp_ctx.g;
|
||||
r[2] = s->srp_ctx.s;
|
||||
r[3] = s->srp_ctx.B;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
|
||||
|| ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
|
||||
lu = NULL;
|
||||
} else if (lu == NULL) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
if (type & SSL_PSK) {
|
||||
size_t len = (s->cert->psk_identity_hint == NULL)
|
||||
? 0 : strlen(s->cert->psk_identity_hint);
|
||||
|
||||
/*
|
||||
* It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
|
||||
* checked this when we set the identity hint - but just in case
|
||||
*/
|
||||
if (len > PSK_MAX_IDENTITY_LEN
|
||||
|| !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
|
||||
len)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
for (i = 0; i < 4 && r[i] != NULL; i++) {
|
||||
unsigned char *binval;
|
||||
int res;
|
||||
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
if ((i == 2) && (type & SSL_kSRP)) {
|
||||
res = WPACKET_start_sub_packet_u8(pkt);
|
||||
} else
|
||||
#endif
|
||||
res = WPACKET_start_sub_packet_u16(pkt);
|
||||
|
||||
if (!res) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_DH
|
||||
/*-
|
||||
* for interoperability with some versions of the Microsoft TLS
|
||||
* stack, we need to zero pad the DHE pub key to the same length
|
||||
* as the prime
|
||||
*/
|
||||
if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
|
||||
size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
|
||||
|
||||
if (len > 0) {
|
||||
if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
memset(binval, 0, len);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
|
||||
|| !WPACKET_close(pkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
BN_bn2bin(r[i], binval);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
|
||||
/*
|
||||
* We only support named (not generic) curves. In this situation, the
|
||||
* ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
|
||||
* [1 byte length of encoded point], followed by the actual encoded
|
||||
* point itself
|
||||
*/
|
||||
if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
|
||||
|| !WPACKET_put_bytes_u8(pkt, 0)
|
||||
|| !WPACKET_put_bytes_u8(pkt, curve_id)
|
||||
|| !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
OPENSSL_free(encodedPoint);
|
||||
encodedPoint = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* not anonymous */
|
||||
if (lu != NULL) {
|
||||
EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
|
||||
const EVP_MD *md;
|
||||
unsigned char *sigbytes1, *sigbytes2, *tbs;
|
||||
size_t siglen = 0, tbslen;
|
||||
|
||||
if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
|
||||
/* Should never happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
/* Get length of the parameters we have written above */
|
||||
if (!WPACKET_get_length(pkt, ¶mlen)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
/* send signature algorithm */
|
||||
if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (lu->sig == EVP_PKEY_RSA_PSS) {
|
||||
if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
|
||||
|| EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_EVP_LIB);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
tbslen = construct_key_exchange_tbs(s, &tbs,
|
||||
s->init_buf->data + paramoffset,
|
||||
paramlen);
|
||||
if (tbslen == 0) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0
|
||||
|| !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
|
||||
|| EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
|
||||
|| !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
|
||||
|| sigbytes1 != sigbytes2) {
|
||||
OPENSSL_free(tbs);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
OPENSSL_free(tbs);
|
||||
}
|
||||
|
||||
EVP_MD_CTX_free(md_ctx);
|
||||
return 1;
|
||||
err:
|
||||
#ifndef OPENSSL_NO_DH
|
||||
EVP_PKEY_free(pkdh);
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_EC
|
||||
OPENSSL_free(encodedPoint);
|
||||
#endif
|
||||
EVP_MD_CTX_free(md_ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
/* Send random context when doing post-handshake auth */
|
||||
if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
|
||||
OPENSSL_free(s->pha_context);
|
||||
s->pha_context_len = 32;
|
||||
if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL
|
||||
|| RAND_bytes(s->pha_context, s->pha_context_len) <= 0
|
||||
|| !WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
/* reset the handshake hash back to just after the ClientFinished */
|
||||
if (!tls13_restore_handshake_digest_for_pha(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (!WPACKET_put_bytes_u8(pkt, 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!tls_construct_extensions(s, pkt,
|
||||
SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
|
||||
0)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* get the list of acceptable cert types */
|
||||
if (!WPACKET_start_sub_packet_u8(pkt)
|
||||
|| !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (SSL_USE_SIGALGS(s)) {
|
||||
const uint16_t *psigs;
|
||||
size_t nl = tls12_get_psigalgs(s, 1, &psigs);
|
||||
|
||||
if (!WPACKET_start_sub_packet_u16(pkt)
|
||||
|| !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
|
||||
|| !tls12_copy_sigalgs(s, pkt, psigs, nl)
|
||||
|| !WPACKET_close(pkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!construct_ca_names(s, get_ca_names(s), pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
done:
|
||||
s->certreqs_sent++;
|
||||
s->s3.tmp.cert_request = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
|
||||
{
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
unsigned char psk[PSK_MAX_PSK_LEN];
|
||||
size_t psklen;
|
||||
PACKET psk_identity;
|
||||
|
||||
if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
|
||||
SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
if (s->psk_server_callback == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
|
||||
SSL_R_PSK_NO_SERVER_CB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
psklen = s->psk_server_callback(s, s->session->psk_identity,
|
||||
psk, sizeof(psk));
|
||||
|
||||
if (psklen > PSK_MAX_PSK_LEN) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
} else if (psklen == 0) {
|
||||
/*
|
||||
* PSK related to the given identity not found
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
|
||||
SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
|
||||
SSL_R_PSK_IDENTITY_NOT_FOUND);
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_free(s->s3.tmp.psk);
|
||||
s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
|
||||
OPENSSL_cleanse(psk, psklen);
|
||||
|
||||
if (s->s3.tmp.psk == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
s->s3.tmp.psklen = psklen;
|
||||
|
||||
return 1;
|
||||
#else
|
||||
/* Should never happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
|
||||
{
|
||||
#ifndef OPENSSL_NO_RSA
|
||||
size_t outlen;
|
||||
PACKET enc_premaster;
|
||||
EVP_PKEY *rsa = NULL;
|
||||
unsigned char *rsa_decrypt = NULL;
|
||||
int ret = 0;
|
||||
EVP_PKEY_CTX *ctx = NULL;
|
||||
OSSL_PARAM params[3], *p = params;
|
||||
|
||||
rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
|
||||
if (rsa == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
SSL_R_MISSING_RSA_CERTIFICATE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* SSLv3 and pre-standard DTLS omit the length bytes. */
|
||||
if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
|
||||
enc_premaster = *pkt;
|
||||
} else {
|
||||
if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
|
||||
|| PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
outlen = SSL_MAX_MASTER_KEY_LENGTH;
|
||||
rsa_decrypt = OPENSSL_malloc(outlen);
|
||||
if (rsa_decrypt == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ctx = EVP_PKEY_CTX_new(rsa, NULL);
|
||||
if (ctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* We must not leak whether a decryption failure occurs because of
|
||||
* Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
|
||||
* section 7.4.7.1). We use the special padding type
|
||||
* RSA_PKCS1_WITH_TLS_PADDING to do that. It will automaticaly decrypt the
|
||||
* RSA, check the padding and check that the client version is as expected
|
||||
* in the premaster secret. If any of that fails then the function appears
|
||||
* to return successfully but with a random result. The call below could
|
||||
* still fail if the input is publicly invalid.
|
||||
* See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
|
||||
*/
|
||||
if (EVP_PKEY_decrypt_init(ctx) <= 0
|
||||
|| EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
|
||||
SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
SSL_R_DECRYPTION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
|
||||
*p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
|
||||
(unsigned int *)&s->client_version);
|
||||
if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
|
||||
*p++ = OSSL_PARAM_construct_uint(
|
||||
OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
|
||||
(unsigned int *)&s->version);
|
||||
*p++ = OSSL_PARAM_construct_end();
|
||||
|
||||
if (!EVP_PKEY_CTX_set_params(ctx, params)
|
||||
|| EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
|
||||
PACKET_data(&enc_premaster),
|
||||
PACKET_remaining(&enc_premaster)) <= 0) {
|
||||
SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
SSL_R_DECRYPTION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* This test should never fail (otherwise we should have failed above) but
|
||||
* we double check anyway.
|
||||
*/
|
||||
if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
|
||||
OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
|
||||
SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
SSL_R_DECRYPTION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Also cleanses rsa_decrypt (on success or failure) */
|
||||
if (!ssl_generate_master_secret(s, rsa_decrypt,
|
||||
SSL_MAX_MASTER_KEY_LENGTH, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
err:
|
||||
OPENSSL_free(rsa_decrypt);
|
||||
EVP_PKEY_CTX_free(ctx);
|
||||
return ret;
|
||||
#else
|
||||
/* Should never happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
|
||||
{
|
||||
#ifndef OPENSSL_NO_DH
|
||||
EVP_PKEY *skey = NULL;
|
||||
DH *cdh;
|
||||
unsigned int i;
|
||||
BIGNUM *pub_key;
|
||||
const unsigned char *data;
|
||||
EVP_PKEY *ckey = NULL;
|
||||
int ret = 0;
|
||||
|
||||
if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
|
||||
SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
|
||||
goto err;
|
||||
}
|
||||
skey = s->s3.tmp.pkey;
|
||||
if (skey == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
|
||||
SSL_R_MISSING_TMP_DH_KEY);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (PACKET_remaining(pkt) == 0L) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
|
||||
SSL_R_MISSING_TMP_DH_KEY);
|
||||
goto err;
|
||||
}
|
||||
if (!PACKET_get_bytes(pkt, &data, i)) {
|
||||
/* We already checked we have enough data */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
ckey = EVP_PKEY_new();
|
||||
if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
|
||||
SSL_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
cdh = EVP_PKEY_get0_DH(ckey);
|
||||
pub_key = BN_bin2bn(data, i, NULL);
|
||||
if (pub_key == NULL || cdh == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
BN_free(pub_key);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (ssl_derive(s, skey, ckey, 1) == 0) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
EVP_PKEY_free(s->s3.tmp.pkey);
|
||||
s->s3.tmp.pkey = NULL;
|
||||
err:
|
||||
EVP_PKEY_free(ckey);
|
||||
return ret;
|
||||
#else
|
||||
/* Should never happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
|
||||
{
|
||||
#ifndef OPENSSL_NO_EC
|
||||
EVP_PKEY *skey = s->s3.tmp.pkey;
|
||||
EVP_PKEY *ckey = NULL;
|
||||
int ret = 0;
|
||||
|
||||
if (PACKET_remaining(pkt) == 0L) {
|
||||
/* We don't support ECDH client auth */
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_CKE_ECDHE,
|
||||
SSL_R_MISSING_TMP_ECDH_KEY);
|
||||
goto err;
|
||||
} else {
|
||||
unsigned int i;
|
||||
const unsigned char *data;
|
||||
|
||||
/*
|
||||
* Get client's public key from encoded point in the
|
||||
* ClientKeyExchange message.
|
||||
*/
|
||||
|
||||
/* Get encoded point length */
|
||||
if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
|
||||
|| PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
if (skey == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
|
||||
SSL_R_MISSING_TMP_ECDH_KEY);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ckey = EVP_PKEY_new();
|
||||
if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
|
||||
ERR_R_EVP_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
|
||||
ERR_R_EC_LIB);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (ssl_derive(s, skey, ckey, 1) == 0) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
EVP_PKEY_free(s->s3.tmp.pkey);
|
||||
s->s3.tmp.pkey = NULL;
|
||||
err:
|
||||
EVP_PKEY_free(ckey);
|
||||
|
||||
return ret;
|
||||
#else
|
||||
/* Should never happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int tls_process_cke_srp(SSL *s, PACKET *pkt)
|
||||
{
|
||||
#ifndef OPENSSL_NO_SRP
|
||||
unsigned int i;
|
||||
const unsigned char *data;
|
||||
|
||||
if (!PACKET_get_net_2(pkt, &i)
|
||||
|| !PACKET_get_bytes(pkt, &data, i)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
|
||||
SSL_R_BAD_SRP_A_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
|
||||
ERR_R_BN_LIB);
|
||||
return 0;
|
||||
}
|
||||
if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CKE_SRP,
|
||||
SSL_R_BAD_SRP_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_free(s->session->srp_username);
|
||||
s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
|
||||
if (s->session->srp_username == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!srp_generate_server_master_secret(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
#else
|
||||
/* Should never happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int tls_process_cke_gost(SSL *s, PACKET *pkt)
|
||||
{
|
||||
#ifndef OPENSSL_NO_GOST
|
||||
EVP_PKEY_CTX *pkey_ctx;
|
||||
EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
|
||||
unsigned char premaster_secret[32];
|
||||
const unsigned char *start;
|
||||
size_t outlen = 32, inlen;
|
||||
unsigned long alg_a;
|
||||
GOST_KX_MESSAGE *pKX = NULL;
|
||||
const unsigned char *ptr;
|
||||
int ret = 0;
|
||||
|
||||
/* Get our certificate private key */
|
||||
alg_a = s->s3.tmp.new_cipher->algorithm_auth;
|
||||
if (alg_a & SSL_aGOST12) {
|
||||
/*
|
||||
* New GOST ciphersuites have SSL_aGOST01 bit too
|
||||
*/
|
||||
pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
|
||||
if (pk == NULL) {
|
||||
pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
|
||||
}
|
||||
if (pk == NULL) {
|
||||
pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
|
||||
}
|
||||
} else if (alg_a & SSL_aGOST01) {
|
||||
pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
|
||||
}
|
||||
|
||||
pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
|
||||
if (pkey_ctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* If client certificate is present and is of the same type, maybe
|
||||
* use it for key exchange. Don't mind errors from
|
||||
* EVP_PKEY_derive_set_peer, because it is completely valid to use a
|
||||
* client certificate for authorization only.
|
||||
*/
|
||||
client_pub_pkey = X509_get0_pubkey(s->session->peer);
|
||||
if (client_pub_pkey) {
|
||||
if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
|
||||
ERR_clear_error();
|
||||
}
|
||||
|
||||
ptr = PACKET_data(pkt);
|
||||
/* Some implementations provide extra data in the opaqueBlob
|
||||
* We have nothing to do with this blob so we just skip it */
|
||||
pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
|
||||
if (pKX == NULL
|
||||
|| pKX->kxBlob == NULL
|
||||
|| ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
SSL_R_DECRYPTION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
SSL_R_DECRYPTION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
SSL_R_DECRYPTION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
|
||||
inlen = pKX->kxBlob->value.sequence->length;
|
||||
start = pKX->kxBlob->value.sequence->data;
|
||||
|
||||
if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
|
||||
inlen) <= 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
SSL_R_DECRYPTION_FAILED);
|
||||
goto err;
|
||||
}
|
||||
/* Generate master secret */
|
||||
if (!ssl_generate_master_secret(s, premaster_secret,
|
||||
sizeof(premaster_secret), 0)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
/* Check if pubkey from client certificate was used */
|
||||
if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
|
||||
NULL) > 0)
|
||||
s->statem.no_cert_verify = 1;
|
||||
|
||||
ret = 1;
|
||||
err:
|
||||
EVP_PKEY_CTX_free(pkey_ctx);
|
||||
GOST_KX_MESSAGE_free(pKX);
|
||||
return ret;
|
||||
#else
|
||||
/* Should never happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
|
||||
{
|
||||
unsigned long alg_k;
|
||||
|
||||
alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
|
||||
|
||||
/* For PSK parse and retrieve identity, obtain PSK key */
|
||||
if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (alg_k & SSL_kPSK) {
|
||||
/* Identity extracted earlier: should be nothing left */
|
||||
if (PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
/* PSK handled by ssl_generate_master_secret */
|
||||
if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
|
||||
if (!tls_process_cke_rsa(s, pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
|
||||
if (!tls_process_cke_dhe(s, pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
|
||||
if (!tls_process_cke_ecdhe(s, pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else if (alg_k & SSL_kSRP) {
|
||||
if (!tls_process_cke_srp(s, pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else if (alg_k & SSL_kGOST) {
|
||||
if (!tls_process_cke_gost(s, pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
|
||||
SSL_R_UNKNOWN_CIPHER_TYPE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
return MSG_PROCESS_CONTINUE_PROCESSING;
|
||||
err:
|
||||
#ifndef OPENSSL_NO_PSK
|
||||
OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
|
||||
s->s3.tmp.psk = NULL;
|
||||
#endif
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
|
||||
{
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
if (wst == WORK_MORE_A) {
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
unsigned char sctpauthkey[64];
|
||||
char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
|
||||
size_t labellen;
|
||||
/*
|
||||
* Add new shared key for SCTP-Auth, will be ignored if no SCTP
|
||||
* used.
|
||||
*/
|
||||
memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
|
||||
sizeof(DTLS1_SCTP_AUTH_LABEL));
|
||||
|
||||
/* Don't include the terminating zero. */
|
||||
labellen = sizeof(labelbuffer) - 1;
|
||||
if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
|
||||
labellen += 1;
|
||||
|
||||
if (SSL_export_keying_material(s, sctpauthkey,
|
||||
sizeof(sctpauthkey), labelbuffer,
|
||||
labellen, NULL, 0,
|
||||
0) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return WORK_ERROR;
|
||||
}
|
||||
|
||||
BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
|
||||
sizeof(sctpauthkey), sctpauthkey);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (s->statem.no_cert_verify || !s->session->peer) {
|
||||
/*
|
||||
* No certificate verify or no peer certificate so we no longer need
|
||||
* the handshake_buffer
|
||||
*/
|
||||
if (!ssl3_digest_cached_records(s, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
return WORK_FINISHED_CONTINUE;
|
||||
} else {
|
||||
if (!s->s3.handshake_buffer) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return WORK_ERROR;
|
||||
}
|
||||
/*
|
||||
* For sigalgs freeze the handshake buffer. If we support
|
||||
* extms we've done this already so this is a no-op
|
||||
*/
|
||||
if (!ssl3_digest_cached_records(s, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
return WORK_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return WORK_FINISHED_CONTINUE;
|
||||
}
|
||||
|
||||
MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
|
||||
{
|
||||
int i;
|
||||
MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
|
||||
X509 *x = NULL;
|
||||
unsigned long l;
|
||||
const unsigned char *certstart, *certbytes;
|
||||
STACK_OF(X509) *sk = NULL;
|
||||
PACKET spkt, context;
|
||||
size_t chainidx;
|
||||
SSL_SESSION *new_sess = NULL;
|
||||
|
||||
/*
|
||||
* To get this far we must have read encrypted data from the client. We no
|
||||
* longer tolerate unencrypted alerts. This value is ignored if less than
|
||||
* TLSv1.3
|
||||
*/
|
||||
s->statem.enc_read_state = ENC_READ_STATE_VALID;
|
||||
|
||||
if ((sk = sk_X509_new_null()) == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (SSL_IS_TLS13(s) && (!PACKET_get_length_prefixed_1(pkt, &context)
|
||||
|| (s->pha_context == NULL && PACKET_remaining(&context) != 0)
|
||||
|| (s->pha_context != NULL &&
|
||||
!PACKET_equal(&context, s->pha_context, s->pha_context_len)))) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_INVALID_CONTEXT);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!PACKET_get_length_prefixed_3(pkt, &spkt)
|
||||
|| PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
|
||||
if (!PACKET_get_net_3(&spkt, &l)
|
||||
|| !PACKET_get_bytes(&spkt, &certbytes, l)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_CERT_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
certstart = certbytes;
|
||||
x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
|
||||
if (x == NULL) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (certbytes != (certstart + l)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_CERT_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
RAW_EXTENSION *rawexts = NULL;
|
||||
PACKET extensions;
|
||||
|
||||
if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_BAD_LENGTH);
|
||||
goto err;
|
||||
}
|
||||
if (!tls_collect_extensions(s, &extensions,
|
||||
SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
|
||||
NULL, chainidx == 0)
|
||||
|| !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
|
||||
rawexts, x, chainidx,
|
||||
PACKET_remaining(&spkt) == 0)) {
|
||||
OPENSSL_free(rawexts);
|
||||
goto err;
|
||||
}
|
||||
OPENSSL_free(rawexts);
|
||||
}
|
||||
|
||||
if (!sk_X509_push(sk, x)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
x = NULL;
|
||||
}
|
||||
|
||||
if (sk_X509_num(sk) <= 0) {
|
||||
/* TLS does not mind 0 certs returned */
|
||||
if (s->version == SSL3_VERSION) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_NO_CERTIFICATES_RETURNED);
|
||||
goto err;
|
||||
}
|
||||
/* Fail for TLS only if we required a certificate */
|
||||
else if ((s->verify_mode & SSL_VERIFY_PEER) &&
|
||||
(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
|
||||
SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
|
||||
goto err;
|
||||
}
|
||||
/* No client certificate so digest cached records */
|
||||
if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
EVP_PKEY *pkey;
|
||||
i = ssl_verify_cert_chain(s, sk);
|
||||
if (i <= 0) {
|
||||
SSLfatal(s, ssl_x509err2alert(s->verify_result),
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_CERTIFICATE_VERIFY_FAILED);
|
||||
goto err;
|
||||
}
|
||||
if (i > 1) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
|
||||
goto err;
|
||||
}
|
||||
pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
|
||||
if (pkey == NULL) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
SSL_R_UNKNOWN_CERTIFICATE_TYPE);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Sessions must be immutable once they go into the session cache. Otherwise
|
||||
* we can get multi-thread problems. Therefore we don't "update" sessions,
|
||||
* we replace them with a duplicate. Here, we need to do this every time
|
||||
* a new certificate is received via post-handshake authentication, as the
|
||||
* session may have already gone into the session cache.
|
||||
*/
|
||||
|
||||
if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
|
||||
if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
SSL_SESSION_free(s->session);
|
||||
s->session = new_sess;
|
||||
}
|
||||
|
||||
X509_free(s->session->peer);
|
||||
s->session->peer = sk_X509_shift(sk);
|
||||
s->session->verify_result = s->verify_result;
|
||||
|
||||
sk_X509_pop_free(s->session->peer_chain, X509_free);
|
||||
s->session->peer_chain = sk;
|
||||
|
||||
/*
|
||||
* Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
|
||||
* message
|
||||
*/
|
||||
if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Inconsistency alert: cert_chain does *not* include the peer's own
|
||||
* certificate, while we do include it in statem_clnt.c
|
||||
*/
|
||||
sk = NULL;
|
||||
|
||||
/* Save the current hash state for when we receive the CertificateVerify */
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
if (!ssl_handshake_hash(s, s->cert_verify_hash,
|
||||
sizeof(s->cert_verify_hash),
|
||||
&s->cert_verify_hash_len)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Resend session tickets */
|
||||
s->sent_tickets = 0;
|
||||
}
|
||||
|
||||
ret = MSG_PROCESS_CONTINUE_READING;
|
||||
|
||||
err:
|
||||
X509_free(x);
|
||||
sk_X509_pop_free(sk, X509_free);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
CERT_PKEY *cpk = s->s3.tmp.cert;
|
||||
|
||||
if (cpk == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* In TLSv1.3 the certificate chain is always preceded by a 0 length context
|
||||
* for the server Certificate message
|
||||
*/
|
||||
if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
if (!ssl3_output_cert_chain(s, pkt, cpk)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int create_ticket_prequel(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
unsigned char *tick_nonce)
|
||||
{
|
||||
/*
|
||||
* Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
|
||||
* unspecified for resumed session (for simplicity).
|
||||
* In TLSv1.3 we reset the "time" field above, and always specify the
|
||||
* timeout.
|
||||
*/
|
||||
if (!WPACKET_put_bytes_u32(pkt,
|
||||
(s->hit && !SSL_IS_TLS13(s))
|
||||
? 0 : s->session->timeout)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
if (!WPACKET_put_bytes_u32(pkt, age_add)
|
||||
|| !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Start the sub-packet for the actual ticket data */
|
||||
if (!WPACKET_start_sub_packet_u16(pkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
unsigned char *tick_nonce)
|
||||
{
|
||||
unsigned char *senc = NULL;
|
||||
EVP_CIPHER_CTX *ctx = NULL;
|
||||
HMAC_CTX *hctx = NULL;
|
||||
unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
|
||||
const unsigned char *const_p;
|
||||
int len, slen_full, slen, lenfinal;
|
||||
SSL_SESSION *sess;
|
||||
unsigned int hlen;
|
||||
SSL_CTX *tctx = s->session_ctx;
|
||||
unsigned char iv[EVP_MAX_IV_LENGTH];
|
||||
unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
|
||||
int iv_len, ok = 0;
|
||||
size_t macoffset, macendoffset;
|
||||
|
||||
/* get session encoding length */
|
||||
slen_full = i2d_SSL_SESSION(s->session, NULL);
|
||||
/*
|
||||
* Some length values are 16 bits, so forget it if session is too
|
||||
* long
|
||||
*/
|
||||
if (slen_full == 0 || slen_full > 0xFF00) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
senc = OPENSSL_malloc(slen_full);
|
||||
if (senc == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ctx = EVP_CIPHER_CTX_new();
|
||||
hctx = HMAC_CTX_new();
|
||||
if (ctx == NULL || hctx == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
p = senc;
|
||||
if (!i2d_SSL_SESSION(s->session, &p)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* create a fresh copy (not shared with other threads) to clean up
|
||||
*/
|
||||
const_p = senc;
|
||||
sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
|
||||
if (sess == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
slen = i2d_SSL_SESSION(sess, NULL);
|
||||
if (slen == 0 || slen > slen_full) {
|
||||
/* shouldn't ever happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
SSL_SESSION_free(sess);
|
||||
goto err;
|
||||
}
|
||||
p = senc;
|
||||
if (!i2d_SSL_SESSION(sess, &p)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
SSL_SESSION_free(sess);
|
||||
goto err;
|
||||
}
|
||||
SSL_SESSION_free(sess);
|
||||
|
||||
/*
|
||||
* Initialize HMAC and cipher contexts. If callback present it does
|
||||
* all the work otherwise use generated values from parent ctx.
|
||||
*/
|
||||
if (tctx->ext.ticket_key_cb) {
|
||||
/* if 0 is returned, write an empty ticket */
|
||||
int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
|
||||
hctx, 1);
|
||||
|
||||
if (ret == 0) {
|
||||
|
||||
/* Put timeout and length */
|
||||
if (!WPACKET_put_bytes_u32(pkt, 0)
|
||||
|| !WPACKET_put_bytes_u16(pkt, 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
OPENSSL_free(senc);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
HMAC_CTX_free(hctx);
|
||||
return 1;
|
||||
}
|
||||
if (ret < 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
SSL_R_CALLBACK_FAILED);
|
||||
goto err;
|
||||
}
|
||||
iv_len = EVP_CIPHER_CTX_iv_length(ctx);
|
||||
} else {
|
||||
const EVP_CIPHER *cipher = EVP_aes_256_cbc();
|
||||
|
||||
iv_len = EVP_CIPHER_iv_length(cipher);
|
||||
if (RAND_bytes(iv, iv_len) <= 0
|
||||
|| !EVP_EncryptInit_ex(ctx, cipher, NULL,
|
||||
tctx->ext.secure->tick_aes_key, iv)
|
||||
|| !HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
|
||||
sizeof(tctx->ext.secure->tick_hmac_key),
|
||||
EVP_sha256(), NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
memcpy(key_name, tctx->ext.tick_key_name,
|
||||
sizeof(tctx->ext.tick_key_name));
|
||||
}
|
||||
|
||||
if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!WPACKET_get_total_written(pkt, &macoffset)
|
||||
/* Output key name */
|
||||
|| !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
|
||||
/* output IV */
|
||||
|| !WPACKET_memcpy(pkt, iv, iv_len)
|
||||
|| !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
|
||||
&encdata1)
|
||||
/* Encrypt session data */
|
||||
|| !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
|
||||
|| !WPACKET_allocate_bytes(pkt, len, &encdata2)
|
||||
|| encdata1 != encdata2
|
||||
|| !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
|
||||
|| !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
|
||||
|| encdata1 + len != encdata2
|
||||
|| len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
|
||||
|| !WPACKET_get_total_written(pkt, &macendoffset)
|
||||
|| !HMAC_Update(hctx,
|
||||
(unsigned char *)s->init_buf->data + macoffset,
|
||||
macendoffset - macoffset)
|
||||
|| !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
|
||||
|| !HMAC_Final(hctx, macdata1, &hlen)
|
||||
|| hlen > EVP_MAX_MD_SIZE
|
||||
|| !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
|
||||
|| macdata1 != macdata2) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Close the sub-packet created by create_ticket_prequel() */
|
||||
if (!WPACKET_close(pkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ok = 1;
|
||||
err:
|
||||
OPENSSL_free(senc);
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
HMAC_CTX_free(hctx);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int construct_stateful_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
|
||||
unsigned char *tick_nonce)
|
||||
{
|
||||
if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!WPACKET_memcpy(pkt, s->session->session_id,
|
||||
s->session->session_id_length)
|
||||
|| !WPACKET_close(pkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATEFUL_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
SSL_CTX *tctx = s->session_ctx;
|
||||
unsigned char tick_nonce[TICKET_NONCE_SIZE];
|
||||
union {
|
||||
unsigned char age_add_c[sizeof(uint32_t)];
|
||||
uint32_t age_add;
|
||||
} age_add_u;
|
||||
|
||||
age_add_u.age_add = 0;
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
size_t i, hashlen;
|
||||
uint64_t nonce;
|
||||
static const unsigned char nonce_label[] = "resumption";
|
||||
const EVP_MD *md = ssl_handshake_md(s);
|
||||
int hashleni = EVP_MD_size(md);
|
||||
|
||||
/* Ensure cast to size_t is safe */
|
||||
if (!ossl_assert(hashleni >= 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
hashlen = (size_t)hashleni;
|
||||
|
||||
/*
|
||||
* If we already sent one NewSessionTicket, or we resumed then
|
||||
* s->session may already be in a cache and so we must not modify it.
|
||||
* Instead we need to take a copy of it and modify that.
|
||||
*/
|
||||
if (s->sent_tickets != 0 || s->hit) {
|
||||
SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
|
||||
|
||||
if (new_sess == NULL) {
|
||||
/* SSLfatal already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
SSL_SESSION_free(s->session);
|
||||
s->session = new_sess;
|
||||
}
|
||||
|
||||
if (!ssl_generate_session_id(s, s->session)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
s->session->ext.tick_age_add = age_add_u.age_add;
|
||||
|
||||
nonce = s->next_ticket_nonce;
|
||||
for (i = TICKET_NONCE_SIZE; i > 0; i--) {
|
||||
tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
|
||||
nonce >>= 8;
|
||||
}
|
||||
|
||||
if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
|
||||
nonce_label,
|
||||
sizeof(nonce_label) - 1,
|
||||
tick_nonce,
|
||||
TICKET_NONCE_SIZE,
|
||||
s->session->master_key,
|
||||
hashlen, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
s->session->master_key_length = hashlen;
|
||||
|
||||
s->session->time = (long)time(NULL);
|
||||
if (s->s3.alpn_selected != NULL) {
|
||||
OPENSSL_free(s->session->ext.alpn_selected);
|
||||
s->session->ext.alpn_selected =
|
||||
OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
|
||||
if (s->session->ext.alpn_selected == NULL) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
|
||||
}
|
||||
s->session->ext.max_early_data = s->max_early_data;
|
||||
}
|
||||
|
||||
if (tctx->generate_ticket_cb != NULL &&
|
||||
tctx->generate_ticket_cb(s, tctx->ticket_cb_data) == 0)
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* If we are using anti-replay protection then we behave as if
|
||||
* SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
|
||||
* is no point in using full stateless tickets.
|
||||
*/
|
||||
if (SSL_IS_TLS13(s)
|
||||
&& ((s->options & SSL_OP_NO_TICKET) != 0
|
||||
|| (s->max_early_data > 0
|
||||
&& (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
|
||||
if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
} else if (!construct_stateless_ticket(s, pkt, age_add_u.age_add,
|
||||
tick_nonce)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
if (!tls_construct_extensions(s, pkt,
|
||||
SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
|
||||
NULL, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
goto err;
|
||||
}
|
||||
/*
|
||||
* Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
|
||||
* gets reset to 0 if we send more tickets following a post-handshake
|
||||
* auth, but |next_ticket_nonce| does not.
|
||||
*/
|
||||
s->sent_tickets++;
|
||||
s->next_ticket_nonce++;
|
||||
ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
|
||||
}
|
||||
|
||||
return 1;
|
||||
err:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* In TLSv1.3 this is called from the extensions code, otherwise it is used to
|
||||
* create a separate message. Returns 1 on success or 0 on failure.
|
||||
*/
|
||||
int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
|
||||
|| !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
|
||||
s->ext.ocsp.resp_len)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls_construct_cert_status(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
if (!tls_construct_cert_status_body(s, pkt)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_NEXTPROTONEG
|
||||
/*
|
||||
* tls_process_next_proto reads a Next Protocol Negotiation handshake message.
|
||||
* It sets the next_proto member in s if found
|
||||
*/
|
||||
MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
|
||||
{
|
||||
PACKET next_proto, padding;
|
||||
size_t next_proto_len;
|
||||
|
||||
/*-
|
||||
* The payload looks like:
|
||||
* uint8 proto_len;
|
||||
* uint8 proto[proto_len];
|
||||
* uint8 padding_len;
|
||||
* uint8 padding[padding_len];
|
||||
*/
|
||||
if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
|
||||
|| !PACKET_get_length_prefixed_1(pkt, &padding)
|
||||
|| PACKET_remaining(pkt) > 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
|
||||
s->ext.npn_len = 0;
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
s->ext.npn_len = (unsigned char)next_proto_len;
|
||||
|
||||
return MSG_PROCESS_CONTINUE_READING;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
|
||||
{
|
||||
if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
|
||||
NULL, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
|
||||
{
|
||||
if (PACKET_remaining(pkt) != 0) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
|
||||
SSL_R_LENGTH_MISMATCH);
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
if (s->early_data_state != SSL_EARLY_DATA_READING
|
||||
&& s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
/*
|
||||
* EndOfEarlyData signals a key change so the end of the message must be on
|
||||
* a record boundary.
|
||||
*/
|
||||
if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
|
||||
SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
|
||||
SSL_R_NOT_ON_RECORD_BOUNDARY);
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
|
||||
if (!s->method->ssl3_enc->change_cipher_state(s,
|
||||
SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
|
||||
/* SSLfatal() already called */
|
||||
return MSG_PROCESS_ERROR;
|
||||
}
|
||||
|
||||
return MSG_PROCESS_CONTINUE_READING;
|
||||
}
|
||||
+1
-1
@@ -55,7 +55,7 @@ static int tls1_PRF(SSL *s,
|
||||
goto err;
|
||||
mdname = EVP_MD_name(md);
|
||||
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
|
||||
(char *)mdname, strlen(mdname) + 1);
|
||||
(char *)mdname, 0);
|
||||
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
|
||||
(unsigned char *)sec,
|
||||
(size_t)slen);
|
||||
|
||||
+160
-16
@@ -7,11 +7,15 @@
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
/* We need access to the deprecated low level HMAC APIs */
|
||||
#define OPENSSL_SUPPRESS_DEPRECATED
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/ocsp.h>
|
||||
#include <openssl/conf.h>
|
||||
#include <openssl/x509v3.h>
|
||||
@@ -22,6 +26,7 @@
|
||||
#include <openssl/ct.h>
|
||||
|
||||
static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey);
|
||||
static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu);
|
||||
|
||||
SSL3_ENC_METHOD const TLSv1_enc_data = {
|
||||
tls1_enc,
|
||||
@@ -928,8 +933,11 @@ static int rsa_pss_check_min_key_size(const RSA *rsa, const SIGALG_LOOKUP *lu)
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a signature algorithm for TLS < 1.2 where the signature type
|
||||
* is fixed by the certificate type.
|
||||
* Returns a signature algorithm when the peer did not send a list of supported
|
||||
* signature algorithms. The signature algorithm is fixed for the certificate
|
||||
* type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the
|
||||
* certificate type from |s| will be used.
|
||||
* Returns the signature algorithm to use, or NULL on error.
|
||||
*/
|
||||
static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
|
||||
{
|
||||
@@ -972,8 +980,12 @@ static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
|
||||
|
||||
if (!tls1_lookup_md(lu, NULL))
|
||||
return NULL;
|
||||
if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
|
||||
return NULL;
|
||||
return lu;
|
||||
}
|
||||
if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg))
|
||||
return NULL;
|
||||
return &legacy_rsa_sigalg;
|
||||
}
|
||||
/* Set peer sigalg based key type */
|
||||
@@ -1255,7 +1267,7 @@ int ssl_set_client_disabled(SSL *s)
|
||||
*
|
||||
* Returns 1 when it's disabled, 0 when enabled.
|
||||
*/
|
||||
int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
|
||||
int ssl_cipher_disabled(const SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
|
||||
{
|
||||
if (c->algorithm_mkey & s->s3.tmp.mask_k
|
||||
|| c->algorithm_auth & s->s3.tmp.mask_a)
|
||||
@@ -1409,7 +1421,7 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER;
|
||||
size_t mlen;
|
||||
unsigned char tick_hmac[EVP_MAX_MD_SIZE];
|
||||
HMAC_CTX *hctx = NULL;
|
||||
SSL_HMAC *hctx = NULL;
|
||||
EVP_CIPHER_CTX *ctx = NULL;
|
||||
SSL_CTX *tctx = s->session_ctx;
|
||||
|
||||
@@ -1439,7 +1451,7 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
}
|
||||
|
||||
/* Initialize session ticket encryption and HMAC contexts */
|
||||
hctx = HMAC_CTX_new();
|
||||
hctx = ssl_hmac_new(tctx);
|
||||
if (hctx == NULL) {
|
||||
ret = SSL_TICKET_FATAL_ERR_MALLOC;
|
||||
goto end;
|
||||
@@ -1449,11 +1461,28 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
ret = SSL_TICKET_FATAL_ERR_MALLOC;
|
||||
goto end;
|
||||
}
|
||||
if (tctx->ext.ticket_key_cb) {
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
|
||||
#else
|
||||
if (tctx->ext.ticket_key_evp_cb != NULL)
|
||||
#endif
|
||||
{
|
||||
unsigned char *nctick = (unsigned char *)etick;
|
||||
int rv = tctx->ext.ticket_key_cb(s, nctick,
|
||||
int rv = 0;
|
||||
|
||||
if (tctx->ext.ticket_key_evp_cb != NULL)
|
||||
rv = tctx->ext.ticket_key_evp_cb(s, nctick,
|
||||
nctick + TLSEXT_KEYNAME_LENGTH,
|
||||
ctx,
|
||||
ssl_hmac_get0_EVP_MAC_CTX(hctx),
|
||||
0);
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
else if (tctx->ext.ticket_key_cb != NULL)
|
||||
/* if 0 is returned, write an empty ticket */
|
||||
rv = tctx->ext.ticket_key_cb(s, nctick,
|
||||
nctick + TLSEXT_KEYNAME_LENGTH,
|
||||
ctx, hctx, 0);
|
||||
ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0);
|
||||
#endif
|
||||
if (rv < 0) {
|
||||
ret = SSL_TICKET_FATAL_ERR_OTHER;
|
||||
goto end;
|
||||
@@ -1471,9 +1500,9 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
ret = SSL_TICKET_NO_DECRYPT;
|
||||
goto end;
|
||||
}
|
||||
if (HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
|
||||
sizeof(tctx->ext.secure->tick_hmac_key),
|
||||
EVP_sha256(), NULL) <= 0
|
||||
if (ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
|
||||
sizeof(tctx->ext.secure->tick_hmac_key),
|
||||
"SHA256") <= 0
|
||||
|| EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
|
||||
tctx->ext.secure->tick_aes_key,
|
||||
etick + TLSEXT_KEYNAME_LENGTH) <= 0) {
|
||||
@@ -1487,7 +1516,7 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
* Attempt to process session ticket, first conduct sanity and integrity
|
||||
* checks on ticket.
|
||||
*/
|
||||
mlen = HMAC_size(hctx);
|
||||
mlen = ssl_hmac_size(hctx);
|
||||
if (mlen == 0) {
|
||||
ret = SSL_TICKET_FATAL_ERR_OTHER;
|
||||
goto end;
|
||||
@@ -1501,8 +1530,8 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
}
|
||||
eticklen -= mlen;
|
||||
/* Check HMAC of encrypted ticket */
|
||||
if (HMAC_Update(hctx, etick, eticklen) <= 0
|
||||
|| HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
|
||||
if (ssl_hmac_update(hctx, etick, eticklen) <= 0
|
||||
|| ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) {
|
||||
ret = SSL_TICKET_FATAL_ERR_OTHER;
|
||||
goto end;
|
||||
}
|
||||
@@ -1565,7 +1594,7 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
|
||||
end:
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
HMAC_CTX_free(hctx);
|
||||
ssl_hmac_free(hctx);
|
||||
|
||||
/*
|
||||
* If set, the decrypt_ticket_cb() is called unless a fatal error was
|
||||
@@ -1635,7 +1664,7 @@ SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
|
||||
}
|
||||
|
||||
/* Check to see if a signature algorithm is allowed */
|
||||
static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu)
|
||||
static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu)
|
||||
{
|
||||
unsigned char sigalgstr[2];
|
||||
int secbits;
|
||||
@@ -2971,3 +3000,118 @@ uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
|
||||
{
|
||||
return session->ext.max_fragment_len_mode;
|
||||
}
|
||||
|
||||
/*
|
||||
* Helper functions for HMAC access with legacy support included.
|
||||
*/
|
||||
SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx)
|
||||
{
|
||||
SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret));
|
||||
EVP_MAC *mac = NULL;
|
||||
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
if (ctx->ext.ticket_key_evp_cb == NULL
|
||||
&& ctx->ext.ticket_key_cb != NULL) {
|
||||
ret->old_ctx = HMAC_CTX_new();
|
||||
if (ret->old_ctx == NULL)
|
||||
goto err;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
mac = EVP_MAC_fetch(ctx->libctx, "HMAC", NULL);
|
||||
if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL)
|
||||
goto err;
|
||||
EVP_MAC_free(mac);
|
||||
return ret;
|
||||
err:
|
||||
EVP_MAC_CTX_free(ret->ctx);
|
||||
EVP_MAC_free(mac);
|
||||
OPENSSL_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ssl_hmac_free(SSL_HMAC *ctx)
|
||||
{
|
||||
if (ctx != NULL) {
|
||||
EVP_MAC_CTX_free(ctx->ctx);
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
HMAC_CTX_free(ctx->old_ctx);
|
||||
#endif
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
HMAC_CTX *ssl_hmac_get0_HMAC_CTX(SSL_HMAC *ctx)
|
||||
{
|
||||
return ctx->old_ctx;
|
||||
}
|
||||
#endif
|
||||
|
||||
EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx)
|
||||
{
|
||||
return ctx->ctx;
|
||||
}
|
||||
|
||||
int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md)
|
||||
{
|
||||
OSSL_PARAM params[3], *p = params;
|
||||
|
||||
if (ctx->ctx != NULL) {
|
||||
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0);
|
||||
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, len);
|
||||
*p = OSSL_PARAM_construct_end();
|
||||
if (EVP_MAC_CTX_set_params(ctx->ctx, params) && EVP_MAC_init(ctx->ctx))
|
||||
return 1;
|
||||
}
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
if (ctx->old_ctx != NULL)
|
||||
return HMAC_Init_ex(ctx->old_ctx, key, len,
|
||||
EVP_get_digestbyname(md), NULL);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len)
|
||||
{
|
||||
if (ctx->ctx != NULL)
|
||||
return EVP_MAC_update(ctx->ctx, data, len);
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
if (ctx->old_ctx != NULL)
|
||||
return HMAC_Update(ctx->old_ctx, data, len);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len,
|
||||
size_t max_size)
|
||||
{
|
||||
if (ctx->ctx != NULL)
|
||||
return EVP_MAC_final(ctx->ctx, md, len, max_size);
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
if (ctx->old_ctx != NULL) {
|
||||
unsigned int l;
|
||||
|
||||
if (HMAC_Final(ctx->old_ctx, md, &l) > 0) {
|
||||
if (len != NULL)
|
||||
*len = l;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t ssl_hmac_size(const SSL_HMAC *ctx)
|
||||
{
|
||||
if (ctx->ctx != NULL)
|
||||
return EVP_MAC_size(ctx->ctx);
|
||||
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
||||
if (ctx->old_ctx != NULL)
|
||||
return HMAC_size(ctx->old_ctx);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
+2
-2
@@ -97,7 +97,7 @@ int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
|
||||
|
||||
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
|
||||
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
|
||||
(char *)mdname, strlen(mdname) + 1);
|
||||
(char *)mdname, 0);
|
||||
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
|
||||
(unsigned char *)secret, hashlen);
|
||||
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
|
||||
@@ -252,7 +252,7 @@ int tls13_generate_secret(SSL *s, const EVP_MD *md,
|
||||
|
||||
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
|
||||
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
|
||||
(char *)mdname, strlen(mdname) + 1);
|
||||
(char *)mdname, 0);
|
||||
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
|
||||
(unsigned char *)insecret,
|
||||
insecretlen);
|
||||
|
||||
Reference in New Issue
Block a user