Line | Count | Source |
1 | | /* encrypt.c - Main encryption driver |
2 | | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
3 | | * 2006, 2009 Free Software Foundation, Inc. |
4 | | * Copyright (C) 2016, 2023 g10 Code GmbH |
5 | | * |
6 | | * This file is part of GnuPG. |
7 | | * |
8 | | * GnuPG is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU General Public License as published by |
10 | | * the Free Software Foundation; either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * GnuPG is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
20 | | * SPDX-License-Identifier: GPL-3.0-or-later |
21 | | */ |
22 | | |
23 | | #include <config.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <errno.h> |
28 | | |
29 | | #include "gpg.h" |
30 | | #include "options.h" |
31 | | #include "packet.h" |
32 | | #include "../common/status.h" |
33 | | #include "../common/iobuf.h" |
34 | | #include "keydb.h" |
35 | | #include "../common/util.h" |
36 | | #include "main.h" |
37 | | #include "filter.h" |
38 | | #include "trustdb.h" |
39 | | #include "../common/i18n.h" |
40 | | #include "../common/status.h" |
41 | | #include "pkglue.h" |
42 | | #include "../common/compliance.h" |
43 | | |
44 | | |
45 | | static int encrypt_simple( const char *filename, int mode, int use_seskey ); |
46 | | static int write_pubkey_enc_from_list (ctrl_t ctrl, pk_list_t pk_list, |
47 | | DEK *dek, iobuf_t out, |
48 | | struct pubkey_enc_info_item *restrct); |
49 | | |
50 | | |
51 | | |
52 | | /* Helper for show the "encrypted for USER" during encryption. |
53 | | * PUBKEY_USAGE is used to figure out whether this is an ADSK key. */ |
54 | | static void |
55 | | show_encrypted_for_user_info (ctrl_t ctrl, unsigned int pubkey_usage, |
56 | | PKT_pubkey_enc *enc, DEK *dek) |
57 | 0 | { |
58 | 0 | char *ustr = get_user_id_string_native (ctrl, enc->keyid); |
59 | 0 | if ((pubkey_usage & PUBKEY_USAGE_RENC)) |
60 | 0 | { |
61 | 0 | char *tmpustr = xstrconcat (ustr, " [ADSK]", NULL); |
62 | 0 | xfree (ustr); |
63 | 0 | ustr = tmpustr; |
64 | 0 | } |
65 | 0 | log_info (_("%s/%s.%s encrypted for: \"%s\"\n"), |
66 | 0 | openpgp_pk_algo_name (enc->pubkey_algo), |
67 | 0 | openpgp_cipher_algo_name (dek->algo), |
68 | 0 | dek->use_aead? openpgp_aead_algo_name (dek->use_aead) |
69 | 0 | /**/ : "CFB", |
70 | 0 | ustr ); |
71 | 0 | xfree (ustr); |
72 | 0 | } |
73 | | |
74 | | |
75 | | /* |
76 | | * Encrypt FILENAME with only the symmetric cipher. Take input from |
77 | | * stdin if FILENAME is NULL. If --force-ocb or --use-ocb-sym is used |
78 | | * we use an SKESK. |
79 | | */ |
80 | | int |
81 | | encrypt_symmetric (const char *filename) |
82 | 0 | { |
83 | 0 | return encrypt_simple (filename, 1, opt.force_ocb || opt.use_ocb_sym); |
84 | 0 | } |
85 | | |
86 | | |
87 | | /**************** |
88 | | * Encrypt FILENAME as a literal data packet only. Take input from |
89 | | * stdin if FILENAME is NULL. |
90 | | */ |
91 | | int |
92 | | encrypt_store (const char *filename) |
93 | 0 | { |
94 | 0 | return encrypt_simple( filename, 0, 0 ); |
95 | 0 | } |
96 | | |
97 | | |
98 | | /* Create and setup a DEK structure and print appropriate warnings. |
99 | | * PK_LIST gives the list of public keys. Always returns a DEK. The |
100 | | * actual session needs to be added later. */ |
101 | | static DEK * |
102 | | create_dek_with_warnings (pk_list_t pk_list) |
103 | 0 | { |
104 | 0 | DEK *dek; |
105 | |
|
106 | 0 | dek = xmalloc_secure_clear (sizeof *dek); |
107 | 0 | if (!opt.def_cipher_algo) |
108 | 0 | { |
109 | | /* Try to get it from the prefs. */ |
110 | 0 | dek->algo = select_algo_from_prefs (pk_list, PREFTYPE_SYM, -1, NULL); |
111 | 0 | if (dek->algo == -1) |
112 | 0 | { |
113 | | /* If does not make sense to fallback to the rfc4880 |
114 | | * required 3DES if we will reject that algo later. Thus we |
115 | | * fallback to AES anticipating RFC4880bis rules. */ |
116 | 0 | if (opt.flags.allow_old_cipher_algos) |
117 | 0 | dek->algo = CIPHER_ALGO_3DES; |
118 | 0 | else |
119 | 0 | dek->algo = CIPHER_ALGO_AES; |
120 | 0 | } |
121 | | |
122 | | /* In case 3DES has been selected, print a warning if any key |
123 | | * does not have a preference for AES. This should help to |
124 | | * identify why encrypting to several recipients falls back to |
125 | | * 3DES. */ |
126 | 0 | if (opt.verbose && dek->algo == CIPHER_ALGO_3DES) |
127 | 0 | warn_missing_aes_from_pklist (pk_list); |
128 | 0 | } |
129 | 0 | else |
130 | 0 | { |
131 | 0 | if (!opt.expert |
132 | 0 | && (select_algo_from_prefs (pk_list, PREFTYPE_SYM, |
133 | 0 | opt.def_cipher_algo, NULL) |
134 | 0 | != opt.def_cipher_algo)) |
135 | 0 | { |
136 | 0 | log_info(_("WARNING: forcing symmetric cipher %s (%d)" |
137 | 0 | " violates recipient preferences\n"), |
138 | 0 | openpgp_cipher_algo_name (opt.def_cipher_algo), |
139 | 0 | opt.def_cipher_algo); |
140 | 0 | } |
141 | |
|
142 | 0 | dek->algo = opt.def_cipher_algo; |
143 | 0 | } |
144 | |
|
145 | 0 | if (dek->algo != CIPHER_ALGO_AES256) |
146 | 0 | { |
147 | | /* If quantum resistance was explicitly required, we force the |
148 | | * use of AES256 no matter what. Otherwise, we force AES256 if we |
149 | | * encrypt to Kyber keys only and the user did not explicity |
150 | | * request another another algo. */ |
151 | 0 | if (opt.flags.require_pqc_encryption) |
152 | 0 | dek->algo = CIPHER_ALGO_AES256; |
153 | 0 | else if (!opt.def_cipher_algo) |
154 | 0 | { |
155 | 0 | int non_kyber_pk = 0; |
156 | 0 | for ( ; pk_list; pk_list = pk_list->next) |
157 | 0 | if (!(pk_list->pk->pubkey_algo == PUBKEY_ALGO_KYBER |
158 | 0 | || (RFC9980 |
159 | 0 | && IS_PUBKEY_ALGO_MLK (pk_list->pk->pubkey_algo)))) |
160 | 0 | non_kyber_pk += 1; |
161 | 0 | if (!non_kyber_pk) |
162 | 0 | dek->algo = CIPHER_ALGO_AES256; |
163 | 0 | } |
164 | 0 | } |
165 | |
|
166 | 0 | return dek; |
167 | 0 | } |
168 | | |
169 | | |
170 | | /* Check whether all encryption keys are compliant with the current |
171 | | * mode and issue respective status lines. DEK has the info about the |
172 | | * session key and PK_LIST the list of public keys. */ |
173 | | static gpg_error_t |
174 | | check_encryption_compliance (DEK *dek, pk_list_t pk_list) |
175 | 0 | { |
176 | 0 | gpg_error_t err = 0; |
177 | 0 | pk_list_t pkr; |
178 | 0 | int compliant; |
179 | | |
180 | | /* First check whether we should use the algo at all. */ |
181 | 0 | if (openpgp_cipher_blocklen (dek->algo) < 16 |
182 | 0 | && !opt.flags.allow_old_cipher_algos) |
183 | 0 | { |
184 | 0 | log_error (_("cipher algorithm '%s' may not be used for encryption\n"), |
185 | 0 | openpgp_cipher_algo_name (dek->algo)); |
186 | 0 | if (!opt.quiet) |
187 | 0 | log_info (_("(use option \"%s\" to override)\n"), |
188 | 0 | "--allow-old-cipher-algos"); |
189 | 0 | err = gpg_error (GPG_ERR_CIPHER_ALGO); |
190 | 0 | goto leave; |
191 | 0 | } |
192 | | |
193 | | /* Now check the compliance. */ |
194 | 0 | if (! gnupg_cipher_is_allowed (opt.compliance, 1, dek->algo, |
195 | 0 | GCRY_CIPHER_MODE_CFB)) |
196 | 0 | { |
197 | 0 | log_error (_("cipher algorithm '%s' may not be used in %s mode\n"), |
198 | 0 | openpgp_cipher_algo_name (dek->algo), |
199 | 0 | gnupg_compliance_option_string (opt.compliance)); |
200 | 0 | err = gpg_error (GPG_ERR_CIPHER_ALGO); |
201 | 0 | goto leave; |
202 | 0 | } |
203 | | |
204 | 0 | if (!gnupg_rng_is_compliant (opt.compliance)) |
205 | 0 | { |
206 | 0 | err = gpg_error (GPG_ERR_FORBIDDEN); |
207 | 0 | log_error (_("%s is not compliant with %s mode\n"), |
208 | 0 | "RNG", |
209 | 0 | gnupg_compliance_option_string (opt.compliance)); |
210 | 0 | write_status_error ("random-compliance", err); |
211 | 0 | goto leave; |
212 | 0 | } |
213 | | |
214 | | /* From here on we only test for CO_DE_VS - if we ever want to |
215 | | * return other compliance mode values we need to change this to |
216 | | * loop over all those values. */ |
217 | | /* FIXME:CO_FIPS */ |
218 | 0 | compliant = gnupg_gcrypt_is_compliant (CO_DE_VS); |
219 | |
|
220 | 0 | if (!gnupg_cipher_is_compliant (CO_DE_VS, dek->algo, GCRY_CIPHER_MODE_CFB)) |
221 | 0 | compliant = 0; |
222 | |
|
223 | 0 | for (pkr = pk_list; pkr; pkr = pkr->next) |
224 | 0 | { |
225 | 0 | PKT_public_key *pk = pkr->pk; |
226 | 0 | unsigned int nbits = nbits_from_pk (pk); |
227 | |
|
228 | 0 | if (!gnupg_pk_is_compliant (opt.compliance, pk->pubkey_algo, 0, |
229 | 0 | pk->pkey, nbits, NULL)) |
230 | 0 | log_info (_("WARNING: key %s is not suitable for encryption" |
231 | 0 | " in %s mode\n"), |
232 | 0 | keystr_from_pk (pk), |
233 | 0 | gnupg_compliance_option_string (opt.compliance)); |
234 | |
|
235 | 0 | if (compliant |
236 | 0 | && !gnupg_pk_is_compliant (CO_DE_VS, pk->pubkey_algo, 0, pk->pkey, |
237 | 0 | nbits, NULL)) |
238 | 0 | compliant = 0; /* Not compliant - reset flag. */ |
239 | 0 | } |
240 | | |
241 | | /* If we are compliant print the status for de-vs compliance. */ |
242 | 0 | if (compliant) |
243 | 0 | write_status_strings (STATUS_ENCRYPTION_COMPLIANCE_MODE, |
244 | 0 | gnupg_status_compliance_flag (CO_DE_VS), |
245 | 0 | NULL); |
246 | | |
247 | | /* Check whether we should fail the operation. */ |
248 | 0 | if (opt.flags.require_compliance |
249 | 0 | && opt.compliance == CO_DE_VS |
250 | 0 | && !compliant) |
251 | 0 | { |
252 | 0 | compliance_failure (); |
253 | 0 | err = gpg_error (GPG_ERR_FORBIDDEN); |
254 | 0 | goto leave; |
255 | 0 | } |
256 | | |
257 | 0 | leave: |
258 | 0 | return err; |
259 | 0 | } |
260 | | |
261 | | |
262 | | /* Encrypt a session key using DEK and store a pointer to the result |
263 | | * at R_ENCKEY and its length at R_ENCKEYLEN. |
264 | | * |
265 | | * R_SESKEY points to the unencrypted session key (.KEY, .KEYLEN) and |
266 | | * the algorithm that will be used to encrypt the contents of the |
267 | | * SKESK packet (.ALGO). If R_SESKEY points to NULL, then a random |
268 | | * session key that is appropriate for DEK->ALGO is generated and |
269 | | * stored at R_SESKEY. If AEAD_ALGO is not 0 the given AEAD algorithm |
270 | | * is used for encryption. |
271 | | */ |
272 | | static gpg_error_t |
273 | | encrypt_seskey (DEK *dek, aead_algo_t aead_algo, |
274 | | DEK **r_seskey, void **r_enckey, size_t *r_enckeylen) |
275 | 0 | { |
276 | 0 | gpg_error_t err; |
277 | 0 | gcry_cipher_hd_t hd = NULL; |
278 | 0 | byte *buf = NULL; |
279 | 0 | DEK *seskey; |
280 | |
|
281 | 0 | *r_enckey = NULL; |
282 | 0 | *r_enckeylen = 0; |
283 | |
|
284 | 0 | if (*r_seskey) |
285 | 0 | seskey = *r_seskey; |
286 | 0 | else |
287 | 0 | { |
288 | 0 | seskey = xtrycalloc (1, sizeof(DEK)); |
289 | 0 | if (!seskey) |
290 | 0 | { |
291 | 0 | err = gpg_error_from_syserror (); |
292 | 0 | goto leave; |
293 | 0 | } |
294 | 0 | seskey->algo = dek->algo; |
295 | 0 | make_session_key (seskey); |
296 | | /*log_hexdump( "thekey", c->key, c->keylen );*/ |
297 | 0 | } |
298 | | |
299 | | |
300 | 0 | if (aead_algo) |
301 | 0 | { |
302 | 0 | unsigned int noncelen; |
303 | 0 | enum gcry_cipher_modes ciphermode; |
304 | 0 | byte ad[4]; |
305 | |
|
306 | 0 | err = openpgp_aead_algo_info (aead_algo, &ciphermode, &noncelen); |
307 | 0 | if (err) |
308 | 0 | goto leave; |
309 | | |
310 | | /* Allocate space for the nonce, the key, and the authentication |
311 | | * tag (16). */ |
312 | 0 | buf = xtrymalloc_secure (noncelen + seskey->keylen + 16); |
313 | 0 | if (!buf) |
314 | 0 | { |
315 | 0 | err = gpg_error_from_syserror (); |
316 | 0 | goto leave; |
317 | 0 | } |
318 | | |
319 | 0 | gcry_randomize (buf, noncelen, GCRY_STRONG_RANDOM); |
320 | |
|
321 | 0 | err = openpgp_cipher_open (&hd, dek->algo, |
322 | 0 | ciphermode, GCRY_CIPHER_SECURE); |
323 | 0 | if (!err) |
324 | 0 | err = gcry_cipher_setkey (hd, dek->key, dek->keylen); |
325 | 0 | if (!err) |
326 | 0 | err = gcry_cipher_setiv (hd, buf, noncelen); |
327 | 0 | if (err) |
328 | 0 | goto leave; |
329 | | |
330 | 0 | ad[0] = (0xc0 | PKT_SYMKEY_ENC); |
331 | 0 | ad[1] = 5; |
332 | 0 | ad[2] = dek->algo; |
333 | 0 | ad[3] = aead_algo; |
334 | 0 | err = gcry_cipher_authenticate (hd, ad, 4); |
335 | 0 | if (err) |
336 | 0 | goto leave; |
337 | | |
338 | 0 | memcpy (buf + noncelen, seskey->key, seskey->keylen); |
339 | 0 | gcry_cipher_final (hd); |
340 | 0 | err = gcry_cipher_encrypt (hd, buf + noncelen, seskey->keylen, NULL,0); |
341 | 0 | if (err) |
342 | 0 | goto leave; |
343 | 0 | err = gcry_cipher_gettag (hd, buf + noncelen + seskey->keylen, 16); |
344 | 0 | if (err) |
345 | 0 | goto leave; |
346 | 0 | *r_enckeylen = noncelen + seskey->keylen + 16; |
347 | 0 | *r_enckey = buf; |
348 | 0 | buf = NULL; |
349 | 0 | } |
350 | 0 | else |
351 | 0 | { |
352 | | /* In the old version 4 SKESK the encrypted session key is |
353 | | * prefixed with a one-octet algorithm id. */ |
354 | 0 | buf = xtrymalloc_secure (1 + seskey->keylen); |
355 | 0 | if (!buf) |
356 | 0 | { |
357 | 0 | err = gpg_error_from_syserror (); |
358 | 0 | goto leave; |
359 | 0 | } |
360 | 0 | buf[0] = seskey->algo; |
361 | 0 | memcpy (buf + 1, seskey->key, seskey->keylen); |
362 | |
|
363 | 0 | err = openpgp_cipher_open (&hd, dek->algo, GCRY_CIPHER_MODE_CFB, 1); |
364 | 0 | if (!err) |
365 | 0 | err = gcry_cipher_setkey (hd, dek->key, dek->keylen); |
366 | 0 | if (!err) |
367 | 0 | err = gcry_cipher_setiv (hd, NULL, 0); |
368 | 0 | if (!err) |
369 | 0 | err = gcry_cipher_encrypt (hd, buf, seskey->keylen + 1, NULL, 0); |
370 | 0 | if (err) |
371 | 0 | goto leave; |
372 | 0 | *r_enckeylen = seskey->keylen + 1; |
373 | 0 | *r_enckey = buf; |
374 | 0 | buf = NULL; |
375 | 0 | } |
376 | | |
377 | | /* Return the session key in case we allocated it. */ |
378 | 0 | *r_seskey = seskey; |
379 | 0 | seskey = NULL; |
380 | |
|
381 | 0 | leave: |
382 | 0 | gcry_cipher_close (hd); |
383 | 0 | if (seskey != *r_seskey) |
384 | 0 | xfree (seskey); |
385 | 0 | xfree (buf); |
386 | 0 | return err; |
387 | 0 | } |
388 | | |
389 | | |
390 | | /* Return the AEAD algo if we shall use AEAD mode. Returns 0 if AEAD |
391 | | * shall not be used. */ |
392 | | aead_algo_t |
393 | | use_aead (pk_list_t pk_list, int algo) |
394 | 0 | { |
395 | 0 | int can_use; |
396 | |
|
397 | 0 | can_use = openpgp_cipher_get_algo_blklen (algo) == 16; |
398 | | |
399 | | /* With --force-aead we want OCB. We also use OCB in symmetric mode |
400 | | * with --use-ocb-sym which is detected by an empty PK_LIST. In |
401 | | * FIPS mode we silently replace OCB by GCM. */ |
402 | 0 | if (opt.force_ocb || (!pk_list && opt.use_ocb_sym)) |
403 | 0 | { |
404 | 0 | if (!can_use) |
405 | 0 | { |
406 | 0 | log_info ("Warning: request to use OCB ignored for cipher '%s'\n", |
407 | 0 | openpgp_cipher_algo_name (algo)); |
408 | 0 | return 0; |
409 | 0 | } |
410 | 0 | return opt.compliance == CO_FIPS? AEAD_ALGO_GCM : AEAD_ALGO_OCB; |
411 | 0 | } |
412 | | |
413 | | /* AEAD does only work with 128 bit cipher blocklength. */ |
414 | 0 | if (!can_use) |
415 | 0 | return 0; |
416 | | |
417 | | /* Note the user which keys have no AEAD feature flag set. */ |
418 | 0 | if (opt.verbose) |
419 | 0 | warn_missing_aead_from_pklist (pk_list); |
420 | | |
421 | | /* If all keys support AEAD we can use it. */ |
422 | 0 | return select_aead_from_pklist (pk_list); |
423 | 0 | } |
424 | | |
425 | | |
426 | | /* Shall we use the MDC? Yes - unless rfc-2440 compatibility is |
427 | | * requested. */ |
428 | | int |
429 | | use_mdc (pk_list_t pk_list,int algo) |
430 | 0 | { |
431 | 0 | (void)pk_list; |
432 | 0 | (void)algo; |
433 | | |
434 | | /* RFC-2440 don't has MDC - this is the only way to create a legacy |
435 | | * non-MDC encryption packet. */ |
436 | 0 | if (RFC2440) |
437 | 0 | return 0; |
438 | | |
439 | 0 | return 1; /* In all other cases we use the MDC */ |
440 | 0 | } |
441 | | |
442 | | |
443 | | /* We use the SEIPDv2 packet only if all keys are from RFC9980. */ |
444 | | int |
445 | | use_rfc9980_seipdv2 (pk_list_t pk_list) |
446 | 0 | { |
447 | 0 | PKT_public_key *pk; |
448 | |
|
449 | 0 | if (!RFC9980 || !pk_list) |
450 | 0 | return 0; /* No. */ |
451 | | |
452 | 0 | for ( ; pk_list; pk_list = pk_list->next ) |
453 | 0 | { |
454 | 0 | pk = pk_list->pk; |
455 | 0 | if (!(pk->pubkey_algo == PUBKEY_ALGO_X25519 |
456 | 0 | || IS_PUBKEY_ALGO_MLK (pk->pubkey_algo))) |
457 | 0 | return 0; /* No. */ |
458 | 0 | } |
459 | 0 | return 1; /* Yes. */ |
460 | 0 | } |
461 | | |
462 | | |
463 | | /* This function handles the --symmetric only (MODE true) and --store |
464 | | * (MODE false) cases. We don't want to use USE_SESKEY by default |
465 | | * very old gnupg versions can't handle it, and there isn't really any |
466 | | * point unless we're making a message that can be decrypted by a |
467 | | * public key or passphrase. */ |
468 | | static int |
469 | | encrypt_simple (const char *filename, int mode, int use_seskey) |
470 | 0 | { |
471 | 0 | iobuf_t inp, out; |
472 | 0 | PACKET pkt; |
473 | 0 | PKT_plaintext *pt = NULL; |
474 | 0 | STRING2KEY *s2k = NULL; |
475 | 0 | void *enckey = NULL; |
476 | 0 | size_t enckeylen = 0; |
477 | 0 | int rc = 0; |
478 | 0 | u32 filesize; |
479 | 0 | cipher_filter_context_t cfx; |
480 | 0 | armor_filter_context_t *afx = NULL; |
481 | 0 | compress_filter_context_t zfx; |
482 | 0 | text_filter_context_t tfx; |
483 | 0 | progress_filter_context_t *pfx; |
484 | 0 | int do_compress = !!default_compress_algo(); |
485 | |
|
486 | 0 | if (!gnupg_rng_is_compliant (opt.compliance)) |
487 | 0 | { |
488 | 0 | rc = gpg_error (GPG_ERR_FORBIDDEN); |
489 | 0 | log_error (_("%s is not compliant with %s mode\n"), |
490 | 0 | "RNG", |
491 | 0 | gnupg_compliance_option_string (opt.compliance)); |
492 | 0 | write_status_error ("random-compliance", rc); |
493 | 0 | return rc; |
494 | 0 | } |
495 | | |
496 | 0 | pfx = new_progress_context (); |
497 | 0 | memset( &cfx, 0, sizeof cfx); |
498 | 0 | memset( &zfx, 0, sizeof zfx); |
499 | 0 | memset( &tfx, 0, sizeof tfx); |
500 | 0 | init_packet(&pkt); |
501 | | |
502 | | /* Prepare iobufs. */ |
503 | 0 | inp = iobuf_open(filename); |
504 | 0 | if (inp) |
505 | 0 | iobuf_ioctl (inp, IOBUF_IOCTL_NO_CACHE, 1, NULL); |
506 | 0 | if (inp && is_secured_file (iobuf_get_fd (inp))) |
507 | 0 | { |
508 | 0 | iobuf_close (inp); |
509 | 0 | inp = NULL; |
510 | 0 | gpg_err_set_errno (EPERM); |
511 | 0 | } |
512 | 0 | if (!inp) |
513 | 0 | { |
514 | 0 | rc = gpg_error_from_syserror (); |
515 | 0 | log_error(_("can't open '%s': %s\n"), filename? filename: "[stdin]", |
516 | 0 | strerror(errno) ); |
517 | 0 | release_progress_context (pfx); |
518 | 0 | return rc; |
519 | 0 | } |
520 | | |
521 | 0 | handle_progress (pfx, inp, filename); |
522 | |
|
523 | 0 | if (opt.textmode) |
524 | 0 | iobuf_push_filter( inp, text_filter, &tfx ); |
525 | |
|
526 | 0 | cfx.dek = NULL; |
527 | 0 | if ( mode ) |
528 | 0 | { |
529 | 0 | aead_algo_t aead_algo; |
530 | |
|
531 | 0 | rc = setup_symkey (&s2k, &cfx.dek); |
532 | 0 | if (rc) |
533 | 0 | { |
534 | 0 | iobuf_close (inp); |
535 | 0 | if (gpg_err_code (rc) == GPG_ERR_CIPHER_ALGO |
536 | 0 | || gpg_err_code (rc) == GPG_ERR_DIGEST_ALGO) |
537 | 0 | ; /* Error has already been printed. */ |
538 | 0 | else |
539 | 0 | log_error (_("error creating passphrase: %s\n"), gpg_strerror (rc)); |
540 | 0 | release_progress_context (pfx); |
541 | 0 | return rc; |
542 | 0 | } |
543 | 0 | if (use_seskey && s2k->mode != 1 && s2k->mode != 3) |
544 | 0 | { |
545 | 0 | use_seskey = 0; |
546 | 0 | log_info (_("can't use a SKESK packet due to the S2K mode\n")); |
547 | 0 | } |
548 | | |
549 | | /* See whether we want to use AEAD. */ |
550 | 0 | aead_algo = use_aead (NULL, cfx.dek->algo); |
551 | |
|
552 | 0 | if ( use_seskey ) |
553 | 0 | { |
554 | 0 | DEK *dek = NULL; |
555 | |
|
556 | 0 | rc = encrypt_seskey (cfx.dek, aead_algo, &dek, &enckey, &enckeylen); |
557 | 0 | if (rc) |
558 | 0 | { |
559 | 0 | xfree (cfx.dek); |
560 | 0 | xfree (s2k); |
561 | 0 | iobuf_close (inp); |
562 | 0 | release_progress_context (pfx); |
563 | 0 | return rc; |
564 | 0 | } |
565 | | /* Replace key in DEK. */ |
566 | 0 | xfree (cfx.dek); |
567 | 0 | cfx.dek = dek; |
568 | 0 | } |
569 | | |
570 | 0 | if (aead_algo) |
571 | 0 | cfx.dek->use_aead = aead_algo; |
572 | 0 | else |
573 | 0 | cfx.dek->use_mdc = !!use_mdc (NULL, cfx.dek->algo); |
574 | |
|
575 | 0 | if (opt.verbose) |
576 | 0 | log_info(_("using cipher %s.%s\n"), |
577 | 0 | openpgp_cipher_algo_name (cfx.dek->algo), |
578 | 0 | cfx.dek->use_aead? openpgp_aead_algo_name (cfx.dek->use_aead) |
579 | 0 | /**/ : "CFB"); |
580 | 0 | } |
581 | | |
582 | 0 | if (rc || (rc = open_outfile (GNUPG_INVALID_FD, filename, opt.armor? 1:0, |
583 | 0 | 0, &out ))) |
584 | 0 | { |
585 | 0 | iobuf_cancel (inp); |
586 | 0 | xfree (cfx.dek); |
587 | 0 | xfree (s2k); |
588 | 0 | release_progress_context (pfx); |
589 | 0 | return rc; |
590 | 0 | } |
591 | | |
592 | 0 | if ( opt.armor ) |
593 | 0 | { |
594 | 0 | afx = new_armor_context (); |
595 | 0 | push_armor_filter (afx, out); |
596 | 0 | } |
597 | |
|
598 | 0 | if ( s2k ) |
599 | 0 | { |
600 | | /* Fixme: This is quite similar to write_symkey_enc. */ |
601 | 0 | PKT_symkey_enc *enc = xmalloc_clear (sizeof *enc); |
602 | 0 | enc->version = cfx.dek->use_aead ? 5 : 4; |
603 | 0 | enc->cipher_algo = cfx.dek->algo; |
604 | 0 | enc->aead_algo = cfx.dek->use_aead; |
605 | 0 | enc->s2k = *s2k; |
606 | 0 | if (enckeylen) |
607 | 0 | { |
608 | 0 | enc->seskeylen = enckeylen; |
609 | 0 | enc->seskey = xmalloc (enckeylen); |
610 | 0 | memcpy (enc->seskey, enckey, enckeylen); |
611 | 0 | } |
612 | 0 | pkt.pkttype = PKT_SYMKEY_ENC; |
613 | 0 | pkt.pkt.symkey_enc = enc; |
614 | 0 | if ((rc = build_packet( out, &pkt ))) |
615 | 0 | log_error("build symkey packet failed: %s\n", gpg_strerror (rc) ); |
616 | 0 | free_symkey_enc (enc); |
617 | 0 | xfree (enckey); |
618 | 0 | enckey = NULL; |
619 | 0 | } |
620 | |
|
621 | 0 | if (!opt.no_literal) |
622 | 0 | pt = setup_plaintext_name (filename, inp); |
623 | | |
624 | | /* Note that PGP 5 has problems decrypting symmetrically encrypted |
625 | | data if the file length is in the inner packet. It works when |
626 | | only partial length headers are use. In the past, we always used |
627 | | partial body length here, but since PGP 2, PGP 6, and PGP 7 need |
628 | | the file length, and nobody should be using PGP 5 nowadays |
629 | | anyway, this is now set to the file length. Note also that this |
630 | | only applies to the RFC-1991 style symmetric messages, and not |
631 | | the RFC-2440 style. PGP 6 and 7 work with either partial length |
632 | | or fixed length with the new style messages. */ |
633 | |
|
634 | 0 | if ( !iobuf_is_pipe_filename (filename) && *filename && !opt.textmode ) |
635 | 0 | { |
636 | 0 | uint64_t tmpsize; |
637 | |
|
638 | 0 | tmpsize = iobuf_get_filelength(inp); |
639 | 0 | if (!tmpsize && opt.verbose) |
640 | 0 | log_info(_("WARNING: '%s' is an empty file\n"), filename ); |
641 | | |
642 | | /* We can't encode the length of very large files because |
643 | | OpenPGP uses only 32 bit for file sizes. So if the |
644 | | size of a file is larger than 2^32 minus some bytes for |
645 | | packet headers, we switch to partial length encoding. */ |
646 | 0 | if ( tmpsize < (IOBUF_FILELENGTH_LIMIT - 65536) ) |
647 | 0 | filesize = tmpsize; |
648 | 0 | else |
649 | 0 | filesize = 0; |
650 | 0 | } |
651 | 0 | else |
652 | 0 | filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */ |
653 | | |
654 | | /* Register the cipher filter. */ |
655 | 0 | if (mode) |
656 | 0 | iobuf_push_filter (out, |
657 | 0 | cfx.dek->use_aead? cipher_filter_aead |
658 | 0 | /**/ : cipher_filter_cfb, |
659 | 0 | &cfx ); |
660 | |
|
661 | 0 | if (do_compress |
662 | 0 | && cfx.dek |
663 | 0 | && (cfx.dek->use_mdc || cfx.dek->use_aead) |
664 | 0 | && !opt.explicit_compress_option |
665 | 0 | && is_file_compressed (inp)) |
666 | 0 | { |
667 | 0 | if (opt.verbose) |
668 | 0 | log_info(_("'%s' already compressed\n"), filename? filename: "[stdin]"); |
669 | 0 | do_compress = 0; |
670 | 0 | } |
671 | |
|
672 | 0 | if (!opt.no_literal) |
673 | 0 | { |
674 | | /* Note that PT has been initialized above in !no_literal mode. */ |
675 | 0 | pt->timestamp = make_timestamp(); |
676 | 0 | pt->mode = opt.mimemode? 'm' : opt.textmode? 't' : 'b'; |
677 | 0 | pt->len = filesize; |
678 | 0 | pt->new_ctb = !pt->len; |
679 | 0 | pt->buf = inp; |
680 | 0 | pkt.pkttype = PKT_PLAINTEXT; |
681 | 0 | pkt.pkt.plaintext = pt; |
682 | 0 | cfx.datalen = filesize && !do_compress ? calc_packet_length( &pkt ) : 0; |
683 | 0 | } |
684 | 0 | else |
685 | 0 | { |
686 | 0 | cfx.datalen = filesize && !do_compress ? filesize : 0; |
687 | 0 | pkt.pkttype = 0; |
688 | 0 | pkt.pkt.generic = NULL; |
689 | 0 | } |
690 | | |
691 | | /* Register the compress filter. */ |
692 | 0 | if ( do_compress ) |
693 | 0 | { |
694 | 0 | if (cfx.dek && (cfx.dek->use_mdc || cfx.dek->use_aead)) |
695 | 0 | zfx.new_ctb = 1; |
696 | 0 | push_compress_filter (out, &zfx, default_compress_algo()); |
697 | 0 | } |
698 | | |
699 | | /* Do the work. */ |
700 | 0 | if (!opt.no_literal) |
701 | 0 | { |
702 | 0 | if ( (rc = build_packet( out, &pkt )) ) |
703 | 0 | log_error("build_packet failed: %s\n", gpg_strerror (rc) ); |
704 | 0 | } |
705 | 0 | else |
706 | 0 | { |
707 | | /* User requested not to create a literal packet, so we copy the |
708 | | plain data. */ |
709 | 0 | iobuf_copy (out, inp); |
710 | 0 | if ((rc = iobuf_error (inp))) |
711 | 0 | log_error (_("error reading '%s': %s\n"), |
712 | 0 | iobuf_get_fname_nonnull (inp), gpg_strerror (rc)); |
713 | 0 | else if ((rc = iobuf_error (out))) |
714 | 0 | log_error (_("error writing '%s': %s\n"), |
715 | 0 | iobuf_get_fname_nonnull (out), gpg_strerror (rc)); |
716 | 0 | } |
717 | | |
718 | | /* Finish the stuff. */ |
719 | 0 | iobuf_close (inp); |
720 | 0 | if (rc) |
721 | 0 | iobuf_cancel(out); |
722 | 0 | else |
723 | 0 | { |
724 | 0 | iobuf_close (out); /* fixme: check returncode */ |
725 | 0 | if (mode) |
726 | 0 | write_status ( STATUS_END_ENCRYPTION ); |
727 | 0 | } |
728 | 0 | if (pt) |
729 | 0 | pt->buf = NULL; |
730 | 0 | free_packet (&pkt, NULL); |
731 | 0 | xfree (enckey); |
732 | 0 | xfree (cfx.dek); |
733 | 0 | xfree (s2k); |
734 | 0 | release_armor_context (afx); |
735 | 0 | release_progress_context (pfx); |
736 | 0 | return rc; |
737 | 0 | } |
738 | | |
739 | | |
740 | | gpg_error_t |
741 | | setup_symkey (STRING2KEY **symkey_s2k, DEK **symkey_dek) |
742 | 0 | { |
743 | 0 | int canceled; |
744 | 0 | int defcipher; |
745 | 0 | int s2kdigest; |
746 | |
|
747 | 0 | defcipher = default_cipher_algo (); |
748 | 0 | if (openpgp_cipher_blocklen (defcipher) < 16 |
749 | 0 | && !opt.flags.allow_old_cipher_algos) |
750 | 0 | { |
751 | 0 | log_error (_("cipher algorithm '%s' may not be used for encryption\n"), |
752 | 0 | openpgp_cipher_algo_name (defcipher)); |
753 | 0 | if (!opt.quiet) |
754 | 0 | log_info (_("(use option \"%s\" to override)\n"), |
755 | 0 | "--allow-old-cipher-algos"); |
756 | 0 | return gpg_error (GPG_ERR_CIPHER_ALGO); |
757 | 0 | } |
758 | | |
759 | 0 | if (!gnupg_cipher_is_allowed (opt.compliance, 1, defcipher, |
760 | 0 | GCRY_CIPHER_MODE_CFB)) |
761 | 0 | { |
762 | 0 | log_error (_("cipher algorithm '%s' may not be used in %s mode\n"), |
763 | 0 | openpgp_cipher_algo_name (defcipher), |
764 | 0 | gnupg_compliance_option_string (opt.compliance)); |
765 | 0 | return gpg_error (GPG_ERR_CIPHER_ALGO); |
766 | 0 | } |
767 | | |
768 | 0 | s2kdigest = S2K_DIGEST_ALGO; |
769 | 0 | if (!gnupg_digest_is_allowed (opt.compliance, 1, s2kdigest)) |
770 | 0 | { |
771 | 0 | log_error (_("digest algorithm '%s' may not be used in %s mode\n"), |
772 | 0 | gcry_md_algo_name (s2kdigest), |
773 | 0 | gnupg_compliance_option_string (opt.compliance)); |
774 | 0 | return gpg_error (GPG_ERR_DIGEST_ALGO); |
775 | 0 | } |
776 | | |
777 | 0 | *symkey_s2k = xmalloc_clear (sizeof **symkey_s2k); |
778 | 0 | (*symkey_s2k)->mode = opt.s2k_mode; |
779 | 0 | (*symkey_s2k)->hash_algo = s2kdigest; |
780 | |
|
781 | 0 | *symkey_dek = passphrase_to_dek (defcipher, |
782 | 0 | *symkey_s2k, 1, 0, NULL, 0, &canceled); |
783 | 0 | if (!*symkey_dek || !(*symkey_dek)->keylen) |
784 | 0 | { |
785 | 0 | xfree(*symkey_dek); |
786 | 0 | xfree(*symkey_s2k); |
787 | 0 | return gpg_error (canceled?GPG_ERR_CANCELED:GPG_ERR_INV_PASSPHRASE); |
788 | 0 | } |
789 | | |
790 | 0 | return 0; |
791 | 0 | } |
792 | | |
793 | | |
794 | | static int |
795 | | write_symkey_enc (STRING2KEY *symkey_s2k, aead_algo_t aead_algo, |
796 | | DEK *symkey_dek, DEK *dek, iobuf_t out) |
797 | 0 | { |
798 | 0 | int rc; |
799 | 0 | void *enckey; |
800 | 0 | size_t enckeylen; |
801 | 0 | PKT_symkey_enc *enc; |
802 | 0 | PACKET pkt; |
803 | |
|
804 | 0 | rc = encrypt_seskey (symkey_dek, aead_algo, &dek, &enckey, &enckeylen); |
805 | 0 | if (rc) |
806 | 0 | return rc; |
807 | 0 | enc = xtrycalloc (1, sizeof (PKT_symkey_enc)); |
808 | 0 | if (!enc) |
809 | 0 | { |
810 | 0 | rc = gpg_error_from_syserror (); |
811 | 0 | xfree (enckey); |
812 | 0 | return rc; |
813 | 0 | } |
814 | | |
815 | 0 | enc->version = aead_algo? 5 : 4; |
816 | 0 | enc->cipher_algo = opt.s2k_cipher_algo; |
817 | 0 | enc->aead_algo = aead_algo; |
818 | 0 | enc->s2k = *symkey_s2k; |
819 | 0 | enc->seskeylen = enckeylen; |
820 | 0 | enc->seskey = xtrymalloc (enckeylen); |
821 | 0 | if (!enc->seskey) |
822 | 0 | { |
823 | 0 | rc = gpg_error_from_syserror (); |
824 | 0 | xfree (enc); |
825 | 0 | xfree (enckey); |
826 | 0 | return rc; |
827 | 0 | } |
828 | 0 | memcpy (enc->seskey, enckey, enckeylen); |
829 | 0 | xfree (enckey); |
830 | |
|
831 | 0 | pkt.pkttype = PKT_SYMKEY_ENC; |
832 | 0 | pkt.pkt.symkey_enc = enc; |
833 | |
|
834 | 0 | if ((rc=build_packet(out,&pkt))) |
835 | 0 | log_error("build symkey_enc packet failed: %s\n",gpg_strerror (rc)); |
836 | |
|
837 | 0 | free_symkey_enc (enc); |
838 | 0 | return rc; |
839 | 0 | } |
840 | | |
841 | | |
842 | | /* |
843 | | * Encrypt the file with the given userids (or ask if none is |
844 | | * supplied). Either FILENAME or FILEFD must be given, but not both. |
845 | | * The caller may provide a checked list of public keys in |
846 | | * PROVIDED_KEYS; if not the function builds a list of keys on its own. |
847 | | * |
848 | | * Note that FILEFD and OUTPUTFD are currently only used by |
849 | | * cmd_encrypt in the not yet finished server.c. |
850 | | */ |
851 | | int |
852 | | encrypt_crypt (ctrl_t ctrl, gnupg_fd_t filefd, const char *filename, |
853 | | strlist_t remusr, int use_symkey, pk_list_t provided_keys, |
854 | | gnupg_fd_t outputfd) |
855 | 0 | { |
856 | 0 | iobuf_t inp = NULL; |
857 | 0 | iobuf_t out = NULL; |
858 | 0 | PACKET pkt; |
859 | 0 | PKT_plaintext *pt = NULL; |
860 | 0 | DEK *symkey_dek = NULL; |
861 | 0 | STRING2KEY *symkey_s2k = NULL; |
862 | 0 | int rc = 0; |
863 | 0 | u32 filesize; |
864 | 0 | cipher_filter_context_t cfx; |
865 | 0 | armor_filter_context_t *afx = NULL; |
866 | 0 | compress_filter_context_t zfx; |
867 | 0 | text_filter_context_t tfx; |
868 | 0 | progress_filter_context_t *pfx; |
869 | 0 | PK_LIST pk_list; |
870 | 0 | int do_compress; |
871 | |
|
872 | 0 | if (filefd != GNUPG_INVALID_FD && filename) |
873 | 0 | return gpg_error (GPG_ERR_INV_ARG); /* Both given. */ |
874 | | |
875 | 0 | do_compress = !!opt.compress_algo; |
876 | |
|
877 | 0 | pfx = new_progress_context (); |
878 | 0 | memset( &cfx, 0, sizeof cfx); |
879 | 0 | memset( &zfx, 0, sizeof zfx); |
880 | 0 | memset( &tfx, 0, sizeof tfx); |
881 | 0 | init_packet(&pkt); |
882 | |
|
883 | 0 | if (use_symkey |
884 | 0 | && (rc=setup_symkey(&symkey_s2k,&symkey_dek))) |
885 | 0 | { |
886 | 0 | release_progress_context (pfx); |
887 | 0 | return rc; |
888 | 0 | } |
889 | | |
890 | 0 | if (provided_keys) |
891 | 0 | pk_list = provided_keys; |
892 | 0 | else |
893 | 0 | { |
894 | 0 | if ((rc = build_pk_list (ctrl, remusr, &pk_list))) |
895 | 0 | { |
896 | 0 | release_progress_context (pfx); |
897 | 0 | return rc; |
898 | 0 | } |
899 | 0 | } |
900 | | |
901 | | /* Prepare iobufs. */ |
902 | | #ifdef HAVE_W32_SYSTEM |
903 | | if (filefd == GNUPG_INVALID_FD) |
904 | | inp = iobuf_open (filename); |
905 | | else |
906 | | { |
907 | | inp = NULL; |
908 | | gpg_err_set_errno (ENOSYS); |
909 | | } |
910 | | #else |
911 | 0 | if (filefd == GNUPG_INVALID_FD) |
912 | 0 | inp = iobuf_open (filename); |
913 | 0 | else |
914 | 0 | inp = iobuf_fdopen_nc (filefd, "rb"); |
915 | 0 | #endif |
916 | 0 | if (inp) |
917 | 0 | iobuf_ioctl (inp, IOBUF_IOCTL_NO_CACHE, 1, NULL); |
918 | 0 | if (inp && is_secured_file (iobuf_get_fd (inp))) |
919 | 0 | { |
920 | 0 | iobuf_close (inp); |
921 | 0 | inp = NULL; |
922 | 0 | gpg_err_set_errno (EPERM); |
923 | 0 | } |
924 | 0 | if (!inp) |
925 | 0 | { |
926 | 0 | char xname[64]; |
927 | |
|
928 | 0 | rc = gpg_error_from_syserror (); |
929 | 0 | if (filefd != GNUPG_INVALID_FD) |
930 | 0 | snprintf (xname, sizeof xname, "[fd %d]", FD_DBG (filefd)); |
931 | 0 | else if (!filename) |
932 | 0 | strcpy (xname, "[stdin]"); |
933 | 0 | else |
934 | 0 | *xname = 0; |
935 | 0 | log_error (_("can't open '%s': %s\n"), |
936 | 0 | *xname? xname : filename, gpg_strerror (rc) ); |
937 | 0 | goto leave; |
938 | 0 | } |
939 | | |
940 | 0 | if (opt.verbose) |
941 | 0 | log_info (_("reading from '%s'\n"), iobuf_get_fname_nonnull (inp)); |
942 | |
|
943 | 0 | handle_progress (pfx, inp, filename); |
944 | |
|
945 | 0 | if (opt.textmode) |
946 | 0 | iobuf_push_filter (inp, text_filter, &tfx); |
947 | |
|
948 | 0 | rc = open_outfile (outputfd, filename, opt.armor? 1:0, 0, &out); |
949 | 0 | if (rc) |
950 | 0 | goto leave; |
951 | | |
952 | 0 | if (opt.armor) |
953 | 0 | { |
954 | 0 | afx = new_armor_context (); |
955 | 0 | push_armor_filter (afx, out); |
956 | 0 | } |
957 | | |
958 | | /* Create a session key. */ |
959 | 0 | cfx.dek = create_dek_with_warnings (pk_list); |
960 | |
|
961 | 0 | rc = check_encryption_compliance (cfx.dek, pk_list); |
962 | 0 | if (rc) |
963 | 0 | goto leave; |
964 | | |
965 | 0 | cfx.dek->use_aead = use_aead (pk_list, cfx.dek->algo); |
966 | 0 | if (!cfx.dek->use_aead) |
967 | 0 | cfx.dek->use_mdc = !!use_mdc (pk_list, cfx.dek->algo); |
968 | 0 | else if (use_rfc9980_seipdv2 (pk_list)) |
969 | 0 | cfx.seipdv2 = 1; /* Use SEIPDV2 and not the OCB.*/ |
970 | |
|
971 | 0 | make_session_key (cfx.dek); |
972 | 0 | if (DBG_CRYPTO) |
973 | 0 | log_printhex (cfx.dek->key, cfx.dek->keylen, "DEK is: "); |
974 | |
|
975 | 0 | rc = write_pubkey_enc_from_list (ctrl, pk_list, cfx.dek, out, NULL); |
976 | 0 | if (rc) |
977 | 0 | goto leave; |
978 | | |
979 | | /* We put the passphrase (if any) after any public keys as this |
980 | | * seems to be the most useful on the recipient side - there is no |
981 | | * point in prompting a user for a passphrase if they have the |
982 | | * secret key needed to decrypt. */ |
983 | 0 | if (use_symkey && (rc = write_symkey_enc (symkey_s2k, cfx.dek->use_aead, |
984 | 0 | symkey_dek, cfx.dek, out))) |
985 | 0 | goto leave; |
986 | | |
987 | 0 | if (!opt.no_literal) |
988 | 0 | pt = setup_plaintext_name (filename, inp); |
989 | | |
990 | | /* Get the size of the file if possible, i.e., if it is a real file. */ |
991 | 0 | if (filename && *filename |
992 | 0 | && !iobuf_is_pipe_filename (filename) && !opt.textmode ) |
993 | 0 | { |
994 | 0 | uint64_t tmpsize; |
995 | |
|
996 | 0 | tmpsize = iobuf_get_filelength (inp); |
997 | 0 | if (!tmpsize && opt.verbose) |
998 | 0 | log_info(_("WARNING: '%s' is an empty file\n"), filename ); |
999 | | /* We can't encode the length of very large files because |
1000 | | OpenPGP uses only 32 bit for file sizes. So if the size |
1001 | | of a file is larger than 2^32 minus some bytes for packet |
1002 | | headers, we switch to partial length encoding. */ |
1003 | 0 | if (tmpsize < (IOBUF_FILELENGTH_LIMIT - 65536) ) |
1004 | 0 | filesize = tmpsize; |
1005 | 0 | else |
1006 | 0 | filesize = 0; |
1007 | 0 | } |
1008 | 0 | else |
1009 | 0 | filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */ |
1010 | |
|
1011 | 0 | if (cfx.seipdv2) |
1012 | 0 | log_info (_("Note: Using the %s encryption packet\n"), "RFC-9580"); |
1013 | | |
1014 | | /* Register the cipher filter. */ |
1015 | 0 | iobuf_push_filter (out, |
1016 | 0 | cfx.dek->use_aead? cipher_filter_aead |
1017 | 0 | /**/ : cipher_filter_cfb, |
1018 | 0 | &cfx); |
1019 | | |
1020 | | /* Only do the is-file-already-compressed check if we are using a |
1021 | | * MDC or AEAD. This forces compressed files to be re-compressed if |
1022 | | * we do not have a MDC to give some protection against chosen |
1023 | | * ciphertext attacks. */ |
1024 | 0 | if (do_compress |
1025 | 0 | && (cfx.dek->use_mdc || cfx.dek->use_aead) |
1026 | 0 | && !opt.explicit_compress_option |
1027 | 0 | && is_file_compressed (inp)) |
1028 | 0 | { |
1029 | 0 | if (opt.verbose) |
1030 | 0 | log_info(_("'%s' already compressed\n"), filename? filename: "[stdin]"); |
1031 | 0 | do_compress = 0; |
1032 | 0 | } |
1033 | |
|
1034 | 0 | if (!opt.no_literal) |
1035 | 0 | { |
1036 | 0 | pt->timestamp = make_timestamp(); |
1037 | 0 | pt->mode = opt.mimemode? 'm' : opt.textmode ? 't' : 'b'; |
1038 | 0 | pt->len = filesize; |
1039 | 0 | pt->new_ctb = !pt->len; |
1040 | 0 | pt->buf = inp; |
1041 | 0 | pkt.pkttype = PKT_PLAINTEXT; |
1042 | 0 | pkt.pkt.plaintext = pt; |
1043 | 0 | cfx.datalen = filesize && !do_compress? calc_packet_length( &pkt ) : 0; |
1044 | 0 | } |
1045 | 0 | else |
1046 | 0 | cfx.datalen = filesize && !do_compress ? filesize : 0; |
1047 | | |
1048 | | /* Register the compress filter. */ |
1049 | 0 | if (do_compress) |
1050 | 0 | { |
1051 | 0 | int compr_algo = opt.compress_algo; |
1052 | |
|
1053 | 0 | if (compr_algo == -1) |
1054 | 0 | { |
1055 | 0 | compr_algo = select_algo_from_prefs (pk_list, PREFTYPE_ZIP, -1, NULL); |
1056 | 0 | if (compr_algo == -1) |
1057 | 0 | compr_algo = DEFAULT_COMPRESS_ALGO; |
1058 | | /* Theoretically impossible to get here since uncompressed |
1059 | | is implicit. */ |
1060 | 0 | } |
1061 | 0 | else if (!opt.expert |
1062 | 0 | && select_algo_from_prefs(pk_list, PREFTYPE_ZIP, |
1063 | 0 | compr_algo, NULL) != compr_algo) |
1064 | 0 | { |
1065 | 0 | log_info (_("WARNING: forcing compression algorithm %s (%d)" |
1066 | 0 | " violates recipient preferences\n"), |
1067 | 0 | compress_algo_to_string(compr_algo), compr_algo); |
1068 | 0 | } |
1069 | | |
1070 | | /* Algo 0 means no compression. */ |
1071 | 0 | if (compr_algo) |
1072 | 0 | { |
1073 | 0 | if (cfx.dek && (cfx.dek->use_mdc || cfx.dek->use_aead)) |
1074 | 0 | zfx.new_ctb = 1; |
1075 | 0 | push_compress_filter (out,&zfx,compr_algo); |
1076 | 0 | } |
1077 | 0 | } |
1078 | | |
1079 | | /* Do the work. */ |
1080 | 0 | if (!opt.no_literal) |
1081 | 0 | { |
1082 | 0 | if ((rc = build_packet( out, &pkt ))) |
1083 | 0 | log_error ("build_packet failed: %s\n", gpg_strerror (rc)); |
1084 | 0 | } |
1085 | 0 | else |
1086 | 0 | { |
1087 | | /* User requested not to create a literal packet, so we copy the |
1088 | | plain data. */ |
1089 | 0 | iobuf_copy (out, inp); |
1090 | 0 | if ((rc = iobuf_error (inp))) |
1091 | 0 | log_error (_("error reading '%s': %s\n"), |
1092 | 0 | iobuf_get_fname_nonnull (inp), gpg_strerror (rc)); |
1093 | 0 | else if ((rc = iobuf_error (out))) |
1094 | 0 | log_error (_("error writing '%s': %s\n"), |
1095 | 0 | iobuf_get_fname_nonnull (out), gpg_strerror (rc)); |
1096 | |
|
1097 | 0 | } |
1098 | | |
1099 | | /* Finish the stuff. */ |
1100 | 0 | leave: |
1101 | 0 | iobuf_close (inp); |
1102 | 0 | if (rc) |
1103 | 0 | iobuf_cancel (out); |
1104 | 0 | else |
1105 | 0 | { |
1106 | 0 | iobuf_close (out); /* fixme: check returncode */ |
1107 | 0 | write_status (STATUS_END_ENCRYPTION); |
1108 | 0 | } |
1109 | 0 | if (pt) |
1110 | 0 | pt->buf = NULL; |
1111 | 0 | free_packet (&pkt, NULL); |
1112 | 0 | xfree (cfx.dek); |
1113 | 0 | xfree (symkey_dek); |
1114 | 0 | xfree (symkey_s2k); |
1115 | 0 | if (!provided_keys) |
1116 | 0 | release_pk_list (pk_list); |
1117 | 0 | release_armor_context (afx); |
1118 | 0 | release_progress_context (pfx); |
1119 | 0 | return rc; |
1120 | 0 | } |
1121 | | |
1122 | | |
1123 | | /* Re-encrypt files with a set of new recipients. Note that this |
1124 | | * function is called by decrypt_message. INFP is the iobuf from the |
1125 | | * input file which is positioned right after the pubkey_enc and |
1126 | | * symkey_enc packets. */ |
1127 | | gpg_error_t |
1128 | | reencrypt_to_new_recipients (ctrl_t ctrl, int armor, const char *filename, |
1129 | | iobuf_t infp, strlist_t recipients, |
1130 | | DEK *dek, struct seskey_enc_list *sesenc_list) |
1131 | 0 | { |
1132 | 0 | gpg_error_t err; |
1133 | 0 | int save_no_encrypt_to; |
1134 | 0 | pk_list_t newpk_list = NULL; |
1135 | 0 | struct pubkey_enc_info_item *restrict_pk_list = NULL; |
1136 | 0 | struct pubkey_enc_info_item *pkei; /* Iterator */ |
1137 | 0 | iobuf_t outfp = NULL; |
1138 | 0 | armor_filter_context_t *outafx = NULL; |
1139 | 0 | PACKET pkt; |
1140 | 0 | struct seskey_enc_list *el; |
1141 | 0 | unsigned int count; |
1142 | | |
1143 | | /* Unless we want to clear the recipients, record the pubkey encrypt |
1144 | | * infos so hat we can avoid to double encrypt to the same |
1145 | | * recipient. We can't do that for wildcards, though. */ |
1146 | 0 | if (!ctrl->clear_recipients) |
1147 | 0 | { |
1148 | 0 | for (el = sesenc_list; el; el = el->next) |
1149 | 0 | { |
1150 | 0 | if (el->u_sym) |
1151 | 0 | continue; |
1152 | 0 | if (!el->u.pub.keyid[0] && !el->u.pub.keyid[1]) |
1153 | 0 | continue; /* Wildcard encrypt - no useful info. */ |
1154 | 0 | pkei = xcalloc (1, sizeof *pkei); |
1155 | 0 | pkei->keyid[0] = el->u.pub.keyid[0]; |
1156 | 0 | pkei->keyid[1] = el->u.pub.keyid[1]; |
1157 | 0 | pkei->version = el->u.pub.version; |
1158 | 0 | pkei->pubkey_algo = el->u.pub.pubkey_algo; |
1159 | 0 | pkei->next = restrict_pk_list; |
1160 | 0 | restrict_pk_list = pkei; |
1161 | 0 | } |
1162 | 0 | } |
1163 | | |
1164 | | /* Get the keys for all additional recipients but do not encrypt to |
1165 | | * the encrypt-to keys. */ |
1166 | 0 | save_no_encrypt_to = opt.no_encrypt_to; |
1167 | 0 | opt.no_encrypt_to = 1; |
1168 | 0 | err = build_pk_list (ctrl, recipients, &newpk_list); |
1169 | 0 | opt.no_encrypt_to = save_no_encrypt_to; |
1170 | 0 | if (err) |
1171 | 0 | goto leave; |
1172 | | |
1173 | | /* Note that we use by default the suffixes .gpg or .asc */ |
1174 | 0 | err = open_outfile (GNUPG_INVALID_FD, filename, armor? 1:0, 0, &outfp); |
1175 | 0 | if (err) |
1176 | 0 | goto leave; |
1177 | | |
1178 | 0 | if (armor) |
1179 | 0 | { |
1180 | 0 | outafx = new_armor_context (); |
1181 | 0 | push_armor_filter (outafx, outfp); |
1182 | 0 | } |
1183 | | |
1184 | | /* Write the new recipients first. */ |
1185 | 0 | err = write_pubkey_enc_from_list (ctrl, newpk_list, dek, outfp, |
1186 | 0 | restrict_pk_list); |
1187 | 0 | if (err) |
1188 | 0 | goto leave; |
1189 | | |
1190 | | /* Write the old recipients in --add-recipients mode. */ |
1191 | 0 | for (count=0, el = sesenc_list; el; el = el->next, count++) |
1192 | 0 | if (!ctrl->clear_recipients && !el->u_sym) |
1193 | 0 | { |
1194 | 0 | if (opt.verbose) |
1195 | 0 | show_encrypted_for_user_info (ctrl, 0, &el->u.pub, dek); |
1196 | 0 | init_packet (&pkt); |
1197 | 0 | pkt.pkttype = PKT_PUBKEY_ENC; |
1198 | 0 | pkt.pkt.pubkey_enc = &el->u.pub; |
1199 | 0 | err = build_packet (outfp, &pkt); |
1200 | 0 | if (err) |
1201 | 0 | log_error ("build_packet(pubkey_enc) failed: %s\n", |
1202 | 0 | gpg_strerror (err)); |
1203 | 0 | } |
1204 | 0 | if (ctrl->clear_recipients && opt.verbose) |
1205 | 0 | log_info (_("number of removed recipients: %u\n"), count); |
1206 | |
|
1207 | 0 | iobuf_put (outfp, ctrl->last_read_ctb); |
1208 | | |
1209 | | /* Finally copy the bulk of the message. */ |
1210 | 0 | iobuf_copy (outfp, infp); |
1211 | 0 | if ((err = iobuf_error (infp))) |
1212 | 0 | log_error (_("error reading '%s': %s\n"), |
1213 | 0 | iobuf_get_fname_nonnull (infp), gpg_strerror (err)); |
1214 | 0 | else if ((err = iobuf_error (outfp))) |
1215 | 0 | log_error (_("error writing '%s': %s\n"), |
1216 | 0 | iobuf_get_fname_nonnull (outfp), gpg_strerror (err)); |
1217 | | |
1218 | |
|
1219 | 0 | leave: |
1220 | 0 | if (err) |
1221 | 0 | iobuf_cancel (outfp); |
1222 | 0 | else |
1223 | 0 | iobuf_close (outfp); |
1224 | 0 | release_armor_context (outafx); |
1225 | 0 | release_pk_list (newpk_list); |
1226 | 0 | while (restrict_pk_list) |
1227 | 0 | { |
1228 | 0 | pkei = restrict_pk_list->next; |
1229 | 0 | xfree (restrict_pk_list); |
1230 | 0 | restrict_pk_list = pkei; |
1231 | 0 | } |
1232 | 0 | return err; |
1233 | 0 | } |
1234 | | |
1235 | | |
1236 | | |
1237 | | /* |
1238 | | * Filter to do a complete public key encryption. |
1239 | | */ |
1240 | | int |
1241 | | encrypt_filter (void *opaque, int control, |
1242 | | iobuf_t a, byte *buf, size_t *ret_len) |
1243 | 0 | { |
1244 | 0 | size_t size = *ret_len; |
1245 | 0 | encrypt_filter_context_t *efx = opaque; |
1246 | 0 | int rc = 0; |
1247 | |
|
1248 | 0 | if (control == IOBUFCTRL_UNDERFLOW) /* decrypt */ |
1249 | 0 | { |
1250 | 0 | BUG(); /* not used */ |
1251 | 0 | } |
1252 | 0 | else if ( control == IOBUFCTRL_FLUSH ) /* encrypt */ |
1253 | 0 | { |
1254 | 0 | if ( !efx->header_okay ) |
1255 | 0 | { |
1256 | 0 | efx->header_okay = 1; |
1257 | | |
1258 | | /* Fixme: The core functionality is duplicated from |
1259 | | * encrypt_crypt. We should use common functions. */ |
1260 | 0 | efx->cfx.dek = create_dek_with_warnings (efx->pk_list); |
1261 | |
|
1262 | 0 | rc = check_encryption_compliance (efx->cfx.dek, efx->pk_list); |
1263 | 0 | if (rc) |
1264 | 0 | return rc; |
1265 | | |
1266 | 0 | efx->cfx.dek->use_aead = use_aead (efx->pk_list, efx->cfx.dek->algo); |
1267 | 0 | if (!efx->cfx.dek->use_aead) |
1268 | 0 | efx->cfx.dek->use_mdc = !!use_mdc (efx->pk_list,efx->cfx.dek->algo); |
1269 | 0 | else if (use_rfc9980_seipdv2 (efx->pk_list)) |
1270 | 0 | efx->cfx.seipdv2 = 1; /* Use SEIPDV2 and not the OCB.*/ |
1271 | |
|
1272 | 0 | make_session_key ( efx->cfx.dek ); |
1273 | 0 | if (DBG_CRYPTO) |
1274 | 0 | log_printhex (efx->cfx.dek->key, efx->cfx.dek->keylen, "DEK is: "); |
1275 | |
|
1276 | 0 | rc = write_pubkey_enc_from_list (efx->ctrl, |
1277 | 0 | efx->pk_list, efx->cfx.dek, a, NULL); |
1278 | 0 | if (rc) |
1279 | 0 | return rc; |
1280 | | |
1281 | 0 | if(efx->symkey_s2k && efx->symkey_dek) |
1282 | 0 | { |
1283 | 0 | rc = write_symkey_enc (efx->symkey_s2k, efx->cfx.dek->use_aead, |
1284 | 0 | efx->symkey_dek, efx->cfx.dek, a); |
1285 | 0 | if (rc) |
1286 | 0 | return rc; |
1287 | 0 | } |
1288 | | |
1289 | 0 | if (efx->cfx.seipdv2) |
1290 | 0 | log_info (_("Note: Using the %s encryption packet\n"), "RFC-9580"); |
1291 | |
|
1292 | 0 | iobuf_push_filter (a, |
1293 | 0 | efx->cfx.dek->use_aead? cipher_filter_aead |
1294 | 0 | /**/ : cipher_filter_cfb, |
1295 | 0 | &efx->cfx); |
1296 | |
|
1297 | 0 | } |
1298 | 0 | rc = iobuf_write (a, buf, size); |
1299 | |
|
1300 | 0 | } |
1301 | 0 | else if (control == IOBUFCTRL_FREE) |
1302 | 0 | { |
1303 | 0 | xfree (efx->symkey_dek); |
1304 | 0 | xfree (efx->symkey_s2k); |
1305 | 0 | } |
1306 | 0 | else if ( control == IOBUFCTRL_DESC ) |
1307 | 0 | { |
1308 | 0 | mem2str (buf, "encrypt_filter", *ret_len); |
1309 | 0 | } |
1310 | 0 | return rc; |
1311 | 0 | } |
1312 | | |
1313 | | |
1314 | | /* |
1315 | | * Write a pubkey-enc packet for the public key PK to OUT. |
1316 | | */ |
1317 | | int |
1318 | | write_pubkey_enc (ctrl_t ctrl, |
1319 | | PKT_public_key *pk, int throw_keyid, DEK *dek, iobuf_t out) |
1320 | 0 | { |
1321 | 0 | PACKET pkt; |
1322 | 0 | PKT_pubkey_enc *enc; |
1323 | 0 | int rc; |
1324 | 0 | gcry_mpi_t frame; |
1325 | 0 | int is_rfc9980; |
1326 | 0 | size_t fprlen; |
1327 | |
|
1328 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_X25519 |
1329 | 0 | || IS_PUBKEY_ALGO_MLK (pk->pubkey_algo)) |
1330 | 0 | is_rfc9980 = 1; |
1331 | 0 | else |
1332 | 0 | is_rfc9980 = 0; |
1333 | |
|
1334 | 0 | print_pubkey_algo_note ( pk->pubkey_algo ); |
1335 | 0 | enc = xmalloc_clear ( sizeof *enc ); |
1336 | 0 | enc->pubkey_algo = pk->pubkey_algo; |
1337 | 0 | keyid_from_pk (pk, enc->keyid); |
1338 | 0 | enc->throw_keyid = throw_keyid; |
1339 | 0 | enc->seskey_algo = dek->algo; /* (Used only by PUBKEY_ALGO_KYBER.) */ |
1340 | 0 | if (is_rfc9980) |
1341 | 0 | { |
1342 | 0 | enc->version = 6; |
1343 | 0 | fingerprint_from_pk (pk, enc->fpr, &fprlen); |
1344 | 0 | log_assert (fprlen == 20 || fprlen == 32); |
1345 | 0 | enc->fprlen = fprlen; |
1346 | 0 | } |
1347 | | |
1348 | | /* Okay, what's going on: We have the session key somewhere in |
1349 | | * the structure DEK and want to encode this session key in an |
1350 | | * integer value of n bits. pubkey_nbits gives us the number of |
1351 | | * bits we have to use. We then encode the session key in some |
1352 | | * way and we get it back in the big integer value FRAME. Then |
1353 | | * we use FRAME, the public key PK->PKEY and the algorithm |
1354 | | * number PK->PUBKEY_ALGO and pass it to pubkey_encrypt which |
1355 | | * returns the encrypted value in the array ENC->DATA. This |
1356 | | * array has a size which depends on the used algorithm (e.g. 2 |
1357 | | * for Elgamal). We don't need frame anymore because we have |
1358 | | * everything now in enc->data which is the passed to |
1359 | | * build_packet(). */ |
1360 | 0 | frame = encode_session_key (pk->pubkey_algo, dek, |
1361 | 0 | pubkey_nbits (pk->pubkey_algo, pk->pkey)); |
1362 | 0 | rc = pk_encrypt (pk, frame, dek->algo, enc->data); |
1363 | 0 | gcry_mpi_release (frame); |
1364 | 0 | if (rc) |
1365 | 0 | log_error ("pubkey_encrypt failed: %s\n", gpg_strerror (rc) ); |
1366 | 0 | else |
1367 | 0 | { |
1368 | 0 | if ( opt.verbose ) |
1369 | 0 | show_encrypted_for_user_info (ctrl, pk->pubkey_usage, enc, dek); |
1370 | | /* And write it. */ |
1371 | 0 | init_packet (&pkt); |
1372 | 0 | pkt.pkttype = PKT_PUBKEY_ENC; |
1373 | 0 | pkt.pkt.pubkey_enc = enc; |
1374 | 0 | rc = build_packet (out, &pkt); |
1375 | 0 | if (rc) |
1376 | 0 | log_error ("build_packet(pubkey_enc) failed: %s\n", |
1377 | 0 | gpg_strerror (rc)); |
1378 | 0 | } |
1379 | 0 | free_pubkey_enc(enc); |
1380 | 0 | return rc; |
1381 | 0 | } |
1382 | | |
1383 | | |
1384 | | /* |
1385 | | * Write pubkey-enc packets from the list of PKs PKLIST to OUT. DEK |
1386 | | * has the session key. If a packet with the same key is also found |
1387 | | * in RESTRICT_PK_LIST, it is not written. |
1388 | | */ |
1389 | | static int |
1390 | | write_pubkey_enc_from_list (ctrl_t ctrl, pk_list_t pk_list, DEK *dek, |
1391 | | iobuf_t out, |
1392 | | struct pubkey_enc_info_item *restrict_pk_list) |
1393 | 0 | { |
1394 | 0 | PKT_public_key *pk; |
1395 | 0 | struct pubkey_enc_info_item *pkei; |
1396 | 0 | int throw_keyid, rc; |
1397 | |
|
1398 | 0 | if (opt.throw_keyids && (PGP7 || PGP8)) |
1399 | 0 | { |
1400 | 0 | log_info(_("option '%s' may not be used in %s mode\n"), |
1401 | 0 | "--throw-keyids", |
1402 | 0 | gnupg_compliance_option_string (opt.compliance)); |
1403 | 0 | compliance_failure(); |
1404 | 0 | } |
1405 | |
|
1406 | 0 | for ( ; pk_list; pk_list = pk_list->next ) |
1407 | 0 | { |
1408 | 0 | pk = pk_list->pk; |
1409 | 0 | for (pkei = restrict_pk_list; pkei; pkei = pkei->next) |
1410 | 0 | if (pk->keyid[0] == pkei->keyid[0] |
1411 | 0 | && pk->keyid[1] == pkei->keyid[1] |
1412 | 0 | && pk->version == pkei->version |
1413 | 0 | && pk->pubkey_algo == pkei->pubkey_algo) |
1414 | 0 | break; |
1415 | 0 | if (pkei) |
1416 | 0 | { |
1417 | 0 | if (opt.verbose) |
1418 | 0 | log_info (_("already encrypted to %08lX\n"), |
1419 | 0 | (ulong) keyid_from_pk (pk, NULL)); |
1420 | 0 | continue; |
1421 | 0 | } |
1422 | | |
1423 | 0 | throw_keyid = (opt.throw_keyids || (pk_list->flags&1)); |
1424 | 0 | rc = write_pubkey_enc (ctrl, pk, throw_keyid, dek, out); |
1425 | 0 | if (rc) |
1426 | 0 | return rc; |
1427 | 0 | } |
1428 | | |
1429 | 0 | return 0; |
1430 | 0 | } |
1431 | | |
1432 | | void |
1433 | | encrypt_crypt_files (ctrl_t ctrl, int nfiles, char **files, strlist_t remusr) |
1434 | 0 | { |
1435 | 0 | int rc = 0; |
1436 | |
|
1437 | 0 | if (opt.outfile) |
1438 | 0 | { |
1439 | 0 | log_error(_("--output doesn't work for this command\n")); |
1440 | 0 | return; |
1441 | 0 | } |
1442 | | |
1443 | 0 | if (!nfiles) |
1444 | 0 | { |
1445 | 0 | char line[2048]; |
1446 | 0 | unsigned int lno = 0; |
1447 | 0 | while ( fgets(line, DIM(line), stdin) ) |
1448 | 0 | { |
1449 | 0 | lno++; |
1450 | 0 | if (!*line || line[strlen(line)-1] != '\n') |
1451 | 0 | { |
1452 | 0 | log_error("input line %u too long or missing LF\n", lno); |
1453 | 0 | return; |
1454 | 0 | } |
1455 | 0 | line[strlen(line)-1] = '\0'; |
1456 | 0 | print_file_status(STATUS_FILE_START, line, 2); |
1457 | 0 | rc = encrypt_crypt (ctrl, GNUPG_INVALID_FD, line, remusr, |
1458 | 0 | 0, NULL, GNUPG_INVALID_FD); |
1459 | 0 | if (rc) |
1460 | 0 | log_error ("encryption of '%s' failed: %s\n", |
1461 | 0 | print_fname_stdin(line), gpg_strerror (rc) ); |
1462 | 0 | write_status( STATUS_FILE_DONE ); |
1463 | 0 | } |
1464 | 0 | } |
1465 | 0 | else |
1466 | 0 | { |
1467 | 0 | while (nfiles--) |
1468 | 0 | { |
1469 | 0 | print_file_status(STATUS_FILE_START, *files, 2); |
1470 | 0 | if ((rc = encrypt_crypt (ctrl, GNUPG_INVALID_FD, *files, remusr, |
1471 | 0 | 0, NULL, GNUPG_INVALID_FD))) |
1472 | 0 | log_error("encryption of '%s' failed: %s\n", |
1473 | 0 | print_fname_stdin(*files), gpg_strerror (rc) ); |
1474 | 0 | write_status( STATUS_FILE_DONE ); |
1475 | 0 | files++; |
1476 | 0 | } |
1477 | 0 | } |
1478 | 0 | } |