EMS/lib/libssl/share/man/man7/ossl-guide-tls-server-block...

484 lines
18 KiB
Plaintext

.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "OSSL-GUIDE-TLS-SERVER-BLOCK 7ossl"
.TH OSSL-GUIDE-TLS-SERVER-BLOCK 7ossl "2024-10-22" "3.4.0" "OpenSSL"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
ossl\-guide\-tls\-server\-block
\&\- OpenSSL Guide: Writing a simple blocking TLS server
.SH "SIMPLE BLOCKING TLS SERVER EXAMPLE"
.IX Header "SIMPLE BLOCKING TLS SERVER EXAMPLE"
This page will present various source code samples demonstrating how to write a
simple, non-concurrent, \s-1TLS\s0 \*(L"echo\*(R" server application which accepts one client
connection at a time, echoing input from the client back to the same client.
Once the current client disconnects, the next client connection is accepted.
.PP
Both the acceptor socket and client connections are \*(L"blocking\*(R". A more typical
server might use nonblocking sockets with an event loop and callbacks for I/O
events.
.PP
The complete source code for this example blocking \s-1TLS\s0 server is available in
the \fBdemos/guide\fR directory of the OpenSSL source distribution in the file
\&\fBtls\-server\-block.c\fR. It is also available online at
<https://github.com/openssl/openssl/blob/master/demos/guide/tls\-server\-block.c>.
.PP
We assume that you already have OpenSSL installed on your system; that you
already have some fundamental understanding of OpenSSL concepts and \s-1TLS\s0 (see
\&\fBossl\-guide\-libraries\-introduction\fR\|(7) and \fBossl\-guide\-tls\-introduction\fR\|(7));
and that you know how to write and build C code and link it against the
libcrypto and libssl libraries that are provided by OpenSSL. It also assumes
that you have a basic understanding of \s-1TCP/IP\s0 and sockets.
.SS "Creating the \s-1SSL_CTX\s0 and \s-1SSL\s0 objects"
.IX Subsection "Creating the SSL_CTX and SSL objects"
The first step is to create an \fB\s-1SSL_CTX\s0\fR object for our server. We use the
\&\fBSSL_CTX_new\fR\|(3) function for this purpose. We could alternatively use
\&\fBSSL_CTX_new_ex\fR\|(3) if we want to associate the \fB\s-1SSL_CTX\s0\fR with a particular
\&\fB\s-1OSSL_LIB_CTX\s0\fR (see \fBossl\-guide\-libraries\-introduction\fR\|(7) to learn about
\&\fB\s-1OSSL_LIB_CTX\s0\fR). We pass as an argument the return value of the function
\&\fBTLS_server_method\fR\|(3). You should use this method whenever you are writing a
\&\s-1TLS\s0 server. This method will automatically use \s-1TLS\s0 version negotiation to select
the highest version of the protocol that is mutually supported by both the
server and the client.
.PP
.Vb 9
\& /*
\& * An SSL_CTX holds shared configuration information for multiple
\& * subsequent per\-client SSL connections.
\& */
\& ctx = SSL_CTX_new(TLS_server_method());
\& if (ctx == NULL) {
\& ERR_print_errors_fp(stderr);
\& errx(res, "Failed to create server SSL_CTX");
\& }
.Ve
.PP
We would also like to restrict the \s-1TLS\s0 versions that we are willing to accept to
TLSv1.2 or above. \s-1TLS\s0 protocol versions earlier than that are generally to be
avoided where possible. We can do that using
\&\fBSSL_CTX_set_min_proto_version\fR\|(3):
.PP
.Vb 9
\& /*
\& * TLS versions older than TLS 1.2 are deprecated by IETF and SHOULD
\& * be avoided if possible.
\& */
\& if (!SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION)) {
\& SSL_CTX_free(ctx);
\& ERR_print_errors_fp(stderr);
\& errx(res, "Failed to set the minimum TLS protocol version");
\& }
.Ve
.PP
Next we configure some option flags, see \fBSSL_CTX_set_options\fR\|(3) for details:
.PP
.Vb 6
\& /*
\& * Tolerate clients hanging up without a TLS "shutdown". Appropriate in all
\& * application protocols which perform their own message "framing", and
\& * don\*(Aqt rely on TLS to defend against "truncation" attacks.
\& */
\& opts = SSL_OP_IGNORE_UNEXPECTED_EOF;
\&
\& /*
\& * Block potential CPU\-exhaustion attacks by clients that request frequent
\& * renegotiation. This is of course only effective if there are existing
\& * limits on initial full TLS handshake or connection rates.
\& */
\& opts |= SSL_OP_NO_RENEGOTIATION;
\&
\& /*
\& * Most servers elect to use their own cipher preference rather than that of
\& * the client.
\& */
\& opts |= SSL_OP_CIPHER_SERVER_PREFERENCE;
\&
\& /* Apply the selection options */
\& SSL_CTX_set_options(ctx, opts);
.Ve
.PP
Servers need a private key and certificate. Though anonymous ciphers (no
server certificate) are possible in \s-1TLS 1.2,\s0 they are rarely applicable, and
are not currently defined for \s-1TLS 1.3.\s0 Additional intermediate issuer \s-1CA\s0
certificates are often also required, and both the server (end-entity or \s-1EE\s0)
certificate and the issuer (\*(L"chain\*(R") certificates are most easily configured in
a single \*(L"chain file\*(R". Below we load such a chain file (the \s-1EE\s0 certificate
must appear first), and then load the corresponding private key, checking that
it matches the server certificate. No checks are performed to check the
integrity of the chain (\s-1CA\s0 signatures or certificate expiration dates, for
example).
.PP
.Vb 10
\& /*
\& * Load the server\*(Aqs certificate *chain* file (PEM format), which includes
\& * not only the leaf (end\-entity) server certificate, but also any
\& * intermediate issuer\-CA certificates. The leaf certificate must be the
\& * first certificate in the file.
\& *
\& * In advanced use\-cases this can be called multiple times, once per public
\& * key algorithm for which the server has a corresponding certificate.
\& * However, the corresponding private key (see below) must be loaded first,
\& * *before* moving on to the next chain file.
\& */
\& if (SSL_CTX_use_certificate_chain_file(ctx, "chain.pem") <= 0) {
\& SSL_CTX_free(ctx);
\& ERR_print_errors_fp(stderr);
\& errx(res, "Failed to load the server certificate chain file");
\& }
\&
\& /*
\& * Load the corresponding private key, this also checks that the private
\& * key matches the just loaded end\-entity certificate. It does not check
\& * whether the certificate chain is valid, the certificates could be
\& * expired, or may otherwise fail to form a chain that a client can validate.
\& */
\& if (SSL_CTX_use_PrivateKey_file(ctx, "pkey.pem", SSL_FILETYPE_PEM) <= 0) {
\& SSL_CTX_free(ctx);
\& ERR_print_errors_fp(stderr);
\& errx(res, "Error loading the server private key file, "
\& "possible key/cert mismatch???");
\& }
.Ve
.PP
Next we enable session caching, which makes it possible for clients to more
efficiently make additional \s-1TLS\s0 connections after completing an initial full
\&\s-1TLS\s0 handshake. With \s-1TLS 1.3,\s0 session resumption typically still performs a fresh
key agreement, but the certificate exchange is avoided.
.PP
.Vb 7
\& /*
\& * Servers that want to enable session resumption must specify a cache id
\& * byte array, that identifies the server application, and reduces the
\& * chance of inappropriate cache sharing.
\& */
\& SSL_CTX_set_session_id_context(ctx, (void *)cache_id, sizeof(cache_id));
\& SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER);
\&
\& /*
\& * How many client TLS sessions to cache. The default is
\& * SSL_SESSION_CACHE_MAX_SIZE_DEFAULT (20k in recent OpenSSL versions),
\& * which may be too small or too large.
\& */
\& SSL_CTX_sess_set_cache_size(ctx, 1024);
\&
\& /*
\& * Sessions older than this are considered a cache miss even if still in
\& * the cache. The default is two hours. Busy servers whose clients make
\& * many connections in a short burst may want a shorter timeout, on lightly
\& * loaded servers with sporadic connections from any given client, a longer
\& * time may be appropriate.
\& */
\& SSL_CTX_set_timeout(ctx, 3600);
.Ve
.PP
Most servers, including this one, do not solicit client certificates. We
therefore do not need a \*(L"trust store\*(R" and allow the handshake to complete even
when the client does not present a certificate. Note: Even if a client did
present a trusted ceritificate, for it to be useful, the server application
would still need custom code to use the verified identity to grant nondefault
access to that particular client. Some servers grant access to all clients
with certificates from a private \s-1CA,\s0 this then requires processing of
certificate revocation lists to deauthorise a client. It is often simpler and
more secure to instead keep a list of authorised public keys.
.PP
Though this is the default setting, we explicitly call the
\&\fBSSL_CTX_set_verify\fR\|(3) function and pass the \fB\s-1SSL_VERIFY_NONE\s0\fR value to it.
The final argument to this function is a callback that you can optionally
supply to override the default handling for certificate verification. Most
applications do not need to do this so this can safely be set to \s-1NULL\s0 to get
the default handling.
.PP
.Vb 12
\& /*
\& * Clients rarely employ certificate\-based authentication, and so we don\*(Aqt
\& * require "mutual" TLS authentication (indeed there\*(Aqs no way to know
\& * whether or how the client authenticated the server, so the term "mutual"
\& * is potentially misleading).
\& *
\& * Since we\*(Aqre not soliciting or processing client certificates, we don\*(Aqt
\& * need to configure a trusted\-certificate store, so no call to
\& * SSL_CTX_set_default_verify_paths() is needed. The server\*(Aqs own
\& * certificate chain is assumed valid.
\& */
\& SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
.Ve
.PP
That is all the setup that we need to do for the \fB\s-1SSL_CTX\s0\fR. Next we create an
acceptor \s-1BIO\s0 on which to accept client connections. This just records the
intended port (and optional \*(L"host:\*(R" prefix), without actually creating the
socket. This delayed processing allows the programmer to specify additional
behaviours before the listening socket is actually created.
.PP
.Vb 10
\& /*
\& * Create a listener socket wrapped in a BIO.
\& * The first call to BIO_do_accept() initialises the socket
\& */
\& acceptor_bio = BIO_new_accept(hostport);
\& if (acceptor_bio == NULL) {
\& SSL_CTX_free(ctx);
\& ERR_print_errors_fp(stderr);
\& errx(res, "Error creating acceptor bio");
\& }
.Ve
.PP
Servers almost always want to use the \*(L"\s-1SO_REUSEADDR\*(R"\s0 option to avoid startup
failures if there are still lingering client connections, so we do that before
making the \fBfirst\fR call to \fBBIO_do_accept\fR\|(3) which creates the listening
socket, without accepting a client connection. Subsequent calls to the same
function will accept new connections.
.PP
.Vb 6
\& BIO_set_bind_mode(acceptor_bio, BIO_BIND_REUSEADDR);
\& if (BIO_do_accept(acceptor_bio) <= 0) {
\& SSL_CTX_free(ctx);
\& ERR_print_errors_fp(stderr);
\& errx(res, "Error setting up acceptor socket");
\& }
.Ve
.SS "Server loop"
.IX Subsection "Server loop"
The server now enters a \*(L"forever\*(R" loop handling one client connection at a
time. Before each connection we clear the OpenSSL error stack, so that any
error reports are related to just the new connection.
.PP
.Vb 2
\& /* Pristine error stack for each new connection */
\& ERR_clear_error();
.Ve
.PP
At this point the server blocks to accept the next client:
.PP
.Vb 5
\& /* Wait for the next client to connect */
\& if (BIO_do_accept(acceptor_bio) <= 0) {
\& /* Client went away before we accepted the connection */
\& continue;
\& }
.Ve
.PP
On success the accepted client connection has been wrapped in a fresh \s-1BIO\s0 and
pushed onto the end of the acceptor \s-1BIO\s0 chain. We pop it off returning the
acceptor \s-1BIO\s0 to its initial state.
.PP
.Vb 3
\& /* Pop the client connection from the BIO chain */
\& client_bio = BIO_pop(acceptor_bio);
\& fprintf(stderr, "New client connection accepted\en");
.Ve
.PP
Next, we create an \fB\s-1SSL\s0\fR object by calling the \fB\fBSSL_new\fB\|(3)\fR function and
passing the \fB\s-1SSL_CTX\s0\fR we created as an argument. The client connection \s-1BIO\s0 is
configured as the I/O conduit for this \s-1SSL\s0 handle. SSL_set_bio transfers
ownership of the \s-1BIO\s0 or BIOs involved (our \fBclient_bio\fR) to the \s-1SSL\s0 handle.
.PP
.Vb 8
\& /* Associate a new SSL handle with the new connection */
\& if ((ssl = SSL_new(ctx)) == NULL) {
\& ERR_print_errors_fp(stderr);
\& warnx("Error creating SSL handle for new connection");
\& BIO_free(client_bio);
\& continue;
\& }
\& SSL_set_bio(ssl, client_bio, client_bio);
.Ve
.PP
And now we're ready to attempt the \s-1SSL\s0 handshake. With a blocking socket
OpenSSL will perform all the read and write operations required to complete the
handshake (or detect and report a failure) before returning.
.PP
.Vb 7
\& /* Attempt an SSL handshake with the client */
\& if (SSL_accept(ssl) <= 0) {
\& ERR_print_errors_fp(stderr);
\& warnx("Error performing SSL handshake with client");
\& SSL_free(ssl);
\& continue;
\& }
.Ve
.PP
With the handshake complete, the server loops echoing client input back to the
client:
.PP
.Vb 9
\& while (SSL_read_ex(ssl, buf, sizeof(buf), &nread) > 0) {
\& if (SSL_write_ex(ssl, buf, nread, &nwritten) > 0 &&
\& nwritten == nread) {
\& total += nwritten;
\& continue;
\& }
\& warnx("Error echoing client input");
\& break;
\& }
.Ve
.PP
Once the client closes its connection, we report the number of bytes sent to
\&\fBstderr\fR and free the \s-1SSL\s0 handle, which also frees the \fBclient_bio\fR and
closes the underlying socket.
.PP
.Vb 2
\& fprintf(stderr, "Client connection closed, %zu bytes sent\en", total);
\& SSL_free(ssl);
.Ve
.PP
The server is now ready to accept the next client connection.
.SS "Final clean up"
.IX Subsection "Final clean up"
If the server could somehow manage to break out of the infinite loop, and
be ready to exit, it would first deallocate the constructed \fB\s-1SSL_CTX\s0\fR.
.PP
.Vb 5
\& /*
\& * Unreachable placeholder cleanup code, the above loop runs forever.
\& */
\& SSL_CTX_free(ctx);
\& return EXIT_SUCCESS;
.Ve
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\fBossl\-guide\-introduction\fR\|(7), \fBossl\-guide\-libraries\-introduction\fR\|(7),
\&\fBossl\-guide\-libssl\-introduction\fR\|(7), \fBossl\-guide\-tls\-introduction\fR\|(7),
\&\fBossl\-guide\-tls\-client\-non\-block\fR\|(7), \fBossl\-guide\-quic\-client\-block\fR\|(7)
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
.PP
Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use
this file except in compliance with the License. You can obtain a copy
in the file \s-1LICENSE\s0 in the source distribution or at
<https://www.openssl.org/source/license.html>.