]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x_crl.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / x509 / x_crl.c
CommitLineData
b1322259 1/*
6ec5fce2 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
9d6b1ce6 12#include <openssl/asn1t.h>
ec577822 13#include <openssl/x509.h>
25f2138b 14#include "crypto/x509.h"
edc54021 15#include <openssl/x509v3.h>
706457b7 16#include "x509_local.h"
d02b48c6 17
0f113f3e
MC
18static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
19 const X509_REVOKED *const *b);
4d50a2b4 20static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
d02b48c6 21
9d6b1ce6 22ASN1_SEQUENCE(X509_REVOKED) = {
34a42e14 23 ASN1_EMBED(X509_REVOKED,serialNumber, ASN1_INTEGER),
0f113f3e
MC
24 ASN1_SIMPLE(X509_REVOKED,revocationDate, ASN1_TIME),
25 ASN1_SEQUENCE_OF_OPT(X509_REVOKED,extensions, X509_EXTENSION)
d339187b 26} ASN1_SEQUENCE_END(X509_REVOKED)
9d6b1ce6 27
f4c630ab
DSH
28static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
29static int def_crl_lookup(X509_CRL *crl,
0f113f3e
MC
30 X509_REVOKED **ret, ASN1_INTEGER *serial,
31 X509_NAME *issuer);
f4c630ab 32
0f113f3e
MC
33static X509_CRL_METHOD int_crl_meth = {
34 0,
35 0, 0,
36 def_crl_lookup,
37 def_crl_verify
38};
f4c630ab
DSH
39
40static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
41
0f113f3e
MC
42/*
43 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
60250017 44 * the original encoding the signature won't be affected by reordering of the
0f113f3e 45 * revoked field.
9d6b1ce6 46 */
24484759 47static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e 48 void *exarg)
9d6b1ce6 49{
0f113f3e
MC
50 X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
51
52 if (!a || !a->revoked)
53 return 1;
54 switch (operation) {
55 /*
56 * Just set cmp function here. We don't sort because that would
57 * affect the output of X509_CRL_print().
58 */
59 case ASN1_OP_D2I_POST:
60 (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
61 break;
62 }
63 return 1;
9d6b1ce6
DSH
64}
65
66
2f605e8d 67ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
0f113f3e 68 ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
6e63c142 69 ASN1_EMBED(X509_CRL_INFO, sig_alg, X509_ALGOR),
0f113f3e
MC
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)
2f605e8d 75} ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
9d6b1ce6 76
0f113f3e
MC
77/*
78 * Set CRL entry issuer according to CRL certificate issuer extension. Check
79 * for unhandled critical CRL entry extensions.
d0fff69d
DSH
80 */
81
82static int crl_set_issuers(X509_CRL *crl)
0f113f3e
MC
83{
84
85 int i, j;
86 GENERAL_NAMES *gens, *gtmp;
87 STACK_OF(X509_REVOKED) *revoked;
88
89 revoked = X509_CRL_get_REVOKED(crl);
90
91 gens = NULL;
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;
96 X509_EXTENSION *ext;
97 gtmp = X509_REVOKED_get_ext_d2i(rev,
98 NID_certificate_issuer, &j, NULL);
99 if (!gtmp && (j != -1)) {
100 crl->flags |= EXFLAG_INVALID;
101 return 1;
102 }
103
104 if (gtmp) {
105 gens = gtmp;
106 if (!crl->issuers) {
107 crl->issuers = sk_GENERAL_NAMES_new_null();
108 if (!crl->issuers)
109 return 0;
110 }
111 if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
112 return 0;
113 }
114 rev->issuer = gens;
115
116 reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
117 if (!reason && (j != -1)) {
118 crl->flags |= EXFLAG_INVALID;
119 return 1;
120 }
121
122 if (reason) {
123 rev->reason = ASN1_ENUMERATED_get(reason);
124 ASN1_ENUMERATED_free(reason);
125 } else
126 rev->reason = CRL_REASON_NONE;
127
128 /* Check for critical CRL entry extensions */
129
130 exts = rev->extensions;
131
132 for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
133 ext = sk_X509_EXTENSION_value(exts, j);
4903abd5
DSH
134 if (X509_EXTENSION_get_critical(ext)) {
135 if (OBJ_obj2nid(X509_EXTENSION_get_object(ext)) == NID_certificate_issuer)
0f113f3e
MC
136 continue;
137 crl->flags |= EXFLAG_CRITICAL;
138 break;
139 }
140 }
141
142 }
143
144 return 1;
145
146}
147
148/*
149 * The X509_CRL structure needs a bit of customisation. Cache some extensions
edc54021
DSH
150 * and hash of the whole CRL.
151 */
152static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e
MC
153 void *exarg)
154{
155 X509_CRL *crl = (X509_CRL *)*pval;
156 STACK_OF(X509_EXTENSION) *exts;
157 X509_EXTENSION *ext;
158 int idx;
159
160 switch (operation) {
a7276279
BE
161 case ASN1_OP_D2I_PRE:
162 if (crl->meth->crl_free) {
163 if (!crl->meth->crl_free(crl))
164 return 0;
165 }
166 AUTHORITY_KEYID_free(crl->akid);
167 ISSUING_DIST_POINT_free(crl->idp);
168 ASN1_INTEGER_free(crl->crl_number);
169 ASN1_INTEGER_free(crl->base_crl_number);
170 sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
171 /* fall thru */
172
0f113f3e
MC
173 case ASN1_OP_NEW_POST:
174 crl->idp = NULL;
175 crl->akid = NULL;
176 crl->flags = 0;
177 crl->idp_flags = 0;
178 crl->idp_reasons = CRLDP_ALL_REASONS;
179 crl->meth = default_crl_method;
180 crl->meth_data = NULL;
181 crl->issuers = NULL;
182 crl->crl_number = NULL;
183 crl->base_crl_number = NULL;
184 break;
185
186 case ASN1_OP_D2I_POST:
0f113f3e 187 X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
0f113f3e
MC
188 crl->idp = X509_CRL_get_ext_d2i(crl,
189 NID_issuing_distribution_point, NULL,
190 NULL);
191 if (crl->idp)
192 setup_idp(crl, crl->idp);
193
194 crl->akid = X509_CRL_get_ext_d2i(crl,
195 NID_authority_key_identifier, NULL,
196 NULL);
197
198 crl->crl_number = X509_CRL_get_ext_d2i(crl,
199 NID_crl_number, NULL, NULL);
200
201 crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
202 NID_delta_crl, NULL,
203 NULL);
204 /* Delta CRLs must have CRL number */
205 if (crl->base_crl_number && !crl->crl_number)
206 crl->flags |= EXFLAG_INVALID;
207
208 /*
209 * See if we have any unhandled critical CRL extensions and indicate
210 * this in a flag. We only currently handle IDP so anything else
211 * critical sets the flag. This code accesses the X509_CRL structure
212 * directly: applications shouldn't do this.
213 */
214
7aef39a7 215 exts = crl->crl.extensions;
0f113f3e
MC
216
217 for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
218 int nid;
219 ext = sk_X509_EXTENSION_value(exts, idx);
4903abd5 220 nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
0f113f3e
MC
221 if (nid == NID_freshest_crl)
222 crl->flags |= EXFLAG_FRESHEST;
4903abd5 223 if (X509_EXTENSION_get_critical(ext)) {
0f113f3e
MC
224 /* We handle IDP and deltas */
225 if ((nid == NID_issuing_distribution_point)
226 || (nid == NID_authority_key_identifier)
227 || (nid == NID_delta_crl))
2b406990 228 continue;
0f113f3e
MC
229 crl->flags |= EXFLAG_CRITICAL;
230 break;
231 }
232 }
233
234 if (!crl_set_issuers(crl))
235 return 0;
236
237 if (crl->meth->crl_init) {
238 if (crl->meth->crl_init(crl) == 0)
239 return 0;
240 }
6195848b
RL
241
242 crl->flags |= EXFLAG_SET;
0f113f3e
MC
243 break;
244
245 case ASN1_OP_FREE_POST:
246 if (crl->meth->crl_free) {
247 if (!crl->meth->crl_free(crl))
248 return 0;
249 }
25aaa98a
RS
250 AUTHORITY_KEYID_free(crl->akid);
251 ISSUING_DIST_POINT_free(crl->idp);
0f113f3e
MC
252 ASN1_INTEGER_free(crl->crl_number);
253 ASN1_INTEGER_free(crl->base_crl_number);
254 sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
255 break;
256 }
257 return 1;
258}
edc54021 259
4d50a2b4
DSH
260/* Convert IDP into a more convenient form */
261
262static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
0f113f3e
MC
263{
264 int idp_only = 0;
265 /* Set various flags according to IDP */
266 crl->idp_flags |= IDP_PRESENT;
267 if (idp->onlyuser > 0) {
268 idp_only++;
269 crl->idp_flags |= IDP_ONLYUSER;
270 }
271 if (idp->onlyCA > 0) {
272 idp_only++;
273 crl->idp_flags |= IDP_ONLYCA;
274 }
275 if (idp->onlyattr > 0) {
276 idp_only++;
277 crl->idp_flags |= IDP_ONLYATTR;
278 }
279
280 if (idp_only > 1)
281 crl->idp_flags |= IDP_INVALID;
282
283 if (idp->indirectCRL > 0)
284 crl->idp_flags |= IDP_INDIRECT;
285
286 if (idp->onlysomereasons) {
287 crl->idp_flags |= IDP_REASONS;
288 if (idp->onlysomereasons->length > 0)
289 crl->idp_reasons = idp->onlysomereasons->data[0];
290 if (idp->onlysomereasons->length > 1)
291 crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
292 crl->idp_reasons &= CRLDP_ALL_REASONS;
293 }
294
295 DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
296}
4d50a2b4 297
c001ce33 298ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
7aef39a7 299 ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
6e63c142 300 ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
34a42e14 301 ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
d339187b 302} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
9d6b1ce6
DSH
303
304IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
0f113f3e 305
2e8cb108 306IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
0f113f3e 307
9d6b1ce6 308IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
739a5eee 309
9d6b1ce6 310IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
739a5eee 311
1241126a 312IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
d02b48c6 313
0f113f3e
MC
314static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
315 const X509_REVOKED *const *b)
316{
34a42e14
DSH
317 return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
318 (ASN1_STRING *)&(*b)->serialNumber));
0f113f3e 319}
d02b48c6 320
9d6b1ce6
DSH
321int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
322{
0f113f3e 323 X509_CRL_INFO *inf;
7fcdbd83 324
7aef39a7 325 inf = &crl->crl;
90945fa3 326 if (inf->revoked == NULL)
0f113f3e 327 inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
90945fa3 328 if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
0f113f3e
MC
329 ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
330 return 0;
331 }
332 inf->enc.modified = 1;
333 return 1;
9d6b1ce6
DSH
334}
335
010fa0b3 336int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
0f113f3e
MC
337{
338 if (crl->meth->crl_verify)
339 return crl->meth->crl_verify(crl, r);
340 return 0;
341}
f4c630ab
DSH
342
343int X509_CRL_get0_by_serial(X509_CRL *crl,
0f113f3e
MC
344 X509_REVOKED **ret, ASN1_INTEGER *serial)
345{
346 if (crl->meth->crl_lookup)
347 return crl->meth->crl_lookup(crl, ret, serial, NULL);
348 return 0;
349}
d0fff69d
DSH
350
351int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
0f113f3e
MC
352{
353 if (crl->meth->crl_lookup)
354 return crl->meth->crl_lookup(crl, ret,
355 X509_get_serialNumber(x),
356 X509_get_issuer_name(x));
357 return 0;
358}
f4c630ab
DSH
359
360static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
0f113f3e
MC
361{
362 return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
34a42e14 363 &crl->sig_alg, &crl->signature, &crl->crl, r));
0f113f3e 364}
010fa0b3 365
d0fff69d 366static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
0f113f3e
MC
367 X509_REVOKED *rev)
368{
369 int i;
370
371 if (!rev->issuer) {
372 if (!nm)
373 return 1;
374 if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
375 return 1;
376 return 0;
377 }
378
379 if (!nm)
380 nm = X509_CRL_get_issuer(crl);
381
382 for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
383 GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
384 if (gen->type != GEN_DIRNAME)
385 continue;
386 if (!X509_NAME_cmp(nm, gen->d.directoryName))
387 return 1;
388 }
389 return 0;
390
391}
d0fff69d 392
f4c630ab 393static int def_crl_lookup(X509_CRL *crl,
0f113f3e
MC
394 X509_REVOKED **ret, ASN1_INTEGER *serial,
395 X509_NAME *issuer)
396{
397 X509_REVOKED rtmp, *rev;
5b37fef0
AP
398 int idx, num;
399
400 if (crl->crl.revoked == NULL)
401 return 0;
402
0f113f3e
MC
403 /*
404 * Sort revoked into serial number order if not already sorted. Do this
405 * under a lock to avoid race condition.
406 */
7aef39a7 407 if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
c001ce33 408 CRYPTO_THREAD_write_lock(crl->lock);
7aef39a7 409 sk_X509_REVOKED_sort(crl->crl.revoked);
c001ce33 410 CRYPTO_THREAD_unlock(crl->lock);
0f113f3e 411 }
5b37fef0 412 rtmp.serialNumber = *serial;
7aef39a7 413 idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
0f113f3e
MC
414 if (idx < 0)
415 return 0;
416 /* Need to look for matching name */
5b37fef0 417 for (num = sk_X509_REVOKED_num(crl->crl.revoked); idx < num; idx++) {
7aef39a7 418 rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
34a42e14 419 if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
0f113f3e
MC
420 return 0;
421 if (crl_revoked_issuer_match(crl, issuer, rev)) {
422 if (ret)
423 *ret = rev;
424 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
425 return 2;
426 return 1;
427 }
428 }
429 return 0;
430}
010fa0b3 431
f4c630ab 432void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
0f113f3e
MC
433{
434 if (meth == NULL)
435 default_crl_method = &int_crl_meth;
436 else
437 default_crl_method = meth;
438}
439
440X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl),
441 int (*crl_free) (X509_CRL *crl),
442 int (*crl_lookup) (X509_CRL *crl,
443 X509_REVOKED **ret,
444 ASN1_INTEGER *ser,
445 X509_NAME *issuer),
446 int (*crl_verify) (X509_CRL *crl,
447 EVP_PKEY *pk))
448{
7fcdbd83
F
449 X509_CRL_METHOD *m = OPENSSL_malloc(sizeof(*m));
450
451 if (m == NULL) {
452 X509err(X509_F_X509_CRL_METHOD_NEW, ERR_R_MALLOC_FAILURE);
0f113f3e 453 return NULL;
7fcdbd83 454 }
0f113f3e
MC
455 m->crl_init = crl_init;
456 m->crl_free = crl_free;
457 m->crl_lookup = crl_lookup;
458 m->crl_verify = crl_verify;
459 m->flags = X509_CRL_METHOD_DYNAMIC;
460 return m;
461}
f4c630ab
DSH
462
463void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
0f113f3e 464{
7cb1ecec 465 if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
0f113f3e
MC
466 return;
467 OPENSSL_free(m);
468}
f4c630ab
DSH
469
470void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
0f113f3e
MC
471{
472 crl->meth_data = dat;
473}
f4c630ab
DSH
474
475void *X509_CRL_get_meth_data(X509_CRL *crl)
0f113f3e
MC
476{
477 return crl->meth_data;
478}