2001-11-27 17:40:09 +00:00
|
|
|
|
/* encrypt.c - Encrypt a message
|
2003-06-03 19:55:50 +00:00
|
|
|
|
* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
|
2001-11-27 17:40:09 +00: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 2 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
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
2003-06-27 20:53:09 +00:00
|
|
|
|
#include "gpgsm.h"
|
2001-11-27 17:40:09 +00:00
|
|
|
|
#include <gcrypt.h>
|
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
|
|
#include "keydb.h"
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
struct dek_s {
|
|
|
|
|
const char *algoid;
|
|
|
|
|
int algo;
|
2003-06-27 20:53:09 +00:00
|
|
|
|
gcry_cipher_hd_t chd;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
char key[32];
|
|
|
|
|
int keylen;
|
|
|
|
|
char iv[32];
|
|
|
|
|
int ivlen;
|
|
|
|
|
};
|
|
|
|
|
typedef struct dek_s *DEK;
|
|
|
|
|
|
|
|
|
|
struct encrypt_cb_parm_s {
|
|
|
|
|
FILE *fp;
|
|
|
|
|
DEK dek;
|
|
|
|
|
int eof_seen;
|
|
|
|
|
int ready;
|
|
|
|
|
int readerror;
|
|
|
|
|
int bufsize;
|
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
int buflen;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* initialize the data encryptionkey (session key) */
|
|
|
|
|
static int
|
|
|
|
|
init_dek (DEK dek)
|
|
|
|
|
{
|
2001-12-11 12:31:04 +00:00
|
|
|
|
int rc=0, mode, i;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
|
|
|
|
|
dek->algo = gcry_cipher_map_name (dek->algoid);
|
2001-12-11 12:31:04 +00:00
|
|
|
|
mode = gcry_cipher_mode_from_oid (dek->algoid);
|
|
|
|
|
if (!dek->algo || !mode)
|
2001-12-10 19:18:27 +00:00
|
|
|
|
{
|
|
|
|
|
log_error ("unsupported algorithm `%s'\n", dek->algoid);
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dek->keylen = gcry_cipher_get_algo_keylen (dek->algo);
|
|
|
|
|
if (!dek->keylen || dek->keylen > sizeof (dek->key))
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
|
|
|
|
|
dek->ivlen = gcry_cipher_get_algo_blklen (dek->algo);
|
|
|
|
|
if (!dek->ivlen || dek->ivlen > sizeof (dek->iv))
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
|
|
|
|
|
if (dek->keylen < 100/8)
|
|
|
|
|
{ /* make sure we don't use weak keys */
|
|
|
|
|
log_error ("key length of `%s' too small\n", dek->algoid);
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-06-27 20:53:09 +00:00
|
|
|
|
rc = gcry_cipher_open (&dek->chd, dek->algo, mode, GCRY_CIPHER_SECURE);
|
|
|
|
|
if (rc)
|
2001-12-10 19:18:27 +00:00
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
log_error ("failed to create cipher context: %s\n", gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i=0; i < 8; i++)
|
|
|
|
|
{
|
|
|
|
|
gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM );
|
|
|
|
|
rc = gcry_cipher_setkey (dek->chd, dek->key, dek->keylen);
|
2003-06-27 20:53:09 +00:00
|
|
|
|
if (gpg_err_code (rc) != GPG_ERR_WEAK_KEY)
|
2001-12-10 19:18:27 +00:00
|
|
|
|
break;
|
|
|
|
|
log_info(_("weak key created - retrying\n") );
|
|
|
|
|
}
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
log_error ("failed to set the key: %s\n", gpg_strerror (rc));
|
2001-12-10 19:18:27 +00:00
|
|
|
|
gcry_cipher_close (dek->chd);
|
|
|
|
|
dek->chd = NULL;
|
2003-06-27 20:53:09 +00:00
|
|
|
|
return rc;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gcry_randomize (dek->iv, dek->ivlen, GCRY_STRONG_RANDOM);
|
|
|
|
|
rc = gcry_cipher_setiv (dek->chd, dek->iv, dek->ivlen);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
log_error ("failed to set the IV: %s\n", gpg_strerror (rc));
|
2001-12-10 19:18:27 +00:00
|
|
|
|
gcry_cipher_close (dek->chd);
|
|
|
|
|
dek->chd = NULL;
|
2003-06-27 20:53:09 +00:00
|
|
|
|
return rc;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Encode the session key. NBITS is the number of bits which should be
|
|
|
|
|
used for packing the session key. returns: An mpi with the session
|
|
|
|
|
key (caller must free) */
|
2003-06-27 20:53:09 +00:00
|
|
|
|
static gcry_mpi_t
|
2001-12-10 19:18:27 +00:00
|
|
|
|
encode_session_key (DEK dek, unsigned int nbits)
|
|
|
|
|
{
|
|
|
|
|
int nframe = (nbits+7) / 8;
|
|
|
|
|
byte *p;
|
|
|
|
|
byte *frame;
|
|
|
|
|
int i,n;
|
2003-06-27 20:53:09 +00:00
|
|
|
|
gcry_mpi_t a;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
|
|
|
|
|
if (dek->keylen + 7 > nframe || !nframe)
|
|
|
|
|
log_bug ("can't encode a %d bit key in a %d bits frame\n",
|
|
|
|
|
dek->keylen*8, nbits );
|
|
|
|
|
|
|
|
|
|
/* We encode the session key in this way:
|
|
|
|
|
*
|
|
|
|
|
* 0 2 RND(n bytes) 0 KEY(k bytes)
|
|
|
|
|
*
|
|
|
|
|
* (But how can we store the leading 0 - the external representaion
|
|
|
|
|
* of MPIs doesn't allow leading zeroes =:-)
|
|
|
|
|
*
|
|
|
|
|
* RND are non-zero random bytes.
|
|
|
|
|
* KEY is the encryption key (session key)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
frame = gcry_xmalloc_secure (nframe);
|
|
|
|
|
n = 0;
|
|
|
|
|
frame[n++] = 0;
|
|
|
|
|
frame[n++] = 2;
|
|
|
|
|
i = nframe - 3 - dek->keylen;
|
|
|
|
|
assert (i > 0);
|
|
|
|
|
p = gcry_random_bytes_secure (i, GCRY_STRONG_RANDOM);
|
|
|
|
|
/* replace zero bytes by new values */
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
int j, k;
|
|
|
|
|
byte *pp;
|
|
|
|
|
|
|
|
|
|
/* count the zero bytes */
|
|
|
|
|
for(j=k=0; j < i; j++ )
|
|
|
|
|
{
|
|
|
|
|
if( !p[j] )
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
if( !k )
|
|
|
|
|
break; /* okay: no zero bytes */
|
|
|
|
|
|
|
|
|
|
k += k/128; /* better get some more */
|
|
|
|
|
pp = gcry_random_bytes_secure (k, GCRY_STRONG_RANDOM);
|
|
|
|
|
for (j=0; j < i && k; j++)
|
|
|
|
|
{
|
|
|
|
|
if( !p[j] )
|
|
|
|
|
p[j] = pp[--k];
|
|
|
|
|
}
|
|
|
|
|
xfree (pp);
|
|
|
|
|
}
|
|
|
|
|
memcpy (frame+n, p, i);
|
|
|
|
|
xfree (p);
|
|
|
|
|
|
|
|
|
|
n += i;
|
|
|
|
|
frame[n++] = 0;
|
|
|
|
|
memcpy (frame+n, dek->key, dek->keylen);
|
|
|
|
|
n += dek->keylen;
|
|
|
|
|
assert (n == nframe);
|
2003-07-28 08:59:18 +00:00
|
|
|
|
if (gcry_mpi_scan (&a, GCRYMPI_FMT_USG, frame, n, &nframe) )
|
2001-12-10 19:18:27 +00:00
|
|
|
|
BUG ();
|
|
|
|
|
gcry_free(frame);
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* encrypt the DEK under the key contained in CERT and return it as a
|
|
|
|
|
canonical S-Exp in encval */
|
|
|
|
|
static int
|
|
|
|
|
encrypt_dek (const DEK dek, KsbaCert cert, char **encval)
|
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
gcry_sexp_t s_ciph, s_data, s_pkey;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
int rc;
|
2001-12-18 17:37:48 +00:00
|
|
|
|
KsbaSexp buf;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
*encval = NULL;
|
|
|
|
|
|
|
|
|
|
/* get the key from the cert */
|
|
|
|
|
buf = ksba_cert_get_public_key (cert);
|
|
|
|
|
if (!buf)
|
|
|
|
|
{
|
|
|
|
|
log_error ("no public key for recipient\n");
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_NO_PUBKEY);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
2001-12-18 17:37:48 +00:00
|
|
|
|
len = gcry_sexp_canon_len (buf, 0, NULL, NULL);
|
|
|
|
|
if (!len)
|
|
|
|
|
{
|
|
|
|
|
log_error ("libksba did not return a proper S-Exp\n");
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-18 17:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
rc = gcry_sexp_sscan (&s_pkey, NULL, buf, len);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
xfree (buf); buf = NULL;
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
log_error ("gcry_sexp_scan failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* put the encoded cleartext into a simple list */
|
|
|
|
|
{
|
|
|
|
|
/* fixme: actually the pkcs-1 encoding should go into libgcrypt */
|
2003-06-27 20:53:09 +00:00
|
|
|
|
gcry_mpi_t data = encode_session_key (dek, gcry_pk_get_nbits (s_pkey));
|
2001-12-10 19:18:27 +00:00
|
|
|
|
if (!data)
|
|
|
|
|
{
|
|
|
|
|
gcry_mpi_release (data);
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return gpg_error (GPG_ERR_GENERAL);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
if (gcry_sexp_build (&s_data, NULL, "%m", data))
|
|
|
|
|
BUG ();
|
|
|
|
|
gcry_mpi_release (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* pass it to libgcrypt */
|
|
|
|
|
rc = gcry_pk_encrypt (&s_ciph, s_data, s_pkey);
|
|
|
|
|
gcry_sexp_release (s_data);
|
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
|
|
|
|
|
|
/* reformat it */
|
|
|
|
|
len = gcry_sexp_sprint (s_ciph, GCRYSEXP_FMT_CANON, NULL, 0);
|
|
|
|
|
assert (len);
|
|
|
|
|
buf = xtrymalloc (len);
|
|
|
|
|
if (!buf)
|
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
gpg_error_t tmperr = OUT_OF_CORE (errno);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
gcry_sexp_release (s_ciph);
|
2003-06-03 19:55:50 +00:00
|
|
|
|
return tmperr;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
}
|
|
|
|
|
len = gcry_sexp_sprint (s_ciph, GCRYSEXP_FMT_CANON, buf, len);
|
|
|
|
|
assert (len);
|
|
|
|
|
|
|
|
|
|
*encval = buf;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* do the actual encryption */
|
|
|
|
|
static int
|
|
|
|
|
encrypt_cb (void *cb_value, char *buffer, size_t count, size_t *nread)
|
|
|
|
|
{
|
|
|
|
|
struct encrypt_cb_parm_s *parm = cb_value;
|
|
|
|
|
int blklen = parm->dek->ivlen;
|
|
|
|
|
unsigned char *p;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
*nread = 0;
|
|
|
|
|
if (!buffer)
|
|
|
|
|
return -1; /* not supported */
|
|
|
|
|
|
|
|
|
|
if (parm->ready)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (count < blklen)
|
|
|
|
|
BUG ();
|
|
|
|
|
|
|
|
|
|
if (!parm->eof_seen)
|
|
|
|
|
{ /* fillup the buffer */
|
|
|
|
|
p = parm->buffer;
|
|
|
|
|
for (n=parm->buflen; n < parm->bufsize; n++)
|
|
|
|
|
{
|
|
|
|
|
int c = getc (parm->fp);
|
|
|
|
|
if (c == EOF)
|
|
|
|
|
{
|
|
|
|
|
if (ferror (parm->fp))
|
|
|
|
|
{
|
|
|
|
|
parm->readerror = errno;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
parm->eof_seen = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
p[n] = c;
|
|
|
|
|
}
|
|
|
|
|
parm->buflen = n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n = parm->buflen < count? parm->buflen : count;
|
|
|
|
|
n = n/blklen * blklen;
|
|
|
|
|
if (n)
|
|
|
|
|
{ /* encrypt the stuff */
|
|
|
|
|
gcry_cipher_encrypt (parm->dek->chd, buffer, n, parm->buffer, n);
|
|
|
|
|
*nread = n;
|
|
|
|
|
/* Who cares about cycles, take the easy way and shift the buffer */
|
|
|
|
|
parm->buflen -= n;
|
|
|
|
|
memmove (parm->buffer, parm->buffer+n, parm->buflen);
|
|
|
|
|
}
|
|
|
|
|
else if (parm->eof_seen)
|
|
|
|
|
{ /* no complete block but eof: add padding */
|
|
|
|
|
/* fixme: we should try to do this also in the above code path */
|
|
|
|
|
int i, npad = blklen - (parm->buflen % blklen);
|
|
|
|
|
p = parm->buffer;
|
|
|
|
|
for (n=parm->buflen, i=0; n < parm->bufsize && i < npad; n++, i++)
|
|
|
|
|
p[n] = npad;
|
|
|
|
|
gcry_cipher_encrypt (parm->dek->chd, buffer, n, parm->buffer, n);
|
|
|
|
|
*nread = n;
|
|
|
|
|
parm->ready = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
|
2001-11-27 17:40:09 +00:00
|
|
|
|
|
|
|
|
|
/* Perform an encrypt operation.
|
|
|
|
|
|
|
|
|
|
Encrypt the data received on DATA-FD and write it to OUT_FP. The
|
2001-12-11 12:31:04 +00:00
|
|
|
|
recipients are take from the certificate given in recplist; if this
|
|
|
|
|
is NULL it will be encrypted for a default recipient */
|
2001-11-27 17:40:09 +00:00
|
|
|
|
int
|
2001-12-11 12:31:04 +00:00
|
|
|
|
gpgsm_encrypt (CTRL ctrl, CERTLIST recplist, int data_fd, FILE *out_fp)
|
2001-11-27 17:40:09 +00:00
|
|
|
|
{
|
2001-12-10 19:18:27 +00:00
|
|
|
|
int rc = 0;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
Base64Context b64writer = NULL;
|
|
|
|
|
KsbaError err;
|
|
|
|
|
KsbaWriter writer;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
KsbaReader reader = NULL;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
KsbaCMS cms = NULL;
|
|
|
|
|
KsbaStopReason stopreason;
|
|
|
|
|
KEYDB_HANDLE kh = NULL;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
struct encrypt_cb_parm_s encparm;
|
|
|
|
|
DEK dek = NULL;
|
|
|
|
|
int recpno;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
FILE *data_fp = NULL;
|
2001-12-11 12:31:04 +00:00
|
|
|
|
CERTLIST cl;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
memset (&encparm, 0, sizeof encparm);
|
2002-03-12 13:36:29 +00:00
|
|
|
|
|
|
|
|
|
if (!recplist)
|
|
|
|
|
{
|
|
|
|
|
log_error(_("no valid recipients given\n"));
|
|
|
|
|
gpgsm_status (ctrl, STATUS_NO_RECP, "0");
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_NO_PUBKEY);
|
2002-03-12 13:36:29 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-27 17:40:09 +00:00
|
|
|
|
kh = keydb_new (0);
|
|
|
|
|
if (!kh)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("failed to allocated keyDB handle\n"));
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_GENERAL);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_fp = fdopen ( dup (data_fd), "rb");
|
|
|
|
|
if (!data_fp)
|
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (gpg_err_code_from_errno (errno));
|
2001-11-27 17:40:09 +00:00
|
|
|
|
log_error ("fdopen() failed: %s\n", strerror (errno));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
reader = ksba_reader_new ();
|
|
|
|
|
if (!reader)
|
|
|
|
|
rc = KSBA_Out_Of_Core;
|
|
|
|
|
if (!rc)
|
|
|
|
|
rc = ksba_reader_set_cb (reader, encrypt_cb, &encparm);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2001-12-10 19:18:27 +00:00
|
|
|
|
rc = map_ksba_err (rc);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-12-10 19:18:27 +00:00
|
|
|
|
encparm.fp = data_fp;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
|
2002-01-10 19:47:20 +00:00
|
|
|
|
ctrl->pem_name = "ENCRYPTED MESSAGE";
|
2001-11-27 17:40:09 +00:00
|
|
|
|
rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, &writer);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
log_error ("can't create writer: %s\n", gpg_strerror (rc));
|
2001-11-27 17:40:09 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cms = ksba_cms_new ();
|
|
|
|
|
if (!cms)
|
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_ENOMEM);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = ksba_cms_set_reader_writer (cms, reader, writer);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("ksba_cms_set_reader_writer failed: %s\n",
|
|
|
|
|
ksba_strerror (err));
|
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
/* We are going to create enveloped data with uninterpreted data as
|
|
|
|
|
inner content */
|
2001-11-27 17:40:09 +00:00
|
|
|
|
err = ksba_cms_set_content_type (cms, 0, KSBA_CT_ENVELOPED_DATA);
|
|
|
|
|
if (!err)
|
2001-12-10 19:18:27 +00:00
|
|
|
|
err = ksba_cms_set_content_type (cms, 1, KSBA_CT_DATA);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("ksba_cms_set_content_type failed: %s\n",
|
|
|
|
|
ksba_strerror (err));
|
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
/* create a session key */
|
|
|
|
|
dek = xtrycalloc (1, sizeof *dek); /* hmmm: should we put it into secmem?*/
|
|
|
|
|
if (!dek)
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = OUT_OF_CORE (errno);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2001-12-11 12:31:04 +00:00
|
|
|
|
dek->algoid = opt.def_cipher_algoid;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
rc = init_dek (dek);
|
|
|
|
|
}
|
|
|
|
|
if (rc)
|
2001-11-27 17:40:09 +00:00
|
|
|
|
{
|
2001-12-10 19:18:27 +00:00
|
|
|
|
log_error ("failed to create the session key: %s\n",
|
2003-06-27 20:53:09 +00:00
|
|
|
|
gpg_strerror (rc));
|
2001-11-27 17:40:09 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-12-10 19:18:27 +00:00
|
|
|
|
|
|
|
|
|
err = ksba_cms_set_content_enc_algo (cms, dek->algoid, dek->iv, dek->ivlen);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
2001-12-10 19:18:27 +00:00
|
|
|
|
log_error ("ksba_cms_set_content_enc_algo failed: %s\n",
|
|
|
|
|
ksba_strerror (err));
|
2001-11-27 17:40:09 +00:00
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
encparm.dek = dek;
|
2002-02-07 18:43:22 +00:00
|
|
|
|
/* Use a ~8k (AES) or ~4k (3DES) buffer */
|
|
|
|
|
encparm.bufsize = 500 * dek->ivlen;
|
2001-12-10 19:18:27 +00:00
|
|
|
|
encparm.buffer = xtrymalloc (encparm.bufsize);
|
|
|
|
|
if (!encparm.buffer)
|
2001-11-27 17:40:09 +00:00
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = OUT_OF_CORE (errno);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-12-10 19:18:27 +00:00
|
|
|
|
|
|
|
|
|
/* gather certificates of recipients, encrypt the session key for
|
|
|
|
|
each and store them in the CMS object */
|
2001-12-11 12:31:04 +00:00
|
|
|
|
for (recpno = 0, cl = recplist; cl; recpno++, cl = cl->next)
|
2001-11-27 17:40:09 +00:00
|
|
|
|
{
|
2001-12-10 19:18:27 +00:00
|
|
|
|
char *encval;
|
|
|
|
|
|
2001-12-11 12:31:04 +00:00
|
|
|
|
rc = encrypt_dek (dek, cl->cert, &encval);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("encryption failed for recipient no. %d: %s\n",
|
2003-06-27 20:53:09 +00:00
|
|
|
|
recpno, gpg_strerror (rc));
|
2001-12-10 19:18:27 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-11 12:31:04 +00:00
|
|
|
|
err = ksba_cms_add_recipient (cms, cl->cert);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ksba_cms_add_recipient failed: %s\n",
|
|
|
|
|
ksba_strerror (err));
|
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
xfree (encval);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = ksba_cms_set_enc_val (cms, recpno, encval);
|
|
|
|
|
xfree (encval);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ksba_cms_set_enc_val failed: %s\n",
|
|
|
|
|
ksba_strerror (err));
|
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-11-27 17:40:09 +00:00
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
/* main control loop for encryption */
|
|
|
|
|
recpno = 0;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
err = ksba_cms_build (cms, &stopreason);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("ksba_cms_build failed: %s\n", ksba_strerror (err));
|
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (stopreason != KSBA_SR_READY);
|
|
|
|
|
|
2001-12-10 19:18:27 +00:00
|
|
|
|
if (encparm.readerror)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error reading input: %s\n", strerror (encparm.readerror));
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (gpg_err_code_from_errno (encparm.readerror));
|
2001-12-10 19:18:27 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-27 18:38:59 +00:00
|
|
|
|
rc = gpgsm_finish_writer (b64writer);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2003-06-27 20:53:09 +00:00
|
|
|
|
log_error ("write failed: %s\n", gpg_strerror (rc));
|
2001-11-27 18:38:59 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
log_info ("encrypted data created\n");
|
2001-11-27 17:40:09 +00:00
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
ksba_cms_release (cms);
|
|
|
|
|
gpgsm_destroy_writer (b64writer);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
ksba_reader_release (reader);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
keydb_release (kh);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
xfree (dek);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
if (data_fp)
|
|
|
|
|
fclose (data_fp);
|
2001-12-10 19:18:27 +00:00
|
|
|
|
xfree (encparm.buffer);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|