Back to index

lightning-sunbird  0.9+nobinonly
p7verify.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * p7verify -- A command to do a verification of a *detached* pkcs7 signature.
00039  *
00040  * $Id: p7verify.c,v 1.9 2004/04/25 15:02:49 gerv%gerv.net Exp $
00041  */
00042 
00043 #include "nspr.h"
00044 #include "secutil.h"
00045 #include "plgetopt.h"
00046 #include "secpkcs7.h"
00047 #include "cert.h"
00048 #include "certdb.h"
00049 #include "secoid.h"
00050 #include "sechash.h" /* for HASH_GetHashObject() */
00051 #include "nss.h"
00052 
00053 #if defined(XP_UNIX)
00054 #include <unistd.h>
00055 #endif
00056 
00057 #include <stdio.h>
00058 #include <string.h>
00059 
00060 #if (defined(XP_WIN) && !defined(WIN32)) || (defined(__sun) && !defined(SVR4))
00061 extern int fread(char *, size_t, size_t, FILE*);
00062 extern int fprintf(FILE *, char *, ...);
00063 #endif
00064 
00065 
00066 static HASH_HashType
00067 AlgorithmToHashType(SECAlgorithmID *digestAlgorithms)
00068 {
00069 
00070     SECOidTag tag;
00071 
00072     tag = SECOID_GetAlgorithmTag(digestAlgorithms);
00073     
00074     switch (tag) {
00075       case SEC_OID_MD2:
00076        return HASH_AlgMD2;
00077       case SEC_OID_MD5:
00078        return HASH_AlgMD5;
00079       case SEC_OID_SHA1:
00080        return HASH_AlgSHA1;
00081       default:
00082        fprintf(stderr, "should never get here\n");
00083        return HASH_AlgNULL;
00084     }
00085 }
00086 
00087 static int
00088 DigestFile(unsigned char *digest, unsigned int *len, unsigned int maxLen,
00089           FILE *inFile, HASH_HashType hashType)
00090 {
00091     int nb;
00092     unsigned char ibuf[4096];
00093     const SECHashObject *hashObj;
00094     void *hashcx;
00095 
00096     hashObj = HASH_GetHashObject(hashType);
00097 
00098     hashcx = (* hashObj->create)();
00099     if (hashcx == NULL)
00100        return -1;
00101 
00102     (* hashObj->begin)(hashcx);
00103 
00104     for (;;) {
00105        if (feof(inFile)) break;
00106        nb = fread(ibuf, 1, sizeof(ibuf), inFile);
00107        if (nb != sizeof(ibuf)) {
00108            if (nb == 0) {
00109               if (ferror(inFile)) {
00110                   PORT_SetError(SEC_ERROR_IO);
00111                   (* hashObj->destroy)(hashcx, PR_TRUE);
00112                   return -1;
00113               }
00114               /* eof */
00115               break;
00116            }
00117        }
00118        (* hashObj->update)(hashcx, ibuf, nb);
00119     }
00120 
00121     (* hashObj->end)(hashcx, digest, len, maxLen);
00122     (* hashObj->destroy)(hashcx, PR_TRUE);
00123 
00124     return 0;
00125 }
00126 
00127 
00128 static void
00129 Usage(char *progName)
00130 {
00131     fprintf(stderr,
00132            "Usage:  %s -c content -s signature [-d dbdir] [-u certusage]\n",
00133            progName);
00134     fprintf(stderr, "%-20s content file that was signed\n",
00135            "-c content");
00136     fprintf(stderr, "%-20s file containing signature for that content\n",
00137            "-s signature");
00138     fprintf(stderr,
00139            "%-20s Key/Cert database directory (default is ~/.netscape)\n",
00140            "-d dbdir");
00141     fprintf(stderr, "%-20s Define the type of certificate usage (default is certUsageEmailSigner)\n",
00142            "-u certusage");
00143     fprintf(stderr, "%-25s  0 - certUsageSSLClient\n", " ");
00144     fprintf(stderr, "%-25s  1 - certUsageSSLServer\n", " ");
00145     fprintf(stderr, "%-25s  2 - certUsageSSLServerWithStepUp\n", " ");
00146     fprintf(stderr, "%-25s  3 - certUsageSSLCA\n", " ");
00147     fprintf(stderr, "%-25s  4 - certUsageEmailSigner\n", " ");
00148     fprintf(stderr, "%-25s  5 - certUsageEmailRecipient\n", " ");
00149     fprintf(stderr, "%-25s  6 - certUsageObjectSigner\n", " ");
00150     fprintf(stderr, "%-25s  7 - certUsageUserCertImport\n", " ");
00151     fprintf(stderr, "%-25s  8 - certUsageVerifyCA\n", " ");
00152     fprintf(stderr, "%-25s  9 - certUsageProtectedObjectSigner\n", " ");
00153     fprintf(stderr, "%-25s 10 - certUsageStatusResponder\n", " ");
00154     fprintf(stderr, "%-25s 11 - certUsageAnyCA\n", " ");
00155 
00156     exit(-1);
00157 }
00158 
00159 static int
00160 HashDecodeAndVerify(FILE *out, FILE *content, PRFileDesc *signature,
00161                   SECCertUsage usage, char *progName)
00162 {
00163     SECItem derdata;
00164     SEC_PKCS7ContentInfo *cinfo;
00165     SEC_PKCS7SignedData *signedData;
00166     HASH_HashType digestType;
00167     SECItem digest;
00168     unsigned char buffer[32];
00169 
00170     if (SECU_ReadDERFromFile(&derdata, signature, PR_FALSE) != SECSuccess) {
00171        SECU_PrintError(progName, "error reading signature file");
00172        return -1;
00173     }
00174 
00175     cinfo = SEC_PKCS7DecodeItem(&derdata, NULL, NULL, NULL, NULL,
00176                             NULL, NULL, NULL);
00177     if (cinfo == NULL)
00178        return -1;
00179 
00180     if (! SEC_PKCS7ContentIsSigned(cinfo)) {
00181        fprintf (out, "Signature file is pkcs7 data, but not signed.\n");
00182        return -1;
00183     }
00184 
00185     signedData = cinfo->content.signedData;
00186 
00187     /* assume that there is only one digest algorithm for now */
00188     digestType = AlgorithmToHashType(signedData->digestAlgorithms[0]);
00189     if (digestType == HASH_AlgNULL) {
00190        fprintf (out, "Invalid hash algorithmID\n");
00191        return -1;
00192     }
00193 
00194     digest.data = buffer;
00195     if (DigestFile (digest.data, &digest.len, 32, content, digestType)) {
00196        SECU_PrintError (progName, "problem computing message digest");
00197        return -1;
00198     }
00199 
00200     fprintf(out, "Signature is ");
00201     if (SEC_PKCS7VerifyDetachedSignature (cinfo, usage, &digest, digestType,
00202                                      PR_FALSE))
00203        fprintf(out, "valid.\n");
00204     else
00205        fprintf(out, "invalid (Reason: %s).\n",
00206               SECU_Strerror(PORT_GetError()));
00207 
00208     SEC_PKCS7DestroyContentInfo(cinfo);
00209     return 0;
00210 }
00211 
00212 
00213 int
00214 main(int argc, char **argv)
00215 {
00216     char *progName;
00217     FILE *contentFile, *outFile;
00218     PRFileDesc *signatureFile;
00219     SECCertUsage certUsage = certUsageEmailSigner;
00220     PLOptState *optstate;
00221     PLOptStatus status;
00222     SECStatus rv;
00223 
00224     progName = strrchr(argv[0], '/');
00225     progName = progName ? progName+1 : argv[0];
00226 
00227     contentFile = NULL;
00228     signatureFile = NULL;
00229     outFile = NULL;
00230 
00231     /*
00232      * Parse command line arguments
00233      */
00234     optstate = PL_CreateOptState(argc, argv, "c:d:o:s:u:");
00235     while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
00236        switch (optstate->option) {
00237          case '?':
00238            Usage(progName);
00239            break;
00240 
00241          case 'c':
00242            contentFile = fopen(optstate->value, "r");
00243            if (!contentFile) {
00244               fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
00245                      progName, optstate->value);
00246               return -1;
00247            }
00248            break;
00249 
00250          case 'd':
00251            SECU_ConfigDirectory(optstate->value);
00252            break;
00253 
00254          case 'o':
00255            outFile = fopen(optstate->value, "w");
00256            if (!outFile) {
00257               fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
00258                      progName, optstate->value);
00259               return -1;
00260            }
00261            break;
00262 
00263          case 's':
00264            signatureFile = PR_Open(optstate->value, PR_RDONLY, 0);
00265            if (!signatureFile) {
00266               fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
00267                      progName, optstate->value);
00268               return -1;
00269            }
00270            break;
00271 
00272          case 'u': {
00273            int usageType;
00274 
00275            usageType = atoi (strdup(optstate->value));
00276            if (usageType < certUsageSSLClient || usageType > certUsageAnyCA)
00277               return -1;
00278            certUsage = (SECCertUsage)usageType;
00279            break;
00280          }
00281              
00282        }
00283     }
00284 
00285     if (!contentFile) Usage (progName);
00286     if (!signatureFile) Usage (progName);
00287     if (!outFile) outFile = stdout;
00288 
00289     /* Call the libsec initialization routines */
00290     PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
00291     rv = NSS_Init(SECU_ConfigDirectory(NULL));
00292     if (rv != SECSuccess) {
00293        SECU_PrintPRandOSError(progName);
00294        return -1;
00295     }
00296 
00297     if (HashDecodeAndVerify(outFile, contentFile, signatureFile,
00298                          certUsage, progName)) {
00299        SECU_PrintError(progName, "problem decoding/verifying signature");
00300        return -1;
00301     }
00302 
00303     if (NSS_Shutdown() != SECSuccess) {
00304         exit(1);
00305     }
00306 
00307     return 0;
00308 }