diff options
author | Rich Salz <rsalz@openssl.org> | 2017-04-26 13:24:37 -0400 |
---|---|---|
committer | Rich Salz <rsalz@openssl.org> | 2017-04-26 13:42:57 -0400 |
commit | 710756a9b384c9e9eaaf42acaf429aebc2a822a1 (patch) | |
tree | e1d9df6f3a5ca6cec71b0d8ee67def696581707a /test/sslapitest.c | |
parent | 30bea14be6bbf77ed60acb9bd1befeb51d4c4b10 (diff) |
Convert sslapitest to test framework
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3252)
Diffstat (limited to 'test/sslapitest.c')
-rw-r--r-- | test/sslapitest.c | 1629 |
1 files changed, 558 insertions, 1071 deletions
diff --git a/test/sslapitest.c b/test/sslapitest.c index 436fbbb10f..7c0171b1db 100644 --- a/test/sslapitest.c +++ b/test/sslapitest.c @@ -24,9 +24,9 @@ static char *privkey = NULL; #define LOG_BUFFER_SIZE 1024 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0}; -static int server_log_buffer_index = 0; +static size_t server_log_buffer_index = 0; static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0}; -static int client_log_buffer_index = 0; +static size_t client_log_buffer_index = 0; static int error_writing_log = 0; #ifndef OPENSSL_NO_OCSP @@ -53,64 +53,54 @@ struct sslapitest_log_counts { unsigned int server_application_secret_count; }; -static void client_keylog_callback(const SSL *ssl, const char *line) { +static void client_keylog_callback(const SSL *ssl, const char *line) +{ int line_length = strlen(line); /* If the log doesn't fit, error out. */ - if ((client_log_buffer_index + line_length) > LOG_BUFFER_SIZE) { - printf("No room in client log\n"); + if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) { + TEST_info("Client log too full"); error_writing_log = 1; return; } strcat(client_log_buffer, line); client_log_buffer_index += line_length; - client_log_buffer[client_log_buffer_index] = '\n'; - client_log_buffer_index += 1; - - return; + client_log_buffer[client_log_buffer_index++] = '\n'; } -static void server_keylog_callback(const SSL *ssl, const char *line) { +static void server_keylog_callback(const SSL *ssl, const char *line) +{ int line_length = strlen(line); /* If the log doesn't fit, error out. */ - if ((server_log_buffer_index + line_length) > LOG_BUFFER_SIZE) { - printf("No room in server log\n"); + if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) { + TEST_info("Server og too full"); error_writing_log = 1; return; } strcat(server_log_buffer, line); server_log_buffer_index += line_length; - server_log_buffer[server_log_buffer_index] = '\n'; - server_log_buffer_index += 1; - - return; + server_log_buffer[server_log_buffer_index++] = '\n'; } static int compare_hex_encoded_buffer(const char *hex_encoded, size_t hex_length, const uint8_t *raw, - size_t raw_length) { - size_t i; - size_t j; - - /* One byte too big, just to be safe. */ - char hexed[3] = {0}; + size_t raw_length) +{ + size_t i, j; + char hexed[3]; - if ((raw_length * 2) != hex_length) { - printf("Inconsistent hex encoded lengths.\n"); + if (!TEST_size_t_eq(raw_length * 2, hex_length)) return 1; - } - for (i = j = 0; (i < raw_length) && ((j + 1) < hex_length); i++) { + for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) { sprintf(hexed, "%02x", raw[i]); - if ((hexed[0] != hex_encoded[j]) || (hexed[1] != hex_encoded[j + 1])) { - printf("Hex output does not match.\n"); + if (!TEST_int_eq(hexed[0], hex_encoded[j]) + || !TEST_int_eq(hexed[1], hex_encoded[j + 1])) return 1; - } - j += 2; } return 0; @@ -118,7 +108,8 @@ static int compare_hex_encoded_buffer(const char *hex_encoded, static int test_keylog_output(char *buffer, const SSL *ssl, const SSL_SESSION *session, - struct sslapitest_log_counts *expected) { + struct sslapitest_log_counts *expected) +{ char *token = NULL; unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0}; size_t client_random_size = SSL3_RANDOM_SIZE; @@ -131,28 +122,20 @@ static int test_keylog_output(char *buffer, const SSL *ssl, unsigned int client_application_secret_count = 0; unsigned int server_application_secret_count = 0; - token = strtok(buffer, " \n"); - while (token) { + for (token = strtok(buffer, " \n"); token != NULL; + token = strtok(NULL, " \n")) { if (strcmp(token, "RSA") == 0) { /* * Premaster secret. Tokens should be: 16 ASCII bytes of * hex-encoded encrypted secret, then the hex-encoded pre-master * secret. */ - token = strtok(NULL, " \n"); - if (!token) { - printf("Unexpectedly short premaster secret log.\n"); + if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; - } - if (strlen(token) != 16) { - printf("Bad value for encrypted secret: %s\n", token); + if (!TEST_size_t_eq(strlen(token), 16)) return 0; - } - token = strtok(NULL, " \n"); - if (!token) { - printf("Unexpectedly short premaster secret log.\n"); + if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; - } /* * We can't sensibly check the log because the premaster secret is * transient, and OpenSSL doesn't keep hold of it once the master @@ -167,51 +150,34 @@ static int test_keylog_output(char *buffer, const SSL *ssl, client_random_size = SSL_get_client_random(ssl, actual_client_random, SSL3_RANDOM_SIZE); - if (client_random_size != SSL3_RANDOM_SIZE) { - printf("Unexpected short client random.\n"); + if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE)) return 0; - } - token = strtok(NULL, " \n"); - if (!token) { - printf("Unexpected short master secret log.\n"); + if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; - } - if (strlen(token) != 64) { - printf("Bad value for client random: %s\n", token); + if (!TEST_size_t_eq(strlen(token), 64)) return 0; - } - if (compare_hex_encoded_buffer(token, 64, actual_client_random, - client_random_size)) { - printf("Bad value for client random: %s\n", token); + if (!TEST_false(compare_hex_encoded_buffer(token, 64, + actual_client_random, + client_random_size))) return 0; - } - token = strtok(NULL, " \n"); - if (!token) { - printf("Unexpectedly short master secret log.\n"); + if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; - } - master_key_size = SSL_SESSION_get_master_key(session, actual_master_key, master_key_size); - if (!master_key_size) { - printf("Error getting master key to compare.\n"); + if (!TEST_size_t_ne(master_key_size, 0)) return 0; - } - if (compare_hex_encoded_buffer(token, strlen(token), - actual_master_key, - master_key_size)) { - printf("Bad value for master key: %s\n", token); + if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token), + actual_master_key, + master_key_size))) return 0; - } - master_secret_count++; - } else if ((strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0) || - (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0) || - (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0) || - (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)) { + } else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0 + || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0 + || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0 + || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0) { /* * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded * client random, and then the hex-encoded secret. In this case, @@ -230,127 +196,95 @@ static int test_keylog_output(char *buffer, const SSL *ssl, client_random_size = SSL_get_client_random(ssl, actual_client_random, SSL3_RANDOM_SIZE); - if (client_random_size != SSL3_RANDOM_SIZE) { - printf("Unexpected short client random.\n"); + if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE)) return 0; - } - token = strtok(NULL, " \n"); - if (!token) { - printf("Unexpected short client handshake secret log.\n"); + if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; - } - if (strlen(token) != 64) { - printf("Bad value for client random: %s\n", token); + if (!TEST_size_t_eq(strlen(token), 64)) return 0; - } - if (compare_hex_encoded_buffer(token, 64, actual_client_random, - client_random_size)) { - printf("Bad value for client random: %s\n", token); + if (!TEST_false(compare_hex_encoded_buffer(token, 64, + actual_client_random, + client_random_size))) return 0; - } - token = strtok(NULL, " \n"); - if (!token) { - printf("Unexpectedly short master secret log.\n"); + if (!TEST_ptr(token = strtok(NULL, " \n"))) return 0; - } /* * TODO(TLS1.3): test that application traffic secrets are what * we expect */ } else { - printf("Unexpected token in buffer: %s\n", token); + TEST_info("Unexpected token %s\n", token); return 0; } - - token = strtok(NULL, " \n"); } - /* Return whether we got what we expected. */ - return ((rsa_key_exchange_count == expected->rsa_key_exchange_count) && - (master_secret_count == expected->master_secret_count) && - (client_handshake_secret_count == expected->client_handshake_secret_count) && - (server_handshake_secret_count == expected->server_handshake_secret_count) && - (client_application_secret_count == expected->client_application_secret_count) && - (server_application_secret_count == expected->server_application_secret_count)); + /* Got what we expected? */ + if (!TEST_size_t_eq(rsa_key_exchange_count, + expected->rsa_key_exchange_count) + || !TEST_size_t_eq(master_secret_count, + expected->master_secret_count) + || !TEST_size_t_eq(client_handshake_secret_count, + expected->client_handshake_secret_count) + || !TEST_size_t_eq(server_handshake_secret_count, + expected->server_handshake_secret_count) + || !TEST_size_t_eq(client_application_secret_count, + expected->client_application_secret_count) + || !TEST_size_t_eq(server_application_secret_count, + expected->server_application_secret_count)) + return 0; + return 1; } -static int test_keylog(void) { +static int test_keylog(void) +{ SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; - int rc; struct sslapitest_log_counts expected = {0}; /* Clean up logging space */ - memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1); - memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1); + memset(client_log_buffer, 0, sizeof(client_log_buffer)); + memset(server_log_buffer, 0, sizeof(server_log_buffer)); client_log_buffer_index = 0; server_log_buffer_index = 0; error_writing_log = 0; - if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx, - &cctx, cert, privkey)) { - printf("Unable to create SSL_CTX pair\n"); + if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), + TLS_client_method(), + &sctx, &cctx, cert, privkey))) return 0; - } /* We cannot log the master secret for TLSv1.3, so we should forbid it. */ SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3); SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3); /* We also want to ensure that we use RSA-based key exchange. */ - rc = SSL_CTX_set_cipher_list(cctx, "RSA"); - if (rc == 0) { - printf("Unable to restrict to RSA key exchange.\n"); + if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA"))) goto end; - } - if (SSL_CTX_get_keylog_callback(cctx)) { - printf("Unexpected initial value for client " - "SSL_CTX_get_keylog_callback()\n"); - goto end; - } - if (SSL_CTX_get_keylog_callback(sctx)) { - printf("Unexpected initial value for server " - "SSL_CTX_get_keylog_callback()\n"); + if (!TEST_ptr_null((void *)SSL_CTX_get_keylog_callback(cctx)) + || !TEST_ptr_null((void *)SSL_CTX_get_keylog_callback(sctx))) goto end; - } - SSL_CTX_set_keylog_callback(cctx, client_keylog_callback); - SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); - - if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) { - printf("Unexpected set value for client " - "SSL_CTX_get_keylog_callback()\n"); - } - - if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) { - printf("Unexpected set value for server " - "SSL_CTX_get_keylog_callback()\n"); - } - - /* Now do a handshake and check that the logs have been written to. */ - if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { - printf("Unable to create SSL objects\n"); + if (!TEST_ptr_eq((void *)SSL_CTX_get_keylog_callback(cctx), + (void *)client_keylog_callback)) goto end; - } - - if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { - printf("Unable to create SSL connection\n"); - goto end; - } - - if (error_writing_log) { - printf("Error encountered while logging\n"); + SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); + if (!TEST_ptr_eq((void *)SSL_CTX_get_keylog_callback(sctx), + (void *)server_keylog_callback)) goto end; - } - if ((client_log_buffer_index == 0) || (server_log_buffer_index == 0)) { - printf("No logs written\n"); + /* Now do a handshake and check that the logs have been written to. */ + if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, + &clientssl, NULL, NULL)) + || !TEST_true(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_NONE)) + || !TEST_false(error_writing_log) + || !TEST_int_gt(client_log_buffer_index, 0) + || !TEST_int_gt(server_log_buffer_index, 0)) goto end; - } /* * Now we want to test that our output data was vaguely sensible. We @@ -360,18 +294,14 @@ static int test_keylog(void) { */ expected.rsa_key_exchange_count = 1; expected.master_secret_count = 1; - if (!test_keylog_output(client_log_buffer, clientssl, - SSL_get_session(clientssl), &expected)) { - printf("Error encountered in client log buffer\n"); + if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, + SSL_get_session(clientssl), &expected))) goto end; - } expected.rsa_key_exchange_count = 0; - if (!test_keylog_output(server_log_buffer, serverssl, - SSL_get_session(serverssl), &expected)) { - printf("Error encountered in server log buffer\n"); + if (!TEST_true(test_keylog_output(server_log_buffer, serverssl, + SSL_get_session(serverssl), &expected))) goto end; - } testresult = 1; @@ -385,64 +315,46 @@ end: } #ifndef OPENSSL_NO_TLS1_3 -static int test_keylog_no_master_key(void) { +static int test_keylog_no_master_key(void) +{ SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; struct sslapitest_log_counts expected = {0}; /* Clean up logging space */ - memset(client_log_buffer, 0, LOG_BUFFER_SIZE + 1); - memset(server_log_buffer, 0, LOG_BUFFER_SIZE + 1); + memset(client_log_buffer, 0, sizeof(client_log_buffer)); + memset(server_log_buffer, 0, sizeof(server_log_buffer)); client_log_buffer_index = 0; server_log_buffer_index = 0; error_writing_log = 0; - if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx, - &cctx, cert, privkey)) { - printf("Unable to create SSL_CTX pair\n"); + if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), + TLS_client_method(), &sctx, + &cctx, cert, privkey))) return 0; - } - if (SSL_CTX_get_keylog_callback(cctx)) { - printf("Unexpected initial value for client " - "SSL_CTX_get_keylog_callback()\n"); - goto end; - } - if (SSL_CTX_get_keylog_callback(sctx)) { - printf("Unexpected initial value for server " - "SSL_CTX_get_keylog_callback()\n"); + if (!TEST_ptr_null((void *)SSL_CTX_get_keylog_callback(cctx)) + || !TEST_ptr_null((void *)SSL_CTX_get_keylog_callback(sctx))) goto end; - } SSL_CTX_set_keylog_callback(cctx, client_keylog_callback); - SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); - - if (SSL_CTX_get_keylog_callback(cctx) != client_keylog_callback) { - printf("Unexpected set value for client " - "SSL_CTX_get_keylog_callback()\n"); - } - - if (SSL_CTX_get_keylog_callback(sctx) != server_keylog_callback) { - printf("Unexpected set value for server " - "SSL_CTX_get_keylog_callback()\n"); - } - - /* Now do a handshake and check that the logs have been written to. */ - if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { - printf("Unable to create SSL objects\n"); + if (!TEST_ptr_eq((void *)SSL_CTX_get_keylog_callback(cctx), + (void *)client_keylog_callback)) goto end; - } - if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { - printf("Unable to create SSL connection\n"); + SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); + if (!TEST_ptr_eq((void *)SSL_CTX_get_keylog_callback(sctx), + (void *)server_keylog_callback)) goto end; - } - if (error_writing_log) { - printf("Error encountered while logging\n"); + /* Now do a handshake and check that the logs have been written to. */ + if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, + &clientssl, NULL, NULL)) + || !TEST_true(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_NONE)) + || !TEST_false(error_writing_log)) goto end; - } /* * Now we want to test that our output data was vaguely sensible. For this @@ -453,16 +365,12 @@ static int test_keylog_no_master_key(void) { expected.server_handshake_secret_count = 1; expected.client_application_secret_count = 1; expected.server_application_secret_count = 1; - if (!test_keylog_output(client_log_buffer, clientssl, - SSL_get_session(clientssl), &expected)) { - printf("Error encountered in client log buffer\n"); - goto end; - } - if (!test_keylog_output(server_log_buffer, serverssl, - SSL_get_session(serverssl), &expected)) { - printf("Error encountered in server log buffer\n"); + if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, + SSL_get_session(clientssl), &expected)) + || !TEST_true(test_keylog_output(server_log_buffer, serverssl, + SSL_get_session(serverssl), + &expected))) goto end; - } testresult = 1; @@ -495,59 +403,42 @@ static int full_early_callback(SSL *s, int *al, void *arg) return -1; len = SSL_early_get0_ciphers(s, &p); - if (len != sizeof(expected_ciphers) || - memcmp(p, expected_ciphers, len) != 0) { - printf("Early callback expected ciphers mismatch\n"); - return 0; - } - len = SSL_early_get0_compression_methods(s, &p); - if (len != 1 || *p != 0) { - printf("Early callback expected compression methods mismatch\n"); + if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers)) + || !TEST_size_t_eq(SSL_early_get0_compression_methods(s, &p), 1) + || !TEST_int_eq(*p, 0)) return 0; - } return 1; } -static int test_early_cb(void) { +static int test_early_cb(void) +{ SSL_CTX *cctx = NULL, *sctx = NULL; SSL *clientssl = NULL, *serverssl = NULL; int testctr = 0, testresult = 0; - if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx, - &cctx, cert, privkey)) { - printf("Unable to create SSL_CTX pair\n"); + if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), + TLS_client_method(), &sctx, + &cctx, cert, privkey))) goto end; - } - SSL_CTX_set_early_cb(sctx, full_early_callback, &testctr); + /* The gimpy cipher list we configure can't do TLS 1.3. */ SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION); - if (!SSL_CTX_set_cipher_list(cctx, - "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384")) { - printf("Failed to set cipher list\n"); - goto end; - } - - if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { - printf("Unable to create SSL objects\n"); - goto end; - } - - if (create_ssl_connection(serverssl, clientssl, SSL_ERROR_WANT_EARLY)) { - printf("Creating SSL connection succeeded with async early return\n"); - goto end; - } - /* Passing a -1 literal is a hack since the real value was lost. */ - if (SSL_get_error(serverssl, -1) != SSL_ERROR_WANT_EARLY) { - printf("Early callback failed to make state SSL_ERROR_WANT_EARLY\n"); + if (!TEST_true(SSL_CTX_set_cipher_list(cctx, + "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384")) + || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, + &clientssl, NULL, NULL)) + || !TEST_false(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_WANT_EARLY)) + /* + * Passing a -1 literal is a hack since + * the real value was lost. + * */ + || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_EARLY) + || !TEST_true(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_NONE))) goto end; - } - - if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { - printf("Restarting SSL connection failed\n"); - goto end; - } testresult = 1; @@ -568,29 +459,23 @@ static int execute_test_large_message(const SSL_METHOD *smeth, SSL *clientssl = NULL, *serverssl = NULL; int testresult = 0; int i; - BIO *certbio = BIO_new_file(cert, "r"); + BIO *certbio = NULL; X509 *chaincert = NULL; int certlen; - if (certbio == NULL) { - printf("Can't load the certificate file\n"); + if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))) goto end; - } chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL); BIO_free(certbio); certbio = NULL; - if (chaincert == NULL) { - printf("Unable to load certificate for chain\n"); + if (!TEST_ptr(chaincert)) goto end; - } - if (!create_ssl_ctx_pair(smeth, cmeth, &sctx, - &cctx, cert, privkey)) { - printf("Unable to create SSL_CTX pair\n"); + if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, &sctx, + &cctx, cert, privkey))) goto end; - } - if(read_ahead) { + if (read_ahead) { /* * Test that read_ahead works correctly when dealing with large * records @@ -602,42 +487,33 @@ static int execute_test_large_message(const SSL_METHOD *smeth, * We assume the supplied certificate is big enough so that if we add * NUM_EXTRA_CERTS it will make the overall message large enough. The * default buffer size is requested to be 16k, but due to the way BUF_MEM - * works, it ends up allocating a little over 21k (16 * 4/3). So, in this test - * we need to have a message larger than that. + * works, it ends up allocating a little over 21k (16 * 4/3). So, in this + * test we need to have a message larger than that. */ certlen = i2d_X509(chaincert, NULL); - OPENSSL_assert((certlen * NUM_EXTRA_CERTS) - > ((SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3)); + OPENSSL_assert(certlen * NUM_EXTRA_CERTS > + (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3); for (i = 0; i < NUM_EXTRA_CERTS; i++) { - if (!X509_up_ref(chaincert)) { - printf("Unable to up ref cert\n"); + if (!X509_up_ref(chaincert)) goto end; - } if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) { - printf("Unable to add extra chain cert %d\n", i); X509_free(chaincert); goto end; } } - if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { - printf("Unable to create SSL objects\n"); + if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, + NULL, NULL)) + || !TEST_true(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_NONE))) goto end; - } - - if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { - printf("Unable to create SSL connection\n"); - goto end; - } /* * Calling SSL_clear() first is not required but this tests that SSL_clear() * doesn't leak (when using enable-crypto-mdebug). */ - if (!SSL_clear(serverssl)) { - printf("Unexpected failure from SSL_clear()\n"); + if (!TEST_true(SSL_clear(serverssl))) goto end; - } testresult = 1; end: @@ -678,7 +554,7 @@ static int test_large_message_dtls(void) static int ocsp_server_cb(SSL *s, void *arg) { int *argi = (int *)arg; - unsigned char *orespdercopy = NULL; + unsigned char *copy = NULL; STACK_OF(OCSP_RESPID) *ids = NULL; OCSP_RESPID *id = NULL; @@ -695,15 +571,11 @@ static int ocsp_server_cb(SSL *s, void *arg) return SSL_TLSEXT_ERR_ALERT_FATAL; } - - orespdercopy = OPENSSL_memdup(orespder, sizeof(orespder)); - if (orespdercopy == NULL) + if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder)))) return SSL_TLSEXT_ERR_ALERT_FATAL; - SSL_set_tlsext_status_ocsp_resp(s, orespdercopy, sizeof(orespder)); - + SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder)); ocsp_server_called = 1; - return SSL_TLSEXT_ERR_OK; } @@ -717,12 +589,10 @@ static int ocsp_client_cb(SSL *s, void *arg) return 0; len = SSL_get_tlsext_status_ocsp_resp(s, &respderin); - - if (memcmp(orespder, respderin, len) != 0) + if (!TEST_mem_eq(orespder, len, respderin, len)) return 0; ocsp_client_called = 1; - return 1; } @@ -736,55 +606,32 @@ static int test_tlsext_status_type(void) BIO *certbio = NULL; if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx, - &cctx, cert, privkey)) { - printf("Unable to create SSL_CTX pair\n"); + &cctx, cert, privkey)) return 0; - } - if (SSL_CTX_get_tlsext_status_type(cctx) != -1) { - printf("Unexpected initial value for " - "SSL_CTX_get_tlsext_status_type()\n"); + if (SSL_CTX_get_tlsext_status_type(cctx) != -1) goto end; - } /* First just do various checks getting and setting tlsext_status_type */ clientssl = SSL_new(cctx); - if (SSL_get_tlsext_status_type(clientssl) != -1) { - printf("Unexpected initial value for SSL_get_tlsext_status_type()\n"); + if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1) + || !TEST_true(SSL_set_tlsext_status_type(clientssl, + TLSEXT_STATUSTYPE_ocsp)) + || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl), + TLSEXT_STATUSTYPE_ocsp)) goto end; - } - - if (!SSL_set_tlsext_status_type(clientssl, TLSEXT_STATUSTYPE_ocsp)) { - printf("Unexpected fail for SSL_set_tlsext_status_type()\n"); - goto end; - } - - if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) { - printf("Unexpected result for SSL_get_tlsext_status_type()\n"); - goto end; - } SSL_free(clientssl); clientssl = NULL; - if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)) { - printf("Unexpected fail for SSL_CTX_set_tlsext_status_type()\n"); + if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp) + || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) goto end; - } - - if (SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) { - printf("Unexpected result for SSL_CTX_get_tlsext_status_type()\n"); - goto end; - } clientssl = SSL_new(cctx); - - if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) { - printf("Unexpected result for SSL_get_tlsext_status_type() (test 2)\n"); + if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) goto end; - } - SSL_free(clientssl); clientssl = NULL; @@ -792,27 +639,17 @@ static int test_tlsext_status_type(void) * Now actually do a handshake and check OCSP information is exchanged and * the callbacks get called */ - SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb); SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg); SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb); SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg); - - if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { - printf("Unable to create SSL objects\n"); - goto end; - } - - if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { - printf("Unable to create SSL connection\n"); - goto end; - } - - if (!ocsp_client_called || !ocsp_server_called) { - printf("OCSP callbacks not called\n"); + if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, + &clientssl, NULL, NULL)) + || !TEST_true(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_NONE)) + || !TEST_true(ocsp_client_called) + || !TEST_true(ocsp_server_called)) goto end; - } - SSL_free(serverssl); SSL_free(clientssl); serverssl = NULL; @@ -822,23 +659,14 @@ static int test_tlsext_status_type(void) ocsp_client_called = 0; ocsp_server_called = 0; cdummyarg = 0; - - if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { - printf("Unable to create SSL objects\n"); + if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, + &clientssl, NULL, NULL)) + /* This should fail because the callback will fail */ + || !TEST_false(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_NONE)) + || !TEST_false(ocsp_client_called) + || !TEST_false(ocsp_server_called)) goto end; - } - - /* This should fail because the callback will fail */ - if (create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { - printf("Unexpected success creating the connection\n"); - goto end; - } - - if (ocsp_client_called || ocsp_server_called) { - printf("OCSP callbacks successfully called unexpectedly\n"); - goto end; - } - SSL_free(serverssl); SSL_free(clientssl); serverssl = NULL; @@ -851,30 +679,22 @@ static int test_tlsext_status_type(void) ocsp_client_called = 0; ocsp_server_called = 0; cdummyarg = 2; - - if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { - printf("Unable to create SSL objects\n"); + if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, + &clientssl, NULL, NULL))) goto end; - } /* * We'll just use any old cert for this test - it doesn't have to be an OCSP * specific one. We'll use the server cert. */ - certbio = BIO_new_file(cert, "r"); - if (certbio == NULL) { - printf("Can't load the certificate file\n"); - goto end; - } - id = OCSP_RESPID_new(); - ids = sk_OCSP_RESPID_new_null(); - ocspcert = PEM_read_bio_X509(certbio, NULL, NULL, NULL); - if (id == NULL || ids == NULL || ocspcert == NULL - || !OCSP_RESPID_set_by_key(id, ocspcert) - || !sk_OCSP_RESPID_push(ids, id)) { - printf("Unable to set OCSP_RESPIDs\n"); + if (!TEST_ptr(certbio = BIO_new_file(cert, "r")) + || !TEST_ptr(id = OCSP_RESPID_new()) + || !TEST_ptr(ids = sk_OCSP_RESPID_new_null()) + || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio, + NULL, NULL, NULL)) + || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert)) + || !TEST_true(sk_OCSP_RESPID_push(ids, id))) goto end; - } id = NULL; SSL_set_tlsext_status_ids(clientssl, ids); /* Control has been transferred */ @@ -883,15 +703,11 @@ static int test_tlsext_status_type(void) BIO_free(certbio); certbio = NULL; - if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { - printf("Unable to create SSL connection\n"); + if (!TEST_true(create_ssl_connection(serverssl, clientssl, + SSL_ERROR_NONE)) + || !TEST_true(ocsp_client_called) + || !TEST_true(ocsp_server_called)) goto end; - } |