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 | #include "secutil.h" |
michael@0 | 6 | #include "secmod.h" |
michael@0 | 7 | #include "cert.h" |
michael@0 | 8 | #include "secoid.h" |
michael@0 | 9 | #include "nss.h" |
michael@0 | 10 | |
michael@0 | 11 | /* NSPR 2.0 header files */ |
michael@0 | 12 | #include "prinit.h" |
michael@0 | 13 | #include "prprf.h" |
michael@0 | 14 | #include "prsystem.h" |
michael@0 | 15 | #include "prmem.h" |
michael@0 | 16 | /* Portable layer header files */ |
michael@0 | 17 | #include "plstr.h" |
michael@0 | 18 | #include "sechash.h" /* for HASH_GetHashObject() */ |
michael@0 | 19 | |
michael@0 | 20 | static PRBool debugInfo; |
michael@0 | 21 | static PRBool verbose; |
michael@0 | 22 | static PRBool doVerify; |
michael@0 | 23 | static PRBool displayAll; |
michael@0 | 24 | |
michael@0 | 25 | static const char * const usageInfo[] = { |
michael@0 | 26 | "signver - verify a detached PKCS7 signature - Version " NSS_VERSION, |
michael@0 | 27 | "Commands:", |
michael@0 | 28 | " -A display all information from pkcs #7", |
michael@0 | 29 | " -V verify the signed object and display result", |
michael@0 | 30 | "Options:", |
michael@0 | 31 | " -a signature file is ASCII", |
michael@0 | 32 | " -d certdir directory containing cert database", |
michael@0 | 33 | " -i dataFileName input file containing signed data (default stdin)", |
michael@0 | 34 | " -o outputFileName output file name, default stdout", |
michael@0 | 35 | " -s signatureFileName input file for signature (default stdin)", |
michael@0 | 36 | " -v display verbose reason for failure" |
michael@0 | 37 | }; |
michael@0 | 38 | static int nUsageInfo = sizeof(usageInfo)/sizeof(char *); |
michael@0 | 39 | |
michael@0 | 40 | extern int SV_PrintPKCS7ContentInfo(FILE *, SECItem *); |
michael@0 | 41 | |
michael@0 | 42 | static void Usage(char *progName, FILE *outFile) |
michael@0 | 43 | { |
michael@0 | 44 | int i; |
michael@0 | 45 | fprintf(outFile, "Usage: %s [ commands ] options\n", progName); |
michael@0 | 46 | for (i = 0; i < nUsageInfo; i++) |
michael@0 | 47 | fprintf(outFile, "%s\n", usageInfo[i]); |
michael@0 | 48 | exit(-1); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | static HASH_HashType |
michael@0 | 52 | AlgorithmToHashType(SECAlgorithmID *digestAlgorithms) |
michael@0 | 53 | { |
michael@0 | 54 | SECOidTag tag = SECOID_GetAlgorithmTag(digestAlgorithms); |
michael@0 | 55 | HASH_HashType hash = HASH_GetHashTypeByOidTag(tag); |
michael@0 | 56 | return hash; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | |
michael@0 | 60 | static SECStatus |
michael@0 | 61 | DigestContent (SECItem * digest, SECItem * content, HASH_HashType hashType) |
michael@0 | 62 | { |
michael@0 | 63 | unsigned int maxLen = digest->len; |
michael@0 | 64 | unsigned int len = HASH_ResultLen(hashType); |
michael@0 | 65 | SECStatus rv; |
michael@0 | 66 | |
michael@0 | 67 | if (len > maxLen) { |
michael@0 | 68 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
michael@0 | 69 | return SECFailure; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | rv = HASH_HashBuf(hashType, digest->data, content->data, content->len); |
michael@0 | 73 | if (rv == SECSuccess) |
michael@0 | 74 | digest->len = len; |
michael@0 | 75 | return rv; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | enum { |
michael@0 | 79 | cmd_DisplayAllPCKS7Info = 0, |
michael@0 | 80 | cmd_VerifySignedObj |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | enum { |
michael@0 | 84 | opt_ASCII, |
michael@0 | 85 | opt_CertDir, |
michael@0 | 86 | opt_InputDataFile, |
michael@0 | 87 | opt_ItemNumber, |
michael@0 | 88 | opt_OutputFile, |
michael@0 | 89 | opt_InputSigFile, |
michael@0 | 90 | opt_PrintWhyFailure, |
michael@0 | 91 | opt_DebugInfo |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | static secuCommandFlag signver_commands[] = |
michael@0 | 95 | { |
michael@0 | 96 | { /* cmd_DisplayAllPCKS7Info*/ 'A', PR_FALSE, 0, PR_FALSE }, |
michael@0 | 97 | { /* cmd_VerifySignedObj */ 'V', PR_FALSE, 0, PR_FALSE } |
michael@0 | 98 | }; |
michael@0 | 99 | |
michael@0 | 100 | static secuCommandFlag signver_options[] = |
michael@0 | 101 | { |
michael@0 | 102 | { /* opt_ASCII */ 'a', PR_FALSE, 0, PR_FALSE }, |
michael@0 | 103 | { /* opt_CertDir */ 'd', PR_TRUE, 0, PR_FALSE }, |
michael@0 | 104 | { /* opt_InputDataFile */ 'i', PR_TRUE, 0, PR_FALSE }, |
michael@0 | 105 | { /* opt_OutputFile */ 'o', PR_TRUE, 0, PR_FALSE }, |
michael@0 | 106 | { /* opt_InputSigFile */ 's', PR_TRUE, 0, PR_FALSE }, |
michael@0 | 107 | { /* opt_PrintWhyFailure */ 'v', PR_FALSE, 0, PR_FALSE }, |
michael@0 | 108 | { /* opt_DebugInfo */ 0, PR_FALSE, 0, PR_FALSE, "debug" } |
michael@0 | 109 | }; |
michael@0 | 110 | |
michael@0 | 111 | int main(int argc, char **argv) |
michael@0 | 112 | { |
michael@0 | 113 | PRFileDesc *contentFile = NULL; |
michael@0 | 114 | PRFileDesc *signFile = PR_STDIN; |
michael@0 | 115 | FILE * outFile = stdout; |
michael@0 | 116 | char * progName; |
michael@0 | 117 | SECStatus rv; |
michael@0 | 118 | int result = 1; |
michael@0 | 119 | SECItem pkcs7der, content; |
michael@0 | 120 | secuCommand signver; |
michael@0 | 121 | |
michael@0 | 122 | pkcs7der.data = NULL; |
michael@0 | 123 | content.data = NULL; |
michael@0 | 124 | |
michael@0 | 125 | signver.numCommands = sizeof(signver_commands) /sizeof(secuCommandFlag); |
michael@0 | 126 | signver.numOptions = sizeof(signver_options) / sizeof(secuCommandFlag); |
michael@0 | 127 | signver.commands = signver_commands; |
michael@0 | 128 | signver.options = signver_options; |
michael@0 | 129 | |
michael@0 | 130 | #ifdef XP_PC |
michael@0 | 131 | progName = strrchr(argv[0], '\\'); |
michael@0 | 132 | #else |
michael@0 | 133 | progName = strrchr(argv[0], '/'); |
michael@0 | 134 | #endif |
michael@0 | 135 | progName = progName ? progName+1 : argv[0]; |
michael@0 | 136 | |
michael@0 | 137 | rv = SECU_ParseCommandLine(argc, argv, progName, &signver); |
michael@0 | 138 | if (SECSuccess != rv) { |
michael@0 | 139 | Usage(progName, outFile); |
michael@0 | 140 | } |
michael@0 | 141 | debugInfo = signver.options[opt_DebugInfo ].activated; |
michael@0 | 142 | verbose = signver.options[opt_PrintWhyFailure ].activated; |
michael@0 | 143 | doVerify = signver.commands[cmd_VerifySignedObj].activated; |
michael@0 | 144 | displayAll= signver.commands[cmd_DisplayAllPCKS7Info].activated; |
michael@0 | 145 | if (!doVerify && !displayAll) |
michael@0 | 146 | doVerify = PR_TRUE; |
michael@0 | 147 | |
michael@0 | 148 | /* Set the certdb directory (default is ~/.netscape) */ |
michael@0 | 149 | rv = NSS_Init(SECU_ConfigDirectory(signver.options[opt_CertDir].arg)); |
michael@0 | 150 | if (rv != SECSuccess) { |
michael@0 | 151 | SECU_PrintPRandOSError(progName); |
michael@0 | 152 | return result; |
michael@0 | 153 | } |
michael@0 | 154 | /* below here, goto cleanup */ |
michael@0 | 155 | SECU_RegisterDynamicOids(); |
michael@0 | 156 | |
michael@0 | 157 | /* Open the input content file. */ |
michael@0 | 158 | if (signver.options[opt_InputDataFile].activated && |
michael@0 | 159 | signver.options[opt_InputDataFile].arg) { |
michael@0 | 160 | if (PL_strcmp("-", signver.options[opt_InputDataFile].arg)) { |
michael@0 | 161 | contentFile = PR_Open(signver.options[opt_InputDataFile].arg, |
michael@0 | 162 | PR_RDONLY, 0); |
michael@0 | 163 | if (!contentFile) { |
michael@0 | 164 | PR_fprintf(PR_STDERR, |
michael@0 | 165 | "%s: unable to open \"%s\" for reading.\n", |
michael@0 | 166 | progName, signver.options[opt_InputDataFile].arg); |
michael@0 | 167 | goto cleanup; |
michael@0 | 168 | } |
michael@0 | 169 | } else |
michael@0 | 170 | contentFile = PR_STDIN; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | /* Open the input signature file. */ |
michael@0 | 174 | if (signver.options[opt_InputSigFile].activated && |
michael@0 | 175 | signver.options[opt_InputSigFile].arg) { |
michael@0 | 176 | if (PL_strcmp("-", signver.options[opt_InputSigFile].arg)) { |
michael@0 | 177 | signFile = PR_Open(signver.options[opt_InputSigFile].arg, |
michael@0 | 178 | PR_RDONLY, 0); |
michael@0 | 179 | if (!signFile) { |
michael@0 | 180 | PR_fprintf(PR_STDERR, |
michael@0 | 181 | "%s: unable to open \"%s\" for reading.\n", |
michael@0 | 182 | progName, signver.options[opt_InputSigFile].arg); |
michael@0 | 183 | goto cleanup; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | if (contentFile == PR_STDIN && signFile == PR_STDIN && doVerify) { |
michael@0 | 189 | PR_fprintf(PR_STDERR, |
michael@0 | 190 | "%s: cannot read both content and signature from standard input\n", |
michael@0 | 191 | progName); |
michael@0 | 192 | goto cleanup; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | /* Open|Create the output file. */ |
michael@0 | 196 | if (signver.options[opt_OutputFile].activated) { |
michael@0 | 197 | outFile = fopen(signver.options[opt_OutputFile].arg, "w"); |
michael@0 | 198 | if (!outFile) { |
michael@0 | 199 | PR_fprintf(PR_STDERR, "%s: unable to open \"%s\" for writing.\n", |
michael@0 | 200 | progName, signver.options[opt_OutputFile].arg); |
michael@0 | 201 | goto cleanup; |
michael@0 | 202 | } |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | /* read in the input files' contents */ |
michael@0 | 206 | rv = SECU_ReadDERFromFile(&pkcs7der, signFile, |
michael@0 | 207 | signver.options[opt_ASCII].activated, PR_FALSE); |
michael@0 | 208 | if (signFile != PR_STDIN) |
michael@0 | 209 | PR_Close(signFile); |
michael@0 | 210 | if (rv != SECSuccess) { |
michael@0 | 211 | SECU_PrintError(progName, "problem reading PKCS7 input"); |
michael@0 | 212 | goto cleanup; |
michael@0 | 213 | } |
michael@0 | 214 | if (contentFile) { |
michael@0 | 215 | rv = SECU_FileToItem(&content, contentFile); |
michael@0 | 216 | if (contentFile != PR_STDIN) |
michael@0 | 217 | PR_Close(contentFile); |
michael@0 | 218 | if (rv != SECSuccess) |
michael@0 | 219 | content.data = NULL; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | /* Signature Verification */ |
michael@0 | 223 | if (doVerify) { |
michael@0 | 224 | SEC_PKCS7ContentInfo *cinfo; |
michael@0 | 225 | SEC_PKCS7SignedData *signedData; |
michael@0 | 226 | HASH_HashType digestType; |
michael@0 | 227 | PRBool contentIsSigned; |
michael@0 | 228 | |
michael@0 | 229 | cinfo = SEC_PKCS7DecodeItem(&pkcs7der, NULL, NULL, NULL, NULL, |
michael@0 | 230 | NULL, NULL, NULL); |
michael@0 | 231 | if (cinfo == NULL) { |
michael@0 | 232 | PR_fprintf(PR_STDERR, "Unable to decode PKCS7 data\n"); |
michael@0 | 233 | goto cleanup; |
michael@0 | 234 | } |
michael@0 | 235 | /* below here, goto done */ |
michael@0 | 236 | |
michael@0 | 237 | contentIsSigned = SEC_PKCS7ContentIsSigned(cinfo); |
michael@0 | 238 | if (debugInfo) { |
michael@0 | 239 | PR_fprintf(PR_STDERR, "Content is%s encrypted.\n", |
michael@0 | 240 | SEC_PKCS7ContentIsEncrypted(cinfo) ? "" : " not"); |
michael@0 | 241 | } |
michael@0 | 242 | if (debugInfo || !contentIsSigned) { |
michael@0 | 243 | PR_fprintf(PR_STDERR, "Content is%s signed.\n", |
michael@0 | 244 | contentIsSigned ? "" : " not"); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | if (!contentIsSigned) |
michael@0 | 248 | goto done; |
michael@0 | 249 | |
michael@0 | 250 | signedData = cinfo->content.signedData; |
michael@0 | 251 | |
michael@0 | 252 | /* assume that there is only one digest algorithm for now */ |
michael@0 | 253 | digestType = AlgorithmToHashType(signedData->digestAlgorithms[0]); |
michael@0 | 254 | if (digestType == HASH_AlgNULL) { |
michael@0 | 255 | PR_fprintf(PR_STDERR, "Invalid hash algorithmID\n"); |
michael@0 | 256 | goto done; |
michael@0 | 257 | } |
michael@0 | 258 | if (content.data) { |
michael@0 | 259 | SECCertUsage usage = certUsageEmailSigner; |
michael@0 | 260 | SECItem digest; |
michael@0 | 261 | unsigned char digestBuffer[HASH_LENGTH_MAX]; |
michael@0 | 262 | |
michael@0 | 263 | if (debugInfo) |
michael@0 | 264 | PR_fprintf(PR_STDERR, "contentToVerify=%s\n", content.data); |
michael@0 | 265 | |
michael@0 | 266 | digest.data = digestBuffer; |
michael@0 | 267 | digest.len = sizeof digestBuffer; |
michael@0 | 268 | |
michael@0 | 269 | if (DigestContent(&digest, &content, digestType)) { |
michael@0 | 270 | SECU_PrintError(progName, "Message digest computation failure"); |
michael@0 | 271 | goto done; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | if (debugInfo) { |
michael@0 | 275 | unsigned int i; |
michael@0 | 276 | PR_fprintf(PR_STDERR, "Data Digest=:"); |
michael@0 | 277 | for (i = 0; i < digest.len; i++) |
michael@0 | 278 | PR_fprintf(PR_STDERR, "%02x:", digest.data[i]); |
michael@0 | 279 | PR_fprintf(PR_STDERR, "\n"); |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | fprintf(outFile, "signatureValid="); |
michael@0 | 283 | PORT_SetError(0); |
michael@0 | 284 | if (SEC_PKCS7VerifyDetachedSignature (cinfo, usage, |
michael@0 | 285 | &digest, digestType, PR_FALSE)) { |
michael@0 | 286 | fprintf(outFile, "yes"); |
michael@0 | 287 | } else { |
michael@0 | 288 | fprintf(outFile, "no"); |
michael@0 | 289 | if (verbose) { |
michael@0 | 290 | fprintf(outFile, ":%s", |
michael@0 | 291 | SECU_Strerror(PORT_GetError())); |
michael@0 | 292 | } |
michael@0 | 293 | } |
michael@0 | 294 | fprintf(outFile, "\n"); |
michael@0 | 295 | result = 0; |
michael@0 | 296 | } |
michael@0 | 297 | done: |
michael@0 | 298 | SEC_PKCS7DestroyContentInfo(cinfo); |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | if (displayAll) { |
michael@0 | 302 | if (SV_PrintPKCS7ContentInfo(outFile, &pkcs7der)) |
michael@0 | 303 | result = 1; |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | cleanup: |
michael@0 | 307 | SECITEM_FreeItem(&pkcs7der, PR_FALSE); |
michael@0 | 308 | SECITEM_FreeItem(&content, PR_FALSE); |
michael@0 | 309 | |
michael@0 | 310 | if (NSS_Shutdown() != SECSuccess) { |
michael@0 | 311 | result = 1; |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | return result; |
michael@0 | 315 | } |