Debian Codegraph @

crt/s2n/bin/https.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 <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "api/s2n.h"
21 #include "error/s2n_errno.h"
22 #include "stuffer/s2n_stuffer.h"
23 #include "utils/s2n_safety.h"
24
25 #define STRING_LEN 1024
26 static char str_buffer[STRING_LEN];
27 static s2n_blocked_status blocked;
28
29 #define SEND(...) \
30 do { \
31 sprintf(str_buffer, __VA_ARGS__); \
32 POSIX_GUARD(s2n_send(conn, str_buffer, strlen(str_buffer), &blocked)); \
33 } while (0)
34
35 #define BUFFER(...) \
36 do { \
37 sprintf(str_buffer, __VA_ARGS__); \
38 POSIX_GUARD(s2n_stuffer_write_bytes(&stuffer, (const uint8_t *) str_buffer, strlen(str_buffer))); \
39 } while (0)
40
41 static int flush(uint32_t left, uint8_t *buffer, struct s2n_connection *conn, s2n_blocked_status *blocked_status)
42 {
43 uint32_t i = 0;
44 while (i < left) {
45 int out = s2n_send(conn, &buffer[i], left - i, blocked_status);
46 if (out < 0) {
47 fprintf(stderr, "Error writing to connection: '%s'\n", s2n_strerror(s2n_errno, "EN"));
48 s2n_print_stacktrace(stdout);
49 return S2N_FAILURE;
50 }
51 i += out;
52 }
53
54 return S2N_SUCCESS;
55 }
56
57 #define HEADERS(length) \
58 do { \
59 SEND("HTTP/1.1 200 OK\r\n"); \
60 SEND("Content-Length: %u\r\n", length); \
61 SEND("\r\n"); \
62 } while (0)
63
64 /* In bench mode, we send some binary output */
65 int bench_handler(struct s2n_connection *conn, uint32_t bench)
66 {
67 HEADERS(bench);
68 fprintf(stdout, "Sending %u bytes...\n", bench);
69
70 uint8_t big_buff[65536] = { 0 };
71 uint32_t len = sizeof(big_buff);
72 uint32_t bytes_remaining = bench;
73
74 while (bytes_remaining) {
75 uint32_t buffer_remaining = bytes_remaining < len ? bytes_remaining : len;
76 POSIX_GUARD(flush(buffer_remaining, big_buff, conn, &blocked));
77 bytes_remaining -= buffer_remaining;
78 }
79
80 fprintf(stdout, "Done. Closing connection.\n\n");
81
82 return 0;
83 }
84
85 /*
86 * simple https handler that allows https clients to connect
87 * but currently does not do any user parsing
88 */
89 int https(struct s2n_connection *conn, uint32_t bench)
90 {
91 if (bench) {
92 return bench_handler(conn, bench);
93 }
94
95 DEFER_CLEANUP(struct s2n_stuffer stuffer, s2n_stuffer_free);
96 POSIX_GUARD(s2n_stuffer_growable_alloc(&stuffer, 1024));
97
98 BUFFER("<html><body><h1>Hello from s2n server</h1><pre>");
99
100 BUFFER("Client hello version: %d\n", s2n_connection_get_client_hello_version(conn));
101 BUFFER("Client protocol version: %d\n", s2n_connection_get_client_protocol_version(conn));
102 BUFFER("Server protocol version: %d\n", s2n_connection_get_server_protocol_version(conn));
103 BUFFER("Actual protocol version: %d\n", s2n_connection_get_actual_protocol_version(conn));
104
105 if (s2n_get_server_name(conn)) {
106 BUFFER("Server name: %s\n", s2n_get_server_name(conn));
107 }
108
109 if (s2n_get_application_protocol(conn)) {
110 BUFFER("Application protocol: %s\n", s2n_get_application_protocol(conn));
111 }
112
113 const char *group_name = NULL;
114 if (s2n_connection_get_key_exchange_group(conn, &group_name) == 0) {
115 BUFFER("Key Exchange Group: %s\n", group_name);
116 }
117 BUFFER("Cipher negotiated: %s\n", s2n_connection_get_cipher(conn));
118 BUFFER("Session resumption: %s\n", s2n_connection_is_session_resumed(conn) ? "true" : "false");
119
120 uint32_t content_length = s2n_stuffer_data_available(&stuffer);
121
122 uint8_t *content = s2n_stuffer_raw_read(&stuffer, content_length);
123 POSIX_ENSURE_REF(content);
124
125 HEADERS(content_length);
126 POSIX_GUARD(flush(content_length, content, conn, &blocked));
127
128 return S2N_SUCCESS;
129 }