|
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 */ |
|
10 |
|
11 #include <stdio.h> |
|
12 |
|
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" |
|
23 |
|
24 static void *plContext = NULL; |
|
25 |
|
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 } |
|
32 |
|
33 static |
|
34 void printFailure(char *msg){ |
|
35 (void) printf("FAILURE: %s\n", msg); |
|
36 } |
|
37 |
|
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; |
|
49 |
|
50 PKIX_TEST_STD_VARS(); |
|
51 |
|
52 crlDER.data = NULL; |
|
53 |
|
54 inFile = PR_Open(inFileName, PR_RDONLY, 0); |
|
55 |
|
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; |
|
64 |
|
65 error = PKIX_PL_ByteArray_Create |
|
66 (buf, len, &byteArray, plContext); |
|
67 |
|
68 if (error){ |
|
69 printFailure("PKIX_PL_ByteArray_Create failed"); |
|
70 goto cleanup; |
|
71 } |
|
72 |
|
73 error = PKIX_PL_CRL_Create(byteArray, &crl, plContext); |
|
74 if (error){ |
|
75 printFailure("PKIX_PL_CRL_Create failed"); |
|
76 goto cleanup; |
|
77 } |
|
78 |
|
79 SECITEM_FreeItem(&crlDER, PR_FALSE); |
|
80 } else { |
|
81 printFailure("Unable to read DER from crl file"); |
|
82 goto cleanup; |
|
83 } |
|
84 } |
|
85 |
|
86 cleanup: |
|
87 |
|
88 if (inFile){ |
|
89 PR_Close(inFile); |
|
90 } |
|
91 |
|
92 if (error){ |
|
93 SECITEM_FreeItem(&crlDER, PR_FALSE); |
|
94 } |
|
95 |
|
96 if (byteArray){ |
|
97 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext); |
|
98 } |
|
99 |
|
100 PKIX_TEST_RETURN(); |
|
101 |
|
102 return (crl); |
|
103 } |
|
104 |
|
105 int dumpcrl(int argc, char *argv[]) |
|
106 { |
|
107 |
|
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; |
|
116 |
|
117 PKIX_TEST_STD_VARS(); |
|
118 |
|
119 if (argc == 1){ |
|
120 printUsage(); |
|
121 return (0); |
|
122 } |
|
123 |
|
124 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]); |
|
125 |
|
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); |
|
134 |
|
135 crl = createCRL(argv[j+1]); |
|
136 |
|
137 if (crl){ |
|
138 |
|
139 error = PKIX_PL_Object_ToString |
|
140 ((PKIX_PL_Object *)crl, &string, plContext); |
|
141 |
|
142 if (error){ |
|
143 printFailure("Unable to get string representation " |
|
144 "of crl"); |
|
145 goto cleanup; |
|
146 } |
|
147 |
|
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 } |
|
158 |
|
159 (void) printf("OUTPUT:\n%s\n", ascii); |
|
160 |
|
161 } else { |
|
162 printFailure("Unable to create CRL"); |
|
163 goto cleanup; |
|
164 } |
|
165 |
|
166 cleanup: |
|
167 |
|
168 if (crl){ |
|
169 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(crl), plContext); |
|
170 } |
|
171 |
|
172 if (string){ |
|
173 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext); |
|
174 } |
|
175 |
|
176 if (ascii){ |
|
177 PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext); |
|
178 } |
|
179 |
|
180 PKIX_Shutdown(plContext); |
|
181 |
|
182 PKIX_TEST_RETURN(); |
|
183 |
|
184 endTests("DUMPCRL"); |
|
185 |
|
186 return (0); |
|
187 } |