2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "crypto/x509.h"
15 #include <openssl/x509v3.h>
16 #include "x509_local.h"
18 DEFINE_STACK_OF(GENERAL_NAME
)
19 DEFINE_STACK_OF(GENERAL_NAMES
)
20 DEFINE_STACK_OF(X509_REVOKED
)
21 DEFINE_STACK_OF(X509_EXTENSION
)
23 static int X509_REVOKED_cmp(const X509_REVOKED
*const *a
,
24 const X509_REVOKED
*const *b
);
25 static int setup_idp(X509_CRL
*crl
, ISSUING_DIST_POINT
*idp
);
27 ASN1_SEQUENCE(X509_REVOKED
) = {
28 ASN1_EMBED(X509_REVOKED
,serialNumber
, ASN1_INTEGER
),
29 ASN1_SIMPLE(X509_REVOKED
,revocationDate
, ASN1_TIME
),
30 ASN1_SEQUENCE_OF_OPT(X509_REVOKED
,extensions
, X509_EXTENSION
)
31 } ASN1_SEQUENCE_END(X509_REVOKED
)
33 static int def_crl_verify(X509_CRL
*crl
, EVP_PKEY
*r
);
34 static int def_crl_lookup(X509_CRL
*crl
,
35 X509_REVOKED
**ret
, const ASN1_INTEGER
*serial
,
36 const X509_NAME
*issuer
);
38 static X509_CRL_METHOD int_crl_meth
= {
45 static const X509_CRL_METHOD
*default_crl_method
= &int_crl_meth
;
48 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
49 * the original encoding the signature won't be affected by reordering of the
52 static int crl_inf_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
55 X509_CRL_INFO
*a
= (X509_CRL_INFO
*)*pval
;
57 if (!a
|| !a
->revoked
)
61 * Just set cmp function here. We don't sort because that would
62 * affect the output of X509_CRL_print().
64 case ASN1_OP_D2I_POST
:
65 (void)sk_X509_REVOKED_set_cmp_func(a
->revoked
, X509_REVOKED_cmp
);
72 ASN1_SEQUENCE_enc(X509_CRL_INFO
, enc
, crl_inf_cb
) = {
73 ASN1_OPT(X509_CRL_INFO
, version
, ASN1_INTEGER
),
74 ASN1_EMBED(X509_CRL_INFO
, sig_alg
, X509_ALGOR
),
75 ASN1_SIMPLE(X509_CRL_INFO
, issuer
, X509_NAME
),
76 ASN1_SIMPLE(X509_CRL_INFO
, lastUpdate
, ASN1_TIME
),
77 ASN1_OPT(X509_CRL_INFO
, nextUpdate
, ASN1_TIME
),
78 ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO
, revoked
, X509_REVOKED
),
79 ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO
, extensions
, X509_EXTENSION
, 0)
80 } ASN1_SEQUENCE_END_enc(X509_CRL_INFO
, X509_CRL_INFO
)
83 * Set CRL entry issuer according to CRL certificate issuer extension. Check
84 * for unhandled critical CRL entry extensions.
87 static int crl_set_issuers(X509_CRL
*crl
)
91 GENERAL_NAMES
*gens
, *gtmp
;
92 STACK_OF(X509_REVOKED
) *revoked
;
94 revoked
= X509_CRL_get_REVOKED(crl
);
97 for (i
= 0; i
< sk_X509_REVOKED_num(revoked
); i
++) {
98 X509_REVOKED
*rev
= sk_X509_REVOKED_value(revoked
, i
);
99 STACK_OF(X509_EXTENSION
) *exts
;
100 ASN1_ENUMERATED
*reason
;
102 gtmp
= X509_REVOKED_get_ext_d2i(rev
,
103 NID_certificate_issuer
, &j
, NULL
);
104 if (!gtmp
&& (j
!= -1)) {
105 crl
->flags
|= EXFLAG_INVALID
;
112 crl
->issuers
= sk_GENERAL_NAMES_new_null();
116 if (!sk_GENERAL_NAMES_push(crl
->issuers
, gtmp
))
121 reason
= X509_REVOKED_get_ext_d2i(rev
, NID_crl_reason
, &j
, NULL
);
122 if (!reason
&& (j
!= -1)) {
123 crl
->flags
|= EXFLAG_INVALID
;
128 rev
->reason
= ASN1_ENUMERATED_get(reason
);
129 ASN1_ENUMERATED_free(reason
);
131 rev
->reason
= CRL_REASON_NONE
;
133 /* Check for critical CRL entry extensions */
135 exts
= rev
->extensions
;
137 for (j
= 0; j
< sk_X509_EXTENSION_num(exts
); j
++) {
138 ext
= sk_X509_EXTENSION_value(exts
, j
);
139 if (X509_EXTENSION_get_critical(ext
)) {
140 if (OBJ_obj2nid(X509_EXTENSION_get_object(ext
)) == NID_certificate_issuer
)
142 crl
->flags
|= EXFLAG_CRITICAL
;
154 * The X509_CRL structure needs a bit of customisation. Cache some extensions
155 * and hash of the whole CRL.
157 static int crl_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
160 X509_CRL
*crl
= (X509_CRL
*)*pval
;
161 STACK_OF(X509_EXTENSION
) *exts
;
166 case ASN1_OP_D2I_PRE
:
167 if (crl
->meth
->crl_free
) {
168 if (!crl
->meth
->crl_free(crl
))
171 AUTHORITY_KEYID_free(crl
->akid
);
172 ISSUING_DIST_POINT_free(crl
->idp
);
173 ASN1_INTEGER_free(crl
->crl_number
);
174 ASN1_INTEGER_free(crl
->base_crl_number
);
175 sk_GENERAL_NAMES_pop_free(crl
->issuers
, GENERAL_NAMES_free
);
178 case ASN1_OP_NEW_POST
:
183 crl
->idp_reasons
= CRLDP_ALL_REASONS
;
184 crl
->meth
= default_crl_method
;
185 crl
->meth_data
= NULL
;
187 crl
->crl_number
= NULL
;
188 crl
->base_crl_number
= NULL
;
191 case ASN1_OP_D2I_POST
:
192 if (!X509_CRL_digest(crl
, EVP_sha1(), crl
->sha1_hash
, NULL
))
193 crl
->flags
|= EXFLAG_INVALID
;
194 crl
->idp
= X509_CRL_get_ext_d2i(crl
,
195 NID_issuing_distribution_point
, &i
,
197 if (crl
->idp
!= NULL
) {
198 if (!setup_idp(crl
, crl
->idp
))
199 crl
->flags
|= EXFLAG_INVALID
;
202 crl
->flags
|= EXFLAG_INVALID
;
205 crl
->akid
= X509_CRL_get_ext_d2i(crl
,
206 NID_authority_key_identifier
, &i
,
208 if (crl
->akid
== NULL
&& i
!= -1)
209 crl
->flags
|= EXFLAG_INVALID
;
211 crl
->crl_number
= X509_CRL_get_ext_d2i(crl
,
212 NID_crl_number
, &i
, NULL
);
213 if (crl
->crl_number
== NULL
&& i
!= -1)
214 crl
->flags
|= EXFLAG_INVALID
;
216 crl
->base_crl_number
= X509_CRL_get_ext_d2i(crl
,
219 if (crl
->base_crl_number
== NULL
&& i
!= -1)
220 crl
->flags
|= EXFLAG_INVALID
;
221 /* Delta CRLs must have CRL number */
222 if (crl
->base_crl_number
&& !crl
->crl_number
)
223 crl
->flags
|= EXFLAG_INVALID
;
226 * See if we have any unhandled critical CRL extensions and indicate
227 * this in a flag. We only currently handle IDP so anything else
228 * critical sets the flag. This code accesses the X509_CRL structure
229 * directly: applications shouldn't do this.
232 exts
= crl
->crl
.extensions
;
234 for (idx
= 0; idx
< sk_X509_EXTENSION_num(exts
); idx
++) {
236 ext
= sk_X509_EXTENSION_value(exts
, idx
);
237 nid
= OBJ_obj2nid(X509_EXTENSION_get_object(ext
));
238 if (nid
== NID_freshest_crl
)
239 crl
->flags
|= EXFLAG_FRESHEST
;
240 if (X509_EXTENSION_get_critical(ext
)) {
241 /* We handle IDP and deltas */
242 if ((nid
== NID_issuing_distribution_point
)
243 || (nid
== NID_authority_key_identifier
)
244 || (nid
== NID_delta_crl
))
246 crl
->flags
|= EXFLAG_CRITICAL
;
251 if (!crl_set_issuers(crl
))
254 if (crl
->meth
->crl_init
) {
255 if (crl
->meth
->crl_init(crl
) == 0)
259 crl
->flags
|= EXFLAG_SET
;
262 case ASN1_OP_FREE_POST
:
263 if (crl
->meth
->crl_free
) {
264 if (!crl
->meth
->crl_free(crl
))
267 AUTHORITY_KEYID_free(crl
->akid
);
268 ISSUING_DIST_POINT_free(crl
->idp
);
269 ASN1_INTEGER_free(crl
->crl_number
);
270 ASN1_INTEGER_free(crl
->base_crl_number
);
271 sk_GENERAL_NAMES_pop_free(crl
->issuers
, GENERAL_NAMES_free
);
277 /* Convert IDP into a more convenient form */
279 static int setup_idp(X509_CRL
*crl
, ISSUING_DIST_POINT
*idp
)
283 /* Set various flags according to IDP */
284 crl
->idp_flags
|= IDP_PRESENT
;
285 if (idp
->onlyuser
> 0) {
287 crl
->idp_flags
|= IDP_ONLYUSER
;
289 if (idp
->onlyCA
> 0) {
291 crl
->idp_flags
|= IDP_ONLYCA
;
293 if (idp
->onlyattr
> 0) {
295 crl
->idp_flags
|= IDP_ONLYATTR
;
299 crl
->idp_flags
|= IDP_INVALID
;
301 if (idp
->indirectCRL
> 0)
302 crl
->idp_flags
|= IDP_INDIRECT
;
304 if (idp
->onlysomereasons
) {
305 crl
->idp_flags
|= IDP_REASONS
;
306 if (idp
->onlysomereasons
->length
> 0)
307 crl
->idp_reasons
= idp
->onlysomereasons
->data
[0];
308 if (idp
->onlysomereasons
->length
> 1)
309 crl
->idp_reasons
|= (idp
->onlysomereasons
->data
[1] << 8);
310 crl
->idp_reasons
&= CRLDP_ALL_REASONS
;
313 return DIST_POINT_set_dpname(idp
->distpoint
, X509_CRL_get_issuer(crl
));
316 ASN1_SEQUENCE_ref(X509_CRL
, crl_cb
) = {
317 ASN1_EMBED(X509_CRL
, crl
, X509_CRL_INFO
),
318 ASN1_EMBED(X509_CRL
, sig_alg
, X509_ALGOR
),
319 ASN1_EMBED(X509_CRL
, signature
, ASN1_BIT_STRING
)
320 } ASN1_SEQUENCE_END_ref(X509_CRL
, X509_CRL
)
322 IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED
)
324 IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED
)
326 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO
)
328 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL
)
330 IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL
)
332 static int X509_REVOKED_cmp(const X509_REVOKED
*const *a
,
333 const X509_REVOKED
*const *b
)
335 return (ASN1_STRING_cmp((ASN1_STRING
*)&(*a
)->serialNumber
,
336 (ASN1_STRING
*)&(*b
)->serialNumber
));
339 int X509_CRL_add0_revoked(X509_CRL
*crl
, X509_REVOKED
*rev
)
344 if (inf
->revoked
== NULL
)
345 inf
->revoked
= sk_X509_REVOKED_new(X509_REVOKED_cmp
);
346 if (inf
->revoked
== NULL
|| !sk_X509_REVOKED_push(inf
->revoked
, rev
)) {
347 ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED
, ERR_R_MALLOC_FAILURE
);
350 inf
->enc
.modified
= 1;
354 int X509_CRL_verify(X509_CRL
*crl
, EVP_PKEY
*r
)
356 if (crl
->meth
->crl_verify
)
357 return crl
->meth
->crl_verify(crl
, r
);
361 int X509_CRL_get0_by_serial(X509_CRL
*crl
,
362 X509_REVOKED
**ret
, const ASN1_INTEGER
*serial
)
364 if (crl
->meth
->crl_lookup
)
365 return crl
->meth
->crl_lookup(crl
, ret
, serial
, NULL
);
369 int X509_CRL_get0_by_cert(X509_CRL
*crl
, X509_REVOKED
**ret
, X509
*x
)
371 if (crl
->meth
->crl_lookup
)
372 return crl
->meth
->crl_lookup(crl
, ret
,
373 X509_get_serialNumber(x
),
374 X509_get_issuer_name(x
));
378 static int def_crl_verify(X509_CRL
*crl
, EVP_PKEY
*r
)
380 return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO
),
381 &crl
->sig_alg
, &crl
->signature
, &crl
->crl
, r
));
384 static int crl_revoked_issuer_match(X509_CRL
*crl
, const X509_NAME
*nm
,
392 if (!X509_NAME_cmp(nm
, X509_CRL_get_issuer(crl
)))
398 nm
= X509_CRL_get_issuer(crl
);
400 for (i
= 0; i
< sk_GENERAL_NAME_num(rev
->issuer
); i
++) {
401 GENERAL_NAME
*gen
= sk_GENERAL_NAME_value(rev
->issuer
, i
);
402 if (gen
->type
!= GEN_DIRNAME
)
404 if (!X509_NAME_cmp(nm
, gen
->d
.directoryName
))
411 static int def_crl_lookup(X509_CRL
*crl
,
412 X509_REVOKED
**ret
, const ASN1_INTEGER
*serial
,
413 const X509_NAME
*issuer
)
415 X509_REVOKED rtmp
, *rev
;
418 if (crl
->crl
.revoked
== NULL
)
422 * Sort revoked into serial number order if not already sorted. Do this
423 * under a lock to avoid race condition.
425 if (!sk_X509_REVOKED_is_sorted(crl
->crl
.revoked
)) {
426 CRYPTO_THREAD_write_lock(crl
->lock
);
427 sk_X509_REVOKED_sort(crl
->crl
.revoked
);
428 CRYPTO_THREAD_unlock(crl
->lock
);
430 rtmp
.serialNumber
= *serial
;
431 idx
= sk_X509_REVOKED_find(crl
->crl
.revoked
, &rtmp
);
434 /* Need to look for matching name */
435 for (num
= sk_X509_REVOKED_num(crl
->crl
.revoked
); idx
< num
; idx
++) {
436 rev
= sk_X509_REVOKED_value(crl
->crl
.revoked
, idx
);
437 if (ASN1_INTEGER_cmp(&rev
->serialNumber
, serial
))
439 if (crl_revoked_issuer_match(crl
, issuer
, rev
)) {
442 if (rev
->reason
== CRL_REASON_REMOVE_FROM_CRL
)
450 void X509_CRL_set_default_method(const X509_CRL_METHOD
*meth
)
453 default_crl_method
= &int_crl_meth
;
455 default_crl_method
= meth
;
458 X509_CRL_METHOD
*X509_CRL_METHOD_new(int (*crl_init
) (X509_CRL
*crl
),
459 int (*crl_free
) (X509_CRL
*crl
),
460 int (*crl_lookup
) (X509_CRL
*crl
,
462 const ASN1_INTEGER
*ser
,
463 const X509_NAME
*issuer
),
464 int (*crl_verify
) (X509_CRL
*crl
,
467 X509_CRL_METHOD
*m
= OPENSSL_malloc(sizeof(*m
));
470 X509err(X509_F_X509_CRL_METHOD_NEW
, ERR_R_MALLOC_FAILURE
);
473 m
->crl_init
= crl_init
;
474 m
->crl_free
= crl_free
;
475 m
->crl_lookup
= crl_lookup
;
476 m
->crl_verify
= crl_verify
;
477 m
->flags
= X509_CRL_METHOD_DYNAMIC
;
481 void X509_CRL_METHOD_free(X509_CRL_METHOD
*m
)
483 if (m
== NULL
|| !(m
->flags
& X509_CRL_METHOD_DYNAMIC
))
488 void X509_CRL_set_meth_data(X509_CRL
*crl
, void *dat
)
490 crl
->meth_data
= dat
;
493 void *X509_CRL_get_meth_data(X509_CRL
*crl
)
495 return crl
->meth_data
;