t_x509.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/x509v3.h>
  16. #include "crypto/asn1.h"
  17. #include "crypto/x509.h"
  18. void OSSL_STACK_OF_X509_free(STACK_OF(X509) *certs)
  19. {
  20. sk_X509_pop_free(certs, X509_free);
  21. }
  22. #ifndef OPENSSL_NO_STDIO
  23. int X509_print_fp(FILE *fp, X509 *x)
  24. {
  25. return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  26. }
  27. int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
  28. unsigned long cflag)
  29. {
  30. BIO *b;
  31. int ret;
  32. if ((b = BIO_new(BIO_s_file())) == NULL) {
  33. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  34. return 0;
  35. }
  36. BIO_set_fp(b, fp, BIO_NOCLOSE);
  37. ret = X509_print_ex(b, x, nmflag, cflag);
  38. BIO_free(b);
  39. return ret;
  40. }
  41. #endif
  42. int X509_print(BIO *bp, X509 *x)
  43. {
  44. return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  45. }
  46. int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
  47. unsigned long cflag)
  48. {
  49. long l;
  50. int ret = 0, i;
  51. char mlch = ' ';
  52. int nmindent = 0, printok = 0;
  53. EVP_PKEY *pkey = NULL;
  54. const char *neg;
  55. if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  56. mlch = '\n';
  57. nmindent = 12;
  58. }
  59. if (nmflags == XN_FLAG_COMPAT)
  60. printok = 1;
  61. if (!(cflag & X509_FLAG_NO_HEADER)) {
  62. if (BIO_write(bp, "Certificate:\n", 13) <= 0)
  63. goto err;
  64. if (BIO_write(bp, " Data:\n", 10) <= 0)
  65. goto err;
  66. }
  67. if (!(cflag & X509_FLAG_NO_VERSION)) {
  68. l = X509_get_version(x);
  69. if (l >= X509_VERSION_1 && l <= X509_VERSION_3) {
  70. if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0)
  71. goto err;
  72. } else {
  73. if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
  74. goto err;
  75. }
  76. }
  77. if (!(cflag & X509_FLAG_NO_SERIAL)) {
  78. const ASN1_INTEGER *bs = X509_get0_serialNumber(x);
  79. if (BIO_write(bp, " Serial Number:", 22) <= 0)
  80. goto err;
  81. if (bs->length <= (int)sizeof(long)) {
  82. ERR_set_mark();
  83. l = ASN1_INTEGER_get(bs);
  84. ERR_pop_to_mark();
  85. } else {
  86. l = -1;
  87. }
  88. if (l != -1) {
  89. unsigned long ul;
  90. if (bs->type == V_ASN1_NEG_INTEGER) {
  91. ul = 0 - (unsigned long)l;
  92. neg = "-";
  93. } else {
  94. ul = l;
  95. neg = "";
  96. }
  97. if (BIO_printf(bp, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0)
  98. goto err;
  99. } else {
  100. neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
  101. if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
  102. goto err;
  103. for (i = 0; i < bs->length; i++) {
  104. if (BIO_printf(bp, "%02x%c", bs->data[i],
  105. ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
  106. goto err;
  107. }
  108. }
  109. }
  110. if (!(cflag & X509_FLAG_NO_SIGNAME)) {
  111. const X509_ALGOR *tsig_alg = X509_get0_tbs_sigalg(x);
  112. if (BIO_puts(bp, " ") <= 0)
  113. goto err;
  114. if (X509_signature_print(bp, tsig_alg, NULL) <= 0)
  115. goto err;
  116. }
  117. if (!(cflag & X509_FLAG_NO_ISSUER)) {
  118. if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
  119. goto err;
  120. if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)
  121. < printok)
  122. goto err;
  123. if (BIO_write(bp, "\n", 1) <= 0)
  124. goto err;
  125. }
  126. if (!(cflag & X509_FLAG_NO_VALIDITY)) {
  127. if (BIO_write(bp, " Validity\n", 17) <= 0)
  128. goto err;
  129. if (BIO_write(bp, " Not Before: ", 24) <= 0)
  130. goto err;
  131. if (ossl_asn1_time_print_ex(bp, X509_get0_notBefore(x), ASN1_DTFLGS_RFC822) == 0)
  132. goto err;
  133. if (BIO_write(bp, "\n Not After : ", 25) <= 0)
  134. goto err;
  135. if (ossl_asn1_time_print_ex(bp, X509_get0_notAfter(x), ASN1_DTFLGS_RFC822) == 0)
  136. goto err;
  137. if (BIO_write(bp, "\n", 1) <= 0)
  138. goto err;
  139. }
  140. if (!(cflag & X509_FLAG_NO_SUBJECT)) {
  141. if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
  142. goto err;
  143. if (X509_NAME_print_ex
  144. (bp, X509_get_subject_name(x), nmindent, nmflags) < printok)
  145. goto err;
  146. if (BIO_write(bp, "\n", 1) <= 0)
  147. goto err;
  148. }
  149. if (!(cflag & X509_FLAG_NO_PUBKEY)) {
  150. X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x);
  151. ASN1_OBJECT *xpoid;
  152. X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey);
  153. if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
  154. goto err;
  155. if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
  156. goto err;
  157. if (i2a_ASN1_OBJECT(bp, xpoid) <= 0)
  158. goto err;
  159. if (BIO_puts(bp, "\n") <= 0)
  160. goto err;
  161. pkey = X509_get0_pubkey(x);
  162. if (pkey == NULL) {
  163. BIO_printf(bp, "%12sUnable to load Public Key\n", "");
  164. ERR_print_errors(bp);
  165. } else {
  166. EVP_PKEY_print_public(bp, pkey, 16, NULL);
  167. }
  168. }
  169. if (!(cflag & X509_FLAG_NO_IDS)) {
  170. const ASN1_BIT_STRING *iuid, *suid;
  171. X509_get0_uids(x, &iuid, &suid);
  172. if (iuid != NULL) {
  173. if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)
  174. goto err;
  175. if (!X509_signature_dump(bp, iuid, 12))
  176. goto err;
  177. }
  178. if (suid != NULL) {
  179. if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)
  180. goto err;
  181. if (!X509_signature_dump(bp, suid, 12))
  182. goto err;
  183. }
  184. }
  185. if (!(cflag & X509_FLAG_NO_EXTENSIONS)
  186. && !X509V3_extensions_print(bp, "X509v3 extensions",
  187. X509_get0_extensions(x), cflag, 8))
  188. goto err;
  189. if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
  190. const X509_ALGOR *sig_alg;
  191. const ASN1_BIT_STRING *sig;
  192. X509_get0_signature(&sig, &sig_alg, x);
  193. if (X509_signature_print(bp, sig_alg, sig) <= 0)
  194. goto err;
  195. }
  196. if (!(cflag & X509_FLAG_NO_AUX)) {
  197. if (!X509_aux_print(bp, x, 0))
  198. goto err;
  199. }
  200. ret = 1;
  201. err:
  202. return ret;
  203. }
  204. int X509_ocspid_print(BIO *bp, X509 *x)
  205. {
  206. unsigned char *der = NULL;
  207. unsigned char *dertmp;
  208. int derlen;
  209. int i;
  210. unsigned char SHA1md[SHA_DIGEST_LENGTH];
  211. ASN1_BIT_STRING *keybstr;
  212. const X509_NAME *subj;
  213. EVP_MD *md = NULL;
  214. if (x == NULL || bp == NULL)
  215. return 0;
  216. /*
  217. * display the hash of the subject as it would appear in OCSP requests
  218. */
  219. if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
  220. goto err;
  221. subj = X509_get_subject_name(x);
  222. derlen = i2d_X509_NAME(subj, NULL);
  223. if (derlen <= 0)
  224. goto err;
  225. if ((der = dertmp = OPENSSL_malloc(derlen)) == NULL)
  226. goto err;
  227. if (i2d_X509_NAME(subj, &dertmp) < 0)
  228. goto err;
  229. md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq);
  230. if (md == NULL)
  231. goto err;
  232. if (!EVP_Digest(der, derlen, SHA1md, NULL, md, NULL))
  233. goto err;
  234. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  235. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  236. goto err;
  237. }
  238. OPENSSL_free(der);
  239. der = NULL;
  240. /*
  241. * display the hash of the public key as it would appear in OCSP requests
  242. */
  243. if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
  244. goto err;
  245. keybstr = X509_get0_pubkey_bitstr(x);
  246. if (keybstr == NULL)
  247. goto err;
  248. if (!EVP_Digest(ASN1_STRING_get0_data(keybstr),
  249. ASN1_STRING_length(keybstr), SHA1md, NULL, md, NULL))
  250. goto err;
  251. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  252. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  253. goto err;
  254. }
  255. BIO_printf(bp, "\n");
  256. EVP_MD_free(md);
  257. return 1;
  258. err:
  259. OPENSSL_free(der);
  260. EVP_MD_free(md);
  261. return 0;
  262. }
  263. int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
  264. {
  265. const unsigned char *s;
  266. int i, n;
  267. n = sig->length;
  268. s = sig->data;
  269. for (i = 0; i < n; i++) {
  270. if ((i % 18) == 0) {
  271. if (i > 0 && BIO_write(bp, "\n", 1) <= 0)
  272. return 0;
  273. if (BIO_indent(bp, indent, indent) <= 0)
  274. return 0;
  275. }
  276. if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0)
  277. return 0;
  278. }
  279. if (BIO_write(bp, "\n", 1) != 1)
  280. return 0;
  281. return 1;
  282. }
  283. int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
  284. const ASN1_STRING *sig)
  285. {
  286. int sig_nid;
  287. int indent = 4;
  288. if (BIO_printf(bp, "%*sSignature Algorithm: ", indent, "") <= 0)
  289. return 0;
  290. if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
  291. return 0;
  292. if (sig && BIO_printf(bp, "\n%*sSignature Value:", indent, "") <= 0)
  293. return 0;
  294. sig_nid = OBJ_obj2nid(sigalg->algorithm);
  295. if (sig_nid != NID_undef) {
  296. int pkey_nid, dig_nid;
  297. const EVP_PKEY_ASN1_METHOD *ameth;
  298. if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
  299. ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
  300. if (ameth && ameth->sig_print)
  301. return ameth->sig_print(bp, sigalg, sig, indent + 4, 0);
  302. }
  303. }
  304. if (BIO_write(bp, "\n", 1) != 1)
  305. return 0;
  306. if (sig)
  307. return X509_signature_dump(bp, sig, indent + 4);
  308. return 1;
  309. }
  310. int X509_aux_print(BIO *out, X509 *x, int indent)
  311. {
  312. char oidstr[80], first;
  313. STACK_OF(ASN1_OBJECT) *trust, *reject;
  314. const unsigned char *alias, *keyid;
  315. int keyidlen;
  316. int i;
  317. if (X509_trusted(x) == 0)
  318. return 1;
  319. trust = X509_get0_trust_objects(x);
  320. reject = X509_get0_reject_objects(x);
  321. if (trust) {
  322. first = 1;
  323. BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
  324. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
  325. if (!first)
  326. BIO_puts(out, ", ");
  327. else
  328. first = 0;
  329. OBJ_obj2txt(oidstr, sizeof(oidstr),
  330. sk_ASN1_OBJECT_value(trust, i), 0);
  331. BIO_puts(out, oidstr);
  332. }
  333. BIO_puts(out, "\n");
  334. } else
  335. BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
  336. if (reject) {
  337. first = 1;
  338. BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
  339. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
  340. if (!first)
  341. BIO_puts(out, ", ");
  342. else
  343. first = 0;
  344. OBJ_obj2txt(oidstr, sizeof(oidstr),
  345. sk_ASN1_OBJECT_value(reject, i), 0);
  346. BIO_puts(out, oidstr);
  347. }
  348. BIO_puts(out, "\n");
  349. } else
  350. BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
  351. alias = X509_alias_get0(x, &i);
  352. if (alias)
  353. BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias);
  354. keyid = X509_keyid_get0(x, &keyidlen);
  355. if (keyid) {
  356. BIO_printf(out, "%*sKey Id: ", indent, "");
  357. for (i = 0; i < keyidlen; i++)
  358. BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]);
  359. BIO_write(out, "\n", 1);
  360. }
  361. return 1;
  362. }
  363. /*
  364. * Helper functions for improving certificate verification error diagnostics
  365. */
  366. int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags)
  367. {
  368. unsigned long flags = ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE |
  369. XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN;
  370. if (cert == NULL)
  371. return BIO_printf(bio, " (no certificate)\n") > 0;
  372. if (BIO_printf(bio, " certificate\n") <= 0
  373. || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_SUBJECT))
  374. return 0;
  375. if (X509_check_issued((X509 *)cert, cert) == X509_V_OK) {
  376. if (BIO_printf(bio, " self-issued\n") <= 0)
  377. return 0;
  378. } else {
  379. if (BIO_printf(bio, " ") <= 0
  380. || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_ISSUER))
  381. return 0;
  382. }
  383. if (!X509_print_ex(bio, cert, flags,
  384. ~(X509_FLAG_NO_SERIAL | X509_FLAG_NO_VALIDITY)))
  385. return 0;
  386. if (X509_cmp_current_time(X509_get0_notBefore(cert)) > 0)
  387. if (BIO_printf(bio, " not yet valid\n") <= 0)
  388. return 0;
  389. if (X509_cmp_current_time(X509_get0_notAfter(cert)) < 0)
  390. if (BIO_printf(bio, " no more valid\n") <= 0)
  391. return 0;
  392. return X509_print_ex(bio, cert, flags,
  393. ~neg_cflags & ~X509_FLAG_EXTENSIONS_ONLY_KID);
  394. }
  395. static int print_certs(BIO *bio, const STACK_OF(X509) *certs)
  396. {
  397. int i;
  398. if (certs == NULL || sk_X509_num(certs) <= 0)
  399. return BIO_printf(bio, " (no certificates)\n") >= 0;
  400. for (i = 0; i < sk_X509_num(certs); i++) {
  401. X509 *cert = sk_X509_value(certs, i);
  402. if (cert != NULL) {
  403. if (!ossl_x509_print_ex_brief(bio, cert, 0))
  404. return 0;
  405. if (!X509V3_extensions_print(bio, NULL,
  406. X509_get0_extensions(cert),
  407. X509_FLAG_EXTENSIONS_ONLY_KID, 8))
  408. return 0;
  409. }
  410. }
  411. return 1;
  412. }
  413. static int print_store_certs(BIO *bio, X509_STORE *store)
  414. {
  415. if (store != NULL) {
  416. STACK_OF(X509) *certs = X509_STORE_get1_all_certs(store);
  417. int ret = print_certs(bio, certs);
  418. OSSL_STACK_OF_X509_free(certs);
  419. return ret;
  420. } else {
  421. return BIO_printf(bio, " (no trusted store)\n") >= 0;
  422. }
  423. }
  424. /* Extend the error queue with details on a failed cert verification */
  425. int X509_STORE_CTX_print_verify_cb(int ok, X509_STORE_CTX *ctx)
  426. {
  427. if (ok == 0 && ctx != NULL) {
  428. int cert_error = X509_STORE_CTX_get_error(ctx);
  429. BIO *bio = BIO_new(BIO_s_mem()); /* may be NULL */
  430. if (bio == NULL)
  431. return 0;
  432. BIO_printf(bio, "%s at depth = %d error = %d (%s)\n",
  433. X509_STORE_CTX_get0_parent_ctx(ctx) != NULL
  434. ? "CRL path validation"
  435. : "Certificate verification",
  436. X509_STORE_CTX_get_error_depth(ctx),
  437. cert_error, X509_verify_cert_error_string(cert_error));
  438. {
  439. X509_STORE *ts = X509_STORE_CTX_get0_store(ctx);
  440. X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
  441. char *str;
  442. int idx = 0;
  443. switch (cert_error) {
  444. case X509_V_ERR_HOSTNAME_MISMATCH:
  445. BIO_printf(bio, "Expected hostname(s) = ");
  446. while ((str = X509_VERIFY_PARAM_get0_host(vpm, idx++)) != NULL)
  447. BIO_printf(bio, "%s%s", idx == 1 ? "" : ", ", str);
  448. BIO_printf(bio, "\n");
  449. break;
  450. case X509_V_ERR_EMAIL_MISMATCH:
  451. str = X509_VERIFY_PARAM_get0_email(vpm);
  452. if (str != NULL)
  453. BIO_printf(bio, "Expected email address = %s\n", str);
  454. break;
  455. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  456. str = X509_VERIFY_PARAM_get1_ip_asc(vpm);
  457. if (str != NULL)
  458. BIO_printf(bio, "Expected IP address = %s\n", str);
  459. OPENSSL_free(str);
  460. break;
  461. default:
  462. break;
  463. }
  464. }
  465. BIO_printf(bio, "Failure for:\n");
  466. ossl_x509_print_ex_brief(bio, X509_STORE_CTX_get_current_cert(ctx),
  467. X509_FLAG_NO_EXTENSIONS);
  468. if (cert_error == X509_V_ERR_CERT_UNTRUSTED
  469. || cert_error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
  470. || cert_error == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
  471. || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
  472. || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  473. || cert_error == X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
  474. || cert_error == X509_V_ERR_STORE_LOOKUP) {
  475. BIO_printf(bio, "Non-trusted certs:\n");
  476. print_certs(bio, X509_STORE_CTX_get0_untrusted(ctx));
  477. BIO_printf(bio, "Certs in trust store:\n");
  478. print_store_certs(bio, X509_STORE_CTX_get0_store(ctx));
  479. }
  480. ERR_raise(ERR_LIB_X509, X509_R_CERTIFICATE_VERIFICATION_FAILED);
  481. ERR_add_error_mem_bio("\n", bio);
  482. BIO_free(bio);
  483. }
  484. return ok;
  485. }