2001-12-06 00:48:01 +01:00
|
|
|
|
/* decrypt.c - Decrypt a message
|
2003-06-03 21:55:50 +02:00
|
|
|
|
* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
|
2001-12-06 00:48:01 +01: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>
|
|
|
|
|
|
|
|
|
|
#include <gcrypt.h>
|
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
|
|
#include "gpgsm.h"
|
|
|
|
|
#include "keydb.h"
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
2001-12-06 21:48:10 +01:00
|
|
|
|
struct decrypt_filter_parm_s {
|
|
|
|
|
int algo;
|
2001-12-11 13:31:04 +01:00
|
|
|
|
int mode;
|
2001-12-06 21:48:10 +01:00
|
|
|
|
int blklen;
|
|
|
|
|
GCRY_CIPHER_HD hd;
|
|
|
|
|
char iv[16];
|
|
|
|
|
size_t ivlen;
|
|
|
|
|
int any_data; /* dod we push anything through the filter at all? */
|
|
|
|
|
unsigned char lastblock[16]; /* to strip the padding we have to
|
|
|
|
|
keep this one */
|
2001-12-10 20:18:27 +01:00
|
|
|
|
char helpblock[16]; /* needed because there is no block buffering in
|
2001-12-06 21:48:10 +01:00
|
|
|
|
libgcrypt (yet) */
|
|
|
|
|
int helpblocklen;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2001-12-06 00:48:01 +01:00
|
|
|
|
|
2001-12-06 21:48:10 +01:00
|
|
|
|
/* decrypt the session key and fill in the parm structure. The
|
|
|
|
|
algo and the IV is expected to be already in PARM. */
|
|
|
|
|
static int
|
2001-12-18 18:37:48 +01:00
|
|
|
|
prepare_decryption (const char *hexkeygrip, KsbaConstSexp enc_val,
|
2001-12-06 21:48:10 +01:00
|
|
|
|
struct decrypt_filter_parm_s *parm)
|
|
|
|
|
{
|
|
|
|
|
char *seskey = NULL;
|
|
|
|
|
size_t n, seskeylen;
|
|
|
|
|
int rc;
|
|
|
|
|
|
2001-12-18 18:37:48 +01:00
|
|
|
|
rc = gpgsm_agent_pkdecrypt (hexkeygrip, enc_val,
|
2001-12-06 21:48:10 +01:00
|
|
|
|
&seskey, &seskeylen);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error decrypting session key: %s\n", gnupg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DBG_CRYPTO)
|
|
|
|
|
log_printhex ("pkcs1 encoded session key:", seskey, seskeylen);
|
|
|
|
|
|
|
|
|
|
n=0;
|
2002-08-09 23:01:03 +02:00
|
|
|
|
if (seskeylen == 24)
|
2001-12-06 21:48:10 +01:00
|
|
|
|
{
|
2002-08-09 23:01:03 +02:00
|
|
|
|
/* Smells like a 3-des key. This might happen because a SC has
|
|
|
|
|
already done the unpacking. fixme! */
|
2001-12-06 21:48:10 +01:00
|
|
|
|
}
|
2002-08-09 23:01:03 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (n + 7 > seskeylen )
|
|
|
|
|
{
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_INV_SESSION_KEY);
|
2002-08-09 23:01:03 +02:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* FIXME: Actually the leading zero is required but due to the way
|
|
|
|
|
we encode the output in libgcrypt as an MPI we are not able to
|
|
|
|
|
encode that leading zero. However, when using a Smartcard we are
|
|
|
|
|
doing it the rightway and therefore we have to skip the zero. This
|
|
|
|
|
should be fixed in gpg-agent of course. */
|
|
|
|
|
if (!seskey[n])
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
|
|
if (seskey[n] != 2 ) /* wrong block type version */
|
|
|
|
|
{
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_INV_SESSION_KEY);
|
2002-08-09 23:01:03 +02:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (n++; n < seskeylen && seskey[n]; n++) /* skip the random bytes */
|
|
|
|
|
;
|
|
|
|
|
n++; /* and the zero byte */
|
|
|
|
|
if (n >= seskeylen )
|
|
|
|
|
{
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_INV_SESSION_KEY);
|
2002-08-09 23:01:03 +02:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-12-06 21:48:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DBG_CRYPTO)
|
|
|
|
|
log_printhex ("session key:", seskey+n, seskeylen-n);
|
|
|
|
|
|
2001-12-11 13:31:04 +01:00
|
|
|
|
parm->hd = gcry_cipher_open (parm->algo, parm->mode, 0);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
if (!parm->hd)
|
|
|
|
|
{
|
|
|
|
|
rc = gcry_errno ();
|
|
|
|
|
log_error ("error creating decryptor: %s\n", gcry_strerror (rc));
|
|
|
|
|
rc = map_gcry_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = gcry_cipher_setkey (parm->hd, seskey+n, seskeylen-n);
|
|
|
|
|
if (rc == GCRYERR_WEAK_KEY)
|
|
|
|
|
{
|
|
|
|
|
log_info (_("WARNING: message was encrypted with "
|
|
|
|
|
"a weak key in the symmetric cipher.\n"));
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error("key setup failed: %s\n", gcry_strerror(rc) );
|
|
|
|
|
rc = map_gcry_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gcry_cipher_setiv (parm->hd, parm->iv, parm->ivlen);
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
xfree (seskey);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-10 20:18:27 +01:00
|
|
|
|
/* This function is called by the KSBA writer just before the actual
|
2001-12-06 21:48:10 +01:00
|
|
|
|
write is done. The function must take INLEN bytes from INBUF,
|
|
|
|
|
decrypt it and store it inoutbuf which has a maximum size of
|
|
|
|
|
maxoutlen. The valid bytes in outbuf should be return in outlen.
|
|
|
|
|
Due to different buffer sizes or different length of input and
|
|
|
|
|
output, it may happen that fewer bytes are process or fewer bytes
|
|
|
|
|
are written. */
|
|
|
|
|
static KsbaError
|
|
|
|
|
decrypt_filter (void *arg,
|
|
|
|
|
const void *inbuf, size_t inlen, size_t *inused,
|
|
|
|
|
void *outbuf, size_t maxoutlen, size_t *outlen)
|
|
|
|
|
{
|
|
|
|
|
struct decrypt_filter_parm_s *parm = arg;
|
|
|
|
|
int blklen = parm->blklen;
|
|
|
|
|
size_t orig_inlen = inlen;
|
|
|
|
|
|
|
|
|
|
/* fixme: Should we issue an error when we have not seen one full block? */
|
|
|
|
|
if (!inlen)
|
|
|
|
|
return KSBA_Bug;
|
|
|
|
|
|
|
|
|
|
if (maxoutlen < 2*parm->blklen)
|
|
|
|
|
return KSBA_Bug;
|
|
|
|
|
/* make some space becuase we will later need an extra block at the end */
|
|
|
|
|
maxoutlen -= blklen;
|
|
|
|
|
|
|
|
|
|
if (parm->helpblocklen)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
|
|
for (i=parm->helpblocklen,j=0; i < blklen && j < inlen; i++, j++)
|
|
|
|
|
parm->helpblock[i] = ((const char*)inbuf)[j];
|
|
|
|
|
inlen -= j;
|
|
|
|
|
if (blklen > maxoutlen)
|
|
|
|
|
return KSBA_Bug;
|
|
|
|
|
if (i < blklen)
|
|
|
|
|
{
|
|
|
|
|
parm->helpblocklen = i;
|
|
|
|
|
*outlen = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parm->helpblocklen = 0;
|
|
|
|
|
if (parm->any_data)
|
|
|
|
|
{
|
|
|
|
|
memcpy (outbuf, parm->lastblock, blklen);
|
|
|
|
|
*outlen =blklen;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
*outlen = 0;
|
|
|
|
|
gcry_cipher_decrypt (parm->hd, parm->lastblock, blklen,
|
|
|
|
|
parm->helpblock, blklen);
|
|
|
|
|
parm->any_data = 1;
|
|
|
|
|
}
|
|
|
|
|
*inused = orig_inlen - inlen;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inlen > maxoutlen)
|
|
|
|
|
inlen = maxoutlen;
|
|
|
|
|
if (inlen % blklen)
|
|
|
|
|
{ /* store the remainder away */
|
|
|
|
|
parm->helpblocklen = inlen%blklen;
|
|
|
|
|
inlen = inlen/blklen*blklen;
|
|
|
|
|
memcpy (parm->helpblock, (const char*)inbuf+inlen, parm->helpblocklen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*inused = inlen + parm->helpblocklen;
|
|
|
|
|
if (inlen)
|
|
|
|
|
{
|
|
|
|
|
assert (inlen >= blklen);
|
|
|
|
|
if (parm->any_data)
|
|
|
|
|
{
|
|
|
|
|
gcry_cipher_decrypt (parm->hd, (char*)outbuf+blklen, inlen,
|
|
|
|
|
inbuf, inlen);
|
|
|
|
|
memcpy (outbuf, parm->lastblock, blklen);
|
|
|
|
|
memcpy (parm->lastblock,(char*)outbuf+inlen, blklen);
|
|
|
|
|
*outlen = inlen;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gcry_cipher_decrypt (parm->hd, outbuf, inlen, inbuf, inlen);
|
|
|
|
|
memcpy (parm->lastblock, (char*)outbuf+inlen-blklen, blklen);
|
|
|
|
|
*outlen = inlen - blklen;
|
|
|
|
|
parm->any_data = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
*outlen = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2001-12-06 00:48:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Perform a decrypt operation. */
|
|
|
|
|
int
|
|
|
|
|
gpgsm_decrypt (CTRL ctrl, int in_fd, FILE *out_fp)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
KsbaError err;
|
|
|
|
|
Base64Context b64reader = NULL;
|
|
|
|
|
Base64Context b64writer = NULL;
|
|
|
|
|
KsbaReader reader;
|
|
|
|
|
KsbaWriter writer;
|
|
|
|
|
KsbaCMS cms = NULL;
|
|
|
|
|
KsbaStopReason stopreason;
|
|
|
|
|
KEYDB_HANDLE kh;
|
|
|
|
|
int recp;
|
|
|
|
|
FILE *in_fp = NULL;
|
2001-12-06 21:48:10 +01:00
|
|
|
|
struct decrypt_filter_parm_s dfparm;
|
|
|
|
|
|
|
|
|
|
memset (&dfparm, 0, sizeof dfparm);
|
2001-12-06 00:48:01 +01:00
|
|
|
|
|
|
|
|
|
kh = keydb_new (0);
|
|
|
|
|
if (!kh)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("failed to allocated keyDB handle\n"));
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_GENERAL);
|
2001-12-06 00:48:01 +01:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
in_fp = fdopen ( dup (in_fd), "rb");
|
|
|
|
|
if (!in_fp)
|
|
|
|
|
{
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (gpg_err_code_from_errno (errno));
|
2001-12-06 00:48:01 +01:00
|
|
|
|
log_error ("fdopen() failed: %s\n", strerror (errno));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = gpgsm_create_reader (&b64reader, ctrl, in_fp, &reader);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't create reader: %s\n", gnupg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-13 10:09:11 +01:00
|
|
|
|
rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, &writer);
|
2001-12-06 00:48:01 +01:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't create writer: %s\n", gnupg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cms = ksba_cms_new ();
|
|
|
|
|
if (!cms)
|
|
|
|
|
{
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_ENOMEM);
|
2001-12-06 00:48:01 +01: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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* parser loop */
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
err = ksba_cms_parse (cms, &stopreason);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_debug ("ksba_cms_parse failed: %s\n", ksba_strerror (err));
|
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stopreason == KSBA_SR_BEGIN_DATA
|
|
|
|
|
|| stopreason == KSBA_SR_DETACHED_DATA)
|
|
|
|
|
{
|
2001-12-11 13:31:04 +01:00
|
|
|
|
int algo, mode;
|
2001-12-06 21:48:10 +01:00
|
|
|
|
const char *algoid;
|
2002-03-13 11:19:50 +01:00
|
|
|
|
int any_key = 0;
|
2001-12-06 21:48:10 +01:00
|
|
|
|
|
|
|
|
|
algoid = ksba_cms_get_content_oid (cms, 2/* encryption algo*/);
|
|
|
|
|
algo = gcry_cipher_map_name (algoid);
|
2001-12-11 13:31:04 +01:00
|
|
|
|
mode = gcry_cipher_mode_from_oid (algoid);
|
|
|
|
|
if (!algo || !mode)
|
2001-12-06 21:48:10 +01:00
|
|
|
|
{
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
log_error ("unsupported algorithm `%s'\n", algoid? algoid:"?");
|
2002-07-22 12:23:10 +02:00
|
|
|
|
if (algoid && !strcmp (algoid, "1.2.840.113549.3.2"))
|
|
|
|
|
log_info (_("(this is the RC2 algorithm)\n"));
|
2002-08-20 15:09:53 +02:00
|
|
|
|
else if (!algoid)
|
|
|
|
|
log_info (_("(this does not seem to be an encrypted"
|
|
|
|
|
" message)\n"));
|
2002-07-22 12:23:10 +02:00
|
|
|
|
gpgsm_status2 (ctrl, STATUS_ERROR, "decrypt.algorithm",
|
2002-08-20 15:09:53 +02:00
|
|
|
|
gnupg_error_token (rc), algoid?algoid:"?", NULL);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
dfparm.algo = algo;
|
2001-12-11 13:31:04 +01:00
|
|
|
|
dfparm.mode = mode;
|
2001-12-06 21:48:10 +01:00
|
|
|
|
dfparm.blklen = gcry_cipher_get_algo_blklen (algo);
|
|
|
|
|
if (dfparm.blklen > sizeof (dfparm.helpblock))
|
2003-06-03 21:55:50 +02:00
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
|
|
|
|
|
rc = ksba_cms_get_content_enc_iv (cms,
|
|
|
|
|
dfparm.iv,
|
|
|
|
|
sizeof (dfparm.iv),
|
|
|
|
|
&dfparm.ivlen);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error getting IV: %s\n", ksba_strerror (err));
|
|
|
|
|
rc = map_ksba_err (err);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 11:19:50 +01:00
|
|
|
|
for (recp=0; !any_key; recp++)
|
2001-12-06 00:48:01 +01:00
|
|
|
|
{
|
|
|
|
|
char *issuer;
|
2001-12-18 18:37:48 +01:00
|
|
|
|
KsbaSexp serial;
|
|
|
|
|
KsbaSexp enc_val;
|
2001-12-06 00:48:01 +01:00
|
|
|
|
char *hexkeygrip = NULL;
|
|
|
|
|
|
|
|
|
|
err = ksba_cms_get_issuer_serial (cms, recp, &issuer, &serial);
|
2002-03-13 11:19:50 +01:00
|
|
|
|
if (err == -1 && recp)
|
|
|
|
|
break; /* no more recipients */
|
2001-12-06 00:48:01 +01:00
|
|
|
|
if (err)
|
|
|
|
|
log_error ("recp %d - error getting info: %s\n",
|
|
|
|
|
recp, ksba_strerror (err));
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
KsbaCert cert = NULL;
|
|
|
|
|
|
|
|
|
|
log_debug ("recp %d - issuer: `%s'\n",
|
|
|
|
|
recp, issuer? issuer:"[NONE]");
|
|
|
|
|
log_debug ("recp %d - serial: ", recp);
|
2001-12-20 14:25:08 +01:00
|
|
|
|
gpgsm_dump_serial (serial);
|
2001-12-06 00:48:01 +01:00
|
|
|
|
log_printf ("\n");
|
|
|
|
|
|
|
|
|
|
keydb_search_reset (kh);
|
|
|
|
|
rc = keydb_search_issuer_sn (kh, issuer, serial);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2002-03-13 11:19:50 +01:00
|
|
|
|
log_error ("failed to find the certificate: %s\n",
|
2001-12-06 00:48:01 +01:00
|
|
|
|
gnupg_strerror(rc));
|
|
|
|
|
goto oops;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = keydb_get_cert (kh, &cert);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2002-03-13 11:19:50 +01:00
|
|
|
|
log_error ("failed to get cert: %s\n", gnupg_strerror (rc));
|
|
|
|
|
goto oops;
|
|
|
|
|
}
|
2002-04-12 20:54:34 +02:00
|
|
|
|
/* Just in case there is a problem with the own
|
|
|
|
|
certificate we print this message - should never
|
|
|
|
|
happen of course */
|
2002-06-20 12:43:02 +02:00
|
|
|
|
rc = gpgsm_cert_use_decrypt_p (cert);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
gpgsm_status2 (ctrl, STATUS_ERROR, "decrypt.keyusage",
|
|
|
|
|
gnupg_error_token (rc), NULL);
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
2001-12-06 00:48:01 +01:00
|
|
|
|
|
|
|
|
|
hexkeygrip = gpgsm_get_keygrip_hexstring (cert);
|
|
|
|
|
|
|
|
|
|
oops:
|
|
|
|
|
xfree (issuer);
|
|
|
|
|
xfree (serial);
|
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-13 11:19:50 +01:00
|
|
|
|
if (!hexkeygrip)
|
|
|
|
|
;
|
|
|
|
|
else if (!(enc_val = ksba_cms_get_enc_val (cms, recp)))
|
2001-12-06 00:48:01 +01:00
|
|
|
|
log_error ("recp %d - error getting encrypted session key\n",
|
|
|
|
|
recp);
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-13 11:19:50 +01:00
|
|
|
|
rc = prepare_decryption (hexkeygrip, enc_val, &dfparm);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
xfree (enc_val);
|
2001-12-06 00:48:01 +01:00
|
|
|
|
if (rc)
|
2002-02-06 15:52:03 +01:00
|
|
|
|
{
|
2002-03-13 11:19:50 +01:00
|
|
|
|
log_debug ("decrypting session key failed: %s\n",
|
2002-02-06 15:52:03 +01:00
|
|
|
|
gnupg_strerror (rc));
|
|
|
|
|
}
|
2001-12-06 00:48:01 +01:00
|
|
|
|
else
|
2001-12-06 21:48:10 +01:00
|
|
|
|
{ /* setup the bulk decrypter */
|
2002-03-13 11:19:50 +01:00
|
|
|
|
any_key = 1;
|
2001-12-06 21:48:10 +01:00
|
|
|
|
ksba_writer_set_filter (writer,
|
|
|
|
|
decrypt_filter,
|
|
|
|
|
&dfparm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-03-13 11:19:50 +01:00
|
|
|
|
if (!any_key)
|
|
|
|
|
{
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_NO_SECKEY);
|
2002-03-13 11:19:50 +01:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-12-06 21:48:10 +01:00
|
|
|
|
}
|
|
|
|
|
else if (stopreason == KSBA_SR_END_DATA)
|
|
|
|
|
{
|
|
|
|
|
ksba_writer_set_filter (writer, NULL, NULL);
|
|
|
|
|
if (dfparm.any_data)
|
|
|
|
|
{ /* write the last block with padding removed */
|
|
|
|
|
int i, npadding = dfparm.lastblock[dfparm.blklen-1];
|
|
|
|
|
if (!npadding || npadding > dfparm.blklen)
|
|
|
|
|
{
|
|
|
|
|
log_error ("invalid padding with value %d\n", npadding);
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_INVALID_DATA);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
rc = ksba_writer_write (writer,
|
|
|
|
|
dfparm.lastblock,
|
|
|
|
|
dfparm.blklen - npadding);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
rc = map_ksba_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
for (i=dfparm.blklen - npadding; i < dfparm.blklen; i++)
|
|
|
|
|
{
|
|
|
|
|
if (dfparm.lastblock[i] != npadding)
|
2001-12-06 00:48:01 +01:00
|
|
|
|
{
|
2001-12-06 21:48:10 +01:00
|
|
|
|
log_error ("inconsistent padding\n");
|
2003-06-03 21:55:50 +02:00
|
|
|
|
rc = gpg_error (GPG_ERR_INVALID_DATA);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
goto leave;
|
2001-12-06 00:48:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
while (stopreason != KSBA_SR_READY);
|
|
|
|
|
|
2001-12-13 10:09:11 +01:00
|
|
|
|
rc = gpgsm_finish_writer (b64writer);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("write failed: %s\n", gnupg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-12-15 13:36:39 +01:00
|
|
|
|
gpgsm_status (ctrl, STATUS_DECRYPTION_OKAY, NULL);
|
2001-12-13 10:09:11 +01:00
|
|
|
|
|
|
|
|
|
|
2001-12-06 00:48:01 +01:00
|
|
|
|
leave:
|
2001-12-15 13:36:39 +01:00
|
|
|
|
if (rc)
|
|
|
|
|
gpgsm_status (ctrl, STATUS_DECRYPTION_FAILED, NULL);
|
2001-12-06 00:48:01 +01:00
|
|
|
|
ksba_cms_release (cms);
|
|
|
|
|
gpgsm_destroy_reader (b64reader);
|
|
|
|
|
gpgsm_destroy_writer (b64writer);
|
|
|
|
|
keydb_release (kh);
|
|
|
|
|
if (in_fp)
|
|
|
|
|
fclose (in_fp);
|
2001-12-06 21:48:10 +01:00
|
|
|
|
if (dfparm.hd)
|
|
|
|
|
gcry_cipher_close (dfparm.hd);
|
2001-12-06 00:48:01 +01:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|