Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/test/ssl_transfer.cc

raw
1 /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 * SPDX-License-Identifier: Apache-2.0 OR ISC
3 */
4
5 #include "ssl_transfer.h"
6
7 #include <stdio.h>
8 #include <time.h>
9
10 #include <openssl/ssl.h>
11
12 #include "test_config.h"
13 #include "../internal.h"
14
15 SSLTransfer::SSLTransfer() {}
16
17 // WriteData writes |input| to disk when |prefix| is not empty.
18 // This is to generate data to seed related Fuzz corpus generation.
19 static bool WriteData(std::string prefix, const uint8_t *input, size_t len) {
20 if (prefix.empty()) {
21 // WriteData is not needed because related config is not enabled.
22 return true;
23 }
24
25 struct FileCloser {
26 void operator()(FILE *f) const { fclose(f); }
27 };
28
29 using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
30 std::string path = prefix + "-" + std::to_string(rand());
31 ScopedFILE file(fopen(path.c_str(), "w"));
32 if (!file) {
33 return false;
34 }
35
36 return fwrite(input, len, 1, file.get()) == 1;
37 }
38
39 // EncodeAndDecodeSSL transfers the states of |in| to a newly allocated SSL
40 // by using |SSL_to/from_bytes|. When success, |in| is freed and |out| holds
41 // the transferred SSL.
42 static bool EncodeAndDecodeSSL(const TestConfig *config, SSL *in, SSL_CTX *ctx,
43 bssl::UniquePtr<SSL> *out) {
44 // Encoding SSL to bytes.
45 size_t encoded_len;
46 bssl::UniquePtr<uint8_t> encoded;
47 uint8_t *encoded_raw;
48 if (!SSL_to_bytes(in, &encoded_raw, &encoded_len)) {
49 fprintf(stderr, "SSL_to_bytes failed. Error code: %s\n",
50 ERR_reason_error_string(ERR_peek_last_error()));
51 return false;
52 }
53 encoded.reset(encoded_raw);
54 // Decoding SSL from the bytes.
55 std::string path_prefix = config->ssl_fuzz_seed_path_prefix;
56 if (!WriteData(path_prefix, encoded.get(), encoded_len)) {
57 fprintf(stderr, "Failed to write the output of |SSL_to_bytes|.\n");
58 return false;
59 }
60 const uint8_t *ptr2 = encoded.get();
61 SSL *server2_ = SSL_from_bytes(ptr2, encoded_len, ctx);
62 if (server2_ == nullptr) {
63 fprintf(stderr, "SSL_from_bytes failed. Error code: %s\n", ERR_reason_error_string(ERR_peek_last_error()));
64 return false;
65 }
66 out->reset(server2_);
67 return true;
68 }
69
70 // MoveBIOs moves the |BIO|s of |src| to |dst|.
71 static void MoveBIOs(SSL *dest, SSL *src) {
72 BIO *rbio = SSL_get_rbio(src);
73 BIO_up_ref(rbio);
74 SSL_set0_rbio(dest, rbio);
75
76 BIO *wbio = SSL_get_wbio(src);
77 BIO_up_ref(wbio);
78 SSL_set0_wbio(dest, wbio);
79
80 SSL_set0_rbio(src, nullptr);
81 SSL_set0_wbio(src, nullptr);
82 }
83
84 // TransferSSL transfers |in| to |out|.
85 static bool TransferSSL(const TestConfig *config, bssl::UniquePtr<SSL> *in, bssl::UniquePtr<SSL> *out) {
86 if (!in || !in->get()) {
87 return false;
88 }
89 SSL_CTX *in_ctx = SSL_get_SSL_CTX(in->get());
90 // Encode the SSL |in| into bytes.
91 // Decode the bytes into a new SSL.
92 bssl::UniquePtr<SSL> decoded_ssl;
93 if (!EncodeAndDecodeSSL(config, in->get(), in_ctx, &decoded_ssl)){
94 return false;
95 }
96 // Move the bio.
97 MoveBIOs(decoded_ssl.get(), in->get());
98 if (!SetTestConfig(decoded_ssl.get(), GetTestConfig(in->get()))) {
99 return false;
100 }
101 // Move the test state.
102 std::unique_ptr<TestState> state(GetTestState(in->get()));
103 if (!SetTestState(decoded_ssl.get(), std::move(state))) {
104 return false;
105 }
106 // Unset the test state of |in|.
107 std::unique_ptr<TestState> tmp1;
108 if (!SetTestState(in->get(), std::move(tmp1)) || !SetTestConfig(in->get(), nullptr)) {
109 return false;
110 }
111 // Free the SSL of |in|.
112 SSL_free(in->release());
113 // If |out| is not nullptr, |out| will hold the decoded SSL.
114 // Else, |in| will get reset to hold the decoded SSL.
115 if (out == nullptr) {
116 in->reset(decoded_ssl.release());
117 } else {
118 out->reset(decoded_ssl.release());
119 }
120 return true;
121 }
122
123 void SSLTransfer::MarkTest(const TestConfig *config, const SSL *ssl) {
124 if (config->check_ssl_transfer && IsSupported(ssl)) {
125 // Below message is to inform runner.go that this test case can
126 // be converted to test SSL transfer.
127 // In the converted test, |IsSupported| should be called again
128 // before |TransferSSL| because each test case may perform
129 // multiple connections. Not all connections can be transferred.
130 fprintf(stderr, "Eligible for testing SSL transfer.\n");
131 }
132 }
133
134 bool SSLTransfer::ResetSSL(const TestConfig *config, bssl::UniquePtr<SSL> *in) {
135 if (config->do_ssl_transfer && IsSupported(in->get())) {
136 // Below message is to inform runner.go that this test case
137 // is going to test SSL transfer.
138 fprintf(stderr, "SSL transfer is going to be tested.\n");
139 if (!TransferSSL(config, in, nullptr)) {
140 return false;
141 }
142 }
143 return true;
144 }
145
146 // IsSupported is wrapper of |ssl_transfer_supported| and includes
147 // some logics to clean error code that may get generated when not supported.
148 bool SSLTransfer::IsSupported(const SSL *in) {
149 // |ssl_transfer_supported| may generate new error code.
150 // |ERR_set_mark| and |ERR_pop_to_mark| are used to clean the error states.
151 ERR_set_mark();
152 bool ret = true;
153 if (!bssl::ssl_transfer_supported(in)) {
154 ret = false;
155 ERR_pop_to_mark();
156 }
157 return ret;
158 }
159
160 bool SSLTransfer::MarkOrReset(const TestConfig *config, bssl::UniquePtr<SSL> *in) {
161 if (!config || !in) {
162 return false;
163 }
164 bool supported = IsSupported(in->get());
165 if (!supported) {
166 // Return true because not all ssl connections can be transferred.
167 return true;
168 }
169 MarkTest(config, in->get());
170 return ResetSSL(config, in);
171 }