t_x509.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright 1995-2022 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. i2d_X509_NAME(subj, &dertmp);
  228. md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq);
  229. if (md == NULL)
  230. goto err;
  231. if (!EVP_Digest(der, derlen, SHA1md, NULL, md, NULL))
  232. goto err;
  233. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  234. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  235. goto err;
  236. }
  237. OPENSSL_free(der);
  238. der = NULL;
  239. /*
  240. * display the hash of the public key as it would appear in OCSP requests
  241. */
  242. if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
  243. goto err;
  244. keybstr = X509_get0_pubkey_bitstr(x);
  245. if (keybstr == NULL)
  246. goto err;
  247. if (!EVP_Digest(ASN1_STRING_get0_data(keybstr),
  248. ASN1_STRING_length(keybstr), SHA1md, NULL, md, NULL))
  249. goto err;
  250. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  251. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  252. goto err;
  253. }
  254. BIO_printf(bp, "\n");
  255. EVP_MD_free(md);
  256. return 1;
  257. err:
  258. OPENSSL_free(der);
  259. EVP_MD_free(md);
  260. return 0;
  261. }
  262. int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
  263. {
  264. const unsigned char *s;
  265. int i, n;
  266. n = sig->length;
  267. s = sig->data;
  268. for (i = 0; i < n; i++) {
  269. if ((i % 18) == 0) {
  270. if (i > 0 && BIO_write(bp, "\n", 1) <= 0)
  271. return 0;
  272. if (BIO_indent(bp, indent, indent) <= 0)
  273. return 0;
  274. }
  275. if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0)
  276. return 0;
  277. }
  278. if (BIO_write(bp, "\n", 1) != 1)
  279. return 0;
  280. return 1;
  281. }
  282. int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
  283. const ASN1_STRING *sig)
  284. {
  285. int sig_nid;
  286. int indent = 4;
  287. if (BIO_printf(bp, "%*sSignature Algorithm: ", indent, "") <= 0)
  288. return 0;
  289. if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
  290. return 0;
  291. if (sig && BIO_printf(bp, "\n%*sSignature Value:", indent, "") <= 0)
  292. return 0;
  293. sig_nid = OBJ_obj2nid(sigalg->algorithm);
  294. if (sig_nid != NID_undef) {
  295. int pkey_nid, dig_nid;
  296. const EVP_PKEY_ASN1_METHOD *ameth;
  297. if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
  298. ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
  299. if (ameth && ameth->sig_print)
  300. return ameth->sig_print(bp, sigalg, sig, indent + 4, 0);
  301. }
  302. }
  303. if (BIO_write(bp, "\n", 1) != 1)
  304. return 0;
  305. if (sig)
  306. return X509_signature_dump(bp, sig, indent + 4);
  307. return 1;
  308. }
  309. int X509_aux_print(BIO *out, X509 *x, int indent)
  310. {
  311. char oidstr[80], first;
  312. STACK_OF(ASN1_OBJECT) *trust, *reject;
  313. const unsigned char *alias, *keyid;
  314. int keyidlen;
  315. int i;
  316. if (X509_trusted(x) == 0)
  317. return 1;
  318. trust = X509_get0_trust_objects(x);
  319. reject = X509_get0_reject_objects(x);
  320. if (trust) {
  321. first = 1;
  322. BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
  323. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
  324. if (!first)
  325. BIO_puts(out, ", ");
  326. else
  327. first = 0;
  328. OBJ_obj2txt(oidstr, sizeof(oidstr),
  329. sk_ASN1_OBJECT_value(trust, i), 0);
  330. BIO_puts(out, oidstr);
  331. }
  332. BIO_puts(out, "\n");
  333. } else
  334. BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
  335. if (reject) {
  336. first = 1;
  337. BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
  338. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
  339. if (!first)
  340. BIO_puts(out, ", ");
  341. else
  342. first = 0;
  343. OBJ_obj2txt(oidstr, sizeof(oidstr),
  344. sk_ASN1_OBJECT_value(reject, i), 0);
  345. BIO_puts(out, oidstr);
  346. }
  347. BIO_puts(out, "\n");
  348. } else
  349. BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
  350. alias = X509_alias_get0(x, &i);
  351. if (alias)
  352. BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias);
  353. keyid = X509_keyid_get0(x, &keyidlen);
  354. if (keyid) {
  355. BIO_printf(out, "%*sKey Id: ", indent, "");
  356. for (i = 0; i < keyidlen; i++)
  357. BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]);
  358. BIO_write(out, "\n", 1);
  359. }
  360. return 1;
  361. }
  362. /*
  363. * Helper functions for improving certificate verification error diagnostics
  364. */
  365. int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags)
  366. {
  367. unsigned long flags = ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE |
  368. XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN;
  369. if (cert == NULL)
  370. return BIO_printf(bio, " (no certificate)\n") > 0;
  371. if (BIO_printf(bio, " certificate\n") <= 0
  372. || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_SUBJECT))
  373. return 0;
  374. if (X509_check_issued((X509 *)cert, cert) == X509_V_OK) {
  375. if (BIO_printf(bio, " self-issued\n") <= 0)
  376. return 0;
  377. } else {
  378. if (BIO_printf(bio, " ") <= 0
  379. || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_ISSUER))
  380. return 0;
  381. }
  382. if (!X509_print_ex(bio, cert, flags,
  383. ~(X509_FLAG_NO_SERIAL | X509_FLAG_NO_VALIDITY)))
  384. return 0;
  385. if (X509_cmp_current_time(X509_get0_notBefore(cert)) > 0)
  386. if (BIO_printf(bio, " not yet valid\n") <= 0)
  387. return 0;
  388. if (X509_cmp_current_time(X509_get0_notAfter(cert)) < 0)
  389. if (BIO_printf(bio, " no more valid\n") <= 0)
  390. return 0;
  391. return X509_print_ex(bio, cert, flags,
  392. ~neg_cflags & ~X509_FLAG_EXTENSIONS_ONLY_KID);
  393. }
  394. static int print_certs(BIO *bio, const STACK_OF(X509) *certs)
  395. {
  396. int i;
  397. if (certs == NULL || sk_X509_num(certs) <= 0)
  398. return BIO_printf(bio, " (no certificates)\n") >= 0;
  399. for (i = 0; i < sk_X509_num(certs); i++) {
  400. X509 *cert = sk_X509_value(certs, i);
  401. if (cert != NULL) {
  402. if (!ossl_x509_print_ex_brief(bio, cert, 0))
  403. return 0;
  404. if (!X509V3_extensions_print(bio, NULL,
  405. X509_get0_extensions(cert),
  406. X509_FLAG_EXTENSIONS_ONLY_KID, 8))
  407. return 0;
  408. }
  409. }
  410. return 1;
  411. }
  412. static int print_store_certs(BIO *bio, X509_STORE *store)
  413. {
  414. if (store != NULL) {
  415. STACK_OF(X509) *certs = X509_STORE_get1_all_certs(store);
  416. int ret = print_certs(bio, certs);
  417. OSSL_STACK_OF_X509_free(certs);
  418. return ret;
  419. } else {
  420. return BIO_printf(bio, " (no trusted store)\n") >= 0;
  421. }
  422. }
  423. /* Extend the error queue with details on a failed cert verification */
  424. int X509_STORE_CTX_print_verify_cb(int ok, X509_STORE_CTX *ctx)
  425. {
  426. if (ok == 0 && ctx != NULL) {
  427. int cert_error = X509_STORE_CTX_get_error(ctx);
  428. BIO *bio = BIO_new(BIO_s_mem()); /* may be NULL */
  429. if (bio == NULL)
  430. return 0;
  431. BIO_printf(bio, "%s at depth = %d error = %d (%s)\n",
  432. X509_STORE_CTX_get0_parent_ctx(ctx) != NULL
  433. ? "CRL path validation"
  434. : "Certificate verification",
  435. X509_STORE_CTX_get_error_depth(ctx),
  436. cert_error, X509_verify_cert_error_string(cert_error));
  437. {
  438. X509_STORE *ts = X509_STORE_CTX_get0_store(ctx);
  439. X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
  440. char *str;
  441. int idx = 0;
  442. switch (cert_error) {
  443. case X509_V_ERR_HOSTNAME_MISMATCH:
  444. BIO_printf(bio, "Expected hostname(s) = ");
  445. while ((str = X509_VERIFY_PARAM_get0_host(vpm, idx++)) != NULL)
  446. BIO_printf(bio, "%s%s", idx == 1 ? "" : ", ", str);
  447. BIO_printf(bio, "\n");
  448. break;
  449. case X509_V_ERR_EMAIL_MISMATCH:
  450. str = X509_VERIFY_PARAM_get0_email(vpm);
  451. if (str != NULL)
  452. BIO_printf(bio, "Expected email address = %s\n", str);
  453. break;
  454. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  455. str = X509_VERIFY_PARAM_get1_ip_asc(vpm);
  456. if (str != NULL)
  457. BIO_printf(bio, "Expected IP address = %s\n", str);
  458. OPENSSL_free(str);
  459. break;
  460. default:
  461. break;
  462. }
  463. }
  464. BIO_printf(bio, "Failure for:\n");
  465. ossl_x509_print_ex_brief(bio, X509_STORE_CTX_get_current_cert(ctx),
  466. X509_FLAG_NO_EXTENSIONS);
  467. if (cert_error == X509_V_ERR_CERT_UNTRUSTED
  468. || cert_error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
  469. || cert_error == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
  470. || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
  471. || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  472. || cert_error == X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
  473. || cert_error == X509_V_ERR_STORE_LOOKUP) {
  474. BIO_printf(bio, "Non-trusted certs:\n");
  475. print_certs(bio, X509_STORE_CTX_get0_untrusted(ctx));
  476. BIO_printf(bio, "Certs in trust store:\n");
  477. print_store_certs(bio, X509_STORE_CTX_get0_store(ctx));
  478. }
  479. ERR_raise(ERR_LIB_X509, X509_R_CERTIFICATE_VERIFICATION_FAILED);
  480. ERR_add_error_mem_bio("\n", bio);
  481. BIO_free(bio);
  482. }
  483. return ok;
  484. }