Wed, 31 Dec 2014 06:09:35 +0100
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 "plgetopt.h" |
michael@0 | 6 | #include "secutil.h" |
michael@0 | 7 | #include "nssb64.h" |
michael@0 | 8 | #include <errno.h> |
michael@0 | 9 | |
michael@0 | 10 | #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4)) |
michael@0 | 11 | #if !defined(WIN32) |
michael@0 | 12 | extern int fread(char *, size_t, size_t, FILE*); |
michael@0 | 13 | extern int fwrite(char *, size_t, size_t, FILE*); |
michael@0 | 14 | extern int fprintf(FILE *, char *, ...); |
michael@0 | 15 | #endif |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | #if defined(WIN32) |
michael@0 | 19 | #include "fcntl.h" |
michael@0 | 20 | #include "io.h" |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | static PRInt32 |
michael@0 | 24 | output_ascii (void *arg, const char *obuf, PRInt32 size) |
michael@0 | 25 | { |
michael@0 | 26 | FILE *outFile = arg; |
michael@0 | 27 | int nb; |
michael@0 | 28 | |
michael@0 | 29 | nb = fwrite(obuf, 1, size, outFile); |
michael@0 | 30 | if (nb != size) { |
michael@0 | 31 | PORT_SetError(SEC_ERROR_IO); |
michael@0 | 32 | return -1; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | return nb; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | static SECStatus |
michael@0 | 39 | encode_file(FILE *outFile, FILE *inFile) |
michael@0 | 40 | { |
michael@0 | 41 | NSSBase64Encoder *cx; |
michael@0 | 42 | int nb; |
michael@0 | 43 | SECStatus status = SECFailure; |
michael@0 | 44 | unsigned char ibuf[4096]; |
michael@0 | 45 | |
michael@0 | 46 | cx = NSSBase64Encoder_Create(output_ascii, outFile); |
michael@0 | 47 | if (!cx) { |
michael@0 | 48 | return -1; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | for (;;) { |
michael@0 | 52 | if (feof(inFile)) break; |
michael@0 | 53 | nb = fread(ibuf, 1, sizeof(ibuf), inFile); |
michael@0 | 54 | if (nb != sizeof(ibuf)) { |
michael@0 | 55 | if (nb == 0) { |
michael@0 | 56 | if (ferror(inFile)) { |
michael@0 | 57 | PORT_SetError(SEC_ERROR_IO); |
michael@0 | 58 | goto loser; |
michael@0 | 59 | } |
michael@0 | 60 | /* eof */ |
michael@0 | 61 | break; |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | status = NSSBase64Encoder_Update(cx, ibuf, nb); |
michael@0 | 66 | if (status != SECSuccess) goto loser; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | status = NSSBase64Encoder_Destroy(cx, PR_FALSE); |
michael@0 | 70 | if (status != SECSuccess) |
michael@0 | 71 | return status; |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | * Add a trailing CRLF. Note this must be done *after* the call |
michael@0 | 75 | * to Destroy above (because only then are we sure all data has |
michael@0 | 76 | * been written out). |
michael@0 | 77 | */ |
michael@0 | 78 | fwrite("\r\n", 1, 2, outFile); |
michael@0 | 79 | return SECSuccess; |
michael@0 | 80 | |
michael@0 | 81 | loser: |
michael@0 | 82 | (void) NSSBase64Encoder_Destroy(cx, PR_TRUE); |
michael@0 | 83 | return status; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | static void Usage(char *progName) |
michael@0 | 87 | { |
michael@0 | 88 | fprintf(stderr, |
michael@0 | 89 | "Usage: %s [-i input] [-o output]\n", |
michael@0 | 90 | progName); |
michael@0 | 91 | fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n", |
michael@0 | 92 | "-i input"); |
michael@0 | 93 | fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n", |
michael@0 | 94 | "-o output"); |
michael@0 | 95 | fprintf(stderr, "%-20s Wrap output in BEGIN/END lines and the given suffix\n", |
michael@0 | 96 | "-w suffix"); |
michael@0 | 97 | fprintf(stderr, "%-20s (use \"c\" as a shortcut for suffix CERTIFICATE)\n", |
michael@0 | 98 | ""); |
michael@0 | 99 | exit(-1); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | int main(int argc, char **argv) |
michael@0 | 103 | { |
michael@0 | 104 | char *progName; |
michael@0 | 105 | SECStatus rv; |
michael@0 | 106 | FILE *inFile, *outFile; |
michael@0 | 107 | PLOptState *optstate; |
michael@0 | 108 | PLOptStatus status; |
michael@0 | 109 | char *suffix = NULL; |
michael@0 | 110 | |
michael@0 | 111 | inFile = 0; |
michael@0 | 112 | outFile = 0; |
michael@0 | 113 | progName = strrchr(argv[0], '/'); |
michael@0 | 114 | if (!progName) |
michael@0 | 115 | progName = strrchr(argv[0], '\\'); |
michael@0 | 116 | progName = progName ? progName+1 : argv[0]; |
michael@0 | 117 | |
michael@0 | 118 | /* Parse command line arguments */ |
michael@0 | 119 | optstate = PL_CreateOptState(argc, argv, "i:o:w:"); |
michael@0 | 120 | while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { |
michael@0 | 121 | switch (optstate->option) { |
michael@0 | 122 | default: |
michael@0 | 123 | Usage(progName); |
michael@0 | 124 | break; |
michael@0 | 125 | |
michael@0 | 126 | case 'i': |
michael@0 | 127 | inFile = fopen(optstate->value, "rb"); |
michael@0 | 128 | if (!inFile) { |
michael@0 | 129 | fprintf(stderr, "%s: unable to open \"%s\" for reading\n", |
michael@0 | 130 | progName, optstate->value); |
michael@0 | 131 | return -1; |
michael@0 | 132 | } |
michael@0 | 133 | break; |
michael@0 | 134 | |
michael@0 | 135 | case 'o': |
michael@0 | 136 | outFile = fopen(optstate->value, "wb"); |
michael@0 | 137 | if (!outFile) { |
michael@0 | 138 | fprintf(stderr, "%s: unable to open \"%s\" for writing\n", |
michael@0 | 139 | progName, optstate->value); |
michael@0 | 140 | return -1; |
michael@0 | 141 | } |
michael@0 | 142 | break; |
michael@0 | 143 | |
michael@0 | 144 | case 'w': |
michael@0 | 145 | if (!strcmp(optstate->value, "c")) |
michael@0 | 146 | suffix = strdup("CERTIFICATE"); |
michael@0 | 147 | else |
michael@0 | 148 | suffix = strdup(optstate->value); |
michael@0 | 149 | break; |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | if (status == PL_OPT_BAD) |
michael@0 | 153 | Usage(progName); |
michael@0 | 154 | if (!inFile) { |
michael@0 | 155 | #if defined(WIN32) |
michael@0 | 156 | /* If we're going to read binary data from stdin, we must put stdin |
michael@0 | 157 | ** into O_BINARY mode or else incoming \r\n's will become \n's. |
michael@0 | 158 | */ |
michael@0 | 159 | |
michael@0 | 160 | int smrv = _setmode(_fileno(stdin), _O_BINARY); |
michael@0 | 161 | if (smrv == -1) { |
michael@0 | 162 | fprintf(stderr, |
michael@0 | 163 | "%s: Cannot change stdin to binary mode. Use -i option instead.\n", |
michael@0 | 164 | progName); |
michael@0 | 165 | return smrv; |
michael@0 | 166 | } |
michael@0 | 167 | #endif |
michael@0 | 168 | inFile = stdin; |
michael@0 | 169 | } |
michael@0 | 170 | if (!outFile) { |
michael@0 | 171 | #if defined(WIN32) |
michael@0 | 172 | /* We're going to write binary data to stdout. We must put stdout |
michael@0 | 173 | ** into O_BINARY mode or else outgoing \r\n's will become \r\r\n's. |
michael@0 | 174 | */ |
michael@0 | 175 | |
michael@0 | 176 | int smrv = _setmode(_fileno(stdout), _O_BINARY); |
michael@0 | 177 | if (smrv == -1) { |
michael@0 | 178 | fprintf(stderr, |
michael@0 | 179 | "%s: Cannot change stdout to binary mode. Use -o option instead.\n", |
michael@0 | 180 | progName); |
michael@0 | 181 | return smrv; |
michael@0 | 182 | } |
michael@0 | 183 | #endif |
michael@0 | 184 | outFile = stdout; |
michael@0 | 185 | } |
michael@0 | 186 | if (suffix) { |
michael@0 | 187 | fprintf(outFile, "-----BEGIN %s-----\n", suffix); |
michael@0 | 188 | } |
michael@0 | 189 | rv = encode_file(outFile, inFile); |
michael@0 | 190 | if (rv != SECSuccess) { |
michael@0 | 191 | fprintf(stderr, "%s: lossage: error=%d errno=%d\n", |
michael@0 | 192 | progName, PORT_GetError(), errno); |
michael@0 | 193 | return -1; |
michael@0 | 194 | } |
michael@0 | 195 | if (suffix) { |
michael@0 | 196 | fprintf(outFile, "-----END %s-----\n", suffix); |
michael@0 | 197 | } |
michael@0 | 198 | return 0; |
michael@0 | 199 | } |