t_x509.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/asn1_int.h"
  17. #ifndef OPENSSL_NO_STDIO
  18. int X509_print_fp(FILE *fp, X509 *x)
  19. {
  20. return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  21. }
  22. int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
  23. unsigned long cflag)
  24. {
  25. BIO *b;
  26. int ret;
  27. if ((b = BIO_new(BIO_s_file())) == NULL) {
  28. X509err(X509_F_X509_PRINT_EX_FP, ERR_R_BUF_LIB);
  29. return (0);
  30. }
  31. BIO_set_fp(b, fp, BIO_NOCLOSE);
  32. ret = X509_print_ex(b, x, nmflag, cflag);
  33. BIO_free(b);
  34. return (ret);
  35. }
  36. #endif
  37. int X509_print(BIO *bp, X509 *x)
  38. {
  39. return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  40. }
  41. int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
  42. unsigned long cflag)
  43. {
  44. long l;
  45. int ret = 0, i;
  46. char *m = NULL, mlch = ' ';
  47. int nmindent = 0;
  48. ASN1_INTEGER *bs;
  49. EVP_PKEY *pkey = NULL;
  50. const char *neg;
  51. if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  52. mlch = '\n';
  53. nmindent = 12;
  54. }
  55. if (nmflags == X509_FLAG_COMPAT)
  56. nmindent = 16;
  57. if (!(cflag & X509_FLAG_NO_HEADER)) {
  58. if (BIO_write(bp, "Certificate:\n", 13) <= 0)
  59. goto err;
  60. if (BIO_write(bp, " Data:\n", 10) <= 0)
  61. goto err;
  62. }
  63. if (!(cflag & X509_FLAG_NO_VERSION)) {
  64. l = X509_get_version(x);
  65. if (l >= 0 && l <= 2) {
  66. if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0)
  67. goto err;
  68. } else {
  69. if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
  70. goto err;
  71. }
  72. }
  73. if (!(cflag & X509_FLAG_NO_SERIAL)) {
  74. if (BIO_write(bp, " Serial Number:", 22) <= 0)
  75. goto err;
  76. bs = X509_get_serialNumber(x);
  77. if (bs->length <= (int)sizeof(long)) {
  78. ERR_set_mark();
  79. l = ASN1_INTEGER_get(bs);
  80. ERR_pop_to_mark();
  81. } else {
  82. l = -1;
  83. }
  84. if (l != -1) {
  85. unsigned long ul;
  86. if (bs->type == V_ASN1_NEG_INTEGER) {
  87. ul = 0 - (unsigned long)l;
  88. neg = "-";
  89. } else {
  90. ul = l;
  91. neg = "";
  92. }
  93. if (BIO_printf(bp, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0)
  94. goto err;
  95. } else {
  96. neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
  97. if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
  98. goto err;
  99. for (i = 0; i < bs->length; i++) {
  100. if (BIO_printf(bp, "%02x%c", bs->data[i],
  101. ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
  102. goto err;
  103. }
  104. }
  105. }
  106. if (!(cflag & X509_FLAG_NO_SIGNAME)) {
  107. const X509_ALGOR *tsig_alg = X509_get0_tbs_sigalg(x);
  108. if (X509_signature_print(bp, tsig_alg, NULL) <= 0)
  109. goto err;
  110. }
  111. if (!(cflag & X509_FLAG_NO_ISSUER)) {
  112. if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
  113. goto err;
  114. if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)
  115. < 0)
  116. goto err;
  117. if (BIO_write(bp, "\n", 1) <= 0)
  118. goto err;
  119. }
  120. if (!(cflag & X509_FLAG_NO_VALIDITY)) {
  121. if (BIO_write(bp, " Validity\n", 17) <= 0)
  122. goto err;
  123. if (BIO_write(bp, " Not Before: ", 24) <= 0)
  124. goto err;
  125. if (!ASN1_TIME_print(bp, X509_get0_notBefore(x)))
  126. goto err;
  127. if (BIO_write(bp, "\n Not After : ", 25) <= 0)
  128. goto err;
  129. if (!ASN1_TIME_print(bp, X509_get0_notAfter(x)))
  130. goto err;
  131. if (BIO_write(bp, "\n", 1) <= 0)
  132. goto err;
  133. }
  134. if (!(cflag & X509_FLAG_NO_SUBJECT)) {
  135. if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
  136. goto err;
  137. if (X509_NAME_print_ex
  138. (bp, X509_get_subject_name(x), nmindent, nmflags) < 0)
  139. goto err;
  140. if (BIO_write(bp, "\n", 1) <= 0)
  141. goto err;
  142. }
  143. if (!(cflag & X509_FLAG_NO_PUBKEY)) {
  144. X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x);
  145. ASN1_OBJECT *xpoid;
  146. X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey);
  147. if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
  148. goto err;
  149. if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
  150. goto err;
  151. if (i2a_ASN1_OBJECT(bp, xpoid) <= 0)
  152. goto err;
  153. if (BIO_puts(bp, "\n") <= 0)
  154. goto err;
  155. pkey = X509_get0_pubkey(x);
  156. if (pkey == NULL) {
  157. BIO_printf(bp, "%12sUnable to load Public Key\n", "");
  158. ERR_print_errors(bp);
  159. } else {
  160. EVP_PKEY_print_public(bp, pkey, 16, NULL);
  161. }
  162. }
  163. if (!(cflag & X509_FLAG_NO_IDS)) {
  164. const ASN1_BIT_STRING *iuid, *suid;
  165. X509_get0_uids(x, &iuid, &suid);
  166. if (iuid != NULL) {
  167. if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)
  168. goto err;
  169. if (!X509_signature_dump(bp, iuid, 12))
  170. goto err;
  171. }
  172. if (suid != NULL) {
  173. if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)
  174. goto err;
  175. if (!X509_signature_dump(bp, suid, 12))
  176. goto err;
  177. }
  178. }
  179. if (!(cflag & X509_FLAG_NO_EXTENSIONS))
  180. X509V3_extensions_print(bp, "X509v3 extensions",
  181. X509_get0_extensions(x), cflag, 8);
  182. if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
  183. const X509_ALGOR *sig_alg;
  184. const ASN1_BIT_STRING *sig;
  185. X509_get0_signature(&sig, &sig_alg, x);
  186. if (X509_signature_print(bp, sig_alg, sig) <= 0)
  187. goto err;
  188. }
  189. if (!(cflag & X509_FLAG_NO_AUX)) {
  190. if (!X509_aux_print(bp, x, 0))
  191. goto err;
  192. }
  193. ret = 1;
  194. err:
  195. OPENSSL_free(m);
  196. return (ret);
  197. }
  198. int X509_ocspid_print(BIO *bp, X509 *x)
  199. {
  200. unsigned char *der = NULL;
  201. unsigned char *dertmp;
  202. int derlen;
  203. int i;
  204. unsigned char SHA1md[SHA_DIGEST_LENGTH];
  205. ASN1_BIT_STRING *keybstr;
  206. X509_NAME *subj;
  207. /*
  208. * display the hash of the subject as it would appear in OCSP requests
  209. */
  210. if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
  211. goto err;
  212. subj = X509_get_subject_name(x);
  213. derlen = i2d_X509_NAME(subj, NULL);
  214. if ((der = dertmp = OPENSSL_malloc(derlen)) == NULL)
  215. goto err;
  216. i2d_X509_NAME(subj, &dertmp);
  217. if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
  218. goto err;
  219. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  220. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  221. goto err;
  222. }
  223. OPENSSL_free(der);
  224. der = NULL;
  225. /*
  226. * display the hash of the public key as it would appear in OCSP requests
  227. */
  228. if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
  229. goto err;
  230. keybstr = X509_get0_pubkey_bitstr(x);
  231. if (keybstr == NULL)
  232. goto err;
  233. if (!EVP_Digest(ASN1_STRING_get0_data(keybstr),
  234. ASN1_STRING_length(keybstr), SHA1md, NULL, EVP_sha1(),
  235. NULL))
  236. goto err;
  237. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  238. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  239. goto err;
  240. }
  241. BIO_printf(bp, "\n");
  242. return (1);
  243. err:
  244. OPENSSL_free(der);
  245. return (0);
  246. }
  247. int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
  248. {
  249. const unsigned char *s;
  250. int i, n;
  251. n = sig->length;
  252. s = sig->data;
  253. for (i = 0; i < n; i++) {
  254. if ((i % 18) == 0) {
  255. if (BIO_write(bp, "\n", 1) <= 0)
  256. return 0;
  257. if (BIO_indent(bp, indent, indent) <= 0)
  258. return 0;
  259. }
  260. if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0)
  261. return 0;
  262. }
  263. if (BIO_write(bp, "\n", 1) != 1)
  264. return 0;
  265. return 1;
  266. }
  267. int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
  268. const ASN1_STRING *sig)
  269. {
  270. int sig_nid;
  271. if (BIO_puts(bp, " Signature Algorithm: ") <= 0)
  272. return 0;
  273. if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
  274. return 0;
  275. sig_nid = OBJ_obj2nid(sigalg->algorithm);
  276. if (sig_nid != NID_undef) {
  277. int pkey_nid, dig_nid;
  278. const EVP_PKEY_ASN1_METHOD *ameth;
  279. if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
  280. ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
  281. if (ameth && ameth->sig_print)
  282. return ameth->sig_print(bp, sigalg, sig, 9, 0);
  283. }
  284. }
  285. if (sig)
  286. return X509_signature_dump(bp, sig, 9);
  287. else if (BIO_puts(bp, "\n") <= 0)
  288. return 0;
  289. return 1;
  290. }
  291. int X509_aux_print(BIO *out, X509 *x, int indent)
  292. {
  293. char oidstr[80], first;
  294. STACK_OF(ASN1_OBJECT) *trust, *reject;
  295. const unsigned char *alias, *keyid;
  296. int keyidlen;
  297. int i;
  298. if (X509_trusted(x) == 0)
  299. return 1;
  300. trust = X509_get0_trust_objects(x);
  301. reject = X509_get0_reject_objects(x);
  302. if (trust) {
  303. first = 1;
  304. BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
  305. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
  306. if (!first)
  307. BIO_puts(out, ", ");
  308. else
  309. first = 0;
  310. OBJ_obj2txt(oidstr, sizeof oidstr,
  311. sk_ASN1_OBJECT_value(trust, i), 0);
  312. BIO_puts(out, oidstr);
  313. }
  314. BIO_puts(out, "\n");
  315. } else
  316. BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
  317. if (reject) {
  318. first = 1;
  319. BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
  320. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
  321. if (!first)
  322. BIO_puts(out, ", ");
  323. else
  324. first = 0;
  325. OBJ_obj2txt(oidstr, sizeof oidstr,
  326. sk_ASN1_OBJECT_value(reject, i), 0);
  327. BIO_puts(out, oidstr);
  328. }
  329. BIO_puts(out, "\n");
  330. } else
  331. BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
  332. alias = X509_alias_get0(x, NULL);
  333. if (alias)
  334. BIO_printf(out, "%*sAlias: %s\n", indent, "", alias);
  335. keyid = X509_keyid_get0(x, &keyidlen);
  336. if (keyid) {
  337. BIO_printf(out, "%*sKey Id: ", indent, "");
  338. for (i = 0; i < keyidlen; i++)
  339. BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]);
  340. BIO_write(out, "\n", 1);
  341. }
  342. return 1;
  343. }