Update contrib.
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2004 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).
59 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
65 #include <openssl/conf.h>
66 #include <openssl/asn1.h>
67 #include <openssl/asn1t.h>
68 #include <openssl/x509v3.h>
71 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
72 #include "libcrypto_wsd_macros.h"
73 #include "libcrypto_wsd.h"
76 /* Certificate policies extension support: this one is a bit complex... */
78 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol, BIO *out, int indent);
79 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *value);
80 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, int indent);
81 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
82 static POLICYINFO *policy_section(X509V3_CTX *ctx,
83 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
84 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
85 STACK_OF(CONF_VALUE) *unot, int ia5org);
86 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
89 X509V3_EXT_METHOD v3_cpols = {
90 NID_certificate_policies, 0,ASN1_ITEM_ref(CERTIFICATEPOLICIES),
94 (X509V3_EXT_I2R)i2r_certpol,
95 (X509V3_EXT_R2I)r2i_certpol,
99 const X509V3_EXT_METHOD v3_cpols = {
100 NID_certificate_policies, 0,ASN1_ITEM_ref(CERTIFICATEPOLICIES),
104 (X509V3_EXT_I2R)i2r_certpol,
105 (X509V3_EXT_R2I)r2i_certpol,
109 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
110 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
111 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
113 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
115 ASN1_SEQUENCE(POLICYINFO) = {
116 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
117 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
118 } ASN1_SEQUENCE_END(POLICYINFO)
120 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
122 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
124 ASN1_ADB(POLICYQUALINFO) = {
125 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
126 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
127 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
129 ASN1_SEQUENCE(POLICYQUALINFO) = {
130 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
131 ASN1_ADB_OBJECT(POLICYQUALINFO)
132 } ASN1_SEQUENCE_END(POLICYQUALINFO)
134 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
136 ASN1_SEQUENCE(USERNOTICE) = {
137 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
138 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
139 } ASN1_SEQUENCE_END(USERNOTICE)
141 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
143 ASN1_SEQUENCE(NOTICEREF) = {
144 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
145 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
146 } ASN1_SEQUENCE_END(NOTICEREF)
148 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
150 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
151 X509V3_CTX *ctx, char *value)
153 STACK_OF(POLICYINFO) *pols = NULL;
157 STACK_OF(CONF_VALUE) *vals;
160 pols = sk_POLICYINFO_new_null();
162 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
165 vals = X509V3_parse_list(value);
167 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
171 for(i = 0; i < sk_CONF_VALUE_num(vals); i++) {
172 cnf = sk_CONF_VALUE_value(vals, i);
173 if(cnf->value || !cnf->name ) {
174 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
175 X509V3_conf_err(cnf);
179 if(!strcmp(pstr,"ia5org")) {
182 } else if(*pstr == '@') {
183 STACK_OF(CONF_VALUE) *polsect;
184 polsect = X509V3_get_section(ctx, pstr + 1);
186 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_SECTION);
188 X509V3_conf_err(cnf);
191 pol = policy_section(ctx, polsect, ia5org);
192 X509V3_section_free(ctx, polsect);
195 if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
196 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
197 X509V3_conf_err(cnf);
200 pol = POLICYINFO_new();
201 pol->policyid = pobj;
203 sk_POLICYINFO_push(pols, pol);
205 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
208 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
209 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
213 static POLICYINFO *policy_section(X509V3_CTX *ctx,
214 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
219 POLICYQUALINFO *qual;
220 if(!(pol = POLICYINFO_new())) goto merr;
221 for(i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
222 cnf = sk_CONF_VALUE_value(polstrs, i);
223 if(!strcmp(cnf->name, "policyIdentifier")) {
225 if(!(pobj = OBJ_txt2obj(cnf->value, 0))) {
226 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OBJECT_IDENTIFIER);
227 X509V3_conf_err(cnf);
230 pol->policyid = pobj;
232 } else if(!name_cmp(cnf->name, "CPS")) {
233 if(!pol->qualifiers) pol->qualifiers =
234 sk_POLICYQUALINFO_new_null();
235 if(!(qual = POLICYQUALINFO_new())) goto merr;
236 if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
238 qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
239 qual->d.cpsuri = M_ASN1_IA5STRING_new();
240 if(!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
241 strlen(cnf->value))) goto merr;
242 } else if(!name_cmp(cnf->name, "userNotice")) {
243 STACK_OF(CONF_VALUE) *unot;
244 if(*cnf->value != '@') {
245 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_EXPECTED_A_SECTION_NAME);
246 X509V3_conf_err(cnf);
249 unot = X509V3_get_section(ctx, cnf->value + 1);
251 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_SECTION);
253 X509V3_conf_err(cnf);
256 qual = notice_section(ctx, unot, ia5org);
257 X509V3_section_free(ctx, unot);
259 if(!pol->qualifiers) pol->qualifiers =
260 sk_POLICYQUALINFO_new_null();
261 if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
264 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OPTION);
266 X509V3_conf_err(cnf);
271 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_NO_POLICY_IDENTIFIER);
278 X509V3err(X509V3_F_POLICY_SECTION,ERR_R_MALLOC_FAILURE);
281 POLICYINFO_free(pol);
287 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
288 STACK_OF(CONF_VALUE) *unot, int ia5org)
293 POLICYQUALINFO *qual;
294 if(!(qual = POLICYQUALINFO_new())) goto merr;
295 qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
296 if(!(not = USERNOTICE_new())) goto merr;
297 qual->d.usernotice = not;
298 for(i = 0; i < sk_CONF_VALUE_num(unot); i++) {
299 cnf = sk_CONF_VALUE_value(unot, i);
300 if(!strcmp(cnf->name, "explicitText")) {
301 not->exptext = M_ASN1_VISIBLESTRING_new();
302 if(!ASN1_STRING_set(not->exptext, cnf->value,
303 strlen(cnf->value))) goto merr;
304 } else if(!strcmp(cnf->name, "organization")) {
306 if(!not->noticeref) {
307 if(!(nref = NOTICEREF_new())) goto merr;
308 not->noticeref = nref;
309 } else nref = not->noticeref;
310 if(ia5org) nref->organization->type = V_ASN1_IA5STRING;
311 else nref->organization->type = V_ASN1_VISIBLESTRING;
312 if(!ASN1_STRING_set(nref->organization, cnf->value,
313 strlen(cnf->value))) goto merr;
314 } else if(!strcmp(cnf->name, "noticeNumbers")) {
316 STACK_OF(CONF_VALUE) *nos;
317 if(!not->noticeref) {
318 if(!(nref = NOTICEREF_new())) goto merr;
319 not->noticeref = nref;
320 } else nref = not->noticeref;
321 nos = X509V3_parse_list(cnf->value);
322 if(!nos || !sk_CONF_VALUE_num(nos)) {
323 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_NUMBERS);
324 X509V3_conf_err(cnf);
327 ret = nref_nos(nref->noticenos, nos);
328 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
332 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_OPTION);
333 X509V3_conf_err(cnf);
339 (!not->noticeref->noticenos || !not->noticeref->organization)) {
340 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
347 X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
350 POLICYQUALINFO_free(qual);
354 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
361 for(i = 0; i < sk_CONF_VALUE_num(nos); i++) {
362 cnf = sk_CONF_VALUE_value(nos, i);
363 if(!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
364 X509V3err(X509V3_F_NREF_NOS,X509V3_R_INVALID_NUMBER);
367 if(!sk_ASN1_INTEGER_push(nnums, aint)) goto merr;
372 X509V3err(X509V3_F_NREF_NOS,ERR_R_MALLOC_FAILURE);
375 sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
380 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
381 BIO *out, int indent)
385 /* First print out the policy OIDs */
386 for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
387 pinfo = sk_POLICYINFO_value(pol, i);
388 BIO_printf(out, "%*sPolicy: ", indent, "");
389 i2a_ASN1_OBJECT(out, pinfo->policyid);
391 if(pinfo->qualifiers)
392 print_qualifiers(out, pinfo->qualifiers, indent + 2);
397 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
400 POLICYQUALINFO *qualinfo;
402 for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
403 qualinfo = sk_POLICYQUALINFO_value(quals, i);
404 switch(OBJ_obj2nid(qualinfo->pqualid))
407 BIO_printf(out, "%*sCPS: %s\n", indent, "",
408 qualinfo->d.cpsuri->data);
411 case NID_id_qt_unotice:
412 BIO_printf(out, "%*sUser Notice:\n", indent, "");
413 print_notice(out, qualinfo->d.usernotice, indent + 2);
417 BIO_printf(out, "%*sUnknown Qualifier: ",
420 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
427 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
430 if(notice->noticeref) {
432 ref = notice->noticeref;
433 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
434 ref->organization->data);
435 BIO_printf(out, "%*sNumber%s: ", indent, "",
436 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
437 for(i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
440 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
441 if(i) BIO_puts(out, ", ");
442 tmp = i2s_ASN1_INTEGER(NULL, num);
449 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
450 notice->exptext->data);
453 EXPORT_C void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
455 const X509_POLICY_DATA *dat = node->data;
457 BIO_printf(out, "%*sPolicy: ", indent, "");
459 i2a_ASN1_OBJECT(out, dat->valid_policy);
461 BIO_printf(out, "%*s%s\n", indent + 2, "",
462 node_data_critical(dat) ? "Critical" : "Non Critical");
463 if (dat->qualifier_set)
464 print_qualifiers(out, dat->qualifier_set, indent + 2);
466 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");