documented gnutls_ocsp_resp_check_crt().
[gnutls.git] / doc / cha-cert-auth2.texi
blobfa6c55d1796daf7510a992b351322d6ff210f301
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 A certificate request can be generated by
67 associating it with a private key, setting the
68 subject's information and finally self signing it.
69 The last step ensures that the requester is in
70 possession of the private key. Each CRL is valid for limited amount of
71 time and is required to provide, except for the current issuing time, also 
72 the issuing time of the next update.
74 @float Table,tab:crl
75 @multitable @columnfractions .2 .7
77 @headitem Field @tab Description
79 @item version @tab
80 The field that indicates the version of the CRL structure.
82 @item signature @tab
83 A signature by the issuing authority.
85 @item issuer @tab
86 Holds the issuer's distinguished name.
88 @item thisUpdate @tab
89 The issuing time of the revocation list.
91 @item nextUpdate @tab
92 The issuing time of the revocation list that will update that one.
94 @item revokedCertificates @tab
95 List of revoked certificates serial numbers.
97 @item extensions @tab
98 Optional CRL structure extensions.
100 @end multitable
101 @caption{Certificate revocation list fields.}
102 @end float
105 @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}
107 The @funcref{gnutls_x509_crl_sign2} and @funcref{gnutls_x509_crl_privkey_sign} 
108 functions sign the revocation list with a private key. The latter function
109 can be used to sign with a key residing in a PKCS #11 token.
111 @showfuncdesc{gnutls_x509_crl_sign2}
112 @showfuncdesc{gnutls_x509_crl_privkey_sign}
114 Few extensions on the CRL structure are supported, including the
115 CRL number extension and the authority key identifier.
117 @showfuncB{gnutls_x509_crl_set_number,gnutls_x509_crl_set_authority_key_id}
119 @node OCSP certificate status checking
120 @section @acronym{OCSP} certificate status checking
121 @cindex certificate status
122 @cindex Online Certificate Status Protocol
123 @cindex OCSP
125 Certificates may be revoked before their expiration time has been
126 reached.  There are several reasons for revoking certificates, but a
127 typical situation is when the private key associated with a
128 certificate has been compromised.  Traditionally, Certificate
129 Revocation Lists (CRLs) have been used by application to implement
130 revocation checking, however, several problems with CRLs have been
131 identified @xcite{RIVESTCRL}.
133 The Online Certificate Status Protocol, or @acronym{OCSP} @xcite{RFC2560}, 
134 is a widely implemented protocol to perform certificate revocation status
135 checking.  An application that wish to verify the
136 identity of a peer will verify the certificate against a set of
137 trusted certificates and then check whether the certificate is listed
138 in a CRL and/or perform an OCSP check for the certificate.
140 Before performing the OCSP query, the application will need to figure
141 out the address of the OCSP server.  The OCSP server address can be
142 provided by the local user in manual configuration or may be stored
143 in the certificate that is being checked.  The latter is due to
144 an extension field called the Authority Information Access (AIA) which
145 may hold the location of the OCSP responder in 
146 the access method called @code{id-ad-ocsp}. The following function
147 extracts this information from a certificate.
149 @showfuncA{gnutls_x509_crt_get_authority_info_access}
151 There are several functions in GnuTLS for creating and manipulating
152 OCSP requests and responses.  The general idea is that a client
153 application create an OCSP request object, store some information
154 about the certificate to check in the request, and then export the
155 request in DER format.  The request will then need to be sent to the
156 OCSP responder, which needs to be done by the application (GnuTLS does
157 not send and receive OCSP packets).  Normally an OCSP response is
158 received that the application will need to import into an OCSP
159 response object.  The digital signature in the OCSP response needs to
160 be verified against a set of trust anchors before the information in
161 the response can be trusted.
163 The ASN.1 structure of OCSP requests are briefly as follows.  It is
164 useful to review the structures to get an understanding of which
165 fields are modified by GnuTLS functions.
167 @example
168 OCSPRequest     ::=     SEQUENCE @{
169     tbsRequest                  TBSRequest,
170     optionalSignature   [0]     EXPLICIT Signature OPTIONAL @}
172 TBSRequest      ::=     SEQUENCE @{
173     version             [0]     EXPLICIT Version DEFAULT v1,
174     requestorName       [1]     EXPLICIT GeneralName OPTIONAL,
175     requestList                 SEQUENCE OF Request,
176     requestExtensions   [2]     EXPLICIT Extensions OPTIONAL @}
178 Request         ::=     SEQUENCE @{
179     reqCert                     CertID,
180     singleRequestExtensions     [0] EXPLICIT Extensions OPTIONAL @}
182 CertID          ::=     SEQUENCE @{
183     hashAlgorithm       AlgorithmIdentifier,
184     issuerNameHash      OCTET STRING, -- Hash of Issuer's DN
185     issuerKeyHash       OCTET STRING, -- Hash of Issuers public key
186     serialNumber        CertificateSerialNumber @}
187 @end example
189 The basic functions to initialize, import, export and deallocate OCSP
190 requests are the following.
192 @showfuncE{gnutls_ocsp_req_init,gnutls_ocsp_req_deinit,gnutls_ocsp_req_import,gnutls_ocsp_req_export,gnutls_ocsp_req_print}
194 There are two interfaces for setting the identity of a certificate in
195 a OCSP request, the first being a low-level function when you have the
196 issuer name hash, issuer key hash, and certificate serial number in
197 binary form.  The second is usually more useful if you have the
198 certificate (and its issuer) in a @code{gnutls_x509_crt_t} type.
199 There is also a function to extract this information from an OCSP
200 request.
202 @showfuncC{gnutls_ocsp_req_add_cert_id,gnutls_ocsp_req_add_cert,gnutls_ocsp_req_get_cert_id}
204 Each OCSP request may contain a number of extensions.  Extensions are
205 identified by an Object Identifier (OID) and an opaque data buffer
206 whose syntax and semantics is implied by the OID.
208 @showfuncB{gnutls_ocsp_req_get_extension,gnutls_ocsp_req_set_extension}
210 A common OCSP Request extension is the nonce extension (OID
211 1.3.6.1.5.5.7.48.1.2), which is used to avoid replay attacks of
212 earlier recorded OCSP responses.  The nonce extension carries a value
213 that is intended to be sufficiently random and unique so that an
214 attacker will not be able to give a stale response for the same nonce.
216 @showfuncC{gnutls_ocsp_req_get_nonce,gnutls_ocsp_req_set_nonce,gnutls_ocsp_req_randomize_nonce}
218 The OCSP response structures is a bit more complex than the request.
219 The important ASN.1 structure is as follows.  In practice, all OCSP
220 responses contain a Basic OCSP response sub-structure.
222 @example
223 OCSPResponse ::= SEQUENCE @{
224    responseStatus         OCSPResponseStatus,
225    responseBytes          [0] EXPLICIT ResponseBytes OPTIONAL @}
227 OCSPResponseStatus ::= ENUMERATED @{
228     successful            (0),  --Response has valid confirmations
229     malformedRequest      (1),  --Illegal confirmation request
230     internalError         (2),  --Internal error in issuer
231     tryLater              (3),  --Try again later
232                                 --(4) is not used
233     sigRequired           (5),  --Must sign the request
234     unauthorized          (6)   --Request unauthorized @}
236 ResponseBytes ::=       SEQUENCE @{
237     responseType   OBJECT IDENTIFIER,
238     response       OCTET STRING @}
240 id-pkix-ocsp-basic     OBJECT IDENTIFIER ::= @{ id-pkix-ocsp 1 @}
242 BasicOCSPResponse       ::= SEQUENCE @{
243    tbsResponseData      ResponseData,
244    signatureAlgorithm   AlgorithmIdentifier,
245    signature            BIT STRING,
246    certs                [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL @}
248 ResponseData ::= SEQUENCE @{
249    version              [0] EXPLICIT Version DEFAULT v1,
250    responderID              ResponderID,
251    producedAt               GeneralizedTime,
252    responses                SEQUENCE OF SingleResponse,
253    responseExtensions   [1] EXPLICIT Extensions OPTIONAL @}
255 ResponderID ::= CHOICE @{
256    byName               [1] Name,
257    byKey                [2] KeyHash @}
259 KeyHash ::= OCTET STRING -- SHA-1 hash of responder's public key
260 (excluding the tag and length fields)
262 SingleResponse ::= SEQUENCE @{
263    certID                       CertID,
264    certStatus                   CertStatus,
265    thisUpdate                   GeneralizedTime,
266    nextUpdate         [0]       EXPLICIT GeneralizedTime OPTIONAL,
267    singleExtensions   [1]       EXPLICIT Extensions OPTIONAL @}
269 CertStatus ::= CHOICE @{
270     good        [0]     IMPLICIT NULL,
271     revoked     [1]     IMPLICIT RevokedInfo,
272     unknown     [2]     IMPLICIT UnknownInfo @}
274 RevokedInfo ::= SEQUENCE @{
275     revocationTime              GeneralizedTime,
276     revocationReason    [0]     EXPLICIT CRLReason OPTIONAL @}
277 @end example
279 We provide basic functions for initialization, importing, exporting
280 and deallocating OCSP responses.  The Basic OCSP Response structure is
281 automatically parsed when an OCSP Response is imported.
283 @showfuncE{gnutls_ocsp_resp_init,gnutls_ocsp_resp_deinit,gnutls_ocsp_resp_import,gnutls_ocsp_resp_export,gnutls_ocsp_resp_print}
285 The OCSP response needs to be verified against some set of trust
286 anchors before it can be relied upon, and it is wise to check whether
287 the OCSP response corresponds to the certificate being checked.
289 @showfuncC{gnutls_ocsp_resp_verify,gnutls_ocsp_resp_verify_direct,gnutls_ocsp_resp_check_crt}
291 @node Managing encrypted keys
292 @section Managing encrypted keys
293 @cindex Encrypted keys
295 Transferring or storing private keys in plain might not be a
296 good idea. Any access on the keys becomes a fatal compromise.
297 Storing the keys in hardware security modules (see @ref{Smart cards and HSMs})
298 could solve the storage problem but it is not always practical
299 or efficient enough. This section describes alternative ways
300 that involve encryption of the private keys to store and
301 transfer.
303 There are two alternatives to use for key encryption, 
304 PKCS #8 and #12 methods of private key encryption. The PKCS #8
305 method only allows encryption of the private key, whilst the
306 PKCS #12 method allows in addition the bundling of other
307 data into the structure. That could be bundling together the
308 certificate as well as the trusted CA certificate.
310 @subheading @acronym{PKCS} #8 structures
311 @cindex PKCS #8
313 PKCS #8 keys can be imported and exported as normal private keys using
314 the functions below. An addition to the normal import functions, are
315 a password and a flags argument. The flags can be any element of the @code{gnutls_pkcs_encrypt_flags_t}
316 enumeration. Note however, that GnuTLS only supports the PKCS #5 PBES2
317 encryption scheme. Keys encrypted with the obsolete PBES1 scheme cannot 
318 be decrypted.
320 @showfuncB{gnutls_x509_privkey_import_pkcs8,gnutls_x509_privkey_export_pkcs8}
322 @showenumdesc{gnutls_pkcs_encrypt_flags_t,Encryption flags}
324 @subheading @acronym{PKCS} #12 structures
325 @cindex PKCS #12
327 A @acronym{PKCS} #12 structure @xcite{PKCS12} usually contains a user's
328 private keys and certificates. It is commonly used in browsers to
329 export and import the user's identities.
331 In @acronym{GnuTLS} the @acronym{PKCS} #12 structures are handled
332 using the @code{gnutls_pkcs12_t} type. This is an abstract type that
333 may hold several @code{gnutls_pkcs12_bag_t} types.  The bag types are
334 the holders of the actual data, which may be certificates, private
335 keys or encrypted data.  A bag of type encrypted should be decrypted
336 in order for its data to be accessed.
338 To reduce the complexity in parsing the structures the simple 
339 helper function @funcref{gnutls_pkcs12_simple_parse} is provided. For more
340 advanced uses, manual parsing of the structure is required using the
341 functions below.
343 @showfuncdesc{gnutls_pkcs12_simple_parse}
345 @showfuncC{gnutls_pkcs12_get_bag,gnutls_pkcs12_verify_mac,gnutls_pkcs12_bag_decrypt}
347 @showfuncD{gnutls_pkcs12_bag_get_count,gnutls_pkcs12_bag_get_data,gnutls_pkcs12_bag_get_key_id,gnutls_pkcs12_bag_get_friendly_name}
349 The functions below are used to generate a PKCS #12 structure. An example
350 of their usage is also shown.
352 @showfuncC{gnutls_pkcs12_set_bag,gnutls_pkcs12_bag_encrypt,gnutls_pkcs12_generate_mac}
353 @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}
355 @verbatiminclude examples/ex-pkcs12.c
357 @subheading Other structures
358 @cindex OpenSSL encrypted keys
359 Unfortunately the structures discussed in the previous sections are
360 not the only structures that may hold an encrypted private key. For example
361 the OpenSSL library offers a custom key encryption method. Those structures
362 are also supported in GnuTLS with @funcref{gnutls_x509_privkey_import_openssl}.
364 @showfuncdesc{gnutls_x509_privkey_import_openssl}
366 Generic and higher level private key import functions are also available.
367 @showfuncB{gnutls_x509_privkey_import2,gnutls_privkey_import_x509_raw}
369 @include invoke-certtool.texi
371 @include invoke-ocsptool.texi