crltest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2015-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 "internal/nelem.h"
  10. #include <string.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/err.h>
  14. #include <openssl/pem.h>
  15. #include <openssl/x509.h>
  16. #include "testutil.h"
  17. #define PARAM_TIME 1474934400 /* Sep 27th, 2016 */
  18. static const char *kCRLTestRoot[] = {
  19. "-----BEGIN CERTIFICATE-----\n",
  20. "MIIDbzCCAlegAwIBAgIJAODri7v0dDUFMA0GCSqGSIb3DQEBCwUAME4xCzAJBgNV\n",
  21. "BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1Nb3VudGFpbiBW\n",
  22. "aWV3MRIwEAYDVQQKDAlCb3JpbmdTU0wwHhcNMTYwOTI2MTUwNjI2WhcNMjYwOTI0\n",
  23. "MTUwNjI2WjBOMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG\n",
  24. "A1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJQm9yaW5nU1NMMIIBIjANBgkq\n",
  25. "hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo16WiLWZuaymsD8n5SKPmxV1y6jjgr3B\n",
  26. "S/dUBpbrzd1aeFzNlI8l2jfAnzUyp+I21RQ+nh/MhqjGElkTtK9xMn1Y+S9GMRh+\n",
  27. "5R/Du0iCb1tCZIPY07Tgrb0KMNWe0v2QKVVruuYSgxIWodBfxlKO64Z8AJ5IbnWp\n",
  28. "uRqO6rctN9qUoMlTIAB6dL4G0tDJ/PGFWOJYwOMEIX54bly2wgyYJVBKiRRt4f7n\n",
  29. "8H922qmvPNA9idmX9G1VAtgV6x97XXi7ULORIQvn9lVQF6nTYDBJhyuPB+mLThbL\n",
  30. "P2o9orxGx7aCtnnBZUIxUvHNOI0FaSaZH7Fi0xsZ/GkG2HZe7ImPJwIDAQABo1Aw\n",
  31. "TjAdBgNVHQ4EFgQUWPt3N5cZ/CRvubbrkqfBnAqhq94wHwYDVR0jBBgwFoAUWPt3\n",
  32. "N5cZ/CRvubbrkqfBnAqhq94wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC\n",
  33. "AQEAORu6M0MOwXy+3VEBwNilfTxyqDfruQsc1jA4PT8Oe8zora1WxE1JB4q2FJOz\n",
  34. "EAuM3H/NXvEnBuN+ITvKZAJUfm4NKX97qmjMJwLKWe1gVv+VQTr63aR7mgWJReQN\n",
  35. "XdMztlVeZs2dppV6uEg3ia1X0G7LARxGpA9ETbMyCpb39XxlYuTClcbA5ftDN99B\n",
  36. "3Xg9KNdd++Ew22O3HWRDvdDpTO/JkzQfzi3sYwUtzMEonENhczJhGf7bQMmvL/w5\n",
  37. "24Wxj4Z7KzzWIHsNqE/RIs6RV3fcW61j/mRgW2XyoWnMVeBzvcJr9NXp4VQYmFPw\n",
  38. "amd8GKMZQvP0ufGnUn7D7uartA==\n",
  39. "-----END CERTIFICATE-----\n",
  40. NULL
  41. };
  42. static const char *kCRLTestLeaf[] = {
  43. "-----BEGIN CERTIFICATE-----\n",
  44. "MIIDkDCCAnigAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwTjELMAkGA1UEBhMCVVMx\n",
  45. "EzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU1vdW50YWluIFZpZXcxEjAQ\n",
  46. "BgNVBAoMCUJvcmluZ1NTTDAeFw0xNjA5MjYxNTA4MzFaFw0xNzA5MjYxNTA4MzFa\n",
  47. "MEsxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRIwEAYDVQQKDAlC\n",
  48. "b3JpbmdTU0wxEzARBgNVBAMMCmJvcmluZy5zc2wwggEiMA0GCSqGSIb3DQEBAQUA\n",
  49. "A4IBDwAwggEKAoIBAQDc5v1S1M0W+QWM+raWfO0LH8uvqEwuJQgODqMaGnSlWUx9\n",
  50. "8iQcnWfjyPja3lWg9K62hSOFDuSyEkysKHDxijz5R93CfLcfnVXjWQDJe7EJTTDP\n",
  51. "ozEvxN6RjAeYv7CF000euYr3QT5iyBjg76+bon1p0jHZBJeNPP1KqGYgyxp+hzpx\n",
  52. "e0gZmTlGAXd8JQK4v8kpdYwD6PPifFL/jpmQpqOtQmH/6zcLjY4ojmqpEdBqIKIX\n",
  53. "+saA29hMq0+NK3K+wgg31RU+cVWxu3tLOIiesETkeDgArjWRS1Vkzbi4v9SJxtNu\n",
  54. "OZuAxWiynRJw3JwH/OFHYZIvQqz68ZBoj96cepjPAgMBAAGjezB5MAkGA1UdEwQC\n",
  55. "MAAwLAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRl\n",
  56. "MB0GA1UdDgQWBBTGn0OVVh/aoYt0bvEKG+PIERqnDzAfBgNVHSMEGDAWgBRY+3c3\n",
  57. "lxn8JG+5tuuSp8GcCqGr3jANBgkqhkiG9w0BAQsFAAOCAQEAd2nM8gCQN2Dc8QJw\n",
  58. "XSZXyuI3DBGGCHcay/3iXu0JvTC3EiQo8J6Djv7WLI0N5KH8mkm40u89fJAB2lLZ\n",
  59. "ShuHVtcC182bOKnePgwp9CNwQ21p0rDEu/P3X46ZvFgdxx82E9xLa0tBB8PiPDWh\n",
  60. "lV16jbaKTgX5AZqjnsyjR5o9/mbZVupZJXx5Syq+XA8qiJfstSYJs4KyKK9UOjql\n",
  61. "ICkJVKpi2ahDBqX4MOH4SLfzVk8pqSpviS6yaA1RXqjpkxiN45WWaXDldVHMSkhC\n",
  62. "5CNXsXi4b1nAntu89crwSLA3rEwzCWeYj+BX7e1T9rr3oJdwOU/2KQtW1js1yQUG\n",
  63. "tjJMFw==\n",
  64. "-----END CERTIFICATE-----\n",
  65. NULL
  66. };
  67. static const char *kBasicCRL[] = {
  68. "-----BEGIN X509 CRL-----\n",
  69. "MIIBpzCBkAIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n",
  70. "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n",
  71. "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoA4wDDAKBgNV\n",
  72. "HRQEAwIBATANBgkqhkiG9w0BAQsFAAOCAQEAnrBKKgvd9x9zwK9rtUvVeFeJ7+LN\n",
  73. "ZEAc+a5oxpPNEsJx6hXoApYEbzXMxuWBQoCs5iEBycSGudct21L+MVf27M38KrWo\n",
  74. "eOkq0a2siqViQZO2Fb/SUFR0k9zb8xl86Zf65lgPplALun0bV/HT7MJcl04Tc4os\n",
  75. "dsAReBs5nqTGNEd5AlC1iKHvQZkM//MD51DspKnDpsDiUVi54h9C1SpfZmX8H2Vv\n",
  76. "diyu0fZ/bPAM3VAGawatf/SyWfBMyKpoPXEG39oAzmjjOj8en82psn7m474IGaho\n",
  77. "/vBbhl1ms5qQiLYPjm4YELtnXQoFyC72tBjbdFd/ZE9k4CNKDbxFUXFbkw==\n",
  78. "-----END X509 CRL-----\n",
  79. NULL
  80. };
  81. static const char *kRevokedCRL[] = {
  82. "-----BEGIN X509 CRL-----\n",
  83. "MIIBvjCBpwIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n",
  84. "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n",
  85. "Qm9yaW5nU1NMFw0xNjA5MjYxNTEyNDRaFw0xNjEwMjYxNTEyNDRaMBUwEwICEAAX\n",
  86. "DTE2MDkyNjE1MTIyNlqgDjAMMAoGA1UdFAQDAgECMA0GCSqGSIb3DQEBCwUAA4IB\n",
  87. "AQCUGaM4DcWzlQKrcZvI8TMeR8BpsvQeo5BoI/XZu2a8h//PyRyMwYeaOM+3zl0d\n",
  88. "sjgCT8b3C1FPgT+P2Lkowv7rJ+FHJRNQkogr+RuqCSPTq65ha4WKlRGWkMFybzVH\n",
  89. "NloxC+aU3lgp/NlX9yUtfqYmJek1CDrOOGPrAEAwj1l/BUeYKNGqfBWYJQtPJu+5\n",
  90. "OaSvIYGpETCZJscUWODmLEb/O3DM438vLvxonwGqXqS0KX37+CHpUlyhnSovxXxp\n",
  91. "Pz4aF+L7OtczxL0GYtD2fR9B7TDMqsNmHXgQrixvvOY7MUdLGbd4RfJL3yA53hyO\n",
  92. "xzfKY2TzxLiOmctG0hXFkH5J\n",
  93. "-----END X509 CRL-----\n",
  94. NULL
  95. };
  96. static const char *kBadIssuerCRL[] = {
  97. "-----BEGIN X509 CRL-----\n",
  98. "MIIBwjCBqwIBATANBgkqhkiG9w0BAQsFADBSMQswCQYDVQQGEwJVUzETMBEGA1UE\n",
  99. "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzEWMBQGA1UECgwN\n",
  100. "Tm90IEJvcmluZ1NTTBcNMTYwOTI2MTUxMjQ0WhcNMTYxMDI2MTUxMjQ0WjAVMBMC\n",
  101. "AhAAFw0xNjA5MjYxNTEyMjZaoA4wDDAKBgNVHRQEAwIBAjANBgkqhkiG9w0BAQsF\n",
  102. "AAOCAQEAlBmjOA3Fs5UCq3GbyPEzHkfAabL0HqOQaCP12btmvIf/z8kcjMGHmjjP\n",
  103. "t85dHbI4Ak/G9wtRT4E/j9i5KML+6yfhRyUTUJKIK/kbqgkj06uuYWuFipURlpDB\n",
  104. "cm81RzZaMQvmlN5YKfzZV/clLX6mJiXpNQg6zjhj6wBAMI9ZfwVHmCjRqnwVmCUL\n",
  105. "TybvuTmkryGBqREwmSbHFFjg5ixG/ztwzON/Ly78aJ8Bql6ktCl9+/gh6VJcoZ0q\n",
  106. "L8V8aT8+Ghfi+zrXM8S9BmLQ9n0fQe0wzKrDZh14EK4sb7zmOzFHSxm3eEXyS98g\n",
  107. "Od4cjsc3ymNk88S4jpnLRtIVxZB+SQ==\n",
  108. "-----END X509 CRL-----\n",
  109. NULL
  110. };
  111. /*
  112. * This is kBasicCRL but with a critical issuing distribution point
  113. * extension.
  114. */
  115. static const char *kKnownCriticalCRL[] = {
  116. "-----BEGIN X509 CRL-----\n",
  117. "MIIBujCBowIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n",
  118. "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n",
  119. "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoCEwHzAKBgNV\n",
  120. "HRQEAwIBATARBgNVHRwBAf8EBzAFoQMBAf8wDQYJKoZIhvcNAQELBQADggEBAA+3\n",
  121. "i+5e5Ub8sccfgOBs6WVJFI9c8gvJjrJ8/dYfFIAuCyeocs7DFXn1n13CRZ+URR/Q\n",
  122. "mVWgU28+xeusuSPYFpd9cyYTcVyNUGNTI3lwgcE/yVjPaOmzSZKdPakApRxtpKKQ\n",
  123. "NN/56aQz3bnT/ZSHQNciRB8U6jiD9V30t0w+FDTpGaG+7bzzUH3UVF9xf9Ctp60A\n",
  124. "3mfLe0scas7owSt4AEFuj2SPvcE7yvdOXbu+IEv21cEJUVExJAbhvIweHXh6yRW+\n",
  125. "7VVeiNzdIjkZjyTmAzoXGha4+wbxXyBRbfH+XWcO/H+8nwyG8Gktdu2QB9S9nnIp\n",
  126. "o/1TpfOMSGhMyMoyPrk=\n",
  127. "-----END X509 CRL-----\n",
  128. NULL
  129. };
  130. /*
  131. * kUnknownCriticalCRL is kBasicCRL but with an unknown critical extension.
  132. */
  133. static const char *kUnknownCriticalCRL[] = {
  134. "-----BEGIN X509 CRL-----\n",
  135. "MIIBvDCBpQIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n",
  136. "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n",
  137. "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoCMwITAKBgNV\n",
  138. "HRQEAwIBATATBgwqhkiG9xIEAYS3CQABAf8EADANBgkqhkiG9w0BAQsFAAOCAQEA\n",
  139. "GvBP0xqL509InMj/3493YVRV+ldTpBv5uTD6jewzf5XdaxEQ/VjTNe5zKnxbpAib\n",
  140. "Kf7cwX0PMSkZjx7k7kKdDlEucwVvDoqC+O9aJcqVmM6GDyNb9xENxd0XCXja6MZC\n",
  141. "yVgP4AwLauB2vSiEprYJyI1APph3iAEeDm60lTXX/wBM/tupQDDujKh2GPyvBRfJ\n",
  142. "+wEDwGg3ICwvu4gO4zeC5qnFR+bpL9t5tOMAQnVZ0NWv+k7mkd2LbHdD44dxrfXC\n",
  143. "nhtfERx99SDmC/jtUAJrGhtCO8acr7exCeYcduN7KKCm91OeCJKK6OzWst0Og1DB\n",
  144. "kwzzU2rL3G65CrZ7H0SZsQ==\n",
  145. "-----END X509 CRL-----\n",
  146. NULL
  147. };
  148. /*
  149. * kUnknownCriticalCRL2 is kBasicCRL but with a critical issuing distribution
  150. * point extension followed by an unknown critical extension
  151. */
  152. static const char *kUnknownCriticalCRL2[] = {
  153. "-----BEGIN X509 CRL-----\n",
  154. "MIIBzzCBuAIBATANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzETMBEGA1UE\n",
  155. "CAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzESMBAGA1UECgwJ\n",
  156. "Qm9yaW5nU1NMFw0xNjA5MjYxNTEwNTVaFw0xNjEwMjYxNTEwNTVaoDYwNDAKBgNV\n",
  157. "HRQEAwIBATARBgNVHRwBAf8EBzAFoQMBAf8wEwYMKoZIhvcSBAGEtwkAAQH/BAAw\n",
  158. "DQYJKoZIhvcNAQELBQADggEBACTcpQC8jXL12JN5YzOcQ64ubQIe0XxRAd30p7qB\n",
  159. "BTXGpgqBjrjxRfLms7EBYodEXB2oXMsDq3km0vT1MfYdsDD05S+SQ9CDsq/pUfaC\n",
  160. "E2WNI5p8WircRnroYvbN2vkjlRbMd1+yNITohXYXCJwjEOAWOx3XIM10bwPYBv4R\n",
  161. "rDobuLHoMgL3yHgMHmAkP7YpkBucNqeBV8cCdeAZLuhXFWi6yfr3r/X18yWbC/r2\n",
  162. "2xXdkrSqXLFo7ToyP8YKTgiXpya4x6m53biEYwa2ULlas0igL6DK7wjYZX95Uy7H\n",
  163. "GKljn9weIYiMPV/BzGymwfv2EW0preLwtyJNJPaxbdin6Jc=\n",
  164. "-----END X509 CRL-----\n",
  165. NULL
  166. };
  167. static const char **unknown_critical_crls[] = {
  168. kUnknownCriticalCRL, kUnknownCriticalCRL2
  169. };
  170. static X509 *test_root = NULL;
  171. static X509 *test_leaf = NULL;
  172. /*
  173. * Glue an array of strings together. Return a BIO and put the string
  174. * into |*out| so we can free it.
  175. */
  176. static BIO *glue2bio(const char **pem, char **out)
  177. {
  178. size_t s = 0;
  179. *out = glue_strings(pem, &s);
  180. return BIO_new_mem_buf(*out, s);
  181. }
  182. /*
  183. * Create a CRL from an array of strings.
  184. */
  185. static X509_CRL *CRL_from_strings(const char **pem)
  186. {
  187. char *p;
  188. BIO *b = glue2bio(pem, &p);
  189. X509_CRL *crl = PEM_read_bio_X509_CRL(b, NULL, NULL, NULL);
  190. OPENSSL_free(p);
  191. BIO_free(b);
  192. return crl;
  193. }
  194. /*
  195. * Create an X509 from an array of strings.
  196. */
  197. static X509 *X509_from_strings(const char **pem)
  198. {
  199. char *p;
  200. BIO *b = glue2bio(pem, &p);
  201. X509 *x = PEM_read_bio_X509(b, NULL, NULL, NULL);
  202. OPENSSL_free(p);
  203. BIO_free(b);
  204. return x;
  205. }
  206. /*
  207. * Verify |leaf| certificate (chained up to |root|). |crls| if
  208. * not NULL, is a list of CRLs to include in the verification. It is
  209. * also free'd before returning, which is kinda yucky but convenient.
  210. * Returns a value from X509_V_ERR_xxx or X509_V_OK.
  211. */
  212. static int verify(X509 *leaf, X509 *root, STACK_OF(X509_CRL) *crls,
  213. unsigned long flags)
  214. {
  215. X509_STORE_CTX *ctx = X509_STORE_CTX_new();
  216. X509_STORE *store = X509_STORE_new();
  217. X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
  218. STACK_OF(X509) *roots = sk_X509_new_null();
  219. int status = X509_V_ERR_UNSPECIFIED;
  220. if (!TEST_ptr(ctx)
  221. || !TEST_ptr(store)
  222. || !TEST_ptr(param)
  223. || !TEST_ptr(roots))
  224. goto err;
  225. /* Create a stack; upref the cert because we free it below. */
  226. X509_up_ref(root);
  227. if (!TEST_true(sk_X509_push(roots, root))
  228. || !TEST_true(X509_STORE_CTX_init(ctx, store, leaf, NULL)))
  229. goto err;
  230. X509_STORE_CTX_set0_trusted_stack(ctx, roots);
  231. X509_STORE_CTX_set0_crls(ctx, crls);
  232. X509_VERIFY_PARAM_set_time(param, PARAM_TIME);
  233. if (!TEST_long_eq((long)X509_VERIFY_PARAM_get_time(param), PARAM_TIME))
  234. goto err;
  235. X509_VERIFY_PARAM_set_depth(param, 16);
  236. if (flags)
  237. X509_VERIFY_PARAM_set_flags(param, flags);
  238. X509_STORE_CTX_set0_param(ctx, param);
  239. param = NULL;
  240. ERR_clear_error();
  241. status = X509_verify_cert(ctx) == 1 ? X509_V_OK
  242. : X509_STORE_CTX_get_error(ctx);
  243. err:
  244. sk_X509_pop_free(roots, X509_free);
  245. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  246. X509_VERIFY_PARAM_free(param);
  247. X509_STORE_CTX_free(ctx);
  248. X509_STORE_free(store);
  249. return status;
  250. }
  251. /*
  252. * Create a stack of CRL's. Upref each one because we call pop_free on
  253. * the stack and need to keep the CRL's around until the test exits.
  254. * Yes this crashes on malloc failure; it forces us to debug.
  255. */
  256. static STACK_OF(X509_CRL) *make_CRL_stack(X509_CRL *x1, X509_CRL *x2)
  257. {
  258. STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null();
  259. sk_X509_CRL_push(sk, x1);
  260. X509_CRL_up_ref(x1);
  261. if (x2 != NULL) {
  262. sk_X509_CRL_push(sk, x2);
  263. X509_CRL_up_ref(x2);
  264. }
  265. return sk;
  266. }
  267. static int test_basic_crl(void)
  268. {
  269. X509_CRL *basic_crl = CRL_from_strings(kBasicCRL);
  270. X509_CRL *revoked_crl = CRL_from_strings(kRevokedCRL);
  271. int r;
  272. r = TEST_ptr(basic_crl)
  273. && TEST_ptr(revoked_crl)
  274. && TEST_int_eq(verify(test_leaf, test_root,
  275. make_CRL_stack(basic_crl, NULL),
  276. X509_V_FLAG_CRL_CHECK), X509_V_OK)
  277. && TEST_int_eq(verify(test_leaf, test_root,
  278. make_CRL_stack(basic_crl, revoked_crl),
  279. X509_V_FLAG_CRL_CHECK), X509_V_ERR_CERT_REVOKED);
  280. X509_CRL_free(basic_crl);
  281. X509_CRL_free(revoked_crl);
  282. return r;
  283. }
  284. static int test_no_crl(void)
  285. {
  286. return TEST_int_eq(verify(test_leaf, test_root, NULL,
  287. X509_V_FLAG_CRL_CHECK),
  288. X509_V_ERR_UNABLE_TO_GET_CRL);
  289. }
  290. static int test_bad_issuer_crl(void)
  291. {
  292. X509_CRL *bad_issuer_crl = CRL_from_strings(kBadIssuerCRL);
  293. int r;
  294. r = TEST_ptr(bad_issuer_crl)
  295. && TEST_int_eq(verify(test_leaf, test_root,
  296. make_CRL_stack(bad_issuer_crl, NULL),
  297. X509_V_FLAG_CRL_CHECK),
  298. X509_V_ERR_UNABLE_TO_GET_CRL);
  299. X509_CRL_free(bad_issuer_crl);
  300. return r;
  301. }
  302. static int test_known_critical_crl(void)
  303. {
  304. X509_CRL *known_critical_crl = CRL_from_strings(kKnownCriticalCRL);
  305. int r;
  306. r = TEST_ptr(known_critical_crl)
  307. && TEST_int_eq(verify(test_leaf, test_root,
  308. make_CRL_stack(known_critical_crl, NULL),
  309. X509_V_FLAG_CRL_CHECK), X509_V_OK);
  310. X509_CRL_free(known_critical_crl);
  311. return r;
  312. }
  313. static int test_unknown_critical_crl(int n)
  314. {
  315. X509_CRL *unknown_critical_crl = CRL_from_strings(unknown_critical_crls[n]);
  316. int r;
  317. r = TEST_ptr(unknown_critical_crl)
  318. && TEST_int_eq(verify(test_leaf, test_root,
  319. make_CRL_stack(unknown_critical_crl, NULL),
  320. X509_V_FLAG_CRL_CHECK),
  321. X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION);
  322. X509_CRL_free(unknown_critical_crl);
  323. return r;
  324. }
  325. static int test_reuse_crl(void)
  326. {
  327. X509_CRL *reused_crl = CRL_from_strings(kBasicCRL);
  328. char *p;
  329. BIO *b = glue2bio(kRevokedCRL, &p);
  330. reused_crl = PEM_read_bio_X509_CRL(b, &reused_crl, NULL, NULL);
  331. OPENSSL_free(p);
  332. BIO_free(b);
  333. X509_CRL_free(reused_crl);
  334. return 1;
  335. }
  336. int setup_tests(void)
  337. {
  338. if (!TEST_ptr(test_root = X509_from_strings(kCRLTestRoot))
  339. || !TEST_ptr(test_leaf = X509_from_strings(kCRLTestLeaf)))
  340. return 0;
  341. ADD_TEST(test_no_crl);
  342. ADD_TEST(test_basic_crl);
  343. ADD_TEST(test_bad_issuer_crl);
  344. ADD_TEST(test_known_critical_crl);
  345. ADD_ALL_TESTS(test_unknown_critical_crl, OSSL_NELEM(unknown_critical_crls));
  346. ADD_TEST(test_reuse_crl);
  347. return 1;
  348. }
  349. void cleanup_tests(void)
  350. {
  351. X509_free(test_root);
  352. X509_free(test_leaf);
  353. }