t_x509.c revision 1.44
1/* $OpenBSD: t_x509.c,v 1.44 2023/12/29 10:59:00 tb 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 "evp_local.h"
81#include "x509_local.h"
82
83int
84X509_print_fp(FILE *fp, X509 *x)
85{
86	return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
87}
88
89int
90X509_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
105int
106X509_print(BIO *bp, X509 *x)
107{
108	return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
109}
110
111int
112X509_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
122	if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
123		mlch = '\n';
124		nmindent = 12;
125	}
126
127	if (nmflags == X509_FLAG_COMPAT)
128		nmindent = 16;
129
130	ci = x->cert_info;
131	if (!(cflag & X509_FLAG_NO_HEADER)) {
132		if (BIO_write(bp, "Certificate:\n", 13) <= 0)
133			goto err;
134		if (BIO_write(bp, "    Data:\n", 10) <= 0)
135			goto err;
136	}
137	if (!(cflag & X509_FLAG_NO_VERSION)) {
138		l = X509_get_version(x);
139		if (l >= 0 && l <= 2) {
140			if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n",
141			    "", l + 1, l) <= 0)
142				goto err;
143		} else {
144			if (BIO_printf(bp, "%8sVersion: unknown (%ld)\n",
145			    "", l) <= 0)
146				goto err;
147		}
148	}
149	if (!(cflag & X509_FLAG_NO_SERIAL)) {
150		if (BIO_write(bp, "        Serial Number:", 22) <= 0)
151			goto err;
152
153		bs = X509_get_serialNumber(x);
154		l = -1;
155		if (bs->length <= (int)sizeof(long))
156			l = ASN1_INTEGER_get(bs);
157		if (l >= 0) {
158			if (BIO_printf(bp, " %ld (0x%lx)\n", l, l) <= 0)
159				goto err;
160		} else {
161			const char *neg = "";
162
163			if (bs->type == V_ASN1_NEG_INTEGER)
164				neg = " (Negative)";
165
166			if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
167				goto err;
168			for (i = 0; i < bs->length; i++) {
169				if (BIO_printf(bp, "%02x%c", bs->data[i],
170				    ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
171					goto err;
172			}
173		}
174
175	}
176
177	if (!(cflag & X509_FLAG_NO_SIGNAME)) {
178		if (X509_signature_print(bp, x->sig_alg, NULL) <= 0)
179			goto err;
180	}
181
182	if (!(cflag & X509_FLAG_NO_ISSUER)) {
183		if (BIO_printf(bp, "        Issuer:%c", mlch) <= 0)
184			goto err;
185		if (X509_NAME_print_ex(bp, X509_get_issuer_name(x),
186		    nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
187			goto err;
188		if (BIO_write(bp, "\n", 1) <= 0)
189			goto err;
190	}
191	if (!(cflag & X509_FLAG_NO_VALIDITY)) {
192		if (BIO_write(bp, "        Validity\n", 17) <= 0)
193			goto err;
194		if (BIO_write(bp, "            Not Before: ", 24) <= 0)
195			goto err;
196		if (!ASN1_TIME_print(bp, X509_get_notBefore(x)))
197			goto err;
198		if (BIO_write(bp, "\n            Not After : ", 25) <= 0)
199			goto err;
200		if (!ASN1_TIME_print(bp, X509_get_notAfter(x)))
201			goto err;
202		if (BIO_write(bp, "\n", 1) <= 0)
203			goto err;
204	}
205	if (!(cflag & X509_FLAG_NO_SUBJECT)) {
206		if (BIO_printf(bp, "        Subject:%c", mlch) <= 0)
207			goto err;
208		if (X509_NAME_print_ex(bp, X509_get_subject_name(x),
209		    nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
210			goto err;
211		if (BIO_write(bp, "\n", 1) <= 0)
212			goto err;
213	}
214	if (!(cflag & X509_FLAG_NO_PUBKEY)) {
215		if (BIO_write(bp, "        Subject Public Key Info:\n",
216		    33) <= 0)
217			goto err;
218		if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
219			goto err;
220		if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
221			goto err;
222		if (BIO_puts(bp, "\n") <= 0)
223			goto err;
224
225		pkey = X509_get_pubkey(x);
226		if (pkey == NULL) {
227			BIO_printf(bp, "%12sUnable to load Public Key\n", "");
228			ERR_print_errors(bp);
229		} else {
230			EVP_PKEY_print_public(bp, pkey, 16, NULL);
231			EVP_PKEY_free(pkey);
232		}
233	}
234
235	if (!(cflag & X509_FLAG_NO_EXTENSIONS))
236		X509V3_extensions_print(bp, "X509v3 extensions",
237		    ci->extensions, cflag, 8);
238
239	if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
240		if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0)
241			goto err;
242	}
243	if (!(cflag & X509_FLAG_NO_AUX)) {
244		if (!X509_CERT_AUX_print(bp, x->aux, 0))
245			goto err;
246	}
247	ret = 1;
248
249 err:
250	free(m);
251	return (ret);
252}
253
254int
255X509_ocspid_print(BIO *bp, X509 *x)
256{
257	unsigned char *der = NULL;
258	unsigned char *dertmp;
259	int derlen;
260	int i;
261	unsigned char SHA1md[SHA_DIGEST_LENGTH];
262
263	/* display the hash of the subject as it would appear
264	   in OCSP requests */
265	if (BIO_printf(bp, "        Subject OCSP hash: ") <= 0)
266		goto err;
267	if ((derlen = i2d_X509_NAME(x->cert_info->subject, NULL)) <= 0)
268		goto err;
269	if ((der = dertmp = malloc(derlen)) == NULL)
270		goto err;
271	if (i2d_X509_NAME(x->cert_info->subject, &dertmp) <= 0)
272		goto err;
273
274	if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
275		goto err;
276	for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
277		if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
278			goto err;
279	}
280	free (der);
281	der = NULL;
282
283	/* display the hash of the public key as it would appear
284	   in OCSP requests */
285	if (BIO_printf(bp, "\n        Public key OCSP hash: ") <= 0)
286		goto err;
287
288	if (!EVP_Digest(x->cert_info->key->public_key->data,
289	    x->cert_info->key->public_key->length,
290	    SHA1md, NULL, EVP_sha1(), NULL))
291		goto err;
292	for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
293		if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
294			goto err;
295	}
296	BIO_printf(bp, "\n");
297
298	return (1);
299
300 err:
301	free(der);
302	return (0);
303}
304
305int
306X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
307{
308	const unsigned char *s;
309	int i, n;
310
311	n = sig->length;
312	s = sig->data;
313	for (i = 0; i < n; i++) {
314		if ((i % 18) == 0) {
315			if (BIO_write(bp, "\n", 1) <= 0)
316				return 0;
317			if (BIO_indent(bp, indent, indent) <= 0)
318				return 0;
319		}
320		if (BIO_printf(bp, "%02x%s", s[i],
321		    ((i + 1) == n) ? "" : ":") <= 0)
322			return 0;
323	}
324	if (BIO_write(bp, "\n", 1) != 1)
325		return 0;
326
327	return 1;
328}
329
330int
331X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, const ASN1_STRING *sig)
332{
333	int sig_nid;
334	if (BIO_puts(bp, "    Signature Algorithm: ") <= 0)
335		return 0;
336	if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
337		return 0;
338
339	sig_nid = OBJ_obj2nid(sigalg->algorithm);
340	if (sig_nid != NID_undef) {
341		int pkey_nid, dig_nid;
342		const EVP_PKEY_ASN1_METHOD *ameth;
343		if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
344			ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
345			if (ameth && ameth->sig_print)
346				return ameth->sig_print(bp, sigalg, sig, 9, 0);
347		}
348	}
349	if (sig)
350		return X509_signature_dump(bp, sig, 9);
351	else if (BIO_puts(bp, "\n") <= 0)
352		return 0;
353	return 1;
354}
355
356int
357ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
358{
359	if (tm->type == V_ASN1_UTCTIME)
360		return ASN1_UTCTIME_print(bp, tm);
361	if (tm->type == V_ASN1_GENERALIZEDTIME)
362		return ASN1_GENERALIZEDTIME_print(bp, tm);
363	BIO_write(bp, "Bad time value", 14);
364	return (0);
365}
366LCRYPTO_ALIAS(ASN1_TIME_print);
367
368static const char *mon[12] = {
369	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
370	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
371};
372
373int
374ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
375{
376	char *v;
377	int gmt = 0;
378	int i;
379	int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
380	char *f = "";
381	int f_len = 0;
382
383	i = tm->length;
384	v = (char *)tm->data;
385
386	if (i < 12)
387		goto err;
388	if (v[i-1] == 'Z')
389		gmt = 1;
390	for (i = 0; i < 12; i++)
391		if ((v[i] > '9') || (v[i] < '0'))
392			goto err;
393	y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 +
394	    (v[2] - '0') * 10 + (v[3] - '0');
395	M = (v[4] - '0') * 10 + (v[5] - '0');
396	if ((M > 12) || (M < 1))
397		goto err;
398	d = (v[6] - '0') * 10 + (v[7] - '0');
399	h = (v[8] - '0') * 10 + (v[9] - '0');
400	m = (v[10] - '0') * 10 + (v[11] - '0');
401	if (tm->length >= 14 &&
402	    (v[12] >= '0') && (v[12] <= '9') &&
403	    (v[13] >= '0') && (v[13] <= '9')) {
404		s =  (v[12] - '0') * 10 + (v[13] - '0');
405		/* Check for fractions of seconds. */
406		if (tm->length >= 15 && v[14] == '.') {
407			int l = tm->length;
408			f = &v[14];	/* The decimal point. */
409			f_len = 1;
410			while (14 + f_len < l && f[f_len] >= '0' &&
411			    f[f_len] <= '9')
412				++f_len;
413		}
414	}
415
416	if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
417	    mon[M - 1], d, h, m, s, f_len, f, y, (gmt) ? " GMT" : "") <= 0)
418		return (0);
419	else
420		return (1);
421
422 err:
423	BIO_write(bp, "Bad time value", 14);
424	return (0);
425}
426LCRYPTO_ALIAS(ASN1_GENERALIZEDTIME_print);
427
428int
429ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
430{
431	const char *v;
432	int gmt = 0;
433	int i;
434	int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
435
436	i = tm->length;
437	v = (const char *)tm->data;
438
439	if (i < 10)
440		goto err;
441	if (v[i-1] == 'Z')
442		gmt = 1;
443	for (i = 0; i < 10; i++)
444		if ((v[i] > '9') || (v[i] < '0'))
445			goto err;
446	y = (v[0] - '0') * 10 + (v[1] - '0');
447	if (y < 50)
448		y += 100;
449	M = (v[2] - '0') * 10 + (v[3] - '0');
450	if ((M > 12) || (M < 1))
451		goto err;
452	d = (v[4] - '0') * 10 + (v[5] - '0');
453	h = (v[6] - '0') * 10 + (v[7] - '0');
454	m = (v[8] - '0') * 10 + (v[9] - '0');
455	if (tm->length >=12 &&
456	    (v[10] >= '0') && (v[10] <= '9') &&
457	    (v[11] >= '0') && (v[11] <= '9'))
458		s = (v[10] - '0') * 10 + (v[11] - '0');
459
460	if (BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
461	    mon[M - 1], d, h, m, s, y + 1900, (gmt) ? " GMT" : "") <= 0)
462		return (0);
463	else
464		return (1);
465
466 err:
467	BIO_write(bp, "Bad time value", 14);
468	return (0);
469}
470LCRYPTO_ALIAS(ASN1_UTCTIME_print);
471
472int
473X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
474{
475	char *s, *c, *b;
476	int i;
477	int ret = 0;
478
479	b = X509_NAME_oneline(name, NULL, 0);
480	if (b == NULL)
481		return 0;
482	if (*b == '\0') {
483		free(b);
484		return 1;
485	}
486	s = b + 1; /* skip the first slash */
487
488	c = s;
489	for (;;) {
490		if (((*s == '/') &&
491		    ((s[1] >= 'A') && (s[1] <= 'Z') &&
492		    ((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') &&
493		    (s[3] == '='))))) || (*s == '\0')) {
494			i = s - c;
495			if (BIO_write(bp, c, i) != i)
496				goto err;
497			c = s + 1;	/* skip following slash */
498			if (*s != '\0') {
499				if (BIO_write(bp, ", ", 2) != 2)
500					goto err;
501			}
502		}
503		if (*s == '\0')
504			break;
505		s++;
506	}
507
508	ret = 1;
509	if (0) {
510 err:
511		X509error(ERR_R_BUF_LIB);
512	}
513	free(b);
514	return (ret);
515}
516