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