From 92e02b50dce9801dfdd713765dc35e6224e83f90 Mon Sep 17 00:00:00 2001 From: Chris Elledge Date: Wed, 20 Jan 2016 15:35:44 -0500 Subject: [PATCH 1/2] pkcs15-crypt: Allow the use of stdin if no input file is specified. --- src/tools/pkcs15-crypt.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/tools/pkcs15-crypt.c b/src/tools/pkcs15-crypt.c index 9bdd2c3a..7544e4cd 100644 --- a/src/tools/pkcs15-crypt.c +++ b/src/tools/pkcs15-crypt.c @@ -157,13 +157,19 @@ static int read_input(u8 *buf, int buflen) FILE *inf; int c; - inf = fopen(opt_input, "rb"); - if (inf == NULL) { - fprintf(stderr, "Unable to open '%s' for reading.\n", opt_input); - return -1; + if (opt_input==NULL) { + inf = stdin; + } else { + inf = fopen(opt_input, "rb"); + if (inf == NULL) { + fprintf(stderr, "Unable to open '%s' for reading.\n", opt_input); + return -1; + } } c = fread(buf, 1, buflen, inf); - fclose(inf); + if (inf!=stdin) { + fclose(inf); + } if (c < 0) { perror("read"); return -1; @@ -203,8 +209,7 @@ static int sign(struct sc_pkcs15_object *obj) int r, c, len; if (opt_input == NULL) { - fprintf(stderr, "No input file specified.\n"); - return 2; + fprintf(stderr, "No input file specified. Reading from stdin\n"); } c = read_input(buf, sizeof(buf)); @@ -258,8 +263,7 @@ static int decipher(struct sc_pkcs15_object *obj) int r, c, len; if (opt_input == NULL) { - fprintf(stderr, "No input file specified.\n"); - return 2; + fprintf(stderr, "No input file specified. Reading from stdin\n"); } c = read_input(buf, sizeof(buf)); if (c < 0) From c56378b8ba3b16c125ed39805017955181892886 Mon Sep 17 00:00:00 2001 From: Chris Elledge Date: Thu, 21 Jan 2016 11:30:26 -0500 Subject: [PATCH 2/2] pkcs15-crypt: Document defaults of stdin/stdout. --- doc/tools/pkcs15-crypt.1.xml | 6 ++++-- src/tools/pkcs15-crypt.c | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/tools/pkcs15-crypt.1.xml b/doc/tools/pkcs15-crypt.1.xml index 40dd262b..5418d4b1 100644 --- a/doc/tools/pkcs15-crypt.1.xml +++ b/doc/tools/pkcs15-crypt.1.xml @@ -62,7 +62,8 @@ file, file - Specifies the input file to use. + Specifies the input file to use. Defaults to stdin if + not specified. @@ -78,7 +79,8 @@ file, file - Any output will be sent to the specified file. + Any output will be sent to the specified file. Defaults + to stdout if not specified. diff --git a/src/tools/pkcs15-crypt.c b/src/tools/pkcs15-crypt.c index 7544e4cd..76b72737 100644 --- a/src/tools/pkcs15-crypt.c +++ b/src/tools/pkcs15-crypt.c @@ -88,8 +88,8 @@ static const char *option_help[] = { "Decipher operation", "Selects the private key ID to use", "Uses reader number ", - "Selects the input file to use", - "Outputs to file ", + "Selects the input file to use (defaults to stdin)", + "Outputs to file (defaults to stdout)", "Format for ECDSA signature : 'rs' (default), 'sequence', 'openssl'", "Outputs raw 8 bit data", "Input file is a SHA-1 hash",