guile: Fix `priorities' test to use `run-test'.
[gnutls.git] / doc / cha-cert-auth.texi
blobc1c0ddf2d078f5146339f987506752d6e5d06477
1 @node More on certificate authentication
2 @chapter More on certificate authentication
3 @cindex certificate authentication
5 @menu
6 * X.509 certificates::
7 * OpenPGP certificates::
8 * Hardware tokens::
9 * Abstract key types::
10 * Digital signatures::
11 @end menu
13 @node X.509 certificates
14 @section @acronym{X.509} certificates
15 @cindex @acronym{X.509} certificates
17 The @acronym{X.509} protocols rely on a hierarchical trust model. In
18 this trust model Certification Authorities (CAs) are used to certify
19 entities.  Usually more than one certification authorities exist, and
20 certification authorities may certify other authorities to issue
21 certificates as well, following a hierarchical model.
23 @float Figure,fig:x509
24 @image{gnutls-x509,7cm}
25 @caption{The X.509 hierarchical trust model.}
26 @end float
28 One needs to trust one or more CAs for his secure communications. In
29 that case only the certificates issued by the trusted authorities are
30 acceptable.  The framework is illustrated on @ref{fig:x509}.
32 @menu
33 * X.509 certificate structure::
34 * Verifying X.509 certificate paths::
35 * Verifying a certificate in the context of TLS session::
36 * Certificate requests::
37 * PKCS 12 structures::
38 @end menu
40 @node X.509 certificate structure
41 @subsection @acronym{X.509} certificate structure
43 An @acronym{X.509} certificate usually contains information about the
44 certificate holder, the signer, a unique serial number, expiration
45 dates and some other fields @xcite{PKIX} as shown in @ref{tab:x509}.
47 @float Table,tab:x509
48 @multitable @columnfractions .3 .7
50 @headitem Field @tab Description
52 @item version @tab
53 The field that indicates the version of the certificate.
55 @item serialNumber @tab
56 This field holds a unique serial number per certificate.
58 @item issuer @tab
59 Holds the issuer's distinguished name.
61 @item validity @tab
62 The activation and expiration dates.
64 @item subject @tab
65 The subject's distinguished name of the certificate.
67 @item extensions @tab
68 The extensions are fields only present in version 3 certificates.
70 @end multitable
71 @caption{X.509 certificate fields.}
72 @end float
74 The certificate's @emph{subject or issuer name} is not just a single
75 string.  It is a Distinguished name and in the @acronym{ASN.1}
76 notation is a sequence of several object identifiers with their corresponding
77 values. Some of available OIDs to be used in an @acronym{X.509}
78 distinguished name are defined in @file{gnutls/x509.h}.
80 The @emph{Version} field in a certificate has values either 1 or 3 for
81 version 3 certificates.  Version 1 certificates do not support the
82 extensions field so it is not possible to distinguish a CA from a
83 person, thus their usage should be avoided.
85 The @emph{validity} dates are there to indicate the date that the
86 specific certificate was activated and the date the certificate's key
87 would be considered invalid.
89 Certificate @emph{extensions} are there to include information about
90 the certificate's subject that did not fit in the typical certificate
91 fields. Those may be e-mail addresses, flags that indicate whether the
92 belongs to a CA etc.  All the supported @acronym{X.509} version 3
93 extensions are shown in @ref{tab:x509-ext}.
95 @float Table,tab:x509-ext
96 @multitable @columnfractions .3 .2 .5
98 @headitem Extension @tab OID @tab Description
100 @item Subject key id @tab 2.5.29.14 @tab
101 An identifier of the key of the subject.
103 @item Authority key id @tab 2.5.29.35 @tab
104 An identifier of the authority's key used to sign the certificate.
106 @item Subject alternative name @tab 2.5.29.17 @tab
107 Alternative names to subject's distinguished name.
109 @item Key usage @tab 2.5.29.15 @tab
110 Constraints the key's usage of the certificate.
112 @item Extended key usage @tab 2.5.29.37 @tab
113 Constraints the purpose of the certificate.
115 @item Basic constraints @tab 2.5.29.19 @tab
116 Indicates whether this is a CA certificate or not, and specify the
117 maximum path lengths of certificate chains.
119 @item CRL distribution points @tab 2.5.29.31 @tab
120 This extension is set by the CA, in order to inform about the issued
121 CRLs.
123 @item Proxy Certification Information @tab 1.3.6.1.5.5.7.1.14 @tab
124 Proxy Certificates includes this extension that contains the OID of
125 the proxy policy language used, and can specify limits on the maximum
126 lengths of proxy chains.  Proxy Certificates are specified in
127 @xcite{RFC3820}.
129 @end multitable
130 @caption{X.509 certificate extensions.}
131 @end float
133 In @acronym{GnuTLS} the @acronym{X.509} certificate structures are
134 handled using the @code{gnutls_x509_crt_t} type and the corresponding
135 private keys with the @code{gnutls_x509_privkey_t} type.  All the
136 available functions for @acronym{X.509} certificate handling have
137 their prototypes in @file{gnutls/x509.h}. An example program to
138 demonstrate the @acronym{X.509} parsing capabilities can be found at
139 @ref{ex:x509-info}.
141 @node Verifying X.509 certificate paths
142 @subsection Verifying @acronym{X.509} certificate paths
143 @cindex verifying certificate paths
145 Verifying certificate paths is important in @acronym{X.509}
146 authentication. For this purpose the following functions are
147 provided.
149 @showfuncB{gnutls_x509_trust_list_init,gnutls_x509_trust_list_deinit}
151 @showfuncdesc{gnutls_x509_trust_list_add_cas}
152 @showfuncdesc{gnutls_x509_trust_list_add_named_crt}
153 @showfuncdesc{gnutls_x509_trust_list_add_crls}
154 @showfuncdesc{gnutls_x509_trust_list_verify_crt}
155 @showfuncdesc{gnutls_x509_trust_list_verify_named_crt}
157 The verification function will verify a given certificate chain against a list of certificate
158 authorities and certificate revocation lists, and output
159 a bit-wise OR of elements of the @code{gnutls_@-certificate_@-status_t} 
160 enumeration. 
161 A detailed description of these elements can be found 
162 in @ref{tab:cert-verify}. An example of certificate verification is shown in @ref{ex:verify2}.
163 It is also possible to have a set of certificates that
164 are trusted for a particular server but not to authorize other certificates.
165 This purpose is served by the functions @funcref{gnutls_x509_trust_list_add_named_crt} and @funcref{gnutls_x509_trust_list_verify_named_crt}.
167 @node Verifying a certificate in the context of TLS session
168 @subsection Verifying a certificate in the context of TLS session
169 @cindex verifying certificate paths
170 @tindex gnutls_certificate_verify_flags
172 When operating in the context of a TLS session, the trusted certificate
173 authority list has been set via the
174 @funcref{gnutls_certificate_set_x509_trust_file} and @funcref{gnutls_certificate_set_x509_crl_file},
175 thus it is not required to setup a trusted list as above.
176 Convenience functions such as @funcref{gnutls_certificate_verify_peers2} 
177 are equivalent and will verify the peer's certificate chain
178 in a TLS session. 
180 @float Table,tab:cert-verify
181 @multitable @columnfractions .55 .45
183 @headitem Flag @tab Description
185 @item GNUTLS_CERT_INVALID @tab
186 The certificate is not signed by one of the known authorities, or
187 the signature is invalid.
189 @item GNUTLS_CERT_REVOKED @tab
190 The certificate has been revoked by its CA.
192 @item GNUTLS_CERT_SIGNER_NOT_FOUND @tab
193 The certificate's issuer is not known. This is the case when the
194 issuer is not in the trusted certificates list.
196 @item GNUTLS_CERT_SIGNER_NOT_CA @tab
197 The certificate's signer was not a CA. This may happen if
198 this was a version 1 certificate, which is common with some CAs, or
199 a version 3 certificate without the basic constrains extension.
201 @item GNUTLS_CERT_INSECURE_ALGORITHM @tab
202 The certificate was signed using an insecure algorithm such as MD2 or
203 MD5.  These algorithms have been broken and should not be trusted.
205 @end multitable
206 @caption{Certificate verification output flags.}
207 @end float
209 There is also the possibility to pass some input to the verification
210 functions in the form of flags. For @funcref{gnutls_x509_trust_list_verify_crt} the
211 flags are passed straightforward, but
212 @funcref{gnutls_certificate_verify_peers2} depends on the flags set by
213 calling @funcref{gnutls_certificate_set_verify_flags}.  All the available
214 flags are part of the enumeration
215 @code{gnutls_certificate_verify_flags} and are explained in @ref{tab:cert-flags}.
217 @float Table,tab:cert-flags
218 @multitable @columnfractions .5 .5
220 @headitem Flag @tab Description
221 @item GNUTLS_VERIFY_@-DISABLE_CA_SIGN @tab
222 If set a signer does not have to be a certificate authority. This
223 flag should normally be disabled, unless you know what this means.
225 @item GNUTLS_VERIFY_@-ALLOW_X509_V1_CA_CRT @tab
226 Allow only trusted CA certificates that have version 1.  This is
227 safer than GNUTLS_VERIFY_@-ALLOW_ANY_X509_V1_CA_CRT, and should be
228 used instead. That way only signers in your trusted list will be
229 allowed to have certificates of version 1. This is the default.
231 @item GNUTLS_VERIFY_@-DO_NOT_ALLOW_X509_V1_CA_CRT @tab
232 Do not allow trusted version 1 CA certificates.  This option is to be used
233 in order consider all V1 certificates as deprecated.
235 @item GNUTLS_VERIFY_@-ALLOW_ANY_X509_V1_CA_CRT @tab
236 Allow CA certificates that have version 1 (both root and
237 intermediate). This is dangerous since those haven't the
238 basicConstraints extension. Must be used in combination with
239 GNUTLS_VERIFY_@-ALLOW_X509_V1_CA_CRT.
241 @item GNUTLS_VERIFY_@-DO_NOT_ALLOW_SAME @tab
242 If a certificate is not signed by anyone trusted but exists in
243 the trusted CA list do not treat it as trusted.
245 @item GNUTLS_VERIFY_@-ALLOW_SIGN_RSA_MD2 @tab
246 Allow certificates to be signed using the old MD2 algorithm.
248 @item GNUTLS_VERIFY_@-ALLOW_SIGN_RSA_MD5 @tab
249 Allow certificates to be signed using the broken MD5 algorithm.
251 @item GNUTLS_VERIFY_@-DISABLE_TIME_CHECKS @tab
252 Disable checking of activation
253 and expiration validity periods of certificate chains. Don't set
254 this unless you understand the security implications.
256 @item GNUTLS_VERIFY_@-DISABLE_CRL_CHECKS @tab
257 Disables checking for validity using certificate revocation lists.
259 @end multitable
260 @caption{Certificate verification flags.}
261 @end float
263 Although the verification of a certificate path indicates that the
264 certificate is signed by trusted authority, does not reveal anything
265 about the peer's identity. It is required to verify if the
266 certificate's owner is the one you expect. For more information
267 consult @xcite{RFC2818} and section @ref{ex:verify} for an example.
269 @node Certificate requests
270 @subsection @acronym{PKCS} #10 certificate requests
271 @cindex certificate requests
272 @cindex @acronym{PKCS} #10
274 A certificate request is a structure, which contain information about
275 an applicant of a certificate service.  It usually contains a private
276 key, a distinguished name and secondary data such as a challenge
277 password. @acronym{GnuTLS} supports the requests defined in
278 @acronym{PKCS} #10 @xcite{RFC2986}. Other formats of certificate requests
279 are not currently supported.
281 @showfuncB{gnutls_x509_crq_init,gnutls_x509_crq_deinit}
283 @showfuncdesc{gnutls_x509_crq_import}
284 @showfuncdesc{gnutls_x509_crq_export}
286 A certificate request can be generated by
287 associating it with a private key, setting the
288 subject's information and finally self signing it.
289 The last step ensures that the requester is in
290 possession of the private key.
292 @showfuncdesc{gnutls_x509_crq_set_version}
293 @showfuncdesc{gnutls_x509_crq_set_dn_by_oid}
294 @showfuncdesc{gnutls_x509_crq_set_key_usage}
295 @showfuncdesc{gnutls_x509_crq_set_key_purpose_oid}
296 @showfuncdesc{gnutls_x509_crq_set_basic_constraints}
298 The following two functions associate the request with
299 a private key and sign it. If a request is to be signed
300 with a key residing in a token it is recommended to use
301 the signing functions shown in @ref{Abstract key types}.
303 @showfuncdesc{gnutls_x509_crq_set_key}
304 @showfuncdesc{gnutls_x509_crq_sign2}
306 The following example is about generating a certificate request, and a
307 private key. A certificate request can be later be processed by a CA
308 which should return a signed certificate.
310 @anchor{ex:crq}
311 @verbatiminclude examples/ex-crq.c
313 @node PKCS 12 structures
314 @subsection @acronym{PKCS} #12 structures
315 @cindex @acronym{PKCS} #12
317 A @acronym{PKCS} #12 structure @xcite{PKCS12} usually contains a user's
318 private keys and certificates. It is commonly used in browsers to
319 export and import the user's identities.
321 In @acronym{GnuTLS} the @acronym{PKCS} #12 structures are handled
322 using the @code{gnutls_pkcs12_t} type. This is an abstract type that
323 may hold several @code{gnutls_pkcs12_bag_t} types.  The bag types are
324 the holders of the actual data, which may be certificates, private
325 keys or encrypted data.  A bag of type encrypted should be decrypted
326 in order for its data to be accessed.
328 @showfuncB{gnutls_pkcs12_init,gnutls_pkcs12_deinit}
330 The following functions are available to read a @acronym{PKCS} #12
331 structure.
333 @showfuncdesc{gnutls_pkcs12_import}
334 @showfuncdesc{gnutls_pkcs12_get_bag}
335 @showfuncdesc{gnutls_pkcs12_verify_mac}
336 @showfuncdesc{gnutls_pkcs12_bag_decrypt}
338 @showfuncB{gnutls_pkcs12_bag_init,gnutls_pkcs12_bag_deinit}
340 @showfuncD{gnutls_pkcs12_bag_get_count,gnutls_pkcs12_bag_get_data,gnutls_pkcs12_bag_get_key_id,gnutls_pkcs12_bag_get_friendly_name}
342 To generate a structure the functions below may be used.
344 @showfuncdesc{gnutls_pkcs12_set_bag}
345 @showfuncdesc{gnutls_pkcs12_bag_encrypt}
346 @showfuncdesc{gnutls_pkcs12_generate_mac}
347 @showfuncdesc{gnutls_pkcs12_export}
348 @showfuncE{gnutls_pkcs12_bag_set_data,gnutls_pkcs12_bag_set_crl,gnutls_pkcs12_bag_set_crt,gnutls_pkcs12_bag_set_key_id,gnutls_pkcs12_bag_set_friendly_name}
350 An example of a @acronym{PKCS} #12 structure generation can be found
351 below.
353 @verbatiminclude examples/ex-pkcs12.c
355 @node OpenPGP certificates
356 @section @acronym{OpenPGP} certificates
357 @cindex @acronym{OpenPGP} certificates
359 The @acronym{OpenPGP} key authentication relies on a distributed trust
360 model, called the ``web of trust''. The ``web of trust'' uses a
361 decentralized system of trusted introducers, which are the same as a
362 CA. @acronym{OpenPGP} allows anyone to sign anyone else's public
363 key. When Alice signs Bob's key, she is introducing Bob's key to
364 anyone who trusts Alice. If someone trusts Alice to introduce keys,
365 then Alice is a trusted introducer in the mind of that observer.
366 For example in @ref{fig:openpgp}, David trusts Alice to be an introducer and Alice
367 signed Bob's key thus Dave trusts Bob's key to be the real one.
369 @float Figure,fig:openpgp
370 @image{gnutls-pgp,8cm}
371 @caption{The OpenPGP trust model.}
372 @end float
374 There are some key points that are important in that model. In the
375 example Alice has to sign Bob's key, only if she is sure that the key
376 belongs to Bob. Otherwise she may also make Dave falsely believe that
377 this is Bob's key. Dave has also the responsibility to know who to
378 trust.  This model is similar to real life relations.
380 Just see how Charlie behaves in the previous example. Although he has
381 signed Bob's key - because he knows, somehow, that it belongs to Bob -
382 he does not trust Bob to be an introducer. Charlie decided to trust
383 only Kevin, for some reason. A reason could be that Bob is lazy
384 enough, and signs other people's keys without being sure that they
385 belong to the actual owner.
387 @subsection @acronym{OpenPGP} certificate structure
389 In @acronym{GnuTLS} the @acronym{OpenPGP} key structures
390 @xcite{RFC2440} are handled using the @code{gnutls_openpgp_crt_t} type
391 and the corresponding private keys with the
392 @code{gnutls_openpgp_privkey_t} type. All the prototypes for the key
393 handling functions can be found at @file{gnutls/openpgp.h}.
395 @subsection Verifying an @acronym{OpenPGP} certificate
397 The verification functions of @acronym{OpenPGP} keys, included in
398 @acronym{GnuTLS}, are simple ones, and do not use the features of the
399 ``web of trust''.  For that reason, if the verification needs are
400 complex, the assistance of external tools like @acronym{GnuPG} and
401 GPGME@footnote{@url{http://www.gnupg.org/related_software/gpgme/}} is
402 recommended.
404 In GnuTLS there is a verification function for OpenPGP certificates,
405 the @funcref{gnutls_openpgp_crt_verify_ring}.  This checks an
406 @acronym{OpenPGP} key against a given set of public keys (keyring) and
407 returns the key status. The key verification status is the same as in
408 @acronym{X.509} certificates, although the meaning and interpretation
409 are different. For example an @acronym{OpenPGP} key may be valid, if
410 the self signature is ok, even if no signers were found.  The meaning
411 of verification status is shown in the figure below.
413 @showfuncdesc{gnutls_openpgp_crt_verify_ring}
415 @showfuncdesc{gnutls_openpgp_crt_verify_self}
417 @table @code
419 @item CERT_INVALID:
420 A signature on the key is invalid. That means that the key was
421 modified by somebody, or corrupted during transport.
423 @item CERT_REVOKED:
424 The key has been revoked by its owner.
426 @item CERT_SIGNER_NOT_FOUND:
427 The key was not signed by a known signer.
429 @item GNUTLS_CERT_INSECURE_ALGORITHM:
430 The certificate was signed using an insecure algorithm such as MD2 or
431 MD5.  These algorithms have been broken and should not be trusted.
433 @end table
435 @subsection Verifying a certificate in the context of a TLS session
437 Similarly with X.509 certificates, one needs to specify
438 the OpenPGP keyring file in the credentials structure. The certificates
439 in this file will be  used by @funcref{gnutls_certificate_verify_peers2}
440 to verify the signatures in the certificate sent by the peer.
442 @showfuncdesc{gnutls_certificate_set_openpgp_keyring_file}
445 @node Hardware tokens
446 @section Hardware tokens
447 @cindex PKCS #11 tokens
448 @cindex hardware tokens
449 @cindex smart cards
451 @subsection Introduction
452 This section copes with hardware token support in @acronym{GnuTLS} using 
453 @acronym{PKCS} #11 @xcite{PKCS11}.
454 @acronym{PKCS} #11 is plugin API allowing applications to access cryptographic
455 operations on a token, as well as to objects residing on the token. A token can 
456 be a real hardware token such as a smart card, or it can be a software component
457 such as @acronym{Gnome Keyring}. The objects residing on such token can be
458 certificates, public keys, private keys or even plain data or  secret keys. Of those
459 certificates and public/private key pairs can be used with @acronym{GnuTLS}. Its
460 main advantage is that it allows operations on private key objects such as decryption
461 and signing without exposing the key.
463 Moreover it can be used to allow all applications in the same operating system to access
464 shared cryptographic keys and certificates in a uniform way, as in @ref{fig:pkcs11-vision}.
466 @float Figure,fig:pkcs11-vision
467 @image{pkcs11-vision,9cm}
468 @caption{PKCS #11 module usage.}
469 @end float
471 @subsection Initialization
472 To allow all the  @acronym{GnuTLS} applications to access @acronym{PKCS} #11 tokens
473 you can use a configuration per module, stored in @code{/etc/pkcs11/modules/}. 
474 These are the configuration files of @acronym{p11-kit}@footnote{@url{http://p11-glue.freedesktop.org/}}.
475 For example a file that will load the @acronym{OpenSC} module, could be named
476 @code{/etc/pkcs11/modules/opensc} and contain the following:
478 @smallexample
479 module: /usr/lib/opensc-pkcs11.so
480 @end smallexample
482 If you use this file, then there is no need for other initialization in
483 @acronym{GnuTLS}, except for the PIN and token functions. Those allow retrieving a PIN
484 when accessing a protected object, such as a private key, as well as probe
485 the user to insert the token. All the initialization functions are below.
487 @showfuncdesc{gnutls_pkcs11_init}
488 @showfuncdesc{gnutls_pkcs11_set_token_function}
489 @showfuncdesc{gnutls_pkcs11_set_pin_function}
490 @showfuncdesc{gnutls_pkcs11_add_provider}
491 @showfuncA{gnutls_pkcs11_deinit}
493 Note that due to limitations of @acronym{PKCS} #11 there are issues when multiple libraries 
494 are sharing a module. To avoid this problem GnuTLS uses @acronym{p11-kit}
495 that provides a middleware to control access to resources over the
496 multiple users.
498 @subsection Reading objects
500 All @acronym{PKCS} #11 objects are referenced by @acronym{GnuTLS} functions by
501 URLs as described in @code{draft-pechanec-pkcs11uri-05}. 
502 This allows for a consistent naming of objects across systems and applications
503 in the same system. For example a public
504 key on a smart card may be referenced as:
506 @smallexample
507 pkcs11:token=Nikos;serial=307521161601031;model=PKCS%2315; \
508 manufacturer=EnterSafe;object=test1;objecttype=public;\
509 id=32f153f3e37990b08624141077ca5dec2d15faed
510 @end smallexample
512 while the smart card itself can be referenced as:
513 @smallexample
514 pkcs11:token=Nikos;serial=307521161601031;model=PKCS%2315;manufacturer=EnterSafe
515 @end smallexample
517 Objects stored in a @acronym{PKCS} #11 token can be extracted
518 if they are not marked as sensitive. Usually only private keys are marked as
519 sensitive and cannot be extracted, while certificates and other data can
520 be retrieved. The functions that can be used to access objects
521 are shown below.
523 @showfuncB{gnutls_pkcs11_obj_init,gnutls_pkcs11_obj_deinit}
525 @showfuncdesc{gnutls_pkcs11_obj_import_url}
527 @showfuncdesc{gnutls_pkcs11_obj_export_url}
529 @showfuncdesc{gnutls_pkcs11_obj_export}
531 @showfuncdesc{gnutls_pkcs11_obj_get_info}
533 @showfuncdesc{gnutls_pkcs11_obj_list_import_url}
535 @showfuncC{gnutls_x509_crt_import_pkcs11,gnutls_x509_crt_import_pkcs11_url,gnutls_x509_crt_list_import_pkcs11}
537 Properties of the physical token can also be accessed and altered with @acronym{GnuTLS}.
538 For example data in a token can be erased (initialized), PIN can be altered, etc.
540 @showfuncdesc{gnutls_pkcs11_token_init}
541 @showfuncdesc{gnutls_pkcs11_token_set_pin}
542 @showfuncdesc{gnutls_pkcs11_token_get_url}
543 @showfuncdesc{gnutls_pkcs11_token_get_info}
544 @showfuncdesc{gnutls_pkcs11_token_get_flags}
546 The following example will list all available PKCS #11 tokens in a system.
547 @example
548 int i;
549 char* url;
551 gnutls_global_init();
553 for (i=0;;i++) 
554   @{
555     ret = gnutls_pkcs11_token_get_url(i, &url);
556     if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
557       break;
559     if (ret < 0)
560       exit(1);
561                 
562     fprintf(stdout, "Token[%d]: URL: %s\n", i, url);
563     gnutls_free(url);
564   @}
565 gnutls_global_deinit();
566 @end example
569 That example will only list all certificates in a token that have a corresponding
570 private key.
571 @verbatiminclude examples/ex-pkcs11-list.c
573 @subsection Writing objects
575 With @acronym{GnuTLS} you can copy existing private keys and certificates
576 to a token. Note that when copying private keys it is recommended to mark
577 them as sensitive using the @code{GNUTLS_@-PKCS11_OBJ_@-FLAG_@-MARK_@-SENSITIVE}
578 to prevent its extraction. An object can be marked as private using the flag 
579 @code{GNUTLS_@-PKCS11_OBJ_@-FLAG_@-MARK_@-PRIVATE}, to require PIN to be
580 entered before accessing the object (for operations or otherwise).
582 @showfuncdesc{gnutls_pkcs11_delete_url}
583 @showfuncdesc{gnutls_pkcs11_copy_x509_privkey}
584 @showfuncdesc{gnutls_pkcs11_copy_x509_crt}
587 @subsection Using a @acronym{PKCS} #11 token with TLS
589 It is possible to use a @acronym{PKCS} #11 token to a TLS
590 session, as shown in @ref{ex:pkcs11-client}. In addition
591 the following functions can be used to load PKCS #11 key and
592 certificates by specifying a PKCS #11 URL instead of a filename.
594 @showfuncB{gnutls_certificate_set_x509_trust_file,gnutls_certificate_set_x509_key_file}
597 @node Abstract key types
598 @section Abstract key types
599 @cindex abstract types
601 Since there are many forms of a public or private keys supported by @acronym{GnuTLS} such as
602 @acronym{X.509}, @acronym{OpenPGP}, or @acronym{PKCS} #11 it is desirable to allow common operations
603 on them. For these reasons the abstract @code{gnutls_privkey_t} and @code{gnutls_pubkey_t} were
604 introduced in @code{gnutls/abstract.h} header. Those types are initialized using a specific type of 
605 key and then can be used to perform operations in an abstract way. For example in order for someone 
606 to sign an X.509 certificate with a key that resides in a smart he has to follow the steps below:
608 @example
609 #inlude <gnutls/abstract.h>
610 #inlude <gnutls/pkcs11.h>
612 void sign_cert( gnutls_x509_crt_t to_be_signed)
614 gnutls_pkcs11_privkey_t ca_key;
615 gnutls_x509_crt_t ca_cert;
616 gnutls_privkey_t abs_key;
618   /* load the PKCS #11 key and certificates */
619   gnutls_pkcs11_privkey_init(&ca_key);
620   gnutls_pkcs11_privkey_import_url(ca_key, key_url);
622   gnutls_x509_crt_init(&ca_cert);
623   gnutls_x509_crt_import_pkcs11_url(&ca_cert, cert_url);
625   /* initialize the abstract key */
626   gnutls_privkey_init(&abs_key);
627   gnutls_privkey_import_pkcs11(abs_key, ca_key);
629   /* sign the certificate to be signed */
630   gnutls_x509_crt_privkey_sign(to_be_signed, ca_cert, ca_key, 
631                                GNUTLS_DIG_SHA1, 0);
633 @end example
635 @subsection Public keys
636 An abstract @code{gnutls_pubkey_t} can be initialized
637 using the functions below. It can be imported through
638 an existing structure like @code{gnutls_x509_crt_t},
639 or through an ASN.1 encoding of the X.509 @code{SubjectPublicKeyInfo}
640 sequence.
642 @showfuncB{gnutls_pubkey_init,gnutls_pubkey_deinit}
644 @showfuncdesc{gnutls_pubkey_import_x509}
645 @showfuncdesc{gnutls_pubkey_import_openpgp}
646 @showfuncdesc{gnutls_pubkey_import_pkcs11}
647 @showfuncdesc{gnutls_pubkey_import_pkcs11_url}
648 @showfuncdesc{gnutls_pubkey_import_privkey}
649 @showfuncdesc{gnutls_pubkey_import}
650 @showfuncdesc{gnutls_pubkey_export}
652 Additional functions are available that will return
653 information over a public key.
655 @showfuncdesc{gnutls_pubkey_get_pk_algorithm}
656 @showfuncdesc{gnutls_pubkey_get_preferred_hash_algorithm}
657 @showfuncdesc{gnutls_pubkey_get_key_id}
659 @subsection Private keys
660 An abstract @code{gnutls_privkey_t} can be initialized
661 using the functions below. It can be imported through
662 an existing structure like @code{gnutls_x509_privkey_t},
663 but unlike public keys it cannot be exported. That is
664 to allow abstraction over @acronym{PKCS} #11 keys that
665 are not extractable.
667 @showfuncB{gnutls_privkey_init,gnutls_privkey_deinit}
669 @showfuncdesc{gnutls_privkey_import_x509}
670 @showfuncdesc{gnutls_privkey_import_openpgp}
671 @showfuncdesc{gnutls_privkey_import_pkcs11}
673 Other information on the private key can be accessed using
674 the following functions.
676 @showfuncdesc{gnutls_privkey_get_pk_algorithm}
677 @showfuncdesc{gnutls_privkey_get_type}
679 @subsection Operations
680 The abstract key types can be used to access signing and
681 signature verification operations with the underlying keys.
683 @showfuncdesc{gnutls_pubkey_verify_data2}
684 @showfuncdesc{gnutls_pubkey_verify_hash}
685 @showfuncdesc{gnutls_privkey_sign_data}
686 @showfuncdesc{gnutls_privkey_sign_hash}
688 Signing existing structures, such as certificates, CRLs,
689 or certificate requests, as well as associating public
690 keys with structures is also possible using the 
691 key abstractions.
693 @showfuncdesc{gnutls_x509_crq_set_pubkey}
694 @showfuncdesc{gnutls_x509_crt_set_pubkey}
695 @showfuncdesc{gnutls_x509_crt_privkey_sign}
696 @showfuncdesc{gnutls_x509_crl_privkey_sign}
697 @showfuncdesc{gnutls_x509_crq_privkey_sign}
699 @node Digital signatures
700 @section Digital signatures
701 @cindex digital signatures
703 In this section we will provide some information about digital
704 signatures, how they work, and give the rationale for disabling some
705 of the algorithms used.
707 Digital signatures work by using somebody's secret key to sign some
708 arbitrary data.  Then anybody else could use the public key of that
709 person to verify the signature.  Since the data may be arbitrary it is
710 not suitable input to a cryptographic digital signature algorithm. For
711 this reason and also for performance cryptographic hash algorithms are
712 used to preprocess the input to the signature algorithm. This works as
713 long as it is difficult enough to generate two different messages with
714 the same hash algorithm output. In that case the same signature could
715 be used as a proof for both messages. Nobody wants to sign an innocent
716 message of donating 1 @euro{} to Greenpeace and find out that he
717 donated 1.000.000 @euro{} to Bad Inc.
719 For a hash algorithm to be called cryptographic the following three
720 requirements must hold:
722 @enumerate
723 @item Preimage resistance.
724 That means the algorithm must be one way and given the output of the
725 hash function @math{H(x)}, it is impossible to calculate @math{x}.
727 @item 2nd preimage resistance.
728 That means that given a pair @math{x,y} with @math{y=H(x)} it is
729 impossible to calculate an @math{x'} such that @math{y=H(x')}.
731 @item Collision resistance.
732 That means that it is impossible to calculate random @math{x} and
733 @math{x'} such @math{H(x')=H(x)}.
734 @end enumerate
736 The last two requirements in the list are the most important in
737 digital signatures. These protect against somebody who would like to
738 generate two messages with the same hash output. When an algorithm is
739 considered broken usually it means that the Collision resistance of
740 the algorithm is less than brute force. Using the birthday paradox the
741 brute force attack takes
742 @iftex
743 @math{2^{(\rm{hash\ size}) / 2}}
744 @end iftex
745 @ifnottex
746 @math{2^{((hash size) / 2)}}
747 @end ifnottex
748 operations. Today colliding certificates using the MD5 hash algorithm
749 have been generated as shown in @xcite{WEGER}.
751 There has been cryptographic results for the SHA-1 hash algorithms as
752 well, although they are not yet critical.  Before 2004, MD5 had a
753 presumed collision strength of @math{2^{64}}, but it has been showed
754 to have a collision strength well under @math{2^{50}}.  As of November
755 2005, it is believed that SHA-1's collision strength is around
756 @math{2^{63}}.  We consider this sufficiently hard so that we still
757 support SHA-1.  We anticipate that SHA-256/386/512 will be used in
758 publicly-distributed certificates in the future.  When @math{2^{63}}
759 can be considered too weak compared to the computer power available
760 sometime in the future, SHA-1 will be disabled as well.  The collision
761 attacks on SHA-1 may also get better, given the new interest in tools
762 for creating them.
764 @subsection Trading security for interoperability
766 If you connect to a server and use GnuTLS' functions to verify the
767 certificate chain, and get a @code{GNUTLS_CERT_INSECURE_ALGORITHM}
768 validation error (see @ref{Verifying X.509 certificate paths}), it means
769 that somewhere in the certificate chain there is a certificate signed
770 using @code{RSA-MD2} or @code{RSA-MD5}.  These two digital signature
771 algorithms are considered broken, so GnuTLS fail when attempting to
772 verify the certificate.  In some situations, it may be useful to be
773 able to verify the certificate chain anyway, assuming an attacker did
774 not utilize the fact that these signatures algorithms are broken.
775 This section will give help on how to achieve that.
777 First, it is important to know that you do not have to enable any of
778 the flags discussed here to be able to use trusted root CA
779 certificates signed using @code{RSA-MD2} or @code{RSA-MD5}.  The only
780 attack today is that it is possible to generate certificates with
781 colliding signatures (collision resistance); you cannot generate a
782 certificate that has the same signature as an already existing
783 signature (2nd preimage resistance).
785 If you are using @funcref{gnutls_certificate_verify_peers2} to verify the
786 certificate chain, you can call
787 @funcref{gnutls_certificate_set_verify_flags} with the flags:
788 @itemize
789 @item @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2}
790 @item @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5}
791 @end itemize
792 as in the following example:
794 @smallexample
795   gnutls_certificate_set_verify_flags (x509cred,
796                                        GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5);
797 @end smallexample
799 This will tell the verifier algorithm to enable @code{RSA-MD5} when
800 verifying the certificates.
802 If you are using @funcref{gnutls_x509_crt_verify} or
803 @funcref{gnutls_x509_crt_list_verify}, you can pass the
804 @code{GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5} parameter directly in the
805 @code{flags} parameter.
807 If you are using these flags, it may also be a good idea to warn the
808 user when verification failure occur for this reason.  The simplest is
809 to not use the flags by default, and only fall back to using them
810 after warning the user.  If you wish to inspect the certificate chain
811 yourself, you can use @funcref{gnutls_certificate_get_peers} to extract
812 the raw server's certificate chain, then use
813 @funcref{gnutls_x509_crt_import} to parse each of the certificates, and
814 then use @funcref{gnutls_x509_crt_get_signature_algorithm} to find out the
815 signing algorithm used for each certificate.  If any of the
816 intermediary certificates are using @code{GNUTLS_SIGN_RSA_MD2} or
817 @code{GNUTLS_SIGN_RSA_MD5}, you could present a warning.