2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
66 /* Certificate policies extension support: this one is a bit complex... */
68 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol, BIO *out, int indent);
69 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *value);
70 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, int indent);
71 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
72 static POLICYINFO *policy_section(X509V3_CTX *ctx, STACK *polstrs);
73 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx, STACK *unot);
74 static STACK *nref_nos(STACK *nos);
76 X509V3_EXT_METHOD v3_cpols = {
77 NID_certificate_policies, 0,
78 (X509V3_EXT_NEW)CERTIFICATEPOLICIES_new,
79 CERTIFICATEPOLICIES_free,
80 (X509V3_EXT_D2I)d2i_CERTIFICATEPOLICIES,
81 i2d_CERTIFICATEPOLICIES,
84 (X509V3_EXT_I2R)i2r_certpol,
85 (X509V3_EXT_R2I)r2i_certpol,
91 * ASN1err(ASN1_F_POLICYINFO_NEW,ERR_R_MALLOC_FAILURE);
92 * ASN1err(ASN1_F_D2I_POLICYINFO,ERR_R_MALLOC_FAILURE);
93 * ASN1err(ASN1_F_POLICYQUALINFO_NEW,ERR_R_MALLOC_FAILURE);
94 * ASN1err(ASN1_F_D2I_POLICYQUALINFO,ERR_R_MALLOC_FAILURE);
95 * ASN1err(ASN1_F_USERNOTICE_NEW,ERR_R_MALLOC_FAILURE);
96 * ASN1err(ASN1_F_D2I_USERNOTICE,ERR_R_MALLOC_FAILURE);
97 * ASN1err(ASN1_F_NOTICEREF_NEW,ERR_R_MALLOC_FAILURE);
98 * ASN1err(ASN1_F_D2I_NOTICEREF,ERR_R_MALLOC_FAILURE);
101 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
102 X509V3_CTX *ctx, char *value)
104 STACK_OF(POLICYINFO) *pols = NULL;
111 pols = sk_POLICYINFO_new_null();
112 vals = X509V3_parse_list(value);
113 for(i = 0; i < sk_num(vals); i++) {
114 cnf = (CONF_VALUE *)sk_value(vals, i);
115 if(cnf->value || !cnf->name ) {
116 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
117 X509V3_conf_err(cnf);
123 polsect = X509V3_get_section(ctx, pstr + 1);
125 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_SECTION);
127 X509V3_conf_err(cnf);
130 pol = policy_section(ctx, polsect);
131 X509V3_section_free(ctx, polsect);
134 if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
135 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
136 X509V3_conf_err(cnf);
139 pol = POLICYINFO_new();
140 pol->policyid = pobj;
142 sk_POLICYINFO_push(pols, pol);
144 sk_pop_free(vals, X509V3_conf_free);
147 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
151 static POLICYINFO *policy_section(X509V3_CTX *ctx, STACK *polstrs)
156 POLICYQUALINFO *qual;
157 if(!(pol = POLICYINFO_new())) goto merr;
158 for(i = 0; i < sk_num(polstrs); i++) {
159 cnf = (CONF_VALUE *)sk_value(polstrs, i);
160 if(!strcmp(cnf->name, "policyIdentifier")) {
162 if(!(pobj = OBJ_txt2obj(cnf->value, 0))) {
163 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OBJECT_IDENTIFIER);
164 X509V3_conf_err(cnf);
167 pol->policyid = pobj;
169 } else if(!name_cmp(cnf->name, "CPS")) {
170 if(!pol->qualifiers) pol->qualifiers =
171 sk_POLICYQUALINFO_new_null();
172 if(!(qual = POLICYQUALINFO_new())) goto merr;
173 if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
175 qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
176 qual->d.cpsuri = ASN1_IA5STRING_new();
177 if(!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
178 strlen(cnf->value))) goto merr;
179 } else if(!name_cmp(cnf->name, "userNotice")) {
181 if(*cnf->value != '@') {
182 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_EXPECTED_A_SECTION_NAME);
183 X509V3_conf_err(cnf);
186 unot = X509V3_get_section(ctx, cnf->value + 1);
188 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_SECTION);
190 X509V3_conf_err(cnf);
193 qual = notice_section(ctx, unot);
194 X509V3_section_free(ctx, unot);
196 if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
199 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OPTION);
201 X509V3_conf_err(cnf);
206 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_NO_POLICY_IDENTIFIER);
213 X509V3err(X509V3_F_POLICY_SECTION,ERR_R_MALLOC_FAILURE);
216 POLICYINFO_free(pol);
222 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx, STACK *unot)
227 POLICYQUALINFO *qual;
228 if(!(qual = POLICYQUALINFO_new())) goto merr;
229 qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
230 if(!(not = USERNOTICE_new())) goto merr;
231 qual->d.usernotice = not;
232 for(i = 0; i < sk_num(unot); i++) {
233 cnf = (CONF_VALUE *)sk_value(unot, i);
234 if(!strcmp(cnf->name, "explicitText")) {
235 not->exptext = ASN1_VISIBLESTRING_new();
236 if(!ASN1_STRING_set(not->exptext, cnf->value,
237 strlen(cnf->value))) goto merr;
238 } else if(!strcmp(cnf->name, "organization")) {
240 if(!not->noticeref) {
241 if(!(nref = NOTICEREF_new())) goto merr;
242 not->noticeref = nref;
243 } else nref = not->noticeref;
244 nref->organization = ASN1_VISIBLESTRING_new();
245 if(!ASN1_STRING_set(nref->organization, cnf->value,
246 strlen(cnf->value))) goto merr;
247 } else if(!strcmp(cnf->name, "noticeNumbers")) {
250 if(!not->noticeref) {
251 if(!(nref = NOTICEREF_new())) goto merr;
252 not->noticeref = nref;
253 } else nref = not->noticeref;
254 nos = X509V3_parse_list(cnf->value);
255 if(!nos || !sk_num(nos)) {
256 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_NUMBERS);
257 X509V3_conf_err(cnf);
260 nref->noticenos = nref_nos(nos);
261 sk_pop_free(nos, X509V3_conf_free);
262 if(!nref->noticenos) goto err;
264 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_OPTION);
266 X509V3_conf_err(cnf);
272 (!not->noticeref->noticenos || !not->noticeref->organization)) {
273 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
280 X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
283 POLICYQUALINFO_free(qual);
287 static STACK *nref_nos(STACK *nos)
293 if(!(nnums = sk_new_null())) goto merr;
294 for(i = 0; i < sk_num(nos); i++) {
295 cnf = (CONF_VALUE *)sk_value(nos, i);
296 if(!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
297 X509V3err(X509V3_F_NREF_NOS,X509V3_R_INVALID_NUMBER);
300 if(!sk_push(nnums, (char *)aint)) goto merr;
305 X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
308 sk_pop_free(nnums, ASN1_STRING_free);
313 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
314 BIO *out, int indent)
318 /* First print out the policy OIDs */
319 for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
320 pinfo = sk_POLICYINFO_value(pol, i);
321 BIO_printf(out, "%*sPolicy: ", indent, "");
322 i2a_ASN1_OBJECT(out, pinfo->policyid);
324 if(pinfo->qualifiers)
325 print_qualifiers(out, pinfo->qualifiers, indent + 2);
331 int i2d_CERTIFICATEPOLICIES(STACK_OF(POLICYINFO) *a, unsigned char **pp)
334 return i2d_ASN1_SET_OF_POLICYINFO(a, pp, i2d_POLICYINFO, V_ASN1_SEQUENCE,
335 V_ASN1_UNIVERSAL, IS_SEQUENCE);}
337 STACK_OF(POLICYINFO) *CERTIFICATEPOLICIES_new(void)
339 return sk_POLICYINFO_new_null();
342 void CERTIFICATEPOLICIES_free(STACK_OF(POLICYINFO) *a)
344 sk_POLICYINFO_pop_free(a, POLICYINFO_free);
347 STACK_OF(POLICYINFO) *d2i_CERTIFICATEPOLICIES(STACK_OF(POLICYINFO) **a,
348 unsigned char **pp,long length)
350 return d2i_ASN1_SET_OF_POLICYINFO(a, pp, length, d2i_POLICYINFO,
351 POLICYINFO_free, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
355 IMPLEMENT_STACK_OF(POLICYINFO)
356 IMPLEMENT_ASN1_SET_OF(POLICYINFO)
358 int i2d_POLICYINFO(POLICYINFO *a, unsigned char **pp)
362 M_ASN1_I2D_len (a->policyid, i2d_ASN1_OBJECT);
363 M_ASN1_I2D_len_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
366 M_ASN1_I2D_seq_total();
368 M_ASN1_I2D_put (a->policyid, i2d_ASN1_OBJECT);
369 M_ASN1_I2D_put_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
375 POLICYINFO *POLICYINFO_new(void)
377 POLICYINFO *ret=NULL;
379 M_ASN1_New_Malloc(ret, POLICYINFO);
380 ret->policyid = NULL;
381 ret->qualifiers = NULL;
383 M_ASN1_New_Error(ASN1_F_POLICYINFO_NEW);
386 POLICYINFO *d2i_POLICYINFO(POLICYINFO **a, unsigned char **pp,long length)
388 M_ASN1_D2I_vars(a,POLICYINFO *,POLICYINFO_new);
390 M_ASN1_D2I_start_sequence();
391 M_ASN1_D2I_get(ret->policyid, d2i_ASN1_OBJECT);
392 if(!M_ASN1_D2I_end_sequence()) {
393 M_ASN1_D2I_get_seq_type (POLICYQUALINFO, ret->qualifiers,
394 d2i_POLICYQUALINFO, POLICYQUALINFO_free);
396 M_ASN1_D2I_Finish(a, POLICYINFO_free, ASN1_F_D2I_POLICYINFO);
399 void POLICYINFO_free(POLICYINFO *a)
401 if (a == NULL) return;
402 ASN1_OBJECT_free(a->policyid);
403 sk_POLICYQUALINFO_pop_free(a->qualifiers, POLICYQUALINFO_free);
407 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
410 POLICYQUALINFO *qualinfo;
412 for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
413 qualinfo = sk_POLICYQUALINFO_value(quals, i);
414 switch(OBJ_obj2nid(qualinfo->pqualid))
417 BIO_printf(out, "%*sCPS: %s\n", indent, "",
418 qualinfo->d.cpsuri->data);
421 case NID_id_qt_unotice:
422 BIO_printf(out, "%*sUser Notice:\n", indent, "");
423 print_notice(out, qualinfo->d.usernotice, indent + 2);
427 BIO_printf(out, "%*sUnknown Qualifier: ",
430 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
437 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
440 if(notice->noticeref) {
442 ref = notice->noticeref;
443 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
444 ref->organization->data);
445 BIO_printf(out, "%*sNumber%s: ", indent, "",
446 (sk_num(ref->noticenos) > 1) ? "s" : "");
447 for(i = 0; i < sk_num(ref->noticenos); i++) {
450 num = (ASN1_INTEGER *)sk_value(ref->noticenos, i);
451 if(i) BIO_puts(out, ", ");
452 tmp = i2s_ASN1_INTEGER(NULL, num);
459 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
460 notice->exptext->data);
465 int i2d_POLICYQUALINFO(POLICYQUALINFO *a, unsigned char **pp)
469 M_ASN1_I2D_len (a->pqualid, i2d_ASN1_OBJECT);
470 switch(OBJ_obj2nid(a->pqualid)) {
472 M_ASN1_I2D_len(a->d.cpsuri, i2d_ASN1_IA5STRING);
475 case NID_id_qt_unotice:
476 M_ASN1_I2D_len(a->d.usernotice, i2d_USERNOTICE);
480 M_ASN1_I2D_len(a->d.other, i2d_ASN1_TYPE);
484 M_ASN1_I2D_seq_total();
486 M_ASN1_I2D_put (a->pqualid, i2d_ASN1_OBJECT);
487 switch(OBJ_obj2nid(a->pqualid)) {
489 M_ASN1_I2D_put(a->d.cpsuri, i2d_ASN1_IA5STRING);
492 case NID_id_qt_unotice:
493 M_ASN1_I2D_put(a->d.usernotice, i2d_USERNOTICE);
497 M_ASN1_I2D_put(a->d.other, i2d_ASN1_TYPE);
504 POLICYQUALINFO *POLICYQUALINFO_new(void)
506 POLICYQUALINFO *ret=NULL;
508 M_ASN1_New_Malloc(ret, POLICYQUALINFO);
512 M_ASN1_New_Error(ASN1_F_POLICYQUALINFO_NEW);
515 POLICYQUALINFO *d2i_POLICYQUALINFO(POLICYQUALINFO **a, unsigned char **pp,
518 M_ASN1_D2I_vars(a,POLICYQUALINFO *,POLICYQUALINFO_new);
520 M_ASN1_D2I_start_sequence();
521 M_ASN1_D2I_get (ret->pqualid, d2i_ASN1_OBJECT);
522 switch(OBJ_obj2nid(ret->pqualid)) {
524 M_ASN1_D2I_get(ret->d.cpsuri, d2i_ASN1_IA5STRING);
527 case NID_id_qt_unotice:
528 M_ASN1_D2I_get(ret->d.usernotice, d2i_USERNOTICE);
532 M_ASN1_D2I_get(ret->d.other, d2i_ASN1_TYPE);
535 M_ASN1_D2I_Finish(a, POLICYQUALINFO_free, ASN1_F_D2I_POLICYQUALINFO);
538 void POLICYQUALINFO_free(POLICYQUALINFO *a)
540 if (a == NULL) return;
541 switch(OBJ_obj2nid(a->pqualid)) {
543 ASN1_IA5STRING_free(a->d.cpsuri);
546 case NID_id_qt_unotice:
547 USERNOTICE_free(a->d.usernotice);
551 ASN1_TYPE_free(a->d.other);
555 ASN1_OBJECT_free(a->pqualid);
559 int i2d_USERNOTICE(USERNOTICE *a, unsigned char **pp)
563 M_ASN1_I2D_len (a->noticeref, i2d_NOTICEREF);
564 M_ASN1_I2D_len (a->exptext, i2d_DISPLAYTEXT);
566 M_ASN1_I2D_seq_total();
568 M_ASN1_I2D_put (a->noticeref, i2d_NOTICEREF);
569 M_ASN1_I2D_put (a->exptext, i2d_DISPLAYTEXT);
574 USERNOTICE *USERNOTICE_new(void)
576 USERNOTICE *ret=NULL;
578 M_ASN1_New_Malloc(ret, USERNOTICE);
579 ret->noticeref = NULL;
582 M_ASN1_New_Error(ASN1_F_USERNOTICE_NEW);
585 USERNOTICE *d2i_USERNOTICE(USERNOTICE **a, unsigned char **pp,long length)
587 M_ASN1_D2I_vars(a,USERNOTICE *,USERNOTICE_new);
589 M_ASN1_D2I_start_sequence();
590 M_ASN1_D2I_get_opt(ret->noticeref, d2i_NOTICEREF, V_ASN1_SEQUENCE);
591 if (!M_ASN1_D2I_end_sequence()) {
592 M_ASN1_D2I_get(ret->exptext, d2i_DISPLAYTEXT);
594 M_ASN1_D2I_Finish(a, USERNOTICE_free, ASN1_F_D2I_USERNOTICE);
597 void USERNOTICE_free(USERNOTICE *a)
599 if (a == NULL) return;
600 NOTICEREF_free(a->noticeref);
601 DISPLAYTEXT_free(a->exptext);
605 int i2d_NOTICEREF(NOTICEREF *a, unsigned char **pp)
609 M_ASN1_I2D_len (a->organization, i2d_DISPLAYTEXT);
610 M_ASN1_I2D_len_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
612 M_ASN1_I2D_seq_total();
614 M_ASN1_I2D_put (a->organization, i2d_DISPLAYTEXT);
615 M_ASN1_I2D_put_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
620 NOTICEREF *NOTICEREF_new(void)
624 M_ASN1_New_Malloc(ret, NOTICEREF);
625 ret->organization = NULL;
626 ret->noticenos = NULL;
628 M_ASN1_New_Error(ASN1_F_NOTICEREF_NEW);
631 NOTICEREF *d2i_NOTICEREF(NOTICEREF **a, unsigned char **pp,long length)
633 M_ASN1_D2I_vars(a,NOTICEREF *,NOTICEREF_new);
635 M_ASN1_D2I_start_sequence();
636 /* This is to cope with some broken encodings that use IA5STRING for
637 * the organization field
639 M_ASN1_D2I_get_opt(ret->organization, d2i_ASN1_IA5STRING,
641 if(!ret->organization) {
642 M_ASN1_D2I_get(ret->organization, d2i_DISPLAYTEXT);
644 M_ASN1_D2I_get_seq(ret->noticenos, d2i_ASN1_INTEGER, ASN1_STRING_free);
645 M_ASN1_D2I_Finish(a, NOTICEREF_free, ASN1_F_D2I_NOTICEREF);
648 void NOTICEREF_free(NOTICEREF *a)
650 if (a == NULL) return;
651 DISPLAYTEXT_free(a->organization);
652 sk_pop_free(a->noticenos, ASN1_STRING_free);
656 IMPLEMENT_STACK_OF(POLICYQUALINFO)
657 IMPLEMENT_ASN1_SET_OF(POLICYQUALINFO)