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