1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* Copyright 2025 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 <stdio.h>
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/ssl.h>
#include "helpers/ssltestlib.h"
#include "testutil.h"
/**
* @brief Global static variables for certificate and key handling.
*
* These variables store the paths and context used for managing
* certificates and private keys in the application.
*
* - certsdir: Directory containing trusted certificates.
* - cert: Path to the certificate file in use.
* - privkey: Path to the private key file.
* - mcount: Number of mallocs counted
* - rcount: Number of reallocs counted
* - fcount: Number of frees counted
* - scount: Number of mallocs counted prior to workload
*/
static char *cert = NULL;
static char *privkey = NULL;
static int mcount, rcount, fcount, scount;
/**
* @brief Performs an SSL/TLS handshake between a test client and server.
*
* This function sets up SSL/TLS contexts and objects for both client and
* server, then initiates a handshake to verify successful connection
* establishment. It is intended for use in testing scenarios to validate
* handshake behavior using specified certificates and keys.
*
* @return 1 on successful handshake, 0 on failure.
*
* @note The function uses @c TEST_true() macros to validate intermediate
* steps. All SSL objects and contexts are freed before returning.
*/
static int do_handshake(OSSL_LIB_CTX *libctx)
{
SSL_CTX *cctx = NULL, *sctx = NULL;
SSL *clientssl = NULL, *serverssl = NULL;
int testresult = 0;
if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
TLS_client_method(),
TLS1_VERSION, 0,
&sctx, &cctx, cert, privkey)))
return 0;
/* Now do a handshake */
if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
&clientssl, NULL, NULL))
|| !TEST_true(create_ssl_connection(serverssl, clientssl,
SSL_ERROR_NONE)))
goto end;
testresult = 1;
end:
SSL_free(serverssl);
SSL_free(clientssl);
SSL_CTX_free(sctx);
SSL_CTX_free(cctx);
return testresult;
}
/**
* @brief run our workload to count the number of allocations we make.
*
* Creates a new OpenSSL library context and performs a test SSL/TLS
* handshake. The number of malloc operations is recorded and printed for
* diagnostic purposes.
*
* @return 1 if the handshake succeeds, 0 otherwise.
*/
static int test_record_alloc_counts(void)
{
int ret;
OSSL_LIB_CTX *libctx;
libctx = OSSL_LIB_CTX_new();
if (!TEST_ptr(libctx))
return 0;
ret = do_handshake(libctx);
OSSL_LIB_CTX_free(libctx);
libctx = NULL;
return ret;
}
/**
* @brief run our workload to count the number of allocations we make.
*
* Creates a new OpenSSL library context and performs a test SSL/TLS
* handshake.
*
* Note this is exactly the same as test_record_alloc_counts with 1 difference
* The test always returns 1. We do this because with allocation failures
* in effect, we can't expect things to work, so we always return success
* so that the test keeps running.
*/
static int test_alloc_failures(void)
{
OSSL_LIB_CTX *libctx;
libctx = OSSL_LIB_CTX_new();
if (!TEST_ptr(libctx))
return 1;
do_handshake(libctx);
OSSL_LIB_CTX_free(libctx);
libctx = NULL;
return 1;
}
static int test_report_alloc_counts(void)
{
CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
/*
* Report our memory allocations from the count run
* NOTE: We report a number of allocations to skip here
* (the scount value). These are the allocations that took
* place while the test harness itself was getting setup
* (i.e. calling OPENSSL_init_crypto/etc). We can't fail
* those allocations as they will cause the test to fail before
* we have even run the workload. So report them so we can
* allow them to function before we start doing any real testing
*/
TEST_info("skip: %d count %d\n", scount, mcount - scount);
return 1;
}
int setup_tests(void)
{
char *opmode = NULL;
char *certsdir = NULL;
if (!TEST_ptr(opmode = test_get_argument(0)))
goto err;
if (!TEST_ptr(certsdir = test_get_argument(1)))
goto err;
cert = test_mk_file_path(certsdir, "servercert.pem");
if (cert == NULL)
goto err;
privkey = test_mk_file_path(certsdir, "serverkey.pem");
if (privkey == NULL)
goto err;
if (strcmp(opmode, "count") == 0) {
CRYPTO_get_alloc_counts(&scount, &rcount, &fcount);
ADD_TEST(test_record_alloc_counts);
ADD_TEST(test_report_alloc_counts);
} else {
ADD_TEST(test_alloc_failures);
}
return 1;
err:
OPENSSL_free(cert);
OPENSSL_free(privkey);
return 0;
}
void cleanup_tests(void)
{
OPENSSL_free(cert);
OPENSSL_free(privkey);
}
|