~ubuntu-branches/ubuntu/lucid/seamonkey/lucid-security

« back to all changes in this revision

Viewing changes to security/nss-fips/cmd/digest/digest.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2008-07-29 21:29:02 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080729212902-spm9kpvchp9udwbw
Tags: 1.1.11+nobinonly-0ubuntu1
* New security upstream release: 1.1.11 (LP: #218534)
  Fixes USN-602-1, USN-619-1, USN-623-1 and USN-629-1
* Refresh diverged patch:
  - update debian/patches/80_security_build.patch
* Fix FTBFS with missing -lfontconfig
  - add debian/patches/11_fix_ftbfs_with_fontconfig.patch
  - update debian/patches/series
* Build with default gcc (hardy: 4.2, intrepid: 4.3)
  - update debian/rules
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "pk11func.h"
 
39
#include "secoid.h"
 
40
 
 
41
#if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
 
42
#if !defined(WIN32)
 
43
extern int fread(char *, size_t, size_t, FILE*);
 
44
extern int fwrite(char *, size_t, size_t, FILE*);
 
45
extern int fprintf(FILE *, char *, ...);
 
46
#endif
 
47
#endif
 
48
 
 
49
#include "plgetopt.h"
 
50
 
 
51
static SECOidData *
 
52
HashTypeToOID(HASH_HashType hashtype)
 
53
{
 
54
    SECOidTag hashtag;
 
55
 
 
56
    if (hashtype <= HASH_AlgNULL || hashtype >= HASH_AlgTOTAL)
 
57
        return NULL;
 
58
 
 
59
    switch (hashtype) {
 
60
      case HASH_AlgMD2:
 
61
        hashtag = SEC_OID_MD2;
 
62
        break;
 
63
      case HASH_AlgMD5:
 
64
        hashtag = SEC_OID_MD5;
 
65
        break;
 
66
      case HASH_AlgSHA1:
 
67
        hashtag = SEC_OID_SHA1;
 
68
        break;
 
69
      default:
 
70
        fprintf(stderr, "A new hash type has been added to HASH_HashType.\n");
 
71
        fprintf(stderr, "This program needs to be updated!\n");
 
72
        return NULL;
 
73
    }
 
74
 
 
75
    return SECOID_FindOIDByTag(hashtag);
 
76
}
 
77
 
 
78
static SECOidData *
 
79
HashNameToOID(const char *hashName)
 
80
{
 
81
    HASH_HashType htype;
 
82
    SECOidData *hashOID;
 
83
 
 
84
    for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) {
 
85
        hashOID = HashTypeToOID(htype);
 
86
        if (PORT_Strcasecmp(hashName, hashOID->desc) == 0)
 
87
            break;
 
88
    }
 
89
 
 
90
    if (htype == HASH_AlgTOTAL)
 
91
        return NULL;
 
92
 
 
93
    return hashOID;
 
94
}
 
95
 
 
96
static void
 
97
Usage(char *progName)
 
98
{
 
99
    HASH_HashType htype;
 
100
 
 
101
    fprintf(stderr,
 
102
            "Usage:  %s -t type [-i input] [-o output]\n",
 
103
            progName);
 
104
    fprintf(stderr, "%-20s Specify the digest method (must be one of\n",
 
105
            "-t type");
 
106
    fprintf(stderr, "%-20s ", "");
 
107
    for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) {
 
108
        fprintf(stderr, HashTypeToOID(htype)->desc);
 
109
        if (htype == (HASH_AlgTOTAL - 2))
 
110
            fprintf(stderr, " or ");
 
111
        else if (htype != (HASH_AlgTOTAL - 1))
 
112
            fprintf(stderr, ", ");
 
113
    }
 
114
    fprintf(stderr, " (case ignored))\n");
 
115
    fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
 
116
            "-i input");
 
117
    fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
 
118
            "-o output");
 
119
    exit(-1);
 
120
}
 
121
 
 
122
static int
 
123
DigestFile(FILE *outFile, FILE *inFile, SECOidData *hashOID)
 
