/security/nss/cmd/libpkix/pkix_pl/module/test_ekuchecker.c

http://github.com/zpao/v8monkey · C · 321 lines · 207 code · 68 blank · 46 comment · 32 complexity · 633827aa519fc473a849a5e5aef6ce08 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the PKIX-C library.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Sun Microsystems, Inc.
  18. * Portions created by the Initial Developer are
  19. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Sun Microsystems, Inc.
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /*
  38. * test_ekuchecker.c
  39. *
  40. * Test Extend Key Usage Checker
  41. *
  42. */
  43. #include "testutil.h"
  44. #include "testutil_nss.h"
  45. #define PKIX_TEST_MAX_CERTS 10
  46. static void *plContext = NULL;
  47. static
  48. void printUsage1(char *pName){
  49. printf("\nUSAGE: %s test-purpose [ENE|EE] ", pName);
  50. printf("[E]oid[,oid]* <data-dir> cert [certs].\n");
  51. }
  52. static void printUsageMax(PKIX_UInt32 numCerts){
  53. printf("\nUSAGE ERROR: number of certs %d exceed maximum %d\n",
  54. numCerts, PKIX_TEST_MAX_CERTS);
  55. }
  56. static PKIX_Error *
  57. testCertSelectorMatchCallback(
  58. PKIX_CertSelector *selector,
  59. PKIX_PL_Cert *cert,
  60. PKIX_Boolean *pResult,
  61. void *plContext)
  62. {
  63. *pResult = PKIX_TRUE;
  64. return (0);
  65. }
  66. static PKIX_Error *
  67. testEkuSetup(
  68. PKIX_ValidateParams *valParams,
  69. char *ekuOidString,
  70. PKIX_Boolean *only4EE)
  71. {
  72. PKIX_ProcessingParams *procParams = NULL;
  73. PKIX_List *ekuList = NULL;
  74. PKIX_PL_OID *ekuOid = NULL;
  75. PKIX_ComCertSelParams *selParams = NULL;
  76. PKIX_CertSelector *certSelector = NULL;
  77. PKIX_Boolean last_token = PKIX_FALSE;
  78. PKIX_UInt32 i, tokeni;
  79. PKIX_TEST_STD_VARS();
  80. subTest("PKIX_ValidateParams_GetProcessingParams");
  81. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams
  82. (valParams, &procParams, plContext));
  83. /* Get extended key usage OID(s) from command line, separated by "," */
  84. if (ekuOidString[0] == '"') {
  85. /* erase doble quotes, if any */
  86. i = 1;
  87. while (ekuOidString[i] != '"' && ekuOidString[i] != '\0') {
  88. ekuOidString[i-1] = ekuOidString[i];
  89. i++;
  90. }
  91. ekuOidString[i-1] = '\0';
  92. }
  93. if (ekuOidString[0] == '\0') {
  94. ekuList = NULL;
  95. } else {
  96. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create
  97. (&ekuList, plContext));
  98. /* if OID string start with E, only check for last cert */
  99. if (ekuOidString[0] == 'E') {
  100. *only4EE = PKIX_TRUE;
  101. tokeni = 2;
  102. i = 1;
  103. } else {
  104. *only4EE = PKIX_FALSE;
  105. tokeni = 1;
  106. i = 0;
  107. }
  108. while (last_token != PKIX_TRUE) {
  109. while (ekuOidString[tokeni] != ',' &&
  110. ekuOidString[tokeni] != '\0') {
  111. tokeni++;
  112. }
  113. if (ekuOidString[tokeni] == '\0') {
  114. last_token = PKIX_TRUE;
  115. } else {
  116. ekuOidString[tokeni] = '\0';
  117. tokeni++;
  118. }
  119. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create
  120. (&ekuOidString[i], &ekuOid, plContext));
  121. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
  122. (ekuList, (PKIX_PL_Object *)ekuOid, plContext));
  123. PKIX_TEST_DECREF_BC(ekuOid);
  124. i = tokeni;
  125. }
  126. }
  127. /* Set extended key usage link to processing params */
  128. subTest("PKIX_ComCertSelParams_Create");
  129. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCertSelParams_Create
  130. (&selParams, plContext));
  131. subTest("PKIX_ComCertSelParams_SetExtendedKeyUsage");
  132. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCertSelParams_SetExtendedKeyUsage
  133. (selParams, ekuList, plContext));
  134. subTest("PKIX_CertSelector_Create");
  135. PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_Create
  136. (testCertSelectorMatchCallback,
  137. NULL,
  138. &certSelector,
  139. plContext));
  140. subTest("PKIX_CertSelector_SetCommonCertSelectorParams");
  141. PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_SetCommonCertSelectorParams
  142. (certSelector, selParams, plContext));
  143. subTest("PKIX_ProcessingParams_SetTargetCertConstraints");
  144. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetTargetCertConstraints
  145. (procParams, certSelector, plContext));
  146. cleanup:
  147. PKIX_TEST_DECREF_AC(selParams);
  148. PKIX_TEST_DECREF_AC(certSelector);
  149. PKIX_TEST_DECREF_AC(procParams);
  150. PKIX_TEST_DECREF_AC(ekuOid);
  151. PKIX_TEST_DECREF_AC(ekuList);
  152. PKIX_TEST_RETURN();
  153. return (0);
  154. }
  155. static PKIX_Error *
  156. testEkuChecker(
  157. PKIX_ValidateParams *valParams,
  158. PKIX_Boolean only4EE)
  159. {
  160. PKIX_ProcessingParams *procParams = NULL;
  161. PKIX_TEST_STD_VARS();
  162. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams
  163. (valParams, &procParams, plContext));
  164. subTest("PKIX_ProcessingParams_SetRevocationEnabled - disable");
  165. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetRevocationEnabled
  166. (procParams, PKIX_FALSE, plContext));
  167. if (only4EE == PKIX_FALSE) {
  168. subTest("PKIX_PL_EkuChecker_Create");
  169. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_EkuChecker_Create
  170. (procParams, plContext));
  171. }
  172. cleanup:
  173. PKIX_TEST_DECREF_AC(procParams);
  174. PKIX_TEST_RETURN();
  175. return (0);
  176. }
  177. int test_ekuchecker(int argc, char *argv[]){
  178. PKIX_List *chain = NULL;
  179. PKIX_ValidateParams *valParams = NULL;
  180. PKIX_ValidateResult *valResult = NULL;
  181. PKIX_UInt32 actualMinorVersion;
  182. char *certNames[PKIX_TEST_MAX_CERTS];
  183. char *dirName = NULL;
  184. PKIX_PL_Cert *certs[PKIX_TEST_MAX_CERTS];
  185. PKIX_UInt32 chainLength = 0;
  186. PKIX_UInt32 i = 0;
  187. PKIX_UInt32 j = 0;
  188. PKIX_Boolean testValid = PKIX_FALSE;
  189. PKIX_Boolean only4EE = PKIX_FALSE;
  190. PKIX_TEST_STD_VARS();
  191. if (argc < 5) {
  192. printUsage1(argv[0]);
  193. return (0);
  194. }
  195. startTests("EKU Checker");
  196. PKIX_TEST_EXPECT_NO_ERROR(
  197. PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
  198. /* ENE = expect no error; EE = expect error */
  199. if (PORT_Strcmp(argv[2+j], "ENE") == 0) {
  200. testValid = PKIX_TRUE;
  201. } else if (PORT_Strcmp(argv[2+j], "EE") == 0) {
  202. testValid = PKIX_FALSE;
  203. } else {
  204. printUsage1(argv[0]);
  205. return (0);
  206. }
  207. dirName = argv[4+j];
  208. chainLength = (argc - j) - 6;
  209. if (chainLength > PKIX_TEST_MAX_CERTS) {
  210. printUsageMax(chainLength);
  211. }
  212. for (i = 0; i < chainLength; i++) {
  213. certNames[i] = argv[6+i+j];
  214. certs[i] = NULL;
  215. }
  216. subTest(argv[1+j]);
  217. subTest("Extended-Key-Usage-Checker");
  218. subTest("Extended-Key-Usage-Checker - Create Cert Chain");
  219. chain = createCertChainPlus
  220. (dirName, certNames, certs, chainLength, plContext);
  221. subTest("Extended-Key-Usage-Checker - Create Params");
  222. valParams = createValidateParams
  223. (dirName,
  224. argv[5+j],
  225. NULL,
  226. NULL,
  227. NULL,
  228. PKIX_FALSE,
  229. PKIX_FALSE,
  230. PKIX_FALSE,
  231. PKIX_FALSE,
  232. chain,
  233. plContext);
  234. subTest("Default CertStore");
  235. testEkuSetup(valParams, argv[3+j], &only4EE);
  236. testEkuChecker(valParams, only4EE);
  237. subTest("Extended-Key-Usage-Checker - Validate Chain");
  238. if (testValid == PKIX_TRUE) {
  239. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateChain
  240. (valParams, &valResult, NULL, plContext));
  241. } else {
  242. PKIX_TEST_EXPECT_ERROR(PKIX_ValidateChain
  243. (valParams, &valResult, NULL, plContext));
  244. }
  245. cleanup:
  246. PKIX_TEST_DECREF_AC(chain);
  247. PKIX_TEST_DECREF_AC(valParams);
  248. PKIX_TEST_DECREF_AC(valResult);
  249. PKIX_Shutdown(plContext);
  250. PKIX_TEST_RETURN();
  251. endTests("EKU Checker");
  252. return (0);
  253. }