Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / sample_apps / dumpcrl.c
blobbde913f66eb7b2498002bc666aea9b586b4425bf
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Sun Microsystems
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.
36 * ***** END LICENSE BLOCK ***** */
38 * dumpcrl.c
40 * dump CRL sample application
44 #include <stdio.h>
46 #include "pkix.h"
47 #include "testutil.h"
48 #include "prlong.h"
49 #include "plstr.h"
50 #include "prthread.h"
51 #include "plarena.h"
52 #include "seccomon.h"
53 #include "secdert.h"
54 #include "secasn1t.h"
55 #include "certt.h"
57 void *plContext = NULL;
59 void printUsage(void){
60 (void) printf("\nUSAGE:\tdumpcrl <crlFile>\n");
61 (void) printf("\tParses a CRL located at <crlFile> "
62 "and displays it.\n");
65 void printFailure(char *msg){
66 (void) printf("FAILURE: %s\n", msg);
69 PKIX_PL_CRL *
70 createCRL(char *inFileName)
72 PKIX_PL_ByteArray *byteArray = NULL;
73 PKIX_PL_CRL *crl = NULL;
74 PKIX_Error *error = NULL;
75 PRFileDesc *inFile = NULL;
76 SECItem crlDER;
77 void *buf = NULL;
78 PKIX_UInt32 len;
79 SECStatus rv;
81 PKIX_TEST_STD_VARS();
83 crlDER.data = NULL;
85 inFile = PR_Open(inFileName, PR_RDONLY, 0);
87 if (!inFile){
88 printFailure("Unable to open crl file");
89 goto cleanup;
90 } else {
91 rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE);
92 if (!rv){
93 buf = (void *)crlDER.data;
94 len = crlDER.len;
96 error = PKIX_PL_ByteArray_Create
97 (buf, len, &byteArray, plContext);
99 if (error){
100 printFailure("PKIX_PL_ByteArray_Create failed");
101 goto cleanup;
104 error = PKIX_PL_CRL_Create(byteArray, &crl, plContext);
105 if (error){
106 printFailure("PKIX_PL_CRL_Create failed");
107 goto cleanup;
110 SECITEM_FreeItem(&crlDER, PR_FALSE);
111 } else {
112 printFailure("Unable to read DER from crl file");
113 goto cleanup;
117 cleanup:
119 if (inFile){
120 PR_Close(inFile);
123 if (error){
124 SECITEM_FreeItem(&crlDER, PR_FALSE);
127 if (byteArray){
128 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext);
131 PKIX_TEST_RETURN();
133 return (crl);
136 int main(int argc, char *argv[])
139 PKIX_PL_String *string = NULL;
140 PKIX_PL_CRL *crl = NULL;
141 PKIX_Error *error = NULL;
142 char *ascii = NULL;
143 PKIX_UInt32 length;
144 PKIX_UInt32 actualMinorVersion;
145 PKIX_UInt32 j = 0;
146 PKIX_Boolean useArenas = PKIX_FALSE;
148 PKIX_TEST_STD_VARS();
150 if (argc == 1){
151 printUsage();
152 return (0);
155 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
157 PKIX_Initialize
158 (PKIX_TRUE, /* nssInitNeeded */
159 useArenas,
160 PKIX_MAJOR_VERSION,
161 PKIX_MINOR_VERSION,
162 PKIX_MINOR_VERSION,
163 &actualMinorVersion,
164 &plContext);
166 crl = createCRL(argv[j+1]);
168 if (crl){
170 error = PKIX_PL_Object_ToString
171 ((PKIX_PL_Object *)crl, &string, plContext);
173 if (error){
174 printFailure("Unable to get string representation "
175 "of crl");
176 goto cleanup;
179 error = PKIX_PL_String_GetEncoded
180 (string,
181 PKIX_ESCASCII,
182 (void **)&ascii,
183 &length,
184 plContext);
185 if (error || !ascii){
186 printFailure("Unable to get ASCII encoding of string");
187 goto cleanup;
190 (void) printf("OUTPUT:\n%s\n", ascii);
192 } else {
193 printFailure("Unable to create CRL");
194 goto cleanup;
197 cleanup:
199 if (crl){
200 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(crl), plContext);
203 if (string){
204 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext);
207 if (ascii){
208 PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext);
211 PKIX_Shutdown(plContext);
213 PKIX_TEST_RETURN();
215 endTests("DUMPCRL");
217 return (0);