PageRenderTime 71ms CodeModel.GetById 8ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 44#include <stdio.h>
 45#include <string.h>
 46#include <stddef.h>
 47
 48#include "pkix_pl_generalname.h"
 49#include "pkix_pl_cert.h"
 50#include "pkix.h"
 51#include "testutil.h"
 52#include "prlong.h"
 53#include "plstr.h"
 54#include "prthread.h"
 55#include "secutil.h"
 56#include "nspr.h"
 57#include "prtypes.h"
 58#include "prtime.h"
 59#include "pk11func.h"
 60#include "secasn1.h"
 61#include "cert.h"
 62#include "cryptohi.h"
 63#include "secoid.h"
 64#include "certdb.h"
 65#include "secitem.h"
 66#include "keythi.h"
 67#include "nss.h"
 68
 69static char *catDirName(char *dir, char *name, void *plContext)
 70{
 71        char *pathName = NULL;
 72        PKIX_UInt32 nameLen;
 73        PKIX_UInt32 dirLen;
 74
 75        PKIX_TEST_STD_VARS();
 76
 77        nameLen = PL_strlen(name);
 78        dirLen = PL_strlen(dir);
 79
 80        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc
 81                                    (dirLen + nameLen + 2,
 82                                    (void **)&pathName,
 83                                    plContext));
 84
 85        PL_strcpy(pathName, dir);
 86        PL_strcat(pathName, "/");
 87        PL_strcat(pathName, name);
 88        printf("pathName = %s\n", pathName);
 89
 90cleanup:
 91
 92        PKIX_TEST_RETURN();
 93
 94        return (pathName);
 95}
 96
 97PKIX_PL_Cert *
 98createCert(
 99        char *dirName,
100        char *certFileName,
101        void *plContext)
102{
103        PKIX_PL_ByteArray *byteArray = NULL;
104        void *buf = NULL;
105        PRFileDesc *certFile = NULL;
106        PKIX_UInt32 len;
107        SECItem certDER;
108        SECStatus rv;
109        /* default: NULL cert (failure case) */
110        PKIX_PL_Cert *cert = NULL;
111        char *pathName = NULL;
112
113        PKIX_TEST_STD_VARS();
114
115
116        certDER.data = NULL;
117
118        pathName = catDirName(dirName, certFileName, plContext);
119        certFile = PR_Open(pathName, PR_RDONLY, 0);
120
121        if (!certFile){
122                pkixTestErrorMsg = "Unable to open cert file";
123                goto cleanup;
124        } else {
125                rv = SECU_ReadDERFromFile(&certDER, certFile, PR_FALSE);
126                if (!rv){
127                        buf = (void *)certDER.data;
128                        len = certDER.len;
129
130                        PKIX_TEST_EXPECT_NO_ERROR
131                                (PKIX_PL_ByteArray_Create
132                                (buf, len, &byteArray, plContext));
133
134                        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_Create
135                                                (byteArray, &cert, plContext));
136
137                        SECITEM_FreeItem(&certDER, PR_FALSE);
138                } else {
139                        pkixTestErrorMsg = "Unable to read DER from cert file";
140                        goto cleanup;
141                }
142        }
143
144cleanup:
145
146        pkixTestErrorResult = PKIX_PL_Free(pathName, plContext);
147
148        if (certFile){
149                PR_Close(certFile);
150        }
151
152        if (PKIX_TEST_ERROR_RECEIVED){
153                SECITEM_FreeItem(&certDER, PR_FALSE);
154        }
155
156        PKIX_TEST_DECREF_AC(byteArray);
157
158        PKIX_TEST_RETURN();
159
160        return (cert);
161}
162
163PKIX_PL_CRL *
164createCRL(
165        char *dirName,
166        char *crlFileName,
167        void *plContext)
168{
169        PKIX_PL_ByteArray *byteArray = NULL;
170        PKIX_PL_CRL *crl = NULL;
171        PKIX_Error *error = NULL;
172        PRFileDesc *inFile = NULL;
173        SECItem crlDER;
174        void *buf = NULL;
175        PKIX_UInt32 len;
176        SECStatus rv;
177        char *pathName = NULL;
178
179        PKIX_TEST_STD_VARS();
180
181        crlDER.data = NULL;
182
183        pathName = catDirName(dirName, crlFileName, plContext);
184        inFile = PR_Open(pathName, PR_RDONLY, 0);
185
186        if (!inFile){
187                pkixTestErrorMsg = "Unable to open crl file";
188                goto cleanup;
189        } else {
190                rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE);
191                if (!rv){
192                        buf = (void *)crlDER.data;
193                        len = crlDER.len;
194
195                        error = PKIX_PL_ByteArray_Create
196                                (buf, len, &byteArray, plContext);
197
198                        if (error){
199                                pkixTestErrorMsg =
200                                        "PKIX_PL_ByteArray_Create failed";
201                                goto cleanup;
202                        }
203
204                        error = PKIX_PL_CRL_Create(byteArray, &crl, plContext);
205                        if (error){
206                                pkixTestErrorMsg = "PKIX_PL_Crl_Create failed";
207                                goto cleanup;
208                        }
209
210                        SECITEM_FreeItem(&crlDER, PR_FALSE);
211                } else {
212                        pkixTestErrorMsg = "Unable to read DER from crl file";
213                        goto cleanup;
214                }
215        }
216
217cleanup:
218
219        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(pathName, plContext));
220
221        if (inFile){
222                PR_Close(inFile);
223        }
224
225        if (error){
226                SECITEM_FreeItem(&crlDER, PR_FALSE);
227        }
228
229        PKIX_TEST_DECREF_AC(byteArray);
230
231        PKIX_TEST_RETURN();
232
233        return (crl);
234
235}
236
237PKIX_TrustAnchor *
238createTrustAnchor(
239        char *dirName,
240        char *certFileName,
241        PKIX_Boolean useCert,
242        void *plContext)
243{
244        PKIX_TrustAnchor *anchor = NULL;
245        PKIX_PL_Cert *cert = NULL;
246        PKIX_PL_X500Name *name = NULL;
247        PKIX_PL_PublicKey *pubKey = NULL;
248        PKIX_PL_CertNameConstraints *nameConstraints = NULL;
249
250        PKIX_TEST_STD_VARS();
251
252        cert = createCert(dirName, certFileName, plContext);
253
254        if (useCert){
255                PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_CreateWithCert
256                                        (cert, &anchor, plContext));
257        } else {
258                PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubject
259                                        (cert, &name, plContext));
260
261                if (name == NULL){
262                        goto cleanup;
263                }
264
265                PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubjectPublicKey
266                                        (cert, &pubKey, plContext));
267
268                PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetNameConstraints
269                                        (cert, &nameConstraints, NULL));
270
271                PKIX_TEST_EXPECT_NO_ERROR
272                        (PKIX_TrustAnchor_CreateWithNameKeyPair
273                        (name, pubKey, nameConstraints, &anchor, plContext));
274        }
275
276cleanup:
277
278        if (PKIX_TEST_ERROR_RECEIVED){
279                PKIX_TEST_DECREF_AC(anchor);
280        }
281
282        PKIX_TEST_DECREF_AC(cert);
283        PKIX_TEST_DECREF_AC(name);
284        PKIX_TEST_DECREF_AC(pubKey);
285        PKIX_TEST_DECREF_AC(nameConstraints);
286
287        PKIX_TEST_RETURN();
288
289        return (anchor);
290}
291
292PKIX_List *
293createCertChain(
294        char *dirName,
295        char *firstCertFileName,
296        char *secondCertFileName,
297        void *plContext)
298{
299        PKIX_PL_Cert *firstCert = NULL;
300        PKIX_PL_Cert *secondCert = NULL;
301        PKIX_List *certList = NULL;
302
303        PKIX_TEST_STD_VARS();
304
305        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&certList, plContext));
306
307        firstCert = createCert(dirName, firstCertFileName, plContext);
308
309        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
310                (certList, (PKIX_PL_Object *)firstCert, plContext));
311
312        if (secondCertFileName){
313                secondCert = createCert(dirName, secondCertFileName, plContext);
314
315                PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
316                        (certList, (PKIX_PL_Object *)secondCert, plContext));
317        }
318
319        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetImmutable
320                                    (certList, plContext));
321
322cleanup:
323
324        if (PKIX_TEST_ERROR_RECEIVED){
325                PKIX_TEST_DECREF_AC(certList);
326        }
327
328        PKIX_TEST_DECREF_AC(firstCert);
329        PKIX_TEST_DECREF_AC(secondCert);
330
331        PKIX_TEST_RETURN();
332
333        return (certList);
334}
335
336PKIX_List *
337createCertChainPlus(
338        char *dirName,
339        char *certNames[],
340        PKIX_PL_Cert *certs[],
341        PKIX_UInt32 numCerts,
342        void *plContext)
343{
344        PKIX_List *certList = NULL;
345        PKIX_UInt32 i;
346
347        PKIX_TEST_STD_VARS();
348
349        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&certList, plContext));
350
351        for (i = 0; i < numCerts; i++) {
352
353                certs[i] = createCert(dirName, certNames[i], plContext);
354
355                /* Create Cert may fail */
356                if (certs[i] == NULL) {
357                        PKIX_TEST_DECREF_BC(certList);
358                        goto cleanup;
359                }
360
361                PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
362                                            (certList,
363                                            (PKIX_PL_Object *)certs[i],
364                                            plContext));
365        }
366
367        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetImmutable
368                                    (certList, plContext));
369
370cleanup:
371
372        if (PKIX_TEST_ERROR_RECEIVED){
373                PKIX_TEST_DECREF_AC(certList);
374        }
375
376        for (i = 0; i < numCerts; i++) {
377                PKIX_TEST_DECREF_AC(certs[i]);
378        }
379
380        PKIX_TEST_RETURN();
381
382        return (certList);
383
384}
385
386PKIX_PL_Date *
387createDate(
388        char *asciiDate,
389        void *plContext)
390{
391        PKIX_PL_Date *date = NULL;
392        PKIX_PL_String *plString = NULL;
393
394        PKIX_TEST_STD_VARS();
395
396        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
397            (PKIX_ESCASCII, asciiDate, 0, &plString, plContext));
398
399        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Date_Create_UTCTime
400                                    (plString, &date, plContext));
401
402cleanup:
403
404        PKIX_TEST_DECREF_AC(plString);
405
406        PKIX_TEST_RETURN();
407
408        return (date);
409}
410
411PKIX_ProcessingParams *
412createProcessingParams(
413        char *dirName,
414        char *firstAnchorFileName,
415        char *secondAnchorFileName,
416        char *dateAscii,
417        PKIX_List *initialPolicies, /* List of PKIX_PL_OID */
418        PKIX_Boolean isCrlEnabled,
419        void *plContext)
420{
421
422        PKIX_TrustAnchor *firstAnchor = NULL;
423        PKIX_TrustAnchor *secondAnchor = NULL;
424        PKIX_List *anchorsList = NULL;
425        PKIX_ProcessingParams *procParams = NULL;
426        PKIX_PL_String *dateString = NULL;
427        PKIX_PL_Date *testDate = NULL;
428
429        PKIX_TEST_STD_VARS();
430
431        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&anchorsList, plContext));
432
433        firstAnchor = createTrustAnchor
434                (dirName, firstAnchorFileName, PKIX_FALSE, plContext);
435
436        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
437                                    (anchorsList,
438                                    (PKIX_PL_Object *)firstAnchor,
439                                    plContext));
440
441        if (secondAnchorFileName){
442                secondAnchor =
443                        createTrustAnchor
444                        (dirName, secondAnchorFileName, PKIX_FALSE, plContext);
445
446                PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
447                                            (anchorsList,
448                                            (PKIX_PL_Object *)secondAnchor,
449                                            plContext));
450        }
451
452        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_Create
453                                    (anchorsList, &procParams, plContext));
454
455        if (dateAscii){
456                PKIX_TEST_EXPECT_NO_ERROR
457                        (PKIX_PL_String_Create
458                        (PKIX_ESCASCII,
459                        dateAscii,
460                        0,
461                        &dateString,
462                        plContext));
463
464                PKIX_TEST_EXPECT_NO_ERROR
465                        (PKIX_PL_Date_Create_UTCTime
466                        (dateString, &testDate, plContext));
467
468                PKIX_TEST_EXPECT_NO_ERROR
469                        (PKIX_ProcessingParams_SetDate
470                        (procParams, testDate, plContext));
471        }
472
473        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetInitialPolicies
474                (procParams, initialPolicies, plContext));
475
476        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetRevocationEnabled
477                                    (procParams, isCrlEnabled, plContext));
478
479cleanup:
480
481        if (PKIX_TEST_ERROR_RECEIVED){
482                PKIX_TEST_DECREF_AC(procParams);
483        }
484
485        PKIX_TEST_DECREF_AC(dateString);
486        PKIX_TEST_DECREF_AC(testDate);
487        PKIX_TEST_DECREF_AC(anchorsList);
488        PKIX_TEST_DECREF_AC(firstAnchor);
489        PKIX_TEST_DECREF_AC(secondAnchor);
490
491        PKIX_TEST_RETURN();
492
493        return (procParams);
494}
495
496PKIX_ValidateParams *
497createValidateParams(
498        char *dirName,
499        char *firstAnchorFileName,
500        char *secondAnchorFileName,
501        char *dateAscii,
502        PKIX_List *initialPolicies, /* List of PKIX_PL_OID */
503        PKIX_Boolean initialPolicyMappingInhibit,
504        PKIX_Boolean initialAnyPolicyInhibit,
505        PKIX_Boolean initialExplicitPolicy,
506        PKIX_Boolean isCrlEnabled,
507        PKIX_List *chain,
508        void *plContext)
509{
510
511        PKIX_ProcessingParams *procParams = NULL;
512        PKIX_ValidateParams *valParams = NULL;
513
514        PKIX_TEST_STD_VARS();
515
516        procParams =
517                createProcessingParams
518                    (dirName,
519                    firstAnchorFileName,
520                    secondAnchorFileName,
521                    dateAscii,
522                    NULL,
523                    isCrlEnabled,
524                    plContext);
525
526        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetInitialPolicies
527                (procParams, initialPolicies, plContext));
528
529        PKIX_TEST_EXPECT_NO_ERROR
530                (PKIX_ProcessingParams_SetPolicyMappingInhibited
531                (procParams, initialPolicyMappingInhibit, NULL));
532
533        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetAnyPolicyInhibited
534                (procParams, initialAnyPolicyInhibit, NULL));
535
536        PKIX_TEST_EXPECT_NO_ERROR
537                (PKIX_ProcessingParams_SetExplicitPolicyRequired
538                (procParams, initialExplicitPolicy, NULL));
539
540        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_Create
541                (procParams, chain, &valParams, plContext));
542
543cleanup:
544
545        if (PKIX_TEST_ERROR_RECEIVED){
546                PKIX_TEST_DECREF_AC(valParams);
547        }
548
549        PKIX_TEST_DECREF_AC(procParams);
550
551        PKIX_TEST_RETURN();
552
553        return (valParams);
554}
555
556PKIX_ValidateResult *
557createValidateResult(
558        char *dirName,
559        char *anchorFileName,
560        char *pubKeyCertFileName,
561        void *plContext)
562{
563
564        PKIX_TrustAnchor *anchor = NULL;
565        PKIX_ValidateResult *valResult = NULL;
566        PKIX_PL_Cert *cert = NULL;
567        PKIX_PL_PublicKey *pubKey = NULL;
568
569        PKIX_TEST_STD_VARS();
570
571        anchor = createTrustAnchor
572                    (dirName, anchorFileName, PKIX_FALSE, plContext);
573        cert = createCert(dirName, pubKeyCertFileName, plContext);
574
575        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubjectPublicKey
576                                    (cert, &pubKey, plContext));
577
578        PKIX_TEST_EXPECT_NO_ERROR
579                (pkix_ValidateResult_Create
580                (pubKey, anchor, NULL, &valResult, plContext));
581
582cleanup:
583
584        if (PKIX_TEST_ERROR_RECEIVED){
585                PKIX_TEST_DECREF_AC(valResult);
586        }
587
588        PKIX_TEST_DECREF_AC(anchor);
589        PKIX_TEST_DECREF_AC(cert);
590        PKIX_TEST_DECREF_AC(pubKey);
591
592        PKIX_TEST_RETURN();
593
594        return (valResult);
595}
596
597PKIX_PL_GeneralName *
598createGeneralName(
599        PKIX_UInt32 nameType,
600        char *asciiName,
601        void *plContext)
602{
603
604        PKIX_PL_GeneralName *generalName = NULL;
605        PKIX_PL_String *plString = NULL;
606
607        PKIX_TEST_STD_VARS();
608
609        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
610            (PKIX_ESCASCII, asciiName, 0, &plString, plContext));
611
612        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_GeneralName_Create
613            (nameType, plString, &generalName, plContext));
614
615cleanup:
616
617        PKIX_TEST_DECREF_AC(plString);
618
619        PKIX_TEST_RETURN();
620
621        return (generalName);
622}
623
624PKIX_BuildResult *
625createBuildResult(
626        char *dirName,
627        char *anchorFileName,
628        char *pubKeyCertFileName,
629        char *firstChainCertFileName,
630        char *secondChainCertFileName,
631        void *plContext)
632{
633        PKIX_BuildResult *buildResult = NULL;
634        PKIX_ValidateResult *valResult = NULL;
635        PKIX_List *certChain = NULL;
636
637        PKIX_TEST_STD_VARS();
638
639        valResult = createValidateResult
640                (dirName, anchorFileName, pubKeyCertFileName, plContext);
641        certChain = createCertChain
642                        (dirName,
643                        firstChainCertFileName,
644                        secondChainCertFileName,
645                        plContext);
646
647        PKIX_TEST_EXPECT_NO_ERROR
648                (pkix_BuildResult_Create
649                (valResult, certChain, &buildResult, plContext));
650
651cleanup:
652
653        if (PKIX_TEST_ERROR_RECEIVED){
654                PKIX_TEST_DECREF_AC(buildResult);
655        }
656
657        PKIX_TEST_DECREF_AC(valResult);
658        PKIX_TEST_DECREF_AC(certChain);
659
660        PKIX_TEST_RETURN();
661
662        return (buildResult);
663}