2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/buffer.h>
13 #include <openssl/bn.h>
14 #include <openssl/objects.h>
15 #include <openssl/x509.h>
16 #include <openssl/x509v3.h>
17 #include "crypto/asn1.h"
18 #include "crypto/x509.h"
20 void OSSL_STACK_OF_X509_free(STACK_OF(X509) *certs)
22 sk_X509_pop_free(certs, X509_free);
25 #ifndef OPENSSL_NO_STDIO
26 int X509_print_fp(FILE *fp, X509 *x)
28 return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
31 int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
37 if ((b = BIO_new(BIO_s_file())) == NULL) {
38 ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
41 BIO_set_fp(b, fp, BIO_NOCLOSE);
42 ret = X509_print_ex(b, x, nmflag, cflag);
48 int X509_print(BIO *bp, X509 *x)
50 return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
53 int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
59 int nmindent = 0, printok = 0;
60 EVP_PKEY *pkey = NULL;
63 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
68 if (nmflags == X509_FLAG_COMPAT) {
73 if (!(cflag & X509_FLAG_NO_HEADER)) {
74 if (BIO_write(bp, "Certificate:\n", 13) <= 0)
76 if (BIO_write(bp, " Data:\n", 10) <= 0)
79 if (!(cflag & X509_FLAG_NO_VERSION)) {
80 l = X509_get_version(x);
81 if (l >= X509_VERSION_1 && l <= X509_VERSION_3) {
82 if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0)
85 if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
89 if (!(cflag & X509_FLAG_NO_SERIAL)) {
90 const ASN1_INTEGER *bs = X509_get0_serialNumber(x);
92 if (BIO_write(bp, " Serial Number:", 22) <= 0)
95 if (bs->length <= (int)sizeof(long)) {
97 l = ASN1_INTEGER_get(bs);
104 if (bs->type == V_ASN1_NEG_INTEGER) {
105 ul = 0 - (unsigned long)l;
111 if (BIO_printf(bp, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0)
114 neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
115 if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
118 for (i = 0; i < bs->length; i++) {
119 if (BIO_printf(bp, "%02x%c", bs->data[i],
120 ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
127 if (!(cflag & X509_FLAG_NO_SIGNAME)) {
128 const X509_ALGOR *tsig_alg = X509_get0_tbs_sigalg(x);
130 if (BIO_puts(bp, " ") <= 0)
132 if (X509_signature_print(bp, tsig_alg, NULL) <= 0)
136 if (!(cflag & X509_FLAG_NO_ISSUER)) {
137 if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
139 if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)
142 if (BIO_write(bp, "\n", 1) <= 0)
145 if (!(cflag & X509_FLAG_NO_VALIDITY)) {
146 if (BIO_write(bp, " Validity\n", 17) <= 0)
148 if (BIO_write(bp, " Not Before: ", 24) <= 0)
150 if (ossl_asn1_time_print_ex(bp, X509_get0_notBefore(x), ASN1_DTFLGS_RFC822) == 0)
152 if (BIO_write(bp, "\n Not After : ", 25) <= 0)
154 if (ossl_asn1_time_print_ex(bp, X509_get0_notAfter(x), ASN1_DTFLGS_RFC822) == 0)
156 if (BIO_write(bp, "\n", 1) <= 0)
159 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
160 if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
162 if (X509_NAME_print_ex
163 (bp, X509_get_subject_name(x), nmindent, nmflags) < printok)
165 if (BIO_write(bp, "\n", 1) <= 0)
168 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
169 X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x);
171 X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey);
172 if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
174 if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
176 if (i2a_ASN1_OBJECT(bp, xpoid) <= 0)
178 if (BIO_puts(bp, "\n") <= 0)
181 pkey = X509_get0_pubkey(x);
183 BIO_printf(bp, "%12sUnable to load Public Key\n", "");
184 ERR_print_errors(bp);
186 EVP_PKEY_print_public(bp, pkey, 16, NULL);
190 if (!(cflag & X509_FLAG_NO_IDS)) {
191 const ASN1_BIT_STRING *iuid, *suid;
192 X509_get0_uids(x, &iuid, &suid);
194 if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)
196 if (!X509_signature_dump(bp, iuid, 12))
200 if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)
202 if (!X509_signature_dump(bp, suid, 12))
207 if (!(cflag & X509_FLAG_NO_EXTENSIONS)
208 && !X509V3_extensions_print(bp, "X509v3 extensions",
209 X509_get0_extensions(x), cflag, 8))
212 if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
213 const X509_ALGOR *sig_alg;
214 const ASN1_BIT_STRING *sig;
215 X509_get0_signature(&sig, &sig_alg, x);
216 if (X509_signature_print(bp, sig_alg, sig) <= 0)
219 if (!(cflag & X509_FLAG_NO_AUX)) {
220 if (!X509_aux_print(bp, x, 0))
228 int X509_ocspid_print(BIO *bp, X509 *x)
230 unsigned char *der = NULL;
231 unsigned char *dertmp;
234 unsigned char SHA1md[SHA_DIGEST_LENGTH];
235 ASN1_BIT_STRING *keybstr;
236 const X509_NAME *subj;
239 if (x == NULL || bp == NULL)
242 * display the hash of the subject as it would appear in OCSP requests
244 if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
246 subj = X509_get_subject_name(x);
247 derlen = i2d_X509_NAME(subj, NULL);
250 if ((der = dertmp = OPENSSL_malloc(derlen)) == NULL)
252 i2d_X509_NAME(subj, &dertmp);
254 md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq);
257 if (!EVP_Digest(der, derlen, SHA1md, NULL, md, NULL))
259 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
260 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
267 * display the hash of the public key as it would appear in OCSP requests
269 if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
272 keybstr = X509_get0_pubkey_bitstr(x);
277 if (!EVP_Digest(ASN1_STRING_get0_data(keybstr),
278 ASN1_STRING_length(keybstr), SHA1md, NULL, md, NULL))
280 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
281 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
284 BIO_printf(bp, "\n");
294 int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
296 const unsigned char *s;
301 for (i = 0; i < n; i++) {
303 if (i > 0 && BIO_write(bp, "\n", 1) <= 0)
305 if (BIO_indent(bp, indent, indent) <= 0)
308 if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0)
311 if (BIO_write(bp, "\n", 1) != 1)
317 int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
318 const ASN1_STRING *sig)
322 if (BIO_printf(bp, "%*sSignature Algorithm: ", indent, "") <= 0)
324 if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
327 if (sig && BIO_printf(bp, "\n%*sSignature Value:", indent, "") <= 0)
329 sig_nid = OBJ_obj2nid(sigalg->algorithm);
330 if (sig_nid != NID_undef) {
331 int pkey_nid, dig_nid;
332 const EVP_PKEY_ASN1_METHOD *ameth;
333 if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
334 ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
335 if (ameth && ameth->sig_print)
336 return ameth->sig_print(bp, sigalg, sig, indent + 4, 0);
339 if (BIO_write(bp, "\n", 1) != 1)
342 return X509_signature_dump(bp, sig, indent + 4);
346 int X509_aux_print(BIO *out, X509 *x, int indent)
348 char oidstr[80], first;
349 STACK_OF(ASN1_OBJECT) *trust, *reject;
350 const unsigned char *alias, *keyid;
353 if (X509_trusted(x) == 0)
355 trust = X509_get0_trust_objects(x);
356 reject = X509_get0_reject_objects(x);
359 BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
360 for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
365 OBJ_obj2txt(oidstr, sizeof(oidstr),
366 sk_ASN1_OBJECT_value(trust, i), 0);
367 BIO_puts(out, oidstr);
371 BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
374 BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
375 for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
380 OBJ_obj2txt(oidstr, sizeof(oidstr),
381 sk_ASN1_OBJECT_value(reject, i), 0);
382 BIO_puts(out, oidstr);
386 BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
387 alias = X509_alias_get0(x, &i);
389 BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias);
390 keyid = X509_keyid_get0(x, &keyidlen);
392 BIO_printf(out, "%*sKey Id: ", indent, "");
393 for (i = 0; i < keyidlen; i++)
394 BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]);
395 BIO_write(out, "\n", 1);
401 * Helper functions for improving certificate verification error diagnostics
404 int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags)
406 unsigned long flags = ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE |
407 XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN;
410 return BIO_printf(bio, " (no certificate)\n") > 0;
411 if (BIO_printf(bio, " certificate\n") <= 0
412 || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_SUBJECT))
414 if (X509_check_issued((X509 *)cert, cert) == X509_V_OK) {
415 if (BIO_printf(bio, " self-issued\n") <= 0)
418 if (BIO_printf(bio, " ") <= 0
419 || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_ISSUER))
422 if (!X509_print_ex(bio, cert, flags,
423 ~(X509_FLAG_NO_SERIAL | X509_FLAG_NO_VALIDITY)))
425 if (X509_cmp_current_time(X509_get0_notBefore(cert)) > 0)
426 if (BIO_printf(bio, " not yet valid\n") <= 0)
428 if (X509_cmp_current_time(X509_get0_notAfter(cert)) < 0)
429 if (BIO_printf(bio, " no more valid\n") <= 0)
431 return X509_print_ex(bio, cert, flags,
432 ~neg_cflags & ~X509_FLAG_EXTENSIONS_ONLY_KID);
435 static int print_certs(BIO *bio, const STACK_OF(X509) *certs)
439 if (certs == NULL || sk_X509_num(certs) <= 0)
440 return BIO_printf(bio, " (no certificates)\n") >= 0;
442 for (i = 0; i < sk_X509_num(certs); i++) {
443 X509 *cert = sk_X509_value(certs, i);
446 if (!ossl_x509_print_ex_brief(bio, cert, 0))
448 if (!X509V3_extensions_print(bio, NULL,
449 X509_get0_extensions(cert),
450 X509_FLAG_EXTENSIONS_ONLY_KID, 8))
457 static int print_store_certs(BIO *bio, X509_STORE *store)
460 STACK_OF(X509) *certs = X509_STORE_get1_all_certs(store);
461 int ret = print_certs(bio, certs);
463 OSSL_STACK_OF_X509_free(certs);
466 return BIO_printf(bio, " (no trusted store)\n") >= 0;
470 /* Extend the error queue with details on a failed cert verification */
471 int X509_STORE_CTX_print_verify_cb(int ok, X509_STORE_CTX *ctx)
473 if (ok == 0 && ctx != NULL) {
474 int cert_error = X509_STORE_CTX_get_error(ctx);
475 BIO *bio = BIO_new(BIO_s_mem()); /* may be NULL */
479 BIO_printf(bio, "%s at depth = %d error = %d (%s)\n",
480 X509_STORE_CTX_get0_parent_ctx(ctx) != NULL
481 ? "CRL path validation"
482 : "Certificate verification",
483 X509_STORE_CTX_get_error_depth(ctx),
484 cert_error, X509_verify_cert_error_string(cert_error));
486 X509_STORE *ts = X509_STORE_CTX_get0_store(ctx);
487 X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
491 switch (cert_error) {
492 case X509_V_ERR_HOSTNAME_MISMATCH:
493 BIO_printf(bio, "Expected hostname(s) = ");
494 while ((str = X509_VERIFY_PARAM_get0_host(vpm, idx++)) != NULL)
495 BIO_printf(bio, "%s%s", idx == 1 ? "" : ", ", str);
496 BIO_printf(bio, "\n");
498 case X509_V_ERR_EMAIL_MISMATCH:
499 str = X509_VERIFY_PARAM_get0_email(vpm);
501 BIO_printf(bio, "Expected email address = %s\n", str);
503 case X509_V_ERR_IP_ADDRESS_MISMATCH:
504 str = X509_VERIFY_PARAM_get1_ip_asc(vpm);
506 BIO_printf(bio, "Expected IP address = %s\n", str);
514 BIO_printf(bio, "Failure for:\n");
515 ossl_x509_print_ex_brief(bio, X509_STORE_CTX_get_current_cert(ctx),
516 X509_FLAG_NO_EXTENSIONS);
517 if (cert_error == X509_V_ERR_CERT_UNTRUSTED
518 || cert_error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
519 || cert_error == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
520 || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
521 || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
522 || cert_error == X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
523 || cert_error == X509_V_ERR_STORE_LOOKUP) {
524 BIO_printf(bio, "Non-trusted certs:\n");
525 print_certs(bio, X509_STORE_CTX_get0_untrusted(ctx));
526 BIO_printf(bio, "Certs in trust store:\n");
527 print_store_certs(bio, X509_STORE_CTX_get0_store(ctx));
529 ERR_raise(ERR_LIB_X509, X509_R_CERTIFICATE_VERIFICATION_FAILED);
530 ERR_add_error_mem_bio("\n", bio);