doc update
[gnutls.git] / doc / cha-cert-auth2.texi
blobe375050a3c6b9571c4cdc3c145db9a3d16612e6b
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 @showfuncC{gnutls_x509_crl_init,gnutls_x509_crl_import,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 @showfuncE{gnutls_x509_crl_set_version,gnutls_x509_crl_set_crt_serial,gnutls_x509_crl_set_crt,gnutls_x509_crl_set_next_update,gnutls_x509_crl_set_this_update}
121 The @funcref{gnutls_x509_crl_sign2} and @funcref{gnutls_x509_crl_privkey_sign} 
122 functions sign the revocation list with a private key. The latter function
123 can be used to sign with a key residing in a PKCS #11 token.
125 @showfuncdesc{gnutls_x509_crl_sign2}
126 @showfuncdesc{gnutls_x509_crl_privkey_sign}
128 Few extensions on the CRL structure are supported, including the
129 CRL number extension and the authority key identifier.
131 @showfuncB{gnutls_x509_crl_set_number,gnutls_x509_crl_set_authority_key_id}
133 @node OCSP certificate status checking
134 @section @acronym{OCSP} certificate status checking
135 @cindex certificate status
136 @cindex Online Certificate Status Protocol
137 @cindex OCSP
139 Certificates may be revoked before their expiration time has been
140 reached.  There are several reasons for revoking certificates, but a
141 typical situation is when the private key associated with a
142 certificate has been compromised.  Traditionally, Certificate
143 Revocation Lists (CRLs) have been used by application to implement
144 revocation checking, however, several problems with CRLs have been
145 identified @xcite{RIVESTCRL}.
147 The Online Certificate Status Protocol, or @acronym{OCSP} @xcite{RFC2560}, 
148 is a widely implemented protocol to perform certificate revocation status
149 checking.  An application that wish to verify the
150 identity of a peer will verify the certificate against a set of
151 trusted certificates and then check whether the certificate is listed
152 in a CRL and/or perform an OCSP check for the certificate.
154 Before performing the OCSP query, the application will need to figure
155 out the address of the OCSP server.  The OCSP server address can be
156 provided by the local user in manual configuration or may be stored
157 in the certificate that is being checked.  When stored in a certificate
158 the OCSP server is in the extension field called the Authority Information 
159 Access (AIA). The following function
160 extracts this information from a certificate.
162 @showfuncA{gnutls_x509_crt_get_authority_info_access}
164 There are several functions in GnuTLS for creating and manipulating
165 OCSP requests and responses.  The general idea is that a client
166 application create an OCSP request object, store some information
167 about the certificate to check in the request, and then export the
168 request in DER format.  The request will then need to be sent to the
169 OCSP responder, which needs to be done by the application (GnuTLS does
170 not send and receive OCSP packets).  Normally an OCSP response is
171 received that the application will need to import into an OCSP
172 response object.  The digital signature in the OCSP response needs to
173 be verified against a set of trust anchors before the information in
174 the response can be trusted.
176 The ASN.1 structure of OCSP requests are briefly as follows.  It is
177 useful to review the structures to get an understanding of which
178 fields are modified by GnuTLS functions.
180 @example
181 OCSPRequest     ::=     SEQUENCE @{
182     tbsRequest                  TBSRequest,
183     optionalSignature   [0]     EXPLICIT Signature OPTIONAL @}
185 TBSRequest      ::=     SEQUENCE @{
186     version             [0]     EXPLICIT Version DEFAULT v1,
187     requestorName       [1]     EXPLICIT GeneralName OPTIONAL,
188     requestList                 SEQUENCE OF Request,
189     requestExtensions   [2]     EXPLICIT Extensions OPTIONAL @}
191 Request         ::=     SEQUENCE @{
192     reqCert                     CertID,
193     singleRequestExtensions     [0] EXPLICIT Extensions OPTIONAL @}
195 CertID          ::=     SEQUENCE @{
196     hashAlgorithm       AlgorithmIdentifier,
197     issuerNameHash      OCTET STRING, -- Hash of Issuer's DN
198     issuerKeyHash       OCTET STRING, -- Hash of Issuers public key
199     serialNumber        CertificateSerialNumber @}
200 @end example
202 The basic functions to initialize, import, export and deallocate OCSP
203 requests are the following.
205 @showfuncE{gnutls_ocsp_req_init,gnutls_ocsp_req_deinit,gnutls_ocsp_req_import,gnutls_ocsp_req_export,gnutls_ocsp_req_print}
207 To generate an OCSP request the issuer name hash, issuer key hash, and 
208 the checked certificate's serial number are required. There are two
209 interfaces available for setting those in an OCSP request.
210 The is a low-level function when you have the
211 issuer name hash, issuer key hash, and certificate serial number in
212 binary form.  The second is more useful if you have the
213 certificate (and its issuer) in a @code{gnutls_x509_crt_t} type.
214 There is also a function to extract this information from existing an OCSP
215 request.
217 @showfuncC{gnutls_ocsp_req_add_cert_id,gnutls_ocsp_req_add_cert,gnutls_ocsp_req_get_cert_id}
219 Each OCSP request may contain a number of extensions.  Extensions are
220 identified by an Object Identifier (OID) and an opaque data buffer
221 whose syntax and semantics is implied by the OID. You can extract or
222 set those extensions using the following functions.
224 @showfuncB{gnutls_ocsp_req_get_extension,gnutls_ocsp_req_set_extension}
226 A common OCSP Request extension is the nonce extension (OID
227 1.3.6.1.5.5.7.48.1.2), which is used to avoid replay attacks of
228 earlier recorded OCSP responses.  The nonce extension carries a value
229 that is intended to be sufficiently random and unique so that an
230 attacker will not be able to give a stale response for the same nonce.
232 @showfuncC{gnutls_ocsp_req_get_nonce,gnutls_ocsp_req_set_nonce,gnutls_ocsp_req_randomize_nonce}
234 The OCSP response structures is a complex structure. A simplified overview
235 of it is in @ref{tab:ocsp-response}. Note that a response may contain 
236 information on multiple certificates.
238 @float Table,tab:ocsp-response
239 @multitable @columnfractions .2 .7
241 @headitem Field @tab Description
243 @item version @tab
244 The OCSP response version number (typically 1).
246 @item responder ID @tab
247 An identifier of the responder (DN name or a hash of its key).
249 @item issue time @tab
250 The time the response was generated.
252 @item thisUpdate @tab
253 The issuing time of the revocation information.
255 @item nextUpdate @tab
256 The issuing time of the revocation information that will update that one.
258 @item certificate status @tab
259 The status of the certificate.
261 @item certificate serial @tab
262 The certificate's serial number.
264 @item Revocation time @tab
265 The time the certificate was revoked.
267 @item Revocation reason @tab
268 The reason the certificate was revoked.
270 @end multitable
271 @caption{The most important OCSP response fields.}
272 @end float
275 We provide basic functions for initialization, importing, exporting
276 and deallocating OCSP responses.  
278 @showfuncE{gnutls_ocsp_resp_init,gnutls_ocsp_resp_deinit,gnutls_ocsp_resp_import,gnutls_ocsp_resp_export,gnutls_ocsp_resp_print}
280 The utility function that extracts the revocation as well as other information
281 from a response is shown below.
283 @showfuncdesc{gnutls_ocsp_resp_get_single}
285 The possible revocation reasons available in an OCSP response are shown
286 below.
288 @showenumdesc{gnutls_x509_crl_reason_t,The revocation reasons}
290 Note, that the OCSP response needs to be verified against some set of trust
291 anchors before it can be relied upon. It is also important to check
292 whether the received OCSP response corresponds to the certificate being checked.
294 @showfuncC{gnutls_ocsp_resp_verify,gnutls_ocsp_resp_verify_direct,gnutls_ocsp_resp_check_crt}
296 @node Managing encrypted keys
297 @section Managing encrypted keys
298 @cindex Encrypted keys
300 Transferring or storing private keys in plain might not be a
301 good idea. Any access on the keys becomes a fatal compromise.
302 Storing the keys in hardware security modules (see @ref{Smart cards and HSMs})
303 could solve the storage problem but it is not always practical
304 or efficient enough. This section describes alternative ways
305 that involve encryption of the private keys to store and
306 transfer.
308 There are two alternatives to use for key encryption, 
309 PKCS #8 and #12 methods of private key encryption. The PKCS #8
310 method only allows encryption of the private key, whilst the
311 PKCS #12 method allows in addition the bundling of other
312 data into the structure. That could be bundling together the
313 certificate as well as the trusted CA certificate.
315 @subheading @acronym{PKCS} #8 structures
316 @cindex PKCS #8
318 PKCS #8 keys can be imported and exported as normal private keys using
319 the functions below. An addition to the normal import functions, are
320 a password and a flags argument. The flags can be any element of the @code{gnutls_pkcs_encrypt_flags_t}
321 enumeration. Note however, that GnuTLS only supports the PKCS #5 PBES2
322 encryption scheme. Keys encrypted with the obsolete PBES1 scheme cannot 
323 be decrypted.
325 @showfuncB{gnutls_x509_privkey_import_pkcs8,gnutls_x509_privkey_export_pkcs8}
327 @showenumdesc{gnutls_pkcs_encrypt_flags_t,Encryption flags}
329 @subheading @acronym{PKCS} #12 structures
330 @cindex PKCS #12
332 A @acronym{PKCS} #12 structure @xcite{PKCS12} usually contains a user's
333 private keys and certificates. It is commonly used in browsers to
334 export and import the user's identities.
336 In @acronym{GnuTLS} the @acronym{PKCS} #12 structures are handled
337 using the @code{gnutls_pkcs12_t} type. This is an abstract type that
338 may hold several @code{gnutls_pkcs12_bag_t} types.  The bag types are
339 the holders of the actual data, which may be certificates, private
340 keys or encrypted data.  A bag of type encrypted should be decrypted
341 in order for its data to be accessed.
343 To reduce the complexity in parsing the structures the simple 
344 helper function @funcref{gnutls_pkcs12_simple_parse} is provided. For more
345 advanced uses, manual parsing of the structure is required using the
346 functions below.
348 @showfuncdesc{gnutls_pkcs12_simple_parse}
350 @showfuncC{gnutls_pkcs12_get_bag,gnutls_pkcs12_verify_mac,gnutls_pkcs12_bag_decrypt}
352 @showfuncD{gnutls_pkcs12_bag_get_count,gnutls_pkcs12_bag_get_data,gnutls_pkcs12_bag_get_key_id,gnutls_pkcs12_bag_get_friendly_name}
354 The functions below are used to generate a PKCS #12 structure. An example
355 of their usage is also shown.
357 @showfuncC{gnutls_pkcs12_set_bag,gnutls_pkcs12_bag_encrypt,gnutls_pkcs12_generate_mac}
358 @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}
360 @verbatiminclude examples/ex-pkcs12.c
362 @subheading Other structures
363 @cindex OpenSSL encrypted keys
364 Unfortunately the structures discussed in the previous sections are
365 not the only structures that may hold an encrypted private key. For example
366 the OpenSSL library offers a custom key encryption method. Those structures
367 are also supported in GnuTLS with @funcref{gnutls_x509_privkey_import_openssl}.
369 @showfuncdesc{gnutls_x509_privkey_import_openssl}
371 Generic and higher level private key import functions are also available.
372 @showfuncB{gnutls_x509_privkey_import2,gnutls_privkey_import_x509_raw}
374 @include invoke-certtool.texi
376 @include invoke-ocsptool.texi