1 /* $OpenBSD: t_x509.c,v 1.50 2025/01/27 10:29:41 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 <limits.h>
60 #include <stdio.h>
61
62 #include <openssl/opensslconf.h>
63
64 #include <openssl/bn.h>
65 #include <openssl/buffer.h>
66 #include <openssl/err.h>
67 #include <openssl/objects.h>
68 #include <openssl/x509.h>
69 #include <openssl/x509v3.h>
70
71 #ifndef OPENSSL_NO_DSA
72 #include <openssl/dsa.h>
73 #endif
74 #ifndef OPENSSL_NO_EC
75 #include <openssl/ec.h>
76 #endif
77 #ifndef OPENSSL_NO_RSA
78 #include <openssl/rsa.h>
79 #endif
80
81 #include "bytestring.h"
82 #include "evp_local.h"
83 #include "x509_local.h"
84
85 int
X509_print_fp(FILE * fp,X509 * x)86 X509_print_fp(FILE *fp, X509 *x)
87 {
88 return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
89 }
90 LCRYPTO_ALIAS(X509_print_fp);
91
92 int
X509_print_ex_fp(FILE * fp,X509 * x,unsigned long nmflag,unsigned long cflag)93 X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag)
94 {
95 BIO *b;
96 int ret;
97
98 if ((b = BIO_new(BIO_s_file())) == NULL) {
99 X509error(ERR_R_BUF_LIB);
100 return (0);
101 }
102 BIO_set_fp(b, fp, BIO_NOCLOSE);
103 ret = X509_print_ex(b, x, nmflag, cflag);
104 BIO_free(b);
105 return (ret);
106 }
107 LCRYPTO_ALIAS(X509_print_ex_fp);
108
109 int
X509_print(BIO * bp,X509 * x)110 X509_print(BIO *bp, X509 *x)
111 {
112 return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
113 }
114 LCRYPTO_ALIAS(X509_print);
115
116 int
X509_print_ex(BIO * bp,X509 * x,unsigned long nmflags,unsigned long cflag)117 X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
118 {
119 long l;
120 int ret = 0, i;
121 char *m = NULL, mlch = ' ';
122 int nmindent = 0;
123 X509_CINF *ci;
124 ASN1_INTEGER *bs;
125 EVP_PKEY *pkey = NULL;
126
127 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
128 mlch = '\n';
129 nmindent = 12;
130 }
131
132 if (nmflags == X509_FLAG_COMPAT)
133 nmindent = 16;
134
135 ci = x->cert_info;
136 if (!(cflag & X509_FLAG_NO_HEADER)) {
137 if (BIO_write(bp, "Certificate:\n", 13) <= 0)
138 goto err;
139 if (BIO_write(bp, " Data:\n", 10) <= 0)
140 goto err;
141 }
142 if (!(cflag & X509_FLAG_NO_VERSION)) {
143 l = X509_get_version(x);
144 if (l >= 0 && l <= 2) {
145 if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n",
146 "", l + 1, l) <= 0)
147 goto err;
148 } else {
149 if (BIO_printf(bp, "%8sVersion: unknown (%ld)\n",
150 "", l) <= 0)
151 goto err;
152 }
153 }
154 if (!(cflag & X509_FLAG_NO_SERIAL)) {
155 if (BIO_write(bp, " Serial Number:", 22) <= 0)
156 goto err;
157
158 bs = X509_get_serialNumber(x);
159 l = -1;
160
161 /*
162 * For historical reasons, non-negative serial numbers are
163 * printed in decimal as long as they fit into a long. Using
164 * ASN1_INTEGER_get_uint64() avoids an error on the stack for
165 * numbers between LONG_MAX and ULONG_MAX. Otherwise fall back
166 * to hexadecimal, also for numbers that are non-conformant
167 * (negative or larger than 2^159 - 1).
168 */
169 if (bs->length <= sizeof(long) && bs->type == V_ASN1_INTEGER) {
170 uint64_t u64;
171
172 if (ASN1_INTEGER_get_uint64(&u64, bs) && u64 <= LONG_MAX)
173 l = (long)u64;
174 }
175 if (l >= 0) {
176 if (BIO_printf(bp, " %ld (0x%lx)\n", l, l) <= 0)
177 goto err;
178 } else {
179 const char *neg = "";
180
181 if (bs->type == V_ASN1_NEG_INTEGER)
182 neg = " (Negative)";
183
184 if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
185 goto err;
186 for (i = 0; i < bs->length; i++) {
187 if (BIO_printf(bp, "%02x%c", bs->data[i],
188 ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
189 goto err;
190 }
191 }
192
193 }
194
195 if (!(cflag & X509_FLAG_NO_SIGNAME)) {
196 if (X509_signature_print(bp, x->sig_alg, NULL) <= 0)
197 goto err;
198 }
199
200 if (!(cflag & X509_FLAG_NO_ISSUER)) {
201 if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
202 goto err;
203 if (X509_NAME_print_ex(bp, X509_get_issuer_name(x),
204 nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
205 goto err;
206 if (BIO_write(bp, "\n", 1) <= 0)
207 goto err;
208 }
209 if (!(cflag & X509_FLAG_NO_VALIDITY)) {
210 if (BIO_write(bp, " Validity\n", 17) <= 0)
211 goto err;
212 if (BIO_write(bp, " Not Before: ", 24) <= 0)
213 goto err;
214 if (!ASN1_TIME_print(bp, X509_get_notBefore(x)))
215 goto err;
216 if (BIO_write(bp, "\n Not After : ", 25) <= 0)
217 goto err;
218 if (!ASN1_TIME_print(bp, X509_get_notAfter(x)))
219 goto err;
220 if (BIO_write(bp, "\n", 1) <= 0)
221 goto err;
222 }
223 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
224 if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
225 goto err;
226 if (X509_NAME_print_ex(bp, X509_get_subject_name(x),
227 nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
228 goto err;
229 if (BIO_write(bp, "\n", 1) <= 0)
230 goto err;
231 }
232 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
233 if (BIO_write(bp, " Subject Public Key Info:\n",
234 33) <= 0)
235 goto err;
236 if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
237 goto err;
238 if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
239 goto err;
240 if (BIO_puts(bp, "\n") <= 0)
241 goto err;
242
243 pkey = X509_get_pubkey(x);
244 if (pkey == NULL) {
245 BIO_printf(bp, "%12sUnable to load Public Key\n", "");
246 ERR_print_errors(bp);
247 } else {
248 EVP_PKEY_print_public(bp, pkey, 16, NULL);
249 EVP_PKEY_free(pkey);
250 }
251 }
252
253 if (!(cflag & X509_FLAG_NO_EXTENSIONS))
254 X509V3_extensions_print(bp, "X509v3 extensions",
255 ci->extensions, cflag, 8);
256
257 if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
258 if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0)
259 goto err;
260 }
261 if (!(cflag & X509_FLAG_NO_AUX)) {
262 if (!X509_CERT_AUX_print(bp, x->aux, 0))
263 goto err;
264 }
265 ret = 1;
266
267 err:
268 free(m);
269 return (ret);
270 }
271 LCRYPTO_ALIAS(X509_print_ex);
272
273 int
X509_ocspid_print(BIO * bp,X509 * x)274 X509_ocspid_print(BIO *bp, X509 *x)
275 {
276 unsigned char *der = NULL;
277 unsigned char *dertmp;
278 int derlen;
279 int i;
280 unsigned char SHA1md[SHA_DIGEST_LENGTH];
281
282 /* display the hash of the subject as it would appear
283 in OCSP requests */
284 if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
285 goto err;
286 if ((derlen = i2d_X509_NAME(x->cert_info->subject, NULL)) <= 0)
287 goto err;
288 if ((der = dertmp = malloc(derlen)) == NULL)
289 goto err;
290 if (i2d_X509_NAME(x->cert_info->subject, &dertmp) <= 0)
291 goto err;
292
293 if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
294 goto err;
295 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
296 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
297 goto err;
298 }
299 free (der);
300 der = NULL;
301
302 /* display the hash of the public key as it would appear
303 in OCSP requests */
304 if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
305 goto err;
306
307 if (!EVP_Digest(x->cert_info->key->public_key->data,
308 x->cert_info->key->public_key->length,
309 SHA1md, NULL, EVP_sha1(), NULL))
310 goto err;
311 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
312 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
313 goto err;
314 }
315 BIO_printf(bp, "\n");
316
317 return (1);
318
319 err:
320 free(der);
321 return (0);
322 }
323 LCRYPTO_ALIAS(X509_ocspid_print);
324
325 int
X509_signature_dump(BIO * bp,const ASN1_STRING * sig,int indent)326 X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
327 {
328 const unsigned char *s;
329 int i, n;
330
331 n = sig->length;
332 s = sig->data;
333 for (i = 0; i < n; i++) {
334 if ((i % 18) == 0) {
335 if (BIO_write(bp, "\n", 1) <= 0)
336 return 0;
337 if (BIO_indent(bp, indent, indent) <= 0)
338 return 0;
339 }
340 if (BIO_printf(bp, "%02x%s", s[i],
341 ((i + 1) == n) ? "" : ":") <= 0)
342 return 0;
343 }
344 if (BIO_write(bp, "\n", 1) != 1)
345 return 0;
346
347 return 1;
348 }
349 LCRYPTO_ALIAS(X509_signature_dump);
350
351 int
X509_signature_print(BIO * bp,const X509_ALGOR * sigalg,const ASN1_STRING * sig)352 X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, const ASN1_STRING *sig)
353 {
354 int sig_nid;
355 if (BIO_puts(bp, " Signature Algorithm: ") <= 0)
356 return 0;
357 if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
358 return 0;
359
360 sig_nid = OBJ_obj2nid(sigalg->algorithm);
361 if (sig_nid != NID_undef) {
362 int pkey_nid, dig_nid;
363 const EVP_PKEY_ASN1_METHOD *ameth;
364 if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
365 ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
366 if (ameth && ameth->sig_print)
367 return ameth->sig_print(bp, sigalg, sig, 9, 0);
368 }
369 }
370 if (sig)
371 return X509_signature_dump(bp, sig, 9);
372 else if (BIO_puts(bp, "\n") <= 0)
373 return 0;
374 return 1;
375 }
376 LCRYPTO_ALIAS(X509_signature_print);
377
378 int
ASN1_TIME_print(BIO * bp,const ASN1_TIME * tm)379 ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
380 {
381 if (tm->type == V_ASN1_UTCTIME)
382 return ASN1_UTCTIME_print(bp, tm);
383 if (tm->type == V_ASN1_GENERALIZEDTIME)
384 return ASN1_GENERALIZEDTIME_print(bp, tm);
385 BIO_write(bp, "Bad time value", 14);
386 return (0);
387 }
388 LCRYPTO_ALIAS(ASN1_TIME_print);
389
390 static const char *mon[12] = {
391 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
392 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
393 };
394
395 int
ASN1_GENERALIZEDTIME_print(BIO * bp,const ASN1_GENERALIZEDTIME * tm)396 ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
397 {
398 char *v;
399 int gmt = 0;
400 int i;
401 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
402 char *f = "";
403 int f_len = 0;
404
405 i = tm->length;
406 v = (char *)tm->data;
407
408 if (i < 12)
409 goto err;
410 if (v[i-1] == 'Z')
411 gmt = 1;
412 for (i = 0; i < 12; i++)
413 if ((v[i] > '9') || (v[i] < '0'))
414 goto err;
415 y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 +
416 (v[2] - '0') * 10 + (v[3] - '0');
417 M = (v[4] - '0') * 10 + (v[5] - '0');
418 if ((M > 12) || (M < 1))
419 goto err;
420 d = (v[6] - '0') * 10 + (v[7] - '0');
421 h = (v[8] - '0') * 10 + (v[9] - '0');
422 m = (v[10] - '0') * 10 + (v[11] - '0');
423 if (tm->length >= 14 &&
424 (v[12] >= '0') && (v[12] <= '9') &&
425 (v[13] >= '0') && (v[13] <= '9')) {
426 s = (v[12] - '0') * 10 + (v[13] - '0');
427 /* Check for fractions of seconds. */
428 if (tm->length >= 15 && v[14] == '.') {
429 int l = tm->length;
430 f = &v[14]; /* The decimal point. */
431 f_len = 1;
432 while (14 + f_len < l && f[f_len] >= '0' &&
433 f[f_len] <= '9')
434 ++f_len;
435 }
436 }
437
438 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
439 mon[M - 1], d, h, m, s, f_len, f, y, (gmt) ? " GMT" : "") <= 0)
440 return (0);
441 else
442 return (1);
443
444 err:
445 BIO_write(bp, "Bad time value", 14);
446 return (0);
447 }
448 LCRYPTO_ALIAS(ASN1_GENERALIZEDTIME_print);
449
450 int
ASN1_UTCTIME_print(BIO * bp,const ASN1_UTCTIME * tm)451 ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
452 {
453 const char *v;
454 int gmt = 0;
455 int i;
456 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
457
458 i = tm->length;
459 v = (const char *)tm->data;
460
461 if (i < 10)
462 goto err;
463 if (v[i-1] == 'Z')
464 gmt = 1;
465 for (i = 0; i < 10; i++)
466 if ((v[i] > '9') || (v[i] < '0'))
467 goto err;
468 y = (v[0] - '0') * 10 + (v[1] - '0');
469 if (y < 50)
470 y += 100;
471 M = (v[2] - '0') * 10 + (v[3] - '0');
472 if ((M > 12) || (M < 1))
473 goto err;
474 d = (v[4] - '0') * 10 + (v[5] - '0');
475 h = (v[6] - '0') * 10 + (v[7] - '0');
476 m = (v[8] - '0') * 10 + (v[9] - '0');
477 if (tm->length >=12 &&
478 (v[10] >= '0') && (v[10] <= '9') &&
479 (v[11] >= '0') && (v[11] <= '9'))
480 s = (v[10] - '0') * 10 + (v[11] - '0');
481
482 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
483 mon[M - 1], d, h, m, s, y + 1900, (gmt) ? " GMT" : "") <= 0)
484 return (0);
485 else
486 return (1);
487
488 err:
489 BIO_write(bp, "Bad time value", 14);
490 return (0);
491 }
492 LCRYPTO_ALIAS(ASN1_UTCTIME_print);
493
494 /* NID with SN of 1-2 letters, which X509_NAME_print() historically included. */
495 static int
x509_name_entry_include(const X509_NAME_ENTRY * ne)496 x509_name_entry_include(const X509_NAME_ENTRY *ne)
497 {
498 int nid;
499
500 if ((nid = OBJ_obj2nid(ne->object)) == NID_undef)
501 return 0;
502
503 switch (nid) {
504 case NID_commonName:
505 case NID_surname:
506 case NID_countryName:
507 case NID_localityName:
508 case NID_stateOrProvinceName:
509 case NID_organizationName:
510 case NID_organizationalUnitName:
511 case NID_givenName:
512 case NID_domainComponent: /* XXX - doesn't really belong here */
513 return 1;
514 }
515
516 return 0;
517 }
518
519 int
X509_NAME_print(BIO * bio,const X509_NAME * name,int obase)520 X509_NAME_print(BIO *bio, const X509_NAME *name, int obase)
521 {
522 CBB cbb;
523 uint8_t *buf = NULL;
524 size_t buf_len;
525 const X509_NAME_ENTRY *ne;
526 int i;
527 int started = 0;
528 int ret = 0;
529
530 if (!CBB_init(&cbb, 0))
531 goto err;
532
533 for (i = 0; i < sk_X509_NAME_ENTRY_num(name->entries); i++) {
534 ne = sk_X509_NAME_ENTRY_value(name->entries, i);
535
536 if (!x509_name_entry_include(ne))
537 continue;
538
539 if (started) {
540 if (!CBB_add_u8(&cbb, ','))
541 goto err;
542 if (!CBB_add_u8(&cbb, ' '))
543 goto err;
544 }
545
546 if (!X509_NAME_ENTRY_add_cbb(&cbb, ne))
547 goto err;
548
549 started = 1;
550 }
551
552 if (!CBB_add_u8(&cbb, '\0'))
553 goto err;
554
555 if (!CBB_finish(&cbb, &buf, &buf_len))
556 goto err;
557
558 if (BIO_printf(bio, "%s", buf) < 0)
559 goto err;
560
561 ret = 1;
562
563 err:
564 CBB_cleanup(&cbb);
565 free(buf);
566
567 return ret;
568 }
569 LCRYPTO_ALIAS(X509_NAME_print);
570