source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/x509/x_crl.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 13.7 KB
Line 
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdio.h>
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>
16#include "x509_lcl.h"
17
18static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
19 const X509_REVOKED *const *b);
20static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
21
22ASN1_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)
27
28static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
29static int def_crl_lookup(X509_CRL *crl,
30 X509_REVOKED **ret, ASN1_INTEGER *serial,
31 X509_NAME *issuer);
32
33static X509_CRL_METHOD int_crl_meth = {
34 0,
35 0, 0,
36 def_crl_lookup,
37 def_crl_verify
38};
39
40static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
41
42/*
43 * The X509_CRL_INFO structure needs a bit of customisation. Since we cache
44 * the original encoding the signature won't be affected by reordering of the
45 * revoked field.
46 */
47static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48 void *exarg)
49{
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;
64}
65
66
67ASN1_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)
76
77/*
78 * Set CRL entry issuer according to CRL certificate issuer extension. Check
79 * for unhandled critical CRL entry extensions.
80 */
81
82static int crl_set_issuers(X509_CRL *crl)
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);
134 if (X509_EXTENSION_get_critical(ext)) {
135 if (OBJ_obj2nid(X509_EXTENSION_get_object(ext)) == NID_certificate_issuer)
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
150 * and hash of the whole CRL.
151 */
152static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
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) {
161 case ASN1_OP_NEW_POST:
162 crl->idp = NULL;
163 crl->akid = NULL;
164 crl->flags = 0;
165 crl->idp_flags = 0;
166 crl->idp_reasons = CRLDP_ALL_REASONS;
167 crl->meth = default_crl_method;
168 crl->meth_data = NULL;
169 crl->issuers = NULL;
170 crl->crl_number = NULL;
171 crl->base_crl_number = NULL;
172 break;
173
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,
178 NULL);
179 if (crl->idp)
180 setup_idp(crl, crl->idp);
181
182 crl->akid = X509_CRL_get_ext_d2i(crl,
183 NID_authority_key_identifier, NULL,
184 NULL);
185
186 crl->crl_number = X509_CRL_get_ext_d2i(crl,
187 NID_crl_number, NULL, NULL);
188
189 crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
190 NID_delta_crl, NULL,
191 NULL);
192 /* Delta CRLs must have CRL number */
193 if (crl->base_crl_number && !crl->crl_number)
194 crl->flags |= EXFLAG_INVALID;
195
196 /*
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.
201 */
202
203 exts = crl->crl.extensions;
204
205 for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
206 int nid;
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))
216 continue;
217 crl->flags |= EXFLAG_CRITICAL;
218 break;
219 }
220 }
221
222 if (!crl_set_issuers(crl))
223 return 0;
224
225 if (crl->meth->crl_init) {
226 if (crl->meth->crl_init(crl) == 0)
227 return 0;
228 }
229
230 crl->flags |= EXFLAG_SET;
231 break;
232
233 case ASN1_OP_FREE_POST:
234 if (crl->meth->crl_free) {
235 if (!crl->meth->crl_free(crl))
236 return 0;
237 }
238 AUTHORITY_KEYID_free(crl->akid);
239 ISSUING_DIST_POINT_free(crl->idp);
240 ASN1_INTEGER_free(crl->crl_number);
241 ASN1_INTEGER_free(crl->base_crl_number);
242 sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
243 break;
244 }
245 return 1;
246}
247
248/* Convert IDP into a more convenient form */
249
250static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
251{
252 int idp_only = 0;
253 /* Set various flags according to IDP */
254 crl->idp_flags |= IDP_PRESENT;
255 if (idp->onlyuser > 0) {
256 idp_only++;
257 crl->idp_flags |= IDP_ONLYUSER;
258 }
259 if (idp->onlyCA > 0) {
260 idp_only++;
261 crl->idp_flags |= IDP_ONLYCA;
262 }
263 if (idp->onlyattr > 0) {
264 idp_only++;
265 crl->idp_flags |= IDP_ONLYATTR;
266 }
267
268 if (idp_only > 1)
269 crl->idp_flags |= IDP_INVALID;
270
271 if (idp->indirectCRL > 0)
272 crl->idp_flags |= IDP_INDIRECT;
273
274 if (idp->onlysomereasons) {
275 crl->idp_flags |= IDP_REASONS;
276 if (idp->onlysomereasons->length > 0)
277 crl->idp_reasons = idp->onlysomereasons->data[0];
278 if (idp->onlysomereasons->length > 1)
279 crl->idp_reasons |= (idp->onlysomereasons->data[1] << 8);
280 crl->idp_reasons &= CRLDP_ALL_REASONS;
281 }
282
283 DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
284}
285
286ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
287 ASN1_EMBED(X509_CRL, crl, X509_CRL_INFO),
288 ASN1_EMBED(X509_CRL, sig_alg, X509_ALGOR),
289 ASN1_EMBED(X509_CRL, signature, ASN1_BIT_STRING)
290} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
291
292IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
293
294IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
295
296IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
297
298IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
299
300IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
301
302static int X509_REVOKED_cmp(const X509_REVOKED *const *a,
303 const X509_REVOKED *const *b)
304{
305 return (ASN1_STRING_cmp((ASN1_STRING *)&(*a)->serialNumber,
306 (ASN1_STRING *)&(*b)->serialNumber));
307}
308
309int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
310{
311 X509_CRL_INFO *inf;
312 inf = &crl->crl;
313 if (inf->revoked == NULL)
314 inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
315 if (inf->revoked == NULL || !sk_X509_REVOKED_push(inf->revoked, rev)) {
316 ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
317 return 0;
318 }
319 inf->enc.modified = 1;
320 return 1;
321}
322
323int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
324{
325 if (crl->meth->crl_verify)
326 return crl->meth->crl_verify(crl, r);
327 return 0;
328}
329
330int X509_CRL_get0_by_serial(X509_CRL *crl,
331 X509_REVOKED **ret, ASN1_INTEGER *serial)
332{
333 if (crl->meth->crl_lookup)
334 return crl->meth->crl_lookup(crl, ret, serial, NULL);
335 return 0;
336}
337
338int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
339{
340 if (crl->meth->crl_lookup)
341 return crl->meth->crl_lookup(crl, ret,
342 X509_get_serialNumber(x),
343 X509_get_issuer_name(x));
344 return 0;
345}
346
347static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
348{
349 return (ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
350 &crl->sig_alg, &crl->signature, &crl->crl, r));
351}
352
353static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
354 X509_REVOKED *rev)
355{
356 int i;
357
358 if (!rev->issuer) {
359 if (!nm)
360 return 1;
361 if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
362 return 1;
363 return 0;
364 }
365
366 if (!nm)
367 nm = X509_CRL_get_issuer(crl);
368
369 for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
370 GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
371 if (gen->type != GEN_DIRNAME)
372 continue;
373 if (!X509_NAME_cmp(nm, gen->d.directoryName))
374 return 1;
375 }
376 return 0;
377
378}
379
380static int def_crl_lookup(X509_CRL *crl,
381 X509_REVOKED **ret, ASN1_INTEGER *serial,
382 X509_NAME *issuer)
383{
384 X509_REVOKED rtmp, *rev;
385 int idx;
386 rtmp.serialNumber = *serial;
387 /*
388 * Sort revoked into serial number order if not already sorted. Do this
389 * under a lock to avoid race condition.
390 */
391 if (!sk_X509_REVOKED_is_sorted(crl->crl.revoked)) {
392 CRYPTO_THREAD_write_lock(crl->lock);
393 sk_X509_REVOKED_sort(crl->crl.revoked);
394 CRYPTO_THREAD_unlock(crl->lock);
395 }
396 idx = sk_X509_REVOKED_find(crl->crl.revoked, &rtmp);
397 if (idx < 0)
398 return 0;
399 /* Need to look for matching name */
400 for (; idx < sk_X509_REVOKED_num(crl->crl.revoked); idx++) {
401 rev = sk_X509_REVOKED_value(crl->crl.revoked, idx);
402 if (ASN1_INTEGER_cmp(&rev->serialNumber, serial))
403 return 0;
404 if (crl_revoked_issuer_match(crl, issuer, rev)) {
405 if (ret)
406 *ret = rev;
407 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
408 return 2;
409 return 1;
410 }
411 }
412 return 0;
413}
414
415void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
416{
417 if (meth == NULL)
418 default_crl_method = &int_crl_meth;
419 else
420 default_crl_method = meth;
421}
422
423X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl),
424 int (*crl_free) (X509_CRL *crl),
425 int (*crl_lookup) (X509_CRL *crl,
426 X509_REVOKED **ret,
427 ASN1_INTEGER *ser,
428 X509_NAME *issuer),
429 int (*crl_verify) (X509_CRL *crl,
430 EVP_PKEY *pk))
431{
432 X509_CRL_METHOD *m;
433 m = OPENSSL_malloc(sizeof(*m));
434 if (m == NULL)
435 return NULL;
436 m->crl_init = crl_init;
437 m->crl_free = crl_free;
438 m->crl_lookup = crl_lookup;
439 m->crl_verify = crl_verify;
440 m->flags = X509_CRL_METHOD_DYNAMIC;
441 return m;
442}
443
444void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
445{
446 if (m == NULL || !(m->flags & X509_CRL_METHOD_DYNAMIC))
447 return;
448 OPENSSL_free(m);
449}
450
451void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
452{
453 crl->meth_data = dat;
454}
455
456void *X509_CRL_get_meth_data(X509_CRL *crl)
457{
458 return crl->meth_data;
459}
Note: See TracBrowser for help on using the repository browser.