Blame doc/cha-cert-auth.texi

Packit Service 4684c1
@node Certificate authentication
Packit Service 4684c1
@section Certificate authentication
Packit Service 4684c1
@cindex certificate authentication
Packit Service 4684c1
Packit Service 4684c1
The most known authentication method of @acronym{TLS} are certificates.
Packit Service 4684c1
The PKIX @xcite{PKIX} public key infrastructure is daily used by anyone
Packit Service 4684c1
using a browser today. @acronym{GnuTLS} provides a simple API to 
Packit Service 4684c1
verify the @acronym{X.509} certificates as in @xcite{PKIX}.
Packit Service 4684c1
Packit Service 4684c1
The key exchange algorithms supported by certificate authentication are
Packit Service 4684c1
shown in @ref{tab:key-exchange}.
Packit Service 4684c1
Packit Service 4684c1
@float Table,tab:key-exchange
Packit Service 4684c1
@multitable @columnfractions .2 .7
Packit Service 4684c1
Packit Service 4684c1
@headitem Key exchange @tab Description
Packit Service 4684c1
Packit Service 4684c1
@item RSA @tab
Packit Service 4684c1
The RSA algorithm is used to encrypt a key and send it to the peer.
Packit Service 4684c1
The certificate must allow the key to be used for encryption.
Packit Service 4684c1
Packit Service 4684c1
@item DHE_@-RSA @tab
Packit Service 4684c1
The RSA algorithm is used to sign ephemeral Diffie-Hellman parameters
Packit Service 4684c1
which are sent to the peer. The key in the certificate must allow the
Packit Service 4684c1
key to be used for signing. Note that key exchange algorithms which
Packit Service 4684c1
use ephemeral Diffie-Hellman parameters, offer perfect forward
Packit Service 4684c1
secrecy. That means that even if the private key used for signing is
Packit Service 4684c1
compromised, it cannot be used to reveal past session data.
Packit Service 4684c1
Packit Service 4684c1
@item ECDHE_@-RSA @tab
Packit Service 4684c1
The RSA algorithm is used to sign ephemeral elliptic curve Diffie-Hellman 
Packit Service 4684c1
parameters which are sent to the peer. The key in the certificate must allow 
Packit Service 4684c1
the key to be used for signing. It also offers perfect forward
Packit Service 4684c1
secrecy. That means that even if the private key used for signing is
Packit Service 4684c1
compromised, it cannot be used to reveal past session data.
Packit Service 4684c1
Packit Service 4684c1
@item DHE_@-DSS @tab
Packit Service 4684c1
The DSA algorithm is used to sign ephemeral Diffie-Hellman parameters
Packit Service 4684c1
which are sent to the peer. The certificate must contain DSA
Packit Service 4684c1
parameters to use this key exchange algorithm. DSA is the algorithm
Packit Service 4684c1
of the Digital Signature Standard (DSS).
Packit Service 4684c1
Packit Service 4684c1
@item ECDHE_@-ECDSA @tab
Packit Service 4684c1
The Elliptic curve DSA algorithm is used to sign ephemeral elliptic
Packit Service 4684c1
curve Diffie-Hellman parameters which are sent to the peer. The 
Packit Service 4684c1
certificate must contain ECDSA parameters (i.e., EC and marked for signing) 
Packit Service 4684c1
to use this key exchange algorithm. 
Packit Service 4684c1
Packit Service 4684c1
@end multitable
Packit Service 4684c1
@caption{Supported key exchange algorithms.}
Packit Service 4684c1
@end float
Packit Service 4684c1
Packit Service 4684c1
@menu
Packit Service 4684c1
* X.509 certificates::
Packit Service 4684c1
* OpenPGP certificates::
Packit Service 4684c1
* Raw public-keys::
Packit Service 4684c1
* Advanced certificate verification::
Packit Service 4684c1
* Digital signatures::
Packit Service 4684c1
@end menu
Packit Service 4684c1
Packit Service 4684c1
@node X.509 certificates
Packit Service 4684c1
@subsection @acronym{X.509} certificates
Packit Service 4684c1
@cindex X.509 certificates
Packit Service 4684c1
Packit Service 4684c1
The @acronym{X.509} protocols rely on a hierarchical trust model. In
Packit Service 4684c1
this trust model Certification Authorities (CAs) are used to certify
Packit Service 4684c1
entities.  Usually more than one certification authorities exist, and
Packit Service 4684c1
certification authorities may certify other authorities to issue
Packit Service 4684c1
certificates as well, following a hierarchical model.
Packit Service 4684c1
Packit Service 4684c1
@float Figure,fig-x509
Packit Service 4684c1
@image{gnutls-x509,7cm}
Packit Service 4684c1
@caption{An example of the X.509 hierarchical trust model.}
Packit Service 4684c1
@end float
Packit Service 4684c1
Packit Service 4684c1
One needs to trust one or more CAs for his secure communications. In
Packit Service 4684c1
that case only the certificates issued by the trusted authorities are
Packit Service 4684c1
acceptable.  The framework is illustrated on @ref{fig-x509}.
Packit Service 4684c1
Packit Service 4684c1
@menu
Packit Service 4684c1
* X.509 certificate structure::
Packit Service 4684c1
* Importing an X.509 certificate::
Packit Service 4684c1
* X.509 certificate names::
Packit Service 4684c1
* X.509 distinguished names::
Packit Service 4684c1
* X.509 extensions::
Packit Service 4684c1
* X.509 public and private keys::
Packit Service 4684c1
* Verifying X.509 certificate paths::
Packit Service 4684c1
* Verifying a certificate in the context of TLS session::
Packit Service 4684c1
* Verification using PKCS11::
Packit Service 4684c1
@end menu
Packit Service 4684c1
Packit Service 4684c1
@node X.509 certificate structure
Packit Service 4684c1
@subsubsection @acronym{X.509} certificate structure
Packit Service 4684c1
Packit Service 4684c1
An @acronym{X.509} certificate usually contains information about the
Packit Service 4684c1
certificate holder, the signer, a unique serial number, expiration
Packit Service 4684c1
dates and some other fields @xcite{PKIX} as shown in @ref{tab:x509}.
Packit Service 4684c1
Packit Service 4684c1
@float Table,tab:x509
Packit Service 4684c1
@multitable @columnfractions .2 .7
Packit Service 4684c1
Packit Service 4684c1
@headitem Field @tab Description
Packit Service 4684c1
Packit Service 4684c1
@item version @tab
Packit Service 4684c1
The field that indicates the version of the certificate.
Packit Service 4684c1
Packit Service 4684c1
@item serialNumber @tab
Packit Service 4684c1
This field holds a unique serial number per certificate.
Packit Service 4684c1
Packit Service 4684c1
@item signature @tab
Packit Service 4684c1
The issuing authority's signature.
Packit Service 4684c1
Packit Service 4684c1
@item issuer @tab
Packit Service 4684c1
Holds the issuer's distinguished name.
Packit Service 4684c1
Packit Service 4684c1
@item validity @tab
Packit Service 4684c1
The activation and expiration dates.
Packit Service 4684c1
Packit Service 4684c1
@item subject @tab
Packit Service 4684c1
The subject's distinguished name of the certificate.
Packit Service 4684c1
Packit Service 4684c1
@item extensions @tab
Packit Service 4684c1
The extensions are fields only present in version 3 certificates.
Packit Service 4684c1
Packit Service 4684c1
@end multitable
Packit Service 4684c1
@caption{X.509 certificate fields.}
Packit Service 4684c1
@end float
Packit Service 4684c1
Packit Service 4684c1
The certificate's @emph{subject or issuer name} is not just a single
Packit Service 4684c1
string.  It is a Distinguished name and in the @acronym{ASN.1}
Packit Service 4684c1
notation is a sequence of several object identifiers with their corresponding
Packit Service 4684c1
values. Some of available OIDs to be used in an @acronym{X.509}
Packit Service 4684c1
distinguished name are defined in @file{gnutls/x509.h}.
Packit Service 4684c1
Packit Service 4684c1
The @emph{Version} field in a certificate has values either 1 or 3 for
Packit Service 4684c1
version 3 certificates.  Version 1 certificates do not support the
Packit Service 4684c1
extensions field so it is not possible to distinguish a CA from a
Packit Service 4684c1
person, thus their usage should be avoided.
Packit Service 4684c1
Packit Service 4684c1
The @emph{validity} dates are there to indicate the date that the
Packit Service 4684c1
specific certificate was activated and the date the certificate's key
Packit Service 4684c1
would be considered invalid.
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
In @acronym{GnuTLS} the @acronym{X.509} certificate structures are
Packit Service 4684c1
handled using the @code{gnutls_x509_crt_t} type and the corresponding
Packit Service 4684c1
private keys with the @code{gnutls_x509_privkey_t} type.  All the
Packit Service 4684c1
available functions for @acronym{X.509} certificate handling have
Packit Service 4684c1
their prototypes in @file{gnutls/x509.h}. An example program to
Packit Service 4684c1
demonstrate the @acronym{X.509} parsing capabilities can be found in
Packit Service 4684c1
@ref{ex-x509-info}.
Packit Service 4684c1
Packit Service 4684c1
@node Importing an X.509 certificate
Packit Service 4684c1
@subsubsection Importing an X.509 certificate
Packit Service 4684c1
Packit Service 4684c1
The certificate structure should be initialized using @funcref{gnutls_x509_crt_init}, and 
Packit Service 4684c1
a certificate structure can be imported using @funcref{gnutls_x509_crt_import}. 
Packit Service 4684c1
Packit Service 4684c1
@showfuncC{gnutls_x509_crt_init,gnutls_x509_crt_import,gnutls_x509_crt_deinit}
Packit Service 4684c1
Packit Service 4684c1
In several functions an array of certificates is required. To assist in initialization
Packit Service 4684c1
and import the following two functions are provided.
Packit Service 4684c1
Packit Service 4684c1
@showfuncB{gnutls_x509_crt_list_import,gnutls_x509_crt_list_import2}
Packit Service 4684c1
Packit Service 4684c1
In all cases after use a certificate must be deinitialized using @funcref{gnutls_x509_crt_deinit}.
Packit Service 4684c1
Note that although the functions above apply to @code{gnutls_x509_crt_t} structure, similar functions
Packit Service 4684c1
exist for the CRL structure @code{gnutls_x509_crl_t}.
Packit Service 4684c1
Packit Service 4684c1
@node X.509 certificate names
Packit Service 4684c1
@subsubsection X.509 certificate names
Packit Service 4684c1
@cindex X.509 certificate name
Packit Service 4684c1
Packit Service 4684c1
X.509 certificates allow for multiple names and types of names to be specified.
Packit Service 4684c1
CA certificates often rely on X.509 distinguished names (see @ref{X.509 distinguished names})
Packit Service 4684c1
for unique identification, while end-user and server certificates rely on the
Packit Service 4684c1
'subject alternative names'. The subject alternative names provide a typed name, e.g.,
Packit Service 4684c1
a DNS name, or an email address, which identifies the owner of the certificate.
Packit Service 4684c1
The following functions provide access to that names.
Packit Service 4684c1
Packit Service 4684c1
@showfuncB{gnutls_x509_crt_get_subject_alt_name2,gnutls_x509_crt_set_subject_alt_name}
Packit Service 4684c1
@showfuncC{gnutls_subject_alt_names_init,gnutls_subject_alt_names_get,gnutls_subject_alt_names_set}
Packit Service 4684c1
Packit Service 4684c1
Note however, that server certificates often used the Common Name (CN), part of the
Packit Service 4684c1
certificate DistinguishedName to place a single DNS address. That practice is discouraged
Packit Service 4684c1
(see @xcite{RFC6125}), because only a single address can be specified, and the CN field is
Packit Service 4684c1
free-form making matching ambiguous.
Packit Service 4684c1
Packit Service 4684c1
@node X.509 distinguished names
Packit Service 4684c1
@subsubsection X.509 distinguished names
Packit Service 4684c1
@cindex X.509 distinguished name
Packit Service 4684c1
Packit Service 4684c1
The ``subject'' of an X.509 certificate is not described by
Packit Service 4684c1
a single name, but rather with a distinguished name. This in
Packit Service 4684c1
X.509 terminology is a list of strings each associated an object
Packit Service 4684c1
identifier. To make things simple GnuTLS provides @funcref{gnutls_x509_crt_get_dn2}
Packit Service 4684c1
which follows the rules in @xcite{RFC4514} and returns a single
Packit Service 4684c1
string. Access to each string by individual object identifiers
Packit Service 4684c1
can be accessed using @funcref{gnutls_x509_crt_get_dn_by_oid}.
Packit Service 4684c1
Packit Service 4684c1
@showfuncdesc{gnutls_x509_crt_get_dn2}
Packit Service 4684c1
@showfuncC{gnutls_x509_crt_get_dn,gnutls_x509_crt_get_dn_by_oid,gnutls_x509_crt_get_dn_oid}
Packit Service 4684c1
Packit Service 4684c1
Similar functions exist to access the distinguished name
Packit Service 4684c1
of the issuer of the certificate.
Packit Service 4684c1
Packit Service 4684c1
@showfuncE{gnutls_x509_crt_get_issuer_dn,gnutls_x509_crt_get_issuer_dn2,gnutls_x509_crt_get_issuer_dn_by_oid,gnutls_x509_crt_get_issuer_dn_oid,gnutls_x509_crt_get_issuer}
Packit Service 4684c1
Packit Service 4684c1
The more powerful @funcref{gnutls_x509_crt_get_subject} and 
Packit Service 4684c1
@funcref{gnutls_x509_dn_get_rdn_ava} provide efficient but low-level access
Packit Service 4684c1
to the contents of the distinguished name structure.
Packit Service 4684c1
Packit Service 4684c1
@showfuncB{gnutls_x509_crt_get_subject,gnutls_x509_crt_get_issuer}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_dn_get_rdn_ava}
Packit Service 4684c1
Packit Service 4684c1
@node X.509 extensions
Packit Service 4684c1
@subsubsection X.509 extensions
Packit Service 4684c1
@cindex X.509 extensions
Packit Service 4684c1
Packit Service 4684c1
X.509 version 3 certificates include a list of extensions that can
Packit Service 4684c1
be used to obtain additional information on the subject or the issuer
Packit Service 4684c1
of the certificate. Those may be e-mail addresses, flags that indicate whether the
Packit Service 4684c1
belongs to a CA etc.  All the supported @acronym{X.509} version 3
Packit Service 4684c1
extensions are shown in @ref{tab:x509-ext}.
Packit Service 4684c1
Packit Service 4684c1
The certificate extensions access is split into two parts. The first
Packit Service 4684c1
requires to retrieve the extension, and the second is the parsing part.
Packit Service 4684c1
Packit Service 4684c1
To enumerate and retrieve the DER-encoded extension data available in a certificate the following
Packit Service 4684c1
two functions are available.
Packit Service 4684c1
@showfuncC{gnutls_x509_crt_get_extension_info,gnutls_x509_crt_get_extension_data2,gnutls_x509_crt_get_extension_by_oid2}
Packit Service 4684c1
Packit Service 4684c1
After a supported DER-encoded extension is retrieved it can be parsed using the APIs in @code{x509-ext.h}.
Packit Service 4684c1
Complex extensions may require initializing an intermediate structure that holds the
Packit Service 4684c1
parsed extension data. Examples of simple parsing functions are shown below.
Packit Service 4684c1
@showfuncD{gnutls_x509_ext_import_basic_constraints,gnutls_x509_ext_export_basic_constraints,gnutls_x509_ext_import_key_usage,gnutls_x509_ext_export_key_usage}
Packit Service 4684c1
Packit Service 4684c1
More complex extensions, such as Name Constraints, require an intermediate structure, in that
Packit Service 4684c1
case @code{gnutls_x509_name_constraints_t} to be initialized in order to store the parsed
Packit Service 4684c1
extension data. 
Packit Service 4684c1
@showfuncB{gnutls_x509_ext_import_name_constraints,gnutls_x509_ext_export_name_constraints}
Packit Service 4684c1
Packit Service 4684c1
After the name constraints are extracted in the structure, the following functions
Packit Service 4684c1
can be used to access them.
Packit Service 4684c1
Packit Service 4684c1
@showfuncD{gnutls_x509_name_constraints_get_permitted,gnutls_x509_name_constraints_get_excluded,gnutls_x509_name_constraints_add_permitted,gnutls_x509_name_constraints_add_excluded}
Packit Service 4684c1
@showfuncB{gnutls_x509_name_constraints_check,gnutls_x509_name_constraints_check_crt}
Packit Service 4684c1
Packit Service 4684c1
Other utility functions are listed below.
Packit Service 4684c1
@showfuncB{gnutls_x509_name_constraints_init,gnutls_x509_name_constraints_deinit}
Packit Service 4684c1
Packit Service 4684c1
Similar functions exist for all of the other supported extensions, listed in @ref{tab:x509-ext}.
Packit Service 4684c1
Packit Service 4684c1
@float Table,tab:x509-ext
Packit Service 4684c1
@multitable @columnfractions .3 .2 .4
Packit Service 4684c1
Packit Service 4684c1
@headitem Extension @tab OID @tab Description
Packit Service 4684c1
Packit Service 4684c1
@item Subject key id @tab 2.5.29.14 @tab
Packit Service 4684c1
An identifier of the key of the subject.
Packit Service 4684c1
Packit Service 4684c1
@item Key usage @tab 2.5.29.15 @tab
Packit Service 4684c1
Constraints the key's usage of the certificate.
Packit Service 4684c1
Packit Service 4684c1
@item Private key usage period @tab 2.5.29.16 @tab
Packit Service 4684c1
Constraints the validity time of the private key.
Packit Service 4684c1
Packit Service 4684c1
@item Subject alternative name @tab 2.5.29.17 @tab
Packit Service 4684c1
Alternative names to subject's distinguished name.
Packit Service 4684c1
Packit Service 4684c1
@item Issuer alternative name @tab 2.5.29.18 @tab
Packit Service 4684c1
Alternative names to the issuer's distinguished name.
Packit Service 4684c1
Packit Service 4684c1
@item Basic constraints @tab 2.5.29.19 @tab
Packit Service 4684c1
Indicates whether this is a CA certificate or not, and specify the
Packit Service 4684c1
maximum path lengths of certificate chains.
Packit Service 4684c1
Packit Service 4684c1
@item Name constraints @tab 2.5.29.30 @tab
Packit Service 4684c1
A field in CA certificates that restricts the scope of the name of
Packit Service 4684c1
issued certificates.
Packit Service 4684c1
Packit Service 4684c1
@item CRL distribution points @tab 2.5.29.31 @tab
Packit Service 4684c1
This extension is set by the CA, in order to inform about the
Packit Service 4684c1
location of issued Certificate Revocation Lists.
Packit Service 4684c1
Packit Service 4684c1
@item Certificate policy @tab 2.5.29.32 @tab
Packit Service 4684c1
This extension is set to indicate the certificate policy as object
Packit Service 4684c1
identifier and may contain a descriptive string or URL.
Packit Service 4684c1
Packit Service 4684c1
@item Extended key usage @tab 2.5.29.54 @tab
Packit Service 4684c1
Inhibit any policy extension. Constraints the any policy OID
Packit Service 4684c1
(@code{GNUTLS_X509_OID_POLICY_ANY}) use in the policy extension.
Packit Service 4684c1
Packit Service 4684c1
@item Authority key identifier @tab 2.5.29.35 @tab
Packit Service 4684c1
An identifier of the key of the issuer of the certificate. That is
Packit Service 4684c1
used to distinguish between different keys of the same issuer.
Packit Service 4684c1
Packit Service 4684c1
@item Extended key usage @tab 2.5.29.37 @tab
Packit Service 4684c1
Constraints the purpose of the certificate.
Packit Service 4684c1
Packit Service 4684c1
@item Authority information access @tab 1.3.6.1.5.5.7.1.1 @tab
Packit Service 4684c1
Information on services by the issuer of the certificate.
Packit Service 4684c1
Packit Service 4684c1
@item Proxy Certification Information @tab 1.3.6.1.5.5.7.1.14 @tab
Packit Service 4684c1
Proxy Certificates includes this extension that contains the OID of
Packit Service 4684c1
the proxy policy language used, and can specify limits on the maximum
Packit Service 4684c1
lengths of proxy chains.  Proxy Certificates are specified in
Packit Service 4684c1
@xcite{RFC3820}.
Packit Service 4684c1
Packit Service 4684c1
@end multitable
Packit Service 4684c1
@caption{Supported X.509 certificate extensions.}
Packit Service 4684c1
@end float
Packit Service 4684c1
Packit Service 4684c1
Note, that there are also direct APIs to access extensions that may
Packit Service 4684c1
be simpler to use for non-complex extensions. They are available
Packit Service 4684c1
in @code{x509.h} and some examples are listed below.
Packit Service 4684c1
@showfuncD{gnutls_x509_crt_get_basic_constraints,gnutls_x509_crt_set_basic_constraints,gnutls_x509_crt_get_key_usage,gnutls_x509_crt_set_key_usage}
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
@node X.509 public and private keys
Packit Service 4684c1
@subsubsection Accessing public and private keys
Packit Service 4684c1
Packit Service 4684c1
Each X.509 certificate contains a public key that corresponds to a private key. To
Packit Service 4684c1
get a unique identifier of the public key the @funcref{gnutls_x509_crt_get_key_id}
Packit Service 4684c1
function is provided. To export the public key or its parameters you may need
Packit Service 4684c1
to convert the X.509 structure to a @code{gnutls_pubkey_t}. See 
Packit Service 4684c1
@ref{Abstract public keys} for more information.
Packit Service 4684c1
Packit Service 4684c1
@showfuncdesc{gnutls_x509_crt_get_key_id}
Packit Service 4684c1
Packit Service 4684c1
The private key parameters may be directly accessed by using one of the following functions.
Packit Service 4684c1
Packit Service 4684c1
@showfuncE{gnutls_x509_privkey_get_pk_algorithm2,gnutls_x509_privkey_export_rsa_raw2,gnutls_x509_privkey_export_ecc_raw,gnutls_x509_privkey_export_dsa_raw,gnutls_x509_privkey_get_key_id}
Packit Service 4684c1
Packit Service 4684c1
@node Verifying X.509 certificate paths
Packit Service 4684c1
@subsubsection Verifying @acronym{X.509} certificate paths
Packit Service 4684c1
@cindex verifying certificate paths
Packit Service 4684c1
Packit Service 4684c1
Verifying certificate paths is important in @acronym{X.509}
Packit Service 4684c1
authentication. For this purpose the following functions are
Packit Service 4684c1
provided.
Packit Service 4684c1
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_add_cas}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_add_named_crt}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_add_crls}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_verify_crt}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_verify_crt2}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_verify_named_crt}
Packit Service 4684c1
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_add_trust_file}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_add_trust_mem}
Packit Service 4684c1
@showfuncdesc{gnutls_x509_trust_list_add_system_trust}
Packit Service 4684c1
Packit Service 4684c1
The verification function will verify a given certificate chain against a list of certificate
Packit Service 4684c1
authorities and certificate revocation lists, and output
Packit Service 4684c1
a bit-wise OR of elements of the @code{gnutls_@-certificate_@-status_t} 
Packit Service 4684c1
enumeration shown in @ref{gnutls_certificate_status_t}. The @code{GNUTLS_@-CERT_@-INVALID} flag
Packit Service 4684c1
is always set on a verification error and more detailed flags will also be set when appropriate.
Packit Service 4684c1
Packit Service 4684c1
@showenumdesc{gnutls_certificate_status_t,The @code{gnutls_@-certificate_@-status_t} enumeration.}
Packit Service 4684c1
Packit Service 4684c1
An example of certificate verification is shown in @ref{ex-verify2}.
Packit Service 4684c1
It is also possible to have a set of certificates that
Packit Service 4684c1
are trusted for a particular server but not to authorize other certificates.
Packit Service 4684c1
This purpose is served by the functions @funcref{gnutls_x509_trust_list_add_named_crt} and @funcref{gnutls_x509_trust_list_verify_named_crt}.
Packit Service 4684c1
Packit Service 4684c1
@node Verifying a certificate in the context of TLS session
Packit Service 4684c1
@subsubsection Verifying a certificate in the context of TLS session
Packit Service 4684c1
@cindex verifying certificate paths
Packit Service 4684c1
@tindex gnutls_certificate_verify_flags
Packit Service 4684c1
Packit Service 4684c1
When operating in the context of a TLS session, the trusted certificate
Packit Service 4684c1
authority list may also be set using:
Packit Service 4684c1
@showfuncD{gnutls_certificate_set_x509_trust_file,gnutls_certificate_set_x509_trust_dir,gnutls_certificate_set_x509_crl_file,gnutls_certificate_set_x509_system_trust}
Packit Service 4684c1
Packit Service 4684c1
These functions allow the specification of the trusted certificate authorities, either
Packit Service 4684c1
via a file, a directory or use the system-specified certificate authorities. 
Packit Service 4684c1
Unless the authorities are application specific, it is generally recommended
Packit Service 4684c1
to use the system trust storage (see @funcref{gnutls_certificate_set_x509_system_trust}).
Packit Service 4684c1
Packit Service 4684c1
Unlike the previous section it is not required to setup a trusted list, and there
Packit Service 4684c1
are two approaches to verify the peer's certificate and identity.
Packit Service 4684c1
The recommended in GnuTLS 3.5.0 and later is via the @funcref{gnutls_session_set_verify_cert},
Packit Service 4684c1
but for older GnuTLS versions you may use an explicit callback set via
Packit Service 4684c1
@funcref{gnutls_certificate_set_verify_function} and then utilize
Packit Service 4684c1
@funcref{gnutls_certificate_verify_peers3} for verification.
Packit Service 4684c1
The reported verification status is identical to the verification functions described 
Packit Service 4684c1
in the previous section.
Packit Service 4684c1
Packit Service 4684c1
Note that in certain cases it is required to check the marked purpose of
Packit Service 4684c1
the end certificate (e.g. @code{GNUTLS_KP_TLS_WWW_SERVER}); in these cases
Packit Service 4684c1
the more advanced @funcref{gnutls_session_set_verify_cert2} and
Packit Service 4684c1
@funcref{gnutls_certificate_verify_peers} should be used instead.
Packit Service 4684c1
Packit Service 4684c1
There is also the possibility to pass some input to the verification
Packit Service 4684c1
functions in the form of flags. For @funcref{gnutls_x509_trust_list_verify_crt2} the
Packit Service 4684c1
flags are passed directly, but for
Packit Service 4684c1
@funcref{gnutls_certificate_verify_peers3}, the flags are set using
Packit Service 4684c1
@funcref{gnutls_certificate_set_verify_flags}.  All the available
Packit Service 4684c1
flags are part of the enumeration
Packit Service 4684c1
@code{gnutls_@-certificate_@-verify_@-flags} shown in @ref{gnutls_certificate_verify_flags}.
Packit Service 4684c1
Packit Service 4684c1
@showenumdesc{gnutls_certificate_verify_flags,The @code{gnutls_@-certificate_@-verify_@-flags} enumeration.}
Packit Service 4684c1
Packit Service 4684c1
@node Verification using PKCS11
Packit Service 4684c1
@subsubsection Verifying a certificate using PKCS #11
Packit Service 4684c1
@cindex verifying certificate with pkcs11
Packit Service 4684c1
Packit Service 4684c1
Some systems provide a system wide trusted certificate storage accessible using
Packit Service 4684c1
the PKCS #11 API. That is, the trusted certificates are queried and accessed using the
Packit Service 4684c1
PKCS #11 API, and trusted certificate properties, such as purpose, are marked using
Packit Service 4684c1
attached extensions. One example is the p11-kit trust module@footnote{see @url{https://p11-glue.github.io/p11-glue/trust-module.html}.}.
Packit Service 4684c1
Packit Service 4684c1
These special PKCS #11 modules can be used for GnuTLS certificate verification if marked as trust 
Packit Service 4684c1
policy modules, i.e., with @code{trust-policy: yes} in the p11-kit module file.
Packit Service 4684c1
The way to use them is by specifying to the file verification function (e.g., @funcref{gnutls_certificate_set_x509_trust_file}),
Packit Service 4684c1
a pkcs11 URL, or simply @code{pkcs11:} to use all the marked with trust policy modules.
Packit Service 4684c1
Packit Service 4684c1
The trust modules of p11-kit assign a purpose to trusted authorities using the extended
Packit Service 4684c1
key usage object identifiers. The common purposes are shown in @ref{tab:purposes}. Note
Packit Service 4684c1
that typically according to @xcite{RFC5280} the extended key usage object identifiers apply to end certificates. Their
Packit Service 4684c1
application to CA certificates is an extension used by the trust modules.
Packit Service 4684c1
Packit Service 4684c1
@float Table,tab:purposes
Packit Service 4684c1
@multitable @columnfractions .2 .2 .6
Packit Service 4684c1
Packit Service 4684c1
@headitem Purpose @tab OID @tab Description
Packit Service 4684c1
Packit Service 4684c1
@item GNUTLS_KP_TLS_WWW_SERVER @tab
Packit Service 4684c1
1.3.6.1.5.5.7.3.1 @tab
Packit Service 4684c1
The certificate is to be used for TLS WWW authentication. When in a CA certificate, it
Packit Service 4684c1
indicates that the CA is allowed to sign certificates for TLS WWW authentication.
Packit Service 4684c1
Packit Service 4684c1
@item GNUTLS_KP_TLS_WWW_CLIENT @tab
Packit Service 4684c1
1.3.6.1.5.5.7.3.2 @tab
Packit Service 4684c1
The certificate is to be used for TLS WWW client authentication. When in a CA certificate, it
Packit Service 4684c1
indicates that the CA is allowed to sign certificates for TLS WWW client authentication.
Packit Service 4684c1
Packit Service 4684c1
@item GNUTLS_KP_CODE_SIGNING @tab
Packit Service 4684c1
1.3.6.1.5.5.7.3.3 @tab
Packit Service 4684c1
The certificate is to be used for code signing. When in a CA certificate, it
Packit Service 4684c1
indicates that the CA is allowed to sign certificates for code signing.
Packit Service 4684c1
Packit Service 4684c1
@item GNUTLS_KP_EMAIL_PROTECTION @tab
Packit Service 4684c1
1.3.6.1.5.5.7.3.4 @tab
Packit Service 4684c1
The certificate is to be used for email protection. When in a CA certificate, it
Packit Service 4684c1
indicates that the CA is allowed to sign certificates for email users.
Packit Service 4684c1
Packit Service 4684c1
@item GNUTLS_KP_OCSP_SIGNING @tab
Packit Service 4684c1
1.3.6.1.5.5.7.3.9 @tab
Packit Service 4684c1
The certificate is to be used for signing OCSP responses. When in a CA certificate, it
Packit Service 4684c1
indicates that the CA is allowed to sign certificates which sign OCSP responses.
Packit Service 4684c1
Packit Service 4684c1
@item GNUTLS_KP_ANY @tab
Packit Service 4684c1
2.5.29.37.0 @tab
Packit Service 4684c1
The certificate is to be used for any purpose. When in a CA certificate, it
Packit Service 4684c1
indicates that the CA is allowed to sign any kind of certificates.
Packit Service 4684c1
Packit Service 4684c1
@end multitable
Packit Service 4684c1
@caption{Key purpose object identifiers.}
Packit Service 4684c1
@end float
Packit Service 4684c1
Packit Service 4684c1
With such modules, it is recommended to use the verification functions @funcref{gnutls_x509_trust_list_verify_crt2},
Packit Service 4684c1
or @funcref{gnutls_certificate_verify_peers}, which allow to explicitly specify the key purpose. The
Packit Service 4684c1
other verification functions which do not allow setting a purpose, would operate as if
Packit Service 4684c1
@code{GNUTLS_KP_TLS_WWW_SERVER} was requested from the trusted authorities.
Packit Service 4684c1
Packit Service 4684c1
@node OpenPGP certificates
Packit Service 4684c1
@subsection @acronym{OpenPGP} certificates
Packit Service 4684c1
@cindex OpenPGP certificates
Packit Service 4684c1
Packit Service 4684c1
Previous versions of GnuTLS supported limited @acronym{OpenPGP} key
Packit Service 4684c1
authentication. That functionality has been deprecated and is no longer
Packit Service 4684c1
made available. The reason is that, supporting alternative authentication
Packit Service 4684c1
methods, when X.509 and PKIX were new on the Internet and not well established, seemed like a
Packit Service 4684c1
good idea, in today's Internet X.509 is unquestionably the main
Packit Service 4684c1
container for certificates. As such supporting more options with no clear
Packit Service 4684c1
use-cases, is a distraction that consumes considerable resources for
Packit Service 4684c1
improving and testing the library. For that we have decided to drop
Packit Service 4684c1
this functionality completely in 3.6.0.
Packit Service 4684c1
Packit Service 4684c1
@node Raw public-keys
Packit Service 4684c1
@subsection Raw public-keys
Packit Service 4684c1
@cindex Raw public-keys
Packit Service 4684c1
Packit Service 4684c1
There are situations in which a rather large certificate / certificate chain is undesirable or impractical.
Packit Service 4684c1
An example could be a resource contrained sensor network in which you do want to use authentication of and
Packit Service 4684c1
encryption between your devices but where your devices lack loads of memory or processing power. Furthermore,
Packit Service 4684c1
there are situations in which you don't want to or can't rely on a PKIX. TLS is, next to a PKIX environment,
Packit Service 4684c1
also commonly used with self-signed certificates in smaller deployments where the self-signed certificates
Packit Service 4684c1
are distributed to all involved protocol endpoints out-of-band. This practice does, however, still require
Packit Service 4684c1
the overhead of the certificate generation even though none of the information found in the certificate is
Packit Service 4684c1
actually used.
Packit Service 4684c1
Packit Service 4684c1
With raw public-keys, only a subset of the information found in typical certificates is utilized: namely,
Packit Service 4684c1
the SubjectPublicKeyInfo structure (in ASN.1 format) of a PKIX certificate that carries the parameters
Packit Service 4684c1
necessary to describe the public-key. Other parameters found in PKIX certificates are omitted. By omitting
Packit Service 4684c1
various certificate-related structures, the resulting raw public-key is kept fairly small in comparison to
Packit Service 4684c1
the original certificate, and the code to process the keys can be simpler.
Packit Service 4684c1
Packit Service 4684c1
It should be noted however, that the authenticity of these raw keys must be verified by an out-of-band mechanism
Packit Service 4684c1
or something like @acronym{TOFU}.
Packit Service 4684c1
Packit Service 4684c1
@menu
Packit Service 4684c1
* Importing raw public-keys::
Packit Service 4684c1
@end menu
Packit Service 4684c1
Packit Service 4684c1
@node Importing raw public-keys
Packit Service 4684c1
@subsubsection Importing raw public-keys
Packit Service 4684c1
Raw public-keys and their private counterparts can best be handled by using the abstract types
Packit Service 4684c1
@code{gnutls_pubkey_t} and @code{gnutls_privkey_t} respectively. To learn how to use these
Packit Service 4684c1
see @ref{Abstract key types}.
Packit Service 4684c1
Packit Service 4684c1
@node Advanced certificate verification
Packit Service 4684c1
@subsection Advanced certificate verification
Packit Service 4684c1
@cindex Certificate verification
Packit Service 4684c1
Packit Service 4684c1
The verification of X.509 certificates in the HTTPS and other Internet protocols is typically 
Packit Service 4684c1
done by loading a trusted list of commercial Certificate Authorities
Packit Service 4684c1
(see @funcref{gnutls_certificate_set_x509_system_trust}), and using them as trusted anchors.
Packit Service 4684c1
However, there are several examples (eg. the Diginotar incident) where one of these
Packit Service 4684c1
authorities was compromised. This risk can be mitigated by using in addition to CA certificate verification,
Packit Service 4684c1
other verification methods. In this section we list the available in GnuTLS methods.
Packit Service 4684c1
Packit Service 4684c1
@menu
Packit Service 4684c1
* Verifying a certificate using trust on first use authentication::
Packit Service 4684c1
* Verifying a certificate using DANE::
Packit Service 4684c1
@end menu
Packit Service 4684c1
Packit Service 4684c1
@node Verifying a certificate using trust on first use authentication
Packit Service 4684c1
@subsubsection Verifying a certificate using trust on first use authentication
Packit Service 4684c1
@cindex verifying certificate paths
Packit Service 4684c1
@cindex SSH-style authentication
Packit Service 4684c1
@cindex Trust on first use
Packit Service 4684c1
@cindex Key pinning
Packit Service 4684c1
Packit Service 4684c1
It is possible to use a trust on first use (TOFU) authentication 
Packit Service 4684c1
method in GnuTLS. That is the concept used by the SSH programs, where the 
Packit Service 4684c1
public key of the peer is not verified, or verified in an out-of-bound way,
Packit Service 4684c1
but subsequent connections to the same peer require the public key to 
Packit Service 4684c1
remain the same.  Such a system in combination with the typical CA 
Packit Service 4684c1
verification of a certificate, and OCSP revocation checks,
Packit Service 4684c1
can help to provide multiple factor verification, where a single point of
Packit Service 4684c1
failure is not enough to compromise the system. For example a server compromise
Packit Service 4684c1
may be detected using OCSP, and a CA compromise can be detected using
Packit Service 4684c1
the trust on first use method.
Packit Service 4684c1
Such a hybrid system with X.509 and trust on first use authentication is 
Packit Service 4684c1
shown in @ref{Client example with SSH-style certificate verification}.
Packit Service 4684c1
Packit Service 4684c1
See @ref{Certificate verification} on how to use the available functionality.
Packit Service 4684c1
Packit Service 4684c1
@node Verifying a certificate using DANE
Packit Service 4684c1
@subsubsection Verifying a certificate using DANE (DNSSEC)
Packit Service 4684c1
@cindex verifying certificate paths
Packit Service 4684c1
@cindex DANE
Packit Service 4684c1
@cindex DNSSEC
Packit Service 4684c1
Packit Service 4684c1
The DANE protocol is a protocol that can be used to verify TLS certificates
Packit Service 4684c1
using the DNS (or better DNSSEC) protocols. The DNS security extensions (DNSSEC)
Packit Service 4684c1
provide an alternative public key infrastructure to the commercial CAs that
Packit Service 4684c1
are typically used to sign TLS certificates. The DANE protocol takes advantage
Packit Service 4684c1
of the DNSSEC infrastructure to verify TLS certificates. This can be 
Packit Service 4684c1
in addition to the verification by CA infrastructure or 
Packit Service 4684c1
may even replace it where DNSSEC is fully deployed. Note however, that DNSSEC deployment is
Packit Service 4684c1
fairly new and it would be better to use it as an additional verification
Packit Service 4684c1
method rather than the only one.
Packit Service 4684c1
Packit Service 4684c1
The DANE functionality is provided by the @code{libgnutls-dane} library that is shipped
Packit Service 4684c1
with GnuTLS and the function prototypes are in @code{gnutls/dane.h}. 
Packit Service 4684c1
See @ref{Certificate verification} for information on how to use the library.
Packit Service 4684c1
Packit Service 4684c1
Note however, that the DANE RFC mandates the verification methods
Packit Service 4684c1
one should use in addition to the validation via DNSSEC TLSA entries.
Packit Service 4684c1
GnuTLS doesn't follow that RFC requirement, and the term DANE verification
Packit Service 4684c1
in this manual refers to the TLSA entry verification. In GnuTLS any 
Packit Service 4684c1
other verification methods can be used (e.g., PKIX or TOFU) on top of
Packit Service 4684c1
DANE.
Packit Service 4684c1
Packit Service 4684c1
@node Digital signatures
Packit Service 4684c1
@subsection Digital signatures
Packit Service 4684c1
@cindex digital signatures
Packit Service 4684c1
Packit Service 4684c1
In this section we will provide some information about digital
Packit Service 4684c1
signatures, how they work, and give the rationale for disabling some
Packit Service 4684c1
of the algorithms used.
Packit Service 4684c1
Packit Service 4684c1
Digital signatures work by using somebody's secret key to sign some
Packit Service 4684c1
arbitrary data.  Then anybody else could use the public key of that
Packit Service 4684c1
person to verify the signature.  Since the data may be arbitrary it is
Packit Service 4684c1
not suitable input to a cryptographic digital signature algorithm. For
Packit Service 4684c1
this reason and also for performance cryptographic hash algorithms are
Packit Service 4684c1
used to preprocess the input to the signature algorithm. This works as
Packit Service 4684c1
long as it is difficult enough to generate two different messages with
Packit Service 4684c1
the same hash algorithm output. In that case the same signature could
Packit Service 4684c1
be used as a proof for both messages. Nobody wants to sign an innocent
Packit Service 4684c1
message of donating 1 euro to Greenpeace and find out that they
Packit Service 4684c1
donated 1.000.000 euros to Bad Inc.
Packit Service 4684c1
Packit Service 4684c1
For a hash algorithm to be called cryptographic the following three
Packit Service 4684c1
requirements must hold:
Packit Service 4684c1
Packit Service 4684c1
@enumerate
Packit Service 4684c1
@item Preimage resistance.
Packit Service 4684c1
That means the algorithm must be one way and given the output of the
Packit Service 4684c1
hash function @math{H(x)}, it is impossible to calculate @math{x}.
Packit Service 4684c1
Packit Service 4684c1
@item 2nd preimage resistance.
Packit Service 4684c1
That means that given a pair @math{x,y} with @math{y=H(x)} it is
Packit Service 4684c1
impossible to calculate an @math{x'} such that @math{y=H(x')}.
Packit Service 4684c1
Packit Service 4684c1
@item Collision resistance.
Packit Service 4684c1
That means that it is impossible to calculate random @math{x} and
Packit Service 4684c1
@math{x'} such @math{H(x')=H(x)}.
Packit Service 4684c1
@end enumerate
Packit Service 4684c1
Packit Service 4684c1
The last two requirements in the list are the most important in
Packit Service 4684c1
digital signatures. These protect against somebody who would like to
Packit Service 4684c1
generate two messages with the same hash output. When an algorithm is
Packit Service 4684c1
considered broken usually it means that the Collision resistance of
Packit Service 4684c1
the algorithm is less than brute force. Using the birthday paradox the
Packit Service 4684c1
brute force attack takes
Packit Service 4684c1
@iftex
Packit Service 4684c1
@math{2^{(\rm{hash\ size}) / 2}}
Packit Service 4684c1
@end iftex
Packit Service 4684c1
@ifnottex
Packit Service 4684c1
@math{2^{((hash size) / 2)}}
Packit Service 4684c1
@end ifnottex
Packit Service 4684c1
operations. Today colliding certificates using the MD5 hash algorithm
Packit Service 4684c1
have been generated as shown in @xcite{WEGER}.
Packit Service 4684c1
Packit Service 4684c1
There has been cryptographic results for the SHA-1 hash algorithms as
Packit Service 4684c1
well, although they are not yet critical.  Before 2004, MD5 had a
Packit Service 4684c1
presumed collision strength of @math{2^{64}}, but it has been showed
Packit Service 4684c1
to have a collision strength well under @math{2^{50}}.  As of November
Packit Service 4684c1
2005, it is believed that SHA-1's collision strength is around
Packit Service 4684c1
@math{2^{63}}.  We consider this sufficiently hard so that we still
Packit Service 4684c1
support SHA-1.  We anticipate that SHA-256/386/512 will be used in
Packit Service 4684c1
publicly-distributed certificates in the future.  When @math{2^{63}}
Packit Service 4684c1
can be considered too weak compared to the computer power available
Packit Service 4684c1
sometime in the future, SHA-1 will be disabled as well.  The collision
Packit Service 4684c1
attacks on SHA-1 may also get better, given the new interest in tools
Packit Service 4684c1
for creating them.
Packit Service 4684c1
Packit Service 4684c1
@subsubsection Trading security for interoperability
Packit Service 4684c1
Packit Service 4684c1
If you connect to a server and use GnuTLS' functions to verify the
Packit Service 4684c1
certificate chain, and get a @code{GNUTLS_CERT_INSECURE_ALGORITHM}
Packit Service 4684c1
validation error (see @ref{Verifying X.509 certificate paths}), it means
Packit Service 4684c1
that somewhere in the certificate chain there is a certificate signed
Packit Service 4684c1
using @code{RSA-MD2} or @code{RSA-MD5}.  These two digital signature
Packit Service 4684c1
algorithms are considered broken, so GnuTLS fails verifying
Packit Service 4684c1
the certificate.  In some situations, it may be useful to be
Packit Service 4684c1
able to verify the certificate chain anyway, assuming an attacker did
Packit Service 4684c1
not utilize the fact that these signatures algorithms are broken.
Packit Service 4684c1
This section will give help on how to achieve that.
Packit Service 4684c1
Packit Service 4684c1
It is important to know that you do not have to enable any of
Packit Service 4684c1
the flags discussed here to be able to use trusted root CA
Packit Service 4684c1
certificates self-signed using @code{RSA-MD2} or @code{RSA-MD5}. The
Packit Service 4684c1
certificates in the trusted list are considered trusted irrespective
Packit Service 4684c1
of the signature.
Packit Service 4684c1
Packit Service 4684c1
If you are using @funcref{gnutls_certificate_verify_peers3} to verify the
Packit Service 4684c1
certificate chain, you can call
Packit Service 4684c1
@funcref{gnutls_certificate_set_verify_flags} with the flags:
Packit Service 4684c1
@itemize
Packit Service 4684c1
@item @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2}
Packit Service 4684c1
@item @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5}
Packit Service 4684c1
@item @code{GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1}
Packit Service 4684c1
@item @code{GNUTLS_VERIFY_ALLOW_BROKEN}
Packit Service 4684c1
@end itemize
Packit Service 4684c1
as in the following example:
Packit Service 4684c1
Packit Service 4684c1
@example
Packit Service 4684c1
  gnutls_certificate_set_verify_flags (x509cred,
Packit Service 4684c1
                                       GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
Packit Service 4684c1
@end example
Packit Service 4684c1
Packit Service 4684c1
This will signal the verifier algorithm to enable @code{RSA-MD5} when
Packit Service 4684c1
verifying the certificates.
Packit Service 4684c1
Packit Service 4684c1
If you are using @funcref{gnutls_x509_crt_verify} or
Packit Service 4684c1
@funcref{gnutls_x509_crt_list_verify}, you can pass the
Packit Service 4684c1
@code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5} parameter directly in the
Packit Service 4684c1
@code{flags} parameter.
Packit Service 4684c1
Packit Service 4684c1
If you are using these flags, it may also be a good idea to warn the
Packit Service 4684c1
user when verification failure occur for this reason.  The simplest is
Packit Service 4684c1
to not use the flags by default, and only fall back to using them
Packit Service 4684c1
after warning the user.  If you wish to inspect the certificate chain
Packit Service 4684c1
yourself, you can use @funcref{gnutls_certificate_get_peers} to extract
Packit Service 4684c1
the raw server's certificate chain, @funcref{gnutls_x509_crt_list_import} to parse each of the certificates, and
Packit Service 4684c1
then @funcref{gnutls_x509_crt_get_signature_algorithm} to find out the
Packit Service 4684c1
signing algorithm used for each certificate.  If any of the
Packit Service 4684c1
intermediary certificates are using @code{GNUTLS_SIGN_RSA_MD2} or
Packit Service 4684c1
@code{GNUTLS_SIGN_RSA_MD5}, you could present a warning.