t_x509.c 17 KB

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