2
* The contents of this file are subject to the Mozilla Public
3
* License Version 1.1 (the "License"); you may not use this file
4
* except in compliance with the License. You may obtain a copy of
5
* the License at http://www.mozilla.org/MPL/
7
* Software distributed under the License is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the License for the specific language governing
10
* rights and limitations under the License.
12
* The Original Code is the Netscape security libraries.
14
* The Initial Developer of the Original Code is Netscape
15
* Communications Corporation. Portions created by Netscape are
16
* Copyright (C) 1994-2000 Netscape Communications Corporation. All
21
* Alternatively, the contents of this file may be used under the
22
* terms of the GNU General Public License Version 2 or later (the
23
* "GPL"), in which case the provisions of the GPL are applicable
24
* instead of those above. If you wish to allow use of your
25
* version of this file only under the terms of the GPL and not to
26
* allow others to use your version of this file under the MPL,
27
* indicate your decision by deleting the provisions above and
28
* replace them with the notice and other provisions required by
29
* the GPL. If you do not delete the provisions above, a recipient
30
* may use your version of this file under either the MPL or the
35
* p7verify -- A command to do a verification of a *detached* pkcs7 signature.
37
* $Id: p7verify.c,v 1.8 2003/01/07 22:50:27 bishakhabanerjee%netscape.com Exp $
47
#include "sechash.h" /* for HASH_GetHashObject() */
57
#if (defined(XP_WIN) && !defined(WIN32)) || (defined(__sun) && !defined(SVR4))
58
extern int fread(char *, size_t, size_t, FILE*);
59
extern int fprintf(FILE *, char *, ...);
64
AlgorithmToHashType(SECAlgorithmID *digestAlgorithms)
69
tag = SECOID_GetAlgorithmTag(digestAlgorithms);
79
fprintf(stderr, "should never get here\n");
85
DigestFile(unsigned char *digest, unsigned int *len, unsigned int maxLen,
86
FILE *inFile, HASH_HashType hashType)
89
unsigned char ibuf[4096];
90
const SECHashObject *hashObj;
93
hashObj = HASH_GetHashObject(hashType);
95
hashcx = (* hashObj->create)();
99
(* hashObj->begin)(hashcx);
102
if (feof(inFile)) break;
103
nb = fread(ibuf, 1, sizeof(ibuf), inFile);
104
if (nb != sizeof(ibuf)) {
106
if (ferror(inFile)) {
107
PORT_SetError(SEC_ERROR_IO);
108
(* hashObj->destroy)(hashcx, PR_TRUE);
115
(* hashObj->update)(hashcx, ibuf, nb);
118
(* hashObj->end)(hashcx, digest, len, maxLen);
119
(* hashObj->destroy)(hashcx, PR_TRUE);
126
Usage(char *progName)
129
"Usage: %s -c content -s signature [-d dbdir] [-u certusage]\n",
131
fprintf(stderr, "%-20s content file that was signed\n",
133
fprintf(stderr, "%-20s file containing signature for that content\n",
136
"%-20s Key/Cert database directory (default is ~/.netscape)\n",
138
fprintf(stderr, "%-20s Define the type of certificate usage (default is certUsageEmailSigner)\n",
140
fprintf(stderr, "%-25s 0 - certUsageSSLClient\n", " ");
141
fprintf(stderr, "%-25s 1 - certUsageSSLServer\n", " ");
142
fprintf(stderr, "%-25s 2 - certUsageSSLServerWithStepUp\n", " ");
143
fprintf(stderr, "%-25s 3 - certUsageSSLCA\n", " ");
144
fprintf(stderr, "%-25s 4 - certUsageEmailSigner\n", " ");
145
fprintf(stderr, "%-25s 5 - certUsageEmailRecipient\n", " ");
146
fprintf(stderr, "%-25s 6 - certUsageObjectSigner\n", " ");
147
fprintf(stderr, "%-25s 7 - certUsageUserCertImport\n", " ");
148
fprintf(stderr, "%-25s 8 - certUsageVerifyCA\n", " ");
149
fprintf(stderr, "%-25s 9 - certUsageProtectedObjectSigner\n", " ");
150
fprintf(stderr, "%-25s 10 - certUsageStatusResponder\n", " ");
151
fprintf(stderr, "%-25s 11 - certUsageAnyCA\n", " ");
157
HashDecodeAndVerify(FILE *out, FILE *content, PRFileDesc *signature,
158
SECCertUsage usage, char *progName)
161
SEC_PKCS7ContentInfo *cinfo;
162
SEC_PKCS7SignedData *signedData;
163
HASH_HashType digestType;
165
unsigned char buffer[32];
167
if (SECU_ReadDERFromFile(&derdata, signature, PR_FALSE) != SECSuccess) {
168
SECU_PrintError(progName, "error reading signature file");
172
cinfo = SEC_PKCS7DecodeItem(&derdata, NULL, NULL, NULL, NULL,
177
if (! SEC_PKCS7ContentIsSigned(cinfo)) {
178
fprintf (out, "Signature file is pkcs7 data, but not signed.\n");
182
signedData = cinfo->content.signedData;
184
/* assume that there is only one digest algorithm for now */
185
digestType = AlgorithmToHashType(signedData->digestAlgorithms[0]);
186
if (digestType == HASH_AlgNULL) {
187
fprintf (out, "Invalid hash algorithmID\n");
191
digest.data = buffer;
192
if (DigestFile (digest.data, &digest.len, 32, content, digestType)) {
193
SECU_PrintError (progName, "problem computing message digest");
197
fprintf(out, "Signature is ");
198
if (SEC_PKCS7VerifyDetachedSignature (cinfo, usage, &digest, digestType,
200
fprintf(out, "valid.\n");
202
fprintf(out, "invalid (Reason: %s).\n",
203
SECU_Strerror(PORT_GetError()));
205
SEC_PKCS7DestroyContentInfo(cinfo);
211
main(int argc, char **argv)
214
FILE *contentFile, *outFile;
215
PRFileDesc *signatureFile;
216
SECCertUsage certUsage = certUsageEmailSigner;
217
PLOptState *optstate;
221
progName = strrchr(argv[0], '/');
222
progName = progName ? progName+1 : argv[0];
225
signatureFile = NULL;
229
* Parse command line arguments
231
optstate = PL_CreateOptState(argc, argv, "c:d:o:s:u:");
232
while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
233
switch (optstate->option) {
239
contentFile = fopen(optstate->value, "r");
241
fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
242
progName, optstate->value);
248
SECU_ConfigDirectory(optstate->value);
252
outFile = fopen(optstate->value, "w");
254
fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
255
progName, optstate->value);
261
signatureFile = PR_Open(optstate->value, PR_RDONLY, 0);
262
if (!signatureFile) {
263
fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
264
progName, optstate->value);
272
usageType = atoi (strdup(optstate->value));
273
if (usageType < certUsageSSLClient || usageType > certUsageAnyCA)
275
certUsage = (SECCertUsage)usageType;
282
if (!contentFile) Usage (progName);
283
if (!signatureFile) Usage (progName);
284
if (!outFile) outFile = stdout;
286
/* Call the libsec initialization routines */
287
PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
288
rv = NSS_Init(SECU_ConfigDirectory(NULL));
289
if (rv != SECSuccess) {
290
SECU_PrintPRandOSError(progName);
294
if (HashDecodeAndVerify(outFile, contentFile, signatureFile,
295
certUsage, progName)) {
296
SECU_PrintError(progName, "problem decoding/verifying signature");
300
if (NSS_Shutdown() != SECSuccess) {