2010-08-31 15:58:39 +00:00
|
|
|
/* cvt-openpgp.c - Convert an OpenPGP key to our internal format.
|
2014-11-04 16:28:03 +01:00
|
|
|
* Copyright (C) 1998-2002, 2006, 2009, 2010 Free Software Foundation, Inc.
|
2014-04-15 16:40:48 +02:00
|
|
|
* Copyright (C) 2013, 2014 Werner Koch
|
2010-08-31 15:58:39 +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 3 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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2010-08-31 15:58:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "agent.h"
|
agent: Resolve conflict of util.h.
* agent/Makefile.am (AM_CPPFLAGS): Remove -I$(top_srcdir)/common.
* agent/call-pinentry.c, agent/call-scd.c: Follow the change.
* agent/command-ssh.c, agent/command.c, agent/cvt-openpgp.c: Ditto.
* agent/divert-scd.c, agent/findkey.c, agent/genkey.c: Ditto.
* agent/gpg-agent.c, agent/pksign.c, agent/preset-passphrase.c: Ditto.
* agent/protect-tool.c, agent/protect.c, agent/trustlist.c: Ditto.
* agent/w32main.c: Ditto.
--
For openpty function, we need to include util.h on some OS.
We also have util.h in common/, so this change is needed.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2017-03-07 19:22:48 +09:00
|
|
|
#include "../common/i18n.h"
|
2010-08-31 15:58:39 +00:00
|
|
|
#include "cvt-openpgp.h"
|
agent: Resolve conflict of util.h.
* agent/Makefile.am (AM_CPPFLAGS): Remove -I$(top_srcdir)/common.
* agent/call-pinentry.c, agent/call-scd.c: Follow the change.
* agent/command-ssh.c, agent/command.c, agent/cvt-openpgp.c: Ditto.
* agent/divert-scd.c, agent/findkey.c, agent/genkey.c: Ditto.
* agent/gpg-agent.c, agent/pksign.c, agent/preset-passphrase.c: Ditto.
* agent/protect-tool.c, agent/protect.c, agent/trustlist.c: Ditto.
* agent/w32main.c: Ditto.
--
For openpty function, we need to include util.h on some OS.
We also have util.h in common/, so this change is needed.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2017-03-07 19:22:48 +09:00
|
|
|
#include "../common/host2net.h"
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Helper to pass data via the callback to do_unprotect. */
|
2011-02-02 15:48:54 +01:00
|
|
|
struct try_do_unprotect_arg_s
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
int is_v4;
|
|
|
|
int is_protected;
|
|
|
|
int pubkey_algo;
|
2014-05-07 13:16:32 +02:00
|
|
|
const char *curve;
|
2010-08-31 15:58:39 +00:00
|
|
|
int protect_algo;
|
|
|
|
char *iv;
|
|
|
|
int ivlen;
|
|
|
|
int s2k_mode;
|
|
|
|
int s2k_algo;
|
|
|
|
byte *s2k_salt;
|
|
|
|
u32 s2k_count;
|
|
|
|
u16 desired_csum;
|
|
|
|
gcry_mpi_t *skey;
|
|
|
|
size_t skeysize;
|
|
|
|
int skeyidx;
|
|
|
|
gcry_sexp_t *r_key;
|
|
|
|
};
|
|
|
|
|
2011-01-12 21:14:45 -08:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
/* Compute the keygrip from the public key and store it at GRIP. */
|
|
|
|
static gpg_error_t
|
2014-05-07 13:16:32 +02:00
|
|
|
get_keygrip (int pubkey_algo, const char *curve, gcry_mpi_t *pkey,
|
|
|
|
unsigned char *grip)
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_sexp_t s_pkey = NULL;
|
|
|
|
|
|
|
|
switch (pubkey_algo)
|
|
|
|
{
|
|
|
|
case GCRY_PK_DSA:
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
"(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
|
|
|
|
pkey[0], pkey[1], pkey[2], pkey[3]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GCRY_PK_ELG:
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
"(public-key(elg(p%m)(g%m)(y%m)))",
|
|
|
|
pkey[0], pkey[1], pkey[2]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GCRY_PK_RSA:
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL,
|
|
|
|
"(public-key(rsa(n%m)(e%m)))", pkey[0], pkey[1]);
|
|
|
|
break;
|
|
|
|
|
2014-04-28 10:36:16 +09:00
|
|
|
case GCRY_PK_ECC:
|
2014-09-02 11:22:07 +02:00
|
|
|
if (!curve)
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
else
|
2015-08-06 17:00:41 +09:00
|
|
|
{
|
|
|
|
const char *format;
|
|
|
|
|
2015-08-10 19:13:13 +09:00
|
|
|
if (!strcmp (curve, "Ed25519"))
|
|
|
|
format = "(public-key(ecc(curve %s)(flags eddsa)(q%m)))";
|
|
|
|
else if (!strcmp (curve, "Curve25519"))
|
|
|
|
format = "(public-key(ecc(curve %s)(flags djb-tweak)(q%m)))";
|
2015-08-06 17:00:41 +09:00
|
|
|
else
|
|
|
|
format = "(public-key(ecc(curve %s)(q%m)))";
|
|
|
|
|
|
|
|
err = gcry_sexp_build (&s_pkey, NULL, format, curve, pkey[0]);
|
|
|
|
}
|
2011-01-05 17:33:17 -08:00
|
|
|
break;
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
default:
|
|
|
|
err = gpg_error (GPG_ERR_PUBKEY_ALGO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err && !gcry_pk_get_keygrip (s_pkey, grip))
|
|
|
|
err = gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Convert a secret key given as algorithm id and an array of key
|
2011-01-21 12:00:57 +01:00
|
|
|
parameters into our s-expression based format. Note that
|
2011-02-02 15:48:54 +01:00
|
|
|
PUBKEY_ALGO has an gcrypt algorithm number. */
|
2010-08-31 15:58:39 +00:00
|
|
|
static gpg_error_t
|
2014-05-07 13:16:32 +02:00
|
|
|
convert_secret_key (gcry_sexp_t *r_key, int pubkey_algo, gcry_mpi_t *skey,
|
|
|
|
const char *curve)
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_sexp_t s_skey = NULL;
|
|
|
|
|
|
|
|
*r_key = NULL;
|
|
|
|
|
|
|
|
switch (pubkey_algo)
|
|
|
|
{
|
|
|
|
case GCRY_PK_DSA:
|
|
|
|
err = gcry_sexp_build (&s_skey, NULL,
|
|
|
|
"(private-key(dsa(p%m)(q%m)(g%m)(y%m)(x%m)))",
|
|
|
|
skey[0], skey[1], skey[2], skey[3], skey[4]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GCRY_PK_ELG:
|
|
|
|
case GCRY_PK_ELG_E:
|
|
|
|
err = gcry_sexp_build (&s_skey, NULL,
|
|
|
|
"(private-key(elg(p%m)(g%m)(y%m)(x%m)))",
|
|
|
|
skey[0], skey[1], skey[2], skey[3]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case GCRY_PK_RSA:
|
|
|
|
case GCRY_PK_RSA_E:
|
|
|
|
case GCRY_PK_RSA_S:
|
|
|
|
err = gcry_sexp_build (&s_skey, NULL,
|
|
|
|
"(private-key(rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
|
|
|
|
skey[0], skey[1], skey[2], skey[3], skey[4],
|
|
|
|
skey[5]);
|
2010-10-06 11:29:10 +00:00
|
|
|
break;
|
2010-08-31 15:58:39 +00:00
|
|
|
|
2014-04-28 10:36:16 +09:00
|
|
|
case GCRY_PK_ECC:
|
2014-05-07 13:16:32 +02:00
|
|
|
if (!curve)
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SECKEY);
|
2015-08-06 17:00:41 +09:00
|
|
|
else
|
2014-09-02 11:22:07 +02:00
|
|
|
{
|
2015-08-06 17:00:41 +09:00
|
|
|
const char *format;
|
|
|
|
|
2015-08-10 19:13:13 +09:00
|
|
|
if (!strcmp (curve, "Ed25519"))
|
|
|
|
/* Do not store the OID as name but the real name and the
|
|
|
|
EdDSA flag. */
|
2020-10-30 09:23:35 +09:00
|
|
|
format = "(private-key(ecc(curve %s)(flags eddsa)(q%m)(d%M)))";
|
2015-08-10 19:13:13 +09:00
|
|
|
else if (!strcmp (curve, "Curve25519"))
|
|
|
|
format = "(private-key(ecc(curve %s)(flags djb-tweak)(q%m)(d%m)))";
|
2015-08-06 17:00:41 +09:00
|
|
|
else
|
|
|
|
format = "(private-key(ecc(curve %s)(q%m)(d%m)))";
|
|
|
|
|
|
|
|
err = gcry_sexp_build (&s_skey, NULL, format, curve, skey[0], skey[1]);
|
2014-09-02 11:22:07 +02:00
|
|
|
}
|
2011-01-05 17:33:17 -08:00
|
|
|
break;
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
default:
|
|
|
|
err = gpg_error (GPG_ERR_PUBKEY_ALGO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
*r_key = s_skey;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-22 09:50:12 +01:00
|
|
|
/* Convert a secret key given as algorithm id, an array of key
|
|
|
|
parameters, and an S-expression of the original OpenPGP transfer
|
|
|
|
key into our s-expression based format. This is a variant of
|
|
|
|
convert_secret_key which is used for the openpgp-native protection
|
|
|
|
mode. Note that PUBKEY_ALGO has an gcrypt algorithm number. */
|
|
|
|
static gpg_error_t
|
|
|
|
convert_transfer_key (gcry_sexp_t *r_key, int pubkey_algo, gcry_mpi_t *skey,
|
2014-05-07 13:16:32 +02:00
|
|
|
const char *curve, gcry_sexp_t transfer_key)
|
2013-05-22 09:50:12 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_sexp_t s_skey = NULL;
|
|
|
|
|
|
|
|
*r_key = NULL;
|
|
|
|
|
|
|
|
switch (pubkey_algo)
|
|
|
|
{
|
|
|
|
case GCRY_PK_DSA:
|
|
|
|
err = gcry_sexp_build
|
|
|
|
(&s_skey, NULL,
|
|
|
|
"(protected-private-key(dsa(p%m)(q%m)(g%m)(y%m)"
|
|
|
|
"(protected openpgp-native%S)))",
|
|
|
|
skey[0], skey[1], skey[2], skey[3], transfer_key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GCRY_PK_ELG:
|
|
|
|
err = gcry_sexp_build
|
|
|
|
(&s_skey, NULL,
|
|
|
|
"(protected-private-key(elg(p%m)(g%m)(y%m)"
|
|
|
|
"(protected openpgp-native%S)))",
|
|
|
|
skey[0], skey[1], skey[2], transfer_key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case GCRY_PK_RSA:
|
|
|
|
err = gcry_sexp_build
|
|
|
|
(&s_skey, NULL,
|
2014-06-25 20:25:28 +02:00
|
|
|
"(protected-private-key(rsa(n%m)(e%m)"
|
2013-05-22 09:50:12 +01:00
|
|
|
"(protected openpgp-native%S)))",
|
|
|
|
skey[0], skey[1], transfer_key );
|
|
|
|
break;
|
|
|
|
|
2014-04-28 10:36:16 +09:00
|
|
|
case GCRY_PK_ECC:
|
2014-09-02 11:22:07 +02:00
|
|
|
if (!curve)
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SECKEY);
|
2015-08-06 17:00:41 +09:00
|
|
|
else
|
2014-09-02 11:22:07 +02:00
|
|
|
{
|
2015-08-06 17:00:41 +09:00
|
|
|
const char *format;
|
|
|
|
|
2015-08-10 19:13:13 +09:00
|
|
|
if (!strcmp (curve, "Ed25519"))
|
|
|
|
/* Do not store the OID as name but the real name and the
|
|
|
|
EdDSA flag. */
|
|
|
|
format = "(protected-private-key(ecc(curve %s)(flags eddsa)(q%m)"
|
|
|
|
"(protected openpgp-native%S)))";
|
|
|
|
else if (!strcmp (curve, "Curve25519"))
|
|
|
|
format = "(protected-private-key(ecc(curve %s)(flags djb-tweak)(q%m)"
|
|
|
|
"(protected openpgp-native%S)))";
|
2015-08-06 17:00:41 +09:00
|
|
|
else
|
2015-08-10 19:13:13 +09:00
|
|
|
format = "(protected-private-key(ecc(curve %s)(q%m)"
|
|
|
|
"(protected openpgp-native%S)))";
|
2015-08-06 17:00:41 +09:00
|
|
|
|
|
|
|
err = gcry_sexp_build (&s_skey, NULL, format, curve, skey[0], transfer_key);
|
2014-09-02 11:22:07 +02:00
|
|
|
}
|
2013-05-22 09:50:12 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = gpg_error (GPG_ERR_PUBKEY_ALGO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err)
|
|
|
|
*r_key = s_skey;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
/* Hash the passphrase and set the key. */
|
|
|
|
static gpg_error_t
|
|
|
|
hash_passphrase_and_set_key (const char *passphrase,
|
|
|
|
gcry_cipher_hd_t hd, int protect_algo,
|
|
|
|
int s2k_mode, int s2k_algo,
|
|
|
|
byte *s2k_salt, u32 s2k_count)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
unsigned char *key;
|
|
|
|
size_t keylen;
|
|
|
|
|
|
|
|
keylen = gcry_cipher_get_algo_keylen (protect_algo);
|
|
|
|
if (!keylen)
|
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
key = xtrymalloc_secure (keylen);
|
|
|
|
if (!key)
|
|
|
|
return gpg_error_from_syserror ();
|
|
|
|
|
|
|
|
err = s2k_hash_passphrase (passphrase,
|
|
|
|
s2k_algo, s2k_mode, s2k_salt, s2k_count,
|
|
|
|
key, keylen);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_setkey (hd, key, keylen);
|
|
|
|
|
|
|
|
xfree (key);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static u16
|
|
|
|
checksum (const unsigned char *p, unsigned int n)
|
|
|
|
{
|
|
|
|
u16 a;
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
for (a=0; n; n-- )
|
|
|
|
a += *p++;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-07 13:16:32 +02:00
|
|
|
/* Return the number of expected key parameters. */
|
|
|
|
static void
|
|
|
|
get_npkey_nskey (int pubkey_algo, size_t *npkey, size_t *nskey)
|
|
|
|
{
|
|
|
|
switch (pubkey_algo)
|
|
|
|
{
|
|
|
|
case GCRY_PK_RSA: *npkey = 2; *nskey = 6; break;
|
|
|
|
case GCRY_PK_ELG: *npkey = 3; *nskey = 4; break;
|
|
|
|
case GCRY_PK_ELG_E: *npkey = 3; *nskey = 4; break;
|
|
|
|
case GCRY_PK_DSA: *npkey = 4; *nskey = 5; break;
|
|
|
|
case GCRY_PK_ECC: *npkey = 1; *nskey = 2; break;
|
|
|
|
default: *npkey = 0; *nskey = 0; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-22 09:50:12 +01:00
|
|
|
/* Helper for do_unprotect. PUBKEY_ALOGO is the gcrypt algo number.
|
|
|
|
On success R_NPKEY and R_NSKEY receive the number or parameters for
|
|
|
|
the algorithm PUBKEY_ALGO and R_SKEYLEN the used length of
|
|
|
|
SKEY. */
|
2010-08-31 15:58:39 +00:00
|
|
|
static int
|
2013-05-22 09:50:12 +01:00
|
|
|
prepare_unprotect (int pubkey_algo, gcry_mpi_t *skey, size_t skeysize,
|
|
|
|
int s2k_mode,
|
|
|
|
unsigned int *r_npkey, unsigned int *r_nskey,
|
|
|
|
unsigned int *r_skeylen)
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
size_t npkey, nskey, skeylen;
|
|
|
|
int i;
|
2011-01-05 17:33:17 -08:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
/* Count the actual number of MPIs is in the array and set the
|
|
|
|
remainder to NULL for easier processing later on. */
|
|
|
|
for (skeylen = 0; skey[skeylen]; skeylen++)
|
|
|
|
;
|
|
|
|
for (i=skeylen; i < skeysize; i++)
|
|
|
|
skey[i] = NULL;
|
|
|
|
|
|
|
|
/* Check some args. */
|
|
|
|
if (s2k_mode == 1001)
|
|
|
|
{
|
|
|
|
/* Stub key. */
|
|
|
|
log_info (_("secret key parts are not available\n"));
|
|
|
|
return gpg_error (GPG_ERR_UNUSABLE_SECKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gcry_pk_test_algo (pubkey_algo))
|
|
|
|
{
|
|
|
|
log_info (_("public key algorithm %d (%s) is not supported\n"),
|
|
|
|
pubkey_algo, gcry_pk_algo_name (pubkey_algo));
|
|
|
|
return gpg_error (GPG_ERR_PUBKEY_ALGO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get properties of the public key algorithm and do some
|
|
|
|
consistency checks. Note that we need at least NPKEY+1 elements
|
|
|
|
in the SKEY array. */
|
2014-05-07 13:16:32 +02:00
|
|
|
get_npkey_nskey (pubkey_algo, &npkey, &nskey);
|
|
|
|
if (!npkey || !nskey || npkey >= nskey)
|
2010-08-31 15:58:39 +00:00
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
if (skeylen <= npkey)
|
|
|
|
return gpg_error (GPG_ERR_MISSING_VALUE);
|
|
|
|
if (nskey+1 >= skeysize)
|
|
|
|
return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2013-05-22 09:50:12 +01:00
|
|
|
/* Check that the public key parameters are all available and not
|
|
|
|
encrypted. */
|
|
|
|
for (i=0; i < npkey; i++)
|
|
|
|
{
|
2014-05-07 13:16:32 +02:00
|
|
|
if (!skey[i] || gcry_mpi_get_flag (skey[i], GCRYMPI_FLAG_USER1))
|
2013-05-22 09:50:12 +01:00
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_npkey)
|
|
|
|
*r_npkey = npkey;
|
|
|
|
if (r_nskey)
|
|
|
|
*r_nskey = nskey;
|
|
|
|
if (r_skeylen)
|
|
|
|
*r_skeylen = skeylen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Note that this function modifies SKEY. SKEYSIZE is the allocated
|
|
|
|
size of the array including the NULL item; this is used for a
|
|
|
|
bounds check. On success a converted key is stored at R_KEY. */
|
|
|
|
static int
|
|
|
|
do_unprotect (const char *passphrase,
|
|
|
|
int pkt_version, int pubkey_algo, int is_protected,
|
2014-05-07 13:16:32 +02:00
|
|
|
const char *curve, gcry_mpi_t *skey, size_t skeysize,
|
2013-05-22 09:50:12 +01:00
|
|
|
int protect_algo, void *protect_iv, size_t protect_ivlen,
|
|
|
|
int s2k_mode, int s2k_algo, byte *s2k_salt, u32 s2k_count,
|
|
|
|
u16 desired_csum, gcry_sexp_t *r_key)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
unsigned int npkey, nskey, skeylen;
|
|
|
|
gcry_cipher_hd_t cipher_hd = NULL;
|
|
|
|
u16 actual_csum;
|
|
|
|
size_t nbytes;
|
|
|
|
int i;
|
|
|
|
gcry_mpi_t tmpmpi;
|
|
|
|
|
|
|
|
*r_key = NULL;
|
|
|
|
|
|
|
|
err = prepare_unprotect (pubkey_algo, skey, skeysize, s2k_mode,
|
|
|
|
&npkey, &nskey, &skeylen);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
/* Check whether SKEY is at all protected. If it is not protected
|
|
|
|
merely verify the checksum. */
|
|
|
|
if (!is_protected)
|
|
|
|
{
|
|
|
|
actual_csum = 0;
|
|
|
|
for (i=npkey; i < nskey; i++)
|
|
|
|
{
|
2014-05-07 13:16:32 +02:00
|
|
|
if (!skey[i] || gcry_mpi_get_flag (skey[i], GCRYMPI_FLAG_USER1))
|
2010-08-31 15:58:39 +00:00
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2014-09-02 11:22:07 +02:00
|
|
|
if (gcry_mpi_get_flag (skey[i], GCRYMPI_FLAG_OPAQUE))
|
2014-05-07 13:16:32 +02:00
|
|
|
{
|
|
|
|
unsigned int nbits;
|
|
|
|
const unsigned char *buffer;
|
|
|
|
buffer = gcry_mpi_get_opaque (skey[i], &nbits);
|
|
|
|
nbytes = (nbits+7)/8;
|
|
|
|
actual_csum += checksum (buffer, nbytes);
|
|
|
|
}
|
|
|
|
else
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
2014-05-07 13:16:32 +02:00
|
|
|
unsigned char *buffer;
|
|
|
|
|
|
|
|
err = gcry_mpi_aprint (GCRYMPI_FMT_PGP, &buffer, &nbytes,
|
|
|
|
skey[i]);
|
2010-08-31 15:58:39 +00:00
|
|
|
if (!err)
|
|
|
|
actual_csum += checksum (buffer, nbytes);
|
|
|
|
xfree (buffer);
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
if (actual_csum != desired_csum)
|
|
|
|
return gpg_error (GPG_ERR_CHECKSUM);
|
2014-05-08 10:24:36 +02:00
|
|
|
|
|
|
|
goto do_convert;
|
2010-08-31 15:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (gcry_cipher_test_algo (protect_algo))
|
|
|
|
{
|
|
|
|
/* The algorithm numbers are Libgcrypt numbers but fortunately
|
|
|
|
the OpenPGP algorithm numbers map one-to-one to the Libgcrypt
|
|
|
|
numbers. */
|
|
|
|
log_info (_("protection algorithm %d (%s) is not supported\n"),
|
2010-12-02 15:49:02 +00:00
|
|
|
protect_algo, gnupg_cipher_algo_name (protect_algo));
|
2010-08-31 15:58:39 +00:00
|
|
|
return gpg_error (GPG_ERR_CIPHER_ALGO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gcry_md_test_algo (s2k_algo))
|
|
|
|
{
|
|
|
|
log_info (_("protection hash algorithm %d (%s) is not supported\n"),
|
|
|
|
s2k_algo, gcry_md_algo_name (s2k_algo));
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
err = gcry_cipher_open (&cipher_hd, protect_algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB,
|
|
|
|
(GCRY_CIPHER_SECURE
|
|
|
|
| (protect_algo >= 100 ?
|
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC)));
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error ("failed to open cipher_algo %d: %s\n",
|
|
|
|
protect_algo, gpg_strerror (err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hash_passphrase_and_set_key (passphrase, cipher_hd, protect_algo,
|
|
|
|
s2k_mode, s2k_algo, s2k_salt, s2k_count);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
gcry_cipher_close (cipher_hd);
|
|
|
|
return err;
|
2011-02-02 15:48:54 +01:00
|
|
|
}
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
gcry_cipher_setiv (cipher_hd, protect_iv, protect_ivlen);
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
actual_csum = 0;
|
|
|
|
if (pkt_version >= 4)
|
|
|
|
{
|
|
|
|
int ndata;
|
|
|
|
unsigned int ndatabits;
|
2014-05-07 13:16:32 +02:00
|
|
|
const unsigned char *p;
|
|
|
|
unsigned char *data;
|
2010-08-31 15:58:39 +00:00
|
|
|
u16 csum_pgp7 = 0;
|
|
|
|
|
|
|
|
if (!gcry_mpi_get_flag (skey[npkey], GCRYMPI_FLAG_OPAQUE ))
|
|
|
|
{
|
|
|
|
gcry_cipher_close (cipher_hd);
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
p = gcry_mpi_get_opaque (skey[npkey], &ndatabits);
|
|
|
|
ndata = (ndatabits+7)/8;
|
|
|
|
|
|
|
|
if (ndata > 1)
|
2015-02-11 10:27:57 +01:00
|
|
|
csum_pgp7 = buf16_to_u16 (p+ndata-2);
|
2010-08-31 15:58:39 +00:00
|
|
|
data = xtrymalloc_secure (ndata);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
gcry_cipher_close (cipher_hd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
gcry_cipher_decrypt (cipher_hd, data, ndata, p, ndata);
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
if (is_protected == 2)
|
|
|
|
{
|
|
|
|
/* This is the new SHA1 checksum method to detect tampering
|
|
|
|
with the key as used by the Klima/Rosa attack. */
|
2011-02-02 15:48:54 +01:00
|
|
|
desired_csum = 0;
|
2010-08-31 15:58:39 +00:00
|
|
|
actual_csum = 1; /* Default to bad checksum. */
|
|
|
|
|
2011-02-02 15:48:54 +01:00
|
|
|
if (ndata < 20)
|
2010-08-31 15:58:39 +00:00
|
|
|
log_error ("not enough bytes for SHA-1 checksum\n");
|
2011-02-02 15:48:54 +01:00
|
|
|
else
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
gcry_md_hd_t h;
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
if (gcry_md_open (&h, GCRY_MD_SHA1, 1))
|
|
|
|
BUG(); /* Algo not available. */
|
|
|
|
gcry_md_write (h, data, ndata - 20);
|
|
|
|
gcry_md_final (h);
|
|
|
|
if (!memcmp (gcry_md_read (h, GCRY_MD_SHA1), data+ndata-20, 20))
|
|
|
|
actual_csum = 0; /* Digest does match. */
|
|
|
|
gcry_md_close (h);
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
else
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
/* Old 16 bit checksum method. */
|
|
|
|
if (ndata < 2)
|
|
|
|
{
|
|
|
|
log_error ("not enough bytes for checksum\n");
|
2011-02-02 15:48:54 +01:00
|
|
|
desired_csum = 0;
|
2010-08-31 15:58:39 +00:00
|
|
|
actual_csum = 1; /* Mark checksum bad. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-11 10:27:57 +01:00
|
|
|
desired_csum = buf16_to_u16 (data+ndata-2);
|
2010-08-31 15:58:39 +00:00
|
|
|
actual_csum = checksum (data, ndata-2);
|
|
|
|
if (desired_csum != actual_csum)
|
|
|
|
{
|
|
|
|
/* This is a PGP 7.0.0 workaround */
|
|
|
|
desired_csum = csum_pgp7; /* Take the encrypted one. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
/* Better check it here. Otherwise the gcry_mpi_scan would fail
|
|
|
|
because the length may have an arbitrary value. */
|
|
|
|
if (desired_csum == actual_csum)
|
|
|
|
{
|
|
|
|
for (i=npkey; i < nskey; i++ )
|
|
|
|
{
|
|
|
|
if (gcry_mpi_scan (&tmpmpi, GCRYMPI_FMT_PGP, p, ndata, &nbytes))
|
|
|
|
{
|
|
|
|
/* Checksum was okay, but not correctly decrypted. */
|
|
|
|
desired_csum = 0;
|
|
|
|
actual_csum = 1; /* Mark checksum bad. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gcry_mpi_release (skey[i]);
|
|
|
|
skey[i] = tmpmpi;
|
|
|
|
ndata -= nbytes;
|
|
|
|
p += nbytes;
|
|
|
|
}
|
|
|
|
skey[i] = NULL;
|
|
|
|
skeylen = i;
|
|
|
|
assert (skeylen <= skeysize);
|
|
|
|
|
|
|
|
/* Note: at this point NDATA should be 2 for a simple
|
|
|
|
checksum or 20 for the sha1 digest. */
|
|
|
|
}
|
|
|
|
xfree(data);
|
|
|
|
}
|
|
|
|
else /* Packet version <= 3. */
|
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
|
|
|
for (i = npkey; i < nskey; i++)
|
|
|
|
{
|
2014-05-07 13:16:32 +02:00
|
|
|
const unsigned char *p;
|
2010-08-31 15:58:39 +00:00
|
|
|
size_t ndata;
|
|
|
|
unsigned int ndatabits;
|
|
|
|
|
|
|
|
if (!skey[i] || !gcry_mpi_get_flag (skey[i], GCRYMPI_FLAG_OPAQUE))
|
|
|
|
{
|
|
|
|
gcry_cipher_close (cipher_hd);
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
p = gcry_mpi_get_opaque (skey[i], &ndatabits);
|
|
|
|
ndata = (ndatabits+7)/8;
|
|
|
|
|
2015-02-11 10:27:57 +01:00
|
|
|
if (!(ndata >= 2) || !(ndata == (buf16_to_ushort (p) + 7)/8 + 2))
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
gcry_cipher_close (cipher_hd);
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
buffer = xtrymalloc_secure (ndata);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
gcry_cipher_close (cipher_hd);
|
|
|
|
return err;
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
gcry_cipher_sync (cipher_hd);
|
|
|
|
buffer[0] = p[0];
|
|
|
|
buffer[1] = p[1];
|
|
|
|
gcry_cipher_decrypt (cipher_hd, buffer+2, ndata-2, p+2, ndata-2);
|
|
|
|
actual_csum += checksum (buffer, ndata);
|
|
|
|
err = gcry_mpi_scan (&tmpmpi, GCRYMPI_FMT_PGP, buffer, ndata, &ndata);
|
|
|
|
xfree (buffer);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
/* Checksum was okay, but not correctly decrypted. */
|
|
|
|
desired_csum = 0;
|
|
|
|
actual_csum = 1; /* Mark checksum bad. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gcry_mpi_release (skey[i]);
|
|
|
|
skey[i] = tmpmpi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gcry_cipher_close (cipher_hd);
|
|
|
|
|
|
|
|
/* Now let's see whether we have used the correct passphrase. */
|
|
|
|
if (actual_csum != desired_csum)
|
|
|
|
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
|
|
|
|
|
2014-05-08 10:24:36 +02:00
|
|
|
do_convert:
|
2010-08-31 15:58:39 +00:00
|
|
|
if (nskey != skeylen)
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
else
|
2014-05-07 13:16:32 +02:00
|
|
|
err = convert_secret_key (r_key, pubkey_algo, skey, curve);
|
2010-08-31 15:58:39 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* The checksum may fail, thus we also check the key itself. */
|
|
|
|
err = gcry_pk_testkey (*r_key);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
gcry_sexp_release (*r_key);
|
|
|
|
*r_key = NULL;
|
|
|
|
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-22 09:50:12 +01:00
|
|
|
/* Callback function to try the unprotection from the passphrase query
|
2010-08-31 15:58:39 +00:00
|
|
|
code. */
|
2015-10-09 11:33:13 +09:00
|
|
|
static gpg_error_t
|
2010-08-31 15:58:39 +00:00
|
|
|
try_do_unprotect_cb (struct pin_entry_info_s *pi)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
struct try_do_unprotect_arg_s *arg = pi->check_cb_arg;
|
|
|
|
|
2010-09-01 09:48:35 +00:00
|
|
|
err = do_unprotect (pi->pin,
|
|
|
|
arg->is_v4? 4:3,
|
2010-08-31 15:58:39 +00:00
|
|
|
arg->pubkey_algo, arg->is_protected,
|
2014-05-07 13:16:32 +02:00
|
|
|
arg->curve,
|
2010-08-31 15:58:39 +00:00
|
|
|
arg->skey, arg->skeysize,
|
|
|
|
arg->protect_algo, arg->iv, arg->ivlen,
|
|
|
|
arg->s2k_mode, arg->s2k_algo,
|
|
|
|
arg->s2k_salt, arg->s2k_count,
|
|
|
|
arg->desired_csum, arg->r_key);
|
|
|
|
/* SKEY may be modified now, thus we need to re-compute SKEYIDX. */
|
|
|
|
for (arg->skeyidx = 0; (arg->skeyidx < arg->skeysize
|
|
|
|
&& arg->skey[arg->skeyidx]); arg->skeyidx++)
|
2010-09-01 09:48:35 +00:00
|
|
|
;
|
2010-08-31 15:58:39 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-22 09:50:12 +01:00
|
|
|
/* See convert_from_openpgp for the core of the description. This
|
|
|
|
function adds an optional PASSPHRASE argument and uses this to
|
|
|
|
silently decrypt the key; CACHE_NONCE and R_PASSPHRASE must both be
|
|
|
|
NULL in this mode. */
|
|
|
|
static gpg_error_t
|
2015-12-24 14:19:53 +09:00
|
|
|
convert_from_openpgp_main (ctrl_t ctrl, gcry_sexp_t s_pgp, int dontcare_exist,
|
2013-05-22 09:50:12 +01:00
|
|
|
unsigned char *grip, const char *prompt,
|
|
|
|
const char *cache_nonce, const char *passphrase,
|
|
|
|
unsigned char **r_key, char **r_passphrase)
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
2013-05-22 09:50:12 +01:00
|
|
|
int unattended;
|
|
|
|
int from_native;
|
2010-08-31 15:58:39 +00:00
|
|
|
gcry_sexp_t top_list;
|
|
|
|
gcry_sexp_t list = NULL;
|
|
|
|
const char *value;
|
|
|
|
size_t valuelen;
|
|
|
|
char *string;
|
|
|
|
int idx;
|
|
|
|
int is_v4, is_protected;
|
|
|
|
int pubkey_algo;
|
|
|
|
int protect_algo = 0;
|
|
|
|
char iv[16];
|
|
|
|
int ivlen = 0;
|
|
|
|
int s2k_mode = 0;
|
|
|
|
int s2k_algo = 0;
|
|
|
|
byte s2k_salt[8];
|
|
|
|
u32 s2k_count = 0;
|
|
|
|
size_t npkey, nskey;
|
|
|
|
gcry_mpi_t skey[10]; /* We support up to 9 parameters. */
|
2014-05-07 13:16:32 +02:00
|
|
|
char *curve = NULL;
|
2010-08-31 15:58:39 +00:00
|
|
|
u16 desired_csum;
|
|
|
|
int skeyidx = 0;
|
2013-05-22 09:50:12 +01:00
|
|
|
gcry_sexp_t s_skey = NULL;
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
*r_key = NULL;
|
2013-05-22 09:50:12 +01:00
|
|
|
if (r_passphrase)
|
|
|
|
*r_passphrase = NULL;
|
|
|
|
unattended = !r_passphrase;
|
|
|
|
from_native = (!cache_nonce && passphrase && !r_passphrase);
|
2010-08-31 15:58:39 +00:00
|
|
|
|
|
|
|
top_list = gcry_sexp_find_token (s_pgp, "openpgp-private-key", 0);
|
|
|
|
if (!top_list)
|
|
|
|
goto bad_seckey;
|
|
|
|
|
|
|
|
list = gcry_sexp_find_token (top_list, "version", 0);
|
|
|
|
if (!list)
|
|
|
|
goto bad_seckey;
|
|
|
|
value = gcry_sexp_nth_data (list, 1, &valuelen);
|
|
|
|
if (!value || valuelen != 1 || !(value[0] == '3' || value[0] == '4'))
|
|
|
|
goto bad_seckey;
|
|
|
|
is_v4 = (value[0] == '4');
|
|
|
|
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = gcry_sexp_find_token (top_list, "protection", 0);
|
|
|
|
if (!list)
|
|
|
|
goto bad_seckey;
|
|
|
|
value = gcry_sexp_nth_data (list, 1, &valuelen);
|
|
|
|
if (!value)
|
|
|
|
goto bad_seckey;
|
|
|
|
if (valuelen == 4 && !memcmp (value, "sha1", 4))
|
|
|
|
is_protected = 2;
|
|
|
|
else if (valuelen == 3 && !memcmp (value, "sum", 3))
|
|
|
|
is_protected = 1;
|
|
|
|
else if (valuelen == 4 && !memcmp (value, "none", 4))
|
|
|
|
is_protected = 0;
|
|
|
|
else
|
|
|
|
goto bad_seckey;
|
2013-05-22 09:50:12 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
if (is_protected)
|
|
|
|
{
|
|
|
|
string = gcry_sexp_nth_string (list, 2);
|
|
|
|
if (!string)
|
|
|
|
goto bad_seckey;
|
|
|
|
protect_algo = gcry_cipher_map_name (string);
|
|
|
|
xfree (string);
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
value = gcry_sexp_nth_data (list, 3, &valuelen);
|
|
|
|
if (!value || !valuelen || valuelen > sizeof iv)
|
|
|
|
goto bad_seckey;
|
|
|
|
memcpy (iv, value, valuelen);
|
|
|
|
ivlen = valuelen;
|
|
|
|
|
|
|
|
string = gcry_sexp_nth_string (list, 4);
|
|
|
|
if (!string)
|
|
|
|
goto bad_seckey;
|
|
|
|
s2k_mode = strtol (string, NULL, 10);
|
|
|
|
xfree (string);
|
|
|
|
|
|
|
|
string = gcry_sexp_nth_string (list, 5);
|
|
|
|
if (!string)
|
|
|
|
goto bad_seckey;
|
|
|
|
s2k_algo = gcry_md_map_name (string);
|
|
|
|
xfree (string);
|
|
|
|
|
|
|
|
value = gcry_sexp_nth_data (list, 6, &valuelen);
|
|
|
|
if (!value || !valuelen || valuelen > sizeof s2k_salt)
|
|
|
|
goto bad_seckey;
|
|
|
|
memcpy (s2k_salt, value, valuelen);
|
|
|
|
|
|
|
|
string = gcry_sexp_nth_string (list, 7);
|
|
|
|
if (!string)
|
|
|
|
goto bad_seckey;
|
|
|
|
s2k_count = strtoul (string, NULL, 10);
|
|
|
|
xfree (string);
|
|
|
|
}
|
|
|
|
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = gcry_sexp_find_token (top_list, "algo", 0);
|
|
|
|
if (!list)
|
|
|
|
goto bad_seckey;
|
|
|
|
string = gcry_sexp_nth_string (list, 1);
|
|
|
|
if (!string)
|
|
|
|
goto bad_seckey;
|
2011-01-21 12:00:57 +01:00
|
|
|
pubkey_algo = gcry_pk_map_name (string);
|
2010-08-31 15:58:39 +00:00
|
|
|
xfree (string);
|
|
|
|
|
2014-05-07 13:16:32 +02:00
|
|
|
get_npkey_nskey (pubkey_algo, &npkey, &nskey);
|
|
|
|
if (!npkey || !nskey || npkey >= nskey)
|
2010-08-31 15:58:39 +00:00
|
|
|
goto bad_seckey;
|
|
|
|
|
2014-05-07 13:16:32 +02:00
|
|
|
if (npkey == 1) /* This is ECC */
|
|
|
|
{
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = gcry_sexp_find_token (top_list, "curve", 0);
|
|
|
|
if (!list)
|
|
|
|
goto bad_seckey;
|
|
|
|
curve = gcry_sexp_nth_string (list, 1);
|
|
|
|
if (!curve)
|
|
|
|
goto bad_seckey;
|
|
|
|
}
|
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = gcry_sexp_find_token (top_list, "skey", 0);
|
|
|
|
if (!list)
|
|
|
|
goto bad_seckey;
|
|
|
|
for (idx=0;;)
|
|
|
|
{
|
|
|
|
int is_enc;
|
|
|
|
|
|
|
|
value = gcry_sexp_nth_data (list, ++idx, &valuelen);
|
|
|
|
if (!value && skeyidx >= npkey)
|
|
|
|
break; /* Ready. */
|
|
|
|
|
|
|
|
/* Check for too many parameters. Note that depending on the
|
|
|
|
protection mode and version number we may see less than NSKEY
|
|
|
|
(but at least NPKEY+1) parameters. */
|
|
|
|
if (idx >= 2*nskey)
|
|
|
|
goto bad_seckey;
|
|
|
|
if (skeyidx >= DIM (skey)-1)
|
|
|
|
goto bad_seckey;
|
|
|
|
|
|
|
|
if (!value || valuelen != 1 || !(value[0] == '_' || value[0] == 'e'))
|
|
|
|
goto bad_seckey;
|
|
|
|
is_enc = (value[0] == 'e');
|
|
|
|
value = gcry_sexp_nth_data (list, ++idx, &valuelen);
|
|
|
|
if (!value || !valuelen)
|
|
|
|
goto bad_seckey;
|
2015-09-19 17:45:17 +09:00
|
|
|
if (is_enc)
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
2015-09-19 17:45:17 +09:00
|
|
|
/* Encrypted parameters need to be stored as opaque. */
|
2014-05-07 13:16:32 +02:00
|
|
|
skey[skeyidx] = gcry_mpi_set_opaque_copy (NULL, value, valuelen*8);
|
2010-08-31 15:58:39 +00:00
|
|
|
if (!skey[skeyidx])
|
|
|
|
goto outofmem;
|
2015-09-19 17:45:17 +09:00
|
|
|
gcry_mpi_set_flag (skey[skeyidx], GCRYMPI_FLAG_USER1);
|
2010-08-31 15:58:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (gcry_mpi_scan (skey + skeyidx, GCRYMPI_FMT_STD,
|
|
|
|
value, valuelen, NULL))
|
|
|
|
goto bad_seckey;
|
|
|
|
}
|
|
|
|
skeyidx++;
|
|
|
|
}
|
|
|
|
skey[skeyidx++] = NULL;
|
|
|
|
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = gcry_sexp_find_token (top_list, "csum", 0);
|
|
|
|
if (list)
|
|
|
|
{
|
|
|
|
string = gcry_sexp_nth_string (list, 1);
|
|
|
|
if (!string)
|
|
|
|
goto bad_seckey;
|
|
|
|
desired_csum = strtoul (string, NULL, 10);
|
|
|
|
xfree (string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
desired_csum = 0;
|
|
|
|
|
|
|
|
|
|
|
|
gcry_sexp_release (list); list = NULL;
|
|
|
|
gcry_sexp_release (top_list); top_list = NULL;
|
|
|
|
|
2014-05-07 13:16:32 +02:00
|
|
|
#if 0
|
|
|
|
log_debug ("XXX is_v4=%d\n", is_v4);
|
|
|
|
log_debug ("XXX pubkey_algo=%d\n", pubkey_algo);
|
|
|
|
log_debug ("XXX is_protected=%d\n", is_protected);
|
|
|
|
log_debug ("XXX protect_algo=%d\n", protect_algo);
|
2020-05-12 18:51:47 +02:00
|
|
|
log_printhex (iv, ivlen, "XXX iv");
|
2014-05-07 13:16:32 +02:00
|
|
|
log_debug ("XXX ivlen=%d\n", ivlen);
|
|
|
|
log_debug ("XXX s2k_mode=%d\n", s2k_mode);
|
|
|
|
log_debug ("XXX s2k_algo=%d\n", s2k_algo);
|
2020-05-12 18:51:47 +02:00
|
|
|
log_printhex (s2k_salt, sizeof s2k_salt, "XXX s2k_salt");
|
2014-05-07 13:16:32 +02:00
|
|
|
log_debug ("XXX s2k_count=%lu\n", (unsigned long)s2k_count);
|
|
|
|
log_debug ("XXX curve='%s'\n", curve);
|
|
|
|
for (idx=0; skey[idx]; idx++)
|
|
|
|
gcry_log_debugmpi (gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_USER1)
|
|
|
|
? "skey(e)" : "skey(_)", skey[idx]);
|
|
|
|
#endif /*0*/
|
|
|
|
|
|
|
|
err = get_keygrip (pubkey_algo, curve, skey, grip);
|
2010-08-31 15:58:39 +00:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
2015-12-24 14:19:53 +09:00
|
|
|
if (!dontcare_exist && !from_native && !agent_key_available (grip))
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_EEXIST);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2013-05-22 09:50:12 +01:00
|
|
|
if (unattended && !from_native)
|
2010-09-01 09:48:35 +00:00
|
|
|
{
|
2014-04-28 10:36:16 +09:00
|
|
|
err = prepare_unprotect (pubkey_algo, skey, DIM(skey), s2k_mode,
|
2013-05-22 09:50:12 +01:00
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
2014-05-07 13:16:32 +02:00
|
|
|
err = convert_transfer_key (&s_skey, pubkey_algo, skey, curve, s_pgp);
|
2013-05-22 09:50:12 +01:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
2010-09-01 09:48:35 +00:00
|
|
|
}
|
2013-05-22 09:50:12 +01:00
|
|
|
else
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
2013-05-22 09:50:12 +01:00
|
|
|
struct pin_entry_info_s *pi;
|
|
|
|
struct try_do_unprotect_arg_s pi_arg;
|
|
|
|
|
2015-08-24 16:14:09 +02:00
|
|
|
pi = xtrycalloc_secure (1, sizeof (*pi) + MAX_PASSPHRASE_LEN + 1);
|
2013-05-22 09:50:12 +01:00
|
|
|
if (!pi)
|
|
|
|
return gpg_error_from_syserror ();
|
2015-08-24 16:14:09 +02:00
|
|
|
pi->max_length = MAX_PASSPHRASE_LEN + 1;
|
2013-05-22 09:50:12 +01:00
|
|
|
pi->min_digits = 0; /* We want a real passphrase. */
|
|
|
|
pi->max_digits = 16;
|
|
|
|
pi->max_tries = 3;
|
|
|
|
pi->check_cb = try_do_unprotect_cb;
|
|
|
|
pi->check_cb_arg = &pi_arg;
|
|
|
|
pi_arg.is_v4 = is_v4;
|
|
|
|
pi_arg.is_protected = is_protected;
|
|
|
|
pi_arg.pubkey_algo = pubkey_algo;
|
2014-05-07 13:16:32 +02:00
|
|
|
pi_arg.curve = curve;
|
2013-05-22 09:50:12 +01:00
|
|
|
pi_arg.protect_algo = protect_algo;
|
|
|
|
pi_arg.iv = iv;
|
|
|
|
pi_arg.ivlen = ivlen;
|
|
|
|
pi_arg.s2k_mode = s2k_mode;
|
|
|
|
pi_arg.s2k_algo = s2k_algo;
|
|
|
|
pi_arg.s2k_salt = s2k_salt;
|
|
|
|
pi_arg.s2k_count = s2k_count;
|
|
|
|
pi_arg.desired_csum = desired_csum;
|
|
|
|
pi_arg.skey = skey;
|
|
|
|
pi_arg.skeysize = DIM (skey);
|
|
|
|
pi_arg.skeyidx = skeyidx;
|
|
|
|
pi_arg.r_key = &s_skey;
|
|
|
|
|
|
|
|
err = gpg_error (GPG_ERR_BAD_PASSPHRASE);
|
2014-05-08 10:24:36 +02:00
|
|
|
if (!is_protected)
|
|
|
|
{
|
|
|
|
err = try_do_unprotect_cb (pi);
|
2021-06-04 13:44:05 +09:00
|
|
|
if (gpg_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SECKEY);
|
2014-05-08 10:24:36 +02:00
|
|
|
}
|
|
|
|
else if (cache_nonce)
|
2013-05-22 09:50:12 +01:00
|
|
|
{
|
|
|
|
char *cache_value;
|
|
|
|
|
2018-03-27 08:40:58 +02:00
|
|
|
cache_value = agent_get_cache (ctrl, cache_nonce, CACHE_MODE_NONCE);
|
2013-05-22 09:50:12 +01:00
|
|
|
if (cache_value)
|
|
|
|
{
|
|
|
|
if (strlen (cache_value) < pi->max_length)
|
|
|
|
strcpy (pi->pin, cache_value);
|
|
|
|
xfree (cache_value);
|
|
|
|
}
|
|
|
|
if (*pi->pin)
|
|
|
|
err = try_do_unprotect_cb (pi);
|
|
|
|
}
|
|
|
|
else if (from_native)
|
|
|
|
{
|
|
|
|
if (strlen (passphrase) < pi->max_length)
|
|
|
|
strcpy (pi->pin, passphrase);
|
|
|
|
err = try_do_unprotect_cb (pi);
|
|
|
|
}
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_BAD_PASSPHRASE && !from_native)
|
2015-04-14 18:41:05 +02:00
|
|
|
err = agent_askpin (ctrl, prompt, NULL, NULL, pi, NULL, 0);
|
2013-05-22 09:50:12 +01:00
|
|
|
skeyidx = pi_arg.skeyidx;
|
2014-05-08 10:24:36 +02:00
|
|
|
if (!err && r_passphrase && is_protected)
|
2013-05-22 09:50:12 +01:00
|
|
|
{
|
|
|
|
*r_passphrase = xtrystrdup (pi->pin);
|
|
|
|
if (!*r_passphrase)
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
}
|
|
|
|
xfree (pi);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
2010-08-31 15:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save some memory and get rid of the SKEY array now. */
|
|
|
|
for (idx=0; idx < skeyidx; idx++)
|
|
|
|
gcry_mpi_release (skey[idx]);
|
|
|
|
skeyidx = 0;
|
|
|
|
|
|
|
|
/* Note that the padding is not required - we use it only because
|
2013-05-22 09:50:12 +01:00
|
|
|
that function allows us to create the result in secure memory. */
|
2010-08-31 15:58:39 +00:00
|
|
|
err = make_canon_sexp_pad (s_skey, 1, r_key, NULL);
|
|
|
|
|
|
|
|
leave:
|
2014-05-07 13:16:32 +02:00
|
|
|
xfree (curve);
|
2013-05-22 09:50:12 +01:00
|
|
|
gcry_sexp_release (s_skey);
|
2010-08-31 15:58:39 +00:00
|
|
|
gcry_sexp_release (list);
|
|
|
|
gcry_sexp_release (top_list);
|
|
|
|
for (idx=0; idx < skeyidx; idx++)
|
|
|
|
gcry_mpi_release (skey[idx]);
|
2013-05-22 09:50:12 +01:00
|
|
|
if (err && r_passphrase)
|
2010-08-31 15:58:39 +00:00
|
|
|
{
|
|
|
|
xfree (*r_passphrase);
|
|
|
|
*r_passphrase = NULL;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
|
|
|
|
bad_seckey:
|
|
|
|
err = gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
goto leave;
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-08-31 15:58:39 +00:00
|
|
|
outofmem:
|
|
|
|
err = gpg_error (GPG_ERR_ENOMEM);
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-22 09:50:12 +01:00
|
|
|
/* Convert an OpenPGP transfer key into our internal format. Before
|
|
|
|
asking for a passphrase we check whether the key already exists in
|
|
|
|
our key storage. S_PGP is the OpenPGP key in transfer format. If
|
|
|
|
CACHE_NONCE is given the passphrase will be looked up in the cache.
|
|
|
|
On success R_KEY will receive a canonical encoded S-expression with
|
|
|
|
the unprotected key in our internal format; the caller needs to
|
|
|
|
release that memory. The passphrase used to decrypt the OpenPGP
|
|
|
|
key will be returned at R_PASSPHRASE; the caller must release this
|
|
|
|
passphrase. If R_PASSPHRASE is NULL the unattended conversion mode
|
|
|
|
will be used which uses the openpgp-native protection format for
|
|
|
|
the key. The keygrip will be stored at the 20 byte buffer pointed
|
|
|
|
to by GRIP. On error NULL is stored at all return arguments. */
|
|
|
|
gpg_error_t
|
2015-12-24 14:19:53 +09:00
|
|
|
convert_from_openpgp (ctrl_t ctrl, gcry_sexp_t s_pgp, int dontcare_exist,
|
2013-05-22 09:50:12 +01:00
|
|
|
unsigned char *grip, const char *prompt,
|
|
|
|
const char *cache_nonce,
|
|
|
|
unsigned char **r_key, char **r_passphrase)
|
|
|
|
{
|
2015-12-24 14:19:53 +09:00
|
|
|
return convert_from_openpgp_main (ctrl, s_pgp, dontcare_exist, grip, prompt,
|
2013-05-22 09:50:12 +01:00
|
|
|
cache_nonce, NULL,
|
|
|
|
r_key, r_passphrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is called by agent_unprotect to re-protect an
|
|
|
|
openpgp-native protected private-key into the standard private-key
|
|
|
|
protection format. */
|
|
|
|
gpg_error_t
|
|
|
|
convert_from_openpgp_native (ctrl_t ctrl,
|
|
|
|
gcry_sexp_t s_pgp, const char *passphrase,
|
|
|
|
unsigned char **r_key)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
unsigned char grip[20];
|
|
|
|
|
|
|
|
if (!passphrase)
|
|
|
|
return gpg_error (GPG_ERR_INTERNAL);
|
|
|
|
|
2015-12-24 14:19:53 +09:00
|
|
|
err = convert_from_openpgp_main (ctrl, s_pgp, 0, grip, NULL,
|
2013-05-22 09:50:12 +01:00
|
|
|
NULL, passphrase,
|
|
|
|
r_key, NULL);
|
|
|
|
|
|
|
|
/* On success try to re-write the key. */
|
|
|
|
if (!err)
|
|
|
|
{
|
2015-01-29 16:26:07 +01:00
|
|
|
if (*passphrase)
|
|
|
|
{
|
|
|
|
unsigned char *protectedkey = NULL;
|
|
|
|
size_t protectedkeylen;
|
|
|
|
|
|
|
|
if (!agent_protect (*r_key, passphrase,
|
|
|
|
&protectedkey, &protectedkeylen,
|
2016-04-12 14:37:26 +02:00
|
|
|
ctrl->s2k_count, -1))
|
2022-08-15 12:49:56 +02:00
|
|
|
agent_write_private_key (grip, protectedkey, protectedkeylen,
|
|
|
|
1, 0, NULL, NULL, NULL);
|
2015-01-29 16:26:07 +01:00
|
|
|
xfree (protectedkey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Empty passphrase: write key without protection. */
|
|
|
|
agent_write_private_key (grip,
|
|
|
|
*r_key,
|
|
|
|
gcry_sexp_canon_len (*r_key, 0, NULL,NULL),
|
2022-08-15 12:49:56 +02:00
|
|
|
1, 0, NULL, NULL, NULL);
|
2015-01-29 16:26:07 +01:00
|
|
|
}
|
2013-05-22 09:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-01 20:33:53 +00:00
|
|
|
/* Given an ARRAY of mpis with the key parameters, protect the secret
|
|
|
|
parameters in that array and replace them by one opaque encoded
|
|
|
|
mpi. NPKEY is the number of public key parameters and NSKEY is
|
|
|
|
the number of secret key parameters (including the public ones).
|
|
|
|
On success the array will have NPKEY+1 elements. */
|
|
|
|
static gpg_error_t
|
|
|
|
apply_protection (gcry_mpi_t *array, int npkey, int nskey,
|
|
|
|
const char *passphrase,
|
|
|
|
int protect_algo, void *protect_iv, size_t protect_ivlen,
|
|
|
|
int s2k_mode, int s2k_algo, byte *s2k_salt, u32 s2k_count)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
int i, j;
|
|
|
|
gcry_cipher_hd_t cipherhd;
|
|
|
|
unsigned char *bufarr[10];
|
|
|
|
size_t narr[10];
|
|
|
|
unsigned int nbits[10];
|
|
|
|
int ndata;
|
|
|
|
unsigned char *p, *data;
|
|
|
|
|
|
|
|
assert (npkey < nskey);
|
|
|
|
assert (nskey < DIM (bufarr));
|
|
|
|
|
|
|
|
/* Collect only the secret key parameters into BUFARR et al and
|
|
|
|
compute the required size of the data buffer. */
|
|
|
|
ndata = 20; /* Space for the SHA-1 checksum. */
|
|
|
|
for (i = npkey, j = 0; i < nskey; i++, j++ )
|
|
|
|
{
|
2015-09-19 17:45:17 +09:00
|
|
|
err = gcry_mpi_aprint (GCRYMPI_FMT_USG, bufarr+j, narr+j, array[i]);
|
|
|
|
if (err)
|
2014-09-02 11:22:07 +02:00
|
|
|
{
|
2015-09-19 17:45:17 +09:00
|
|
|
for (i = 0; i < j; i++)
|
|
|
|
xfree (bufarr[i]);
|
|
|
|
return err;
|
2010-10-01 20:33:53 +00:00
|
|
|
}
|
2015-09-19 17:45:17 +09:00
|
|
|
nbits[j] = gcry_mpi_get_nbits (array[i]);
|
2010-10-01 20:33:53 +00:00
|
|
|
ndata += 2 + narr[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate data buffer and stuff it with the secret key parameters. */
|
|
|
|
data = xtrymalloc_secure (ndata);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
for (i = 0; i < (nskey-npkey); i++ )
|
|
|
|
xfree (bufarr[i]);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
p = data;
|
|
|
|
for (i = 0; i < (nskey-npkey); i++ )
|
|
|
|
{
|
|
|
|
*p++ = nbits[i] >> 8 ;
|
|
|
|
*p++ = nbits[i];
|
|
|
|
memcpy (p, bufarr[i], narr[i]);
|
|
|
|
p += narr[i];
|
|
|
|
xfree (bufarr[i]);
|
|
|
|
bufarr[i] = NULL;
|
|
|
|
}
|
|
|
|
assert (p == data + ndata - 20);
|
|
|
|
|
|
|
|
/* Append a hash of the secret key parameters. */
|
|
|
|
gcry_md_hash_buffer (GCRY_MD_SHA1, p, data, ndata - 20);
|
|
|
|
|
|
|
|
/* Encrypt it. */
|
|
|
|
err = gcry_cipher_open (&cipherhd, protect_algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB, GCRY_CIPHER_SECURE);
|
|
|
|
if (!err)
|
|
|
|
err = hash_passphrase_and_set_key (passphrase, cipherhd, protect_algo,
|
|
|
|
s2k_mode, s2k_algo, s2k_salt, s2k_count);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_setiv (cipherhd, protect_iv, protect_ivlen);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_encrypt (cipherhd, data, ndata, NULL, 0);
|
|
|
|
gcry_cipher_close (cipherhd);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
xfree (data);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace the secret key parameters in the array by one opaque value. */
|
|
|
|
for (i = npkey; i < nskey; i++ )
|
|
|
|
{
|
|
|
|
gcry_mpi_release (array[i]);
|
|
|
|
array[i] = NULL;
|
|
|
|
}
|
|
|
|
array[npkey] = gcry_mpi_set_opaque (NULL, data, ndata*8);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 09:30:11 +09:00
|
|
|
/*
|
|
|
|
* Examining S_KEY in S-Expression and extract data.
|
|
|
|
* When REQ_PRIVATE_KEY_DATA == 1, S_KEY's CAR should be 'private-key',
|
|
|
|
* but it also allows shadowed or protected versions.
|
|
|
|
* On success, it returns 0, otherwise error number.
|
|
|
|
* R_ALGONAME is static string which is no need to free by caller.
|
|
|
|
* R_NPKEY is pointer to number of public key data.
|
|
|
|
* R_NSKEY is pointer to number of private key data.
|
|
|
|
* R_ELEMS is static string which is no need to free by caller.
|
|
|
|
* ARRAY contains public and private key data.
|
2015-01-27 10:22:47 +01:00
|
|
|
* ARRAYSIZE is the allocated size of the array for cross-checking.
|
2015-01-27 09:30:11 +09:00
|
|
|
* R_CURVE is pointer to S-Expression of the curve (can be NULL).
|
|
|
|
* R_FLAGS is pointer to S-Expression of the flags (can be NULL).
|
|
|
|
*/
|
2010-10-01 20:33:53 +00:00
|
|
|
gpg_error_t
|
2015-01-27 09:30:11 +09:00
|
|
|
extract_private_key (gcry_sexp_t s_key, int req_private_key_data,
|
|
|
|
const char **r_algoname, int *r_npkey, int *r_nskey,
|
2015-01-27 10:22:47 +01:00
|
|
|
const char **r_elems,
|
|
|
|
gcry_mpi_t *array, int arraysize,
|
2015-01-27 09:30:11 +09:00
|
|
|
gcry_sexp_t *r_curve, gcry_sexp_t *r_flags)
|
2010-10-01 20:33:53 +00:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_sexp_t list, l2;
|
|
|
|
char *name;
|
2015-01-27 09:30:11 +09:00
|
|
|
const char *algoname, *format;
|
2010-10-01 20:33:53 +00:00
|
|
|
int npkey, nskey;
|
2014-06-20 14:54:01 +02:00
|
|
|
gcry_sexp_t curve = NULL;
|
2015-01-27 09:30:11 +09:00
|
|
|
gcry_sexp_t flags = NULL;
|
2010-10-01 20:33:53 +00:00
|
|
|
|
2015-01-27 10:22:47 +01:00
|
|
|
*r_curve = NULL;
|
|
|
|
*r_flags = NULL;
|
|
|
|
|
2015-01-27 09:30:11 +09:00
|
|
|
if (!req_private_key_data)
|
|
|
|
{
|
|
|
|
list = gcry_sexp_find_token (s_key, "shadowed-private-key", 0 );
|
|
|
|
if (!list)
|
|
|
|
list = gcry_sexp_find_token (s_key, "protected-private-key", 0 );
|
|
|
|
if (!list)
|
|
|
|
list = gcry_sexp_find_token (s_key, "private-key", 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
list = gcry_sexp_find_token (s_key, "private-key", 0);
|
2010-10-01 20:33:53 +00:00
|
|
|
|
|
|
|
if (!list)
|
2015-01-27 09:30:11 +09:00
|
|
|
{
|
|
|
|
log_error ("invalid private key format\n");
|
|
|
|
return gpg_error (GPG_ERR_BAD_SECKEY);
|
|
|
|
}
|
|
|
|
|
2010-10-01 20:33:53 +00:00
|
|
|
l2 = gcry_sexp_cadr (list);
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = l2;
|
|
|
|
name = gcry_sexp_nth_string (list, 0);
|
|
|
|
if (!name)
|
|
|
|
{
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
return gpg_error (GPG_ERR_INV_OBJ); /* Invalid structure of object. */
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2015-01-27 10:22:47 +01:00
|
|
|
if (arraysize < 7)
|
|
|
|
BUG ();
|
|
|
|
|
2014-03-22 20:51:16 +01:00
|
|
|
/* Map NAME to a name as used by Libgcrypt. We do not use the
|
|
|
|
Libgcrypt function here because we need a lowercase name and
|
|
|
|
require special treatment for some algorithms. */
|
|
|
|
strlwr (name);
|
|
|
|
if (!strcmp (name, "rsa"))
|
|
|
|
{
|
|
|
|
algoname = "rsa";
|
2015-01-27 09:30:11 +09:00
|
|
|
format = "ned?p?q?u?";
|
2014-03-22 20:51:16 +01:00
|
|
|
npkey = 2;
|
2014-04-15 16:40:48 +02:00
|
|
|
nskey = 6;
|
2015-01-27 09:30:11 +09:00
|
|
|
err = gcry_sexp_extract_param (list, NULL, format,
|
2014-04-15 16:40:48 +02:00
|
|
|
array+0, array+1, array+2, array+3,
|
|
|
|
array+4, array+5, NULL);
|
2014-03-22 20:51:16 +01:00
|
|
|
}
|
|
|
|
else if (!strcmp (name, "elg"))
|
|
|
|
{
|
|
|
|
algoname = "elg";
|
2015-01-27 09:30:11 +09:00
|
|
|
format = "pgyx?";
|
2014-03-22 20:51:16 +01:00
|
|
|
npkey = 3;
|
2014-04-15 16:40:48 +02:00
|
|
|
nskey = 4;
|
2015-01-27 09:30:11 +09:00
|
|
|
err = gcry_sexp_extract_param (list, NULL, format,
|
2014-04-15 16:40:48 +02:00
|
|
|
array+0, array+1, array+2, array+3,
|
|
|
|
NULL);
|
2014-03-22 20:51:16 +01:00
|
|
|
}
|
|
|
|
else if (!strcmp (name, "dsa"))
|
|
|
|
{
|
|
|
|
algoname = "dsa";
|
2015-01-27 09:30:11 +09:00
|
|
|
format = "pqgyx?";
|
2014-03-22 20:51:16 +01:00
|
|
|
npkey = 4;
|
2014-04-15 16:40:48 +02:00
|
|
|
nskey = 5;
|
2015-01-27 09:30:11 +09:00
|
|
|
err = gcry_sexp_extract_param (list, NULL, format,
|
2014-04-15 16:40:48 +02:00
|
|
|
array+0, array+1, array+2, array+3,
|
|
|
|
array+4, NULL);
|
2014-03-22 20:51:16 +01:00
|
|
|
}
|
2016-08-09 11:42:20 +09:00
|
|
|
else if (!strcmp (name, "ecc") || !strcmp (name, "ecdsa"))
|
2014-03-22 20:51:16 +01:00
|
|
|
{
|
2015-01-27 09:30:11 +09:00
|
|
|
algoname = "ecc";
|
2015-09-19 17:45:17 +09:00
|
|
|
format = "qd?";
|
2014-06-20 14:54:01 +02:00
|
|
|
npkey = 1;
|
|
|
|
nskey = 2;
|
2015-01-27 09:30:11 +09:00
|
|
|
curve = gcry_sexp_find_token (list, "curve", 0);
|
|
|
|
flags = gcry_sexp_find_token (list, "flags", 0);
|
|
|
|
err = gcry_sexp_extract_param (list, NULL, format,
|
|
|
|
array+0, array+1, NULL);
|
2010-10-01 20:33:53 +00:00
|
|
|
}
|
2014-03-22 20:51:16 +01:00
|
|
|
else
|
|
|
|
{
|
2014-04-15 16:40:48 +02:00
|
|
|
err = gpg_error (GPG_ERR_PUBKEY_ALGO);
|
2014-03-22 20:51:16 +01:00
|
|
|
}
|
|
|
|
xfree (name);
|
2015-01-27 09:30:11 +09:00
|
|
|
gcry_sexp_release (list);
|
2010-10-01 20:33:53 +00:00
|
|
|
if (err)
|
2014-06-20 14:54:01 +02:00
|
|
|
{
|
|
|
|
gcry_sexp_release (curve);
|
2015-01-27 09:30:11 +09:00
|
|
|
gcry_sexp_release (flags);
|
2014-06-20 14:54:01 +02:00
|
|
|
return err;
|
|
|
|
}
|
2015-01-27 09:30:11 +09:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*r_algoname = algoname;
|
|
|
|
if (r_elems)
|
2015-09-19 17:45:17 +09:00
|
|
|
*r_elems = format;
|
2015-01-27 09:30:11 +09:00
|
|
|
*r_npkey = npkey;
|
|
|
|
if (r_nskey)
|
|
|
|
*r_nskey = nskey;
|
|
|
|
*r_curve = curve;
|
|
|
|
*r_flags = flags;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert our key S_KEY into an OpenPGP key transfer format. On
|
|
|
|
success a canonical encoded S-expression is stored at R_TRANSFERKEY
|
|
|
|
and its length at R_TRANSFERKEYLEN; this S-expression is also
|
|
|
|
padded to a multiple of 64 bits. */
|
|
|
|
gpg_error_t
|
|
|
|
convert_to_openpgp (ctrl_t ctrl, gcry_sexp_t s_key, const char *passphrase,
|
|
|
|
unsigned char **r_transferkey, size_t *r_transferkeylen)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
const char *algoname;
|
|
|
|
int npkey, nskey;
|
|
|
|
gcry_mpi_t array[10];
|
|
|
|
gcry_sexp_t curve = NULL;
|
|
|
|
gcry_sexp_t flags = NULL;
|
|
|
|
char protect_iv[16];
|
|
|
|
char salt[8];
|
|
|
|
unsigned long s2k_count;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
(void)ctrl;
|
|
|
|
|
|
|
|
*r_transferkey = NULL;
|
|
|
|
|
|
|
|
for (i=0; i < DIM (array); i++)
|
|
|
|
array[i] = NULL;
|
|
|
|
|
|
|
|
err = extract_private_key (s_key, 1, &algoname, &npkey, &nskey, NULL,
|
2015-01-27 10:22:47 +01:00
|
|
|
array, DIM (array), &curve, &flags);
|
2015-01-27 09:30:11 +09:00
|
|
|
if (err)
|
|
|
|
return err;
|
2010-10-01 20:33:53 +00:00
|
|
|
|
|
|
|
gcry_create_nonce (protect_iv, sizeof protect_iv);
|
|
|
|
gcry_create_nonce (salt, sizeof salt);
|
2011-04-26 20:33:46 +02:00
|
|
|
/* We need to use the encoded S2k count. It is not possible to
|
|
|
|
encode it after it has been used because the encoding procedure
|
|
|
|
may round the value up. */
|
|
|
|
s2k_count = get_standard_s2k_count_rfc4880 ();
|
2010-10-01 20:33:53 +00:00
|
|
|
err = apply_protection (array, npkey, nskey, passphrase,
|
|
|
|
GCRY_CIPHER_AES, protect_iv, sizeof protect_iv,
|
|
|
|
3, GCRY_MD_SHA1, salt, s2k_count);
|
|
|
|
/* Turn it into the transfer key S-expression. Note that we always
|
|
|
|
return a protected key. */
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
char countbuf[35];
|
|
|
|
membuf_t mbuf;
|
|
|
|
void *format_args[10+2];
|
2014-05-07 13:16:32 +02:00
|
|
|
gcry_sexp_t tmpkey;
|
|
|
|
gcry_sexp_t tmpsexp = NULL;
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-10-01 20:33:53 +00:00
|
|
|
snprintf (countbuf, sizeof countbuf, "%lu", s2k_count);
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2010-10-01 20:33:53 +00:00
|
|
|
init_membuf (&mbuf, 50);
|
|
|
|
put_membuf_str (&mbuf, "(skey");
|
|
|
|
for (i=j=0; i < npkey; i++)
|
|
|
|
{
|
|
|
|
put_membuf_str (&mbuf, " _ %m");
|
|
|
|
format_args[j++] = array + i;
|
|
|
|
}
|
2014-05-07 13:16:32 +02:00
|
|
|
put_membuf_str (&mbuf, " e %m");
|
|
|
|
format_args[j++] = array + npkey;
|
2010-10-01 20:33:53 +00:00
|
|
|
put_membuf_str (&mbuf, ")\n");
|
|
|
|
put_membuf (&mbuf, "", 1);
|
|
|
|
|
|
|
|
tmpkey = NULL;
|
|
|
|
{
|
|
|
|
char *format = get_membuf (&mbuf, NULL);
|
|
|
|
if (!format)
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
else
|
|
|
|
err = gcry_sexp_build_array (&tmpkey, NULL, format, format_args);
|
|
|
|
xfree (format);
|
|
|
|
}
|
|
|
|
if (!err)
|
|
|
|
err = gcry_sexp_build (&tmpsexp, NULL,
|
|
|
|
"(openpgp-private-key\n"
|
|
|
|
" (version 1:4)\n"
|
|
|
|
" (algo %s)\n"
|
2014-06-20 14:54:01 +02:00
|
|
|
" %S%S\n"
|
2010-10-01 20:33:53 +00:00
|
|
|
" (protection sha1 aes %b 1:3 sha1 %b %s))\n",
|
|
|
|
algoname,
|
2014-06-20 14:54:01 +02:00
|
|
|
curve,
|
2011-02-02 15:48:54 +01:00
|
|
|
tmpkey,
|
2010-10-01 20:33:53 +00:00
|
|
|
(int)sizeof protect_iv, protect_iv,
|
|
|
|
(int)sizeof salt, salt,
|
|
|
|
countbuf);
|
|
|
|
gcry_sexp_release (tmpkey);
|
|
|
|
if (!err)
|
|
|
|
err = make_canon_sexp_pad (tmpsexp, 0, r_transferkey, r_transferkeylen);
|
|
|
|
gcry_sexp_release (tmpsexp);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i < DIM (array); i++)
|
|
|
|
gcry_mpi_release (array[i]);
|
2014-06-20 14:54:01 +02:00
|
|
|
gcry_sexp_release (curve);
|
2015-01-27 09:30:11 +09:00
|
|
|
gcry_sexp_release (flags);
|
2011-01-05 17:33:17 -08:00
|
|
|
|
2010-10-01 20:33:53 +00:00
|
|
|
return err;
|
|
|
|
}
|