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