2002-02-28 11:05:57 +00:00
|
|
|
/* divert-scd.c - divert operations to the scdaemon
|
2003-06-27 20:53:09 +00:00
|
|
|
* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
|
2002-02-28 11:05:57 +00:00
|
|
|
*
|
|
|
|
* 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 <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "agent.h"
|
2002-03-04 10:34:51 +00:00
|
|
|
#include "sexp-parse.h"
|
2002-08-21 08:18:23 +00:00
|
|
|
#include "i18n.h"
|
2002-03-04 10:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2003-01-09 12:59:25 +00:00
|
|
|
ask_for_card (CTRL ctrl, const unsigned char *shadow_info, char **r_kid)
|
2002-03-04 10:34:51 +00:00
|
|
|
{
|
|
|
|
int rc, i;
|
|
|
|
const unsigned char *s;
|
|
|
|
size_t n;
|
|
|
|
char *serialno;
|
|
|
|
int no_card = 0;
|
|
|
|
char *desc;
|
|
|
|
char *want_sn, *want_kid;
|
2002-03-28 11:42:40 +00:00
|
|
|
int want_sn_displen;
|
2002-03-04 10:34:51 +00:00
|
|
|
|
|
|
|
*r_kid = NULL;
|
|
|
|
s = shadow_info;
|
|
|
|
if (*s != '(')
|
2003-06-04 21:37:56 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2002-03-04 10:34:51 +00:00
|
|
|
s++;
|
|
|
|
n = snext (&s);
|
|
|
|
if (!n)
|
2003-06-04 21:37:56 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2002-03-06 14:16:37 +00:00
|
|
|
want_sn = xtrymalloc (n*2+1);
|
2002-03-04 10:34:51 +00:00
|
|
|
if (!want_sn)
|
2003-06-03 19:55:50 +00:00
|
|
|
return out_of_core ();
|
2002-03-06 14:16:37 +00:00
|
|
|
for (i=0; i < n; i++)
|
|
|
|
sprintf (want_sn+2*i, "%02X", s[i]);
|
2002-03-04 10:34:51 +00:00
|
|
|
s += n;
|
2002-03-28 11:42:40 +00:00
|
|
|
/* We assume that a 20 byte serial number is a standard one which
|
|
|
|
seems to have the property to have a zero in the last nibble. We
|
|
|
|
don't display this '0' because it may confuse the user */
|
|
|
|
want_sn_displen = strlen (want_sn);
|
|
|
|
if (want_sn_displen == 20 && want_sn[19] == '0')
|
|
|
|
want_sn_displen--;
|
2002-03-04 10:34:51 +00:00
|
|
|
|
|
|
|
n = snext (&s);
|
|
|
|
if (!n)
|
2003-06-04 21:37:56 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2002-03-04 10:34:51 +00:00
|
|
|
want_kid = xtrymalloc (n+1);
|
|
|
|
if (!want_kid)
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
gpg_error_t tmperr = out_of_core ();
|
2002-03-04 10:34:51 +00:00
|
|
|
xfree (want_sn);
|
2003-06-03 19:55:50 +00:00
|
|
|
return tmperr;
|
2002-03-04 10:34:51 +00:00
|
|
|
}
|
|
|
|
memcpy (want_kid, s, n);
|
|
|
|
want_kid[n] = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
rc = agent_card_serialno (&serialno);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
log_debug ("detected card with S/N %s\n", serialno);
|
|
|
|
i = strcmp (serialno, want_sn);
|
|
|
|
xfree (serialno);
|
|
|
|
serialno = NULL;
|
|
|
|
if (!i)
|
|
|
|
{
|
|
|
|
xfree (want_sn);
|
|
|
|
*r_kid = want_kid;
|
|
|
|
return 0; /* yes, we have the correct card */
|
|
|
|
}
|
|
|
|
}
|
2003-06-03 19:55:50 +00:00
|
|
|
else if (gpg_err_code (rc) == GPG_ERR_CARD_NOT_PRESENT)
|
2002-03-04 10:34:51 +00:00
|
|
|
{
|
|
|
|
log_debug ("no card present\n");
|
|
|
|
rc = 0;
|
|
|
|
no_card = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
log_error ("error accesing card: %s\n", gpg_strerror (rc));
|
2002-03-04 10:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
if (asprintf (&desc,
|
|
|
|
"%s:%%0A%%0A"
|
2002-03-28 11:42:40 +00:00
|
|
|
" \"%.*s\"",
|
2002-03-04 10:34:51 +00:00
|
|
|
no_card? "Please insert the card with serial number"
|
|
|
|
: "Please remove the current card and "
|
|
|
|
"insert the one with serial number",
|
2002-03-28 11:42:40 +00:00
|
|
|
want_sn_displen, want_sn) < 0)
|
2002-03-04 10:34:51 +00:00
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
rc = out_of_core ();
|
2002-03-04 10:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-01-09 12:59:25 +00:00
|
|
|
rc = agent_get_confirmation (ctrl, desc, NULL, NULL);
|
2002-03-04 10:34:51 +00:00
|
|
|
free (desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
xfree (want_sn);
|
|
|
|
xfree (want_kid);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-02-28 11:05:57 +00:00
|
|
|
|
|
|
|
|
2002-03-28 11:42:40 +00:00
|
|
|
/* Put the DIGEST into an DER encoded comtainer and return it in R_VAL. */
|
2002-03-05 17:14:45 +00:00
|
|
|
static int
|
|
|
|
encode_md_for_card (const unsigned char *digest, size_t digestlen, int algo,
|
2002-03-28 11:42:40 +00:00
|
|
|
unsigned char **r_val, size_t *r_len)
|
2002-03-05 17:14:45 +00:00
|
|
|
{
|
|
|
|
byte *frame;
|
|
|
|
byte asn[100];
|
|
|
|
size_t asnlen;
|
|
|
|
|
|
|
|
asnlen = DIM(asn);
|
|
|
|
if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
|
|
|
|
{
|
|
|
|
log_error ("no object identifier for algo %d\n", algo);
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
2002-03-05 17:14:45 +00:00
|
|
|
}
|
|
|
|
|
2002-03-28 11:42:40 +00:00
|
|
|
frame = xtrymalloc (asnlen + digestlen);
|
2002-03-05 17:14:45 +00:00
|
|
|
if (!frame)
|
2003-06-03 19:55:50 +00:00
|
|
|
return out_of_core ();
|
2002-03-28 11:42:40 +00:00
|
|
|
memcpy (frame, asn, asnlen);
|
|
|
|
memcpy (frame+asnlen, digest, digestlen);
|
2002-03-05 17:14:45 +00:00
|
|
|
if (DBG_CRYPTO)
|
2002-03-28 11:42:40 +00:00
|
|
|
log_printhex ("encoded hash:", frame, asnlen+digestlen);
|
2002-03-05 17:14:45 +00:00
|
|
|
|
|
|
|
*r_val = frame;
|
2002-03-28 11:42:40 +00:00
|
|
|
*r_len = asnlen+digestlen;
|
2002-03-05 17:14:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Callback used to ask for the PIN which should be set into BUF. The
|
|
|
|
buf has been allocated by the caller and is of size MAXBUF which
|
|
|
|
includes the terminating null. The function should return an UTF-8
|
2002-06-17 10:11:34 +00:00
|
|
|
string with the passphrase, the buffer may optionally be padded
|
2002-03-05 17:14:45 +00:00
|
|
|
with arbitrary characters */
|
|
|
|
static int
|
|
|
|
getpin_cb (void *opaque, const char *info, char *buf, size_t maxbuf)
|
|
|
|
{
|
|
|
|
struct pin_entry_info_s *pi;
|
|
|
|
int rc;
|
2002-08-21 08:18:23 +00:00
|
|
|
char *desc;
|
2003-01-09 12:59:25 +00:00
|
|
|
CTRL ctrl = opaque;
|
2002-03-05 17:14:45 +00:00
|
|
|
|
|
|
|
if (maxbuf < 2)
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
2002-03-05 17:14:45 +00:00
|
|
|
|
2002-08-21 08:18:23 +00:00
|
|
|
|
|
|
|
/* FIXME: keep PI and TRIES in OPAQUE. Frankly this is a whole
|
|
|
|
mess because we should call the card's verify function from the
|
2002-06-17 10:11:34 +00:00
|
|
|
pinentry check pin CB. */
|
2002-03-05 17:14:45 +00:00
|
|
|
pi = gcry_calloc_secure (1, sizeof (*pi) + 100);
|
|
|
|
pi->max_length = maxbuf-1;
|
|
|
|
pi->min_digits = 0; /* we want a real passphrase */
|
|
|
|
pi->max_digits = 8;
|
|
|
|
pi->max_tries = 3;
|
|
|
|
|
2002-08-21 08:18:23 +00:00
|
|
|
if ( asprintf (&desc, _("Please enter the PIN%s%s%s to unlock the card"),
|
|
|
|
info? " (`":"",
|
|
|
|
info? info:"",
|
|
|
|
info? "')":"") < 0)
|
|
|
|
desc = NULL;
|
2003-01-09 12:59:25 +00:00
|
|
|
rc = agent_askpin (ctrl, desc?desc:info, pi);
|
2002-08-21 08:18:23 +00:00
|
|
|
free (desc);
|
2002-06-17 10:11:34 +00:00
|
|
|
if (!rc)
|
2002-03-05 17:14:45 +00:00
|
|
|
{
|
2002-06-17 10:11:34 +00:00
|
|
|
strncpy (buf, pi->pin, maxbuf-1);
|
|
|
|
buf[maxbuf-1] = 0;
|
2002-03-05 17:14:45 +00:00
|
|
|
}
|
|
|
|
xfree (pi);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-28 11:05:57 +00:00
|
|
|
|
|
|
|
int
|
2003-01-09 12:59:25 +00:00
|
|
|
divert_pksign (CTRL ctrl,
|
|
|
|
const unsigned char *digest, size_t digestlen, int algo,
|
2002-03-06 14:16:37 +00:00
|
|
|
const unsigned char *shadow_info, unsigned char **r_sig)
|
2002-02-28 11:05:57 +00:00
|
|
|
{
|
2002-03-04 10:34:51 +00:00
|
|
|
int rc;
|
|
|
|
char *kid;
|
2002-03-05 17:14:45 +00:00
|
|
|
size_t siglen;
|
|
|
|
char *sigval;
|
|
|
|
unsigned char *data;
|
|
|
|
size_t ndata;
|
2002-03-04 10:34:51 +00:00
|
|
|
|
2003-01-09 12:59:25 +00:00
|
|
|
rc = ask_for_card (ctrl, shadow_info, &kid);
|
2002-03-04 10:34:51 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2002-03-28 11:42:40 +00:00
|
|
|
rc = encode_md_for_card (digest, digestlen, algo,
|
2002-03-05 17:14:45 +00:00
|
|
|
&data, &ndata);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2003-01-09 12:59:25 +00:00
|
|
|
rc = agent_card_pksign (kid, getpin_cb, ctrl,
|
2002-03-05 17:14:45 +00:00
|
|
|
data, ndata, &sigval, &siglen);
|
|
|
|
if (!rc)
|
|
|
|
*r_sig = sigval;
|
|
|
|
xfree (data);
|
2002-03-04 10:34:51 +00:00
|
|
|
xfree (kid);
|
2002-03-05 17:14:45 +00:00
|
|
|
|
|
|
|
return rc;
|
2002-02-28 11:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-06 09:01:12 +00:00
|
|
|
/* Decrypt the the value given asn an S-expression in CIPHER using the
|
|
|
|
key identified by SHADOW_INFO and return the plaintext in an
|
|
|
|
allocated buffer in R_BUF. */
|
|
|
|
int
|
2003-01-09 12:59:25 +00:00
|
|
|
divert_pkdecrypt (CTRL ctrl,
|
|
|
|
const unsigned char *cipher,
|
2002-03-06 14:16:37 +00:00
|
|
|
const unsigned char *shadow_info,
|
2002-03-06 09:01:12 +00:00
|
|
|
char **r_buf, size_t *r_len)
|
2002-02-28 11:05:57 +00:00
|
|
|
{
|
2002-03-04 10:34:51 +00:00
|
|
|
int rc;
|
|
|
|
char *kid;
|
2002-03-06 09:01:12 +00:00
|
|
|
const unsigned char *s;
|
|
|
|
size_t n;
|
|
|
|
const unsigned char *ciphertext;
|
|
|
|
size_t ciphertextlen;
|
|
|
|
char *plaintext;
|
|
|
|
size_t plaintextlen;
|
|
|
|
|
|
|
|
s = cipher;
|
|
|
|
if (*s != '(')
|
2003-06-04 21:37:56 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
s++;
|
|
|
|
n = snext (&s);
|
|
|
|
if (!n)
|
2003-06-04 21:37:56 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
if (!smatch (&s, n, "enc-val"))
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_UNKNOWN_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
if (*s != '(')
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_UNKNOWN_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
s++;
|
|
|
|
n = snext (&s);
|
|
|
|
if (!n)
|
2003-06-04 21:37:56 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
if (!smatch (&s, n, "rsa"))
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
|
2002-03-06 09:01:12 +00:00
|
|
|
if (*s != '(')
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_UNKNOWN_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
s++;
|
|
|
|
n = snext (&s);
|
|
|
|
if (!n)
|
2003-06-04 21:37:56 +00:00
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
if (!smatch (&s, n, "a"))
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_UNKNOWN_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
n = snext (&s);
|
|
|
|
if (!n)
|
2003-06-03 19:55:50 +00:00
|
|
|
return gpg_error (GPG_ERR_UNKNOWN_SEXP);
|
2002-03-06 09:01:12 +00:00
|
|
|
ciphertext = s;
|
|
|
|
ciphertextlen = n;
|
2002-03-04 10:34:51 +00:00
|
|
|
|
2003-01-09 12:59:25 +00:00
|
|
|
rc = ask_for_card (ctrl, shadow_info, &kid);
|
2002-03-04 10:34:51 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2003-01-09 12:59:25 +00:00
|
|
|
rc = agent_card_pkdecrypt (kid, getpin_cb, ctrl,
|
2002-03-06 09:01:12 +00:00
|
|
|
ciphertext, ciphertextlen,
|
|
|
|
&plaintext, &plaintextlen);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
*r_buf = plaintext;
|
|
|
|
*r_len = plaintextlen;
|
|
|
|
}
|
2002-03-04 10:34:51 +00:00
|
|
|
xfree (kid);
|
2002-03-06 09:01:12 +00:00
|
|
|
return rc;
|
2002-02-28 11:05:57 +00:00
|
|
|
}
|
2003-06-27 20:53:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
divert_generic_cmd (CTRL ctrl, const char *cmdline, void *assuan_context)
|
|
|
|
{
|
|
|
|
return agent_card_scd (cmdline, getpin_cb, ctrl, assuan_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|