~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/security/nss/cmd/p7verify/p7verify.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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/
 
6
 * 
 
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.
 
11
 * 
 
12
 * The Original Code is the Netscape security libraries.
 
13
 * 
 
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
 
17
 * Rights Reserved.
 
18
 * 
 
19
 * Contributor(s):
 
20
 * 
 
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
 
31
 * GPL.
 
32
 */
 
33
 
 
34
/*
 
35
 * p7verify -- A command to do a verification of a *detached* pkcs7 signature.
 
36
 *
 
37
 * $Id: p7verify.c,v 1.8 2003/01/07 22:50:27 bishakhabanerjee%netscape.com Exp $
 
38
 */
 
39
 
 
40
#include "nspr.h"
 
41
#include "secutil.h"
 
42
#include "plgetopt.h"
 
43
#include "secpkcs7.h"
 
44
#include "cert.h"
 
45
#include "certdb.h"
 
46
#include "secoid.h"
 
47
#include "sechash.h"    /* for HASH_GetHashObject() */
 
48
#include "nss.h"
 
49
 
 
50
#if defined(XP_UNIX)
 
51
#include <unistd.h>
 
52
#endif
 
53
 
 
54
#include <stdio.h>
 
55
#include <string.h>
 
56
 
 
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 *, ...);
 
60
#endif
 
61
 
 
62
 
 
63
static HASH_HashType
 
64
AlgorithmToHashType(SECAlgorithmID *digestAlgorithms)
 
65
{
 
66
 
 
67
    SECOidTag tag;
 
68
 
 
69
    tag = SECOID_GetAlgorithmTag(digestAlgorithms);
 
70
    
 
71
    switch (tag) {
 
72
      case SEC_OID_MD2:
 
73
        return HASH_AlgMD2;
 
74
      case SEC_OID_MD5:
 
75
        return HASH_AlgMD5;
 
76
      case SEC_OID_SHA1:
 
77
        return HASH_AlgSHA1;
 
78
      default:
 
79
        fprintf(stderr, "should never get here\n");
 
80
        return HASH_AlgNULL;
 
81
    }
 
82
}
 
83
 
 
84
static int
 
85
DigestFile(unsigned char *digest, unsigned int *len, unsigned int maxLen,
 
86
           FILE *inFile, HASH_HashType hashType)
 
87
{
 
88
    int nb;
 
89
    unsigned char ibuf[4096];
 
90
    const SECHashObject *hashObj;
 
91
    void *hashcx;
 
92
 
 
93
    hashObj = HASH_GetHashObject(hashType);
 
94
 
 
95
    hashcx = (* hashObj->create)();
 
96
    if (hashcx == NULL)
 
97
        return -1;
 
98
 
 
99
    (* hashObj->begin)(hashcx);
 
100
 
 
101
    for (;;) {
 
102
        if (feof(inFile)) break;
 
103
        nb = fread(ibuf, 1, sizeof(ibuf), inFile);
 
104
        if (nb != sizeof(ibuf)) {
 
105
            if (nb == 0) {
 
106
                if (ferror(inFile)) {
 
107
                    PORT_SetError(SEC_ERROR_IO);
 
108
                    (* hashObj->destroy)(hashcx, PR_TRUE);
 
109
                    return -1;
 
110
                }
 
111
                /* eof */
 
112
                break;
 
113
            }
 
114
        }
 
115
        (* hashObj->update)(hashcx, ibuf, nb);
 
116
    }
 
117
 
 
118
    (* hashObj->end)(hashcx, digest, len, maxLen);
 
119
    (* hashObj->destroy)(hashcx, PR_TRUE);
 
120
 
 
121
    return 0;
 
122
}
 
123
 
 
124
 
 
125
static void
 
126
Usage(char *progName)
 
127
{
 
128
    fprintf(stderr,
 
129
            "Usage:  %s -c content -s signature [-d dbdir] [-u certusage]\n",
 
130
            progName);
 
131
    fprintf(stderr, "%-20s content file that was signed\n",
 
132
            "-c content");
 
133
    fprintf(stderr, "%-20s file containing signature for that content\n",
 
134
            "-s signature");
 
135
    fprintf(stderr,
 
136
            "%-20s Key/Cert database directory (default is ~/.netscape)\n",
 
137
            "-d dbdir");
 
138
    fprintf(stderr, "%-20s Define the type of certificate usage (default is certUsageEmailSigner)\n",
 
139
            "-u certusage");
 
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", " ");
 
152
 
 
153
    exit(-1);
 
154
}
 
155
 
 
156
static int
 
157
HashDecodeAndVerify(FILE *out, FILE *content, PRFileDesc *signature,
 
158
                    SECCertUsage usage, char *progName)
 
