2009-09-28 16:37:48 +02:00
|
|
|
/* encrypt.c - Main encryption driver
|
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
|
|
|
* 2006, 2009 Free Software Foundation, Inc.
|
2016-03-02 15:38:16 +01:00
|
|
|
* Copyright (C) 2016 g10 Code GmbH
|
2009-09-28 16:37:48 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2009-09-28 16:37:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "gpg.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/iobuf.h"
|
2009-09-28 16:37:48 +02:00
|
|
|
#include "keydb.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/util.h"
|
2009-09-28 16:37:48 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "trustdb.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/i18n.h"
|
|
|
|
#include "../common/status.h"
|
2009-09-28 16:37:48 +02:00
|
|
|
#include "pkglue.h"
|
2017-06-06 16:01:40 +02:00
|
|
|
#include "../common/compliance.h"
|
2009-09-28 16:37:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int encrypt_simple( const char *filename, int mode, int use_seskey );
|
2017-03-31 20:03:52 +02:00
|
|
|
static int write_pubkey_enc_from_list (ctrl_t ctrl,
|
|
|
|
PK_LIST pk_list, DEK *dek, iobuf_t out);
|
2009-09-28 16:37:48 +02:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* Encrypt FILENAME with only the symmetric cipher. Take input from
|
2018-01-23 12:07:25 +01:00
|
|
|
* stdin if FILENAME is NULL. If --force-aead is used we use an SKESK.
|
2009-09-28 16:37:48 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
encrypt_symmetric (const char *filename)
|
|
|
|
{
|
2018-01-23 12:07:25 +01:00
|
|
|
return encrypt_simple( filename, 1, opt.force_aead);
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Encrypt FILENAME as a literal data packet only. Take input from
|
|
|
|
* stdin if FILENAME is NULL.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
encrypt_store (const char *filename)
|
|
|
|
{
|
|
|
|
return encrypt_simple( filename, 0, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-29 15:03:26 +02:00
|
|
|
/* Create and setup a DEK structure and print approriate warnings.
|
|
|
|
* PK_LIST gives the list of public keys. Always returns a DEK. The
|
|
|
|
* actual session needs to be added later. */
|
|
|
|
static DEK *
|
|
|
|
create_dek_with_warnings (pk_list_t pk_list)
|
|
|
|
{
|
|
|
|
DEK *dek;
|
|
|
|
|
|
|
|
dek = xmalloc_secure_clear (sizeof *dek);
|
|
|
|
if (!opt.def_cipher_algo)
|
|
|
|
{
|
|
|
|
/* Try to get it from the prefs. */
|
|
|
|
dek->algo = select_algo_from_prefs (pk_list, PREFTYPE_SYM, -1, NULL);
|
|
|
|
if (dek->algo == -1)
|
|
|
|
{
|
|
|
|
/* If does not make sense to fallback to the rfc4880
|
|
|
|
* required 3DES if we will reject that algo later. Thus we
|
|
|
|
* fallback to AES anticipating RFC4880bis rules. */
|
|
|
|
if (opt.flags.allow_old_cipher_algos)
|
|
|
|
dek->algo = CIPHER_ALGO_3DES;
|
|
|
|
else
|
|
|
|
dek->algo = CIPHER_ALGO_AES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In case 3DES has been selected, print a warning if any key
|
|
|
|
* does not have a preference for AES. This should help to
|
|
|
|
* indentify why encrypting to several recipients falls back to
|
|
|
|
* 3DES. */
|
|
|
|
if (opt.verbose && dek->algo == CIPHER_ALGO_3DES)
|
|
|
|
warn_missing_aes_from_pklist (pk_list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!opt.expert
|
|
|
|
&& (select_algo_from_prefs (pk_list, PREFTYPE_SYM,
|
|
|
|
opt.def_cipher_algo, NULL)
|
|
|
|
!= opt.def_cipher_algo))
|
|
|
|
{
|
|
|
|
log_info(_("WARNING: forcing symmetric cipher %s (%d)"
|
|
|
|
" violates recipient preferences\n"),
|
|
|
|
openpgp_cipher_algo_name (opt.def_cipher_algo),
|
|
|
|
opt.def_cipher_algo);
|
|
|
|
}
|
|
|
|
|
|
|
|
dek->algo = opt.def_cipher_algo;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dek;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check whether all encryption keys are compliant with the current
|
|
|
|
* mode and issue respective status lines. DEK has the info about the
|
|
|
|
* session key and PK_LIST the list of public keys. */
|
|
|
|
static gpg_error_t
|
|
|
|
check_encryption_compliance (DEK *dek, pk_list_t pk_list)
|
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
pk_list_t pkr;
|
|
|
|
int compliant;
|
|
|
|
|
|
|
|
/* First check whether we should use the algo at all. */
|
|
|
|
if (openpgp_cipher_blocklen (dek->algo) < 16
|
|
|
|
&& !opt.flags.allow_old_cipher_algos)
|
|
|
|
{
|
|
|
|
log_error (_("cipher algorithm '%s' may not be used for encryption\n"),
|
|
|
|
openpgp_cipher_algo_name (dek->algo));
|
|
|
|
if (!opt.quiet)
|
|
|
|
log_info (_("(use option \"%s\" to override)\n"),
|
|
|
|
"--allow-old-cipher-algos");
|
|
|
|
err = gpg_error (GPG_ERR_CIPHER_ALGO);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now check the compliance. */
|
|
|
|
if (! gnupg_cipher_is_allowed (opt.compliance, 1, dek->algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB))
|
|
|
|
{
|
|
|
|
log_error (_("cipher algorithm '%s' may not be used in %s mode\n"),
|
|
|
|
openpgp_cipher_algo_name (dek->algo),
|
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
|
|
|
err = gpg_error (GPG_ERR_CIPHER_ALGO);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gnupg_rng_is_compliant (opt.compliance))
|
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_FORBIDDEN);
|
|
|
|
log_error (_("%s is not compliant with %s mode\n"),
|
|
|
|
"RNG",
|
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
|
|
|
write_status_error ("random-compliance", err);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2022-09-29 15:16:35 +02:00
|
|
|
/* From here on we only test for CO_DE_VS - if we ever want to
|
|
|
|
* return other compliance mode values we need to change this to
|
|
|
|
* loop over all those values. */
|
|
|
|
compliant = gnupg_gcrypt_is_compliant (CO_DE_VS);
|
|
|
|
|
|
|
|
if (!gnupg_cipher_is_compliant (CO_DE_VS, dek->algo, GCRY_CIPHER_MODE_CFB))
|
|
|
|
compliant = 0;
|
2022-09-29 15:03:26 +02:00
|
|
|
|
|
|
|
for (pkr = pk_list; pkr; pkr = pkr->next)
|
|
|
|
{
|
|
|
|
PKT_public_key *pk = pkr->pk;
|
|
|
|
unsigned int nbits = nbits_from_pk (pk);
|
|
|
|
|
|
|
|
if (!gnupg_pk_is_compliant (opt.compliance, pk->pubkey_algo, 0,
|
|
|
|
pk->pkey, nbits, NULL))
|
|
|
|
log_info (_("WARNING: key %s is not suitable for encryption"
|
|
|
|
" in %s mode\n"),
|
|
|
|
keystr_from_pk (pk),
|
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
|
|
|
|
|
|
|
if (compliant
|
|
|
|
&& !gnupg_pk_is_compliant (CO_DE_VS, pk->pubkey_algo, 0, pk->pkey,
|
|
|
|
nbits, NULL))
|
|
|
|
compliant = 0; /* Not compliant - reset flag. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are compliant print the status for de-vs compliance. */
|
|
|
|
if (compliant)
|
|
|
|
write_status_strings (STATUS_ENCRYPTION_COMPLIANCE_MODE,
|
|
|
|
gnupg_status_compliance_flag (CO_DE_VS),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Check whether we should fail the operation. */
|
|
|
|
if (opt.flags.require_compliance
|
|
|
|
&& opt.compliance == CO_DE_VS
|
|
|
|
&& !compliant)
|
|
|
|
{
|
|
|
|
compliance_failure ();
|
|
|
|
err = gpg_error (GPG_ERR_FORBIDDEN);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-22 15:50:24 +01:00
|
|
|
/* Encrypt a session key using DEK and store a pointer to the result
|
|
|
|
* at R_ENCKEY and its length at R_ENCKEYLEN.
|
|
|
|
*
|
2018-01-23 12:07:25 +01:00
|
|
|
* R_SESKEY points to the unencrypted session key (.KEY, .KEYLEN) and
|
2018-01-22 15:50:24 +01:00
|
|
|
* the algorithm that will be used to encrypt the contents of the
|
|
|
|
* SKESK packet (.ALGO). If R_SESKEY points to NULL, then a random
|
|
|
|
* session key that is appropriate for DEK->ALGO is generated and
|
2018-01-23 12:07:25 +01:00
|
|
|
* stored at R_SESKEY. If AEAD_ALGO is not 0 the given AEAD algorithm
|
|
|
|
* is used for encryption.
|
2018-01-22 15:50:24 +01:00
|
|
|
*/
|
|
|
|
gpg_error_t
|
2018-01-23 12:07:25 +01:00
|
|
|
encrypt_seskey (DEK *dek, aead_algo_t aead_algo,
|
|
|
|
DEK **r_seskey, void **r_enckey, size_t *r_enckeylen)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2018-01-22 15:50:24 +01:00
|
|
|
gpg_error_t err;
|
|
|
|
gcry_cipher_hd_t hd = NULL;
|
|
|
|
byte *buf = NULL;
|
|
|
|
DEK *seskey;
|
|
|
|
|
|
|
|
*r_enckey = NULL;
|
|
|
|
*r_enckeylen = 0;
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2018-01-22 15:50:24 +01:00
|
|
|
if (*r_seskey)
|
|
|
|
seskey = *r_seskey;
|
|
|
|
else
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2018-01-22 15:50:24 +01:00
|
|
|
seskey = xtrycalloc (1, sizeof(DEK));
|
|
|
|
if (!seskey)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
seskey->algo = dek->algo;
|
|
|
|
make_session_key (seskey);
|
2009-09-28 16:37:48 +02:00
|
|
|
/*log_hexdump( "thekey", c->key, c->keylen );*/
|
|
|
|
}
|
|
|
|
|
2018-01-23 12:07:25 +01:00
|
|
|
|
|
|
|
if (aead_algo)
|
2018-01-22 15:50:24 +01:00
|
|
|
{
|
2018-01-23 12:07:25 +01:00
|
|
|
unsigned int noncelen;
|
|
|
|
enum gcry_cipher_modes ciphermode;
|
|
|
|
byte ad[4];
|
|
|
|
|
|
|
|
err = openpgp_aead_algo_info (aead_algo, &ciphermode, &noncelen);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
/* Allocate space for the nonce, the key, and the authentication
|
|
|
|
* tag (16). */
|
|
|
|
buf = xtrymalloc_secure (noncelen + seskey->keylen + 16);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
goto leave;
|
|
|
|
}
|
2018-01-22 15:50:24 +01:00
|
|
|
|
2018-01-23 12:07:25 +01:00
|
|
|
gcry_randomize (buf, noncelen, GCRY_STRONG_RANDOM);
|
|
|
|
|
|
|
|
err = openpgp_cipher_open (&hd, dek->algo,
|
|
|
|
ciphermode, GCRY_CIPHER_SECURE);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_setkey (hd, dek->key, dek->keylen);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_setiv (hd, buf, noncelen);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
ad[0] = (0xc0 | PKT_SYMKEY_ENC);
|
|
|
|
ad[1] = 5;
|
|
|
|
ad[2] = dek->algo;
|
|
|
|
ad[3] = aead_algo;
|
|
|
|
err = gcry_cipher_authenticate (hd, ad, 4);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
memcpy (buf + noncelen, seskey->key, seskey->keylen);
|
|
|
|
gcry_cipher_final (hd);
|
|
|
|
err = gcry_cipher_encrypt (hd, buf + noncelen, seskey->keylen, NULL,0);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
err = gcry_cipher_gettag (hd, buf + noncelen + seskey->keylen, 16);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
*r_enckeylen = noncelen + seskey->keylen + 16;
|
|
|
|
*r_enckey = buf;
|
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In the old version 4 SKESK the encrypted session key is
|
|
|
|
* prefixed with a one-octet algorithm id. */
|
|
|
|
buf = xtrymalloc_secure (1 + seskey->keylen);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
buf[0] = seskey->algo;
|
|
|
|
memcpy (buf + 1, seskey->key, seskey->keylen);
|
|
|
|
|
|
|
|
err = openpgp_cipher_open (&hd, dek->algo, GCRY_CIPHER_MODE_CFB, 1);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_setkey (hd, dek->key, dek->keylen);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_setiv (hd, NULL, 0);
|
|
|
|
if (!err)
|
|
|
|
err = gcry_cipher_encrypt (hd, buf, seskey->keylen + 1, NULL, 0);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
*r_enckeylen = seskey->keylen + 1;
|
|
|
|
*r_enckey = buf;
|
|
|
|
buf = NULL;
|
|
|
|
}
|
2018-01-22 15:50:24 +01:00
|
|
|
|
|
|
|
/* Return the session key in case we allocated it. */
|
|
|
|
*r_seskey = seskey;
|
|
|
|
seskey = NULL;
|
|
|
|
|
|
|
|
leave:
|
2009-09-28 16:37:48 +02:00
|
|
|
gcry_cipher_close (hd);
|
2018-01-22 15:50:24 +01:00
|
|
|
if (seskey != *r_seskey)
|
|
|
|
xfree (seskey);
|
|
|
|
xfree (buf);
|
|
|
|
return err;
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-22 16:23:02 +01:00
|
|
|
/* Return the AEAD algo if we shall use AEAD mode. Returns 0 if AEAD
|
|
|
|
* shall not be used. */
|
|
|
|
aead_algo_t
|
2018-01-10 17:07:11 +01:00
|
|
|
use_aead (pk_list_t pk_list, int algo)
|
|
|
|
{
|
|
|
|
int can_use;
|
|
|
|
|
|
|
|
if (!opt.flags.rfc4880bis)
|
|
|
|
{
|
|
|
|
if (opt.force_aead)
|
|
|
|
log_info ("Warning: Option %s currently requires option '%s'\n",
|
|
|
|
"--force-aead", "--rfc4880bis");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
can_use = openpgp_cipher_get_algo_blklen (algo) == 16;
|
2018-01-10 17:07:11 +01:00
|
|
|
|
2018-05-29 12:42:44 +02:00
|
|
|
/* With --force-aead we want AEAD. */
|
2018-01-10 17:07:11 +01:00
|
|
|
if (opt.force_aead)
|
|
|
|
{
|
|
|
|
if (!can_use)
|
2018-01-21 16:24:43 +01:00
|
|
|
{
|
|
|
|
log_info ("Warning: request to use AEAD ignored for cipher '%s'\n",
|
|
|
|
openpgp_cipher_algo_name (algo));
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-22 16:23:02 +01:00
|
|
|
return default_aead_algo ();
|
2018-01-10 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 16:24:43 +01:00
|
|
|
/* AEAD does only work with 128 bit cipher blocklength. */
|
2018-01-10 17:07:11 +01:00
|
|
|
if (!can_use)
|
|
|
|
return 0;
|
|
|
|
|
2018-05-29 12:42:44 +02:00
|
|
|
/* Note the user which keys have no AEAD feature flag set. */
|
|
|
|
if (opt.verbose)
|
|
|
|
warn_missing_aead_from_pklist (pk_list);
|
|
|
|
|
2018-01-10 17:07:11 +01:00
|
|
|
/* If all keys support AEAD we can use it. */
|
2018-01-22 16:23:02 +01:00
|
|
|
return select_aead_from_pklist (pk_list);
|
2018-01-10 17:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-29 12:42:44 +02:00
|
|
|
/* Shall we use the MDC? Yes - unless rfc-2440 compatibility is
|
|
|
|
* requested. */
|
2015-10-12 09:31:44 +02:00
|
|
|
int
|
|
|
|
use_mdc (pk_list_t pk_list,int algo)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2018-05-29 12:42:44 +02:00
|
|
|
(void)pk_list;
|
|
|
|
(void)algo;
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2018-05-29 12:42:44 +02:00
|
|
|
/* RFC-2440 don't has MDC - this is the only way to create a legacy
|
|
|
|
* non-MDC encryption packet. */
|
|
|
|
if (RFC2440)
|
2009-09-28 16:37:48 +02:00
|
|
|
return 0;
|
|
|
|
|
2018-05-29 12:42:44 +02:00
|
|
|
return 1; /* In all other cases we use the MDC */
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We don't want to use use_seskey yet because older gnupg versions
|
|
|
|
can't handle it, and there isn't really any point unless we're
|
|
|
|
making a message that can be decrypted by a public key or
|
|
|
|
passphrase. */
|
|
|
|
static int
|
|
|
|
encrypt_simple (const char *filename, int mode, int use_seskey)
|
|
|
|
{
|
|
|
|
iobuf_t inp, out;
|
|
|
|
PACKET pkt;
|
|
|
|
PKT_plaintext *pt = NULL;
|
|
|
|
STRING2KEY *s2k = NULL;
|
2018-01-22 15:50:24 +01:00
|
|
|
void *enckey = NULL;
|
|
|
|
size_t enckeylen = 0;
|
2009-09-28 16:37:48 +02:00
|
|
|
int rc = 0;
|
|
|
|
u32 filesize;
|
|
|
|
cipher_filter_context_t cfx;
|
|
|
|
armor_filter_context_t *afx = NULL;
|
|
|
|
compress_filter_context_t zfx;
|
|
|
|
text_filter_context_t tfx;
|
|
|
|
progress_filter_context_t *pfx;
|
2014-08-12 10:36:30 +02:00
|
|
|
int do_compress = !!default_compress_algo();
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2017-07-17 15:52:26 +02:00
|
|
|
if (!gnupg_rng_is_compliant (opt.compliance))
|
|
|
|
{
|
|
|
|
rc = gpg_error (GPG_ERR_FORBIDDEN);
|
|
|
|
log_error (_("%s is not compliant with %s mode\n"),
|
|
|
|
"RNG",
|
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
|
|
|
write_status_error ("random-compliance", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
pfx = new_progress_context ();
|
|
|
|
memset( &cfx, 0, sizeof cfx);
|
|
|
|
memset( &zfx, 0, sizeof zfx);
|
|
|
|
memset( &tfx, 0, sizeof tfx);
|
|
|
|
init_packet(&pkt);
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Prepare iobufs. */
|
|
|
|
inp = iobuf_open(filename);
|
|
|
|
if (inp)
|
2010-03-08 18:05:37 +01:00
|
|
|
iobuf_ioctl (inp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
|
2009-09-28 16:37:48 +02:00
|
|
|
if (inp && is_secured_file (iobuf_get_fd (inp)))
|
|
|
|
{
|
|
|
|
iobuf_close (inp);
|
|
|
|
inp = NULL;
|
2010-04-01 15:24:55 +02:00
|
|
|
gpg_err_set_errno (EPERM);
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
if (!inp)
|
|
|
|
{
|
|
|
|
rc = gpg_error_from_syserror ();
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error(_("can't open '%s': %s\n"), filename? filename: "[stdin]",
|
2009-09-28 16:37:48 +02:00
|
|
|
strerror(errno) );
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
handle_progress (pfx, inp, filename);
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if (opt.textmode)
|
|
|
|
iobuf_push_filter( inp, text_filter, &tfx );
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
cfx.dek = NULL;
|
2011-02-02 17:40:32 +01:00
|
|
|
if ( mode )
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2018-01-22 15:50:24 +01:00
|
|
|
aead_algo_t aead_algo;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2020-07-07 12:58:29 +02:00
|
|
|
rc = setup_symkey (&s2k, &cfx.dek);
|
|
|
|
if (rc)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
iobuf_close (inp);
|
2020-11-03 13:55:25 +01:00
|
|
|
if (gpg_err_code (rc) == GPG_ERR_CIPHER_ALGO
|
|
|
|
|| gpg_err_code (rc) == GPG_ERR_DIGEST_ALGO)
|
|
|
|
; /* Error has already been printed. */
|
|
|
|
else
|
|
|
|
log_error (_("error creating passphrase: %s\n"), gpg_strerror (rc));
|
2009-09-28 16:37:48 +02:00
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
if (use_seskey && s2k->mode != 1 && s2k->mode != 3)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
use_seskey = 0;
|
|
|
|
log_info (_("can't use a symmetric ESK packet "
|
|
|
|
"due to the S2K mode\n"));
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2018-01-22 15:50:24 +01:00
|
|
|
/* See whether we want to use AEAD. */
|
2018-01-22 16:23:02 +01:00
|
|
|
aead_algo = use_aead (NULL, cfx.dek->algo);
|
2018-01-22 15:50:24 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if ( use_seskey )
|
|
|
|
{
|
|
|
|
DEK *dek = NULL;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2018-01-23 12:07:25 +01:00
|
|
|
rc = encrypt_seskey (cfx.dek, aead_algo, &dek, &enckey, &enckeylen);
|
2018-01-22 15:50:24 +01:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
xfree (cfx.dek);
|
|
|
|
xfree (s2k);
|
|
|
|
iobuf_close (inp);
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/* Replace key in DEK. */
|
|
|
|
xfree (cfx.dek);
|
|
|
|
cfx.dek = dek;
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2018-01-22 15:50:24 +01:00
|
|
|
if (aead_algo)
|
|
|
|
cfx.dek->use_aead = aead_algo;
|
2018-01-10 17:07:11 +01:00
|
|
|
else
|
|
|
|
cfx.dek->use_mdc = !!use_mdc (NULL, cfx.dek->algo);
|
2018-01-23 12:07:25 +01:00
|
|
|
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info(_("using cipher %s.%s\n"),
|
|
|
|
openpgp_cipher_algo_name (cfx.dek->algo),
|
|
|
|
cfx.dek->use_aead? openpgp_aead_algo_name (cfx.dek->use_aead)
|
|
|
|
/**/ : "CFB");
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2018-01-10 17:07:11 +01:00
|
|
|
if (do_compress
|
|
|
|
&& cfx.dek
|
|
|
|
&& (cfx.dek->use_mdc || cfx.dek->use_aead)
|
2009-09-28 16:37:48 +02:00
|
|
|
&& is_file_compressed(filename, &rc))
|
|
|
|
{
|
|
|
|
if (opt.verbose)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info(_("'%s' already compressed\n"), filename);
|
2011-02-02 17:40:32 +01:00
|
|
|
do_compress = 0;
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2017-08-01 19:08:16 +02:00
|
|
|
if ( rc || (rc = open_outfile (-1, filename, opt.armor? 1:0, 0, &out )))
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
iobuf_cancel (inp);
|
|
|
|
xfree (cfx.dek);
|
|
|
|
xfree (s2k);
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if ( opt.armor )
|
|
|
|
{
|
|
|
|
afx = new_armor_context ();
|
|
|
|
push_armor_filter (afx, out);
|
|
|
|
}
|
|
|
|
|
2014-08-12 10:36:30 +02:00
|
|
|
if ( s2k )
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2018-01-22 15:50:24 +01:00
|
|
|
/* Fixme: This is quite similar to write_symkey_enc. */
|
|
|
|
PKT_symkey_enc *enc = xmalloc_clear (sizeof *enc + enckeylen);
|
2018-01-23 12:07:25 +01:00
|
|
|
enc->version = cfx.dek->use_aead ? 5 : 4;
|
2009-09-28 16:37:48 +02:00
|
|
|
enc->cipher_algo = cfx.dek->algo;
|
2018-01-23 12:07:25 +01:00
|
|
|
enc->aead_algo = cfx.dek->use_aead;
|
2009-09-28 16:37:48 +02:00
|
|
|
enc->s2k = *s2k;
|
2018-01-22 15:50:24 +01:00
|
|
|
if (enckeylen)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2018-01-22 15:50:24 +01:00
|
|
|
enc->seskeylen = enckeylen;
|
|
|
|
memcpy (enc->seskey, enckey, enckeylen);
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
pkt.pkttype = PKT_SYMKEY_ENC;
|
|
|
|
pkt.pkt.symkey_enc = enc;
|
|
|
|
if ((rc = build_packet( out, &pkt )))
|
2015-01-22 12:06:11 +01:00
|
|
|
log_error("build symkey packet failed: %s\n", gpg_strerror (rc) );
|
2009-09-28 16:37:48 +02:00
|
|
|
xfree (enc);
|
2018-01-22 15:50:24 +01:00
|
|
|
xfree (enckey);
|
|
|
|
enckey = NULL;
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if (!opt.no_literal)
|
|
|
|
pt = setup_plaintext_name (filename, inp);
|
|
|
|
|
|
|
|
/* Note that PGP 5 has problems decrypting symmetrically encrypted
|
|
|
|
data if the file length is in the inner packet. It works when
|
|
|
|
only partial length headers are use. In the past, we always used
|
|
|
|
partial body length here, but since PGP 2, PGP 6, and PGP 7 need
|
|
|
|
the file length, and nobody should be using PGP 5 nowadays
|
|
|
|
anyway, this is now set to the file length. Note also that this
|
|
|
|
only applies to the RFC-1991 style symmetric messages, and not
|
|
|
|
the RFC-2440 style. PGP 6 and 7 work with either partial length
|
|
|
|
or fixed length with the new style messages. */
|
|
|
|
|
|
|
|
if ( !iobuf_is_pipe_filename (filename) && *filename && !opt.textmode )
|
|
|
|
{
|
|
|
|
off_t tmpsize;
|
|
|
|
int overflow;
|
|
|
|
|
|
|
|
if ( !(tmpsize = iobuf_get_filelength(inp, &overflow))
|
|
|
|
&& !overflow && opt.verbose)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info(_("WARNING: '%s' is an empty file\n"), filename );
|
2009-09-28 16:37:48 +02:00
|
|
|
/* We can't encode the length of very large files because
|
2017-02-20 22:19:50 +01:00
|
|
|
OpenPGP uses only 32 bit for file sizes. So if the
|
2009-09-28 16:37:48 +02:00
|
|
|
size of a file is larger than 2^32 minus some bytes for
|
|
|
|
packet headers, we switch to partial length encoding. */
|
|
|
|
if ( tmpsize < (IOBUF_FILELENGTH_LIMIT - 65536) )
|
|
|
|
filesize = tmpsize;
|
|
|
|
else
|
|
|
|
filesize = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */
|
|
|
|
|
|
|
|
if (!opt.no_literal)
|
|
|
|
{
|
2016-02-03 14:23:51 +01:00
|
|
|
/* Note that PT has been initialized above in !no_literal mode. */
|
2009-09-28 16:37:48 +02:00
|
|
|
pt->timestamp = make_timestamp();
|
2016-07-13 13:31:12 +02:00
|
|
|
pt->mode = opt.mimemode? 'm' : opt.textmode? 't' : 'b';
|
2009-09-28 16:37:48 +02:00
|
|
|
pt->len = filesize;
|
2014-08-12 10:36:30 +02:00
|
|
|
pt->new_ctb = !pt->len;
|
2009-09-28 16:37:48 +02:00
|
|
|
pt->buf = inp;
|
|
|
|
pkt.pkttype = PKT_PLAINTEXT;
|
|
|
|
pkt.pkt.plaintext = pt;
|
|
|
|
cfx.datalen = filesize && !do_compress ? calc_packet_length( &pkt ) : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfx.datalen = filesize && !do_compress ? filesize : 0;
|
|
|
|
pkt.pkttype = 0;
|
|
|
|
pkt.pkt.generic = NULL;
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Register the cipher filter. */
|
|
|
|
if (mode)
|
2018-01-10 17:33:50 +01:00
|
|
|
iobuf_push_filter (out,
|
|
|
|
cfx.dek->use_aead? cipher_filter_aead
|
|
|
|
/**/ : cipher_filter_cfb,
|
|
|
|
&cfx );
|
2009-09-28 16:37:48 +02:00
|
|
|
|
|
|
|
/* Register the compress filter. */
|
|
|
|
if ( do_compress )
|
|
|
|
{
|
2018-01-10 17:07:11 +01:00
|
|
|
if (cfx.dek && (cfx.dek->use_mdc || cfx.dek->use_aead))
|
2009-09-28 16:37:48 +02:00
|
|
|
zfx.new_ctb = 1;
|
|
|
|
push_compress_filter (out, &zfx, default_compress_algo());
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Do the work. */
|
|
|
|
if (!opt.no_literal)
|
|
|
|
{
|
|
|
|
if ( (rc = build_packet( out, &pkt )) )
|
2015-01-22 12:06:11 +01:00
|
|
|
log_error("build_packet failed: %s\n", gpg_strerror (rc) );
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
else
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
/* User requested not to create a literal packet, so we copy the
|
|
|
|
plain data. */
|
2022-02-13 14:09:42 +01:00
|
|
|
rc = iobuf_copy (out, inp);
|
|
|
|
if (rc)
|
|
|
|
log_error ("copying input to output failed: %s\n", gpg_strerror (rc));
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Finish the stuff. */
|
|
|
|
iobuf_close (inp);
|
|
|
|
if (rc)
|
|
|
|
iobuf_cancel(out);
|
2011-02-02 17:40:32 +01:00
|
|
|
else
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
iobuf_close (out); /* fixme: check returncode */
|
|
|
|
if (mode)
|
|
|
|
write_status ( STATUS_END_ENCRYPTION );
|
|
|
|
}
|
|
|
|
if (pt)
|
|
|
|
pt->buf = NULL;
|
2017-03-29 11:57:40 +02:00
|
|
|
free_packet (&pkt, NULL);
|
2018-01-22 15:50:24 +01:00
|
|
|
xfree (enckey);
|
2009-09-28 16:37:48 +02:00
|
|
|
xfree (cfx.dek);
|
|
|
|
xfree (s2k);
|
|
|
|
release_armor_context (afx);
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-07 12:58:29 +02:00
|
|
|
gpg_error_t
|
|
|
|
setup_symkey (STRING2KEY **symkey_s2k, DEK **symkey_dek)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
int canceled;
|
2020-11-03 13:55:25 +01:00
|
|
|
int defcipher;
|
|
|
|
int s2kdigest;
|
|
|
|
|
|
|
|
defcipher = default_cipher_algo ();
|
2021-02-10 14:31:34 +01:00
|
|
|
if (openpgp_cipher_blocklen (defcipher) < 16
|
|
|
|
&& !opt.flags.allow_old_cipher_algos)
|
|
|
|
{
|
|
|
|
log_error (_("cipher algorithm '%s' may not be used for encryption\n"),
|
|
|
|
openpgp_cipher_algo_name (defcipher));
|
|
|
|
if (!opt.quiet)
|
|
|
|
log_info (_("(use option \"%s\" to override)\n"),
|
|
|
|
"--allow-old-cipher-algos");
|
|
|
|
return gpg_error (GPG_ERR_CIPHER_ALGO);
|
|
|
|
}
|
|
|
|
|
2020-11-03 13:55:25 +01:00
|
|
|
if (!gnupg_cipher_is_allowed (opt.compliance, 1, defcipher,
|
|
|
|
GCRY_CIPHER_MODE_CFB))
|
|
|
|
{
|
|
|
|
log_error (_("cipher algorithm '%s' may not be used in %s mode\n"),
|
|
|
|
openpgp_cipher_algo_name (defcipher),
|
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
|
|
|
return gpg_error (GPG_ERR_CIPHER_ALGO);
|
|
|
|
}
|
|
|
|
|
|
|
|
s2kdigest = S2K_DIGEST_ALGO;
|
|
|
|
if (!gnupg_digest_is_allowed (opt.compliance, 1, s2kdigest))
|
|
|
|
{
|
|
|
|
log_error (_("digest algorithm '%s' may not be used in %s mode\n"),
|
|
|
|
gcry_md_algo_name (s2kdigest),
|
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
|
|
|
return gpg_error (GPG_ERR_DIGEST_ALGO);
|
|
|
|
}
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2020-07-07 12:58:29 +02:00
|
|
|
*symkey_s2k = xmalloc_clear (sizeof **symkey_s2k);
|
2009-09-28 16:37:48 +02:00
|
|
|
(*symkey_s2k)->mode = opt.s2k_mode;
|
2020-11-03 13:55:25 +01:00
|
|
|
(*symkey_s2k)->hash_algo = s2kdigest;
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2020-11-03 13:55:25 +01:00
|
|
|
*symkey_dek = passphrase_to_dek (defcipher,
|
2021-05-17 19:27:54 +02:00
|
|
|
*symkey_s2k, 1, 0, NULL, 0, &canceled);
|
2020-07-07 12:58:29 +02:00
|
|
|
if (!*symkey_dek || !(*symkey_dek)->keylen)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
xfree(*symkey_dek);
|
|
|
|
xfree(*symkey_s2k);
|
2020-07-07 12:58:29 +02:00
|
|
|
return gpg_error (canceled?GPG_ERR_CANCELED:GPG_ERR_INV_PASSPHRASE);
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2018-01-23 12:07:25 +01:00
|
|
|
write_symkey_enc (STRING2KEY *symkey_s2k, aead_algo_t aead_algo,
|
|
|
|
DEK *symkey_dek, DEK *dek, iobuf_t out)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2018-01-22 15:50:24 +01:00
|
|
|
int rc;
|
|
|
|
void *enckey;
|
|
|
|
size_t enckeylen;
|
2009-09-28 16:37:48 +02:00
|
|
|
PKT_symkey_enc *enc;
|
|
|
|
PACKET pkt;
|
|
|
|
|
2018-01-23 12:07:25 +01:00
|
|
|
rc = encrypt_seskey (symkey_dek, aead_algo, &dek, &enckey, &enckeylen);
|
2018-01-22 15:50:24 +01:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
enc = xtrycalloc (1, sizeof (PKT_symkey_enc) + enckeylen);
|
|
|
|
if (!enc)
|
|
|
|
{
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
xfree (enckey);
|
|
|
|
return rc;
|
|
|
|
}
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2018-01-23 12:07:25 +01:00
|
|
|
enc->version = aead_algo? 5 : 4;
|
2009-09-28 16:37:48 +02:00
|
|
|
enc->cipher_algo = opt.s2k_cipher_algo;
|
2018-01-23 12:07:25 +01:00
|
|
|
enc->aead_algo = aead_algo;
|
2009-09-28 16:37:48 +02:00
|
|
|
enc->s2k = *symkey_s2k;
|
2018-01-22 15:50:24 +01:00
|
|
|
enc->seskeylen = enckeylen;
|
|
|
|
memcpy (enc->seskey, enckey, enckeylen);
|
|
|
|
xfree (enckey);
|
2009-09-28 16:37:48 +02:00
|
|
|
|
|
|
|
pkt.pkttype = PKT_SYMKEY_ENC;
|
|
|
|
pkt.pkt.symkey_enc = enc;
|
|
|
|
|
|
|
|
if ((rc=build_packet(out,&pkt)))
|
2015-01-22 12:06:11 +01:00
|
|
|
log_error("build symkey_enc packet failed: %s\n",gpg_strerror (rc));
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2018-01-22 15:50:24 +01:00
|
|
|
xfree (enc);
|
2009-09-28 16:37:48 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2009-09-30 17:28:38 +02:00
|
|
|
* Encrypt the file with the given userids (or ask if none is
|
|
|
|
* supplied). Either FILENAME or FILEFD must be given, but not both.
|
|
|
|
* The caller may provide a checked list of public keys in
|
|
|
|
* PROVIDED_PKS; if not the function builds a list of keys on its own.
|
gpg: Fix possible dead code elimination.
* g10/encrypt.c: Change condition for detecting a real file.
--
Detected by Stack 3.0:
bug: anti-dce
model: |
%tobool155 = icmp ne i32 %call154, 0, !dbg !1298
--> true
************************************************************
land.lhs.true156:
%96 = icmp eq i8* %filename, null
call void @opt.bugon(i1 %96), !dbg !1298, !bug !1250
%97 = load i8* %filename, align 1, !dbg !1298
%conv157 = sext i8 %97 to i32, !dbg !1298
%tobool158 = icmp ne i32 %conv157, 0, !dbg !1298
br i1 %tobool158, label %land.lhs.true159, label %if.else177,\
!dbg !1298
stack:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
ncore: 1
core:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
- null pointer dereference
2015-03-15 14:01:36 +01:00
|
|
|
*
|
2017-02-20 22:19:50 +01:00
|
|
|
* Note that FILEFD is currently only used by cmd_encrypt in the
|
gpg: Fix possible dead code elimination.
* g10/encrypt.c: Change condition for detecting a real file.
--
Detected by Stack 3.0:
bug: anti-dce
model: |
%tobool155 = icmp ne i32 %call154, 0, !dbg !1298
--> true
************************************************************
land.lhs.true156:
%96 = icmp eq i8* %filename, null
call void @opt.bugon(i1 %96), !dbg !1298, !bug !1250
%97 = load i8* %filename, align 1, !dbg !1298
%conv157 = sext i8 %97 to i32, !dbg !1298
%tobool158 = icmp ne i32 %conv157, 0, !dbg !1298
br i1 %tobool158, label %land.lhs.true159, label %if.else177,\
!dbg !1298
stack:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
ncore: 1
core:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
- null pointer dereference
2015-03-15 14:01:36 +01:00
|
|
|
* not yet finished server.c.
|
2009-09-28 16:37:48 +02:00
|
|
|
*/
|
|
|
|
int
|
2010-10-01 22:33:53 +02:00
|
|
|
encrypt_crypt (ctrl_t ctrl, int filefd, const char *filename,
|
2009-09-30 17:28:38 +02:00
|
|
|
strlist_t remusr, int use_symkey, pk_list_t provided_keys,
|
2009-10-02 16:57:55 +02:00
|
|
|
int outputfd)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
iobuf_t inp = NULL;
|
|
|
|
iobuf_t out = NULL;
|
|
|
|
PACKET pkt;
|
|
|
|
PKT_plaintext *pt = NULL;
|
|
|
|
DEK *symkey_dek = NULL;
|
|
|
|
STRING2KEY *symkey_s2k = NULL;
|
|
|
|
int rc = 0, rc2 = 0;
|
|
|
|
u32 filesize;
|
|
|
|
cipher_filter_context_t cfx;
|
|
|
|
armor_filter_context_t *afx = NULL;
|
|
|
|
compress_filter_context_t zfx;
|
|
|
|
text_filter_context_t tfx;
|
|
|
|
progress_filter_context_t *pfx;
|
2014-08-12 10:36:30 +02:00
|
|
|
PK_LIST pk_list;
|
2009-09-28 16:37:48 +02:00
|
|
|
int do_compress;
|
|
|
|
|
2009-10-02 16:57:55 +02:00
|
|
|
if (filefd != -1 && filename)
|
gpg: Fix possible dead code elimination.
* g10/encrypt.c: Change condition for detecting a real file.
--
Detected by Stack 3.0:
bug: anti-dce
model: |
%tobool155 = icmp ne i32 %call154, 0, !dbg !1298
--> true
************************************************************
land.lhs.true156:
%96 = icmp eq i8* %filename, null
call void @opt.bugon(i1 %96), !dbg !1298, !bug !1250
%97 = load i8* %filename, align 1, !dbg !1298
%conv157 = sext i8 %97 to i32, !dbg !1298
%tobool158 = icmp ne i32 %conv157, 0, !dbg !1298
br i1 %tobool158, label %land.lhs.true159, label %if.else177,\
!dbg !1298
stack:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
ncore: 1
core:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
- null pointer dereference
2015-03-15 14:01:36 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_ARG); /* Both given. */
|
2009-09-30 17:28:38 +02:00
|
|
|
|
2014-08-12 10:36:30 +02:00
|
|
|
do_compress = !!opt.compress_algo;
|
2009-09-28 16:37:48 +02:00
|
|
|
|
|
|
|
pfx = new_progress_context ();
|
|
|
|
memset( &cfx, 0, sizeof cfx);
|
|
|
|
memset( &zfx, 0, sizeof zfx);
|
|
|
|
memset( &tfx, 0, sizeof tfx);
|
|
|
|
init_packet(&pkt);
|
|
|
|
|
2011-02-02 17:40:32 +01:00
|
|
|
if (use_symkey
|
2009-09-28 16:37:48 +02:00
|
|
|
&& (rc=setup_symkey(&symkey_s2k,&symkey_dek)))
|
|
|
|
{
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-09-30 17:28:38 +02:00
|
|
|
if (provided_keys)
|
|
|
|
pk_list = provided_keys;
|
|
|
|
else
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2015-12-18 13:26:40 +01:00
|
|
|
if ((rc = build_pk_list (ctrl, remusr, &pk_list)))
|
2009-09-30 17:28:38 +02:00
|
|
|
{
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Prepare iobufs. */
|
2014-03-07 09:46:44 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
if (filefd == -1)
|
2015-08-13 16:09:15 +02:00
|
|
|
inp = iobuf_open (filename);
|
2014-03-07 09:46:44 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
inp = NULL;
|
|
|
|
gpg_err_set_errno (ENOSYS);
|
|
|
|
}
|
|
|
|
#else
|
2022-10-07 04:16:55 +02:00
|
|
|
if (filefd == -1)
|
2015-08-13 16:09:15 +02:00
|
|
|
inp = iobuf_open (filename);
|
|
|
|
else
|
2022-10-07 04:16:55 +02:00
|
|
|
inp = iobuf_fdopen_nc (filefd, "rb");
|
2014-03-07 09:46:44 +01:00
|
|
|
#endif
|
2009-09-28 16:37:48 +02:00
|
|
|
if (inp)
|
2010-03-08 18:05:37 +01:00
|
|
|
iobuf_ioctl (inp, IOBUF_IOCTL_NO_CACHE, 1, NULL);
|
2009-09-28 16:37:48 +02:00
|
|
|
if (inp && is_secured_file (iobuf_get_fd (inp)))
|
|
|
|
{
|
|
|
|
iobuf_close (inp);
|
|
|
|
inp = NULL;
|
2010-04-01 15:24:55 +02:00
|
|
|
gpg_err_set_errno (EPERM);
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
if (!inp)
|
|
|
|
{
|
2009-09-30 17:28:38 +02:00
|
|
|
char xname[64];
|
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
rc = gpg_error_from_syserror ();
|
2009-10-02 16:57:55 +02:00
|
|
|
if (filefd != -1)
|
2009-09-30 17:28:38 +02:00
|
|
|
snprintf (xname, sizeof xname, "[fd %d]", filefd);
|
|
|
|
else if (!filename)
|
|
|
|
strcpy (xname, "[stdin]");
|
|
|
|
else
|
|
|
|
*xname = 0;
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("can't open '%s': %s\n"),
|
2009-09-30 17:28:38 +02:00
|
|
|
*xname? xname : filename, gpg_strerror (rc) );
|
2009-09-28 16:37:48 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2009-09-30 17:28:38 +02:00
|
|
|
|
|
|
|
if (opt.verbose)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info (_("reading from '%s'\n"), iobuf_get_fname_nonnull (inp));
|
2009-09-28 16:37:48 +02:00
|
|
|
|
|
|
|
handle_progress (pfx, inp, filename);
|
|
|
|
|
|
|
|
if (opt.textmode)
|
|
|
|
iobuf_push_filter (inp, text_filter, &tfx);
|
|
|
|
|
2017-08-01 19:08:16 +02:00
|
|
|
rc = open_outfile (outputfd, filename, opt.armor? 1:0, 0, &out);
|
2009-09-30 17:28:38 +02:00
|
|
|
if (rc)
|
2009-09-28 16:37:48 +02:00
|
|
|
goto leave;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if (opt.armor)
|
|
|
|
{
|
|
|
|
afx = new_armor_context ();
|
|
|
|
push_armor_filter (afx, out);
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Create a session key. */
|
2022-09-29 15:03:26 +02:00
|
|
|
cfx.dek = create_dek_with_warnings (pk_list);
|
2017-06-20 09:25:56 +02:00
|
|
|
|
2022-09-29 15:03:26 +02:00
|
|
|
rc = check_encryption_compliance (cfx.dek, pk_list);
|
|
|
|
if (rc)
|
|
|
|
goto leave;
|
2022-03-08 10:13:44 +01:00
|
|
|
|
2018-01-22 16:23:02 +01:00
|
|
|
cfx.dek->use_aead = use_aead (pk_list, cfx.dek->algo);
|
|
|
|
if (!cfx.dek->use_aead)
|
2018-01-10 17:07:11 +01:00
|
|
|
cfx.dek->use_mdc = !!use_mdc (pk_list, cfx.dek->algo);
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Only do the is-file-already-compressed check if we are using a
|
2018-01-10 17:07:11 +01:00
|
|
|
* MDC or AEAD. This forces compressed files to be re-compressed if
|
|
|
|
* we do not have a MDC to give some protection against chosen
|
|
|
|
* ciphertext attacks. */
|
|
|
|
if (do_compress
|
|
|
|
&& (cfx.dek->use_mdc || cfx.dek->use_aead)
|
|
|
|
&& is_file_compressed (filename, &rc2))
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
if (opt.verbose)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info(_("'%s' already compressed\n"), filename);
|
2011-02-02 17:40:32 +01:00
|
|
|
do_compress = 0;
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
if (rc2)
|
|
|
|
{
|
|
|
|
rc = rc2;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
make_session_key (cfx.dek);
|
2015-04-06 13:07:09 +02:00
|
|
|
if (DBG_CRYPTO)
|
2017-11-27 15:00:25 +01:00
|
|
|
log_printhex (cfx.dek->key, cfx.dek->keylen, "DEK is: ");
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
rc = write_pubkey_enc_from_list (ctrl, pk_list, cfx.dek, out);
|
2009-09-28 16:37:48 +02:00
|
|
|
if (rc)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
/* We put the passphrase (if any) after any public keys as this
|
2018-01-23 12:07:25 +01:00
|
|
|
* seems to be the most useful on the recipient side - there is no
|
|
|
|
* point in prompting a user for a passphrase if they have the
|
|
|
|
* secret key needed to decrypt. */
|
|
|
|
if (use_symkey && (rc = write_symkey_enc (symkey_s2k, cfx.dek->use_aead,
|
|
|
|
symkey_dek, cfx.dek, out)))
|
2009-09-28 16:37:48 +02:00
|
|
|
goto leave;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if (!opt.no_literal)
|
|
|
|
pt = setup_plaintext_name (filename, inp);
|
2011-02-02 17:40:32 +01:00
|
|
|
|
gpg: Fix possible dead code elimination.
* g10/encrypt.c: Change condition for detecting a real file.
--
Detected by Stack 3.0:
bug: anti-dce
model: |
%tobool155 = icmp ne i32 %call154, 0, !dbg !1298
--> true
************************************************************
land.lhs.true156:
%96 = icmp eq i8* %filename, null
call void @opt.bugon(i1 %96), !dbg !1298, !bug !1250
%97 = load i8* %filename, align 1, !dbg !1298
%conv157 = sext i8 %97 to i32, !dbg !1298
%tobool158 = icmp ne i32 %conv157, 0, !dbg !1298
br i1 %tobool158, label %land.lhs.true159, label %if.else177,\
!dbg !1298
stack:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
ncore: 1
core:
- /home/wk/s/gnupg/g10/encrypt.c:639:0
- null pointer dereference
2015-03-15 14:01:36 +01:00
|
|
|
/* Get the size of the file if possible, i.e., if it is a real file. */
|
|
|
|
if (filename && *filename
|
|
|
|
&& !iobuf_is_pipe_filename (filename) && !opt.textmode )
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
off_t tmpsize;
|
|
|
|
int overflow;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if ( !(tmpsize = iobuf_get_filelength(inp, &overflow))
|
|
|
|
&& !overflow && opt.verbose)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info(_("WARNING: '%s' is an empty file\n"), filename );
|
2009-09-28 16:37:48 +02:00
|
|
|
/* We can't encode the length of very large files because
|
2017-02-20 22:19:50 +01:00
|
|
|
OpenPGP uses only 32 bit for file sizes. So if the size
|
2009-09-28 16:37:48 +02:00
|
|
|
of a file is larger than 2^32 minus some bytes for packet
|
|
|
|
headers, we switch to partial length encoding. */
|
|
|
|
if (tmpsize < (IOBUF_FILELENGTH_LIMIT - 65536) )
|
|
|
|
filesize = tmpsize;
|
|
|
|
else
|
|
|
|
filesize = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
filesize = opt.set_filesize ? opt.set_filesize : 0; /* stdin */
|
|
|
|
|
2011-02-02 17:40:32 +01:00
|
|
|
if (!opt.no_literal)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
pt->timestamp = make_timestamp();
|
2016-07-13 13:31:12 +02:00
|
|
|
pt->mode = opt.mimemode? 'm' : opt.textmode ? 't' : 'b';
|
2009-09-28 16:37:48 +02:00
|
|
|
pt->len = filesize;
|
2014-08-12 10:36:30 +02:00
|
|
|
pt->new_ctb = !pt->len;
|
2009-09-28 16:37:48 +02:00
|
|
|
pt->buf = inp;
|
|
|
|
pkt.pkttype = PKT_PLAINTEXT;
|
|
|
|
pkt.pkt.plaintext = pt;
|
|
|
|
cfx.datalen = filesize && !do_compress? calc_packet_length( &pkt ) : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cfx.datalen = filesize && !do_compress ? filesize : 0;
|
|
|
|
|
|
|
|
/* Register the cipher filter. */
|
2018-01-10 17:33:50 +01:00
|
|
|
iobuf_push_filter (out,
|
|
|
|
cfx.dek->use_aead? cipher_filter_aead
|
|
|
|
/**/ : cipher_filter_cfb,
|
|
|
|
&cfx);
|
2009-09-28 16:37:48 +02:00
|
|
|
|
|
|
|
/* Register the compress filter. */
|
|
|
|
if (do_compress)
|
|
|
|
{
|
|
|
|
int compr_algo = opt.compress_algo;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if (compr_algo == -1)
|
|
|
|
{
|
|
|
|
compr_algo = select_algo_from_prefs (pk_list, PREFTYPE_ZIP, -1, NULL);
|
|
|
|
if (compr_algo == -1)
|
|
|
|
compr_algo = DEFAULT_COMPRESS_ALGO;
|
|
|
|
/* Theoretically impossible to get here since uncompressed
|
|
|
|
is implicit. */
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
else if (!opt.expert
|
2009-09-28 16:37:48 +02:00
|
|
|
&& select_algo_from_prefs(pk_list, PREFTYPE_ZIP,
|
|
|
|
compr_algo, NULL) != compr_algo)
|
|
|
|
{
|
|
|
|
log_info (_("WARNING: forcing compression algorithm %s (%d)"
|
|
|
|
" violates recipient preferences\n"),
|
|
|
|
compress_algo_to_string(compr_algo), compr_algo);
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Algo 0 means no compression. */
|
|
|
|
if (compr_algo)
|
|
|
|
{
|
2018-01-10 17:07:11 +01:00
|
|
|
if (cfx.dek && (cfx.dek->use_mdc || cfx.dek->use_aead))
|
2009-09-28 16:37:48 +02:00
|
|
|
zfx.new_ctb = 1;
|
|
|
|
push_compress_filter (out,&zfx,compr_algo);
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
/* Do the work. */
|
|
|
|
if (!opt.no_literal)
|
|
|
|
{
|
|
|
|
if ((rc = build_packet( out, &pkt )))
|
2015-01-22 12:06:11 +01:00
|
|
|
log_error ("build_packet failed: %s\n", gpg_strerror (rc));
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* User requested not to create a literal packet, so we copy the
|
|
|
|
plain data. */
|
|
|
|
byte copy_buffer[4096];
|
|
|
|
int bytes_copied;
|
2009-09-30 17:28:38 +02:00
|
|
|
while ((bytes_copied = iobuf_read (inp, copy_buffer, 4096)) != -1)
|
|
|
|
{
|
|
|
|
rc = iobuf_write (out, copy_buffer, bytes_copied);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("copying input to output failed: %s\n",
|
|
|
|
gpg_strerror (rc));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-28 16:37:48 +02:00
|
|
|
wipememory (copy_buffer, 4096); /* Burn the buffer. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish the stuff. */
|
|
|
|
leave:
|
|
|
|
iobuf_close (inp);
|
|
|
|
if (rc)
|
|
|
|
iobuf_cancel (out);
|
2011-02-02 17:40:32 +01:00
|
|
|
else
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
iobuf_close (out); /* fixme: check returncode */
|
|
|
|
write_status (STATUS_END_ENCRYPTION);
|
|
|
|
}
|
|
|
|
if (pt)
|
|
|
|
pt->buf = NULL;
|
2017-03-29 11:57:40 +02:00
|
|
|
free_packet (&pkt, NULL);
|
2009-09-28 16:37:48 +02:00
|
|
|
xfree (cfx.dek);
|
|
|
|
xfree (symkey_dek);
|
|
|
|
xfree (symkey_s2k);
|
2009-09-30 17:28:38 +02:00
|
|
|
if (!provided_keys)
|
|
|
|
release_pk_list (pk_list);
|
2009-09-28 16:37:48 +02:00
|
|
|
release_armor_context (afx);
|
|
|
|
release_progress_context (pfx);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter to do a complete public key encryption.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
encrypt_filter (void *opaque, int control,
|
|
|
|
iobuf_t a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
|
|
|
size_t size = *ret_len;
|
|
|
|
encrypt_filter_context_t *efx = opaque;
|
|
|
|
int rc = 0;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if (control == IOBUFCTRL_UNDERFLOW) /* decrypt */
|
|
|
|
{
|
|
|
|
BUG(); /* not used */
|
|
|
|
}
|
|
|
|
else if ( control == IOBUFCTRL_FLUSH ) /* encrypt */
|
|
|
|
{
|
|
|
|
if ( !efx->header_okay )
|
|
|
|
{
|
2022-08-31 13:35:41 +02:00
|
|
|
efx->header_okay = 1;
|
|
|
|
|
2022-09-29 15:03:26 +02:00
|
|
|
efx->cfx.dek = create_dek_with_warnings (efx->pk_list);
|
|
|
|
|
|
|
|
rc = check_encryption_compliance (efx->cfx.dek, efx->pk_list);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2018-01-22 16:23:02 +01:00
|
|
|
efx->cfx.dek->use_aead = use_aead (efx->pk_list, efx->cfx.dek->algo);
|
|
|
|
if (!efx->cfx.dek->use_aead)
|
2018-01-10 17:07:11 +01:00
|
|
|
efx->cfx.dek->use_mdc = !!use_mdc (efx->pk_list,efx->cfx.dek->algo);
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
make_session_key ( efx->cfx.dek );
|
2015-04-06 13:07:09 +02:00
|
|
|
if (DBG_CRYPTO)
|
2017-11-27 15:00:25 +01:00
|
|
|
log_printhex (efx->cfx.dek->key, efx->cfx.dek->keylen, "DEK is: ");
|
2009-09-28 16:37:48 +02:00
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
rc = write_pubkey_enc_from_list (efx->ctrl,
|
|
|
|
efx->pk_list, efx->cfx.dek, a);
|
2009-09-28 16:37:48 +02:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2016-02-15 03:05:29 +01:00
|
|
|
if(efx->symkey_s2k && efx->symkey_dek)
|
|
|
|
{
|
2018-01-23 12:07:25 +01:00
|
|
|
rc = write_symkey_enc (efx->symkey_s2k, efx->cfx.dek->use_aead,
|
|
|
|
efx->symkey_dek, efx->cfx.dek, a);
|
|
|
|
if (rc)
|
2016-02-15 03:05:29 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:33:50 +01:00
|
|
|
iobuf_push_filter (a,
|
|
|
|
efx->cfx.dek->use_aead? cipher_filter_aead
|
|
|
|
/**/ : cipher_filter_cfb,
|
|
|
|
&efx->cfx);
|
2016-02-15 03:05:29 +01:00
|
|
|
|
|
|
|
}
|
2009-09-28 16:37:48 +02:00
|
|
|
rc = iobuf_write (a, buf, size);
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
else if (control == IOBUFCTRL_FREE)
|
|
|
|
{
|
|
|
|
xfree (efx->symkey_dek);
|
|
|
|
xfree (efx->symkey_s2k);
|
|
|
|
}
|
|
|
|
else if ( control == IOBUFCTRL_DESC )
|
|
|
|
{
|
common: Fix iobuf API of filter function for alignment.
* common/iobuf.h (IOBUFCTRL_DESC): Change the call semantics.
* common/iobuf.c (iobuf_desc): Add the second argument DESC.
(print_chain, iobuf_close, do_open, iobuf_sockopen, iobuf_ioctl)
(iobuf_push_filter2, pop_filter, iobuf_write_temp): Change calls
of iobuf_desc.
(file_filter, file_es_filter, sock_filter, block_filter): Fill the
description.
* common/t-iobuf.c (every_other_filter, double_filter): Likewise.
* g10/armor.c, g10/cipher.c, g10/compress-bz2.c, g10/compress.c,
g10/decrypt-data.c, g10/encrypt.c, g10/mdfilter.c, g10/progress.c,
g10/textfilter.c: Likewise.
--
Newer GCC warns against possible alignment difference of pointers.
This change can silence those warnings.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-01-12 02:32:20 +01:00
|
|
|
mem2str (buf, "encrypt_filter", *ret_len);
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2016-03-02 15:38:16 +01:00
|
|
|
* Write a pubkey-enc packet for the public key PK to OUT.
|
2009-09-28 16:37:48 +02:00
|
|
|
*/
|
2016-03-02 15:38:16 +01:00
|
|
|
int
|
2017-03-31 20:03:52 +02:00
|
|
|
write_pubkey_enc (ctrl_t ctrl,
|
|
|
|
PKT_public_key *pk, int throw_keyid, DEK *dek, iobuf_t out)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
PACKET pkt;
|
2016-03-02 15:38:16 +01:00
|
|
|
PKT_pubkey_enc *enc;
|
2009-09-28 16:37:48 +02:00
|
|
|
int rc;
|
2016-03-02 15:38:16 +01:00
|
|
|
gcry_mpi_t frame;
|
|
|
|
|
|
|
|
print_pubkey_algo_note ( pk->pubkey_algo );
|
|
|
|
enc = xmalloc_clear ( sizeof *enc );
|
|
|
|
enc->pubkey_algo = pk->pubkey_algo;
|
|
|
|
keyid_from_pk( pk, enc->keyid );
|
|
|
|
enc->throw_keyid = throw_keyid;
|
|
|
|
|
|
|
|
/* Okay, what's going on: We have the session key somewhere in
|
|
|
|
* the structure DEK and want to encode this session key in an
|
|
|
|
* integer value of n bits. pubkey_nbits gives us the number of
|
|
|
|
* bits we have to use. We then encode the session key in some
|
|
|
|
* way and we get it back in the big intger value FRAME. Then
|
|
|
|
* we use FRAME, the public key PK->PKEY and the algorithm
|
|
|
|
* number PK->PUBKEY_ALGO and pass it to pubkey_encrypt which
|
|
|
|
* returns the encrypted value in the array ENC->DATA. This
|
|
|
|
* array has a size which depends on the used algorithm (e.g. 2
|
|
|
|
* for Elgamal). We don't need frame anymore because we have
|
|
|
|
* everything now in enc->data which is the passed to
|
|
|
|
* build_packet(). */
|
|
|
|
frame = encode_session_key (pk->pubkey_algo, dek,
|
|
|
|
pubkey_nbits (pk->pubkey_algo, pk->pkey));
|
|
|
|
rc = pk_encrypt (pk->pubkey_algo, enc->data, frame, pk, pk->pkey);
|
|
|
|
gcry_mpi_release (frame);
|
|
|
|
if (rc)
|
|
|
|
log_error ("pubkey_encrypt failed: %s\n", gpg_strerror (rc) );
|
|
|
|
else
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2016-03-02 15:38:16 +01:00
|
|
|
if ( opt.verbose )
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
2017-03-31 20:03:52 +02:00
|
|
|
char *ustr = get_user_id_string_native (ctrl, enc->keyid);
|
2018-01-23 12:07:25 +01:00
|
|
|
log_info (_("%s/%s.%s encrypted for: \"%s\"\n"),
|
2016-03-02 15:38:16 +01:00
|
|
|
openpgp_pk_algo_name (enc->pubkey_algo),
|
|
|
|
openpgp_cipher_algo_name (dek->algo),
|
2018-01-23 12:07:25 +01:00
|
|
|
dek->use_aead? openpgp_aead_algo_name (dek->use_aead)
|
|
|
|
/**/ : "CFB",
|
2016-03-02 15:38:16 +01:00
|
|
|
ustr );
|
|
|
|
xfree (ustr);
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2016-03-02 15:38:16 +01:00
|
|
|
/* And write it. */
|
|
|
|
init_packet (&pkt);
|
|
|
|
pkt.pkttype = PKT_PUBKEY_ENC;
|
|
|
|
pkt.pkt.pubkey_enc = enc;
|
|
|
|
rc = build_packet (out, &pkt);
|
2009-09-28 16:37:48 +02:00
|
|
|
if (rc)
|
2016-03-02 15:38:16 +01:00
|
|
|
log_error ("build_packet(pubkey_enc) failed: %s\n",
|
|
|
|
gpg_strerror (rc));
|
|
|
|
}
|
|
|
|
free_pubkey_enc(enc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write pubkey-enc packets from the list of PKs to OUT.
|
|
|
|
*/
|
|
|
|
static int
|
2017-03-31 20:03:52 +02:00
|
|
|
write_pubkey_enc_from_list (ctrl_t ctrl, PK_LIST pk_list, DEK *dek, iobuf_t out)
|
2016-03-02 15:38:16 +01:00
|
|
|
{
|
2018-05-29 13:01:12 +02:00
|
|
|
if (opt.throw_keyids && (PGP7 || PGP8))
|
2016-03-02 15:38:16 +01:00
|
|
|
{
|
2017-07-28 17:46:43 +02:00
|
|
|
log_info(_("option '%s' may not be used in %s mode\n"),
|
2017-06-20 09:25:56 +02:00
|
|
|
"--throw-keyids",
|
|
|
|
gnupg_compliance_option_string (opt.compliance));
|
2016-03-02 15:38:16 +01:00
|
|
|
compliance_failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; pk_list; pk_list = pk_list->next )
|
|
|
|
{
|
|
|
|
PKT_public_key *pk = pk_list->pk;
|
|
|
|
int throw_keyid = (opt.throw_keyids || (pk_list->flags&1));
|
2017-03-31 20:03:52 +02:00
|
|
|
int rc = write_pubkey_enc (ctrl, pk, throw_keyid, dek, out);
|
2009-09-28 16:37:48 +02:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
2016-03-02 15:38:16 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-10-01 22:33:53 +02:00
|
|
|
encrypt_crypt_files (ctrl_t ctrl, int nfiles, char **files, strlist_t remusr)
|
2009-09-28 16:37:48 +02:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (opt.outfile)
|
|
|
|
{
|
|
|
|
log_error(_("--output doesn't work for this command\n"));
|
2011-02-02 17:40:32 +01:00
|
|
|
return;
|
2009-09-28 16:37:48 +02:00
|
|
|
}
|
2011-02-02 17:40:32 +01:00
|
|
|
|
2009-09-28 16:37:48 +02:00
|
|
|
if (!nfiles)
|
|
|
|
{
|
|
|
|
char line[2048];
|
|
|
|
unsigned int lno = 0;
|
|
|
|
while ( fgets(line, DIM(line), stdin) )
|
|
|
|
{
|
|
|
|
lno++;
|
|
|
|
if (!*line || line[strlen(line)-1] != '\n')
|
|
|
|
{
|
|
|
|
log_error("input line %u too long or missing LF\n", lno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line[strlen(line)-1] = '\0';
|
|
|
|
print_file_status(STATUS_FILE_START, line, 2);
|
2010-10-01 22:33:53 +02:00
|
|
|
rc = encrypt_crypt (ctrl, -1, line, remusr, 0, NULL, -1);
|
2009-09-30 17:28:38 +02:00
|
|
|
if (rc)
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error ("encryption of '%s' failed: %s\n",
|
2015-01-22 12:06:11 +01:00
|
|
|
print_fname_stdin(line), gpg_strerror (rc) );
|
2009-09-28 16:37:48 +02:00
|
|
|
write_status( STATUS_FILE_DONE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (nfiles--)
|
|
|
|
{
|
|
|
|
print_file_status(STATUS_FILE_START, *files, 2);
|
2010-10-01 22:33:53 +02:00
|
|
|
if ( (rc = encrypt_crypt (ctrl, -1, *files, remusr, 0, NULL, -1)) )
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error("encryption of '%s' failed: %s\n",
|
2015-01-22 12:06:11 +01:00
|
|
|
print_fname_stdin(*files), gpg_strerror (rc) );
|
2009-09-28 16:37:48 +02:00
|
|
|
write_status( STATUS_FILE_DONE );
|
|
|
|
files++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|