1
/* seckey-cert.c - secret key certificate packet handling
2
* Copyright (C) 1998, 1999, 2000, 2001, 2002,
3
* 2003 Free Software Foundation, Inc.
5
* This file is part of GnuPG.
7
* GnuPG is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* GnuPG is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
42
do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
51
if( sk->is_protected ) { /* remove the protection */
53
u32 keyid[4]; /* 4! because we need two of them */
54
CIPHER_HANDLE cipher_hd=NULL;
55
PKT_secret_key *save_sk;
57
if( sk->protect.s2k.mode == 1001 ) {
58
log_info(_("secret key parts are not available\n"));
59
return GPG_ERR_GENERAL;
61
if( sk->protect.algo == CIPHER_ALGO_NONE )
63
if( openpgp_cipher_test_algo( sk->protect.algo ) ) {
64
log_info(_("protection algorithm %d%s is not supported\n"),
65
sk->protect.algo,sk->protect.algo==1?" (IDEA)":"" );
66
if (sk->protect.algo==CIPHER_ALGO_IDEA)
68
write_status (STATUS_RSA_OR_IDEA);
71
return GPG_ERR_CIPHER_ALGO;
73
keyid_from_sk( sk, keyid );
74
keyid[2] = keyid[3] = 0;
75
if( !sk->is_primary ) {
76
keyid[2] = sk->main_keyid[0];
77
keyid[3] = sk->main_keyid[1];
79
dek = passphrase_to_dek( keyid, sk->pubkey_algo, sk->protect.algo,
80
&sk->protect.s2k, mode,
81
tryagain_text, canceled );
82
if (!dek && canceled && *canceled)
83
return GPG_ERR_GENERAL;
85
rc = gcry_cipher_open (&cipher_hd, sk->protect.algo,
88
| (sk->protect.algo >= 100 ?
89
0 : GCRY_CIPHER_ENABLE_SYNC));
91
log_fatal ("cipher open failed: %s\n", gpg_strerror (rc) );
93
rc = gcry_cipher_setkey (cipher_hd, dek->key, dek->keylen);
95
log_fatal ("set key failed: %s\n", gpg_strerror (rc) );
98
save_sk = copy_secret_key( NULL, sk );
99
gcry_cipher_setiv (cipher_hd, sk->protect.iv, sk->protect.ivlen);
101
if( sk->version >= 4 ) {
103
unsigned int ndatabits;
107
i = pubkey_get_npkey(sk->pubkey_algo);
108
assert( gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE ));
109
p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits );
110
ndata = (ndatabits+7)/8;
112
csumc = p[ndata-2] << 8 | p[ndata-1];
113
data = gcry_xmalloc_secure ( ndata );
114
gcry_cipher_decrypt( cipher_hd, data, ndata, p, ndata );
115
gcry_mpi_release ( sk->skey[i] ); sk->skey[i] = NULL ;
117
if (sk->protect.sha1chk) {
118
/* This is the new SHA1 checksum method to detect
119
tampering with the key as used by the Klima/Rosa
124
log_error("not enough bytes for SHA-1 checksum\n");
128
if ( gcry_md_open (&h, DIGEST_ALGO_SHA1, 1))
129
BUG(); /* algo not available */
130
gcry_md_write (h, data, ndata - 20);
132
if (!memcmp (gcry_md_read (h, DIGEST_ALGO_SHA1),
133
data + ndata - 20, 20) ) {
134
/* digest does match. We have to keep the old
135
style checksum in sk->csum, so that the
136
test used for unprotected keys does work.
137
This test gets used when we are adding new
139
sk->csum = csum = checksum (data, ndata-20);
146
log_error("not enough bytes for checksum\n");
151
csum = checksum( data, ndata-2);
152
sk->csum = data[ndata-2] << 8 | data[ndata-1];
153
if ( sk->csum != csum ) {
154
/* This is a PGP 7.0.0 workaround */
155
sk->csum = csumc; /* take the encrypted one */
160
/* must check it here otherwise the mpi_read_xx would fail
161
because the length may have an arbitrary value */
162
if( sk->csum == csum ) {
163
for( ; i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
164
assert( gcry_is_secure( p ) );
165
res = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_PGP,
168
log_bug ("gcry_mpi_scan failed in do_check: %s\n",
173
/* Note: at this point ndata should be 2 for a simple
174
checksum or 20 for the sha1 digest */
179
for(i=pubkey_get_npkey(sk->pubkey_algo);
180
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
183
unsigned int ndatabits;
185
assert( gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE));
186
p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits );
187
ndata = (ndatabits+7)/8;
189
assert (ndata == ((p[0] << 8 | p[1]) + 7)/8 + 2);
190
buffer = gcry_xmalloc_secure (ndata);
191
gcry_cipher_sync (cipher_hd);
194
gcry_cipher_decrypt (cipher_hd, buffer+2, ndata-2,
196
csum += checksum (buffer, ndata);
197
gcry_mpi_release (sk->skey[i]);
198
res = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_USG,
199
buffer, ndata, &ndata );
201
log_bug ("gcry_mpi_scan failed in do_check: %s\n",
204
assert (sk->skey[i]);
206
/* csum += checksum_mpi (sk->skey[i]); */
209
gcry_cipher_close (cipher_hd);
210
/* now let's see whether we have used the right passphrase */
211
if( csum != sk->csum ) {
212
copy_secret_key( sk, save_sk );
213
passphrase_clear_cache ( keyid, sk->pubkey_algo );
214
free_secret_key( save_sk );
215
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
217
/* the checksum may fail, so we also check the key itself */
218
res = pk_check_secret_key (sk->pubkey_algo, sk->skey);
220
copy_secret_key( sk, save_sk );
221
passphrase_clear_cache ( keyid, sk->pubkey_algo );
222
free_secret_key( save_sk );
223
return gpg_error (GPG_ERR_BAD_PASSPHRASE);
225
free_secret_key( save_sk );
226
sk->is_protected = 0;
228
else { /* not protected, assume it is okay if the checksum is okay */
230
for(i=pubkey_get_npkey(sk->pubkey_algo);
231
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
232
csum += checksum_mpi( sk->skey[i] );
234
if( csum != sk->csum )
235
return GPG_ERR_CHECKSUM;
244
* Check the secret key
245
* Ask up to 3 (or n) times for a correct passphrase
246
* If n is negative, disable the key info prompt and make n=abs(n)
249
check_secret_key( PKT_secret_key *sk, int n )
251
int rc = gpg_error (GPG_ERR_BAD_PASSPHRASE);
254
if (sk && sk->is_protected && sk->protect.s2k.mode == 1002)
255
return 0; /* Let the scdaemon handle it. */
266
n = (opt.batch && !opt.use_agent)? 1 : 3; /* use the default value */
268
for(i=0; i < n && gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE; i++ ) {
270
const char *tryagain = NULL;
272
tryagain = N_("Invalid passphrase; please try again");
273
log_info (_("%s ...\n"), _(tryagain));
275
rc = do_check( sk, tryagain, mode, &canceled );
276
if( gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
277
&& is_status_enabled() ) {
281
keyid_from_sk( sk, kid );
282
sprintf(buf, "%08lX%08lX", (ulong)kid[0], (ulong)kid[1]);
283
write_status_text( STATUS_BAD_PASSPHRASE, buf );
285
if( have_static_passphrase() || canceled)
290
write_status( STATUS_GOOD_PASSPHRASE );
296
* check whether the secret key is protected.
297
* Returns: 0 not protected, -1 on error or the protection algorithm
298
* -2 indicates a card stub.
301
is_secret_key_protected( PKT_secret_key *sk )
303
return sk->is_protected?
304
sk->protect.s2k.mode == 1002? -2
305
: sk->protect.algo : 0;
311
* Protect the secret key with the passphrase from DEK
314
protect_secret_key( PKT_secret_key *sk, DEK *dek )
324
if( !sk->is_protected ) { /* okay, apply the protection */
325
gcry_cipher_hd_t cipher_hd=NULL;
327
if( openpgp_cipher_test_algo( sk->protect.algo ) )
329
rc = gpg_error (GPG_ERR_CIPHER_ALGO); /* unsupport
334
print_cipher_algo_note( sk->protect.algo );
335
rc = gcry_cipher_open (&cipher_hd, sk->protect.algo,
336
GCRY_CIPHER_MODE_CFB,
338
| (sk->protect.algo >= 100 ?
339
0 : GCRY_CIPHER_ENABLE_SYNC) );
342
if( gcry_cipher_setkey( cipher_hd, dek->key, dek->keylen ) )
343
log_info(_("WARNING: Weak key detected"
344
" - please change passphrase again.\n"));
345
sk->protect.ivlen = gcry_cipher_get_algo_blklen(sk->protect.algo);
346
assert( sk->protect.ivlen <= DIM(sk->protect.iv) );
347
if( sk->protect.ivlen != 8 && sk->protect.ivlen != 16 )
348
BUG(); /* yes, we are very careful */
349
gcry_create_nonce (sk->protect.iv, sk->protect.ivlen);
350
gcry_cipher_setiv( cipher_hd, sk->protect.iv, sk->protect.ivlen );
351
if( sk->version >= 4 ) {
352
unsigned char *bufarr[PUBKEY_MAX_NSKEY];
353
unsigned narr[PUBKEY_MAX_NSKEY];
354
unsigned nbits[PUBKEY_MAX_NSKEY];
358
for(j=0, i = pubkey_get_npkey(sk->pubkey_algo);
359
i < pubkey_get_nskey(sk->pubkey_algo); i++, j++ ) {
360
assert( !gcry_mpi_get_flag( sk->skey[i],
361
GCRYMPI_FLAG_OPAQUE ));
363
if( gcry_mpi_aprint( GCRYMPI_FMT_USG, bufarr+j,
364
narr+j, sk->skey[i]))
367
nbits[j] = gcry_mpi_get_nbits( sk->skey[i] );
368
ndata += narr[j] + 2;
370
for( ; j < PUBKEY_MAX_NSKEY; j++ )
372
ndata += opt.simple_sk_checksum? 2 : 20; /* for checksum */
374
data = xmalloc_secure ( ndata );
376
for(j=0; j < PUBKEY_MAX_NSKEY && bufarr[j]; j++ ) {
377
p[0] = nbits[j] >> 8 ;
380
memcpy(p, bufarr[j], narr[j] );
385
if (opt.simple_sk_checksum) {
386
log_info (_("generating the deprecated 16-bit checksum"
387
" for secret key protection\n"));
388
csum = checksum( data, ndata-2);
392
sk->protect.sha1chk = 0;
397
if (gcry_md_open (&h, GCRY_MD_SHA1, 1))
398
BUG(); /* algo not available */
399
gcry_md_write (h, data, ndata - 20);
401
memcpy (p, gcry_md_read (h, GCRY_MD_SHA1), 20);
405
sk->protect.sha1chk = 1;
407
assert( p == data+ndata );
409
gcry_cipher_encrypt( cipher_hd, data, ndata, NULL, 0 );
410
for(i = pubkey_get_npkey(sk->pubkey_algo);
411
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
412
gcry_mpi_release ( sk->skey[i] );
415
i = pubkey_get_npkey(sk->pubkey_algo);
416
sk->skey[i] = gcry_mpi_set_opaque(NULL, data, ndata*8);
420
for(i=pubkey_get_npkey(sk->pubkey_algo);
421
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
425
csum += checksum_mpi (sk->skey[i]);
426
if( gcry_mpi_aprint( GCRYMPI_FMT_USG, &buffer,
427
&nbytes, sk->skey[i] ) )
429
gcry_cipher_sync (cipher_hd);
430
assert (!gcry_mpi_get_flag( sk->skey[i],
431
GCRYMPI_FLAG_OPAQUE ));
432
data = xmalloc (nbytes+2);
433
nbits = gcry_mpi_get_nbits (sk->skey[i]);
434
assert (nbytes == (nbits + 7)/8);
435
data[0] = nbits >> 8;
437
gcry_cipher_encrypt (cipher_hd, data+2, nbytes,
441
gcry_mpi_release (sk->skey[i]);
442
sk->skey[i] = gcry_mpi_set_opaque (NULL, data,
447
sk->is_protected = 1;
448
gcry_cipher_close( cipher_hd );