2003-08-05 17:11:04 +00:00
|
|
|
/* learncard.c - Handle the LEARN command
|
2009-03-20 19:04:47 +00:00
|
|
|
* Copyright (C) 2002, 2003, 2004, 2009 Free Software Foundation, Inc.
|
2003-08-05 17:11:04 +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
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 17:11:04 +00:00
|
|
|
* (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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-08-05 17:11:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "agent.h"
|
|
|
|
#include <assuan.h>
|
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Structures used by the callback mechanism to convey information
|
|
|
|
pertaining to key pairs. */
|
2011-02-04 12:57:53 +01:00
|
|
|
struct keypair_info_s
|
2009-03-20 19:04:47 +00:00
|
|
|
{
|
2003-08-05 17:11:04 +00:00
|
|
|
struct keypair_info_s *next;
|
|
|
|
int no_cert;
|
2005-02-22 18:08:28 +00:00
|
|
|
char *id; /* points into grip */
|
|
|
|
char hexgrip[1]; /* The keygrip (i.e. a hash over the public key
|
|
|
|
parameters) formatted as a hex string.
|
|
|
|
Allocated somewhat large to also act as
|
2018-10-24 15:56:18 -04:00
|
|
|
memory for the above ID field. */
|
2003-08-05 17:11:04 +00:00
|
|
|
};
|
|
|
|
typedef struct keypair_info_s *KEYPAIR_INFO;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
struct kpinfo_cb_parm_s
|
2009-03-20 19:04:47 +00:00
|
|
|
{
|
|
|
|
ctrl_t ctrl;
|
2003-08-05 17:11:04 +00:00
|
|
|
int error;
|
|
|
|
KEYPAIR_INFO info;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Structures used by the callback mechanism to convey information
|
|
|
|
pertaining to certificates. */
|
2003-08-05 17:11:04 +00:00
|
|
|
struct certinfo_s {
|
|
|
|
struct certinfo_s *next;
|
2011-02-04 12:57:53 +01:00
|
|
|
int type;
|
2003-08-05 17:11:04 +00:00
|
|
|
int done;
|
|
|
|
char id[1];
|
|
|
|
};
|
|
|
|
typedef struct certinfo_s *CERTINFO;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
struct certinfo_cb_parm_s
|
2009-03-20 19:04:47 +00:00
|
|
|
{
|
|
|
|
ctrl_t ctrl;
|
2003-08-05 17:11:04 +00:00
|
|
|
int error;
|
|
|
|
CERTINFO info;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Structures used by the callback mechanism to convey assuan status
|
|
|
|
lines. */
|
2003-08-05 17:11:04 +00:00
|
|
|
struct sinfo_s {
|
|
|
|
struct sinfo_s *next;
|
|
|
|
char *data; /* Points into keyword. */
|
2011-02-04 12:57:53 +01:00
|
|
|
char keyword[1];
|
2003-08-05 17:11:04 +00:00
|
|
|
};
|
2011-02-04 12:57:53 +01:00
|
|
|
typedef struct sinfo_s *SINFO;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
struct sinfo_cb_parm_s {
|
2008-12-05 12:01:01 +00:00
|
|
|
int error;
|
2003-08-05 17:11:04 +00:00
|
|
|
SINFO info;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Destructor for key information objects. */
|
2003-08-05 17:11:04 +00:00
|
|
|
static void
|
|
|
|
release_keypair_info (KEYPAIR_INFO info)
|
|
|
|
{
|
|
|
|
while (info)
|
|
|
|
{
|
|
|
|
KEYPAIR_INFO tmp = info->next;
|
|
|
|
xfree (info);
|
|
|
|
info = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Destructor for certificate information objects. */
|
2003-08-05 17:11:04 +00:00
|
|
|
static void
|
|
|
|
release_certinfo (CERTINFO info)
|
|
|
|
{
|
|
|
|
while (info)
|
|
|
|
{
|
|
|
|
CERTINFO tmp = info->next;
|
|
|
|
xfree (info);
|
|
|
|
info = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Destructor for status information objects. */
|
2003-08-05 17:11:04 +00:00
|
|
|
static void
|
|
|
|
release_sinfo (SINFO info)
|
|
|
|
{
|
|
|
|
while (info)
|
|
|
|
{
|
|
|
|
SINFO tmp = info->next;
|
|
|
|
xfree (info);
|
|
|
|
info = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This callback is used by agent_card_learn and passed the content of
|
|
|
|
all KEYPAIRINFO lines. It merely stores this data away */
|
|
|
|
static void
|
|
|
|
kpinfo_cb (void *opaque, const char *line)
|
|
|
|
{
|
|
|
|
struct kpinfo_cb_parm_s *parm = opaque;
|
|
|
|
KEYPAIR_INFO item;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (parm->error)
|
2016-08-01 22:19:15 -04:00
|
|
|
return; /* no need to gather data after an error occurred */
|
2009-03-20 19:04:47 +00:00
|
|
|
|
|
|
|
if ((parm->error = agent_write_status (parm->ctrl, "PROGRESS",
|
|
|
|
"learncard", "k", "0", "0", NULL)))
|
|
|
|
return;
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
item = xtrycalloc (1, sizeof *item + strlen (line));
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
parm->error = out_of_core ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy (item->hexgrip, line);
|
|
|
|
for (p = item->hexgrip; hexdigitp (p); p++)
|
|
|
|
;
|
|
|
|
if (p == item->hexgrip && *p == 'X' && spacep (p+1))
|
|
|
|
{
|
|
|
|
item->no_cert = 1;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else if ((p - item->hexgrip) != 40 || !spacep (p))
|
|
|
|
{ /* not a 20 byte hex keygrip or not followed by a space */
|
|
|
|
parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
|
|
|
|
xfree (item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*p++ = 0;
|
|
|
|
while (spacep (p))
|
|
|
|
p++;
|
|
|
|
item->id = p;
|
|
|
|
while (*p && !spacep (p))
|
|
|
|
p++;
|
|
|
|
if (p == item->id)
|
|
|
|
{ /* invalid ID string */
|
|
|
|
parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
|
|
|
|
xfree (item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*p = 0; /* ignore trailing stuff */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
/* store it */
|
|
|
|
item->next = parm->info;
|
|
|
|
parm->info = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This callback is used by agent_card_learn and passed the content of
|
|
|
|
all CERTINFO lines. It merely stores this data away */
|
|
|
|
static void
|
|
|
|
certinfo_cb (void *opaque, const char *line)
|
|
|
|
{
|
|
|
|
struct certinfo_cb_parm_s *parm = opaque;
|
|
|
|
CERTINFO item;
|
|
|
|
int type;
|
|
|
|
char *p, *pend;
|
|
|
|
|
|
|
|
if (parm->error)
|
2016-08-01 22:19:15 -04:00
|
|
|
return; /* no need to gather data after an error occurred */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
2009-03-20 19:04:47 +00:00
|
|
|
if ((parm->error = agent_write_status (parm->ctrl, "PROGRESS",
|
|
|
|
"learncard", "c", "0", "0", NULL)))
|
|
|
|
return;
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
type = strtol (line, &p, 10);
|
|
|
|
while (spacep (p))
|
|
|
|
p++;
|
|
|
|
for (pend = p; *pend && !spacep (pend); pend++)
|
|
|
|
;
|
|
|
|
if (p == pend || !*p)
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2003-08-05 17:11:04 +00:00
|
|
|
parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*pend = 0; /* ignore trailing stuff */
|
|
|
|
|
|
|
|
item = xtrycalloc (1, sizeof *item + strlen (p));
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
parm->error = out_of_core ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
item->type = type;
|
|
|
|
strcpy (item->id, p);
|
|
|
|
/* store it */
|
|
|
|
item->next = parm->info;
|
|
|
|
parm->info = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This callback is used by agent_card_learn and passed the content of
|
|
|
|
all SINFO lines. It merely stores this data away */
|
|
|
|
static void
|
|
|
|
sinfo_cb (void *opaque, const char *keyword, size_t keywordlen,
|
|
|
|
const char *data)
|
|
|
|
{
|
|
|
|
struct sinfo_cb_parm_s *sparm = opaque;
|
|
|
|
SINFO item;
|
|
|
|
|
|
|
|
if (sparm->error)
|
2016-08-01 22:19:15 -04:00
|
|
|
return; /* no need to gather data after an error occurred */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
item = xtrycalloc (1, sizeof *item + keywordlen + 1 + strlen (data));
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
sparm->error = out_of_core ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy (item->keyword, keyword, keywordlen);
|
|
|
|
item->data = item->keyword + keywordlen;
|
|
|
|
*item->data = 0;
|
|
|
|
item->data++;
|
|
|
|
strcpy (item->data, data);
|
|
|
|
/* store it */
|
|
|
|
item->next = sparm->info;
|
|
|
|
sparm->info = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2004-01-29 20:17:27 +00:00
|
|
|
send_cert_back (ctrl_t ctrl, const char *id, void *assuan_context)
|
2003-08-05 17:11:04 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *derbuf;
|
|
|
|
size_t derbuflen;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-01-29 20:17:27 +00:00
|
|
|
rc = agent_card_readcert (ctrl, id, &derbuf, &derbuflen);
|
2003-08-05 17:11:04 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
2009-06-09 19:11:28 +00:00
|
|
|
const char *action;
|
|
|
|
|
|
|
|
switch (gpg_err_code (rc))
|
|
|
|
{
|
|
|
|
case GPG_ERR_INV_ID:
|
|
|
|
case GPG_ERR_NOT_FOUND:
|
|
|
|
action = " - ignored";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
action = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (opt.verbose || !*action)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info ("error reading certificate '%s': %s%s\n",
|
2009-06-09 19:11:28 +00:00
|
|
|
id? id:"?", gpg_strerror (rc), action);
|
|
|
|
|
|
|
|
return *action? 0 : rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = assuan_send_data (assuan_context, derbuf, derbuflen);
|
|
|
|
xfree (derbuf);
|
|
|
|
if (!rc)
|
|
|
|
rc = assuan_send_data (assuan_context, NULL, 0);
|
|
|
|
if (!rc)
|
|
|
|
rc = assuan_write_line (assuan_context, "END");
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("sending certificate failed: %s\n",
|
2006-09-06 16:35:52 +00:00
|
|
|
gpg_strerror (rc));
|
|
|
|
return rc;
|
2003-08-05 17:11:04 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-12 12:35:45 +01:00
|
|
|
/* Perform the learn operation. If ASSUAN_CONTEXT is not NULL and
|
|
|
|
SEND is true all new certificates are send back via Assuan. */
|
2003-08-05 17:11:04 +00:00
|
|
|
int
|
2015-04-03 17:33:11 +09:00
|
|
|
agent_handle_learn (ctrl_t ctrl, int send, void *assuan_context, int force)
|
2003-08-05 17:11:04 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct kpinfo_cb_parm_s parm;
|
|
|
|
struct certinfo_cb_parm_s cparm;
|
|
|
|
struct sinfo_cb_parm_s sparm;
|
2017-03-16 14:32:51 +09:00
|
|
|
const char *serialno = NULL;
|
2003-08-05 17:11:04 +00:00
|
|
|
KEYPAIR_INFO item;
|
|
|
|
SINFO sitem;
|
|
|
|
unsigned char grip[20];
|
|
|
|
char *p;
|
|
|
|
int i;
|
2011-02-04 12:57:53 +01:00
|
|
|
static int certtype_list[] = {
|
2009-03-30 12:46:06 +00:00
|
|
|
111, /* Root CA */
|
2003-08-05 17:11:04 +00:00
|
|
|
101, /* trusted */
|
|
|
|
102, /* useful */
|
|
|
|
100, /* regular */
|
2009-03-30 12:46:06 +00:00
|
|
|
/* We don't include 110 here because gpgsm can't handle that
|
|
|
|
special root CA format. */
|
2003-08-05 17:11:04 +00:00
|
|
|
-1 /* end of list */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
memset (&parm, 0, sizeof parm);
|
|
|
|
memset (&cparm, 0, sizeof cparm);
|
|
|
|
memset (&sparm, 0, sizeof sparm);
|
2009-03-20 19:04:47 +00:00
|
|
|
parm.ctrl = ctrl;
|
|
|
|
cparm.ctrl = ctrl;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Now gather all the available info. */
|
2004-01-29 20:17:27 +00:00
|
|
|
rc = agent_card_learn (ctrl, kpinfo_cb, &parm, certinfo_cb, &cparm,
|
2003-08-05 17:11:04 +00:00
|
|
|
sinfo_cb, &sparm);
|
|
|
|
if (!rc && (parm.error || cparm.error || sparm.error))
|
|
|
|
rc = parm.error? parm.error : cparm.error? cparm.error : sparm.error;
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_debug ("agent_card_learn failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
/* Pass on all the collected status information. */
|
2017-11-02 16:23:10 +09:00
|
|
|
for (sitem = sparm.info; sitem; sitem = sitem->next)
|
2003-08-05 17:11:04 +00:00
|
|
|
{
|
2017-11-02 16:23:10 +09:00
|
|
|
if (!strcmp (sitem->keyword, "SERIALNO"))
|
|
|
|
serialno = sitem->data;
|
|
|
|
if (assuan_context)
|
|
|
|
assuan_write_status (assuan_context, sitem->keyword, sitem->data);
|
2003-08-05 17:11:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 14:32:51 +09:00
|
|
|
if (!serialno)
|
|
|
|
{
|
|
|
|
rc = GPG_ERR_NOT_FOUND;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info ("card has S/N: %s\n", serialno);
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
/* Write out the certificates in a standard order. */
|
|
|
|
for (i=0; certtype_list[i] != -1; i++)
|
|
|
|
{
|
|
|
|
CERTINFO citem;
|
|
|
|
for (citem = cparm.info; citem; citem = citem->next)
|
|
|
|
{
|
|
|
|
if (certtype_list[i] != citem->type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info (" id: %s (type=%d)\n",
|
|
|
|
citem->id, citem->type);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2014-12-12 12:35:45 +01:00
|
|
|
if (assuan_context && send)
|
2003-08-05 17:11:04 +00:00
|
|
|
{
|
2004-01-29 20:17:27 +00:00
|
|
|
rc = send_cert_back (ctrl, citem->id, assuan_context);
|
2003-08-05 17:11:04 +00:00
|
|
|
if (rc)
|
|
|
|
goto leave;
|
|
|
|
citem->done = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
for (item = parm.info; item; item = item->next)
|
|
|
|
{
|
2016-10-20 12:05:15 +09:00
|
|
|
unsigned char *pubkey;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info (" id: %s (grip=%s)\n", item->id, item->hexgrip);
|
|
|
|
|
|
|
|
if (item->no_cert)
|
2005-02-22 18:08:28 +00:00
|
|
|
continue; /* No public key yet available. */
|
2003-08-05 17:11:04 +00:00
|
|
|
|
2006-09-26 17:32:28 +00:00
|
|
|
if (assuan_context)
|
|
|
|
{
|
|
|
|
agent_write_status (ctrl, "KEYPAIRINFO",
|
|
|
|
item->hexgrip, item->id, NULL);
|
|
|
|
}
|
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
for (p=item->hexgrip, i=0; i < 20; p += 2, i++)
|
|
|
|
grip[i] = xtoi_2 (p);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2015-04-03 17:33:11 +09:00
|
|
|
if (!force && !agent_key_available (grip))
|
2005-02-22 18:08:28 +00:00
|
|
|
continue; /* The key is already available. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2005-02-22 18:08:28 +00:00
|
|
|
/* Unknown key - store it. */
|
2020-02-13 11:45:41 +01:00
|
|
|
rc = agent_card_readkey (ctrl, item->id, &pubkey, NULL);
|
2003-08-05 17:11:04 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_debug ("agent_card_readkey failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2016-10-20 12:05:15 +09:00
|
|
|
rc = agent_write_shadow_key (grip, serialno, item->id, pubkey, force);
|
2003-08-05 17:11:04 +00:00
|
|
|
xfree (pubkey);
|
|
|
|
if (rc)
|
2016-10-20 12:05:15 +09:00
|
|
|
goto leave;
|
2003-08-05 17:11:04 +00:00
|
|
|
|
|
|
|
if (opt.verbose)
|
2014-12-12 12:35:45 +01:00
|
|
|
log_info (" id: %s - shadow key created\n", item->id);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2014-12-12 12:35:45 +01:00
|
|
|
if (assuan_context && send)
|
2003-08-05 17:11:04 +00:00
|
|
|
{
|
|
|
|
CERTINFO citem;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
/* only send the certificate if we have not done so before */
|
|
|
|
for (citem = cparm.info; citem; citem = citem->next)
|
|
|
|
{
|
|
|
|
if (!strcmp (citem->id, item->id))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!citem)
|
|
|
|
{
|
2004-01-29 20:17:27 +00:00
|
|
|
rc = send_cert_back (ctrl, item->id, assuan_context);
|
2003-08-05 17:11:04 +00:00
|
|
|
if (rc)
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 17:11:04 +00:00
|
|
|
leave:
|
|
|
|
release_keypair_info (parm.info);
|
|
|
|
release_certinfo (cparm.info);
|
|
|
|
release_sinfo (sparm.info);
|
|
|
|
return rc;
|
|
|
|
}
|