1 /* $OpenBSD: t_x509.c,v 1.40 2022/08/11 10:36:32 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 "asn1_locl.h"
81 #include "x509_lcl.h"
82
83 int
X509_print_fp(FILE * fp,X509 * x)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
X509_print_ex_fp(FILE * fp,X509 * x,unsigned long nmflag,unsigned long cflag)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
X509_print(BIO * bp,X509 * x)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
X509_print_ex(BIO * bp,X509 * x,unsigned long nmflags,unsigned long cflag)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
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
254 int
X509_ocspid_print(BIO * bp,X509 * x)255 X509_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
305 int
X509_signature_dump(BIO * bp,const ASN1_STRING * sig,int indent)306 X509_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
330 int
X509_signature_print(BIO * bp,const X509_ALGOR * sigalg,const ASN1_STRING * sig)331 X509_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
356 int
ASN1_TIME_print(BIO * bp,const ASN1_TIME * tm)357 ASN1_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 }
366
367 static const char *mon[12] = {
368 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
369 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
370 };
371
372 int
ASN1_GENERALIZEDTIME_print(BIO * bp,const ASN1_GENERALIZEDTIME * tm)373 ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
374 {
375 char *v;
376 int gmt = 0;
377 int i;
378 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
379 char *f = "";
380 int f_len = 0;
381
382 i = tm->length;
383 v = (char *)tm->data;
384
385 if (i < 12)
386 goto err;
387 if (v[i-1] == 'Z')
388 gmt = 1;
389 for (i = 0; i < 12; i++)
390 if ((v[i] > '9') || (v[i] < '0'))
391 goto err;
392 y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 +
393 (v[2] - '0') * 10 + (v[3] - '0');
394 M = (v[4] - '0') * 10 + (v[5] - '0');
395 if ((M > 12) || (M < 1))
396 goto err;
397 d = (v[6] - '0') * 10 + (v[7] - '0');
398 h = (v[8] - '0') * 10 + (v[9] - '0');
399 m = (v[10] - '0') * 10 + (v[11] - '0');
400 if (tm->length >= 14 &&
401 (v[12] >= '0') && (v[12] <= '9') &&
402 (v[13] >= '0') && (v[13] <= '9')) {
403 s = (v[12] - '0') * 10 + (v[13] - '0');
404 /* Check for fractions of seconds. */
405 if (tm->length >= 15 && v[14] == '.') {
406 int l = tm->length;
407 f = &v[14]; /* The decimal point. */
408 f_len = 1;
409 while (14 + f_len < l && f[f_len] >= '0' &&
410 f[f_len] <= '9')
411 ++f_len;
412 }
413 }
414
415 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
416 mon[M - 1], d, h, m, s, f_len, f, y, (gmt) ? " GMT" : "") <= 0)
417 return (0);
418 else
419 return (1);
420
421 err:
422 BIO_write(bp, "Bad time value", 14);
423 return (0);
424 }
425
426 int
ASN1_UTCTIME_print(BIO * bp,const ASN1_UTCTIME * tm)427 ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
428 {
429 const char *v;
430 int gmt = 0;
431 int i;
432 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
433
434 i = tm->length;
435 v = (const char *)tm->data;
436
437 if (i < 10)
438 goto err;
439 if (v[i-1] == 'Z')
440 gmt = 1;
441 for (i = 0; i < 10; i++)
442 if ((v[i] > '9') || (v[i] < '0'))
443 goto err;
444 y = (v[0] - '0') * 10 + (v[1] - '0');
445 if (y < 50)
446 y += 100;
447 M = (v[2] - '0') * 10 + (v[3] - '0');
448 if ((M > 12) || (M < 1))
449 goto err;
450 d = (v[4] - '0') * 10 + (v[5] - '0');
451 h = (v[6] - '0') * 10 + (v[7] - '0');
452 m = (v[8] - '0') * 10 + (v[9] - '0');
453 if (tm->length >=12 &&
454 (v[10] >= '0') && (v[10] <= '9') &&
455 (v[11] >= '0') && (v[11] <= '9'))
456 s = (v[10] - '0') * 10 + (v[11] - '0');
457
458 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
459 mon[M - 1], d, h, m, s, y + 1900, (gmt) ? " GMT" : "") <= 0)
460 return (0);
461 else
462 return (1);
463
464 err:
465 BIO_write(bp, "Bad time value", 14);
466 return (0);
467 }
468
469 int
X509_NAME_print(BIO * bp,const X509_NAME * name,int obase)470 X509_NAME_print(BIO *bp, const X509_NAME *name, int obase)
471 {
472 char *s, *c, *b;
473 int i;
474 int ret = 0;
475
476 b = X509_NAME_oneline(name, NULL, 0);
477 if (b == NULL)
478 return 0;
479 if (*b == '\0') {
480 free(b);
481 return 1;
482 }
483 s = b + 1; /* skip the first slash */
484
485 c = s;
486 for (;;) {
487 if (((*s == '/') &&
488 ((s[1] >= 'A') && (s[1] <= 'Z') &&
489 ((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') &&
490 (s[3] == '='))))) || (*s == '\0')) {
491 i = s - c;
492 if (BIO_write(bp, c, i) != i)
493 goto err;
494 c = s + 1; /* skip following slash */
495 if (*s != '\0') {
496 if (BIO_write(bp, ", ", 2) != 2)
497 goto err;
498 }
499 }
500 if (*s == '\0')
501 break;
502 s++;
503 }
504
505 ret = 1;
506 if (0) {
507 err:
508 X509error(ERR_R_BUF_LIB);
509 }
510 free(b);
511 return (ret);
512 }
513