pkcs12_api_test.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright 2022-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 <stdlib.h>
  12. #include "internal/nelem.h"
  13. #include <openssl/pkcs12.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/pem.h>
  17. #include "testutil.h"
  18. #include "helpers/pkcs12.h"
  19. static OSSL_LIB_CTX *testctx = NULL;
  20. static OSSL_PROVIDER *nullprov = NULL;
  21. static int test_null_args(void)
  22. {
  23. return TEST_false(PKCS12_parse(NULL, NULL, NULL, NULL, NULL));
  24. }
  25. static PKCS12 *PKCS12_load(const char *fpath)
  26. {
  27. BIO *bio = NULL;
  28. PKCS12 *p12 = NULL;
  29. bio = BIO_new_file(fpath, "rb");
  30. if (!TEST_ptr(bio))
  31. goto err;
  32. p12 = PKCS12_init_ex(NID_pkcs7_data, testctx, "provider=default");
  33. if (!TEST_ptr(p12))
  34. goto err;
  35. if (!TEST_true(p12 == d2i_PKCS12_bio(bio, &p12)))
  36. goto err;
  37. BIO_free(bio);
  38. return p12;
  39. err:
  40. BIO_free(bio);
  41. PKCS12_free(p12);
  42. return NULL;
  43. }
  44. static const char *in_file = NULL;
  45. static const char *in_pass = "";
  46. static int has_key = 0;
  47. static int has_cert = 0;
  48. static int has_ca = 0;
  49. static int changepass(PKCS12 *p12, EVP_PKEY *key, X509 *cert, STACK_OF(X509) *ca)
  50. {
  51. int ret = 0;
  52. PKCS12 *p12new = NULL;
  53. EVP_PKEY *key2 = NULL;
  54. X509 *cert2 = NULL;
  55. STACK_OF(X509) *ca2 = NULL;
  56. BIO *bio = NULL;
  57. if (!TEST_true(PKCS12_newpass(p12, in_pass, "NEWPASS")))
  58. goto err;
  59. if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
  60. goto err;
  61. if (!TEST_true(i2d_PKCS12_bio(bio, p12)))
  62. goto err;
  63. if (!TEST_ptr(p12new = PKCS12_init_ex(NID_pkcs7_data, testctx, "provider=default")))
  64. goto err;
  65. if (!TEST_ptr(d2i_PKCS12_bio(bio, &p12new)))
  66. goto err;
  67. if (!TEST_true(PKCS12_parse(p12new, "NEWPASS", &key2, &cert2, &ca2)))
  68. goto err;
  69. if (has_key) {
  70. if (!TEST_ptr(key2) || !TEST_int_eq(EVP_PKEY_eq(key, key2), 1))
  71. goto err;
  72. }
  73. if (has_cert) {
  74. if (!TEST_ptr(cert2) || !TEST_int_eq(X509_cmp(cert, cert2), 0))
  75. goto err;
  76. }
  77. ret = 1;
  78. err:
  79. BIO_free(bio);
  80. PKCS12_free(p12new);
  81. EVP_PKEY_free(key2);
  82. X509_free(cert2);
  83. OSSL_STACK_OF_X509_free(ca2);
  84. return ret;
  85. }
  86. static int pkcs12_parse_test(void)
  87. {
  88. int ret = 0;
  89. PKCS12 *p12 = NULL;
  90. EVP_PKEY *key = NULL;
  91. X509 *cert = NULL;
  92. STACK_OF(X509) *ca = NULL;
  93. if (in_file != NULL) {
  94. p12 = PKCS12_load(in_file);
  95. if (!TEST_ptr(p12))
  96. goto err;
  97. if (!TEST_true(PKCS12_parse(p12, in_pass, &key, &cert, &ca)))
  98. goto err;
  99. if ((has_key && !TEST_ptr(key)) || (!has_key && !TEST_ptr_null(key)))
  100. goto err;
  101. if ((has_cert && !TEST_ptr(cert)) || (!has_cert && !TEST_ptr_null(cert)))
  102. goto err;
  103. if ((has_ca && !TEST_ptr(ca)) || (!has_ca && !TEST_ptr_null(ca)))
  104. goto err;
  105. if (has_key && !changepass(p12, key, cert, ca))
  106. goto err;
  107. }
  108. ret = 1;
  109. err:
  110. PKCS12_free(p12);
  111. EVP_PKEY_free(key);
  112. X509_free(cert);
  113. OSSL_STACK_OF_X509_free(ca);
  114. return TEST_true(ret);
  115. }
  116. static int pkcs12_create_cb(PKCS12_SAFEBAG *bag, void *cbarg)
  117. {
  118. int cb_ret = *((int*)cbarg);
  119. return cb_ret;
  120. }
  121. static PKCS12 *pkcs12_create_ex2_setup(EVP_PKEY **key, X509 **cert, STACK_OF(X509) **ca)
  122. {
  123. PKCS12 *p12 = NULL;
  124. p12 = PKCS12_load("out6.p12");
  125. if (!TEST_ptr(p12))
  126. goto err;
  127. if (!TEST_true(PKCS12_parse(p12, "", key, cert, ca)))
  128. goto err;
  129. return p12;
  130. err:
  131. PKCS12_free(p12);
  132. return NULL;
  133. }
  134. static int pkcs12_create_ex2_test(int test)
  135. {
  136. int ret = 0, cb_ret = 0;
  137. PKCS12 *ptr = NULL, *p12 = NULL;
  138. EVP_PKEY *key = NULL;
  139. X509 *cert = NULL;
  140. STACK_OF(X509) *ca = NULL;
  141. p12 = pkcs12_create_ex2_setup(&key, &cert, &ca);
  142. if (!TEST_ptr(p12))
  143. goto err;
  144. if (test == 0) {
  145. /* Confirm PKCS12_create_ex2 returns NULL */
  146. ptr = PKCS12_create_ex2(NULL, NULL, NULL,
  147. NULL, NULL, NID_undef, NID_undef,
  148. 0, 0, 0,
  149. testctx, NULL,
  150. NULL, NULL);
  151. if (TEST_ptr(ptr))
  152. goto err;
  153. /* Can't proceed without a valid cert at least */
  154. if (!TEST_ptr(cert))
  155. goto err;
  156. /* Specified call back called - return success */
  157. cb_ret = 1;
  158. ptr = PKCS12_create_ex2(NULL, NULL, NULL,
  159. cert, NULL, NID_undef, NID_undef,
  160. 0, 0, 0,
  161. testctx, NULL,
  162. pkcs12_create_cb, (void*)&cb_ret);
  163. /* PKCS12 successfully created */
  164. if (!TEST_ptr(ptr))
  165. goto err;
  166. } else if (test == 1) {
  167. /* Specified call back called - return error*/
  168. cb_ret = -1;
  169. ptr = PKCS12_create_ex2(NULL, NULL, NULL,
  170. cert, NULL, NID_undef, NID_undef,
  171. 0, 0, 0,
  172. testctx, NULL,
  173. pkcs12_create_cb, (void*)&cb_ret);
  174. /* PKCS12 not created */
  175. if (TEST_ptr(ptr))
  176. goto err;
  177. } else if (test == 2) {
  178. /* Specified call back called - return failure */
  179. cb_ret = 0;
  180. ptr = PKCS12_create_ex2(NULL, NULL, NULL,
  181. cert, NULL, NID_undef, NID_undef,
  182. 0, 0, 0,
  183. testctx, NULL,
  184. pkcs12_create_cb, (void*)&cb_ret);
  185. /* PKCS12 successfully created */
  186. if (!TEST_ptr(ptr))
  187. goto err;
  188. }
  189. ret = 1;
  190. err:
  191. PKCS12_free(p12);
  192. PKCS12_free(ptr);
  193. EVP_PKEY_free(key);
  194. X509_free(cert);
  195. OSSL_STACK_OF_X509_free(ca);
  196. return TEST_true(ret);
  197. }
  198. typedef enum OPTION_choice {
  199. OPT_ERR = -1,
  200. OPT_EOF = 0,
  201. OPT_IN_FILE,
  202. OPT_IN_PASS,
  203. OPT_IN_HAS_KEY,
  204. OPT_IN_HAS_CERT,
  205. OPT_IN_HAS_CA,
  206. OPT_LEGACY,
  207. OPT_TEST_ENUM
  208. } OPTION_CHOICE;
  209. const OPTIONS *test_get_options(void)
  210. {
  211. static const OPTIONS options[] = {
  212. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  213. { "in", OPT_IN_FILE, '<', "PKCS12 input file" },
  214. { "pass", OPT_IN_PASS, 's', "PKCS12 input file password" },
  215. { "has-key", OPT_IN_HAS_KEY, 'n', "Whether the input file does contain an user key" },
  216. { "has-cert", OPT_IN_HAS_CERT, 'n', "Whether the input file does contain an user certificate" },
  217. { "has-ca", OPT_IN_HAS_CA, 'n', "Whether the input file does contain other certificate" },
  218. { "legacy", OPT_LEGACY, '-', "Test the legacy APIs" },
  219. { NULL }
  220. };
  221. return options;
  222. }
  223. int setup_tests(void)
  224. {
  225. OPTION_CHOICE o;
  226. while ((o = opt_next()) != OPT_EOF) {
  227. switch (o) {
  228. case OPT_IN_FILE:
  229. in_file = opt_arg();
  230. break;
  231. case OPT_IN_PASS:
  232. in_pass = opt_arg();
  233. break;
  234. case OPT_LEGACY:
  235. break;
  236. case OPT_IN_HAS_KEY:
  237. has_key = opt_int_arg();
  238. break;
  239. case OPT_IN_HAS_CERT:
  240. has_cert = opt_int_arg();
  241. break;
  242. case OPT_IN_HAS_CA:
  243. has_ca = opt_int_arg();
  244. break;
  245. case OPT_TEST_CASES:
  246. break;
  247. default:
  248. return 0;
  249. }
  250. }
  251. if (!test_get_libctx(&testctx, &nullprov, NULL, NULL, NULL)) {
  252. OSSL_LIB_CTX_free(testctx);
  253. testctx = NULL;
  254. return 0;
  255. }
  256. ADD_TEST(test_null_args);
  257. ADD_TEST(pkcs12_parse_test);
  258. ADD_ALL_TESTS(pkcs12_create_ex2_test, 3);
  259. return 1;
  260. }
  261. void cleanup_tests(void)
  262. {
  263. OSSL_LIB_CTX_free(testctx);
  264. OSSL_PROVIDER_unload(nullprov);
  265. }