/security/nss/cmd/libpkix/testutil/testutil_nss.c

http://github.com/zpao/v8monkey · C · 663 lines · 466 code · 153 blank · 44 comment · 32 complexity · 41bf9e91d4be6d5ce89599435f1bbf07 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. * testutil_nss.c
  39. *
  40. * NSS-specific utility functions for handling test errors
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stddef.h>
  46. #include "pkix_pl_generalname.h"
  47. #include "pkix_pl_cert.h"
  48. #include "pkix.h"
  49. #include "testutil.h"
  50. #include "prlong.h"
  51. #include "plstr.h"
  52. #include "prthread.h"
  53. #include "secutil.h"
  54. #include "nspr.h"
  55. #include "prtypes.h"
  56. #include "prtime.h"
  57. #include "pk11func.h"
  58. #include "secasn1.h"
  59. #include "cert.h"
  60. #include "cryptohi.h"
  61. #include "secoid.h"
  62. #include "certdb.h"
  63. #include "secitem.h"
  64. #include "keythi.h"
  65. #include "nss.h"
  66. static char *catDirName(char *dir, char *name, void *plContext)
  67. {
  68. char *pathName = NULL;
  69. PKIX_UInt32 nameLen;
  70. PKIX_UInt32 dirLen;
  71. PKIX_TEST_STD_VARS();
  72. nameLen = PL_strlen(name);
  73. dirLen = PL_strlen(dir);
  74. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc
  75. (dirLen + nameLen + 2,
  76. (void **)&pathName,
  77. plContext));
  78. PL_strcpy(pathName, dir);
  79. PL_strcat(pathName, "/");
  80. PL_strcat(pathName, name);
  81. printf("pathName = %s\n", pathName);
  82. cleanup:
  83. PKIX_TEST_RETURN();
  84. return (pathName);
  85. }
  86. PKIX_PL_Cert *
  87. createCert(
  88. char *dirName,
  89. char *certFileName,
  90. void *plContext)
  91. {
  92. PKIX_PL_ByteArray *byteArray = NULL;
  93. void *buf = NULL;
  94. PRFileDesc *certFile = NULL;
  95. PKIX_UInt32 len;
  96. SECItem certDER;
  97. SECStatus rv;
  98. /* default: NULL cert (failure case) */
  99. PKIX_PL_Cert *cert = NULL;
  100. char *pathName = NULL;
  101. PKIX_TEST_STD_VARS();
  102. certDER.data = NULL;
  103. pathName = catDirName(dirName, certFileName, plContext);
  104. certFile = PR_Open(pathName, PR_RDONLY, 0);
  105. if (!certFile){
  106. pkixTestErrorMsg = "Unable to open cert file";
  107. goto cleanup;
  108. } else {
  109. rv = SECU_ReadDERFromFile(&certDER, certFile, PR_FALSE);
  110. if (!rv){
  111. buf = (void *)certDER.data;
  112. len = certDER.len;
  113. PKIX_TEST_EXPECT_NO_ERROR
  114. (PKIX_PL_ByteArray_Create
  115. (buf, len, &byteArray, plContext));
  116. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_Create
  117. (byteArray, &cert, plContext));
  118. SECITEM_FreeItem(&certDER, PR_FALSE);
  119. } else {
  120. pkixTestErrorMsg = "Unable to read DER from cert file";
  121. goto cleanup;
  122. }
  123. }
  124. cleanup:
  125. pkixTestErrorResult = PKIX_PL_Free(pathName, plContext);
  126. if (certFile){
  127. PR_Close(certFile);
  128. }
  129. if (PKIX_TEST_ERROR_RECEIVED){
  130. SECITEM_FreeItem(&certDER, PR_FALSE);
  131. }
  132. PKIX_TEST_DECREF_AC(byteArray);
  133. PKIX_TEST_RETURN();
  134. return (cert);
  135. }
  136. PKIX_PL_CRL *
  137. createCRL(
  138. char *dirName,
  139. char *crlFileName,
  140. void *plContext)
  141. {
  142. PKIX_PL_ByteArray *byteArray = NULL;
  143. PKIX_PL_CRL *crl = NULL;
  144. PKIX_Error *error = NULL;
  145. PRFileDesc *inFile = NULL;
  146. SECItem crlDER;
  147. void *buf = NULL;
  148. PKIX_UInt32 len;
  149. SECStatus rv;
  150. char *pathName = NULL;
  151. PKIX_TEST_STD_VARS();
  152. crlDER.data = NULL;
  153. pathName = catDirName(dirName, crlFileName, plContext);
  154. inFile = PR_Open(pathName, PR_RDONLY, 0);
  155. if (!inFile){
  156. pkixTestErrorMsg = "Unable to open crl file";
  157. goto cleanup;
  158. } else {
  159. rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE);
  160. if (!rv){
  161. buf = (void *)crlDER.data;
  162. len = crlDER.len;
  163. error = PKIX_PL_ByteArray_Create
  164. (buf, len, &byteArray, plContext);
  165. if (error){
  166. pkixTestErrorMsg =
  167. "PKIX_PL_ByteArray_Create failed";
  168. goto cleanup;
  169. }
  170. error = PKIX_PL_CRL_Create(byteArray, &crl, plContext);
  171. if (error){
  172. pkixTestErrorMsg = "PKIX_PL_Crl_Create failed";
  173. goto cleanup;
  174. }
  175. SECITEM_FreeItem(&crlDER, PR_FALSE);
  176. } else {
  177. pkixTestErrorMsg = "Unable to read DER from crl file";
  178. goto cleanup;
  179. }
  180. }
  181. cleanup:
  182. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(pathName, plContext));
  183. if (inFile){
  184. PR_Close(inFile);
  185. }
  186. if (error){
  187. SECITEM_FreeItem(&crlDER, PR_FALSE);
  188. }
  189. PKIX_TEST_DECREF_AC(byteArray);
  190. PKIX_TEST_RETURN();
  191. return (crl);
  192. }
  193. PKIX_TrustAnchor *
  194. createTrustAnchor(
  195. char *dirName,
  196. char *certFileName,
  197. PKIX_Boolean useCert,
  198. void *plContext)
  199. {
  200. PKIX_TrustAnchor *anchor = NULL;
  201. PKIX_PL_Cert *cert = NULL;
  202. PKIX_PL_X500Name *name = NULL;
  203. PKIX_PL_PublicKey *pubKey = NULL;
  204. PKIX_PL_CertNameConstraints *nameConstraints = NULL;
  205. PKIX_TEST_STD_VARS();
  206. cert = createCert(dirName, certFileName, plContext);
  207. if (useCert){
  208. PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_CreateWithCert
  209. (cert, &anchor, plContext));
  210. } else {
  211. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubject
  212. (cert, &name, plContext));
  213. if (name == NULL){
  214. goto cleanup;
  215. }
  216. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubjectPublicKey
  217. (cert, &pubKey, plContext));
  218. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetNameConstraints
  219. (cert, &nameConstraints, NULL));
  220. PKIX_TEST_EXPECT_NO_ERROR
  221. (PKIX_TrustAnchor_CreateWithNameKeyPair
  222. (name, pubKey, nameConstraints, &anchor, plContext));
  223. }
  224. cleanup:
  225. if (PKIX_TEST_ERROR_RECEIVED){
  226. PKIX_TEST_DECREF_AC(anchor);
  227. }
  228. PKIX_TEST_DECREF_AC(cert);
  229. PKIX_TEST_DECREF_AC(name);
  230. PKIX_TEST_DECREF_AC(pubKey);
  231. PKIX_TEST_DECREF_AC(nameConstraints);
  232. PKIX_TEST_RETURN();
  233. return (anchor);
  234. }
  235. PKIX_List *
  236. createCertChain(
  237. char *dirName,
  238. char *firstCertFileName,
  239. char *secondCertFileName,
  240. void *plContext)
  241. {
  242. PKIX_PL_Cert *firstCert = NULL;
  243. PKIX_PL_Cert *secondCert = NULL;
  244. PKIX_List *certList = NULL;
  245. PKIX_TEST_STD_VARS();
  246. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&certList, plContext));
  247. firstCert = createCert(dirName, firstCertFileName, plContext);
  248. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
  249. (certList, (PKIX_PL_Object *)firstCert, plContext));
  250. if (secondCertFileName){
  251. secondCert = createCert(dirName, secondCertFileName, plContext);
  252. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
  253. (certList, (PKIX_PL_Object *)secondCert, plContext));
  254. }
  255. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetImmutable
  256. (certList, plContext));
  257. cleanup:
  258. if (PKIX_TEST_ERROR_RECEIVED){
  259. PKIX_TEST_DECREF_AC(certList);
  260. }
  261. PKIX_TEST_DECREF_AC(firstCert);
  262. PKIX_TEST_DECREF_AC(secondCert);
  263. PKIX_TEST_RETURN();
  264. return (certList);
  265. }
  266. PKIX_List *
  267. createCertChainPlus(
  268. char *dirName,
  269. char *certNames[],
  270. PKIX_PL_Cert *certs[],
  271. PKIX_UInt32 numCerts,
  272. void *plContext)
  273. {
  274. PKIX_List *certList = NULL;
  275. PKIX_UInt32 i;
  276. PKIX_TEST_STD_VARS();
  277. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&certList, plContext));
  278. for (i = 0; i < numCerts; i++) {
  279. certs[i] = createCert(dirName, certNames[i], plContext);
  280. /* Create Cert may fail */
  281. if (certs[i] == NULL) {
  282. PKIX_TEST_DECREF_BC(certList);
  283. goto cleanup;
  284. }
  285. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
  286. (certList,
  287. (PKIX_PL_Object *)certs[i],
  288. plContext));
  289. }
  290. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetImmutable
  291. (certList, plContext));
  292. cleanup:
  293. if (PKIX_TEST_ERROR_RECEIVED){
  294. PKIX_TEST_DECREF_AC(certList);
  295. }
  296. for (i = 0; i < numCerts; i++) {
  297. PKIX_TEST_DECREF_AC(certs[i]);
  298. }
  299. PKIX_TEST_RETURN();
  300. return (certList);
  301. }
  302. PKIX_PL_Date *
  303. createDate(
  304. char *asciiDate,
  305. void *plContext)
  306. {
  307. PKIX_PL_Date *date = NULL;
  308. PKIX_PL_String *plString = NULL;
  309. PKIX_TEST_STD_VARS();
  310. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
  311. (PKIX_ESCASCII, asciiDate, 0, &plString, plContext));
  312. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Date_Create_UTCTime
  313. (plString, &date, plContext));
  314. cleanup:
  315. PKIX_TEST_DECREF_AC(plString);
  316. PKIX_TEST_RETURN();
  317. return (date);
  318. }
  319. PKIX_ProcessingParams *
  320. createProcessingParams(
  321. char *dirName,
  322. char *firstAnchorFileName,
  323. char *secondAnchorFileName,
  324. char *dateAscii,
  325. PKIX_List *initialPolicies, /* List of PKIX_PL_OID */
  326. PKIX_Boolean isCrlEnabled,
  327. void *plContext)
  328. {
  329. PKIX_TrustAnchor *firstAnchor = NULL;
  330. PKIX_TrustAnchor *secondAnchor = NULL;
  331. PKIX_List *anchorsList = NULL;
  332. PKIX_ProcessingParams *procParams = NULL;
  333. PKIX_PL_String *dateString = NULL;
  334. PKIX_PL_Date *testDate = NULL;
  335. PKIX_TEST_STD_VARS();
  336. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&anchorsList, plContext));
  337. firstAnchor = createTrustAnchor
  338. (dirName, firstAnchorFileName, PKIX_FALSE, plContext);
  339. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
  340. (anchorsList,
  341. (PKIX_PL_Object *)firstAnchor,
  342. plContext));
  343. if (secondAnchorFileName){
  344. secondAnchor =
  345. createTrustAnchor
  346. (dirName, secondAnchorFileName, PKIX_FALSE, plContext);
  347. PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
  348. (anchorsList,
  349. (PKIX_PL_Object *)secondAnchor,
  350. plContext));
  351. }
  352. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_Create
  353. (anchorsList, &procParams, plContext));
  354. if (dateAscii){
  355. PKIX_TEST_EXPECT_NO_ERROR
  356. (PKIX_PL_String_Create
  357. (PKIX_ESCASCII,
  358. dateAscii,
  359. 0,
  360. &dateString,
  361. plContext));
  362. PKIX_TEST_EXPECT_NO_ERROR
  363. (PKIX_PL_Date_Create_UTCTime
  364. (dateString, &testDate, plContext));
  365. PKIX_TEST_EXPECT_NO_ERROR
  366. (PKIX_ProcessingParams_SetDate
  367. (procParams, testDate, plContext));
  368. }
  369. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetInitialPolicies
  370. (procParams, initialPolicies, plContext));
  371. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetRevocationEnabled
  372. (procParams, isCrlEnabled, plContext));
  373. cleanup:
  374. if (PKIX_TEST_ERROR_RECEIVED){
  375. PKIX_TEST_DECREF_AC(procParams);
  376. }
  377. PKIX_TEST_DECREF_AC(dateString);
  378. PKIX_TEST_DECREF_AC(testDate);
  379. PKIX_TEST_DECREF_AC(anchorsList);
  380. PKIX_TEST_DECREF_AC(firstAnchor);
  381. PKIX_TEST_DECREF_AC(secondAnchor);
  382. PKIX_TEST_RETURN();
  383. return (procParams);
  384. }
  385. PKIX_ValidateParams *
  386. createValidateParams(
  387. char *dirName,
  388. char *firstAnchorFileName,
  389. char *secondAnchorFileName,
  390. char *dateAscii,
  391. PKIX_List *initialPolicies, /* List of PKIX_PL_OID */
  392. PKIX_Boolean initialPolicyMappingInhibit,
  393. PKIX_Boolean initialAnyPolicyInhibit,
  394. PKIX_Boolean initialExplicitPolicy,
  395. PKIX_Boolean isCrlEnabled,
  396. PKIX_List *chain,
  397. void *plContext)
  398. {
  399. PKIX_ProcessingParams *procParams = NULL;
  400. PKIX_ValidateParams *valParams = NULL;
  401. PKIX_TEST_STD_VARS();
  402. procParams =
  403. createProcessingParams
  404. (dirName,
  405. firstAnchorFileName,
  406. secondAnchorFileName,
  407. dateAscii,
  408. NULL,
  409. isCrlEnabled,
  410. plContext);
  411. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetInitialPolicies
  412. (procParams, initialPolicies, plContext));
  413. PKIX_TEST_EXPECT_NO_ERROR
  414. (PKIX_ProcessingParams_SetPolicyMappingInhibited
  415. (procParams, initialPolicyMappingInhibit, NULL));
  416. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetAnyPolicyInhibited
  417. (procParams, initialAnyPolicyInhibit, NULL));
  418. PKIX_TEST_EXPECT_NO_ERROR
  419. (PKIX_ProcessingParams_SetExplicitPolicyRequired
  420. (procParams, initialExplicitPolicy, NULL));
  421. PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_Create
  422. (procParams, chain, &valParams, plContext));
  423. cleanup:
  424. if (PKIX_TEST_ERROR_RECEIVED){
  425. PKIX_TEST_DECREF_AC(valParams);
  426. }
  427. PKIX_TEST_DECREF_AC(procParams);
  428. PKIX_TEST_RETURN();
  429. return (valParams);
  430. }
  431. PKIX_ValidateResult *
  432. createValidateResult(
  433. char *dirName,
  434. char *anchorFileName,
  435. char *pubKeyCertFileName,
  436. void *plContext)
  437. {
  438. PKIX_TrustAnchor *anchor = NULL;
  439. PKIX_ValidateResult *valResult = NULL;
  440. PKIX_PL_Cert *cert = NULL;
  441. PKIX_PL_PublicKey *pubKey = NULL;
  442. PKIX_TEST_STD_VARS();
  443. anchor = createTrustAnchor
  444. (dirName, anchorFileName, PKIX_FALSE, plContext);
  445. cert = createCert(dirName, pubKeyCertFileName, plContext);
  446. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubjectPublicKey
  447. (cert, &pubKey, plContext));
  448. PKIX_TEST_EXPECT_NO_ERROR
  449. (pkix_ValidateResult_Create
  450. (pubKey, anchor, NULL, &valResult, plContext));
  451. cleanup:
  452. if (PKIX_TEST_ERROR_RECEIVED){
  453. PKIX_TEST_DECREF_AC(valResult);
  454. }
  455. PKIX_TEST_DECREF_AC(anchor);
  456. PKIX_TEST_DECREF_AC(cert);
  457. PKIX_TEST_DECREF_AC(pubKey);
  458. PKIX_TEST_RETURN();
  459. return (valResult);
  460. }
  461. PKIX_PL_GeneralName *
  462. createGeneralName(
  463. PKIX_UInt32 nameType,
  464. char *asciiName,
  465. void *plContext)
  466. {
  467. PKIX_PL_GeneralName *generalName = NULL;
  468. PKIX_PL_String *plString = NULL;
  469. PKIX_TEST_STD_VARS();
  470. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
  471. (PKIX_ESCASCII, asciiName, 0, &plString, plContext));
  472. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_GeneralName_Create
  473. (nameType, plString, &generalName, plContext));
  474. cleanup:
  475. PKIX_TEST_DECREF_AC(plString);
  476. PKIX_TEST_RETURN();
  477. return (generalName);
  478. }
  479. PKIX_BuildResult *
  480. createBuildResult(
  481. char *dirName,
  482. char *anchorFileName,
  483. char *pubKeyCertFileName,
  484. char *firstChainCertFileName,
  485. char *secondChainCertFileName,
  486. void *plContext)
  487. {
  488. PKIX_BuildResult *buildResult = NULL;
  489. PKIX_ValidateResult *valResult = NULL;
  490. PKIX_List *certChain = NULL;
  491. PKIX_TEST_STD_VARS();
  492. valResult = createValidateResult
  493. (dirName, anchorFileName, pubKeyCertFileName, plContext);
  494. certChain = createCertChain
  495. (dirName,
  496. firstChainCertFileName,
  497. secondChainCertFileName,
  498. plContext);
  499. PKIX_TEST_EXPECT_NO_ERROR
  500. (pkix_BuildResult_Create
  501. (valResult, certChain, &buildResult, plContext));
  502. cleanup:
  503. if (PKIX_TEST_ERROR_RECEIVED){
  504. PKIX_TEST_DECREF_AC(buildResult);
  505. }
  506. PKIX_TEST_DECREF_AC(valResult);
  507. PKIX_TEST_DECREF_AC(certChain);
  508. PKIX_TEST_RETURN();
  509. return (buildResult);
  510. }