2010-10-21 12:25:03 +02:00
|
|
|
/* seckey-cert.c - Not anymore used
|
2003-06-18 21:56:13 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002,
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
* 2006, 2009 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +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-06-05 09:14:21 +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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
2010-10-21 12:25:03 +02:00
|
|
|
#error Not anymore used - only kept for reference in the repository.
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-06-18 21:56:13 +02:00
|
|
|
|
|
|
|
#include "gpg.h"
|
2017-03-07 12:32:09 +01:00
|
|
|
#include "../common/util.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "packet.h"
|
|
|
|
#include "keydb.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "options.h"
|
2017-03-07 12:32:09 +01:00
|
|
|
#include "../common/i18n.h"
|
|
|
|
#include "../common/status.h"
|
2003-06-18 21:56:13 +02:00
|
|
|
#include "pkglue.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
static int
|
2010-10-21 12:25:03 +02:00
|
|
|
xxxx_do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
|
|
|
|
int *canceled )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
gpg_error_t err;
|
2003-06-05 09:14:21 +02:00
|
|
|
byte *buffer;
|
|
|
|
u16 csum=0;
|
|
|
|
int i, res;
|
2006-11-21 12:00:14 +01:00
|
|
|
size_t nbytes;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if( sk->is_protected ) { /* remove the protection */
|
|
|
|
DEK *dek = NULL;
|
|
|
|
u32 keyid[4]; /* 4! because we need two of them */
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_cipher_hd_t cipher_hd=NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
PKT_secret_key *save_sk;
|
|
|
|
|
|
|
|
if( sk->protect.s2k.mode == 1001 ) {
|
|
|
|
log_info(_("secret key parts are not available\n"));
|
2015-01-22 12:06:11 +01:00
|
|
|
return GPG_ERR_UNUSABLE_SECKEY;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
if( sk->protect.algo == CIPHER_ALGO_NONE )
|
|
|
|
BUG();
|
2003-06-18 21:56:13 +02:00
|
|
|
if( openpgp_cipher_test_algo( sk->protect.algo ) ) {
|
2003-06-05 09:14:21 +02:00
|
|
|
log_info(_("protection algorithm %d%s is not supported\n"),
|
|
|
|
sk->protect.algo,sk->protect.algo==1?" (IDEA)":"" );
|
2015-01-22 12:06:11 +01:00
|
|
|
return GPG_ERR_CIPHER_ALGO;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
if(gcry_md_test_algo (sk->protect.s2k.hash_algo))
|
|
|
|
{
|
|
|
|
log_info(_("protection digest %d is not supported\n"),
|
|
|
|
sk->protect.s2k.hash_algo);
|
2015-01-22 12:06:11 +01:00
|
|
|
return GPG_ERR_DIGEST_ALGO;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
keyid_from_sk( sk, keyid );
|
|
|
|
keyid[2] = keyid[3] = 0;
|
2010-10-20 13:33:50 +02:00
|
|
|
if (!sk->flags.primary)
|
|
|
|
{
|
2003-06-05 09:14:21 +02:00
|
|
|
keyid[2] = sk->main_keyid[0];
|
|
|
|
keyid[3] = sk->main_keyid[1];
|
2010-10-20 13:33:50 +02:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
dek = passphrase_to_dek( keyid, sk->pubkey_algo, sk->protect.algo,
|
|
|
|
&sk->protect.s2k, mode,
|
|
|
|
tryagain_text, canceled );
|
|
|
|
if (!dek && canceled && *canceled)
|
2010-03-15 12:15:45 +01:00
|
|
|
return GPG_ERR_CANCELED;
|
2003-06-18 21:56:13 +02:00
|
|
|
|
|
|
|
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
err = openpgp_cipher_open (&cipher_hd, sk->protect.algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB,
|
|
|
|
(GCRY_CIPHER_SECURE
|
|
|
|
| (sk->protect.algo >= 100 ?
|
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC)));
|
2006-04-19 13:26:11 +02:00
|
|
|
if (err)
|
|
|
|
log_fatal ("cipher open failed: %s\n", gpg_strerror (err) );
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
err = gcry_cipher_setkey (cipher_hd, dek->key, dek->keylen);
|
|
|
|
if (err)
|
|
|
|
log_fatal ("set key failed: %s\n", gpg_strerror (err) );
|
|
|
|
|
|
|
|
xfree(dek);
|
2003-06-05 09:14:21 +02:00
|
|
|
save_sk = copy_secret_key( NULL, sk );
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
gcry_cipher_setiv ( cipher_hd, sk->protect.iv, sk->protect.ivlen );
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
csum = 0;
|
|
|
|
if( sk->version >= 4 ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
int ndata;
|
|
|
|
unsigned int ndatabits;
|
2003-06-05 09:14:21 +02:00
|
|
|
byte *p, *data;
|
|
|
|
u16 csumc = 0;
|
|
|
|
|
|
|
|
i = pubkey_get_npkey(sk->pubkey_algo);
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
assert ( gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE ));
|
|
|
|
p = gcry_mpi_get_opaque ( sk->skey[i], &ndatabits );
|
2003-06-18 21:56:13 +02:00
|
|
|
ndata = (ndatabits+7)/8;
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if ( ndata > 1 )
|
2015-02-11 10:27:57 +01:00
|
|
|
csumc = buf16_to_u16 (p+ndata-2);
|
2006-04-19 13:26:11 +02:00
|
|
|
data = xmalloc_secure ( ndata );
|
|
|
|
gcry_cipher_decrypt ( cipher_hd, data, ndata, p, ndata );
|
|
|
|
gcry_mpi_release (sk->skey[i]); sk->skey[i] = NULL ;
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
p = data;
|
|
|
|
if (sk->protect.sha1chk) {
|
|
|
|
/* This is the new SHA1 checksum method to detect
|
|
|
|
tampering with the key as used by the Klima/Rosa
|
|
|
|
attack */
|
|
|
|
sk->csum = 0;
|
|
|
|
csum = 1;
|
2011-02-04 12:57:53 +01:00
|
|
|
if( ndata < 20 )
|
2003-06-05 09:14:21 +02:00
|
|
|
log_error("not enough bytes for SHA-1 checksum\n");
|
|
|
|
else {
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_hd_t h;
|
|
|
|
|
|
|
|
if ( gcry_md_open (&h, DIGEST_ALGO_SHA1, 1))
|
2006-04-19 13:26:11 +02:00
|
|
|
BUG(); /* Algo not available. */
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_write (h, data, ndata - 20);
|
|
|
|
gcry_md_final (h);
|
|
|
|
if (!memcmp (gcry_md_read (h, DIGEST_ALGO_SHA1),
|
2011-02-04 12:57:53 +01:00
|
|
|
data + ndata - 20, 20) )
|
2006-04-19 13:26:11 +02:00
|
|
|
{
|
|
|
|
/* Digest does match. We have to keep the old
|
2003-06-05 09:14:21 +02:00
|
|
|
style checksum in sk->csum, so that the
|
|
|
|
test used for unprotected keys does work.
|
|
|
|
This test gets used when we are adding new
|
|
|
|
keys. */
|
|
|
|
sk->csum = csum = checksum (data, ndata-20);
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_md_close (h);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if( ndata < 2 ) {
|
|
|
|
log_error("not enough bytes for checksum\n");
|
|
|
|
sk->csum = 0;
|
|
|
|
csum = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
csum = checksum( data, ndata-2);
|
|
|
|
sk->csum = data[ndata-2] << 8 | data[ndata-1];
|
|
|
|
if ( sk->csum != csum ) {
|
|
|
|
/* This is a PGP 7.0.0 workaround */
|
|
|
|
sk->csum = csumc; /* take the encrypted one */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
/* Must check it here otherwise the mpi_read_xx would fail
|
2003-06-05 09:14:21 +02:00
|
|
|
because the length may have an arbitrary value */
|
|
|
|
if( sk->csum == csum ) {
|
|
|
|
for( ; i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
if ( gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_PGP,
|
|
|
|
p, ndata, &nbytes))
|
|
|
|
{
|
|
|
|
/* Checksum was okay, but not correctly
|
|
|
|
decrypted. */
|
|
|
|
sk->csum = 0;
|
|
|
|
csum = 1;
|
|
|
|
break;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
ndata -= nbytes;
|
|
|
|
p += nbytes;
|
|
|
|
}
|
|
|
|
/* Note: at this point ndata should be 2 for a simple
|
|
|
|
checksum or 20 for the sha1 digest */
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(data);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(i=pubkey_get_npkey(sk->pubkey_algo);
|
|
|
|
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
|
|
|
|
byte *p;
|
2006-11-21 12:00:14 +01:00
|
|
|
size_t ndata;
|
2003-06-18 21:56:13 +02:00
|
|
|
unsigned int ndatabits;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
assert (gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE));
|
|
|
|
p = gcry_mpi_get_opaque (sk->skey[i], &ndatabits);
|
2003-06-18 21:56:13 +02:00
|
|
|
ndata = (ndatabits+7)/8;
|
2003-06-05 09:14:21 +02:00
|
|
|
assert (ndata >= 2);
|
|
|
|
assert (ndata == ((p[0] << 8 | p[1]) + 7)/8 + 2);
|
2006-04-19 13:26:11 +02:00
|
|
|
buffer = xmalloc_secure (ndata);
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_cipher_sync (cipher_hd);
|
2003-06-05 09:14:21 +02:00
|
|
|
buffer[0] = p[0];
|
|
|
|
buffer[1] = p[1];
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_cipher_decrypt (cipher_hd, buffer+2, ndata-2,
|
|
|
|
p+2, ndata-2);
|
2003-06-05 09:14:21 +02:00
|
|
|
csum += checksum (buffer, ndata);
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_mpi_release (sk->skey[i]);
|
|
|
|
|
2008-03-18 18:09:45 +01:00
|
|
|
err = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_PGP,
|
2006-04-19 13:26:11 +02:00
|
|
|
buffer, ndata, &ndata );
|
2003-06-18 21:56:13 +02:00
|
|
|
xfree (buffer);
|
2006-04-19 13:26:11 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
/* Checksum was okay, but not correctly
|
|
|
|
decrypted. */
|
|
|
|
sk->csum = 0;
|
|
|
|
csum = 1;
|
|
|
|
break;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
/* csum += checksum_mpi (sk->skey[i]); */
|
|
|
|
}
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_cipher_close ( cipher_hd );
|
|
|
|
|
|
|
|
/* Now let's see whether we have used the correct passphrase. */
|
2003-06-05 09:14:21 +02:00
|
|
|
if( csum != sk->csum ) {
|
|
|
|
copy_secret_key( sk, save_sk );
|
2006-04-19 13:26:11 +02:00
|
|
|
passphrase_clear_cache ( keyid, NULL, sk->pubkey_algo );
|
|
|
|
free_secret_key( save_sk );
|
2006-04-19 15:24:36 +02:00
|
|
|
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The checksum may fail, so we also check the key itself. */
|
|
|
|
res = pk_check_secret_key ( sk->pubkey_algo, sk->skey );
|
|
|
|
if( res ) {
|
|
|
|
copy_secret_key( sk, save_sk );
|
|
|
|
passphrase_clear_cache ( keyid, NULL, sk->pubkey_algo );
|
2003-06-05 09:14:21 +02:00
|
|
|
free_secret_key( save_sk );
|
2006-04-19 15:24:36 +02:00
|
|
|
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
free_secret_key( save_sk );
|
|
|
|
sk->is_protected = 0;
|
|
|
|
}
|
|
|
|
else { /* not protected, assume it is okay if the checksum is okay */
|
|
|
|
csum = 0;
|
|
|
|
for(i=pubkey_get_npkey(sk->pubkey_algo);
|
|
|
|
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
|
|
|
|
csum += checksum_mpi( sk->skey[i] );
|
|
|
|
}
|
|
|
|
if( csum != sk->csum )
|
2015-01-22 12:06:11 +01:00
|
|
|
return GPG_ERR_CHECKSUM;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|