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