security/nss/cmd/libpkix/sample_apps/dumpcrl.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial