/*
* Copyright 2016-2021 The OpenSSL Project Authors. 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 <string.h>
#include <openssl/e_os2.h>
#include <openssl/crypto.h>
#include "internal/nelem.h"
#include "ssl_test_ctx.h"
#include "../testutil.h"
#ifdef OPENSSL_SYS_WINDOWS
# define strcasecmp _stricmp
#endif
static const int default_app_data_size = 256;
/* Default set to be as small as possible to exercise fragmentation. */
static const int default_max_fragment_size = 512;
static int parse_boolean(const char *value, int *result)
{
if (strcasecmp(value, "Yes") == 0) {
*result = 1;
return 1;
}
else if (strcasecmp(value, "No") == 0) {
*result = 0;
return 1;
}
TEST_error("parse_boolean given: '%s'", value);
return 0;
}
#define IMPLEMENT_SSL_TEST_BOOL_OPTION(struct_type, name, field) \
static int parse_##name##_##field(struct_type *ctx, const char *value) \
{ \
return parse_boolean(value, &ctx->field); \
}
#define IMPLEMENT_SSL_TEST_STRING_OPTION(struct_type, name, field) \
static int parse_##name##_##field(struct_type *ctx, const char *value) \
{ \
OPENSSL_free(ctx->field); \
ctx->field = OPENSSL_strdup(value); \
return TEST_ptr(ctx->field); \
}
#define IMPLEMENT_SSL_TEST_INT_OPTION(struct_type, name, field) \
static int parse_##name##_##field(struct_type *ctx, const char *value) \
{ \
ctx->field = atoi(value); \
return 1; \
}
/* True enums and other test configuration values that map to an int. */
typedef struct {
const char *name;
int value;
} test_enum;
__owur static int parse_enum(const test_enum *enums, size_t num_enums,
int *value, const char *name)
{
size_t i;
for (i = 0; i < num_enums; i++) {
if (strcmp(enums[i].name, name) == 0) {
*value = enums[i].value;
return 1;
}
}
return 0;
}
static const char *enum_name(const test_enum *enums, size_t num_enums,
int value)
{
size_t i;
for (i = 0; i < num_enums; i++) {
if (enums[i].value == value) {
return enums[i].name;
}
}
return "InvalidValue";
}
/* ExpectedResult */
static const test_enum ssl_test_results[] = {
{"Success", SSL_TEST_SUCCESS},
{"ServerFail", SSL_TEST_SERVER_FAIL},
{"ClientFail", SSL_TEST_CLIENT_FAIL},
{"InternalError", SSL_TEST_INTERNAL_ERROR},
{"FirstHandshakeFailed", SSL_TEST_FIRST_HANDSHAKE_FAILED},
};
__owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
{
int ret_value;
if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
&ret_value, value)) {
return 0;
}
test_ctx->expected_result = ret_value;
return 1;
}
const char *ssl_test_result_name(ssl_test_result_t result)
{
return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
}
/* ExpectedClientAlert / ExpectedServerAlert */
static const test_enum ssl_alerts[] = {
{"UnknownCA", SSL_AD_UNKNOWN_CA},
{"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE},
{"UnrecognizedName", SSL_AD_UNRECOGNIZED_NAME},
{"BadCertificate", SSL_AD_BAD_CERTIFICATE},
{"NoApplicationProtocol", SSL_AD_NO_APPLICATION_PROTOCOL},
{"CertificateRequired", SSL_AD_CERTIFICATE_REQUIRED},
};
__owur static int parse_alert(int *alert, const char *value)
{
return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
}
__owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
{
return parse_alert(&test_ctx->expected_client_alert, value);
}
__owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
{
return parse_alert(&test_ctx->expected_server_alert, value);
}
const char *ssl_alert_name(int alert)
{
return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
}
/* ExpectedProtocol */
static const test_enum ssl_protocols[] = {
{"TLSv1.3", TLS1_3_VERSION},
{"TLSv1.2", TLS1_2_VERSION},
{"TLSv1.1", TLS1_1_VERSION},
{"TLSv1", TLS1_VERSION},
{"SSLv3", SSL3_VERSION},
{"DTLSv1", DTLS1_VERSION},
{"DTLSv1.2", DTLS1_2_VERSION},
};
__owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value