2003-08-05 19:11:04 +02:00
|
|
|
|
/* minip12.c - A minimal pkcs-12 implementation.
|
2011-03-10 15:27:10 +01:00
|
|
|
|
* Copyright (C) 2002, 2003, 2004, 2006, 2011 Free Software Foundation, Inc.
|
2014-06-03 18:57:33 +02:00
|
|
|
|
* Copyright (C) 2014 Werner Koch
|
2003-08-05 19:11:04 +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
|
2007-07-04 21:49:40 +02:00
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 19:11:04 +02:00
|
|
|
|
* (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
|
2007-07-04 21:49:40 +02:00
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <gcrypt.h>
|
2007-03-20 11:00:55 +01:00
|
|
|
|
#include <errno.h>
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-03-10 13:24:58 +01:00
|
|
|
|
#include "../common/logging.h"
|
|
|
|
|
#include "../common/utf8conv.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#include "minip12.h"
|
|
|
|
|
|
|
|
|
|
#ifndef DIM
|
|
|
|
|
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-03-19 19:54:34 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
UNIVERSAL = 0,
|
|
|
|
|
APPLICATION = 1,
|
2007-06-06 20:12:30 +02:00
|
|
|
|
ASNCONTEXT = 2,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
PRIVATE = 3
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
TAG_NONE = 0,
|
|
|
|
|
TAG_BOOLEAN = 1,
|
|
|
|
|
TAG_INTEGER = 2,
|
|
|
|
|
TAG_BIT_STRING = 3,
|
|
|
|
|
TAG_OCTET_STRING = 4,
|
|
|
|
|
TAG_NULL = 5,
|
|
|
|
|
TAG_OBJECT_ID = 6,
|
|
|
|
|
TAG_OBJECT_DESCRIPTOR = 7,
|
|
|
|
|
TAG_EXTERNAL = 8,
|
|
|
|
|
TAG_REAL = 9,
|
|
|
|
|
TAG_ENUMERATED = 10,
|
|
|
|
|
TAG_EMBEDDED_PDV = 11,
|
|
|
|
|
TAG_UTF8_STRING = 12,
|
|
|
|
|
TAG_REALTIVE_OID = 13,
|
|
|
|
|
TAG_SEQUENCE = 16,
|
|
|
|
|
TAG_SET = 17,
|
|
|
|
|
TAG_NUMERIC_STRING = 18,
|
|
|
|
|
TAG_PRINTABLE_STRING = 19,
|
|
|
|
|
TAG_TELETEX_STRING = 20,
|
|
|
|
|
TAG_VIDEOTEX_STRING = 21,
|
|
|
|
|
TAG_IA5_STRING = 22,
|
|
|
|
|
TAG_UTC_TIME = 23,
|
|
|
|
|
TAG_GENERALIZED_TIME = 24,
|
|
|
|
|
TAG_GRAPHIC_STRING = 25,
|
|
|
|
|
TAG_VISIBLE_STRING = 26,
|
|
|
|
|
TAG_GENERAL_STRING = 27,
|
|
|
|
|
TAG_UNIVERSAL_STRING = 28,
|
|
|
|
|
TAG_CHARACTER_STRING = 29,
|
|
|
|
|
TAG_BMP_STRING = 30
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char const oid_data[9] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
|
|
|
|
|
static unsigned char const oid_encryptedData[9] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x06 };
|
2006-07-21 11:41:11 +02:00
|
|
|
|
static unsigned char const oid_pkcs_12_keyBag[11] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0C, 0x0A, 0x01, 0x01 };
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static unsigned char const oid_pkcs_12_pkcs_8ShroudedKeyBag[11] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0C, 0x0A, 0x01, 0x02 };
|
2004-02-04 20:13:16 +01:00
|
|
|
|
static unsigned char const oid_pkcs_12_CertBag[11] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0C, 0x0A, 0x01, 0x03 };
|
2004-02-12 10:27:36 +01:00
|
|
|
|
static unsigned char const oid_pkcs_12_CrlBag[11] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0C, 0x0A, 0x01, 0x04 };
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static unsigned char const oid_pbeWithSHAAnd3_KeyTripleDES_CBC[10] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0C, 0x01, 0x03 };
|
2004-02-04 20:13:16 +01:00
|
|
|
|
static unsigned char const oid_pbeWithSHAAnd40BitRC2_CBC[10] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0C, 0x01, 0x06 };
|
2004-02-10 20:26:55 +01:00
|
|
|
|
static unsigned char const oid_x509Certificate_for_pkcs_12[10] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x16, 0x01 };
|
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
static unsigned char const oid_pkcs5PBKDF2[9] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0C };
|
|
|
|
|
static unsigned char const oid_pkcs5PBES2[9] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0D };
|
|
|
|
|
static unsigned char const oid_aes128_CBC[9] = {
|
|
|
|
|
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x02 };
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
static unsigned char const oid_rsaEncryption[9] = {
|
|
|
|
|
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
static unsigned char const data_3desiter2048[30] = {
|
2003-08-05 19:11:04 +02:00
|
|
|
|
0x30, 0x1C, 0x06, 0x0A, 0x2A, 0x86, 0x48, 0x86,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
0xF7, 0x0D, 0x01, 0x0C, 0x01, 0x03, 0x30, 0x0E,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
0x04, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
2004-02-19 17:26:32 +01:00
|
|
|
|
0xFF, 0xFF, 0x02, 0x02, 0x08, 0x00 };
|
|
|
|
|
#define DATA_3DESITER2048_SALT_OFF 18
|
|
|
|
|
|
|
|
|
|
static unsigned char const data_rc2iter2048[30] = {
|
|
|
|
|
0x30, 0x1C, 0x06, 0x0A, 0x2A, 0x86, 0x48, 0x86,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
0xF7, 0x0D, 0x01, 0x0C, 0x01, 0x06, 0x30, 0x0E,
|
2004-02-19 17:26:32 +01:00
|
|
|
|
0x04, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
|
|
|
|
0xFF, 0xFF, 0x02, 0x02, 0x08, 0x00 };
|
|
|
|
|
#define DATA_RC2ITER2048_SALT_OFF 18
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2006-06-20 17:48:09 +02:00
|
|
|
|
static unsigned char const data_mactemplate[51] = {
|
|
|
|
|
0x30, 0x31, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
|
|
|
|
|
0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04,
|
|
|
|
|
0x14, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0x04, 0x08, 0xff,
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
0x02, 0x08, 0x00 };
|
2006-06-20 17:48:09 +02:00
|
|
|
|
#define DATA_MACTEMPLATE_MAC_OFF 17
|
|
|
|
|
#define DATA_MACTEMPLATE_SALT_OFF 39
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
static unsigned char const data_attrtemplate[106] = {
|
|
|
|
|
0x31, 0x7c, 0x30, 0x55, 0x06, 0x09, 0x2a, 0x86,
|
|
|
|
|
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x14, 0x31,
|
|
|
|
|
0x48, 0x1e, 0x46, 0x00, 0x47, 0x00, 0x6e, 0x00,
|
|
|
|
|
0x75, 0x00, 0x50, 0x00, 0x47, 0x00, 0x20, 0x00,
|
|
|
|
|
0x65, 0x00, 0x78, 0x00, 0x70, 0x00, 0x6f, 0x00,
|
|
|
|
|
0x72, 0x00, 0x74, 0x00, 0x65, 0x00, 0x64, 0x00,
|
|
|
|
|
0x20, 0x00, 0x63, 0x00, 0x65, 0x00, 0x72, 0x00,
|
|
|
|
|
0x74, 0x00, 0x69, 0x00, 0x66, 0x00, 0x69, 0x00,
|
|
|
|
|
0x63, 0x00, 0x61, 0x00, 0x74, 0x00, 0x65, 0x00,
|
|
|
|
|
0x20, 0x00, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00,
|
|
|
|
|
0x66, 0x00, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00,
|
|
|
|
|
0x66, 0x30, 0x23, 0x06, 0x09, 0x2a, 0x86, 0x48,
|
|
|
|
|
0x86, 0xf7, 0x0d, 0x01, 0x09, 0x15, 0x31, 0x16,
|
|
|
|
|
0x04, 0x14 }; /* Need to append SHA-1 digest. */
|
|
|
|
|
#define DATA_ATTRTEMPLATE_KEYID_OFF 73
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
struct buffer_s
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
size_t length;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
};
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
struct tag_info
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
int class;
|
|
|
|
|
int is_constructed;
|
|
|
|
|
unsigned long tag;
|
|
|
|
|
unsigned long length; /* length part of the TLV */
|
|
|
|
|
int nhdr;
|
|
|
|
|
int ndef; /* It is an indefinite length */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse the buffer at the address BUFFER which is of SIZE and return
|
|
|
|
|
the tag and the length part from the TLV triplet. Update BUFFER
|
2005-12-16 16:52:48 +01:00
|
|
|
|
and SIZE on success. Checks that the encoded length does not
|
|
|
|
|
exhaust the length of the provided buffer. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
static int
|
2003-08-05 19:11:04 +02:00
|
|
|
|
parse_tag (unsigned char const **buffer, size_t *size, struct tag_info *ti)
|
|
|
|
|
{
|
|
|
|
|
int c;
|
|
|
|
|
unsigned long tag;
|
|
|
|
|
const unsigned char *buf = *buffer;
|
|
|
|
|
size_t length = *size;
|
|
|
|
|
|
|
|
|
|
ti->length = 0;
|
|
|
|
|
ti->ndef = 0;
|
|
|
|
|
ti->nhdr = 0;
|
|
|
|
|
|
|
|
|
|
/* Get the tag */
|
|
|
|
|
if (!length)
|
|
|
|
|
return -1; /* premature eof */
|
|
|
|
|
c = *buf++; length--;
|
|
|
|
|
ti->nhdr++;
|
|
|
|
|
|
|
|
|
|
ti->class = (c & 0xc0) >> 6;
|
|
|
|
|
ti->is_constructed = !!(c & 0x20);
|
|
|
|
|
tag = c & 0x1f;
|
|
|
|
|
|
|
|
|
|
if (tag == 0x1f)
|
|
|
|
|
{
|
|
|
|
|
tag = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
tag <<= 7;
|
|
|
|
|
if (!length)
|
|
|
|
|
return -1; /* premature eof */
|
|
|
|
|
c = *buf++; length--;
|
|
|
|
|
ti->nhdr++;
|
|
|
|
|
tag |= c & 0x7f;
|
|
|
|
|
}
|
|
|
|
|
while (c & 0x80);
|
|
|
|
|
}
|
|
|
|
|
ti->tag = tag;
|
|
|
|
|
|
|
|
|
|
/* Get the length */
|
|
|
|
|
if (!length)
|
|
|
|
|
return -1; /* prematureeof */
|
|
|
|
|
c = *buf++; length--;
|
|
|
|
|
ti->nhdr++;
|
|
|
|
|
|
|
|
|
|
if ( !(c & 0x80) )
|
|
|
|
|
ti->length = c;
|
|
|
|
|
else if (c == 0x80)
|
|
|
|
|
ti->ndef = 1;
|
|
|
|
|
else if (c == 0xff)
|
|
|
|
|
return -1; /* forbidden length value */
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned long len = 0;
|
|
|
|
|
int count = c & 0x7f;
|
|
|
|
|
|
|
|
|
|
for (; count; count--)
|
|
|
|
|
{
|
|
|
|
|
len <<= 8;
|
|
|
|
|
if (!length)
|
|
|
|
|
return -1; /* premature_eof */
|
|
|
|
|
c = *buf++; length--;
|
|
|
|
|
ti->nhdr++;
|
|
|
|
|
len |= c & 0xff;
|
|
|
|
|
}
|
|
|
|
|
ti->length = len;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (ti->class == UNIVERSAL && !ti->tag)
|
|
|
|
|
ti->length = 0;
|
|
|
|
|
|
|
|
|
|
if (ti->length > length)
|
|
|
|
|
return -1; /* data larger than buffer. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
*buffer = buf;
|
|
|
|
|
*size = length;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
/* Given an ASN.1 chunk of a structure like:
|
|
|
|
|
|
|
|
|
|
24 NDEF: OCTET STRING -- This is not passed to us
|
|
|
|
|
04 1: OCTET STRING -- INPUT point s to here
|
|
|
|
|
: 30
|
|
|
|
|
04 1: OCTET STRING
|
|
|
|
|
: 80
|
|
|
|
|
[...]
|
|
|
|
|
04 2: OCTET STRING
|
|
|
|
|
: 00 00
|
2011-02-04 12:57:53 +01:00
|
|
|
|
: } -- This denotes a Null tag and are the last
|
2005-12-16 16:52:48 +01:00
|
|
|
|
-- two bytes in INPUT.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
Create a new buffer with the content of that octet string. INPUT
|
2015-11-16 12:41:46 +01:00
|
|
|
|
is the original buffer with a length as stored at LENGTH. Returns
|
2005-12-16 16:52:48 +01:00
|
|
|
|
NULL on error or a new malloced buffer with the length of this new
|
|
|
|
|
buffer stored at LENGTH and the number of bytes parsed from input
|
|
|
|
|
are added to the value stored at INPUT_CONSUMED. INPUT_CONSUMED is
|
|
|
|
|
allowed to be passed as NULL if the caller is not interested in
|
|
|
|
|
this value. */
|
|
|
|
|
static unsigned char *
|
|
|
|
|
cram_octet_string (const unsigned char *input, size_t *length,
|
|
|
|
|
size_t *input_consumed)
|
|
|
|
|
{
|
|
|
|
|
const unsigned char *s = input;
|
|
|
|
|
size_t n = *length;
|
|
|
|
|
unsigned char *output, *d;
|
|
|
|
|
struct tag_info ti;
|
|
|
|
|
|
|
|
|
|
/* Allocate output buf. We know that it won't be longer than the
|
|
|
|
|
input buffer. */
|
|
|
|
|
d = output = gcry_malloc (n);
|
|
|
|
|
if (!output)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if (parse_tag (&s, &n, &ti))
|
|
|
|
|
goto bailout;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
if (ti.class == UNIVERSAL && ti.tag == TAG_OCTET_STRING
|
2005-12-16 16:52:48 +01:00
|
|
|
|
&& !ti.ndef && !ti.is_constructed)
|
|
|
|
|
{
|
|
|
|
|
memcpy (d, s, ti.length);
|
|
|
|
|
s += ti.length;
|
|
|
|
|
d += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
|
|
|
|
else if (ti.class == UNIVERSAL && !ti.tag && !ti.is_constructed)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
break; /* Ready */
|
2005-12-16 16:52:48 +01:00
|
|
|
|
else
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*length = d - output;
|
|
|
|
|
if (input_consumed)
|
|
|
|
|
*input_consumed += s - input;
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
|
|
bailout:
|
|
|
|
|
if (input_consumed)
|
|
|
|
|
*input_consumed += s - input;
|
|
|
|
|
gcry_free (output);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
static int
|
2005-12-16 16:52:48 +01:00
|
|
|
|
string_to_key (int id, char *salt, size_t saltlen, int iter, const char *pw,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int req_keylen, unsigned char *keybuf)
|
|
|
|
|
{
|
|
|
|
|
int rc, i, j;
|
|
|
|
|
gcry_md_hd_t md;
|
|
|
|
|
gcry_mpi_t num_b1 = NULL;
|
|
|
|
|
int pwlen;
|
|
|
|
|
unsigned char hash[20], buf_b[64], buf_i[128], *p;
|
|
|
|
|
size_t cur_keylen;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
cur_keylen = 0;
|
|
|
|
|
pwlen = strlen (pw);
|
|
|
|
|
if (pwlen > 63/2)
|
|
|
|
|
{
|
|
|
|
|
log_error ("password too long\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (saltlen < 8)
|
|
|
|
|
{
|
|
|
|
|
log_error ("salt too short\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* Store salt and password in BUF_I */
|
|
|
|
|
p = buf_i;
|
|
|
|
|
for(i=0; i < 64; i++)
|
2005-12-16 16:52:48 +01:00
|
|
|
|
*p++ = salt [i%saltlen];
|
2003-08-05 19:11:04 +02:00
|
|
|
|
for(i=j=0; i < 64; i += 2)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
*p++ = pw[j];
|
|
|
|
|
if (++j > pwlen) /* Note, that we include the trailing zero */
|
|
|
|
|
j = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
rc = gcry_md_open (&md, GCRY_MD_SHA1, 0);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ( "gcry_md_open failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
for(i=0; i < 64; i++)
|
|
|
|
|
gcry_md_putc (md, id);
|
|
|
|
|
gcry_md_write (md, buf_i, 128);
|
|
|
|
|
memcpy (hash, gcry_md_read (md, 0), 20);
|
|
|
|
|
gcry_md_close (md);
|
|
|
|
|
for (i=1; i < iter; i++)
|
|
|
|
|
gcry_md_hash_buffer (GCRY_MD_SHA1, hash, hash, 20);
|
|
|
|
|
|
|
|
|
|
for (i=0; i < 20 && cur_keylen < req_keylen; i++)
|
|
|
|
|
keybuf[cur_keylen++] = hash[i];
|
|
|
|
|
if (cur_keylen == req_keylen)
|
|
|
|
|
{
|
|
|
|
|
gcry_mpi_release (num_b1);
|
|
|
|
|
return 0; /* ready */
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* need more bytes. */
|
|
|
|
|
for(i=0; i < 64; i++)
|
|
|
|
|
buf_b[i] = hash[i % 20];
|
|
|
|
|
rc = gcry_mpi_scan (&num_b1, GCRYMPI_FMT_USG, buf_b, 64, &n);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ( "gcry_mpi_scan failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
gcry_mpi_add_ui (num_b1, num_b1, 1);
|
|
|
|
|
for (i=0; i < 128; i += 64)
|
|
|
|
|
{
|
|
|
|
|
gcry_mpi_t num_ij;
|
|
|
|
|
|
|
|
|
|
rc = gcry_mpi_scan (&num_ij, GCRYMPI_FMT_USG, buf_i + i, 64, &n);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ( "gcry_mpi_scan failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
gcry_mpi_add (num_ij, num_ij, num_b1);
|
|
|
|
|
gcry_mpi_clear_highbit (num_ij, 64*8);
|
|
|
|
|
rc = gcry_mpi_print (GCRYMPI_FMT_USG, buf_i + i, 64, &n, num_ij);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ( "gcry_mpi_print failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
gcry_mpi_release (num_ij);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
static int
|
2005-12-16 16:52:48 +01:00
|
|
|
|
set_key_iv (gcry_cipher_hd_t chd, char *salt, size_t saltlen, int iter,
|
|
|
|
|
const char *pw, int keybytes)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
unsigned char keybuf[24];
|
|
|
|
|
int rc;
|
|
|
|
|
|
2004-02-10 20:26:55 +01:00
|
|
|
|
assert (keybytes == 5 || keybytes == 24);
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (string_to_key (1, salt, saltlen, iter, pw, keybytes, keybuf))
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return -1;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
rc = gcry_cipher_setkey (chd, keybuf, keybytes);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ( "gcry_cipher_setkey failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (string_to_key (2, salt, saltlen, iter, pw, 8, keybuf))
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return -1;
|
|
|
|
|
rc = gcry_cipher_setiv (chd, keybuf, 8);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("gcry_cipher_setiv failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
static int
|
|
|
|
|
set_key_iv_pbes2 (gcry_cipher_hd_t chd, char *salt, size_t saltlen, int iter,
|
|
|
|
|
const void *iv, size_t ivlen, const char *pw, int algo)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *keybuf;
|
|
|
|
|
size_t keylen;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
keylen = gcry_cipher_get_algo_keylen (algo);
|
|
|
|
|
if (!keylen)
|
|
|
|
|
return -1;
|
|
|
|
|
keybuf = gcry_malloc_secure (keylen);
|
|
|
|
|
if (!keybuf)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
rc = gcry_kdf_derive (pw, strlen (pw),
|
|
|
|
|
GCRY_KDF_PBKDF2, GCRY_MD_SHA1,
|
|
|
|
|
salt, saltlen, iter, keylen, keybuf);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("gcry_kdf_derive failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
gcry_free (keybuf);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = gcry_cipher_setkey (chd, keybuf, keylen);
|
|
|
|
|
gcry_free (keybuf);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("gcry_cipher_setkey failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rc = gcry_cipher_setiv (chd, iv, ivlen);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("gcry_cipher_setiv failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static void
|
2005-12-16 16:52:48 +01:00
|
|
|
|
crypt_block (unsigned char *buffer, size_t length, char *salt, size_t saltlen,
|
2011-03-10 15:27:10 +01:00
|
|
|
|
int iter, const void *iv, size_t ivlen,
|
|
|
|
|
const char *pw, int cipher_algo, int encrypt)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
gcry_cipher_hd_t chd;
|
|
|
|
|
int rc;
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
rc = gcry_cipher_open (&chd, cipher_algo, GCRY_CIPHER_MODE_CBC, 0);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2004-02-10 20:26:55 +01:00
|
|
|
|
log_error ( "gcry_cipher_open failed: %s\n", gpg_strerror(rc));
|
2004-02-19 17:26:32 +01:00
|
|
|
|
wipememory (buffer, length);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2011-03-10 15:27:10 +01:00
|
|
|
|
|
|
|
|
|
if (cipher_algo == GCRY_CIPHER_AES128
|
|
|
|
|
? set_key_iv_pbes2 (chd, salt, saltlen, iter, iv, ivlen, pw, cipher_algo)
|
|
|
|
|
: set_key_iv (chd, salt, saltlen, iter, pw,
|
|
|
|
|
cipher_algo == GCRY_CIPHER_RFC2268_40? 5:24))
|
2004-02-19 17:26:32 +01:00
|
|
|
|
{
|
|
|
|
|
wipememory (buffer, length);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
rc = encrypt? gcry_cipher_encrypt (chd, buffer, length, NULL, 0)
|
|
|
|
|
: gcry_cipher_decrypt (chd, buffer, length, NULL, 0);
|
|
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2004-02-19 17:26:32 +01:00
|
|
|
|
wipememory (buffer, length);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
log_error ( "en/de-crytion failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
gcry_cipher_close (chd);
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2007-03-19 19:54:34 +01:00
|
|
|
|
/* Decrypt a block of data and try several encodings of the key.
|
|
|
|
|
CIPHERTEXT is the encrypted data of size LENGTH bytes; PLAINTEXT is
|
|
|
|
|
a buffer of the same size to receive the decryption result. SALT,
|
|
|
|
|
SALTLEN, ITER and PW are the information required for decryption
|
|
|
|
|
and CIPHER_ALGO is the algorithm id to use. CHECK_FNC is a
|
|
|
|
|
function called with the plaintext and used to check whether the
|
|
|
|
|
decryption succeeded; i.e. that a correct passphrase has been
|
|
|
|
|
given. That function shall return true if the decryption has likely
|
|
|
|
|
succeeded. */
|
|
|
|
|
static void
|
|
|
|
|
decrypt_block (const void *ciphertext, unsigned char *plaintext, size_t length,
|
|
|
|
|
char *salt, size_t saltlen,
|
2011-03-10 15:27:10 +01:00
|
|
|
|
int iter, const void *iv, size_t ivlen,
|
|
|
|
|
const char *pw, int cipher_algo,
|
2007-03-19 19:54:34 +01:00
|
|
|
|
int (*check_fnc) (const void *, size_t))
|
|
|
|
|
{
|
2008-12-05 13:01:01 +01:00
|
|
|
|
static const char * const charsets[] = {
|
2007-03-19 19:54:34 +01:00
|
|
|
|
"", /* No conversion - use the UTF-8 passphrase direct. */
|
|
|
|
|
"ISO-8859-1",
|
|
|
|
|
"ISO-8859-15",
|
|
|
|
|
"ISO-8859-2",
|
|
|
|
|
"ISO-8859-3",
|
|
|
|
|
"ISO-8859-4",
|
|
|
|
|
"ISO-8859-5",
|
|
|
|
|
"ISO-8859-6",
|
|
|
|
|
"ISO-8859-7",
|
|
|
|
|
"ISO-8859-8",
|
|
|
|
|
"ISO-8859-9",
|
|
|
|
|
"KOI8-R",
|
2007-03-20 11:00:55 +01:00
|
|
|
|
"IBM437",
|
|
|
|
|
"IBM850",
|
|
|
|
|
"EUC-JP",
|
|
|
|
|
"BIG5",
|
2007-03-19 19:54:34 +01:00
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
int charsetidx = 0;
|
|
|
|
|
char *convertedpw = NULL; /* Malloced and converted password or NULL. */
|
|
|
|
|
size_t convertedpwsize = 0; /* Allocated length. */
|
|
|
|
|
|
|
|
|
|
for (charsetidx=0; charsets[charsetidx]; charsetidx++)
|
|
|
|
|
{
|
|
|
|
|
if (*charsets[charsetidx])
|
|
|
|
|
{
|
2007-06-14 19:05:07 +02:00
|
|
|
|
jnlib_iconv_t cd;
|
2007-03-19 19:54:34 +01:00
|
|
|
|
const char *inptr;
|
|
|
|
|
char *outptr;
|
|
|
|
|
size_t inbytes, outbytes;
|
|
|
|
|
|
|
|
|
|
if (!convertedpw)
|
|
|
|
|
{
|
|
|
|
|
/* We assume one byte encodings. Thus we can allocate
|
|
|
|
|
the buffer of the same size as the original
|
|
|
|
|
passphrase; the result will actually be shorter
|
|
|
|
|
then. */
|
|
|
|
|
convertedpwsize = strlen (pw) + 1;
|
|
|
|
|
convertedpw = gcry_malloc_secure (convertedpwsize);
|
|
|
|
|
if (!convertedpw)
|
|
|
|
|
{
|
|
|
|
|
log_info ("out of secure memory while"
|
|
|
|
|
" converting passphrase\n");
|
|
|
|
|
break; /* Give up. */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
|
cd = jnlib_iconv_open (charsets[charsetidx], "utf-8");
|
|
|
|
|
if (cd == (jnlib_iconv_t)(-1))
|
2007-03-19 19:54:34 +01:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
inptr = pw;
|
|
|
|
|
inbytes = strlen (pw);
|
|
|
|
|
outptr = convertedpw;
|
|
|
|
|
outbytes = convertedpwsize - 1;
|
2007-06-14 19:05:07 +02:00
|
|
|
|
if ( jnlib_iconv (cd, (const char **)&inptr, &inbytes,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
&outptr, &outbytes) == (size_t)-1)
|
2007-03-19 19:54:34 +01:00
|
|
|
|
{
|
2007-06-14 19:05:07 +02:00
|
|
|
|
jnlib_iconv_close (cd);
|
2007-03-19 19:54:34 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
*outptr = 0;
|
2007-06-14 19:05:07 +02:00
|
|
|
|
jnlib_iconv_close (cd);
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_info ("decryption failed; trying charset '%s'\n",
|
2007-03-19 19:54:34 +01:00
|
|
|
|
charsets[charsetidx]);
|
|
|
|
|
}
|
|
|
|
|
memcpy (plaintext, ciphertext, length);
|
2011-03-10 15:27:10 +01:00
|
|
|
|
crypt_block (plaintext, length, salt, saltlen, iter, iv, ivlen,
|
2007-03-19 19:54:34 +01:00
|
|
|
|
convertedpw? convertedpw:pw, cipher_algo, 0);
|
|
|
|
|
if (check_fnc (plaintext, length))
|
|
|
|
|
break; /* Decryption succeeded. */
|
|
|
|
|
}
|
|
|
|
|
gcry_free (convertedpw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Return true if the decryption of an bag_encrypted_data object has
|
|
|
|
|
likely succeeded. */
|
|
|
|
|
static int
|
|
|
|
|
bag_decrypted_data_p (const void *plaintext, size_t length)
|
|
|
|
|
{
|
|
|
|
|
struct tag_info ti;
|
|
|
|
|
const unsigned char *p = plaintext;
|
|
|
|
|
size_t n = length;
|
|
|
|
|
|
|
|
|
|
/* { */
|
|
|
|
|
/* # warning debug code is enabled */
|
|
|
|
|
/* FILE *fp = fopen ("tmp-rc2-plain.der", "wb"); */
|
|
|
|
|
/* if (!fp || fwrite (p, n, 1, fp) != 1) */
|
|
|
|
|
/* exit (2); */
|
|
|
|
|
/* fclose (fp); */
|
|
|
|
|
/* } */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2007-03-19 19:54:34 +01:00
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
return 0;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
return 0;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
return 1;
|
2007-03-19 19:54:34 +01:00
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2006-07-21 11:41:11 +02:00
|
|
|
|
/* Note: If R_RESULT is passed as NULL, a key object as already be
|
|
|
|
|
processed and thus we need to skip it here. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static int
|
|
|
|
|
parse_bag_encrypted_data (const unsigned char *buffer, size_t length,
|
2005-12-16 16:52:48 +01:00
|
|
|
|
int startoffset, size_t *r_consumed, const char *pw,
|
2004-02-10 20:26:55 +01:00
|
|
|
|
void (*certcb)(void*, const unsigned char*, size_t),
|
2010-06-17 17:44:44 +02:00
|
|
|
|
void *certcbarg, gcry_mpi_t **r_result,
|
|
|
|
|
int *r_badpass)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
struct tag_info ti;
|
|
|
|
|
const unsigned char *p = buffer;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
const unsigned char *p_start = buffer;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
size_t n = length;
|
|
|
|
|
const char *where;
|
2006-10-23 12:52:23 +02:00
|
|
|
|
char salt[20];
|
2005-12-16 16:52:48 +01:00
|
|
|
|
size_t saltlen;
|
2011-03-10 15:27:10 +01:00
|
|
|
|
char iv[16];
|
2004-02-10 20:26:55 +01:00
|
|
|
|
unsigned int iter;
|
|
|
|
|
unsigned char *plain = NULL;
|
2004-09-29 15:50:31 +02:00
|
|
|
|
int bad_pass = 0;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
unsigned char *cram_buffer = NULL;
|
2015-11-16 12:41:46 +01:00
|
|
|
|
size_t consumed = 0; /* Number of bytes consumed from the original buffer. */
|
2006-07-21 11:41:11 +02:00
|
|
|
|
int is_3des = 0;
|
2011-03-10 15:27:10 +01:00
|
|
|
|
int is_pbes2 = 0;
|
2006-07-21 11:41:11 +02:00
|
|
|
|
gcry_mpi_t *result = NULL;
|
|
|
|
|
int result_count;
|
|
|
|
|
|
|
|
|
|
if (r_result)
|
|
|
|
|
*r_result = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
where = "start";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2007-06-06 20:12:30 +02:00
|
|
|
|
if (ti.class != ASNCONTEXT || ti.tag)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
|
|
where = "bag.encryptedData.version";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_INTEGER || ti.length != 1 || *p != 0)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p++; n--;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
|
|
where = "bag.encryptedData.data";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_OBJECT_ID || ti.length != DIM(oid_data)
|
|
|
|
|
|| memcmp (p, oid_data, DIM(oid_data)))
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += DIM(oid_data);
|
|
|
|
|
n -= DIM(oid_data);
|
|
|
|
|
|
2004-02-10 20:26:55 +01:00
|
|
|
|
where = "bag.encryptedData.keyinfo";
|
2004-02-04 20:13:16 +01:00
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
if (!ti.class && ti.tag == TAG_OBJECT_ID
|
2004-02-04 20:13:16 +01:00
|
|
|
|
&& ti.length == DIM(oid_pbeWithSHAAnd40BitRC2_CBC)
|
2004-02-10 20:26:55 +01:00
|
|
|
|
&& !memcmp (p, oid_pbeWithSHAAnd40BitRC2_CBC,
|
|
|
|
|
DIM(oid_pbeWithSHAAnd40BitRC2_CBC)))
|
2004-02-04 20:13:16 +01:00
|
|
|
|
{
|
|
|
|
|
p += DIM(oid_pbeWithSHAAnd40BitRC2_CBC);
|
|
|
|
|
n -= DIM(oid_pbeWithSHAAnd40BitRC2_CBC);
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
else if (!ti.class && ti.tag == TAG_OBJECT_ID
|
2006-07-21 11:41:11 +02:00
|
|
|
|
&& ti.length == DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC)
|
|
|
|
|
&& !memcmp (p, oid_pbeWithSHAAnd3_KeyTripleDES_CBC,
|
|
|
|
|
DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC)))
|
|
|
|
|
{
|
|
|
|
|
p += DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC);
|
|
|
|
|
n -= DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC);
|
|
|
|
|
is_3des = 1;
|
|
|
|
|
}
|
2011-03-10 15:27:10 +01:00
|
|
|
|
else if (!ti.class && ti.tag == TAG_OBJECT_ID
|
|
|
|
|
&& ti.length == DIM(oid_pkcs5PBES2)
|
|
|
|
|
&& !memcmp (p, oid_pkcs5PBES2, ti.length))
|
|
|
|
|
{
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
is_pbes2 = 1;
|
|
|
|
|
}
|
2004-02-04 20:13:16 +01:00
|
|
|
|
else
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
if (is_pbes2)
|
|
|
|
|
{
|
|
|
|
|
where = "pkcs5PBES2-params";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OBJECT_ID
|
|
|
|
|
&& ti.length == DIM(oid_pkcs5PBKDF2)
|
|
|
|
|
&& !memcmp (p, oid_pkcs5PBKDF2, ti.length)))
|
|
|
|
|
goto bailout; /* Not PBKDF2. */
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OCTET_STRING
|
|
|
|
|
&& ti.length >= 8 && ti.length < sizeof salt))
|
|
|
|
|
goto bailout; /* No salt or unsupported length. */
|
|
|
|
|
saltlen = ti.length;
|
|
|
|
|
memcpy (salt, p, saltlen);
|
|
|
|
|
p += saltlen;
|
|
|
|
|
n -= saltlen;
|
|
|
|
|
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_INTEGER && ti.length))
|
|
|
|
|
goto bailout; /* No valid iteration count. */
|
|
|
|
|
for (iter=0; ti.length; ti.length--)
|
|
|
|
|
{
|
|
|
|
|
iter <<= 8;
|
|
|
|
|
iter |= (*p++) & 0xff;
|
|
|
|
|
n--;
|
|
|
|
|
}
|
|
|
|
|
/* Note: We don't support the optional parameters but assume
|
|
|
|
|
that the algorithmIdentifier follows. */
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OBJECT_ID
|
|
|
|
|
&& ti.length == DIM(oid_aes128_CBC)
|
|
|
|
|
&& !memcmp (p, oid_aes128_CBC, ti.length)))
|
|
|
|
|
goto bailout; /* Not AES-128. */
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OCTET_STRING && ti.length == sizeof iv))
|
|
|
|
|
goto bailout; /* Bad IV. */
|
|
|
|
|
memcpy (iv, p, sizeof iv);
|
|
|
|
|
p += sizeof iv;
|
|
|
|
|
n -= sizeof iv;
|
|
|
|
|
}
|
|
|
|
|
else
|
2004-02-04 20:13:16 +01:00
|
|
|
|
{
|
2011-03-10 15:27:10 +01:00
|
|
|
|
where = "rc2or3des-params";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OCTET_STRING
|
|
|
|
|
|| ti.length < 8 || ti.length > 20 )
|
|
|
|
|
goto bailout;
|
|
|
|
|
saltlen = ti.length;
|
|
|
|
|
memcpy (salt, p, saltlen);
|
|
|
|
|
p += saltlen;
|
|
|
|
|
n -= saltlen;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_INTEGER || !ti.length )
|
|
|
|
|
goto bailout;
|
|
|
|
|
for (iter=0; ti.length; ti.length--)
|
|
|
|
|
{
|
|
|
|
|
iter <<= 8;
|
|
|
|
|
iter |= (*p++) & 0xff;
|
|
|
|
|
n--;
|
|
|
|
|
}
|
2004-02-04 20:13:16 +01:00
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
where = "rc2or3desoraes-ciphertext";
|
2004-02-04 20:13:16 +01:00
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
|
|
|
|
|
consumed = p - p_start;
|
2007-06-06 20:12:30 +02:00
|
|
|
|
if (ti.class == ASNCONTEXT && ti.tag == 0 && ti.is_constructed && ti.ndef)
|
2005-12-16 16:52:48 +01:00
|
|
|
|
{
|
|
|
|
|
/* Mozilla exported certs now come with single byte chunks of
|
|
|
|
|
octect strings. (Mozilla Firefox 1.0.4). Arghh. */
|
2006-07-21 11:41:11 +02:00
|
|
|
|
where = "cram-rc2or3des-ciphertext";
|
2005-12-16 16:52:48 +01:00
|
|
|
|
cram_buffer = cram_octet_string ( p, &n, &consumed);
|
|
|
|
|
if (!cram_buffer)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p = p_start = cram_buffer;
|
|
|
|
|
if (r_consumed)
|
|
|
|
|
*r_consumed = consumed;
|
|
|
|
|
r_consumed = NULL; /* Ugly hack to not update that value any further. */
|
|
|
|
|
ti.length = n;
|
|
|
|
|
}
|
2007-06-06 20:12:30 +02:00
|
|
|
|
else if (ti.class == ASNCONTEXT && ti.tag == 0 && ti.length )
|
2005-12-16 16:52:48 +01:00
|
|
|
|
;
|
|
|
|
|
else
|
2004-02-04 20:13:16 +01:00
|
|
|
|
goto bailout;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
log_info ("%lu bytes of %s encrypted text\n",ti.length,
|
|
|
|
|
is_pbes2?"AES128":is_3des?"3DES":"RC2");
|
2004-02-04 20:13:16 +01:00
|
|
|
|
|
2004-02-10 20:26:55 +01:00
|
|
|
|
plain = gcry_malloc_secure (ti.length);
|
|
|
|
|
if (!plain)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating decryption buffer\n");
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
2011-03-10 15:27:10 +01:00
|
|
|
|
decrypt_block (p, plain, ti.length, salt, saltlen, iter,
|
|
|
|
|
iv, is_pbes2?16:0, pw,
|
|
|
|
|
is_pbes2 ? GCRY_CIPHER_AES128 :
|
|
|
|
|
is_3des ? GCRY_CIPHER_3DES : GCRY_CIPHER_RFC2268_40,
|
2007-03-19 19:54:34 +01:00
|
|
|
|
bag_decrypted_data_p);
|
2004-02-10 20:26:55 +01:00
|
|
|
|
n = ti.length;
|
|
|
|
|
startoffset = 0;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
p_start = p = plain;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
|
|
|
|
|
where = "outer.outer.seq";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
2004-09-29 15:50:31 +02:00
|
|
|
|
{
|
|
|
|
|
bad_pass = 1;
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
2004-02-10 20:26:55 +01:00
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
2004-09-29 15:50:31 +02:00
|
|
|
|
{
|
|
|
|
|
bad_pass = 1;
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
2004-02-10 20:26:55 +01:00
|
|
|
|
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
2004-09-29 15:50:31 +02:00
|
|
|
|
{
|
|
|
|
|
bad_pass = 1;
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
2004-02-10 20:26:55 +01:00
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
|
/* Loop over all certificates inside the bag. */
|
2004-02-10 20:26:55 +01:00
|
|
|
|
while (n)
|
|
|
|
|
{
|
2006-07-21 11:41:11 +02:00
|
|
|
|
int iscrlbag = 0;
|
|
|
|
|
int iskeybag = 0;
|
2004-02-12 10:27:36 +01:00
|
|
|
|
|
2004-02-10 20:26:55 +01:00
|
|
|
|
where = "certbag.nextcert";
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
2004-02-04 20:13:16 +01:00
|
|
|
|
|
2004-02-10 20:26:55 +01:00
|
|
|
|
where = "certbag.objectidentifier";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2004-02-12 10:27:36 +01:00
|
|
|
|
if (ti.class || ti.tag != TAG_OBJECT_ID)
|
2004-02-10 20:26:55 +01:00
|
|
|
|
goto bailout;
|
2004-02-12 10:27:36 +01:00
|
|
|
|
if ( ti.length == DIM(oid_pkcs_12_CertBag)
|
|
|
|
|
&& !memcmp (p, oid_pkcs_12_CertBag, DIM(oid_pkcs_12_CertBag)))
|
|
|
|
|
{
|
|
|
|
|
p += DIM(oid_pkcs_12_CertBag);
|
|
|
|
|
n -= DIM(oid_pkcs_12_CertBag);
|
|
|
|
|
}
|
|
|
|
|
else if ( ti.length == DIM(oid_pkcs_12_CrlBag)
|
|
|
|
|
&& !memcmp (p, oid_pkcs_12_CrlBag, DIM(oid_pkcs_12_CrlBag)))
|
|
|
|
|
{
|
|
|
|
|
p += DIM(oid_pkcs_12_CrlBag);
|
|
|
|
|
n -= DIM(oid_pkcs_12_CrlBag);
|
2006-07-21 11:41:11 +02:00
|
|
|
|
iscrlbag = 1;
|
|
|
|
|
}
|
|
|
|
|
else if ( ti.length == DIM(oid_pkcs_12_keyBag)
|
|
|
|
|
&& !memcmp (p, oid_pkcs_12_keyBag, DIM(oid_pkcs_12_keyBag)))
|
|
|
|
|
{
|
|
|
|
|
/* The TrustedMIME plugin for MS Outlook started to create
|
|
|
|
|
files with just one outer 3DES encrypted container and
|
|
|
|
|
inside the certificates as well as the key. */
|
|
|
|
|
p += DIM(oid_pkcs_12_keyBag);
|
|
|
|
|
n -= DIM(oid_pkcs_12_keyBag);
|
|
|
|
|
iskeybag = 1;
|
2004-02-12 10:27:36 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2004-02-10 20:26:55 +01:00
|
|
|
|
goto bailout;
|
|
|
|
|
|
2004-02-12 10:27:36 +01:00
|
|
|
|
where = "certbag.before.certheader";
|
2004-02-10 20:26:55 +01:00
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2007-06-06 20:12:30 +02:00
|
|
|
|
if (ti.class != ASNCONTEXT || ti.tag)
|
2004-02-10 20:26:55 +01:00
|
|
|
|
goto bailout;
|
2006-07-21 11:41:11 +02:00
|
|
|
|
if (iscrlbag)
|
2004-02-12 10:27:36 +01:00
|
|
|
|
{
|
|
|
|
|
log_info ("skipping unsupported crlBag\n");
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
2006-07-21 11:41:11 +02:00
|
|
|
|
else if (iskeybag && (result || !r_result))
|
|
|
|
|
{
|
|
|
|
|
log_info ("one keyBag already processed; skipping this one\n");
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
|
|
|
|
else if (iskeybag)
|
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
log_info ("processing simple keyBag\n");
|
|
|
|
|
|
|
|
|
|
/* Fixme: This code is duplicated from parse_bag_data. */
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_INTEGER
|
|
|
|
|
|| ti.length != 1 || *p)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p++; n--;
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len = ti.length;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (len < ti.nhdr)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.nhdr;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OBJECT_ID
|
|
|
|
|
|| ti.length != DIM(oid_rsaEncryption)
|
|
|
|
|
|| memcmp (p, oid_rsaEncryption,
|
|
|
|
|
DIM(oid_rsaEncryption)))
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += DIM (oid_rsaEncryption);
|
|
|
|
|
n -= DIM (oid_rsaEncryption);
|
|
|
|
|
if (len < ti.length)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.length;
|
|
|
|
|
if (n < len)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += len;
|
|
|
|
|
n -= len;
|
|
|
|
|
if ( parse_tag (&p, &n, &ti)
|
|
|
|
|
|| ti.class || ti.tag != TAG_OCTET_STRING)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if ( parse_tag (&p, &n, &ti)
|
|
|
|
|
|| ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len = ti.length;
|
|
|
|
|
|
|
|
|
|
result = gcry_calloc (10, sizeof *result);
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
log_error ( "error allocating result array\n");
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
|
|
|
|
result_count = 0;
|
|
|
|
|
|
|
|
|
|
where = "reading.keybag.key-parameters";
|
|
|
|
|
for (result_count = 0; len && result_count < 9;)
|
|
|
|
|
{
|
|
|
|
|
if ( parse_tag (&p, &n, &ti)
|
|
|
|
|
|| ti.class || ti.tag != TAG_INTEGER)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (len < ti.nhdr)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.nhdr;
|
|
|
|
|
if (len < ti.length)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.length;
|
|
|
|
|
if (!result_count && ti.length == 1 && !*p)
|
|
|
|
|
; /* ignore the very first one if it is a 0 */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
else
|
2006-07-21 11:41:11 +02:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
rc = gcry_mpi_scan (result+result_count, GCRYMPI_FMT_USG, p,
|
|
|
|
|
ti.length, NULL);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error parsing key parameter: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
|
|
|
|
result_count++;
|
|
|
|
|
}
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
|
|
|
|
if (len)
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
2004-02-12 10:27:36 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
2006-07-21 11:41:11 +02:00
|
|
|
|
log_info ("processing certBag\n");
|
2004-02-12 10:27:36 +01:00
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OBJECT_ID
|
|
|
|
|
|| ti.length != DIM(oid_x509Certificate_for_pkcs_12)
|
|
|
|
|
|| memcmp (p, oid_x509Certificate_for_pkcs_12,
|
|
|
|
|
DIM(oid_x509Certificate_for_pkcs_12)))
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += DIM(oid_x509Certificate_for_pkcs_12);
|
|
|
|
|
n -= DIM(oid_x509Certificate_for_pkcs_12);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-12 10:27:36 +01:00
|
|
|
|
where = "certbag.before.octetstring";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2007-06-06 20:12:30 +02:00
|
|
|
|
if (ti.class != ASNCONTEXT || ti.tag)
|
2004-02-12 10:27:36 +01:00
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OCTET_STRING || ti.ndef)
|
|
|
|
|
goto bailout;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-12 10:27:36 +01:00
|
|
|
|
/* Return the certificate. */
|
|
|
|
|
if (certcb)
|
|
|
|
|
certcb (certcbarg, p, ti.length);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-12 10:27:36 +01:00
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
2004-02-10 20:26:55 +01:00
|
|
|
|
|
2004-02-12 10:27:36 +01:00
|
|
|
|
/* Ugly hack to cope with the padding: Forget about the rest if
|
2005-04-20 21:19:24 +02:00
|
|
|
|
that is less or equal to the cipher's block length. We can
|
|
|
|
|
reasonable assume that all valid data will be longer than
|
|
|
|
|
just one block. */
|
2011-03-10 15:27:10 +01:00
|
|
|
|
if (n <= (is_pbes2? 16:8))
|
2011-02-04 12:57:53 +01:00
|
|
|
|
n = 0;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
|
|
|
|
|
/* Skip the optional SET with the pkcs12 cert attributes. */
|
|
|
|
|
if (n)
|
|
|
|
|
{
|
2004-02-12 10:27:36 +01:00
|
|
|
|
where = "bag.attributes";
|
2004-02-10 20:26:55 +01:00
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!ti.class && ti.tag == TAG_SEQUENCE)
|
|
|
|
|
; /* No attributes. */
|
|
|
|
|
else if (!ti.class && ti.tag == TAG_SET && !ti.ndef)
|
|
|
|
|
{ /* The optional SET. */
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
2011-03-10 15:27:10 +01:00
|
|
|
|
if (n <= (is_pbes2?16:8))
|
2004-02-10 20:26:55 +01:00
|
|
|
|
n = 0;
|
|
|
|
|
if (n && parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (r_consumed)
|
|
|
|
|
*r_consumed = consumed;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
gcry_free (plain);
|
2005-12-16 16:52:48 +01:00
|
|
|
|
gcry_free (cram_buffer);
|
2006-07-21 11:41:11 +02:00
|
|
|
|
if (r_result)
|
|
|
|
|
*r_result = result;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return 0;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
bailout:
|
2006-07-21 11:41:11 +02:00
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i=0; result[i]; i++)
|
|
|
|
|
gcry_mpi_release (result[i]);
|
|
|
|
|
gcry_free (result);
|
|
|
|
|
}
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (r_consumed)
|
|
|
|
|
*r_consumed = consumed;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
gcry_free (plain);
|
2005-12-16 16:52:48 +01:00
|
|
|
|
gcry_free (cram_buffer);
|
2004-02-04 20:13:16 +01:00
|
|
|
|
log_error ("encryptedData error at \"%s\", offset %u\n",
|
2006-11-20 17:49:41 +01:00
|
|
|
|
where, (unsigned int)((p - p_start)+startoffset));
|
2004-09-29 15:50:31 +02:00
|
|
|
|
if (bad_pass)
|
|
|
|
|
{
|
|
|
|
|
/* Note, that the following string might be used by other programs
|
|
|
|
|
to check for a bad passphrase; it should therefore not be
|
|
|
|
|
translated or changed. */
|
|
|
|
|
log_error ("possibly bad passphrase given\n");
|
2010-06-17 17:44:44 +02:00
|
|
|
|
*r_badpass = 1;
|
2004-09-29 15:50:31 +02:00
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-19 19:54:34 +01:00
|
|
|
|
|
|
|
|
|
/* Return true if the decryption of a bag_data object has likely
|
|
|
|
|
succeeded. */
|
|
|
|
|
static int
|
|
|
|
|
bag_data_p (const void *plaintext, size_t length)
|
|
|
|
|
{
|
|
|
|
|
struct tag_info ti;
|
|
|
|
|
const unsigned char *p = plaintext;
|
|
|
|
|
size_t n = length;
|
|
|
|
|
|
|
|
|
|
/* { */
|
|
|
|
|
/* # warning debug code is enabled */
|
|
|
|
|
/* FILE *fp = fopen ("tmp-3des-plain-key.der", "wb"); */
|
|
|
|
|
/* if (!fp || fwrite (p, n, 1, fp) != 1) */
|
|
|
|
|
/* exit (2); */
|
|
|
|
|
/* fclose (fp); */
|
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
return 0;
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_INTEGER
|
|
|
|
|
|| ti.length != 1 || *p)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
return 1;
|
2007-03-19 19:54:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static gcry_mpi_t *
|
|
|
|
|
parse_bag_data (const unsigned char *buffer, size_t length, int startoffset,
|
2005-12-16 16:52:48 +01:00
|
|
|
|
size_t *r_consumed, const char *pw)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
struct tag_info ti;
|
|
|
|
|
const unsigned char *p = buffer;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
const unsigned char *p_start = buffer;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
size_t n = length;
|
|
|
|
|
const char *where;
|
2006-10-23 12:52:23 +02:00
|
|
|
|
char salt[20];
|
2005-12-16 16:52:48 +01:00
|
|
|
|
size_t saltlen;
|
2011-03-10 15:27:10 +01:00
|
|
|
|
char iv[16];
|
2003-08-05 19:11:04 +02:00
|
|
|
|
unsigned int iter;
|
|
|
|
|
int len;
|
|
|
|
|
unsigned char *plain = NULL;
|
|
|
|
|
gcry_mpi_t *result = NULL;
|
|
|
|
|
int result_count, i;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
unsigned char *cram_buffer = NULL;
|
2015-11-16 12:41:46 +01:00
|
|
|
|
size_t consumed = 0; /* Number of bytes consumed from the original buffer. */
|
2011-03-10 15:27:10 +01:00
|
|
|
|
int is_pbes2 = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
where = "start";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2007-06-06 20:12:30 +02:00
|
|
|
|
if (ti.class != ASNCONTEXT || ti.tag)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OCTET_STRING)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
consumed = p - p_start;
|
|
|
|
|
if (ti.is_constructed && ti.ndef)
|
|
|
|
|
{
|
|
|
|
|
/* Mozilla exported certs now come with single byte chunks of
|
|
|
|
|
octect strings. (Mozilla Firefox 1.0.4). Arghh. */
|
|
|
|
|
where = "cram-data.outersegs";
|
|
|
|
|
cram_buffer = cram_octet_string ( p, &n, &consumed);
|
|
|
|
|
if (!cram_buffer)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p = p_start = cram_buffer;
|
|
|
|
|
if (r_consumed)
|
|
|
|
|
*r_consumed = consumed;
|
|
|
|
|
r_consumed = NULL; /* Ugly hack to not update that value any further. */
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
where = "data.outerseqs";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
|
|
where = "data.objectidentifier";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OBJECT_ID
|
|
|
|
|
|| ti.length != DIM(oid_pkcs_12_pkcs_8ShroudedKeyBag)
|
|
|
|
|
|| memcmp (p, oid_pkcs_12_pkcs_8ShroudedKeyBag,
|
|
|
|
|
DIM(oid_pkcs_12_pkcs_8ShroudedKeyBag)))
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += DIM(oid_pkcs_12_pkcs_8ShroudedKeyBag);
|
|
|
|
|
n -= DIM(oid_pkcs_12_pkcs_8ShroudedKeyBag);
|
|
|
|
|
|
|
|
|
|
where = "shrouded,outerseqs";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2007-06-06 20:12:30 +02:00
|
|
|
|
if (ti.class != ASNCONTEXT || ti.tag)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2011-03-10 15:27:10 +01:00
|
|
|
|
if (ti.class == 0 && ti.tag == TAG_OBJECT_ID
|
|
|
|
|
&& ti.length == DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC)
|
|
|
|
|
&& !memcmp (p, oid_pbeWithSHAAnd3_KeyTripleDES_CBC,
|
|
|
|
|
DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC)))
|
|
|
|
|
{
|
|
|
|
|
p += DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC);
|
|
|
|
|
n -= DIM(oid_pbeWithSHAAnd3_KeyTripleDES_CBC);
|
|
|
|
|
}
|
|
|
|
|
else if (ti.class == 0 && ti.tag == TAG_OBJECT_ID
|
|
|
|
|
&& ti.length == DIM(oid_pkcs5PBES2)
|
|
|
|
|
&& !memcmp (p, oid_pkcs5PBES2, DIM(oid_pkcs5PBES2)))
|
|
|
|
|
{
|
|
|
|
|
p += DIM(oid_pkcs5PBES2);
|
|
|
|
|
n -= DIM(oid_pkcs5PBES2);
|
|
|
|
|
is_pbes2 = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
2003-08-05 19:11:04 +02:00
|
|
|
|
goto bailout;
|
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
if (is_pbes2)
|
|
|
|
|
{
|
|
|
|
|
where = "pkcs5PBES2-params";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OBJECT_ID
|
|
|
|
|
&& ti.length == DIM(oid_pkcs5PBKDF2)
|
|
|
|
|
&& !memcmp (p, oid_pkcs5PBKDF2, ti.length)))
|
|
|
|
|
goto bailout; /* Not PBKDF2. */
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OCTET_STRING
|
|
|
|
|
&& ti.length >= 8 && ti.length < sizeof salt))
|
|
|
|
|
goto bailout; /* No salt or unsupported length. */
|
|
|
|
|
saltlen = ti.length;
|
|
|
|
|
memcpy (salt, p, saltlen);
|
|
|
|
|
p += saltlen;
|
|
|
|
|
n -= saltlen;
|
|
|
|
|
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_INTEGER && ti.length))
|
|
|
|
|
goto bailout; /* No valid iteration count. */
|
|
|
|
|
for (iter=0; ti.length; ti.length--)
|
|
|
|
|
{
|
|
|
|
|
iter <<= 8;
|
|
|
|
|
iter |= (*p++) & 0xff;
|
|
|
|
|
n--;
|
|
|
|
|
}
|
|
|
|
|
/* Note: We don't support the optional parameters but assume
|
|
|
|
|
that the algorithmIdentifier follows. */
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OBJECT_ID
|
|
|
|
|
&& ti.length == DIM(oid_aes128_CBC)
|
|
|
|
|
&& !memcmp (p, oid_aes128_CBC, ti.length)))
|
|
|
|
|
goto bailout; /* Not AES-128. */
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(!ti.class && ti.tag == TAG_OCTET_STRING && ti.length == sizeof iv))
|
|
|
|
|
goto bailout; /* Bad IV. */
|
|
|
|
|
memcpy (iv, p, sizeof iv);
|
|
|
|
|
p += sizeof iv;
|
|
|
|
|
n -= sizeof iv;
|
|
|
|
|
}
|
|
|
|
|
else
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
2011-03-10 15:27:10 +01:00
|
|
|
|
where = "3des-params";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OCTET_STRING
|
|
|
|
|
|| ti.length < 8 || ti.length > 20)
|
|
|
|
|
goto bailout;
|
|
|
|
|
saltlen = ti.length;
|
|
|
|
|
memcpy (salt, p, saltlen);
|
|
|
|
|
p += saltlen;
|
|
|
|
|
n -= saltlen;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_INTEGER || !ti.length )
|
|
|
|
|
goto bailout;
|
|
|
|
|
for (iter=0; ti.length; ti.length--)
|
|
|
|
|
{
|
|
|
|
|
iter <<= 8;
|
|
|
|
|
iter |= (*p++) & 0xff;
|
|
|
|
|
n--;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
where = "3desoraes-ciphertext";
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OCTET_STRING || !ti.length )
|
|
|
|
|
goto bailout;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2011-03-10 15:27:10 +01:00
|
|
|
|
log_info ("%lu bytes of %s encrypted text\n",
|
|
|
|
|
ti.length, is_pbes2? "AES128":"3DES");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
plain = gcry_malloc_secure (ti.length);
|
|
|
|
|
if (!plain)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating decryption buffer\n");
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
2005-12-16 16:52:48 +01:00
|
|
|
|
consumed += p - p_start + ti.length;
|
2011-03-10 15:27:10 +01:00
|
|
|
|
decrypt_block (p, plain, ti.length, salt, saltlen, iter,
|
|
|
|
|
iv, is_pbes2? 16:0, pw,
|
|
|
|
|
is_pbes2? GCRY_CIPHER_AES128 : GCRY_CIPHER_3DES,
|
2007-03-19 19:54:34 +01:00
|
|
|
|
bag_data_p);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
n = ti.length;
|
|
|
|
|
startoffset = 0;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
p_start = p = plain;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
where = "decrypted-text";
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_INTEGER
|
|
|
|
|
|| ti.length != 1 || *p)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p++; n--;
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len = ti.length;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (len < ti.nhdr)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.nhdr;
|
|
|
|
|
if (ti.class || ti.tag != TAG_OBJECT_ID
|
|
|
|
|
|| ti.length != DIM(oid_rsaEncryption)
|
|
|
|
|
|| memcmp (p, oid_rsaEncryption,
|
|
|
|
|
DIM(oid_rsaEncryption)))
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += DIM (oid_rsaEncryption);
|
|
|
|
|
n -= DIM (oid_rsaEncryption);
|
|
|
|
|
if (len < ti.length)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.length;
|
|
|
|
|
if (n < len)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += len;
|
|
|
|
|
n -= len;
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_OCTET_STRING)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len = ti.length;
|
|
|
|
|
|
|
|
|
|
result = gcry_calloc (10, sizeof *result);
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
log_error ( "error allocating result array\n");
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
|
|
|
|
result_count = 0;
|
|
|
|
|
|
|
|
|
|
where = "reading.key-parameters";
|
|
|
|
|
for (result_count=0; len && result_count < 9;)
|
|
|
|
|
{
|
|
|
|
|
if (parse_tag (&p, &n, &ti) || ti.class || ti.tag != TAG_INTEGER)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (len < ti.nhdr)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.nhdr;
|
|
|
|
|
if (len < ti.length)
|
|
|
|
|
goto bailout;
|
|
|
|
|
len -= ti.length;
|
|
|
|
|
if (!result_count && ti.length == 1 && !*p)
|
|
|
|
|
; /* ignore the very first one if it is a 0 */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
else
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
rc = gcry_mpi_scan (result+result_count, GCRYMPI_FMT_USG, p,
|
|
|
|
|
ti.length, NULL);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error parsing key parameter: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
|
|
|
|
result_count++;
|
|
|
|
|
}
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
|
|
|
|
if (len)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
gcry_free (cram_buffer);
|
|
|
|
|
if (r_consumed)
|
|
|
|
|
*r_consumed = consumed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
bailout:
|
|
|
|
|
gcry_free (plain);
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
for (i=0; result[i]; i++)
|
|
|
|
|
gcry_mpi_release (result[i]);
|
|
|
|
|
gcry_free (result);
|
|
|
|
|
}
|
2005-12-16 16:52:48 +01:00
|
|
|
|
gcry_free (cram_buffer);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
log_error ( "data error at \"%s\", offset %u\n",
|
2006-11-20 17:49:41 +01:00
|
|
|
|
where, (unsigned int)((p - buffer) + startoffset));
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (r_consumed)
|
|
|
|
|
*r_consumed = consumed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse a PKCS12 object and return an array of MPI representing the
|
2004-02-04 20:13:16 +01:00
|
|
|
|
secret key parameters. This is a very limited implementation in
|
|
|
|
|
that it is only able to look for 3DES encoded encryptedData and
|
2003-08-05 19:11:04 +02:00
|
|
|
|
tries to extract the first private key object it finds. In case of
|
2004-02-10 20:26:55 +01:00
|
|
|
|
an error NULL is returned. CERTCB and CERRTCBARG are used to pass
|
|
|
|
|
X.509 certificates back to the caller. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
gcry_mpi_t *
|
2004-02-10 20:26:55 +01:00
|
|
|
|
p12_parse (const unsigned char *buffer, size_t length, const char *pw,
|
|
|
|
|
void (*certcb)(void*, const unsigned char*, size_t),
|
2010-06-17 17:44:44 +02:00
|
|
|
|
void *certcbarg, int *r_badpass)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
struct tag_info ti;
|
|
|
|
|
const unsigned char *p = buffer;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
const unsigned char *p_start = buffer;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
size_t n = length;
|
|
|
|
|
const char *where;
|
|
|
|
|
int bagseqlength, len;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
int bagseqndef, lenndef;
|
2005-11-28 12:52:25 +01:00
|
|
|
|
gcry_mpi_t *result = NULL;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
unsigned char *cram_buffer = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2010-06-17 17:44:44 +02:00
|
|
|
|
*r_badpass = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
where = "pfx";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
|
|
where = "pfxVersion";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_INTEGER || ti.length != 1 || *p != 3)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p++; n--;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
where = "authSave";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.tag != TAG_OBJECT_ID || ti.length != DIM(oid_data)
|
|
|
|
|
|| memcmp (p, oid_data, DIM(oid_data)))
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += DIM(oid_data);
|
|
|
|
|
n -= DIM(oid_data);
|
|
|
|
|
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2007-06-06 20:12:30 +02:00
|
|
|
|
if (ti.class != ASNCONTEXT || ti.tag)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
goto bailout;
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class != UNIVERSAL || ti.tag != TAG_OCTET_STRING)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (ti.is_constructed && ti.ndef)
|
|
|
|
|
{
|
|
|
|
|
/* Mozilla exported certs now come with single byte chunks of
|
|
|
|
|
octect strings. (Mozilla Firefox 1.0.4). Arghh. */
|
|
|
|
|
where = "cram-bags";
|
|
|
|
|
cram_buffer = cram_octet_string ( p, &n, NULL);
|
|
|
|
|
if (!cram_buffer)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p = p_start = cram_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
where = "bags";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (ti.class != UNIVERSAL || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
bagseqndef = ti.ndef;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
bagseqlength = ti.length;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
while (bagseqlength || bagseqndef)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
2006-07-21 11:41:11 +02:00
|
|
|
|
/* log_debug ( "at offset %u\n", (p - p_start)); */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
where = "bag-sequence";
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (bagseqndef && ti.class == UNIVERSAL && !ti.tag && !ti.is_constructed)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
break; /* Ready */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (ti.class != UNIVERSAL || ti.tag != TAG_SEQUENCE)
|
|
|
|
|
goto bailout;
|
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (!bagseqndef)
|
|
|
|
|
{
|
|
|
|
|
if (bagseqlength < ti.nhdr)
|
|
|
|
|
goto bailout;
|
|
|
|
|
bagseqlength -= ti.nhdr;
|
|
|
|
|
if (bagseqlength < ti.length)
|
|
|
|
|
goto bailout;
|
|
|
|
|
bagseqlength -= ti.length;
|
|
|
|
|
}
|
|
|
|
|
lenndef = ti.ndef;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
len = ti.length;
|
|
|
|
|
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (lenndef)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
len = ti.nhdr;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
else
|
2011-02-04 12:57:53 +01:00
|
|
|
|
len -= ti.nhdr;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (ti.tag == TAG_OBJECT_ID && ti.length == DIM(oid_encryptedData)
|
|
|
|
|
&& !memcmp (p, oid_encryptedData, DIM(oid_encryptedData)))
|
|
|
|
|
{
|
2005-12-16 16:52:48 +01:00
|
|
|
|
size_t consumed = 0;
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
p += DIM(oid_encryptedData);
|
|
|
|
|
n -= DIM(oid_encryptedData);
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (!lenndef)
|
|
|
|
|
len -= DIM(oid_encryptedData);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
where = "bag.encryptedData";
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (parse_bag_encrypted_data (p, n, (p - p_start), &consumed, pw,
|
2006-07-21 11:41:11 +02:00
|
|
|
|
certcb, certcbarg,
|
2010-06-17 17:44:44 +02:00
|
|
|
|
result? NULL : &result, r_badpass))
|
2003-08-05 19:11:04 +02:00
|
|
|
|
goto bailout;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (lenndef)
|
|
|
|
|
len += consumed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
else if (ti.tag == TAG_OBJECT_ID && ti.length == DIM(oid_data)
|
2005-12-16 16:52:48 +01:00
|
|
|
|
&& !memcmp (p, oid_data, DIM(oid_data)))
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
2005-11-28 12:52:25 +01:00
|
|
|
|
if (result)
|
2005-12-16 16:52:48 +01:00
|
|
|
|
{
|
2006-07-21 11:41:11 +02:00
|
|
|
|
log_info ("already got an key object, skipping this one\n");
|
2005-12-16 16:52:48 +01:00
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
2005-11-28 12:52:25 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
2005-12-16 16:52:48 +01:00
|
|
|
|
size_t consumed = 0;
|
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
|
p += DIM(oid_data);
|
|
|
|
|
n -= DIM(oid_data);
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (!lenndef)
|
|
|
|
|
len -= DIM(oid_data);
|
|
|
|
|
result = parse_bag_data (p, n, (p - p_start), &consumed, pw);
|
2005-11-28 12:52:25 +01:00
|
|
|
|
if (!result)
|
|
|
|
|
goto bailout;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (lenndef)
|
|
|
|
|
len += consumed;
|
2005-11-28 12:52:25 +01:00
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
2005-12-16 16:52:48 +01:00
|
|
|
|
{
|
|
|
|
|
log_info ("unknown bag type - skipped\n");
|
|
|
|
|
p += ti.length;
|
|
|
|
|
n -= ti.length;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
if (len < 0 || len > n)
|
|
|
|
|
goto bailout;
|
|
|
|
|
p += len;
|
|
|
|
|
n -= len;
|
2005-12-16 16:52:48 +01:00
|
|
|
|
if (lenndef)
|
|
|
|
|
{
|
|
|
|
|
/* Need to skip the Null Tag. */
|
|
|
|
|
if (parse_tag (&p, &n, &ti))
|
|
|
|
|
goto bailout;
|
|
|
|
|
if (!(ti.class == UNIVERSAL && !ti.tag && !ti.is_constructed))
|
|
|
|
|
goto bailout;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2005-12-16 16:52:48 +01:00
|
|
|
|
gcry_free (cram_buffer);
|
2005-11-28 12:52:25 +01:00
|
|
|
|
return result;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
bailout:
|
2006-11-20 17:49:41 +01:00
|
|
|
|
log_error ("error at \"%s\", offset %u\n",
|
|
|
|
|
where, (unsigned int)(p - p_start));
|
2006-07-21 11:41:11 +02:00
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i=0; result[i]; i++)
|
|
|
|
|
gcry_mpi_release (result[i]);
|
|
|
|
|
gcry_free (result);
|
|
|
|
|
}
|
2005-12-16 16:52:48 +01:00
|
|
|
|
gcry_free (cram_buffer);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
|
compute_tag_length (size_t n)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
{
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int needed = 0;
|
|
|
|
|
|
|
|
|
|
if (n < 128)
|
|
|
|
|
needed += 2; /* tag and one length byte */
|
|
|
|
|
else if (n < 256)
|
|
|
|
|
needed += 3; /* tag, number of length bytes, 1 length byte */
|
|
|
|
|
else if (n < 65536)
|
|
|
|
|
needed += 4; /* tag, number of length bytes, 2 length bytes */
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log_error ("object too larger to encode\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return needed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned char *
|
|
|
|
|
store_tag_length (unsigned char *p, int tag, size_t n)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
{
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (tag == TAG_SEQUENCE)
|
|
|
|
|
tag |= 0x20; /* constructed */
|
|
|
|
|
|
|
|
|
|
*p++ = tag;
|
|
|
|
|
if (n < 128)
|
|
|
|
|
*p++ = n;
|
|
|
|
|
else if (n < 256)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0x81;
|
|
|
|
|
*p++ = n;
|
|
|
|
|
}
|
|
|
|
|
else if (n < 65536)
|
|
|
|
|
{
|
|
|
|
|
*p++ = 0x82;
|
|
|
|
|
*p++ = n >> 8;
|
|
|
|
|
*p++ = n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Create the final PKCS-12 object from the sequences contained in
|
2006-06-20 17:48:09 +02:00
|
|
|
|
SEQLIST. PW is the password. That array is terminated with an NULL
|
|
|
|
|
object. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
static unsigned char *
|
2006-06-20 17:48:09 +02:00
|
|
|
|
create_final (struct buffer_s *sequences, const char *pw, size_t *r_length)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
size_t needed = 0;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
size_t len[8], n;
|
2006-06-20 17:48:09 +02:00
|
|
|
|
unsigned char *macstart;
|
|
|
|
|
size_t maclen;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
unsigned char *result, *p;
|
|
|
|
|
size_t resultlen;
|
2006-06-20 17:48:09 +02:00
|
|
|
|
char salt[8];
|
|
|
|
|
unsigned char keybuf[20];
|
|
|
|
|
gcry_md_hd_t md;
|
|
|
|
|
int rc;
|
2006-07-21 11:41:11 +02:00
|
|
|
|
int with_mac = 1;
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2006-06-20 17:48:09 +02:00
|
|
|
|
/* 9 steps to create the pkcs#12 Krampf. */
|
|
|
|
|
|
|
|
|
|
/* 8. The MAC. */
|
|
|
|
|
/* We add this at step 0. */
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 7. All the buffers. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
for (i=0; sequences[i].buffer; i++)
|
|
|
|
|
needed += sequences[i].length;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 6. This goes into a sequences. */
|
|
|
|
|
len[6] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
needed += n;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 5. Encapsulate all in an octet string. */
|
|
|
|
|
len[5] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
needed += n;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 4. And tag it with [0]. */
|
|
|
|
|
len[4] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
needed += n;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 3. Prepend an data OID. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += 2 + DIM (oid_data);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 2. Put all into a sequences. */
|
|
|
|
|
len[2] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
needed += n;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 1. Prepend the version integer 3. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += 3;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 0. And the final outer sequence. */
|
2006-07-21 11:41:11 +02:00
|
|
|
|
if (with_mac)
|
|
|
|
|
needed += DIM (data_mactemplate);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
len[0] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
needed += n;
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Allocate a buffer. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
result = gcry_malloc (needed);
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating buffer\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
p = result;
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* 0. Store the very outer sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[0]);
|
|
|
|
|
|
|
|
|
|
/* 1. Store the version integer 3. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
*p++ = TAG_INTEGER;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
*p++ = 1;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
*p++ = 3;
|
2006-06-20 17:48:09 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* 2. Store another sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[2]);
|
|
|
|
|
|
|
|
|
|
/* 3. Store the data OID. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID, DIM (oid_data));
|
2011-02-04 12:57:53 +01:00
|
|
|
|
memcpy (p, oid_data, DIM (oid_data));
|
|
|
|
|
p += DIM (oid_data);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 4. Next comes a context tag. */
|
|
|
|
|
p = store_tag_length (p, 0xa0, len[4]);
|
|
|
|
|
|
|
|
|
|
/* 5. And an octet string. */
|
|
|
|
|
p = store_tag_length (p, TAG_OCTET_STRING, len[5]);
|
|
|
|
|
|
|
|
|
|
/* 6. And the inner sequence. */
|
2006-06-20 17:48:09 +02:00
|
|
|
|
macstart = p;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[6]);
|
|
|
|
|
|
|
|
|
|
/* 7. Append all the buffers. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
for (i=0; sequences[i].buffer; i++)
|
|
|
|
|
{
|
|
|
|
|
memcpy (p, sequences[i].buffer, sequences[i].length);
|
|
|
|
|
p += sequences[i].length;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-21 11:41:11 +02:00
|
|
|
|
if (with_mac)
|
2006-06-20 17:48:09 +02:00
|
|
|
|
{
|
2006-07-21 11:41:11 +02:00
|
|
|
|
/* Intermezzo to compute the MAC. */
|
|
|
|
|
maclen = p - macstart;
|
|
|
|
|
gcry_randomize (salt, 8, GCRY_STRONG_RANDOM);
|
|
|
|
|
if (string_to_key (3, salt, 8, 2048, pw, 20, keybuf))
|
|
|
|
|
{
|
|
|
|
|
gcry_free (result);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
rc = gcry_md_open (&md, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("gcry_md_open failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
gcry_free (result);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
rc = gcry_md_setkey (md, keybuf, 20);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("gcry_md_setkey failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
gcry_md_close (md);
|
|
|
|
|
gcry_free (result);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
gcry_md_write (md, macstart, maclen);
|
|
|
|
|
|
|
|
|
|
/* 8. Append the MAC template and fix it up. */
|
|
|
|
|
memcpy (p, data_mactemplate, DIM (data_mactemplate));
|
|
|
|
|
memcpy (p + DATA_MACTEMPLATE_SALT_OFF, salt, 8);
|
|
|
|
|
memcpy (p + DATA_MACTEMPLATE_MAC_OFF, gcry_md_read (md, 0), 20);
|
|
|
|
|
p += DIM (data_mactemplate);
|
2006-06-20 17:48:09 +02:00
|
|
|
|
gcry_md_close (md);
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* Ready. */
|
|
|
|
|
resultlen = p - result;
|
|
|
|
|
if (needed != resultlen)
|
2005-06-29 16:12:18 +02:00
|
|
|
|
log_debug ("length mismatch: %lu, %lu\n",
|
|
|
|
|
(unsigned long)needed, (unsigned long)resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
*r_length = resultlen;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Build a DER encoded SEQUENCE with the key:
|
|
|
|
|
|
|
|
|
|
SEQUENCE {
|
|
|
|
|
INTEGER 0
|
|
|
|
|
SEQUENCE {
|
|
|
|
|
OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
|
|
|
|
|
NULL
|
|
|
|
|
}
|
|
|
|
|
OCTET STRING, encapsulates {
|
|
|
|
|
SEQUENCE {
|
|
|
|
|
INTEGER 0
|
|
|
|
|
INTEGER
|
2011-02-04 12:57:53 +01:00
|
|
|
|
INTEGER
|
2004-02-19 17:26:32 +01:00
|
|
|
|
INTEGER
|
|
|
|
|
INTEGER
|
|
|
|
|
INTEGER
|
|
|
|
|
INTEGER
|
|
|
|
|
INTEGER
|
|
|
|
|
INTEGER
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-03 18:57:33 +02:00
|
|
|
|
|
|
|
|
|
MODE controls what is being generated:
|
|
|
|
|
0 - As described above
|
|
|
|
|
1 - Ditto but without the padding
|
|
|
|
|
2 - Only the inner part (pkcs#1)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static unsigned char *
|
2014-06-03 18:57:33 +02:00
|
|
|
|
build_key_sequence (gcry_mpi_t *kparms, int mode, size_t *r_length)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
int rc, i;
|
|
|
|
|
size_t needed, n;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
unsigned char *plain, *p;
|
|
|
|
|
size_t plainlen;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
size_t outseqlen, oidseqlen, octstrlen, inseqlen;
|
|
|
|
|
|
2014-06-03 18:57:33 +02:00
|
|
|
|
needed = 3; /* The version integer with value 0. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
for (i=0; kparms[i]; i++)
|
|
|
|
|
{
|
|
|
|
|
n = 0;
|
|
|
|
|
rc = gcry_mpi_print (GCRYMPI_FMT_STD, NULL, 0, &n, kparms[i]);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error formatting parameter: %s\n", gpg_strerror (rc));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
needed += n;
|
|
|
|
|
n = compute_tag_length (n);
|
|
|
|
|
if (!n)
|
|
|
|
|
return NULL;
|
|
|
|
|
needed += n;
|
|
|
|
|
}
|
|
|
|
|
if (i != 8)
|
|
|
|
|
{
|
2010-01-08 20:18:49 +01:00
|
|
|
|
log_error ("invalid parameters for p12_build\n");
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
/* Now this all goes into a sequence. */
|
|
|
|
|
inseqlen = needed;
|
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
if (!n)
|
|
|
|
|
return NULL;
|
|
|
|
|
needed += n;
|
2014-06-03 18:57:33 +02:00
|
|
|
|
|
|
|
|
|
if (mode != 2)
|
|
|
|
|
{
|
|
|
|
|
/* Encapsulate all into an octet string. */
|
|
|
|
|
octstrlen = needed;
|
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
if (!n)
|
|
|
|
|
return NULL;
|
|
|
|
|
needed += n;
|
|
|
|
|
/* Prepend the object identifier sequence. */
|
|
|
|
|
oidseqlen = 2 + DIM (oid_rsaEncryption) + 2;
|
|
|
|
|
needed += 2 + oidseqlen;
|
|
|
|
|
/* The version number. */
|
|
|
|
|
needed += 3;
|
|
|
|
|
/* And finally put the whole thing into a sequence. */
|
|
|
|
|
outseqlen = needed;
|
|
|
|
|
n = compute_tag_length (needed);
|
|
|
|
|
if (!n)
|
|
|
|
|
return NULL;
|
|
|
|
|
needed += n;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* allocate 8 extra bytes for padding */
|
|
|
|
|
plain = gcry_malloc_secure (needed+8);
|
|
|
|
|
if (!plain)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating encryption buffer\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
/* And now fill the plaintext buffer. */
|
|
|
|
|
p = plain;
|
2014-06-03 18:57:33 +02:00
|
|
|
|
if (mode != 2)
|
|
|
|
|
{
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, outseqlen);
|
|
|
|
|
/* Store version. */
|
|
|
|
|
*p++ = TAG_INTEGER;
|
|
|
|
|
*p++ = 1;
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
/* Store object identifier sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, oidseqlen);
|
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID, DIM (oid_rsaEncryption));
|
|
|
|
|
memcpy (p, oid_rsaEncryption, DIM (oid_rsaEncryption));
|
|
|
|
|
p += DIM (oid_rsaEncryption);
|
|
|
|
|
*p++ = TAG_NULL;
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
/* Start with the octet string. */
|
|
|
|
|
p = store_tag_length (p, TAG_OCTET_STRING, octstrlen);
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, inseqlen);
|
|
|
|
|
/* Store the key parameters. */
|
|
|
|
|
*p++ = TAG_INTEGER;
|
|
|
|
|
*p++ = 1;
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
for (i=0; kparms[i]; i++)
|
|
|
|
|
{
|
|
|
|
|
n = 0;
|
|
|
|
|
rc = gcry_mpi_print (GCRYMPI_FMT_STD, NULL, 0, &n, kparms[i]);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("oops: error formatting parameter: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
gcry_free (plain);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
p = store_tag_length (p, TAG_INTEGER, n);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
n = plain + needed - p;
|
|
|
|
|
rc = gcry_mpi_print (GCRYMPI_FMT_STD, p, n, &n, kparms[i]);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("oops: error storing parameter: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
gcry_free (plain);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
p += n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plainlen = p - plain;
|
|
|
|
|
assert (needed == plainlen);
|
2014-06-03 18:57:33 +02:00
|
|
|
|
|
|
|
|
|
if (!mode)
|
|
|
|
|
{
|
|
|
|
|
/* Append some pad characters; we already allocated extra space. */
|
|
|
|
|
n = 8 - plainlen % 8;
|
|
|
|
|
for (i=0; i < n; i++, plainlen++)
|
|
|
|
|
*p++ = n;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
*r_length = plainlen;
|
|
|
|
|
return plain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char *
|
|
|
|
|
build_key_bag (unsigned char *buffer, size_t buflen, char *salt,
|
2006-07-24 13:20:33 +02:00
|
|
|
|
const unsigned char *sha1hash, const char *keyidstr,
|
2004-02-19 17:26:32 +01:00
|
|
|
|
size_t *r_length)
|
|
|
|
|
{
|
|
|
|
|
size_t len[11], needed;
|
|
|
|
|
unsigned char *p, *keybag;
|
|
|
|
|
size_t keybaglen;
|
|
|
|
|
|
|
|
|
|
/* Walk 11 steps down to collect the info: */
|
|
|
|
|
|
|
|
|
|
/* 10. The data goes into an octet string. */
|
|
|
|
|
needed = compute_tag_length (buflen);
|
|
|
|
|
needed += buflen;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* 9. Prepend the algorithm identifier. */
|
|
|
|
|
needed += DIM (data_3desiter2048);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* 8. Put a sequence around. */
|
|
|
|
|
len[8] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += compute_tag_length (needed);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 7. Prepend a [0] tag. */
|
|
|
|
|
len[7] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += compute_tag_length (needed);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
/* 6b. The attributes which are appended at the end. */
|
|
|
|
|
if (sha1hash)
|
|
|
|
|
needed += DIM (data_attrtemplate) + 20;
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* 6. Prepend the shroudedKeyBag OID. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += 2 + DIM (oid_pkcs_12_pkcs_8ShroudedKeyBag);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 5+4. Put all into two sequences. */
|
|
|
|
|
len[5] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += compute_tag_length ( needed);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
len[4] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += compute_tag_length (needed);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 3. This all goes into an octet string. */
|
|
|
|
|
len[3] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += compute_tag_length (needed);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 2. Prepend another [0] tag. */
|
|
|
|
|
len[2] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += compute_tag_length (needed);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 1. Prepend the data OID. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += 2 + DIM (oid_data);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 0. Prepend another sequence. */
|
|
|
|
|
len[0] = needed;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Now that we have all length information, allocate a buffer. */
|
|
|
|
|
p = keybag = gcry_malloc (needed);
|
|
|
|
|
if (!keybag)
|
2003-08-05 19:11:04 +02:00
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating buffer\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* Walk 11 steps up to store the data. */
|
|
|
|
|
|
|
|
|
|
/* 0. Store the first sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[0]);
|
|
|
|
|
|
|
|
|
|
/* 1. Store the data OID. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID, DIM (oid_data));
|
2011-02-04 12:57:53 +01:00
|
|
|
|
memcpy (p, oid_data, DIM (oid_data));
|
|
|
|
|
p += DIM (oid_data);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 2. Store a [0] tag. */
|
|
|
|
|
p = store_tag_length (p, 0xa0, len[2]);
|
|
|
|
|
|
|
|
|
|
/* 3. And an octet string. */
|
|
|
|
|
p = store_tag_length (p, TAG_OCTET_STRING, len[3]);
|
|
|
|
|
|
|
|
|
|
/* 4+5. Two sequences. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[4]);
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[5]);
|
|
|
|
|
|
|
|
|
|
/* 6. Store the shroudedKeyBag OID. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID,
|
|
|
|
|
DIM (oid_pkcs_12_pkcs_8ShroudedKeyBag));
|
|
|
|
|
memcpy (p, oid_pkcs_12_pkcs_8ShroudedKeyBag,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
DIM (oid_pkcs_12_pkcs_8ShroudedKeyBag));
|
|
|
|
|
p += DIM (oid_pkcs_12_pkcs_8ShroudedKeyBag);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 7. Store a [0] tag. */
|
|
|
|
|
p = store_tag_length (p, 0xa0, len[7]);
|
|
|
|
|
|
|
|
|
|
/* 8. Store a sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[8]);
|
|
|
|
|
|
|
|
|
|
/* 9. Now for the pre-encoded algorithm identifier and the salt. */
|
|
|
|
|
memcpy (p, data_3desiter2048, DIM (data_3desiter2048));
|
|
|
|
|
memcpy (p + DATA_3DESITER2048_SALT_OFF, salt, 8);
|
|
|
|
|
p += DIM (data_3desiter2048);
|
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
/* 10. And the octet string with the encrypted data. */
|
2004-02-19 17:26:32 +01:00
|
|
|
|
p = store_tag_length (p, TAG_OCTET_STRING, buflen);
|
|
|
|
|
memcpy (p, buffer, buflen);
|
|
|
|
|
p += buflen;
|
2006-07-24 13:20:33 +02:00
|
|
|
|
|
|
|
|
|
/* Append the attributes whose length we calculated at step 2b. */
|
|
|
|
|
if (sha1hash)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
memcpy (p, data_attrtemplate, DIM (data_attrtemplate));
|
|
|
|
|
for (i=0; i < 8; i++)
|
|
|
|
|
p[DATA_ATTRTEMPLATE_KEYID_OFF+2*i+1] = keyidstr[i];
|
|
|
|
|
p += DIM (data_attrtemplate);
|
|
|
|
|
memcpy (p, sha1hash, 20);
|
|
|
|
|
p += 20;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
keybaglen = p - keybag;
|
|
|
|
|
if (needed != keybaglen)
|
2005-06-29 16:12:18 +02:00
|
|
|
|
log_debug ("length mismatch: %lu, %lu\n",
|
|
|
|
|
(unsigned long)needed, (unsigned long)keybaglen);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
*r_length = keybaglen;
|
|
|
|
|
return keybag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char *
|
|
|
|
|
build_cert_bag (unsigned char *buffer, size_t buflen, char *salt,
|
|
|
|
|
size_t *r_length)
|
|
|
|
|
{
|
|
|
|
|
size_t len[9], needed;
|
|
|
|
|
unsigned char *p, *certbag;
|
|
|
|
|
size_t certbaglen;
|
|
|
|
|
|
|
|
|
|
/* Walk 9 steps down to collect the info: */
|
|
|
|
|
|
|
|
|
|
/* 8. The data goes into an octet string. */
|
|
|
|
|
needed = compute_tag_length (buflen);
|
|
|
|
|
needed += buflen;
|
|
|
|
|
|
|
|
|
|
/* 7. The algorithm identifier. */
|
|
|
|
|
needed += DIM (data_rc2iter2048);
|
|
|
|
|
|
|
|
|
|
/* 6. The data OID. */
|
|
|
|
|
needed += 2 + DIM (oid_data);
|
|
|
|
|
|
|
|
|
|
/* 5. A sequence. */
|
|
|
|
|
len[5] = needed;
|
|
|
|
|
needed += compute_tag_length ( needed);
|
|
|
|
|
|
|
|
|
|
/* 4. An integer. */
|
|
|
|
|
needed += 3;
|
|
|
|
|
|
|
|
|
|
/* 3. A sequence. */
|
|
|
|
|
len[3] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
|
|
|
|
/* 2. A [0] tag. */
|
|
|
|
|
len[2] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
|
|
|
|
/* 1. The encryptedData OID. */
|
|
|
|
|
needed += 2 + DIM (oid_encryptedData);
|
|
|
|
|
|
|
|
|
|
/* 0. The first sequence. */
|
|
|
|
|
len[0] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
|
|
|
|
/* Now that we have all length information, allocate a buffer. */
|
|
|
|
|
p = certbag = gcry_malloc (needed);
|
|
|
|
|
if (!certbag)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating buffer\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Walk 9 steps up to store the data. */
|
|
|
|
|
|
|
|
|
|
/* 0. Store the first sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[0]);
|
|
|
|
|
|
|
|
|
|
/* 1. Store the encryptedData OID. */
|
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID, DIM (oid_encryptedData));
|
2011-02-04 12:57:53 +01:00
|
|
|
|
memcpy (p, oid_encryptedData, DIM (oid_encryptedData));
|
|
|
|
|
p += DIM (oid_encryptedData);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 2. Store a [0] tag. */
|
2005-09-09 15:48:48 +02:00
|
|
|
|
p = store_tag_length (p, 0xa0, len[2]);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 3. Store a sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[3]);
|
|
|
|
|
|
|
|
|
|
/* 4. Store the integer 0. */
|
|
|
|
|
*p++ = TAG_INTEGER;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
*p++ = 1;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
*p++ = 0;
|
|
|
|
|
|
|
|
|
|
/* 5. Store a sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[5]);
|
|
|
|
|
|
|
|
|
|
/* 6. Store the data OID. */
|
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID, DIM (oid_data));
|
2011-02-04 12:57:53 +01:00
|
|
|
|
memcpy (p, oid_data, DIM (oid_data));
|
|
|
|
|
p += DIM (oid_data);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 7. Now for the pre-encoded algorithm identifier and the salt. */
|
|
|
|
|
memcpy (p, data_rc2iter2048, DIM (data_rc2iter2048));
|
|
|
|
|
memcpy (p + DATA_RC2ITER2048_SALT_OFF, salt, 8);
|
|
|
|
|
p += DIM (data_rc2iter2048);
|
|
|
|
|
|
|
|
|
|
/* 8. And finally the [0] tag with the encrypted data. */
|
2005-09-09 15:48:48 +02:00
|
|
|
|
p = store_tag_length (p, 0x80, buflen);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
memcpy (p, buffer, buflen);
|
|
|
|
|
p += buflen;
|
|
|
|
|
certbaglen = p - certbag;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
if (needed != certbaglen)
|
2005-06-29 16:12:18 +02:00
|
|
|
|
log_debug ("length mismatch: %lu, %lu\n",
|
|
|
|
|
(unsigned long)needed, (unsigned long)certbaglen);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
*r_length = certbaglen;
|
|
|
|
|
return certbag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char *
|
2011-02-04 12:57:53 +01:00
|
|
|
|
build_cert_sequence (const unsigned char *buffer, size_t buflen,
|
2006-07-24 13:20:33 +02:00
|
|
|
|
const unsigned char *sha1hash, const char *keyidstr,
|
|
|
|
|
size_t *r_length)
|
2004-02-19 17:26:32 +01:00
|
|
|
|
{
|
|
|
|
|
size_t len[8], needed, n;
|
|
|
|
|
unsigned char *p, *certseq;
|
|
|
|
|
size_t certseqlen;
|
2005-09-16 14:49:53 +02:00
|
|
|
|
int i;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
assert (strlen (keyidstr) == 8);
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Walk 8 steps down to collect the info: */
|
|
|
|
|
|
|
|
|
|
/* 7. The data goes into an octet string. */
|
|
|
|
|
needed = compute_tag_length (buflen);
|
|
|
|
|
needed += buflen;
|
|
|
|
|
|
|
|
|
|
/* 6. A [0] tag. */
|
|
|
|
|
len[6] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
|
|
|
|
/* 5. An OID. */
|
|
|
|
|
needed += 2 + DIM (oid_x509Certificate_for_pkcs_12);
|
|
|
|
|
|
|
|
|
|
/* 4. A sequence. */
|
|
|
|
|
len[4] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
|
|
|
|
/* 3. A [0] tag. */
|
|
|
|
|
len[3] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
/* 2b. The attributes which are appended at the end. */
|
|
|
|
|
if (sha1hash)
|
|
|
|
|
needed += DIM (data_attrtemplate) + 20;
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* 2. An OID. */
|
|
|
|
|
needed += 2 + DIM (oid_pkcs_12_CertBag);
|
|
|
|
|
|
|
|
|
|
/* 1. A sequence. */
|
|
|
|
|
len[1] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
|
|
|
|
/* 0. The first sequence. */
|
|
|
|
|
len[0] = needed;
|
|
|
|
|
needed += compute_tag_length (needed);
|
|
|
|
|
|
|
|
|
|
/* Now that we have all length information, allocate a buffer. */
|
|
|
|
|
p = certseq = gcry_malloc (needed + 8 /*(for padding)*/);
|
|
|
|
|
if (!certseq)
|
|
|
|
|
{
|
|
|
|
|
log_error ("error allocating buffer\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Walk 8 steps up to store the data. */
|
|
|
|
|
|
|
|
|
|
/* 0. Store the first sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[0]);
|
|
|
|
|
|
|
|
|
|
/* 1. Store the second sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[1]);
|
|
|
|
|
|
|
|
|
|
/* 2. Store the pkcs12-cert-bag OID. */
|
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID, DIM (oid_pkcs_12_CertBag));
|
2011-02-04 12:57:53 +01:00
|
|
|
|
memcpy (p, oid_pkcs_12_CertBag, DIM (oid_pkcs_12_CertBag));
|
|
|
|
|
p += DIM (oid_pkcs_12_CertBag);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 3. Store a [0] tag. */
|
|
|
|
|
p = store_tag_length (p, 0xa0, len[3]);
|
|
|
|
|
|
|
|
|
|
/* 4. Store a sequence. */
|
|
|
|
|
p = store_tag_length (p, TAG_SEQUENCE, len[4]);
|
|
|
|
|
|
|
|
|
|
/* 5. Store the x509Certificate OID. */
|
|
|
|
|
p = store_tag_length (p, TAG_OBJECT_ID,
|
|
|
|
|
DIM (oid_x509Certificate_for_pkcs_12));
|
|
|
|
|
memcpy (p, oid_x509Certificate_for_pkcs_12,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
DIM (oid_x509Certificate_for_pkcs_12));
|
|
|
|
|
p += DIM (oid_x509Certificate_for_pkcs_12);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* 6. Store a [0] tag. */
|
|
|
|
|
p = store_tag_length (p, 0xa0, len[6]);
|
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
/* 7. And the octet string with the actual certificate. */
|
2004-02-19 17:26:32 +01:00
|
|
|
|
p = store_tag_length (p, TAG_OCTET_STRING, buflen);
|
|
|
|
|
memcpy (p, buffer, buflen);
|
|
|
|
|
p += buflen;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
/* Append the attributes whose length we calculated at step 2b. */
|
|
|
|
|
if (sha1hash)
|
|
|
|
|
{
|
|
|
|
|
memcpy (p, data_attrtemplate, DIM (data_attrtemplate));
|
|
|
|
|
for (i=0; i < 8; i++)
|
|
|
|
|
p[DATA_ATTRTEMPLATE_KEYID_OFF+2*i+1] = keyidstr[i];
|
|
|
|
|
p += DIM (data_attrtemplate);
|
|
|
|
|
memcpy (p, sha1hash, 20);
|
|
|
|
|
p += 20;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
certseqlen = p - certseq;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
if (needed != certseqlen)
|
2005-06-29 16:12:18 +02:00
|
|
|
|
log_debug ("length mismatch: %lu, %lu\n",
|
|
|
|
|
(unsigned long)needed, (unsigned long)certseqlen);
|
2006-07-24 13:20:33 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Append some pad characters; we already allocated extra space. */
|
|
|
|
|
n = 8 - certseqlen % 8;
|
2005-09-16 14:49:53 +02:00
|
|
|
|
for (i=0; i < n; i++, certseqlen++)
|
2004-02-19 17:26:32 +01:00
|
|
|
|
*p++ = n;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
*r_length = certseqlen;
|
|
|
|
|
return certseq;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-03-20 11:00:55 +01:00
|
|
|
|
/* Expect the RSA key parameters in KPARMS and a password in PW.
|
|
|
|
|
Create a PKCS structure from it and return it as well as the length
|
|
|
|
|
in R_LENGTH; return NULL in case of an error. If CHARSET is not
|
|
|
|
|
NULL, re-encode PW to that character set. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
unsigned char *
|
2010-06-21 12:01:24 +02:00
|
|
|
|
p12_build (gcry_mpi_t *kparms, const void *cert, size_t certlen,
|
2007-03-20 11:00:55 +01:00
|
|
|
|
const char *pw, const char *charset, size_t *r_length)
|
2004-02-19 17:26:32 +01:00
|
|
|
|
{
|
2007-03-20 11:00:55 +01:00
|
|
|
|
unsigned char *buffer = NULL;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
size_t n, buflen;
|
|
|
|
|
char salt[8];
|
2005-09-09 13:18:08 +02:00
|
|
|
|
struct buffer_s seqlist[3];
|
2004-02-19 17:26:32 +01:00
|
|
|
|
int seqlistidx = 0;
|
2006-07-24 13:20:33 +02:00
|
|
|
|
unsigned char sha1hash[20];
|
|
|
|
|
char keyidstr[8+1];
|
2007-03-20 11:00:55 +01:00
|
|
|
|
char *pwbuf = NULL;
|
|
|
|
|
size_t pwbufsize = 0;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
2005-06-16 10:12:03 +02:00
|
|
|
|
n = buflen = 0; /* (avoid compiler warning). */
|
2006-07-24 13:20:33 +02:00
|
|
|
|
memset (sha1hash, 0, 20);
|
|
|
|
|
*keyidstr = 0;
|
2005-06-16 10:12:03 +02:00
|
|
|
|
|
2007-03-20 11:00:55 +01:00
|
|
|
|
if (charset && pw && *pw)
|
|
|
|
|
{
|
2007-06-14 19:05:07 +02:00
|
|
|
|
jnlib_iconv_t cd;
|
2007-03-20 11:00:55 +01:00
|
|
|
|
const char *inptr;
|
|
|
|
|
char *outptr;
|
|
|
|
|
size_t inbytes, outbytes;
|
|
|
|
|
|
|
|
|
|
/* We assume that the converted passphrase is at max 2 times
|
|
|
|
|
longer than its utf-8 encoding. */
|
|
|
|
|
pwbufsize = strlen (pw)*2 + 1;
|
|
|
|
|
pwbuf = gcry_malloc_secure (pwbufsize);
|
|
|
|
|
if (!pwbuf)
|
|
|
|
|
{
|
|
|
|
|
log_error ("out of secure memory while converting passphrase\n");
|
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-14 19:05:07 +02:00
|
|
|
|
cd = jnlib_iconv_open (charset, "utf-8");
|
|
|
|
|
if (cd == (jnlib_iconv_t)(-1))
|
2007-03-20 11:00:55 +01:00
|
|
|
|
{
|
|
|
|
|
log_error ("can't convert passphrase to"
|
2012-06-05 19:29:22 +02:00
|
|
|
|
" requested charset '%s': %s\n",
|
2007-03-20 11:00:55 +01:00
|
|
|
|
charset, strerror (errno));
|
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inptr = pw;
|
|
|
|
|
inbytes = strlen (pw);
|
|
|
|
|
outptr = pwbuf;
|
|
|
|
|
outbytes = pwbufsize - 1;
|
2007-06-14 19:05:07 +02:00
|
|
|
|
if ( jnlib_iconv (cd, (const char **)&inptr, &inbytes,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
&outptr, &outbytes) == (size_t)-1)
|
2007-03-20 11:00:55 +01:00
|
|
|
|
{
|
|
|
|
|
log_error ("error converting passphrase to"
|
2012-06-05 19:29:22 +02:00
|
|
|
|
" requested charset '%s': %s\n",
|
2007-03-20 11:00:55 +01:00
|
|
|
|
charset, strerror (errno));
|
2007-06-14 19:05:07 +02:00
|
|
|
|
jnlib_iconv_close (cd);
|
2007-03-20 11:00:55 +01:00
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
*outptr = 0;
|
2007-06-14 19:05:07 +02:00
|
|
|
|
jnlib_iconv_close (cd);
|
2007-03-20 11:00:55 +01:00
|
|
|
|
pw = pwbuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
if (cert && certlen)
|
|
|
|
|
{
|
2006-07-24 13:20:33 +02:00
|
|
|
|
/* Calculate the hash value we need for the bag attributes. */
|
|
|
|
|
gcry_md_hash_buffer (GCRY_MD_SHA1, sha1hash, cert, certlen);
|
|
|
|
|
sprintf (keyidstr, "%02x%02x%02x%02x",
|
|
|
|
|
sha1hash[16], sha1hash[17], sha1hash[18], sha1hash[19]);
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Encode the certificate. */
|
2006-07-24 13:20:33 +02:00
|
|
|
|
buffer = build_cert_sequence (cert, certlen, sha1hash, keyidstr,
|
|
|
|
|
&buflen);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
if (!buffer)
|
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
|
|
/* Encrypt it. */
|
|
|
|
|
gcry_randomize (salt, 8, GCRY_STRONG_RANDOM);
|
2011-03-10 15:27:10 +01:00
|
|
|
|
crypt_block (buffer, buflen, salt, 8, 2048, NULL, 0, pw,
|
2005-12-16 16:52:48 +01:00
|
|
|
|
GCRY_CIPHER_RFC2268_40, 1);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Encode the encrypted stuff into a bag. */
|
|
|
|
|
seqlist[seqlistidx].buffer = build_cert_bag (buffer, buflen, salt, &n);
|
|
|
|
|
seqlist[seqlistidx].length = n;
|
|
|
|
|
gcry_free (buffer);
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
if (!seqlist[seqlistidx].buffer)
|
|
|
|
|
goto failure;
|
|
|
|
|
seqlistidx++;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-24 13:20:33 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
if (kparms)
|
|
|
|
|
{
|
|
|
|
|
/* Encode the key. */
|
2014-06-03 18:57:33 +02:00
|
|
|
|
buffer = build_key_sequence (kparms, 0, &buflen);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
if (!buffer)
|
|
|
|
|
goto failure;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
/* Encrypt it. */
|
|
|
|
|
gcry_randomize (salt, 8, GCRY_STRONG_RANDOM);
|
2011-03-10 15:27:10 +01:00
|
|
|
|
crypt_block (buffer, buflen, salt, 8, 2048, NULL, 0,
|
|
|
|
|
pw, GCRY_CIPHER_3DES, 1);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
/* Encode the encrypted stuff into a bag. */
|
2006-07-24 13:20:33 +02:00
|
|
|
|
if (cert && certlen)
|
2011-02-04 12:57:53 +01:00
|
|
|
|
seqlist[seqlistidx].buffer = build_key_bag (buffer, buflen, salt,
|
2006-07-24 13:20:33 +02:00
|
|
|
|
sha1hash, keyidstr, &n);
|
|
|
|
|
else
|
|
|
|
|
seqlist[seqlistidx].buffer = build_key_bag (buffer, buflen, salt,
|
|
|
|
|
NULL, NULL, &n);
|
2004-02-19 17:26:32 +01:00
|
|
|
|
seqlist[seqlistidx].length = n;
|
|
|
|
|
gcry_free (buffer);
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
if (!seqlist[seqlistidx].buffer)
|
|
|
|
|
goto failure;
|
|
|
|
|
seqlistidx++;
|
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
seqlist[seqlistidx].buffer = NULL;
|
|
|
|
|
seqlist[seqlistidx].length = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2006-06-20 17:48:09 +02:00
|
|
|
|
buffer = create_final (seqlist, pw, &buflen);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
failure:
|
2007-03-20 11:00:55 +01:00
|
|
|
|
if (pwbuf)
|
|
|
|
|
{
|
2015-01-26 17:56:52 +01:00
|
|
|
|
/* Note that wipememory is not really needed due to the use of
|
|
|
|
|
gcry_malloc_secure. */
|
2007-03-20 11:00:55 +01:00
|
|
|
|
wipememory (pwbuf, pwbufsize);
|
|
|
|
|
gcry_free (pwbuf);
|
|
|
|
|
}
|
2004-02-19 17:26:32 +01:00
|
|
|
|
for ( ; seqlistidx; seqlistidx--)
|
|
|
|
|
gcry_free (seqlist[seqlistidx].buffer);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
|
*r_length = buffer? buflen : 0;
|
|
|
|
|
return buffer;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-06-03 18:57:33 +02:00
|
|
|
|
/* This is actually not a pkcs#12 function but one which creates an
|
|
|
|
|
unencrypted a pkcs#1 private key. */
|
|
|
|
|
unsigned char *
|
|
|
|
|
p12_raw_build (gcry_mpi_t *kparms, int rawmode, size_t *r_length)
|
|
|
|
|
{
|
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
size_t buflen;
|
|
|
|
|
|
|
|
|
|
assert (rawmode == 1 || rawmode == 2);
|
|
|
|
|
buffer = build_key_sequence (kparms, rawmode, &buflen);
|
|
|
|
|
if (!buffer)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
*r_length = buflen;
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#ifdef TEST
|
2004-02-10 20:26:55 +01:00
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
static void
|
2004-02-10 20:26:55 +01:00
|
|
|
|
cert_cb (void *opaque, const unsigned char *cert, size_t certlen)
|
|
|
|
|
{
|
|
|
|
|
printf ("got a certificate of %u bytes length\n", certlen);
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
struct stat st;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
unsigned char *buf;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
size_t buflen;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
gcry_mpi_t *result;
|
2010-06-21 12:01:24 +02:00
|
|
|
|
int badpass;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
if (argc != 3)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "usage: testp12 file passphrase\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gcry_control (GCRYCTL_DISABLE_SECMEM, NULL);
|
|
|
|
|
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL);
|
|
|
|
|
|
|
|
|
|
fp = fopen (argv[1], "rb");
|
|
|
|
|
if (!fp)
|
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
|
fprintf (stderr, "can't open '%s': %s\n", argv[1], strerror (errno));
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (fstat (fileno(fp), &st))
|
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
|
fprintf (stderr, "can't stat '%s': %s\n", argv[1], strerror (errno));
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buflen = st.st_size;
|
|
|
|
|
buf = gcry_malloc (buflen+1);
|
|
|
|
|
if (!buf || fread (buf, buflen, 1, fp) != 1)
|
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
|
fprintf (stderr, "error reading '%s': %s\n", argv[1], strerror (errno));
|
2003-08-05 19:11:04 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
fclose (fp);
|
|
|
|
|
|
2010-06-21 12:01:24 +02:00
|
|
|
|
result = p12_parse (buf, buflen, argv[2], cert_cb, NULL, &badpass);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
int i, rc;
|
2004-02-10 20:26:55 +01:00
|
|
|
|
unsigned char *tmpbuf;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
for (i=0; result[i]; i++)
|
|
|
|
|
{
|
2004-02-10 20:26:55 +01:00
|
|
|
|
rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &tmpbuf,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
NULL, result[i]);
|
|
|
|
|
if (rc)
|
|
|
|
|
printf ("%d: [error printing number: %s]\n",
|
|
|
|
|
i, gpg_strerror (rc));
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-02-10 20:26:55 +01:00
|
|
|
|
printf ("%d: %s\n", i, tmpbuf);
|
|
|
|
|
gcry_free (tmpbuf);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
2004-02-10 20:26:55 +01:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Local Variables:
|
2010-03-10 13:24:58 +01:00
|
|
|
|
compile-command: "gcc -Wall -O0 -g -DTEST=1 -o minip12 minip12.c ../common/libcommon.a -L /usr/local/lib -lgcrypt -lgpg-error"
|
2004-02-10 20:26:55 +01:00
|
|
|
|
End:
|
|
|
|
|
*/
|
2003-08-05 19:11:04 +02:00
|
|
|
|
#endif /* TEST */
|