2017-06-28 07:46:13 +00:00
|
|
|
/*
|
2018-09-11 12:22:14 +00:00
|
|
|
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2017-06-28 07:46:13 +00:00
|
|
|
* Copyright 2017 BaishanCloud. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (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 <string.h>
|
|
|
|
|
|
|
|
#include <openssl/opensslconf.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "../ssl/packet_locl.h"
|
|
|
|
|
|
|
|
#include "testutil.h"
|
2017-08-22 12:35:43 +00:00
|
|
|
#include "internal/nelem.h"
|
2018-09-07 14:17:34 +00:00
|
|
|
#include "ssltestlib.h"
|
2017-06-28 07:46:13 +00:00
|
|
|
|
|
|
|
#define CLIENT_VERSION_LEN 2
|
|
|
|
|
|
|
|
static const char *host = "dummy-host";
|
|
|
|
|
2018-09-07 14:17:34 +00:00
|
|
|
static char *cert = NULL;
|
|
|
|
static char *privkey = NULL;
|
|
|
|
|
2017-06-28 07:46:13 +00:00
|
|
|
static int get_sni_from_client_hello(BIO *bio, char **sni)
|
|
|
|
{
|
|
|
|
long len;
|
|
|
|
unsigned char *data;
|
|
|
|
PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0}, pkt4 = {0}, pkt5 = {0};
|
|
|
|
unsigned int servname_type = 0, type = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
len = BIO_get_mem_data(bio, (char **)&data);
|
|
|
|
if (!TEST_true(PACKET_buf_init(&pkt, data, len))
|
|
|
|
/* Skip the record header */
|
|
|
|
|| !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
|
|
|
|
/* Skip the handshake message header */
|
|
|
|
|| !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
|
|
|
|
/* Skip client version and random */
|
|
|
|
|| !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
|
|
|
|
+ SSL3_RANDOM_SIZE))
|
|
|
|
/* Skip session id */
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
|
|
|
|
/* Skip ciphers */
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
|
|
|
|
/* Skip compression */
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
|
|
|
|
/* Extensions len */
|
|
|
|
|| !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Loop through all extensions for SNI */
|
|
|
|
while (PACKET_remaining(&pkt2)) {
|
|
|
|
if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
|
|
|
|
goto end;
|
|
|
|
if (type == TLSEXT_TYPE_server_name) {
|
|
|
|
if (!TEST_true(PACKET_get_length_prefixed_2(&pkt3, &pkt4))
|
|
|
|
|| !TEST_uint_ne(PACKET_remaining(&pkt4), 0)
|
|
|
|
|| !TEST_true(PACKET_get_1(&pkt4, &servname_type))
|
|
|
|
|| !TEST_uint_eq(servname_type, TLSEXT_NAMETYPE_host_name)
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_2(&pkt4, &pkt5))
|
|
|
|
|| !TEST_uint_le(PACKET_remaining(&pkt5), TLSEXT_MAXLEN_host_name)
|
|
|
|
|| !TEST_false(PACKET_contains_zero_byte(&pkt5))
|
|
|
|
|| !TEST_true(PACKET_strndup(&pkt5, sni)))
|
|
|
|
goto end;
|
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:39:03 +00:00
|
|
|
static int client_setup_sni_before_state(void)
|
2017-06-28 07:46:13 +00:00
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
SSL *con = NULL;
|
|
|
|
BIO *rbio;
|
|
|
|
BIO *wbio;
|
|
|
|
char *hostname = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* use TLS_method to blur 'side' */
|
|
|
|
ctx = SSL_CTX_new(TLS_method());
|
|
|
|
if (!TEST_ptr(ctx))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
con = SSL_new(ctx);
|
|
|
|
if (!TEST_ptr(con))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* set SNI before 'client side' is set */
|
|
|
|
SSL_set_tlsext_host_name(con, host);
|
|
|
|
|
|
|
|
rbio = BIO_new(BIO_s_mem());
|
|
|
|
wbio = BIO_new(BIO_s_mem());
|
|
|
|
if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
|
|
|
|
BIO_free(rbio);
|
|
|
|
BIO_free(wbio);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_bio(con, rbio, wbio);
|
|
|
|
|
|
|
|
if (!TEST_int_le(SSL_connect(con), 0))
|
|
|
|
/* This shouldn't succeed because we don't have a server! */
|
|
|
|
goto end;
|
|
|
|
if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
|
|
|
|
/* no SNI in client hello */
|
|
|
|
goto end;
|
|
|
|
if (!TEST_str_eq(hostname, host))
|
|
|
|
/* incorrect SNI value */
|
|
|
|
goto end;
|
|
|
|
ret = 1;
|
|
|
|
end:
|
|
|
|
OPENSSL_free(hostname);
|
|
|
|
SSL_free(con);
|
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:39:03 +00:00
|
|
|
static int client_setup_sni_after_state(void)
|
2017-06-28 07:46:13 +00:00
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
SSL *con = NULL;
|
|
|
|
BIO *rbio;
|
|
|
|
BIO *wbio;
|
|
|
|
char *hostname = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* use TLS_method to blur 'side' */
|
|
|
|
ctx = SSL_CTX_new(TLS_method());
|
|
|
|
if (!TEST_ptr(ctx))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
con = SSL_new(ctx);
|
|
|
|
if (!TEST_ptr(con))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
rbio = BIO_new(BIO_s_mem());
|
|
|
|
wbio = BIO_new(BIO_s_mem());
|
|
|
|
if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
|
|
|
|
BIO_free(rbio);
|
|
|
|
BIO_free(wbio);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_bio(con, rbio, wbio);
|
|
|
|
SSL_set_connect_state(con);
|
|
|
|
|
|
|
|
/* set SNI after 'client side' is set */
|
|
|
|
SSL_set_tlsext_host_name(con, host);
|
|
|
|
|
|
|
|
if (!TEST_int_le(SSL_connect(con), 0))
|
|
|
|
/* This shouldn't succeed because we don't have a server! */
|
|
|
|
goto end;
|
|
|
|
if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
|
|
|
|
/* no SNI in client hello */
|
|
|
|
goto end;
|
|
|
|
if (!TEST_str_eq(hostname, host))
|
|
|
|
/* incorrect SNI value */
|
|
|
|
goto end;
|
|
|
|
ret = 1;
|
|
|
|
end:
|
|
|
|
OPENSSL_free(hostname);
|
|
|
|
SSL_free(con);
|
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:39:03 +00:00
|
|
|
static int server_setup_sni(void)
|
2017-06-28 07:46:13 +00:00
|
|
|
{
|
2018-09-07 14:17:34 +00:00
|
|
|
SSL_CTX *cctx = NULL, *sctx = NULL;
|
|
|
|
SSL *clientssl = NULL, *serverssl = NULL;
|
|
|
|
int testresult = 0;
|
2017-06-28 07:46:13 +00:00
|
|
|
|
2018-09-07 14:17:34 +00:00
|
|
|
if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
|
|
|
|
TLS_client_method(),
|
|
|
|
TLS1_VERSION, TLS_MAX_VERSION,
|
|
|
|
&sctx, &cctx, cert, privkey))
|
|
|
|
|| !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
|
|
|
|
NULL, NULL)))
|
2017-06-28 07:46:13 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* set SNI at server side */
|
2018-09-07 14:17:34 +00:00
|
|
|
SSL_set_tlsext_host_name(serverssl, host);
|
2017-06-28 07:46:13 +00:00
|
|
|
|
2018-09-07 14:17:34 +00:00
|
|
|
if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
|
2017-06-28 07:46:13 +00:00
|
|
|
goto end;
|
2018-09-07 14:17:34 +00:00
|
|
|
|
|
|
|
if (!TEST_ptr_null(SSL_get_servername(serverssl,
|
|
|
|
TLSEXT_NAMETYPE_host_name))) {
|
|
|
|
/* SNI should have been cleared during handshake */
|
2017-06-28 07:46:13 +00:00
|
|
|
goto end;
|
2018-09-07 14:17:34 +00:00
|
|
|
}
|
2019-01-31 18:31:35 +00:00
|
|
|
|
2018-09-07 14:17:34 +00:00
|
|
|
testresult = 1;
|
2017-06-28 07:46:13 +00:00
|
|
|
end:
|
2018-09-07 14:17:34 +00:00
|
|
|
SSL_free(serverssl);
|
|
|
|
SSL_free(clientssl);
|
|
|
|
SSL_CTX_free(sctx);
|
|
|
|
SSL_CTX_free(cctx);
|
|
|
|
|
|
|
|
return testresult;
|
2017-06-28 07:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*sni_test_fn)(void);
|
|
|
|
|
|
|
|
static sni_test_fn sni_test_fns[3] = {
|
|
|
|
client_setup_sni_before_state,
|
|
|
|
client_setup_sni_after_state,
|
|
|
|
server_setup_sni
|
|
|
|
};
|
|
|
|
|
|
|
|
static int test_servername(int test)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For each test set up an SSL_CTX and SSL and see
|
|
|
|
* what SNI behaves.
|
|
|
|
*/
|
|
|
|
return sni_test_fns[test]();
|
|
|
|
}
|
|
|
|
|
|
|
|
int setup_tests(void)
|
|
|
|
{
|
2018-09-07 14:17:34 +00:00
|
|
|
if (!TEST_ptr(cert = test_get_argument(0))
|
|
|
|
|| !TEST_ptr(privkey = test_get_argument(1)))
|
|
|
|
return 0;
|
|
|
|
|
2017-06-28 07:46:13 +00:00
|
|
|
ADD_ALL_TESTS(test_servername, OSSL_NELEM(sni_test_fns));
|
|
|
|
return 1;
|
|
|
|
}
|