texinfo documentation is similar to the printed manual.
[gnutls.git] / doc / cha-cert-auth.texi
blobcb79a7d4c56ce0f3ac1b51bab43b20ae7ea4a7f9
1 @node Certificate authentication
2 @chapter Certificate authentication
3 @cindex certificate authentication
5 The most known authentication method of @acronym{TLS} are certificates.
6 The PKIX @xcite{PKIX} public key infrastructure is daily used by anyone
7 using a browser today. @acronym{GnuTLS} supports both 
8 @acronym{X.509} certificates @xcite{PKIX} and @acronym{OpenPGP}
9 certificates using a common API.
11 The key exchange algorithms supported by certificate authentication are
12 shown in @ref{tab:key-exchange}.
14 @float Table,tab:key-exchange
15 @multitable @columnfractions .2 .7
17 @headitem Key exchange @tab Description
19 @item RSA @tab
20 The RSA algorithm is used to encrypt a key and send it to the peer.
21 The certificate must allow the key to be used for encryption.
23 @item RSA_@-EXPORT @tab
24 The RSA algorithm is used to encrypt a key and send it to the peer.
25 In the EXPORT algorithm, the server signs temporary RSA parameters of
26 512 bits --- which are considered weak --- and sends them to the
27 client.
29 @item DHE_@-RSA @tab
30 The RSA algorithm is used to sign ephemeral Diffie-Hellman parameters
31 which are sent to the peer. The key in the certificate must allow the
32 key to be used for signing. Note that key exchange algorithms which
33 use ephemeral Diffie-Hellman parameters, offer perfect forward
34 secrecy. That means that even if the private key used for signing is
35 compromised, it cannot be used to reveal past session data.
37 @item ECDHE_@-RSA @tab
38 The RSA algorithm is used to sign ephemeral elliptic curve Diffie-Hellman 
39 parameters which are sent to the peer. The key in the certificate must allow 
40 the key to be used for signing. It also offers perfect forward
41 secrecy. That means that even if the private key used for signing is
42 compromised, it cannot be used to reveal past session data.
44 @item DHE_@-DSS @tab
45 The DSA algorithm is used to sign ephemeral Diffie-Hellman parameters
46 which are sent to the peer. The certificate must contain DSA
47 parameters to use this key exchange algorithm. DSA is the algorithm
48 of the Digital Signature Standard (DSS).
50 @item ECDHE_@-ECDSA @tab
51 The Elliptic curve DSA algorithm is used to sign ephemeral elliptic
52 curve Diffie-Hellman parameters which are sent to the peer. The 
53 certificate must contain ECDSA parameters to use this key exchange 
54 algorithm. 
56 @end multitable
57 @caption{Supported key exchange algorithms.}
58 @end float
60 @menu
61 * X.509 certificates::
62 * OpenPGP certificates::
63 * Digital signatures::
64 @end menu
66 @node X.509 certificates
67 @section @acronym{X.509} certificates
68 @cindex X.509 certificates
70 The @acronym{X.509} protocols rely on a hierarchical trust model. In
71 this trust model Certification Authorities (CAs) are used to certify
72 entities.  Usually more than one certification authorities exist, and
73 certification authorities may certify other authorities to issue
74 certificates as well, following a hierarchical model.
76 @float Figure,fig:x509
77 @image{gnutls-x509,7cm}
78 @caption{An example of the X.509 hierarchical trust model.}
79 @end float
81 One needs to trust one or more CAs for his secure communications. In
82 that case only the certificates issued by the trusted authorities are
83 acceptable.  The framework is illustrated on @ref{fig:x509}.
85 @menu
86 * X.509 certificate structure::
87 * X.509 distinguished names::
88 * Verifying X.509 certificate paths::
89 * Verifying a certificate in the context of TLS session::
90 * Verifying a certificate using trust on first use authentication::
91 @end menu
93 @node X.509 certificate structure
94 @subsection @acronym{X.509} certificate structure
96 An @acronym{X.509} certificate usually contains information about the
97 certificate holder, the signer, a unique serial number, expiration
98 dates and some other fields @xcite{PKIX} as shown in @ref{tab:x509}.
100 @float Table,tab:x509
101 @multitable @columnfractions .2 .7
103 @headitem Field @tab Description
105 @item version @tab
106 The field that indicates the version of the certificate.
108 @item serialNumber @tab
109 This field holds a unique serial number per certificate.
111 @item signature @tab
112 The issuing authority's signature.
114 @item issuer @tab
115 Holds the issuer's distinguished name.
117 @item validity @tab
118 The activation and expiration dates.
120 @item subject @tab
121 The subject's distinguished name of the certificate.
123 @item extensions @tab
124 The extensions are fields only present in version 3 certificates.
126 @end multitable
127 @caption{X.509 certificate fields.}
128 @end float
130 The certificate's @emph{subject or issuer name} is not just a single
131 string.  It is a Distinguished name and in the @acronym{ASN.1}
132 notation is a sequence of several object identifiers with their corresponding
133 values. Some of available OIDs to be used in an @acronym{X.509}
134 distinguished name are defined in @file{gnutls/x509.h}.
136 The @emph{Version} field in a certificate has values either 1 or 3 for
137 version 3 certificates.  Version 1 certificates do not support the
138 extensions field so it is not possible to distinguish a CA from a
139 person, thus their usage should be avoided.
141 The @emph{validity} dates are there to indicate the date that the
142 specific certificate was activated and the date the certificate's key
143 would be considered invalid.
145 Certificate @emph{extensions} are there to include information about
146 the certificate's subject that did not fit in the typical certificate
147 fields. Those may be e-mail addresses, flags that indicate whether the
148 belongs to a CA etc.  All the supported @acronym{X.509} version 3
149 extensions are shown in @ref{tab:x509-ext}.
151 @float Table,tab:x509-ext
152 @multitable @columnfractions .3 .2 .4
154 @headitem Extension @tab OID @tab Description
156 @item Subject key id @tab 2.5.29.14 @tab
157 An identifier of the key of the subject.
159 @item Authority key id @tab 2.5.29.35 @tab
160 An identifier of the authority's key used to sign the certificate.
162 @item Subject alternative name @tab 2.5.29.17 @tab
163 Alternative names to subject's distinguished name.
165 @item Key usage @tab 2.5.29.15 @tab
166 Constraints the key's usage of the certificate.
168 @item Extended key usage @tab 2.5.29.37 @tab
169 Constraints the purpose of the certificate.
171 @item Basic constraints @tab 2.5.29.19 @tab
172 Indicates whether this is a CA certificate or not, and specify the
173 maximum path lengths of certificate chains.
175 @item CRL distribution points @tab 2.5.29.31 @tab
176 This extension is set by the CA, in order to inform about the issued
177 CRLs.
179 @item Proxy Certification Information @tab 1.3.6.1.5.5.7.1.14 @tab
180 Proxy Certificates includes this extension that contains the OID of
181 the proxy policy language used, and can specify limits on the maximum
182 lengths of proxy chains.  Proxy Certificates are specified in
183 @xcite{RFC3820}.
185 @end multitable
186 @caption{X.509 certificate extensions.}
187 @end float
189 In @acronym{GnuTLS} the @acronym{X.509} certificate structures are
190 handled using the @code{gnutls_x509_crt_t} type and the corresponding
191 private keys with the @code{gnutls_x509_privkey_t} type.  All the
192 available functions for @acronym{X.509} certificate handling have
193 their prototypes in @file{gnutls/x509.h}. An example program to
194 demonstrate the @acronym{X.509} parsing capabilities can be found at
195 @ref{ex:x509-info}.
197 @node X.509 distinguished names
198 @subsection X.509 distinguished names
199 @cindex X.509 distinguished name
201 The ``subject'' of an X.509 certificate is not described by
202 a single name, but rather with a distinguished name. This in
203 X.509 terminology is a list of strings each associated an object
204 identifier. To make things simple GnuTLS provides @funcref{gnutls_x509_crt_get_dn}
205 which follows the rules in @xcite{RFC4514} and returns a single
206 string. Access to each string by individual object identifiers
207 can be accessed using @funcref{gnutls_x509_crt_get_dn_by_oid}.
209 @showfuncdesc{gnutls_x509_crt_get_dn}
210 @showfuncdesc{gnutls_x509_crt_get_dn_by_oid}
211 @showfuncdesc{gnutls_x509_crt_get_dn_oid}
213 The more powerful @funcref{gnutls_x509_crt_get_subject} and 
214 @funcref{gnutls_x509_dn_get_rdn_ava} provide efficient access
215 to the contents of the distinguished name structure.
217 @showfuncdesc{gnutls_x509_crt_get_subject}
218 @showfuncdesc{gnutls_x509_dn_get_rdn_ava}
220 Similar functions exist to access the distinguished name
221 of the issuer of the certificate.
223 @showfuncD{gnutls_x509_crt_get_issuer_dn,gnutls_x509_crt_get_issuer_dn_by_oid,gnutls_x509_crt_get_issuer_dn_oid,gnutls_x509_crt_get_issuer}
225 @node Verifying X.509 certificate paths
226 @subsection Verifying @acronym{X.509} certificate paths
227 @cindex verifying certificate paths
229 Verifying certificate paths is important in @acronym{X.509}
230 authentication. For this purpose the following functions are
231 provided.
233 @showfuncdesc{gnutls_x509_trust_list_add_cas}
234 @showfuncdesc{gnutls_x509_trust_list_add_named_crt}
235 @showfuncdesc{gnutls_x509_trust_list_add_crls}
236 @showfuncdesc{gnutls_x509_trust_list_verify_crt}
237 @showfuncdesc{gnutls_x509_trust_list_verify_named_crt}
239 The verification function will verify a given certificate chain against a list of certificate
240 authorities and certificate revocation lists, and output
241 a bit-wise OR of elements of the @code{gnutls_@-certificate_@-status_t} 
242 enumeration shown in @ref{gnutls_certificate_status_t}. 
244 @showenumdesc{gnutls_certificate_status_t,The @code{gnutls_@-certificate_@-status_t} enumeration.}
246 An example of certificate verification is shown in @ref{ex:verify2}.
247 It is also possible to have a set of certificates that
248 are trusted for a particular server but not to authorize other certificates.
249 This purpose is served by the functions @funcref{gnutls_x509_trust_list_add_named_crt} and @funcref{gnutls_x509_trust_list_verify_named_crt}.
251 @node Verifying a certificate in the context of TLS session
252 @subsection Verifying a certificate in the context of TLS session
253 @cindex verifying certificate paths
254 @tindex gnutls_certificate_verify_flags
256 When operating in the context of a TLS session, if the trusted certificate
257 authority list has been set via the
258 @funcref{gnutls_certificate_set_x509_trust_file} and @funcref{gnutls_certificate_set_x509_crl_file},
259 it is not required to setup a trusted list as above.
260 The function @funcref{gnutls_certificate_verify_peers2} 
261 may then be used to verify the peer's certificate chain.
263 There is also the possibility to pass some input to the verification
264 functions in the form of flags. For @funcref{gnutls_x509_trust_list_verify_crt} the
265 flags are passed straightforward, but
266 @funcref{gnutls_certificate_verify_peers2} depends on the flags set by
267 calling @funcref{gnutls_certificate_set_verify_flags}.  All the available
268 flags are part of the enumeration
269 @code{gnutls_@-certificate_@-verify_@-flags} shown in @ref{gnutls_certificate_verify_flags}.
271 @showenumdesc{gnutls_certificate_verify_flags,The @code{gnutls_@-certificate_@-verify_@-flags} enumeration.}
273 Although the verification of a certificate path indicates that the
274 certificate is signed by trusted authority, does not reveal anything
275 about the peer's identity. It is required to verify if the
276 certificate's owner is the one you expect. For more information
277 consult @xcite{RFC2818} and section @ref{ex:verify} for an example.
279 @node Verifying a certificate using trust on first use authentication
280 @subsection Verifying a certificate using trust on first use authentication
281 @cindex verifying certificate paths
282 @cindex SSH-style authentication
283 @cindex Trust on first use
284 @cindex Key pinning
285 @tindex gnutls_certificate_verify_flags
287 It is possible to use a trust on first use (similar to SSH) authentication 
288 method in GnuTLS. That is the concept used by the SSH programs, where the 
289 public key of the peer is not verified, or verified in an out-of-bound way,
290 but subsequent connections to the same peer require the public key to 
291 remain the same.  Such a system in combination with the typical CA 
292 verification of a certificate, and OCSP revocation checks,
293 can help to provide multiple factor verification, where a single point of
294 failure is not enough to compromise the system. For example a server compromise
295 may be detected using OCSP, and a CA compromise can be detected using
296 the trust on first use method.
297 Such a hybrid system with X.509 and trust on first use authentication is 
298 shown in @ref{Simple client example with SSH-style certificate verification}.
300 @showfuncdesc{gnutls_verify_stored_pubkey}
301 @showfuncdesc{gnutls_store_pubkey}
303 In addition to the above the @funcref{gnutls_store_commitment} can be 
304 used to implement a key-pinning architecture as in @xcite{KEYPIN}. 
305 This provides a way for web server to commit on a public key that is
306 not yet active.
308 @showfuncdesc{gnutls_store_commitment}
310 The storage and verification functions may be used with the default
311 text file based back-end, or another back-end may be specified. That
312 should contain storage and retrieval functions and specified as below.
314 @showfuncE{gnutls_tdb_init,gnutls_tdb_deinit,gnutls_tdb_set_verify_func,gnutls_tdb_set_store_func,gnutls_tdb_set_store_commitment_func}
316 @node OpenPGP certificates
317 @section @acronym{OpenPGP} certificates
318 @cindex OpenPGP certificates
320 The @acronym{OpenPGP} key authentication relies on a distributed trust
321 model, called the ``web of trust''. The ``web of trust'' uses a
322 decentralized system of trusted introducers, which are the same as a
323 CA. @acronym{OpenPGP} allows anyone to sign anyone else's public
324 key. When Alice signs Bob's key, she is introducing Bob's key to
325 anyone who trusts Alice. If someone trusts Alice to introduce keys,
326 then Alice is a trusted introducer in the mind of that observer.
327 For example in @ref{fig:openpgp}, David trusts Alice to be an introducer and Alice
328 signed Bob's key thus Dave trusts Bob's key to be the real one.
330 @float Figure,fig:openpgp
331 @image{gnutls-pgp,8cm}
332 @caption{An example of the OpenPGP trust model.}
333 @end float
335 There are some key points that are important in that model. In the
336 example Alice has to sign Bob's key, only if she is sure that the key
337 belongs to Bob. Otherwise she may also make Dave falsely believe that
338 this is Bob's key. Dave has also the responsibility to know who to
339 trust.  This model is similar to real life relations.
341 Just see how Charlie behaves in the previous example. Although he has
342 signed Bob's key - because he knows, somehow, that it belongs to Bob -
343 he does not trust Bob to be an introducer. Charlie decided to trust
344 only Kevin, for some reason. A reason could be that Bob is lazy
345 enough, and signs other people's keys without being sure that they
346 belong to the actual owner.
348 @subsection @acronym{OpenPGP} certificate structure
350 In @acronym{GnuTLS} the @acronym{OpenPGP} key structures
351 @xcite{RFC2440} are handled using the @code{gnutls_openpgp_crt_t} type
352 and the corresponding private keys with the
353 @code{gnutls_openpgp_privkey_t} type. All the prototypes for the key
354 handling functions can be found at @file{gnutls/openpgp.h}.
356 @subsection Verifying an @acronym{OpenPGP} certificate
358 The verification functions of @acronym{OpenPGP} keys, included in
359 @acronym{GnuTLS}, are simple ones, and do not use the features of the
360 ``web of trust''.  For that reason, if the verification needs are
361 complex, the assistance of external tools like @acronym{GnuPG} and
362 GPGME@footnote{@url{http://www.gnupg.org/related_software/gpgme/}} is
363 recommended.
365 In GnuTLS there is a verification function for OpenPGP certificates,
366 the @funcref{gnutls_openpgp_crt_verify_ring}.  This checks an
367 @acronym{OpenPGP} key against a given set of public keys (keyring) and
368 returns the key status. The key verification status is the same as in
369 @acronym{X.509} certificates, although the meaning and interpretation
370 are different. For example an @acronym{OpenPGP} key may be valid, if
371 the self signature is ok, even if no signers were found.  The meaning
372 of verification status flags is the same as in the @acronym{X.509} certificates
373 (see @ref{gnutls_certificate_verify_flags}).
375 @showfuncdesc{gnutls_openpgp_crt_verify_ring}
377 @showfuncdesc{gnutls_openpgp_crt_verify_self}
379 @subsection Verifying a certificate in the context of a TLS session
381 Similarly with X.509 certificates, one needs to specify
382 the OpenPGP keyring file in the credentials structure. The certificates
383 in this file will be  used by @funcref{gnutls_certificate_verify_peers2}
384 to verify the signatures in the certificate sent by the peer.
386 @showfuncdesc{gnutls_certificate_set_openpgp_keyring_file}
391 @node Digital signatures
392 @section Digital signatures
393 @cindex digital signatures
395 In this section we will provide some information about digital
396 signatures, how they work, and give the rationale for disabling some
397 of the algorithms used.
399 Digital signatures work by using somebody's secret key to sign some
400 arbitrary data.  Then anybody else could use the public key of that
401 person to verify the signature.  Since the data may be arbitrary it is
402 not suitable input to a cryptographic digital signature algorithm. For
403 this reason and also for performance cryptographic hash algorithms are
404 used to preprocess the input to the signature algorithm. This works as
405 long as it is difficult enough to generate two different messages with
406 the same hash algorithm output. In that case the same signature could
407 be used as a proof for both messages. Nobody wants to sign an innocent
408 message of donating 1 @euro{} to Greenpeace and find out that he
409 donated 1.000.000 @euro{} to Bad Inc.
411 For a hash algorithm to be called cryptographic the following three
412 requirements must hold:
414 @enumerate
415 @item Preimage resistance.
416 That means the algorithm must be one way and given the output of the
417 hash function @math{H(x)}, it is impossible to calculate @math{x}.
419 @item 2nd preimage resistance.
420 That means that given a pair @math{x,y} with @math{y=H(x)} it is
421 impossible to calculate an @math{x'} such that @math{y=H(x')}.
423 @item Collision resistance.
424 That means that it is impossible to calculate random @math{x} and
425 @math{x'} such @math{H(x')=H(x)}.
426 @end enumerate
428 The last two requirements in the list are the most important in
429 digital signatures. These protect against somebody who would like to
430 generate two messages with the same hash output. When an algorithm is
431 considered broken usually it means that the Collision resistance of
432 the algorithm is less than brute force. Using the birthday paradox the
433 brute force attack takes
434 @iftex
435 @math{2^{(\rm{hash\ size}) / 2}}
436 @end iftex
437 @ifnottex
438 @math{2^{((hash size) / 2)}}
439 @end ifnottex
440 operations. Today colliding certificates using the MD5 hash algorithm
441 have been generated as shown in @xcite{WEGER}.
443 There has been cryptographic results for the SHA-1 hash algorithms as
444 well, although they are not yet critical.  Before 2004, MD5 had a
445 presumed collision strength of @math{2^{64}}, but it has been showed
446 to have a collision strength well under @math{2^{50}}.  As of November
447 2005, it is believed that SHA-1's collision strength is around
448 @math{2^{63}}.  We consider this sufficiently hard so that we still
449 support SHA-1.  We anticipate that SHA-256/386/512 will be used in
450 publicly-distributed certificates in the future.  When @math{2^{63}}
451 can be considered too weak compared to the computer power available
452 sometime in the future, SHA-1 will be disabled as well.  The collision
453 attacks on SHA-1 may also get better, given the new interest in tools
454 for creating them.
456 @subsection Trading security for interoperability
458 If you connect to a server and use GnuTLS' functions to verify the
459 certificate chain, and get a @code{GNUTLS_CERT_INSECURE_ALGORITHM}
460 validation error (see @ref{Verifying X.509 certificate paths}), it means
461 that somewhere in the certificate chain there is a certificate signed
462 using @code{RSA-MD2} or @code{RSA-MD5}.  These two digital signature
463 algorithms are considered broken, so GnuTLS fails verifying
464 the certificate.  In some situations, it may be useful to be
465 able to verify the certificate chain anyway, assuming an attacker did
466 not utilize the fact that these signatures algorithms are broken.
467 This section will give help on how to achieve that.
469 It is important to know that you do not have to enable any of
470 the flags discussed here to be able to use trusted root CA
471 certificates self-signed using @code{RSA-MD2} or @code{RSA-MD5}. The
472 certificates in the trusted list are considered trusted irrespective
473 of the signature.
475 If you are using @funcref{gnutls_certificate_verify_peers2} to verify the
476 certificate chain, you can call
477 @funcref{gnutls_certificate_set_verify_flags} with the flags:
478 @itemize
479 @item @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2}
480 @item @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5}
481 @end itemize
482 as in the following example:
484 @example
485   gnutls_certificate_set_verify_flags (x509cred,
486                                        GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
487 @end example
489 This will tell the verifier algorithm to enable @code{RSA-MD5} when
490 verifying the certificates.
492 If you are using @funcref{gnutls_x509_crt_verify} or
493 @funcref{gnutls_x509_crt_list_verify}, you can pass the
494 @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5} parameter directly in the
495 @code{flags} parameter.
497 If you are using these flags, it may also be a good idea to warn the
498 user when verification failure occur for this reason.  The simplest is
499 to not use the flags by default, and only fall back to using them
500 after warning the user.  If you wish to inspect the certificate chain
501 yourself, you can use @funcref{gnutls_certificate_get_peers} to extract
502 the raw server's certificate chain, @funcref{gnutls_x509_crt_list_import} to parse each of the certificates, and
503 then @funcref{gnutls_x509_crt_get_signature_algorithm} to find out the
504 signing algorithm used for each certificate.  If any of the
505 intermediary certificates are using @code{GNUTLS_SIGN_RSA_MD2} or
506 @code{GNUTLS_SIGN_RSA_MD5}, you could present a warning.