OSSL-GUIDE-TLS-INTRODUCTION(7ossl)
πΈ OSSL-GUIDE-TLS-INTRODUCTION(7ossl) β OpenSSL Guide: An introduction to SSL/TLS in OpenSSL πΈ
π NAME π
ossl-guide-tls-introduction β OpenSSL Guide: An introduction to SSL/TLS in OpenSSL.
β¨ INTRODUCTION β¨
okay so bestie, this page is your lil backstage pass to some basic SSL/TLS concepts and background, plus how all of that shows up inside OpenSSL. It assumes you already have a basic understanding of TCP/IP and sockets, fr fr. π«Ά
π₯ WHAT IS TLS? π₯
TLS stands for Transport Layer Security. It lets applications securely communicate with each other across a network so that the confidentiality of the info being exchanged is protected β i.e. it stops eavesdroppers from listening in on the communication. It also protects the integrity of that info so an attacker can't change it. And finally it provides authentication, so one or both parties can be sure they're talking to who they think they're talking to, and not some imposter. π
Sometimes TLS gets called by its predecessor's name, SSL (Secure Sockets Layer). OpenSSL dates from a time when the SSL name was still in common use, and hence many of the functions and names used by OpenSSL contain the "SSL" abbreviation. Nonetheless, OpenSSL contains a fully fledged TLS implementation. No cap. β¨
TLS is based on a client/server model. The application that initiates a communication is known as the client. The application that responds to a remotely initiated communication is the server. The term "endpoint" refers to either the client or the server in a communication. The term "peer" refers to the endpoint at the other side of the communication that we are currently referring to. So if we're currently talking about the client, then the peer would be the server. iykyk π¦
TLS is a standardised protocol and there are numerous different implementations of it. Because of the standards, an OpenSSL client or server is able to communicate seamlessly with an application using some different implementation of TLS. TLS (and its predecessor SSL) have been around for a significant period of time, and the protocol has undergone various changes over the years. Consequently, there are different versions of the protocol available. TLS includes the ability to perform version negotiation, so that the highest protocol version that the client and server share in common is used. π
TLS acts as a security layer over some lower level transport protocol. Typically, the transport layer will be TCP. π
π¦ SSL AND TLS VERSIONS π¦
SSL was initially developed by Netscape Communications, and its first publicly released version was SSLv2 in 1995. Note that SSLv1 was never publicly released. SSLv3 came along quickly afterwards in 1996. Subsequently, development of the protocol moved to the IETF, which released the first version of TLS (TLSv1.0) in 1999 as RFC2246. TLSv1.1 was released in 2006 as RFC4346, and TLSv1.2 came along in 2008 as RFC5246. The most recent version of the standard is TLSv1.3, which was released in 2018 as RFC8446. β
Today TLSv1.3 and TLSv1.2 are the most commonly deployed versions of the protocol. The IETF have formally deprecated TLSv1.1 and TLSv1.0, so anything below TLSv1.2 should be avoided since the older protocol versions are susceptible to security problems. β οΈ
OpenSSL does not support SSLv2 (it was removed in OpenSSL 1.1.0). Support for SSLv3 is available as a compile time option β but it is not built by default. Support for TLSv1.0, TLSv1.1, TLSv1.2 and TLSv1.3 are all available by default in a standard build of OpenSSL. However, special run-time configuration is required in order to make TLSv1.0 and TLSv1.1 work successfully. π«Ά
OpenSSL will always try to negotiate the highest protocol version that it has been configured to support. In most cases this will mean either TLSv1.3 or TLSv1.2 is chosen. π
π CERTIFICATES π
In order for a client to establish a connection to a server, it must authenticate the identity of that server β i.e. it needs to confirm that the server is really the server it claims to be and not some imposter. In order to do this, the server will send to the client a digital certificate (also commonly referred to as an X.509 certificate). The certificate contains various information about the server, including its full DNS hostname. Also within the certificate is the server's public key. The server operator will have a private key which is linked to the public key and must not be published. π
Along with the certificate, the server will also send to the client proof that it knows the private key associated with the public key in the certificate. It does this by digitally signing a message to the client using that private key. The client can verify the signature using the public key from the certificate. If the signature verifies successfully, then the client knows that the server is in possession of the correct private key. β
The certificate that the server sends will also be signed by a Certificate Authority. The Certificate Authority (commonly known as a CA) is a third party organisation that is responsible for verifying the information in the server's certificate (including its DNS hostname). The CA should only sign the certificate if it has been able to confirm that the server operator does indeed have control of the server associated with its DNS hostname, and that the server operator has control of the private key. π
In this way, if the client trusts the CA that has signed the server's certificate and it can verify that the server has the right private key, then it can trust that the server truly does represent the DNS hostname given in the certificate. The client must also verify that the hostname given in the certificate matches the hostname that it originally sent the request to. π―
Once all of these checks have been done, the client has successfully verified the identity of the server. OpenSSL can perform all of these checks automatically, but it must be provided with certain information in order to do so β i.e. the set of CAs that the client trusts, as well as the DNS hostname for the server that this client is trying to connect to. π
Note that it is common for certificates to be built up into a chain. For example, a server's certificate may be signed by a key owned by an intermediate CA. That intermediate CA also has a certificate containing its public key, which is in turn signed by a key owned by a root CA. The client may only trust the root CA, but if the server sends both its own certificate and the certificate for the intermediate CA, then the client can still successfully verify the identity of the server. There is a chain of trust between the root CA and the server. π
By default, it is only the client that authenticates the server using this method. However, it is also possible to set things up such that the server additionally authenticates the client. This is known as "client authentication". In this approach, the client will still authenticate the server in the same way, but the server will request a certificate from the client. The client sends the server its certificate, and the server authenticates it in the same way that the client does. π
π TRUSTED CERTIFICATE STORE π
okay so real talk bestie β everything we just talked about? the whole chain-of-trust situation? it only works if you can actually build a chain between the CAs that the endpoint trusts and the certificate the peer is handing you. so like, the endpoint must have a set of certificates for CAs that it trusts before any communication can even happen. and here's the gag: OpenSSL itself does not provide such a set of certificates. π yeah. you have to make sure you've got them yourself before you start verifying certs β which, reminder, is always the case if the endpoint is a client, and only if client authentication is in use for a server.
the good news? other organisations do maintain these sets. so if you got your copy of OpenSSL from an Operating System (OS) vendor (think: your Linux distro), then normally the CA certificates come bundled along with that copy. slay. π
you can check this β literally just run the command line app like this:
openssl version -d
that prints a value for OPENSSLDIR. then go poke around in the certs sub directory of OPENSSLDIR and check what's in there. for example, if OPENSSLDIR is "/usr/local/ssl", then check the contents of the "/usr/local/ssl/certs" directory. π
you're expecting to see a list of files, typically with the suffix ".pem" or ".0". if those exist? congrats bestie, you already have a suitable trusted certificate store. periodt. β¨
if you're running your OpenSSL on Windows, then OpenSSL (from version 3.2 onwards) will use the default Windows set of trusted CAs. easy.
but if you built OpenSSL from source, or got it from somewhere else and it doesn't have a set of trusted CA certificates β then yeah, you're on your own and you'll have to obtain them yourself. π one such source is the Curl project. see the page <https://curl.se/docs/caextract.html> where you can download trusted certificates in a single file. rename that file to "cert.pem" and store it directly in OPENSSLDIR. for example if OPENSSLDIR is "/usr/local/ssl", then save it as "/usr/local/ssl/cert.pem".
you can also use environment variables to override the default location OpenSSL looks for its trusted certificate store in. set the SSL_CERT_DIR environment variable to give the directory where OpenSSL should look for its certificates, or the SSL_CERT_FILE environment variable to give the name of a single file containing all of the certificates. see openssl-env(7) for further details about OpenSSL environment variables. for example, you could use this capability to have multiple versions of OpenSSL all installed on the same system using different values for OPENSSLDIR but all using the same trusted certificate store. cute, right? π¦
vibe check: is your store actually working? π
test that your trusted certificate store is set up correctly by using it via the OpenSSL command line. use this to connect to a TLS server:
openssl s_client www.openssl.org:443
once the command has connected, type the letter "Q" followed by <enter> to exit the session. it'll print a lot of info on the screen about the connection. look for a block of text like this:
SSL handshake has read 4584 bytes and written 403 bytes
Verification: OK
hopefully if everything worked, that "Verification" line says "OK". π
but if it's not working as expected, you might see this instead:
SSL handshake has read 4584 bytes and written 403 bytes
Verification error: unable to get local issuer certificate
the "unable to get local issuer certificate" error means OpenSSL couldn't find a trusted CA for the chain of certificates the server provided, in its trusted certificate store. so go double-check your trusted certificate store config again. π
heads up bestie: s_client is a testing tool, and it will still let you connect to the TLS server regardless of the verification error. most applications should not do this and should abort the connection in the event of a verification error. do not copy that energy into your app. π«
π IMPORTANT OBJECTS FOR AN OPENSSL TLS APPLICATION π
so a TLS connection is represented by the SSL object in an OpenSSL based application. once a connection with a remote peer is established, an endpoint can "write" data to the SSL object to send data to the peer, or "read" data from it to receive data from the server. simple enough, iykyk. π
a new SSL object is created from an SSL_CTX object. think of an SSL_CTX as a "factory" for creating SSL objects. you can create a single SSL_CTX object and then create multiple connections (i.e. SSL objects) from it. typically you set up common configuration options on the SSL_CTX so that all the SSL objects created from it inherit the same configuration options. main character energy, honestly. π
ngl, internally to OpenSSL various items that are shared between multipleSSLobjects are cached in theSSL_CTXfor performance reasons. therefore it's considered best practice to create oneSSL_CTXfor use by multipleSSLobjects instead of having oneSSL_CTXfor eachSSLobject you create. (your CPU will thank you. π«Ά)
each SSL object is also associated with two BIO objects. a BIO object is used for sending or receiving data from the underlying transport layer β for example, you might create a BIO to represent a TCP socket. the SSL object uses one BIO for reading data and one BIO for writing data. in most cases you'd use the same BIO for each direction, but there could be some circumstances where you want them to be different. β¨
it's up to the application programmer to create the BIO objects that are needed and supply them to the SSL object. see ossl-guide-tls-client-block(7) and ossl-guide-tls-server-block(7) for usage examples.
finally β an endpoint can establish a "session" with its peer. the session holds various TLS parameters about the connection between the client and the server. those session details can then be reused in a subsequent connection attempt to speed up the process of connecting. this is known as "resumption". sessions are represented in OpenSSL by the SSL_SESSION object. in TLSv1.2 there's always exactly one session per connection. in TLSv1.3 there can be any number per connection β including none. wild, i know. π
πΈ PHASES OF A TLS CONNECTION πΈ
phase one: set up. π a TLS connection starts with an initial "set up" phase. the endpoint creates the SSL_CTX (if one hasn't already been created) and configures it.
a client then creates an SSL object to represent the new TLS connection. any connection specific configuration parameters get applied, and then the underlying socket is created and associated with the SSL via BIO objects.
a server creates a socket for listening for incoming connection attempts from clients. once a connection attempt is made, the server creates an SSL object in the same way as for a client, and associates it with a BIO for the newly created incoming socket. π
phase two: the handshake. π€ after set up is complete, the TLS "handshake" phase begins. a TLS handshake consists of the client and server exchanging a series of TLS handshake messages to establish the connection. the client starts by sending a "ClientHello" handshake message and the server responds with a "ServerHello". the handshake is complete once an endpoint has sent its last message (known as the "Finished" message) and received a Finished message from its peer.
and here's the messy part β this might occur at slightly different times for each peer. for example, in TLSv1.3 the server always sends its Finished message before the client. the client later responds with its Finished message. at this point the client has completed the handshake, because it has both sent and received a Finished message. the server has sent its Finished message, but the Finished message from the client may still be in-flight, so the server is still in the handshake phase. it's even possible that the server will fail to complete the handshake (if it considers there's some problem with the messages sent from the client), even though the client may have already progressed to sending application data. π in TLSv1.2 this can happen the other way around β i.e. the server finishes first and the client finishes second. chaotic but that's literally how it is. π
phase three: application data. π¨ once the handshake is complete, the application data transfer phase begins. strictly speaking there are some situations where the client can start sending application data even earlier (using the TLSv1.3 "early data" capability) β but we're going to skip over that for this basic introduction. π
during application data transfer the client and server can read and write data to the connection freely. the details of this are typically left to some higher level application protocol (for example HTTP). and not all information exchanged during this phase is application data β some protocol level messages may still be exchanged. so it's not necessarily the case that just because the underlying socket is "readable", application data will be available to read. noted. β
phase four: shutdown. π¦ when the connection is no longer required, it should be shut down. a shutdown may be initiated by either the client or the server via a message known as a "close_notify" alert. the client or server that receives a close_notify may respond with one, and then the connection is fully closed and application data can no longer be sent or received.
once shutdown is complete, a TLS application must clean up by freeing the SSL object. don't leave it lying around, bestie. π§Ήβ¨
π FURTHER READING π
- see
ossl-guide-tls-client-block(7)for an example of how to apply these concepts in order to write a simple TLS client based on a blocking socket. π₯ - see
ossl-guide-tls-server-block(7)for an example of how to apply these concepts in order to write a simple TLS server handling one client at a time over a blocking socket. π - see
ossl-guide-quic-introduction(7)for an introduction to QUIC in OpenSSL. π¦
π SEE ALSO π
ossl-guide-introduction(7), ossl-guide-libraries-introduction(7), ossl-guide-libssl-introduction(7), ossl-guide-tls-client-block(7), ossl-guide-tls-server-block(7), ossl-guide-quic-introduction(7)
π COPYRIGHT π
Copyright 2023-2026 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
aaaand that's the whole page bestie!! π this one's from OpenSSL 3.5.6, dated 2026-04-07 β go build something iconic with your new TLS knowledge, mwah π«Άπβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms