x509_req_test.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2024 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 <openssl/pem.h>
  10. #include <openssl/x509.h>
  11. #include "testutil.h"
  12. static char *certsDir = NULL;
  13. /*
  14. * Test for the missing X509 version check discussed in issue #5738 and
  15. * added in PR #24677.
  16. * This test tries to verify a malformed CSR with the X509 version set
  17. * version 6, instead of 1. As this request is malformed, even its
  18. * signature is valid, the verification must fail.
  19. */
  20. static int test_x509_req_detect_invalid_version(void)
  21. {
  22. char *certFilePath;
  23. BIO *bio = NULL;
  24. EVP_PKEY *pkey = NULL;
  25. X509_REQ *req = NULL;
  26. int ret = 0;
  27. certFilePath = test_mk_file_path(certsDir, "x509-req-detect-invalid-version.pem");
  28. if (certFilePath == NULL)
  29. goto err;
  30. if (!TEST_ptr(bio = BIO_new_file(certFilePath, "r")))
  31. goto err;
  32. req = PEM_read_bio_X509_REQ(bio, NULL, 0, NULL);
  33. if (req == NULL) {
  34. ret = 1; /* success, reading PEM with invalid CSR data is allowed to fail. */
  35. goto err;
  36. }
  37. if (!TEST_ptr(pkey = X509_REQ_get_pubkey(req)))
  38. goto err;
  39. /* Verification MUST fail at this point. ret != 1. */
  40. if (!TEST_int_ne(X509_REQ_verify(req, pkey), 1))
  41. goto err;
  42. ret = 1; /* success */
  43. err:
  44. EVP_PKEY_free(pkey);
  45. X509_REQ_free(req);
  46. BIO_free(bio);
  47. OPENSSL_free(certFilePath);
  48. return ret;
  49. }
  50. OPT_TEST_DECLARE_USAGE("certdir\n")
  51. int setup_tests(void)
  52. {
  53. if (!test_skip_common_options()) {
  54. TEST_error("Error parsing test options\n");
  55. return 0;
  56. }
  57. if (!TEST_ptr(certsDir = test_get_argument(0)))
  58. return 0;
  59. ADD_TEST(test_x509_req_detect_invalid_version);
  60. return 1;
  61. }
  62. void cleanup_tests(void)
  63. {
  64. }