159
{
 
160
    SECItem derdata;
 
161
    SEC_PKCS7ContentInfo *cinfo;
 
162
    SEC_PKCS7SignedData *signedData;
 
163
    HASH_HashType digestType;
 
164
    SECItem digest;
 
165
    unsigned char buffer[32];
 
166
 
 
167
    if (SECU_ReadDERFromFile(&derdata, signature, PR_FALSE) != SECSuccess) {
 
168
        SECU_PrintError(progName, "error reading signature file");
 
169
        return -1;
 
170
    }
 
171
 
 
172
    cinfo = SEC_PKCS7DecodeItem(&derdata, NULL, NULL, NULL, NULL,
 
173
                                NULL, NULL, NULL);
 
174
    if (cinfo == NULL)
 
175
        return -1;
 
176
 
 
177
    if (! SEC_PKCS7ContentIsSigned(cinfo)) {
 
178
        fprintf (out, "Signature file is pkcs7 data, but not signed.\n");
 
179
        return -1;
 
180
    }
 
181
 
 
182
    signedData = cinfo->content.signedData;
 
183
 
 
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");
 
188
        return -1;
 
189
    }
 
190
 
 
191
    digest.data = buffer;
 
192
    if (DigestFile (digest.data, &digest.len, 32, content, digestType)) {
 
193
        SECU_PrintError (progName, "problem computing message digest");
 
194
        return -1;
 
195
    }
 
196
 
 
197
    fprintf(out, "Signature is ");
 
198
    if (SEC_PKCS7VerifyDetachedSignature (cinfo, usage, &digest, digestType,
 
199
                                          PR_FALSE))
 
200
        fprintf(out, "valid.\n");
 
201
    else
 
202
        fprintf(out, "invalid (Reason: %s).\n",
 
203
                SECU_Strerror(PORT_GetError()));
 
204
 
 
205
    SEC_PKCS7DestroyContentInfo(cinfo);
 
206
    return 0;
 
207
}
 
208
 
 
209
 
 
210
int
 
211
main(int argc, char **argv)
 
212
{
 
213
    char *progName;
 
214
    FILE *contentFile, *outFile;
 
215
    PRFileDesc *signatureFile;
 
216
    SECCertUsage certUsage = certUsageEmailSigner;
 
217
    PLOptState *optstate;
 
218
    PLOptStatus status;
 
219
    SECStatus rv;
 
220
 
 
221
    progName = strrchr(argv[0], '/');
 
222
    progName = progName ? progName+1 : argv[0];
 
223
 
 
224
    contentFile = NULL;
 
225
    signatureFile = NULL;
 
226
    outFile = NULL;
 
227
 
 
228
    /*
 
229
     * Parse command line arguments
 
230
     */
 
231
    optstate = PL_CreateOptState(argc, argv, "c:d:o:s:u:");
 
232
    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
 
233
        switch (optstate->option) {
 
234
          case '?':
 
235
            Usage(progName);
 
236
            break;
 
237
 
 
238
          case 'c':
 
239
            contentFile = fopen(optstate->value, "r");
 
240
            if (!contentFile) {
 
241
                fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
 
242
                        progName, optstate->value);
 
243
                return -1;
 
244
            }
 
245
            break;
 
246
 
 
247
          case 'd':
 
248
            SECU_ConfigDirectory(optstate->value);
 
249
            break;
 
250
 
 
251
          case 'o':
 
252
            outFile = fopen(optstate->value, "w");
 
253
            if (!outFile) {
 
254
                fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
 
255
                        progName, optstate->value);
 
256
                return -1;
 
257
            }
 
258
            break;
 
259
 
 
260
          case 's':
 
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);
 
265
                return -1;
 
266
            }
 
267
            break;
 
268
 
 
269
          case 'u': {
 
270
            int usageType;
 
271
 
 
272
            usageType = atoi (strdup(optstate->value));
 
273
            if (usageType < certUsageSSLClient || usageType > certUsageAnyCA)
 
274
                return -1;
 
275
            certUsage = (SECCertUsage)usageType;
 
276
            break;
 
277
          }
 
278
              
 
279
        }
 
280
    }
 
281
 
 
282
    if (!contentFile) Usage (progName);
 
283
    if (!signatureFile) Usage (progName);
 
284
    if (!outFile) outFile = stdout;
 
285
 
 
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);
 
291
        return -1;
 
292
    }
 
293
 
 
294
    if (HashDecodeAndVerify(outFile, contentFile, signatureFile,
 
295
                            certUsage, progName)) {
 
296
        SECU_PrintError(progName, "problem decoding/verifying signature");
 
297
        return -1;
 
298
    }
 
299
 
 
300
    if (NSS_Shutdown() != SECSuccess) {
 
301
        exit(1);
 
302
    }
 
303
 
 
304
    return 0;
 
305
}