x509aux.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "testutil.h"
  18. static int test_certs(int num)
  19. {
  20. int c;
  21. char *name = 0;
  22. char *header = 0;
  23. unsigned char *data = 0;
  24. long len;
  25. typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
  26. typedef int (*i2d_X509_t)(const X509 *, unsigned char **);
  27. int err = 0;
  28. BIO *fp = BIO_new_file(test_get_argument(num), "r");
  29. if (!TEST_ptr(fp))
  30. return 0;
  31. for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) {
  32. const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0);
  33. d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509;
  34. i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509;
  35. X509 *cert = NULL;
  36. X509 *reuse = NULL;
  37. const unsigned char *p = data;
  38. unsigned char *buf = NULL;
  39. unsigned char *bufp;
  40. long enclen;
  41. if (!trusted
  42. && strcmp(name, PEM_STRING_X509) != 0
  43. && strcmp(name, PEM_STRING_X509_OLD) != 0) {
  44. TEST_error("unexpected PEM object: %s", name);
  45. err = 1;
  46. goto next;
  47. }
  48. cert = d2i(NULL, &p, len);
  49. if (cert == NULL || (p - data) != len) {
  50. TEST_error("error parsing input %s", name);
  51. err = 1;
  52. goto next;
  53. }
  54. /* Test traditional 2-pass encoding into caller allocated buffer */
  55. enclen = i2d(cert, NULL);
  56. if (len != enclen) {
  57. TEST_error("encoded length %ld of %s != input length %ld",
  58. enclen, name, len);
  59. err = 1;
  60. goto next;
  61. }
  62. if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
  63. TEST_perror("malloc");
  64. err = 1;
  65. goto next;
  66. }
  67. enclen = i2d(cert, &bufp);
  68. if (len != enclen) {
  69. TEST_error("encoded length %ld of %s != input length %ld",
  70. enclen, name, len);
  71. err = 1;
  72. goto next;
  73. }
  74. enclen = (long) (bufp - buf);
  75. if (enclen != len) {
  76. TEST_error("unexpected buffer position after encoding %s", name);
  77. err = 1;
  78. goto next;
  79. }
  80. if (memcmp(buf, data, len) != 0) {
  81. TEST_error("encoded content of %s does not match input", name);
  82. err = 1;
  83. goto next;
  84. }
  85. p = buf;
  86. reuse = d2i(NULL, &p, enclen);
  87. if (reuse == NULL) {
  88. TEST_error("second d2i call failed for %s", name);
  89. err = 1;
  90. goto next;
  91. }
  92. err = X509_cmp(reuse, cert);
  93. if (err != 0) {
  94. TEST_error("X509_cmp for %s resulted in %d", name, err);
  95. err = 1;
  96. goto next;
  97. }
  98. OPENSSL_free(buf);
  99. buf = NULL;
  100. /* Test 1-pass encoding into library allocated buffer */
  101. enclen = i2d(cert, &buf);
  102. if (len != enclen) {
  103. TEST_error("encoded length %ld of %s != input length %ld",
  104. enclen, name, len);
  105. err = 1;
  106. goto next;
  107. }
  108. if (memcmp(buf, data, len) != 0) {
  109. TEST_error("encoded content of %s does not match input", name);
  110. err = 1;
  111. goto next;
  112. }
  113. if (trusted) {
  114. /* Encode just the cert and compare with initial encoding */
  115. OPENSSL_free(buf);
  116. buf = NULL;
  117. /* Test 1-pass encoding into library allocated buffer */
  118. enclen = i2d(cert, &buf);
  119. if (enclen > len) {
  120. TEST_error("encoded length %ld of %s > input length %ld",
  121. enclen, name, len);
  122. err = 1;
  123. goto next;
  124. }
  125. if (memcmp(buf, data, enclen) != 0) {
  126. TEST_error("encoded cert content does not match input");
  127. err = 1;
  128. goto next;
  129. }
  130. }
  131. /*
  132. * If any of these were null, PEM_read() would have failed.
  133. */
  134. next:
  135. X509_free(cert);
  136. X509_free(reuse);
  137. OPENSSL_free(buf);
  138. OPENSSL_free(name);
  139. OPENSSL_free(header);
  140. OPENSSL_free(data);
  141. }
  142. BIO_free(fp);
  143. if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
  144. /* Reached end of PEM file */
  145. if (c > 0) {
  146. ERR_clear_error();
  147. return 1;
  148. }
  149. }
  150. /* Some other PEM read error */
  151. return 0;
  152. }
  153. OPT_TEST_DECLARE_USAGE("certfile...\n")
  154. int setup_tests(void)
  155. {
  156. size_t n;
  157. if (!test_skip_common_options()) {
  158. TEST_error("Error parsing test options\n");
  159. return 0;
  160. }
  161. n = test_get_argument_count();
  162. if (n == 0)
  163. return 0;
  164. ADD_ALL_TESTS(test_certs, (int)n);
  165. return 1;
  166. }