security/nss/cmd/digest/digest.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "secutil.h"
michael@0 6 #include "pk11func.h"
michael@0 7 #include "secoid.h"
michael@0 8
michael@0 9 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
michael@0 10 #if !defined(WIN32)
michael@0 11 extern int fread(char *, size_t, size_t, FILE*);
michael@0 12 extern int fwrite(char *, size_t, size_t, FILE*);
michael@0 13 extern int fprintf(FILE *, char *, ...);
michael@0 14 #endif
michael@0 15 #endif
michael@0 16
michael@0 17 #include "plgetopt.h"
michael@0 18
michael@0 19 static SECOidData *
michael@0 20 HashTypeToOID(HASH_HashType hashtype)
michael@0 21 {
michael@0 22 SECOidTag hashtag;
michael@0 23
michael@0 24 if (hashtype <= HASH_AlgNULL || hashtype >= HASH_AlgTOTAL)
michael@0 25 return NULL;
michael@0 26
michael@0 27 switch (hashtype) {
michael@0 28 case HASH_AlgMD2:
michael@0 29 hashtag = SEC_OID_MD2;
michael@0 30 break;
michael@0 31 case HASH_AlgMD5:
michael@0 32 hashtag = SEC_OID_MD5;
michael@0 33 break;
michael@0 34 case HASH_AlgSHA1:
michael@0 35 hashtag = SEC_OID_SHA1;
michael@0 36 break;
michael@0 37 default:
michael@0 38 fprintf(stderr, "A new hash type has been added to HASH_HashType.\n");
michael@0 39 fprintf(stderr, "This program needs to be updated!\n");
michael@0 40 return NULL;
michael@0 41 }
michael@0 42
michael@0 43 return SECOID_FindOIDByTag(hashtag);
michael@0 44 }
michael@0 45
michael@0 46 static SECOidData *
michael@0 47 HashNameToOID(const char *hashName)
michael@0 48 {
michael@0 49 HASH_HashType htype;
michael@0 50 SECOidData *hashOID;
michael@0 51
michael@0 52 for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) {
michael@0 53 hashOID = HashTypeToOID(htype);
michael@0 54 if (PORT_Strcasecmp(hashName, hashOID->desc) == 0)
michael@0 55 break;
michael@0 56 }
michael@0 57
michael@0 58 if (htype == HASH_AlgTOTAL)
michael@0 59 return NULL;
michael@0 60
michael@0 61 return hashOID;
michael@0 62 }
michael@0 63
michael@0 64 static void
michael@0 65 Usage(char *progName)
michael@0 66 {
michael@0 67 HASH_HashType htype;
michael@0 68
michael@0 69 fprintf(stderr,
michael@0 70 "Usage: %s -t type [-i input] [-o output]\n",
michael@0 71 progName);
michael@0 72 fprintf(stderr, "%-20s Specify the digest method (must be one of\n",
michael@0 73 "-t type");
michael@0 74 fprintf(stderr, "%-20s ", "");
michael@0 75 for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) {
michael@0 76 fprintf(stderr, "%s", HashTypeToOID(htype)->desc);
michael@0 77 if (htype == (HASH_AlgTOTAL - 2))
michael@0 78 fprintf(stderr, " or ");
michael@0 79 else if (htype != (HASH_AlgTOTAL - 1))
michael@0 80 fprintf(stderr, ", ");
michael@0 81 }
michael@0 82 fprintf(stderr, " (case ignored))\n");
michael@0 83 fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
michael@0 84 "-i input");
michael@0 85 fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
michael@0 86 "-o output");
michael@0 87 exit(-1);
michael@0 88 }
michael@0 89
michael@0 90 static int
michael@0 91 DigestFile(FILE *outFile, FILE *inFile, SECOidData *hashOID)
michael@0 92 {
michael@0 93 int nb;
michael@0 94 unsigned char ibuf[4096], digest[32];
michael@0 95 PK11Context *hashcx;
michael@0 96 unsigned int len;
michael@0 97 SECStatus rv;
michael@0 98
michael@0 99 hashcx = PK11_CreateDigestContext(hashOID->offset);
michael@0 100 if (hashcx == NULL) {
michael@0 101 return -1;
michael@0 102 }
michael@0 103 PK11_DigestBegin(hashcx);
michael@0 104
michael@0 105
michael@0 106 for (;;) {
michael@0 107 if (feof(inFile)) break;
michael@0 108 nb = fread(ibuf, 1, sizeof(ibuf), inFile);
michael@0 109 if (nb != sizeof(ibuf)) {
michael@0 110 if (nb == 0) {
michael@0 111 if (ferror(inFile)) {
michael@0 112 PORT_SetError(SEC_ERROR_IO);
michael@0 113 PK11_DestroyContext(hashcx,PR_TRUE);
michael@0 114 return -1;
michael@0 115 }
michael@0 116 /* eof */
michael@0 117 break;
michael@0 118 }
michael@0 119 }
michael@0 120 rv = PK11_DigestOp(hashcx, ibuf, nb);
michael@0 121 if (rv != SECSuccess) {
michael@0 122 PK11_DestroyContext(hashcx, PR_TRUE);
michael@0 123 return -1;
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 rv = PK11_DigestFinal(hashcx, digest, &len, 32);
michael@0 128 PK11_DestroyContext(hashcx, PR_TRUE);
michael@0 129
michael@0 130 if (rv != SECSuccess) return -1;
michael@0 131
michael@0 132 nb = fwrite(digest, 1, len, outFile);
michael@0 133 if (nb != len) {
michael@0 134 PORT_SetError(SEC_ERROR_IO);
michael@0 135 return -1;
michael@0 136 }
michael@0 137
michael@0 138 return 0;
michael@0 139 }
michael@0 140
michael@0 141 #include "nss.h"
michael@0 142
michael@0 143 int
michael@0 144 main(int argc, char **argv)
michael@0 145 {
michael@0 146 char *progName;
michael@0 147 FILE *inFile, *outFile;
michael@0 148 char *hashName;
michael@0 149 SECOidData *hashOID;
michael@0 150 PLOptState *optstate;
michael@0 151 PLOptStatus status;
michael@0 152 SECStatus rv;
michael@0 153
michael@0 154 progName = strrchr(argv[0], '/');
michael@0 155 progName = progName ? progName+1 : argv[0];
michael@0 156
michael@0 157 inFile = NULL;
michael@0 158 outFile = NULL;
michael@0 159 hashName = NULL;
michael@0 160
michael@0 161 rv = NSS_Init("/tmp");
michael@0 162 if (rv != SECSuccess) {
michael@0 163 fprintf(stderr, "%s: NSS_Init failed in directory %s\n",
michael@0 164 progName, "/tmp");
michael@0 165 return -1;
michael@0 166 }
michael@0 167
michael@0 168 /*
michael@0 169 * Parse command line arguments
michael@0 170 */
michael@0 171 optstate = PL_CreateOptState(argc, argv, "t:i:o:");
michael@0 172 while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
michael@0 173 switch (optstate->option) {
michael@0 174 case '?':
michael@0 175 Usage(progName);
michael@0 176 break;
michael@0 177
michael@0 178 case 'i':
michael@0 179 inFile = fopen(optstate->value, "r");
michael@0 180 if (!inFile) {
michael@0 181 fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
michael@0 182 progName, optstate->value);
michael@0 183 return -1;
michael@0 184 }
michael@0 185 break;
michael@0 186
michael@0 187 case 'o':
michael@0 188 outFile = fopen(optstate->value, "w");
michael@0 189 if (!outFile) {
michael@0 190 fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
michael@0 191 progName, optstate->value);
michael@0 192 return -1;
michael@0 193 }
michael@0 194 break;
michael@0 195
michael@0 196 case 't':
michael@0 197 hashName = strdup(optstate->value);
michael@0 198 break;
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 if (!hashName) Usage(progName);
michael@0 203
michael@0 204 if (!inFile) inFile = stdin;
michael@0 205 if (!outFile) outFile = stdout;
michael@0 206
michael@0 207 hashOID = HashNameToOID(hashName);
michael@0 208 if (hashOID == NULL) {
michael@0 209 fprintf(stderr, "%s: invalid digest type\n", progName);
michael@0 210 Usage(progName);
michael@0 211 }
michael@0 212
michael@0 213 if (DigestFile(outFile, inFile, hashOID)) {
michael@0 214 fprintf(stderr, "%s: problem digesting data (%s)\n",
michael@0 215 progName, SECU_Strerror(PORT_GetError()));
michael@0 216 return -1;
michael@0 217 }
michael@0 218
michael@0 219 if (NSS_Shutdown() != SECSuccess) {
michael@0 220 exit(1);
michael@0 221 }
michael@0 222
michael@0 223 return 0;
michael@0 224 }

mercurial