/security/nss/cmd/pwdecrypt/pwdecrypt.c

http://github.com/zpao/v8monkey · C · 368 lines · 274 code · 43 blank · 51 comment · 65 complexity · a28efdf91ddafd49940c6f1dc604a63f MD5 · raw file

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