t_x509.c 16 KB

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