1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58 #include <openssl/asn1t.h>
59 #include <openssl/digest.h>
60 #include <openssl/err.h>
61 #include <openssl/mem.h>
62 #include <openssl/obj.h>
63 #include <openssl/stack.h>
64 #include <openssl/thread.h>
65 #include <openssl/x509.h>
66 #include <openssl/x509v3.h>
67
68 #include "../internal.h"
69 #include "internal.h"
70
71 static int X509_REVOKED_cmp(const X509_REVOKED **a, const X509_REVOKED **b);
72 static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
73
74 ASN1_SEQUENCE(X509_REVOKED) = {
75 ASN1_SIMPLE(X509_REVOKED,serialNumber, ASN1_INTEGER),
76 ASN1_SIMPLE(X509_REVOKED,revocationDate, ASN1_TIME),
77 ASN1_SEQUENCE_OF_OPT(X509_REVOKED,extensions, X509_EXTENSION)
78 } ASN1_SEQUENCE_END(X509_REVOKED)
79
80 static int crl_lookup(X509_CRL *crl, X509_REVOKED **ret, ASN1_INTEGER *serial,
81 X509_NAME *issuer);
82
83 /*
84 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
85 * the original encoding the signature wont be affected by reordering of the
86 * revoked field.
87 */
crl_inf_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)88 static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
89 void *exarg)
90 {
91 X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
92
93 if (!a || !a->revoked)
94 return 1;
95 switch (operation) {
96 /*
97 * Just set cmp function here. We don't sort because that would
98 * affect the output of X509_CRL_print().
99 */
100 case ASN1_OP_D2I_POST:
101 (void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
102 break;
103 }
104 return 1;
105 }
106
107
108 ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
109 ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
110 ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR),
111 ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
112 ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
113 ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
114 ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
115 ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
116 } ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
117
118 /*
119 * Set CRL entry issuer according to CRL certificate issuer extension. Check
120 * for unhandled critical CRL entry extensions.
121 */
122
123 static int crl_set_issuers(X509_CRL *crl)
124 {
125
126 size_t i, k;
127 int j;
128 GENERAL_NAMES *gens, *gtmp;
129 STACK_OF(X509_REVOKED) *revoked;
130
131 revoked = X509_CRL_get_REVOKED(crl);
132
133 gens = NULL;
134 for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
135 X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
136 STACK_OF(X509_EXTENSION) *exts;
137 ASN1_ENUMERATED *reason;
138 X509_EXTENSION *ext;
139 gtmp = X509_REVOKED_get_ext_d2i(rev,
140 NID_certificate_issuer, &j, NULL);
141 if (!gtmp && (j != -1)) {
142 crl->flags |= EXFLAG_INVALID;
143 return 1;
144 }
145
146 if (gtmp) {
147 gens = gtmp;
148 if (!crl->issuers) {
149 crl->issuers = sk_GENERAL_NAMES_new_null();
150 if (!crl->issuers)
151 return 0;
152 }
153 if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
154 return 0;
155 }
156 rev->issuer = gens;
157
158 reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason, &j, NULL);
159 if (!reason && (j != -1)) {
160 crl->flags |= EXFLAG_INVALID;
161 return 1;
162 }
163
164 if (reason) {
165 rev->reason = ASN1_ENUMERATED_get(reason);
166 ASN1_ENUMERATED_free(reason);
167 } else
168 rev->reason = CRL_REASON_NONE;
169
170 /* Check for critical CRL entry extensions */
171
172 exts = rev->extensions;
173
174 for (k = 0; k < sk_X509_EXTENSION_num(exts); k++) {
175 ext = sk_X509_EXTENSION_value(exts, k);
176 if (X509_EXTENSION_get_critical(ext)) {
177 if (OBJ_obj2nid(X509_EXTENSION_get_object(ext)) ==
178 NID_certificate_issuer)
179 continue;
180 crl->flags |= EXFLAG_CRITICAL;
181 break;
182 }
183 }
184
185 }
186
187 return 1;
188
189 }
190
191 /*
192 * The X509_CRL structure needs a bit of customisation. Cache some extensions
193 * and hash of the whole CRL.
194 */
crl_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)195 static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
196 void *exarg)
197 {
198 X509_CRL *crl = (X509_CRL *)*pval;
199 STACK_OF(X509_EXTENSION) *exts;
200 X509_EXTENSION *ext;
201 size_t idx;
202 int i;
203
204 switch (operation) {
205 case ASN1_OP_NEW_POST:
206 crl->idp = NULL;
207 crl->akid = NULL;
208 crl->flags = 0;
209 crl->idp_flags = 0;
210 crl->idp_reasons = CRLDP_ALL_REASONS;
211 crl->issuers = NULL;
212 crl->crl_number = NULL;
213 crl->base_crl_number = NULL;
214 break;
215
216 case ASN1_OP_D2I_POST: {
217 /* The version must be one of v1(0) or v2(1). */
218 long version = X509_CRL_VERSION_1;
219 if (crl->crl->version != NULL) {
220 version = ASN1_INTEGER_get(crl->crl->version);
221 /* TODO(https://crbug.com/boringssl/364): |X509_CRL_VERSION_1|
222 * should also be rejected. This means an explicitly-encoded X.509v1
223 * version. v1 is DEFAULT, so DER requires it be omitted. */
224 if (version < X509_CRL_VERSION_1 || version > X509_CRL_VERSION_2) {
225 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
226 return 0;
227 }
228 }
229
230 /* Per RFC 5280, section 5.1.2.1, extensions require v2. */
231 if (version != X509_CRL_VERSION_2 && crl->crl->extensions != NULL) {
232 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_FOR_VERSION);
233 return 0;
234 }
235
236 if (!X509_CRL_digest(crl, EVP_sha256(), crl->crl_hash, NULL)) {
237 return 0;
238 }
239
240 crl->idp = X509_CRL_get_ext_d2i(crl,
241 NID_issuing_distribution_point, &i,
242 NULL);
243 if (crl->idp != NULL) {
244 if (!setup_idp(crl, crl->idp)) {
245 return 0;
246 }
247 } else if (i != -1) {
248 return 0;
249 }
250
251 crl->akid = X509_CRL_get_ext_d2i(crl,
252 NID_authority_key_identifier, &i,
253 NULL);
254 if (crl->akid == NULL && i != -1) {
255 return 0;
256 }
257
258 crl->crl_number = X509_CRL_get_ext_d2i(crl,
259 NID_crl_number, &i, NULL);
260 if (crl->crl_number == NULL && i != -1) {
261 return 0;
262 }
263
264 crl->base_crl_number = X509_CRL_get_ext_d2i(crl, NID_delta_crl, &i,
265 NULL);
266 if (crl->base_crl_number == NULL && i != -1) {
267 return 0;
268 }
269 /* Delta CRLs must have CRL number */
270 if (crl->base_crl_number && !crl->crl_number) {
271 OPENSSL_PUT_ERROR(X509, X509_R_DELTA_CRL_WITHOUT_CRL_NUMBER);
272 return 0;
273 }
274
275 /*
276 * See if we have any unhandled critical CRL extensions and indicate
277 * this in a flag. We only currently handle IDP so anything else
278 * critical sets the flag. This code accesses the X509_CRL structure
279 * directly: applications shouldn't do this.
280 */
281
282 exts = crl->crl->extensions;
283
284 for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
285 int nid;
286 ext = sk_X509_EXTENSION_value(exts, idx);
287 nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
288 if (nid == NID_freshest_crl)
289 crl->flags |= EXFLAG_FRESHEST;
290 if (X509_EXTENSION_get_critical(ext)) {
291 /* We handle IDP and deltas */
292 if ((nid == NID_issuing_distribution_point)
293 || (nid == NID_authority_key_identifier)
294 || (nid == NID_delta_crl))
295 continue;
296 crl->flags |= EXFLAG_CRITICAL;
297 break;
298 }
299 }
300
301 if (!crl_set_issuers(crl))
302 return 0;
303
304 break;
305 }
306
307 case ASN1_OP_FREE_POST:
308 AUTHORITY_KEYID_free(crl->akid);
309 ISSUING_DIST_POINT_free(crl->idp);
310 ASN1_INTEGER_free(crl->crl_number);
311 ASN1_INTEGER_free(crl->base_crl_number);
312 sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
313 break;
314 }
315 return 1;
316 }
317
318 /* Convert IDP into a more convenient form */
319
setup_idp(X509_CRL * crl,ISSUING_DIST_POINT * idp)320 static int setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
321 {
322 int idp_only = 0;
323 /* Set various flags according to IDP */
324 crl->idp_flags |= IDP_PRESENT;
325 if (idp->onlyuser > 0) {
326 idp_only++;
327 crl->idp_flags |= IDP_ONLYUSER;
328 }
329 if (idp->onlyCA > 0) {
330 idp_only++;
331 crl->idp_flags |= IDP_ONLYCA;
332 }
333 if (idp->onlyattr > 0) {
334 idp_only++;
335 crl->idp_flags |= IDP_ONLYATTR;
336 }
337
338 if (idp_only > 1)
339 crl->idp_flags |= IDP_INVALID;
340
341 if (idp->indirectCRL > 0)
342 crl->idp_flags |= IDP_INDIRECT;
343
344 if (idp->onlysomereasons) {
345 crl->idp_flags |= IDP_REASONS;
346 if (idp->onlysomereasons->length > 0)
347 crl->idp_reasons = idp->onlysomereasons->data[0];
348 if (idp->onlysomereasons->length > 1)
349 crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
350 crl->idp_reasons &= CRLDP_ALL_REASONS;
351 }
352
353 return DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
354 }
355
356 ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
357 ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
358 ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
359 ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING)
360 } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
361
362 IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
363
364 IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
365
366 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
367 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
368 IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
369
370 static int X509_REVOKED_cmp(const X509_REVOKED **a, const X509_REVOKED **b)
371 {
372 return ASN1_STRING_cmp((*a)->serialNumber, (*b)->serialNumber);
373 }
374
X509_CRL_add0_revoked(X509_CRL * crl,X509_REVOKED * rev)375 int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
376 {
377 X509_CRL_INFO *inf;
378 inf = crl->crl;
379 if (!inf->revoked)
380 inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
381 if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
382 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
383 return 0;
384 }
385 inf->enc.modified = 1;
386 return 1;
387 }
388
X509_CRL_verify(X509_CRL * crl,EVP_PKEY * pkey)389 int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *pkey)
390 {
391 if (X509_ALGOR_cmp(crl->sig_alg, crl->crl->sig_alg) != 0) {
392 OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH);
393 return 0;
394 }
395
396 return ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
397 crl->sig_alg, crl->signature, crl->crl, pkey);
398 }
399
X509_CRL_get0_by_serial(X509_CRL * crl,X509_REVOKED ** ret,ASN1_INTEGER * serial)400 int X509_CRL_get0_by_serial(X509_CRL *crl,
401 X509_REVOKED **ret, ASN1_INTEGER *serial)
402 {
403 return crl_lookup(crl, ret, serial, NULL);
404 }
405
X509_CRL_get0_by_cert(X509_CRL * crl,X509_REVOKED ** ret,X509 * x)406 int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
407 {
408 return crl_lookup(crl, ret, X509_get_serialNumber(x),
409 X509_get_issuer_name(x));
410 }
411
crl_revoked_issuer_match(X509_CRL * crl,X509_NAME * nm,X509_REVOKED * rev)412 static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
413 X509_REVOKED *rev)
414 {
415 size_t i;
416
417 if (!rev->issuer) {
418 if (!nm)
419 return 1;
420 if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
421 return 1;
422 return 0;
423 }
424
425 if (!nm)
426 nm = X509_CRL_get_issuer(crl);
427
428 for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
429 GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
430 if (gen->type != GEN_DIRNAME)
431 continue;
432 if (!X509_NAME_cmp(nm, gen->d.directoryName))
433 return 1;
434 }
435 return 0;
436
437 }
438
439 static struct CRYPTO_STATIC_MUTEX g_crl_sort_lock = CRYPTO_STATIC_MUTEX_INIT;
440
crl_lookup(X509_CRL * crl,X509_REVOKED ** ret,ASN1_INTEGER * serial,X509_NAME * issuer)441 static int crl_lookup(X509_CRL *crl, X509_REVOKED **ret, ASN1_INTEGER *serial,
442 X509_NAME *issuer)
443 {
444 X509_REVOKED rtmp, *rev;
445 size_t idx;
446 rtmp.serialNumber = serial;
447 /*
448 * Sort revoked into serial number order if not already sorted. Do this
449 * under a lock to avoid race condition.
450 */
451
452 CRYPTO_STATIC_MUTEX_lock_read(&g_crl_sort_lock);
453 const int is_sorted = sk_X509_REVOKED_is_sorted(crl->crl->revoked);
454 CRYPTO_STATIC_MUTEX_unlock_read(&g_crl_sort_lock);
455
456 if (!is_sorted) {
457 CRYPTO_STATIC_MUTEX_lock_write(&g_crl_sort_lock);
458 if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) {
459 sk_X509_REVOKED_sort(crl->crl->revoked);
460 }
461 CRYPTO_STATIC_MUTEX_unlock_write(&g_crl_sort_lock);
462 }
463
464 if (!sk_X509_REVOKED_find(crl->crl->revoked, &idx, &rtmp))
465 return 0;
466 /* Need to look for matching name */
467 for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) {
468 rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
469 if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
470 return 0;
471 if (crl_revoked_issuer_match(crl, issuer, rev)) {
472 if (ret)
473 *ret = rev;
474 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
475 return 2;
476 return 1;
477 }
478 }
479 return 0;
480 }
481