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 | /* |
michael@0 | 6 | * Test program for SDR (Secret Decoder Ring) functions. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | #include "nspr.h" |
michael@0 | 10 | #include "string.h" |
michael@0 | 11 | #include "nss.h" |
michael@0 | 12 | #include "secutil.h" |
michael@0 | 13 | #include "cert.h" |
michael@0 | 14 | #include "pk11func.h" |
michael@0 | 15 | #include "nssb64.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "plgetopt.h" |
michael@0 | 18 | #include "pk11sdr.h" |
michael@0 | 19 | |
michael@0 | 20 | #define DEFAULT_VALUE "Test" |
michael@0 | 21 | |
michael@0 | 22 | static void |
michael@0 | 23 | synopsis (char *program_name) |
michael@0 | 24 | { |
michael@0 | 25 | PRFileDesc *pr_stderr; |
michael@0 | 26 | |
michael@0 | 27 | pr_stderr = PR_STDERR; |
michael@0 | 28 | PR_fprintf (pr_stderr, |
michael@0 | 29 | "Usage:\t%s [-i <input-file>] [-o <output-file>] [-d <dir>]\n" |
michael@0 | 30 | " \t[-l logfile] [-p pwd] [-f pwfile]\n", program_name); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | static void |
michael@0 | 35 | short_usage (char *program_name) |
michael@0 | 36 | { |
michael@0 | 37 | PR_fprintf (PR_STDERR, |
michael@0 | 38 | "Type %s -H for more detailed descriptions\n", |
michael@0 | 39 | program_name); |
michael@0 | 40 | synopsis (program_name); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | |
michael@0 | 44 | static void |
michael@0 | 45 | long_usage (char *program_name) |
michael@0 | 46 | { |
michael@0 | 47 | PRFileDesc *pr_stderr; |
michael@0 | 48 | |
michael@0 | 49 | pr_stderr = PR_STDERR; |
michael@0 | 50 | synopsis (program_name); |
michael@0 | 51 | PR_fprintf (pr_stderr, "\nDecode encrypted passwords (and other data).\n"); |
michael@0 | 52 | PR_fprintf (pr_stderr, |
michael@0 | 53 | "This program reads in standard configuration files looking\n" |
michael@0 | 54 | "for base 64 encoded data. Data that looks like it's base 64 encode\n" |
michael@0 | 55 | "is decoded an passed to the NSS SDR code. If the decode and decrypt\n" |
michael@0 | 56 | "is successful, then decrypted data is outputted in place of the\n" |
michael@0 | 57 | "original base 64 data. If the decode or decrypt fails, the original\n" |
michael@0 | 58 | "data is written and the reason for failure is logged to the \n" |
michael@0 | 59 | "optional logfile.\n"); |
michael@0 | 60 | PR_fprintf (pr_stderr, |
michael@0 | 61 | " %-13s Read stream including encrypted data from " |
michael@0 | 62 | "\"read_file\"\n", |
michael@0 | 63 | "-i read_file"); |
michael@0 | 64 | PR_fprintf (pr_stderr, |
michael@0 | 65 | " %-13s Write results to \"write_file\"\n", |
michael@0 | 66 | "-o write_file"); |
michael@0 | 67 | PR_fprintf (pr_stderr, |
michael@0 | 68 | " %-13s Find security databases in \"dbdir\"\n", |
michael@0 | 69 | "-d dbdir"); |
michael@0 | 70 | PR_fprintf (pr_stderr, |
michael@0 | 71 | " %-13s Log failed decrypt/decode attempts to \"log_file\"\n", |
michael@0 | 72 | "-l log_file"); |
michael@0 | 73 | PR_fprintf (pr_stderr, |
michael@0 | 74 | " %-13s Token password\n", |
michael@0 | 75 | "-p pwd"); |
michael@0 | 76 | PR_fprintf (pr_stderr, |
michael@0 | 77 | " %-13s Password file\n", |
michael@0 | 78 | "-f pwfile"); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | /* |
michael@0 | 82 | * base64 table only used to identify the end of a base64 string |
michael@0 | 83 | */ |
michael@0 | 84 | static unsigned char b64[256] = { |
michael@0 | 85 | /* 00: */ 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 86 | /* 08: */ 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 87 | /* 10: */ 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 88 | /* 18: */ 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 89 | /* 20: */ 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 90 | /* 28: */ 0, 0, 0, 1, 0, 0, 0, 1, |
michael@0 | 91 | /* 30: */ 1, 1, 1, 1, 1, 1, 1, 1, |
michael@0 | 92 | /* 38: */ 1, 1, 0, 0, 0, 0, 0, 0, |
michael@0 | 93 | /* 40: */ 0, 1, 1, 1, 1, 1, 1, 1, |
michael@0 | 94 | /* 48: */ 1, 1, 1, 1, 1, 1, 1, 1, |
michael@0 | 95 | /* 50: */ 1, 1, 1, 1, 1, 1, 1, 1, |
michael@0 | 96 | /* 58: */ 1, 1, 1, 0, 0, 0, 0, 0, |
michael@0 | 97 | /* 60: */ 0, 1, 1, 1, 1, 1, 1, 1, |
michael@0 | 98 | /* 68: */ 1, 1, 1, 1, 1, 1, 1, 1, |
michael@0 | 99 | /* 70: */ 1, 1, 1, 1, 1, 1, 1, 1, |
michael@0 | 100 | /* 78: */ 1, 1, 1, 0, 0, 0, 0, 0, |
michael@0 | 101 | }; |
michael@0 | 102 | |
michael@0 | 103 | enum { |
michael@0 | 104 | false = 0, |
michael@0 | 105 | true = 1 |
michael@0 | 106 | } bool; |
michael@0 | 107 | |
michael@0 | 108 | #define isatobchar(c) (b64[c]) |
michael@0 | 109 | |
michael@0 | 110 | #define MAX_STRING 8192 |
michael@0 | 111 | |
michael@0 | 112 | int |
michael@0 | 113 | isBase64(char *inString) |
michael@0 | 114 | { |
michael@0 | 115 | unsigned int i; |
michael@0 | 116 | unsigned char c; |
michael@0 | 117 | |
michael@0 | 118 | for (i = 0; (c = inString[i]) != 0 && isatobchar(c); ++i) |
michael@0 | 119 | ; |
michael@0 | 120 | if (c == '=') { |
michael@0 | 121 | while ((c = inString[++i]) == '=') |
michael@0 | 122 | ; /* skip trailing '=' characters */ |
michael@0 | 123 | } |
michael@0 | 124 | if (c && c != '\n' && c != '\r') |
michael@0 | 125 | return false; |
michael@0 | 126 | if (i == 0 || i % 4) |
michael@0 | 127 | return false; |
michael@0 | 128 | return true; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | void |
michael@0 | 132 | doDecrypt(char * dataString, FILE *outFile, FILE *logFile, secuPWData *pwdata) |
michael@0 | 133 | { |
michael@0 | 134 | int strLen = strlen(dataString); |
michael@0 | 135 | SECItem *decoded = NSSBase64_DecodeBuffer(NULL, NULL, dataString, strLen); |
michael@0 | 136 | SECStatus rv; |
michael@0 | 137 | int err; |
michael@0 | 138 | SECItem result = { siBuffer, NULL, 0 }; |
michael@0 | 139 | |
michael@0 | 140 | if ((decoded == NULL) || (decoded->len == 0)) { |
michael@0 | 141 | if (logFile) { |
michael@0 | 142 | err = PORT_GetError(); |
michael@0 | 143 | fprintf(logFile,"Base 64 decode failed on <%s>\n", dataString); |
michael@0 | 144 | fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err)); |
michael@0 | 145 | } |
michael@0 | 146 | fputs(dataString, outFile); |
michael@0 | 147 | if (decoded) |
michael@0 | 148 | SECITEM_FreeItem(decoded, PR_TRUE); |
michael@0 | 149 | return; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | rv = PK11SDR_Decrypt(decoded, &result, pwdata); |
michael@0 | 153 | SECITEM_ZfreeItem(decoded, PR_TRUE); |
michael@0 | 154 | if (rv == SECSuccess) { |
michael@0 | 155 | /* result buffer has no extra space for a NULL */ |
michael@0 | 156 | fprintf(outFile, "Decrypted: \"%.*s\"\n", result.len, result.data); |
michael@0 | 157 | SECITEM_ZfreeItem(&result, PR_FALSE); |
michael@0 | 158 | return; |
michael@0 | 159 | } |
michael@0 | 160 | /* Encryption failed. output raw input. */ |
michael@0 | 161 | if (logFile) { |
michael@0 | 162 | err = PORT_GetError(); |
michael@0 | 163 | fprintf(logFile,"SDR decrypt failed on <%s>\n", dataString); |
michael@0 | 164 | fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err)); |
michael@0 | 165 | } |
michael@0 | 166 | fputs(dataString,outFile); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | void |
michael@0 | 170 | doDecode(char * dataString, FILE *outFile, FILE *logFile) |
michael@0 | 171 | { |
michael@0 | 172 | int strLen = strlen(dataString + 1); |
michael@0 | 173 | SECItem *decoded; |
michael@0 | 174 | |
michael@0 | 175 | decoded = NSSBase64_DecodeBuffer(NULL, NULL, dataString + 1, strLen); |
michael@0 | 176 | if ((decoded == NULL) || (decoded->len == 0)) { |
michael@0 | 177 | if (logFile) { |
michael@0 | 178 | int err = PORT_GetError(); |
michael@0 | 179 | fprintf(logFile,"Base 64 decode failed on <%s>\n", dataString + 1); |
michael@0 | 180 | fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err)); |
michael@0 | 181 | } |
michael@0 | 182 | fputs(dataString, outFile); |
michael@0 | 183 | if (decoded) |
michael@0 | 184 | SECITEM_FreeItem(decoded, PR_TRUE); |
michael@0 | 185 | return; |
michael@0 | 186 | } |
michael@0 | 187 | fprintf(outFile, "Decoded: \"%.*s\"\n", decoded->len, decoded->data); |
michael@0 | 188 | SECITEM_ZfreeItem(decoded, PR_TRUE); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | char dataString[MAX_STRING + 1]; |
michael@0 | 192 | |
michael@0 | 193 | int |
michael@0 | 194 | main (int argc, char **argv) |
michael@0 | 195 | { |
michael@0 | 196 | int retval = 0; /* 0 - test succeeded. -1 - test failed */ |
michael@0 | 197 | SECStatus rv; |
michael@0 | 198 | PLOptState *optstate; |
michael@0 | 199 | char *program_name; |
michael@0 | 200 | char *input_file = NULL; /* read encrypted data from here (or create) */ |
michael@0 | 201 | char *output_file = NULL; /* write new encrypted data here */ |
michael@0 | 202 | char *log_file = NULL; /* write new encrypted data here */ |
michael@0 | 203 | FILE *inFile = stdin; |
michael@0 | 204 | FILE *outFile = stdout; |
michael@0 | 205 | FILE *logFile = NULL; |
michael@0 | 206 | PLOptStatus optstatus; |
michael@0 | 207 | secuPWData pwdata = { PW_NONE, NULL }; |
michael@0 | 208 | |
michael@0 | 209 | |
michael@0 | 210 | program_name = PL_strrchr(argv[0], '/'); |
michael@0 | 211 | program_name = program_name ? (program_name + 1) : argv[0]; |
michael@0 | 212 | |
michael@0 | 213 | optstate = PL_CreateOptState (argc, argv, "Hd:f:i:o:l:p:?"); |
michael@0 | 214 | if (optstate == NULL) { |
michael@0 | 215 | SECU_PrintError (program_name, "PL_CreateOptState failed"); |
michael@0 | 216 | return 1; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) { |
michael@0 | 220 | switch (optstate->option) { |
michael@0 | 221 | case '?': |
michael@0 | 222 | short_usage (program_name); |
michael@0 | 223 | return 1; |
michael@0 | 224 | |
michael@0 | 225 | case 'H': |
michael@0 | 226 | long_usage (program_name); |
michael@0 | 227 | return 1; |
michael@0 | 228 | |
michael@0 | 229 | case 'd': |
michael@0 | 230 | SECU_ConfigDirectory(optstate->value); |
michael@0 | 231 | break; |
michael@0 | 232 | |
michael@0 | 233 | case 'i': |
michael@0 | 234 | input_file = PL_strdup(optstate->value); |
michael@0 | 235 | break; |
michael@0 | 236 | |
michael@0 | 237 | case 'o': |
michael@0 | 238 | output_file = PL_strdup(optstate->value); |
michael@0 | 239 | break; |
michael@0 | 240 | |
michael@0 | 241 | case 'l': |
michael@0 | 242 | log_file = PL_strdup(optstate->value); |
michael@0 | 243 | break; |
michael@0 | 244 | |
michael@0 | 245 | case 'f': |
michael@0 | 246 | pwdata.source = PW_FROMFILE; |
michael@0 | 247 | pwdata.data = PL_strdup(optstate->value); |
michael@0 | 248 | break; |
michael@0 | 249 | |
michael@0 | 250 | case 'p': |
michael@0 | 251 | pwdata.source = PW_PLAINTEXT; |
michael@0 | 252 | pwdata.data = PL_strdup(optstate->value); |
michael@0 | 253 | break; |
michael@0 | 254 | |
michael@0 | 255 | } |
michael@0 | 256 | } |
michael@0 | 257 | PL_DestroyOptState(optstate); |
michael@0 | 258 | if (optstatus == PL_OPT_BAD) { |
michael@0 | 259 | short_usage (program_name); |
michael@0 | 260 | return 1; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | if (input_file) { |
michael@0 | 264 | inFile = fopen(input_file,"r"); |
michael@0 | 265 | if (inFile == NULL) { |
michael@0 | 266 | perror(input_file); |
michael@0 | 267 | return 1; |
michael@0 | 268 | } |
michael@0 | 269 | PR_Free(input_file); |
michael@0 | 270 | } |
michael@0 | 271 | if (output_file) { |
michael@0 | 272 | outFile = fopen(output_file,"w+"); |
michael@0 | 273 | if (outFile == NULL) { |
michael@0 | 274 | perror(output_file); |
michael@0 | 275 | return 1; |
michael@0 | 276 | } |
michael@0 | 277 | PR_Free(output_file); |
michael@0 | 278 | } |
michael@0 | 279 | if (log_file) { |
michael@0 | 280 | if (log_file[0] == '-') |
michael@0 | 281 | logFile = stderr; |
michael@0 | 282 | else |
michael@0 | 283 | logFile = fopen(log_file,"w+"); |
michael@0 | 284 | if (logFile == NULL) { |
michael@0 | 285 | perror(log_file); |
michael@0 | 286 | return 1; |
michael@0 | 287 | } |
michael@0 | 288 | PR_Free(log_file); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | /* |
michael@0 | 292 | * Initialize the Security libraries. |
michael@0 | 293 | */ |
michael@0 | 294 | PK11_SetPasswordFunc(SECU_GetModulePassword); |
michael@0 | 295 | rv = NSS_Init(SECU_ConfigDirectory(NULL)); |
michael@0 | 296 | if (rv != SECSuccess) { |
michael@0 | 297 | SECU_PrintError (program_name, "NSS_Init failed"); |
michael@0 | 298 | retval = 1; |
michael@0 | 299 | goto prdone; |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | /* Get the encrypted result, either from the input file |
michael@0 | 303 | * or from encrypting the plaintext value |
michael@0 | 304 | */ |
michael@0 | 305 | while (fgets(dataString, sizeof dataString, inFile)) { |
michael@0 | 306 | unsigned char c = dataString[0]; |
michael@0 | 307 | |
michael@0 | 308 | if (c == 'M' && isBase64(dataString)) { |
michael@0 | 309 | doDecrypt(dataString, outFile, logFile, &pwdata); |
michael@0 | 310 | } else if (c == '~' && isBase64(dataString + 1)) { |
michael@0 | 311 | doDecode(dataString, outFile, logFile); |
michael@0 | 312 | } else { |
michael@0 | 313 | fputs(dataString, outFile); |
michael@0 | 314 | } |
michael@0 | 315 | } |
michael@0 | 316 | if (pwdata.data) |
michael@0 | 317 | PR_Free(pwdata.data); |
michael@0 | 318 | |
michael@0 | 319 | fclose(outFile); |
michael@0 | 320 | fclose(inFile); |
michael@0 | 321 | if (logFile && logFile != stderr) { |
michael@0 | 322 | fclose(logFile); |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | if (NSS_Shutdown() != SECSuccess) { |
michael@0 | 326 | SECU_PrintError (program_name, "NSS_Shutdown failed"); |
michael@0 | 327 | exit(1); |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | prdone: |
michael@0 | 331 | PR_Cleanup (); |
michael@0 | 332 | return retval; |
michael@0 | 333 | } |