124
{
 
125
    int nb;
 
126
    unsigned char ibuf[4096], digest[32];
 
127
    PK11Context *hashcx;
 
128
    unsigned int len;
 
129
    SECStatus rv;
 
130
 
 
131
    hashcx = PK11_CreateDigestContext(hashOID->offset);
 
132
    if (hashcx == NULL) {
 
133
        return -1;
 
134
    }
 
135
    PK11_DigestBegin(hashcx);
 
136
 
 
137
 
 
138
    for (;;) {
 
139
        if (feof(inFile)) break;
 
140
        nb = fread(ibuf, 1, sizeof(ibuf), inFile);
 
141
        if (nb != sizeof(ibuf)) {
 
142
            if (nb == 0) {
 
143
                if (ferror(inFile)) {
 
144
                    PORT_SetError(SEC_ERROR_IO);
 
145
                    PK11_DestroyContext(hashcx,PR_TRUE);
 
146
                    return -1;
 
147
                }
 
148
                /* eof */
 
149
                break;
 
150
            }
 
151
        }
 
152
        rv = PK11_DigestOp(hashcx, ibuf, nb);
 
153
        if (rv != SECSuccess) {
 
154
           PK11_DestroyContext(hashcx, PR_TRUE);
 
155
           return -1;
 
156
        }
 
157
    }
 
158
 
 
159
    rv = PK11_DigestFinal(hashcx, digest, &len, 32);
 
160
    PK11_DestroyContext(hashcx, PR_TRUE);
 
161
 
 
162
    if (rv != SECSuccess) return -1;
 
163
 
 
164
    nb = fwrite(digest, 1, len, outFile);
 
165
    if (nb != len) {
 
166
        PORT_SetError(SEC_ERROR_IO);
 
167
        return -1;
 
168
    }
 
169
 
 
170
    return 0;
 
171
}
 
172
 
 
173
#include "nss.h"
 
174
 
 
175
int
 
176
main(int argc, char **argv)
 
177
{
 
178
    char *progName;
 
179
    FILE *inFile, *outFile;
 
180
    char *hashName;
 
181
    SECOidData *hashOID;
 
182
    PLOptState *optstate;
 
183
    PLOptStatus status;
 
184
    SECStatus   rv;
 
185
 
 
186
    progName = strrchr(argv[0], '/');
 
187
    progName = progName ? progName+1 : argv[0];
 
188
 
 
189
    inFile = NULL;
 
190
    outFile = NULL;
 
191
    hashName = NULL;
 
192
 
 
193
    rv = NSS_Init("/tmp");
 
194
    if (rv != SECSuccess) {
 
195
        fprintf(stderr, "%s: NSS_Init failed in directory %s\n",
 
196
                progName, "/tmp");
 
197
        return -1;
 
198
    }
 
199
 
 
200
    /*
 
201
     * Parse command line arguments
 
202
     */
 
203
    optstate = PL_CreateOptState(argc, argv, "t:i:o:");
 
204
    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
 
205
        switch (optstate->option) {
 
206
          case '?':
 
207
            Usage(progName);
 
208
            break;
 
209
 
 
210
          case 'i':
 
211
            inFile = fopen(optstate->value, "r");
 
212
            if (!inFile) {
 
213
                fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
 
214
                        progName, optstate->value);
 
215
                return -1;
 
216
            }
 
217
            break;
 
218
 
 
219
          case 'o':
 
220
            outFile = fopen(optstate->value, "w");
 
221
            if (!outFile) {
 
222
                fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
 
223
                        progName, optstate->value);
 
224
                return -1;
 
225
            }
 
226
            break;
 
227
 
 
228
          case 't':
 
229
            hashName = strdup(optstate->value);
 
230
            break;
 
231
        }
 
232
    }
 
233
 
 
234
    if (!hashName) Usage(progName);
 
235
 
 
236
    if (!inFile) inFile = stdin;
 
237
    if (!outFile) outFile = stdout;
 
238
 
 
239
    hashOID = HashNameToOID(hashName);
 
240
    if (hashOID == NULL) {
 
241
        fprintf(stderr, "%s: invalid digest type\n", progName);
 
242
        Usage(progName);
 
243
    }
 
244
 
 
245
    if (DigestFile(outFile, inFile, hashOID)) {
 
246
        fprintf(stderr, "%s: problem digesting data (%s)\n",
 
247
                progName, SECU_Strerror(PORT_GetError()));
 
248
        return -1;
 
249
    }
 
250
 
 
251
    if (NSS_Shutdown() != SECSuccess) {
 
252
        exit(1);
 
253
    } 
 
254
    
 
255
    return 0;
 
256
}