Blame doc/cha-gtls-app.texi

Packit aea12f
@node How to use GnuTLS in applications
Packit aea12f
@chapter How to use @acronym{GnuTLS} in applications
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Introduction to the library::
Packit aea12f
* Preparation::
Packit aea12f
* Session initialization::
Packit aea12f
* Associating the credentials::
Packit aea12f
* Setting up the transport layer::
Packit aea12f
* TLS handshake::
Packit aea12f
* Data transfer and termination::
Packit aea12f
* Buffered data transfer::
Packit aea12f
* Handling alerts::
Packit aea12f
* Priority Strings::
Packit aea12f
* Selecting cryptographic key sizes::
Packit aea12f
* Advanced topics::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Introduction to the library
Packit aea12f
@section Introduction
Packit aea12f
Packit aea12f
This chapter tries to explain the basic functionality of the current GnuTLS
Packit aea12f
library. Note that there may be additional functionality not discussed here
Packit aea12f
but included in the library. Checking the header files in @file{/usr/include/gnutls/}
Packit aea12f
and the manpages is recommended.
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* General idea::
Packit aea12f
* Error handling::
Packit aea12f
* Common types::
Packit aea12f
* Debugging and auditing::
Packit aea12f
* Thread safety::
Packit aea12f
* Running in a sandbox::
Packit aea12f
* Sessions and fork::
Packit aea12f
* Callback functions::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node General idea
Packit aea12f
@subsection General idea
Packit aea12f
Packit aea12f
A brief description of how @acronym{GnuTLS} sessions operate is shown
Packit aea12f
at @ref{fig-gnutls-design}. This section will become more clear when it
Packit aea12f
is completely read.
Packit aea12f
As shown in the figure, there is a read-only global state that is
Packit aea12f
initialized once by the global initialization function.  This global
Packit aea12f
structure, among others, contains the memory allocation functions
Packit aea12f
used, structures needed for the @acronym{ASN.1} parser and depending
Packit aea12f
on the system's CPU, pointers to hardware accelerated encryption functions.  This
Packit aea12f
structure is never modified by any @acronym{GnuTLS} function, except
Packit aea12f
for the deinitialization function which frees all allocated memory
Packit aea12f
and must be called after the program has permanently
Packit aea12f
finished using @acronym{GnuTLS}.
Packit aea12f
Packit aea12f
@float Figure,fig-gnutls-design
Packit aea12f
@image{gnutls-internals,12cm}
Packit aea12f
@caption{High level design of GnuTLS.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
The credentials structures are used by the authentication methods, such
Packit aea12f
as certificate authentication. They store certificates, privates keys,
Packit aea12f
and other information that is needed to prove the identity to the peer,
Packit aea12f
and/or verify the identity of the peer. The information stored in
Packit aea12f
the credentials structures is initialized once and then can be 
Packit aea12f
shared by many @acronym{TLS} sessions.
Packit aea12f
Packit aea12f
A @acronym{GnuTLS} session contains all the required state and 
Packit aea12f
information to handle one secure connection. The session communicates with the
Packit aea12f
peers using the provided functions of the transport layer.
Packit aea12f
Every session has a unique session ID shared with the peer.
Packit aea12f
Packit aea12f
Since TLS sessions can be resumed, servers need a
Packit aea12f
database back-end to hold the session's parameters.  Every
Packit aea12f
@acronym{GnuTLS} session after a successful handshake calls the
Packit aea12f
appropriate back-end function (see @ref{resume})
Packit aea12f
to store the newly negotiated session. The session
Packit aea12f
database is examined by the server just after having received the
Packit aea12f
client hello@footnote{The first message in a @acronym{TLS} handshake},
Packit aea12f
and if the session ID sent by the client, matches a stored session,
Packit aea12f
the stored session will be retrieved, and the new session will be a
Packit aea12f
resumed one, and will share the same session ID with the previous one.
Packit aea12f
Packit aea12f
@node Error handling
Packit aea12f
@subsection Error handling
Packit aea12f
Packit aea12f
There two types of @acronym{GnuTLS} functions. The first type returns
Packit aea12f
a boolean value, true (non-zero) or false (zero) value; these functions
Packit aea12f
are defined to return an unsigned integer type. The other type returns a
Packit aea12f
signed integer type with zero (or a positive number) indicating
Packit aea12f
success and a negative value indicating failure. For the latter
Packit aea12f
type it is recommended to check for errors as following.
Packit aea12f
@example
Packit aea12f
    ret = gnutls_function();
Packit aea12f
    if (ret < 0) @{
Packit aea12f
        return -1;
Packit aea12f
    @}
Packit aea12f
@end example
Packit aea12f
The above example checks for a failure condition rather than
Packit aea12f
for explicit success (e.g., equality to zero). That has the advantage
Packit aea12f
that future extensions of the API can be extended to provide
Packit aea12f
additional information via positive returned values (see for example
Packit aea12f
@funcref{gnutls_certificate_set_x509_key_file}).
Packit aea12f
Packit aea12f
For certain operations such as TLS handshake and TLS packet receive
Packit aea12f
there is the notion of fatal and non-fatal error codes.
Packit aea12f
Fatal errors terminate the TLS session immediately and further sends
Packit aea12f
and receives will be disallowed.  Such an example is
Packit aea12f
@code{GNUTLS_@-E_@-DECRYPTION_@-FAILED}. Non-fatal errors may warn about
Packit aea12f
something, i.e., a warning alert was received, or indicate the some
Packit aea12f
action has to be taken. This is the case with the error code
Packit aea12f
@code{GNUTLS_@-E_@-REHANDSHAKE} returned by @funcref{gnutls_record_recv}.
Packit aea12f
This error code indicates that the server requests a re-handshake. The
Packit aea12f
client may ignore this request, or may reply with an alert.  You can
Packit aea12f
test if an error code is a fatal one by using the
Packit aea12f
@funcref{gnutls_error_is_fatal}.
Packit aea12f
All errors can be converted to a descriptive string using @funcref{gnutls_strerror}.
Packit aea12f
Packit aea12f
If any non fatal errors, that require an action, are to be returned by
Packit aea12f
a function, these error codes will be documented in the function's
Packit aea12f
reference. For example the error codes @code{GNUTLS_@-E_@-WARNING_@-ALERT_@-RECEIVED} and @code{GNUTLS_@-E_@-FATAL_@-ALERT_@-RECEIVED}
Packit aea12f
that may returned when receiving data, should be handled by notifying the
Packit aea12f
user of the alert (as explained in @ref{Handling alerts}).
Packit aea12f
See @ref{Error codes}, for a description of the available error codes.
Packit aea12f
Packit aea12f
@node Common types
Packit aea12f
@subsection Common types
Packit aea12f
@cindex gnutls_datum_t
Packit aea12f
@cindex giovec_t
Packit aea12f
Packit aea12f
All strings that are to provided as input to @acronym{GnuTLS} functions
Packit aea12f
should be in UTF-8 unless otherwise specified. Output strings are also
Packit aea12f
in UTF-8 format unless otherwise specified. When functions take as input
Packit aea12f
passwords, they will normalize them using @xcite{RFC7613} rules (since
Packit aea12f
GnuTLS 3.5.7).
Packit aea12f
Packit aea12f
When data of a fixed size are provided to @acronym{GnuTLS} functions then
Packit aea12f
the helper structure @code{gnutls_datum_t} is often used. Its definition is
Packit aea12f
shown below.
Packit aea12f
@verbatim
Packit aea12f
  typedef struct
Packit aea12f
  {
Packit aea12f
    unsigned char *data;
Packit aea12f
    unsigned int size;
Packit aea12f
  } gnutls_datum_t;
Packit aea12f
@end verbatim
Packit aea12f
Packit aea12f
In functions where this structure is a returned type, if the function succeeds,
Packit aea12f
it is expected from the caller to use @code{gnutls_free()} to deinitialize the
Packit aea12f
data element after use, unless otherwise specified. If the function fails, the
Packit aea12f
contents of the @code{gnutls_datum_t} should be considered undefined and must
Packit aea12f
not be deinitialized.
Packit aea12f
Packit aea12f
Other functions that require data for scattered read use a structure similar
Packit aea12f
to @code{struct iovec} typically used by @funcintref{readv}. It is shown
Packit aea12f
below.
Packit aea12f
@verbatim
Packit aea12f
  typedef struct
Packit aea12f
  {
Packit aea12f
    void *iov_base;             /* Starting address */
Packit aea12f
    size_t iov_len;             /* Number of bytes to transfer */
Packit aea12f
  } giovec_t;
Packit aea12f
@end verbatim
Packit aea12f
Packit aea12f
Packit aea12f
@node Debugging and auditing
Packit aea12f
@subsection Debugging and auditing
Packit aea12f
Packit aea12f
In many cases things may not go as expected and further information,
Packit aea12f
to assist debugging, from @acronym{GnuTLS} is desired. 
Packit aea12f
Those are the cases where the @funcref{gnutls_global_set_log_level} and
Packit aea12f
@funcref{gnutls_global_set_log_function} are to be used. Those will print
Packit aea12f
verbose information on the @acronym{GnuTLS} functions internal flow.
Packit aea12f
Packit aea12f
@showfuncB{gnutls_global_set_log_level,gnutls_global_set_log_function}
Packit aea12f
Packit aea12f
Alternatively the environment variable @code{GNUTLS_DEBUG_LEVEL} can be
Packit aea12f
set to a logging level and GnuTLS will output debugging output to standard
Packit aea12f
error. Other available environment variables are shown in @ref{tab:environment}.
Packit aea12f
Packit aea12f
@float Table,tab:environment
Packit aea12f
@multitable @columnfractions .30 .70
Packit aea12f
Packit aea12f
@headitem Variable @tab Purpose
Packit aea12f
Packit aea12f
@item @code{GNUTLS_DEBUG_LEVEL}
Packit aea12f
@tab When set to a numeric value, it sets the default debugging level for GnuTLS applications.
Packit aea12f
Packit aea12f
@item @code{SSLKEYLOGFILE}
Packit aea12f
@tab When set to a filename, GnuTLS will append to it the session keys in the NSS Key Log
Packit aea12f
format. That format can be read by wireshark and will allow decryption of the session for debugging.
Packit aea12f
Packit aea12f
@item @code{GNUTLS_CPUID_OVERRIDE}
Packit aea12f
@tab That environment variable can be used to
Packit aea12f
explicitly enable/disable the use of certain CPU capabilities. Note that CPU
Packit aea12f
detection cannot be overridden, i.e., VIA options cannot be enabled on an Intel
Packit aea12f
CPU. The currently available options are:
Packit aea12f
@itemize
Packit aea12f
@item 0x1: Disable all run-time detected optimizations
Packit aea12f
@item 0x2: Enable AES-NI
Packit aea12f
@item 0x4: Enable SSSE3
Packit aea12f
@item 0x8: Enable PCLMUL
Packit aea12f
@item 0x10: Enable AVX
Packit Service 991b93
@item 0x20: Enable SHA_NI
Packit aea12f
@item 0x100000: Enable VIA padlock
Packit aea12f
@item 0x200000: Enable VIA PHE
Packit aea12f
@item 0x400000: Enable VIA PHE SHA512
Packit aea12f
@end itemize
Packit aea12f
Packit aea12f
@item @code{GNUTLS_FORCE_FIPS_MODE}
Packit aea12f
@tab In setups where GnuTLS is compiled with support for FIPS140-2 (see @ref{FIPS140-2 mode})
Packit aea12f
if set to one it will force the FIPS mode enablement.
Packit aea12f
Packit aea12f
@end multitable
Packit aea12f
@caption{Environment variables used by the library.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
Packit aea12f
When debugging is not required, important issues, such as detected
Packit aea12f
attacks on the protocol still need to be logged. This is provided
Packit aea12f
by the logging function set by
Packit aea12f
@funcref{gnutls_global_set_audit_log_function}. The provided function
Packit aea12f
will receive an message and the corresponding
Packit aea12f
TLS session. The session information might be used to derive IP addresses
Packit aea12f
or other information about the peer involved.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_global_set_audit_log_function}
Packit aea12f
Packit aea12f
@node Thread safety
Packit aea12f
@subsection Thread safety
Packit aea12f
@cindex thread safety
Packit aea12f
Packit aea12f
The @acronym{GnuTLS} library is thread safe by design, meaning that
Packit aea12f
objects of the library such as TLS sessions, can be safely divided across
Packit aea12f
threads as long as a single thread accesses a single object. This is
Packit aea12f
sufficient to support a server which handles several sessions per thread.
Packit aea12f
Read-only access to objects, for example the credentials holding structures,
Packit aea12f
is also thread-safe. 
Packit aea12f
Packit aea12f
A @code{gnutls_session_t} object could also be shared by two threads, one sending,
Packit aea12f
the other receiving. However, care must be taken on the following use cases:
Packit aea12f
@itemize
Packit aea12f
@item The re-handshake process in TLS 1.2 or earlier must be handled only in
Packit aea12f
a single thread and no other thread may be performing any operation.
Packit aea12f
@item The flag @code{GNUTLS_AUTO_REAUTH} cannot be used safely in this mode of operation.
Packit aea12f
@item Any other operation which may send or receive data, like key update (c.f.,
Packit aea12f
@funcref{gnutls_session_key_update}), must not be performed while threads
Packit aea12f
are receiving or writing.
Packit aea12f
@item The termination of a session should be handled, either by a single thread being
Packit aea12f
active, or by the sender thread using @funcref{gnutls_bye} with @code{GNUTLS_SHUT_WR}
Packit aea12f
and the receiving thread waiting for a return value of zero (or timeout on
Packit aea12f
certain servers which do not respond).
Packit aea12f
@item The functions @funcref{gnutls_transport_set_errno} and @funcref{gnutls_record_get_direction}
Packit aea12f
should not be relied during parallel operation.
Packit aea12f
@end itemize
Packit aea12f
Packit aea12f
For several aspects of the library (e.g., the random generator, PKCS#11
Packit aea12f
operations), the library may utilize mutex locks (e.g., pthreads on GNU/Linux and CriticalSection on Windows)
Packit aea12f
which are transparently setup on library initialization. Prior to version 3.3.0
Packit aea12f
these were setup by explicitly calling @funcref{gnutls_global_init}.@footnote{On special systems
Packit aea12f
you could manually specify the locking system using
Packit aea12f
the function @funcref{gnutls_global_set_mutex} before calling any other
Packit aea12f
GnuTLS function. Setting mutexes manually is not recommended.}
Packit aea12f
Packit aea12f
Note that, on Glibc systems, unless the application is explicitly linked
Packit aea12f
with the libpthread library, no mutex locks are used and setup by GnuTLS. It 
Packit aea12f
will use the Glibc mutex stubs.
Packit aea12f
Packit aea12f
@node Running in a sandbox
Packit aea12f
@subsection Running in a sandbox
Packit aea12f
@cindex seccomp
Packit aea12f
@cindex isolated mode
Packit aea12f
Packit aea12f
Given that TLS protocol handling as well as X.509 certificate
Packit aea12f
parsing are complicated processes involving several thousands lines of code,
Packit aea12f
it is often desirable (and recommended) to run the TLS session handling in
Packit aea12f
a sandbox like seccomp. That has to be allowed by the overall software design,
Packit aea12f
but if available, it adds an additional layer of protection by
Packit aea12f
preventing parsing errors from becoming vessels for further security issues such
Packit aea12f
as code execution.
Packit aea12f
Packit aea12f
GnuTLS requires the following system calls to be available for its proper
Packit aea12f
operation.
Packit aea12f
Packit aea12f
@itemize
Packit aea12f
@item nanosleep
Packit aea12f
@item time
Packit aea12f
@item gettimeofday
Packit aea12f
@item clock_gettime
Packit aea12f
@item getrusage
Packit aea12f
@item getpid
Packit aea12f
@item send
Packit aea12f
@item recv
Packit aea12f
@item sendmsg
Packit aea12f
@item read (to read from /dev/urandom)
Packit aea12f
@item getrandom (this is Linux-kernel specific)
Packit aea12f
@item poll
Packit aea12f
@end itemize
Packit aea12f
Packit aea12f
As well as any calls needed for memory allocation to work. Note however, that GnuTLS
Packit aea12f
depends on libc for the system calls, and there is no guarantee that libc will
Packit aea12f
call the expected system call. For that it is recommended to test your
Packit aea12f
program in all the targeted platforms when filters like seccomp are in place.
Packit aea12f
Packit aea12f
An example with a seccomp filter from GnuTLS' test suite is at:
Packit aea12f
@url{https://gitlab.com/gnutls/gnutls/blob/master/tests/seccomp.c}.
Packit aea12f
Packit aea12f
@node Sessions and fork
Packit aea12f
@subsection Sessions and fork
Packit aea12f
@cindex fork
Packit aea12f
Packit aea12f
A @code{gnutls_session_t} object can be shared by two processes after a fork,
Packit aea12f
one sending, the other receiving. In that case rehandshakes, 
Packit aea12f
cannot and must not be performed. As with threads, the termination of a session should be
Packit aea12f
handled by the sender process using @funcref{gnutls_bye} with @code{GNUTLS_SHUT_WR}
Packit aea12f
and the receiving process waiting for a return value of zero.
Packit aea12f
Packit aea12f
Packit aea12f
@node Callback functions
Packit aea12f
@subsection Callback functions
Packit aea12f
@cindex callback functions
Packit aea12f
Packit aea12f
There are several cases where @acronym{GnuTLS} may need out of
Packit aea12f
band input from your program. This is now implemented using some
Packit aea12f
callback functions, which your program is expected to register.
Packit aea12f
Packit aea12f
An example of this type of functions are the push and pull callbacks
Packit aea12f
which are used to specify the functions that will retrieve and send
Packit aea12f
data to the transport layer.
Packit aea12f
Packit aea12f
@showfuncB{gnutls_transport_set_push_function,gnutls_transport_set_pull_function}
Packit aea12f
Packit aea12f
Other callback functions may require more complicated input and data
Packit aea12f
to be allocated. Such an example is 
Packit aea12f
@funcref{gnutls_srp_set_server_credentials_function}.
Packit aea12f
All callbacks should allocate and free memory using 
Packit aea12f
@funcintref{gnutls_malloc} and @funcintref{gnutls_free}.
Packit aea12f
Packit aea12f
Packit aea12f
@node Preparation
Packit aea12f
@section Preparation
Packit aea12f
Packit aea12f
To use @acronym{GnuTLS}, you have to perform some changes to your
Packit aea12f
sources and your build system. The necessary changes are explained in
Packit aea12f
the following subsections.
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Headers::
Packit aea12f
* Initialization::
Packit aea12f
* Version check::
Packit aea12f
* Building the source::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Headers
Packit aea12f
@subsection Headers
Packit aea12f
Packit aea12f
All the data types and functions of the @acronym{GnuTLS} library are
Packit aea12f
defined in the header file @file{gnutls/gnutls.h}.  This must be
Packit aea12f
included in all programs that make use of the @acronym{GnuTLS}
Packit aea12f
library.
Packit aea12f
Packit aea12f
@node Initialization
Packit aea12f
@subsection Initialization
Packit aea12f
Packit aea12f
The GnuTLS library is initialized on load; prior to 3.3.0 was initialized by calling @funcref{gnutls_global_init}@footnote{
Packit aea12f
The original behavior of requiring explicit initialization can obtained by setting the
Packit aea12f
GNUTLS_NO_EXPLICIT_INIT environment variable to 1, or by using the macro GNUTLS_SKIP_GLOBAL_INIT
Packit aea12f
in a global section of your program --the latter works in systems with
Packit Service 991b93
support for weak symbols only.}. @funcref{gnutls_global_init} in
Packit Service 991b93
versions after 3.3.0 is thread-safe (see @ref{Thread safety}).
Packit Service 991b93
Packit aea12f
The initialization typically enables CPU-specific acceleration, performs any required
Packit aea12f
precalculations needed, opens any required system devices (e.g., /dev/urandom on Linux)
Packit aea12f
and initializes subsystems that could be used later.
Packit aea12f
Packit aea12f
The resources allocated by the initialization process will be released 
Packit aea12f
on library deinitialization.
Packit aea12f
Packit aea12f
Note that on certain systems file descriptors may be kept open by
Packit aea12f
GnuTLS (e.g. /dev/urandom) on library load. Applications closing all unknown file
Packit aea12f
descriptors must immediately call @funcref{gnutls_global_init}, after that, to
Packit aea12f
ensure they don't disrupt GnuTLS' operation.
Packit aea12f
Packit aea12f
@c In order to take advantage of the internationalization features in
Packit aea12f
@c GnuTLS, such as translated error messages, the application must set
Packit aea12f
@c the current locale using @code{setlocale} before initializing GnuTLS.
Packit aea12f
Packit aea12f
@node Version check
Packit aea12f
@subsection Version check
Packit aea12f
Packit aea12f
It is often desirable to check that the version of `gnutls' used is
Packit aea12f
indeed one which fits all requirements.  Even with binary
Packit aea12f
compatibility new features may have been introduced but due to problem
Packit aea12f
with the dynamic linker an old version is actually used.  So you may
Packit aea12f
want to check that the version is okay right after program start-up.
Packit aea12f
See the function @funcref{gnutls_check_version}.
Packit aea12f
Packit aea12f
On the other hand, it is often desirable to support more than one
Packit aea12f
versions of the library. In that case you could utilize compile-time 
Packit aea12f
feature checks using the @code{GNUTLS_VERSION_NUMBER} macro. 
Packit aea12f
For example, to conditionally add code for GnuTLS 3.2.1 or later, you may use:
Packit aea12f
@example
Packit aea12f
#if GNUTLS_VERSION_NUMBER >= 0x030201
Packit aea12f
 ...
Packit aea12f
#endif
Packit aea12f
@end example
Packit aea12f
Packit aea12f
@node Building the source
Packit aea12f
@subsection Building the source
Packit aea12f
Packit aea12f
If you want to compile a source file including the
Packit aea12f
@file{gnutls/gnutls.h} header file, you must make sure that the
Packit aea12f
compiler can find it in the directory hierarchy.  This is accomplished
Packit aea12f
by adding the path to the directory in which the header file is
Packit aea12f
located to the compilers include file search path (via the @option{-I}
Packit aea12f
option).
Packit aea12f
Packit aea12f
However, the path to the include file is determined at the time the
Packit aea12f
source is configured.  To solve this problem, the library uses the
Packit aea12f
external package @command{pkg-config} that knows the path to the
Packit aea12f
include file and other configuration options.  The options that need
Packit aea12f
to be added to the compiler invocation at compile time are output by
Packit aea12f
the @option{--cflags} option to @command{pkg-config gnutls}.  The
Packit aea12f
following example shows how it can be used at the command line:
Packit aea12f
Packit aea12f
@example
Packit aea12f
gcc -c foo.c `pkg-config gnutls --cflags`
Packit aea12f
@end example
Packit aea12f
Packit aea12f
Adding the output of @samp{pkg-config gnutls --cflags} to the
Packit aea12f
compilers command line will ensure that the compiler can find the
Packit aea12f
@file{gnutls/gnutls.h} header file.
Packit aea12f
Packit aea12f
A similar problem occurs when linking the program with the library.
Packit aea12f
Again, the compiler has to find the library files.  For this to work,
Packit aea12f
the path to the library files has to be added to the library search
Packit aea12f
path (via the @option{-L} option).  For this, the option
Packit aea12f
@option{--libs} to @command{pkg-config gnutls} can be used.  For
Packit aea12f
convenience, this option also outputs all other options that are
Packit aea12f
required to link the program with the library (for instance, the
Packit aea12f
@samp{-ltasn1} option).  The example shows how to link @file{foo.o}
Packit aea12f
with the library to a program @command{foo}.
Packit aea12f
Packit aea12f
@example
Packit aea12f
gcc -o foo foo.o `pkg-config gnutls --libs`
Packit aea12f
@end example
Packit aea12f
Packit aea12f
Of course you can also combine both examples to a single command by
Packit aea12f
specifying both options to @command{pkg-config}:
Packit aea12f
Packit aea12f
@example
Packit aea12f
gcc -o foo foo.c `pkg-config gnutls --cflags --libs`
Packit aea12f
@end example
Packit aea12f
Packit aea12f
When a program uses the GNU autoconf system, then the following
Packit aea12f
line or similar can be used to detect the presence of GnuTLS.
Packit aea12f
Packit aea12f
@example
Packit aea12f
PKG_CHECK_MODULES([LIBGNUTLS], [gnutls >= 3.3.0])
Packit aea12f
Packit aea12f
AC_SUBST([LIBGNUTLS_CFLAGS])
Packit aea12f
AC_SUBST([LIBGNUTLS_LIBS])
Packit aea12f
@end example
Packit aea12f
Packit aea12f
@node Session initialization
Packit aea12f
@section Session initialization
Packit aea12f
Packit aea12f
In the previous sections we have discussed the global initialization
Packit aea12f
required for GnuTLS as well as the initialization required for each
Packit aea12f
authentication method's credentials (see @ref{Authentication}).
Packit aea12f
In this section we elaborate on the TLS or DTLS session initiation.
Packit aea12f
Each session is initialized using @funcref{gnutls_init} which among
Packit aea12f
others is used to specify the type of the connection (server or client), 
Packit aea12f
and the underlying protocol type, i.e., datagram (UDP) or reliable (TCP).
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_init}
Packit aea12f
Packit aea12f
@showenumdesc{gnutls_init_flags_t,The @code{gnutls_init_@-flags_t} enumeration.}
Packit aea12f
Packit aea12f
After the session initialization details on the allowed ciphersuites
Packit aea12f
and protocol versions should be set using the priority functions
Packit aea12f
such as @funcref{gnutls_priority_set} and @funcref{gnutls_priority_set_direct}.
Packit aea12f
We elaborate on them in @ref{Priority Strings}.
Packit aea12f
The credentials used for the key exchange method, such as certificates 
Packit aea12f
or usernames and passwords should also be associated with the session
Packit aea12f
current session using @funcref{gnutls_credentials_set}. 
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_credentials_set}
Packit aea12f
Packit aea12f
@node Associating the credentials
Packit aea12f
@section Associating the credentials
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Certificate credentials::
Packit aea12f
* Raw public-key credentials::
Packit aea12f
* SRP credentials::
Packit aea12f
* PSK credentials::
Packit aea12f
* Anonymous credentials::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
Each authentication method is associated with a key exchange method, and a credentials type. 
Packit aea12f
The contents of the credentials is method-dependent, e.g. certificates
Packit aea12f
for certificate authentication and should be initialized and associated
Packit aea12f
with a session (see @funcref{gnutls_credentials_set}).  A mapping of the key exchange methods
Packit aea12f
with the credential types is shown in @ref{tab:key-exchange-cred}.
Packit aea12f
Packit aea12f
@float Table,tab:key-exchange-cred
Packit aea12f
@multitable @columnfractions .25 .25 .2 .2
Packit aea12f
Packit aea12f
@headitem Authentication method @tab Key exchange @tab Client credentials @tab Server credentials
Packit aea12f
Packit aea12f
@item Certificate and Raw public-key
Packit aea12f
@tab @code{KX_RSA},
Packit aea12f
@code{KX_DHE_RSA},
Packit aea12f
@code{KX_DHE_DSS},
Packit aea12f
@code{KX_ECDHE_RSA},
Packit aea12f
@code{KX_ECDHE_ECDSA}
Packit aea12f
@tab @code{CRD_CERTIFICATE}
Packit aea12f
@tab @code{CRD_CERTIFICATE}
Packit aea12f
Packit aea12f
@item Password and certificate
Packit aea12f
@tab @code{KX_SRP_RSA}, @code{KX_SRP_DSS}
Packit aea12f
@tab @code{CRD_SRP}
Packit aea12f
@tab @code{CRD_CERTIFICATE}, @code{CRD_SRP}
Packit aea12f
Packit aea12f
@item Password
Packit aea12f
@tab @code{KX_SRP}
Packit aea12f
@tab @code{CRD_SRP}
Packit aea12f
@tab @code{CRD_SRP}
Packit aea12f
Packit aea12f
@item Anonymous
Packit aea12f
@tab @code{KX_ANON_DH},
Packit aea12f
@code{KX_ANON_ECDH}
Packit aea12f
@tab @code{CRD_ANON}
Packit aea12f
@tab @code{CRD_ANON}
Packit aea12f
Packit aea12f
@item Pre-shared key
Packit aea12f
@tab @code{KX_PSK},
Packit aea12f
@code{KX_DHE_PSK}, @code{KX_ECDHE_PSK}
Packit aea12f
@tab @code{CRD_PSK}
Packit aea12f
@tab @code{CRD_PSK}
Packit aea12f
Packit aea12f
@end multitable
Packit aea12f
@caption{Key exchange algorithms and the corresponding credential types.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
@node Certificate credentials
Packit aea12f
@subsection Certificates
Packit aea12f
@subsubheading Server certificate authentication
Packit aea12f
Packit aea12f
When using certificates the server is required to have at least one
Packit aea12f
certificate and private key pair. Clients may not hold such
Packit aea12f
a pair, but a server could require it. In this section we discuss
Packit aea12f
general issues applying to both client and server certificates. The next
Packit aea12f
section will elaborate on issues arising from client authentication only.
Packit aea12f
Packit aea12f
In order to use certificate credentials one must first initialize a credentials
Packit aea12f
structure of type @code{gnutls_certificate_credentials_t}. After use this structure must
Packit aea12f
be freed. This can be done with the following functions.
Packit aea12f
Packit aea12f
@showfuncB{gnutls_certificate_allocate_credentials,gnutls_certificate_free_credentials}
Packit aea12f
Packit aea12f
After the credentials structures are initialized, the certificate 
Packit aea12f
and key pair must be loaded. This occurs before any @acronym{TLS} 
Packit aea12f
session is initialized, and the same structures are reused for multiple sessions.
Packit aea12f
Depending on the certificate type different loading functions
Packit aea12f
are available, as shown below.
Packit aea12f
For @acronym{X.509} certificates, the functions will
Packit aea12f
accept and use a certificate chain that leads to a trusted
Packit aea12f
authority. The certificate chain must be ordered in such way that every
Packit aea12f
certificate certifies the one before it. The trusted authority's
Packit aea12f
certificate need not to be included since the peer should possess it
Packit aea12f
already.
Packit aea12f
Packit aea12f
@showfuncC{gnutls_certificate_set_x509_key_file2,gnutls_certificate_set_x509_key_mem2,gnutls_certificate_set_x509_key}
Packit aea12f
Packit aea12f
It is recommended to use the higher level functions such as @funcref{gnutls_certificate_set_x509_key_file2}
Packit aea12f
which accept not only file names but URLs that specify objects stored in token,
Packit aea12f
or system certificates and keys (see @ref{Application-specific keys}). For these cases, another important 
Packit aea12f
function is @funcref{gnutls_certificate_set_pin_function}, that
Packit aea12f
allows setting a callback function to retrieve a PIN if the input keys are
Packit aea12f
protected by PIN.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_certificate_set_pin_function}
Packit aea12f
Packit aea12f
If the imported keys and certificates need to be accessed before any TLS session
Packit aea12f
is established, it is convenient to use @funcref{gnutls_certificate_set_key}
Packit aea12f
in combination with @funcref{gnutls_pcert_import_x509_raw} and @funcref{gnutls_privkey_import_x509_raw}.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_certificate_set_key}
Packit aea12f
Packit aea12f
If multiple certificates are used with the functions above each
Packit aea12f
client's request will be served with the certificate that matches the
Packit aea12f
requested name (see @ref{Server name indication}).
Packit aea12f
Packit aea12f
As an alternative to loading from files or buffers, a callback may be used for the 
Packit aea12f
server or the client to specify the certificate and the key at the handshake time.
Packit aea12f
In that case a certificate should be selected according the peer's signature
Packit aea12f
algorithm preferences. To get those preferences use
Packit aea12f
@funcref{gnutls_sign_algorithm_get_requested}. Both functions are shown below.
Packit aea12f
Packit aea12f
@showfuncD{gnutls_certificate_set_retrieve_function,gnutls_certificate_set_retrieve_function2,gnutls_certificate_set_retrieve_function3,gnutls_sign_algorithm_get_requested}
Packit aea12f
Packit aea12f
The functions above do not handle the requested server name automatically.
Packit aea12f
A server would need to check the name requested by the client
Packit aea12f
using @funcref{gnutls_server_name_get}, and serve the appropriate
Packit aea12f
certificate. Note that some of these functions require the @code{gnutls_pcert_st} structure to be
Packit aea12f
filled in. Helper functions to fill in the structure are listed below.
Packit aea12f
Packit aea12f
@verbatim
Packit aea12f
typedef struct gnutls_pcert_st
Packit aea12f
{
Packit aea12f
  gnutls_pubkey_t pubkey;
Packit aea12f
  gnutls_datum_t cert;
Packit aea12f
  gnutls_certificate_type_t type;
Packit aea12f
} gnutls_pcert_st;
Packit aea12f
@end verbatim
Packit aea12f
Packit aea12f
@showfuncC{gnutls_pcert_import_x509,gnutls_pcert_import_x509_raw,gnutls_pcert_deinit}
Packit aea12f
Packit aea12f
In a handshake, the negotiated cipher suite depends on the
Packit aea12f
certificate's parameters, so some key exchange methods might not be
Packit aea12f
available with all certificates. @acronym{GnuTLS} will disable
Packit aea12f
ciphersuites that are not compatible with the key, or the enabled
Packit aea12f
authentication methods.  For example keys marked as sign-only, will
Packit aea12f
not be able to access the plain RSA ciphersuites, that require
Packit aea12f
decryption. It is not recommended to use RSA keys for both
Packit aea12f
signing and encryption. If possible use a different key for the
Packit aea12f
@code{DHE-RSA} which uses signing and @code{RSA} that requires decryption.
Packit aea12f
All the key exchange methods shown in @ref{tab:key-exchange} are
Packit aea12f
available in certificate authentication.
Packit aea12f
Packit aea12f
Packit aea12f
@subsubheading Client certificate authentication
Packit aea12f
Packit aea12f
If a certificate is to be requested from the client during the handshake, the server
Packit aea12f
will send a certificate request message. This behavior is controlled by @funcref{gnutls_certificate_server_set_request}.
Packit aea12f
The request contains a list of the by the server accepted certificate signers. This list
Packit aea12f
is constructed using the trusted certificate authorities of the server.
Packit aea12f
In cases where the server supports a large number of certificate authorities
Packit aea12f
it makes sense not to advertise all of the names to save bandwidth. That can
Packit aea12f
be controlled using the function @funcref{gnutls_certificate_send_x509_rdn_sequence}. 
Packit aea12f
This however will have the side-effect of not restricting the client to certificates
Packit aea12f
signed by server's acceptable signers.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_certificate_server_set_request}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_certificate_send_x509_rdn_sequence}
Packit aea12f
Packit aea12f
On the client side, it needs to set its certificates on the credentials
Packit aea12f
structure, similarly to server side from a file, or via a callback. Once the
Packit aea12f
certificates are available in the credentials structure, the client will
Packit aea12f
send them if during the handshake the server requests a certificate signed
Packit aea12f
by the issuer of its CA.
Packit aea12f
Packit aea12f
In the case a single certificate is available and the server does not
Packit aea12f
specify a signer's list, then that certificate is always sent. It is,
Packit aea12f
however possible, to send a certificate even when the advertised CA
Packit aea12f
list by the server contains CAs other than its signer. That can be achieved
Packit aea12f
using the @code{GNUTLS_FORCE_CLIENT_CERT} flag in @funcref{gnutls_init}.
Packit aea12f
Packit aea12f
@showfuncC{gnutls_certificate_set_x509_key_file,gnutls_certificate_set_x509_simple_pkcs12_file,gnutls_certificate_set_retrieve_function2}
Packit aea12f
Packit aea12f
Packit aea12f
@subsubheading Client or server certificate verification
Packit aea12f
Packit aea12f
Certificate verification is possible by loading the trusted
Packit aea12f
authorities into the credentials structure by using
Packit aea12f
the following functions, applicable to X.509 certificates.
Packit aea12f
In modern systems it is recommended to utilize @funcref{gnutls_certificate_set_x509_system_trust}
Packit aea12f
which will load the trusted authorities from the system store.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_certificate_set_x509_system_trust}
Packit aea12f
@showfuncB{gnutls_certificate_set_x509_trust_file,gnutls_certificate_set_x509_trust_dir}
Packit aea12f
Packit aea12f
The peer's certificate will be automatically verified if
Packit aea12f
@funcref{gnutls_session_set_verify_cert} is called prior to handshake.
Packit aea12f
Packit aea12f
Alternatively, one must set a callback function during the handshake
Packit aea12f
using @funcref{gnutls_certificate_set_verify_function}, which
Packit aea12f
will verify the peer's certificate once received. The verification
Packit aea12f
should happen using @funcref{gnutls_certificate_verify_peers3} within
Packit aea12f
the callback. It will verify the certificate's signature and the owner 
Packit aea12f
of the certificate. That will provide a brief verification output. If a
Packit aea12f
detailed output is required one should call @funcref{gnutls_certificate_get_peers}
Packit aea12f
to obtain the raw certificate of the peer and verify it using the
Packit aea12f
functions discussed in @ref{X.509 certificates}.
Packit aea12f
Packit aea12f
In both the automatic and the manual cases, the verification status returned
Packit aea12f
can be printed using @funcref{gnutls_certificate_verification_status_print}.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_session_set_verify_cert}
Packit aea12f
Packit aea12f
@showfuncB{gnutls_certificate_verify_peers3,gnutls_certificate_set_verify_function}
Packit aea12f
Packit aea12f
Note that when using raw public-keys verification will not work because there is no corresponding
Packit aea12f
certificate body belonging to the raw key that can be verified. In that case the @funcref{gnutls_certificate_verify_peers}
Packit aea12f
family of functions will return a GNUTLS_E_INVALID_REQUEST error code. For authenticating raw public-keys
Packit aea12f
one must use an out-of-band mechanism, e.g. by comparing hashes or using trust on first use
Packit aea12f
(see @ref{Verifying a certificate using trust on first use authentication}).
Packit aea12f
Packit aea12f
Packit aea12f
@node Raw public-key credentials
Packit aea12f
@subsection Raw public-keys
Packit aea12f
As of version 3.6.6 GnuTLS supports @ref{Raw public-keys}. With raw public-keys only the
Packit aea12f
public-key part (that is normally embedded in a certificate) is transmitted to the peer.
Packit aea12f
In order to load a raw public-key and its corresponding private key in a credentials
Packit aea12f
structure one can use the following functions.
Packit aea12f
Packit aea12f
@showfuncC{gnutls_certificate_set_key,gnutls_certificate_set_rawpk_key_mem,gnutls_certificate_set_rawpk_key_file}
Packit aea12f
Packit aea12f
Packit aea12f
@node SRP credentials
Packit aea12f
@subsection SRP
Packit aea12f
Packit aea12f
The initialization functions in SRP credentials differ between
Packit aea12f
client and server.
Packit aea12f
Clients supporting @acronym{SRP} should set the username and password
Packit aea12f
prior to connection, to the credentials structure.
Packit aea12f
Alternatively @funcref{gnutls_srp_set_client_credentials_function}
Packit aea12f
may be used instead, to specify a callback function that should return the
Packit aea12f
SRP username and password.
Packit aea12f
The callback is called once during the @acronym{TLS} handshake.
Packit aea12f
Packit aea12f
@showfuncE{gnutls_srp_allocate_server_credentials,gnutls_srp_allocate_client_credentials,gnutls_srp_free_server_credentials,gnutls_srp_free_client_credentials,gnutls_srp_set_client_credentials}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_srp_set_client_credentials_function}
Packit aea12f
Packit aea12f
In server side the default behavior of @acronym{GnuTLS} is to read
Packit aea12f
the usernames and @acronym{SRP} verifiers from password files. These
Packit aea12f
password file format is compatible the with the @emph{Stanford srp libraries}
Packit aea12f
format.  If a different password file format is to be used, then 
Packit aea12f
@funcref{gnutls_srp_set_server_credentials_function} should be called,
Packit aea12f
to set an appropriate callback. 
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_srp_set_server_credentials_file}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_srp_set_server_credentials_function}
Packit aea12f
Packit aea12f
Packit aea12f
@node PSK credentials
Packit aea12f
@subsection PSK
Packit aea12f
The initialization functions in PSK credentials differ between
Packit aea12f
client and server.
Packit aea12f
Packit aea12f
@showfuncD{gnutls_psk_allocate_server_credentials,gnutls_psk_allocate_client_credentials,gnutls_psk_free_server_credentials,gnutls_psk_free_client_credentials}
Packit aea12f
Packit aea12f
Clients supporting @acronym{PSK} should supply the username and key
Packit aea12f
before a TLS session is established.  Alternatively 
Packit aea12f
@funcref{gnutls_psk_set_client_credentials_function} can be used to
Packit aea12f
specify a callback function. This has the
Packit aea12f
advantage that the callback will be called only if @acronym{PSK} has
Packit aea12f
been negotiated.
Packit aea12f
Packit aea12f
@showfuncA{gnutls_psk_set_client_credentials}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_psk_set_client_credentials_function}
Packit aea12f
Packit aea12f
In server side the default behavior of @acronym{GnuTLS} is to read
Packit aea12f
the usernames and @acronym{PSK} keys from a password file. The
Packit aea12f
password file should contain usernames and keys in hexadecimal
Packit aea12f
format. The name of the password file can be stored to the credentials
Packit aea12f
structure by calling @funcref{gnutls_psk_set_server_credentials_file}.  If
Packit aea12f
a different password file format is to be used, then
Packit aea12f
a callback should be set instead by @funcref{gnutls_psk_set_server_credentials_function}.
Packit aea12f
Packit aea12f
The server can help the client chose a suitable username and password,
Packit aea12f
by sending a hint. Note that there is no common profile for the PSK hint and applications
Packit aea12f
are discouraged to use it.
Packit aea12f
A server, may specify the hint by calling
Packit aea12f
@funcref{gnutls_psk_set_server_credentials_hint}.  The client can retrieve
Packit aea12f
the hint, for example in the callback function, using
Packit aea12f
@funcref{gnutls_psk_client_get_hint}.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_psk_set_server_credentials_file}
Packit aea12f
Packit aea12f
@showfuncC{gnutls_psk_set_server_credentials_function,gnutls_psk_set_server_credentials_hint,gnutls_psk_client_get_hint}
Packit aea12f
Packit aea12f
@node Anonymous credentials
Packit aea12f
@subsection Anonymous
Packit aea12f
The key exchange methods for anonymous authentication
Packit aea12f
since GnuTLS 3.6.0 will utilize the RFC7919 parameters, unless
Packit aea12f
explicit parameters have been provided and associated with an
Packit aea12f
anonymous credentials structure. Check @ref{Parameter generation} for more information.
Packit aea12f
The initialization functions for the credentials are shown below.
Packit aea12f
Packit aea12f
@showfuncD{gnutls_anon_allocate_server_credentials,gnutls_anon_allocate_client_credentials,gnutls_anon_free_server_credentials,gnutls_anon_free_client_credentials}
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
@node Setting up the transport layer
Packit aea12f
@section Setting up the transport layer
Packit aea12f
Packit aea12f
The next step is to setup the underlying transport layer details. The
Packit aea12f
Berkeley sockets are implicitly used by GnuTLS, thus a
Packit aea12f
call to @funcref{gnutls_transport_set_int} would be sufficient to
Packit aea12f
specify the socket descriptor. 
Packit aea12f
Packit aea12f
@showfuncB{gnutls_transport_set_int,gnutls_transport_set_int2}
Packit aea12f
Packit aea12f
If however another transport layer than TCP is selected, then
Packit aea12f
a pointer should be used instead to express the parameter to be
Packit aea12f
passed to custom functions. In that case the following functions should
Packit aea12f
be used instead.
Packit aea12f
Packit aea12f
@showfuncB{gnutls_transport_set_ptr,gnutls_transport_set_ptr2}
Packit aea12f
Packit aea12f
Moreover all of the following push and pull callbacks should be set.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_transport_set_push_function}
Packit aea12f
@showfuncdesc{gnutls_transport_set_vec_push_function}
Packit aea12f
@showfuncdesc{gnutls_transport_set_pull_function}
Packit aea12f
@showfuncdesc{gnutls_transport_set_pull_timeout_function}
Packit aea12f
Packit aea12f
Packit aea12f
The functions above accept a callback function which
Packit aea12f
should return the number of bytes written, or -1 on
Packit aea12f
error and should set @code{errno} appropriately.
Packit aea12f
In some environments, setting @code{errno} is unreliable. For example
Packit aea12f
Windows have several errno variables in different CRTs, or in other
Packit aea12f
systems it may be a non thread-local variable.  If this is a concern to
Packit aea12f
you, call @funcref{gnutls_transport_set_errno} with the intended errno
Packit aea12f
value instead of setting @code{errno} directly.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_transport_set_errno}
Packit aea12f
Packit aea12f
@acronym{GnuTLS} currently only interprets the EINTR, EAGAIN and EMSGSIZE errno
Packit aea12f
values and returns the corresponding @acronym{GnuTLS} error codes:
Packit aea12f
@itemize
Packit aea12f
@item @code{GNUTLS_E_INTERRUPTED} 
Packit aea12f
@item @code{GNUTLS_E_AGAIN}
Packit aea12f
@item @code{GNUTLS_E_LARGE_PACKET}
Packit aea12f
@end itemize
Packit aea12f
The EINTR and EAGAIN values are returned by interrupted system calls, 
Packit aea12f
or when non blocking IO is used.  All @acronym{GnuTLS} functions can be 
Packit aea12f
resumed (called again), if any of the above error codes is returned. The
Packit aea12f
EMSGSIZE value is returned when attempting to send a large datagram.
Packit aea12f
Packit aea12f
In the case of DTLS it is also desirable to override the generic 
Packit aea12f
transport functions with functions that emulate the operation
Packit aea12f
of @code{recvfrom} and @code{sendto}. In addition
Packit aea12f
@acronym{DTLS} requires timers during the receive of a handshake
Packit aea12f
message, set using the @funcref{gnutls_transport_set_pull_timeout_function} 
Packit aea12f
function. To check the retransmission timers the function
Packit aea12f
@funcref{gnutls_dtls_get_timeout} is provided, which returns the time
Packit aea12f
remaining until the next retransmission, or better the time until 
Packit aea12f
@funcref{gnutls_handshake} should be called again.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_transport_set_pull_timeout_function}
Packit aea12f
@showfuncdesc{gnutls_dtls_get_timeout}
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Asynchronous operation::
Packit aea12f
* Reducing round-trips::
Packit aea12f
* Zero-roundtrip mode::
Packit aea12f
* Anti-replay protection::
Packit aea12f
* DTLS sessions::
Packit aea12f
* DTLS and SCTP::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Asynchronous operation
Packit aea12f
@subsection Asynchronous operation
Packit aea12f
Packit aea12f
@acronym{GnuTLS} can be used with asynchronous socket or event-driven programming.
Packit aea12f
The approach is similar to using Berkeley sockets under such an environment.
Packit aea12f
The blocking, due to network interaction, calls such as
Packit aea12f
@funcref{gnutls_handshake}, @funcref{gnutls_record_recv},
Packit aea12f
can be set to non-blocking by setting the underlying sockets to non-blocking.
Packit aea12f
If other push and pull functions are setup, then they should behave the same
Packit aea12f
way as @funcintref{recv} and @funcintref{send} when used in a non-blocking
Packit aea12f
way, i.e., return -1 and set errno to @code{EAGAIN}. Since, during a TLS protocol session 
Packit aea12f
@acronym{GnuTLS} does not block except for network interaction, the non blocking
Packit aea12f
@code{EAGAIN} errno will be propagated and @acronym{GnuTLS} functions 
Packit aea12f
will return the @code{GNUTLS_E_AGAIN} error code. Such calls can be resumed the 
Packit aea12f
same way as a system call would. 
Packit aea12f
The only exception is @funcref{gnutls_record_send},
Packit aea12f
which if interrupted subsequent calls need not to include the data to be
Packit aea12f
sent (can be called with NULL argument).
Packit aea12f
Packit aea12f
When using the @funcintref{poll} or @funcintref{select} system calls though, one should remember
Packit aea12f
that they only apply to the kernel sockets API. To check for any
Packit aea12f
available buffered data in a @acronym{GnuTLS} session, 
Packit aea12f
utilize @funcref{gnutls_record_check_pending},
Packit aea12f
either before the @funcintref{poll} system call, or after a call to
Packit aea12f
@funcref{gnutls_record_recv}. Data queued by @funcref{gnutls_record_send}
Packit aea12f
(when interrupted) can be discarded using @funcref{gnutls_record_discard_queued}.
Packit aea12f
Packit aea12f
An example of GnuTLS' usage with asynchronous operation can be found
Packit aea12f
in @code{doc/examples/tlsproxy}.
Packit aea12f
Packit aea12f
The following paragraphs describe the detailed requirements for non-blocking
Packit aea12f
operation when using the TLS or DTLS protocols.
Packit aea12f
Packit aea12f
@subsubsection TLS protocol
Packit aea12f
There are no special requirements for the TLS protocol operation in non-blocking
Packit aea12f
mode if a non-blocking socket is used.
Packit aea12f
Packit aea12f
It is recommended, however, for future compatibility, when in non-blocking mode, to
Packit aea12f
call the @funcref{gnutls_init} function with the
Packit aea12f
@code{GNUTLS_NONBLOCK} flag set (see @ref{Session initialization}).
Packit aea12f
Packit aea12f
@subsubsection Datagram TLS protocol
Packit aea12f
When in non-blocking mode the function, the @funcref{gnutls_init} function 
Packit aea12f
must be called with the @code{GNUTLS_NONBLOCK} flag set (see @ref{Session initialization}). 
Packit aea12f
Packit aea12f
In contrast with the TLS protocol, the pull timeout function is required,
Packit aea12f
but will only be called with a timeout of zero. In that case it should indicate
Packit aea12f
whether there are data to be received or not. When not using the default pull function,
Packit aea12f
then @funcref{gnutls_transport_set_pull_timeout_function} should be called.
Packit aea12f
Packit aea12f
Although in the TLS protocol implementation each call to receive or send
Packit aea12f
function implies to restoring the same function that was interrupted, in
Packit aea12f
the DTLS protocol this requirement isn't true.
Packit aea12f
There are cases where a retransmission is required, which are indicated by
Packit aea12f
a received message and thus @funcref{gnutls_record_get_direction} must be called 
Packit aea12f
to decide which direction to check prior to restoring a function call.
Packit aea12f
@showfuncdesc{gnutls_record_get_direction}
Packit aea12f
Packit aea12f
When calling @funcref{gnutls_handshake} through a multi-plexer,
Packit aea12f
to be able to handle properly the DTLS handshake retransmission timers,
Packit aea12f
the function @funcref{gnutls_dtls_get_timeout}
Packit aea12f
should be used to estimate when to call @funcref{gnutls_handshake} if
Packit aea12f
no data have been received.
Packit aea12f
Packit aea12f
@node Reducing round-trips
Packit aea12f
@subsection Reducing round-trips
Packit aea12f
Packit aea12f
The full TLS 1.2 handshake requires 2 round-trips to complete, and when
Packit aea12f
combined with TCP's SYN and SYN-ACK negotiation it extends to 3 full
Packit aea12f
round-trips. While, TLS 1.3 reduces that to two round-trips when under TCP,
Packit aea12f
it still adds considerable latency, making the protocol unsuitable for
Packit aea12f
certain applications.
Packit aea12f
Packit aea12f
To optimize the handshake latency, in client side, it is possible to take
Packit aea12f
advantage of the TCP fast open @xcite{RFC7413} mechanism on operating
Packit aea12f
systems that support it. That can be done either by manually crafting the push and pull
Packit aea12f
callbacks, or by utilizing @funcref{gnutls_transport_set_fastopen}. In that
Packit aea12f
case the initial TCP handshake is eliminated, reducing the TLS 1.2 handshake round-trip
Packit aea12f
to 2, and the TLS 1.3 handshake to a single round-trip.
Packit aea12f
Note, that when this function is used, any connection failures will be reported during the
Packit aea12f
@funcref{gnutls_handshake} function call with error code @code{GNUTLS_E_PUSH_ERROR}.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_transport_set_fastopen}
Packit aea12f
Packit aea12f
When restricted to TLS 1.2, and non-resumed sessions, it is possible to further
Packit aea12f
reduce the round-trips to a single one by taking advantage of the @ref{False Start}
Packit aea12f
TLS extension. This can be enabled by setting the @acronym{GNUTLS_ENABLE_FALSE_START}
Packit aea12f
flag on @funcref{gnutls_init}.
Packit aea12f
Packit aea12f
Under TLS 1.3, the server side can start transmitting before the handshake
Packit aea12f
is complete (i.e., while the client Finished message is still in flight),
Packit aea12f
when no client certificate authentication is requested. This, unlike false
Packit aea12f
start, is part of protocol design with no known security implications.
Packit aea12f
It can be enabled by setting the @acronym{GNUTLS_ENABLE_EARLY_START} on
Packit aea12f
@funcref{gnutls_init}, and the @funcref{gnutls_handshake} function will
Packit aea12f
return early, allowing the server to send data earlier.
Packit aea12f
Packit aea12f
Packit aea12f
@node Zero-roundtrip mode
Packit aea12f
@subsection Zero-roundtrip mode
Packit aea12f
Packit aea12f
Under TLS 1.3, when the client has already connected to the server and
Packit aea12f
is resuming a session, it can start transmitting application data during
Packit aea12f
handshake.  This is called zero round-trip time (0-RTT) mode, and the
Packit aea12f
application data sent in this mode is called early data.  The client can
Packit aea12f
send early data with @funcref{gnutls_record_send_early_data}.  The
Packit aea12f
client should call this function before calling
Packit aea12f
@funcref{gnutls_handshake} and after calling
Packit aea12f
@funcref{gnutls_session_set_data}.
Packit aea12f
Packit aea12f
Note, however, that early data has weaker security properties than
Packit aea12f
normal application data sent after handshake, such as lack of forward
Packit aea12f
secrecy, no guarantees of non-replay between connections.  Thus it is
Packit aea12f
disabled on the server side by default.  To enable it, the server
Packit aea12f
needs to:
Packit aea12f
@enumerate
Packit aea12f
@item Set @acronym{GNUTLS_ENABLE_EARLY_DATA} on @funcref{gnutls_init}.  Note that this option only has effect on server.
Packit aea12f
Packit aea12f
@item Enable anti-replay measure.  See @ref{Anti-replay protection} for the details.
Packit aea12f
@end enumerate
Packit aea12f
Packit aea12f
The server caches the received early data until it is read.  To set the
Packit aea12f
maximum amount of data to be stored in the cache, use
Packit aea12f
@funcref{gnutls_record_set_max_early_data_size}.  After receiving the
Packit aea12f
EndOfEarlyData handshake message, the server can start retrieving the
Packit aea12f
received data with @funcref{gnutls_record_recv_early_data}.  You can
Packit aea12f
call the function either after the handshake is complete, or through a
Packit aea12f
handshake hook (@funcref{gnutls_handshake_set_hook_function}).
Packit aea12f
Packit aea12f
When sending early data, the client should respect the maximum amount
Packit aea12f
of early data, which may have been previously advertised by the
Packit aea12f
server.  It can be checked using
Packit aea12f
@funcref{gnutls_record_get_max_early_data_size}, right after calling
Packit aea12f
@funcref{gnutls_session_set_data}.
Packit aea12f
Packit aea12f
After sending early data, to check whether the sent early data was
Packit aea12f
accepted by the server, use @funcref{gnutls_session_get_flags} and
Packit aea12f
compare the result with @acronym{GNUTLS_SFLAGS_EARLY_DATA}.
Packit aea12f
Similarly, on the server side, the same function and flag can be used
Packit aea12f
to check whether it has actually accepted early data.
Packit aea12f
Packit aea12f
Packit aea12f
@node Anti-replay protection
Packit aea12f
@subsection Anti-replay protection
Packit aea12f
Packit aea12f
When 0-RTT mode is used, the server must protect itself from replay
Packit aea12f
attacks, where adversary client reuses duplicate session ticket to send
Packit aea12f
early data, before the server authenticates the client.
Packit aea12f
Packit aea12f
GnuTLS provides a simple mechanism against replay attacks, following the
Packit aea12f
method called ClientHello recording.  When a session ticket is accepted,
Packit aea12f
the server checks if the ClientHello message has been already seen.  If
Packit aea12f
there is a duplicate, the server rejects early data.
Packit aea12f
Packit aea12f
The problem of this approach is that the number of recorded messages
Packit aea12f
grows indefinitely.  To prevent that, the server can limit the recording
Packit aea12f
to a certain time window, which can be configured with
Packit aea12f
@funcref{gnutls_anti_replay_set_window}.
Packit aea12f
Packit aea12f
The anti-replay mechanism shall be globally initialized with
Packit aea12f
@funcref{gnutls_anti_replay_init}, and then attached to a session using
Packit aea12f
@funcref{gnutls_anti_replay_enable}.  It can be deinitialized with
Packit aea12f
@funcref{gnutls_anti_replay_deinit}.
Packit aea12f
Packit aea12f
The server must also set up a database back-end to store ClientHello
Packit aea12f
messages.  That can be achieved using
Packit aea12f
@funcref{gnutls_anti_replay_set_add_function} and
Packit aea12f
@funcref{gnutls_anti_replay_set_ptr}.
Packit aea12f
Packit aea12f
Note that, if the back-end stores arbitrary number of ClientHello, it
Packit aea12f
needs to periodically clean up the stored entries based on the time
Packit aea12f
window set with @funcref{gnutls_anti_replay_set_window}.  The cleanup
Packit aea12f
can be implemented by iterating through the database entries and calling
Packit aea12f
@funcref{gnutls_db_check_entry_expire_time}.  This is similar to session
Packit aea12f
database cleanup used by TLS1.2 sessions.
Packit aea12f
Packit aea12f
The full set up of the server using early data would be like the
Packit aea12f
following example:
Packit aea12f
@example
Packit aea12f
#define MAX_EARLY_DATA_SIZE 16384
Packit aea12f
Packit aea12f
static int
Packit aea12f
db_add_func(void *dbf, gnutls_datum_t key, gnutls_datum_t data)
Packit aea12f
@{
Packit aea12f
    /* Return GNUTLS_E_DB_ENTRY_EXISTS, if KEY is found in the database.
Packit aea12f
     * Otherwise, store it and return 0.
Packit aea12f
     */
Packit aea12f
@}
Packit aea12f
Packit aea12f
static int
Packit aea12f
handshake_hook_func(gnutls_session_t session, unsigned int htype,
Packit aea12f
                    unsigned when, unsigned int incoming, const gnutls_datum_t *msg)
Packit aea12f
@{
Packit aea12f
    int ret;
Packit aea12f
    char buf[MAX_EARLY_DATA_SIZE];
Packit aea12f
Packit aea12f
    assert(htype == GNUTLS_HANDSHAKE_END_OF_EARLY_DATA);
Packit aea12f
    assert(when == GNUTLS_HOOK_POST);
Packit aea12f
Packit aea12f
    if (gnutls_session_get_flags(session) & GNUTLS_SFLAGS_EARLY_DATA) @{
Packit aea12f
        ret = gnutls_record_recv_early_data(session, buf, sizeof(buf));
Packit aea12f
        assert(ret >= 0);
Packit aea12f
    @}
Packit aea12f
Packit aea12f
    return ret;
Packit aea12f
@}
Packit aea12f
Packit aea12f
int main()
Packit aea12f
@{
Packit aea12f
  ...
Packit aea12f
  /* Initialize anti-replay measure, which can be shared
Packit aea12f
   * among multiple sessions.
Packit aea12f
   */
Packit aea12f
  gnutls_anti_replay_init(&anti_replay);
Packit aea12f
Packit aea12f
  /* Set the database back-end function for the anti-replay data. */
Packit aea12f
  gnutls_anti_replay_set_add_function(anti_replay, db_add_func);
Packit aea12f
  gnutls_anti_replay_set_ptr(anti_replay, NULL);
Packit aea12f
Packit aea12f
  ...
Packit aea12f
Packit aea12f
  gnutls_init(&server, GNUTLS_SERVER | GNUTLS_ENABLE_EARLY_DATA);
Packit aea12f
  gnutls_record_set_max_early_data_size(server, MAX_EARLY_DATA_SIZE);
Packit aea12f
Packit aea12f
  ...
Packit aea12f
Packit aea12f
  /* Set the anti-replay measure to the session.
Packit aea12f
   */
Packit aea12f
  gnutls_anti_replay_enable(server, anti_replay);
Packit aea12f
  ...
Packit aea12f
Packit aea12f
  /* Retrieve early data in a handshake hook;
Packit aea12f
   * you can also do that after handshake.
Packit aea12f
   */
Packit aea12f
  gnutls_handshake_set_hook_function(server, GNUTLS_HANDSHAKE_END_OF_EARLY_DATA,
Packit aea12f
                                     GNUTLS_HOOK_POST, handshake_hook_func);
Packit aea12f
  ...
Packit aea12f
@}
Packit aea12f
@end example
Packit aea12f
Packit aea12f
Packit aea12f
@node DTLS sessions
Packit aea12f
@subsection DTLS sessions
Packit aea12f
Packit aea12f
Because datagram TLS can operate over connections where the client
Packit aea12f
cannot be reliably verified, functionality in the form of cookies, is available to prevent
Packit aea12f
denial of service attacks to servers. @acronym{GnuTLS} requires a server
Packit aea12f
to generate a secret key that is used to sign a cookie@footnote{A key of 128 bits or 16 bytes should be sufficient for this purpose.}. 
Packit aea12f
That cookie is sent to the client using @funcref{gnutls_dtls_cookie_send}, and 
Packit aea12f
the client must reply using the correct cookie. The server side
Packit aea12f
should verify the initial message sent by client using @funcref{gnutls_dtls_cookie_verify}.
Packit aea12f
If successful the session should be initialized and associated with
Packit aea12f
the cookie using @funcref{gnutls_dtls_prestate_set}, before proceeding to
Packit aea12f
the handshake.
Packit aea12f
Packit aea12f
@showfuncD{gnutls_key_generate,gnutls_dtls_cookie_send,gnutls_dtls_cookie_verify,gnutls_dtls_prestate_set}
Packit aea12f
Packit aea12f
Note that the above apply to server side only and they are not mandatory to be
Packit aea12f
used. Not using them, however, allows denial of service attacks.
Packit aea12f
The client side cookie handling is part of @funcref{gnutls_handshake}. 
Packit aea12f
Packit aea12f
Datagrams are typically restricted by a maximum transfer unit (MTU). For that
Packit aea12f
both client and server side should set the correct maximum transfer unit for
Packit aea12f
the layer underneath @acronym{GnuTLS}. This will allow proper fragmentation
Packit aea12f
of DTLS messages and prevent messages from being silently discarded by the
Packit aea12f
transport layer. The ``correct'' maximum transfer unit can be obtained through
Packit aea12f
a path MTU discovery mechanism @xcite{RFC4821}.
Packit aea12f
Packit aea12f
@showfuncC{gnutls_dtls_set_mtu,gnutls_dtls_get_mtu,gnutls_dtls_get_data_mtu}
Packit aea12f
Packit aea12f
@node DTLS and SCTP
Packit aea12f
@subsection DTLS and SCTP
Packit aea12f
Packit aea12f
Although DTLS can run under any reliable or unreliable layer, there are
Packit aea12f
special requirements for SCTP according to @xcite{RFC6083}. We summarize the
Packit aea12f
most important below, however for a full treatment we refer to @xcite{RFC6083}.
Packit aea12f
Packit aea12f
@itemize
Packit aea12f
@item The MTU set via @funcref{gnutls_dtls_set_mtu} must be 2^14.
Packit aea12f
@item Replay detection must be disabled; use the flag @code{GNUTLS_NO_REPLAY_PROTECTION} with @funcref{gnutls_init}.
Packit aea12f
@item Retransmission of messages must be disabled; use @funcref{gnutls_dtls_set_timeouts}
Packit aea12f
   with a retransmission timeout larger than the total.
Packit aea12f
@item Handshake, Alert and ChangeCipherSpec messages must be sent over stream 0 with unlimited reliability
Packit aea12f
   and with the ordered delivery feature.
Packit aea12f
@item During a rehandshake, the caching of messages with unknown epoch is
Packit aea12f
   not handled by GnuTLS; this must be implemented in a special pull function.
Packit aea12f
@end itemize
Packit aea12f
Packit aea12f
@node TLS handshake
Packit aea12f
@section TLS handshake
Packit aea12f
Once a session has been initialized and a network
Packit aea12f
connection has been set up, TLS and DTLS protocols
Packit aea12f
perform a handshake. The handshake is the actual key
Packit aea12f
exchange.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_handshake}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_handshake_set_timeout}
Packit aea12f
Packit aea12f
In GnuTLS 3.5.0 and later it is recommended to use @funcref{gnutls_session_set_verify_cert}
Packit aea12f
for the handshake process to ensure the verification of the peer's identity.
Packit aea12f
That will verify the peer's certificate, against the trusted CA store while
Packit aea12f
accounting for stapled OCSP responses during the handshake; any error will
Packit aea12f
be returned as a handshake error.
Packit aea12f
Packit aea12f
In older GnuTLS versions it is required to verify the peer's certificate
Packit aea12f
during the handshake by setting a callback with @funcref{gnutls_certificate_set_verify_function},
Packit aea12f
and then using @funcref{gnutls_certificate_verify_peers3} from it. See @ref{Certificate authentication}
Packit aea12f
for more information.
Packit aea12f
Packit aea12f
@showfuncB{gnutls_session_set_verify_cert,gnutls_certificate_verify_peers3}
Packit aea12f
Packit aea12f
@node Data transfer and termination
Packit aea12f
@section Data transfer and termination
Packit aea12f
Once the handshake is complete and peer's identity
Packit aea12f
has been verified data can be exchanged. The available
Packit aea12f
functions resemble the POSIX @code{recv} and @code{send}
Packit aea12f
functions. It is suggested to use @funcref{gnutls_error_is_fatal}
Packit aea12f
to check whether the error codes returned by these functions are
Packit aea12f
fatal for the protocol or can be ignored.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_record_send}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_record_recv}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_error_is_fatal}
Packit aea12f
Packit aea12f
Although, in the TLS protocol the receive function can be called
Packit aea12f
at any time, when DTLS is used the GnuTLS receive functions must be
Packit aea12f
called once a message is available for reading, even if no data are
Packit aea12f
expected. This is because in DTLS various (internal) actions
Packit aea12f
may be required due to retransmission timers. Moreover,
Packit aea12f
an extended receive function is shown below, which allows the extraction
Packit aea12f
of the message's sequence number. Due to the unreliable nature of the
Packit aea12f
protocol, this field allows distinguishing out-of-order messages.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_record_recv_seq}
Packit aea12f
Packit aea12f
The @funcref{gnutls_record_check_pending} helper function is available to 
Packit aea12f
allow checking whether data are available to be read in a @acronym{GnuTLS} session 
Packit aea12f
buffers. Note that this function complements but does not replace @funcintref{poll},
Packit aea12f
i.e., @funcref{gnutls_record_check_pending} reports no data to be read, @funcintref{poll}
Packit aea12f
should be called to check for data in the network buffers.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_record_check_pending}
Packit aea12f
@showfuncA{gnutls_record_get_direction}
Packit aea12f
Packit aea12f
Once a TLS or DTLS session is no longer needed, it is
Packit aea12f
recommended to use @funcref{gnutls_bye} to terminate the
Packit aea12f
session. That way the peer is notified securely about the
Packit aea12f
intention of termination, which allows distinguishing it
Packit aea12f
from a malicious connection termination.
Packit aea12f
A session can be deinitialized with the @funcref{gnutls_deinit} function.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_bye}
Packit aea12f
@showfuncdesc{gnutls_deinit}
Packit aea12f
Packit aea12f
@node Buffered data transfer
Packit aea12f
@section Buffered data transfer
Packit aea12f
Packit aea12f
Although @funcref{gnutls_record_send} is sufficient to transmit data
Packit aea12f
to the peer, when many small chunks of data are to be transmitted
Packit aea12f
it is inefficient and wastes bandwidth due to the TLS record
Packit aea12f
overhead. In that case it is preferable to combine the small chunks
Packit aea12f
before transmission. The following functions provide that functionality.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_record_cork}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_record_uncork}
Packit aea12f
Packit aea12f
Packit aea12f
@node Handling alerts
Packit aea12f
@section Handling alerts
Packit aea12f
During a TLS connection alert messages may be exchanged by the
Packit aea12f
two peers. Those messages may be fatal, meaning the connection
Packit aea12f
must be terminated afterwards, or warning when something needs
Packit aea12f
to be reported to the peer, but without interrupting the session.
Packit aea12f
The error codes @code{GNUTLS_E_@-WARNING_@-ALERT_@-RECEIVED}
Packit aea12f
or @code{GNUTLS_E_@-FATAL_@-ALERT_@-RECEIVED} signal those alerts
Packit aea12f
when received, and may be returned by all GnuTLS functions that receive 
Packit aea12f
data from the peer, being @funcref{gnutls_handshake} and @funcref{gnutls_record_recv}.
Packit aea12f
Packit aea12f
If those error codes are received the alert and its level should be logged
Packit aea12f
or reported to the peer using the functions below.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_alert_get}
Packit aea12f
@showfuncdesc{gnutls_alert_get_name}
Packit aea12f
Packit aea12f
The peer may also be warned or notified of a fatal issue
Packit aea12f
by using one of the functions below. All the available alerts
Packit aea12f
are listed in @ref{The Alert Protocol}.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_alert_send}
Packit aea12f
@showfuncdesc{gnutls_error_to_alert}
Packit aea12f
Packit aea12f
Packit aea12f
@node Priority Strings
Packit aea12f
@section Priority strings
Packit aea12f
@cindex Priority strings
Packit aea12f
Packit aea12f
@subheading How to use Priority Strings
Packit aea12f
Packit aea12f
The GnuTLS priority strings specify the TLS session's handshake
Packit aea12f
algorithms and options in a compact, easy-to-use format. These
Packit aea12f
strings are intended as a user-specified override of the library defaults.
Packit aea12f
Packit aea12f
That is, we recommend applications using the default settings
Packit aea12f
(c.f. @funcref{gnutls_set_default_priority} or
Packit aea12f
@funcref{gnutls_set_default_priority_append}), and provide the user 
Packit aea12f
with access to priority strings for overriding the default behavior,
Packit aea12f
on configuration files, or other UI. Following such a principle,
Packit aea12f
makes the GnuTLS library as the default settings provider. That is
Packit aea12f
necessary and a good practice, because TLS protocol hardening and
Packit aea12f
phasing out of legacy algorithms, is easier to co-ordinate when happens
Packit aea12f
in a single library.
Packit aea12f
Packit aea12f
@showfuncC{gnutls_set_default_priority,gnutls_set_default_priority_append,gnutls_priority_set_direct}
Packit aea12f
Packit aea12f
The priority string translation to the internal GnuTLS form requires
Packit aea12f
processing and the generated internal form also occupies some memory.
Packit aea12f
For that, it is recommended to do that processing once in server side,
Packit aea12f
and share the generated data across sessions. The following functions
Packit aea12f
allow the generation of a "priority cache" and the sharing of it across
Packit aea12f
sessions.
Packit aea12f
Packit aea12f
@showfuncD{gnutls_priority_init2,gnutls_priority_init,gnutls_priority_set,gnutls_priority_deinit}
Packit aea12f
Packit aea12f
@subheading Using Priority Strings
Packit aea12f
Packit aea12f
A priority string string may contain a single initial keyword such as in
Packit aea12f
@ref{tab:prio-keywords} and may be followed by additional algorithm or
Packit aea12f
special keywords. Note that their description is intentionally avoiding
Packit aea12f
specific algorithm details, as the priority strings are not constant between
Packit aea12f
gnutls versions (they are periodically updated to account for cryptographic
Packit aea12f
advances while providing compatibility with old clients and servers).
Packit aea12f
Packit aea12f
@float Table,tab:prio-keywords
Packit aea12f
@multitable @columnfractions .20 .70
Packit aea12f
@headitem Keyword @tab Description
Packit aea12f
@item @@KEYWORD @tab
Packit Service 991b93
Means that a compile-time specified system configuration file (see @ref{System-wide configuration of the library})
Packit aea12f
will be used to expand the provided keyword. That is used to impose system-specific policies.
Packit aea12f
It may be followed by additional options that will be appended to the
Packit aea12f
system string (e.g., "@@SYSTEM:+SRP"). The system file should have the
Packit aea12f
format 'KEYWORD=VALUE', e.g., 'SYSTEM=NORMAL:+ARCFOUR-128'.
Packit aea12f
Packit aea12f
Since version 3.5.1 it is allowed to specify fallback keywords such
Packit aea12f
as @@KEYWORD1,@@KEYWORD2, and the first valid keyword will be used.
Packit aea12f
Packit aea12f
@item PERFORMANCE @tab
Packit aea12f
All the known to be secure ciphersuites are enabled,
Packit aea12f
limited to 128 bit ciphers and sorted by terms of speed
Packit aea12f
performance. The message authenticity security level is of 64 bits or more,
Packit aea12f
and the certificate verification profile is set to GNUTLS_PROFILE_LOW (80-bits).
Packit aea12f
Packit aea12f
@item NORMAL @tab
Packit aea12f
Means all the known to be secure ciphersuites. The ciphers are sorted by security
Packit aea12f
margin, although the 256-bit ciphers are included as a fallback only.
Packit aea12f
The message authenticity security level is of 64 bits or more,
Packit aea12f
and the certificate verification profile is set to GNUTLS_PROFILE_LOW (80-bits).
Packit aea12f
Packit aea12f
This priority string implicitly enables ECDHE and DHE. The ECDHE ciphersuites
Packit aea12f
are placed first in the priority order, but due to compatibility 
Packit aea12f
issues with the DHE ciphersuites they are placed last in the priority order,
Packit aea12f
after the plain RSA ciphersuites.
Packit aea12f
Packit aea12f
@item LEGACY @tab
Packit aea12f
This sets the NORMAL settings that were used for GnuTLS 3.2.x or earlier. There is
Packit aea12f
no verification profile set, and the allowed DH primes are considered
Packit aea12f
weak today (but are often used by misconfigured servers).
Packit aea12f
Packit aea12f
@item PFS @tab
Packit aea12f
Means all the known to be secure ciphersuites that support perfect forward
Packit aea12f
secrecy (ECDHE and DHE). The ciphers are sorted by security
Packit aea12f
margin, although the 256-bit ciphers are included as a fallback only.
Packit aea12f
The message authenticity security level is of 80 bits or more,
Packit aea12f
and the certificate verification profile is set to GNUTLS_PROFILE_LOW (80-bits).
Packit aea12f
This option is available since 3.2.4 or later.
Packit aea12f
Packit aea12f
@item SECURE128 @tab
Packit aea12f
Means all known to be secure ciphersuites that offer a 
Packit aea12f
security level 128-bit or more.
Packit aea12f
The message authenticity security level is of 80 bits or more,
Packit aea12f
and the certificate verification profile is set to GNUTLS_PROFILE_LOW (80-bits).
Packit aea12f
Packit aea12f
@item SECURE192 @tab
Packit aea12f
Means all the known to be secure ciphersuites that offer a 
Packit aea12f
security level 192-bit or more.
Packit aea12f
The message authenticity security level is of 128 bits or more,
Packit aea12f
and the certificate verification profile is set to GNUTLS_PROFILE_HIGH (128-bits).
Packit aea12f
Packit aea12f
@item SECURE256 @tab
Packit aea12f
Currently alias for SECURE192. This option, will enable ciphers which use a
Packit aea12f
256-bit key but, due to limitations of the TLS protocol, the overall security
Packit aea12f
level will be 192-bits (the security level depends on more factors than cipher key size).
Packit aea12f
Packit aea12f
@item SUITEB128 @tab
Packit aea12f
Means all the NSA Suite B cryptography (RFC5430) ciphersuites
Packit aea12f
with an 128 bit security level, as well as the enabling of the corresponding
Packit aea12f
verification profile.
Packit aea12f
Packit aea12f
@item SUITEB192 @tab
Packit aea12f
Means all the NSA Suite B cryptography (RFC5430) ciphersuites
Packit aea12f
with an 192 bit security level, as well as the enabling of the corresponding
Packit aea12f
verification profile.
Packit aea12f
Packit aea12f
@item NONE @tab
Packit aea12f
Means nothing is enabled.  This disables even protocol versions.
Packit aea12f
It should be followed by the algorithms to be enabled. Note that
Packit aea12f
using this option to build a priority string gives detailed control
Packit aea12f
into the resulting settings, however with new revisions of the TLS protocol
Packit aea12f
new priority items are routinely added, and such strings are not
Packit aea12f
forward compatible with new protocols. As such, we
Packit aea12f
advice against using that option for applications targeting multiple versions
Packit aea12f
of the GnuTLS library, and recommend using the defaults (see above) or
Packit aea12f
adjusting the defaults via @funcref{gnutls_set_default_priority_append}.
Packit aea12f
Packit aea12f
@end multitable
Packit aea12f
@caption{Supported initial keywords.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
Unless the initial keyword is "NONE" the defaults (in preference
Packit aea12f
order) are for TLS protocols TLS 1.2, TLS1.1, TLS1.0;
Packit aea12f
for certificate types X.509.
Packit aea12f
In key exchange algorithms when in NORMAL or SECURE levels the
Packit aea12f
perfect forward secrecy algorithms take precedence of the other
Packit aea12f
protocols.  In all cases all the supported key exchange algorithms
Packit aea12f
are enabled.
Packit aea12f
Packit aea12f
Note that the SECURE levels distinguish between overall security level and
Packit aea12f
message authenticity security level. That is because the message
Packit aea12f
authenticity security level requires the adversary to break
Packit aea12f
the algorithms at real-time during the protocol run, whilst 
Packit aea12f
the overall security level refers to off-line adversaries 
Packit aea12f
(e.g. adversaries breaking the ciphertext years after it was captured).
Packit aea12f
Packit aea12f
The NONE keyword, if used, must followed by keywords specifying 
Packit aea12f
the algorithms and protocols to be enabled. The other initial keywords 
Packit aea12f
do not require, but may be followed by such keywords. All level keywords
Packit aea12f
can be combined, and for example a level of "SECURE256:+SECURE128" is
Packit aea12f
allowed.
Packit aea12f
Packit aea12f
The order with which every algorithm or protocol
Packit aea12f
is specified is significant. Algorithms specified before others
Packit aea12f
will take precedence. The supported in the GnuTLS version corresponding
Packit aea12f
to this document algorithms and protocols are shown in @ref{tab:prio-algorithms};
Packit aea12f
to list the supported algorithms in your currently using version use
Packit aea12f
@code{gnutls-cli -l}.
Packit aea12f
Packit aea12f
To avoid collisions in order to specify a protocol version
Packit aea12f
with "VERS-", signature algorithms with "SIGN-" and certificate types with "CTYPE-". 
Packit aea12f
All other algorithms don't need a prefix. Each specified keyword (except
Packit aea12f
for @emph{special keywords}) can be prefixed with any of the following
Packit aea12f
characters.
Packit aea12f
Packit aea12f
@table @asis
Packit aea12f
@item '!' or '-' 
Packit aea12f
appended with an algorithm will remove this algorithm.
Packit aea12f
@item "+" 
Packit aea12f
appended with an algorithm will add this algorithm.
Packit aea12f
@end table
Packit aea12f
Packit aea12f
@float Table,tab:prio-algorithms
Packit aea12f
@multitable @columnfractions .20 .70
Packit aea12f
@headitem Type @tab Keywords
Packit aea12f
@item Ciphers @tab
Packit Service 991b93
Examples are AES-128-GCM, AES-256-GCM, AES-256-CBC, GOST28147-TC26Z-CNT; see also
Packit aea12f
@ref{tab:ciphers} for more options. Catch all name is CIPHER-ALL which will add
Packit Service 991b93
all the algorithms from NORMAL priority. The shortcut for secure GOST
Packit Service 991b93
algorithms is CIPHER-GOST-ALL.
Packit aea12f
Packit aea12f
@item Key exchange @tab
Packit aea12f
RSA, DHE-RSA, DHE-DSS, SRP, SRP-RSA, SRP-DSS,
Packit Service 991b93
PSK, DHE-PSK, ECDHE-PSK, ECDHE-RSA, ECDHE-ECDSA, VKO-GOST-12, ANON-ECDH, ANON-DH.
Packit aea12f
Catch all name is KX-ALL which will add all the algorithms from NORMAL
Packit aea12f
priority. Under TLS1.3, the DHE-PSK and ECDHE-PSK strings are equivalent
Packit Service 991b93
and instruct for a Diffie-Hellman key exchange using the enabled groups. The
Packit Service 991b93
shortcut for secure GOST algorithms is KX-GOST-ALL.
Packit aea12f
Packit aea12f
@item MAC @tab
Packit Service 991b93
MD5, SHA1, SHA256, SHA384, GOST28147-TC26Z-IMIT, AEAD (used with
Packit Service 991b93
GCM ciphers only). All algorithms from NORMAL priority can be accessed with
Packit Service 991b93
MAC-ALL. The shortcut for secure GOST algorithms is MAC-GOST-ALL.
Packit aea12f
Packit aea12f
@item Compression algorithms @tab
Packit aea12f
COMP-NULL, COMP-DEFLATE. Catch all is COMP-ALL.
Packit aea12f
Packit aea12f
@item TLS versions @tab
Packit aea12f
VERS-TLS1.0, VERS-TLS1.1, VERS-TLS1.2, VERS-TLS1.3,
Packit aea12f
VERS-DTLS1.0, VERS-DTLS1.2. 
Packit aea12f
Catch all are VERS-ALL, and will enable
Packit aea12f
all protocols from NORMAL priority. To distinguish between TLS and DTLS
Packit aea12f
versions you can use VERS-TLS-ALL and VERS-DTLS-ALL.
Packit aea12f
Packit aea12f
@item Signature algorithms @tab
Packit aea12f
SIGN-RSA-SHA1, SIGN-RSA-SHA224,
Packit aea12f
SIGN-RSA-SHA256, SIGN-RSA-SHA384, SIGN-RSA-SHA512, SIGN-DSA-SHA1,
Packit aea12f
SIGN-DSA-SHA224, SIGN-DSA-SHA256, SIGN-RSA-MD5, SIGN-ECDSA-SHA1,
Packit aea12f
SIGN-ECDSA-SHA224, SIGN-ECDSA-SHA256, SIGN-ECDSA-SHA384, SIGN-ECDSA-SHA512,
Packit Service 991b93
SIGN-RSA-PSS-SHA256, SIGN-RSA-PSS-SHA384, SIGN-RSA-PSS-SHA512,
Packit Service 991b93
SIGN-GOSTR341001, SIGN-GOSTR341012-256, SIGN-GOSTR341012-512.
Packit aea12f
Catch all which enables all algorithms from NORMAL priority is SIGN-ALL.
Packit Service 991b93
Shortcut which enables secure GOST algorithms is SIGN-GOST-ALL.
Packit aea12f
This option is only considered for TLS 1.2 and later.
Packit aea12f
Packit aea12f
@item Groups @tab
Packit Service 991b93
GROUP-SECP256R1, GROUP-SECP384R1, GROUP-SECP521R1, GROUP-X25519, GROUP-X448,
Packit aea12f
GROUP-FFDHE2048, GROUP-FFDHE3072, GROUP-FFDHE4096, GROUP-FFDHE6144, and
Packit aea12f
GROUP-FFDHE8192.
Packit aea12f
Groups include both elliptic curve groups, e.g., SECP256R1, as well as 
Packit aea12f
finite field groups such as FFDHE2048. Catch all which enables all groups
Packit Service 991b93
from NORMAL priority is GROUP-ALL. The helper keywords GROUP-DH-ALL,
Packit Service 991b93
GROUP-GOST-ALL and GROUP-EC-ALL are also available, restricting the groups
Packit Service 991b93
to finite fields (DH), GOST curves and generic elliptic curves.
Packit aea12f
Packit aea12f
@item Elliptic curves (legacy) @tab
Packit aea12f
CURVE-SECP192R1, CURVE-SECP224R1, CURVE-SECP256R1, CURVE-SECP384R1,
Packit Service 991b93
CURVE-SECP521R1, CURVE-X25519, and CURVE-X448.
Packit aea12f
Catch all which enables all curves from NORMAL priority is CURVE-ALL. Note
Packit aea12f
that the CURVE keyword is kept for backwards compatibility only, for new
Packit aea12f
applications see the GROUP keyword above.
Packit aea12f
Packit aea12f
@item Certificate types @tab
Packit aea12f
Certificate types can be given in a symmetric fashion (i.e. the same for
Packit aea12f
both client and server) or, as of GnuTLS 3.6.4, in an asymmetric fashion
Packit aea12f
(i.e. different for the client than for the server). Alternative certificate
Packit aea12f
types must be explicitly enabled via flags in @funcref{gnutls_init}.
Packit aea12f
Packit aea12f
The currently supported types are CTYPE-X509, CTYPE-RAWPK which apply both to
Packit aea12f
client and server; catch all is CTYPE-ALL. The types CTYPE-CLI-X509, CTYPE-SRV-X509,
Packit aea12f
CTYPE-CLI-RAWPK, CTYPE-SRV-RAWPK can be used to specialize on client or server;
Packit aea12f
catch all is CTYPE-CLI-ALL and CTYPE-SRV-ALL. The type 'X509' is aliased to 'X.509'
Packit aea12f
for legacy reasons.
Packit aea12f
Packit Service 991b93
@item Generic @tab
Packit Service 991b93
The keyword GOST is a shortcut for secure GOST algorithms (MACs, ciphers,
Packit Service 991b93
KXes, groups and signatures). For example the following string will enable all
Packit Service 991b93
TLS 1.2 GOST ciphersuites: 'NONE:+VERS-TLS1.2:+GOST'.
Packit Service 991b93
Packit aea12f
@end multitable
Packit aea12f
@caption{The supported algorithm keywords in priority strings.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
Note that the finite field groups (indicated by the FFDHE prefix) and DHE key
Packit aea12f
exchange methods are generally slower@footnote{It depends on the group in use.  Groups with
Packit aea12f
less bits are always faster, but the number of bits ties with the security
Packit aea12f
parameter.  See @ref{Selecting cryptographic key sizes}
Packit aea12f
for the acceptable security levels.} than their elliptic curves counterpart
Packit aea12f
(ECDHE). 
Packit aea12f
Packit aea12f
The available special keywords are shown in @ref{tab:prio-special1}
Packit aea12f
and @ref{tab:prio-special2}. 
Packit aea12f
Packit aea12f
@float Table,tab:prio-special1
Packit aea12f
@multitable @columnfractions .45 .45
Packit aea12f
@headitem Keyword @tab Description
Packit aea12f
Packit aea12f
@item %COMPAT @tab
Packit aea12f
will enable compatibility mode. It might mean that violations
Packit aea12f
of the protocols are allowed as long as maximum compatibility with
Packit aea12f
problematic clients and servers is achieved. More specifically this
Packit aea12f
string will tolerate packets over the maximum allowed TLS record,
Packit aea12f
and add a padding to TLS Client Hello packet to prevent it being in the
Packit aea12f
256-512 range which is known to be causing issues with a commonly used
Packit aea12f
firewall (see the %DUMBFW option).
Packit aea12f
Packit aea12f
@item %DUMBFW @tab
Packit aea12f
will add a private extension with bogus data that make the client
Packit aea12f
hello exceed 512 bytes. This avoids a black hole behavior in some
Packit aea12f
firewalls. This is the @xcite{RFC7685} client hello padding extension, also enabled
Packit aea12f
with %COMPAT.
Packit aea12f
Packit aea12f
@item %NO_EXTENSIONS @tab
Packit aea12f
will prevent the sending of any TLS extensions in client side. Note
Packit aea12f
that TLS 1.2 requires extensions to be used, as well as safe
Packit aea12f
renegotiation thus this option must be used with care. When this option
Packit aea12f
is set no versions later than TLS1.2 can be negotiated.
Packit aea12f
Packit aea12f
@item %NO_TICKETS @tab
Packit aea12f
will prevent the advertizing of the TLS session ticket extension.
Packit aea12f
This is implied by the PFS keyword.
Packit aea12f
Packit aea12f
@item %NO_SESSION_HASH @tab
Packit aea12f
will prevent the advertizing the TLS extended master secret (session hash)
Packit aea12f
extension.
Packit aea12f
Packit aea12f
@item %SERVER_PRECEDENCE @tab
Packit aea12f
The ciphersuite will be selected according to server priorities
Packit aea12f
and not the client's.
Packit aea12f
Packit aea12f
@item %SSL3_RECORD_VERSION @tab
Packit aea12f
will use SSL3.0 record version in client hello.
Packit aea12f
By default GnuTLS will set the minimum supported version as the
Packit aea12f
client hello record version (do not confuse that version with the
Packit aea12f
proposed handshake version at the client hello).
Packit aea12f
Packit aea12f
@item %LATEST_RECORD_VERSION @tab
Packit aea12f
will use the latest TLS version record version in client hello.
Packit aea12f
Packit aea12f
@end multitable
Packit aea12f
@caption{Special priority string keywords.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
@float Table,tab:prio-special2
Packit aea12f
@multitable @columnfractions .45 .45
Packit aea12f
@headitem Keyword @tab Description
Packit aea12f
Packit aea12f
@item %STATELESS_COMPRESSION @tab
Packit aea12f
ignored; no longer used.
Packit aea12f
Packit aea12f
@item %DISABLE_WILDCARDS @tab
Packit aea12f
will disable matching wildcards when comparing hostnames
Packit aea12f
in certificates.
Packit aea12f
Packit aea12f
@item %NO_ETM @tab
Packit aea12f
will disable the encrypt-then-mac TLS extension (RFC7366). This is
Packit aea12f
implied by the %COMPAT keyword.
Packit aea12f
Packit aea12f
@item %FORCE_ETM @tab
Packit aea12f
negotiate CBC ciphersuites only when both sides of the connection support
Packit aea12f
encrypt-then-mac TLS extension (RFC7366).
Packit aea12f
Packit aea12f
@item %DISABLE_SAFE_RENEGOTIATION @tab
Packit aea12f
will completely disable safe renegotiation
Packit aea12f
completely.  Do not use unless you know what you are doing.
Packit aea12f
Packit aea12f
@item %UNSAFE_RENEGOTIATION @tab
Packit aea12f
will allow handshakes and re-handshakes
Packit aea12f
without the safe renegotiation extension.  Note that for clients
Packit aea12f
this mode is insecure (you may be under attack), and for servers it
Packit aea12f
will allow insecure clients to connect (which could be fooled by an
Packit aea12f
attacker).  Do not use unless you know what you are doing and want
Packit aea12f
maximum compatibility.
Packit aea12f
Packit aea12f
@item %PARTIAL_RENEGOTIATION @tab
Packit aea12f
will allow initial handshakes to proceed,
Packit aea12f
but not re-handshakes.  This leaves the client vulnerable to attack,
Packit aea12f
and servers will be compatible with non-upgraded clients for
Packit aea12f
initial handshakes.  This is currently the default for clients and
Packit aea12f
servers, for compatibility reasons.
Packit aea12f
Packit aea12f
@item %SAFE_RENEGOTIATION @tab
Packit aea12f
will enforce safe renegotiation.  Clients and
Packit aea12f
servers will refuse to talk to an insecure peer.  Currently this
Packit aea12f
causes interoperability problems, but is required for full protection.
Packit aea12f
Packit aea12f
@item %FALLBACK_SCSV @tab
Packit aea12f
will enable the use of the fallback signaling cipher suite value in the
Packit aea12f
client hello.  Note that this should be set only by applications that
Packit aea12f
try to reconnect with a downgraded protocol version. See RFC7507 for
Packit aea12f
details.
Packit aea12f
Packit aea12f
@item %VERIFY_ALLOW_BROKEN @tab
Packit aea12f
will allow signatures with known to be broken algorithms (such as MD5 or
Packit aea12f
SHA1) in certificate chains.
Packit aea12f
Packit aea12f
@item %VERIFY_ALLOW_SIGN_RSA_MD5 @tab
Packit aea12f
will allow RSA-MD5 signatures in certificate chains.
Packit aea12f
Packit aea12f
@item %VERIFY_ALLOW_SIGN_WITH_SHA1 @tab
Packit aea12f
will allow signatures with SHA1 hash algorithm in certificate chains.
Packit aea12f
Packit aea12f
@item %VERIFY_DISABLE_CRL_CHECKS @tab
Packit aea12f
will disable CRL or OCSP checks in the verification of the certificate chain.
Packit aea12f
Packit aea12f
@item %VERIFY_ALLOW_X509_V1_CA_CRT @tab
Packit aea12f
will allow V1 CAs in chains.
Packit aea12f
Packit aea12f
@item %PROFILE_(LOW|LEGACY|MEDIUM|HIGH|ULTRA|FUTURE) @tab
Packit aea12f
require a certificate verification profile the corresponds to the specified
Packit aea12f
security level, see @ref{tab:key-sizes} for the mappings to values.
Packit aea12f
Packit aea12f
@item %PROFILE_(SUITEB128|SUITEB192) @tab
Packit aea12f
require a certificate verification profile the corresponds to SUITEB. Note
Packit aea12f
that an initial keyword that enables SUITEB automatically sets the profile.
Packit aea12f
Packit aea12f
Packit aea12f
@end multitable
Packit aea12f
@caption{More priority string keywords.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
Finally the ciphersuites enabled by any priority string can be
Packit aea12f
listed using the @code{gnutls-cli} application (see @ref{gnutls-cli Invocation}), 
Packit aea12f
or by using the priority functions as in @ref{Listing the ciphersuites in a priority string}.
Packit aea12f
Packit aea12f
Example priority strings are:
Packit aea12f
@example
Packit aea12f
The system imposed security level:
Packit aea12f
    "SYSTEM"
Packit aea12f
Packit aea12f
The default priority without the HMAC-MD5:
Packit aea12f
    "NORMAL:-MD5"
Packit aea12f
Packit aea12f
Specifying RSA with AES-128-CBC:
Packit aea12f
    "NONE:+VERS-TLS-ALL:+MAC-ALL:+RSA:+AES-128-CBC:+SIGN-ALL:+COMP-NULL"
Packit aea12f
Packit aea12f
Specifying the defaults plus ARCFOUR-128:
Packit aea12f
    "NORMAL:+ARCFOUR-128"
Packit aea12f
Packit aea12f
Enabling the 128-bit secure ciphers, while disabling TLS 1.0:
Packit aea12f
    "SECURE128:-VERS-TLS1.0"
Packit aea12f
Packit aea12f
Enabling the 128-bit and 192-bit secure ciphers, while disabling all TLS versions 
Packit aea12f
except TLS 1.2:
Packit aea12f
    "SECURE128:+SECURE192:-VERS-ALL:+VERS-TLS1.2"
Packit aea12f
@end example
Packit aea12f
Packit aea12f
@node Selecting cryptographic key sizes
Packit aea12f
@section Selecting cryptographic key sizes
Packit aea12f
@cindex key sizes
Packit aea12f
Packit aea12f
Because many algorithms are involved in TLS, it is not easy to set
Packit aea12f
a consistent security level.  For this reason in @ref{tab:key-sizes} we
Packit aea12f
present some correspondence between key sizes of symmetric algorithms
Packit aea12f
and public key algorithms based on @xcite{ECRYPT}. 
Packit aea12f
Those can be used to generate certificates with
Packit aea12f
appropriate key sizes as well as select parameters for Diffie-Hellman and SRP
Packit aea12f
authentication.
Packit aea12f
Packit aea12f
@float Table,tab:key-sizes
Packit aea12f
@multitable @columnfractions .10 .12 .10 .20 .32
Packit aea12f
Packit aea12f
@headitem Security bits @tab RSA, DH and SRP parameter size @tab ECC key size @tab Security parameter (profile) @tab Description
Packit aea12f
Packit aea12f
@item <64
Packit aea12f
@tab <768
Packit aea12f
@tab <128
Packit aea12f
@tab @code{INSECURE}
Packit aea12f
@tab Considered to be insecure
Packit aea12f
Packit aea12f
@item 64
Packit aea12f
@tab 768
Packit aea12f
@tab 128
Packit aea12f
@tab @code{VERY WEAK}
Packit aea12f
@tab Short term protection against individuals
Packit aea12f
Packit aea12f
@item 72
Packit aea12f
@tab 1008
Packit aea12f
@tab 160
Packit aea12f
@tab @code{WEAK}
Packit aea12f
@tab Short term protection against small organizations
Packit aea12f
Packit aea12f
@item 80
Packit aea12f
@tab 1024
Packit aea12f
@tab 160
Packit aea12f
@tab @code{LOW}
Packit aea12f
@tab Very short term protection against agencies (corresponds to ENISA legacy level)
Packit aea12f
Packit aea12f
@item 96
Packit aea12f
@tab 1776
Packit aea12f
@tab 192
Packit aea12f
@tab @code{LEGACY}
Packit aea12f
@tab Legacy standard level
Packit aea12f
Packit aea12f
@item 112
Packit aea12f
@tab 2048
Packit aea12f
@tab 224
Packit aea12f
@tab @code{MEDIUM}
Packit aea12f
@tab Medium-term protection
Packit aea12f
Packit aea12f
@item 128
Packit aea12f
@tab 3072
Packit aea12f
@tab 256
Packit aea12f
@tab @code{HIGH}
Packit aea12f
@tab Long term protection (corresponds to ENISA future level)
Packit aea12f
Packit aea12f
@item 192
Packit aea12f
@tab 8192
Packit aea12f
@tab 384
Packit aea12f
@tab @code{ULTRA}
Packit aea12f
@tab Even longer term protection
Packit aea12f
Packit aea12f
@item 256
Packit aea12f
@tab 15424
Packit aea12f
@tab 512
Packit aea12f
@tab @code{FUTURE}
Packit aea12f
@tab Foreseeable future
Packit aea12f
Packit aea12f
@end multitable
Packit aea12f
@caption{Key sizes and security parameters.}
Packit aea12f
@end float
Packit aea12f
Packit aea12f
The first column  provides a security parameter in a number of bits. This
Packit aea12f
gives an indication of the number of combinations to be tried by an adversary
Packit aea12f
to brute force a key. For example to test all possible keys in a 112 bit security parameter
Packit aea12f
@math{2^{112}} combinations have to be tried. For today's technology this is infeasible.
Packit aea12f
The next two columns correlate the security
Packit aea12f
parameter with actual bit sizes of parameters for DH, RSA, SRP and ECC algorithms.
Packit aea12f
A mapping to @code{gnutls_sec_param_t} value is given for each security parameter, on
Packit aea12f
the next column, and finally a brief description of the level.
Packit aea12f
Packit aea12f
@c @showenumdesc{gnutls_sec_param_t,The @code{gnutls_sec_@-param_t} enumeration.}
Packit aea12f
Packit aea12f
Note, however, that the values suggested here are nothing more than an
Packit aea12f
educated guess that is valid today. There are no guarantees that an
Packit aea12f
algorithm will remain unbreakable or that these values will remain
Packit aea12f
constant in time. There could be scientific breakthroughs that cannot
Packit aea12f
be predicted or total failure of the current public key systems by
Packit aea12f
quantum computers. On the other hand though the cryptosystems used in
Packit aea12f
TLS are selected in a conservative way and such catastrophic
Packit aea12f
breakthroughs or failures are believed to be unlikely.
Packit aea12f
The NIST publication SP 800-57 @xcite{NISTSP80057} contains a similar
Packit aea12f
table.
Packit aea12f
Packit aea12f
When using @acronym{GnuTLS} and a decision on bit sizes for a public
Packit aea12f
key algorithm is required, use of the following functions is  
Packit aea12f
recommended:
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_sec_param_to_pk_bits}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_pk_bits_to_sec_param}
Packit aea12f
Packit aea12f
Those functions will convert a human understandable security parameter
Packit aea12f
of @code{gnutls_sec_param_t} type, to a number of bits suitable for a public 
Packit aea12f
key algorithm.
Packit aea12f
Packit aea12f
@showfuncA{gnutls_sec_param_get_name}
Packit aea12f
Packit aea12f
The following functions will set the minimum acceptable group size for Diffie-Hellman
Packit aea12f
and SRP authentication. 
Packit aea12f
@showfuncB{gnutls_dh_set_prime_bits,gnutls_srp_set_prime_bits}
Packit aea12f
Packit aea12f
Packit aea12f
@node Advanced topics
Packit aea12f
@section Advanced topics
Packit aea12f
Packit aea12f
@menu
Packit aea12f
* Virtual hosts and credentials::
Packit aea12f
* Session resumption::
Packit aea12f
* Certificate verification::
Packit aea12f
* TLS 1.2 re-authentication::
Packit aea12f
* TLS 1.3 re-authentication and re-key::
Packit aea12f
* Parameter generation::
Packit aea12f
* Deriving keys for other applications/protocols::
Packit aea12f
* Channel Bindings::
Packit aea12f
* Interoperability::
Packit aea12f
* Compatibility with the OpenSSL library::
Packit aea12f
@end menu
Packit aea12f
Packit aea12f
@node Virtual hosts and credentials
Packit aea12f
@subsection Virtual hosts and credentials
Packit aea12f
@cindex virtual hosts
Packit aea12f
@cindex credentials
Packit aea12f
Packit aea12f
Often when operating with virtual hosts, one may not want to associate
Packit aea12f
a particular certificate set to the credentials function early, before
Packit aea12f
the virtual host is known. That can be achieved by calling
Packit aea12f
@funcref{gnutls_credentials_set} within a handshake pre-hook for client
Packit aea12f
hello. That message contains the peer's intended hostname, and if read,
Packit aea12f
and the appropriate credentials are set, gnutls will be able to
Packit aea12f
continue in the handshake process. A brief usage example is shown
Packit aea12f
below.
Packit aea12f
Packit aea12f
@example
Packit aea12f
static int ext_hook_func(void *ctx, unsigned tls_id,
Packit aea12f
                         const unsigned char *data, unsigned size)
Packit aea12f
@{
Packit aea12f
	if (tls_id == 0) @{ /* server name */
Packit aea12f
		/* figure the advertized name - the following hack
Packit aea12f
                 * relies on the fact that this extension only supports
Packit aea12f
                 * DNS names, and due to a protocol bug cannot be extended
Packit aea12f
                 * to support anything else. */
Packit aea12f
		if (name < 5) return 0;
Packit aea12f
		name = data+5;
Packit aea12f
		name_size = size-5;
Packit aea12f
	@}
Packit aea12f
	return 0;
Packit aea12f
@}
Packit aea12f
Packit aea12f
static int
Packit aea12f
handshake_hook_func(gnutls_session_t session, unsigned int htype,
Packit aea12f
                    unsigned when, unsigned int incoming, const gnutls_datum_t *msg)
Packit aea12f
@{
Packit aea12f
    int ret;
Packit aea12f
Packit aea12f
    assert(htype == GNUTLS_HANDSHAKE_CLIENT_HELLO);
Packit aea12f
    assert(when == GNUTLS_HOOK_PRE);
Packit aea12f
Packit aea12f
    ret = gnutls_ext_raw_parse(NULL, ext_hook_func, msg,
Packit aea12f
                               GNUTLS_EXT_RAW_FLAG_TLS_CLIENT_HELLO);
Packit aea12f
    assert(ret >= 0);
Packit aea12f
Packit aea12f
    gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cred);
Packit aea12f
Packit aea12f
    return ret;
Packit aea12f
@}
Packit aea12f
Packit aea12f
int main()
Packit aea12f
@{
Packit aea12f
  ...
Packit aea12f
Packit aea12f
  gnutls_handshake_set_hook_function(server, GNUTLS_HANDSHAKE_CLIENT_HELLO,
Packit aea12f
                                     GNUTLS_HOOK_PRE, handshake_hook_func);
Packit aea12f
  ...
Packit aea12f
@}
Packit aea12f
@end example
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_handshake_set_hook_function}
Packit aea12f
Packit aea12f
@node Session resumption
Packit aea12f
@subsection Session resumption
Packit aea12f
@cindex resuming sessions
Packit aea12f
@cindex session resumption
Packit aea12f
Packit aea12f
To reduce time and network traffic spent in a handshake the client can
Packit aea12f
request session resumption from a server that previously shared a
Packit aea12f
session with the client.
Packit aea12f
Packit aea12f
Under TLS 1.2, in order to support resumption a server can either store
Packit aea12f
the session security parameters in a local database or use session
Packit aea12f
tickets (see @ref{Session tickets}) to delegate storage to the client.
Packit aea12f
Packit aea12f
Under TLS 1.3, session resumption is only available through session
Packit aea12f
tickets, and multiple tickets could be sent from server to client. That
Packit aea12f
provides the following advantages:
Packit aea12f
@itemize
Packit aea12f
@item When tickets are not re-used the subsequent client sessions cannot be associated with each other by an eavesdropper
Packit aea12f
@item On post-handshake authentication the server may send different tickets asynchronously for each identity used by client.
Packit aea12f
@end itemize
Packit aea12f
Packit aea12f
@subsubheading Client side
Packit aea12f
Packit aea12f
The client has to retrieve and store the session parameters. Before
Packit aea12f
establishing a new session to the same server the parameters must be
Packit aea12f
re-associated with the GnuTLS session using
Packit aea12f
@funcref{gnutls_session_set_data}.
Packit aea12f
Packit aea12f
@showfuncB{gnutls_session_get_data2,gnutls_session_set_data}
Packit aea12f
Packit aea12f
Keep in mind that sessions will be expired after some time, depending
Packit aea12f
on the server, and a server may choose not to resume a session
Packit aea12f
even when requested to.  The expiration is to prevent temporal session keys
Packit aea12f
from becoming long-term keys. Also note that as a client you must enable, 
Packit aea12f
using the priority functions, at least the algorithms used in the last session.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_session_is_resumed}
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_session_get_id2}
Packit aea12f
Packit aea12f
@subsubheading Server side
Packit aea12f
Packit aea12f
A server enabling both session tickets and a storage for session data
Packit aea12f
would use session tickets when clients support it and the storage otherwise.
Packit aea12f
Packit aea12f
A storing server needs to specify callback functions to store, retrieve and delete session data. These can be
Packit aea12f
registered with the functions below. The stored sessions in the database can be checked using @funcref{gnutls_db_check_entry}
Packit aea12f
for expiration.
Packit aea12f
Packit aea12f
@showfuncD{gnutls_db_set_retrieve_function,gnutls_db_set_store_function,gnutls_db_set_ptr,gnutls_db_set_remove_function}
Packit aea12f
@showfuncA{gnutls_db_check_entry}
Packit aea12f
Packit aea12f
A server supporting session tickets must generate ticket encryption
Packit aea12f
and authentication keys using @funcref{gnutls_session_ticket_key_generate}.
Packit aea12f
Those keys should be associated with the GnuTLS session using
Packit aea12f
@funcref{gnutls_session_ticket_enable_server}.
Packit aea12f
Packit aea12f
Those will be the initial keys, but GnuTLS will rotate them regularly. The key rotation interval
Packit aea12f
can be changed with @funcref{gnutls_db_set_cache_expiration} and will be set to
Packit aea12f
three times the ticket expiration time (ie. three times the value given in that function).
Packit aea12f
Every such interval, new keys will be generated from those initial keys. This is a necessary mechanism
Packit aea12f
to prevent the keys from becoming long-term keys
Packit aea12f
and as such preserve forward-secrecy in the issued session tickets. If no explicit key rotation interval
Packit aea12f
is provided, GnuTLS will rotate them every 18 hours by default.
Packit aea12f
Packit aea12f
The master key can be shared between processes or between systems. Processes which share the same master key
Packit aea12f
will generate the same rotated subkeys, assuming they share the same time (irrespective of timezone differences).
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_session_ticket_enable_server}
Packit aea12f
@showfuncdesc{gnutls_session_ticket_key_generate}
Packit aea12f
@showfuncdesc{gnutls_session_resumption_requested}
Packit aea12f
Packit aea12f
The expiration time for session resumption, either in tickets or stored data
Packit aea12f
is set using @funcref{gnutls_db_set_cache_expiration}. This function also controls
Packit aea12f
the ticket key rotation period. Currently, the session key rotation interval is set
Packit aea12f
to 3 times the expiration time set by this function.
Packit aea12f
Packit aea12f
Under TLS 1.3, the server sends by default 2 tickets, and can send
Packit aea12f
additional session tickets at any time using @funcref{gnutls_session_ticket_send}.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_session_ticket_send}
Packit aea12f
Packit aea12f
@node Certificate verification
Packit aea12f
@subsection Certificate verification
Packit aea12f
@cindex DANE
Packit aea12f
@cindex DNSSEC
Packit aea12f
@cindex SSH-style authentication
Packit aea12f
@cindex Trust on first use
Packit aea12f
@cindex Key pinning
Packit aea12f
@tindex gnutls_certificate_verify_flags
Packit aea12f
Packit aea12f
In this section the functionality for additional certificate verification methods is listed. 
Packit aea12f
These methods are intended to be used in addition to normal PKI verification, in order to reduce 
Packit aea12f
the risk of a compromised CA being undetected.
Packit aea12f
Packit aea12f
@subsubsection Trust on first use
Packit aea12f
Packit aea12f
The GnuTLS library includes functionality to use an SSH-like trust on first use authentication.
Packit aea12f
The available functions to store and verify public keys are listed below.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_verify_stored_pubkey}
Packit aea12f
@showfuncdesc{gnutls_store_pubkey}
Packit aea12f
Packit aea12f
In addition to the above the @funcref{gnutls_store_commitment} can be 
Packit aea12f
used to implement a key-pinning architecture as in @xcite{KEYPIN}. 
Packit aea12f
This provides a way for web server to commit on a public key that is
Packit aea12f
not yet active.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_store_commitment}
Packit aea12f
Packit aea12f
The storage and verification functions may be used with the default
Packit aea12f
text file based back-end, or another back-end may be specified. That
Packit aea12f
should contain storage and retrieval functions and specified as below.
Packit aea12f
Packit aea12f
@showfuncE{gnutls_tdb_init,gnutls_tdb_deinit,gnutls_tdb_set_verify_func,gnutls_tdb_set_store_func,gnutls_tdb_set_store_commitment_func}
Packit aea12f
Packit aea12f
@subsubsection DANE verification
Packit aea12f
Since the DANE library is not included in GnuTLS it requires programs
Packit aea12f
to be linked against it. This can be achieved with the following commands.
Packit aea12f
Packit aea12f
@example
Packit aea12f
gcc -o foo foo.c `pkg-config gnutls-dane --cflags --libs`
Packit aea12f
@end example
Packit aea12f
Packit aea12f
When a program uses the GNU autoconf system, then the following
Packit aea12f
line or similar can be used to detect the presence of the library.
Packit aea12f
Packit aea12f
@example
Packit aea12f
PKG_CHECK_MODULES([LIBDANE], [gnutls-dane >= 3.0.0])
Packit aea12f
Packit aea12f
AC_SUBST([LIBDANE_CFLAGS])
Packit aea12f
AC_SUBST([LIBDANE_LIBS])
Packit aea12f
@end example
Packit aea12f
Packit aea12f
The high level functionality provided by the DANE library is shown below.
Packit aea12f
Packit aea12f
@showfuncdesc{dane_verify_crt}
Packit aea12f
Packit aea12f
@showfuncB{dane_verify_session_crt,dane_strerror}
Packit aea12f
Packit aea12f
Note that the @code{dane_state_t} structure that is accepted by both
Packit aea12f
verification functions is optional. It is required when many queries
Packit aea12f
are performed to optimize against multiple re-initializations of the
Packit aea12f
resolving back-end and loading of DNSSEC keys.
Packit aea12f
Packit aea12f
The following flags are returned by the verify functions to
Packit aea12f
indicate the status of the verification.
Packit aea12f
Packit aea12f
@showenumdesc{dane_verify_status_t,The DANE verification status flags.}
Packit aea12f
Packit aea12f
In order to generate a DANE TLSA entry to use in a DNS server 
Packit aea12f
you may use danetool (see @ref{danetool Invocation}).
Packit aea12f
Packit aea12f
Packit aea12f
Packit aea12f
@node TLS 1.2 re-authentication
Packit aea12f
@subsection TLS 1.2 re-authentication
Packit aea12f
@cindex re-negotiation
Packit aea12f
@cindex re-authentication
Packit aea12f
Packit aea12f
In TLS 1.2 or earlier there is no distinction between re-key, re-authentication, and re-negotiation.
Packit aea12f
All of these use cases are handled by the TLS' rehandshake process. For that reason
Packit aea12f
in GnuTLS rehandshake is not transparent to the application, and the application
Packit aea12f
must explicitly take control of that process. In addition GnuTLS since version 3.5.0 will not
Packit aea12f
allow the peer to switch identities during a rehandshake.
Packit aea12f
The threat addressed by that behavior depends on the application protocol,
Packit aea12f
but primarily it protects applications from being misled
Packit aea12f
by a rehandshake which switches the peer's identity. Applications can
Packit aea12f
disable this protection by using the @code{GNUTLS_ALLOW_ID_CHANGE} flag in
Packit aea12f
@funcref{gnutls_init}.
Packit aea12f
Packit aea12f
The following paragraphs explain how to safely use the rehandshake process.
Packit aea12f
Packit aea12f
@subsubsection Client side
Packit aea12f
Packit aea12f
According to the TLS specification a client may initiate a rehandshake at any
Packit aea12f
time. That can be achieved by calling @funcref{gnutls_handshake} and rely on its
Packit aea12f
return value for the outcome of the handshake (the server may deny a rehandshake).
Packit aea12f
If a server requests a re-handshake, then a call to @funcref{gnutls_record_recv} will
Packit aea12f
return GNUTLS_E_REHANDSHAKE in the client, instructing it to call @funcref{gnutls_handshake}.
Packit aea12f
To deny a rehandshake request by the server it is recommended to send a warning alert
Packit aea12f
of type GNUTLS_A_NO_RENEGOTIATION.
Packit aea12f
Packit aea12f
Due to limitations of early protocol versions, it is required to check whether
Packit aea12f
safe renegotiation is in place, i.e., using @funcref{gnutls_safe_renegotiation_status},
Packit aea12f
which ensures that the server remains the same as the initial.
Packit aea12f
Packit aea12f
To make re-authentication transparent to the application when requested
Packit aea12f
by the server, use the @code{GNUTLS_AUTO_REAUTH} flag on the
Packit aea12f
@funcref{gnutls_init} call. In that case the re-authentication will happen
Packit aea12f
in the call of @funcref{gnutls_record_recv} that received the
Packit aea12f
reauthentication request.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_safe_renegotiation_status}
Packit aea12f
Packit aea12f
@subsubsection Server side
Packit aea12f
Packit aea12f
A server which wants to instruct the client to re-authenticate, should call
Packit aea12f
@funcref{gnutls_rehandshake} and wait for the client to re-authenticate.
Packit aea12f
It is recommended to only request re-handshake when safe renegotiation is
Packit aea12f
enabled for that session (see @funcref{gnutls_safe_renegotiation_status} and
Packit aea12f
the discussion in @ref{Safe renegotiation}). A server could also encounter
Packit aea12f
the GNUTLS_E_REHANDSHAKE error code while receiving data. That indicates
Packit aea12f
a client-initiated re-handshake request. In that case the server could
Packit aea12f
ignore that request, perform handshake (unsafe when done generally), or
Packit aea12f
even drop the connection.
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_rehandshake}
Packit aea12f
Packit aea12f
@node TLS 1.3 re-authentication and re-key
Packit aea12f
@subsection TLS 1.3 re-authentication and re-key
Packit aea12f
@cindex re-key
Packit aea12f
@cindex re-negotiation
Packit aea12f
@cindex re-authentication
Packit aea12f
@cindex post-handshake authentication
Packit aea12f
Packit aea12f
The TLS 1.3 protocol distinguishes between re-key and re-authentication.
Packit aea12f
The re-key process ensures that fresh keys are supplied to the already
Packit aea12f
negotiated parameters, and on GnuTLS can be initiated using
Packit aea12f
@funcref{gnutls_session_key_update}. The re-key process can be one-way
Packit aea12f
(i.e., the calling party only changes its keys), or two-way where the peer
Packit aea12f
is requested to change keys as well.
Packit aea12f
Packit aea12f
The re-authentication process, allows the connected client to switch
Packit aea12f
identity by presenting a new certificate. Unlike TLS 1.2, the server
Packit aea12f
is not allowed to change identities. That client re-authentication, or
Packit aea12f
post-handshake authentication can be initiated only by the server using
Packit aea12f
@funcref{gnutls_reauth}, and only if a client has advertized support for it.
Packit aea12f
Both server and client have to explicitly enable support for post handshake
Packit aea12f
authentication using the @code{GNUTLS_POST_HANDSHAKE_AUTH} flag at @funcref{gnutls_init}.
Packit aea12f
Packit aea12f
A client receiving a re-authentication request will "see" the error code
Packit aea12f
@code{GNUTLS_E_REAUTH_REQUEST} at @funcref{gnutls_record_recv}. At this
Packit aea12f
point, it should also call @funcref{gnutls_reauth}.
Packit aea12f
Packit aea12f
To make re-authentication transparent to the application when requested
Packit aea12f
by the server, use the @code{GNUTLS_AUTO_REAUTH} and @code{GNUTLS_POST_HANDSHAKE_AUTH}
Packit aea12f
flags on the @funcref{gnutls_init} call. In that case the re-authentication will happen
Packit aea12f
in the call of @funcref{gnutls_record_recv} that received the
Packit aea12f
reauthentication request.
Packit aea12f
Packit aea12f
@node Parameter generation
Packit aea12f
@subsection Parameter generation
Packit aea12f
@cindex parameter generation
Packit aea12f
@cindex generating parameters
Packit aea12f
Packit aea12f
Prior to GnuTLS 3.6.0 for the ephemeral or anonymous Diffie-Hellman (DH) TLS ciphersuites
Packit aea12f
the application was required to generate or provide
Packit aea12f
DH parameters. That is no longer necessary as GnuTLS utilizes DH parameters
Packit aea12f
and negotiation from @xcite{RFC7919}.
Packit aea12f
Packit aea12f
Applications can tune the used parameters by explicitly specifying them
Packit aea12f
in the priority string. In server side applications can set the
Packit aea12f
minimum acceptable level of DH parameters by calling
Packit aea12f
@funcref{gnutls_certificate_set_known_dh_params},
Packit aea12f
@funcref{gnutls_anon_set_server_known_dh_params}, or
Packit aea12f
@funcref{gnutls_psk_set_server_known_dh_params}, depending on the type
Packit aea12f
of the credentials, to set the lower acceptable parameter limits. Typical
Packit aea12f
applications should rely on the default settings.
Packit aea12f
Packit aea12f
@showfuncC{gnutls_certificate_set_known_dh_params,gnutls_anon_set_server_known_dh_params,gnutls_psk_set_server_known_dh_params}
Packit aea12f
Packit aea12f
Packit aea12f
@subsubsection Legacy parameter generation
Packit aea12f
Note that older than 3.5.6 versions of GnuTLS provided functions
Packit aea12f
to generate or import arbitrary DH parameters from a file. This
Packit aea12f
practice is still supported but discouraged in current versions.
Packit aea12f
There is no known advantage from using random parameters, while there
Packit aea12f
have been several occasions where applications were utilizing incorrect,
Packit aea12f
weak or insecure parameters. This is the main reason GnuTLS includes the
Packit aea12f
well-known parameters of @xcite{RFC7919} and recommends applications
Packit aea12f
utilizing them.
Packit aea12f
Packit aea12f
In older applications which require to specify explicit DH parameters, we recommend
Packit aea12f
using @code{certtool} (of GnuTLS 3.5.6 or later) with the @code{--get-dh-params}
Packit aea12f
option to obtain the FFDHE parameters discussed above. The output
Packit aea12f
parameters of the tool are in PKCS#3 format and can be imported by
Packit aea12f
most existing applications.
Packit aea12f
Packit aea12f
The following functions are still supported but considered obsolete.
Packit aea12f
Packit aea12f
@showfuncC{gnutls_dh_params_generate2,gnutls_dh_params_import_pkcs3,gnutls_certificate_set_dh_params}
Packit aea12f
Packit aea12f
Packit aea12f
@node Deriving keys for other applications/protocols
Packit aea12f
@subsection Deriving keys for other applications/protocols
Packit aea12f
@cindex keying material exporters
Packit aea12f
@cindex exporting keying material
Packit aea12f
@cindex deriving keys
Packit aea12f
@cindex key extraction
Packit aea12f
Packit aea12f
In several cases, after a TLS connection is established, it is desirable
Packit aea12f
to derive keys to be used in another application or protocol (e.g., in an
Packit aea12f
other TLS session using pre-shared keys). The following describe GnuTLS'
Packit aea12f
implementation of RFC5705 to extract keys based on a session's master secret.
Packit aea12f
Packit aea12f
The API to use is @funcref{gnutls_prf_rfc5705}.  The 
Packit aea12f
function needs to be provided with a label,
Packit aea12f
and additional context data to mix in the @code{context} parameter. 
Packit aea12f
Packit aea12f
@showfuncdesc{gnutls_prf_rfc5705}
Packit aea12f
Packit aea12f
For example, after establishing a TLS session using
Packit aea12f
@funcref{gnutls_handshake}, you can obtain 32-bytes to be used as key, using this call:
Packit aea12f
Packit aea12f
@example
Packit aea12f
#define MYLABEL "EXPORTER-My-protocol-name"
Packit aea12f
#define MYCONTEXT "my-protocol's-1st-session"
Packit aea12f
Packit aea12f
char out[32];
Packit aea12f
rc = gnutls_prf_rfc5705 (session, sizeof(MYLABEL)-1, MYLABEL,
Packit aea12f
                         sizeof(MYCONTEXT)-1, MYCONTEXT, 32, out);
Packit aea12f
@end example
Packit aea12f
Packit aea12f
The output key depends on TLS' master secret, and is the same on both client
Packit aea12f
and server.
Packit aea12f
Packit aea12f
For legacy applications which need to use a more flexible API, there is
Packit aea12f
@funcref{gnutls_prf}, which in addition, allows to switch the mix of the
Packit aea12f
client and server random nonces, using the @code{server_random_first} parameter.
Packit aea12f
For additional flexibility and low-level access to the TLS1.2 PRF,
Packit aea12f
there is a low-level TLS PRF interface called @funcref{gnutls_prf_raw}.
Packit aea12f
That however is not functional under newer protocol versions.
Packit aea12f
Packit aea12f
@node Channel Bindings
Packit aea12f
@subsection Channel bindings
Packit aea12f
@cindex channel bindings
Packit aea12f
Packit aea12f
In user authentication protocols (e.g., EAP or SASL mechanisms) it is
Packit aea12f
useful to have a unique string that identifies the secure channel that
Packit aea12f
is used, to bind together the user authentication with the secure
Packit aea12f
channel.  This can protect against man-in-the-middle attacks in some
Packit aea12f
situations.  That unique string is called a ``channel binding''.  For
Packit aea12f
background and discussion see @xcite{RFC5056}.
Packit aea12f
Packit aea12f
In @acronym{GnuTLS} you can extract a channel binding using the
Packit aea12f
@funcref{gnutls_session_channel_binding} function.  Currently only the
Packit aea12f
type @code{GNUTLS_CB_TLS_UNIQUE} is supported, which corresponds to
Packit aea12f
the @code{tls-unique} channel binding for TLS defined in
Packit aea12f
@xcite{RFC5929}.
Packit aea12f
Packit aea12f
The following example describes how to print the channel binding data.
Packit aea12f
Note that it must be run after a successful TLS handshake.
Packit aea12f
Packit aea12f
@example
Packit aea12f
@{
Packit aea12f
  gnutls_datum_t cb;
Packit aea12f
  int rc;
Packit aea12f
Packit aea12f
  rc = gnutls_session_channel_binding (session,
Packit aea12f
                                       GNUTLS_CB_TLS_UNIQUE,
Packit aea12f
                                       &cb;;
Packit aea12f
  if (rc)
Packit aea12f
    fprintf (stderr, "Channel binding error: %s\n",
Packit aea12f
             gnutls_strerror (rc));
Packit aea12f
  else
Packit aea12f
    @{
Packit aea12f
      size_t i;
Packit aea12f
      printf ("- Channel binding 'tls-unique': ");
Packit aea12f
      for (i = 0; i < cb.size; i++)
Packit aea12f
        printf ("%02x", cb.data[i]);
Packit aea12f
      printf ("\n");
Packit aea12f
    @}
Packit aea12f
@}
Packit aea12f
@end example
Packit aea12f
Packit aea12f
@node Interoperability
Packit aea12f
@subsection Interoperability
Packit aea12f
Packit aea12f
The @acronym{TLS} protocols support many ciphersuites, extensions and version
Packit aea12f
numbers. As a result, few implementations are 
Packit aea12f
not able to properly interoperate once faced with extensions or version protocols
Packit aea12f
they do not support and understand. The @acronym{TLS} protocol allows for a
Packit aea12f
graceful downgrade to the commonly supported options, but practice shows 
Packit aea12f
it is not always implemented correctly. 
Packit aea12f
Packit aea12f
Because there is no way to achieve maximum interoperability with broken peers
Packit aea12f
without sacrificing security, @acronym{GnuTLS} ignores such peers by default. 
Packit aea12f
This might not be acceptable in cases where maximum compatibility
Packit aea12f
is required. Thus we allow enabling compatibility with broken peers using
Packit aea12f
priority strings (see @ref{Priority Strings}). A conservative priority
Packit aea12f
string that would disable certain @acronym{TLS} protocol
Packit aea12f
options that are known to cause compatibility problems, is shown below. 
Packit aea12f
@verbatim
Packit aea12f
NORMAL:%COMPAT
Packit aea12f
@end verbatim
Packit aea12f
Packit aea12f
For very old broken peers that do not tolerate TLS version numbers over TLS 1.0
Packit aea12f
another priority string is:
Packit aea12f
@verbatim
Packit aea12f
NORMAL:-VERS-ALL:+VERS-TLS1.0:+VERS-SSL3.0:%COMPAT
Packit aea12f
@end verbatim
Packit aea12f
This priority string will in addition to above, only enable SSL 3.0 and 
Packit aea12f
TLS 1.0 as protocols. 
Packit aea12f
Packit aea12f
Packit aea12f
@node Compatibility with the OpenSSL library
Packit aea12f
@subsection Compatibility with the OpenSSL library
Packit aea12f
@cindex OpenSSL
Packit aea12f
Packit aea12f
To ease @acronym{GnuTLS}' integration with existing applications, a
Packit aea12f
compatibility layer with the OpenSSL library is included
Packit aea12f
in the @code{gnutls-openssl} library. This compatibility layer is not
Packit aea12f
complete and it is not intended to completely re-implement the OpenSSL
Packit aea12f
API with @acronym{GnuTLS}.  It only provides limited source-level
Packit aea12f
compatibility. 
Packit aea12f
Packit aea12f
The prototypes for the compatibility functions are in the
Packit aea12f
@file{gnutls/openssl.h} header file. The limitations 
Packit aea12f
imposed by the compatibility layer include:
Packit aea12f
Packit aea12f
@itemize
Packit aea12f
Packit aea12f
@item Error handling is not thread safe.
Packit aea12f
Packit aea12f
@end itemize
Packit aea12f