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,
58 char *m = NULL, mlch = ' ';
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))
229 int X509_ocspid_print(BIO *bp, X509 *x)
231 unsigned char *der = NULL;
232 unsigned char *dertmp;
235 unsigned char SHA1md[SHA_DIGEST_LENGTH];
236 ASN1_BIT_STRING *keybstr;
237 const X509_NAME *subj;
240 if (x == NULL || bp == NULL)
243 * display the hash of the subject as it would appear in OCSP requests
245 if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
247 subj = X509_get_subject_name(x);
248 derlen = i2d_X509_NAME(subj, NULL);
251 if ((der = dertmp = OPENSSL_malloc(derlen)) == NULL)
253 i2d_X509_NAME(subj, &dertmp);
255 md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq);
258 if (!EVP_Digest(der, derlen, SHA1md, NULL, md, NULL))
260 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
261 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
268 * display the hash of the public key as it would appear in OCSP requests
270 if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
273 keybstr = X509_get0_pubkey_bitstr(x);
278 if (!EVP_Digest(ASN1_STRING_get0_data(keybstr),
279 ASN1_STRING_length(keybstr), SHA1md, NULL, md, NULL))
281 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
282 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
285 BIO_printf(bp, "\n");
295 int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
297 const unsigned char *s;
302 for (i = 0; i < n; i++) {
304 if (i > 0 && BIO_write(bp, "\n", 1) <= 0)
306 if (BIO_indent(bp, indent, indent) <= 0)
309 if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0)
312 if (BIO_write(bp, "\n", 1) != 1)
318 int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
319 const ASN1_STRING *sig)
323 if (BIO_printf(bp, "%*sSignature Algorithm: ", indent, "") <= 0)
325 if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
328 if (sig && BIO_printf(bp, "\n%*sSignature Value:", indent, "") <= 0)
330 sig_nid = OBJ_obj2nid(sigalg->algorithm);
331 if (sig_nid != NID_undef) {
332 int pkey_nid, dig_nid;
333 const EVP_PKEY_ASN1_METHOD *ameth;
334 if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
335 ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
336 if (ameth && ameth->sig_print)
337 return ameth->sig_print(bp, sigalg, sig, indent + 4, 0);
340 if (BIO_write(bp, "\n", 1) != 1)
343 return X509_signature_dump(bp, sig, indent + 4);
347 int X509_aux_print(BIO *out, X509 *x, int indent)
349 char oidstr[80], first;
350 STACK_OF(ASN1_OBJECT) *trust, *reject;
351 const unsigned char *alias, *keyid;
354 if (X509_trusted(x) == 0)
356 trust = X509_get0_trust_objects(x);
357 reject = X509_get0_reject_objects(x);
360 BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
361 for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
366 OBJ_obj2txt(oidstr, sizeof(oidstr),
367 sk_ASN1_OBJECT_value(trust, i), 0);
368 BIO_puts(out, oidstr);
372 BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
375 BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
376 for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
381 OBJ_obj2txt(oidstr, sizeof(oidstr),
382 sk_ASN1_OBJECT_value(reject, i), 0);
383 BIO_puts(out, oidstr);
387 BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
388 alias = X509_alias_get0(x, &i);
390 BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias);
391 keyid = X509_keyid_get0(x, &keyidlen);
393 BIO_printf(out, "%*sKey Id: ", indent, "");
394 for (i = 0; i < keyidlen; i++)
395 BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]);
396 BIO_write(out, "\n", 1);
402 * Helper functions for improving certificate verification error diagnostics
405 int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags)
407 unsigned long flags = ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE |
408 XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN;
411 return BIO_printf(bio, " (no certificate)\n") > 0;
412 if (BIO_printf(bio, " certificate\n") <= 0
413 || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_SUBJECT))
415 if (X509_check_issued((X509 *)cert, cert) == X509_V_OK) {
416 if (BIO_printf(bio, " self-issued\n") <= 0)
419 if (BIO_printf(bio, " ") <= 0
420 || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_ISSUER))
423 if (!X509_print_ex(bio, cert, flags,
424 ~(X509_FLAG_NO_SERIAL | X509_FLAG_NO_VALIDITY)))
426 if (X509_cmp_current_time(X509_get0_notBefore(cert)) > 0)
427 if (BIO_printf(bio, " not yet valid\n") <= 0)
429 if (X509_cmp_current_time(X509_get0_notAfter(cert)) < 0)
430 if (BIO_printf(bio, " no more valid\n") <= 0)
432 return X509_print_ex(bio, cert, flags,
433 ~neg_cflags & ~X509_FLAG_EXTENSIONS_ONLY_KID);
436 static int print_certs(BIO *bio, const STACK_OF(X509) *certs)
440 if (certs == NULL || sk_X509_num(certs) <= 0)
441 return BIO_printf(bio, " (no certificates)\n") >= 0;
443 for (i = 0; i < sk_X509_num(certs); i++) {
444 X509 *cert = sk_X509_value(certs, i);
447 if (!ossl_x509_print_ex_brief(bio, cert, 0))
449 if (!X509V3_extensions_print(bio, NULL,
450 X509_get0_extensions(cert),
451 X509_FLAG_EXTENSIONS_ONLY_KID, 8))
458 static int print_store_certs(BIO *bio, X509_STORE *store)
461 STACK_OF(X509) *certs = X509_STORE_get1_all_certs(store);
462 int ret = print_certs(bio, certs);
464 OSSL_STACK_OF_X509_free(certs);
467 return BIO_printf(bio, " (no trusted store)\n") >= 0;
471 /* Extend the error queue with details on a failed cert verification */
472 int X509_STORE_CTX_print_verify_cb(int ok, X509_STORE_CTX *ctx)
474 if (ok == 0 && ctx != NULL) {
475 int cert_error = X509_STORE_CTX_get_error(ctx);
476 BIO *bio = BIO_new(BIO_s_mem()); /* may be NULL */
480 BIO_printf(bio, "%s at depth = %d error = %d (%s)\n",
481 X509_STORE_CTX_get0_parent_ctx(ctx) != NULL
482 ? "CRL path validation"
483 : "Certificate verification",
484 X509_STORE_CTX_get_error_depth(ctx),
485 cert_error, X509_verify_cert_error_string(cert_error));
487 X509_STORE *ts = X509_STORE_CTX_get0_store(ctx);
488 X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
492 switch (cert_error) {
493 case X509_V_ERR_HOSTNAME_MISMATCH:
494 BIO_printf(bio, "Expected hostname(s) = ");
495 while ((str = X509_VERIFY_PARAM_get0_host(vpm, idx++)) != NULL)
496 BIO_printf(bio, "%s%s", idx == 1 ? "" : ", ", str);
497 BIO_printf(bio, "\n");
499 case X509_V_ERR_EMAIL_MISMATCH:
500 str = X509_VERIFY_PARAM_get0_email(vpm);
502 BIO_printf(bio, "Expected email address = %s\n", str);
504 case X509_V_ERR_IP_ADDRESS_MISMATCH:
505 str = X509_VERIFY_PARAM_get1_ip_asc(vpm);
507 BIO_printf(bio, "Expected IP address = %s\n", str);
515 BIO_printf(bio, "Failure for:\n");
516 ossl_x509_print_ex_brief(bio, X509_STORE_CTX_get_current_cert(ctx),
517 X509_FLAG_NO_EXTENSIONS);
518 if (cert_error == X509_V_ERR_CERT_UNTRUSTED
519 || cert_error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
520 || cert_error == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
521 || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
522 || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
523 || cert_error == X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
524 || cert_error == X509_V_ERR_STORE_LOOKUP) {
525 BIO_printf(bio, "Non-trusted certs:\n");
526 print_certs(bio, X509_STORE_CTX_get0_untrusted(ctx));
527 BIO_printf(bio, "Certs in trust store:\n");
528 print_store_certs(bio, X509_STORE_CTX_get0_store(ctx));
530 ERR_raise(ERR_LIB_X509, X509_R_CERTIFICATE_VERIFICATION_FAILED);
531 ERR_add_error_mem_bio("\n", bio);