| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
|
| 6 |
#include <openssl/asn1.h> |
| 7 |
#include <openssl/bio.h> |
| 8 |
#include <openssl/digest.h> |
| 9 |
#include <openssl/err.h> |
| 10 |
#include <openssl/evp.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/obj.h> |
| 13 |
#include <openssl/x509.h> |
| 14 |
|
| 15 |
#include "internal.h" |
| 16 |
|
| 17 |
|
| 18 |
int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, |
| 19 |
unsigned long cflag) { |
| 20 |
BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
| 21 |
if (b == NULL) { |
| 22 |
OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB); |
| 23 |
return 0; |
| 24 |
} |
| 25 |
int ret = X509_print_ex(b, x, nmflag, cflag); |
| 26 |
BIO_free(b); |
| 27 |
return ret; |
| 28 |
} |
| 29 |
|
| 30 |
int X509_print_fp(FILE *fp, X509 *x) { |
| 31 |
return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT); |
| 32 |
} |
| 33 |
|
| 34 |
int X509_print(BIO *bp, X509 *x) { |
| 35 |
return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT); |
| 36 |
} |
| 37 |
|
| 38 |
int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, |
| 39 |
unsigned long cflag) { |
| 40 |
char mlch = ' '; |
| 41 |
int nmindent = 0; |
| 42 |
if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { |
| 43 |
mlch = '\n'; |
| 44 |
nmindent = 12; |
| 45 |
} |
| 46 |
|
| 47 |
if (nmflags == X509_FLAG_COMPAT) { |
| 48 |
nmindent = 16; |
| 49 |
} |
| 50 |
|
| 51 |
const X509_CINF *ci = x->cert_info; |
| 52 |
if (!(cflag & X509_FLAG_NO_HEADER)) { |
| 53 |
if (BIO_write(bp, "Certificate:\n", 13) <= 0) { |
| 54 |
return 0; |
| 55 |
} |
| 56 |
if (BIO_write(bp, " Data:\n", 10) <= 0) { |
| 57 |
return 0; |
| 58 |
} |
| 59 |
} |
| 60 |
if (!(cflag & X509_FLAG_NO_VERSION)) { |
| 61 |
long l = X509_get_version(x); |
| 62 |
assert(X509_VERSION_1 <= l && l <= X509_VERSION_3); |
| 63 |
if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, |
| 64 |
(unsigned long)l) <= 0) { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
} |
| 68 |
if (!(cflag & X509_FLAG_NO_SERIAL)) { |
| 69 |
if (BIO_write(bp, " Serial Number:", 22) <= 0) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
const ASN1_INTEGER *serial = X509_get0_serialNumber(x); |
| 74 |
uint64_t serial_u64; |
| 75 |
if (ASN1_INTEGER_get_uint64(&serial_u64, serial)) { |
| 76 |
assert(serial->type != V_ASN1_NEG_INTEGER); |
| 77 |
if (BIO_printf(bp, " %" PRIu64 " (0x%" PRIx64 ")\n", serial_u64, |
| 78 |
serial_u64) <= 0) { |
| 79 |
return 0; |
| 80 |
} |
| 81 |
} else { |
| 82 |
ERR_clear_error(); // Clear |ASN1_INTEGER_get_uint64|'s error. |
| 83 |
const char *neg = |
| 84 |
(serial->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : ""; |
| 85 |
if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0) { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
for (int i = 0; i < serial->length; i++) { |
| 90 |
if (BIO_printf(bp, "%02x%c", serial->data[i], |
| 91 |
((i + 1 == serial->length) ? '\n' : ':')) <= 0) { |
| 92 |
return 0; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (!(cflag & X509_FLAG_NO_SIGNAME)) { |
| 99 |
if (X509_signature_print(bp, ci->signature, NULL) <= 0) { |
| 100 |
return 0; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (!(cflag & X509_FLAG_NO_ISSUER)) { |
| 105 |
if (BIO_printf(bp, " Issuer:%c", mlch) <= 0) { |
| 106 |
return 0; |
| 107 |
} |
| 108 |
if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags) < |
| 109 |
0) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
if (BIO_write(bp, "\n", 1) <= 0) { |
| 113 |
return 0; |
| 114 |
} |
| 115 |
} |
| 116 |
if (!(cflag & X509_FLAG_NO_VALIDITY)) { |
| 117 |
if (BIO_write(bp, " Validity\n", 17) <= 0) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
if (BIO_write(bp, " Not Before: ", 24) <= 0) { |
| 121 |
return 0; |
| 122 |
} |
| 123 |
if (!ASN1_TIME_print(bp, X509_get_notBefore(x))) { |
| 124 |
return 0; |
| 125 |
} |
| 126 |
if (BIO_write(bp, "\n Not After : ", 25) <= 0) { |
| 127 |
return 0; |
| 128 |
} |
| 129 |
if (!ASN1_TIME_print(bp, X509_get_notAfter(x))) { |
| 130 |
return 0; |
| 131 |
} |
| 132 |
if (BIO_write(bp, "\n", 1) <= 0) { |
| 133 |
return 0; |
| 134 |
} |
| 135 |
} |
| 136 |
if (!(cflag & X509_FLAG_NO_SUBJECT)) { |
| 137 |
if (BIO_printf(bp, " Subject:%c", mlch) <= 0) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
if (X509_NAME_print_ex(bp, X509_get_subject_name(x), nmindent, nmflags) < |
| 141 |
0) { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
if (BIO_write(bp, "\n", 1) <= 0) { |
| 145 |
return 0; |
| 146 |
} |
| 147 |
} |
| 148 |
if (!(cflag & X509_FLAG_NO_PUBKEY)) { |
| 149 |
if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0) { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0) { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0) { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
if (BIO_puts(bp, "\n") <= 0) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
const EVP_PKEY *pkey = X509_get0_pubkey(x); |
| 163 |
if (pkey == NULL) { |
| 164 |
BIO_printf(bp, "%12sUnable to load Public Key\n", ""); |
| 165 |
ERR_print_errors(bp); |
| 166 |
} else { |
| 167 |
EVP_PKEY_print_public(bp, pkey, 16, NULL); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if (!(cflag & X509_FLAG_NO_IDS)) { |
| 172 |
if (ci->issuerUID) { |
| 173 |
if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0) { |
| 174 |
return 0; |
| 175 |
} |
| 176 |
if (!X509_signature_dump(bp, ci->issuerUID, 12)) { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
} |
| 180 |
if (ci->subjectUID) { |
| 181 |
if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0) { |
| 182 |
return 0; |
| 183 |
} |
| 184 |
if (!X509_signature_dump(bp, ci->subjectUID, 12)) { |
| 185 |
return 0; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if (!(cflag & X509_FLAG_NO_EXTENSIONS)) { |
| 191 |
X509V3_extensions_print(bp, "X509v3 extensions", ci->extensions, cflag, 8); |
| 192 |
} |
| 193 |
|
| 194 |
if (!(cflag & X509_FLAG_NO_SIGDUMP)) { |
| 195 |
if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0) { |
| 196 |
return 0; |
| 197 |
} |
| 198 |
} |
| 199 |
if (!(cflag & X509_FLAG_NO_AUX)) { |
| 200 |
if (!X509_CERT_AUX_print(bp, x->aux, 0)) { |
| 201 |
return 0; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, |
| 209 |
const ASN1_STRING *sig) { |
| 210 |
if (BIO_puts(bp, " Signature Algorithm: ") <= 0) { |
| 211 |
return 0; |
| 212 |
} |
| 213 |
if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) { |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
// RSA-PSS signatures have parameters to print. |
| 218 |
int sig_nid = OBJ_obj2nid(sigalg->algorithm); |
| 219 |
if (sig_nid == NID_rsassaPss && |
| 220 |
!x509_print_rsa_pss_params(bp, sigalg, 9, 0)) { |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
if (sig) { |
| 225 |
return X509_signature_dump(bp, sig, 9); |
| 226 |
} else if (BIO_puts(bp, "\n") <= 0) { |
| 227 |
return 0; |
| 228 |
} |
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) { |
| 233 |
char *s, *c, *b; |
| 234 |
int ret = 0, i; |
| 235 |
|
| 236 |
b = X509_NAME_oneline(name, NULL, 0); |
| 237 |
if (!b) { |
| 238 |
return 0; |
| 239 |
} |
| 240 |
if (!*b) { |
| 241 |
OPENSSL_free(b); |
| 242 |
return 1; |
| 243 |
} |
| 244 |
s = b + 1; // skip the first slash |
| 245 |
|
| 246 |
c = s; |
| 247 |
for (;;) { |
| 248 |
if (((*s == '/') && ((s[1] >= 'A') && (s[1] <= 'Z') && |
| 249 |
((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') && |
| 250 |
(s[3] == '='))))) || |
| 251 |
(*s == '\0')) { |
| 252 |
i = s - c; |
| 253 |
if (BIO_write(bp, c, i) != i) { |
| 254 |
goto err; |
| 255 |
} |
| 256 |
c = s + 1; // skip following slash |
| 257 |
if (*s != '\0') { |
| 258 |
if (BIO_write(bp, ", ", 2) != 2) { |
| 259 |
goto err; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
if (*s == '\0') { |
| 264 |
break; |
| 265 |
} |
| 266 |
s++; |
| 267 |
} |
| 268 |
|
| 269 |
ret = 1; |
| 270 |
if (0) { |
| 271 |
err: |
| 272 |
OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB); |
| 273 |
} |
| 274 |
OPENSSL_free(b); |
| 275 |
return ret; |
| 276 |
} |