2002-02-18 20:47:17 +00:00
|
|
|
|
/* card.c - SCdaemon card functions
|
|
|
|
|
* Copyright (C) 2002 Free Software Foundation, Inc.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2002-08-09 18:12:01 +00:00
|
|
|
|
#include <opensc/pkcs15.h>
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#endif
|
2002-02-28 11:07:59 +00:00
|
|
|
|
#include <ksba.h>
|
2002-02-18 20:47:17 +00:00
|
|
|
|
|
|
|
|
|
#include "scdaemon.h"
|
2002-03-18 09:30:35 +00:00
|
|
|
|
#include "card-common.h"
|
2002-02-18 20:47:17 +00:00
|
|
|
|
|
|
|
|
|
/* Map the SC error codes to the GNUPG ones */
|
2003-06-03 19:55:50 +00:00
|
|
|
|
gpg_error_t
|
2002-02-18 20:47:17 +00:00
|
|
|
|
map_sc_err (int rc)
|
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
gpg_err_code_t e;
|
|
|
|
|
|
2002-02-18 20:47:17 +00:00
|
|
|
|
switch (rc)
|
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
case 0: e = 0; break;
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2003-06-03 19:55:50 +00:00
|
|
|
|
case SC_ERROR_NOT_SUPPORTED: e = GPG_ERR_NOT_SUPPORTED; break;
|
|
|
|
|
case SC_ERROR_PKCS15_APP_NOT_FOUND: e = GPG_ERR_NO_PKCS15_APP; break;
|
|
|
|
|
case SC_ERROR_OUT_OF_MEMORY: e = GPG_ERR_ENOMEM; break;
|
|
|
|
|
case SC_ERROR_CARD_NOT_PRESENT: e = GPG_ERR_CARD_NOT_PRESENT; break;
|
|
|
|
|
case SC_ERROR_CARD_REMOVED: e = GPG_ERR_CARD_REMOVED; break;
|
2003-06-04 21:37:56 +00:00
|
|
|
|
case SC_ERROR_INVALID_CARD: e = GPG_ERR_INV_CARD; break;
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#endif
|
2003-06-04 21:37:56 +00:00
|
|
|
|
default: e = GPG_ERR_CARD; break;
|
2002-02-18 20:47:17 +00:00
|
|
|
|
}
|
2003-06-18 19:56:13 +00:00
|
|
|
|
return gpg_err_make (GPG_ERR_SOURCE_UNKNOWN, e);
|
2002-02-18 20:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-03-18 09:30:35 +00:00
|
|
|
|
/* Get the keygrip from CERT, return 0 on success */
|
|
|
|
|
int
|
|
|
|
|
card_help_get_keygrip (KsbaCert cert, unsigned char *array)
|
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
gcry_sexp_t s_pkey;
|
2002-03-18 09:30:35 +00:00
|
|
|
|
int rc;
|
|
|
|
|
KsbaSexp p;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
p = ksba_cert_get_public_key (cert);
|
|
|
|
|
if (!p)
|
|
|
|
|
return -1; /* oops */
|
|
|
|
|
n = gcry_sexp_canon_len (p, 0, NULL, NULL);
|
|
|
|
|
if (!n)
|
|
|
|
|
return -1; /* libksba did not return a proper S-expression */
|
|
|
|
|
rc = gcry_sexp_sscan ( &s_pkey, NULL, p, n);
|
|
|
|
|
xfree (p);
|
|
|
|
|
if (rc)
|
|
|
|
|
return -1; /* can't parse that S-expression */
|
|
|
|
|
array = gcry_pk_get_keygrip (s_pkey, array);
|
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
|
if (!array)
|
|
|
|
|
return -1; /* failed to calculate the keygrip */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-18 20:47:17 +00:00
|
|
|
|
|
2002-03-18 09:30:35 +00:00
|
|
|
|
|
2002-02-18 20:47:17 +00:00
|
|
|
|
/* Create a new context for the card and figures out some basic
|
2002-03-18 09:30:35 +00:00
|
|
|
|
information of the card. Detects whgether a PKCS_15 application is
|
2002-02-18 20:47:17 +00:00
|
|
|
|
stored.
|
|
|
|
|
|
2003-06-03 19:55:50 +00:00
|
|
|
|
Common errors: GPG_ERR_CARD_NOT_PRESENT */
|
2002-02-18 20:47:17 +00:00
|
|
|
|
int
|
|
|
|
|
card_open (CARD *rcard)
|
|
|
|
|
{
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2002-02-18 20:47:17 +00:00
|
|
|
|
CARD card;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
card = xtrycalloc (1, sizeof *card);
|
|
|
|
|
if (!card)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return out_of_core ();
|
2002-02-18 20:47:17 +00:00
|
|
|
|
card->reader = 0;
|
|
|
|
|
|
2002-03-28 11:43:24 +00:00
|
|
|
|
rc = sc_establish_context (&card->ctx, "scdaemon");
|
2002-02-18 20:47:17 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to establish SC context: %s\n", sc_strerror (rc));
|
|
|
|
|
rc = map_sc_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (card->reader >= card->ctx->reader_count)
|
|
|
|
|
{
|
|
|
|
|
log_error ("no card reader available\n");
|
2003-06-04 21:37:56 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_CARD);
|
2002-04-10 11:45:11 +00:00
|
|
|
|
goto leave;
|
2002-02-18 20:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
card->ctx->error_file = log_get_stream ();
|
2002-04-12 18:55:05 +00:00
|
|
|
|
card->ctx->debug = opt.debug_sc;
|
|
|
|
|
card->ctx->debug_file = log_get_stream ();
|
|
|
|
|
|
2002-02-28 11:07:59 +00:00
|
|
|
|
if (sc_detect_card_presence (card->ctx->reader[card->reader], 0) != 1)
|
2002-02-18 20:47:17 +00:00
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_CARD_NOT_PRESENT);
|
2002-02-18 20:47:17 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-28 11:07:59 +00:00
|
|
|
|
rc = sc_connect_card (card->ctx->reader[card->reader], 0, &card->scard);
|
2002-02-18 20:47:17 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("failed to connect card in reader %d: %s\n",
|
|
|
|
|
card->reader, sc_strerror (rc));
|
|
|
|
|
rc = map_sc_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("connected to card in reader %d using driver `%s'\n",
|
|
|
|
|
card->reader, card->scard->driver->name);
|
|
|
|
|
|
|
|
|
|
rc = sc_lock (card->scard);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't lock card in reader %d: %s\n",
|
|
|
|
|
card->reader, sc_strerror (rc));
|
|
|
|
|
rc = map_sc_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
if (rc)
|
|
|
|
|
card_close (card);
|
|
|
|
|
else
|
|
|
|
|
*rcard = card;
|
2003-04-29 19:08:35 +00:00
|
|
|
|
|
2002-02-18 20:47:17 +00:00
|
|
|
|
return rc;
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#else
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_NOT_SUPPORTED);
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#endif
|
2002-02-18 20:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Close a card and release all resources */
|
|
|
|
|
void
|
|
|
|
|
card_close (CARD card)
|
|
|
|
|
{
|
|
|
|
|
if (card)
|
|
|
|
|
{
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2002-02-18 20:47:17 +00:00
|
|
|
|
if (card->p15card)
|
|
|
|
|
{
|
|
|
|
|
sc_pkcs15_unbind (card->p15card);
|
|
|
|
|
card->p15card = NULL;
|
|
|
|
|
}
|
2002-08-16 10:33:31 +00:00
|
|
|
|
if (card->p15priv)
|
|
|
|
|
p15_release_private_data (card);
|
2002-02-18 20:47:17 +00:00
|
|
|
|
if (card->scard)
|
|
|
|
|
{
|
|
|
|
|
sc_unlock (card->scard);
|
2002-02-28 11:07:59 +00:00
|
|
|
|
sc_disconnect_card (card->scard, 0);
|
2002-02-18 20:47:17 +00:00
|
|
|
|
card->scard = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (card->ctx)
|
|
|
|
|
{
|
2002-03-28 11:43:24 +00:00
|
|
|
|
sc_release_context (card->ctx);
|
2002-02-18 20:47:17 +00:00
|
|
|
|
card->ctx = NULL;
|
|
|
|
|
}
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#endif
|
2002-02-18 20:47:17 +00:00
|
|
|
|
xfree (card);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-09 18:12:01 +00:00
|
|
|
|
/* Locate a simple TLV encoded data object in BUFFER of LENGTH and
|
|
|
|
|
return a pointer to value as well as its length in NBYTES. Return
|
|
|
|
|
NULL if it was not found. Note, that the function does not check
|
|
|
|
|
whether the value fits into the provided buffer. */
|
2003-07-28 08:59:18 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2002-08-09 18:12:01 +00:00
|
|
|
|
static const char *
|
|
|
|
|
find_simple_tlv (const unsigned char *buffer, size_t length,
|
|
|
|
|
int tag, size_t *nbytes)
|
|
|
|
|
{
|
|
|
|
|
const char *s = buffer;
|
|
|
|
|
size_t n = length;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
buffer = s;
|
|
|
|
|
if (n < 2)
|
|
|
|
|
return NULL; /* buffer too short for tag and length. */
|
|
|
|
|
len = s[1];
|
|
|
|
|
s += 2; n -= 2;
|
|
|
|
|
if (len == 255)
|
|
|
|
|
{
|
|
|
|
|
if (n < 2)
|
|
|
|
|
return NULL; /* we expected 2 more bytes with the length. */
|
|
|
|
|
len = (s[0] << 8) | s[1];
|
|
|
|
|
s += 2; n -= 2;
|
|
|
|
|
}
|
|
|
|
|
if (*buffer == tag)
|
|
|
|
|
{
|
|
|
|
|
*nbytes = len;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
if (len > n)
|
|
|
|
|
return NULL; /* buffer too short to skip to the next tag. */
|
|
|
|
|
s += len; n -= len;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-07-28 08:59:18 +00:00
|
|
|
|
#endif /*HAVE_OPENSC*/
|
2002-08-09 18:12:01 +00:00
|
|
|
|
|
|
|
|
|
/* Find the ICC Serial Number within the provided BUFFER of LENGTH
|
|
|
|
|
(which should contain the GDO file) and return it as a hex encoded
|
|
|
|
|
string and allocated string in SERIAL. Return an error code when
|
|
|
|
|
the ICCSN was not found. */
|
2003-07-28 08:59:18 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2002-08-09 18:12:01 +00:00
|
|
|
|
static int
|
|
|
|
|
find_iccsn (const unsigned char *buffer, size_t length, char **serial)
|
|
|
|
|
{
|
|
|
|
|
size_t n;
|
|
|
|
|
const unsigned char *s;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
s = find_simple_tlv (buffer, length, 0x5A, &n);
|
|
|
|
|
if (!s)
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD);
|
2002-08-09 18:12:01 +00:00
|
|
|
|
length -= s - buffer;
|
|
|
|
|
if (n > length)
|
|
|
|
|
{
|
|
|
|
|
/* Oops, it does not fit into the buffer. This is an invalid
|
|
|
|
|
encoding (or the buffer is too short. However, I have some
|
|
|
|
|
test cards with such an invalid encoding and therefore I use
|
|
|
|
|
this ugly workaround to return something I can further
|
|
|
|
|
experiment with. */
|
|
|
|
|
if (n == 0x0D && length+1 == n)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("enabling BMI testcard workaround\n");
|
|
|
|
|
n--;
|
|
|
|
|
}
|
|
|
|
|
else
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD); /* Bad encoding; does
|
|
|
|
|
not fit into buffer. */
|
2002-08-09 18:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
if (!n)
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD); /* Well, that is too short. */
|
2002-08-09 18:12:01 +00:00
|
|
|
|
|
|
|
|
|
*serial = p = xtrymalloc (2*n+1);
|
|
|
|
|
if (!*serial)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return out_of_core ();
|
2002-08-09 18:12:01 +00:00
|
|
|
|
for (; n; n--, p += 2, s++)
|
|
|
|
|
sprintf (p, "%02X", *s);
|
|
|
|
|
*p = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2003-07-28 08:59:18 +00:00
|
|
|
|
#endif /*HAVE_OPENSC*/
|
2002-03-18 09:30:35 +00:00
|
|
|
|
|
2002-02-18 20:47:17 +00:00
|
|
|
|
/* Retrieve the serial number and the time of the last update of the
|
|
|
|
|
card. The serial number is returned as a malloced string (hex
|
|
|
|
|
encoded) in SERIAL and the time of update is returned in STAMP.
|
|
|
|
|
If no update time is available the returned value is 0. The serial
|
|
|
|
|
is mandatory for a PKCS_15 application and an error will be
|
|
|
|
|
returned if this value is not availbale. For non-PKCS-15 cards a
|
|
|
|
|
serial number is constructed by other means. Caller must free
|
2002-03-18 09:30:35 +00:00
|
|
|
|
SERIAL unless the function returns an error. */
|
2002-02-18 20:47:17 +00:00
|
|
|
|
int
|
|
|
|
|
card_get_serial_and_stamp (CARD card, char **serial, time_t *stamp)
|
|
|
|
|
{
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2002-03-18 09:30:35 +00:00
|
|
|
|
int rc;
|
2002-03-28 11:43:24 +00:00
|
|
|
|
struct sc_path path;
|
|
|
|
|
struct sc_file *file;
|
2002-08-09 18:12:01 +00:00
|
|
|
|
unsigned char buf[256];
|
|
|
|
|
int buflen;
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#endif
|
2002-02-18 20:47:17 +00:00
|
|
|
|
|
|
|
|
|
if (!card || !serial || !stamp)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2002-02-18 20:47:17 +00:00
|
|
|
|
|
|
|
|
|
*serial = NULL;
|
|
|
|
|
*stamp = 0; /* not available */
|
2002-03-18 09:30:35 +00:00
|
|
|
|
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#ifdef HAVE_OPENSC
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.initialized)
|
|
|
|
|
{
|
|
|
|
|
card->fnc.initialized = 1;
|
|
|
|
|
/* The first use of this card tries to figure out the type of the card
|
|
|
|
|
and sets up the function pointers. */
|
|
|
|
|
rc = sc_pkcs15_bind (card->scard, &card->p15card);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
if (rc != SC_ERROR_PKCS15_APP_NOT_FOUND)
|
|
|
|
|
log_error ("binding of existing PKCS-15 failed in reader %d: %s\n",
|
|
|
|
|
card->reader, sc_strerror (rc));
|
|
|
|
|
card->p15card = NULL;
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
if (card->p15card)
|
|
|
|
|
card_p15_bind (card);
|
|
|
|
|
else
|
|
|
|
|
card_dinsig_bind (card);
|
|
|
|
|
card->fnc.initialized = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-28 11:43:24 +00:00
|
|
|
|
/* We should lookup the iso 7812-1 and 8583-3 - argh ISO
|
|
|
|
|
practice is suppressing innovation - IETF rules! So we
|
2002-04-12 18:55:05 +00:00
|
|
|
|
always get the serialnumber from the 2F02 GDO file. */
|
|
|
|
|
/* FIXME: in case we can't parse the 2F02 EF and we have a P15 card,
|
|
|
|
|
we should get the serial number from the respective P15 file */
|
2002-03-28 11:43:24 +00:00
|
|
|
|
sc_format_path ("3F002F02", &path);
|
|
|
|
|
rc = sc_select_file (card->scard, &path, &file);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("sc_select_file failed: %s\n", sc_strerror (rc));
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD);
|
2002-03-28 11:43:24 +00:00
|
|
|
|
}
|
|
|
|
|
if (file->type != SC_FILE_TYPE_WORKING_EF
|
|
|
|
|
|| file->ef_structure != SC_FILE_EF_TRANSPARENT)
|
|
|
|
|
{
|
|
|
|
|
log_error ("wrong type or structure of GDO file\n");
|
2002-03-18 09:30:35 +00:00
|
|
|
|
sc_file_free (file);
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD);
|
2002-03-28 11:43:24 +00:00
|
|
|
|
}
|
2002-08-09 18:12:01 +00:00
|
|
|
|
|
|
|
|
|
if (!file->size || file->size >= DIM(buf) )
|
2002-03-28 11:43:24 +00:00
|
|
|
|
{ /* FIXME: Use a real parser */
|
2002-08-09 18:12:01 +00:00
|
|
|
|
log_error ("unsupported size of GDO file (%d)\n", file->size);
|
2002-03-28 11:43:24 +00:00
|
|
|
|
sc_file_free (file);
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD);
|
2002-03-28 11:43:24 +00:00
|
|
|
|
}
|
2002-08-09 18:12:01 +00:00
|
|
|
|
buflen = file->size;
|
2002-03-28 11:43:24 +00:00
|
|
|
|
|
2002-08-09 18:12:01 +00:00
|
|
|
|
rc = sc_read_binary (card->scard, 0, buf, buflen, 0);
|
2002-03-28 11:43:24 +00:00
|
|
|
|
sc_file_free (file);
|
|
|
|
|
if (rc < 0)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error reading GDO file: %s\n", sc_strerror (rc));
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD);
|
2002-03-28 11:43:24 +00:00
|
|
|
|
}
|
2002-08-09 18:12:01 +00:00
|
|
|
|
if (rc != buflen)
|
2002-03-28 11:43:24 +00:00
|
|
|
|
{
|
|
|
|
|
log_error ("short read on GDO file\n");
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD);
|
2002-03-28 11:43:24 +00:00
|
|
|
|
}
|
2002-08-09 18:12:01 +00:00
|
|
|
|
|
|
|
|
|
rc = find_iccsn (buf, buflen, serial);
|
2003-06-04 21:37:56 +00:00
|
|
|
|
if (gpg_err_code (rc) == GPG_ERR_CARD)
|
2002-08-09 18:12:01 +00:00
|
|
|
|
log_error ("invalid structure of GDO file\n");
|
|
|
|
|
if (!rc && card->p15card && !strcmp (*serial, "D27600000000000000000000"))
|
|
|
|
|
{ /* This is a German card with a silly serial number. Try to get
|
|
|
|
|
the serial number from the EF(TokenInfo). We indicate such a
|
|
|
|
|
serial number by the using the prefix: "FF0100". */
|
|
|
|
|
const char *efser = card->p15card->serial_number;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
if (!efser)
|
|
|
|
|
efser = "";
|
|
|
|
|
|
|
|
|
|
xfree (*serial);
|
|
|
|
|
*serial = NULL;
|
|
|
|
|
p = xtrymalloc (strlen (efser) + 7);
|
|
|
|
|
if (!p)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = out_of_core ();
|
2002-08-09 18:12:01 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strcpy (p, "FF0100");
|
|
|
|
|
strcpy (p+6, efser);
|
|
|
|
|
*serial = p;
|
|
|
|
|
}
|
2002-02-18 20:47:17 +00:00
|
|
|
|
}
|
2002-08-09 18:12:01 +00:00
|
|
|
|
else if (!rc && **serial == 'F' && (*serial)[1] == 'F')
|
|
|
|
|
{ /* The serial number starts with our special prefix. This
|
|
|
|
|
requires that we put our default prefix "FF0000" in front. */
|
|
|
|
|
char *p = xtrymalloc (strlen (*serial) + 7);
|
|
|
|
|
if (!p)
|
|
|
|
|
{
|
|
|
|
|
xfree (*serial);
|
|
|
|
|
*serial = NULL;
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = out_of_core ();
|
2002-08-09 18:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strcpy (p, "FF0000");
|
|
|
|
|
strcpy (p+6, *serial);
|
|
|
|
|
xfree (*serial);
|
|
|
|
|
*serial = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#else
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_NOT_SUPPORTED);
|
2003-04-29 19:08:35 +00:00
|
|
|
|
#endif
|
2002-02-18 20:47:17 +00:00
|
|
|
|
}
|
2002-02-28 11:07:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Enumerate all keypairs on the card and return the Keygrip as well
|
|
|
|
|
as the internal identification of the key. KEYGRIP must be a
|
|
|
|
|
caller provided buffer with a size of 20 bytes which will receive
|
|
|
|
|
the KEYGRIP of the keypair. If KEYID is not NULL, it returns the
|
2002-03-18 09:30:35 +00:00
|
|
|
|
ID field of the key in allocated memory; this is a string without
|
|
|
|
|
spaces. The function returns -1 when all keys have been
|
2003-06-03 19:55:50 +00:00
|
|
|
|
enumerated. Note that the error GPG_ERR_MISSING_CERTIFICATE may be
|
2002-02-28 11:07:59 +00:00
|
|
|
|
returned if there is just the private key but no public key (ie.e a
|
|
|
|
|
certificate) available. Applications might want to continue
|
|
|
|
|
enumerating after this error.*/
|
|
|
|
|
int
|
|
|
|
|
card_enum_keypairs (CARD card, int idx,
|
|
|
|
|
unsigned char *keygrip,
|
2002-03-18 09:30:35 +00:00
|
|
|
|
char **keyid)
|
2002-02-28 11:07:59 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
if (keyid)
|
|
|
|
|
*keyid = NULL;
|
|
|
|
|
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card || !keygrip)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2002-02-28 11:07:59 +00:00
|
|
|
|
if (idx < 0)
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_INDEX);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.initialized)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.enum_keypairs)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
rc = card->fnc.enum_keypairs (card, idx, keygrip, keyid);
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("card operation enum_keypairs result: %s\n",
|
2003-06-18 19:56:13 +00:00
|
|
|
|
gpg_strerror (rc));
|
2002-03-18 09:30:35 +00:00
|
|
|
|
return rc;
|
2002-02-28 11:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-16 14:23:40 +00:00
|
|
|
|
/* Enumerate all trusted certificates available on the card, return
|
|
|
|
|
their ID in CERT and the type in CERTTYPE. Types of certificates
|
|
|
|
|
are:
|
|
|
|
|
0 := Unknown
|
|
|
|
|
100 := Regular X.509 cert
|
|
|
|
|
101 := Trusted X.509 cert
|
|
|
|
|
102 := Useful X.509 cert
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
card_enum_certs (CARD card, int idx, char **certid, int *certtype)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
if (certid)
|
|
|
|
|
*certid = NULL;
|
|
|
|
|
|
|
|
|
|
if (!card)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2002-08-16 14:23:40 +00:00
|
|
|
|
if (idx < 0)
|
2003-06-04 21:37:56 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_INDEX);
|
2002-08-16 14:23:40 +00:00
|
|
|
|
if (!card->fnc.initialized)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
|
2002-08-16 14:23:40 +00:00
|
|
|
|
if (!card->fnc.enum_certs)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
|
2002-08-16 14:23:40 +00:00
|
|
|
|
rc = card->fnc.enum_certs (card, idx, certid, certtype);
|
|
|
|
|
if (opt.verbose)
|
|
|
|
|
log_info ("card operation enum_certs result: %s\n",
|
2003-06-18 19:56:13 +00:00
|
|
|
|
gpg_strerror (rc));
|
2002-08-16 14:23:40 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-28 11:07:59 +00:00
|
|
|
|
/* Read the certificate identified by CERTIDSTR which is the
|
|
|
|
|
hexadecimal encoded ID of the certificate, prefixed with the string
|
|
|
|
|
"3F005015.". The certificate is return in DER encoded form in CERT
|
|
|
|
|
and NCERT. */
|
|
|
|
|
int
|
|
|
|
|
card_read_cert (CARD card, const char *certidstr,
|
|
|
|
|
unsigned char **cert, size_t *ncert)
|
|
|
|
|
{
|
2002-03-04 10:34:09 +00:00
|
|
|
|
int rc;
|
2002-02-28 11:07:59 +00:00
|
|
|
|
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card || !certidstr || !cert || !ncert)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.initialized)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.read_cert)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
rc = card->fnc.read_cert (card, certidstr, cert, ncert);
|
|
|
|
|
if (opt.verbose)
|
2003-06-18 19:56:13 +00:00
|
|
|
|
log_info ("card operation read_cert result: %s\n", gpg_strerror (rc));
|
2002-03-18 09:30:35 +00:00
|
|
|
|
return rc;
|
2002-02-28 11:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-05 17:17:08 +00:00
|
|
|
|
/* Create the signature and return the allocated result in OUTDATA.
|
|
|
|
|
If a PIN is required the PINCB will be used to ask for the PIN; it
|
|
|
|
|
should return the PIN in an allocated buffer and put it into PIN. */
|
2002-03-04 10:34:09 +00:00
|
|
|
|
int
|
2002-03-18 09:30:35 +00:00
|
|
|
|
card_sign (CARD card, const char *keyidstr, int hashalgo,
|
|
|
|
|
int (pincb)(void*, const char *, char **),
|
|
|
|
|
void *pincb_arg,
|
|
|
|
|
const void *indata, size_t indatalen,
|
2003-07-03 18:10:13 +00:00
|
|
|
|
unsigned char **outdata, size_t *outdatalen )
|
2002-03-04 10:34:09 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card || !indata || !indatalen || !outdata || !outdatalen || !pincb)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.initialized)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.sign)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
rc = card->fnc.sign (card, keyidstr, hashalgo,
|
|
|
|
|
pincb, pincb_arg,
|
|
|
|
|
indata, indatalen,
|
|
|
|
|
outdata, outdatalen);
|
|
|
|
|
if (opt.verbose)
|
2003-06-18 19:56:13 +00:00
|
|
|
|
log_info ("card operation sign result: %s\n", gpg_strerror (rc));
|
2002-03-04 10:34:09 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
2002-02-28 11:07:59 +00:00
|
|
|
|
|
|
|
|
|
|
2002-03-06 09:01:12 +00:00
|
|
|
|
/* Create the signature and return the allocated result in OUTDATA.
|
|
|
|
|
If a PIN is required the PINCB will be used to ask for the PIN; it
|
|
|
|
|
should return the PIN in an allocated buffer and put it into PIN. */
|
|
|
|
|
int
|
|
|
|
|
card_decipher (CARD card, const char *keyidstr,
|
|
|
|
|
int (pincb)(void*, const char *, char **),
|
|
|
|
|
void *pincb_arg,
|
|
|
|
|
const void *indata, size_t indatalen,
|
2003-07-03 18:10:13 +00:00
|
|
|
|
unsigned char **outdata, size_t *outdatalen )
|
2002-03-06 09:01:12 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card || !indata || !indatalen || !outdata || !outdatalen || !pincb)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.initialized)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_CARD_NOT_INITIALIZED);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
if (!card->fnc.decipher)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_OPERATION);
|
2002-03-18 09:30:35 +00:00
|
|
|
|
rc = card->fnc.decipher (card, keyidstr,
|
|
|
|
|
pincb, pincb_arg,
|
|
|
|
|
indata, indatalen,
|
|
|
|
|
outdata, outdatalen);
|
|
|
|
|
if (opt.verbose)
|
2003-06-18 19:56:13 +00:00
|
|
|
|
log_info ("card operation decipher result: %s\n", gpg_strerror (rc));
|
2002-03-06 09:01:12 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
2003-06-27 20:53:09 +00:00
|
|
|
|
|