2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include "internal/x509_int.h"
15 #include <openssl/x509v3.h>
18 static int X509_REVOKED_cmp(const X509_REVOKED
*const *a
,
19 const X509_REVOKED
*const *b
);
20 static void setup_idp(X509_CRL
*crl
, ISSUING_DIST_POINT
*idp
);
22 ASN1_SEQUENCE(X509_REVOKED
) = {
23 ASN1_EMBED(X509_REVOKED
,serialNumber
, ASN1_INTEGER
),
24 ASN1_SIMPLE(X509_REVOKED
,revocationDate
, ASN1_TIME
),
25 ASN1_SEQUENCE_OF_OPT(X509_REVOKED
,extensions
, X509_EXTENSION
)
26 } ASN1_SEQUENCE_END(X509_REVOKED
)
28 static int def_crl_verify(X509_CRL
*crl
, EVP_PKEY
*r
);
29 static int def_crl_lookup(X509_CRL
*crl
,
30 X509_REVOKED
**ret
, ASN1_INTEGER
*serial
,
33 static X509_CRL_METHOD int_crl_meth
= {
40 static const X509_CRL_METHOD
*default_crl_method
= &int_crl_meth
;
43 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
44 * the original encoding the signature wont be affected by reordering of the
47 static int crl_inf_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
50 X509_CRL_INFO
*a
= (X509_CRL_INFO
*)*pval
;
52 if (!a
|| !a
->revoked
)
56 * Just set cmp function here. We don't sort because that would
57 * affect the output of X509_CRL_print().
59 case ASN1_OP_D2I_POST
:
60 (void)sk_X509_REVOKED_set_cmp_func(a
->revoked
, X509_REVOKED_cmp
);
67 ASN1_SEQUENCE_enc(X509_CRL_INFO
, enc
, crl_inf_cb
) = {
68 ASN1_OPT(X509_CRL_INFO
, version
, ASN1_INTEGER
),
69 ASN1_EMBED(X509_CRL_INFO
, sig_alg
, X509_ALGOR
),
70 ASN1_SIMPLE(X509_CRL_INFO
, issuer
, X509_NAME
),
71 ASN1_SIMPLE(X509_CRL_INFO
, lastUpdate
, ASN1_TIME
),
72 ASN1_OPT(X509_CRL_INFO
, nextUpdate
, ASN1_TIME
),
73 ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO
, revoked
, X509_REVOKED
),
74 ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO
, extensions
, X509_EXTENSION
, 0)
75 } ASN1_SEQUENCE_END_enc(X509_CRL_INFO
, X509_CRL_INFO
)
78 * Set CRL entry issuer according to CRL certificate issuer extension. Check
79 * for unhandled critical CRL entry extensions.
82 static int crl_set_issuers(X509_CRL
*crl
)
86 GENERAL_NAMES
*gens
, *gtmp
;
87 STACK_OF(X509_REVOKED
) *revoked
;
89 revoked
= X509_CRL_get_REVOKED(crl
);
92 for (i
= 0; i
< sk_X509_REVOKED_num(revoked
); i
++) {
93 X509_REVOKED
*rev
= sk_X509_REVOKED_value(revoked
, i
);
94 STACK_OF(X509_EXTENSION
) *exts
;
95 ASN1_ENUMERATED
*reason
;
97 gtmp
= X509_REVOKED_get_ext_d2i(rev
,
98 NID_certificate_issuer
, &j
, NULL
);
99 if (!gtmp
&& (j
!= -1)) {
100 crl
->flags
|= EXFLAG_INVALID
;
107 crl
->issuers
= sk_GENERAL_NAMES_new_null();
111 if (!sk_GENERAL_NAMES_push(crl
->issuers
, gtmp
))
116 reason
= X509_REVOKED_get_ext_d2i(rev
, NID_crl_reason
, &j
, NULL
);
117 if (!reason
&& (j
!= -1)) {
118 crl
->flags
|= EXFLAG_INVALID
;
123 rev
->reason
= ASN1_ENUMERATED_get(reason
);
124 ASN1_ENUMERATED_free(reason
);
126 rev
->reason
= CRL_REASON_NONE
;
128 /* Check for critical CRL entry extensions */
130 exts
= rev
->extensions
;
132 for (j
= 0; j
< sk_X509_EXTENSION_num(exts
); j
++) {
133 ext
= sk_X509_EXTENSION_value(exts
, j
);
134 if (X509_EXTENSION_get_critical(ext
)) {
135 if (OBJ_obj2nid(X509_EXTENSION_get_object(ext
)) == NID_certificate_issuer
)
137 crl
->flags
|= EXFLAG_CRITICAL
;
149 * The X509_CRL structure needs a bit of customisation. Cache some extensions
150 * and hash of the whole CRL.
152 static int crl_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
155 X509_CRL
*crl
= (X509_CRL
*)*pval
;
156 STACK_OF(X509_EXTENSION
) *exts
;
161 case ASN1_OP_NEW_POST
:
166 crl
->idp_reasons
= CRLDP_ALL_REASONS
;
167 crl
->meth
= default_crl_method
;
168 crl
->meth_data
= NULL
;
170 crl
->crl_number
= NULL
;
171 crl
->base_crl_number
= NULL
;
174 case ASN1_OP_D2I_POST
:
175 X509_CRL_digest(crl
, EVP_sha1(), crl
->sha1_hash
, NULL
);
176 crl
->idp
= X509_CRL_get_ext_d2i(crl
,
177 NID_issuing_distribution_point
, NULL
,
180 setup_idp(crl
, crl
->idp
);
182 crl
->akid
= X509_CRL_get_ext_d2i(crl
,
183 NID_authority_key_identifier
, NULL
,
186 crl
->crl_number
= X509_CRL_get_ext_d2i(crl
,
187 NID_crl_number
, NULL
, NULL
);
189 crl
->base_crl_number
= X509_CRL_get_ext_d2i(crl
,
192 /* Delta CRLs must have CRL number */
193 if (crl
->base_crl_number
&& !crl
->crl_number
)
194 crl
->flags
|= EXFLAG_INVALID
;
197 * See if we have any unhandled critical CRL extensions and indicate
198 * this in a flag. We only currently handle IDP so anything else
199 * critical sets the flag. This code accesses the X509_CRL structure
200 * directly: applications shouldn't do this.
203 exts
= crl
->crl
.extensions
;
205 for (idx
= 0; idx
< sk_X509_EXTENSION_num(exts
); idx
++) {
207 ext
= sk_X509_EXTENSION_value(exts
, idx
);
208 nid
= OBJ_obj2nid(X509_EXTENSION_get_object(ext
));
209 if (nid
== NID_freshest_crl
)
210 crl
->flags
|= EXFLAG_FRESHEST
;
211 if (X509_EXTENSION_get_critical(ext
)) {
212 /* We handle IDP and deltas */
213 if ((nid
== NID_issuing_distribution_point
)
214 || (nid
== NID_authority_key_identifier
)
215 || (nid
== NID_delta_crl
))
217 crl
->flags
|= EXFLAG_CRITICAL
;
222 if (!crl_set_issuers(crl
))
225 if (crl
->meth
->crl_init
) {
226 if (crl
->meth
->crl_init(crl
) == 0)
231 case ASN1_OP_FREE_POST
:
232 if (crl
->meth
->crl_free
) {
233 if (!crl
->meth
->crl_free(crl
))
236 AUTHORITY_KEYID_free(crl
->akid
);
237 ISSUING_DIST_POINT_free(crl
->idp
);
238 ASN1_INTEGER_free(crl
->crl_number
);
239 ASN1_INTEGER_free(crl
->base_crl_number
);
240 sk_GENERAL_NAMES_pop_free(crl
->issuers
, GENERAL_NAMES_free
);
246 /* Convert IDP into a more convenient form */
248 static void setup_idp(X509_CRL
*crl
, ISSUING_DIST_POINT
*idp
)
251 /* Set various flags according to IDP */
252 crl
->idp_flags
|= IDP_PRESENT
;
253 if (idp
->onlyuser
> 0) {
255 crl
->idp_flags
|= IDP_ONLYUSER
;
257 if (idp
->onlyCA
> 0) {
259 crl
->idp_flags
|= IDP_ONLYCA
;
261 if (idp
->onlyattr
> 0) {
263 crl
->idp_flags
|= IDP_ONLYATTR
;
267 crl
->idp_flags
|= IDP_INVALID
;
269 if (idp
->indirectCRL
> 0)
270 crl
->idp_flags
|= IDP_INDIRECT
;
272 if (idp
->onlysomereasons
) {
273 crl
->idp_flags
|= IDP_REASONS
;
274 if (idp
->onlysomereasons
->length
> 0)
275 crl
->idp_reasons
= idp
->onlysomereasons
->data
[0];
276 if (idp
->onlysomereasons
->length
> 1)
277 crl
->idp_reasons
|= (idp
->onlysomereasons
->data
[1] << 8);
278 crl
->idp_reasons
&= CRLDP_ALL_REASONS
;
281 DIST_POINT_set_dpname(idp
->distpoint
, X509_CRL_get_issuer(crl
));
284 ASN1_SEQUENCE_ref(X509_CRL
, crl_cb
) = {
285 ASN1_EMBED(X509_CRL
, crl
, X509_CRL_INFO
),
286 ASN1_EMBED(X509_CRL
, sig_alg
, X509_ALGOR
),
287 ASN1_EMBED(X509_CRL
, signature
, ASN1_BIT_STRING
)
288 } ASN1_SEQUENCE_END_ref(X509_CRL
, X509_CRL
)
290 IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED
)
292 IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED
)
294 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO
)
296 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL
)
298 IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL
)
300 static int X509_REVOKED_cmp(const X509_REVOKED
*const *a
,
301 const X509_REVOKED
*const *b
)
303 return (ASN1_STRING_cmp((ASN1_STRING
*)&(*a
)->serialNumber
,
304 (ASN1_STRING
*)&(*b
)->serialNumber
));
307 int X509_CRL_add0_revoked(X509_CRL
*crl
, X509_REVOKED
*rev
)
311 if (inf
->revoked
== NULL
)
312 inf
->revoked
= sk_X509_REVOKED_new(X509_REVOKED_cmp
);
313 if (inf
->revoked
== NULL
|| !sk_X509_REVOKED_push(inf
->revoked
, rev
)) {
314 ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED
, ERR_R_MALLOC_FAILURE
);
317 inf
->enc
.modified
= 1;
321 int X509_CRL_verify(X509_CRL
*crl
, EVP_PKEY
*r
)
323 if (crl
->meth
->crl_verify
)
324 return crl
->meth
->crl_verify(crl
, r
);
328 int X509_CRL_get0_by_serial(X509_CRL
*crl
,
329 X509_REVOKED
**ret
, ASN1_INTEGER
*serial
)
331 if (crl
->meth
->crl_lookup
)
332 return crl
->meth
->crl_lookup(crl
, ret
, serial
, NULL
);
336 int X509_CRL_get0_by_cert(X509_CRL
*crl
, X509_REVOKED
**ret
, X509
*x
)
338 if (crl
->meth
->crl_lookup
)
339 return crl
->meth
->crl_lookup(crl
, ret
,
340 X509_get_serialNumber(x
),
341 X509_get_issuer_name(x
));
345 static int def_crl_verify(X509_CRL
*crl
, EVP_PKEY
*r
)
347 return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO
),
348 &crl
->sig_alg
, &crl
->signature
, &crl
->crl
, r
));
351 static int crl_revoked_issuer_match(X509_CRL
*crl
, X509_NAME
*nm
,
359 if (!X509_NAME_cmp(nm
, X509_CRL_get_issuer(crl
)))
365 nm
= X509_CRL_get_issuer(crl
);
367 for (i
= 0; i
< sk_GENERAL_NAME_num(rev
->issuer
); i
++) {
368 GENERAL_NAME
*gen
= sk_GENERAL_NAME_value(rev
->issuer
, i
);
369 if (gen
->type
!= GEN_DIRNAME
)
371 if (!X509_NAME_cmp(nm
, gen
->d
.directoryName
))
378 static int def_crl_lookup(X509_CRL
*crl
,
379 X509_REVOKED
**ret
, ASN1_INTEGER
*serial
,
382 X509_REVOKED rtmp
, *rev
;
384 rtmp
.serialNumber
= *serial
;
386 * Sort revoked into serial number order if not already sorted. Do this
387 * under a lock to avoid race condition.
389 if (!sk_X509_REVOKED_is_sorted(crl
->crl
.revoked
)) {
390 CRYPTO_THREAD_write_lock(crl
->lock
);
391 sk_X509_REVOKED_sort(crl
->crl
.revoked
);
392 CRYPTO_THREAD_unlock(crl
->lock
);
394 idx
= sk_X509_REVOKED_find(crl
->crl
.revoked
, &rtmp
);
397 /* Need to look for matching name */
398 for (; idx
< sk_X509_REVOKED_num(crl
->crl
.revoked
); idx
++) {
399 rev
= sk_X509_REVOKED_value(crl
->crl
.revoked
, idx
);
400 if (ASN1_INTEGER_cmp(&rev
->serialNumber
, serial
))
402 if (crl_revoked_issuer_match(crl
, issuer
, rev
)) {
405 if (rev
->reason
== CRL_REASON_REMOVE_FROM_CRL
)
413 void X509_CRL_set_default_method(const X509_CRL_METHOD
*meth
)
416 default_crl_method
= &int_crl_meth
;
418 default_crl_method
= meth
;
421 X509_CRL_METHOD
*X509_CRL_METHOD_new(int (*crl_init
) (X509_CRL
*crl
),
422 int (*crl_free
) (X509_CRL
*crl
),
423 int (*crl_lookup
) (X509_CRL
*crl
,
427 int (*crl_verify
) (X509_CRL
*crl
,
431 m
= OPENSSL_malloc(sizeof(*m
));
434 m
->crl_init
= crl_init
;
435 m
->crl_free
= crl_free
;
436 m
->crl_lookup
= crl_lookup
;
437 m
->crl_verify
= crl_verify
;
438 m
->flags
= X509_CRL_METHOD_DYNAMIC
;
442 void X509_CRL_METHOD_free(X509_CRL_METHOD
*m
)
444 if (!(m
->flags
& X509_CRL_METHOD_DYNAMIC
))
449 void X509_CRL_set_meth_data(X509_CRL
*crl
, void *dat
)
451 crl
->meth_data
= dat
;
454 void *X509_CRL_get_meth_data(X509_CRL
*crl
)
456 return crl
->meth_data
;