x509aux.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL licenses, (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/pem.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/err.h>
  17. #include "../e_os.h"
  18. static const char *progname;
  19. static void test_usage(void)
  20. {
  21. fprintf(stderr, "usage: %s certfile\n", progname);
  22. }
  23. static void print_errors(void)
  24. {
  25. unsigned long err;
  26. char buffer[1024];
  27. const char *file;
  28. const char *data;
  29. int line;
  30. int flags;
  31. while ((err = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
  32. ERR_error_string_n(err, buffer, sizeof(buffer));
  33. if (flags & ERR_TXT_STRING)
  34. fprintf(stderr, "Error: %s:%s:%d:%s\n", buffer, file, line, data);
  35. else
  36. fprintf(stderr, "Error: %s:%s:%d\n", buffer, file, line);
  37. }
  38. }
  39. static int test_certs(BIO *fp)
  40. {
  41. int count;
  42. char *name = 0;
  43. char *header = 0;
  44. unsigned char *data = 0;
  45. long len;
  46. typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
  47. typedef int (*i2d_X509_t)(X509 *, unsigned char **);
  48. int err = 0;
  49. for (count = 0;
  50. !err && PEM_read_bio(fp, &name, &header, &data, &len);
  51. ++count) {
  52. int trusted = strcmp(name, PEM_STRING_X509_TRUSTED) == 0;
  53. d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509;
  54. i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509;
  55. X509 *cert = NULL;
  56. const unsigned char *p = data;
  57. unsigned char *buf = NULL;
  58. unsigned char *bufp;
  59. long enclen;
  60. if (!trusted
  61. && strcmp(name, PEM_STRING_X509) != 0
  62. && strcmp(name, PEM_STRING_X509_OLD) != 0) {
  63. fprintf(stderr, "unexpected PEM object: %s\n", name);
  64. err = 1;
  65. goto next;
  66. }
  67. cert = d2i(NULL, &p, len);
  68. if (cert == NULL || (p - data) != len) {
  69. fprintf(stderr, "error parsing input %s\n", name);
  70. err = 1;
  71. goto next;
  72. }
  73. /* Test traditional 2-pass encoding into caller allocated buffer */
  74. enclen = i2d(cert, NULL);
  75. if (len != enclen) {
  76. fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
  77. enclen, name, len);
  78. err = 1;
  79. goto next;
  80. }
  81. if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
  82. perror("malloc");
  83. err = 1;
  84. goto next;
  85. }
  86. enclen = i2d(cert, &bufp);
  87. if (len != enclen) {
  88. fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
  89. enclen, name, len);
  90. err = 1;
  91. goto next;
  92. }
  93. enclen = (long) (bufp - buf);
  94. if (enclen != len) {
  95. fprintf(stderr, "unexpected buffer position after encoding %s\n",
  96. name);
  97. err = 1;
  98. goto next;
  99. }
  100. if (memcmp(buf, data, len) != 0) {
  101. fprintf(stderr, "encoded content of %s does not match input\n",
  102. name);
  103. err = 1;
  104. goto next;
  105. }
  106. OPENSSL_free(buf);
  107. buf = NULL;
  108. /* Test 1-pass encoding into library allocated buffer */
  109. enclen = i2d(cert, &buf);
  110. if (len != enclen) {
  111. fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
  112. enclen, name, len);
  113. err = 1;
  114. goto next;
  115. }
  116. if (memcmp(buf, data, len) != 0) {
  117. fprintf(stderr, "encoded content of %s does not match input\n",
  118. name);
  119. err = 1;
  120. goto next;
  121. }
  122. if (trusted) {
  123. /* Encode just the cert and compare with initial encoding */
  124. OPENSSL_free(buf);
  125. buf = NULL;
  126. /* Test 1-pass encoding into library allocated buffer */
  127. enclen = i2d(cert, &buf);
  128. if (enclen > len) {
  129. fprintf(stderr, "encoded length %ld of %s > input length %ld\n",
  130. enclen, name, len);
  131. err = 1;
  132. goto next;
  133. }
  134. if (memcmp(buf, data, enclen) != 0) {
  135. fprintf(stderr, "encoded cert content does not match input\n");
  136. err = 1;
  137. goto next;
  138. }
  139. }
  140. /*
  141. * If any of these were null, PEM_read() would have failed.
  142. */
  143. next:
  144. X509_free(cert);
  145. OPENSSL_free(buf);
  146. OPENSSL_free(name);
  147. OPENSSL_free(header);
  148. OPENSSL_free(data);
  149. }
  150. if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
  151. /* Reached end of PEM file */
  152. if (count > 0) {
  153. ERR_clear_error();
  154. return 1;
  155. }
  156. }
  157. /* Some other PEM read error */
  158. print_errors();
  159. return 0;
  160. }
  161. int main(int argc, char *argv[])
  162. {
  163. BIO *bio_err;
  164. const char *p;
  165. int ret = 1;
  166. progname = argv[0];
  167. if (argc < 2) {
  168. test_usage();
  169. EXIT(ret);
  170. }
  171. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  172. p = getenv("OPENSSL_DEBUG_MEMORY");
  173. if (p != NULL && strcmp(p, "on") == 0)
  174. CRYPTO_set_mem_debug(1);
  175. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  176. argc--;
  177. argv++;
  178. while (argc >= 1) {
  179. BIO *f = BIO_new_file(*argv, "r");
  180. int ok;
  181. if (f == NULL) {
  182. fprintf(stderr, "%s: Error opening cert file: '%s': %s\n",
  183. progname, *argv, strerror(errno));
  184. EXIT(ret);
  185. }
  186. ret = !(ok = test_certs(f));
  187. BIO_free(f);
  188. if (!ok) {
  189. printf("%s ERROR\n", *argv);
  190. ret = 1;
  191. break;
  192. }
  193. printf("%s OK\n", *argv);
  194. argc--;
  195. argv++;
  196. }
  197. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  198. if (CRYPTO_mem_leaks(bio_err) <= 0)
  199. ret = 1;
  200. #endif
  201. BIO_free(bio_err);
  202. EXIT(ret);
  203. }