2001-11-22 15:40:51 +00:00
|
|
|
|
/*
|
2001-12-30 21:30:39 +00:00
|
|
|
|
* opensc-crypt.c: Tool for cryptography operations with SmartCards
|
2001-11-22 15:40:51 +00:00
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2001 Juha Yrj<EFBFBD>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
#include "util.h"
|
2001-12-30 21:17:34 +00:00
|
|
|
|
#include <stdio.h>
|
2001-11-22 15:40:51 +00:00
|
|
|
|
#include <stdlib.h>
|
2001-12-30 21:17:34 +00:00
|
|
|
|
#include <unistd.h>
|
2001-11-22 15:40:51 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2001-11-24 13:32:52 +00:00
|
|
|
|
#include <opensc.h>
|
|
|
|
|
#include <opensc-pkcs15.h>
|
2001-11-22 15:40:51 +00:00
|
|
|
|
|
|
|
|
|
int opt_reader = 0, opt_pin = 0, quiet = 0;
|
2001-12-20 12:16:05 +00:00
|
|
|
|
int opt_debug = 0;
|
2001-11-22 15:40:51 +00:00
|
|
|
|
char * opt_pincode = NULL, * opt_key_id = NULL;
|
|
|
|
|
char * opt_input = NULL, * opt_output = NULL;
|
|
|
|
|
int opt_hash_type = SC_PKCS15_HASH_NONE;
|
|
|
|
|
|
|
|
|
|
#define OPT_SHA1 0x101
|
|
|
|
|
|
|
|
|
|
const struct option options[] = {
|
|
|
|
|
{ "sign", 0, 0, 's' },
|
2001-12-15 01:27:51 +00:00
|
|
|
|
{ "decipher", 0, 0, 'c' },
|
2001-11-22 15:40:51 +00:00
|
|
|
|
{ "key", 1, 0, 'k' },
|
|
|
|
|
{ "reader", 1, 0, 'r' },
|
|
|
|
|
{ "input", 1, 0, 'i' },
|
|
|
|
|
{ "output", 1, 0, 'o' },
|
|
|
|
|
{ "sha-1", 0, 0, OPT_SHA1 },
|
|
|
|
|
{ "quiet", 0, 0, 'q' },
|
2001-12-15 01:27:51 +00:00
|
|
|
|
{ "debug", 0, 0, 'd' },
|
2001-11-22 15:40:51 +00:00
|
|
|
|
{ "pin", 1, 0, 'p' },
|
|
|
|
|
{ "pin-id", 1, &opt_pin, 0 },
|
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char *option_help[] = {
|
|
|
|
|
"Performs digital signature operation",
|
|
|
|
|
"Decipher operation",
|
|
|
|
|
"Selects the private key ID to use",
|
|
|
|
|
"Uses reader number <arg>",
|
|
|
|
|
"Selects the input file to use",
|
|
|
|
|
"Outputs to file <arg>",
|
|
|
|
|
"Input file is a SHA-1 hash",
|
|
|
|
|
"Quiet operation",
|
|
|
|
|
"Debug output -- may be supplied several times",
|
|
|
|
|
"Uses password (PIN) <arg>",
|
2002-01-08 13:56:50 +00:00
|
|
|
|
"The auth ID of the PIN to use",
|
2001-11-22 15:40:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct sc_context *ctx = NULL;
|
|
|
|
|
struct sc_card *card = NULL;
|
|
|
|
|
struct sc_pkcs15_card *p15card = NULL;
|
|
|
|
|
|
2001-12-22 20:52:57 +00:00
|
|
|
|
char * get_pin(struct sc_pkcs15_pin_info *pinfo)
|
2001-11-22 15:40:51 +00:00
|
|
|
|
{
|
|
|
|
|
char buf[80];
|
|
|
|
|
char *pincode;
|
|
|
|
|
|
|
|
|
|
if (opt_pincode != NULL)
|
2001-12-22 20:52:57 +00:00
|
|
|
|
return strdup(opt_pincode);
|
2001-11-22 15:40:51 +00:00
|
|
|
|
sprintf(buf, "Enter PIN [%s]: ", pinfo->com_attr.label);
|
|
|
|
|
while (1) {
|
|
|
|
|
pincode = getpass(buf);
|
|
|
|
|
if (strlen(pincode) == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
if (strlen(pincode) < pinfo->min_length ||
|
|
|
|
|
strlen(pincode) > pinfo->stored_length)
|
|
|
|
|
continue;
|
|
|
|
|
return pincode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-30 21:17:34 +00:00
|
|
|
|
int read_input(u8 *buf, int buflen)
|
2001-11-22 15:40:51 +00:00
|
|
|
|
{
|
|
|
|
|
FILE *inf;
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
inf = fopen(opt_input, "r");
|
|
|
|
|
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 (c < 0) {
|
|
|
|
|
perror("read");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int write_output(const u8 *buf, int len)
|
|
|
|
|
{
|
|
|
|
|
FILE *outf;
|
|
|
|
|
int output_binary = 1;
|
|
|
|
|
|
|
|
|
|
if (opt_output != NULL) {
|
|
|
|
|
outf = fopen(opt_output, "w");
|
|
|
|
|
if (outf == NULL) {
|
|
|
|
|
fprintf(stderr, "Unable to open '%s' for writing.\n", opt_output);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
outf = stdout;
|
|
|
|
|
output_binary = 0;
|
|
|
|
|
}
|
|
|
|
|
if (output_binary == 0)
|
2001-12-29 02:07:32 +00:00
|
|
|
|
print_binary(outf, buf, len);
|
2001-11-22 15:40:51 +00:00
|
|
|
|
else
|
|
|
|
|
fwrite(buf, len, 1, outf);
|
|
|
|
|
if (outf != stdout)
|
|
|
|
|
fclose(outf);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sign(struct sc_pkcs15_prkey_info *key)
|
|
|
|
|
{
|
|
|
|
|
u8 buf[1024], out[1024];
|
|
|
|
|
int r, c, len;
|
|
|
|
|
|
|
|
|
|
if (opt_input == NULL) {
|
|
|
|
|
fprintf(stderr, "No input file specified.\n");
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
if (opt_output == NULL) {
|
|
|
|
|
fprintf(stderr, "No output file specified.\n");
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
c = read_input(buf, sizeof(buf));
|
|
|
|
|
if (c < 0)
|
|
|
|
|
return 2;
|
|
|
|
|
len = sizeof(out);
|
|
|
|
|
r = sc_pkcs15_compute_signature(p15card, key, opt_hash_type,
|
|
|
|
|
buf, c, out, len);
|
|
|
|
|
if (r < 0) {
|
|
|
|
|
fprintf(stderr, "Compute signature failed: %s\n", sc_strerror(r));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
r = write_output(out, r);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int decipher(struct sc_pkcs15_prkey_info *key)
|
|
|
|
|
{
|
|
|
|
|
u8 buf[1024], out[1024];
|
|
|
|
|
int r, c, len;
|
|
|
|
|
|
|
|
|
|
if (opt_input == NULL) {
|
|
|
|
|
fprintf(stderr, "No input file specified.\n");
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
c = read_input(buf, sizeof(buf));
|
|
|
|
|
if (c < 0)
|
|
|
|
|
return 2;
|
|
|
|
|
len = sizeof(out);
|
|
|
|
|
r = sc_pkcs15_decipher(p15card, key, buf, c, out, len);
|
|
|
|
|
if (r < 0) {
|
|
|
|
|
fprintf(stderr, "Decrypt failed: %s\n", sc_strerror(r));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
r = write_output(out, r);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char * const argv[])
|
|
|
|
|
{
|
|
|
|
|
int err = 0, r, c, long_optind = 0;
|
|
|
|
|
int do_decipher = 0;
|
|
|
|
|
int do_sign = 0;
|
|
|
|
|
int action_count = 0;
|
|
|
|
|
struct sc_pkcs15_prkey_info *key;
|
|
|
|
|
struct sc_pkcs15_pin_info *pin;
|
|
|
|
|
struct sc_pkcs15_id id;
|
2001-12-22 20:52:57 +00:00
|
|
|
|
char *pincode;
|
2001-11-22 15:40:51 +00:00
|
|
|
|
|
|
|
|
|
while (1) {
|
2001-12-22 20:52:57 +00:00
|
|
|
|
c = getopt_long(argc, argv, "sck:r:i:o:qp:d", options, &long_optind);
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (c == -1)
|
|
|
|
|
break;
|
|
|
|
|
if (c == '?')
|
2002-01-08 13:56:50 +00:00
|
|
|
|
print_usage_and_die("opensc-crypt");
|
2001-11-22 15:40:51 +00:00
|
|
|
|
switch (c) {
|
|
|
|
|
case 's':
|
|
|
|
|
do_sign++;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
2001-12-15 01:27:51 +00:00
|
|
|
|
case 'c':
|
2001-11-22 15:40:51 +00:00
|
|
|
|
do_decipher++;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
|
|
|
|
case 'k':
|
|
|
|
|
opt_key_id = optarg;
|
|
|
|
|
action_count++;
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
opt_reader = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'i':
|
|
|
|
|
opt_input = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'o':
|
|
|
|
|
opt_output = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case OPT_SHA1:
|
|
|
|
|
opt_hash_type = SC_PKCS15_HASH_SHA1;
|
|
|
|
|
break;
|
|
|
|
|
case 'q':
|
|
|
|
|
quiet++;
|
|
|
|
|
break;
|
2001-12-15 01:27:51 +00:00
|
|
|
|
case 'd':
|
2001-12-20 12:16:05 +00:00
|
|
|
|
opt_debug++;
|
2001-11-22 15:40:51 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
opt_pincode = optarg;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (action_count == 0)
|
2002-01-08 13:56:50 +00:00
|
|
|
|
print_usage_and_die("opensc-crypt");
|
2001-11-22 15:40:51 +00:00
|
|
|
|
r = sc_establish_context(&ctx);
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2001-12-14 16:37:45 +00:00
|
|
|
|
ctx->use_std_output = 1;
|
2001-12-20 12:16:05 +00:00
|
|
|
|
ctx->debug = opt_debug;
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (opt_reader >= ctx->reader_count || opt_reader < 0) {
|
|
|
|
|
fprintf(stderr, "Illegal reader number. Only %d reader(s) configured.\n", ctx->reader_count);
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
if (sc_detect_card(ctx, opt_reader) != 1) {
|
|
|
|
|
fprintf(stderr, "Card not present.\n");
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
if (!quiet)
|
|
|
|
|
fprintf(stderr, "Connecting to card in reader %s...\n", ctx->readers[opt_reader]);
|
|
|
|
|
r = sc_connect_card(ctx, opt_reader, &card);
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "Failed to connect to card: %s\n", sc_strerror(r));
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#if 0
|
|
|
|
|
r = sc_lock(card);
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "Unable to lock card: %s\n", sc_strerror(r));
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (!quiet)
|
|
|
|
|
fprintf(stderr, "Trying to find a PKCS#15 compatible card...\n");
|
2001-12-22 20:52:57 +00:00
|
|
|
|
r = sc_pkcs15_bind(card, &p15card);
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "PKCS#15 initialization failed: %s\n", sc_strerror(r));
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
if (!quiet)
|
|
|
|
|
fprintf(stderr, "Found %s!\n", p15card->label);
|
|
|
|
|
|
|
|
|
|
r = sc_pkcs15_enum_private_keys(p15card);
|
|
|
|
|
if (r <= 0) {
|
|
|
|
|
if (r == 0)
|
|
|
|
|
r = SC_ERROR_OBJECT_NOT_FOUND;
|
|
|
|
|
fprintf(stderr, "Private key enumeration failed: %s\n", sc_strerror(r));
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
if (opt_key_id != NULL) {
|
|
|
|
|
sc_pkcs15_hex_string_to_id(opt_key_id, &id);
|
|
|
|
|
r = sc_pkcs15_find_prkey_by_id(p15card, &id, &key);
|
|
|
|
|
if (r < 0) {
|
|
|
|
|
fprintf(stderr, "Unable to find private key '%s': %s\n",
|
|
|
|
|
opt_key_id, sc_strerror(r));
|
|
|
|
|
err = 2;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
key = &p15card->prkey_info[0];
|
|
|
|
|
r = sc_pkcs15_find_pin_by_auth_id(p15card, &key->com_attr.auth_id, &pin);
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "Unable to find PIN code for private key: %s\n",
|
|
|
|
|
sc_strerror(r));
|
|
|
|
|
err = 1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
pincode = get_pin(pin);
|
|
|
|
|
if (pincode == NULL) {
|
|
|
|
|
err = 5;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
r = sc_pkcs15_verify_pin(p15card, pin, (const u8 *) pincode, strlen(pincode));
|
|
|
|
|
if (r) {
|
|
|
|
|
fprintf(stderr, "PIN code verification failed: %s\n", sc_strerror(r));
|
|
|
|
|
err = 5;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2001-12-22 20:52:57 +00:00
|
|
|
|
free(pincode);
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (!quiet)
|
|
|
|
|
fprintf(stderr, "PIN code correct.\n");
|
|
|
|
|
if (do_decipher) {
|
|
|
|
|
if ((err = decipher(key)))
|
|
|
|
|
goto end;
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
|
|
|
|
if (do_sign) {
|
|
|
|
|
if ((err = sign(key)))
|
|
|
|
|
goto end;
|
|
|
|
|
action_count--;
|
|
|
|
|
}
|
|
|
|
|
end:
|
|
|
|
|
if (p15card)
|
2001-12-22 20:52:57 +00:00
|
|
|
|
sc_pkcs15_unbind(p15card);
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (card) {
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#if 0
|
2001-11-22 15:40:51 +00:00
|
|
|
|
sc_unlock(card);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#endif
|
2001-11-22 15:40:51 +00:00
|
|
|
|
sc_disconnect_card(card);
|
|
|
|
|
}
|
|
|
|
|
if (ctx)
|
|
|
|
|
sc_destroy_context(ctx);
|
|
|
|
|
return err;
|
|
|
|
|
}
|