#include "ssltestlib.h"
#include "testutil.h"
+#include "testutil/output.h"
#include "internal/nelem.h"
#include "../ssl/ssl_locl.h"
goto end;
if (use_ext_cache) {
- if (!TEST_int_eq(new_called, 0)
- || !TEST_int_eq(remove_called, 0))
+ if (!TEST_int_eq(remove_called, 0))
goto end;
if (maxprot == TLS1_3_VERSION) {
- if (!TEST_int_eq(get_called, 0))
+ /*
+ * Every time we issue a NewSessionTicket we are creating a new
+ * session for next time in TLSv1.3
+ */
+ if (!TEST_int_eq(new_called, 1)
+ || !TEST_int_eq(get_called, 0))
goto end;
} else {
- if (!TEST_int_eq(get_called, 1))
+ if (!TEST_int_eq(new_called, 0)
+ || !TEST_int_eq(get_called, 1))
goto end;
}
}
static int use_session_cb_cnt = 0;
static int find_session_cb_cnt = 0;
+static int psk_client_cb_cnt = 0;
+static int psk_server_cb_cnt = 0;
static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
size_t *idlen, SSL_SESSION **sess)
return 1;
}
+static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
+ unsigned int max_id_len,
+ unsigned char *psk,
+ unsigned int max_psk_len)
+{
+ unsigned int psklen = 0;
+
+ psk_client_cb_cnt++;
+
+ if (strlen(pskid) + 1 > max_id_len)
+ return 0;
+
+ /* We should only ever be called a maximum of twice per connection */
+ if (psk_client_cb_cnt > 2)
+ return 0;
+
+ if (clientpsk == NULL)
+ return 0;
+
+ /* We'll reuse the PSK we set up for TLSv1.3 */
+ if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
+ return 0;
+ psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
+ strncpy(id, pskid, max_id_len);
+
+ return psklen;
+}
+
static int find_session_cb(SSL *ssl, const unsigned char *identity,
size_t identity_len, SSL_SESSION **sess)
{
return 1;
}
+static unsigned int psk_server_cb(SSL *ssl, const char *identity,
+ unsigned char *psk, unsigned int max_psk_len)
+{
+ unsigned int psklen = 0;
+
+ psk_server_cb_cnt++;
+
+ /* We should only ever be called a maximum of twice per connection */
+ if (find_session_cb_cnt > 2)
+ return 0;
+
+ if (serverpsk == NULL)
+ return 0;
+
+ /* Identity should match that set by the client */
+ if (strcmp(srvid, identity) != 0) {
+ return 0;
+ }
+
+ /* We'll reuse the PSK we set up for TLSv1.3 */
+ if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
+ return 0;
+ psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
+
+ return psklen;
+}
+
#define MSG1 "Hello"
#define MSG2 "World."
#define MSG3 "This"
#define MSG7 "message."
#define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
+#define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
/*
* Helper method to setup objects for early data test. Caller frees objects on
{
if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
TLS_client_method(), sctx,
- cctx, cert, privkey)))
+ cctx, cert, privkey))
+ || !TEST_true(SSL_CTX_set_max_early_data(*sctx,
+ SSL3_RT_MAX_PLAIN_LENGTH))
+ || !TEST_true(SSL_CTX_set_max_early_data(*cctx,
+ SSL3_RT_MAX_PLAIN_LENGTH)))
return 0;
if (idx == 1) {
if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
TLS_client_method(), &sctx,
&cctx, cert, privkey))
- || !TEST_true(SSL_CTX_set_cipher_list(cctx,
- "TLS13-AES-128-GCM-SHA256"))
+ || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
+ "TLS_AES_128_GCM_SHA256"))
|| !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
&clientssl, NULL, NULL))
|| !TEST_true(create_ssl_connection(serverssl, clientssl,
# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
/* Check we can resume a session with a different SHA-256 ciphersuite */
- if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
- "TLS13-CHACHA20-POLY1305-SHA256"))
+ if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
+ "TLS_CHACHA20_POLY1305_SHA256"))
|| !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
NULL, NULL))
|| !TEST_true(SSL_set_session(clientssl, clntsess))
* Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
* succeeds but does not resume.
*/
- if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "TLS13-AES-256-GCM-SHA384"))
+ if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
|| !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
NULL, NULL))
|| !TEST_true(SSL_set_session(clientssl, clntsess))
serverssl = clientssl = NULL;
/* Create a session based on SHA384 */
- if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "TLS13-AES-256-GCM-SHA384"))
+ if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
|| !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
&clientssl, NULL, NULL))
|| !TEST_true(create_ssl_connection(serverssl, clientssl,
SSL_free(clientssl);
serverssl = clientssl = NULL;
- if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
- "TLS13-AES-128-GCM-SHA256:TLS13-AES-256-GCM-SHA384"))
- || !TEST_true(SSL_CTX_set_cipher_list(sctx,
- "TLS13-AES-256-GCM-SHA384"))
+ if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
+ "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
+ || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
+ "TLS_AES_256_GCM_SHA384"))
|| !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
NULL, NULL))
|| !TEST_true(SSL_set_session(clientssl, clntsess))
return testresult;
}
-static int test_tls13_psk(void)
+static int test_tls13_psk(int idx)
{
SSL_CTX *sctx = NULL, *cctx = NULL;
SSL *serverssl = NULL, *clientssl = NULL;
&cctx, cert, privkey)))
goto end;
- SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
- SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
+ /*
+ * We use a ciphersuite with SHA256 to ease testing old style PSK callbacks
+ * which will always default to SHA256
+ */
+ if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
+ goto end;
+
+ /*
+ * Test 0: New style callbacks only
+ * Test 1: New and old style callbacks (only the new ones should be used)
+ * Test 2: Old style callbacks only
+ */
+ if (idx == 0 || idx == 1) {
+ SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
+ SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
+ }
+ if (idx == 1 || idx == 2) {
+ SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
+ SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
+ }
srvid = pskid;
use_session_cb_cnt = 0;
find_session_cb_cnt = 0;
+ psk_client_cb_cnt = 0;
+ psk_server_cb_cnt = 0;
/* Check we can create a connection if callback decides not to send a PSK */
if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
|| !TEST_true(create_ssl_connection(serverssl, clientssl,
SSL_ERROR_NONE))
|| !TEST_false(SSL_session_reused(clientssl))
- || !TEST_false(SSL_session_reused(serverssl))
- || !TEST_true(use_session_cb_cnt == 1)
- || !TEST_true(find_session_cb_cnt == 0))
+ || !TEST_false(SSL_session_reused(serverssl)))
goto end;
+ if (idx == 0 || idx == 1) {
+ if (!TEST_true(use_session_cb_cnt == 1)
+ || !TEST_true(find_session_cb_cnt == 0)
+ /*
+ * If no old style callback then below should be 0
+ * otherwise 1
+ */
+ || !TEST_true(psk_client_cb_cnt == idx)
+ || !TEST_true(psk_server_cb_cnt == 0))
+ goto end;
+ } else {
+ if (!TEST_true(use_session_cb_cnt == 0)
+ || !TEST_true(find_session_cb_cnt == 0)
+ || !TEST_true(psk_client_cb_cnt == 1)
+ || !TEST_true(psk_server_cb_cnt == 0))
+ goto end;
+ }
+
shutdown_ssl_connection(serverssl, clientssl);
serverssl = clientssl = NULL;
- use_session_cb_cnt = 0;
+ use_session_cb_cnt = psk_client_cb_cnt = 0;
if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
NULL, NULL)))
goto end;
/* Create the PSK */
- cipher = SSL_CIPHER_find(clientssl, TLS13_AES_256_GCM_SHA384_BYTES);
+ cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
clientpsk = SSL_SESSION_new();
if (!TEST_ptr(clientpsk)
|| !TEST_ptr(cipher)
/* Check we can create a connection and the PSK is used */
if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
|| !TEST_true(SSL_session_reused(clientssl))
- || !TEST_true(SSL_session_reused(serverssl))
- || !TEST_true(use_session_cb_cnt == 1)
- || !TEST_true(find_session_cb_cnt == 1))
+ || !TEST_true(SSL_session_reused(serverssl)))
goto end;
+ if (idx == 0 || idx == 1) {
+ if (!TEST_true(use_session_cb_cnt == 1)
+ || !TEST_true(find_session_cb_cnt == 1)
+ || !TEST_true(psk_client_cb_cnt == 0)
+ || !TEST_true(psk_server_cb_cnt == 0))
+ goto end;
+ } else {
+ if (!TEST_true(use_session_cb_cnt == 0)
+ || !TEST_true(find_session_cb_cnt == 0)
+ || !TEST_true(psk_client_cb_cnt == 1)
+ || !TEST_true(psk_server_cb_cnt == 1))
+ goto end;
+ }
+
shutdown_ssl_connection(serverssl, clientssl);
serverssl = clientssl = NULL;
use_session_cb_cnt = find_session_cb_cnt = 0;
+ psk_client_cb_cnt = psk_server_cb_cnt = 0;
if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
NULL, NULL)))
*/
if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
|| !TEST_true(SSL_session_reused(clientssl))
- || !TEST_true(SSL_session_reused(serverssl))
- || !TEST_true(use_session_cb_cnt == 2)
- || !TEST_true(find_session_cb_cnt == 2))
+ || !TEST_true(SSL_session_reused(serverssl)))
goto end;
+ if (idx == 0 || idx == 1) {
+ if (!TEST_true(use_session_cb_cnt == 2)
+ || !TEST_true(find_session_cb_cnt == 2)
+ || !TEST_true(psk_client_cb_cnt == 0)
+ || !TEST_true(psk_server_cb_cnt == 0))
+ goto end;
+ } else {
+ if (!TEST_true(use_session_cb_cnt == 0)
+ || !TEST_true(find_session_cb_cnt == 0)
+ || !TEST_true(psk_client_cb_cnt == 2)
+ || !TEST_true(psk_server_cb_cnt == 2))
+ goto end;
+ }
+
shutdown_ssl_connection(serverssl, clientssl);
serverssl = clientssl = NULL;
use_session_cb_cnt = find_session_cb_cnt = 0;
+ psk_client_cb_cnt = psk_server_cb_cnt = 0;
/*
* Check that if the server rejects the PSK we can still connect, but with
|| !TEST_true(create_ssl_connection(serverssl, clientssl,
SSL_ERROR_NONE))
|| !TEST_false(SSL_session_reused(clientssl))
- || !TEST_false(SSL_session_reused(serverssl))
- || !TEST_true(use_session_cb_cnt == 1)
- || !TEST_true(find_session_cb_cnt == 1))
+ || !TEST_false(SSL_session_reused(serverssl)))
goto end;
+ if (idx == 0 || idx == 1) {
+ if (!TEST_true(use_session_cb_cnt == 1)
+ || !TEST_true(find_session_cb_cnt == 1)
+ || !TEST_true(psk_client_cb_cnt == 0)
+ /*
+ * If no old style callback then below should be 0
+ * otherwise 1
+ */
+ || !TEST_true(psk_server_cb_cnt == idx))
+ goto end;
+ } else {
+ if (!TEST_true(use_session_cb_cnt == 0)
+ || !TEST_true(find_session_cb_cnt == 0)
+ || !TEST_true(psk_client_cb_cnt == 1)
+ || !TEST_true(psk_server_cb_cnt == 1))
+ goto end;
+ }
+
shutdown_ssl_connection(serverssl, clientssl);
serverssl = clientssl = NULL;
testresult = 1;
return 0;
}
+static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
+ size_t *cookie_len)
+{
+ unsigned int temp;
+ int res = generate_cookie_callback(ssl, cookie, &temp);
+ *cookie_len = temp;
+ return res;
+}
+
+static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
+ size_t cookie_len)
+{
+ return verify_cookie_callback(ssl, cookie, cookie_len);
+}
+
static int test_stateless(void)
{
SSL_CTX *sctx = NULL, *cctx = NULL;
&cctx, cert, privkey)))
goto end;
- /* Set up the cookie generation and verification callbacks */
- SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_callback);
- SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_callback);
-
/* The arrival of CCS messages can confuse the test */
SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
+ if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
+ NULL, NULL))
+ /* Send the first ClientHello */
+ || !TEST_false(create_ssl_connection(serverssl, clientssl,
+ SSL_ERROR_WANT_READ))
+ /*
+ * This should fail with a -1 return because we have no callbacks
+ * set up
+ */
+ || !TEST_int_eq(SSL_stateless(serverssl), -1))
+ goto end;
+
+ /* Fatal error so abandon the connection from this client */
+ SSL_free(clientssl);
+ clientssl = NULL;
+
+ /* Set up the cookie generation and verification callbacks */
+ SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
+ SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
+
+ /*
+ * Create a new connection from the client (we can reuse the server SSL
+ * object).
+ */
if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
NULL, NULL))
/* Send the first ClientHello */
|| !TEST_false(create_ssl_connection(serverssl, clientssl,
SSL_ERROR_WANT_READ))
/* This should fail because there is no cookie */
- || !TEST_false(SSL_stateless(serverssl)))
+ || !TEST_int_eq(SSL_stateless(serverssl), 0))
goto end;
/* Abandon the connection from this client */
|| !TEST_false(create_ssl_connection(serverssl, clientssl,
SSL_ERROR_WANT_READ))
/* This should fail because there is no cookie */
- || !TEST_false(SSL_stateless(serverssl))
+ || !TEST_int_eq(SSL_stateless(serverssl), 0)
/* Send the second ClientHello */
|| !TEST_false(create_ssl_connection(serverssl, clientssl,
SSL_ERROR_WANT_READ))
/* This should succeed because a cookie is now present */
- || !TEST_true(SSL_stateless(serverssl))
+ || !TEST_int_eq(SSL_stateless(serverssl), 1)
/* Complete the connection */
|| !TEST_true(create_ssl_connection(serverssl, clientssl,
SSL_ERROR_NONE)))
|| !TEST_ptr(privkey = test_get_argument(1)))
return 0;
+ if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
+#ifdef OPENSSL_NO_CRYPTO_MDEBUG
+ TEST_error("not supported in this build");
+ return 0;
+#else
+ int i, mcount, rcount, fcount;
+
+ for (i = 0; i < 4; i++)
+ test_export_key_mat(i);
+ CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
+ test_printf_stdout("malloc %d realloc %d free %d\n",
+ mcount, rcount, fcount);
+ return 1;
+#endif
+ }
+
ADD_TEST(test_large_message_tls);
ADD_TEST(test_large_message_tls_read_ahead);
#ifndef OPENSSL_NO_DTLS
#endif
#ifndef OPENSSL_NO_TLS1_3
ADD_TEST(test_ciphersuite_change);
- ADD_TEST(test_tls13_psk);
+ ADD_ALL_TESTS(test_tls13_psk, 3);
ADD_ALL_TESTS(test_custom_exts, 5);
ADD_TEST(test_stateless);
ADD_TEST(test_pha_key_update);