opensc/src/tools/openpgp-tool.c

417 lines
9.9 KiB
C
Raw Normal View History

2012-05-19 18:28:25 +00:00
/*
* openpgp-tool.c: OpenPGP card utility
*
* Copyright (C) 2012 Peter Marschall <peter@adpm.de>
*
* 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
*/
#include "config.h"
#include <stdio.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "common/compat_getopt.h"
#include "libopensc/opensc.h"
#include "libopensc/asn1.h"
#include "libopensc/cards.h"
#include "util.h"
2012-05-26 16:52:05 +00:00
#define OPT_RAW 256
#define OPT_PRETTY 257
2012-05-19 18:28:25 +00:00
/* define structures */
struct ef_name_map {
const char *name;
const char *env_name;
const char *ef;
char *(*prettify_value)(char *);
};
/* declare functions */
static void show_version(void);
2012-05-26 16:52:05 +00:00
static char *prettify_name(char *str);
2012-05-19 18:28:25 +00:00
static char *prettify_language(char *str);
static char *prettify_gender(char *str);
static void display_data(const struct ef_name_map *mapping, char *value);
static int decode_options(int argc, char **argv);
2012-05-26 16:52:05 +00:00
static int do_userinfo(sc_card_t *card);
2012-05-19 18:28:25 +00:00
static int read_transp(sc_card_t *card, const char *pathstring, unsigned char *buf, int buflen);
static void bintohex(char *buf, int len);
/* define global variables */
2012-05-26 16:52:05 +00:00
static int actions = 0;
2012-05-19 18:28:25 +00:00
static char *opt_reader = NULL;
static int opt_wait = 0;
2012-05-26 16:52:05 +00:00
static int opt_raw = 0;
static int verbose = 0;
static int opt_userinfo = 0;
static int opt_cardinfo = 0;
2012-05-19 18:28:25 +00:00
static char *exec_program = NULL;
2012-05-26 16:52:05 +00:00
static const char *app_name = "openpgp-tool";
2012-05-19 18:28:25 +00:00
static const struct option options[] = {
2012-05-26 16:52:05 +00:00
{ "reader", required_argument, NULL, 'r' },
{ "wait", no_argument, NULL, 'w' },
{ "exec", required_argument, NULL, 'x' },
{ "raw", no_argument, NULL, OPT_RAW },
{ "pretty", no_argument, NULL, OPT_PRETTY },
{ "card-info", no_argument, NULL, 'C' },
{ "user-info", no_argument, NULL, 'U' },
{ "help", no_argument, NULL, 'h' },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
2012-05-19 18:28:25 +00:00
{ NULL, 0, NULL, 0 }
};
2012-05-26 16:52:05 +00:00
static const char *option_help[] = {
/* r */ "Use reader number <arg> [0]",
/* w */ "Wait for card insertion",
/* x */ "Execute program <arg> with data in env vars",
"Print values in raw format",
"Print values in pretty format",
/* C */ NULL,
/* U */ "Show card holder information",
/* h */ "Print this help message",
/* v */ "Verbose operation. Use several times to enable debug output.",
/* V */ "Show version number"
};
2012-05-19 18:28:25 +00:00
static const struct ef_name_map openpgp_data[] = {
2012-05-26 16:52:05 +00:00
{ "Account", "OPENGPG_ACCOUNT", "3F00:005E", NULL },
{ "URL", "OPENPGP_URL", "3F00:5F50", NULL },
{ "Name", "OPENPGP_NAME", "3F00:0065:005B", prettify_name },
2012-05-19 18:28:25 +00:00
{ "Language", "OPENPGP_LANG", "3F00:0065:5F2D", prettify_language },
{ "Gender", "OPENPGP_GENDER", "3F00:0065:5F35", prettify_gender },
2012-05-26 16:52:05 +00:00
{ "DO 0101", "OPENPGP_DO0101", "3F00:0101", NULL },
{ "DO 0102", "OPENPGP_DO0102", "3F00:0102", NULL },
// { "DO 0103", "OPENPGP_DO0103", "3F00:0103", NULL },
// { "DO 0104", "OPENPGP_DO0104", "3F00:0104", NULL },
2012-05-19 18:28:25 +00:00
{ NULL, NULL, NULL, NULL }
};
static void show_version(void)
{
fprintf(stderr,
"openpgp-tool - OpenPGP card utility version " PACKAGE_VERSION "\n"
"\n"
"Copyright (c) 2012 Peter Marschall <peter@adpm.de>\n"
"Licensed under LGPL v2\n");
}
2012-05-26 16:52:05 +00:00
/* prettify card holder's name */
static char *prettify_name(char *str)
2012-05-19 18:28:25 +00:00
{
2012-05-26 16:52:05 +00:00
if (str != NULL) {
char *src = str;
char *dst = str;
while (*src != '\0') {
*dst = *src++;
if (*dst == '<') {
if (*src == '<')
src++;
*dst = ' ';
}
dst++;
}
*dst = '\0';
}
return str;
2012-05-19 18:28:25 +00:00
}
/* prettify language */
static char *prettify_language(char *str)
{
if (str != NULL) {
switch (strlen(str)) {
case 8: memmove(str+7, str+6, 1+strlen(str+6));
str[6] = ',';
/* fall through */
case 6: memmove(str+5, str+4, 1+strlen(str+4));
str[4] = ',';
/* fall through */
2012-05-26 16:52:05 +00:00
case 4: memmove(str+3, str+2, 1+strlen(str+2));
2012-05-19 18:28:25 +00:00
str[2] = ',';
/* fall through */
case 2: return str;
}
}
return NULL;
}
/* convert the raw ISO-5218 SEX value to an english word */
static char *prettify_gender(char *str)
{
if (str != NULL) {
switch (*str) {
case '0': return "unknown";
case '1': return "male";
case '2': return "female";
case '9': return "not applicable";
}
}
return NULL;
}
static void display_data(const struct ef_name_map *mapping, char *value)
{
if (mapping != NULL && value != NULL) {
2012-05-26 16:52:05 +00:00
if (mapping->prettify_value != NULL && !opt_raw)
2012-05-19 18:28:25 +00:00
value = mapping->prettify_value(value);
if (value != NULL) {
if (exec_program) {
char *envvar;
envvar = malloc(strlen(mapping->env_name) +
strlen(value) + 2);
if (envvar != NULL) {
strcpy(envvar, mapping->env_name);
strcat(envvar, "=");
strcat(envvar, value);
putenv(envvar);
}
} else {
const char *label = mapping->name;
printf("%s:%*s%s\n", label, 10-strlen(label), "", value);
}
}
}
}
static int decode_options(int argc, char **argv)
{
int c;
2012-05-26 16:52:05 +00:00
while ((c = getopt_long(argc, argv,"r:x:CUhwvV", options, (int *) 0)) != EOF) {
2012-05-19 18:28:25 +00:00
switch (c) {
case 'r':
opt_reader = optarg;
break;
case 'x':
if (exec_program)
free(exec_program);
exec_program = strdup(optarg);
break;
2012-05-26 16:52:05 +00:00
case OPT_RAW:
opt_raw = 1;
break;
case OPT_PRETTY:
opt_raw = 0;
2012-05-19 18:28:25 +00:00
break;
2012-05-26 16:52:05 +00:00
case 'C':
opt_cardinfo++;
actions++;;
break;
case 'U':
opt_userinfo++;
actions++;;
break;
case 'h':
util_print_usage_and_die(app_name, options, option_help);
2012-05-19 18:28:25 +00:00
break;
case 'w':
opt_wait = 1;
break;
2012-05-26 16:52:05 +00:00
case 'v':
verbose++;
break;
2012-05-19 18:28:25 +00:00
case 'V':
show_version();
exit(EXIT_SUCCESS);
break;
default:
2012-05-26 16:52:05 +00:00
util_print_usage_and_die(app_name, options, option_help);
2012-05-19 18:28:25 +00:00
}
}
2012-05-26 16:52:05 +00:00
return optind;
2012-05-19 18:28:25 +00:00
}
2012-05-26 16:52:05 +00:00
static int do_userinfo(sc_card_t *card)
2012-05-19 18:28:25 +00:00
{
int i;
unsigned char buf[2048];
for (i = 0; openpgp_data[i].ef != NULL; i++) {
sc_path_t path;
sc_file_t *file;
size_t count;
size_t offset = 0;
int r;
sc_format_path(openpgp_data[i].ef, &path);
r = sc_select_file(card, &path, &file);
if (r) {
fprintf(stderr, "Failed to select EF %s: %s\n",
openpgp_data[i].ef, sc_strerror(r));
2012-05-26 16:52:05 +00:00
return EXIT_FAILURE;
2012-05-19 18:28:25 +00:00
}
count = file->size;
while (count > 0) {
int c = count > sizeof(buf) ? sizeof(buf) : count;
r = sc_read_binary(card, offset, buf+offset, c, 0);
if (r < 0) {
fprintf(stderr, "%s: read failed - %s\n",
openpgp_data[i].ef, sc_strerror(r));
2012-05-26 16:52:05 +00:00
return EXIT_FAILURE;
2012-05-19 18:28:25 +00:00
}
if (r != c) {
fprintf(stderr, "%s: expecting %d, got only %d bytes\n",
openpgp_data[i].ef, c, r);
2012-05-26 16:52:05 +00:00
return EXIT_FAILURE;
2012-05-19 18:28:25 +00:00
}
offset += r;
count -= r;
}
buf[file->size] = '\0';
if (file->size > 0) {
display_data(openpgp_data + i, buf);
}
}
2012-05-26 16:52:05 +00:00
return EXIT_SUCCESS;
2012-05-19 18:28:25 +00:00
}
/* Select and read a transparent EF */
static int read_transp(sc_card_t *card, const char *pathstring, unsigned char *buf, int buflen)
{
sc_path_t path;
int r;
sc_format_path(pathstring, &path);
r = sc_select_file(card, &path, NULL);
if (r < 0)
fprintf(stderr, "\nFailed to select file %s: %s\n", pathstring, sc_strerror(r));
else {
r = sc_read_binary(card, 0, buf, buflen, 0);
if (r < 0)
fprintf(stderr, "\nFailed to read %s: %s\n", pathstring, sc_strerror(r));
}
return r;
}
/* Hex-encode the buf, 2*len+1 bytes must be reserved. E.g. {'1','2'} -> {'3','1','3','2','\0'} */
static void bintohex(char *buf, int len)
{
static const char hextable[] = "0123456789ABCDEF";
int i;
for (i = len - 1; i >= 0; i--) {
unsigned char c = (unsigned char) buf[i];
buf[2 * i + 1] = hextable[c % 16];
buf[2 * i] = hextable[c / 16];
}
}
int main(int argc, char **argv)
{
sc_context_t *ctx = NULL;
sc_context_param_t ctx_param;
sc_card_t *card = NULL;
int r;
2012-05-26 16:52:05 +00:00
int argind = 0;
int exit_status = EXIT_FAILURE;
2012-05-19 18:28:25 +00:00
2012-05-26 16:52:05 +00:00
/* decode options */
2012-05-19 18:28:25 +00:00
argind = decode_options(argc, argv);
/* connect to the card */
memset(&ctx_param, 0, sizeof(ctx_param));
ctx_param.ver = 0;
ctx_param.app_name = app_name;
r = sc_context_create(&ctx, &ctx_param);
if (r) {
2012-05-26 16:52:05 +00:00
util_fatal("failed to establish context: %s\n",
2012-05-19 18:28:25 +00:00
sc_strerror(r));
2012-05-26 16:52:05 +00:00
return EXIT_FAILURE;
}
if (verbose > 1) {
ctx->debug = verbose;
sc_ctx_log_to_file(ctx, "stderr");
2012-05-19 18:28:25 +00:00
}
2012-05-26 16:52:05 +00:00
r = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose);
2012-05-19 18:28:25 +00:00
if (r) {
2012-05-26 16:52:05 +00:00
util_fatal("failed to connect to card: %s\n",
2012-05-19 18:28:25 +00:00
sc_strerror(r));
2012-05-26 16:52:05 +00:00
return EXIT_FAILURE;
2012-05-19 18:28:25 +00:00
}
2012-05-26 16:52:05 +00:00
/* check card type */
2012-05-19 18:28:25 +00:00
if ((card->type != SC_CARD_TYPE_OPENPGP_V1) &&
(card->type != SC_CARD_TYPE_OPENPGP_V2)) {
2012-05-26 16:52:05 +00:00
util_error("not an OpenPGP card");
exit_status = EXIT_FAILURE;
2012-05-19 18:28:25 +00:00
goto out;
}
2012-05-26 16:52:05 +00:00
/* fail on too many arguments */
2012-05-19 18:28:25 +00:00
if (argind > argc)
2012-05-26 16:52:05 +00:00
util_print_usage_and_die(app_name, options, option_help);
2012-05-19 18:28:25 +00:00
2012-05-26 16:52:05 +00:00
/* set default action */
if (!actions)
opt_userinfo = 1;
if (opt_userinfo)
exit_status |= do_userinfo(card);
2012-05-19 18:28:25 +00:00
if (exec_program) {
char *const largv[] = {exec_program, NULL};
sc_unlock(card);
sc_disconnect_card(card);
sc_release_context(ctx);
execv(exec_program, largv);
/* we should not get here */
perror("execv()");
2012-05-26 16:52:05 +00:00
exit(EXIT_FAILURE);
2012-05-19 18:28:25 +00:00
}
out:
sc_unlock(card);
sc_disconnect_card(card);
sc_release_context(ctx);
2012-05-26 16:52:05 +00:00
2012-05-19 18:28:25 +00:00
exit(exit_status);
}