dumpcrl.c (4191B)
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 void 27 printUsage(void) 28 { 29 (void)printf("\nUSAGE:\tdumpcrl <crlFile>\n"); 30 (void)printf("\tParses a CRL located at <crlFile> " 31 "and displays it.\n"); 32 } 33 34 static void 35 printFailure(char *msg) 36 { 37 (void)printf("FAILURE: %s\n", msg); 38 } 39 40 static PKIX_PL_CRL * 41 createCRL(char *inFileName) 42 { 43 PKIX_PL_ByteArray *byteArray = NULL; 44 PKIX_PL_CRL *crl = NULL; 45 PKIX_Error *error = NULL; 46 PRFileDesc *inFile = NULL; 47 SECItem crlDER; 48 void *buf = NULL; 49 PKIX_UInt32 len; 50 SECStatus rv; 51 52 PKIX_TEST_STD_VARS(); 53 54 crlDER.data = NULL; 55 56 inFile = PR_Open(inFileName, PR_RDONLY, 0); 57 58 if (!inFile) { 59 printFailure("Unable to open crl file"); 60 goto cleanup; 61 } else { 62 rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE, PR_FALSE); 63 if (!rv) { 64 buf = (void *)crlDER.data; 65 len = crlDER.len; 66 67 error = PKIX_PL_ByteArray_Create(buf, len, &byteArray, plContext); 68 69 if (error) { 70 printFailure("PKIX_PL_ByteArray_Create failed"); 71 goto cleanup; 72 } 73 74 error = PKIX_PL_CRL_Create(byteArray, &crl, plContext); 75 if (error) { 76 printFailure("PKIX_PL_CRL_Create failed"); 77 goto cleanup; 78 } 79 80 SECITEM_FreeItem(&crlDER, PR_FALSE); 81 } else { 82 printFailure("Unable to read DER from crl file"); 83 goto cleanup; 84 } 85 } 86 87 cleanup: 88 89 if (inFile) { 90 PR_Close(inFile); 91 } 92 93 if (error) { 94 SECITEM_FreeItem(&crlDER, PR_FALSE); 95 } 96 97 if (byteArray) { 98 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext); 99 } 100 101 PKIX_TEST_RETURN(); 102 103 return (crl); 104 } 105 106 int 107 dumpcrl(int argc, char *argv[]) 108 { 109 110 PKIX_PL_String *string = NULL; 111 PKIX_PL_CRL *crl = NULL; 112 PKIX_Error *error = NULL; 113 char *ascii = NULL; 114 PKIX_UInt32 length; 115 PKIX_UInt32 actualMinorVersion; 116 PKIX_UInt32 j = 0; 117 PKIX_Boolean useArenas = PKIX_FALSE; 118 119 PKIX_TEST_STD_VARS(); 120 121 if (argc == 1) { 122 printUsage(); 123 return (0); 124 } 125 126 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]); 127 128 PKIX_Initialize(PKIX_TRUE, /* nssInitNeeded */ 129 useArenas, 130 PKIX_MAJOR_VERSION, 131 PKIX_MINOR_VERSION, 132 PKIX_MINOR_VERSION, 133 &actualMinorVersion, 134 &plContext); 135 136 crl = createCRL(argv[j + 1]); 137 138 if (crl) { 139 140 error = PKIX_PL_Object_ToString((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(string, 149 PKIX_ESCASCII, 150 (void **)&ascii, 151 &length, 152 plContext); 153 if (error || !ascii) { 154 printFailure("Unable to get ASCII encoding of string"); 155 goto cleanup; 156 } 157 158 (void)printf("OUTPUT:\n%s\n", ascii); 159 160 } else { 161 printFailure("Unable to create CRL"); 162 goto cleanup; 163 } 164 165 cleanup: 166 167 if (crl) { 168 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(crl), plContext); 169 } 170 171 if (string) { 172 PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext); 173 } 174 175 if (ascii) { 176 PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext); 177 } 178 179 PKIX_Shutdown(plContext); 180 181 PKIX_TEST_RETURN(); 182 183 endTests("DUMPCRL"); 184 185 return (0); 186 }