PageRenderTime 39ms CodeModel.GetById 17ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/cmd/signver/signver.c

http://github.com/zpao/v8monkey
C | 347 lines | 260 code | 41 blank | 46 comment | 53 complexity | b536f1170063a6caa468a0732260d771 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37#include "secutil.h"
 38#include "secmod.h"
 39#include "cert.h"
 40#include "secoid.h"
 41#include "nss.h"
 42
 43/* NSPR 2.0 header files */
 44#include "prinit.h"
 45#include "prprf.h"
 46#include "prsystem.h"
 47#include "prmem.h"
 48/* Portable layer header files */
 49#include "plstr.h"
 50#include "sechash.h"	/* for HASH_GetHashObject() */
 51
 52static PRBool debugInfo;
 53static PRBool verbose;
 54static PRBool doVerify;
 55static PRBool displayAll;
 56
 57static const char * const usageInfo[] = {
 58    "signver - verify a detached PKCS7 signature - Version " NSS_VERSION,
 59    "Commands:",
 60    " -A		    display all information from pkcs #7",
 61    " -V		    verify the signed object and display result",
 62    "Options:",
 63    " -a		    signature file is ASCII",
 64    " -d certdir	    directory containing cert database",
 65    " -i dataFileName	    input file containing signed data (default stdin)",
 66    " -o outputFileName     output file name, default stdout",
 67    " -s signatureFileName  input file for signature (default stdin)",
 68    " -v		    display verbose reason for failure"
 69};
 70static int nUsageInfo = sizeof(usageInfo)/sizeof(char *);
 71
 72extern int SV_PrintPKCS7ContentInfo(FILE *, SECItem *);
 73
 74static void Usage(char *progName, FILE *outFile)
 75{
 76    int i;
 77    fprintf(outFile, "Usage:  %s [ commands ] options\n", progName);
 78    for (i = 0; i < nUsageInfo; i++)
 79	fprintf(outFile, "%s\n", usageInfo[i]);
 80    exit(-1);
 81}
 82
 83static HASH_HashType
 84AlgorithmToHashType(SECAlgorithmID *digestAlgorithms)
 85{
 86    SECOidTag	  tag  = SECOID_GetAlgorithmTag(digestAlgorithms);
 87    HASH_HashType hash = HASH_GetHashTypeByOidTag(tag);
 88    return hash;
 89}
 90
 91
 92static SECStatus
 93DigestContent (SECItem * digest, SECItem * content, HASH_HashType hashType)
 94{
 95    unsigned int maxLen = digest->len;
 96    unsigned int len	= HASH_ResultLen(hashType);
 97    SECStatus	 rv;
 98
 99    if (len > maxLen) {
100	PORT_SetError(SEC_ERROR_OUTPUT_LEN);
101	return SECFailure;
102    }
103
104    rv = HASH_HashBuf(hashType, digest->data, content->data, content->len);
105    if (rv == SECSuccess)
106	digest->len = len;
107    return rv;
108}
109
110enum {
111    cmd_DisplayAllPCKS7Info = 0,
112    cmd_VerifySignedObj
113};
114
115enum {
116    opt_ASCII,
117    opt_CertDir,
118    opt_InputDataFile,
119    opt_ItemNumber,
120    opt_OutputFile,
121    opt_InputSigFile,
122    opt_PrintWhyFailure,
123    opt_DebugInfo
124};
125
126static secuCommandFlag signver_commands[] =
127{
128    { /* cmd_DisplayAllPCKS7Info*/  'A', PR_FALSE, 0, PR_FALSE },
129    { /* cmd_VerifySignedObj	*/  'V', PR_FALSE, 0, PR_FALSE }
130};
131
132static secuCommandFlag signver_options[] =
133{
134    { /* opt_ASCII		*/  'a', PR_FALSE, 0, PR_FALSE },
135    { /* opt_CertDir		*/  'd', PR_TRUE,  0, PR_FALSE },
136    { /* opt_InputDataFile	*/  'i', PR_TRUE,  0, PR_FALSE },
137    { /* opt_OutputFile 	*/  'o', PR_TRUE,  0, PR_FALSE },
138    { /* opt_InputSigFile	*/  's', PR_TRUE,  0, PR_FALSE },
139    { /* opt_PrintWhyFailure	*/  'v', PR_FALSE, 0, PR_FALSE },
140    { /* opt_DebugInfo		*/    0, PR_FALSE, 0, PR_FALSE, "debug" }
141};
142
143int main(int argc, char **argv)
144{
145    PRFileDesc *contentFile = NULL;
146    PRFileDesc *signFile = PR_STDIN;
147    FILE *	outFile  = stdout;
148    char *	progName;
149    SECStatus	rv;
150    int 	result	 = 1;
151    SECItem	pkcs7der, content;
152    secuCommand signver;
153
154    pkcs7der.data  = NULL;
155    content.data = NULL;
156
157    signver.numCommands = sizeof(signver_commands) /sizeof(secuCommandFlag);
158    signver.numOptions = sizeof(signver_options) / sizeof(secuCommandFlag);
159    signver.commands = signver_commands;
160    signver.options = signver_options;
161
162#ifdef XP_PC
163    progName = strrchr(argv[0], '\\');
164#else
165    progName = strrchr(argv[0], '/');
166#endif
167    progName = progName ? progName+1 : argv[0];
168
169    rv = SECU_ParseCommandLine(argc, argv, progName, &signver);
170    if (SECSuccess != rv) {
171	Usage(progName, outFile);
172    }
173    debugInfo = signver.options[opt_DebugInfo	    ].activated;
174    verbose   = signver.options[opt_PrintWhyFailure ].activated;
175    doVerify  = signver.commands[cmd_VerifySignedObj].activated;
176    displayAll= signver.commands[cmd_DisplayAllPCKS7Info].activated;
177    if (!doVerify && !displayAll)
178	doVerify = PR_TRUE;
179
180    /*	Set the certdb directory (default is ~/.netscape) */
181    rv = NSS_Init(SECU_ConfigDirectory(signver.options[opt_CertDir].arg));
182    if (rv != SECSuccess) {
183	SECU_PrintPRandOSError(progName);
184	return result;
185    }
186    /* below here, goto cleanup */
187    SECU_RegisterDynamicOids();
188
189    /*	Open the input content file. */
190    if (signver.options[opt_InputDataFile].activated &&
191	signver.options[opt_InputDataFile].arg) {
192	if (PL_strcmp("-", signver.options[opt_InputDataFile].arg)) {
193	    contentFile = PR_Open(signver.options[opt_InputDataFile].arg,
194			       PR_RDONLY, 0);
195	    if (!contentFile) {
196		PR_fprintf(PR_STDERR,
197			   "%s: unable to open \"%s\" for reading.\n",
198			   progName, signver.options[opt_InputDataFile].arg);
199		goto cleanup;
200	    }
201	} else
202	    contentFile = PR_STDIN;
203    }
204
205    /*	Open the input signature file.	*/
206    if (signver.options[opt_InputSigFile].activated &&
207	signver.options[opt_InputSigFile].arg) {
208	if (PL_strcmp("-", signver.options[opt_InputSigFile].arg)) {
209	    signFile = PR_Open(signver.options[opt_InputSigFile].arg,
210			       PR_RDONLY, 0);
211	    if (!signFile) {
212		PR_fprintf(PR_STDERR,
213			   "%s: unable to open \"%s\" for reading.\n",
214			   progName, signver.options[opt_InputSigFile].arg);
215		goto cleanup;
216	    }
217	}
218    }
219
220    if (contentFile == PR_STDIN && signFile == PR_STDIN && doVerify) {
221	PR_fprintf(PR_STDERR,
222	    "%s: cannot read both content and signature from standard input\n",
223		   progName);
224	goto cleanup;
225    }
226
227    /*	Open|Create the output file.  */
228    if (signver.options[opt_OutputFile].activated) {
229	outFile = fopen(signver.options[opt_OutputFile].arg, "w");
230	if (!outFile) {
231	    PR_fprintf(PR_STDERR, "%s: unable to open \"%s\" for writing.\n",
232		       progName, signver.options[opt_OutputFile].arg);
233	    goto cleanup;
234	}
235    }
236
237    /* read in the input files' contents */
238    rv = SECU_ReadDERFromFile(&pkcs7der, signFile,
239			      signver.options[opt_ASCII].activated);
240    if (signFile != PR_STDIN)
241	PR_Close(signFile);
242    if (rv != SECSuccess) {
243	SECU_PrintError(progName, "problem reading PKCS7 input");
244	goto cleanup;
245    }
246    if (contentFile) {
247	rv = SECU_FileToItem(&content, contentFile);
248	if (contentFile != PR_STDIN)
249	    PR_Close(contentFile);
250	if (rv != SECSuccess)
251	    content.data = NULL;
252    }
253
254    /* Signature Verification */
255    if (doVerify) {
256	SEC_PKCS7ContentInfo *cinfo;
257	SEC_PKCS7SignedData *signedData;
258	HASH_HashType digestType;
259	PRBool contentIsSigned;
260
261	cinfo = SEC_PKCS7DecodeItem(&pkcs7der, NULL, NULL, NULL, NULL,
262				    NULL, NULL, NULL);
263	if (cinfo == NULL) {
264	    PR_fprintf(PR_STDERR, "Unable to decode PKCS7 data\n");
265	    goto cleanup;
266	}
267	/* below here, goto done */
268
269	contentIsSigned = SEC_PKCS7ContentIsSigned(cinfo);
270	if (debugInfo) {
271	    PR_fprintf(PR_STDERR, "Content is%s encrypted.\n",
272		       SEC_PKCS7ContentIsEncrypted(cinfo) ? "" : " not");
273	}
274	if (debugInfo || !contentIsSigned) {
275	    PR_fprintf(PR_STDERR, "Content is%s signed.\n",
276		       contentIsSigned ? "" : " not");
277	}
278
279	if (!contentIsSigned)
280	    goto done;
281
282	signedData = cinfo->content.signedData;
283
284	/* assume that there is only one digest algorithm for now */
285	digestType = AlgorithmToHashType(signedData->digestAlgorithms[0]);
286	if (digestType == HASH_AlgNULL) {
287	    PR_fprintf(PR_STDERR, "Invalid hash algorithmID\n");
288	    goto done;
289	}
290	if (content.data) {
291	    SECCertUsage   usage = certUsageEmailSigner;
292	    SECItem	   digest;
293	    unsigned char  digestBuffer[HASH_LENGTH_MAX];
294
295	    if (debugInfo)
296		PR_fprintf(PR_STDERR, "contentToVerify=%s\n", content.data);
297
298	    digest.data = digestBuffer;
299	    digest.len	= sizeof digestBuffer;
300
301	    if (DigestContent(&digest, &content, digestType)) {
302		SECU_PrintError(progName, "Message digest computation failure");
303		goto done;
304	    }
305
306	    if (debugInfo) {
307		unsigned int i;
308		PR_fprintf(PR_STDERR, "Data Digest=:");
309		for (i = 0; i < digest.len; i++)
310		    PR_fprintf(PR_STDERR, "%02x:", digest.data[i]);
311		PR_fprintf(PR_STDERR, "\n");
312	    }
313
314	    fprintf(outFile, "signatureValid=");
315	    PORT_SetError(0);
316	    if (SEC_PKCS7VerifyDetachedSignature (cinfo, usage,
317				   &digest, digestType, PR_FALSE)) {
318		fprintf(outFile, "yes");
319	    } else {
320		fprintf(outFile, "no");
321		if (verbose) {
322		    fprintf(outFile, ":%s",
323			    SECU_Strerror(PORT_GetError()));
324		}
325	    }
326	    fprintf(outFile, "\n");
327	    result = 0;
328	}
329done:
330	SEC_PKCS7DestroyContentInfo(cinfo);
331    }
332
333    if (displayAll) {
334	if (SV_PrintPKCS7ContentInfo(outFile, &pkcs7der))
335	    result = 1;
336    }
337
338cleanup:
339    SECITEM_FreeItem(&pkcs7der, PR_FALSE);
340    SECITEM_FreeItem(&content, PR_FALSE);
341
342    if (NSS_Shutdown() != SECSuccess) {
343	result = 1;
344    }
345
346    return result;
347}