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/>.
27 #include "../common/util.h"
31 #include "../common/status.h"
34 #include "../common/i18n.h"
36 #include "call-agent.h"
37 #include "../common/host2net.h"
38 #include "../common/compliance.h"
41 static gpg_error_t get_it (ctrl_t ctrl, PKT_pubkey_enc *k,
42 DEK *dek, PKT_public_key *sk, u32 *keyid);
45 /* Check that the given algo is mentioned in one of the valid user-ids. */
47 is_algo_in_prefs (kbnode_t keyblock, preftype_t type, int algo)
51 for (k = keyblock; k; k = k->next)
53 if (k->pkt->pkttype == PKT_USER_ID)
55 PKT_user_id *uid = k->pkt->pkt.user_id;
56 prefitem_t *prefs = uid->prefs;
58 if (uid->created && prefs && !uid->flags.revoked && !uid->flags.expired)
60 for (; prefs->type; prefs++)
61 if (prefs->type == type && prefs->value == algo)
71 * Get the session key from a pubkey enc packet and return it in DEK,
72 * which should have been allocated in secure memory by the caller.
75 get_session_key (ctrl_t ctrl, PKT_pubkey_enc * k, DEK * dek)
77 PKT_public_key *sk = NULL;
81 log_clock ("get_session_key enter");
83 rc = openpgp_pk_test_algo2 (k->pubkey_algo, PUBKEY_USAGE_ENC);
87 if ((k->keyid[0] || k->keyid[1]) && !opt.try_all_secrets)
89 sk = xmalloc_clear (sizeof *sk);
90 sk->pubkey_algo = k->pubkey_algo; /* We want a pubkey with this algo. */
91 if (!(rc = get_seckey (ctrl, sk, k->keyid)))
93 /* Print compliance warning. */
94 if (!gnupg_pk_is_compliant (opt.compliance,
96 sk->pkey, nbits_from_pk (sk), NULL))
97 log_info (_("Note: key %s is not suitable for encryption"
100 gnupg_compliance_option_string (opt.compliance));
102 rc = get_it (ctrl, k, dek, sk, k->keyid);
105 else if (opt.skip_hidden_recipients)
106 rc = gpg_error (GPG_ERR_NO_SECKEY);
107 else /* Anonymous receiver: Try all available secret keys. */
109 void *enum_context = NULL;
114 free_public_key (sk);
115 sk = xmalloc_clear (sizeof *sk);
116 rc = enum_secret_keys (ctrl, &enum_context, sk);
119 rc = GPG_ERR_NO_SECKEY;
122 if (sk->pubkey_algo != k->pubkey_algo)
124 if (!(sk->pubkey_usage & PUBKEY_USAGE_ENC))
126 keyid_from_pk (sk, keyid);
128 log_info (_("anonymous recipient; trying secret key %s ...\n"),
131 /* Print compliance warning. */
132 if (!gnupg_pk_is_compliant (opt.compliance,
134 sk->pkey, nbits_from_pk (sk), NULL))
135 log_info (_("Note: key %s is not suitable for encryption"
138 gnupg_compliance_option_string (opt.compliance));
140 rc = get_it (ctrl, k, dek, sk, keyid);
144 log_info (_("okay, we are the anonymous recipient.\n"));
147 else if (gpg_err_code (rc) == GPG_ERR_FULLY_CANCELED)
148 break; /* Don't try any more secret keys. */
150 enum_secret_keys (ctrl, &enum_context, NULL); /* free context */
154 free_public_key (sk);
156 log_clock ("get_session_key leave");
163 PKT_pubkey_enc *enc, DEK *dek, PKT_public_key *sk, u32 *keyid)
174 byte fp[MAX_FINGERPRINT_LEN];
178 log_clock ("decryption start");
180 /* Get the keygrip. */
181 err = hexkeygrip_from_pk (sk, &keygrip);
185 /* Convert the data to an S-expression. */
186 if (sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL
187 || sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E)
189 if (!enc->data[0] || !enc->data[1])
190 err = gpg_error (GPG_ERR_BAD_MPI);
192 err = gcry_sexp_build (&s_data, NULL, "(enc-val(elg(a%m)(b%m)))",
193 enc->data[0], enc->data[1]);
195 else if (sk->pubkey_algo == PUBKEY_ALGO_RSA
196 || sk->pubkey_algo == PUBKEY_ALGO_RSA_E)
199 err = gpg_error (GPG_ERR_BAD_MPI);
201 err = gcry_sexp_build (&s_data, NULL, "(enc-val(rsa(a%m)))",
204 else if (sk->pubkey_algo == PUBKEY_ALGO_ECDH)
206 if (!enc->data[0] || !enc->data[1])
207 err = gpg_error (GPG_ERR_BAD_MPI);
209 err = gcry_sexp_build (&s_data, NULL, "(enc-val(ecdh(s%m)(e%m)))",
210 enc->data[1], enc->data[0]);
213 err = gpg_error (GPG_ERR_BUG);
218 if (sk->pubkey_algo == PUBKEY_ALGO_ECDH)
220 fingerprint_from_pk (sk, fp, &fpn);
221 log_assert (fpn == 20);
225 desc = gpg_format_keydesc (ctrl, sk, FORMAT_KEYDESC_NORMAL, 1);
226 err = agent_pkdecrypt (NULL, keygrip,
227 desc, sk->keyid, sk->main_keyid, sk->pubkey_algo,
228 s_data, &frame, &nframe, &padding);
230 gcry_sexp_release (s_data);
234 /* Now get the DEK (data encryption key) from the frame
236 * Old versions encode the DEK in this format (msb is left):
238 * 0 1 DEK(16 bytes) CSUM(2 bytes) 0 RND(n bytes) 2
240 * Later versions encode the DEK like this:
242 * 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes)
244 * (mpi_get_buffer already removed the leading zero).
246 * RND are non-zero randow bytes.
247 * A is the cipher algorithm
248 * DEK is the encryption key (session key) with length k
252 log_printhex ("DEK frame:", frame, nframe);
255 if (sk->pubkey_algo == PUBKEY_ALGO_ECDH)
257 gcry_mpi_t shared_mpi;
260 /* At the beginning the frame are the bytes of shared point MPI. */
261 err = gcry_mpi_scan (&shared_mpi, GCRYMPI_FMT_USG, frame, nframe, NULL);
264 err = gpg_error (GPG_ERR_WRONG_SECKEY);
268 err = pk_ecdh_decrypt (&decoded, fp, enc->data[1]/*encr data as an MPI*/,
269 shared_mpi, sk->pkey);
270 mpi_release (shared_mpi);
275 err = gcry_mpi_aprint (GCRYMPI_FMT_USG, &frame, &nframe, decoded);
276 mpi_release (decoded);
280 /* Now the frame are the bytes decrypted but padded session key. */
282 /* Allow double padding for the benefit of DEK size concealment.
283 Higher than this is wasteful. */
284 if (!nframe || frame[nframe-1] > 8*2 || nframe <= 8
285 || frame[nframe-1] > nframe)
287 err = gpg_error (GPG_ERR_WRONG_SECKEY);
290 nframe -= frame[nframe-1]; /* Remove padding. */
291 log_assert (!n); /* (used just below) */
299 err = gpg_error (GPG_ERR_WRONG_SECKEY);
302 if (frame[n] == 1 && frame[nframe - 1] == 2)
304 log_info (_("old encoding of the DEK is not supported\n"));
305 err = gpg_error (GPG_ERR_CIPHER_ALGO);
308 if (frame[n] != 2) /* Something went wrong. */
310 err = gpg_error (GPG_ERR_WRONG_SECKEY);
313 for (n++; n < nframe && frame[n]; n++) /* Skip the random bytes. */
315 n++; /* Skip the zero byte. */
321 err = gpg_error (GPG_ERR_WRONG_SECKEY);
325 dek->keylen = nframe - (n + 1) - 2;
326 dek->algo = frame[n++];
327 err = openpgp_cipher_test_algo (dek->algo);
330 if (!opt.quiet && gpg_err_code (err) == GPG_ERR_CIPHER_ALGO)
332 log_info (_("cipher algorithm %d%s is unknown or disabled\n"),
334 dek->algo == CIPHER_ALGO_IDEA ? " (IDEA)" : "");
339 if (dek->keylen != openpgp_cipher_get_algo_keylen (dek->algo))
341 err = gpg_error (GPG_ERR_WRONG_SECKEY);
345 /* Copy the key to DEK and compare the checksum. */
346 csum = buf16_to_u16 (frame+nframe-2);
347 memcpy (dek->key, frame + n, dek->keylen);
348 for (csum2 = 0, n = 0; n < dek->keylen; n++)
349 csum2 += dek->key[n];
352 err = gpg_error (GPG_ERR_WRONG_SECKEY);
356 log_clock ("decryption ready");
358 log_printhex ("DEK is:", dek->key, dek->keylen);
360 /* Check that the algo is in the preferences and whether it has
361 * expired. Also print a status line with the key's fingerprint. */
363 PKT_public_key *pk = NULL;
364 PKT_public_key *mainpk = NULL;
365 KBNODE pkb = get_pubkeyblock (ctrl, keyid);
370 log_error ("oops: public key not found for preference check\n");
372 else if (pkb->pkt->pkt.public_key->selfsigversion > 3
373 && dek->algo != CIPHER_ALGO_3DES
375 && !is_algo_in_prefs (pkb, PREFTYPE_SYM, dek->algo))
376 log_info (_("WARNING: cipher algorithm %s not found in recipient"
377 " preferences\n"), openpgp_cipher_algo_name (dek->algo));
384 for (k = pkb; k; k = k->next)
386 if (k->pkt->pkttype == PKT_PUBLIC_KEY
387 || k->pkt->pkttype == PKT_PUBLIC_SUBKEY)
394 mainpk = k->pkt->pkt.public_key;
397 keyid_from_pk (k->pkt->pkt.public_key, aki);
398 if (aki[0] == keyid[0] && aki[1] == keyid[1])
400 pk = k->pkt->pkt.public_key;
407 if (pk->expiredate && pk->expiredate <= make_timestamp ())
409 log_info (_("Note: secret key %s expired at %s\n"),
410 keystr (keyid), asctimestamp (pk->expiredate));
414 if (pk && pk->flags.revoked)
416 log_info (_("Note: key has been revoked"));
418 show_revocation_reason (ctrl, pk, 1);
421 if (is_status_enabled () && pk && mainpk)
423 char pkhex[MAX_FINGERPRINT_LEN*2+1];
424 char mainpkhex[MAX_FINGERPRINT_LEN*2+1];
426 hexfingerprint (pk, pkhex, sizeof pkhex);
427 hexfingerprint (mainpk, mainpkhex, sizeof mainpkhex);
429 /* Note that we do not want to create a trustdb just for
430 * getting the ownertrust: If there is no trustdb there can't
431 * be ulitmately trusted key anyway and thus the ownertrust
432 * value is irrelevant. */
433 write_status_printf (STATUS_DECRYPTION_KEY, "%s %s %c",
435 get_ownertrust_info (ctrl, mainpk, 1));
439 release_kbnode (pkb);
451 * Get the session key from the given string.
452 * String is supposed to be formatted as this:
453 * <algo-id>:<even-number-of-hex-digits>
456 get_override_session_key (DEK *dek, const char *string)
462 return GPG_ERR_BAD_KEY;
463 dek->algo = atoi (string);
465 return GPG_ERR_BAD_KEY;
466 if (!(s = strchr (string, ':')))
467 return GPG_ERR_BAD_KEY;
469 for (i = 0; i < DIM (dek->key) && *s; i++, s += 2)
471 int c = hextobyte (s);
473 return GPG_ERR_BAD_KEY;
477 return GPG_ERR_BAD_KEY;