2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
41 * This sample code can use three possible certificate chains:
42 * -- A full-RSA chain (server key is RSA, certificates are signed with RSA)
43 * -- A full-EC chain (server key is EC, certificates are signed with ECDSA)
44 * -- A mixed chain (server key is EC, certificates are signed with RSA)
46 * The macros below define which chain is selected. This impacts the list
47 * of supported cipher suites.
50 #if !(SERVER_RSA || SERVER_EC || SERVER_MIXED)
53 #define SERVER_MIXED 0
57 #include "chain-rsa.h"
65 #include "chain-ec+rsa.h"
69 #error Must use one of RSA, EC or MIXED chains.
73 * Create a server socket bound to the specified host and port. If 'host'
74 * is NULL, this will bind "generically" (all addresses).
76 * Returned value is the server socket descriptor, or -1 on error.
79 host_bind(const char *host
, const char *port
)
81 struct addrinfo hints
, *si
, *p
;
85 memset(&hints
, 0, sizeof hints
);
86 hints
.ai_family
= PF_UNSPEC
;
87 hints
.ai_socktype
= SOCK_STREAM
;
88 err
= getaddrinfo(host
, port
, &hints
, &si
);
90 fprintf(stderr
, "ERROR: getaddrinfo(): %s\n",
95 for (p
= si
; p
!= NULL
; p
= p
->ai_next
) {
97 struct sockaddr_in sa4
;
98 struct sockaddr_in6 sa6
;
101 char tmp
[INET6_ADDRSTRLEN
+ 50];
104 sa
= (struct sockaddr
*)p
->ai_addr
;
105 if (sa
->sa_family
== AF_INET
) {
106 sa4
= *(struct sockaddr_in
*)sa
;
107 sa
= (struct sockaddr
*)&sa4
;
109 addr
= &sa4
.sin_addr
;
111 sa4
.sin_addr
.s_addr
= INADDR_ANY
;
113 } else if (sa
->sa_family
== AF_INET6
) {
114 sa6
= *(struct sockaddr_in6
*)sa
;
115 sa
= (struct sockaddr
*)&sa6
;
117 addr
= &sa6
.sin6_addr
;
119 sa6
.sin6_addr
= in6addr_any
;
123 sa_len
= p
->ai_addrlen
;
126 inet_ntop(p
->ai_family
, addr
, tmp
, sizeof tmp
);
128 sprintf(tmp
, "<unknown family: %d>",
131 fprintf(stderr
, "binding to: %s\n", tmp
);
132 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
138 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof opt
);
140 setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &opt
, sizeof opt
);
141 if (bind(fd
, sa
, sa_len
) < 0) {
150 fprintf(stderr
, "ERROR: failed to bind\n");
154 if (listen(fd
, 5) < 0) {
159 fprintf(stderr
, "bound.\n");
164 * Accept a single client on the provided server socket. This is blocking.
165 * On error, this returns -1.
168 accept_client(int server_fd
)
173 char tmp
[INET6_ADDRSTRLEN
+ 50];
177 fd
= accept(server_fd
, &sa
, &sa_len
);
183 switch (sa
.sa_family
) {
185 name
= inet_ntop(AF_INET
,
186 &((struct sockaddr_in
*)&sa
)->sin_addr
,
190 name
= inet_ntop(AF_INET
,
191 &((struct sockaddr_in
*)&sa
)->sin_addr
,
196 sprintf(tmp
, "<unknown: %lu>", (unsigned long)sa
.sa_family
);
199 fprintf(stderr
, "accepting connection from: %s\n", name
);
204 * Low-level data read callback for the simplified SSL I/O API.
207 sock_read(void *ctx
, unsigned char *buf
, size_t len
)
212 rlen
= read(*(int *)ctx
, buf
, len
);
214 if (rlen
< 0 && errno
== EINTR
) {
224 * Low-level data write callback for the simplified SSL I/O API.
227 sock_write(void *ctx
, const unsigned char *buf
, size_t len
)
232 wlen
= write(*(int *)ctx
, buf
, len
);
234 if (wlen
< 0 && errno
== EINTR
) {
244 * Sample HTTP response to send.
246 static const char *HTTP_RES
=
247 "HTTP/1.0 200 OK\r\n"
248 "Content-Length: 46\r\n"
249 "Connection: close\r\n"
250 "Content-Type: text/html; charset=iso-8859-1\r\n"
259 * Main program: this is a simple program that expects 1 argument: a
260 * port number. This will start a simple network server on that port,
261 * that expects incoming SSL clients. It handles only one client at a
262 * time (handling several would require threads, sub-processes, or
263 * multiplexing with select()/poll(), all of which being possible).
265 * For each client, the server will wait for two successive newline
266 * characters (ignoring CR characters, so CR+LF is accepted), then
267 * produce a sample static HTTP response. This is very crude, but
268 * sufficient for explanatory purposes.
271 main(int argc
, char *argv
[])
282 * Open the server socket.
284 fd
= host_bind(NULL
, port
);
290 * Process each client, one at a time.
294 br_ssl_server_context sc
;
295 unsigned char iobuf
[BR_SSL_BUFSIZE_BIDI
];
296 br_sslio_context ioc
;
299 cfd
= accept_client(fd
);
305 * Initialise the context with the cipher suites and
306 * algorithms. This depends on the server key type
307 * (and, for EC keys, the signature algorithm used by
308 * the CA to sign the server's certificate).
310 * Depending on the defined macros, we may select one of
311 * the "minimal" profiles. Key exchange algorithm depends
313 * RSA key: RSA or ECDHE_RSA
314 * EC key, cert signed with ECDSA: ECDH_ECDSA or ECDHE_ECDSA
315 * EC key, cert signed with RSA: ECDH_RSA or ECDHE_ECDSA
318 #if SERVER_PROFILE_MIN_FS
319 br_ssl_server_init_mine2g(&sc
, CHAIN
, CHAIN_LEN
, &SKEY
);
320 #elif SERVER_PROFILE_MIN_NOFS
321 br_ssl_server_init_minr2g(&sc
, CHAIN
, CHAIN_LEN
, &SKEY
);
323 br_ssl_server_init_full_rsa(&sc
, CHAIN
, CHAIN_LEN
, &SKEY
);
326 #if SERVER_PROFILE_MIN_FS
327 br_ssl_server_init_minf2g(&sc
, CHAIN
, CHAIN_LEN
, &SKEY
);
328 #elif SERVER_PROFILE_MIN_NOFS
329 br_ssl_server_init_minv2g(&sc
, CHAIN
, CHAIN_LEN
, &SKEY
);
331 br_ssl_server_init_full_ec(&sc
, CHAIN
, CHAIN_LEN
,
332 BR_KEYTYPE_EC
, &SKEY
);
334 #else /* SERVER_MIXED */
335 #if SERVER_PROFILE_MIN_FS
336 br_ssl_server_init_minf2g(&sc
, CHAIN
, CHAIN_LEN
, &SKEY
);
337 #elif SERVER_PROFILE_MIN_NOFS
338 br_ssl_server_init_minu2g(&sc
, CHAIN
, CHAIN_LEN
, &SKEY
);
340 br_ssl_server_init_full_ec(&sc
, CHAIN
, CHAIN_LEN
,
341 BR_KEYTYPE_RSA
, &SKEY
);
345 * Set the I/O buffer to the provided array. We
346 * allocated a buffer large enough for full-duplex
347 * behaviour with all allowed sizes of SSL records,
348 * hence we set the last argument to 1 (which means
349 * "split the buffer into separate input and output
352 br_ssl_engine_set_buffer(&sc
.eng
, iobuf
, sizeof iobuf
, 1);
355 * Reset the server context, for a new handshake.
357 br_ssl_server_reset(&sc
);
360 * Initialise the simplified I/O wrapper context.
362 br_sslio_init(&ioc
, &sc
.eng
, sock_read
, &cfd
, sock_write
, &cfd
);
365 * Read bytes until two successive LF (or CR+LF) are received.
371 if (br_sslio_read(&ioc
, &x
, 1) < 0) {
388 * Write a response and close the connection.
390 br_sslio_write_all(&ioc
, HTTP_RES
, strlen(HTTP_RES
));
391 br_sslio_close(&ioc
);
394 err
= br_ssl_engine_last_error(&sc
.eng
);
396 fprintf(stderr
, "SSL closed (correctly).\n");
398 fprintf(stderr
, "SSL error: %d\n", err
);