VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.1j/crypto/x509v3/v3_cpols.c@ 90248

最後變更 在這個檔案從90248是 87984,由 vboxsync 提交於 4 年 前

openssl-1.1.1j: Applied and adjusted our OpenSSL changes to 1.1.1j. bugref:9963

檔案大小: 16.0 KB
 
1/*
2 * Copyright 1999-2018 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/conf.h>
13#include <openssl/asn1.h>
14#include <openssl/asn1t.h>
15#include <openssl/x509v3.h>
16
17#include "pcy_local.h"
18#include "ext_dat.h"
19
20/* Certificate policies extension support: this one is a bit complex... */
21
22static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
23 BIO *out, int indent);
24static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
25 X509V3_CTX *ctx, const char *value);
26static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
27 int indent);
28static void print_notice(BIO *out, USERNOTICE *notice, int indent);
29static POLICYINFO *policy_section(X509V3_CTX *ctx,
30 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
31static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
32 STACK_OF(CONF_VALUE) *unot, int ia5org);
33static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
34static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len);
35static int displaytext_get_tag_len(const char *tagstr);
36
37const X509V3_EXT_METHOD v3_cpols = {
38 NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
39 0, 0, 0, 0,
40 0, 0,
41 0, 0,
42 (X509V3_EXT_I2R)i2r_certpol,
43 (X509V3_EXT_R2I)r2i_certpol,
44 NULL
45};
46
47ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
48 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
49ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
50
51IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
52
53ASN1_SEQUENCE(POLICYINFO) = {
54 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
55 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
56} ASN1_SEQUENCE_END(POLICYINFO)
57
58IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
59
60ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
61
62ASN1_ADB(POLICYQUALINFO) = {
63 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
64 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
65} ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
66
67ASN1_SEQUENCE(POLICYQUALINFO) = {
68 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
69 ASN1_ADB_OBJECT(POLICYQUALINFO)
70} ASN1_SEQUENCE_END(POLICYQUALINFO)
71
72IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
73
74ASN1_SEQUENCE(USERNOTICE) = {
75 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
76 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
77} ASN1_SEQUENCE_END(USERNOTICE)
78
79IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
80
81ASN1_SEQUENCE(NOTICEREF) = {
82 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
83 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
84} ASN1_SEQUENCE_END(NOTICEREF)
85
86IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
87
88static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
89 X509V3_CTX *ctx, const char *value)
90{
91 STACK_OF(POLICYINFO) *pols;
92 char *pstr;
93 POLICYINFO *pol;
94 ASN1_OBJECT *pobj;
95 STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value);
96 CONF_VALUE *cnf;
97 const int num = sk_CONF_VALUE_num(vals);
98 int i, ia5org;
99
100 if (vals == NULL) {
101 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
102 return NULL;
103 }
104
105 pols = sk_POLICYINFO_new_reserve(NULL, num);
106 if (pols == NULL) {
107 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
108 goto err;
109 }
110
111 ia5org = 0;
112 for (i = 0; i < num; i++) {
113 cnf = sk_CONF_VALUE_value(vals, i);
114
115 if (cnf->value || !cnf->name) {
116 X509V3err(X509V3_F_R2I_CERTPOL,
117 X509V3_R_INVALID_POLICY_IDENTIFIER);
118 X509V3_conf_err(cnf);
119 goto err;
120 }
121 pstr = cnf->name;
122 if (strcmp(pstr, "ia5org") == 0) {
123 ia5org = 1;
124 continue;
125 } else if (*pstr == '@') {
126 STACK_OF(CONF_VALUE) *polsect;
127 polsect = X509V3_get_section(ctx, pstr + 1);
128 if (!polsect) {
129 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
130
131 X509V3_conf_err(cnf);
132 goto err;
133 }
134 pol = policy_section(ctx, polsect, ia5org);
135 X509V3_section_free(ctx, polsect);
136 if (pol == NULL)
137 goto err;
138 } else {
139 if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
140 X509V3err(X509V3_F_R2I_CERTPOL,
141 X509V3_R_INVALID_OBJECT_IDENTIFIER);
142 X509V3_conf_err(cnf);
143 goto err;
144 }
145 pol = POLICYINFO_new();
146 if (pol == NULL) {
147 ASN1_OBJECT_free(pobj);
148 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
149 goto err;
150 }
151 pol->policyid = pobj;
152 }
153 if (!sk_POLICYINFO_push(pols, pol)) {
154 POLICYINFO_free(pol);
155 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
156 goto err;
157 }
158 }
159 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
160 return pols;
161 err:
162 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
163 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
164 return NULL;
165}
166
167static POLICYINFO *policy_section(X509V3_CTX *ctx,
168 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
169{
170 int i;
171 CONF_VALUE *cnf;
172 POLICYINFO *pol;
173 POLICYQUALINFO *qual;
174
175 if ((pol = POLICYINFO_new()) == NULL)
176 goto merr;
177 for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
178 cnf = sk_CONF_VALUE_value(polstrs, i);
179 if (strcmp(cnf->name, "policyIdentifier") == 0) {
180 ASN1_OBJECT *pobj;
181 if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
182 X509V3err(X509V3_F_POLICY_SECTION,
183 X509V3_R_INVALID_OBJECT_IDENTIFIER);
184 X509V3_conf_err(cnf);
185 goto err;
186 }
187 pol->policyid = pobj;
188
189 } else if (!name_cmp(cnf->name, "CPS")) {
190 if (pol->qualifiers == NULL)
191 pol->qualifiers = sk_POLICYQUALINFO_new_null();
192 if ((qual = POLICYQUALINFO_new()) == NULL)
193 goto merr;
194 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
195 goto merr;
196 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
197 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
198 goto err;
199 }
200 if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
201 goto merr;
202 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
203 strlen(cnf->value)))
204 goto merr;
205 } else if (!name_cmp(cnf->name, "userNotice")) {
206 STACK_OF(CONF_VALUE) *unot;
207 if (*cnf->value != '@') {
208 X509V3err(X509V3_F_POLICY_SECTION,
209 X509V3_R_EXPECTED_A_SECTION_NAME);
210 X509V3_conf_err(cnf);
211 goto err;
212 }
213 unot = X509V3_get_section(ctx, cnf->value + 1);
214 if (!unot) {
215 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
216
217 X509V3_conf_err(cnf);
218 goto err;
219 }
220 qual = notice_section(ctx, unot, ia5org);
221 X509V3_section_free(ctx, unot);
222 if (!qual)
223 goto err;
224 if (!pol->qualifiers)
225 pol->qualifiers = sk_POLICYQUALINFO_new_null();
226 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
227 goto merr;
228 } else {
229 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
230
231 X509V3_conf_err(cnf);
232 goto err;
233 }
234 }
235 if (!pol->policyid) {
236 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
237 goto err;
238 }
239
240 return pol;
241
242 merr:
243 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
244
245 err:
246 POLICYINFO_free(pol);
247 return NULL;
248}
249
250static int displaytext_get_tag_len(const char *tagstr)
251{
252 char *colon = strchr(tagstr, ':');
253
254 return (colon == NULL) ? -1 : colon - tagstr;
255}
256
257static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
258{
259 int len;
260
261 *tag_len = 0;
262 len = displaytext_get_tag_len(tagstr);
263
264 if (len == -1)
265 return V_ASN1_VISIBLESTRING;
266 *tag_len = len;
267 if (len == sizeof("UTF8") - 1 && strncmp(tagstr, "UTF8", len) == 0)
268 return V_ASN1_UTF8STRING;
269 if (len == sizeof("UTF8String") - 1 && strncmp(tagstr, "UTF8String", len) == 0)
270 return V_ASN1_UTF8STRING;
271 if (len == sizeof("BMP") - 1 && strncmp(tagstr, "BMP", len) == 0)
272 return V_ASN1_BMPSTRING;
273 if (len == sizeof("BMPSTRING") - 1 && strncmp(tagstr, "BMPSTRING", len) == 0)
274 return V_ASN1_BMPSTRING;
275 if (len == sizeof("VISIBLE") - 1 && strncmp(tagstr, "VISIBLE", len) == 0)
276 return V_ASN1_VISIBLESTRING;
277 if (len == sizeof("VISIBLESTRING") - 1 && strncmp(tagstr, "VISIBLESTRING", len) == 0)
278 return V_ASN1_VISIBLESTRING;
279 *tag_len = 0;
280 return V_ASN1_VISIBLESTRING;
281}
282
283static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
284 STACK_OF(CONF_VALUE) *unot, int ia5org)
285{
286 int i, ret, len, tag;
287 unsigned int tag_len;
288 CONF_VALUE *cnf;
289 USERNOTICE *not;
290 POLICYQUALINFO *qual;
291 char *value = NULL;
292
293 if ((qual = POLICYQUALINFO_new()) == NULL)
294 goto merr;
295 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
296 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
297 goto err;
298 }
299 if ((not = USERNOTICE_new()) == NULL)
300 goto merr;
301 qual->d.usernotice = not;
302 for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
303 cnf = sk_CONF_VALUE_value(unot, i);
304 value = cnf->value;
305 if (strcmp(cnf->name, "explicitText") == 0) {
306 tag = displaytext_str2tag(value, &tag_len);
307 if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL)
308 goto merr;
309 if (tag_len != 0)
310 value += tag_len + 1;
311 len = strlen(value);
312 if (!ASN1_STRING_set(not->exptext, value, len))
313 goto merr;
314 } else if (strcmp(cnf->name, "organization") == 0) {
315 NOTICEREF *nref;
316 if (!not->noticeref) {
317 if ((nref = NOTICEREF_new()) == NULL)
318 goto merr;
319 not->noticeref = nref;
320 } else
321 nref = not->noticeref;
322 if (ia5org)
323 nref->organization->type = V_ASN1_IA5STRING;
324 else
325 nref->organization->type = V_ASN1_VISIBLESTRING;
326 if (!ASN1_STRING_set(nref->organization, cnf->value,
327 strlen(cnf->value)))
328 goto merr;
329 } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
330 NOTICEREF *nref;
331 STACK_OF(CONF_VALUE) *nos;
332 if (!not->noticeref) {
333 if ((nref = NOTICEREF_new()) == NULL)
334 goto merr;
335 not->noticeref = nref;
336 } else
337 nref = not->noticeref;
338 nos = X509V3_parse_list(cnf->value);
339 if (!nos || !sk_CONF_VALUE_num(nos)) {
340 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
341 X509V3_conf_err(cnf);
342 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
343 goto err;
344 }
345 ret = nref_nos(nref->noticenos, nos);
346 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
347 if (!ret)
348 goto err;
349 } else {
350 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
351 X509V3_conf_err(cnf);
352 goto err;
353 }
354 }
355
356 if (not->noticeref &&
357 (!not->noticeref->noticenos || !not->noticeref->organization)) {
358 X509V3err(X509V3_F_NOTICE_SECTION,
359 X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
360 goto err;
361 }
362
363 return qual;
364
365 merr:
366 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
367
368 err:
369 POLICYQUALINFO_free(qual);
370 return NULL;
371}
372
373static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
374{
375 CONF_VALUE *cnf;
376 ASN1_INTEGER *aint;
377
378 int i;
379
380 for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
381 cnf = sk_CONF_VALUE_value(nos, i);
382 if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
383 X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
384 goto err;
385 }
386 if (!sk_ASN1_INTEGER_push(nnums, aint))
387 goto merr;
388 }
389 return 1;
390
391 merr:
392 ASN1_INTEGER_free(aint);
393 X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
394
395 err:
396 return 0;
397}
398
399static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
400 BIO *out, int indent)
401{
402 int i;
403 POLICYINFO *pinfo;
404 /* First print out the policy OIDs */
405 for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
406 pinfo = sk_POLICYINFO_value(pol, i);
407 BIO_printf(out, "%*sPolicy: ", indent, "");
408 i2a_ASN1_OBJECT(out, pinfo->policyid);
409 BIO_puts(out, "\n");
410 if (pinfo->qualifiers)
411 print_qualifiers(out, pinfo->qualifiers, indent + 2);
412 }
413 return 1;
414}
415
416static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
417 int indent)
418{
419 POLICYQUALINFO *qualinfo;
420 int i;
421 for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
422 qualinfo = sk_POLICYQUALINFO_value(quals, i);
423 switch (OBJ_obj2nid(qualinfo->pqualid)) {
424 case NID_id_qt_cps:
425 BIO_printf(out, "%*sCPS: %s\n", indent, "",
426 qualinfo->d.cpsuri->data);
427 break;
428
429 case NID_id_qt_unotice:
430 BIO_printf(out, "%*sUser Notice:\n", indent, "");
431 print_notice(out, qualinfo->d.usernotice, indent + 2);
432 break;
433
434 default:
435 BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
436
437 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
438 BIO_puts(out, "\n");
439 break;
440 }
441 }
442}
443
444static void print_notice(BIO *out, USERNOTICE *notice, int indent)
445{
446 int i;
447 if (notice->noticeref) {
448 NOTICEREF *ref;
449 ref = notice->noticeref;
450 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
451 ref->organization->data);
452 BIO_printf(out, "%*sNumber%s: ", indent, "",
453 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
454 for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
455 ASN1_INTEGER *num;
456 char *tmp;
457 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
458 if (i)
459 BIO_puts(out, ", ");
460 if (num == NULL)
461 BIO_puts(out, "(null)");
462 else {
463 tmp = i2s_ASN1_INTEGER(NULL, num);
464 if (tmp == NULL)
465 return;
466 BIO_puts(out, tmp);
467 OPENSSL_free(tmp);
468 }
469 }
470 BIO_puts(out, "\n");
471 }
472 if (notice->exptext)
473 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
474 notice->exptext->data);
475}
476
477void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
478{
479 const X509_POLICY_DATA *dat = node->data;
480
481 BIO_printf(out, "%*sPolicy: ", indent, "");
482
483 i2a_ASN1_OBJECT(out, dat->valid_policy);
484 BIO_puts(out, "\n");
485 BIO_printf(out, "%*s%s\n", indent + 2, "",
486 node_data_critical(dat) ? "Critical" : "Non Critical");
487 if (dat->qualifier_set)
488 print_qualifiers(out, dat->qualifier_set, indent + 2);
489 else
490 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
491}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette