xref: /openbsd-src/lib/libcrypto/asn1/t_x509.c (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1 /* $OpenBSD: t_x509.c,v 1.37 2021/12/25 13:17:48 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/opensslconf.h>
62 
63 #include <openssl/bn.h>
64 #include <openssl/buffer.h>
65 #include <openssl/err.h>
66 #include <openssl/objects.h>
67 #include <openssl/x509.h>
68 #include <openssl/x509v3.h>
69 
70 #ifndef OPENSSL_NO_DSA
71 #include <openssl/dsa.h>
72 #endif
73 #ifndef OPENSSL_NO_EC
74 #include <openssl/ec.h>
75 #endif
76 #ifndef OPENSSL_NO_RSA
77 #include <openssl/rsa.h>
78 #endif
79 
80 #include "asn1_locl.h"
81 #include "x509_lcl.h"
82 
83 int
84 X509_print_fp(FILE *fp, X509 *x)
85 {
86 	return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
87 }
88 
89 int
90 X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag)
91 {
92 	BIO *b;
93 	int ret;
94 
95 	if ((b = BIO_new(BIO_s_file())) == NULL) {
96 		X509error(ERR_R_BUF_LIB);
97 		return (0);
98 	}
99 	BIO_set_fp(b, fp, BIO_NOCLOSE);
100 	ret = X509_print_ex(b, x, nmflag, cflag);
101 	BIO_free(b);
102 	return (ret);
103 }
104 
105 int
106 X509_print(BIO *bp, X509 *x)
107 {
108 	return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
109 }
110 
111 int
112 X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
113 {
114 	long l;
115 	int ret = 0, i;
116 	char *m = NULL, mlch = ' ';
117 	int nmindent = 0;
118 	X509_CINF *ci;
119 	ASN1_INTEGER *bs;
120 	EVP_PKEY *pkey = NULL;
121 	const char *neg;
122 
123 	if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
124 		mlch = '\n';
125 		nmindent = 12;
126 	}
127 
128 	if (nmflags == X509_FLAG_COMPAT)
129 		nmindent = 16;
130 
131 	ci = x->cert_info;
132 	if (!(cflag & X509_FLAG_NO_HEADER)) {
133 		if (BIO_write(bp, "Certificate:\n", 13) <= 0)
134 			goto err;
135 		if (BIO_write(bp, "    Data:\n", 10) <= 0)
136 			goto err;
137 	}
138 	if (!(cflag & X509_FLAG_NO_VERSION)) {
139 		l = X509_get_version(x);
140 		if (BIO_printf(bp, "%8sVersion: %lu (0x%lx)\n",
141 		    "", l + 1, l) <= 0)
142 			goto err;
143 	}
144 	if (!(cflag & X509_FLAG_NO_SERIAL)) {
145 		if (BIO_write(bp, "        Serial Number:", 22) <= 0)
146 			goto err;
147 
148 		bs = X509_get_serialNumber(x);
149 		l = -1;
150 		if (bs->length <= (int)sizeof(long))
151 			l = ASN1_INTEGER_get(bs);
152 		if (l != -1) {
153 			if (bs->type == V_ASN1_NEG_INTEGER) {
154 				l = -l;
155 				neg = "-";
156 			} else
157 				neg = "";
158 			if (BIO_printf(bp, " %s%lu (%s0x%lx)\n",
159 			    neg, l, neg, l) <= 0)
160 				goto err;
161 		} else {
162 			neg = (bs->type == V_ASN1_NEG_INTEGER) ?
163 			    " (Negative)" : "";
164 			if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
165 				goto err;
166 			for (i = 0; i < bs->length; i++) {
167 				if (BIO_printf(bp, "%02x%c", bs->data[i],
168 				    ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
169 					goto err;
170 			}
171 		}
172 
173 	}
174 
175 	if (!(cflag & X509_FLAG_NO_SIGNAME)) {
176 		if (X509_signature_print(bp, x->sig_alg, NULL) <= 0)
177 			goto err;
178 	}
179 
180 	if (!(cflag & X509_FLAG_NO_ISSUER)) {
181 		if (BIO_printf(bp, "        Issuer:%c", mlch) <= 0)
182 			goto err;
183 		if (X509_NAME_print_ex(bp, X509_get_issuer_name(x),
184 		    nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
185 			goto err;
186 		if (BIO_write(bp, "\n", 1) <= 0)
187 			goto err;
188 	}
189 	if (!(cflag & X509_FLAG_NO_VALIDITY)) {
190 		if (BIO_write(bp, "        Validity\n", 17) <= 0)
191 			goto err;
192 		if (BIO_write(bp, "            Not Before: ", 24) <= 0)
193 			goto err;
194 		if (!ASN1_TIME_print(bp, X509_get_notBefore(x)))
195 			goto err;
196 		if (BIO_write(bp, "\n            Not After : ", 25) <= 0)
197 			goto err;
198 		if (!ASN1_TIME_print(bp, X509_get_notAfter(x)))
199 			goto err;
200 		if (BIO_write(bp, "\n", 1) <= 0)
201 			goto err;
202 	}
203 	if (!(cflag & X509_FLAG_NO_SUBJECT)) {
204 		if (BIO_printf(bp, "        Subject:%c", mlch) <= 0)
205 			goto err;
206 		if (X509_NAME_print_ex(bp, X509_get_subject_name(x),
207 		    nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
208 			goto err;
209 		if (BIO_write(bp, "\n", 1) <= 0)
210 			goto err;
211 	}
212 	if (!(cflag & X509_FLAG_NO_PUBKEY)) {
213 		if (BIO_write(bp, "        Subject Public Key Info:\n",
214 		    33) <= 0)
215 			goto err;
216 		if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
217 			goto err;
218 		if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
219 			goto err;
220 		if (BIO_puts(bp, "\n") <= 0)
221 			goto err;
222 
223 		pkey = X509_get_pubkey(x);
224 		if (pkey == NULL) {
225 			BIO_printf(bp, "%12sUnable to load Public Key\n", "");
226 			ERR_print_errors(bp);
227 		} else {
228 			EVP_PKEY_print_public(bp, pkey, 16, NULL);
229 			EVP_PKEY_free(pkey);
230 		}
231 	}
232 
233 	if (!(cflag & X509_FLAG_NO_EXTENSIONS))
234 		X509V3_extensions_print(bp, "X509v3 extensions",
235 		    ci->extensions, cflag, 8);
236 
237 	if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
238 		if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0)
239 			goto err;
240 	}
241 	if (!(cflag & X509_FLAG_NO_AUX)) {
242 		if (!X509_CERT_AUX_print(bp, x->aux, 0))
243 			goto err;
244 	}
245 	ret = 1;
246 
247  err:
248 	free(m);
249 	return (ret);
250 }
251 
252 int
253 X509_ocspid_print(BIO *bp, X509 *x)
254 {
255 	unsigned char *der = NULL;
256 	unsigned char *dertmp;
257 	int derlen;
258 	int i;
259 	unsigned char SHA1md[SHA_DIGEST_LENGTH];
260 
261 	/* display the hash of the subject as it would appear
262 	   in OCSP requests */
263 	if (BIO_printf(bp, "        Subject OCSP hash: ") <= 0)
264 		goto err;
265 	if ((derlen = i2d_X509_NAME(x->cert_info->subject, NULL)) <= 0)
266 		goto err;
267 	if ((der = dertmp = malloc(derlen)) == NULL)
268 		goto err;
269 	if (i2d_X509_NAME(x->cert_info->subject, &dertmp) <= 0)
270 		goto err;
271 
272 	if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
273 		goto err;
274 	for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
275 		if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
276 			goto err;
277 	}
278 	free (der);
279 	der = NULL;
280 
281 	/* display the hash of the public key as it would appear
282 	   in OCSP requests */
283 	if (BIO_printf(bp, "\n        Public key OCSP hash: ") <= 0)
284 		goto err;
285 
286 	if (!EVP_Digest(x->cert_info->key->public_key->data,
287 	    x->cert_info->key->public_key->length,
288 	    SHA1md, NULL, EVP_sha1(), NULL))
289 		goto err;
290 	for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
291 		if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
292 			goto err;
293 	}
294 	BIO_printf(bp, "\n");
295 
296 	return (1);
297 
298  err:
299 	free(der);
300 	return (0);
301 }
302 
303 int
304 X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
305 {
306 	const unsigned char *s;
307 	int i, n;
308 
309 	n = sig->length;
310 	s = sig->data;
311 	for (i = 0; i < n; i++) {
312 		if ((i % 18) == 0) {
313 			if (BIO_write(bp, "\n", 1) <= 0)
314 				return 0;
315 			if (BIO_indent(bp, indent, indent) <= 0)
316 				return 0;
317 		}
318 		if (BIO_printf(bp, "%02x%s", s[i],
319 		    ((i + 1) == n) ? "" : ":") <= 0)
320 			return 0;
321 	}
322 	if (BIO_write(bp, "\n", 1) != 1)
323 		return 0;
324 
325 	return 1;
326 }
327 
328 int
329 X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, const ASN1_STRING *sig)
330 {
331 	int sig_nid;
332 	if (BIO_puts(bp, "    Signature Algorithm: ") <= 0)
333 		return 0;
334 	if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
335 		return 0;
336 
337 	sig_nid = OBJ_obj2nid(sigalg->algorithm);
338 	if (sig_nid != NID_undef) {
339 		int pkey_nid, dig_nid;
340 		const EVP_PKEY_ASN1_METHOD *ameth;
341 		if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
342 			ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
343 			if (ameth && ameth->sig_print)
344 				return ameth->sig_print(bp, sigalg, sig, 9, 0);
345 		}
346 	}
347 	if (sig)
348 		return X509_signature_dump(bp, sig, 9);
349 	else if (BIO_puts(bp, "\n") <= 0)
350 		return 0;
351 	return 1;
352 }
353 
354 int
355 ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
356 {
357 	if (tm->type == V_ASN1_UTCTIME)
358 		return ASN1_UTCTIME_print(bp, tm);
359 	if (tm->type == V_ASN1_GENERALIZEDTIME)
360 		return ASN1_GENERALIZEDTIME_print(bp, tm);
361 	BIO_write(bp, "Bad time value", 14);
362 	return (0);
363 }
364 
365 static const char *mon[12] = {
366 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
367 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
368 };
369 
370 int
371 ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
372 {
373 	char *v;
374 	int gmt = 0;
375 	int i;
376 	int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
377 	char *f = "";
378 	int f_len = 0;
379 
380 	i = tm->length;
381 	v = (char *)tm->data;
382 
383 	if (i < 12)
384 		goto err;
385 	if (v[i-1] == 'Z')
386 		gmt = 1;
387 	for (i = 0; i < 12; i++)
388 		if ((v[i] > '9') || (v[i] < '0'))
389 			goto err;
390 	y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 +
391 	    (v[2] - '0') * 10 + (v[3] - '0');
392 	M = (v[4] - '0') * 10 + (v[5] - '0');
393 	if ((M > 12) || (M < 1))
394 		goto err;
395 	d = (v[6] - '0') * 10 + (v[7] - '0');
396 	h = (v[8] - '0') * 10 + (v[9] - '0');
397 	m = (v[10] - '0') * 10 + (v[11] - '0');
398 	if (tm->length >= 14 &&
399 	    (v[12] >= '0') && (v[12] <= '9') &&
400 	    (v[13] >= '0') && (v[13] <= '9')) {
401 		s =  (v[12] - '0') * 10 + (v[13] - '0');
402 		/* Check for fractions of seconds. */
403 		if (tm->length >= 15 && v[14] == '.') {
404 			int l = tm->length;
405 			f = &v[14];	/* The decimal point. */
406 			f_len = 1;
407 			while (14 + f_len < l && f[f_len] >= '0' &&
408 			    f[f_len] <= '9')
409 				++f_len;
410 		}
411 	}
412 
413 	if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
414 	    mon[M - 1], d, h, m, s, f_len, f, y, (gmt) ? " GMT" : "") <= 0)
415 		return (0);
416 	else
417 		return (1);
418 
419  err:
420 	BIO_write(bp, "Bad time value", 14);
421 	return (0);
422 }
423 
424 int
425 ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
426 {
427 	const char *v;
428 	int gmt = 0;
429 	int i;
430 	int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
431 
432 	i = tm->length;
433 	v = (const char *)tm->data;
434 
435 	if (i < 10)
436 		goto err;
437 	if (v[i-1] == 'Z')
438 		gmt = 1;
439 	for (i = 0; i < 10; i++)
440 		if ((v[i] > '9') || (v[i] < '0'))
441 			goto err;
442 	y = (v[0] - '0') * 10 + (v[1] - '0');
443 	if (y < 50)
444 		y += 100;
445 	M = (v[2] - '0') * 10 + (v[3] - '0');
446 	if ((M > 12) || (M < 1))
447 		goto err;
448 	d = (v[4] - '0') * 10 + (v[5] - '0');
449 	h = (v[6] - '0') * 10 + (v[7] - '0');
450 	m = (v[8] - '0') * 10 + (v[9] - '0');
451 	if (tm->length >=12 &&
452 	    (v[10] >= '0') && (v[10] <= '9') &&
453 	    (v[11] >= '0') && (v[11] <= '9'))
454 		s = (v[10] - '0') * 10 + (v[11] - '0');
455 
456 	if (BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
457 	    mon[M - 1], d, h, m, s, y + 1900, (gmt) ? " GMT" : "") <= 0)
458 		return (0);
459 	else
460 		return (1);
461 
462  err:
463 	BIO_write(bp, "Bad time value", 14);
464 	return (0);
465 }
466 
467 int
468 X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
469 {
470 	char *s, *c, *b;
471 	int ret = 0, l, i;
472 
473 	l = 80 - 2 - obase;
474 
475 	b = X509_NAME_oneline(name, NULL, 0);
476 	if (b == NULL)
477 		return 0;
478 	if (*b == '\0') {
479 		free(b);
480 		return 1;
481 	}
482 	s = b + 1; /* skip the first slash */
483 
484 	c = s;
485 	for (;;) {
486 		if (((*s == '/') &&
487 		    ((s[1] >= 'A') && (s[1] <= 'Z') &&
488 		    ((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') &&
489 		    (s[3] == '='))))) || (*s == '\0')) {
490 			i = s - c;
491 			if (BIO_write(bp, c, i) != i)
492 				goto err;
493 			c = s + 1;	/* skip following slash */
494 			if (*s != '\0') {
495 				if (BIO_write(bp, ", ", 2) != 2)
496 					goto err;
497 			}
498 			l--;
499 		}
500 		if (*s == '\0')
501 			break;
502 		s++;
503 		l--;
504 	}
505 
506 	ret = 1;
507 	if (0) {
508  err:
509 		X509error(ERR_R_BUF_LIB);
510 	}
511 	free(b);
512 	return (ret);
513 }
514