1 /* pubkey-enc.c - Process a public key encoded packet.
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2006, 2009,
3 * 2010 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 3 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, see <https://www.gnu.org/licenses/>.
36 #include "call-agent.h"
40 static gpg_error_t get_it (PKT_pubkey_enc *k,
41 DEK *dek, PKT_public_key *sk, u32 *keyid);
44 /* Check that the given algo is mentioned in one of the valid user-ids. */
46 is_algo_in_prefs (kbnode_t keyblock, preftype_t type, int algo)
50 for (k = keyblock; k; k = k->next)
52 if (k->pkt->pkttype == PKT_USER_ID)
54 PKT_user_id *uid = k->pkt->pkt.user_id;
55 prefitem_t *prefs = uid->prefs;
57 if (uid->created && prefs && !uid->is_revoked && !uid->is_expired)
59 for (; prefs->type; prefs++)
60 if (prefs->type == type && prefs->value == algo)
70 * Get the session key from a pubkey enc packet and return it in DEK,
71 * which should have been allocated in secure memory by the caller.
74 get_session_key (ctrl_t ctrl, PKT_pubkey_enc * k, DEK * dek)
76 PKT_public_key *sk = NULL;
80 log_clock ("get_session_key enter");
82 rc = openpgp_pk_test_algo2 (k->pubkey_algo, PUBKEY_USAGE_ENC);
86 if ((k->keyid[0] || k->keyid[1]) && !opt.try_all_secrets)
88 sk = xmalloc_clear (sizeof *sk);
89 sk->pubkey_algo = k->pubkey_algo; /* We want a pubkey with this algo. */
90 if (!(rc = get_seckey (sk, k->keyid)))
91 rc = get_it (k, dek, sk, k->keyid);
93 else if (opt.skip_hidden_recipients)
94 rc = gpg_error (GPG_ERR_NO_SECKEY);
95 else /* Anonymous receiver: Try all available secret keys. */
97 void *enum_context = NULL;
102 free_public_key (sk);
103 sk = xmalloc_clear (sizeof *sk);
104 rc = enum_secret_keys (ctrl, &enum_context, sk);
107 rc = GPG_ERR_NO_SECKEY;
110 if (sk->pubkey_algo != k->pubkey_algo)
112 if (!(sk->pubkey_usage & PUBKEY_USAGE_ENC))
114 keyid_from_pk (sk, keyid);
116 log_info (_("anonymous recipient; trying secret key %s ...\n"),
119 rc = get_it (k, dek, sk, keyid);
123 log_info (_("okay, we are the anonymous recipient.\n"));
126 else if (gpg_err_code (rc) == GPG_ERR_FULLY_CANCELED)
127 break; /* Don't try any more secret keys. */
129 enum_secret_keys (ctrl, &enum_context, NULL); /* free context */
133 free_public_key (sk);
135 log_clock ("get_session_key leave");
141 get_it (PKT_pubkey_enc *enc, DEK *dek, PKT_public_key *sk, u32 *keyid)
152 byte fp[MAX_FINGERPRINT_LEN];
156 log_clock ("decryption start");
158 /* Get the keygrip. */
159 err = hexkeygrip_from_pk (sk, &keygrip);
163 /* Convert the data to an S-expression. */
164 if (sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL
165 || sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E)
167 if (!enc->data[0] || !enc->data[1])
168 err = gpg_error (GPG_ERR_BAD_MPI);
170 err = gcry_sexp_build (&s_data, NULL, "(enc-val(elg(a%m)(b%m)))",
171 enc->data[0], enc->data[1]);
173 else if (sk->pubkey_algo == PUBKEY_ALGO_RSA
174 || sk->pubkey_algo == PUBKEY_ALGO_RSA_E)
177 err = gpg_error (GPG_ERR_BAD_MPI);
179 err = gcry_sexp_build (&s_data, NULL, "(enc-val(rsa(a%m)))",
182 else if (sk->pubkey_algo == PUBKEY_ALGO_ECDH)
184 if (!enc->data[0] || !enc->data[1])
185 err = gpg_error (GPG_ERR_BAD_MPI);
187 err = gcry_sexp_build (&s_data, NULL, "(enc-val(ecdh(s%m)(e%m)))",
188 enc->data[1], enc->data[0]);
191 err = gpg_error (GPG_ERR_BUG);
196 if (sk->pubkey_algo == PUBKEY_ALGO_ECDH)
198 fingerprint_from_pk (sk, fp, &fpn);
199 log_assert (fpn == 20);
203 desc = gpg_format_keydesc (sk, FORMAT_KEYDESC_NORMAL, 1);
204 err = agent_pkdecrypt (NULL, keygrip,
205 desc, sk->keyid, sk->main_keyid, sk->pubkey_algo,
206 s_data, &frame, &nframe, &padding);
208 gcry_sexp_release (s_data);
212 /* Now get the DEK (data encryption key) from the frame
214 * Old versions encode the DEK in in this format (msb is left):
216 * 0 1 DEK(16 bytes) CSUM(2 bytes) 0 RND(n bytes) 2
218 * Later versions encode the DEK like this:
220 * 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes)
222 * (mpi_get_buffer already removed the leading zero).
224 * RND are non-zero randow bytes.
225 * A is the cipher algorithm
226 * DEK is the encryption key (session key) with length k
230 log_printhex ("DEK frame:", frame, nframe);
233 if (sk->pubkey_algo == PUBKEY_ALGO_ECDH)
235 gcry_mpi_t shared_mpi;
238 /* At the beginning the frame are the bytes of shared point MPI. */
239 err = gcry_mpi_scan (&shared_mpi, GCRYMPI_FMT_USG, frame, nframe, NULL);
242 err = gpg_error (GPG_ERR_WRONG_SECKEY);
246 err = pk_ecdh_decrypt (&decoded, fp, enc->data[1]/*encr data as an MPI*/,
247 shared_mpi, sk->pkey);
248 mpi_release (shared_mpi);
253 err = gcry_mpi_aprint (GCRYMPI_FMT_USG, &frame, &nframe, decoded);
254 mpi_release (decoded);
258 /* Now the frame are the bytes decrypted but padded session key. */
260 /* Allow double padding for the benefit of DEK size concealment.
261 Higher than this is wasteful. */
262 if (!nframe || frame[nframe-1] > 8*2 || nframe <= 8
263 || frame[nframe-1] > nframe)
265 err = gpg_error (GPG_ERR_WRONG_SECKEY);
268 nframe -= frame[nframe-1]; /* Remove padding. */
269 log_assert (!n); /* (used just below) */
277 err = gpg_error (GPG_ERR_WRONG_SECKEY);
280 if (frame[n] == 1 && frame[nframe - 1] == 2)
282 log_info (_("old encoding of the DEK is not supported\n"));
283 err = gpg_error (GPG_ERR_CIPHER_ALGO);
286 if (frame[n] != 2) /* Something went wrong. */
288 err = gpg_error (GPG_ERR_WRONG_SECKEY);
291 for (n++; n < nframe && frame[n]; n++) /* Skip the random bytes. */
293 n++; /* Skip the zero byte. */
299 err = gpg_error (GPG_ERR_WRONG_SECKEY);
303 dek->keylen = nframe - (n + 1) - 2;
304 dek->algo = frame[n++];
305 err = openpgp_cipher_test_algo (dek->algo);
308 if (!opt.quiet && gpg_err_code (err) == GPG_ERR_CIPHER_ALGO)
310 log_info (_("cipher algorithm %d%s is unknown or disabled\n"),
312 dek->algo == CIPHER_ALGO_IDEA ? " (IDEA)" : "");
317 if (dek->keylen != openpgp_cipher_get_algo_keylen (dek->algo))
319 err = gpg_error (GPG_ERR_WRONG_SECKEY);
323 /* Copy the key to DEK and compare the checksum. */
324 csum = buf16_to_u16 (frame+nframe-2);
325 memcpy (dek->key, frame + n, dek->keylen);
326 for (csum2 = 0, n = 0; n < dek->keylen; n++)
327 csum2 += dek->key[n];
330 err = gpg_error (GPG_ERR_WRONG_SECKEY);
334 log_clock ("decryption ready");
336 log_printhex ("DEK is:", dek->key, dek->keylen);
338 /* Check that the algo is in the preferences and whether it has expired. */
340 PKT_public_key *pk = NULL;
341 KBNODE pkb = get_pubkeyblock (keyid);
346 log_error ("oops: public key not found for preference check\n");
348 else if (pkb->pkt->pkt.public_key->selfsigversion > 3
349 && dek->algo != CIPHER_ALGO_3DES
351 && !is_algo_in_prefs (pkb, PREFTYPE_SYM, dek->algo))
352 log_info (_("WARNING: cipher algorithm %s not found in recipient"
353 " preferences\n"), openpgp_cipher_algo_name (dek->algo));
358 for (k = pkb; k; k = k->next)
360 if (k->pkt->pkttype == PKT_PUBLIC_KEY
361 || k->pkt->pkttype == PKT_PUBLIC_SUBKEY)
364 keyid_from_pk (k->pkt->pkt.public_key, aki);
366 if (aki[0] == keyid[0] && aki[1] == keyid[1])
368 pk = k->pkt->pkt.public_key;
375 if (pk->expiredate && pk->expiredate <= make_timestamp ())
377 log_info (_("Note: secret key %s expired at %s\n"),
378 keystr (keyid), asctimestamp (pk->expiredate));
382 if (pk && pk->flags.revoked)
384 log_info (_("Note: key has been revoked"));
386 show_revocation_reason (pk, 1);
389 release_kbnode (pkb);
401 * Get the session key from the given string.
402 * String is supposed to be formatted as this:
403 * <algo-id>:<even-number-of-hex-digits>
406 get_override_session_key (DEK *dek, const char *string)
412 return GPG_ERR_BAD_KEY;
413 dek->algo = atoi (string);
415 return GPG_ERR_BAD_KEY;
416 if (!(s = strchr (string, ':')))
417 return GPG_ERR_BAD_KEY;
419 for (i = 0; i < DIM (dek->key) && *s; i++, s += 2)
421 int c = hextobyte (s);
423 return GPG_ERR_BAD_KEY;
427 return GPG_ERR_BAD_KEY;