1 | =pod
|
---|
2 |
|
---|
3 | =head1 NAME
|
---|
4 |
|
---|
5 | ossl-guide-tls-introduction
|
---|
6 | - OpenSSL Guide: An introduction to SSL/TLS in OpenSSL
|
---|
7 |
|
---|
8 | =head1 INTRODUCTION
|
---|
9 |
|
---|
10 | This page will provide an introduction to some basic SSL/TLS concepts and
|
---|
11 | background and how it is used within OpenSSL. It assumes that you have a basic
|
---|
12 | understanding of TCP/IP and sockets.
|
---|
13 |
|
---|
14 | =head1 WHAT IS TLS?
|
---|
15 |
|
---|
16 | TLS stands for Transport Layer Security. TLS allows applications to securely
|
---|
17 | communicate with each other across a network such that the confidentiality of
|
---|
18 | the information exchanged is protected (i.e. it prevents eavesdroppers from
|
---|
19 | listening in to the communication). Additionally it protects the integrity of
|
---|
20 | the information exchanged to prevent an attacker from changing it. Finally it
|
---|
21 | provides authentication so that one or both parties can be sure that they are
|
---|
22 | talking to who they think they are talking to and not some imposter.
|
---|
23 |
|
---|
24 | Sometimes TLS is referred to by its predecessor's name SSL (Secure Sockets
|
---|
25 | Layer). OpenSSL dates from a time when the SSL name was still in common use and
|
---|
26 | hence many of the functions and names used by OpenSSL contain the "SSL"
|
---|
27 | abbreviation. Nonetheless OpenSSL contains a fully fledged TLS implementation.
|
---|
28 |
|
---|
29 | TLS is based on a client/server model. The application that initiates a
|
---|
30 | communication is known as the client. The application that responds to a
|
---|
31 | remotely initiated communication is the server. The term "endpoint" refers to
|
---|
32 | either of the client or the server in a communication. The term "peer" refers to
|
---|
33 | the endpoint at the other side of the communication that we are currently
|
---|
34 | referring to. So if we are currently talking about the client then the peer
|
---|
35 | would be the server.
|
---|
36 |
|
---|
37 | TLS is a standardised protocol and there are numerous different implementations
|
---|
38 | of it. Due to the standards an OpenSSL client or server is able to communicate
|
---|
39 | seamlessly with an application using some different implementation of TLS. TLS
|
---|
40 | (and its predecessor SSL) have been around for a significant period of time and
|
---|
41 | the protocol has undergone various changes over the years. Consequently there
|
---|
42 | are different versions of the protocol available. TLS includes the ability to
|
---|
43 | perform version negotiation so that the highest protocol version that the client
|
---|
44 | and server share in common is used.
|
---|
45 |
|
---|
46 | TLS acts as a security layer over some lower level transport protocol. Typically
|
---|
47 | the transport layer will be TCP.
|
---|
48 |
|
---|
49 | =head1 SSL AND TLS VERSIONS
|
---|
50 |
|
---|
51 | SSL was initially developed by Netscape Communications and its first publicly
|
---|
52 | released version was SSLv2 in 1995. Note that SSLv1 was never publicly released.
|
---|
53 | SSLv3 came along quickly afterwards in 1996. Subsequently development of the
|
---|
54 | protocol moved to the IETF which released the first version of TLS (TLSv1.0) in
|
---|
55 | 1999 as RFC2246. TLSv1.1 was released in 2006 as RFC4346 and TLSv1.2 came along
|
---|
56 | in 2008 as RFC5246. The most recent version of the standard is TLSv1.3 which
|
---|
57 | was released in 2018 as RFC8446.
|
---|
58 |
|
---|
59 | Today TLSv1.3 and TLSv1.2 are the most commonly deployed versions of the
|
---|
60 | protocol. The IETF have formally deprecated TLSv1.1 and TLSv1.0, so anything
|
---|
61 | below TLSv1.2 should be avoided since the older protocol versions are
|
---|
62 | susceptible to security problems.
|
---|
63 |
|
---|
64 | OpenSSL does not support SSLv2 (it was removed in OpenSSL 1.1.0). Support for
|
---|
65 | SSLv3 is available as a compile time option - but it is not built by default.
|
---|
66 | Support for TLSv1.0, TLSv1.1, TLSv1.2 and TLSv1.3 are all available by default
|
---|
67 | in a standard build of OpenSSL. However special run-time configuration is
|
---|
68 | required in order to make TLSv1.0 and TLSv1.1 work successfully.
|
---|
69 |
|
---|
70 | OpenSSL will always try to negotiate the highest protocol version that it has
|
---|
71 | been configured to support. In most cases this will mean either TLSv1.3 or
|
---|
72 | TLSv1.2 is chosen.
|
---|
73 |
|
---|
74 | =head1 CERTIFICATES
|
---|
75 |
|
---|
76 | In order for a client to establish a connection to a server it must authenticate
|
---|
77 | the identify of that server, i.e. it needs to confirm that the server is really
|
---|
78 | the server that it claims to be and not some imposter. In order to do this the
|
---|
79 | server will send to the client a digital certificate (also commonly referred to
|
---|
80 | as an X.509 certificate). The certificate contains various information about the
|
---|
81 | server including its full DNS hostname. Also within the certificate is the
|
---|
82 | server's public key. The server operator will have a private key which is
|
---|
83 | linked to the public key and must not be published.
|
---|
84 |
|
---|
85 | Along with the certificate the server will also send to the client proof that it
|
---|
86 | knows the private key associated with the public key in the certificate. It does
|
---|
87 | this by digitally signing a message to the client using that private key. The
|
---|
88 | client can verify the signature using the public key from the certificate. If
|
---|
89 | the signature verifies successfully then the client knows that the server is in
|
---|
90 | possession of the correct private key.
|
---|
91 |
|
---|
92 | The certificate that the server sends will also be signed by a Certificate
|
---|
93 | Authority. The Certificate Authority (commonly known as a CA) is a third party
|
---|
94 | organisation that is responsible for verifying the information in the server's
|
---|
95 | certificate (including its DNS hostname). The CA should only sign the
|
---|
96 | certificate if it has been able to confirm that the server operator does indeed
|
---|
97 | have control of the server associated with its DNS hostname and that the server
|
---|
98 | operator has control of the private key.
|
---|
99 |
|
---|
100 | In this way, if the client trusts the CA that has signed the server's
|
---|
101 | certificate and it can verify that the server has the right private key then it
|
---|
102 | can trust that the server truly does represent the DNS hostname given in the
|
---|
103 | certificate. The client must also verify that the hostname given in the
|
---|
104 | certificate matches the hostname that it originally sent the request to.
|
---|
105 |
|
---|
106 | Once all of these checks have been done the client has successfully verified the
|
---|
107 | identify of the server. OpenSSL can perform all of these checks automatically
|
---|
108 | but it must be provided with certain information in order to do so, i.e. the set
|
---|
109 | of CAs that the client trusts as well as the DNS hostname for the server that
|
---|
110 | this client is trying to connect to.
|
---|
111 |
|
---|
112 | Note that it is common for certificates to be built up into a chain. For example
|
---|
113 | a server's certificate may be signed by a key owned by a an intermediate CA.
|
---|
114 | That intermediate CA also has a certificate containing its public key which is
|
---|
115 | in turn signed by a key owned by a root CA. The client may only trust the root
|
---|
116 | CA, but if the server sends both its own certificate and the certificate for the
|
---|
117 | intermediate CA then the client can still successfully verify the identity of
|
---|
118 | the server. There is a chain of trust between the root CA and the server.
|
---|
119 |
|
---|
120 | By default it is only the client that authenticates the server using this
|
---|
121 | method. However it is also possible to set things up such that the server
|
---|
122 | additionally authenticates the client. This is known as "client authentication".
|
---|
123 | In this approach the client will still authenticate the server in the same way,
|
---|
124 | but the server will request a certificate from the client. The client sends the
|
---|
125 | server its certificate and the server authenticates it in the same way that the
|
---|
126 | client does.
|
---|
127 |
|
---|
128 | =head1 TRUSTED CERTIFICATE STORE
|
---|
129 |
|
---|
130 | The system described above only works if a chain of trust can be built between
|
---|
131 | the set of CAs that the endpoint trusts and the certificate that the peer is
|
---|
132 | using. The endpoint must therefore have a set of certificates for CAs that it
|
---|
133 | trusts before any communication can take place. OpenSSL itself does not provide
|
---|
134 | such a set of certificates. Therefore you will need to make sure you have them
|
---|
135 | before you start if you are going to be verifying certificates (i.e. always if
|
---|
136 | the endpoint is a client, and only if client authentication is in use for a
|
---|
137 | server).
|
---|
138 |
|
---|
139 | Fortunately other organisations do maintain such a set of certificates. If you
|
---|
140 | have obtained your copy of OpenSSL from an Operating System (OS) vendor (e.g. a
|
---|
141 | Linux distribution) then normally the set of CA certificates will also be
|
---|
142 | distributed with that copy.
|
---|
143 |
|
---|
144 | You can check this by running the OpenSSL command line application like this:
|
---|
145 |
|
---|
146 | openssl version -d
|
---|
147 |
|
---|
148 | This will display a value for B<OPENSSLDIR>. Look in the B<certs> sub directory
|
---|
149 | of B<OPENSSLDIR> and check its contents. For example if B<OPENSSLDIR> is
|
---|
150 | "/usr/local/ssl", then check the contents of the "/usr/local/ssl/certs"
|
---|
151 | directory.
|
---|
152 |
|
---|
153 | You are expecting to see a list of files, typically with the suffix ".pem" or
|
---|
154 | ".0". If they exist then you already have a suitable trusted certificate store.
|
---|
155 |
|
---|
156 | If you are running your version of OpenSSL on Windows then OpenSSL (from version
|
---|
157 | 3.2 onwards) will use the default Windows set of trusted CAs.
|
---|
158 |
|
---|
159 | If you have built your version of OpenSSL from source, or obtained it from some
|
---|
160 | other location and it does not have a set of trusted CA certificates then you
|
---|
161 | will have to obtain them yourself. One such source is the Curl project. See the
|
---|
162 | page L<https://curl.se/docs/caextract.html> where you can download trusted
|
---|
163 | certificates in a single file. Rename the file to "cert.pem" and store it
|
---|
164 | directly in B<OPENSSLDIR>. For example if B<OPENSSLDIR> is "/usr/local/ssl",
|
---|
165 | then save it as "/usr/local/ssl/cert.pem".
|
---|
166 |
|
---|
167 | You can also use environment variables to override the default location that
|
---|
168 | OpenSSL will look for its trusted certificate store. Set the B<SSL_CERT_PATH>
|
---|
169 | environment variable to give the directory where OpenSSL should looks for its
|
---|
170 | certificates or the B<SSL_CERT_FILE> environment variable to give the name of
|
---|
171 | a single file containing all of the certificates. See L<openssl-env(7)> for
|
---|
172 | further details about OpenSSL environment variables. For example you could use
|
---|
173 | this capability to have multiple versions of OpenSSL all installed on the same
|
---|
174 | system using different values for B<OPENSSLDIR> but all using the same
|
---|
175 | trusted certificate store.
|
---|
176 |
|
---|
177 | You can test that your trusted certificate store is setup correctly by using it
|
---|
178 | via the OpenSSL command line. Use the following command to connect to a TLS
|
---|
179 | server:
|
---|
180 |
|
---|
181 | openssl s_client www.openssl.org:443
|
---|
182 |
|
---|
183 | Once the command has connected type the letter "Q" followed by "<enter>" to exit
|
---|
184 | the session. This will print a lot of information on the screen about the
|
---|
185 | connection. Look for a block of text like this:
|
---|
186 |
|
---|
187 | SSL handshake has read 4584 bytes and written 403 bytes
|
---|
188 | Verification: OK
|
---|
189 |
|
---|
190 | Hopefully if everything has worked then the "Verification" line will say "OK".
|
---|
191 | If its not working as expected then you might see output like this instead:
|
---|
192 |
|
---|
193 | SSL handshake has read 4584 bytes and written 403 bytes
|
---|
194 | Verification error: unable to get local issuer certificate
|
---|
195 |
|
---|
196 | The "unable to get local issuer certificate" error means that OpenSSL has been
|
---|
197 | unable to find a trusted CA for the chain of certificates provided by the server
|
---|
198 | in its trusted certificate store. Check your trusted certificate store
|
---|
199 | configuration again.
|
---|
200 |
|
---|
201 | Note that s_client is a testing tool and will still allow you to connect to the
|
---|
202 | TLS server regardless of the verification error. Most applications should not do
|
---|
203 | this and should abort the connection in the event of a verification error.
|
---|
204 |
|
---|
205 | =head1 IMPORTANT OBJECTS FOR AN OPENSSL TLS APPLICATION
|
---|
206 |
|
---|
207 | A TLS connection is represented by the B<SSL> object in an OpenSSL based
|
---|
208 | application. Once a connection with a remote peer has been established an
|
---|
209 | endpoint can "write" data to the B<SSL> object to send data to the peer, or
|
---|
210 | "read" data from it to receive data from the server.
|
---|
211 |
|
---|
212 | A new B<SSL> object is created from an B<SSL_CTX> object. Think of an B<SSL_CTX>
|
---|
213 | as a "factory" for creating B<SSL> objects. You can create a single B<SSL_CTX>
|
---|
214 | object and then create multiple connections (i.e. B<SSL> objects) from it.
|
---|
215 | Typically you can set up common configuration options on the B<SSL_CTX> so that
|
---|
216 | all the B<SSL> object created from it inherit the same configuration options.
|
---|
217 |
|
---|
218 | Note that internally to OpenSSL various items that are shared between multiple
|
---|
219 | B<SSL> objects are cached in the B<SSL_CTX> for performance reasons. Therefore
|
---|
220 | it is considered best practice to create one B<SSL_CTX> for use by multiple
|
---|
221 | B<SSL> objects instead of having one B<SSL_CTX> for each B<SSL> object that you
|
---|
222 | create.
|
---|
223 |
|
---|
224 | Each B<SSL> object is also associated with two B<BIO> objects. A B<BIO> object
|
---|
225 | is used for sending or receiving data from the underlying transport layer. For
|
---|
226 | example you might create a B<BIO> to represent a TCP socket. The B<SSL> object
|
---|
227 | uses one B<BIO> for reading data and one B<BIO> for writing data. In most cases
|
---|
228 | you would use the same B<BIO> for each direction but there could be some
|
---|
229 | circumstances where you want them to be different.
|
---|
230 |
|
---|
231 | It is up to the application programmer to create the B<BIO> objects that are
|
---|
232 | needed and supply them to the B<SSL> object. See
|
---|
233 | L<ossl-guide-tls-client-block(7)> and L<ossl-guide-tls-server-block(7)> for
|
---|
234 | usage examples.
|
---|
235 |
|
---|
236 | Finally, an endpoint can establish a "session" with its peer. The session holds
|
---|
237 | various TLS parameters about the connection between the client and the server.
|
---|
238 | The session details can then be reused in a subsequent connection attempt to
|
---|
239 | speed up the process of connecting. This is known as "resumption". Sessions are
|
---|
240 | represented in OpenSSL by the B<SSL_SESSION> object. In TLSv1.2 there is always
|
---|
241 | exactly one session per connection. In TLSv1.3 there can be any number per
|
---|
242 | connection including none.
|
---|
243 |
|
---|
244 | =head1 PHASES OF A TLS CONNECTION
|
---|
245 |
|
---|
246 | A TLS connection starts with an initial "set up" phase. The endpoint creates the
|
---|
247 | B<SSL_CTX> (if one has not already been created) and configures it.
|
---|
248 |
|
---|
249 | A client then creates an B<SSL> object to represent the new TLS connection. Any
|
---|
250 | connection specific configuration parameters are then applied and the underlying
|
---|
251 | socket is created and associated with the B<SSL> via B<BIO> objects.
|
---|
252 |
|
---|
253 | A server will create a socket for listening for incoming connection attempts
|
---|
254 | from clients. Once a connection attempt is made the server will create an B<SSL>
|
---|
255 | object in the same way as for a client and associate it with a B<BIO> for the
|
---|
256 | newly created incoming socket.
|
---|
257 |
|
---|
258 | After set up is complete the TLS "handshake" phase begins. A TLS handshake
|
---|
259 | consists of the client and server exchanging a series of TLS handshake messages
|
---|
260 | to establish the connection. The client starts by sending a "ClientHello"
|
---|
261 | handshake message and the server responds with a "ServerHello". The handshake is
|
---|
262 | complete once an endpoint has sent its last message (known as the "Finished"
|
---|
263 | message) and received a Finished message from its peer. Note that this might
|
---|
264 | occur at slightly different times for each peer. For example in TLSv1.3 the
|
---|
265 | server always sends its Finished message before the client. The client later
|
---|
266 | responds with its Finished message. At this point the client has completed the
|
---|
267 | handshake because it has both sent and received a Finished message. The server
|
---|
268 | has sent its Finished message but the Finished message from the client may still
|
---|
269 | be in-flight, so the server is still in the handshake phase. It is even possible
|
---|
270 | that the server will fail to complete the handshake (if it considers there is
|
---|
271 | some problem with the messages sent from the client), even though the client may
|
---|
272 | have already progressed to sending application data. In TLSv1.2 this can happen
|
---|
273 | the other way around, i.e. the server finishes first and the client finishes
|
---|
274 | second.
|
---|
275 |
|
---|
276 | Once the handshake is complete the application data transfer phase begins.
|
---|
277 | Strictly speaking there are some situations where the client can start sending
|
---|
278 | application data even earlier (using the TLSv1.3 "early data" capability) - but
|
---|
279 | we're going to skip over that for this basic introduction.
|
---|
280 |
|
---|
281 | During application data transfer the client and server can read and write data
|
---|
282 | to the connection freely. The details of this are typically left to some higher
|
---|
283 | level application protocol (for example HTTP). Not all information exchanged
|
---|
284 | during this phase is application data. Some protocol level messages may still
|
---|
285 | be exchanged - so it is not necessarily the case that, just because the
|
---|
286 | underlying socket is "readable", that application data will be available to read.
|
---|
287 |
|
---|
288 | When the connection is no longer required then it should be shutdown. A shutdown
|
---|
289 | may be initiated by either the client or the server via a message known as a
|
---|
290 | "close_notify" alert. The client or server that receives a close_notify may
|
---|
291 | respond with one and then the connection is fully closed and application data
|
---|
292 | can no longer be sent or received.
|
---|
293 |
|
---|
294 | Once shutdown is complete a TLS application must clean up by freeing the SSL
|
---|
295 | object.
|
---|
296 |
|
---|
297 | =head1 FURTHER READING
|
---|
298 |
|
---|
299 | See L<ossl-guide-tls-client-block(7)> for an example of how to apply these
|
---|
300 | concepts in order to write a simple TLS client based on a blocking socket.
|
---|
301 | See L<ossl-guide-tls-server-block(7)> for an example of how to apply these
|
---|
302 | concepts in order to write a simple TLS server handling one client at a time
|
---|
303 | over a blocking socket.
|
---|
304 | See L<ossl-guide-quic-introduction(7)> for an introduction to QUIC in OpenSSL.
|
---|
305 |
|
---|
306 | =head1 SEE ALSO
|
---|
307 |
|
---|
308 | L<ossl-guide-introduction(7)>, L<ossl-guide-libraries-introduction(7)>,
|
---|
309 | L<ossl-guide-libssl-introduction(7)>, L<ossl-guide-tls-client-block(7)>,
|
---|
310 | L<ossl-guide-tls-server-block(7)>, L<ossl-guide-quic-introduction(7)>
|
---|
311 |
|
---|
312 | =head1 COPYRIGHT
|
---|
313 |
|
---|
314 | Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved.
|
---|
315 |
|
---|
316 | Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
317 | this file except in compliance with the License. You can obtain a copy
|
---|
318 | in the file LICENSE in the source distribution or at
|
---|
319 | L<https://www.openssl.org/source/license.html>.
|
---|
320 |
|
---|
321 | =cut
|
---|