Documentation updates
[gnutls.git] / doc / cha-cert-auth2.texi
bloba89b2ed08dcaf163974e229d4a95fc89e7fb677a
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 @end menu
19 @node PKCS 10 certificate requests
20 @section @acronym{PKCS} #10 certificate requests
21 @cindex certificate requests
22 @cindex PKCS #10
24 A certificate request is a structure, which contain information about
25 an applicant of a certificate service.  It usually contains a private
26 key, a distinguished name and secondary data such as a challenge
27 password. @acronym{GnuTLS} supports the requests defined in
28 @acronym{PKCS} #10 @xcite{RFC2986}. Other formats of certificate requests
29 are not currently supported.
31 A certificate request can be generated by
32 associating it with a private key, setting the
33 subject's information and finally self signing it.
34 The last step ensures that the requester is in
35 possession of the private key.
37 @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}
39 The @funcref{gnutls_x509_crq_set_key} and @funcref{gnutls_x509_crq_sign2} 
40 functions associate the request with a private key and sign it. If a 
41 request is to be signed with a key residing in a PKCS #11 token it is recommended to use
42 the signing functions shown in @ref{Abstract key types}.
44 @showfuncdesc{gnutls_x509_crq_set_key}
45 @showfuncdesc{gnutls_x509_crq_sign2}
47 The following example is about generating a certificate request, and a
48 private key. A certificate request can be later be processed by a CA
49 which should return a signed certificate.
51 @anchor{ex:crq}
52 @verbatiminclude examples/ex-crq.c
54 @node PKIX certificate revocation lists
55 @section PKIX certificate revocation lists
56 @cindex certificate revocation lists
57 @cindex CRL
59 A certificate revocation list (CRL) is a structure issued by an authority
60 periodically containing a list of revoked certificates serial numbers.
61 The CRL structure is signed with the issuing authorities' keys. A typical
62 CRL contains the fields as shown in @ref{tab:crl}.
63 Certificate revocation lists are used to complement the expiration date of a certificate,
64 in order to account for other reasons of revocation, such as compromised keys, etc.
66 Each CRL is valid for limited amount of
67 time and is required to provide, except for the current issuing time, also 
68 the issuing time of the next update.
70 @float Table,tab:crl
71 @multitable @columnfractions .2 .7
73 @headitem Field @tab Description
75 @item version @tab
76 The field that indicates the version of the CRL structure.
78 @item signature @tab
79 A signature by the issuing authority.
81 @item issuer @tab
82 Holds the issuer's distinguished name.
84 @item thisUpdate @tab
85 The issuing time of the revocation list.
87 @item nextUpdate @tab
88 The issuing time of the revocation list that will update that one.
90 @item revokedCertificates @tab
91 List of revoked certificates serial numbers.
93 @item extensions @tab
94 Optional CRL structure extensions.
96 @end multitable
97 @caption{Certificate revocation list fields.}
98 @end float
100 The basic CRL structure functions follow.
102 @showfuncD{gnutls_x509_crl_init,gnutls_x509_crl_import,gnutls_x509_crl_export,gnutls_x509_crl_export}
104 @subheading Reading a CRL
106 The most important function that extracts the certificate revocation
107 information from a CRL is @funcref{gnutls_x509_crl_get_crt_serial}. Other
108 functions that return other fields of the CRL structure are also provided.
110 @showfuncdesc{gnutls_x509_crl_get_crt_serial}
112 @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}
115 @subheading Generation of a CRL
117 The following functions can be used to generate a CRL.
119 @showfuncB{gnutls_x509_crl_set_version,gnutls_x509_crl_set_crt_serial}
120 @showfuncC{gnutls_x509_crl_set_crt,gnutls_x509_crl_set_next_update,gnutls_x509_crl_set_this_update}
122 The @funcref{gnutls_x509_crl_sign2} and @funcref{gnutls_x509_crl_privkey_sign} 
123 functions sign the revocation list with a private key. The latter function
124 can be used to sign with a key residing in a PKCS #11 token.
126 @showfuncdesc{gnutls_x509_crl_sign2}
127 @showfuncdesc{gnutls_x509_crl_privkey_sign}
129 Few extensions on the CRL structure are supported, including the
130 CRL number extension and the authority key identifier.
132 @showfuncB{gnutls_x509_crl_set_number,gnutls_x509_crl_set_authority_key_id}
134 @node OCSP certificate status checking
135 @section @acronym{OCSP} certificate status checking
136 @cindex certificate status
137 @cindex Online Certificate Status Protocol
138 @cindex OCSP
140 Certificates may be revoked before their expiration time has been
141 reached.  There are several reasons for revoking certificates, but a
142 typical situation is when the private key associated with a
143 certificate has been compromised.  Traditionally, Certificate
144 Revocation Lists (CRLs) have been used by application to implement
145 revocation checking, however, several problems with CRLs have been
146 identified @xcite{RIVESTCRL}.
148 The Online Certificate Status Protocol, or @acronym{OCSP} @xcite{RFC2560}, 
149 is a widely implemented protocol to perform certificate revocation status
150 checking.  An application that wish to verify the
151 identity of a peer will verify the certificate against a set of
152 trusted certificates and then check whether the certificate is listed
153 in a CRL and/or perform an OCSP check for the certificate.
155 Before performing the OCSP query, the application will need to figure
156 out the address of the OCSP server.  The OCSP server address can be
157 provided by the local user in manual configuration or may be stored
158 in the certificate that is being checked.  When stored in a certificate
159 the OCSP server is in the extension field called the Authority Information 
160 Access (AIA). The following function
161 extracts this information from a certificate.
163 @showfuncA{gnutls_x509_crt_get_authority_info_access}
165 There are several functions in GnuTLS for creating and manipulating
166 OCSP requests and responses.  The general idea is that a client
167 application create an OCSP request object, store some information
168 about the certificate to check in the request, and then export the
169 request in DER format.  The request will then need to be sent to the
170 OCSP responder, which needs to be done by the application (GnuTLS does
171 not send and receive OCSP packets).  Normally an OCSP response is
172 received that the application will need to import into an OCSP
173 response object.  The digital signature in the OCSP response needs to
174 be verified against a set of trust anchors before the information in
175 the response can be trusted.
177 The ASN.1 structure of OCSP requests are briefly as follows.  It is
178 useful to review the structures to get an understanding of which
179 fields are modified by GnuTLS functions.
181 @example
182 OCSPRequest     ::=     SEQUENCE @{
183     tbsRequest                  TBSRequest,
184     optionalSignature   [0]     EXPLICIT Signature OPTIONAL @}
186 TBSRequest      ::=     SEQUENCE @{
187     version             [0]     EXPLICIT Version DEFAULT v1,
188     requestorName       [1]     EXPLICIT GeneralName OPTIONAL,
189     requestList                 SEQUENCE OF Request,
190     requestExtensions   [2]     EXPLICIT Extensions OPTIONAL @}
192 Request         ::=     SEQUENCE @{
193     reqCert                     CertID,
194     singleRequestExtensions     [0] EXPLICIT Extensions OPTIONAL @}
196 CertID          ::=     SEQUENCE @{
197     hashAlgorithm       AlgorithmIdentifier,
198     issuerNameHash      OCTET STRING, -- Hash of Issuer's DN
199     issuerKeyHash       OCTET STRING, -- Hash of Issuers public key
200     serialNumber        CertificateSerialNumber @}
201 @end example
203 The basic functions to initialize, import, export and deallocate OCSP
204 requests are the following.
206 @showfuncE{gnutls_ocsp_req_init,gnutls_ocsp_req_deinit,gnutls_ocsp_req_import,gnutls_ocsp_req_export,gnutls_ocsp_req_print}
208 To generate an OCSP request the issuer name hash, issuer key hash, and 
209 the checked certificate's serial number are required. There are two
210 interfaces available for setting those in an OCSP request.
211 The is a low-level function when you have the
212 issuer name hash, issuer key hash, and certificate serial number in
213 binary form.  The second is more useful if you have the
214 certificate (and its issuer) in a @code{gnutls_x509_crt_t} type.
215 There is also a function to extract this information from existing an OCSP
216 request.
218 @showfuncC{gnutls_ocsp_req_add_cert_id,gnutls_ocsp_req_add_cert,gnutls_ocsp_req_get_cert_id}
220 Each OCSP request may contain a number of extensions.  Extensions are
221 identified by an Object Identifier (OID) and an opaque data buffer
222 whose syntax and semantics is implied by the OID. You can extract or
223 set those extensions using the following functions.
225 @showfuncB{gnutls_ocsp_req_get_extension,gnutls_ocsp_req_set_extension}
227 A common OCSP Request extension is the nonce extension (OID
228 1.3.6.1.5.5.7.48.1.2), which is used to avoid replay attacks of
229 earlier recorded OCSP responses.  The nonce extension carries a value
230 that is intended to be sufficiently random and unique so that an
231 attacker will not be able to give a stale response for the same nonce.
233 @showfuncC{gnutls_ocsp_req_get_nonce,gnutls_ocsp_req_set_nonce,gnutls_ocsp_req_randomize_nonce}
235 The OCSP response structures is a complex structure. A simplified overview
236 of it is in @ref{tab:ocsp-response}. Note that a response may contain 
237 information on multiple certificates.
239 @float Table,tab:ocsp-response
240 @multitable @columnfractions .2 .7
242 @headitem Field @tab Description
244 @item version @tab
245 The OCSP response version number (typically 1).
247 @item responder ID @tab
248 An identifier of the responder (DN name or a hash of its key).
250 @item issue time @tab
251 The time the response was generated.
253 @item thisUpdate @tab
254 The issuing time of the revocation information.
256 @item nextUpdate @tab
257 The issuing time of the revocation information that will update that one.
259 @item @tab Revoked certificates
261 @item certificate status @tab
262 The status of the certificate.
264 @item certificate serial @tab
265 The certificate's serial number.
267 @item revocationTime @tab
268 The time the certificate was revoked.
270 @item revocationReason @tab
271 The reason the certificate was revoked.
273 @end multitable
274 @caption{The most important OCSP response fields.}
275 @end float
278 We provide basic functions for initialization, importing, exporting
279 and deallocating OCSP responses.  
281 @showfuncE{gnutls_ocsp_resp_init,gnutls_ocsp_resp_deinit,gnutls_ocsp_resp_import,gnutls_ocsp_resp_export,gnutls_ocsp_resp_print}
283 The utility function that extracts the revocation as well as other information
284 from a response is shown below.
286 @showfuncdesc{gnutls_ocsp_resp_get_single}
288 The possible revocation reasons available in an OCSP response are shown
289 below.
291 @showenumdesc{gnutls_x509_crl_reason_t,The revocation reasons}
293 Note, that the OCSP response needs to be verified against some set of trust
294 anchors before it can be relied upon. It is also important to check
295 whether the received OCSP response corresponds to the certificate being checked.
297 @showfuncC{gnutls_ocsp_resp_verify,gnutls_ocsp_resp_verify_direct,gnutls_ocsp_resp_check_crt}
299 @node Managing encrypted keys
300 @section Managing encrypted keys
301 @cindex Encrypted keys
303 Transferring or storing private keys in plain might not be a
304 good idea. Any access on the keys becomes a fatal compromise.
305 Storing the keys in hardware security modules (see @ref{Smart cards and HSMs})
306 could solve the storage problem but it is not always practical
307 or efficient enough. This section describes alternative ways
308 that involve encryption of the private keys to store and
309 transfer.
311 There are two alternatives to use for key encryption, 
312 PKCS #8 and #12 methods of private key encryption. The PKCS #8
313 method only allows encryption of the private key, whilst the
314 PKCS #12 method allows in addition the bundling of other
315 data into the structure. That could be bundling together the
316 certificate as well as the trusted CA certificate.
318 @subheading @acronym{PKCS} #8 structures
319 @cindex PKCS #8
321 PKCS #8 keys can be imported and exported as normal private keys using
322 the functions below. An addition to the normal import functions, are
323 a password and a flags argument. The flags can be any element of the @code{gnutls_pkcs_encrypt_flags_t}
324 enumeration. Note however, that GnuTLS only supports the PKCS #5 PBES2
325 encryption scheme. Keys encrypted with the obsolete PBES1 scheme cannot 
326 be decrypted.
328 @showfuncC{gnutls_x509_privkey_import_pkcs8,gnutls_x509_privkey_export_pkcs8,gnutls_x509_privkey_export2_pkcs8}
330 @showenumdesc{gnutls_pkcs_encrypt_flags_t,Encryption flags}
332 @subheading @acronym{PKCS} #12 structures
333 @cindex PKCS #12
335 A @acronym{PKCS} #12 structure @xcite{PKCS12} usually contains a user's
336 private keys and certificates. It is commonly used in browsers to
337 export and import the user's identities.
339 In @acronym{GnuTLS} the @acronym{PKCS} #12 structures are handled
340 using the @code{gnutls_pkcs12_t} type. This is an abstract type that
341 may hold several @code{gnutls_pkcs12_bag_t} types.  The bag types are
342 the holders of the actual data, which may be certificates, private
343 keys or encrypted data.  A bag of type encrypted should be decrypted
344 in order for its data to be accessed.
346 To reduce the complexity in parsing the structures the simple 
347 helper function @funcref{gnutls_pkcs12_simple_parse} is provided. For more
348 advanced uses, manual parsing of the structure is required using the
349 functions below.
351 @showfuncD{gnutls_pkcs12_get_bag,gnutls_pkcs12_verify_mac,gnutls_pkcs12_bag_decrypt,gnutls_pkcs12_bag_get_count}
353 @showfuncdesc{gnutls_pkcs12_simple_parse}
354 @showfuncC{gnutls_pkcs12_bag_get_data,gnutls_pkcs12_bag_get_key_id,gnutls_pkcs12_bag_get_friendly_name}
356 @page
358 The functions below are used to generate a PKCS #12 structure. An example
359 of their usage is also shown.
361 @showfuncC{gnutls_pkcs12_set_bag,gnutls_pkcs12_bag_encrypt,gnutls_pkcs12_generate_mac}
362 @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}
364 @verbatiminclude examples/ex-pkcs12.c
366 @subheading Other structures
367 @cindex OpenSSL encrypted keys
368 Unfortunately the structures discussed in the previous sections are
369 not the only structures that may hold an encrypted private key. For example
370 the OpenSSL library offers a custom key encryption method. Those structures
371 are also supported in GnuTLS with @funcref{gnutls_x509_privkey_import_openssl}.
373 @showfuncdesc{gnutls_x509_privkey_import_openssl}
375 Generic and higher level private key import functions are also available.
376 @showfuncB{gnutls_x509_privkey_import2,gnutls_privkey_import_x509_raw}
378 @include invoke-certtool.texi
380 @include invoke-ocsptool.texi