x509_dup_cert_test.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. /* ====================================================================
  10. * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
  11. */
  12. #include <stdio.h>
  13. #include <openssl/err.h>
  14. #include <openssl/x509_vfy.h>
  15. static int test_509_dup_cert(const char *cert_f)
  16. {
  17. int ret = 0;
  18. X509_STORE_CTX *sctx = NULL;
  19. X509_STORE *store = NULL;
  20. X509_LOOKUP *lookup = NULL;
  21. store = X509_STORE_new();
  22. if (store == NULL)
  23. goto err;
  24. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  25. if (lookup == NULL)
  26. goto err;
  27. if (!X509_load_cert_file(lookup, cert_f, X509_FILETYPE_PEM))
  28. goto err;
  29. if (!X509_load_cert_file(lookup, cert_f, X509_FILETYPE_PEM))
  30. goto err;
  31. ret = 1;
  32. err:
  33. X509_STORE_CTX_free(sctx);
  34. X509_STORE_free(store);
  35. if (ret != 1)
  36. ERR_print_errors_fp(stderr);
  37. return ret;
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. CRYPTO_set_mem_debug(1);
  42. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  43. if (argc != 2) {
  44. fprintf(stderr, "usage: x509_dup_cert_test cert.pem\n");
  45. return 1;
  46. }
  47. if (!test_509_dup_cert(argv[1])) {
  48. fprintf(stderr, "Test X509 duplicate cert failed\n");
  49. return 1;
  50. }
  51. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  52. if (CRYPTO_mem_leaks_fp(stderr) <= 0)
  53. return 1;
  54. #endif
  55. printf("PASS\n");
  56. return 0;
  57. }