2
0

x509_check_cert_pkey_test.c 5.0 KB

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