PageRenderTime 41ms CodeModel.GetById 21ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/zpao/v8monkey
C | 274 lines | 163 code | 55 blank | 56 comment | 12 complexity | a88d3136afa9ace2cf22eaaa35d47df2 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_defaultcrlchecker2stores.c
 39 *
 40 * Test Default CRL with multiple CertStore Checking
 41 *
 42 */
 43
 44#include "testutil.h"
 45#include "testutil_nss.h"
 46
 47#define PKIX_TEST_MAX_CERTS     10
 48
 49static void *plContext = NULL;
 50
 51static
 52void printUsage1(char *pName){
 53        printf("\nUSAGE: %s test-purpose [ENE|EE] ", pName);
 54        printf("crl-directory cert [certs].\n");
 55}
 56
 57static
 58void printUsageMax(PKIX_UInt32 numCerts){
 59        printf("\nUSAGE ERROR: number of certs %d exceed maximum %d\n",
 60                numCerts, PKIX_TEST_MAX_CERTS);
 61}
 62
 63static PKIX_Error *
 64getCertCallback(
 65        PKIX_CertStore *store,
 66        PKIX_CertSelector *certSelector,
 67        PKIX_List **pCerts,
 68        void *plContext)
 69{
 70        return (NULL);
 71}
 72
 73static PKIX_Error *
 74testDefaultMultipleCertStores(PKIX_ValidateParams *valParams,
 75                        char *crlDir1,
 76                        char *crlDir2)
 77{
 78        PKIX_PL_String *dirString1 = NULL;
 79        PKIX_PL_String *dirString2 = NULL;
 80        PKIX_CertStore *certStore1 = NULL;
 81        PKIX_CertStore *certStore2 = NULL;
 82        PKIX_List *certStoreList = NULL;
 83        PKIX_ProcessingParams *procParams = NULL;
 84
 85        PKIX_TEST_STD_VARS();
 86
 87        subTest("PKIX_PL_CollectionCertStore_Create");
 88
 89        /* Create CollectionCertStore */
 90
 91        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
 92                                    (PKIX_ESCASCII,
 93                                    crlDir1,
 94                                    0,
 95                                    &dirString1,
 96                                    plContext));
 97
 98        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_CollectionCertStore_Create
 99                                    (dirString1,
100                                    &certStore1,
101                                    plContext));
102
103        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
104                                    (PKIX_ESCASCII,
105                                    crlDir2,
106                                    0,
107                                    &dirString2,
108                                    plContext));
109
110        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_CollectionCertStore_Create
111                                    (dirString2,
112                                    &certStore2,
113                                    plContext));
114
115        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams
116                                    (valParams, &procParams, plContext));
117
118        /* Add multiple CollectionCertStores */
119
120        subTest("PKIX_ProcessingParams_SetCertStores");
121        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&certStoreList, plContext));
122        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
123                    (certStoreList, (PKIX_PL_Object *)certStore1, plContext));
124        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetCertStores
125                    (procParams, certStoreList, plContext));
126
127        subTest("PKIX_ProcessingParams_AddCertStore");
128        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_AddCertStore
129                    (procParams, certStore2, plContext));
130
131        subTest("PKIX_ProcessingParams_SetRevocationEnabled");
132        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetRevocationEnabled
133                                    (procParams, PKIX_TRUE, plContext));
134
135cleanup:
136
137        PKIX_TEST_DECREF_AC(dirString1);
138        PKIX_TEST_DECREF_AC(dirString2);
139        PKIX_TEST_DECREF_AC(certStore1);
140        PKIX_TEST_DECREF_AC(certStore2);
141        PKIX_TEST_DECREF_AC(certStoreList);
142        PKIX_TEST_DECREF_AC(procParams);
143
144        PKIX_TEST_RETURN();
145
146        return (0);
147}
148
149/*
150 * Validate Certificate Chain with Certificate Revocation List
151 *      Certificate Chain is build based on input certs' sequence.
152 *      CRL is fetched from the directory specified in CollectionCertStore.
153 *      while CollectionCertStore is linked in CertStore Object which then
154 *      linked in ProcessParam. During validation, CRLChecker will invoke
155 *      the crlCallback (this test uses PKIX_PL_CollectionCertStore_GetCRL)
156 *      to get CRL data for revocation check.
157 *      This test gets CRL's from two CertStores, each has a valid CRL
158 *      required for revocation check to pass.
159 */
160
161int test_defaultcrlchecker2stores(int argc, char *argv[]){
162
163        PKIX_List *chain = NULL;
164        PKIX_ValidateParams *valParams = NULL;
165        PKIX_ValidateResult *valResult = NULL;
166        PKIX_UInt32 actualMinorVersion;
167        char *certNames[PKIX_TEST_MAX_CERTS];
168        PKIX_PL_Cert *certs[PKIX_TEST_MAX_CERTS];
169	PKIX_VerifyNode *verifyTree = NULL;
170	PKIX_PL_String *verifyString = NULL;
171        PKIX_UInt32 chainLength = 0;
172        PKIX_UInt32 i = 0;
173        PKIX_UInt32 j = 0;
174        PKIX_Boolean testValid = PKIX_TRUE;
175        char *dirName = NULL;
176        char *anchorName = NULL;
177
178        PKIX_TEST_STD_VARS();
179
180        if (argc < 6) {
181                printUsage1(argv[0]);
182                return (0);
183        }
184
185        startTests("CRL Checker");
186
187        PKIX_TEST_EXPECT_NO_ERROR(
188            PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
189
190        /* ENE = expect no error; EE = expect error */
191        if (PORT_Strcmp(argv[2+j], "ENE") == 0) {
192                testValid = PKIX_TRUE;
193        } else if (PORT_Strcmp(argv[2+j], "EE") == 0) {
194                testValid = PKIX_FALSE;
195        } else {
196                printUsage1(argv[0]);
197                return (0);
198        }
199
200        chainLength = (argc - j) - 7;
201        if (chainLength > PKIX_TEST_MAX_CERTS) {
202                printUsageMax(chainLength);
203        }
204
205        for (i = 0; i < chainLength; i++) {
206
207                certNames[i] = argv[(7+j)+i];
208                certs[i] = NULL;
209        }
210
211
212        subTest(argv[1+j]);
213
214        subTest("Default-CRL-Checker");
215
216        subTest("Default-CRL-Checker - Create Cert Chain");
217
218        dirName = argv[3+j];
219
220        chain = createCertChainPlus
221                (dirName, certNames, certs, chainLength, plContext);
222
223        subTest("Default-CRL-Checker - Create Params");
224
225        anchorName = argv[6+j];
226
227        valParams = createValidateParams
228                (dirName,
229                anchorName,
230                NULL,
231                NULL,
232                NULL,
233                PKIX_FALSE,
234                PKIX_FALSE,
235                PKIX_FALSE,
236                PKIX_FALSE,
237                chain,
238                plContext);
239
240        subTest("Multiple-CertStores");
241
242        testDefaultMultipleCertStores(valParams, argv[4+j], argv[5+j]);
243
244        subTest("Default-CRL-Checker - Validate Chain");
245
246        if (testValid == PKIX_TRUE) {
247                PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateChain
248                        (valParams, &valResult, &verifyTree, plContext));
249        } else {
250                PKIX_TEST_EXPECT_ERROR(PKIX_ValidateChain
251                        (valParams, &valResult, &verifyTree, plContext));
252        }
253
254        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString
255                ((PKIX_PL_Object*)verifyTree, &verifyString, plContext));
256        (void) printf("verifyTree is\n%s\n", verifyString->escAsciiString);
257
258cleanup:
259
260        PKIX_TEST_DECREF_AC(verifyString);
261        PKIX_TEST_DECREF_AC(verifyTree);
262
263        PKIX_TEST_DECREF_AC(valParams);
264        PKIX_TEST_DECREF_AC(valResult);
265        PKIX_TEST_DECREF_AC(chain);
266
267        PKIX_Shutdown(plContext);
268
269        PKIX_TEST_RETURN();
270
271        endTests("CRL Checker");
272
273        return (0);
274}