#include "ssl_transfer.h"
#include <stdio.h>
#include <time.h>
#include <openssl/ssl.h>
#include "test_config.h"
#include "../internal.h"
SSLTransfer::SSLTransfer() {}
static bool WriteData(std::string prefix, const uint8_t *input, size_t len) {
if (prefix.empty()) {
return true;
}
struct FileCloser {
void operator()(FILE *f) const { fclose(f); }
};
using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
std::string path = prefix + "-" + std::to_string(rand());
ScopedFILE file(fopen(path.c_str(), "w"));
if (!file) {
return false;
}
return fwrite(input, len, 1, file.get()) == 1;
}
static bool EncodeAndDecodeSSL(const TestConfig *config, SSL *in, SSL_CTX *ctx,
bssl::UniquePtr<SSL> *out) {
size_t encoded_len;
bssl::UniquePtr<uint8_t> encoded;
uint8_t *encoded_raw;
if (!SSL_to_bytes(in, &encoded_raw, &encoded_len)) {
fprintf(stderr, "SSL_to_bytes failed. Error code: %s\n",
ERR_reason_error_string(ERR_peek_last_error()));
return false;
}
encoded.reset(encoded_raw);
std::string path_prefix = config->ssl_fuzz_seed_path_prefix;
if (!WriteData(path_prefix, encoded.get(), encoded_len)) {
fprintf(stderr, "Failed to write the output of |SSL_to_bytes|.\n");
return false;
}
const uint8_t *ptr2 = encoded.get();
SSL *server2_ = SSL_from_bytes(ptr2, encoded_len, ctx);
if (server2_ == nullptr) {
fprintf(stderr, "SSL_from_bytes failed. Error code: %s\n", ERR_reason_error_string(ERR_peek_last_error()));
return false;
}
out->reset(server2_);
return true;
}
static void MoveBIOs(SSL *dest, SSL *src) {
BIO *rbio = SSL_get_rbio(src);
BIO_up_ref(rbio);
SSL_set0_rbio(dest, rbio);
BIO *wbio = SSL_get_wbio(src);
BIO_up_ref(wbio);
SSL_set0_wbio(dest, wbio);
SSL_set0_rbio(src, nullptr);
SSL_set0_wbio(src, nullptr);
}
static bool TransferSSL(const TestConfig *config, bssl::UniquePtr<SSL> *in, bssl::UniquePtr<SSL> *out) {
if (!in || !in->get()) {
return false;
}
SSL_CTX *in_ctx = SSL_get_SSL_CTX(in->get());
bssl::UniquePtr<SSL> decoded_ssl;
if (!EncodeAndDecodeSSL(config, in->get(), in_ctx, &decoded_ssl)){
return false;
}
MoveBIOs(decoded_ssl.get(), in->get());
if (!SetTestConfig(decoded_ssl.get(), GetTestConfig(in->get()))) {
return false;
}
std::unique_ptr<TestState> state(GetTestState(in->get()));
if (!SetTestState(decoded_ssl.get(), std::move(state))) {
return false;
}
std::unique_ptr<TestState> tmp1;
if (!SetTestState(in->get(), std::move(tmp1)) || !SetTestConfig(in->get(), nullptr)) {
return false;
}
SSL_free(in->release());
if (out == nullptr) {
in->reset(decoded_ssl.release());
} else {
out->reset(decoded_ssl.release());
}
return true;
}
void SSLTransfer::MarkTest(const TestConfig *config, const SSL *ssl) {
if (config->check_ssl_transfer && IsSupported(ssl)) {
fprintf(stderr, "Eligible for testing SSL transfer.\n");
}
}
bool SSLTransfer::ResetSSL(const TestConfig *config, bssl::UniquePtr<SSL> *in) {
if (config->do_ssl_transfer && IsSupported(in->get())) {
fprintf(stderr, "SSL transfer is going to be tested.\n");
if (!TransferSSL(config, in, nullptr)) {
return false;
}
}
return true;
}
bool SSLTransfer::IsSupported(const SSL *in) {
ERR_set_mark();
bool ret = true;
if (!bssl::ssl_transfer_supported(in)) {
ret = false;
ERR_pop_to_mark();
}
return ret;
}
bool SSLTransfer::MarkOrReset(const TestConfig *config, bssl::UniquePtr<SSL> *in) {
if (!config || !in) {
return false;
}
bool supported = IsSupported(in->get());
if (!supported) {
return true;
}
MarkTest(config, in->get());
return ResetSSL(config, in);
}