Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 /*
5 * dumpcrl.c
6 *
7 * dump CRL sample application
8 *
9 */
11 #include <stdio.h>
13 #include "pkix.h"
14 #include "testutil.h"
15 #include "prlong.h"
16 #include "plstr.h"
17 #include "prthread.h"
18 #include "plarena.h"
19 #include "seccomon.h"
20 #include "secdert.h"
21 #include "secasn1t.h"
22 #include "certt.h"
24 static void *plContext = NULL;
26 static
27 void printUsage(void){
28 (void) printf("\nUSAGE:\tdumpcrl <crlFile>\n");
29 (void) printf("\tParses a CRL located at <crlFile> "
30 "and displays it.\n");
31 }
33 static
34 void printFailure(char *msg){
35 (void) printf("FAILURE: %s\n", msg);
36 }
38 static PKIX_PL_CRL *
39 createCRL(char *inFileName)
40 {
41 PKIX_PL_ByteArray *byteArray = NULL;
42 PKIX_PL_CRL *crl = NULL;
43 PKIX_Error *error = NULL;
44 PRFileDesc *inFile = NULL;
45 SECItem crlDER;
46 void *buf = NULL;
47 PKIX_UInt32 len;
48 SECStatus rv;
50 PKIX_TEST_STD_VARS();
52 crlDER.data = NULL;
54 inFile = PR_Open(inFileName, PR_RDONLY, 0);
56 if (!inFile){
57 printFailure("Unable to open crl file");
58 goto cleanup;
59 } else {
60 rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE, PR_FALSE);
61 if (!rv){
62 buf = (void *)crlDER.data;
63 len = crlDER.len;
65 error = PKIX_PL_ByteArray_Create
66 (buf, len, &byteArray, plContext);
68 if (error){
69 printFailure("PKIX_PL_ByteArray_Create failed");
70 goto cleanup;
71 }
73 error = PKIX_PL_CRL_Create(byteArray, &crl, plContext);
74 if (error){
75 printFailure("PKIX_PL_CRL_Create failed");
76 goto cleanup;
77 }
79 SECITEM_FreeItem(&crlDER, PR_FALSE);
80 } else {
81 printFailure("Unable to read DER from crl file");
82 goto cleanup;
83 }
84 }
86 cleanup:
88 if (inFile){
89 PR_Close(inFile);
90 }
92 if (error){
93 SECITEM_FreeItem(&crlDER, PR_FALSE);
94 }
96 if (byteArray){
97 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
98 }
100 PKIX_TEST_RETURN();
102 return (crl);
103 }
105 int dumpcrl(int argc, char *argv[])
106 {
108 PKIX_PL_String *string = NULL;
109 PKIX_PL_CRL *crl = NULL;
110 PKIX_Error *error = NULL;
111 char *ascii = NULL;
112 PKIX_UInt32 length;
113 PKIX_UInt32 actualMinorVersion;
114 PKIX_UInt32 j = 0;
115 PKIX_Boolean useArenas = PKIX_FALSE;
117 PKIX_TEST_STD_VARS();
119 if (argc == 1){
120 printUsage();
121 return (0);
122 }
124 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
126 PKIX_Initialize
127 (PKIX_TRUE, /* nssInitNeeded */
128 useArenas,
129 PKIX_MAJOR_VERSION,
130 PKIX_MINOR_VERSION,
131 PKIX_MINOR_VERSION,
132 &actualMinorVersion,
133 &plContext);
135 crl = createCRL(argv[j+1]);
137 if (crl){
139 error = PKIX_PL_Object_ToString
140 ((PKIX_PL_Object *)crl, &string, plContext);
142 if (error){
143 printFailure("Unable to get string representation "
144 "of crl");
145 goto cleanup;
146 }
148 error = PKIX_PL_String_GetEncoded
149 (string,
150 PKIX_ESCASCII,
151 (void **)&ascii,
152 &length,
153 plContext);
154 if (error || !ascii){
155 printFailure("Unable to get ASCII encoding of string");
156 goto cleanup;
157 }
159 (void) printf("OUTPUT:\n%s\n", ascii);
161 } else {
162 printFailure("Unable to create CRL");
163 goto cleanup;
164 }
166 cleanup:
168 if (crl){
169 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(crl), plContext);
170 }
172 if (string){
173 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
174 }
176 if (ascii){
177 PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
178 }
180 PKIX_Shutdown(plContext);
182 PKIX_TEST_RETURN();
184 endTests("DUMPCRL");
186 return (0);
187 }