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 "plgetopt.h" michael@0: #include "secutil.h" michael@0: #include "nssb64.h" michael@0: #include 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: #if defined(WIN32) michael@0: #include "fcntl.h" michael@0: #include "io.h" michael@0: #endif michael@0: michael@0: static PRInt32 michael@0: output_ascii (void *arg, const char *obuf, PRInt32 size) michael@0: { michael@0: FILE *outFile = arg; michael@0: int nb; michael@0: michael@0: nb = fwrite(obuf, 1, size, outFile); michael@0: if (nb != size) { michael@0: PORT_SetError(SEC_ERROR_IO); michael@0: return -1; michael@0: } michael@0: michael@0: return nb; michael@0: } michael@0: michael@0: static SECStatus michael@0: encode_file(FILE *outFile, FILE *inFile) michael@0: { michael@0: NSSBase64Encoder *cx; michael@0: int nb; michael@0: SECStatus status = SECFailure; michael@0: unsigned char ibuf[4096]; michael@0: michael@0: cx = NSSBase64Encoder_Create(output_ascii, outFile); michael@0: if (!cx) { michael@0: return -1; 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: goto loser; michael@0: } michael@0: /* eof */ michael@0: break; michael@0: } michael@0: } michael@0: michael@0: status = NSSBase64Encoder_Update(cx, ibuf, nb); michael@0: if (status != SECSuccess) goto loser; michael@0: } michael@0: michael@0: status = NSSBase64Encoder_Destroy(cx, PR_FALSE); michael@0: if (status != SECSuccess) michael@0: return status; michael@0: michael@0: /* michael@0: * Add a trailing CRLF. Note this must be done *after* the call michael@0: * to Destroy above (because only then are we sure all data has michael@0: * been written out). michael@0: */ michael@0: fwrite("\r\n", 1, 2, outFile); michael@0: return SECSuccess; michael@0: michael@0: loser: michael@0: (void) NSSBase64Encoder_Destroy(cx, PR_TRUE); michael@0: return status; michael@0: } michael@0: michael@0: static void Usage(char *progName) michael@0: { michael@0: fprintf(stderr, michael@0: "Usage: %s [-i input] [-o output]\n", michael@0: progName); 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: fprintf(stderr, "%-20s Wrap output in BEGIN/END lines and the given suffix\n", michael@0: "-w suffix"); michael@0: fprintf(stderr, "%-20s (use \"c\" as a shortcut for suffix CERTIFICATE)\n", michael@0: ""); michael@0: exit(-1); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: char *progName; michael@0: SECStatus rv; michael@0: FILE *inFile, *outFile; michael@0: PLOptState *optstate; michael@0: PLOptStatus status; michael@0: char *suffix = NULL; michael@0: michael@0: inFile = 0; michael@0: outFile = 0; michael@0: progName = strrchr(argv[0], '/'); michael@0: if (!progName) michael@0: progName = strrchr(argv[0], '\\'); michael@0: progName = progName ? progName+1 : argv[0]; michael@0: michael@0: /* Parse command line arguments */ michael@0: optstate = PL_CreateOptState(argc, argv, "i:o:w:"); michael@0: while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: default: michael@0: Usage(progName); michael@0: break; michael@0: michael@0: case 'i': michael@0: inFile = fopen(optstate->value, "rb"); 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, "wb"); 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 'w': michael@0: if (!strcmp(optstate->value, "c")) michael@0: suffix = strdup("CERTIFICATE"); michael@0: else michael@0: suffix = strdup(optstate->value); michael@0: break; michael@0: } michael@0: } michael@0: if (status == PL_OPT_BAD) michael@0: Usage(progName); michael@0: if (!inFile) { michael@0: #if defined(WIN32) michael@0: /* If we're going to read binary data from stdin, we must put stdin michael@0: ** into O_BINARY mode or else incoming \r\n's will become \n's. michael@0: */ michael@0: michael@0: int smrv = _setmode(_fileno(stdin), _O_BINARY); michael@0: if (smrv == -1) { michael@0: fprintf(stderr, michael@0: "%s: Cannot change stdin to binary mode. Use -i option instead.\n", michael@0: progName); michael@0: return smrv; michael@0: } michael@0: #endif michael@0: inFile = stdin; michael@0: } michael@0: if (!outFile) { michael@0: #if defined(WIN32) michael@0: /* We're going to write binary data to stdout. We must put stdout michael@0: ** into O_BINARY mode or else outgoing \r\n's will become \r\r\n's. michael@0: */ michael@0: michael@0: int smrv = _setmode(_fileno(stdout), _O_BINARY); michael@0: if (smrv == -1) { michael@0: fprintf(stderr, michael@0: "%s: Cannot change stdout to binary mode. Use -o option instead.\n", michael@0: progName); michael@0: return smrv; michael@0: } michael@0: #endif michael@0: outFile = stdout; michael@0: } michael@0: if (suffix) { michael@0: fprintf(outFile, "-----BEGIN %s-----\n", suffix); michael@0: } michael@0: rv = encode_file(outFile, inFile); michael@0: if (rv != SECSuccess) { michael@0: fprintf(stderr, "%s: lossage: error=%d errno=%d\n", michael@0: progName, PORT_GetError(), errno); michael@0: return -1; michael@0: } michael@0: if (suffix) { michael@0: fprintf(outFile, "-----END %s-----\n", suffix); michael@0: } michael@0: return 0; michael@0: }