x509_check_cert_pkey_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2017-2021 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. static const char *file; /* path of a cert/CRL/key file in PEM format */
  94. static const char *num; /* expected number of certs/CRLs/keys included */
  95. static int test_PEM_X509_INFO_read_bio(void)
  96. {
  97. BIO *in;
  98. STACK_OF(X509_INFO) *sk;
  99. X509_INFO *it;
  100. int i, count = 0;
  101. int expected = 0;
  102. if (!TEST_ptr((in = BIO_new_file(file, "r"))))
  103. return 0;
  104. sk = PEM_X509_INFO_read_bio(in, NULL, NULL, "");
  105. BIO_free(in);
  106. sscanf(num, "%d", &expected);
  107. for (i = 0; i < sk_X509_INFO_num(sk); i++) {
  108. it = sk_X509_INFO_value(sk, i);
  109. if (it->x509 != NULL)
  110. count++;
  111. if (it->crl != NULL)
  112. count++;
  113. if (it->x_pkey != NULL)
  114. count++;
  115. }
  116. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  117. return TEST_int_eq(count, expected);
  118. }
  119. const OPTIONS *test_get_options(void)
  120. {
  121. enum { OPT_TEST_ENUM };
  122. static const OPTIONS test_options[] = {
  123. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("cert key type expected\n"
  124. " or [options] file num\n"),
  125. { OPT_HELP_STR, 1, '-', "cert\tcertificate or CSR filename in PEM\n" },
  126. { OPT_HELP_STR, 1, '-', "key\tprivate key filename in PEM\n" },
  127. { OPT_HELP_STR, 1, '-', "type\t\tvalue must be 'cert' or 'req'\n" },
  128. { OPT_HELP_STR, 1, '-', "expected\tthe expected return value, either 'ok' or 'failed'\n" },
  129. { OPT_HELP_STR, 1, '-', "file\tPEM format file containing certs, keys, and/OR CRLs\n" },
  130. { OPT_HELP_STR, 1, '-', "num\texpected number of credentials to be loaded from file\n" },
  131. { NULL }
  132. };
  133. return test_options;
  134. }
  135. int setup_tests(void)
  136. {
  137. if (!test_skip_common_options()) {
  138. TEST_error("Error parsing test options\n");
  139. return 0;
  140. }
  141. if (test_get_argument_count() == 2) {
  142. if (!TEST_ptr(file = test_get_argument(0))
  143. || !TEST_ptr(num = test_get_argument(1)))
  144. return 0;
  145. ADD_TEST(test_PEM_X509_INFO_read_bio);
  146. return 1;
  147. }
  148. if (!TEST_ptr(c = test_get_argument(0))
  149. || !TEST_ptr(k = test_get_argument(1))
  150. || !TEST_ptr(t = test_get_argument(2))
  151. || !TEST_ptr(e = test_get_argument(3))) {
  152. return 0;
  153. }
  154. ADD_TEST(test_x509_check_cert_pkey);
  155. return 1;
  156. }