x509aux.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2016-2018 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 "internal/nelem.h"
  18. #include "testutil.h"
  19. static int test_certs(int num)
  20. {
  21. int c;
  22. char *name = 0;
  23. char *header = 0;
  24. unsigned char *data = 0;
  25. long len;
  26. typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
  27. typedef int (*i2d_X509_t)(X509 *, unsigned char **);
  28. int err = 0;
  29. BIO *fp = BIO_new_file(test_get_argument(num), "r");
  30. if (!TEST_ptr(fp))
  31. return 0;
  32. for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) {
  33. const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0);
  34. d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509;
  35. i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509;
  36. X509 *cert = 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. OPENSSL_free(buf);
  86. buf = NULL;
  87. /* Test 1-pass encoding into library allocated buffer */
  88. enclen = i2d(cert, &buf);
  89. if (len != enclen) {
  90. TEST_error("encoded length %ld of %s != input length %ld",
  91. enclen, name, len);
  92. err = 1;
  93. goto next;
  94. }
  95. if (memcmp(buf, data, len) != 0) {
  96. TEST_error("encoded content of %s does not match input", name);
  97. err = 1;
  98. goto next;
  99. }
  100. if (trusted) {
  101. /* Encode just the cert and compare with initial encoding */
  102. OPENSSL_free(buf);
  103. buf = NULL;
  104. /* Test 1-pass encoding into library allocated buffer */
  105. enclen = i2d(cert, &buf);
  106. if (enclen > len) {
  107. TEST_error("encoded length %ld of %s > input length %ld",
  108. enclen, name, len);
  109. err = 1;
  110. goto next;
  111. }
  112. if (memcmp(buf, data, enclen) != 0) {
  113. TEST_error("encoded cert content does not match input");
  114. err = 1;
  115. goto next;
  116. }
  117. }
  118. /*
  119. * If any of these were null, PEM_read() would have failed.
  120. */
  121. next:
  122. X509_free(cert);
  123. OPENSSL_free(buf);
  124. OPENSSL_free(name);
  125. OPENSSL_free(header);
  126. OPENSSL_free(data);
  127. }
  128. BIO_free(fp);
  129. if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
  130. /* Reached end of PEM file */
  131. if (c > 0) {
  132. ERR_clear_error();
  133. return 1;
  134. }
  135. }
  136. /* Some other PEM read error */
  137. return 0;
  138. }
  139. int setup_tests(void)
  140. {
  141. size_t n = test_get_argument_count();
  142. if (n == 0) {
  143. TEST_error("usage: %s certfile...", test_get_program_name());
  144. return 0;
  145. }
  146. ADD_ALL_TESTS(test_certs, (int)n);
  147. return 1;
  148. }