Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
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 | /* |
michael@0 | 6 | * p7verify -- A command to do a verification of a *detached* pkcs7 signature. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | #include "nspr.h" |
michael@0 | 10 | #include "secutil.h" |
michael@0 | 11 | #include "plgetopt.h" |
michael@0 | 12 | #include "secpkcs7.h" |
michael@0 | 13 | #include "cert.h" |
michael@0 | 14 | #include "certdb.h" |
michael@0 | 15 | #include "secoid.h" |
michael@0 | 16 | #include "sechash.h" /* for HASH_GetHashObject() */ |
michael@0 | 17 | #include "nss.h" |
michael@0 | 18 | |
michael@0 | 19 | #if defined(XP_UNIX) |
michael@0 | 20 | #include <unistd.h> |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | #include <stdio.h> |
michael@0 | 24 | #include <string.h> |
michael@0 | 25 | |
michael@0 | 26 | #if (defined(XP_WIN) && !defined(WIN32)) || (defined(__sun) && !defined(SVR4)) |
michael@0 | 27 | extern int fread(char *, size_t, size_t, FILE*); |
michael@0 | 28 | extern int fprintf(FILE *, char *, ...); |
michael@0 | 29 | #endif |
michael@0 | 30 | |
michael@0 | 31 | |
michael@0 | 32 | static HASH_HashType |
michael@0 | 33 | AlgorithmToHashType(SECAlgorithmID *digestAlgorithms) |
michael@0 | 34 | { |
michael@0 | 35 | |
michael@0 | 36 | SECOidTag tag; |
michael@0 | 37 | |
michael@0 | 38 | tag = SECOID_GetAlgorithmTag(digestAlgorithms); |
michael@0 | 39 | |
michael@0 | 40 | switch (tag) { |
michael@0 | 41 | case SEC_OID_MD2: |
michael@0 | 42 | return HASH_AlgMD2; |
michael@0 | 43 | case SEC_OID_MD5: |
michael@0 | 44 | return HASH_AlgMD5; |
michael@0 | 45 | case SEC_OID_SHA1: |
michael@0 | 46 | return HASH_AlgSHA1; |
michael@0 | 47 | default: |
michael@0 | 48 | fprintf(stderr, "should never get here\n"); |
michael@0 | 49 | return HASH_AlgNULL; |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | static int |
michael@0 | 54 | DigestFile(unsigned char *digest, unsigned int *len, unsigned int maxLen, |
michael@0 | 55 | FILE *inFile, HASH_HashType hashType) |
michael@0 | 56 | { |
michael@0 | 57 | int nb; |
michael@0 | 58 | unsigned char ibuf[4096]; |
michael@0 | 59 | const SECHashObject *hashObj; |
michael@0 | 60 | void *hashcx; |
michael@0 | 61 | |
michael@0 | 62 | hashObj = HASH_GetHashObject(hashType); |
michael@0 | 63 | |
michael@0 | 64 | hashcx = (* hashObj->create)(); |
michael@0 | 65 | if (hashcx == NULL) |
michael@0 | 66 | return -1; |
michael@0 | 67 | |
michael@0 | 68 | (* hashObj->begin)(hashcx); |
michael@0 | 69 | |
michael@0 | 70 | for (;;) { |
michael@0 | 71 | if (feof(inFile)) break; |
michael@0 | 72 | nb = fread(ibuf, 1, sizeof(ibuf), inFile); |
michael@0 | 73 | if (nb != sizeof(ibuf)) { |
michael@0 | 74 | if (nb == 0) { |
michael@0 | 75 | if (ferror(inFile)) { |
michael@0 | 76 | PORT_SetError(SEC_ERROR_IO); |
michael@0 | 77 | (* hashObj->destroy)(hashcx, PR_TRUE); |
michael@0 | 78 | return -1; |
michael@0 | 79 | } |
michael@0 | 80 | /* eof */ |
michael@0 | 81 | break; |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | (* hashObj->update)(hashcx, ibuf, nb); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | (* hashObj->end)(hashcx, digest, len, maxLen); |
michael@0 | 88 | (* hashObj->destroy)(hashcx, PR_TRUE); |
michael@0 | 89 | |
michael@0 | 90 | return 0; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | |
michael@0 | 94 | static void |
michael@0 | 95 | Usage(char *progName) |
michael@0 | 96 | { |
michael@0 | 97 | fprintf(stderr, |
michael@0 | 98 | "Usage: %s -c content -s signature [-d dbdir] [-u certusage]\n", |
michael@0 | 99 | progName); |
michael@0 | 100 | fprintf(stderr, "%-20s content file that was signed\n", |
michael@0 | 101 | "-c content"); |
michael@0 | 102 | fprintf(stderr, "%-20s file containing signature for that content\n", |
michael@0 | 103 | "-s signature"); |
michael@0 | 104 | fprintf(stderr, |
michael@0 | 105 | "%-20s Key/Cert database directory (default is ~/.netscape)\n", |
michael@0 | 106 | "-d dbdir"); |
michael@0 | 107 | fprintf(stderr, "%-20s Define the type of certificate usage (default is certUsageEmailSigner)\n", |
michael@0 | 108 | "-u certusage"); |
michael@0 | 109 | fprintf(stderr, "%-25s 0 - certUsageSSLClient\n", " "); |
michael@0 | 110 | fprintf(stderr, "%-25s 1 - certUsageSSLServer\n", " "); |
michael@0 | 111 | fprintf(stderr, "%-25s 2 - certUsageSSLServerWithStepUp\n", " "); |
michael@0 | 112 | fprintf(stderr, "%-25s 3 - certUsageSSLCA\n", " "); |
michael@0 | 113 | fprintf(stderr, "%-25s 4 - certUsageEmailSigner\n", " "); |
michael@0 | 114 | fprintf(stderr, "%-25s 5 - certUsageEmailRecipient\n", " "); |
michael@0 | 115 | fprintf(stderr, "%-25s 6 - certUsageObjectSigner\n", " "); |
michael@0 | 116 | fprintf(stderr, "%-25s 7 - certUsageUserCertImport\n", " "); |
michael@0 | 117 | fprintf(stderr, "%-25s 8 - certUsageVerifyCA\n", " "); |
michael@0 | 118 | fprintf(stderr, "%-25s 9 - certUsageProtectedObjectSigner\n", " "); |
michael@0 | 119 | fprintf(stderr, "%-25s 10 - certUsageStatusResponder\n", " "); |
michael@0 | 120 | fprintf(stderr, "%-25s 11 - certUsageAnyCA\n", " "); |
michael@0 | 121 | |
michael@0 | 122 | exit(-1); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | static int |
michael@0 | 126 | HashDecodeAndVerify(FILE *out, FILE *content, PRFileDesc *signature, |
michael@0 | 127 | SECCertUsage usage, char *progName) |
michael@0 | 128 | { |
michael@0 | 129 | SECItem derdata; |
michael@0 | 130 | SEC_PKCS7ContentInfo *cinfo; |
michael@0 | 131 | SEC_PKCS7SignedData *signedData; |
michael@0 | 132 | HASH_HashType digestType; |
michael@0 | 133 | SECItem digest; |
michael@0 | 134 | unsigned char buffer[32]; |
michael@0 | 135 | |
michael@0 | 136 | if (SECU_ReadDERFromFile(&derdata, signature, PR_FALSE, |
michael@0 | 137 | PR_FALSE) != SECSuccess) { |
michael@0 | 138 | SECU_PrintError(progName, "error reading signature file"); |
michael@0 | 139 | return -1; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | cinfo = SEC_PKCS7DecodeItem(&derdata, NULL, NULL, NULL, NULL, |
michael@0 | 143 | NULL, NULL, NULL); |
michael@0 | 144 | if (cinfo == NULL) |
michael@0 | 145 | return -1; |
michael@0 | 146 | |
michael@0 | 147 | if (! SEC_PKCS7ContentIsSigned(cinfo)) { |
michael@0 | 148 | fprintf (out, "Signature file is pkcs7 data, but not signed.\n"); |
michael@0 | 149 | return -1; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | signedData = cinfo->content.signedData; |
michael@0 | 153 | |
michael@0 | 154 | /* assume that there is only one digest algorithm for now */ |
michael@0 | 155 | digestType = AlgorithmToHashType(signedData->digestAlgorithms[0]); |
michael@0 | 156 | if (digestType == HASH_AlgNULL) { |
michael@0 | 157 | fprintf (out, "Invalid hash algorithmID\n"); |
michael@0 | 158 | return -1; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | digest.data = buffer; |
michael@0 | 162 | if (DigestFile (digest.data, &digest.len, 32, content, digestType)) { |
michael@0 | 163 | SECU_PrintError (progName, "problem computing message digest"); |
michael@0 | 164 | return -1; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | fprintf(out, "Signature is "); |
michael@0 | 168 | if (SEC_PKCS7VerifyDetachedSignature (cinfo, usage, &digest, digestType, |
michael@0 | 169 | PR_FALSE)) |
michael@0 | 170 | fprintf(out, "valid.\n"); |
michael@0 | 171 | else |
michael@0 | 172 | fprintf(out, "invalid (Reason: %s).\n", |
michael@0 | 173 | SECU_Strerror(PORT_GetError())); |
michael@0 | 174 | |
michael@0 | 175 | SEC_PKCS7DestroyContentInfo(cinfo); |
michael@0 | 176 | return 0; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | |
michael@0 | 180 | int |
michael@0 | 181 | main(int argc, char **argv) |
michael@0 | 182 | { |
michael@0 | 183 | char *progName; |
michael@0 | 184 | FILE *contentFile, *outFile; |
michael@0 | 185 | PRFileDesc *signatureFile; |
michael@0 | 186 | SECCertUsage certUsage = certUsageEmailSigner; |
michael@0 | 187 | PLOptState *optstate; |
michael@0 | 188 | PLOptStatus status; |
michael@0 | 189 | SECStatus rv; |
michael@0 | 190 | |
michael@0 | 191 | progName = strrchr(argv[0], '/'); |
michael@0 | 192 | progName = progName ? progName+1 : argv[0]; |
michael@0 | 193 | |
michael@0 | 194 | contentFile = NULL; |
michael@0 | 195 | signatureFile = NULL; |
michael@0 | 196 | outFile = NULL; |
michael@0 | 197 | |
michael@0 | 198 | /* |
michael@0 | 199 | * Parse command line arguments |
michael@0 | 200 | */ |
michael@0 | 201 | optstate = PL_CreateOptState(argc, argv, "c:d:o:s:u:"); |
michael@0 | 202 | while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { |
michael@0 | 203 | switch (optstate->option) { |
michael@0 | 204 | case '?': |
michael@0 | 205 | Usage(progName); |
michael@0 | 206 | break; |
michael@0 | 207 | |
michael@0 | 208 | case 'c': |
michael@0 | 209 | contentFile = fopen(optstate->value, "r"); |
michael@0 | 210 | if (!contentFile) { |
michael@0 | 211 | fprintf(stderr, "%s: unable to open \"%s\" for reading\n", |
michael@0 | 212 | progName, optstate->value); |
michael@0 | 213 | return -1; |
michael@0 | 214 | } |
michael@0 | 215 | break; |
michael@0 | 216 | |
michael@0 | 217 | case 'd': |
michael@0 | 218 | SECU_ConfigDirectory(optstate->value); |
michael@0 | 219 | break; |
michael@0 | 220 | |
michael@0 | 221 | case 'o': |
michael@0 | 222 | outFile = fopen(optstate->value, "w"); |
michael@0 | 223 | if (!outFile) { |
michael@0 | 224 | fprintf(stderr, "%s: unable to open \"%s\" for writing\n", |
michael@0 | 225 | progName, optstate->value); |
michael@0 | 226 | return -1; |
michael@0 | 227 | } |
michael@0 | 228 | break; |
michael@0 | 229 | |
michael@0 | 230 | case 's': |
michael@0 | 231 | signatureFile = PR_Open(optstate->value, PR_RDONLY, 0); |
michael@0 | 232 | if (!signatureFile) { |
michael@0 | 233 | fprintf(stderr, "%s: unable to open \"%s\" for reading\n", |
michael@0 | 234 | progName, optstate->value); |
michael@0 | 235 | return -1; |
michael@0 | 236 | } |
michael@0 | 237 | break; |
michael@0 | 238 | |
michael@0 | 239 | case 'u': { |
michael@0 | 240 | int usageType; |
michael@0 | 241 | |
michael@0 | 242 | usageType = atoi (strdup(optstate->value)); |
michael@0 | 243 | if (usageType < certUsageSSLClient || usageType > certUsageAnyCA) |
michael@0 | 244 | return -1; |
michael@0 | 245 | certUsage = (SECCertUsage)usageType; |
michael@0 | 246 | break; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | if (!contentFile) Usage (progName); |
michael@0 | 253 | if (!signatureFile) Usage (progName); |
michael@0 | 254 | if (!outFile) outFile = stdout; |
michael@0 | 255 | |
michael@0 | 256 | /* Call the NSS initialization routines */ |
michael@0 | 257 | PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1); |
michael@0 | 258 | rv = NSS_Init(SECU_ConfigDirectory(NULL)); |
michael@0 | 259 | if (rv != SECSuccess) { |
michael@0 | 260 | SECU_PrintPRandOSError(progName); |
michael@0 | 261 | return -1; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | if (HashDecodeAndVerify(outFile, contentFile, signatureFile, |
michael@0 | 265 | certUsage, progName)) { |
michael@0 | 266 | SECU_PrintError(progName, "problem decoding/verifying signature"); |
michael@0 | 267 | return -1; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | if (NSS_Shutdown() != SECSuccess) { |
michael@0 | 271 | exit(1); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | return 0; |
michael@0 | 275 | } |