/security/nss/cmd/libpkix/pkix/top/test_subjaltnamechecker.c

http://github.com/zpao/v8monkey · C · 299 lines · 200 code · 47 blank · 52 comment · 29 complexity · e64e5b9e82f4fb90adf58070fd37d8dd 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_subjaltnamechecker.c
  39. *
  40. * Test Subject Alternative Name Checking
  41. *
  42. */
  43. /*
  44. * There is no subjaltnamechecker. Instead, targetcertchecker is doing
  45. * the job for checking subject alternative names' validity. For testing,
  46. * in order to enter names with various type, we create this test excutable
  47. * to parse different scenario.
  48. */
  49. #include "testutil.h"
  50. #include "testutil_nss.h"
  51. #define PKIX_TEST_MAX_CERTS 10
  52. static void *plContext = NULL;
  53. static
  54. void printUsage1(char *pName){
  55. printf("\nUSAGE: %s test-name [ENE|EE] ", pName);
  56. printf("cert [certs].\n");
  57. }
  58. static
  59. void printUsage2(char *name) {
  60. printf("\ninvalid test-name syntax - %s", name);
  61. printf("\ntest-name syntax: [01][DNORU]:<name>+...");
  62. printf("\n [01] 1 - match all; 0 - match one");
  63. printf("\n name - type can be specified as");
  64. printf("\n [DNORU] D-Directory name");
  65. printf("\n N-DNS name");
  66. printf("\n O-OID name");
  67. printf("\n R-RFC822 name");
  68. printf("\n U-URI name");
  69. printf("\n + separator for more names\n\n");
  70. }
  71. static
  72. void printUsageMax(PKIX_UInt32 numCerts){
  73. printf("\nUSAGE ERROR: number of certs %d exceed maximum %d\n",
  74. numCerts, PKIX_TEST_MAX_CERTS);
  75. }
  76. static
  77. PKIX_UInt32 getNameType(char *name){
  78. PKIX_UInt32 nameType;
  79. PKIX_TEST_STD_VARS();
  80. switch (*name) {
  81. case 'D':
  82. nameType = PKIX_DIRECTORY_NAME;
  83. break;
  84. case 'N':
  85. nameType = PKIX_DNS_NAME;
  86. break;
  87. case 'O':
  88. nameType = PKIX_OID_NAME;
  89. break;
  90. case 'R':
  91. nameType = PKIX_RFC822_NAME;
  92. break;
  93. case 'U':
  94. nameType = PKIX_URI_NAME;
  95. break;
  96. default:
  97. printUsage2(name);
  98. nameType = 0xFFFF;
  99. }
  100. goto cleanup;
  101. cleanup:
  102. PKIX_TEST_RETURN();
  103. return (nameType);
  104. }
  105. int test_subjaltnamechecker(int argc, char *argv[]){
  106. PKIX_List *chain = NULL;
  107. PKIX_ValidateParams *valParams = NULL;
  108. PKIX_ValidateResult *valResult = NULL;
  109. PKIX_CertSelector *selector = NULL;
  110. PKIX_ComCertSelParams *selParams = NULL;
  111. PKIX_ProcessingParams *procParams = NULL;
  112. PKIX_PL_GeneralName *name = NULL;
  113. PKIX_UInt32 actualMinorVersion;
  114. char *certNames[PKIX_TEST_MAX_CERTS];
  115. PKIX_PL_Cert *certs[PKIX_TEST_MAX_CERTS];
  116. PKIX_UInt32 chainLength = 0;
  117. PKIX_UInt32 i = 0;
  118. PKIX_UInt32 j = 0;
  119. char *nameStr;
  120. char *nameEnd;
  121. char *names[PKIX_TEST_MAX_CERTS];
  122. PKIX_UInt32 numNames = 0;
  123. PKIX_UInt32 nameType;
  124. PKIX_Boolean matchAll = PKIX_TRUE;
  125. PKIX_Boolean testValid = PKIX_TRUE;
  126. char *dirName = NULL;
  127. char *anchorName = NULL;
  128. PKIX_VerifyNode *verifyTree = NULL;
  129. PKIX_PL_String *verifyString = NULL;
  130. PKIX_TEST_STD_VARS();
  131. if (argc < 5) {
  132. printUsage1(argv[0]);
  133. return (0);
  134. }
  135. startTests("SubjAltNameConstraintChecker");
  136. PKIX_TEST_EXPECT_NO_ERROR(
  137. PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
  138. j++; /* skip test-purpose string */
  139. /* ENE = expect no error; EE = expect error */
  140. if (PORT_Strcmp(argv[2+j], "ENE") == 0) {
  141. testValid = PKIX_TRUE;
  142. } else if (PORT_Strcmp(argv[2+j], "EE") == 0) {
  143. testValid = PKIX_FALSE;
  144. } else {
  145. printUsage1(argv[0]);
  146. return (0);
  147. }
  148. /* taking out leading and trailing ", if any */
  149. nameStr = argv[1+j];
  150. subTest(nameStr);
  151. if (*nameStr == '"'){
  152. nameStr++;
  153. nameEnd = nameStr;
  154. while (*nameEnd != '"' && *nameEnd != '\0') {
  155. nameEnd++;
  156. }
  157. *nameEnd = '\0';
  158. }
  159. /* extract first [0|1] inidcating matchAll or not */
  160. matchAll = (*nameStr == '0')?PKIX_FALSE:PKIX_TRUE;
  161. nameStr++;
  162. numNames = 0;
  163. while (*nameStr != '\0') {
  164. names[numNames++] = nameStr;
  165. while (*nameStr != '+' && *nameStr != '\0') {
  166. nameStr++;
  167. }
  168. if (*nameStr == '+') {
  169. *nameStr = '\0';
  170. nameStr++;
  171. }
  172. }
  173. chainLength = (argc - j) - 4;
  174. if (chainLength > PKIX_TEST_MAX_CERTS) {
  175. printUsageMax(chainLength);
  176. }
  177. for (i = 0; i < chainLength; i++) {
  178. certNames[i] = argv[(4+j)+i];
  179. certs[i] = NULL;
  180. }
  181. /* SubjAltName for validation */
  182. subTest("Add Subject Alt Name for NameConstraint checking");
  183. subTest("Create Selector and ComCertSelParams");
  184. PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_Create
  185. (NULL, NULL, &selector, plContext));
  186. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCertSelParams_Create
  187. (&selParams, plContext));
  188. PKIX_TEST_EXPECT_NO_ERROR
  189. (PKIX_CertSelector_SetCommonCertSelectorParams
  190. (selector, selParams, plContext));
  191. subTest("PKIX_ComCertSelParams_SetMatchAllSubjAltNames");
  192. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCertSelParams_SetMatchAllSubjAltNames
  193. (selParams, matchAll, plContext));
  194. subTest("PKIX_ComCertSelParams_AddSubjAltName(s)");
  195. for (i = 0; i < numNames; i++) {
  196. nameType = getNameType(names[i]);
  197. if (nameType == 0xFFFF) {
  198. return (0);
  199. }
  200. nameStr = names[i] + 2;
  201. name = createGeneralName(nameType, nameStr, plContext);
  202. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCertSelParams_AddSubjAltName
  203. (selParams, name, plContext));
  204. PKIX_TEST_DECREF_BC(name);
  205. }
  206. subTest("SubjAltName-Constraints - Create Cert Chain");
  207. dirName = argv[3+j];
  208. chain = createCertChainPlus
  209. (dirName, certNames, certs, chainLength, plContext);
  210. subTest("SubjAltName-Constraints - Create Params");
  211. valParams = createValidateParams
  212. (dirName,
  213. argv[4+j],
  214. NULL,
  215. NULL,
  216. NULL,
  217. PKIX_FALSE,
  218. PKIX_FALSE,
  219. PKIX_FALSE,
  220. PKIX_FALSE,
  221. chain,
  222. plContext);
  223. subTest("PKIX_ValidateParams_getProcessingParams");
  224. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams
  225. (valParams, &procParams, plContext));
  226. subTest("PKIX_ProcessingParams_SetTargetCertConstraints");
  227. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetTargetCertConstraints
  228. (procParams, selector, plContext));
  229. subTest("Subject Alt Name - Validate Chain");
  230. if (testValid == PKIX_TRUE) {
  231. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateChain
  232. (valParams, &valResult, &verifyTree, plContext));
  233. } else {
  234. PKIX_TEST_EXPECT_ERROR(PKIX_ValidateChain
  235. (valParams, &valResult, &verifyTree, plContext));
  236. }
  237. cleanup:
  238. PKIX_PL_Free(anchorName, plContext);
  239. PKIX_TEST_DECREF_AC(verifyString);
  240. PKIX_TEST_DECREF_AC(verifyTree);
  241. PKIX_TEST_DECREF_AC(chain);
  242. PKIX_TEST_DECREF_AC(valParams);
  243. PKIX_TEST_DECREF_AC(valResult);
  244. PKIX_TEST_DECREF_AC(selector);
  245. PKIX_TEST_DECREF_AC(selParams);
  246. PKIX_TEST_DECREF_AC(procParams);
  247. PKIX_TEST_DECREF_AC(name);
  248. PKIX_Shutdown(plContext);
  249. PKIX_TEST_RETURN();
  250. endTests("SubjAltNameConstraintsChecker");
  251. return (0);
  252. }