Blame doc/cha-gtls-examples.texi

Packit aea12f
@node GnuTLS application examples
Packit aea12f
@chapter GnuTLS application examples
Packit aea12f
@anchor{examples}
Packit aea12f
@cindex example programs
Packit aea12f
@cindex examples
Packit aea12f
Packit aea12f
In this chapter several examples of real-world use cases are listed.
Packit aea12f
The examples are simplified to promote readability and contain little or 
Packit aea12f
no error checking.  
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Client examples::
Packit aea12f
* Server examples::
Packit aea12f
* More advanced client and servers::
Packit aea12f
* OCSP example::
Packit aea12f
* Miscellaneous examples::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Client examples
Packit aea12f
@section Client examples
Packit aea12f
Packit aea12f
This section contains examples of @acronym{TLS} and @acronym{SSL}
Packit aea12f
clients, using @acronym{GnuTLS}. Note that some of the examples require functions
Packit aea12f
implemented by another example.
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Client example with X.509 certificate support::
Packit aea12f
* Datagram TLS client example::
Packit aea12f
* Client using a smart card with TLS::
Packit aea12f
* Client with Resume capability example::
Packit aea12f
* Client example with SSH-style certificate verification::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Client example with X.509 certificate support
Packit aea12f
@subsection Client example with @acronym{X.509} certificate support
Packit aea12f
@anchor{ex-verify}
Packit aea12f
Packit aea12f
Let's assume now that we want to create a TCP client which
Packit aea12f
communicates with servers that use @acronym{X.509} certificate authentication.
Packit aea12f
The following client is a very simple @acronym{TLS} client, which uses
Packit aea12f
the high level verification functions for certificates, but does not support session
Packit aea12f
resumption.
Packit aea12f
Packit aea12f
Note that this client utilizes functionality present in the latest GnuTLS
Packit aea12f
version. For a reasonably portable version see @ref{Legacy client example with X.509 certificate support}.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-client-x509.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Datagram TLS client example
Packit aea12f
@subsection Datagram @acronym{TLS} client example
Packit aea12f
Packit aea12f
This is a client that uses @acronym{UDP} to connect to a
Packit aea12f
server. This is the @acronym{DTLS} equivalent to the TLS example
Packit aea12f
with X.509 certificates.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-client-dtls.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Client using a smart card with TLS
Packit aea12f
@subsection Using a smart card with TLS
Packit aea12f
@anchor{ex-pkcs11-client}
Packit aea12f
@cindex Smart card example
Packit aea12f
Packit aea12f
This example will demonstrate how to load keys and certificates
Packit aea12f
from a smart-card or any other @acronym{PKCS} #11 token, and 
Packit aea12f
use it in a TLS connection. The difference between this and the
Packit aea12f
@ref{Client example with X.509 certificate support} is that the
Packit aea12f
client keys are provided as PKCS #11 URIs instead of files.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-cert-select-pkcs11.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Client with Resume capability example
Packit aea12f
@subsection Client with resume capability example
Packit aea12f
@anchor{ex-resume-client}
Packit aea12f
Packit aea12f
This is a modification of the simple client example. Here we
Packit aea12f
demonstrate the use of session resumption. The client tries to connect
Packit aea12f
once using @acronym{TLS}, close the connection and then try to
Packit aea12f
establish a new connection using the previously negotiated data.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-client-resume.c
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
@node Client example with SSH-style certificate verification
Packit aea12f
@subsection Client example with SSH-style certificate verification
Packit aea12f
Packit aea12f
This is an alternative verification function that will use the
Packit aea12f
X.509 certificate authorities for verification, but also assume an
Packit aea12f
trust on first use (SSH-like) authentication system. That is the user is 
Packit aea12f
prompted on unknown public keys and known public keys are considered 
Packit aea12f
trusted.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-verify-ssh.c
Packit aea12f
Packit aea12f
@node Server examples
Packit aea12f
@section Server examples
Packit aea12f
Packit aea12f
This section contains examples of @acronym{TLS} and @acronym{SSL}
Packit aea12f
servers, using @acronym{GnuTLS}.
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Echo server with X.509 authentication::
Packit aea12f
* DTLS echo server with X.509 authentication::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Echo server with X.509 authentication
Packit aea12f
@subsection Echo server with @acronym{X.509} authentication
Packit aea12f
Packit aea12f
This example is a very simple echo server which supports
Packit aea12f
@acronym{X.509} authentication.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-serv-x509.c
Packit aea12f
Packit aea12f
Packit aea12f
@node DTLS echo server with X.509 authentication
Packit aea12f
@subsection DTLS echo server with @acronym{X.509} authentication
Packit aea12f
Packit aea12f
This example is a very simple echo server using Datagram TLS and 
Packit aea12f
@acronym{X.509} authentication.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-serv-dtls.c
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
@node More advanced client and servers
Packit aea12f
@section More advanced client and servers
Packit aea12f
Packit aea12f
This section has various, more advanced topics in client and servers.
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Client example with anonymous authentication::
Packit aea12f
* Using a callback to select the certificate to use::
Packit aea12f
* Obtaining session information::
Packit aea12f
* Advanced certificate verification example::
Packit aea12f
* Client example with PSK authentication::
Packit aea12f
* Client example with SRP authentication::
Packit aea12f
* Legacy client example with X.509 certificate support::
Packit aea12f
* Client example in C++::
Packit aea12f
* Echo server with PSK authentication::
Packit aea12f
* Echo server with SRP authentication::
Packit aea12f
* Echo server with anonymous authentication::
Packit aea12f
* Helper functions for TCP connections::
Packit aea12f
* Helper functions for UDP connections::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Client example with anonymous authentication
Packit aea12f
@subsection Client example with anonymous authentication
Packit aea12f
Packit aea12f
The simplest client using TLS is the one that doesn't do any
Packit aea12f
authentication.  This means no external certificates or passwords are
Packit aea12f
needed to set up the connection.  As could be expected, the connection
Packit aea12f
is vulnerable to man-in-the-middle (active or redirection) attacks.
Packit aea12f
However, the data are integrity protected and encrypted from
Packit aea12f
passive eavesdroppers.
Packit aea12f
Packit aea12f
Note that due to the vulnerable nature of this method very few public
Packit aea12f
servers support it.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-client-anon.c
Packit aea12f
Packit aea12f
@node Using a callback to select the certificate to use
Packit aea12f
@subsection Using a callback to select the certificate to use
Packit aea12f
Packit aea12f
There are cases where a client holds several certificate and key
Packit aea12f
pairs, and may not want to load all of them in the credentials
Packit aea12f
structure.  The following example demonstrates the use of the
Packit aea12f
certificate selection callback.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-cert-select.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Obtaining session information
Packit aea12f
@subsection Obtaining session information
Packit aea12f
Packit aea12f
Most of the times it is desirable to know the security properties of
Packit aea12f
the current established session.  This includes the underlying ciphers
Packit aea12f
and the protocols involved.  That is the purpose of the following
Packit aea12f
function.  Note that this function will print meaningful values only
Packit aea12f
if called after a successful @funcref{gnutls_handshake}.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-session-info.c
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
@node Advanced certificate verification example
Packit aea12f
@subsection Advanced certificate verification
Packit aea12f
@anchor{ex-verify2}
Packit aea12f
Packit aea12f
An example is listed below which uses the high level verification
Packit aea12f
functions to verify a given certificate chain against a set of CAs
Packit aea12f
and CRLs.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-verify.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Client example with PSK authentication
Packit aea12f
@subsection Client example with @acronym{PSK} authentication
Packit aea12f
Packit aea12f
The following client is a very simple @acronym{PSK} @acronym{TLS}
Packit aea12f
client which connects to a server and authenticates using a
Packit aea12f
@emph{username} and a @emph{key}.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-client-psk.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Client example with SRP authentication
Packit aea12f
@subsection Client example with @acronym{SRP} authentication
Packit aea12f
Packit aea12f
The following client is a very simple @acronym{SRP} @acronym{TLS}
Packit aea12f
client which connects to a server and authenticates using a
Packit aea12f
@emph{username} and a @emph{password}. The server may authenticate
Packit aea12f
itself using a certificate, and in that case it has to be verified.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-client-srp.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Legacy client example with X.509 certificate support
Packit aea12f
@subsection Legacy client example with @acronym{X.509} certificate support
Packit aea12f
@anchor{ex-verify-legacy}
Packit aea12f
Packit aea12f
For applications that need to maintain compatibility with the GnuTLS 3.1.x
Packit aea12f
library, this client example is identical to @ref{Client example with X.509 certificate support}
Packit aea12f
but utilizes APIs that were available in GnuTLS 3.1.4. 
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-client-x509-3.1.c
Packit aea12f
Packit aea12f
@node Client example in C++
Packit aea12f
@subsection Client example using the C++ API
Packit aea12f
Packit aea12f
The following client is a simple example of a client client utilizing
Packit aea12f
the GnuTLS C++ API.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-cxx.cpp
Packit aea12f
Packit aea12f
Packit aea12f
@node Echo server with PSK authentication
Packit aea12f
@subsection Echo server with @acronym{PSK} authentication
Packit aea12f
Packit aea12f
This is a server which supports @acronym{PSK} authentication.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-serv-psk.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Echo server with SRP authentication
Packit aea12f
@subsection Echo server with @acronym{SRP} authentication
Packit aea12f
Packit aea12f
This is a server which supports @acronym{SRP} authentication. It is
Packit aea12f
also possible to combine this functionality with a certificate
Packit aea12f
server. Here it is separate for simplicity.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-serv-srp.c
Packit aea12f
Packit aea12f
Packit aea12f
@node Echo server with anonymous authentication
Packit aea12f
@subsection Echo server with anonymous authentication
Packit aea12f
Packit aea12f
This example server supports anonymous authentication, and could be
Packit aea12f
used to serve the example client for anonymous authentication.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-serv-anon.c
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
@node Helper functions for TCP connections
Packit aea12f
@subsection Helper functions for TCP connections
Packit aea12f
Packit aea12f
Those helper function abstract away TCP connection handling from the
Packit aea12f
other examples.  It is required to build some examples.
Packit aea12f
Packit aea12f
@verbatiminclude examples/tcp.c
Packit aea12f
Packit aea12f
@node Helper functions for UDP connections
Packit aea12f
@subsection Helper functions for UDP connections
Packit aea12f
Packit aea12f
The UDP helper functions abstract away UDP connection handling from the
Packit aea12f
other examples.  It is required to build the examples using UDP.
Packit aea12f
Packit aea12f
@verbatiminclude examples/udp.c
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
@node OCSP example
Packit aea12f
@section OCSP example
Packit aea12f
Packit aea12f
@anchor{Generate OCSP request}
Packit aea12f
@subheading Generate @acronym{OCSP} request
Packit aea12f
Packit aea12f
A small tool to generate OCSP requests.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-ocsp-client.c
Packit aea12f
Packit aea12f
@node Miscellaneous examples
Packit aea12f
@section Miscellaneous examples
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Checking for an alert::
Packit aea12f
* X.509 certificate parsing example::
Packit aea12f
* Listing the ciphersuites in a priority string::
Packit aea12f
* PKCS12 structure generation example::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Checking for an alert
Packit aea12f
@subsection Checking for an alert
Packit aea12f
Packit aea12f
This is a function that checks if an alert has been received in the
Packit aea12f
current session.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-alert.c
Packit aea12f
Packit aea12f
@node X.509 certificate parsing example
Packit aea12f
@subsection @acronym{X.509} certificate parsing example
Packit aea12f
@anchor{ex-x509-info}
Packit aea12f
Packit aea12f
To demonstrate the @acronym{X.509} parsing capabilities an example program is
Packit aea12f
listed below.  That program reads the peer's certificate, and prints
Packit aea12f
information about it.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-x509-info.c
Packit aea12f
Packit aea12f
@node Listing the ciphersuites in a priority string
Packit aea12f
@subsection Listing the ciphersuites in a priority string
Packit aea12f
Packit aea12f
This is a small program to list the enabled ciphersuites by a 
Packit aea12f
priority string.
Packit aea12f
Packit aea12f
@verbatiminclude examples/print-ciphersuites.c
Packit aea12f
Packit aea12f
@node PKCS12 structure generation example
Packit aea12f
@subsection PKCS #12 structure generation example
Packit aea12f
Packit aea12f
This small program demonstrates the usage of the PKCS #12 API, by generating
Packit aea12f
such a structure.
Packit aea12f
Packit aea12f
@verbatiminclude examples/ex-pkcs12.c
Packit aea12f