name_print.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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 <inttypes.h>
60 #include <string.h>
61 
62 #include <openssl/asn1.h>
63 #include <openssl/bio.h>
64 #include <openssl/obj.h>
65 
66 
67 static int maybe_write(BIO *out, const void *buf, int len)
68 {
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|. */
74 static int do_indent(BIO *out, int indent)
75 {
76  for (int i = 0; i < indent; i++) {
77  if (!maybe_write(out, " ", 1)) {
78  return 0;
79  }
80  }
81  return 1;
82 }
83 
84 #define FN_WIDTH_LN 25
85 #define FN_WIDTH_SN 10
86 
87 static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
88  unsigned long flags)
89 {
90  int i, prev = -1, orflags, cnt;
91  int fn_opt, fn_nid;
92  ASN1_OBJECT *fn;
93  ASN1_STRING *val;
94  X509_NAME_ENTRY *ent;
95  char objtmp[80];
96  const char *objbuf;
97  int outlen, len;
98  const char *sep_dn, *sep_mv, *sep_eq;
99  int sep_dn_len, sep_mv_len, sep_eq_len;
100  if (indent < 0)
101  indent = 0;
102  outlen = indent;
103  if (!do_indent(out, indent))
104  return -1;
105  switch (flags & XN_FLAG_SEP_MASK) {
107  sep_dn = "\n";
108  sep_dn_len = 1;
109  sep_mv = " + ";
110  sep_mv_len = 3;
111  break;
112 
114  sep_dn = ",";
115  sep_dn_len = 1;
116  sep_mv = "+";
117  sep_mv_len = 1;
118  indent = 0;
119  break;
120 
122  sep_dn = ", ";
123  sep_dn_len = 2;
124  sep_mv = " + ";
125  sep_mv_len = 3;
126  indent = 0;
127  break;
128 
130  sep_dn = "; ";
131  sep_dn_len = 2;
132  sep_mv = " + ";
133  sep_mv_len = 3;
134  indent = 0;
135  break;
136 
137  default:
138  return -1;
139  }
140 
141  if (flags & XN_FLAG_SPC_EQ) {
142  sep_eq = " = ";
143  sep_eq_len = 3;
144  } else {
145  sep_eq = "=";
146  sep_eq_len = 1;
147  }
148 
149  fn_opt = flags & XN_FLAG_FN_MASK;
150 
151  cnt = X509_NAME_entry_count(n);
152  for (i = 0; i < cnt; i++) {
153  if (flags & XN_FLAG_DN_REV)
154  ent = X509_NAME_get_entry(n, cnt - i - 1);
155  else
156  ent = X509_NAME_get_entry(n, i);
157  if (prev != -1) {
158  if (prev == X509_NAME_ENTRY_set(ent)) {
159  if (!maybe_write(out, sep_mv, sep_mv_len))
160  return -1;
161  outlen += sep_mv_len;
162  } else {
163  if (!maybe_write(out, sep_dn, sep_dn_len))
164  return -1;
165  outlen += sep_dn_len;
166  if (!do_indent(out, indent))
167  return -1;
168  outlen += indent;
169  }
170  }
171  prev = X509_NAME_ENTRY_set(ent);
173  val = X509_NAME_ENTRY_get_data(ent);
174  fn_nid = OBJ_obj2nid(fn);
175  if (fn_opt != XN_FLAG_FN_NONE) {
176  int objlen, fld_len;
177  if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
178  OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
179  fld_len = 0; /* XXX: what should this be? */
180  objbuf = objtmp;
181  } else {
182  if (fn_opt == XN_FLAG_FN_SN) {
183  fld_len = FN_WIDTH_SN;
184  objbuf = OBJ_nid2sn(fn_nid);
185  } else if (fn_opt == XN_FLAG_FN_LN) {
186  fld_len = FN_WIDTH_LN;
187  objbuf = OBJ_nid2ln(fn_nid);
188  } else {
189  fld_len = 0; /* XXX: what should this be? */
190  objbuf = "";
191  }
192  }
193  objlen = strlen(objbuf);
194  if (!maybe_write(out, objbuf, objlen))
195  return -1;
196  if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
197  if (!do_indent(out, fld_len - objlen))
198  return -1;
199  outlen += fld_len - objlen;
200  }
201  if (!maybe_write(out, sep_eq, sep_eq_len))
202  return -1;
203  outlen += objlen + sep_eq_len;
204  }
205  /*
206  * If the field name is unknown then fix up the DER dump flag. We
207  * might want to limit this further so it will DER dump on anything
208  * other than a few 'standard' fields.
209  */
210  if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
211  orflags = ASN1_STRFLGS_DUMP_ALL;
212  else
213  orflags = 0;
214 
215  len = ASN1_STRING_print_ex(out, val, flags | orflags);
216  if (len < 0)
217  return -1;
218  outlen += len;
219  }
220  return outlen;
221 }
222 
224  unsigned long flags)
225 {
226  if (flags == XN_FLAG_COMPAT)
227  return X509_NAME_print(out, nm, indent);
228  return do_name_ex(out, nm, indent, flags);
229 }
230 
232  unsigned long flags)
233 {
234  BIO *bio = NULL;
235  if (fp != NULL) {
236  /* If |fp| is NULL, this function returns the number of bytes without
237  * writing. */
238  bio = BIO_new_fp(fp, BIO_NOCLOSE);
239  if (bio == NULL) {
240  return -1;
241  }
242  }
243  int ret = X509_NAME_print_ex(bio, nm, indent, flags);
244  BIO_free(bio);
245  return ret;
246 }
BIO_new_fp
#define BIO_new_fp
Definition: boringssl_prefix_symbols.h:819
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
bio_st
Definition: bio.h:822
X509_NAME_entry_count
#define X509_NAME_entry_count
Definition: boringssl_prefix_symbols.h:2380
XN_FLAG_FN_LN
#define XN_FLAG_FN_LN
Definition: x509.h:221
XN_FLAG_DN_REV
#define XN_FLAG_DN_REV
Definition: x509.h:214
maybe_write
static int maybe_write(BIO *out, const void *buf, int len)
Definition: name_print.c:67
bio.h
string.h
XN_FLAG_FN_SN
#define XN_FLAG_FN_SN
Definition: x509.h:220
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
BIO_write
#define BIO_write
Definition: boringssl_prefix_symbols.h:870
python_utils.upload_rbe_results.indent
indent
Definition: upload_rbe_results.py:183
asn1_object_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:102
X509_NAME_ENTRY_set
#define X509_NAME_ENTRY_set
Definition: boringssl_prefix_symbols.h:2368
XN_FLAG_FN_ALIGN
#define XN_FLAG_FN_ALIGN
Definition: x509.h:232
OBJ_obj2nid
#define OBJ_obj2nid
Definition: boringssl_prefix_symbols.h:1857
XN_FLAG_SEP_COMMA_PLUS
#define XN_FLAG_SEP_COMMA_PLUS
Definition: x509.h:209
generate-asm-lcov.fn
fn
Definition: generate-asm-lcov.py:146
X509_NAME_get_entry
#define X509_NAME_get_entry
Definition: boringssl_prefix_symbols.h:2383
XN_FLAG_SEP_MULTILINE
#define XN_FLAG_SEP_MULTILINE
Definition: x509.h:212
FN_WIDTH_SN
#define FN_WIDTH_SN
Definition: name_print.c:85
X509_NAME_ENTRY_get_data
#define X509_NAME_ENTRY_get_data
Definition: boringssl_prefix_symbols.h:2364
X509_NAME_print_ex_fp
int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, unsigned long flags)
Definition: name_print.c:231
NID_undef
#define NID_undef
Definition: nid.h:85
FN_WIDTH_LN
#define FN_WIDTH_LN
Definition: name_print.c:84
XN_FLAG_SPC_EQ
#define XN_FLAG_SPC_EQ
Definition: x509.h:225
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
X509_name_entry_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:88
do_name_ex
static int do_name_ex(BIO *out, const X509_NAME *n, int indent, unsigned long flags)
Definition: name_print.c:87
XN_FLAG_FN_MASK
#define XN_FLAG_FN_MASK
Definition: x509.h:218
BIO_free
#define BIO_free
Definition: boringssl_prefix_symbols.h:787
benchmark.FILE
FILE
Definition: benchmark.py:21
XN_FLAG_SEP_CPLUS_SPC
#define XN_FLAG_SEP_CPLUS_SPC
Definition: x509.h:210
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
XN_FLAG_FN_OID
#define XN_FLAG_FN_OID
Definition: x509.h:222
XN_FLAG_SEP_SPLUS_SPC
#define XN_FLAG_SEP_SPLUS_SPC
Definition: x509.h:211
OBJ_nid2sn
#define OBJ_nid2sn
Definition: boringssl_prefix_symbols.h:1856
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
XN_FLAG_SEP_MASK
#define XN_FLAG_SEP_MASK
Definition: x509.h:206
X509_NAME_ENTRY_get_object
#define X509_NAME_ENTRY_get_object
Definition: boringssl_prefix_symbols.h:2365
XN_FLAG_FN_NONE
#define XN_FLAG_FN_NONE
Definition: x509.h:223
OBJ_obj2txt
#define OBJ_obj2txt
Definition: boringssl_prefix_symbols.h:1858
obj.h
BIO_NOCLOSE
#define BIO_NOCLOSE
Definition: bio.h:373
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
XN_FLAG_DUMP_UNKNOWN_FIELDS
#define XN_FLAG_DUMP_UNKNOWN_FIELDS
Definition: x509.h:230
do_indent
static int do_indent(BIO *out, int indent)
Definition: name_print.c:74
ASN1_STRING_print_ex
#define ASN1_STRING_print_ex
Definition: boringssl_prefix_symbols.h:686
XN_FLAG_COMPAT
#define XN_FLAG_COMPAT
Definition: x509.h:208
X509_NAME_print_ex
int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, unsigned long flags)
Definition: name_print.c:223
X509_NAME_print
#define X509_NAME_print
Definition: boringssl_prefix_symbols.h:2393
OBJ_nid2ln
#define OBJ_nid2ln
Definition: boringssl_prefix_symbols.h:1854
X509_name_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:95
asn1_string_st
Definition: asn1.h:543
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
x509.h
ASN1_STRFLGS_DUMP_ALL
#define ASN1_STRFLGS_DUMP_ALL
Definition: asn1.h:1655


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:31