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