You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

548 lines
17 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <ctype.h>
  57. #include <openssl/asn1.h>
  58. #include <openssl/bio.h>
  59. #include <openssl/digest.h>
  60. #include <openssl/err.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/obj.h>
  64. #include <openssl/x509.h>
  65. #include <openssl/x509v3.h>
  66. #include "internal.h"
  67. #ifndef OPENSSL_NO_FP_API
  68. int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
  69. unsigned long cflag)
  70. {
  71. BIO *b;
  72. int ret;
  73. if ((b = BIO_new(BIO_s_file())) == NULL) {
  74. OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
  75. return (0);
  76. }
  77. BIO_set_fp(b, fp, BIO_NOCLOSE);
  78. ret = X509_print_ex(b, x, nmflag, cflag);
  79. BIO_free(b);
  80. return (ret);
  81. }
  82. int X509_print_fp(FILE *fp, X509 *x)
  83. {
  84. return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  85. }
  86. #endif
  87. int X509_print(BIO *bp, X509 *x)
  88. {
  89. return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  90. }
  91. int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
  92. unsigned long cflag)
  93. {
  94. long l;
  95. int ret = 0, i;
  96. char *m = NULL, mlch = ' ';
  97. int nmindent = 0;
  98. X509_CINF *ci;
  99. ASN1_INTEGER *bs;
  100. EVP_PKEY *pkey = NULL;
  101. const char *neg;
  102. if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  103. mlch = '\n';
  104. nmindent = 12;
  105. }
  106. if (nmflags == X509_FLAG_COMPAT)
  107. nmindent = 16;
  108. ci = x->cert_info;
  109. if (!(cflag & X509_FLAG_NO_HEADER)) {
  110. if (BIO_write(bp, "Certificate:\n", 13) <= 0)
  111. goto err;
  112. if (BIO_write(bp, " Data:\n", 10) <= 0)
  113. goto err;
  114. }
  115. if (!(cflag & X509_FLAG_NO_VERSION)) {
  116. l = X509_get_version(x);
  117. if (BIO_printf(bp, "%8sVersion: %lu (0x%lx)\n", "", l + 1, l) <= 0)
  118. goto err;
  119. }
  120. if (!(cflag & X509_FLAG_NO_SERIAL)) {
  121. if (BIO_write(bp, " Serial Number:", 22) <= 0)
  122. goto err;
  123. bs = X509_get_serialNumber(x);
  124. if (bs->length < (int)sizeof(long)
  125. || (bs->length == sizeof(long) && (bs->data[0] & 0x80) == 0)) {
  126. l = ASN1_INTEGER_get(bs);
  127. if (bs->type == V_ASN1_NEG_INTEGER) {
  128. l = -l;
  129. neg = "-";
  130. } else
  131. neg = "";
  132. if (BIO_printf(bp, " %s%lu (%s0x%lx)\n", neg, l, neg, l) <= 0)
  133. goto err;
  134. } else {
  135. neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
  136. if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
  137. goto err;
  138. for (i = 0; i < bs->length; i++) {
  139. if (BIO_printf(bp, "%02x%c", bs->data[i],
  140. ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
  141. goto err;
  142. }
  143. }
  144. }
  145. if (!(cflag & X509_FLAG_NO_SIGNAME)) {
  146. if (X509_signature_print(bp, ci->signature, NULL) <= 0)
  147. goto err;
  148. }
  149. if (!(cflag & X509_FLAG_NO_ISSUER)) {
  150. if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
  151. goto err;
  152. if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)
  153. < 0)
  154. goto err;
  155. if (BIO_write(bp, "\n", 1) <= 0)
  156. goto err;
  157. }
  158. if (!(cflag & X509_FLAG_NO_VALIDITY)) {
  159. if (BIO_write(bp, " Validity\n", 17) <= 0)
  160. goto err;
  161. if (BIO_write(bp, " Not Before: ", 24) <= 0)
  162. goto err;
  163. if (!ASN1_TIME_print(bp, X509_get_notBefore(x)))
  164. goto err;
  165. if (BIO_write(bp, "\n Not After : ", 25) <= 0)
  166. goto err;
  167. if (!ASN1_TIME_print(bp, X509_get_notAfter(x)))
  168. goto err;
  169. if (BIO_write(bp, "\n", 1) <= 0)
  170. goto err;
  171. }
  172. if (!(cflag & X509_FLAG_NO_SUBJECT)) {
  173. if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
  174. goto err;
  175. if (X509_NAME_print_ex
  176. (bp, X509_get_subject_name(x), nmindent, nmflags) < 0)
  177. goto err;
  178. if (BIO_write(bp, "\n", 1) <= 0)
  179. goto err;
  180. }
  181. if (!(cflag & X509_FLAG_NO_PUBKEY)) {
  182. if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
  183. goto err;
  184. if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
  185. goto err;
  186. if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
  187. goto err;
  188. if (BIO_puts(bp, "\n") <= 0)
  189. goto err;
  190. pkey = X509_get_pubkey(x);
  191. if (pkey == NULL) {
  192. BIO_printf(bp, "%12sUnable to load Public Key\n", "");
  193. ERR_print_errors(bp);
  194. } else {
  195. EVP_PKEY_print_public(bp, pkey, 16, NULL);
  196. EVP_PKEY_free(pkey);
  197. }
  198. }
  199. if (!(cflag & X509_FLAG_NO_IDS)) {
  200. if (ci->issuerUID) {
  201. if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)
  202. goto err;
  203. if (!X509_signature_dump(bp, ci->issuerUID, 12))
  204. goto err;
  205. }
  206. if (ci->subjectUID) {
  207. if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)
  208. goto err;
  209. if (!X509_signature_dump(bp, ci->subjectUID, 12))
  210. goto err;
  211. }
  212. }
  213. if (!(cflag & X509_FLAG_NO_EXTENSIONS))
  214. X509V3_extensions_print(bp, "X509v3 extensions",
  215. ci->extensions, cflag, 8);
  216. if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
  217. if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0)
  218. goto err;
  219. }
  220. if (!(cflag & X509_FLAG_NO_AUX)) {
  221. if (!X509_CERT_AUX_print(bp, x->aux, 0))
  222. goto err;
  223. }
  224. ret = 1;
  225. err:
  226. if (m != NULL)
  227. OPENSSL_free(m);
  228. return (ret);
  229. }
  230. int X509_ocspid_print(BIO *bp, X509 *x)
  231. {
  232. unsigned char *der = NULL;
  233. unsigned char *dertmp;
  234. int derlen;
  235. int i;
  236. unsigned char SHA1md[SHA_DIGEST_LENGTH];
  237. /*
  238. * display the hash of the subject as it would appear in OCSP requests
  239. */
  240. if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
  241. goto err;
  242. derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
  243. if ((der = dertmp = (unsigned char *)OPENSSL_malloc(derlen)) == NULL)
  244. goto err;
  245. i2d_X509_NAME(x->cert_info->subject, &dertmp);
  246. if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
  247. goto err;
  248. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  249. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  250. goto err;
  251. }
  252. OPENSSL_free(der);
  253. der = NULL;
  254. /*
  255. * display the hash of the public key as it would appear in OCSP requests
  256. */
  257. if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
  258. goto err;
  259. if (!EVP_Digest(x->cert_info->key->public_key->data,
  260. x->cert_info->key->public_key->length,
  261. SHA1md, NULL, EVP_sha1(), NULL))
  262. goto err;
  263. for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
  264. if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
  265. goto err;
  266. }
  267. BIO_printf(bp, "\n");
  268. return (1);
  269. err:
  270. if (der != NULL)
  271. OPENSSL_free(der);
  272. return (0);
  273. }
  274. int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
  275. const ASN1_STRING *sig)
  276. {
  277. if (BIO_puts(bp, " Signature Algorithm: ") <= 0)
  278. return 0;
  279. if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
  280. return 0;
  281. /* RSA-PSS signatures have parameters to print. */
  282. int sig_nid = OBJ_obj2nid(sigalg->algorithm);
  283. if (sig_nid == NID_rsassaPss &&
  284. !x509_print_rsa_pss_params(bp, sigalg, 9, 0)) {
  285. return 0;
  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 ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
  294. {
  295. int i, n;
  296. char buf[80];
  297. const char *p;
  298. if (v == NULL)
  299. return (0);
  300. n = 0;
  301. p = (const char *)v->data;
  302. for (i = 0; i < v->length; i++) {
  303. if ((p[i] > '~') || ((p[i] < ' ') &&
  304. (p[i] != '\n') && (p[i] != '\r')))
  305. buf[n] = '.';
  306. else
  307. buf[n] = p[i];
  308. n++;
  309. if (n >= 80) {
  310. if (BIO_write(bp, buf, n) <= 0)
  311. return (0);
  312. n = 0;
  313. }
  314. }
  315. if (n > 0)
  316. if (BIO_write(bp, buf, n) <= 0)
  317. return (0);
  318. return (1);
  319. }
  320. int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
  321. {
  322. if (tm->type == V_ASN1_UTCTIME)
  323. return ASN1_UTCTIME_print(bp, tm);
  324. if (tm->type == V_ASN1_GENERALIZEDTIME)
  325. return ASN1_GENERALIZEDTIME_print(bp, tm);
  326. BIO_write(bp, "Bad time value", 14);
  327. return (0);
  328. }
  329. static const char *const mon[12] = {
  330. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  331. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  332. };
  333. int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
  334. {
  335. char *v;
  336. int gmt = 0;
  337. int i;
  338. int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
  339. char *f = NULL;
  340. int f_len = 0;
  341. i = tm->length;
  342. v = (char *)tm->data;
  343. if (i < 12)
  344. goto err;
  345. if (v[i - 1] == 'Z')
  346. gmt = 1;
  347. for (i = 0; i < 12; i++)
  348. if ((v[i] > '9') || (v[i] < '0'))
  349. goto err;
  350. y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 + (v[2] - '0') * 10 + (v[3] -
  351. '0');
  352. M = (v[4] - '0') * 10 + (v[5] - '0');
  353. if ((M > 12) || (M < 1))
  354. goto err;
  355. d = (v[6] - '0') * 10 + (v[7] - '0');
  356. h = (v[8] - '0') * 10 + (v[9] - '0');
  357. m = (v[10] - '0') * 10 + (v[11] - '0');
  358. if (tm->length >= 14 &&
  359. (v[12] >= '0') && (v[12] <= '9') &&
  360. (v[13] >= '0') && (v[13] <= '9')) {
  361. s = (v[12] - '0') * 10 + (v[13] - '0');
  362. /* Check for fractions of seconds. */
  363. if (tm->length >= 15 && v[14] == '.') {
  364. int l = tm->length;
  365. f = &v[14]; /* The decimal point. */
  366. f_len = 1;
  367. while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
  368. ++f_len;
  369. }
  370. }
  371. if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
  372. mon[M - 1], d, h, m, s, f_len, f, y,
  373. (gmt) ? " GMT" : "") <= 0)
  374. return (0);
  375. else
  376. return (1);
  377. err:
  378. BIO_write(bp, "Bad time value", 14);
  379. return (0);
  380. }
  381. // consume_two_digits is a helper function for ASN1_UTCTIME_print. If |*v|,
  382. // assumed to be |*len| bytes long, has two leading digits, updates |*out| with
  383. // their value, updates |v| and |len|, and returns one. Otherwise, returns
  384. // zero.
  385. static int consume_two_digits(int* out, const char **v, int *len) {
  386. if (*len < 2|| !isdigit((*v)[0]) || !isdigit((*v)[1])) {
  387. return 0;
  388. }
  389. *out = ((*v)[0] - '0') * 10 + ((*v)[1] - '0');
  390. *len -= 2;
  391. *v += 2;
  392. return 1;
  393. }
  394. // consume_zulu_timezone is a helper function for ASN1_UTCTIME_print. If |*v|,
  395. // assumed to be |*len| bytes long, starts with "Z" then it updates |*v| and
  396. // |*len| and returns one. Otherwise returns zero.
  397. static int consume_zulu_timezone(const char **v, int *len) {
  398. if (*len == 0 || (*v)[0] != 'Z') {
  399. return 0;
  400. }
  401. *len -= 1;
  402. *v += 1;
  403. return 1;
  404. }
  405. int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) {
  406. const char *v = (const char *)tm->data;
  407. int len = tm->length;
  408. int Y = 0, M = 0, D = 0, h = 0, m = 0, s = 0;
  409. // YYMMDDhhmm are required to be present.
  410. if (!consume_two_digits(&Y, &v, &len) ||
  411. !consume_two_digits(&M, &v, &len) ||
  412. !consume_two_digits(&D, &v, &len) ||
  413. !consume_two_digits(&h, &v, &len) ||
  414. !consume_two_digits(&m, &v, &len)) {
  415. goto err;
  416. }
  417. // https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires seconds
  418. // to be present, but historically this code has forgiven its absence.
  419. consume_two_digits(&s, &v, &len);
  420. // https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, specifies this
  421. // interpretation of the year.
  422. if (Y < 50) {
  423. Y += 2000;
  424. } else {
  425. Y += 1900;
  426. }
  427. if (M > 12 || M == 0) {
  428. goto err;
  429. }
  430. if (D > 31 || D == 0) {
  431. goto err;
  432. }
  433. if (h > 23 || m > 59 || s > 60) {
  434. goto err;
  435. }
  436. // https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires the "Z"
  437. // to be present, but historically this code has forgiven its absence.
  438. const int is_gmt = consume_zulu_timezone(&v, &len);
  439. // https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, does not permit
  440. // the specification of timezones using the +hhmm / -hhmm syntax, which is
  441. // the only other thing that might legitimately be found at the end.
  442. if (len) {
  443. goto err;
  444. }
  445. return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s", mon[M - 1], D, h, m, s, Y,
  446. is_gmt ? " GMT" : "") > 0;
  447. err:
  448. BIO_write(bp, "Bad time value", 14);
  449. return 0;
  450. }
  451. int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
  452. {
  453. char *s, *c, *b;
  454. int ret = 0, l, i;
  455. l = 80 - 2 - obase;
  456. b = X509_NAME_oneline(name, NULL, 0);
  457. if (!b)
  458. return 0;
  459. if (!*b) {
  460. OPENSSL_free(b);
  461. return 1;
  462. }
  463. s = b + 1; /* skip the first slash */
  464. c = s;
  465. for (;;) {
  466. if (((*s == '/') &&
  467. ((s[1] >= 'A') && (s[1] <= 'Z') && ((s[2] == '=') ||
  468. ((s[2] >= 'A')
  469. && (s[2] <= 'Z')
  470. && (s[3] == '='))
  471. ))) || (*s == '\0')) {
  472. i = s - c;
  473. if (BIO_write(bp, c, i) != i)
  474. goto err;
  475. c = s + 1; /* skip following slash */
  476. if (*s != '\0') {
  477. if (BIO_write(bp, ", ", 2) != 2)
  478. goto err;
  479. }
  480. l--;
  481. }
  482. if (*s == '\0')
  483. break;
  484. s++;
  485. l--;
  486. }
  487. ret = 1;
  488. if (0) {
  489. err:
  490. OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
  491. }
  492. OPENSSL_free(b);
  493. return (ret);
  494. }