PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/suites/security/kmf/tests/kmf_api/kmf_verify_cert.c

https://bitbucket.org/illumos/illumos-stc
C | 265 lines | 175 code | 32 blank | 58 comment | 39 complexity | 1b96b76a3a7cebfc81589492333fcd64 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
  23. * Use is subject to license terms.
  24. */
  25. #include "kmf_test_util.h"
  26. #ifdef __stc_assertion__
  27. /*
  28. * ASSERTION: kmf_verify_cert_001, kmf_verify_cert_002,
  29. * kmf_verify_cert_003
  30. *
  31. * DESCRIPTION:
  32. * If kmf_verify_cert() is called with one or some unaccepted
  33. * parameters, it will fail with KMF_ERR_BAD_PARAMETER.
  34. *
  35. * If kmf_verify_cert() is called with an invalid kstype,
  36. * it will fail with KMF_ERR_PLUGIN_NOTFOUND.
  37. *
  38. * If kmf_verify_cert() is called with cert data couldn't be
  39. * decoded, it will fail with KMF_ERR_BAD_CERT_FORMAT.
  40. *
  41. * STRATEGY:
  42. * 1) Call KMF_Initialize.
  43. * 2) Set one or some of the four input parameters
  44. * to NULL, then call kmf_verify_cert.
  45. * 3) Verify the return value is KMF_ERR_BAD_PARAMETER.
  46. *
  47. * 4) Call kmf_verify_cert with an invalid params->kstype
  48. * (other than NSS, PKCS11, OpenSSL).
  49. * 5) Verify the return value is KMF_ERR_PLUGIN_NOTFOUND.
  50. *
  51. * 6) Given cert format couldn't be decoded,
  52. * call kmf_verify_cert.
  53. * 7) Verify the return value is KMF_ERR_BAD_CERT_FORMAT.
  54. *
  55. * 8) Free memory.
  56. * 9) Call KMF_Finalize.
  57. *
  58. * INTERFACES: kmf_verify_cert
  59. */
  60. #endif /* __stc_assertion_ */
  61. static int failure = 0;
  62. static int param_fail = 0;
  63. static int param_tests = 0;
  64. static int test_num = 0;
  65. static char *func_name = "kmf_verify_cert()";
  66. static int
  67. set_attrlist(KMF_ATTRIBUTE *attrs, KMF_KEY_HANDLE *key, KMF_DATA *cert,
  68. KMF_DATA *SignerCert)
  69. {
  70. int i = 0;
  71. if (key) {
  72. kmf_set_attr_at_index(attrs, i, KMF_KEY_HANDLE_ATTR,
  73. key, sizeof (KMF_KEY_HANDLE));
  74. i++;
  75. }
  76. if (cert) {
  77. kmf_set_attr_at_index(attrs, i, KMF_CERT_DATA_ATTR,
  78. cert, sizeof (KMF_DATA));
  79. i++;
  80. }
  81. if (SignerCert) {
  82. kmf_set_attr_at_index(attrs, i, KMF_SIGNER_CERT_DATA_ATTR,
  83. SignerCert, sizeof (KMF_DATA));
  84. i++;
  85. }
  86. return (i);
  87. }
  88. int
  89. main(int argc, char *argv[])
  90. {
  91. KMF_RETURN exp_ret;
  92. int ret = STF_PASS;
  93. KMF_HANDLE_T kmfhandle, tmphandle = NULL;
  94. KMF_KEY_HANDLE key, privKey, pubKey;
  95. KMF_DATA signedCert;
  96. KMF_HANDLE_T *arg1[2] = {&tmphandle, &kmfhandle};
  97. KMF_KEY_HANDLE *arg21[2] = {NULL, &pubKey};
  98. KMF_DATA *arg3[2] = {NULL, &signedCert};
  99. int i, j, k;
  100. KMF_CREATEKEYPAIR_PARAMS params;
  101. KMF_X509_CERTIFICATE cert, tobesigned;
  102. uchar_t sernum[16];
  103. uint32_t numlen;
  104. KMF_DATA x509Cert = {NULL, 0};
  105. int numattr = 0;
  106. KMF_ATTRIBUTE attrlist[32];
  107. KMF_CRYPTOWITHCERT_PARAMS crypto_params;
  108. KMF_DATA signercert = {NULL, 0};
  109. KMF_DATA nokeyusage_signercert = {NULL, 0};
  110. KMF_DATA *arg22[2] = {NULL, &signercert};
  111. uint32_t isCert = 1;
  112. if ((kmf_test_initialize(&kmfhandle, NULL)) != 0)
  113. return (STF_UNRESOLVED);
  114. exp_ret = KMF_ERR_BAD_PARAMETER;
  115. print_test(++test_num, func_name, exp_ret);
  116. for (i = 0; i < 2; i++) {
  117. for (j = 0; j < 2; j++) {
  118. for (k = 0; k < 2; k++) {
  119. if (k == 1 && j == 1 && i == 1)
  120. continue;
  121. param_tests++;
  122. numattr = set_attrlist(attrlist, arg21[j], arg3[k], NULL);
  123. if ((compare_result(kmf_verify_cert(
  124. *arg1[i], numattr, attrlist), exp_ret))
  125. != 0 && (++param_fail) == 1)
  126. failure++;
  127. param_tests++;
  128. numattr = set_attrlist(attrlist, NULL, arg3[k], arg22[j]);
  129. if ((compare_result(kmf_verify_cert(
  130. *arg1[i], numattr, attrlist), exp_ret))
  131. != 0 && (++param_fail) == 1)
  132. failure++;
  133. }
  134. }
  135. }
  136. if (param_fail)
  137. jnl_printf("%d parameter tests, %d failed\n",
  138. param_tests, param_fail);
  139. exp_ret = KMF_ERR_PLUGIN_NOTFOUND;
  140. print_test(++test_num, func_name, exp_ret);
  141. unlink(SSL_KEY_FILE);
  142. numlen = sizeof (sernum);
  143. memset(sernum, 0xa, numlen);
  144. if (create_signed_cert(kmfhandle, &params, &privKey, &pubKey,
  145. KMF_KEYSTORE_OPENSSL, KMF_RSA, RSA_SIZE, NULL, &cert,
  146. sernum, numlen, &signedCert)) {
  147. failure++;
  148. goto out;
  149. }
  150. pubKey.kstype = 100;
  151. numattr = set_attrlist(attrlist, &pubKey, &signedCert, NULL);
  152. if (compare_result(kmf_verify_cert(kmfhandle, numattr, attrlist),
  153. exp_ret) != 0)
  154. failure++;
  155. pubKey.kstype = KMF_KEYSTORE_OPENSSL;
  156. exp_ret = KMF_ERR_BAD_CERT_FORMAT;
  157. print_test(++test_num, func_name, exp_ret);
  158. memset(signedCert.Data, 1, signedCert.Length);
  159. numattr = set_attrlist(attrlist, &pubKey, &signedCert, NULL);
  160. if (compare_result(kmf_verify_cert(kmfhandle, numattr, attrlist),
  161. exp_ret) != 0)
  162. failure++;
  163. kmf_free_data(&signedCert);
  164. kmf_free_signed_cert(&cert);
  165. exp_ret = KMF_ERR_KEYUSAGE;
  166. unlink(SSL_KEY_FILE);
  167. memset(sernum, 0xa, numlen);
  168. if (create_signed_cert(kmfhandle, &params, &privKey, &pubKey,
  169. KMF_KEYSTORE_OPENSSL, KMF_RSA, RSA_SIZE, NULL, &cert,
  170. sernum, numlen, &signercert)) {
  171. failure++;
  172. goto out;
  173. }
  174. kmf_free_signed_cert(&cert);
  175. memset(sernum, 0xb, numlen);
  176. /* Create an unsigned X509_CERTIFICATE record */
  177. unlink(SSL_KEY_FILE);
  178. if (create_keypair(kmfhandle, &privKey, &pubKey, KMF_KEYSTORE_OPENSSL,
  179. KMF_RSA, RSA_SIZE, NULL)) {
  180. failure++;
  181. goto out;
  182. }
  183. if (build_x509_cert(kmfhandle, &tobesigned, sernum, numlen, &pubKey)) {
  184. failure++;
  185. goto out;
  186. }
  187. signedCert.Length = 0;
  188. signedCert.Data = NULL;
  189. print_test(++test_num, func_name, exp_ret);
  190. if (set_cryptowithcert_params(&crypto_params, KMF_KEYSTORE_OPENSSL,
  191. KMF_FORMAT_ASN1, NULL)) {
  192. failure++;
  193. } else {
  194. set_SignCertWithCert_attrs(attrlist, &numattr,
  195. &crypto_params, &tobesigned, &signercert, &signedCert);
  196. if (testcall(kmf_sign_cert(kmfhandle, numattr, attrlist),
  197. "kmf_sign_cert")) {
  198. failure++;
  199. goto out;
  200. }
  201. unlink(SSL_KEY_FILE);
  202. memset(sernum, 0xc, numlen);
  203. if (create_base_signed_cert(kmfhandle, &params,
  204. &privKey, &pubKey,
  205. KMF_KEYSTORE_OPENSSL, KMF_RSA, RSA_SIZE, NULL, &cert,
  206. sernum, numlen, &nokeyusage_signercert)) {
  207. failure++;
  208. goto out;
  209. }
  210. numattr = set_attrlist(attrlist, NULL, &signedCert,
  211. &nokeyusage_signercert);
  212. if (compare_result(kmf_verify_cert(kmfhandle,
  213. numattr, attrlist), exp_ret) != 0)
  214. failure++;
  215. free_cryptowithcert_params(&crypto_params);
  216. }
  217. out:
  218. kmf_free_data(&x509Cert);
  219. kmf_free_data(&signedCert);
  220. kmf_free_kmf_key(kmfhandle, &privKey);
  221. kmf_free_kmf_key(kmfhandle, &pubKey);
  222. kmf_free_signed_cert(&cert);
  223. kmf_free_signed_cert(&tobesigned);
  224. kmf_free_data(&nokeyusage_signercert);
  225. kmf_test_finalize(kmfhandle);
  226. if (failure != 0) {
  227. jnl_printf("%d tests, only %d passed\n", test_num,
  228. test_num - failure);
  229. ret = STF_FAIL;
  230. }
  231. kmf_test_finalize(kmfhandle);
  232. return (ret);
  233. }