2
0

x509_check_cert_pkey_test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2017-2018 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 <stdio.h>
  10. #include <string.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509.h>
  13. #include "testutil.h"
  14. /*
  15. * c: path of a cert in PEM format
  16. * k: path of a key in PEM format
  17. * t: API type, "cert" for X509_ and "req" for X509_REQ_ APIs.
  18. * e: expected, "ok" for success, "failed" for what should fail.
  19. */
  20. static const char *c;
  21. static const char *k;
  22. static const char *t;
  23. static const char *e;
  24. static int test_x509_check_cert_pkey(void)
  25. {
  26. BIO *bio = NULL;
  27. X509 *x509 = NULL;
  28. X509_REQ *x509_req = NULL;
  29. EVP_PKEY *pkey = NULL;
  30. int ret = 0, type = 0, expected = 0, result = 0;
  31. /*
  32. * we check them first thus if fails we don't need to do
  33. * those PEM parsing operations.
  34. */
  35. if (strcmp(t, "cert") == 0) {
  36. type = 1;
  37. } else if (strcmp(t, "req") == 0) {
  38. type = 2;
  39. } else {
  40. TEST_error("invalid 'type'");
  41. goto failed;
  42. }
  43. if (strcmp(e, "ok") == 0) {
  44. expected = 1;
  45. } else if (strcmp(e, "failed") == 0) {
  46. expected = 0;
  47. } else {
  48. TEST_error("invalid 'expected'");
  49. goto failed;
  50. }
  51. /* process private key */
  52. if (!TEST_ptr(bio = BIO_new_file(k, "r")))
  53. goto failed;
  54. if (!TEST_ptr(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)))
  55. goto failed;
  56. BIO_free(bio);
  57. /* process cert or cert request, use the same local var */
  58. if (!TEST_ptr(bio = BIO_new_file(c, "r")))
  59. goto failed;
  60. switch (type) {
  61. case 1:
  62. x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
  63. if (x509 == NULL) {
  64. TEST_error("read PEM x509 failed");
  65. goto failed;
  66. }
  67. result = X509_check_private_key(x509, pkey);
  68. break;
  69. case 2:
  70. x509_req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL);
  71. if (x509_req == NULL) {
  72. TEST_error("read PEM x509 req failed");
  73. goto failed;
  74. }
  75. result = X509_REQ_check_private_key(x509_req, pkey);
  76. break;
  77. default:
  78. /* should never be here */
  79. break;
  80. }
  81. if (!TEST_int_eq(result, expected)) {
  82. TEST_error("check private key: expected: %d, got: %d", expected, result);
  83. goto failed;
  84. }
  85. ret = 1;
  86. failed:
  87. BIO_free(bio);
  88. X509_free(x509);
  89. X509_REQ_free(x509_req);
  90. EVP_PKEY_free(pkey);
  91. return ret;
  92. }
  93. const OPTIONS *test_get_options(void)
  94. {
  95. enum { OPT_TEST_ENUM };
  96. static const OPTIONS test_options[] = {
  97. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("cert key type expected\n"),
  98. { OPT_HELP_STR, 1, '-', "cert\tcertificate or CSR filename in PEM\n" },
  99. { OPT_HELP_STR, 1, '-', "key\tprivate key filename in PEM\n" },
  100. { OPT_HELP_STR, 1, '-', "type\t\tvalue must be 'cert' or 'req'\n" },
  101. { OPT_HELP_STR, 1, '-', "expected\tthe expected return value, either 'ok' or 'failed'\n" },
  102. { NULL }
  103. };
  104. return test_options;
  105. }
  106. int setup_tests(void)
  107. {
  108. if (!TEST_ptr(c = test_get_argument(0))
  109. || !TEST_ptr(k = test_get_argument(1))
  110. || !TEST_ptr(t = test_get_argument(2))
  111. || !TEST_ptr(e = test_get_argument(3))) {
  112. return 0;
  113. }
  114. ADD_TEST(test_x509_check_cert_pkey);
  115. return 1;
  116. }