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