ocspapitest.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <string.h>
  10. #include <openssl/opensslconf.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/ocsp.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/pem.h>
  16. #include "testutil.h"
  17. DEFINE_STACK_OF(X509)
  18. static const char *certstr;
  19. static const char *privkeystr;
  20. #ifndef OPENSSL_NO_OCSP
  21. static int get_cert_and_key(X509 **cert_out, EVP_PKEY **key_out)
  22. {
  23. BIO *certbio, *keybio;
  24. X509 *cert = NULL;
  25. EVP_PKEY *key = NULL;
  26. if (!TEST_ptr(certbio = BIO_new_file(certstr, "r")))
  27. return 0;
  28. cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
  29. BIO_free(certbio);
  30. if (!TEST_ptr(keybio = BIO_new_file(privkeystr, "r")))
  31. goto end;
  32. key = PEM_read_bio_PrivateKey(keybio, NULL, NULL, NULL);
  33. BIO_free(keybio);
  34. if (!TEST_ptr(cert) || !TEST_ptr(key))
  35. goto end;
  36. *cert_out = cert;
  37. *key_out = key;
  38. return 1;
  39. end:
  40. X509_free(cert);
  41. EVP_PKEY_free(key);
  42. return 0;
  43. }
  44. static int get_cert(X509 **cert_out)
  45. {
  46. BIO *certbio;
  47. X509 *cert = NULL;
  48. if (!TEST_ptr(certbio = BIO_new_file(certstr, "r")))
  49. return 0;
  50. cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
  51. BIO_free(certbio);
  52. if (!TEST_ptr(cert))
  53. goto end;
  54. *cert_out = cert;
  55. return 1;
  56. end:
  57. X509_free(cert);
  58. return 0;
  59. }
  60. static OCSP_BASICRESP *make_dummy_resp(void)
  61. {
  62. const unsigned char namestr[] = "openssl.example.com";
  63. unsigned char keybytes[128] = {7};
  64. OCSP_BASICRESP *bs = OCSP_BASICRESP_new();
  65. OCSP_BASICRESP *bs_out = NULL;
  66. OCSP_CERTID *cid = NULL;
  67. ASN1_TIME *thisupd = ASN1_TIME_set(NULL, time(NULL));
  68. ASN1_TIME *nextupd = ASN1_TIME_set(NULL, time(NULL) + 200);
  69. X509_NAME *name = X509_NAME_new();
  70. ASN1_BIT_STRING *key = ASN1_BIT_STRING_new();
  71. ASN1_INTEGER *serial = ASN1_INTEGER_new();
  72. if (!X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC,
  73. namestr, -1, -1, 1)
  74. || !ASN1_BIT_STRING_set(key, keybytes, sizeof(keybytes))
  75. || !ASN1_INTEGER_set_uint64(serial, (uint64_t)1))
  76. goto err;
  77. cid = OCSP_cert_id_new(EVP_sha256(), name, key, serial);
  78. if (!TEST_ptr(bs)
  79. || !TEST_ptr(thisupd)
  80. || !TEST_ptr(nextupd)
  81. || !TEST_ptr(cid)
  82. || !TEST_true(OCSP_basic_add1_status(bs, cid,
  83. V_OCSP_CERTSTATUS_UNKNOWN,
  84. 0, NULL, thisupd, nextupd)))
  85. goto err;
  86. bs_out = bs;
  87. bs = NULL;
  88. err:
  89. ASN1_TIME_free(thisupd);
  90. ASN1_TIME_free(nextupd);
  91. ASN1_BIT_STRING_free(key);
  92. ASN1_INTEGER_free(serial);
  93. OCSP_CERTID_free(cid);
  94. OCSP_BASICRESP_free(bs);
  95. X509_NAME_free(name);
  96. return bs_out;
  97. }
  98. static int test_resp_signer(void)
  99. {
  100. OCSP_BASICRESP *bs = NULL;
  101. X509 *signer = NULL, *tmp;
  102. EVP_PKEY *key = NULL;
  103. STACK_OF(X509) *extra_certs = NULL;
  104. int ret = 0;
  105. /*
  106. * Test a response with no certs at all; get the signer from the
  107. * extra certs given to OCSP_resp_get0_signer().
  108. */
  109. bs = make_dummy_resp();
  110. extra_certs = sk_X509_new_null();
  111. if (!TEST_ptr(bs)
  112. || !TEST_ptr(extra_certs)
  113. || !TEST_true(get_cert_and_key(&signer, &key))
  114. || !TEST_true(sk_X509_push(extra_certs, signer))
  115. || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(),
  116. NULL, OCSP_NOCERTS)))
  117. goto err;
  118. if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, extra_certs))
  119. || !TEST_int_eq(X509_cmp(tmp, signer), 0))
  120. goto err;
  121. OCSP_BASICRESP_free(bs);
  122. /* Do it again but include the signer cert */
  123. bs = make_dummy_resp();
  124. tmp = NULL;
  125. if (!TEST_ptr(bs)
  126. || !TEST_true(OCSP_basic_sign(bs, signer, key, EVP_sha1(),
  127. NULL, 0)))
  128. goto err;
  129. if (!TEST_true(OCSP_resp_get0_signer(bs, &tmp, NULL))
  130. || !TEST_int_eq(X509_cmp(tmp, signer), 0))
  131. goto err;
  132. ret = 1;
  133. err:
  134. OCSP_BASICRESP_free(bs);
  135. sk_X509_free(extra_certs);
  136. X509_free(signer);
  137. EVP_PKEY_free(key);
  138. return ret;
  139. }
  140. static int test_access_description(int testcase)
  141. {
  142. ACCESS_DESCRIPTION *ad = ACCESS_DESCRIPTION_new();
  143. int ret = 0;
  144. if (!TEST_ptr(ad))
  145. goto err;
  146. switch (testcase) {
  147. case 0: /* no change */
  148. break;
  149. case 1: /* check and release current location */
  150. if (!TEST_ptr(ad->location))
  151. goto err;
  152. GENERAL_NAME_free(ad->location);
  153. ad->location = NULL;
  154. break;
  155. case 2: /* replace current location */
  156. GENERAL_NAME_free(ad->location);
  157. ad->location = GENERAL_NAME_new();
  158. if (!TEST_ptr(ad->location))
  159. goto err;
  160. break;
  161. }
  162. ACCESS_DESCRIPTION_free(ad);
  163. ret = 1;
  164. err:
  165. return ret;
  166. }
  167. static int test_ocsp_url_svcloc_new(void)
  168. {
  169. static const char *urls[] = {
  170. "www.openssl.org",
  171. "www.openssl.net",
  172. NULL
  173. };
  174. X509 *issuer = NULL;
  175. X509_EXTENSION * ext = NULL;
  176. int ret = 0;
  177. if (!TEST_true(get_cert(&issuer)))
  178. goto err;
  179. /*
  180. * Test calling this ocsp method to catch any memory leak
  181. */
  182. ext = OCSP_url_svcloc_new(X509_get_issuer_name(issuer), urls);
  183. if (!TEST_ptr(ext))
  184. goto err;
  185. X509_EXTENSION_free(ext);
  186. ret = 1;
  187. err:
  188. X509_free(issuer);
  189. return ret;
  190. }
  191. #endif /* OPENSSL_NO_OCSP */
  192. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  193. int setup_tests(void)
  194. {
  195. if (!test_skip_common_options()) {
  196. TEST_error("Error parsing test options\n");
  197. return 0;
  198. }
  199. if (!TEST_ptr(certstr = test_get_argument(0))
  200. || !TEST_ptr(privkeystr = test_get_argument(1)))
  201. return 0;
  202. #ifndef OPENSSL_NO_OCSP
  203. ADD_TEST(test_resp_signer);
  204. ADD_ALL_TESTS(test_access_description, 3);
  205. ADD_TEST(test_ocsp_url_svcloc_new);
  206. #endif
  207. return 1;
  208. }