diff options
author | nelsonb%netscape.com <devnull@localhost> | 2004-01-22 22:08:59 +0000 |
---|---|---|
committer | nelsonb%netscape.com <devnull@localhost> | 2004-01-22 22:08:59 +0000 |
commit | 91adfb9f606fd68c83f36ee109cf2375f9910222 (patch) | |
tree | 436e516ca89c70204d9256a53d57a4e49ad70cef | |
parent | e46199d2130404d38c2e4082b8c2de9349c6741b (diff) | |
download | nss-hg-91adfb9f606fd68c83f36ee109cf2375f9910222.tar.gz |
Add -a option for Base64 encoded ASCII input and output.
The -i and -o options now understand "-" to mean stdin and stdout.
Usage is displayed when -? or no arguments are given.
Bug 231536. r=relyea.
-rw-r--r-- | security/nss/cmd/sdrtest/sdrtest.c | 235 |
1 files changed, 169 insertions, 66 deletions
diff --git a/security/nss/cmd/sdrtest/sdrtest.c b/security/nss/cmd/sdrtest/sdrtest.c index d54e6b5fc..66b772ef3 100644 --- a/security/nss/cmd/sdrtest/sdrtest.c +++ b/security/nss/cmd/sdrtest/sdrtest.c @@ -46,26 +46,27 @@ #include "plgetopt.h" #include "pk11sdr.h" +#include "nssb64.h" #define DEFAULT_VALUE "Test" +static const char default_value[] = { DEFAULT_VALUE }; + +PRFileDesc *pr_stderr; +PRBool verbose = PR_FALSE; static void synopsis (char *program_name) { - PRFileDesc *pr_stderr; - - pr_stderr = PR_STDERR; - PR_fprintf (pr_stderr, "Usage:"); - PR_fprintf (pr_stderr, - "\t%s [-i <input-file>] [-o <output-file>] [-r <text>] [-d <dir>]\n", - program_name); + PR_fprintf (pr_stderr, +"Usage: %s [-d <dir>] [-v] [-t <text>] [-a] -i <input-file>\n" +" %s [-d <dir>] [-v] [-t <text>] [-a] -o <output-file>\n", + program_name, program_name); } - static void short_usage (char *program_name) { - PR_fprintf (PR_STDERR, + PR_fprintf (pr_stderr, "Type %s -H for more detailed descriptions\n", program_name); synopsis (program_name); @@ -75,9 +76,6 @@ short_usage (char *program_name) static void long_usage (char *program_name) { - PRFileDesc *pr_stderr; - - pr_stderr = PR_STDERR; synopsis (program_name); PR_fprintf (pr_stderr, "\nSecret Decoder Test:\n"); PR_fprintf (pr_stderr, @@ -94,34 +92,106 @@ long_usage (char *program_name) "-d dbdir"); } +int +readStdin(SECItem * result) +{ + int bufsize = 0; + int cc; + int wanted = 8192; + + result->len = 0; + result->data = NULL; + do { + if (bufsize < wanted) { + unsigned char * tmpData = (unsigned char *)realloc(result->data, wanted); + if (!tmpData) { + if (verbose) PR_fprintf(pr_stderr, "Allocation of buffer failed\n"); + return -1; + } + result->data = tmpData; + bufsize = wanted; + } + cc = PR_Read(PR_STDIN, result->data + result->len, bufsize - result->len); + if (cc > 0) { + result->len += (unsigned)cc; + if (result->len >= wanted) + wanted *= 2; + } + } while (cc > 0); + return cc; +} + +int +readInputFile(const char * filename, SECItem * result) +{ + PRFileDesc *file /* = PR_OpenFile(input_file, 0) */; + PRFileInfo info; + PRStatus s; + PRInt32 count; + int retval = -1; + + file = PR_Open(filename, PR_RDONLY, 0); + if (!file) { + if (verbose) PR_fprintf(pr_stderr, "Open of file %s failed\n", filename); + goto loser; + } + + s = PR_GetOpenFileInfo(file, &info); + if (s != PR_SUCCESS) { + if (verbose) PR_fprintf(pr_stderr, "File info operation failed\n"); + goto file_loser; + } + + result->len = info.size; + result->data = (unsigned char *)malloc(result->len); + if (!result->data) { + if (verbose) PR_fprintf(pr_stderr, "Allocation of buffer failed\n"); + goto file_loser; + } + + count = PR_Read(file, result->data, result->len); + if (count != result->len) { + if (verbose) PR_fprintf(pr_stderr, "Read failed\n"); + goto file_loser; + } + retval = 0; + +file_loser: + PR_Close(file); +loser: + return retval; +} + int main (int argc, char **argv) { int retval = 0; /* 0 - test succeeded. -1 - test failed */ SECStatus rv; PLOptState *optstate; + PLOptStatus optstatus; char *program_name; const char *input_file = NULL; /* read encrypted data from here (or create) */ const char *output_file = NULL; /* write new encrypted data here */ - const char *value = DEFAULT_VALUE; /* Use this for plaintext */ + const char *value = default_value; /* Use this for plaintext */ SECItem data; - SECItem result; + SECItem result = {0, 0, 0}; SECItem text; - PRBool verbose = PR_FALSE; + PRBool ascii = PR_FALSE; + pr_stderr = PR_STDERR; result.data = 0; text.data = 0; text.len = 0; program_name = PL_strrchr(argv[0], '/'); program_name = program_name ? (program_name + 1) : argv[0]; - optstate = PL_CreateOptState (argc, argv, "Hd:i:o:t:v"); + optstate = PL_CreateOptState (argc, argv, "?Had:i:o:t:v"); if (optstate == NULL) { SECU_PrintError (program_name, "PL_CreateOptState failed"); return -1; } - while (PL_GetNextOpt (optstate) == PL_OPT_OK) { + while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) { switch (optstate->option) { case '?': short_usage (program_name); @@ -131,6 +201,10 @@ main (int argc, char **argv) long_usage (program_name); return retval; + case 'a': + ascii = PR_TRUE; + break; + case 'd': SECU_ConfigDirectory(optstate->value); break; @@ -152,6 +226,16 @@ main (int argc, char **argv) break; } } + PL_DestroyOptState(optstate); + if (optstatus == PL_OPT_BAD) { + short_usage (program_name); + return -1; + } + if (!output_file && !input_file && value == default_value) { + short_usage (program_name); + PR_fprintf (pr_stderr, "Must specify at least one of -t, -i or -o \n"); + return -1; + } /* * Initialize the Security libraries. @@ -164,6 +248,7 @@ main (int argc, char **argv) rv = NSS_Init(SECU_ConfigDirectory(NULL)); } if (rv != SECSuccess) { + SECU_PrintError(program_name, "NSS_Init failed"); retval = -1; goto prdone; } @@ -177,49 +262,34 @@ main (int argc, char **argv) */ if (input_file) { - PRFileDesc *file /* = PR_OpenFile(input_file, 0) */; - PRFileInfo info; - PRStatus s; - PRInt32 count; - if (verbose) printf("Reading data from %s\n", input_file); - file = PR_Open(input_file, PR_RDONLY, 0); - if (!file) { - if (verbose) printf("Open of file failed\n"); - retval = -1; - goto loser; - } - - s = PR_GetOpenFileInfo(file, &info); - if (s != PR_SUCCESS) { - if (verbose) printf("File info operation failed\n"); - retval = -1; - goto file_loser; + if (!strcmp(input_file, "-")) { + retval = readStdin(&result); + ascii = PR_TRUE; + } else { + retval = readInputFile(input_file, &result); } - - result.len = info.size; - result.data = (unsigned char *)malloc(result.len); - if (!result.data) { - if (verbose) printf("Allocation of buffer failed\n"); - retval = -1; - goto file_loser; - } - - count = PR_Read(file, result.data, result.len); - if (count != result.len) { - if (verbose) printf("Read failed\n"); - retval = -1; - goto file_loser; + if (retval != 0) + goto loser; + if (ascii) { + /* input was base64 encoded. Decode it. */ + SECItem newResult = {0, 0, 0}; + SECItem *ok = NSSBase64_DecodeBuffer(NULL, &newResult, + (const char *)result.data, result.len); + if (!ok) { + SECU_PrintError(program_name, "Base 64 decode failed"); + retval = -1; + goto loser; + } + free(result.data); + result = *ok; } - -file_loser: - PR_Close(file); - if (retval != 0) goto loser; } else { SECItem keyid = { 0, 0, 0 }; + SECItem outBuf = { 0, 0, 0 }; PK11SlotInfo *slot = NULL; /* sigh, initialize the key database */ @@ -238,55 +308,88 @@ file_loser: rv = PK11SDR_Encrypt(&keyid, &data, &result, 0); if (rv != SECSuccess) { - if (verbose) printf("Encrypt operation failed\n"); + if (verbose) + SECU_PrintError(program_name, "Encrypt operation failed\n"); retval = -1; goto loser; } if (verbose) printf("Encrypted result is %d bytes long\n", result.len); + if (!strcmp(output_file, "-")) { + ascii = PR_TRUE; + } + + if (ascii) { + /* base64 encode output. */ + char * newResult = NSSBase64_EncodeItem(NULL, NULL, 0, &result); + if (!newResult) { + SECU_PrintError(program_name, "Base 64 encode failed\n"); + retval = -1; + goto loser; + } + outBuf.data = (unsigned char *)newResult; + outBuf.len = strlen(newResult); + if (verbose) + printf("Base 64 encoded result is %d bytes long\n", outBuf.len); + } else { + outBuf = result; + } + /* -v printf("Result is %.*s\n", text.len, text.data); */ - if (output_file) - { + if (output_file) { PRFileDesc *file; PRInt32 count; if (verbose) printf("Writing result to %s\n", output_file); - - /* Write to file */ - file = PR_Open(output_file, PR_CREATE_FILE|PR_WRONLY, 0666); + if (!strcmp(output_file, "-")) { + file = PR_STDOUT; + } else { + /* Write to file */ + file = PR_Open(output_file, PR_CREATE_FILE|PR_WRONLY, 0666); + } if (!file) { - if (verbose) printf("Open of output file failed\n"); + if (verbose) + SECU_PrintError(program_name, + "Open of output file %s failed\n", + output_file); retval = -1; goto loser; } - count = PR_Write(file, result.data, result.len); + count = PR_Write(file, outBuf.data, outBuf.len); - PR_Close(file); + if (file == PR_STDOUT) { + puts(""); + } else { + PR_Close(file); + } - if (count != result.len) { - if (verbose) printf("Write failed\n"); + if (count != outBuf.len) { + if (verbose) SECU_PrintError(program_name, "Write failed\n"); retval = -1; goto loser; } + if (ascii) { + free(outBuf.data); + } } } /* Decrypt the value */ rv = PK11SDR_Decrypt(&result, &text, 0); if (rv != SECSuccess) { - if (verbose) printf("Decrypt operation failed\n"); + if (verbose) SECU_PrintError(program_name, "Decrypt operation failed\n"); retval = -1; goto loser; } - if (verbose) printf("Decrypted result is %.*s\n", text.len, text.data); + if (verbose) printf("Decrypted result is \"%.*s\"\n", text.len, text.data); /* Compare to required value */ if (text.len != data.len || memcmp(data.data, text.data, text.len) != 0) { - if (verbose) printf("Comparison failed\n"); + if (verbose) PR_fprintf(pr_stderr, "Comparison failed\n"); retval = -1; goto loser; } |