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 <assert.h>
58
59 #include <openssl/asn1.h>
60 #include <openssl/bio.h>
61 #include <openssl/digest.h>
62 #include <openssl/err.h>
63 #include <openssl/evp.h>
64 #include <openssl/mem.h>
65 #include <openssl/obj.h>
66 #include <openssl/x509.h>
67
68 #include "internal.h"
69
70
X509_print_ex_fp(FILE * fp,X509 * x,unsigned long nmflag,unsigned long cflag)71 int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
72 unsigned long cflag) {
73 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
74 if (b == NULL) {
75 OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
76 return 0;
77 }
78 int ret = X509_print_ex(b, x, nmflag, cflag);
79 BIO_free(b);
80 return ret;
81 }
82
X509_print_fp(FILE * fp,X509 * x)83 int X509_print_fp(FILE *fp, X509 *x) {
84 return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
85 }
86
X509_print(BIO * bp,X509 * x)87 int X509_print(BIO *bp, X509 *x) {
88 return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
89 }
90
X509_print_ex(BIO * bp,X509 * x,unsigned long nmflags,unsigned long cflag)91 int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
92 unsigned long cflag) {
93 char mlch = ' ';
94 int nmindent = 0;
95 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
96 mlch = '\n';
97 nmindent = 12;
98 }
99
100 if (nmflags == X509_FLAG_COMPAT) {
101 nmindent = 16;
102 }
103
104 const X509_CINF *ci = x->cert_info;
105 if (!(cflag & X509_FLAG_NO_HEADER)) {
106 if (BIO_write(bp, "Certificate:\n", 13) <= 0) {
107 return 0;
108 }
109 if (BIO_write(bp, " Data:\n", 10) <= 0) {
110 return 0;
111 }
112 }
113 if (!(cflag & X509_FLAG_NO_VERSION)) {
114 long l = X509_get_version(x);
115 assert(X509_VERSION_1 <= l && l <= X509_VERSION_3);
116 if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1,
117 (unsigned long)l) <= 0) {
118 return 0;
119 }
120 }
121 if (!(cflag & X509_FLAG_NO_SERIAL)) {
122 if (BIO_write(bp, " Serial Number:", 22) <= 0) {
123 return 0;
124 }
125
126 const ASN1_INTEGER *serial = X509_get0_serialNumber(x);
127 uint64_t serial_u64;
128 if (ASN1_INTEGER_get_uint64(&serial_u64, serial)) {
129 assert(serial->type != V_ASN1_NEG_INTEGER);
130 if (BIO_printf(bp, " %" PRIu64 " (0x%" PRIx64 ")\n", serial_u64,
131 serial_u64) <= 0) {
132 return 0;
133 }
134 } else {
135 ERR_clear_error(); // Clear |ASN1_INTEGER_get_uint64|'s error.
136 const char *neg =
137 (serial->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
138 if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0) {
139 return 0;
140 }
141
142 for (int i = 0; i < serial->length; i++) {
143 if (BIO_printf(bp, "%02x%c", serial->data[i],
144 ((i + 1 == serial->length) ? '\n' : ':')) <= 0) {
145 return 0;
146 }
147 }
148 }
149 }
150
151 if (!(cflag & X509_FLAG_NO_SIGNAME)) {
152 if (X509_signature_print(bp, ci->signature, NULL) <= 0) {
153 return 0;
154 }
155 }
156
157 if (!(cflag & X509_FLAG_NO_ISSUER)) {
158 if (BIO_printf(bp, " Issuer:%c", mlch) <= 0) {
159 return 0;
160 }
161 if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags) <
162 0) {
163 return 0;
164 }
165 if (BIO_write(bp, "\n", 1) <= 0) {
166 return 0;
167 }
168 }
169 if (!(cflag & X509_FLAG_NO_VALIDITY)) {
170 if (BIO_write(bp, " Validity\n", 17) <= 0) {
171 return 0;
172 }
173 if (BIO_write(bp, " Not Before: ", 24) <= 0) {
174 return 0;
175 }
176 if (!ASN1_TIME_print(bp, X509_get_notBefore(x))) {
177 return 0;
178 }
179 if (BIO_write(bp, "\n Not After : ", 25) <= 0) {
180 return 0;
181 }
182 if (!ASN1_TIME_print(bp, X509_get_notAfter(x))) {
183 return 0;
184 }
185 if (BIO_write(bp, "\n", 1) <= 0) {
186 return 0;
187 }
188 }
189 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
190 if (BIO_printf(bp, " Subject:%c", mlch) <= 0) {
191 return 0;
192 }
193 if (X509_NAME_print_ex(bp, X509_get_subject_name(x), nmindent, nmflags) <
194 0) {
195 return 0;
196 }
197 if (BIO_write(bp, "\n", 1) <= 0) {
198 return 0;
199 }
200 }
201 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
202 if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0) {
203 return 0;
204 }
205 if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0) {
206 return 0;
207 }
208 if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0) {
209 return 0;
210 }
211 if (BIO_puts(bp, "\n") <= 0) {
212 return 0;
213 }
214
215 const EVP_PKEY *pkey = X509_get0_pubkey(x);
216 if (pkey == NULL) {
217 BIO_printf(bp, "%12sUnable to load Public Key\n", "");
218 ERR_print_errors(bp);
219 } else {
220 EVP_PKEY_print_public(bp, pkey, 16, NULL);
221 }
222 }
223
224 if (!(cflag & X509_FLAG_NO_IDS)) {
225 if (ci->issuerUID) {
226 if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0) {
227 return 0;
228 }
229 if (!X509_signature_dump(bp, ci->issuerUID, 12)) {
230 return 0;
231 }
232 }
233 if (ci->subjectUID) {
234 if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0) {
235 return 0;
236 }
237 if (!X509_signature_dump(bp, ci->subjectUID, 12)) {
238 return 0;
239 }
240 }
241 }
242
243 if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
244 X509V3_extensions_print(bp, "X509v3 extensions", ci->extensions, cflag, 8);
245 }
246
247 if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
248 if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0) {
249 return 0;
250 }
251 }
252 if (!(cflag & X509_FLAG_NO_AUX)) {
253 if (!X509_CERT_AUX_print(bp, x->aux, 0)) {
254 return 0;
255 }
256 }
257
258 return 1;
259 }
260
X509_signature_print(BIO * bp,const X509_ALGOR * sigalg,const ASN1_STRING * sig)261 int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
262 const ASN1_STRING *sig) {
263 if (BIO_puts(bp, " Signature Algorithm: ") <= 0) {
264 return 0;
265 }
266 if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) {
267 return 0;
268 }
269
270 // RSA-PSS signatures have parameters to print.
271 int sig_nid = OBJ_obj2nid(sigalg->algorithm);
272 if (sig_nid == NID_rsassaPss &&
273 !x509_print_rsa_pss_params(bp, sigalg, 9, 0)) {
274 return 0;
275 }
276
277 if (sig) {
278 return X509_signature_dump(bp, sig, 9);
279 } else if (BIO_puts(bp, "\n") <= 0) {
280 return 0;
281 }
282 return 1;
283 }
284
X509_NAME_print(BIO * bp,const X509_NAME * name,int obase)285 int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) {
286 char *s, *c, *b;
287 int ret = 0, i;
288
289 b = X509_NAME_oneline(name, NULL, 0);
290 if (!b) {
291 return 0;
292 }
293 if (!*b) {
294 OPENSSL_free(b);
295 return 1;
296 }
297 s = b + 1; // skip the first slash
298
299 c = s;
300 for (;;) {
301 if (((*s == '/') && ((s[1] >= 'A') && (s[1] <= 'Z') &&
302 ((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') &&
303 (s[3] == '='))))) ||
304 (*s == '\0')) {
305 i = s - c;
306 if (BIO_write(bp, c, i) != i) {
307 goto err;
308 }
309 c = s + 1; // skip following slash
310 if (*s != '\0') {
311 if (BIO_write(bp, ", ", 2) != 2) {
312 goto err;
313 }
314 }
315 }
316 if (*s == '\0') {
317 break;
318 }
319 s++;
320 }
321
322 ret = 1;
323 if (0) {
324 err:
325 OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
326 }
327 OPENSSL_free(b);
328 return ret;
329 }
330