97
95
free_secret_key( sk );
98
sk = xcalloc (1, sizeof *sk );
96
sk = xmalloc_clear( sizeof *sk );
99
97
rc=enum_secret_keys( &enum_context, sk, 1, 0);
101
rc = GPG_ERR_NO_SECKEY;
99
rc = G10ERR_NO_SECKEY;
104
102
if( sk->pubkey_algo != k->pubkey_algo )
106
104
keyid_from_sk( sk, keyid );
107
log_info(_("anonymous recipient; trying secret key %08lX ...\n"),
105
log_info(_("anonymous recipient; trying secret key %s ...\n"),
110
108
if(!opt.try_all_secrets && !is_status_enabled())
112
110
p=get_last_passphrase();
113
111
set_next_passphrase(p);
117
115
rc = check_secret_key( sk, opt.try_all_secrets?1:-1 ); /* ask
121
120
rc = get_it( k, dek, sk, keyid );
121
/* Successfully checked the secret key (either it was
122
a card, had no passphrase, or had the right
123
passphrase) but couldn't decrypt the session key,
124
so thus that key is not the anonymous recipient.
125
Move the next passphrase into last for the next
126
round. We only do this if the secret key was
127
successfully checked as in the normal case,
128
check_secret_key handles this for us via
131
next_to_last_passphrase();
123
136
log_info(_("okay, we are the anonymous recipient.\n") );
127
140
enum_secret_keys( &enum_context, NULL, 0, 0 ); /* free context */
168
183
nframe = rbuflen;
186
rc = gpg_error (GPG_ERR_NOT_SUPPORTED);
188
#endif /*!ENABLE_CARD_SUPPORT*/
173
rc = pk_decrypt (sk->pubkey_algo, &plain_dek, enc->data, sk->skey);
192
rc = pk_decrypt (sk->pubkey_algo, &plain_dek, enc->data, sk->skey );
176
195
if (gcry_mpi_aprint (GCRYMPI_FMT_USG, &frame, &nframe, plain_dek))
178
197
gcry_mpi_release (plain_dek); plain_dek = NULL;
182
/* Now get the DEK (data encryption key) from the frame
184
* Old versions encode the DEK in in this format (msb is left):
186
* 0 1 DEK(16 bytes) CSUM(2 bytes) 0 RND(n bytes) 2
188
* Later versions encode the DEK like this:
190
* 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes)
192
* (mpi_get_buffer already removed the leading zero).
194
* RND are non-zero randow bytes.
195
* A is the cipher algorithm
196
* DEK is the encryption key (session key) with length k
200
log_printhex ("DEK frame:", frame, nframe );
200
/* Now get the DEK (data encryption key) from the frame
202
* Old versions encode the DEK in in this format (msb is left):
204
* 0 1 DEK(16 bytes) CSUM(2 bytes) 0 RND(n bytes) 2
206
* Later versions encode the DEK like this:
208
* 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes)
210
* (mpi_get_buffer already removed the leading zero).
212
* RND are non-zero randow bytes.
213
* A is the cipher algorithm
214
* DEK is the encryption key (session key) with length k
218
log_printhex ("DEK frame:", frame, nframe );
223
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
224
if( frame[n] == 1 && frame[nframe-1] == 2 ) {
225
log_info(_("old encoding of the DEK is not supported\n"));
226
rc = G10ERR_CIPHER_ALGO;
229
if( frame[n] != 2 ) /* somethink is wrong */
230
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
231
for(n++; n < nframe && frame[n]; n++ ) /* skip the random bytes */
233
n++; /* and the zero byte */
237
{ rc = G10ERR_WRONG_SECKEY; goto leave; }
239
dek->keylen = nframe - (n+1) - 2;
240
dek->algo = frame[n++];
241
if( dek->algo == CIPHER_ALGO_IDEA )
242
write_status(STATUS_RSA_OR_IDEA);
243
rc = openpgp_cipher_test_algo (dek->algo);
245
if( !opt.quiet && gpg_err_code (rc) == GPG_ERR_CIPHER_ALGO ) {
246
log_info(_("cipher algorithm %d%s is unknown or disabled\n"),
247
dek->algo, dek->algo == CIPHER_ALGO_IDEA? " (IDEA)":"");
248
if(dek->algo==CIPHER_ALGO_IDEA)
254
if ( dek->keylen != gcry_cipher_get_algo_keylen (dek->algo) ) {
255
rc = GPG_ERR_WRONG_SECKEY;
259
/* copy the key to DEK and compare the checksum */
260
csum = frame[nframe-2] << 8;
261
csum |= frame[nframe-1];
262
memcpy( dek->key, frame+n, dek->keylen );
263
for( csum2=0, n=0; n < dek->keylen; n++ )
264
csum2 += dek->key[n];
265
if( csum != csum2 ) {
266
rc = G10ERR_WRONG_SECKEY;
270
log_printhex ("DEK is:", dek->key, dek->keylen );
271
/* check that the algo is in the preferences and whether it has expired */
205
{ rc = GPG_ERR_WRONG_SECKEY; goto leave; }
206
if( frame[n] == 1 && frame[nframe-1] == 2 ) {
207
log_info(_("old encoding of the DEK is not supported\n"));
208
rc = GPG_ERR_CIPHER_ALGO;
211
if( frame[n] != 2 ) /* somethink is wrong */
212
{ rc = GPG_ERR_WRONG_SECKEY; goto leave; }
213
for(n++; n < nframe && frame[n]; n++ ) /* skip the random bytes */
215
n++; /* and the zero byte */
219
{ rc = GPG_ERR_WRONG_SECKEY; goto leave; }
220
dek->keylen = nframe - (n+1) - 2;
221
dek->algo = frame[n++];
222
if( dek->algo == CIPHER_ALGO_IDEA )
223
write_status(STATUS_RSA_OR_IDEA);
224
rc = openpgp_cipher_test_algo (dek->algo);
226
if( !opt.quiet && gpg_err_code (rc) == GPG_ERR_CIPHER_ALGO ) {
227
log_info(_("cipher algorithm %d%s is unknown or disabled\n"),
228
dek->algo, dek->algo == CIPHER_ALGO_IDEA? " (IDEA)":"");
229
if(dek->algo==CIPHER_ALGO_IDEA)
235
if( dek->keylen != gcry_cipher_get_algo_keylen (dek->algo) ) {
236
rc = GPG_ERR_WRONG_SECKEY;
240
/* copy the key to DEK and compare the checksum */
241
csum = frame[nframe-2] << 8;
242
csum |= frame[nframe-1];
243
memcpy( dek->key, frame+n, dek->keylen );
244
for( csum2=0, n=0; n < dek->keylen; n++ )
245
csum2 += dek->key[n];
246
if( csum != csum2 ) {
247
rc = GPG_ERR_WRONG_SECKEY;
251
log_printhex ("DEK is:", dek->key, dek->keylen );
252
/* check that the algo is in the preferences and whether it has expired */
254
PKT_public_key *pk = NULL;
255
KBNODE pkb = get_pubkeyblock (keyid);
259
log_error("oops: public key not found for preference check\n");
261
else if( pkb->pkt->pkt.public_key->selfsigversion > 3
262
&& dek->algo != CIPHER_ALGO_3DES
263
&& !is_algo_in_prefs( pkb, PREFTYPE_SYM, dek->algo ) ) {
264
/* Don't print a note while we are not on verbose mode,
265
* the cipher is blowfish and the preferences have twofish
267
if( opt.verbose || dek->algo != CIPHER_ALGO_BLOWFISH
268
|| !is_algo_in_prefs( pkb, PREFTYPE_SYM, CIPHER_ALGO_TWOFISH))
270
"NOTE: cipher algorithm %d not found in preferences\n"),
273
PKT_public_key *pk = NULL;
274
KBNODE pkb = get_pubkeyblock (keyid);
278
log_error("oops: public key not found for preference check\n");
280
else if(pkb->pkt->pkt.public_key->selfsigversion > 3
281
&& dek->algo != CIPHER_ALGO_3DES
283
&& !is_algo_in_prefs( pkb, PREFTYPE_SYM, dek->algo ))
284
log_info (_("WARNING: cipher algorithm %s not found in recipient"
285
" preferences\n"), openpgp_cipher_algo_name (dek->algo));
277
for (k=pkb; k; k = k->next) {
278
if (k->pkt->pkttype == PKT_PUBLIC_KEY
279
|| k->pkt->pkttype == PKT_PUBLIC_SUBKEY){
281
keyid_from_pk(k->pkt->pkt.public_key, aki);
283
if (aki[0]==keyid[0] && aki[1]==keyid[1]) {
284
pk = k->pkt->pkt.public_key;
291
if ( pk->expiredate && pk->expiredate <= make_timestamp() ) {
292
log_info(_("NOTE: secret key %08lX expired at %s\n"),
293
(ulong)keyid[1], asctimestamp( pk->expiredate) );
297
if ( pk && pk->is_revoked ) {
298
log_info( _("NOTE: key has been revoked") );
299
putc( '\n', log_get_stream() );
300
show_revocation_reason( pk, 1 );
303
release_kbnode (pkb);
309
gcry_mpi_release (plain_dek);
289
for (k=pkb; k; k = k->next) {
290
if (k->pkt->pkttype == PKT_PUBLIC_KEY
291
|| k->pkt->pkttype == PKT_PUBLIC_SUBKEY){
293
keyid_from_pk(k->pkt->pkt.public_key, aki);
295
if (aki[0]==keyid[0] && aki[1]==keyid[1]) {
296
pk = k->pkt->pkt.public_key;
303
if ( pk->expiredate && pk->expiredate <= make_timestamp() ) {
304
log_info(_("NOTE: secret key %s expired at %s\n"),
305
keystr(keyid), asctimestamp( pk->expiredate) );
309
if ( pk && pk->is_revoked ) {
310
log_info( _("NOTE: key has been revoked") );
312
show_revocation_reason( pk, 1 );
315
release_kbnode (pkb);
321
gcry_mpi_release (plain_dek);