michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "secutil.h" michael@0: #include "pk11func.h" michael@0: #include "secoid.h" michael@0: michael@0: #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4)) michael@0: #if !defined(WIN32) michael@0: extern int fread(char *, size_t, size_t, FILE*); michael@0: extern int fwrite(char *, size_t, size_t, FILE*); michael@0: extern int fprintf(FILE *, char *, ...); michael@0: #endif michael@0: #endif michael@0: michael@0: #include "plgetopt.h" michael@0: michael@0: static SECOidData * michael@0: HashTypeToOID(HASH_HashType hashtype) michael@0: { michael@0: SECOidTag hashtag; michael@0: michael@0: if (hashtype <= HASH_AlgNULL || hashtype >= HASH_AlgTOTAL) michael@0: return NULL; michael@0: michael@0: switch (hashtype) { michael@0: case HASH_AlgMD2: michael@0: hashtag = SEC_OID_MD2; michael@0: break; michael@0: case HASH_AlgMD5: michael@0: hashtag = SEC_OID_MD5; michael@0: break; michael@0: case HASH_AlgSHA1: michael@0: hashtag = SEC_OID_SHA1; michael@0: break; michael@0: default: michael@0: fprintf(stderr, "A new hash type has been added to HASH_HashType.\n"); michael@0: fprintf(stderr, "This program needs to be updated!\n"); michael@0: return NULL; michael@0: } michael@0: michael@0: return SECOID_FindOIDByTag(hashtag); michael@0: } michael@0: michael@0: static SECOidData * michael@0: HashNameToOID(const char *hashName) michael@0: { michael@0: HASH_HashType htype; michael@0: SECOidData *hashOID; michael@0: michael@0: for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) { michael@0: hashOID = HashTypeToOID(htype); michael@0: if (PORT_Strcasecmp(hashName, hashOID->desc) == 0) michael@0: break; michael@0: } michael@0: michael@0: if (htype == HASH_AlgTOTAL) michael@0: return NULL; michael@0: michael@0: return hashOID; michael@0: } michael@0: michael@0: static void michael@0: Usage(char *progName) michael@0: { michael@0: HASH_HashType htype; michael@0: michael@0: fprintf(stderr, michael@0: "Usage: %s -t type [-i input] [-o output]\n", michael@0: progName); michael@0: fprintf(stderr, "%-20s Specify the digest method (must be one of\n", michael@0: "-t type"); michael@0: fprintf(stderr, "%-20s ", ""); michael@0: for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) { michael@0: fprintf(stderr, "%s", HashTypeToOID(htype)->desc); michael@0: if (htype == (HASH_AlgTOTAL - 2)) michael@0: fprintf(stderr, " or "); michael@0: else if (htype != (HASH_AlgTOTAL - 1)) michael@0: fprintf(stderr, ", "); michael@0: } michael@0: fprintf(stderr, " (case ignored))\n"); michael@0: fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n", michael@0: "-i input"); michael@0: fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n", michael@0: "-o output"); michael@0: exit(-1); michael@0: } michael@0: michael@0: static int michael@0: DigestFile(FILE *outFile, FILE *inFile, SECOidData *hashOID) michael@0: { michael@0: int nb; michael@0: unsigned char ibuf[4096], digest[32]; michael@0: PK11Context *hashcx; michael@0: unsigned int len; michael@0: SECStatus rv; michael@0: michael@0: hashcx = PK11_CreateDigestContext(hashOID->offset); michael@0: if (hashcx == NULL) { michael@0: return -1; michael@0: } michael@0: PK11_DigestBegin(hashcx); michael@0: michael@0: michael@0: for (;;) { michael@0: if (feof(inFile)) break; michael@0: nb = fread(ibuf, 1, sizeof(ibuf), inFile); michael@0: if (nb != sizeof(ibuf)) { michael@0: if (nb == 0) { michael@0: if (ferror(inFile)) { michael@0: PORT_SetError(SEC_ERROR_IO); michael@0: PK11_DestroyContext(hashcx,PR_TRUE); michael@0: return -1; michael@0: } michael@0: /* eof */ michael@0: break; michael@0: } michael@0: } michael@0: rv = PK11_DigestOp(hashcx, ibuf, nb); michael@0: if (rv != SECSuccess) { michael@0: PK11_DestroyContext(hashcx, PR_TRUE); michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: rv = PK11_DigestFinal(hashcx, digest, &len, 32); michael@0: PK11_DestroyContext(hashcx, PR_TRUE); michael@0: michael@0: if (rv != SECSuccess) return -1; michael@0: michael@0: nb = fwrite(digest, 1, len, outFile); michael@0: if (nb != len) { michael@0: PORT_SetError(SEC_ERROR_IO); michael@0: return -1; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: #include "nss.h" michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: char *progName; michael@0: FILE *inFile, *outFile; michael@0: char *hashName; michael@0: SECOidData *hashOID; michael@0: PLOptState *optstate; michael@0: PLOptStatus status; michael@0: SECStatus rv; michael@0: michael@0: progName = strrchr(argv[0], '/'); michael@0: progName = progName ? progName+1 : argv[0]; michael@0: michael@0: inFile = NULL; michael@0: outFile = NULL; michael@0: hashName = NULL; michael@0: michael@0: rv = NSS_Init("/tmp"); michael@0: if (rv != SECSuccess) { michael@0: fprintf(stderr, "%s: NSS_Init failed in directory %s\n", michael@0: progName, "/tmp"); michael@0: return -1; michael@0: } michael@0: michael@0: /* michael@0: * Parse command line arguments michael@0: */ michael@0: optstate = PL_CreateOptState(argc, argv, "t:i:o:"); michael@0: while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: case '?': michael@0: Usage(progName); michael@0: break; michael@0: michael@0: case 'i': michael@0: inFile = fopen(optstate->value, "r"); michael@0: if (!inFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for reading\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: michael@0: case 'o': michael@0: outFile = fopen(optstate->value, "w"); michael@0: if (!outFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for writing\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: michael@0: case 't': michael@0: hashName = strdup(optstate->value); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (!hashName) Usage(progName); michael@0: michael@0: if (!inFile) inFile = stdin; michael@0: if (!outFile) outFile = stdout; michael@0: michael@0: hashOID = HashNameToOID(hashName); michael@0: if (hashOID == NULL) { michael@0: fprintf(stderr, "%s: invalid digest type\n", progName); michael@0: Usage(progName); michael@0: } michael@0: michael@0: if (DigestFile(outFile, inFile, hashOID)) { michael@0: fprintf(stderr, "%s: problem digesting data (%s)\n", michael@0: progName, SECU_Strerror(PORT_GetError())); michael@0: return -1; michael@0: } michael@0: michael@0: if (NSS_Shutdown() != SECSuccess) { michael@0: exit(1); michael@0: } michael@0: michael@0: return 0; michael@0: }