Separated DANE functionality from certtool and added danetool.
[gnutls.git] / doc / cha-cert-auth2.texi
blobfe10087c2adb35fba03ed5e88fc412068183cadb
1 @node More on certificate authentication
2 @chapter More on certificate authentication
3 @cindex certificate authentication
5 Certificates are not the only structures involved in a public key
6 infrastructure. Several other structures that are used for certificate
7 requests, encrypted private keys, revocation lists, GnuTLS abstract key
8 structures, etc., are discussed in this chapter.
10 @menu
11 * PKCS 10 certificate requests::
12 * PKIX certificate revocation lists::
13 * OCSP certificate status checking::
14 * Managing encrypted keys::
15 * certtool Invocation::            Invoking certtool
16 * ocsptool Invocation::            Invoking ocsptool
17 * danetool Invocation::            Invoking danetool
18 @end menu
20 @node PKCS 10 certificate requests
21 @section @acronym{PKCS} #10 certificate requests
22 @cindex certificate requests
23 @cindex PKCS #10
25 A certificate request is a structure, which contain information about
26 an applicant of a certificate service.  It usually contains a private
27 key, a distinguished name and secondary data such as a challenge
28 password. @acronym{GnuTLS} supports the requests defined in
29 @acronym{PKCS} #10 @xcite{RFC2986}. Other formats of certificate requests
30 are not currently supported.
32 A certificate request can be generated by
33 associating it with a private key, setting the
34 subject's information and finally self signing it.
35 The last step ensures that the requester is in
36 possession of the private key.
38 @showfuncE{gnutls_x509_crq_set_version,gnutls_x509_crq_set_dn_by_oid,gnutls_x509_crq_set_key_usage,gnutls_x509_crq_set_key_purpose_oid,gnutls_x509_crq_set_basic_constraints}
40 The @funcref{gnutls_x509_crq_set_key} and @funcref{gnutls_x509_crq_sign2} 
41 functions associate the request with a private key and sign it. If a 
42 request is to be signed with a key residing in a PKCS #11 token it is recommended to use
43 the signing functions shown in @ref{Abstract key types}.
45 @showfuncdesc{gnutls_x509_crq_set_key}
46 @showfuncdesc{gnutls_x509_crq_sign2}
48 The following example is about generating a certificate request, and a
49 private key. A certificate request can be later be processed by a CA
50 which should return a signed certificate.
52 @anchor{ex:crq}
53 @verbatiminclude examples/ex-crq.c
55 @node PKIX certificate revocation lists
56 @section PKIX certificate revocation lists
57 @cindex certificate revocation lists
58 @cindex CRL
60 A certificate revocation list (CRL) is a structure issued by an authority
61 periodically containing a list of revoked certificates serial numbers.
62 The CRL structure is signed with the issuing authorities' keys. A typical
63 CRL contains the fields as shown in @ref{tab:crl}.
64 Certificate revocation lists are used to complement the expiration date of a certificate,
65 in order to account for other reasons of revocation, such as compromised keys, etc.
67 Each CRL is valid for limited amount of
68 time and is required to provide, except for the current issuing time, also 
69 the issuing time of the next update.
71 @float Table,tab:crl
72 @multitable @columnfractions .2 .7
74 @headitem Field @tab Description
76 @item version @tab
77 The field that indicates the version of the CRL structure.
79 @item signature @tab
80 A signature by the issuing authority.
82 @item issuer @tab
83 Holds the issuer's distinguished name.
85 @item thisUpdate @tab
86 The issuing time of the revocation list.
88 @item nextUpdate @tab
89 The issuing time of the revocation list that will update that one.
91 @item revokedCertificates @tab
92 List of revoked certificates serial numbers.
94 @item extensions @tab
95 Optional CRL structure extensions.
97 @end multitable
98 @caption{Certificate revocation list fields.}
99 @end float
101 The basic CRL structure functions follow.
103 @showfuncD{gnutls_x509_crl_init,gnutls_x509_crl_import,gnutls_x509_crl_export,gnutls_x509_crl_export}
105 @subheading Reading a CRL
107 The most important function that extracts the certificate revocation
108 information from a CRL is @funcref{gnutls_x509_crl_get_crt_serial}. Other
109 functions that return other fields of the CRL structure are also provided.
111 @showfuncdesc{gnutls_x509_crl_get_crt_serial}
113 @showfuncE{gnutls_x509_crl_get_version,gnutls_x509_crl_get_issuer_dn,gnutls_x509_crl_get_this_update,gnutls_x509_crl_get_next_update,gnutls_x509_crl_get_crt_count}
116 @subheading Generation of a CRL
118 The following functions can be used to generate a CRL.
120 @showfuncB{gnutls_x509_crl_set_version,gnutls_x509_crl_set_crt_serial}
121 @showfuncC{gnutls_x509_crl_set_crt,gnutls_x509_crl_set_next_update,gnutls_x509_crl_set_this_update}
123 The @funcref{gnutls_x509_crl_sign2} and @funcref{gnutls_x509_crl_privkey_sign} 
124 functions sign the revocation list with a private key. The latter function
125 can be used to sign with a key residing in a PKCS #11 token.
127 @showfuncdesc{gnutls_x509_crl_sign2}
128 @showfuncdesc{gnutls_x509_crl_privkey_sign}
130 Few extensions on the CRL structure are supported, including the
131 CRL number extension and the authority key identifier.
133 @showfuncB{gnutls_x509_crl_set_number,gnutls_x509_crl_set_authority_key_id}
135 @node OCSP certificate status checking
136 @section @acronym{OCSP} certificate status checking
137 @cindex certificate status
138 @cindex Online Certificate Status Protocol
139 @cindex OCSP
141 Certificates may be revoked before their expiration time has been
142 reached.  There are several reasons for revoking certificates, but a
143 typical situation is when the private key associated with a
144 certificate has been compromised.  Traditionally, Certificate
145 Revocation Lists (CRLs) have been used by application to implement
146 revocation checking, however, several problems with CRLs have been
147 identified @xcite{RIVESTCRL}.
149 The Online Certificate Status Protocol, or @acronym{OCSP} @xcite{RFC2560}, 
150 is a widely implemented protocol to perform certificate revocation status
151 checking.  An application that wish to verify the
152 identity of a peer will verify the certificate against a set of
153 trusted certificates and then check whether the certificate is listed
154 in a CRL and/or perform an OCSP check for the certificate.
156 Before performing the OCSP query, the application will need to figure
157 out the address of the OCSP server.  The OCSP server address can be
158 provided by the local user in manual configuration or may be stored
159 in the certificate that is being checked.  When stored in a certificate
160 the OCSP server is in the extension field called the Authority Information 
161 Access (AIA). The following function
162 extracts this information from a certificate.
164 @showfuncA{gnutls_x509_crt_get_authority_info_access}
166 There are several functions in GnuTLS for creating and manipulating
167 OCSP requests and responses.  The general idea is that a client
168 application create an OCSP request object, store some information
169 about the certificate to check in the request, and then export the
170 request in DER format.  The request will then need to be sent to the
171 OCSP responder, which needs to be done by the application (GnuTLS does
172 not send and receive OCSP packets).  Normally an OCSP response is
173 received that the application will need to import into an OCSP
174 response object.  The digital signature in the OCSP response needs to
175 be verified against a set of trust anchors before the information in
176 the response can be trusted.
178 The ASN.1 structure of OCSP requests are briefly as follows.  It is
179 useful to review the structures to get an understanding of which
180 fields are modified by GnuTLS functions.
182 @example
183 OCSPRequest     ::=     SEQUENCE @{
184     tbsRequest                  TBSRequest,
185     optionalSignature   [0]     EXPLICIT Signature OPTIONAL @}
187 TBSRequest      ::=     SEQUENCE @{
188     version             [0]     EXPLICIT Version DEFAULT v1,
189     requestorName       [1]     EXPLICIT GeneralName OPTIONAL,
190     requestList                 SEQUENCE OF Request,
191     requestExtensions   [2]     EXPLICIT Extensions OPTIONAL @}
193 Request         ::=     SEQUENCE @{
194     reqCert                     CertID,
195     singleRequestExtensions     [0] EXPLICIT Extensions OPTIONAL @}
197 CertID          ::=     SEQUENCE @{
198     hashAlgorithm       AlgorithmIdentifier,
199     issuerNameHash      OCTET STRING, -- Hash of Issuer's DN
200     issuerKeyHash       OCTET STRING, -- Hash of Issuers public key
201     serialNumber        CertificateSerialNumber @}
202 @end example
204 The basic functions to initialize, import, export and deallocate OCSP
205 requests are the following.
207 @showfuncE{gnutls_ocsp_req_init,gnutls_ocsp_req_deinit,gnutls_ocsp_req_import,gnutls_ocsp_req_export,gnutls_ocsp_req_print}
209 To generate an OCSP request the issuer name hash, issuer key hash, and 
210 the checked certificate's serial number are required. There are two
211 interfaces available for setting those in an OCSP request.
212 The is a low-level function when you have the
213 issuer name hash, issuer key hash, and certificate serial number in
214 binary form.  The second is more useful if you have the
215 certificate (and its issuer) in a @code{gnutls_x509_crt_t} type.
216 There is also a function to extract this information from existing an OCSP
217 request.
219 @showfuncC{gnutls_ocsp_req_add_cert_id,gnutls_ocsp_req_add_cert,gnutls_ocsp_req_get_cert_id}
221 Each OCSP request may contain a number of extensions.  Extensions are
222 identified by an Object Identifier (OID) and an opaque data buffer
223 whose syntax and semantics is implied by the OID. You can extract or
224 set those extensions using the following functions.
226 @showfuncB{gnutls_ocsp_req_get_extension,gnutls_ocsp_req_set_extension}
228 A common OCSP Request extension is the nonce extension (OID
229 1.3.6.1.5.5.7.48.1.2), which is used to avoid replay attacks of
230 earlier recorded OCSP responses.  The nonce extension carries a value
231 that is intended to be sufficiently random and unique so that an
232 attacker will not be able to give a stale response for the same nonce.
234 @showfuncC{gnutls_ocsp_req_get_nonce,gnutls_ocsp_req_set_nonce,gnutls_ocsp_req_randomize_nonce}
236 The OCSP response structures is a complex structure. A simplified overview
237 of it is in @ref{tab:ocsp-response}. Note that a response may contain 
238 information on multiple certificates.
240 @float Table,tab:ocsp-response
241 @multitable @columnfractions .2 .7
243 @headitem Field @tab Description
245 @item version @tab
246 The OCSP response version number (typically 1).
248 @item responder ID @tab
249 An identifier of the responder (DN name or a hash of its key).
251 @item issue time @tab
252 The time the response was generated.
254 @item thisUpdate @tab
255 The issuing time of the revocation information.
257 @item nextUpdate @tab
258 The issuing time of the revocation information that will update that one.
260 @item @tab Revoked certificates
262 @item certificate status @tab
263 The status of the certificate.
265 @item certificate serial @tab
266 The certificate's serial number.
268 @item revocationTime @tab
269 The time the certificate was revoked.
271 @item revocationReason @tab
272 The reason the certificate was revoked.
274 @end multitable
275 @caption{The most important OCSP response fields.}
276 @end float
279 We provide basic functions for initialization, importing, exporting
280 and deallocating OCSP responses.  
282 @showfuncE{gnutls_ocsp_resp_init,gnutls_ocsp_resp_deinit,gnutls_ocsp_resp_import,gnutls_ocsp_resp_export,gnutls_ocsp_resp_print}
284 The utility function that extracts the revocation as well as other information
285 from a response is shown below.
287 @showfuncdesc{gnutls_ocsp_resp_get_single}
289 The possible revocation reasons available in an OCSP response are shown
290 below.
292 @showenumdesc{gnutls_x509_crl_reason_t,The revocation reasons}
294 Note, that the OCSP response needs to be verified against some set of trust
295 anchors before it can be relied upon. It is also important to check
296 whether the received OCSP response corresponds to the certificate being checked.
298 @showfuncC{gnutls_ocsp_resp_verify,gnutls_ocsp_resp_verify_direct,gnutls_ocsp_resp_check_crt}
300 @node Managing encrypted keys
301 @section Managing encrypted keys
302 @cindex Encrypted keys
304 Transferring or storing private keys in plain might not be a
305 good idea. Any access on the keys becomes a fatal compromise.
306 Storing the keys in hardware security modules (see @ref{Smart cards and HSMs})
307 could solve the storage problem but it is not always practical
308 or efficient enough. This section describes alternative ways
309 that involve encryption of the private keys to store and
310 transfer.
312 There are two alternatives to use for key encryption, 
313 PKCS #8 and #12 methods of private key encryption. The PKCS #8
314 method only allows encryption of the private key, whilst the
315 PKCS #12 method allows in addition the bundling of other
316 data into the structure. That could be bundling together the
317 certificate as well as the trusted CA certificate.
319 @subheading @acronym{PKCS} #8 structures
320 @cindex PKCS #8
322 PKCS #8 keys can be imported and exported as normal private keys using
323 the functions below. An addition to the normal import functions, are
324 a password and a flags argument. The flags can be any element of the @code{gnutls_pkcs_encrypt_flags_t}
325 enumeration. Note however, that GnuTLS only supports the PKCS #5 PBES2
326 encryption scheme. Keys encrypted with the obsolete PBES1 scheme cannot 
327 be decrypted.
329 @showfuncC{gnutls_x509_privkey_import_pkcs8,gnutls_x509_privkey_export_pkcs8,gnutls_x509_privkey_export2_pkcs8}
331 @showenumdesc{gnutls_pkcs_encrypt_flags_t,Encryption flags}
333 @subheading @acronym{PKCS} #12 structures
334 @cindex PKCS #12
336 A @acronym{PKCS} #12 structure @xcite{PKCS12} usually contains a user's
337 private keys and certificates. It is commonly used in browsers to
338 export and import the user's identities.
340 In @acronym{GnuTLS} the @acronym{PKCS} #12 structures are handled
341 using the @code{gnutls_pkcs12_t} type. This is an abstract type that
342 may hold several @code{gnutls_pkcs12_bag_t} types.  The bag types are
343 the holders of the actual data, which may be certificates, private
344 keys or encrypted data.  A bag of type encrypted should be decrypted
345 in order for its data to be accessed.
347 To reduce the complexity in parsing the structures the simple 
348 helper function @funcref{gnutls_pkcs12_simple_parse} is provided. For more
349 advanced uses, manual parsing of the structure is required using the
350 functions below.
352 @showfuncD{gnutls_pkcs12_get_bag,gnutls_pkcs12_verify_mac,gnutls_pkcs12_bag_decrypt,gnutls_pkcs12_bag_get_count}
354 @showfuncdesc{gnutls_pkcs12_simple_parse}
355 @showfuncC{gnutls_pkcs12_bag_get_data,gnutls_pkcs12_bag_get_key_id,gnutls_pkcs12_bag_get_friendly_name}
357 @page
359 The functions below are used to generate a PKCS #12 structure. An example
360 of their usage is also shown.
362 @showfuncC{gnutls_pkcs12_set_bag,gnutls_pkcs12_bag_encrypt,gnutls_pkcs12_generate_mac}
363 @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}
365 @verbatiminclude examples/ex-pkcs12.c
367 @subheading Other structures
368 @cindex OpenSSL encrypted keys
369 Unfortunately the structures discussed in the previous sections are
370 not the only structures that may hold an encrypted private key. For example
371 the OpenSSL library offers a custom key encryption method. Those structures
372 are also supported in GnuTLS with @funcref{gnutls_x509_privkey_import_openssl}.
374 @showfuncdesc{gnutls_x509_privkey_import_openssl}
376 Generic and higher level private key import functions are also available.
377 @showfuncB{gnutls_x509_privkey_import2,gnutls_privkey_import_x509_raw}
379 @include invoke-certtool.texi
381 @include invoke-ocsptool.texi
383 @include invoke-danetool.texi