t_x509.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright 1995-2021 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 "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 (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. if (BIO_puts(bp, " Signature Algorithm: ") <= 0)
  274. return 0;
  275. if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
  276. return 0;
  277. sig_nid = OBJ_obj2nid(sigalg->algorithm);
  278. if (sig_nid != NID_undef) {
  279. int pkey_nid, dig_nid;
  280. const EVP_PKEY_ASN1_METHOD *ameth;
  281. if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
  282. ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
  283. if (ameth && ameth->sig_print)
  284. return ameth->sig_print(bp, sigalg, sig, 9, 0);
  285. }
  286. }
  287. if (sig)
  288. return X509_signature_dump(bp, sig, 9);
  289. else if (BIO_puts(bp, "\n") <= 0)
  290. return 0;
  291. return 1;
  292. }
  293. int X509_aux_print(BIO *out, X509 *x, int indent)
  294. {
  295. char oidstr[80], first;
  296. STACK_OF(ASN1_OBJECT) *trust, *reject;
  297. const unsigned char *alias, *keyid;
  298. int keyidlen;
  299. int i;
  300. if (X509_trusted(x) == 0)
  301. return 1;
  302. trust = X509_get0_trust_objects(x);
  303. reject = X509_get0_reject_objects(x);
  304. if (trust) {
  305. first = 1;
  306. BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, "");
  307. for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
  308. if (!first)
  309. BIO_puts(out, ", ");
  310. else
  311. first = 0;
  312. OBJ_obj2txt(oidstr, sizeof(oidstr),
  313. sk_ASN1_OBJECT_value(trust, i), 0);
  314. BIO_puts(out, oidstr);
  315. }
  316. BIO_puts(out, "\n");
  317. } else
  318. BIO_printf(out, "%*sNo Trusted Uses.\n", indent, "");
  319. if (reject) {
  320. first = 1;
  321. BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, "");
  322. for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
  323. if (!first)
  324. BIO_puts(out, ", ");
  325. else
  326. first = 0;
  327. OBJ_obj2txt(oidstr, sizeof(oidstr),
  328. sk_ASN1_OBJECT_value(reject, i), 0);
  329. BIO_puts(out, oidstr);
  330. }
  331. BIO_puts(out, "\n");
  332. } else
  333. BIO_printf(out, "%*sNo Rejected Uses.\n", indent, "");
  334. alias = X509_alias_get0(x, &i);
  335. if (alias)
  336. BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias);
  337. keyid = X509_keyid_get0(x, &keyidlen);
  338. if (keyid) {
  339. BIO_printf(out, "%*sKey Id: ", indent, "");
  340. for (i = 0; i < keyidlen; i++)
  341. BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]);
  342. BIO_write(out, "\n", 1);
  343. }
  344. return 1;
  345. }