Debian Codegraph @

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

raw
1 // Copyright (c) 2018, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <unistd.h>
9
10 #include <memory>
11
12 #include <openssl/bytestring.h>
13 #include <openssl/rand.h>
14 #include <openssl/ssl.h>
15
16 #include "handshake_util.h"
17 #include "test_config.h"
18 #include "test_state.h"
19 #include "../../crypto/internal.h"
20 #include "../../crypto/ube/vm_ube_detect.h"
21
22 using namespace bssl;
23
24 namespace {
25
26 ssize_t read_eintr(int fd, void *out, size_t len) {
27 ssize_t ret;
28 do {
29 ret = read(fd, out, len);
30 } while (ret < 0 && errno == EINTR);
31 return ret;
32 }
33
34 ssize_t write_eintr(int fd, const void *in, size_t len) {
35 ssize_t ret;
36 do {
37 ret = write(fd, in, len);
38 } while (ret < 0 && errno == EINTR);
39 return ret;
40 }
41
42 bool HandbackReady(SSL *ssl, int ret) {
43 return ret < 0 && SSL_get_error(ssl, ret) == SSL_ERROR_HANDBACK;
44 }
45
46 bool Handshaker(const TestConfig *config, int rfd, int wfd,
47 Span<const uint8_t> input, int control) {
48 UniquePtr<SSL_CTX> ctx = config->SetupCtx(/*old_ctx=*/nullptr);
49 if (!ctx) {
50 return false;
51 }
52 UniquePtr<SSL> ssl =
53 config->NewSSL(ctx.get(), /*session=*/nullptr, /*test_state=*/nullptr);
54 if (!ssl) {
55 fprintf(stderr, "Error creating SSL object in handshaker.\n");
56 ERR_print_errors_fp(stderr);
57 return false;
58 }
59
60 // Set |O_NONBLOCK| in order to break out of the loop when we hit
61 // |SSL_ERROR_WANT_READ|, so that we can send |kControlMsgWantRead| to the
62 // proxy.
63 if (fcntl(rfd, F_SETFL, O_NONBLOCK) != 0) {
64 perror("fcntl");
65 return false;
66 }
67 SSL_set_rfd(ssl.get(), rfd);
68 SSL_set_wfd(ssl.get(), wfd);
69
70 CBS cbs, handoff;
71 CBS_init(&cbs, input.data(), input.size());
72 if (!CBS_get_asn1_element(&cbs, &handoff, CBS_ASN1_SEQUENCE) ||
73 !DeserializeContextState(&cbs, ctx.get()) ||
74 !SetTestState(ssl.get(), TestState::Deserialize(&cbs, ctx.get())) ||
75 !GetTestState(ssl.get()) ||
76 !SSL_apply_handoff(ssl.get(), handoff)) {
77 fprintf(stderr, "Handoff application failed.\n");
78 return false;
79 }
80
81 int ret = 0;
82 for (;;) {
83 ret = CheckIdempotentError(
84 "SSL_do_handshake", ssl.get(),
85 [&]() -> int { return SSL_do_handshake(ssl.get()); });
86 if (SSL_get_error(ssl.get(), ret) == SSL_ERROR_WANT_READ) {
87 // Synchronize with the proxy, i.e. don't let the handshake continue until
88 // the proxy has sent more data.
89 char msg = kControlMsgWantRead;
90 if (write_eintr(control, &msg, 1) != 1 ||
91 read_eintr(control, &msg, 1) != 1 ||
92 msg != kControlMsgWriteCompleted) {
93 fprintf(stderr, "read via proxy failed\n");
94 return false;
95 }
96 continue;
97 }
98 if (!RetryAsync(ssl.get(), ret)) {
99 break;
100 }
101 }
102 if (!HandbackReady(ssl.get(), ret)) {
103 fprintf(stderr, "Handshaker: %s\n",
104 SSL_error_description(SSL_get_error(ssl.get(), ret)));
105 ERR_print_errors_fp(stderr);
106 return false;
107 }
108
109 ScopedCBB output;
110 CBB handback;
111 if (!CBB_init(output.get(), 1024) ||
112 !CBB_add_u24_length_prefixed(output.get(), &handback) ||
113 !SSL_serialize_handback(ssl.get(), &handback) ||
114 !SerializeContextState(ctx.get(), output.get()) ||
115 !GetTestState(ssl.get())->Serialize(output.get())) {
116 fprintf(stderr, "Handback serialisation failed.\n");
117 return false;
118 }
119
120 char msg = kControlMsgDone;
121 if (write_eintr(control, &msg, 1) == -1 ||
122 write_eintr(control, CBB_data(output.get()), CBB_len(output.get())) ==
123 -1) {
124 perror("write");
125 return false;
126 }
127 return true;
128 }
129
130 bool GenerateHandshakeHint(const TestConfig *config,
131 bssl::Span<const uint8_t> request, int control) {
132 // The handshake hint contains the ClientHello and the capabilities string.
133 CBS cbs = request;
134 CBS client_hello, capabilities;
135 if (!CBS_get_u24_length_prefixed(&cbs, &client_hello) ||
136 !CBS_get_u24_length_prefixed(&cbs, &capabilities) || //
137 CBS_len(&cbs) != 0) {
138 fprintf(stderr, "Handshaker: Could not parse hint request\n");
139 return false;
140 }
141
142 UniquePtr<SSL_CTX> ctx = config->SetupCtx(/*old_ctx=*/nullptr);
143 if (!ctx) {
144 return false;
145 }
146
147 UniquePtr<SSL> ssl =
148 config->NewSSL(ctx.get(), /*session=*/nullptr,
149 std::unique_ptr<TestState>(new TestState));
150 if (!ssl) {
151 fprintf(stderr, "Error creating SSL object in handshaker.\n");
152 ERR_print_errors_fp(stderr);
153 return false;
154 }
155
156 // TODO(davidben): When split handshakes is replaced, move this into |NewSSL|.
157 assert(config->is_server);
158 SSL_set_accept_state(ssl.get());
159
160 if (!SSL_request_handshake_hints(
161 ssl.get(), CBS_data(&client_hello), CBS_len(&client_hello),
162 CBS_data(&capabilities), CBS_len(&capabilities))) {
163 fprintf(stderr, "Handshaker: SSL_request_handshake_hints failed\n");
164 return false;
165 }
166
167 int ret = 0;
168 do {
169 ret = CheckIdempotentError("SSL_do_handshake", ssl.get(),
170 [&] { return SSL_do_handshake(ssl.get()); });
171 } while (RetryAsync(ssl.get(), ret));
172
173 if (ret > 0) {
174 fprintf(stderr, "Handshaker: handshake unexpectedly succeeded.\n");
175 return false;
176 }
177
178 if (SSL_get_error(ssl.get(), ret) != SSL_ERROR_HANDSHAKE_HINTS_READY) {
179 // Errors here may be expected if the test is testing a failing case. The
180 // shim should continue executing without a hint, so we report an error
181 // "successfully". This allows the shim to distinguish this from the other
182 // unexpected error cases.
183 //
184 // We intentionally avoid printing the error in this case, to avoid mixing
185 // up test expectations with errors from the shim.
186 char msg = kControlMsgError;
187 if (write_eintr(control, &msg, 1) == -1) {
188 return false;
189 }
190 return true;
191 }
192
193 bssl::ScopedCBB hints;
194 if (!CBB_init(hints.get(), 256) ||
195 !SSL_serialize_handshake_hints(ssl.get(), hints.get())) {
196 fprintf(stderr, "Handshaker: failed to serialize handshake hints\n");
197 return false;
198 }
199
200 char msg = kControlMsgDone;
201 if (write_eintr(control, &msg, 1) == -1 ||
202 write_eintr(control, CBB_data(hints.get()), CBB_len(hints.get())) == -1) {
203 perror("write");
204 return false;
205 }
206
207 return true;
208 }
209
210 int SignalError() {
211 const char msg = kControlMsgError;
212 if (write_eintr(kFdControl, &msg, 1) != 1) {
213 return 2;
214 }
215 return 1;
216 }
217
218 } // namespace
219
220 int main(int argc, char **argv) {
221 #if defined(OPENSSL_LINUX) && defined(AWSLC_VM_UBE_TESTING)
222 if (1 != HAZMAT_init_sysgenid_file()) {
223 abort();
224 }
225 #endif
226
227 TestConfig initial_config, resume_config, retry_config;
228 if (!ParseConfig(argc - 1, argv + 1, /*is_shim=*/false, &initial_config,
229 &resume_config, &retry_config)) {
230 return SignalError();
231 }
232 const TestConfig *config =
233 initial_config.handshaker_resume ? &resume_config : &initial_config;
234 #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
235 if (initial_config.handshaker_resume) {
236 // If the PRNG returns exactly the same values when trying to resume then a
237 // "random" session ID will happen to exactly match the session ID
238 // "randomly" generated on the initial connection. The client will thus
239 // incorrectly believe that the server is resuming.
240 uint8_t byte;
241 RAND_bytes(&byte, 1);
242 }
243 #endif // BORINGSSL_UNSAFE_DETERMINISTIC_MODE
244
245 // read() will return the entire message in one go, because it's a datagram
246 // socket.
247 constexpr size_t kBufSize = 1024 * 1024;
248 std::vector<uint8_t> request(kBufSize);
249 ssize_t len = read_eintr(kFdControl, request.data(), request.size());
250 if (len == -1) {
251 perror("read");
252 return 2;
253 }
254 request.resize(static_cast<size_t>(len));
255
256 if (config->handshake_hints) {
257 if (!GenerateHandshakeHint(config, request, kFdControl)) {
258 return SignalError();
259 }
260 } else {
261 if (!Handshaker(config, kFdProxyToHandshaker, kFdHandshakerToProxy,
262 request, kFdControl)) {
263 return SignalError();
264 }
265 }
266 return 0;
267 }