t_x509.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/asn1.h>
58 #include <openssl/bio.h>
59 #include <openssl/digest.h>
60 #include <openssl/err.h>
61 #include <openssl/evp.h>
62 #include <openssl/mem.h>
63 #include <openssl/obj.h>
64 #include <openssl/x509.h>
65 #include <openssl/x509v3.h>
66 
67 #include "internal.h"
68 
69 
70 int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
71  unsigned long cflag)
72 {
73  BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
74  if (b == NULL) {
76  return 0;
77  }
78  int ret = X509_print_ex(b, x, nmflag, cflag);
79  BIO_free(b);
80  return ret;
81 }
82 
84 {
86 }
87 
88 int X509_print(BIO *bp, X509 *x)
89 {
91 }
92 
93 int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
94  unsigned long cflag)
95 {
96  long l;
97  int ret = 0, i;
98  char *m = NULL, mlch = ' ';
99  int nmindent = 0;
100  X509_CINF *ci;
101  ASN1_INTEGER *bs;
102  EVP_PKEY *pkey = NULL;
103  const char *neg;
104 
105  if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
106  mlch = '\n';
107  nmindent = 12;
108  }
109 
110  if (nmflags == X509_FLAG_COMPAT)
111  nmindent = 16;
112 
113  ci = x->cert_info;
114  if (!(cflag & X509_FLAG_NO_HEADER)) {
115  if (BIO_write(bp, "Certificate:\n", 13) <= 0)
116  goto err;
117  if (BIO_write(bp, " Data:\n", 10) <= 0)
118  goto err;
119  }
120  if (!(cflag & X509_FLAG_NO_VERSION)) {
121  l = X509_get_version(x);
122  if (BIO_printf(bp, "%8sVersion: %lu (0x%lx)\n", "", l + 1, l) <= 0)
123  goto err;
124  }
125  if (!(cflag & X509_FLAG_NO_SERIAL)) {
126 
127  if (BIO_write(bp, " Serial Number:", 22) <= 0)
128  goto err;
129 
130  bs = X509_get_serialNumber(x);
131  if (bs->length < (int)sizeof(long)
132  || (bs->length == sizeof(long) && (bs->data[0] & 0x80) == 0)) {
133  l = ASN1_INTEGER_get(bs);
134  if (bs->type == V_ASN1_NEG_INTEGER) {
135  l = -l;
136  neg = "-";
137  } else
138  neg = "";
139  if (BIO_printf(bp, " %s%lu (%s0x%lx)\n", neg, l, neg, l) <= 0)
140  goto err;
141  } else {
142  neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
143  if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
144  goto err;
145 
146  for (i = 0; i < bs->length; i++) {
147  if (BIO_printf(bp, "%02x%c", bs->data[i],
148  ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
149  goto err;
150  }
151  }
152 
153  }
154 
155  if (!(cflag & X509_FLAG_NO_SIGNAME)) {
156  if (X509_signature_print(bp, ci->signature, NULL) <= 0)
157  goto err;
158  }
159 
160  if (!(cflag & X509_FLAG_NO_ISSUER)) {
161  if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
162  goto err;
163  if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)
164  < 0)
165  goto err;
166  if (BIO_write(bp, "\n", 1) <= 0)
167  goto err;
168  }
169  if (!(cflag & X509_FLAG_NO_VALIDITY)) {
170  if (BIO_write(bp, " Validity\n", 17) <= 0)
171  goto err;
172  if (BIO_write(bp, " Not Before: ", 24) <= 0)
173  goto err;
175  goto err;
176  if (BIO_write(bp, "\n Not After : ", 25) <= 0)
177  goto err;
179  goto err;
180  if (BIO_write(bp, "\n", 1) <= 0)
181  goto err;
182  }
183  if (!(cflag & X509_FLAG_NO_SUBJECT)) {
184  if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
185  goto err;
187  (bp, X509_get_subject_name(x), nmindent, nmflags) < 0)
188  goto err;
189  if (BIO_write(bp, "\n", 1) <= 0)
190  goto err;
191  }
192  if (!(cflag & X509_FLAG_NO_PUBKEY)) {
193  if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
194  goto err;
195  if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
196  goto err;
197  if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
198  goto err;
199  if (BIO_puts(bp, "\n") <= 0)
200  goto err;
201 
202  pkey = X509_get_pubkey(x);
203  if (pkey == NULL) {
204  BIO_printf(bp, "%12sUnable to load Public Key\n", "");
205  ERR_print_errors(bp);
206  } else {
207  EVP_PKEY_print_public(bp, pkey, 16, NULL);
208  EVP_PKEY_free(pkey);
209  }
210  }
211 
212  if (!(cflag & X509_FLAG_NO_IDS)) {
213  if (ci->issuerUID) {
214  if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)
215  goto err;
216  if (!X509_signature_dump(bp, ci->issuerUID, 12))
217  goto err;
218  }
219  if (ci->subjectUID) {
220  if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)
221  goto err;
222  if (!X509_signature_dump(bp, ci->subjectUID, 12))
223  goto err;
224  }
225  }
226 
227  if (!(cflag & X509_FLAG_NO_EXTENSIONS))
228  X509V3_extensions_print(bp, "X509v3 extensions",
229  ci->extensions, cflag, 8);
230 
231  if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
232  if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0)
233  goto err;
234  }
235  if (!(cflag & X509_FLAG_NO_AUX)) {
236  if (!X509_CERT_AUX_print(bp, x->aux, 0))
237  goto err;
238  }
239  ret = 1;
240  err:
241  if (m != NULL)
242  OPENSSL_free(m);
243  return (ret);
244 }
245 
247 {
248  unsigned char *der = NULL;
249  unsigned char *dertmp;
250  int derlen;
251  int i;
252  unsigned char SHA1md[SHA_DIGEST_LENGTH];
253 
254  /*
255  * display the hash of the subject as it would appear in OCSP requests
256  */
257  if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
258  goto err;
259  derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
260  if ((der = dertmp = (unsigned char *)OPENSSL_malloc(derlen)) == NULL)
261  goto err;
262  i2d_X509_NAME(x->cert_info->subject, &dertmp);
263 
264  if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
265  goto err;
266  for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
267  if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
268  goto err;
269  }
270  OPENSSL_free(der);
271  der = NULL;
272 
273  /*
274  * display the hash of the public key as it would appear in OCSP requests
275  */
276  if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
277  goto err;
278 
279  if (!EVP_Digest(x->cert_info->key->public_key->data,
280  x->cert_info->key->public_key->length,
281  SHA1md, NULL, EVP_sha1(), NULL))
282  goto err;
283  for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
284  if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
285  goto err;
286  }
287  BIO_printf(bp, "\n");
288 
289  return (1);
290  err:
291  if (der != NULL)
292  OPENSSL_free(der);
293  return (0);
294 }
295 
296 int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
297  const ASN1_STRING *sig)
298 {
299  if (BIO_puts(bp, " Signature Algorithm: ") <= 0)
300  return 0;
301  if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
302  return 0;
303 
304  /* RSA-PSS signatures have parameters to print. */
305  int sig_nid = OBJ_obj2nid(sigalg->algorithm);
306  if (sig_nid == NID_rsassaPss &&
307  !x509_print_rsa_pss_params(bp, sigalg, 9, 0)) {
308  return 0;
309  }
310 
311  if (sig)
312  return X509_signature_dump(bp, sig, 9);
313  else if (BIO_puts(bp, "\n") <= 0)
314  return 0;
315  return 1;
316 }
317 
318 int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
319 {
320  char *s, *c, *b;
321  int ret = 0, l, i;
322 
323  l = 80 - 2 - obase;
324 
325  b = X509_NAME_oneline(name, NULL, 0);
326  if (!b)
327  return 0;
328  if (!*b) {
329  OPENSSL_free(b);
330  return 1;
331  }
332  s = b + 1; /* skip the first slash */
333 
334  c = s;
335  for (;;) {
336  if (((*s == '/') &&
337  ((s[1] >= 'A') && (s[1] <= 'Z') && ((s[2] == '=') ||
338  ((s[2] >= 'A')
339  && (s[2] <= 'Z')
340  && (s[3] == '='))
341  ))) || (*s == '\0')) {
342  i = s - c;
343  if (BIO_write(bp, c, i) != i)
344  goto err;
345  c = s + 1; /* skip following slash */
346  if (*s != '\0') {
347  if (BIO_write(bp, ", ", 2) != 2)
348  goto err;
349  }
350  l--;
351  }
352  if (*s == '\0')
353  break;
354  s++;
355  l--;
356  }
357 
358  ret = 1;
359  if (0) {
360  err:
362  }
363  OPENSSL_free(b);
364  return (ret);
365 }
X509_NAME_print_ex
#define X509_NAME_print_ex
Definition: boringssl_prefix_symbols.h:2394
BIO_new_fp
#define BIO_new_fp
Definition: boringssl_prefix_symbols.h:819
X509V3_extensions_print
#define X509V3_extensions_print
Definition: boringssl_prefix_symbols.h:2239
X509_get_subject_name
#define X509_get_subject_name
Definition: boringssl_prefix_symbols.h:2672
X509_CINF::subjectUID
ASN1_BIT_STRING * subjectUID
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:132
bio_st
Definition: bio.h:822
X509_algor_st::algorithm
ASN1_OBJECT * algorithm
Definition: x509.h:114
evp.h
X509_FLAG_NO_SIGNAME
#define X509_FLAG_NO_SIGNAME
Definition: x509.h:191
X509_FLAG_NO_HEADER
#define X509_FLAG_NO_HEADER
Definition: x509.h:188
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
X509_CINF::issuerUID
ASN1_BIT_STRING * issuerUID
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:131
X509_ocspid_print
int X509_ocspid_print(BIO *bp, X509 *x)
Definition: t_x509.c:246
bio.h
X509_get_notBefore
#define X509_get_notBefore
Definition: boringssl_prefix_symbols.h:2667
ASN1_INTEGER_get
#define ASN1_INTEGER_get
Definition: boringssl_prefix_symbols.h:645
X509_FLAG_NO_ISSUER
#define X509_FLAG_NO_ISSUER
Definition: x509.h:192
error_ref_leak.err
err
Definition: error_ref_leak.py:35
x509v3.h
X509_FLAG_NO_VERSION
#define X509_FLAG_NO_VERSION
Definition: x509.h:189
EVP_PKEY_print_public
#define EVP_PKEY_print_public
Definition: boringssl_prefix_symbols.h:1650
X509_signature_dump
#define X509_signature_dump
Definition: boringssl_prefix_symbols.h:2721
setup.name
name
Definition: setup.py:542
BIO_write
#define BIO_write
Definition: boringssl_prefix_symbols.h:870
X509_FLAG_NO_SIGDUMP
#define X509_FLAG_NO_SIGDUMP
Definition: x509.h:197
X509_NAME_oneline
#define X509_NAME_oneline
Definition: boringssl_prefix_symbols.h:2392
asn1_string_st::data
unsigned char * data
Definition: asn1.h:546
BIO_printf
#define BIO_printf
Definition: boringssl_prefix_symbols.h:827
NID_rsassaPss
#define NID_rsassaPss
Definition: nid.h:4039
X509_FLAG_NO_AUX
#define X509_FLAG_NO_AUX
Definition: x509.h:198
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
X509_get_pubkey
#define X509_get_pubkey
Definition: boringssl_prefix_symbols.h:2669
OBJ_obj2nid
#define OBJ_obj2nid
Definition: boringssl_prefix_symbols.h:1857
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
X509_signature_print
int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, const ASN1_STRING *sig)
Definition: t_x509.c:296
evp_pkey_st
Definition: evp.h:1046
X509_get_issuer_name
#define X509_get_issuer_name
Definition: boringssl_prefix_symbols.h:2664
asn1_string_st::length
int length
Definition: asn1.h:544
XN_FLAG_SEP_MULTILINE
#define XN_FLAG_SEP_MULTILINE
Definition: x509.h:212
EVP_PKEY_free
#define EVP_PKEY_free
Definition: boringssl_prefix_symbols.h:1625
X509_get_notAfter
#define X509_get_notAfter
Definition: boringssl_prefix_symbols.h:2666
err.h
X509_FLAG_NO_VALIDITY
#define X509_FLAG_NO_VALIDITY
Definition: x509.h:193
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
internal.h
X509_CINF::key
X509_PUBKEY * key
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:130
X509_FLAG_NO_EXTENSIONS
#define X509_FLAG_NO_EXTENSIONS
Definition: x509.h:196
x509_print_rsa_pss_params
int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent, ASN1_PCTX *pctx)
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
ERR_R_BUF_LIB
#define ERR_R_BUF_LIB
Definition: err.h:335
X509_FLAG_NO_SERIAL
#define X509_FLAG_NO_SERIAL
Definition: x509.h:190
X509_print_ex
int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
Definition: t_x509.c:93
i2d_X509_NAME
#define i2d_X509_NAME
Definition: boringssl_prefix_symbols.h:3288
X509_CINF
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:123
BIO_free
#define BIO_free
Definition: boringssl_prefix_symbols.h:787
X509_FLAG_NO_SUBJECT
#define X509_FLAG_NO_SUBJECT
Definition: x509.h:194
X509_algor_st
Definition: x509.h:113
digest.h
i2a_ASN1_OBJECT
#define i2a_ASN1_OBJECT
Definition: boringssl_prefix_symbols.h:3172
benchmark.FILE
FILE
Definition: benchmark.py:21
EVP_Digest
#define EVP_Digest
Definition: boringssl_prefix_symbols.h:1506
x509_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:139
X509_print_ex_fp
int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag)
Definition: t_x509.c:70
X509_pubkey_st::algor
X509_ALGOR * algor
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:83
X509_CERT_AUX_print
#define X509_CERT_AUX_print
Definition: boringssl_prefix_symbols.h:2276
X509_FLAG_NO_IDS
#define X509_FLAG_NO_IDS
Definition: x509.h:200
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
X509_print_fp
int X509_print_fp(FILE *fp, X509 *x)
Definition: t_x509.c:83
XN_FLAG_SEP_MASK
#define XN_FLAG_SEP_MASK
Definition: x509.h:206
X509_get_version
#define X509_get_version
Definition: boringssl_prefix_symbols.h:2673
ASN1_TIME_print
#define ASN1_TIME_print
Definition: boringssl_prefix_symbols.h:706
BIO_puts
#define BIO_puts
Definition: boringssl_prefix_symbols.h:830
EVP_sha1
const OPENSSL_EXPORT EVP_MD * EVP_sha1(void)
SHA_DIGEST_LENGTH
#define SHA_DIGEST_LENGTH
Definition: sha.h:74
asn1_string_st::type
int type
Definition: asn1.h:545
X509_FLAG_NO_PUBKEY
#define X509_FLAG_NO_PUBKEY
Definition: x509.h:195
obj.h
BIO_NOCLOSE
#define BIO_NOCLOSE
Definition: bio.h:373
X509_get_serialNumber
#define X509_get_serialNumber
Definition: boringssl_prefix_symbols.h:2670
X509_CINF::signature
X509_ALGOR * signature
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:126
mem.h
run_grpclb_interop_tests.l
dictionary l
Definition: run_grpclb_interop_tests.py:410
regress.m
m
Definition: regress/regress.py:25
XN_FLAG_COMPAT
#define XN_FLAG_COMPAT
Definition: x509.h:208
X509_print
int X509_print(BIO *bp, X509 *x)
Definition: t_x509.c:88
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
X509_NAME_print
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
Definition: t_x509.c:318
V_ASN1_NEG_INTEGER
#define V_ASN1_NEG_INTEGER
Definition: asn1.h:156
X509_name_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:95
X509_FLAG_COMPAT
#define X509_FLAG_COMPAT
Definition: x509.h:187
asn1_string_st
Definition: asn1.h:543
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
x509.h
ERR_print_errors
#define ERR_print_errors
Definition: boringssl_prefix_symbols.h:1435


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:25