]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509v3/v3_cpols.c
cac91b94e12c1fe7ff784fa207b9239d5b386370
[thirdparty/openssl.git] / crypto / x509v3 / v3_cpols.c
1 /* v3_cpols.c */
2 /*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 1999.
5 */
6 /* ====================================================================
7 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60 #include <stdio.h>
61 #include "cryptlib.h"
62 #include <openssl/conf.h>
63 #include <openssl/asn1.h>
64 #include <openssl/asn1t.h>
65 #include <openssl/x509v3.h>
66
67 #include "pcy_int.h"
68
69 /* Certificate policies extension support: this one is a bit complex... */
70
71 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
72 BIO *out, int indent);
73 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
74 X509V3_CTX *ctx, char *value);
75 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
76 int indent);
77 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
78 static POLICYINFO *policy_section(X509V3_CTX *ctx,
79 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
80 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
81 STACK_OF(CONF_VALUE) *unot, int ia5org);
82 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
83
84 const X509V3_EXT_METHOD v3_cpols = {
85 NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
86 0, 0, 0, 0,
87 0, 0,
88 0, 0,
89 (X509V3_EXT_I2R)i2r_certpol,
90 (X509V3_EXT_R2I)r2i_certpol,
91 NULL
92 };
93
94 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
95 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
96 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
97
98 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
99
100 ASN1_SEQUENCE(POLICYINFO) = {
101 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
102 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
103 } ASN1_SEQUENCE_END(POLICYINFO)
104
105 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
106
107 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
108
109 ASN1_ADB(POLICYQUALINFO) = {
110 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
111 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
112 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
113
114 ASN1_SEQUENCE(POLICYQUALINFO) = {
115 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
116 ASN1_ADB_OBJECT(POLICYQUALINFO)
117 } ASN1_SEQUENCE_END(POLICYQUALINFO)
118
119 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
120
121 ASN1_SEQUENCE(USERNOTICE) = {
122 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
123 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
124 } ASN1_SEQUENCE_END(USERNOTICE)
125
126 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
127
128 ASN1_SEQUENCE(NOTICEREF) = {
129 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
130 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
131 } ASN1_SEQUENCE_END(NOTICEREF)
132
133 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
134
135 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
136 X509V3_CTX *ctx, char *value)
137 {
138 STACK_OF(POLICYINFO) *pols = NULL;
139 char *pstr;
140 POLICYINFO *pol;
141 ASN1_OBJECT *pobj;
142 STACK_OF(CONF_VALUE) *vals;
143 CONF_VALUE *cnf;
144 int i, ia5org;
145 pols = sk_POLICYINFO_new_null();
146 if (pols == NULL) {
147 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
148 return NULL;
149 }
150 vals = X509V3_parse_list(value);
151 if (vals == NULL) {
152 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
153 goto err;
154 }
155 ia5org = 0;
156 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
157 cnf = sk_CONF_VALUE_value(vals, i);
158 if (cnf->value || !cnf->name) {
159 X509V3err(X509V3_F_R2I_CERTPOL,
160 X509V3_R_INVALID_POLICY_IDENTIFIER);
161 X509V3_conf_err(cnf);
162 goto err;
163 }
164 pstr = cnf->name;
165 if (strcmp(pstr, "ia5org") == 0) {
166 ia5org = 1;
167 continue;
168 } else if (*pstr == '@') {
169 STACK_OF(CONF_VALUE) *polsect;
170 polsect = X509V3_get_section(ctx, pstr + 1);
171 if (!polsect) {
172 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
173
174 X509V3_conf_err(cnf);
175 goto err;
176 }
177 pol = policy_section(ctx, polsect, ia5org);
178 X509V3_section_free(ctx, polsect);
179 if (pol == NULL)
180 goto err;
181 } else {
182 if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
183 X509V3err(X509V3_F_R2I_CERTPOL,
184 X509V3_R_INVALID_OBJECT_IDENTIFIER);
185 X509V3_conf_err(cnf);
186 goto err;
187 }
188 pol = POLICYINFO_new();
189 pol->policyid = pobj;
190 }
191 if (!sk_POLICYINFO_push(pols, pol)) {
192 POLICYINFO_free(pol);
193 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
194 goto err;
195 }
196 }
197 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
198 return pols;
199 err:
200 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
201 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
202 return NULL;
203 }
204
205 static POLICYINFO *policy_section(X509V3_CTX *ctx,
206 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
207 {
208 int i;
209 CONF_VALUE *cnf;
210 POLICYINFO *pol;
211 POLICYQUALINFO *qual;
212
213 if ((pol = POLICYINFO_new()) == NULL)
214 goto merr;
215 for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
216 cnf = sk_CONF_VALUE_value(polstrs, i);
217 if (strcmp(cnf->name, "policyIdentifier") == 0) {
218 ASN1_OBJECT *pobj;
219 if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
220 X509V3err(X509V3_F_POLICY_SECTION,
221 X509V3_R_INVALID_OBJECT_IDENTIFIER);
222 X509V3_conf_err(cnf);
223 goto err;
224 }
225 pol->policyid = pobj;
226
227 } else if (!name_cmp(cnf->name, "CPS")) {
228 if (pol->qualifiers == NULL)
229 pol->qualifiers = sk_POLICYQUALINFO_new_null();
230 if ((qual = POLICYQUALINFO_new()) == NULL)
231 goto merr;
232 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
233 goto merr;
234 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
235 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
236 goto err;
237 }
238 if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
239 goto merr;
240 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
241 strlen(cnf->value)))
242 goto merr;
243 } else if (!name_cmp(cnf->name, "userNotice")) {
244 STACK_OF(CONF_VALUE) *unot;
245 if (*cnf->value != '@') {
246 X509V3err(X509V3_F_POLICY_SECTION,
247 X509V3_R_EXPECTED_A_SECTION_NAME);
248 X509V3_conf_err(cnf);
249 goto err;
250 }
251 unot = X509V3_get_section(ctx, cnf->value + 1);
252 if (!unot) {
253 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
254
255 X509V3_conf_err(cnf);
256 goto err;
257 }
258 qual = notice_section(ctx, unot, ia5org);
259 X509V3_section_free(ctx, unot);
260 if (!qual)
261 goto err;
262 if (!pol->qualifiers)
263 pol->qualifiers = sk_POLICYQUALINFO_new_null();
264 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
265 goto merr;
266 } else {
267 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
268
269 X509V3_conf_err(cnf);
270 goto err;
271 }
272 }
273 if (!pol->policyid) {
274 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
275 goto err;
276 }
277
278 return pol;
279
280 merr:
281 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
282
283 err:
284 POLICYINFO_free(pol);
285 return NULL;
286
287 }
288
289 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
290 STACK_OF(CONF_VALUE) *unot, int ia5org)
291 {
292 int i, ret;
293 CONF_VALUE *cnf;
294 USERNOTICE *not;
295 POLICYQUALINFO *qual;
296
297 if ((qual = POLICYQUALINFO_new()) == NULL)
298 goto merr;
299 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
300 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
301 goto err;
302 }
303 if ((not = USERNOTICE_new()) == NULL)
304 goto merr;
305 qual->d.usernotice = not;
306 for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
307 cnf = sk_CONF_VALUE_value(unot, i);
308 if (strcmp(cnf->name, "explicitText") == 0) {
309 if ((not->exptext = ASN1_VISIBLESTRING_new()) == NULL)
310 goto merr;
311 if (!ASN1_STRING_set(not->exptext, cnf->value,
312 strlen(cnf->value)))
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 goto err;
343 }
344 ret = nref_nos(nref->noticenos, nos);
345 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
346 if (!ret)
347 goto err;
348 } else {
349 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
350 X509V3_conf_err(cnf);
351 goto err;
352 }
353 }
354
355 if (not->noticeref &&
356 (!not->noticeref->noticenos || !not->noticeref->organization)) {
357 X509V3err(X509V3_F_NOTICE_SECTION,
358 X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
359 goto err;
360 }
361
362 return qual;
363
364 merr:
365 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
366
367 err:
368 POLICYQUALINFO_free(qual);
369 return NULL;
370 }
371
372 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
373 {
374 CONF_VALUE *cnf;
375 ASN1_INTEGER *aint;
376
377 int i;
378
379 for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
380 cnf = sk_CONF_VALUE_value(nos, i);
381 if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
382 X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
383 goto err;
384 }
385 if (!sk_ASN1_INTEGER_push(nnums, aint))
386 goto merr;
387 }
388 return 1;
389
390 merr:
391 X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
392
393 err:
394 sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
395 return 0;
396 }
397
398 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
399 BIO *out, int indent)
400 {
401 int i;
402 POLICYINFO *pinfo;
403 /* First print out the policy OIDs */
404 for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
405 pinfo = sk_POLICYINFO_value(pol, i);
406 BIO_printf(out, "%*sPolicy: ", indent, "");
407 i2a_ASN1_OBJECT(out, pinfo->policyid);
408 BIO_puts(out, "\n");
409 if (pinfo->qualifiers)
410 print_qualifiers(out, pinfo->qualifiers, indent + 2);
411 }
412 return 1;
413 }
414
415 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
416 int indent)
417 {
418 POLICYQUALINFO *qualinfo;
419 int i;
420 for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
421 qualinfo = sk_POLICYQUALINFO_value(quals, i);
422 switch (OBJ_obj2nid(qualinfo->pqualid)) {
423 case NID_id_qt_cps:
424 BIO_printf(out, "%*sCPS: %s\n", indent, "",
425 qualinfo->d.cpsuri->data);
426 break;
427
428 case NID_id_qt_unotice:
429 BIO_printf(out, "%*sUser Notice:\n", indent, "");
430 print_notice(out, qualinfo->d.usernotice, indent + 2);
431 break;
432
433 default:
434 BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
435
436 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
437 BIO_puts(out, "\n");
438 break;
439 }
440 }
441 }
442
443 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
444 {
445 int i;
446 if (notice->noticeref) {
447 NOTICEREF *ref;
448 ref = notice->noticeref;
449 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
450 ref->organization->data);
451 BIO_printf(out, "%*sNumber%s: ", indent, "",
452 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
453 for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
454 ASN1_INTEGER *num;
455 char *tmp;
456 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
457 if (i)
458 BIO_puts(out, ", ");
459 tmp = i2s_ASN1_INTEGER(NULL, num);
460 BIO_puts(out, tmp);
461 OPENSSL_free(tmp);
462 }
463 BIO_puts(out, "\n");
464 }
465 if (notice->exptext)
466 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
467 notice->exptext->data);
468 }
469
470 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
471 {
472 const X509_POLICY_DATA *dat = node->data;
473
474 BIO_printf(out, "%*sPolicy: ", indent, "");
475
476 i2a_ASN1_OBJECT(out, dat->valid_policy);
477 BIO_puts(out, "\n");
478 BIO_printf(out, "%*s%s\n", indent + 2, "",
479 node_data_critical(dat) ? "Critical" : "Non Critical");
480 if (dat->qualifier_set)
481 print_qualifiers(out, dat->qualifier_set, indent + 2);
482 else
483 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
484 }