1 /* $OpenBSD: t_x509.c,v 1.35 2021/11/01 20:53:08 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 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_STRING_print(BIO *bp, const ASN1_STRING *v) 356 { 357 int i, n; 358 char buf[80]; 359 const char *p; 360 361 if (v == NULL) 362 return (0); 363 n = 0; 364 p = (const char *)v->data; 365 for (i = 0; i < v->length; i++) { 366 if ((p[i] > '~') || ((p[i] < ' ') && 367 (p[i] != '\n') && (p[i] != '\r'))) 368 buf[n] = '.'; 369 else 370 buf[n] = p[i]; 371 n++; 372 if (n >= 80) { 373 if (BIO_write(bp, buf, n) <= 0) 374 return (0); 375 n = 0; 376 } 377 } 378 if (n > 0) 379 if (BIO_write(bp, buf, n) <= 0) 380 return (0); 381 return (1); 382 } 383 384 int 385 ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) 386 { 387 if (tm->type == V_ASN1_UTCTIME) 388 return ASN1_UTCTIME_print(bp, tm); 389 if (tm->type == V_ASN1_GENERALIZEDTIME) 390 return ASN1_GENERALIZEDTIME_print(bp, tm); 391 BIO_write(bp, "Bad time value", 14); 392 return (0); 393 } 394 395 static const char *mon[12] = { 396 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 397 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 398 }; 399 400 int 401 ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) 402 { 403 char *v; 404 int gmt = 0; 405 int i; 406 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0; 407 char *f = ""; 408 int f_len = 0; 409 410 i = tm->length; 411 v = (char *)tm->data; 412 413 if (i < 12) 414 goto err; 415 if (v[i-1] == 'Z') 416 gmt = 1; 417 for (i = 0; i < 12; i++) 418 if ((v[i] > '9') || (v[i] < '0')) 419 goto err; 420 y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 + 421 (v[2] - '0') * 10 + (v[3] - '0'); 422 M = (v[4] - '0') * 10 + (v[5] - '0'); 423 if ((M > 12) || (M < 1)) 424 goto err; 425 d = (v[6] - '0') * 10 + (v[7] - '0'); 426 h = (v[8] - '0') * 10 + (v[9] - '0'); 427 m = (v[10] - '0') * 10 + (v[11] - '0'); 428 if (tm->length >= 14 && 429 (v[12] >= '0') && (v[12] <= '9') && 430 (v[13] >= '0') && (v[13] <= '9')) { 431 s = (v[12] - '0') * 10 + (v[13] - '0'); 432 /* Check for fractions of seconds. */ 433 if (tm->length >= 15 && v[14] == '.') { 434 int l = tm->length; 435 f = &v[14]; /* The decimal point. */ 436 f_len = 1; 437 while (14 + f_len < l && f[f_len] >= '0' && 438 f[f_len] <= '9') 439 ++f_len; 440 } 441 } 442 443 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s", 444 mon[M - 1], d, h, m, s, f_len, f, y, (gmt) ? " GMT" : "") <= 0) 445 return (0); 446 else 447 return (1); 448 449 err: 450 BIO_write(bp, "Bad time value", 14); 451 return (0); 452 } 453 454 int 455 ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) 456 { 457 const char *v; 458 int gmt = 0; 459 int i; 460 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0; 461 462 i = tm->length; 463 v = (const char *)tm->data; 464 465 if (i < 10) 466 goto err; 467 if (v[i-1] == 'Z') 468 gmt = 1; 469 for (i = 0; i < 10; i++) 470 if ((v[i] > '9') || (v[i] < '0')) 471 goto err; 472 y = (v[0] - '0') * 10 + (v[1] - '0'); 473 if (y < 50) 474 y += 100; 475 M = (v[2] - '0') * 10 + (v[3] - '0'); 476 if ((M > 12) || (M < 1)) 477 goto err; 478 d = (v[4] - '0') * 10 + (v[5] - '0'); 479 h = (v[6] - '0') * 10 + (v[7] - '0'); 480 m = (v[8] - '0') * 10 + (v[9] - '0'); 481 if (tm->length >=12 && 482 (v[10] >= '0') && (v[10] <= '9') && 483 (v[11] >= '0') && (v[11] <= '9')) 484 s = (v[10] - '0') * 10 + (v[11] - '0'); 485 486 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s", 487 mon[M - 1], d, h, m, s, y + 1900, (gmt) ? " GMT" : "") <= 0) 488 return (0); 489 else 490 return (1); 491 492 err: 493 BIO_write(bp, "Bad time value", 14); 494 return (0); 495 } 496 497 int 498 X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) 499 { 500 char *s, *c, *b; 501 int ret = 0, l, i; 502 503 l = 80 - 2 - obase; 504 505 b = X509_NAME_oneline(name, NULL, 0); 506 if (b == NULL) 507 return 0; 508 if (*b == '\0') { 509 free(b); 510 return 1; 511 } 512 s = b + 1; /* skip the first slash */ 513 514 c = s; 515 for (;;) { 516 if (((*s == '/') && 517 ((s[1] >= 'A') && (s[1] <= 'Z') && 518 ((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') && 519 (s[3] == '='))))) || (*s == '\0')) { 520 i = s - c; 521 if (BIO_write(bp, c, i) != i) 522 goto err; 523 c = s + 1; /* skip following slash */ 524 if (*s != '\0') { 525 if (BIO_write(bp, ", ", 2) != 2) 526 goto err; 527 } 528 l--; 529 } 530 if (*s == '\0') 531 break; 532 s++; 533 l--; 534 } 535 536 ret = 1; 537 if (0) { 538 err: 539 X509error(ERR_R_BUF_LIB); 540 } 541 free(b); 542 return (ret); 543 } 544