2007-08-28 19:48:13 +02:00
|
|
|
/* findkey.c - Locate the secret key
|
2010-04-21 18:26:17 +02:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007,
|
2011-07-20 20:49:41 +02:00
|
|
|
* 2010, 2011 Free Software Foundation, Inc.
|
2003-08-05 19:11:04 +02: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 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 19:11:04 +02: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
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <assert.h>
|
2006-10-19 16:22:06 +02:00
|
|
|
#include <pth.h> /* (we use pth_sleep) */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
#include "agent.h"
|
2007-08-28 19:48:13 +02:00
|
|
|
#include "i18n.h"
|
2011-07-20 20:49:41 +02:00
|
|
|
#include "../common/ssh-utils.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2007-08-22 22:36:33 +02:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/* Helper to pass data to the check callback of the unprotect function. */
|
2011-02-04 12:57:53 +01:00
|
|
|
struct try_unprotect_arg_s
|
2007-08-28 19:48:13 +02:00
|
|
|
{
|
|
|
|
ctrl_t ctrl;
|
2003-08-05 19:11:04 +02:00
|
|
|
const unsigned char *protected_key;
|
|
|
|
unsigned char *unprotected_key;
|
2007-08-28 19:48:13 +02:00
|
|
|
int change_required; /* Set by the callback to indicate that the
|
|
|
|
user should chnage the passphrase. */
|
2003-08-05 19:11:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
/* Write an S-expression formatted key to our key storage. With FORCE
|
2009-04-01 15:23:27 +02:00
|
|
|
passed as true an existing key with the given GRIP will get
|
2005-02-23 22:06:32 +01:00
|
|
|
overwritten. */
|
2003-08-05 19:11:04 +02:00
|
|
|
int
|
|
|
|
agent_write_private_key (const unsigned char *grip,
|
|
|
|
const void *buffer, size_t length, int force)
|
|
|
|
{
|
|
|
|
char *fname;
|
2010-04-14 13:24:02 +02:00
|
|
|
estream_t fp;
|
2003-08-05 19:11:04 +02:00
|
|
|
char hexgrip[40+4+1];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
bin2hex (grip, 20, hexgrip);
|
2003-08-05 19:11:04 +02:00
|
|
|
strcpy (hexgrip+40, ".key");
|
|
|
|
|
|
|
|
fname = make_filename (opt.homedir, GNUPG_PRIVATE_KEYS_DIR, hexgrip, NULL);
|
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
if (!force && !access (fname, F_OK))
|
|
|
|
{
|
|
|
|
log_error ("secret key file `%s' already exists\n", fname);
|
|
|
|
xfree (fname);
|
2010-06-17 17:44:44 +02:00
|
|
|
return gpg_error (GPG_ERR_EEXIST);
|
2005-02-23 22:06:32 +01:00
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2010-08-26 10:47:42 +02:00
|
|
|
fp = es_fopen (fname, force? "wb,mode=-rw" : "wbx,mode=-rw");
|
2011-02-04 12:57:53 +01:00
|
|
|
if (!fp)
|
|
|
|
{
|
2010-04-14 13:24:02 +02:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
|
|
|
log_error ("can't create `%s': %s\n", fname, gpg_strerror (tmperr));
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
|
|
|
|
2010-04-14 13:24:02 +02:00
|
|
|
if (es_fwrite (buffer, length, 1, fp) != 1)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2010-04-14 13:24:02 +02:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
|
|
|
log_error ("error writing `%s': %s\n", fname, gpg_strerror (tmperr));
|
|
|
|
es_fclose (fp);
|
|
|
|
gnupg_remove (fname);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
2010-04-14 13:24:02 +02:00
|
|
|
if (es_fclose (fp))
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2010-04-14 13:24:02 +02:00
|
|
|
gpg_error_t tmperr = gpg_error_from_syserror ();
|
|
|
|
log_error ("error closing `%s': %s\n", fname, gpg_strerror (tmperr));
|
|
|
|
gnupg_remove (fname);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
|
|
|
return tmperr;
|
|
|
|
}
|
2006-11-14 15:53:42 +01:00
|
|
|
bump_key_eventcounter ();
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-01 22:33:53 +02:00
|
|
|
/* Callback function to try the unprotection from the passphrase query
|
2003-08-05 19:11:04 +02:00
|
|
|
code. */
|
|
|
|
static int
|
|
|
|
try_unprotect_cb (struct pin_entry_info_s *pi)
|
|
|
|
{
|
|
|
|
struct try_unprotect_arg_s *arg = pi->check_cb_arg;
|
|
|
|
size_t dummy;
|
2007-08-28 19:48:13 +02:00
|
|
|
gpg_error_t err;
|
|
|
|
gnupg_isotime_t now, protected_at, tmptime;
|
|
|
|
char *desc = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
assert (!arg->unprotected_key);
|
2007-08-28 19:48:13 +02:00
|
|
|
|
|
|
|
arg->change_required = 0;
|
|
|
|
err = agent_unprotect (arg->protected_key, pi->pin, protected_at,
|
|
|
|
&arg->unprotected_key, &dummy);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (!opt.max_passphrase_days || arg->ctrl->in_passwd)
|
|
|
|
return 0; /* No regular passphrase change required. */
|
|
|
|
|
|
|
|
if (!*protected_at)
|
|
|
|
{
|
|
|
|
/* No protection date known - must force passphrase change. */
|
|
|
|
desc = xtrystrdup (_("Note: This passphrase has never been changed.%0A"
|
|
|
|
"Please change it now."));
|
|
|
|
if (!desc)
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gnupg_get_isotime (now);
|
|
|
|
gnupg_copy_time (tmptime, protected_at);
|
|
|
|
err = add_days_to_isotime (tmptime, opt.max_passphrase_days);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (strcmp (now, tmptime) > 0 )
|
|
|
|
{
|
|
|
|
/* Passphrase "expired". */
|
2011-02-04 12:57:53 +01:00
|
|
|
desc = xtryasprintf
|
2007-08-28 19:48:13 +02:00
|
|
|
(_("This passphrase has not been changed%%0A"
|
|
|
|
"since %.4s-%.2s-%.2s. Please change it now."),
|
|
|
|
protected_at, protected_at+4, protected_at+6);
|
|
|
|
if (!desc)
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
{
|
|
|
|
/* Change required. */
|
|
|
|
if (opt.enforce_passphrase_constraints)
|
|
|
|
{
|
|
|
|
err = agent_get_confirmation (arg->ctrl, desc,
|
2009-06-17 12:19:50 +02:00
|
|
|
_("Change passphrase"), NULL, 0);
|
2007-08-28 19:48:13 +02:00
|
|
|
if (!err)
|
|
|
|
arg->change_required = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = agent_get_confirmation (arg->ctrl, desc,
|
|
|
|
_("Change passphrase"),
|
2009-06-17 12:19:50 +02:00
|
|
|
_("I'll change it later"), 0);
|
2007-08-28 19:48:13 +02:00
|
|
|
if (!err)
|
|
|
|
arg->change_required = 1;
|
2010-10-13 17:57:08 +02:00
|
|
|
else if (gpg_err_code (err) == GPG_ERR_CANCELED
|
|
|
|
|| gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
|
2007-08-28 19:48:13 +02:00
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
xfree (desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-26 23:20:21 +01:00
|
|
|
/* Modify a Key description, replacing certain special format
|
|
|
|
characters. List of currently supported replacements:
|
|
|
|
|
2005-02-03 18:40:02 +01:00
|
|
|
%% - Replaced by a single %
|
|
|
|
%c - Replaced by the content of COMMENT.
|
2011-07-20 20:49:41 +02:00
|
|
|
%F - Replaced by an ssh style fingerprint computed from KEY.
|
2005-01-26 23:20:21 +01:00
|
|
|
|
2005-02-03 18:40:02 +01:00
|
|
|
The functions returns 0 on success or an error code. On success a
|
|
|
|
newly allocated string is stored at the address of RESULT.
|
|
|
|
*/
|
|
|
|
static gpg_error_t
|
2011-07-20 20:49:41 +02:00
|
|
|
modify_description (const char *in, const char *comment, const gcry_sexp_t key,
|
|
|
|
char **result)
|
2005-02-03 18:40:02 +01:00
|
|
|
{
|
|
|
|
size_t comment_length;
|
|
|
|
size_t in_len;
|
|
|
|
size_t out_len;
|
|
|
|
char *out;
|
|
|
|
size_t i;
|
|
|
|
int special, pass;
|
2011-07-20 20:49:41 +02:00
|
|
|
char *ssh_fpr = NULL;
|
2005-02-03 18:40:02 +01:00
|
|
|
|
|
|
|
comment_length = strlen (comment);
|
|
|
|
in_len = strlen (in);
|
|
|
|
|
|
|
|
/* First pass calculates the length, second pass does the actual
|
|
|
|
copying. */
|
|
|
|
out = NULL;
|
|
|
|
out_len = 0;
|
|
|
|
for (pass=0; pass < 2; pass++)
|
2005-01-26 23:20:21 +01:00
|
|
|
{
|
2005-02-03 18:40:02 +01:00
|
|
|
special = 0;
|
|
|
|
for (i = 0; i < in_len; i++)
|
|
|
|
{
|
2005-02-25 17:14:55 +01:00
|
|
|
if (special)
|
2005-02-03 18:40:02 +01:00
|
|
|
{
|
|
|
|
special = 0;
|
|
|
|
switch (in[i])
|
|
|
|
{
|
|
|
|
case '%':
|
|
|
|
if (out)
|
|
|
|
*out++ = '%';
|
2005-02-15 17:23:45 +01:00
|
|
|
else
|
|
|
|
out_len++;
|
2005-02-03 18:40:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c': /* Comment. */
|
2005-02-15 17:23:45 +01:00
|
|
|
if (out)
|
2005-02-03 18:40:02 +01:00
|
|
|
{
|
|
|
|
memcpy (out, comment, comment_length);
|
|
|
|
out += comment_length;
|
|
|
|
}
|
2005-02-15 17:23:45 +01:00
|
|
|
else
|
|
|
|
out_len += comment_length;
|
2005-02-03 18:40:02 +01:00
|
|
|
break;
|
|
|
|
|
2011-07-20 20:49:41 +02:00
|
|
|
case 'F': /* SSH style fingerprint. */
|
|
|
|
if (!ssh_fpr && key)
|
|
|
|
ssh_get_fingerprint_string (key, &ssh_fpr);
|
|
|
|
if (ssh_fpr)
|
|
|
|
{
|
|
|
|
if (out)
|
|
|
|
out = stpcpy (out, ssh_fpr);
|
|
|
|
else
|
|
|
|
out_len += strlen (ssh_fpr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-02-25 17:14:55 +01:00
|
|
|
default: /* Invalid special sequences are kept as they are. */
|
|
|
|
if (out)
|
|
|
|
{
|
|
|
|
*out++ = '%';
|
|
|
|
*out++ = in[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out_len+=2;
|
2005-02-03 18:40:02 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-02-25 17:14:55 +01:00
|
|
|
else if (in[i] == '%')
|
|
|
|
special = 1;
|
2005-02-03 18:40:02 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (out)
|
|
|
|
*out++ = in[i];
|
2005-02-15 17:23:45 +01:00
|
|
|
else
|
|
|
|
out_len++;
|
2005-02-03 18:40:02 +01:00
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2005-02-03 18:40:02 +01:00
|
|
|
if (!pass)
|
|
|
|
{
|
|
|
|
*result = out = xtrymalloc (out_len + 1);
|
|
|
|
if (!out)
|
2011-07-20 20:49:41 +02:00
|
|
|
{
|
|
|
|
xfree (ssh_fpr);
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
}
|
2005-02-03 18:40:02 +01:00
|
|
|
}
|
2005-01-26 23:20:21 +01:00
|
|
|
}
|
|
|
|
|
2005-02-03 18:40:02 +01:00
|
|
|
*out = 0;
|
|
|
|
assert (*result + out_len == out);
|
2011-07-20 20:49:41 +02:00
|
|
|
xfree (ssh_fpr);
|
2005-02-03 18:40:02 +01:00
|
|
|
return 0;
|
2005-01-26 23:20:21 +01:00
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2005-01-26 23:20:21 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/* Unprotect the canconical encoded S-expression key in KEYBUF. GRIP
|
|
|
|
should be the hex encoded keygrip of that key to be used with the
|
2004-02-13 18:06:34 +01:00
|
|
|
caching mechanism. DESC_TEXT may be set to override the default
|
2009-05-15 13:16:28 +02:00
|
|
|
description used for the pinentry. If LOOKUP_TTL is given this
|
2010-10-01 22:33:53 +02:00
|
|
|
function is used to lookup the default ttl. If R_PASSPHRASE is not
|
|
|
|
NULL, the function succeeded and the key was protected the used
|
|
|
|
passphrase (entered or from the cache) is stored there; if not NULL
|
|
|
|
will be stored. The caller needs to free the returned
|
|
|
|
passphrase. */
|
2003-08-05 19:11:04 +02:00
|
|
|
static int
|
2010-09-01 14:49:05 +02:00
|
|
|
unprotect (ctrl_t ctrl, const char *cache_nonce, const char *desc_text,
|
2011-02-04 12:57:53 +01:00
|
|
|
unsigned char **keybuf, const unsigned char *grip,
|
2010-10-01 22:33:53 +02:00
|
|
|
cache_mode_t cache_mode, lookup_ttl_t lookup_ttl,
|
|
|
|
char **r_passphrase)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
struct pin_entry_info_s *pi;
|
|
|
|
struct try_unprotect_arg_s arg;
|
2009-03-06 18:31:27 +01:00
|
|
|
int rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
unsigned char *result;
|
|
|
|
size_t resultlen;
|
|
|
|
char hexgrip[40+1];
|
2010-10-01 22:33:53 +02:00
|
|
|
|
|
|
|
if (r_passphrase)
|
|
|
|
*r_passphrase = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
bin2hex (grip, 20, hexgrip);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2010-09-01 14:49:05 +02:00
|
|
|
/* Initially try to get it using a cache nonce. */
|
|
|
|
if (cache_nonce)
|
|
|
|
{
|
2010-09-02 12:46:23 +02:00
|
|
|
char *pw;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-09-02 12:46:23 +02:00
|
|
|
pw = agent_get_cache (cache_nonce, CACHE_MODE_NONCE);
|
2010-09-01 14:49:05 +02:00
|
|
|
if (pw)
|
|
|
|
{
|
|
|
|
rc = agent_unprotect (*keybuf, pw, NULL, &result, &resultlen);
|
|
|
|
if (!rc)
|
|
|
|
{
|
2010-10-01 22:33:53 +02:00
|
|
|
if (r_passphrase)
|
|
|
|
*r_passphrase = pw;
|
|
|
|
else
|
|
|
|
xfree (pw);
|
2010-09-01 14:49:05 +02:00
|
|
|
xfree (*keybuf);
|
|
|
|
*keybuf = result;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-01 22:33:53 +02:00
|
|
|
xfree (pw);
|
2010-09-01 14:49:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-03 17:24:37 +01:00
|
|
|
/* First try to get it from the cache - if there is none or we can't
|
2003-08-05 19:11:04 +02:00
|
|
|
unprotect it, we fall back to ask the user */
|
2005-06-07 21:09:18 +02:00
|
|
|
if (cache_mode != CACHE_MODE_IGNORE)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2010-09-02 12:46:23 +02:00
|
|
|
char *pw;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2006-10-19 16:22:06 +02:00
|
|
|
retry:
|
2010-09-02 12:46:23 +02:00
|
|
|
pw = agent_get_cache (hexgrip, cache_mode);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (pw)
|
|
|
|
{
|
2007-08-28 19:48:13 +02:00
|
|
|
rc = agent_unprotect (*keybuf, pw, NULL, &result, &resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (!rc)
|
|
|
|
{
|
2010-10-01 22:33:53 +02:00
|
|
|
if (r_passphrase)
|
|
|
|
*r_passphrase = pw;
|
|
|
|
else
|
|
|
|
xfree (pw);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (*keybuf);
|
|
|
|
*keybuf = result;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-01 22:33:53 +02:00
|
|
|
xfree (pw);
|
2003-08-05 19:11:04 +02:00
|
|
|
rc = 0;
|
|
|
|
}
|
2006-10-19 16:22:06 +02:00
|
|
|
|
|
|
|
/* If the pinentry is currently in use, we wait up to 60 seconds
|
2007-08-28 19:48:13 +02:00
|
|
|
for it to close and check the cache again. This solves a common
|
2006-10-19 16:22:06 +02:00
|
|
|
situation where several requests for unprotecting a key have
|
|
|
|
been made but the user is still entering the passphrase for
|
|
|
|
the first request. Because all requests to agent_askpin are
|
|
|
|
serialized they would then pop up one after the other to
|
|
|
|
request the passphrase - despite that the user has already
|
|
|
|
entered it and is then available in the cache. This
|
|
|
|
implementation is not race free but in the worst case the
|
|
|
|
user has to enter the passphrase only once more. */
|
|
|
|
if (pinentry_active_p (ctrl, 0))
|
|
|
|
{
|
|
|
|
/* Active - wait */
|
|
|
|
if (!pinentry_active_p (ctrl, 60))
|
|
|
|
{
|
|
|
|
/* We need to give the other thread a chance to actually put
|
|
|
|
it into the cache. */
|
2011-02-04 12:57:53 +01:00
|
|
|
pth_sleep (1);
|
2006-10-19 16:22:06 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
/* Timeout - better call pinentry now the plain way. */
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2007-08-28 19:48:13 +02:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
pi = gcry_calloc_secure (1, sizeof (*pi) + 100);
|
2005-02-23 22:06:32 +01:00
|
|
|
if (!pi)
|
2006-09-14 18:50:33 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2003-08-05 19:11:04 +02:00
|
|
|
pi->max_length = 100;
|
|
|
|
pi->min_digits = 0; /* we want a real passphrase */
|
2009-03-05 20:19:37 +01:00
|
|
|
pi->max_digits = 16;
|
2003-08-05 19:11:04 +02:00
|
|
|
pi->max_tries = 3;
|
|
|
|
pi->check_cb = try_unprotect_cb;
|
2007-08-28 19:48:13 +02:00
|
|
|
arg.ctrl = ctrl;
|
2003-08-05 19:11:04 +02:00
|
|
|
arg.protected_key = *keybuf;
|
|
|
|
arg.unprotected_key = NULL;
|
2007-08-28 19:48:13 +02:00
|
|
|
arg.change_required = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
pi->check_cb_arg = &arg;
|
|
|
|
|
2010-10-13 17:57:08 +02:00
|
|
|
rc = agent_askpin (ctrl, desc_text, NULL, NULL, pi);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
assert (arg.unprotected_key);
|
2007-08-28 19:48:13 +02:00
|
|
|
if (arg.change_required)
|
|
|
|
{
|
|
|
|
size_t canlen, erroff;
|
|
|
|
gcry_sexp_t s_skey;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-08-28 19:48:13 +02:00
|
|
|
assert (arg.unprotected_key);
|
|
|
|
canlen = gcry_sexp_canon_len (arg.unprotected_key, 0, NULL, NULL);
|
|
|
|
rc = gcry_sexp_sscan (&s_skey, &erroff,
|
|
|
|
(char*)arg.unprotected_key, canlen);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("failed to build S-Exp (off=%u): %s\n",
|
|
|
|
(unsigned int)erroff, gpg_strerror (rc));
|
|
|
|
wipememory (arg.unprotected_key, canlen);
|
|
|
|
xfree (arg.unprotected_key);
|
|
|
|
xfree (pi);
|
|
|
|
return rc;
|
|
|
|
}
|
2010-10-26 11:10:29 +02:00
|
|
|
rc = agent_protect_and_store (ctrl, s_skey, NULL);
|
2007-08-28 19:48:13 +02:00
|
|
|
gcry_sexp_release (s_skey);
|
|
|
|
if (rc)
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
log_error ("changing the passphrase failed: %s\n",
|
2007-08-28 19:48:13 +02:00
|
|
|
gpg_strerror (rc));
|
|
|
|
wipememory (arg.unprotected_key, canlen);
|
|
|
|
xfree (arg.unprotected_key);
|
|
|
|
xfree (pi);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2010-10-01 22:33:53 +02:00
|
|
|
else
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
agent_put_cache (hexgrip, cache_mode, pi->pin,
|
2010-10-01 22:33:53 +02:00
|
|
|
lookup_ttl? lookup_ttl (hexgrip) : 0);
|
|
|
|
if (r_passphrase && *pi->pin)
|
|
|
|
*r_passphrase = xtrystrdup (pi->pin);
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (*keybuf);
|
|
|
|
*keybuf = arg.unprotected_key;
|
|
|
|
}
|
|
|
|
xfree (pi);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
/* Read the key identified by GRIP from the private key directory and
|
|
|
|
return it as an gcrypt S-expression object in RESULT. On failure
|
|
|
|
returns an error code and stores NULL at RESULT. */
|
|
|
|
static gpg_error_t
|
|
|
|
read_key_file (const unsigned char *grip, gcry_sexp_t *result)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2009-03-06 18:31:27 +01:00
|
|
|
int rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
char *fname;
|
2010-04-14 13:24:02 +02:00
|
|
|
estream_t fp;
|
2003-08-05 19:11:04 +02:00
|
|
|
struct stat st;
|
|
|
|
unsigned char *buf;
|
2005-02-23 22:06:32 +01:00
|
|
|
size_t buflen, erroff;
|
2003-08-05 19:11:04 +02:00
|
|
|
gcry_sexp_t s_skey;
|
|
|
|
char hexgrip[40+4+1];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-01-16 18:39:58 +01:00
|
|
|
*result = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
bin2hex (grip, 20, hexgrip);
|
2003-08-05 19:11:04 +02:00
|
|
|
strcpy (hexgrip+40, ".key");
|
|
|
|
|
|
|
|
fname = make_filename (opt.homedir, GNUPG_PRIVATE_KEYS_DIR, hexgrip, NULL);
|
2010-04-14 13:24:02 +02:00
|
|
|
fp = es_fopen (fname, "rb");
|
2003-08-05 19:11:04 +02:00
|
|
|
if (!fp)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2009-03-06 18:31:27 +01:00
|
|
|
if (gpg_err_code (rc) != GPG_ERR_ENOENT)
|
|
|
|
log_error ("can't open `%s': %s\n", fname, strerror (errno));
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
2004-01-16 18:39:58 +01:00
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-04-14 13:24:02 +02:00
|
|
|
if (fstat (es_fileno (fp), &st))
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2003-08-05 19:11:04 +02:00
|
|
|
log_error ("can't stat `%s': %s\n", fname, strerror (errno));
|
|
|
|
xfree (fname);
|
2010-04-14 13:24:02 +02:00
|
|
|
es_fclose (fp);
|
2004-01-16 18:39:58 +01:00
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buflen = st.st_size;
|
2005-02-23 22:06:32 +01:00
|
|
|
buf = xtrymalloc (buflen+1);
|
2010-04-14 13:24:02 +02:00
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
log_error ("error allocating %zu bytes for `%s': %s\n",
|
|
|
|
buflen, fname, strerror (errno));
|
|
|
|
xfree (fname);
|
|
|
|
es_fclose (fp);
|
|
|
|
xfree (buf);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (es_fread (buf, buflen, 1, fp) != 1)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2011-02-04 12:57:53 +01:00
|
|
|
log_error ("error reading %zu bytes from `%s': %s\n",
|
2010-04-14 13:24:02 +02:00
|
|
|
buflen, fname, strerror (errno));
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
2010-04-14 13:24:02 +02:00
|
|
|
es_fclose (fp);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (buf);
|
2004-01-16 18:39:58 +01:00
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
/* Convert the file into a gcrypt S-expression object. */
|
2005-06-16 10:12:03 +02:00
|
|
|
rc = gcry_sexp_sscan (&s_skey, &erroff, (char*)buf, buflen);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
2010-04-14 13:24:02 +02:00
|
|
|
es_fclose (fp);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (buf);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("failed to build S-Exp (off=%u): %s\n",
|
|
|
|
(unsigned int)erroff, gpg_strerror (rc));
|
2004-01-16 18:39:58 +01:00
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2005-02-23 22:06:32 +01:00
|
|
|
*result = s_skey;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the secret key as an S-Exp in RESULT after locating it using
|
2009-03-06 18:31:27 +01:00
|
|
|
the GRIP. Stores NULL at RESULT if the operation shall be diverted
|
|
|
|
to a token; in this case an allocated S-expression with the
|
|
|
|
shadow_info part from the file is stored at SHADOW_INFO.
|
|
|
|
CACHE_MODE defines now the cache shall be used. DESC_TEXT may be
|
2009-05-15 13:16:28 +02:00
|
|
|
set to present a custom description for the pinentry. LOOKUP_TTL
|
|
|
|
is an optional function to convey a TTL to the cache manager; we do
|
2010-09-01 13:07:16 +02:00
|
|
|
not simply pass the TTL value because the value is only needed if
|
|
|
|
an unprotect action was needed and looking up the TTL may have some
|
|
|
|
overhead (e.g. scanning the sshcontrol file). If a CACHE_NONCE is
|
2010-10-01 22:33:53 +02:00
|
|
|
given that cache item is first tried to get a passphrase. If
|
|
|
|
R_PASSPHRASE is not NULL, the function succeeded and the key was
|
|
|
|
protected the used passphrase (entered or from the cache) is stored
|
|
|
|
there; if not NULL will be stored. The caller needs to free the
|
|
|
|
returned passphrase. */
|
2005-02-23 22:06:32 +01:00
|
|
|
gpg_error_t
|
2010-09-01 13:07:16 +02:00
|
|
|
agent_key_from_file (ctrl_t ctrl, const char *cache_nonce,
|
|
|
|
const char *desc_text,
|
2005-02-23 22:06:32 +01:00
|
|
|
const unsigned char *grip, unsigned char **shadow_info,
|
2009-05-15 13:16:28 +02:00
|
|
|
cache_mode_t cache_mode, lookup_ttl_t lookup_ttl,
|
2010-10-01 22:33:53 +02:00
|
|
|
gcry_sexp_t *result, char **r_passphrase)
|
2005-02-23 22:06:32 +01:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t len, buflen, erroff;
|
|
|
|
gcry_sexp_t s_skey;
|
|
|
|
int got_shadow_info = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
*result = NULL;
|
|
|
|
if (shadow_info)
|
2009-05-15 13:16:28 +02:00
|
|
|
*shadow_info = NULL;
|
2010-10-01 22:33:53 +02:00
|
|
|
if (r_passphrase)
|
|
|
|
*r_passphrase = NULL;
|
2005-02-23 22:06:32 +01:00
|
|
|
|
|
|
|
rc = read_key_file (grip, &s_skey);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* For use with the protection functions we also need the key as an
|
2009-03-06 18:31:27 +01:00
|
|
|
canonical encoded S-expression in a buffer. Create this buffer
|
2005-02-23 22:06:32 +01:00
|
|
|
now. */
|
2009-03-06 18:31:27 +01:00
|
|
|
rc = make_canon_sexp (s_skey, &buf, &len);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
switch (agent_private_key_type (buf))
|
|
|
|
{
|
|
|
|
case PRIVATE_KEY_CLEAR:
|
|
|
|
break; /* no unprotection needed */
|
|
|
|
case PRIVATE_KEY_PROTECTED:
|
2005-01-26 23:20:21 +01:00
|
|
|
{
|
|
|
|
char *desc_text_final;
|
2011-07-20 20:49:41 +02:00
|
|
|
char *comment = NULL;
|
2005-02-03 18:40:02 +01:00
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
/* Note, that we will take the comment as a C string for
|
2005-02-03 18:40:02 +01:00
|
|
|
display purposes; i.e. all stuff beyond a Nul character is
|
|
|
|
ignored. */
|
2011-07-20 20:49:41 +02:00
|
|
|
{
|
|
|
|
gcry_sexp_t comment_sexp;
|
|
|
|
|
|
|
|
comment_sexp = gcry_sexp_find_token (s_skey, "comment", 0);
|
|
|
|
if (comment_sexp)
|
|
|
|
comment = gcry_sexp_nth_string (comment_sexp, 1);
|
|
|
|
gcry_sexp_release (comment_sexp);
|
|
|
|
}
|
2005-01-26 23:20:21 +01:00
|
|
|
|
2005-02-03 18:40:02 +01:00
|
|
|
desc_text_final = NULL;
|
2005-01-26 23:20:21 +01:00
|
|
|
if (desc_text)
|
2011-07-20 20:49:41 +02:00
|
|
|
rc = modify_description (desc_text, comment? comment:"", s_skey,
|
|
|
|
&desc_text_final);
|
|
|
|
gcry_free (comment);
|
2005-01-26 23:20:21 +01:00
|
|
|
|
2005-02-03 18:40:02 +01:00
|
|
|
if (!rc)
|
2005-01-26 23:20:21 +01:00
|
|
|
{
|
2010-09-01 14:49:05 +02:00
|
|
|
rc = unprotect (ctrl, cache_nonce, desc_text_final, &buf, grip,
|
2010-10-01 22:33:53 +02:00
|
|
|
cache_mode, lookup_ttl, r_passphrase);
|
2005-01-26 23:20:21 +01:00
|
|
|
if (rc)
|
|
|
|
log_error ("failed to unprotect the secret key: %s\n",
|
|
|
|
gpg_strerror (rc));
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2005-01-26 23:20:21 +01:00
|
|
|
xfree (desc_text_final);
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
break;
|
|
|
|
case PRIVATE_KEY_SHADOWED:
|
|
|
|
if (shadow_info)
|
|
|
|
{
|
|
|
|
const unsigned char *s;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
rc = agent_get_shadow_info (buf, &s);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
n = gcry_sexp_canon_len (s, 0, NULL,NULL);
|
|
|
|
assert (n);
|
|
|
|
*shadow_info = xtrymalloc (n);
|
|
|
|
if (!*shadow_info)
|
|
|
|
rc = out_of_core ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy (*shadow_info, s, n);
|
|
|
|
rc = 0;
|
2004-01-16 18:39:58 +01:00
|
|
|
got_shadow_info = 1;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc)
|
|
|
|
log_error ("get_shadow_info failed: %s\n", gpg_strerror (rc));
|
|
|
|
}
|
2004-01-16 18:39:58 +01:00
|
|
|
else
|
|
|
|
rc = gpg_error (GPG_ERR_UNUSABLE_SECKEY);
|
2003-08-05 19:11:04 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_error ("invalid private key format\n");
|
|
|
|
rc = gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
break;
|
|
|
|
}
|
2005-02-23 22:06:32 +01:00
|
|
|
gcry_sexp_release (s_skey);
|
|
|
|
s_skey = NULL;
|
2004-01-16 18:39:58 +01:00
|
|
|
if (rc || got_shadow_info)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
xfree (buf);
|
2010-10-01 22:33:53 +02:00
|
|
|
if (r_passphrase)
|
|
|
|
{
|
|
|
|
xfree (*r_passphrase);
|
|
|
|
*r_passphrase = NULL;
|
|
|
|
}
|
2004-01-16 18:39:58 +01:00
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2004-02-03 17:24:37 +01:00
|
|
|
buflen = gcry_sexp_canon_len (buf, 0, NULL, NULL);
|
2005-06-16 10:12:03 +02:00
|
|
|
rc = gcry_sexp_sscan (&s_skey, &erroff, (char*)buf, buflen);
|
2004-02-03 17:24:37 +01:00
|
|
|
wipememory (buf, buflen);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (buf);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("failed to build S-Exp (off=%u): %s\n",
|
|
|
|
(unsigned int)erroff, gpg_strerror (rc));
|
2010-10-01 22:33:53 +02:00
|
|
|
if (r_passphrase)
|
|
|
|
{
|
|
|
|
xfree (*r_passphrase);
|
|
|
|
*r_passphrase = NULL;
|
|
|
|
}
|
2004-01-16 18:39:58 +01:00
|
|
|
return rc;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2004-01-16 18:39:58 +01:00
|
|
|
*result = s_skey;
|
|
|
|
return 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
/* Return the string name from the S-expression S_KEY as well as a
|
|
|
|
string describing the names of the parameters. ALGONAMESIZE and
|
|
|
|
ELEMSSIZE give the allocated size of the provided buffers. The
|
|
|
|
buffers may be NULL if not required. If R_LIST is not NULL the top
|
|
|
|
level list will be stored tehre; the caller needs to release it in
|
|
|
|
this case. */
|
|
|
|
static gpg_error_t
|
|
|
|
key_parms_from_sexp (gcry_sexp_t s_key, gcry_sexp_t *r_list,
|
|
|
|
char *r_algoname, size_t algonamesize,
|
|
|
|
char *r_elems, size_t elemssize)
|
2005-02-23 22:06:32 +01:00
|
|
|
{
|
|
|
|
gcry_sexp_t list, l2;
|
2010-04-21 18:26:17 +02:00
|
|
|
const char *name, *algoname, *elems;
|
2005-02-23 22:06:32 +01:00
|
|
|
size_t n;
|
2008-10-20 15:53:23 +02:00
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
if (r_list)
|
|
|
|
*r_list = NULL;
|
2005-02-23 22:06:32 +01:00
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
list = gcry_sexp_find_token (s_key, "shadowed-private-key", 0 );
|
2005-02-23 22:06:32 +01:00
|
|
|
if (!list)
|
2010-04-21 18:26:17 +02:00
|
|
|
list = gcry_sexp_find_token (s_key, "protected-private-key", 0 );
|
2005-02-23 22:06:32 +01:00
|
|
|
if (!list)
|
2010-04-21 18:26:17 +02:00
|
|
|
list = gcry_sexp_find_token (s_key, "private-key", 0 );
|
2005-02-23 22:06:32 +01:00
|
|
|
if (!list)
|
|
|
|
{
|
|
|
|
log_error ("invalid private key format\n");
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
l2 = gcry_sexp_cadr (list);
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = l2;
|
|
|
|
name = gcry_sexp_nth_data (list, 0, &n);
|
|
|
|
if (n==3 && !memcmp (name, "rsa", 3))
|
|
|
|
{
|
|
|
|
algoname = "rsa";
|
|
|
|
elems = "ne";
|
|
|
|
}
|
|
|
|
else if (n==3 && !memcmp (name, "dsa", 3))
|
|
|
|
{
|
|
|
|
algoname = "dsa";
|
|
|
|
elems = "pqgy";
|
|
|
|
}
|
2011-01-06 02:33:17 +01:00
|
|
|
else if (n==5 && !memcmp (name, "ecdsa", 5))
|
|
|
|
{
|
|
|
|
algoname = "ecdsa";
|
2011-01-31 15:44:24 +01:00
|
|
|
elems = "pabgnq";
|
2011-01-06 02:33:17 +01:00
|
|
|
}
|
|
|
|
else if (n==4 && !memcmp (name, "ecdh", 4))
|
|
|
|
{
|
|
|
|
algoname = "ecdh";
|
2011-01-31 15:44:24 +01:00
|
|
|
elems = "pabgnq";
|
2011-01-06 02:33:17 +01:00
|
|
|
}
|
2005-02-23 22:06:32 +01:00
|
|
|
else if (n==3 && !memcmp (name, "elg", 3))
|
|
|
|
{
|
|
|
|
algoname = "elg";
|
|
|
|
elems = "pgy";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_error ("unknown private key algorithm\n");
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
if (r_algoname)
|
|
|
|
{
|
|
|
|
if (strlen (algoname) >= algonamesize)
|
|
|
|
return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);
|
|
|
|
strcpy (r_algoname, algoname);
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|
2010-04-21 18:26:17 +02:00
|
|
|
if (r_elems)
|
|
|
|
{
|
|
|
|
if (strlen (elems) >= elemssize)
|
|
|
|
return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);
|
|
|
|
strcpy (r_elems, elems);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_list)
|
|
|
|
*r_list = list;
|
|
|
|
else
|
|
|
|
gcry_sexp_release (list);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-31 17:58:39 +02:00
|
|
|
/* Return the public key algorithm number if S_KEY is a DSA style key.
|
|
|
|
If it is not a DSA style key, return 0. */
|
2011-02-04 12:57:53 +01:00
|
|
|
int
|
2010-04-21 18:26:17 +02:00
|
|
|
agent_is_dsa_key (gcry_sexp_t s_key)
|
|
|
|
{
|
|
|
|
char algoname[6];
|
|
|
|
|
|
|
|
if (!s_key)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (key_parms_from_sexp (s_key, NULL, algoname, sizeof algoname, NULL, 0))
|
|
|
|
return 0; /* Error - assume it is not an DSA key. */
|
|
|
|
|
2010-08-31 17:58:39 +02:00
|
|
|
if (!strcmp (algoname, "dsa"))
|
|
|
|
return GCRY_PK_DSA;
|
|
|
|
else if (!strcmp (algoname, "ecdsa"))
|
|
|
|
return GCRY_PK_ECDSA;
|
|
|
|
else
|
|
|
|
return 0;
|
2010-04-21 18:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-20 20:49:41 +02:00
|
|
|
/* Return the key for the keygrip GRIP. The result is stored at
|
|
|
|
RESULT. This function extracts the key from the private key
|
|
|
|
database and returns it as an S-expression object as it is. On
|
|
|
|
failure an error code is returned and NULL stored at RESULT. */
|
|
|
|
gpg_error_t
|
|
|
|
agent_raw_key_from_file (ctrl_t ctrl, const unsigned char *grip,
|
|
|
|
gcry_sexp_t *result)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_sexp_t s_skey;
|
|
|
|
|
|
|
|
(void)ctrl;
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
|
|
|
err = read_key_file (grip, &s_skey);
|
|
|
|
if (!err)
|
|
|
|
*result = s_skey;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
/* Return the public key for the keygrip GRIP. The result is stored
|
|
|
|
at RESULT. This function extracts the public key from the private
|
|
|
|
key database. On failure an error code is returned and NULL stored
|
|
|
|
at RESULT. */
|
|
|
|
gpg_error_t
|
2011-02-04 12:57:53 +01:00
|
|
|
agent_public_key_from_file (ctrl_t ctrl,
|
2010-04-21 18:26:17 +02:00
|
|
|
const unsigned char *grip,
|
|
|
|
gcry_sexp_t *result)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
int i, idx;
|
|
|
|
gcry_sexp_t s_skey;
|
|
|
|
char algoname[6];
|
|
|
|
char elems[6];
|
|
|
|
gcry_sexp_t uri_sexp, comment_sexp;
|
|
|
|
const char *uri, *comment;
|
|
|
|
size_t uri_length, comment_length;
|
|
|
|
char *format, *p;
|
|
|
|
void *args[4+2+2+1]; /* Size is max. # of elements + 2 for uri + 2
|
|
|
|
for comment + end-of-list. */
|
|
|
|
int argidx;
|
|
|
|
gcry_sexp_t list, l2;
|
|
|
|
const char *s;
|
|
|
|
gcry_mpi_t *array;
|
|
|
|
|
|
|
|
(void)ctrl;
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
|
|
|
err = read_key_file (grip, &s_skey);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
err = key_parms_from_sexp (s_skey, &list,
|
2010-04-21 18:26:17 +02:00
|
|
|
algoname, sizeof algoname,
|
|
|
|
elems, sizeof elems);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
gcry_sexp_release (s_skey);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-02-23 22:06:32 +01:00
|
|
|
/* Allocate an array for the parameters and copy them out of the
|
|
|
|
secret key. FIXME: We should have a generic copy function. */
|
|
|
|
array = xtrycalloc (strlen(elems) + 1, sizeof *array);
|
|
|
|
if (!array)
|
|
|
|
{
|
2010-04-21 18:26:17 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2005-02-23 22:06:32 +01:00
|
|
|
gcry_sexp_release (list);
|
|
|
|
gcry_sexp_release (s_skey);
|
2010-04-21 18:26:17 +02:00
|
|
|
return err;
|
2005-02-23 22:06:32 +01:00
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
for (idx=0, s=elems; *s; s++, idx++ )
|
2005-02-23 22:06:32 +01:00
|
|
|
{
|
|
|
|
l2 = gcry_sexp_find_token (list, s, 1);
|
|
|
|
if (!l2)
|
|
|
|
{
|
|
|
|
/* Required parameter not found. */
|
|
|
|
for (i=0; i<idx; i++)
|
|
|
|
gcry_mpi_release (array[i]);
|
|
|
|
xfree (array);
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
gcry_sexp_release (s_skey);
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
|
|
|
|
gcry_sexp_release (l2);
|
|
|
|
if (!array[idx])
|
|
|
|
{
|
|
|
|
/* Required parameter is invalid. */
|
|
|
|
for (i=0; i<idx; i++)
|
|
|
|
gcry_mpi_release (array[i]);
|
|
|
|
xfree (array);
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
gcry_sexp_release (s_skey);
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = NULL;
|
|
|
|
|
|
|
|
uri = NULL;
|
|
|
|
uri_length = 0;
|
|
|
|
uri_sexp = gcry_sexp_find_token (s_skey, "uri", 0);
|
|
|
|
if (uri_sexp)
|
|
|
|
uri = gcry_sexp_nth_data (uri_sexp, 1, &uri_length);
|
|
|
|
|
|
|
|
comment = NULL;
|
|
|
|
comment_length = 0;
|
|
|
|
comment_sexp = gcry_sexp_find_token (s_skey, "comment", 0);
|
|
|
|
if (comment_sexp)
|
|
|
|
comment = gcry_sexp_nth_data (comment_sexp, 1, &comment_length);
|
|
|
|
|
|
|
|
gcry_sexp_release (s_skey);
|
|
|
|
s_skey = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/* FIXME: The following thing is pretty ugly code; we should
|
2010-04-21 18:26:17 +02:00
|
|
|
investigate how to make it cleaner. Probably code to handle
|
|
|
|
canonical S-expressions in a memory buffer is better suited for
|
2005-02-23 22:06:32 +01:00
|
|
|
such a task. After all that is what we do in protect.c. Neeed
|
|
|
|
to find common patterns and write a straightformward API to use
|
|
|
|
them. */
|
|
|
|
assert (sizeof (size_t) <= sizeof (void*));
|
|
|
|
|
|
|
|
format = xtrymalloc (15+7*strlen (elems)+10+15+1+1);
|
|
|
|
if (!format)
|
|
|
|
{
|
2010-04-21 18:26:17 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2005-02-23 22:06:32 +01:00
|
|
|
for (i=0; array[i]; i++)
|
|
|
|
gcry_mpi_release (array[i]);
|
|
|
|
xfree (array);
|
|
|
|
gcry_sexp_release (uri_sexp);
|
|
|
|
gcry_sexp_release (comment_sexp);
|
2010-04-21 18:26:17 +02:00
|
|
|
return err;
|
2005-02-23 22:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
argidx = 0;
|
|
|
|
p = stpcpy (stpcpy (format, "(public-key("), algoname);
|
2011-02-04 12:57:53 +01:00
|
|
|
for (idx=0, s=elems; *s; s++, idx++ )
|
2005-02-23 22:06:32 +01:00
|
|
|
{
|
|
|
|
*p++ = '(';
|
|
|
|
*p++ = *s;
|
|
|
|
p = stpcpy (p, " %m)");
|
|
|
|
assert (argidx < DIM (args));
|
2005-07-25 16:35:04 +02:00
|
|
|
args[argidx++] = &array[idx];
|
2005-02-23 22:06:32 +01:00
|
|
|
}
|
|
|
|
*p++ = ')';
|
|
|
|
if (uri)
|
|
|
|
{
|
|
|
|
p = stpcpy (p, "(uri %b)");
|
|
|
|
assert (argidx+1 < DIM (args));
|
|
|
|
args[argidx++] = (void *)uri_length;
|
|
|
|
args[argidx++] = (void *)uri;
|
|
|
|
}
|
|
|
|
if (comment)
|
|
|
|
{
|
|
|
|
p = stpcpy (p, "(comment %b)");
|
|
|
|
assert (argidx+1 < DIM (args));
|
|
|
|
args[argidx++] = (void *)comment_length;
|
|
|
|
args[argidx++] = (void*)comment;
|
|
|
|
}
|
|
|
|
*p++ = ')';
|
|
|
|
*p = 0;
|
|
|
|
assert (argidx < DIM (args));
|
|
|
|
args[argidx] = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
err = gcry_sexp_build_array (&list, NULL, format, args);
|
2005-02-23 22:06:32 +01:00
|
|
|
xfree (format);
|
|
|
|
for (i=0; array[i]; i++)
|
|
|
|
gcry_mpi_release (array[i]);
|
|
|
|
xfree (array);
|
|
|
|
gcry_sexp_release (uri_sexp);
|
|
|
|
gcry_sexp_release (comment_sexp);
|
|
|
|
|
2010-04-21 18:26:17 +02:00
|
|
|
if (!err)
|
2005-02-23 22:06:32 +01:00
|
|
|
*result = list;
|
2010-04-21 18:26:17 +02:00
|
|
|
return err;
|
2005-02-23 22:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-06-17 17:44:44 +02:00
|
|
|
/* Check whether the the secret key identified by GRIP is available.
|
|
|
|
Returns 0 is the key is available. */
|
2003-08-05 19:11:04 +02:00
|
|
|
int
|
|
|
|
agent_key_available (const unsigned char *grip)
|
|
|
|
{
|
2009-03-06 18:31:27 +01:00
|
|
|
int result;
|
2003-08-05 19:11:04 +02:00
|
|
|
char *fname;
|
|
|
|
char hexgrip[40+4+1];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
bin2hex (grip, 20, hexgrip);
|
2003-08-05 19:11:04 +02:00
|
|
|
strcpy (hexgrip+40, ".key");
|
|
|
|
|
|
|
|
fname = make_filename (opt.homedir, GNUPG_PRIVATE_KEYS_DIR, hexgrip, NULL);
|
2009-03-06 18:31:27 +01:00
|
|
|
result = !access (fname, R_OK)? 0 : -1;
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (fname);
|
2009-03-06 18:31:27 +01:00
|
|
|
return result;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
/* Return the information about the secret key specified by the binary
|
|
|
|
keygrip GRIP. If the key is a shadowed one the shadow information
|
|
|
|
will be stored at the address R_SHADOW_INFO as an allocated
|
|
|
|
S-expression. */
|
|
|
|
gpg_error_t
|
|
|
|
agent_key_info_from_file (ctrl_t ctrl, const unsigned char *grip,
|
|
|
|
int *r_keytype, unsigned char **r_shadow_info)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t len;
|
|
|
|
int keytype;
|
|
|
|
|
|
|
|
(void)ctrl;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
if (r_keytype)
|
|
|
|
*r_keytype = PRIVATE_KEY_UNKNOWN;
|
|
|
|
if (r_shadow_info)
|
|
|
|
*r_shadow_info = NULL;
|
|
|
|
|
|
|
|
{
|
|
|
|
gcry_sexp_t sexp;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
err = read_key_file (grip, &sexp);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_ENOENT)
|
|
|
|
return gpg_error (GPG_ERR_NOT_FOUND);
|
|
|
|
else
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
err = make_canon_sexp (sexp, &buf, &len);
|
|
|
|
gcry_sexp_release (sexp);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
keytype = agent_private_key_type (buf);
|
|
|
|
switch (keytype)
|
|
|
|
{
|
|
|
|
case PRIVATE_KEY_CLEAR:
|
2011-02-04 12:57:53 +01:00
|
|
|
break;
|
2009-03-06 18:31:27 +01:00
|
|
|
case PRIVATE_KEY_PROTECTED:
|
|
|
|
/* If we ever require it we could retrieve the comment fields
|
|
|
|
from such a key. */
|
|
|
|
break;
|
|
|
|
case PRIVATE_KEY_SHADOWED:
|
|
|
|
if (r_shadow_info)
|
|
|
|
{
|
|
|
|
const unsigned char *s;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
err = agent_get_shadow_info (buf, &s);
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
n = gcry_sexp_canon_len (s, 0, NULL, NULL);
|
|
|
|
assert (n);
|
|
|
|
*r_shadow_info = xtrymalloc (n);
|
|
|
|
if (!*r_shadow_info)
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
else
|
|
|
|
memcpy (*r_shadow_info, s, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err && r_keytype)
|
|
|
|
*r_keytype = keytype;
|
|
|
|
|
|
|
|
xfree (buf);
|
|
|
|
return err;
|
|
|
|
}
|