116
116
* base64 table only used to identify the end of a base64 string
118
118
static unsigned char b64[256] = {
119
/* 0: */ 0, 0, 0, 0, 0, 0, 0, 0,
120
/* 8: */ 0, 0, 0, 0, 0, 0, 0, 0,
121
/* 16: */ 0, 0, 0, 0, 0, 0, 0, 0,
122
/* 24: */ 0, 0, 0, 0, 0, 0, 0, 0,
123
/* 32: */ 0, 0, 0, 0, 0, 0, 0, 0,
124
/* 40: */ 0, 0, 0, 1, 0, 0, 0, 1,
125
/* 48: */ 1, 1, 1, 1, 1, 1, 1, 1,
126
/* 56: */ 1, 1, 0, 0, 0, 0, 0, 0,
127
/* 64: */ 0, 1, 1, 1, 1, 1, 1, 1,
128
/* 72: */ 1, 1, 1, 1, 1, 1, 1, 1,
129
/* 80: */ 1, 1, 1, 1, 1, 1, 1, 1,
130
/* 88: */ 1, 1, 1, 0, 0, 0, 0, 0,
131
/* 96: */ 0, 1, 1, 1, 1, 1, 1, 1,
132
/* 104: */ 1, 1, 1, 1, 1, 1, 1, 1,
133
/* 112: */ 1, 1, 1, 1, 1, 1, 1, 1,
134
/* 120: */ 1, 1, 1, 0, 0, 0, 0, 0,
135
/* 128: */ 0, 0, 0, 0, 0, 0, 0, 0
119
/* 00: */ 0, 0, 0, 0, 0, 0, 0, 0,
120
/* 08: */ 0, 0, 0, 0, 0, 0, 0, 0,
121
/* 10: */ 0, 0, 0, 0, 0, 0, 0, 0,
122
/* 18: */ 0, 0, 0, 0, 0, 0, 0, 0,
123
/* 20: */ 0, 0, 0, 0, 0, 0, 0, 0,
124
/* 28: */ 0, 0, 0, 1, 0, 0, 0, 1,
125
/* 30: */ 1, 1, 1, 1, 1, 1, 1, 1,
126
/* 38: */ 1, 1, 0, 0, 0, 0, 0, 0,
127
/* 40: */ 0, 1, 1, 1, 1, 1, 1, 1,
128
/* 48: */ 1, 1, 1, 1, 1, 1, 1, 1,
129
/* 50: */ 1, 1, 1, 1, 1, 1, 1, 1,
130
/* 58: */ 1, 1, 1, 0, 0, 0, 0, 0,
131
/* 60: */ 0, 1, 1, 1, 1, 1, 1, 1,
132
/* 68: */ 1, 1, 1, 1, 1, 1, 1, 1,
133
/* 70: */ 1, 1, 1, 1, 1, 1, 1, 1,
134
/* 78: */ 1, 1, 1, 0, 0, 0, 0, 0,
144
isatobchar(int c) { return b64[c] != 0; }
147
#define MAX_STRING 256
149
getData(FILE *inFile,char **inString) {
151
int space = MAX_STRING;
152
int oneequal = false;
154
char *string = (char *) malloc(space);
158
while ((c = getc(inFile)) != EOF) {
163
newString = (char *)realloc(string,space);
164
if (newString == NULL) {
171
if (!isatobchar(c)) {
192
string = (char *)realloc(string,space);
142
#define isatobchar(c) (b64[c])
144
#define MAX_STRING 8192
147
isBase64(char *inString)
152
for (i = 0; (c = inString[i]) != 0 && isatobchar(c); ++i)
155
while ((c = inString[++i]) == '=')
156
; /* skip trailing '=' characters */
158
if (c && c != '\n' && c != '\r')
166
doDecrypt(char * dataString, FILE *outFile, FILE *logFile, secuPWData *pwdata)
168
int strLen = strlen(dataString);
169
SECItem *decoded = NSSBase64_DecodeBuffer(NULL, NULL, dataString, strLen);
173
SECItem result = { siBuffer, NULL, 0 };
175
if ((decoded == NULL) || (decoded->len == 0)) {
177
err = PORT_GetError();
178
fprintf(logFile,"Base 64 decode failed on <%s>\n", dataString);
179
fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err));
181
fputs(dataString, outFile);
183
SECITEM_FreeItem(decoded, PR_TRUE);
187
rv = PK11SDR_Decrypt(decoded, &result, pwdata);
188
SECITEM_ZfreeItem(decoded, PR_TRUE);
189
if (rv == SECSuccess) {
190
/* result buffer has no extra space for a NULL */
191
fprintf(outFile, "Decrypted: \"%.*s\"\n", result.len, result.data);
192
SECITEM_ZfreeItem(&result, PR_FALSE);
195
/* Encryption failed. output raw input. */
197
err = PORT_GetError();
198
fprintf(logFile,"SDR decrypt failed on <%s>\n", dataString);
199
fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err));
201
fputs(dataString,outFile);
205
doDecode(char * dataString, FILE *outFile, FILE *logFile)
207
int strLen = strlen(dataString + 1);
210
decoded = NSSBase64_DecodeBuffer(NULL, NULL, dataString + 1, strLen);
211
if ((decoded == NULL) || (decoded->len == 0)) {
213
int err = PORT_GetError();
214
fprintf(logFile,"Base 64 decode failed on <%s>\n", dataString + 1);
215
fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err));
217
fputs(dataString, outFile);
219
SECITEM_FreeItem(decoded, PR_TRUE);
222
fprintf(outFile, "Decoded: \"%.*s\"\n", decoded->len, decoded->data);
223
SECITEM_ZfreeItem(decoded, PR_TRUE);
226
char dataString[MAX_STRING + 1];
200
229
main (int argc, char **argv)
308
337
/* Get the encrypted result, either from the input file
309
338
* or from encrypting the plaintext value
312
while ((c = getc(inFile)) != EOF) {
314
char *dataString = NULL;
317
rv = getData(inFile, &dataString);
319
fputs(dataString,outFile);
323
inText = NSSBase64_DecodeBuffer(NULL, NULL, dataString,
325
if ((inText == NULL) || (inText->len == 0)) {
327
fprintf(logFile,"Base 64 decode failed on <%s>\n",
329
fprintf(logFile," Error %x: %s\n",PORT_GetError(),
330
SECU_Strerror(PORT_GetError()));
332
fputs(dataString,outFile);
338
rv = PK11SDR_Decrypt(inText, &result, &pwdata);
339
SECITEM_FreeItem(inText, PR_TRUE);
340
if (rv != SECSuccess) {
342
fprintf(logFile,"SDR decrypt failed on <%s>\n",
344
fprintf(logFile," Error %x: %s\n",PORT_GetError(),
345
SECU_Strerror(PORT_GetError()));
347
fputs(dataString,outFile);
349
SECITEM_ZfreeItem(&result, PR_FALSE);
352
/* result buffer has no extra space for a NULL */
353
fprintf(outFile, "%.*s", result.len, result.data);
354
SECITEM_ZfreeItem(&result, PR_FALSE);
340
while (fgets(dataString, sizeof dataString, inFile)) {
341
unsigned char c = dataString[0];
343
if (c == 'M' && isBase64(dataString)) {
344
doDecrypt(dataString, outFile, logFile, &pwdata);
345
} else if (c == '~' && isBase64(dataString + 1)) {
346
doDecode(dataString, outFile, logFile);
348
fputs(dataString, outFile);
352
PR_Free(pwdata.data);
356
if (logFile && logFile != stderr) {
366
360
if (NSS_Shutdown() != SECSuccess) {
367
361
SECU_PrintError (program_name, "NSS_Shutdown failed");