2
0

t_x509.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 1995-2016 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. #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 (BIO_puts(bp, " ") <= 0)
  109. goto err;
  110. if (X509_signature_print(bp, tsig_alg, NULL) <= 0)
  111. goto err;
  112. }
  113. if (!(cflag & X509_FLAG_NO_ISSUER)) {
  114. if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
  115. goto err;
  116. if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)
  117. < 0)
  118. goto err;
  119. if (BIO_write(bp, "\n", 1) <= 0)
  120. goto err;
  121. }
  122. if (!(cflag & X509_FLAG_NO_VALIDITY)) {
  123. if (BIO_write(bp, " Validity\n", 17) <= 0)
  124. goto err;
  125. if (BIO_write(bp, " Not Before: ", 24) <= 0)
  126. goto err;
  127. if (!ASN1_TIME_print(bp, X509_get0_notBefore(x)))
  128. goto err;
  129. if (BIO_write(bp, "\n Not After : ", 25) <= 0)
  130. goto err;
  131. if (!ASN1_TIME_print(bp, X509_get0_notAfter(x)))
  132. goto err;
  133. if (BIO_write(bp, "\n", 1) <= 0)
  134. goto err;
  135. }
  136. if (!(cflag & X509_FLAG_NO_SUBJECT)) {
  137. if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
  138. goto err;
  139. if (X509_NAME_print_ex
  140. (bp, X509_get_subject_name(x), nmindent, nmflags) < 0)
  141. goto err;
  142. if (BIO_write(bp, "\n", 1) <= 0)
  143. goto err;
  144. }
  145. if (!(cflag & X509_FLAG_NO_PUBKEY)) {
  146. X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x);
  147. ASN1_OBJECT *xpoid;
  148. X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey);
  149. if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
  150. goto err;
  151. if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
  152. goto err;
  153. if (i2a_ASN1_OBJECT(bp, xpoid) <= 0)
  154. goto err;
  155. if (BIO_puts(bp, "\n") <= 0)
  156. goto err;
  157. pkey = X509_get0_pubkey(x);
  158. if (pkey == NULL) {
  159. BIO_printf(bp, "%12sUnable to load Public Key\n", "");
  160. ERR_print_errors(bp);
  161. } else {
  162. EVP_PKEY_print_public(bp, pkey, 16, NULL);
  163. }
  164. }
  165. if (!(cflag & X509_FLAG_NO_IDS)) {
  166. const ASN1_BIT_STRING *iuid, *suid;
  167. X509_get0_uids(x, &iuid, &suid);
  168. if (iuid != NULL) {
  169. if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)
  170. goto err;
  171. if (!X509_signature_dump(bp, iuid, 12))
  172. goto err;
  173. }
  174. if (suid != NULL) {
  175. if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)
  176. goto err;
  177. if (!X509_signature_dump(bp, suid, 12))
  178. goto err;
  179. }
  180. }
  181. if (!(cflag & X509_FLAG_NO_EXTENSIONS))
  182. X509V3_extensions_print(bp, "X509v3 extensions",
  183. X509_get0_extensions(x), cflag, 8);
  184. if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
  185. const X509_ALGOR *sig_alg;
  186. const ASN1_BIT_STRING *sig;
  187. X509_get0_signature(&sig, &sig_alg, x);
  188. if (X509_signature_print(bp, sig_alg, sig) <= 0)
  189. goto err;
  190. }
  191. if (!(cflag & X509_FLAG_NO_AUX)) {
  192. if (!X509_aux_print(bp, x, 0))
  193. goto err;
  194. }
  195. ret = 1;
  196. err:
  197. OPENSSL_free(m);
  198. return ret;
  199. }
  200. int X509_ocspid_print(BIO *bp, X509 *x)
  201. {
  202. unsigned char *der = NULL;
  203. unsigned char *dertmp;
  204. int derlen;
  205. int i;
  206. unsigned char SHA1md[SHA_DIGEST_LENGTH];
  207. ASN1_BIT_STRING *keybstr;
  208. X509_NAME *subj;
  209. /*
  210. * display the hash of the subject as it would appear in OCSP requests
  211. */
  212. if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
  213. goto err;
  214. subj = X509_get_subject_name(x);
  215. derlen = i2d_X509_NAME(subj, NULL);
  216. if ((der = dertmp = OPENSSL_malloc(derlen)) == NULL)
  217. goto err;
  218. i2d_X509_NAME(subj, &dertmp);
  219. if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
  220. goto err;
  221. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  222. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  223. goto err;
  224. }
  225. OPENSSL_free(der);
  226. der = NULL;
  227. /*
  228. * display the hash of the public key as it would appear in OCSP requests
  229. */
  230. if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
  231. goto err;
  232. keybstr = X509_get0_pubkey_bitstr(x);
  233. if (keybstr == NULL)
  234. goto err;
  235. if (!EVP_Digest(ASN1_STRING_get0_data(keybstr),
  236. ASN1_STRING_length(keybstr), SHA1md, NULL, EVP_sha1(),
  237. NULL))
  238. goto err;
  239. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  240. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  241. goto err;
  242. }
  243. BIO_printf(bp, "\n");
  244. return 1;
  245. err:
  246. OPENSSL_free(der);
  247. return 0;
  248. }
  249. int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
  250. {
  251. const unsigned char *s;
  252. int i, n;
  253. n = sig->length;
  254. s = sig->data;
  255. for (i = 0; i < n; i++) {
  256. if ((i % 18) == 0) {
  257. if (i > 0 && BIO_write(bp, "\n", 1) <= 0)
  258. return 0;
  259. if (BIO_indent(bp, indent, indent) <= 0)
  260. return 0;
  261. }
  262. if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0)
  263. return 0;
  264. }
  265. if (BIO_write(bp, "\n", 1) != 1)
  266. return 0;
  267. return 1;
  268. }
  269. int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
  270. const ASN1_STRING *sig)
  271. {
  272. int sig_nid;
  273. int indent = 4;
  274. if (BIO_printf(bp, "%*sSignature Algorithm: ", indent, "") <= 0)
  275. return 0;
  276. if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
  277. return 0;
  278. if (sig && BIO_printf(bp, "\n%*sSignature Value:", indent, "") <= 0)
  279. return 0;
  280. sig_nid = OBJ_obj2nid(sigalg->algorithm);
  281. if (sig_nid != NID_undef) {
  282. int pkey_nid, dig_nid;
  283. const EVP_PKEY_ASN1_METHOD *ameth;
  284. if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
  285. ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
  286. if (ameth && ameth->sig_print)
  287. return ameth->sig_print(bp, sigalg, sig, indent + 4, 0);
  288. }
  289. }
  290. if (BIO_write(bp, "\n", 1) != 1)
  291. return 0;
  292. if (sig)
  293. return X509_signature_dump(bp, sig, indent + 4);
  294. return 1;
  295. }
  296. int X509_aux_print(BIO *out, X509 *x, int indent)
  297. {
  298. char oidstr[80], first;
  299. STACK_OF(ASN1_OBJECT) *trust, *reject;
  300. const unsigned char *alias, *keyid;
  301. int keyidlen;
  302. int i;
  303. if (X509_trusted(x) == 0)
  304. return 1;
  305. trust = X509_get0_trust_objects(x);
  306. reject = X509_get0_reject_objects(x);
  307. if (trust) {
  308. first = 1;
  309. BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
  310. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
  311. if (!first)
  312. BIO_puts(out, ", ");
  313. else
  314. first = 0;
  315. OBJ_obj2txt(oidstr, sizeof(oidstr),
  316. sk_ASN1_OBJECT_value(trust, i), 0);
  317. BIO_puts(out, oidstr);
  318. }
  319. BIO_puts(out, "\n");
  320. } else
  321. BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
  322. if (reject) {
  323. first = 1;
  324. BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
  325. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
  326. if (!first)
  327. BIO_puts(out, ", ");
  328. else
  329. first = 0;
  330. OBJ_obj2txt(oidstr, sizeof(oidstr),
  331. sk_ASN1_OBJECT_value(reject, i), 0);
  332. BIO_puts(out, oidstr);
  333. }
  334. BIO_puts(out, "\n");
  335. } else
  336. BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
  337. alias = X509_alias_get0(x, NULL);
  338. if (alias)
  339. BIO_printf(out, "%*sAlias: %s\n", indent, "", alias);
  340. keyid = X509_keyid_get0(x, &keyidlen);
  341. if (keyid) {
  342. BIO_printf(out, "%*sKey Id: ", indent, "");
  343. for (i = 0; i < keyidlen; i++)
  344. BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]);
  345. BIO_write(out, "\n", 1);
  346. }
  347. return 1;
  348. }