Debian Codegraph @

crt/s2n/bin/echo.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <inttypes.h>
18 #include <netdb.h>
19 #include <openssl/rsa.h>
20 #include <openssl/x509.h>
21 #include <poll.h>
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
24 #include <sys/select.h>
25 #include <unistd.h>
26
27 #include "api/s2n.h"
28 #include "api/unstable/fingerprint.h"
29 #include "api/unstable/renegotiate.h"
30 #include "common.h"
31 #include "crypto/s2n_pkey.h"
32 #include "error/s2n_errno.h"
33 #include "utils/s2n_safety.h"
34
35 #define STDIO_BUFSIZE 10240
36
37 const char *sig_alg_strs[] = {
38 [S2N_TLS_SIGNATURE_ANONYMOUS] = "None",
39 [S2N_TLS_SIGNATURE_RSA] = "RSA",
40 [S2N_TLS_SIGNATURE_ECDSA] = "ECDSA",
41 [S2N_TLS_SIGNATURE_RSA_PSS_RSAE] = "RSA-PSS-RSAE",
42 [S2N_TLS_SIGNATURE_RSA_PSS_PSS] = "RSA-PSS-PSS",
43 [S2N_TLS_SIGNATURE_MLDSA] = "MLDSA",
44 };
45
46 const char *sig_hash_strs[] = {
47 [S2N_TLS_HASH_NONE] = "None",
48 [S2N_TLS_HASH_MD5] = "MD5",
49 [S2N_TLS_HASH_SHA1] = "SHA1",
50 [S2N_TLS_HASH_SHA224] = "SHA224",
51 [S2N_TLS_HASH_SHA256] = "SHA256",
52 [S2N_TLS_HASH_SHA384] = "SHA384",
53 [S2N_TLS_HASH_SHA512] = "SHA512",
54 [S2N_TLS_HASH_MD5_SHA1] = "MD5_SHA1",
55 };
56
57 /* Careful: don't change this without updating the integration tests that check for it!
58 * Negative cases may stop working but not fail when this is updated.
59 */
60 const char *pq_enabled_note = "PQ key exchange enabled";
61
62 void print_s2n_error(const char *app_error)
63 {
64 fprintf(stderr, "[%d] %s: '%s' : '%s'\n", getpid(), app_error, s2n_strerror(s2n_errno, "EN"),
65 s2n_strerror_debug(s2n_errno, "EN"));
66 }
67
68 /* Poll the given file descriptor for an event determined by the blocked status */
69 int wait_for_event(int fd, s2n_blocked_status blocked)
70 {
71 struct pollfd reader = { .fd = fd, .events = 0 };
72
73 switch (blocked) {
74 case S2N_NOT_BLOCKED:
75 return S2N_SUCCESS;
76 case S2N_BLOCKED_ON_READ:
77 reader.events |= POLLIN;
78 break;
79 case S2N_BLOCKED_ON_WRITE:
80 reader.events |= POLLOUT;
81 break;
82 case S2N_BLOCKED_ON_EARLY_DATA:
83 case S2N_BLOCKED_ON_APPLICATION_INPUT:
84 /* This case is not encountered by the s2nc/s2nd applications,
85 * but is detected for completeness */
86 return S2N_SUCCESS;
87 }
88
89 if (poll(&reader, 1, -1) < 0) {
90 fprintf(stderr, "Failed to poll connection: %s\n", strerror(errno));
91 S2N_ERROR_PRESERVE_ERRNO();
92 }
93
94 return S2N_SUCCESS;
95 }
96
97 int early_data_recv(struct s2n_connection *conn)
98 {
99 uint32_t max_early_data_size = 0;
100 GUARD_RETURN(s2n_connection_get_max_early_data_size(conn, &max_early_data_size), "Error getting max early data size");
101 if (max_early_data_size == 0) {
102 return S2N_SUCCESS;
103 }
104
105 ssize_t total_data_recv = 0;
106 ssize_t data_recv = 0;
107 bool server_success = 0;
108 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
109 uint8_t *early_data_received = (uint8_t *) malloc(max_early_data_size);
110 GUARD_EXIT_NULL(early_data_received);
111
112 do {
113 server_success = (s2n_recv_early_data(conn, early_data_received + total_data_recv,
114 max_early_data_size - total_data_recv, &data_recv, &blocked)
115 >= S2N_SUCCESS);
116 total_data_recv += data_recv;
117 } while (!server_success);
118
119 if (total_data_recv > 0) {
120 fprintf(stdout, "Early Data received: ");
121 for (ssize_t i = 0; i < total_data_recv; i++) {
122 fprintf(stdout, "%c", early_data_received[i]);
123 }
124 fprintf(stdout, "\n");
125 }
126
127 free(early_data_received);
128
129 return S2N_SUCCESS;
130 }
131
132 int early_data_send(struct s2n_connection *conn, uint8_t *data, uint32_t len)
133 {
134 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
135 ssize_t total_data_sent = 0;
136 ssize_t data_sent = 0;
137 bool client_success = 0;
138 do {
139 client_success = (s2n_send_early_data(conn, data + total_data_sent,
140 len - total_data_sent, &data_sent, &blocked)
141 >= S2N_SUCCESS);
142 total_data_sent += data_sent;
143 } while (total_data_sent < len && !client_success);
144
145 return S2N_SUCCESS;
146 }
147
148 int print_connection_info(struct s2n_connection *conn)
149 {
150 int client_hello_version = 0;
151 int client_protocol_version = 0;
152 int server_protocol_version = 0;
153 int actual_protocol_version = 0;
154
155 if ((client_hello_version = s2n_connection_get_client_hello_version(conn)) < 0) {
156 fprintf(stderr, "Could not get client hello version\n");
157 POSIX_BAIL(S2N_ERR_CLIENT_HELLO_VERSION);
158 }
159 if ((client_protocol_version = s2n_connection_get_client_protocol_version(conn)) < 0) {
160 fprintf(stderr, "Could not get client protocol version\n");
161 POSIX_BAIL(S2N_ERR_CLIENT_PROTOCOL_VERSION);
162 }
163 if ((server_protocol_version = s2n_connection_get_server_protocol_version(conn)) < 0) {
164 fprintf(stderr, "Could not get server protocol version\n");
165 POSIX_BAIL(S2N_ERR_SERVER_PROTOCOL_VERSION);
166 }
167 if ((actual_protocol_version = s2n_connection_get_actual_protocol_version(conn)) < 0) {
168 fprintf(stderr, "Could not get actual protocol version\n");
169 POSIX_BAIL(S2N_ERR_ACTUAL_PROTOCOL_VERSION);
170 }
171 printf("CONNECTED:\n");
172 printf("Handshake: %s\n", s2n_connection_get_handshake_type_name(conn));
173 printf("Client hello version: %d\n", client_hello_version);
174 printf("Client protocol version: %d\n", client_protocol_version);
175 printf("Server protocol version: %d\n", server_protocol_version);
176 printf("Actual protocol version: %d\n", actual_protocol_version);
177
178 if (s2n_get_server_name(conn)) {
179 printf("Server name: %s\n", s2n_get_server_name(conn));
180 }
181
182 if (s2n_get_application_protocol(conn)) {
183 printf("Application protocol: %s\n", s2n_get_application_protocol(conn));
184 }
185
186 const char *kem = s2n_connection_get_kem_name(conn);
187 if (strcmp(kem, "NONE") != 0) {
188 printf("Legacy TLS1.2 KEM: %s (%s, but the use of PQ key exchange with "
189 "TLS1.2 was experimental and is neither stable nor on a path to "
190 "standardization. The TLS1.3 version should be used instead: "
191 "https://aws.github.io/s2n-tls/usage-guide/ch15-post-quantum.html)\n",
192 kem, pq_enabled_note);
193 }
194
195 const char *kem_group = s2n_connection_get_kem_group_name(conn);
196 if (strcmp(kem_group, "NONE") != 0) {
197 printf("KEM Group: %s (%s)\n", kem_group, pq_enabled_note);
198 } else {
199 printf("Curve: %s\n", s2n_connection_get_curve(conn));
200 }
201
202 uint32_t length = 0;
203 const uint8_t *status = s2n_connection_get_ocsp_response(conn, &length);
204 if (status && length > 0) {
205 printf("OCSP response received, length %u\n", length);
206 }
207
208 printf("Cipher negotiated: %s\n", s2n_connection_get_cipher(conn));
209
210 s2n_tls_signature_algorithm server_sig_alg = 0, client_sig_alg = 0;
211 s2n_tls_hash_algorithm server_sig_hash = 0, client_sig_hash = 0;
212 GUARD_EXIT(s2n_connection_get_selected_signature_algorithm(conn, &server_sig_alg),
213 "Error getting server signature algorithm");
214 GUARD_EXIT(s2n_connection_get_selected_client_cert_signature_algorithm(conn, &client_sig_alg),
215 "Error getting client signature algorithm");
216 GUARD_EXIT(s2n_connection_get_selected_digest_algorithm(conn, &server_sig_hash),
217 "Error getting server signature hash algorithm");
218 GUARD_EXIT(s2n_connection_get_selected_client_cert_digest_algorithm(conn, &client_sig_hash),
219 "Error getting client signature hash algorithm");
220 printf("Server signature negotiated: %s+%s\n", sig_alg_strs[server_sig_alg], sig_hash_strs[server_sig_hash]);
221 if (client_sig_alg != S2N_TLS_SIGNATURE_ANONYMOUS) {
222 printf("Client signature negotiated: %s+%s\n", sig_alg_strs[client_sig_alg], sig_hash_strs[client_sig_hash]);
223 }
224
225 bool session_resumed = s2n_connection_is_session_resumed(conn);
226 if (session_resumed) {
227 printf("Resumed session\n");
228 }
229
230 uint16_t identity_length = 0;
231 GUARD_EXIT(s2n_connection_get_negotiated_psk_identity_length(conn, &identity_length), "Error getting negotiated psk identity length from the connection\n");
232 if (identity_length != 0 && !session_resumed) {
233 uint8_t *identity = (uint8_t *) malloc(identity_length);
234 GUARD_EXIT_NULL(identity);
235 GUARD_EXIT(s2n_connection_get_negotiated_psk_identity(conn, identity, identity_length), "Error getting negotiated psk identity from the connection\n");
236 printf("Negotiated PSK identity: %.*s\n", identity_length, identity);
237 free(identity);
238 }
239
240 s2n_early_data_status_t early_data_status = (s2n_early_data_status_t) 0;
241 GUARD_EXIT(s2n_connection_get_early_data_status(conn, &early_data_status), "Error getting early data status");
242 const char *status_str = NULL;
243 switch (early_data_status) {
244 case S2N_EARLY_DATA_STATUS_OK:
245 status_str = "IN PROGRESS";
246 break;
247 case S2N_EARLY_DATA_STATUS_NOT_REQUESTED:
248 status_str = "NOT REQUESTED";
249 break;
250 case S2N_EARLY_DATA_STATUS_REJECTED:
251 status_str = "REJECTED";
252 break;
253 case S2N_EARLY_DATA_STATUS_END:
254 status_str = "ACCEPTED";
255 break;
256 }
257 GUARD_EXIT_NULL(status_str);
258 printf("Early Data status: %s\n", status_str);
259
260 struct s2n_client_hello *ch = s2n_connection_get_client_hello(conn);
261 if (ch && client_hello_version > S2N_SSLv2) {
262 uint8_t ja3[16] = { 0 };
263 uint32_t ja3_size = 0, str_size = 0;
264 GUARD_EXIT(s2n_client_hello_get_fingerprint_hash(ch, S2N_FINGERPRINT_JA3,
265 sizeof(ja3), ja3, &ja3_size, &str_size),
266 "Error calculating JA3");
267 printf("JA3: ");
268 for (size_t i = 0; i < ja3_size; i++) {
269 printf("%02x", ja3[i]);
270 }
271 printf("\n");
272 }
273
274 printf("Wire bytes in: %" PRIu64 "\n", s2n_connection_get_wire_bytes_in(conn));
275 printf("Wire bytes out: %" PRIu64 "\n", s2n_connection_get_wire_bytes_out(conn));
276
277 return 0;
278 }
279
280 int negotiate(struct s2n_connection *conn, int fd)
281 {
282 s2n_blocked_status blocked;
283 while (s2n_negotiate(conn, &blocked) != S2N_SUCCESS) {
284 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
285 fprintf(stderr, "Failed to negotiate: '%s'. %s\n",
286 s2n_strerror(s2n_errno, "EN"),
287 s2n_strerror_debug(s2n_errno, "EN"));
288 if (s2n_error_get_type(s2n_errno) == S2N_ERR_T_ALERT) {
289 fprintf(stderr, "Alert: %d\n",
290 s2n_connection_get_alert(conn));
291 }
292 S2N_ERROR_PRESERVE_ERRNO();
293 }
294
295 if (wait_for_event(fd, blocked) != S2N_SUCCESS) {
296 S2N_ERROR_PRESERVE_ERRNO();
297 }
298 }
299
300 print_connection_info(conn);
301
302 printf("s2n is ready\n");
303 return 0;
304 }
305
306 int renegotiate(struct s2n_connection *conn, int fd)
307 {
308 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
309 uint8_t buffer[STDIO_BUFSIZE] = { 0 };
310 ssize_t data_read = 0;
311
312 GUARD_RETURN(s2n_renegotiate_wipe(conn), "Unable to prepare connection for renegotiate");
313 GUARD_RETURN(s2n_connection_set_client_auth_type(conn, S2N_CERT_AUTH_OPTIONAL), "Error setting ClientAuth optional");
314
315 fprintf(stdout, "RENEGOTIATE\n");
316 fflush(stdout);
317
318 while (s2n_renegotiate(conn, buffer, sizeof(buffer), &data_read, &blocked) != S2N_SUCCESS) {
319 uint8_t *data_ptr = buffer;
320 while (data_read > 0) {
321 ssize_t data_written = write(STDOUT_FILENO, data_ptr, data_read);
322 GUARD_RETURN(data_written, "Error writing to stdout\n");
323 data_read -= data_written;
324 data_ptr += data_written;
325 }
326
327 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
328 fprintf(stderr, "Failed to renegotiate: '%s'. %s\n", s2n_strerror(s2n_errno, NULL),
329 s2n_strerror_debug(s2n_errno, NULL));
330 if (s2n_error_get_type(s2n_errno) == S2N_ERR_T_ALERT) {
331 fprintf(stderr, "Alert: %d\n", s2n_connection_get_alert(conn));
332 }
333 return S2N_FAILURE;
334 }
335
336 GUARD_RETURN(wait_for_event(fd, blocked), "Error polling IO for renegotiate");
337 }
338
339 print_connection_info(conn);
340 printf("s2n is ready, again\n");
341 return S2N_SUCCESS;
342 }
343
344 void send_data(struct s2n_connection *conn, int sockfd, const char *data, uint64_t len, s2n_blocked_status *blocked)
345 {
346 uint64_t bytes_remaining = len;
347 const char *data_ptr = data;
348 do {
349 ssize_t send_len = MIN(bytes_remaining, SSIZE_MAX);
350 ssize_t bytes_written = s2n_send(conn, data_ptr, send_len, blocked);
351 if (bytes_written < 0) {
352 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
353 fprintf(stderr, "Error writing to connection: '%s'\n",
354 s2n_strerror(s2n_errno, "EN"));
355 exit(1);
356 }
357
358 GUARD_EXIT(wait_for_event(sockfd, *blocked), "Unable to send data");
359 continue;
360 }
361
362 bytes_remaining -= bytes_written;
363 data_ptr += bytes_written;
364
365 } while (bytes_remaining > 0);
366 }
367
368 int echo(struct s2n_connection *conn, int sockfd, bool *stop_echo)
369 {
370 struct pollfd readers[2];
371
372 readers[0].fd = sockfd;
373 readers[0].events = POLLIN;
374 readers[1].fd = STDIN_FILENO;
375 readers[1].events = POLLIN;
376
377 /* Reset errno so that we can't inherit the errno == EINTR exit condition. */
378 errno = 0;
379
380 /* Act as a simple proxy between stdin and the SSL connection */
381 int p = 0;
382 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
383 do {
384 /* echo will send and receive Application Data back and forth between
385 * client and server, until stop_echo is true or stdin EOF is reached. */
386 while (!(*stop_echo) && (p = poll(readers, 2, -1)) > 0) {
387 char buffer[STDIO_BUFSIZE];
388 ssize_t bytes_read = 0;
389
390 if (readers[0].revents & POLLIN) {
391 s2n_errno = S2N_ERR_T_OK;
392 bytes_read = s2n_recv(conn, buffer, STDIO_BUFSIZE, &blocked);
393 if (bytes_read == 0) {
394 return 0;
395 }
396 if (bytes_read < 0) {
397 switch (s2n_error_get_type(s2n_errno)) {
398 case S2N_ERR_T_BLOCKED:
399 /* Wait until poll tells us data is ready */
400 continue;
401 case S2N_ERR_T_ALERT:
402 fprintf(stderr, "Received alert: %d\n", s2n_connection_get_alert(conn));
403 break;
404 default:
405 fprintf(stderr, "Error reading from connection: '%s'\n", s2n_strerror(s2n_errno, "EN"));
406 break;
407 }
408 exit(1);
409 }
410
411 char *buf_ptr = buffer;
412 do {
413 ssize_t bytes_written = write(STDOUT_FILENO, buf_ptr, bytes_read);
414 if (bytes_written < 0) {
415 fprintf(stderr, "Error writing to stdout\n");
416 exit(1);
417 }
418
419 bytes_read -= bytes_written;
420 buf_ptr += bytes_written;
421 } while (bytes_read > 0);
422 }
423
424 if (readers[1].revents & POLLIN) {
425 size_t bytes_available = 0;
426
427 if (ioctl(STDIN_FILENO, FIONREAD, &bytes_available) < 0) {
428 bytes_available = 1;
429 }
430
431 do {
432 /* We can only read as much data as we have space for. So it may
433 * take a couple loops to empty stdin. */
434 size_t bytes_to_read = bytes_available;
435 if (bytes_available > sizeof(buffer)) {
436 bytes_to_read = sizeof(buffer);
437 }
438
439 bytes_read = read(STDIN_FILENO, buffer, bytes_to_read);
440 if (bytes_read < 0 && errno != EINTR) {
441 fprintf(stderr, "Error reading from stdin\n");
442 exit(1);
443 }
444 if (bytes_read == 0) {
445 fprintf(stderr, "Exiting on stdin EOF\n");
446 return 0;
447 }
448 bytes_available -= bytes_read;
449
450 /* We may not be able to write all the data we read in one shot, so
451 * keep sending until we have cleared our buffer. */
452 send_data(conn, sockfd, buffer, bytes_read, &blocked);
453
454 } while (bytes_available || blocked);
455 }
456
457 if (readers[1].revents & POLLHUP) {
458 /* The stdin pipe hanged up, and we've handled all read from it above */
459 return 0;
460 }
461
462 if (readers[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
463 fprintf(stderr, "Error polling from socket: err=%d hup=%d nval=%d\n",
464 (readers[0].revents & POLLERR) ? 1 : 0,
465 (readers[0].revents & POLLHUP) ? 1 : 0,
466 (readers[0].revents & POLLNVAL) ? 1 : 0);
467 POSIX_BAIL(S2N_ERR_POLLING_FROM_SOCKET);
468 }
469
470 if (readers[1].revents & (POLLERR | POLLNVAL)) {
471 fprintf(stderr, "Error polling from socket: err=%d nval=%d\n",
472 (readers[1].revents & POLLERR) ? 1 : 0,
473 (readers[1].revents & POLLNVAL) ? 1 : 0);
474 POSIX_BAIL(S2N_ERR_POLLING_FROM_SOCKET);
475 }
476 }
477 } while (p < 0 && errno == EINTR);
478
479 return 0;
480 }