xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/x509/name_print.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/x509.h>
58 
59 #include <assert.h>
60 #include <inttypes.h>
61 #include <string.h>
62 
63 #include <openssl/asn1.h>
64 #include <openssl/bio.h>
65 #include <openssl/obj.h>
66 
67 
maybe_write(BIO * out,const void * buf,int len)68 static int maybe_write(BIO *out, const void *buf, int len) {
69   // If |out| is NULL, ignore the output but report the length.
70   return out == NULL || BIO_write(out, buf, len) == len;
71 }
72 
73 // do_indent prints |indent| spaces to |out|.
do_indent(BIO * out,int indent)74 static int do_indent(BIO *out, int indent) {
75   for (int i = 0; i < indent; i++) {
76     if (!maybe_write(out, " ", 1)) {
77       return 0;
78     }
79   }
80   return 1;
81 }
82 
83 #define FN_WIDTH_LN 25
84 #define FN_WIDTH_SN 10
85 
do_name_ex(BIO * out,const X509_NAME * n,int indent,unsigned long flags)86 static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
87                       unsigned long flags) {
88   int prev = -1, orflags;
89   char objtmp[80];
90   const char *objbuf;
91   int outlen, len;
92   const char *sep_dn, *sep_mv, *sep_eq;
93   int sep_dn_len, sep_mv_len, sep_eq_len;
94   if (indent < 0) {
95     indent = 0;
96   }
97   outlen = indent;
98   if (!do_indent(out, indent)) {
99     return -1;
100   }
101   switch (flags & XN_FLAG_SEP_MASK) {
102     case XN_FLAG_SEP_MULTILINE:
103       sep_dn = "\n";
104       sep_dn_len = 1;
105       sep_mv = " + ";
106       sep_mv_len = 3;
107       break;
108 
109     case XN_FLAG_SEP_COMMA_PLUS:
110       sep_dn = ",";
111       sep_dn_len = 1;
112       sep_mv = "+";
113       sep_mv_len = 1;
114       indent = 0;
115       break;
116 
117     case XN_FLAG_SEP_CPLUS_SPC:
118       sep_dn = ", ";
119       sep_dn_len = 2;
120       sep_mv = " + ";
121       sep_mv_len = 3;
122       indent = 0;
123       break;
124 
125     case XN_FLAG_SEP_SPLUS_SPC:
126       sep_dn = "; ";
127       sep_dn_len = 2;
128       sep_mv = " + ";
129       sep_mv_len = 3;
130       indent = 0;
131       break;
132 
133     default:
134       return -1;
135   }
136 
137   if (flags & XN_FLAG_SPC_EQ) {
138     sep_eq = " = ";
139     sep_eq_len = 3;
140   } else {
141     sep_eq = "=";
142     sep_eq_len = 1;
143   }
144 
145   int cnt = X509_NAME_entry_count(n);
146   for (int i = 0; i < cnt; i++) {
147     const X509_NAME_ENTRY *ent;
148     if (flags & XN_FLAG_DN_REV) {
149       ent = X509_NAME_get_entry(n, cnt - i - 1);
150     } else {
151       ent = X509_NAME_get_entry(n, i);
152     }
153     if (prev != -1) {
154       if (prev == X509_NAME_ENTRY_set(ent)) {
155         if (!maybe_write(out, sep_mv, sep_mv_len)) {
156           return -1;
157         }
158         outlen += sep_mv_len;
159       } else {
160         if (!maybe_write(out, sep_dn, sep_dn_len)) {
161           return -1;
162         }
163         outlen += sep_dn_len;
164         if (!do_indent(out, indent)) {
165           return -1;
166         }
167         outlen += indent;
168       }
169     }
170     prev = X509_NAME_ENTRY_set(ent);
171     const ASN1_OBJECT *fn = X509_NAME_ENTRY_get_object(ent);
172     const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
173     assert((flags & XN_FLAG_FN_MASK) == XN_FLAG_FN_SN);
174     int fn_nid = OBJ_obj2nid(fn);
175     if (fn_nid == NID_undef) {
176       OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
177       objbuf = objtmp;
178     } else {
179       objbuf = OBJ_nid2sn(fn_nid);
180     }
181     int objlen = strlen(objbuf);
182     if (!maybe_write(out, objbuf, objlen) ||
183         !maybe_write(out, sep_eq, sep_eq_len)) {
184       return -1;
185     }
186     outlen += objlen + sep_eq_len;
187     // If the field name is unknown then fix up the DER dump flag. We
188     // might want to limit this further so it will DER dump on anything
189     // other than a few 'standard' fields.
190     if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS)) {
191       orflags = ASN1_STRFLGS_DUMP_ALL;
192     } else {
193       orflags = 0;
194     }
195 
196     len = ASN1_STRING_print_ex(out, val, flags | orflags);
197     if (len < 0) {
198       return -1;
199     }
200     outlen += len;
201   }
202   return outlen;
203 }
204 
X509_NAME_print_ex(BIO * out,const X509_NAME * nm,int indent,unsigned long flags)205 int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
206                        unsigned long flags) {
207   if (flags == XN_FLAG_COMPAT) {
208     return X509_NAME_print(out, nm, indent);
209   }
210   return do_name_ex(out, nm, indent, flags);
211 }
212 
X509_NAME_print_ex_fp(FILE * fp,const X509_NAME * nm,int indent,unsigned long flags)213 int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,
214                           unsigned long flags) {
215   BIO *bio = NULL;
216   if (fp != NULL) {
217     // If |fp| is NULL, this function returns the number of bytes without
218     // writing.
219     bio = BIO_new_fp(fp, BIO_NOCLOSE);
220     if (bio == NULL) {
221       return -1;
222     }
223   }
224   int ret = X509_NAME_print_ex(bio, nm, indent, flags);
225   BIO_free(bio);
226   return ret;
227 }
228