2006-05-16 02:01:28 +02:00
|
|
|
/*
|
2009-10-14 22:09:10 +02:00
|
|
|
* Copyright 2005-2009 Juan Lang
|
2006-05-16 02:01:28 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2006-05-16 02:01:28 +02:00
|
|
|
*
|
|
|
|
* This file implements ASN.1 DER decoding of a limited set of types.
|
|
|
|
* It isn't a full ASN.1 implementation. Microsoft implements BER
|
2007-09-18 00:00:30 +02:00
|
|
|
* encoding of many of the basic types in msasn1.dll, but that interface isn't
|
|
|
|
* implemented, so I implement them here.
|
2006-05-16 02:01:28 +02:00
|
|
|
*
|
|
|
|
* References:
|
|
|
|
* "A Layman's Guide to a Subset of ASN.1, BER, and DER", by Burton Kaliski
|
|
|
|
* (available online, look for a PDF copy as the HTML versions tend to have
|
|
|
|
* translation errors.)
|
|
|
|
*
|
|
|
|
* RFC3280, http://www.faqs.org/rfcs/rfc3280.html
|
|
|
|
*
|
2007-09-18 00:00:30 +02:00
|
|
|
* MSDN, especially "Constants for CryptEncodeObject and CryptDecodeObject"
|
2006-05-16 02:01:28 +02:00
|
|
|
*/
|
|
|
|
|
2008-02-01 06:28:58 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wincrypt.h"
|
2006-07-19 16:44:48 +02:00
|
|
|
#include "winnls.h"
|
2006-05-16 02:01:28 +02:00
|
|
|
#include "snmp.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/exception.h"
|
|
|
|
#include "crypt32_private.h"
|
|
|
|
|
|
|
|
/* This is a bit arbitrary, but to set some limit: */
|
|
|
|
#define MAX_ENCODED_LEN 0x02000000
|
|
|
|
|
|
|
|
#define ASN_FLAGS_MASK 0xe0
|
|
|
|
#define ASN_TYPE_MASK 0x1f
|
|
|
|
|
2007-08-07 18:19:17 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(cryptasn);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(crypt);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
typedef BOOL (WINAPI *CryptDecodeObjectFunc)(DWORD, LPCSTR, const BYTE *,
|
|
|
|
DWORD, DWORD, void *, DWORD *);
|
|
|
|
typedef BOOL (WINAPI *CryptDecodeObjectExFunc)(DWORD, LPCSTR, const BYTE *,
|
|
|
|
DWORD, DWORD, PCRYPT_DECODE_PARA, void *, DWORD *);
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
/* Internal decoders don't do memory allocation or exception handling, and
|
|
|
|
* they report how many bytes they decoded.
|
|
|
|
*/
|
|
|
|
typedef BOOL (*InternalDecodeFunc)(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded);
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeChoiceOfTimeInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded);
|
|
|
|
static BOOL CRYPT_AsnDecodePubKeyInfoInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded);
|
2009-10-14 21:43:14 +02:00
|
|
|
/* Assumes pvStructInfo is a CERT_EXTENSION whose pszObjId is set ahead of time.
|
|
|
|
*/
|
|
|
|
static BOOL CRYPT_AsnDecodeExtension(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded);
|
2007-09-18 02:13:12 +02:00
|
|
|
/* Assumes algo->Parameters.pbData is set ahead of time. */
|
|
|
|
static BOOL CRYPT_AsnDecodeAlgorithmId(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded);
|
|
|
|
static BOOL CRYPT_AsnDecodeBool(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
/* Assumes the CRYPT_DATA_BLOB's pbData member has been initialized */
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeOctetsInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded);
|
2008-11-05 20:47:38 +01:00
|
|
|
/* Doesn't check the tag, assumes the caller does so */
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeBitsInternal(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded);
|
|
|
|
static BOOL CRYPT_AsnDecodeIntInternal(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
/* Like CRYPT_AsnDecodeInteger, but assumes the CRYPT_INTEGER_BLOB's pbData
|
|
|
|
* member has been initialized, doesn't do exception handling, and doesn't do
|
2006-09-27 06:20:35 +02:00
|
|
|
* memory allocation. Also doesn't check tag, assumes the caller has checked
|
|
|
|
* it.
|
2006-05-16 02:01:28 +02:00
|
|
|
*/
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeIntegerInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
/* Like CRYPT_AsnDecodeInteger, but unsigned. */
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeUnsignedIntegerInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded);
|
2009-10-14 21:56:38 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePKCSAttributeInternal(const BYTE *pbEncoded,
|
2008-08-27 23:15:57 +02:00
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
/* Gets the number of length bytes from the given (leading) length byte */
|
2007-09-18 00:01:29 +02:00
|
|
|
#define GET_LEN_BYTES(b) ((b) <= 0x80 ? 1 : 1 + ((b) & 0x7f))
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
/* Helper function to get the encoded length of the data starting at pbEncoded,
|
|
|
|
* where pbEncoded[0] is the tag. If the data are too short to contain a
|
|
|
|
* length or if the length is too large for cbEncoded, sets an appropriate
|
2007-09-18 00:09:16 +02:00
|
|
|
* error code and returns FALSE. If the encoded length is unknown due to
|
|
|
|
* indefinite length encoding, *len is set to CMSG_INDEFINITE_LENGTH.
|
2006-05-16 02:01:28 +02:00
|
|
|
*/
|
2007-09-18 00:09:16 +02:00
|
|
|
static BOOL CRYPT_GetLengthIndefinite(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD *len)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (cbEncoded <= 1)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (pbEncoded[1] <= 0x7f)
|
|
|
|
{
|
|
|
|
if (pbEncoded[1] + 1 > cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*len = pbEncoded[1];
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2007-07-24 00:30:20 +02:00
|
|
|
else if (pbEncoded[1] == 0x80)
|
|
|
|
{
|
2007-09-18 00:09:16 +02:00
|
|
|
*len = CMSG_INDEFINITE_LENGTH;
|
|
|
|
ret = TRUE;
|
2007-07-24 00:30:20 +02:00
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
BYTE lenLen = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
|
|
|
if (lenLen > sizeof(DWORD) + 1)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_LARGE);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (lenLen + 2 > cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD out = 0;
|
|
|
|
|
|
|
|
pbEncoded += 2;
|
|
|
|
while (--lenLen)
|
|
|
|
{
|
|
|
|
out <<= 8;
|
|
|
|
out |= *pbEncoded++;
|
|
|
|
}
|
|
|
|
if (out + lenLen + 1 > cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*len = out;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:09:16 +02:00
|
|
|
/* Like CRYPT_GetLengthIndefinite, but disallows indefinite-length encoding. */
|
|
|
|
static BOOL CRYPT_GetLen(const BYTE *pbEncoded, DWORD cbEncoded, DWORD *len)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(pbEncoded, cbEncoded, len)) &&
|
|
|
|
*len == CMSG_INDEFINITE_LENGTH)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
/* Helper function to check *pcbStructInfo, set it to the required size, and
|
|
|
|
* optionally to allocate memory. Assumes pvStructInfo is not NULL.
|
|
|
|
* If CRYPT_DECODE_ALLOC_FLAG is set in dwFlags, *pvStructInfo will be set to a
|
|
|
|
* pointer to the newly allocated memory.
|
|
|
|
*/
|
|
|
|
static BOOL CRYPT_DecodeEnsureSpace(DWORD dwFlags,
|
2010-08-15 21:15:26 +02:00
|
|
|
const CRYPT_DECODE_PARA *pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo,
|
2006-05-16 02:01:28 +02:00
|
|
|
DWORD bytesNeeded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
{
|
|
|
|
if (pDecodePara && pDecodePara->pfnAlloc)
|
|
|
|
*(BYTE **)pvStructInfo = pDecodePara->pfnAlloc(bytesNeeded);
|
|
|
|
else
|
|
|
|
*(BYTE **)pvStructInfo = LocalAlloc(0, bytesNeeded);
|
|
|
|
if (!*(BYTE **)pvStructInfo)
|
|
|
|
ret = FALSE;
|
|
|
|
else
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
}
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2008-08-18 21:11:15 +02:00
|
|
|
else
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-15 21:15:26 +02:00
|
|
|
static void CRYPT_FreeSpace(const CRYPT_DECODE_PARA *pDecodePara, LPVOID pv)
|
2007-11-08 17:55:36 +01:00
|
|
|
{
|
|
|
|
if (pDecodePara && pDecodePara->pfnFree)
|
|
|
|
pDecodePara->pfnFree(pv);
|
|
|
|
else
|
|
|
|
LocalFree(pv);
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:04:40 +02:00
|
|
|
/* Helper function to check *pcbStructInfo and set it to the required size.
|
|
|
|
* Assumes pvStructInfo is not NULL.
|
|
|
|
*/
|
|
|
|
static BOOL CRYPT_DecodeCheckSpace(DWORD *pcbStructInfo, DWORD bytesNeeded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
/* tag:
|
|
|
|
* The expected tag of the item. If tag is 0, decodeFunc is called
|
|
|
|
* regardless of the tag value seen.
|
|
|
|
* offset:
|
|
|
|
* A sequence is decoded into a struct. The offset member is the
|
|
|
|
* offset of this item within that struct.
|
|
|
|
* decodeFunc:
|
|
|
|
* The decoder function to use. If this is NULL, then the member isn't
|
|
|
|
* decoded, but minSize space is reserved for it.
|
|
|
|
* minSize:
|
|
|
|
* The minimum amount of space occupied after decoding. You must set this.
|
|
|
|
* optional:
|
|
|
|
* If true, and the tag doesn't match the expected tag for this item,
|
|
|
|
* or the decodeFunc fails with CRYPT_E_ASN1_BADTAG, then minSize space is
|
|
|
|
* filled with 0 for this member.
|
2007-07-17 16:25:56 +02:00
|
|
|
* hasPointer, pointerOffset:
|
2006-05-16 02:01:28 +02:00
|
|
|
* If the item has dynamic data, set hasPointer to TRUE, pointerOffset to
|
2007-07-17 16:25:56 +02:00
|
|
|
* the offset within the struct of the data pointer (or to the
|
2006-05-16 02:01:28 +02:00
|
|
|
* first data pointer, if more than one exist).
|
|
|
|
* size:
|
|
|
|
* Used by CRYPT_AsnDecodeSequence, not for your use.
|
|
|
|
*/
|
|
|
|
struct AsnDecodeSequenceItem
|
|
|
|
{
|
2007-09-18 02:13:12 +02:00
|
|
|
BYTE tag;
|
|
|
|
DWORD offset;
|
|
|
|
InternalDecodeFunc decodeFunc;
|
|
|
|
DWORD minSize;
|
|
|
|
BOOL optional;
|
|
|
|
BOOL hasPointer;
|
|
|
|
DWORD pointerOffset;
|
|
|
|
DWORD size;
|
2006-05-16 02:01:28 +02:00
|
|
|
};
|
|
|
|
|
2009-10-14 21:43:14 +02:00
|
|
|
#define FINALMEMBERSIZE(s, member) (sizeof(s) - offsetof(s, member))
|
2009-10-14 21:42:24 +02:00
|
|
|
#define MEMBERSIZE(s, member, nextmember) \
|
|
|
|
(offsetof(s, nextmember) - offsetof(s, member))
|
|
|
|
|
2007-07-13 19:35:58 +02:00
|
|
|
/* Decodes the items in a sequence, where the items are described in items,
|
|
|
|
* the encoded data are in pbEncoded with length cbEncoded. Decodes into
|
|
|
|
* pvStructInfo. nextData is a pointer to the memory location at which the
|
|
|
|
* first decoded item with a dynamic pointer should point.
|
|
|
|
* Upon decoding, *cbDecoded is the total number of bytes decoded.
|
2007-09-18 02:04:02 +02:00
|
|
|
* Each item decoder is never called with CRYPT_DECODE_ALLOC_FLAG set.
|
2007-07-13 19:35:58 +02:00
|
|
|
*/
|
2007-09-18 02:11:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeSequenceItems(struct AsnDecodeSequenceItem items[],
|
|
|
|
DWORD cItem, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
void *pvStructInfo, BYTE *nextData, DWORD *cbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2007-07-13 19:35:58 +02:00
|
|
|
DWORD i, decoded = 0;
|
|
|
|
const BYTE *ptr = pbEncoded;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-07-30 21:08:57 +02:00
|
|
|
TRACE("%p, %d, %p, %d, %08x, %p, %p, %p\n", items, cItem, pbEncoded,
|
|
|
|
cbEncoded, dwFlags, pvStructInfo, nextData, cbDecoded);
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
for (i = 0, ret = TRUE; ret && i < cItem; i++)
|
|
|
|
{
|
|
|
|
if (cbEncoded - (ptr - pbEncoded) != 0)
|
|
|
|
{
|
2007-09-18 02:27:12 +02:00
|
|
|
DWORD itemLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:26:51 +02:00
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(ptr,
|
2007-09-18 02:27:12 +02:00
|
|
|
cbEncoded - (ptr - pbEncoded), &itemLen)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:27:12 +02:00
|
|
|
BYTE itemLenBytes = GET_LEN_BYTES(ptr[1]);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
if (ptr[0] == items[i].tag || !items[i].tag)
|
|
|
|
{
|
2007-09-18 02:27:12 +02:00
|
|
|
DWORD itemEncodedLen;
|
|
|
|
|
|
|
|
if (itemLen == CMSG_INDEFINITE_LENGTH)
|
|
|
|
itemEncodedLen = cbEncoded - (ptr - pbEncoded);
|
|
|
|
else
|
|
|
|
itemEncodedLen = 1 + itemLenBytes + itemLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
if (nextData && pvStructInfo && items[i].hasPointer)
|
|
|
|
{
|
|
|
|
TRACE("Setting next pointer to %p\n",
|
|
|
|
nextData);
|
|
|
|
*(BYTE **)((BYTE *)pvStructInfo +
|
|
|
|
items[i].pointerOffset) = nextData;
|
|
|
|
}
|
|
|
|
if (items[i].decodeFunc)
|
|
|
|
{
|
2007-09-18 02:27:12 +02:00
|
|
|
DWORD itemDecoded;
|
2007-09-18 02:26:51 +02:00
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
if (pvStructInfo)
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("decoding item %d\n", i);
|
2006-05-16 02:01:28 +02:00
|
|
|
else
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("sizing item %d\n", i);
|
2007-09-18 02:27:12 +02:00
|
|
|
ret = items[i].decodeFunc(ptr, itemEncodedLen,
|
2007-09-18 02:13:12 +02:00
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG,
|
2006-05-16 02:01:28 +02:00
|
|
|
pvStructInfo ? (BYTE *)pvStructInfo + items[i].offset
|
2007-09-18 02:27:12 +02:00
|
|
|
: NULL, &items[i].size, &itemDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2009-10-22 22:31:00 +02:00
|
|
|
if (items[i].size < items[i].minSize)
|
|
|
|
items[i].size = items[i].minSize;
|
|
|
|
else if (items[i].size > items[i].minSize)
|
|
|
|
{
|
|
|
|
/* Account for alignment padding */
|
|
|
|
items[i].size = ALIGN_DWORD_PTR(items[i].size);
|
|
|
|
}
|
2007-07-30 21:08:57 +02:00
|
|
|
TRACE("item %d size: %d\n", i, items[i].size);
|
2007-07-30 21:08:24 +02:00
|
|
|
if (nextData && items[i].hasPointer &&
|
|
|
|
items[i].size > items[i].minSize)
|
|
|
|
nextData += items[i].size - items[i].minSize;
|
2007-09-18 02:27:12 +02:00
|
|
|
if (itemDecoded > itemEncodedLen)
|
2007-09-18 02:26:51 +02:00
|
|
|
{
|
|
|
|
WARN("decoded length %d exceeds encoded %d\n",
|
2007-09-18 02:27:12 +02:00
|
|
|
itemDecoded, itemEncodedLen);
|
2007-09-18 02:26:51 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-20 00:37:35 +01:00
|
|
|
ptr += itemDecoded;
|
|
|
|
decoded += itemDecoded;
|
|
|
|
TRACE("item %d: decoded %d bytes\n", i,
|
|
|
|
itemDecoded);
|
2007-09-18 02:26:51 +02:00
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
else if (items[i].optional &&
|
|
|
|
GetLastError() == CRYPT_E_ASN1_BADTAG)
|
|
|
|
{
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("skipping optional item %d\n", i);
|
2006-05-16 02:01:28 +02:00
|
|
|
items[i].size = items[i].minSize;
|
|
|
|
SetLastError(NOERROR);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("item %d failed: %08x\n", i,
|
2006-05-16 02:01:28 +02:00
|
|
|
GetLastError());
|
|
|
|
}
|
2007-09-18 02:27:12 +02:00
|
|
|
else if (itemLen == CMSG_INDEFINITE_LENGTH)
|
2007-09-18 02:26:51 +02:00
|
|
|
{
|
|
|
|
ERR("can't use indefinite length encoding without a decoder\n");
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
else
|
2007-07-13 19:35:58 +02:00
|
|
|
{
|
2007-09-18 02:27:12 +02:00
|
|
|
TRACE("item %d: decoded %d bytes\n", i, itemEncodedLen);
|
|
|
|
ptr += itemEncodedLen;
|
|
|
|
decoded += itemEncodedLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
items[i].size = items[i].minSize;
|
2007-07-13 19:35:58 +02:00
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
else if (items[i].optional)
|
|
|
|
{
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("skipping optional item %d\n", i);
|
2006-05-16 02:01:28 +02:00
|
|
|
items[i].size = items[i].minSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-26 03:14:25 +02:00
|
|
|
TRACE("item %d: tag %02x doesn't match expected %02x\n",
|
|
|
|
i, ptr[0], items[i].tag);
|
2006-05-16 02:01:28 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (items[i].optional)
|
|
|
|
{
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("missing optional item %d, skipping\n", i);
|
2006-05-16 02:01:28 +02:00
|
|
|
items[i].size = items[i].minSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("not enough bytes for item %d, failing\n", i);
|
2006-05-16 02:01:28 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 02:18:53 +02:00
|
|
|
if (cbDecoded)
|
2007-07-13 19:35:58 +02:00
|
|
|
*cbDecoded = decoded;
|
2007-07-30 21:08:57 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This decodes an arbitrary sequence into a contiguous block of memory
|
|
|
|
* (basically, a struct.) Each element being decoded is described by a struct
|
|
|
|
* AsnDecodeSequenceItem, see above.
|
|
|
|
* startingPointer is an optional pointer to the first place where dynamic
|
|
|
|
* data will be stored. If you know the starting offset, you may pass it
|
|
|
|
* here. Otherwise, pass NULL, and one will be inferred from the items.
|
|
|
|
*/
|
2007-09-18 02:12:27 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeSequence(struct AsnDecodeSequenceItem items[],
|
|
|
|
DWORD cItem, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded, void *startingPointer)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %p, %d, %08x, %p, %p, %d, %p\n", items, cItem, pbEncoded,
|
2006-05-16 02:01:28 +02:00
|
|
|
cbEncoded, dwFlags, pDecodePara, pvStructInfo, *pcbStructInfo,
|
|
|
|
startingPointer);
|
|
|
|
|
2008-09-08 21:30:01 +02:00
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
if (pbEncoded[0] == ASN_SEQUENCE)
|
|
|
|
{
|
|
|
|
DWORD dataLen;
|
|
|
|
|
2007-09-18 02:20:52 +02:00
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(pbEncoded, cbEncoded, &dataLen)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-07-13 19:35:58 +02:00
|
|
|
DWORD lenBytes = GET_LEN_BYTES(pbEncoded[1]), cbDecoded;
|
|
|
|
const BYTE *ptr = pbEncoded + 1 + lenBytes;
|
2007-10-02 01:04:34 +02:00
|
|
|
BOOL indefinite = FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-07-13 19:35:58 +02:00
|
|
|
cbEncoded -= 1 + lenBytes;
|
2007-09-18 02:20:52 +02:00
|
|
|
if (dataLen == CMSG_INDEFINITE_LENGTH)
|
2007-10-02 01:04:34 +02:00
|
|
|
{
|
2007-09-18 02:28:09 +02:00
|
|
|
dataLen = cbEncoded;
|
2007-10-02 01:04:34 +02:00
|
|
|
indefinite = TRUE;
|
|
|
|
}
|
2007-09-18 02:20:52 +02:00
|
|
|
else if (cbEncoded < dataLen)
|
2007-07-13 19:35:58 +02:00
|
|
|
{
|
|
|
|
TRACE("dataLen %d exceeds cbEncoded %d, failing\n", dataLen,
|
|
|
|
cbEncoded);
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2007-09-18 02:20:52 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2007-09-18 02:11:54 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequenceItems(items, cItem,
|
2007-09-18 02:28:09 +02:00
|
|
|
ptr, dataLen, dwFlags, NULL, NULL, &cbDecoded);
|
2007-09-18 02:20:52 +02:00
|
|
|
if (ret && dataLen == CMSG_INDEFINITE_LENGTH)
|
|
|
|
{
|
|
|
|
if (cbDecoded > cbEncoded - 2)
|
|
|
|
{
|
|
|
|
/* Not enough space for 0 TLV */
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (*(ptr + cbDecoded) != 0 ||
|
|
|
|
*(ptr + cbDecoded + 1) != 0)
|
|
|
|
{
|
|
|
|
TRACE("expected 0 TLV\n");
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cbDecoded += 2;
|
|
|
|
}
|
|
|
|
}
|
2007-10-02 01:04:34 +02:00
|
|
|
if (ret && !indefinite && cbDecoded != dataLen)
|
2007-07-13 19:35:58 +02:00
|
|
|
{
|
2007-09-18 02:28:09 +02:00
|
|
|
TRACE("expected %d decoded, got %d, failing\n", dataLen,
|
2007-07-13 19:35:58 +02:00
|
|
|
cbDecoded);
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2007-07-13 19:35:58 +02:00
|
|
|
DWORD i, bytesNeeded = 0, structSize = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
for (i = 0; i < cItem; i++)
|
|
|
|
{
|
2010-04-07 23:07:43 +02:00
|
|
|
if (items[i].size > items[i].minSize)
|
|
|
|
bytesNeeded += items[i].size - items[i].minSize;
|
2009-06-22 14:53:24 +02:00
|
|
|
structSize = max( structSize, items[i].offset + items[i].minSize );
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2010-04-07 23:07:43 +02:00
|
|
|
bytesNeeded += structSize;
|
2007-09-18 02:59:53 +02:00
|
|
|
if (pcbDecoded)
|
2007-09-18 01:05:06 +02:00
|
|
|
*pcbDecoded = 1 + lenBytes + cbDecoded;
|
2006-05-16 02:01:28 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
BYTE *nextData;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
if (startingPointer)
|
2009-01-19 10:23:15 +01:00
|
|
|
nextData = startingPointer;
|
2006-05-16 02:01:28 +02:00
|
|
|
else
|
|
|
|
nextData = (BYTE *)pvStructInfo + structSize;
|
|
|
|
memset(pvStructInfo, 0, structSize);
|
2007-09-18 02:11:54 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequenceItems(items, cItem,
|
2007-09-18 02:28:09 +02:00
|
|
|
ptr, dataLen, dwFlags, pvStructInfo, nextData,
|
2007-07-13 19:35:58 +02:00
|
|
|
&cbDecoded);
|
2007-11-08 17:55:36 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, pvStructInfo);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tag:
|
|
|
|
* The expected tag of the entire encoded array (usually a variant
|
2007-06-12 23:39:19 +02:00
|
|
|
* of ASN_SETOF or ASN_SEQUENCEOF.) If tag is 0, decodeFunc is called
|
|
|
|
* regardless of the tag seen.
|
2009-10-14 21:58:49 +02:00
|
|
|
* countOffset:
|
|
|
|
* The offset within the outer structure at which the count exists.
|
|
|
|
* For example, a structure such as CRYPT_ATTRIBUTES has countOffset == 0,
|
|
|
|
* while CRYPT_ATTRIBUTE has countOffset ==
|
|
|
|
* offsetof(CRYPT_ATTRIBUTE, cValue).
|
|
|
|
* arrayOffset:
|
|
|
|
* The offset within the outer structure at which the array pointer exists.
|
|
|
|
* For example, CRYPT_ATTRIBUTES has arrayOffset ==
|
|
|
|
* offsetof(CRYPT_ATTRIBUTES, rgAttr).
|
2009-10-14 22:01:00 +02:00
|
|
|
* minArraySize:
|
|
|
|
* The minimum size of the decoded array. On WIN32, this is always 8:
|
|
|
|
* sizeof(DWORD) + sizeof(void *). On WIN64, it can be larger due to
|
|
|
|
* alignment.
|
2006-05-16 02:01:28 +02:00
|
|
|
* decodeFunc:
|
|
|
|
* used to decode each item in the array
|
|
|
|
* itemSize:
|
|
|
|
* is the minimum size of each decoded item
|
|
|
|
* hasPointer:
|
|
|
|
* indicates whether each item has a dynamic pointer
|
|
|
|
* pointerOffset:
|
|
|
|
* indicates the offset within itemSize at which the pointer exists
|
|
|
|
*/
|
|
|
|
struct AsnArrayDescriptor
|
|
|
|
{
|
2007-09-18 00:02:54 +02:00
|
|
|
BYTE tag;
|
2009-10-14 21:58:49 +02:00
|
|
|
DWORD countOffset;
|
|
|
|
DWORD arrayOffset;
|
2009-10-14 22:01:00 +02:00
|
|
|
DWORD minArraySize;
|
2007-09-18 00:02:54 +02:00
|
|
|
InternalDecodeFunc decodeFunc;
|
|
|
|
DWORD itemSize;
|
|
|
|
BOOL hasPointer;
|
|
|
|
DWORD pointerOffset;
|
2006-05-16 02:01:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AsnArrayItemSize
|
|
|
|
{
|
|
|
|
DWORD encodedLen;
|
|
|
|
DWORD size;
|
|
|
|
};
|
|
|
|
|
2009-10-14 21:59:50 +02:00
|
|
|
/* Decodes an array of like types into a structure described by a struct
|
2009-10-14 22:08:24 +02:00
|
|
|
* AsnArrayDescriptor.
|
2009-10-14 21:42:24 +02:00
|
|
|
*/
|
2009-10-14 22:07:36 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeArray(const struct AsnArrayDescriptor *arrayDesc,
|
|
|
|
const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
2010-08-15 21:15:26 +02:00
|
|
|
const CRYPT_DECODE_PARA *pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo,
|
2009-10-14 22:07:36 +02:00
|
|
|
DWORD *pcbDecoded)
|
2009-10-14 21:42:24 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2009-10-14 21:59:50 +02:00
|
|
|
TRACE("%p, %p, %d, %p, %d\n", arrayDesc, pbEncoded,
|
2009-10-14 22:01:00 +02:00
|
|
|
cbEncoded, pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
2009-10-14 21:42:24 +02:00
|
|
|
|
2009-10-14 21:46:16 +02:00
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (!arrayDesc->tag || pbEncoded[0] == arrayDesc->tag)
|
2009-10-14 21:42:24 +02:00
|
|
|
{
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
2009-10-14 22:01:00 +02:00
|
|
|
DWORD bytesNeeded = arrayDesc->minArraySize, cItems = 0, decoded;
|
2009-10-14 21:42:24 +02:00
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
/* There can be arbitrarily many items, but there is often only one.
|
|
|
|
*/
|
|
|
|
struct AsnArrayItemSize itemSize = { 0 }, *itemSizes = &itemSize;
|
|
|
|
|
|
|
|
decoded = 1 + lenBytes;
|
|
|
|
if (dataLen)
|
|
|
|
{
|
|
|
|
const BYTE *ptr;
|
|
|
|
BOOL doneDecoding = FALSE;
|
|
|
|
|
|
|
|
for (ptr = pbEncoded + 1 + lenBytes; ret && !doneDecoding; )
|
|
|
|
{
|
|
|
|
if (dataLen == CMSG_INDEFINITE_LENGTH)
|
|
|
|
{
|
|
|
|
if (ptr[0] == 0)
|
|
|
|
{
|
|
|
|
doneDecoding = TRUE;
|
|
|
|
if (ptr[1] != 0)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
decoded += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ptr - pbEncoded - 1 - lenBytes >= dataLen)
|
|
|
|
doneDecoding = TRUE;
|
|
|
|
if (!doneDecoding)
|
|
|
|
{
|
|
|
|
DWORD itemEncoded, itemDataLen, itemDecoded, size = 0;
|
|
|
|
|
|
|
|
/* Each item decoded may not tolerate extraneous bytes,
|
|
|
|
* so get the length of the next element if known.
|
|
|
|
*/
|
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(ptr,
|
|
|
|
cbEncoded - (ptr - pbEncoded), &itemDataLen)))
|
|
|
|
{
|
|
|
|
if (itemDataLen == CMSG_INDEFINITE_LENGTH)
|
|
|
|
itemEncoded = cbEncoded - (ptr - pbEncoded);
|
|
|
|
else
|
|
|
|
itemEncoded = 1 + GET_LEN_BYTES(ptr[1]) +
|
|
|
|
itemDataLen;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
ret = arrayDesc->decodeFunc(ptr, itemEncoded,
|
2009-10-15 17:57:13 +02:00
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &size,
|
|
|
|
&itemDecoded);
|
2009-10-14 21:42:24 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
cItems++;
|
|
|
|
if (itemSizes != &itemSize)
|
|
|
|
itemSizes = CryptMemRealloc(itemSizes,
|
|
|
|
cItems * sizeof(struct AsnArrayItemSize));
|
|
|
|
else if (cItems > 1)
|
|
|
|
{
|
|
|
|
itemSizes =
|
|
|
|
CryptMemAlloc(
|
|
|
|
cItems * sizeof(struct AsnArrayItemSize));
|
|
|
|
if (itemSizes)
|
2013-01-24 15:18:24 +01:00
|
|
|
*itemSizes = itemSize;
|
2009-10-14 21:42:24 +02:00
|
|
|
}
|
|
|
|
if (itemSizes)
|
|
|
|
{
|
|
|
|
decoded += itemDecoded;
|
|
|
|
itemSizes[cItems - 1].encodedLen = itemEncoded;
|
|
|
|
itemSizes[cItems - 1].size = size;
|
|
|
|
bytesNeeded += size;
|
|
|
|
ptr += itemEncoded;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = decoded;
|
2009-10-14 21:59:50 +02:00
|
|
|
if (!pvStructInfo)
|
2009-10-14 22:01:00 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
2009-10-14 22:08:24 +02:00
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
2009-10-14 21:42:24 +02:00
|
|
|
{
|
2009-10-14 21:59:50 +02:00
|
|
|
DWORD i, *pcItems;
|
2009-10-14 21:42:24 +02:00
|
|
|
BYTE *nextData;
|
|
|
|
const BYTE *ptr;
|
2009-10-14 21:59:50 +02:00
|
|
|
void *rgItems;
|
2009-10-14 21:42:24 +02:00
|
|
|
|
2009-10-14 22:08:24 +02:00
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(void **)pvStructInfo;
|
2009-10-14 21:59:50 +02:00
|
|
|
pcItems = pvStructInfo;
|
2009-10-14 21:42:24 +02:00
|
|
|
*pcItems = cItems;
|
2009-10-14 22:08:24 +02:00
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
{
|
|
|
|
rgItems = (BYTE *)pvStructInfo +
|
|
|
|
arrayDesc->minArraySize;
|
|
|
|
*(void **)((BYTE *)pcItems -
|
|
|
|
arrayDesc->countOffset + arrayDesc->arrayOffset) =
|
|
|
|
rgItems;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rgItems = *(void **)((BYTE *)pcItems -
|
|
|
|
arrayDesc->countOffset + arrayDesc->arrayOffset);
|
2009-10-14 21:42:24 +02:00
|
|
|
nextData = (BYTE *)rgItems + cItems * arrayDesc->itemSize;
|
|
|
|
for (i = 0, ptr = pbEncoded + 1 + lenBytes; ret &&
|
|
|
|
i < cItems && ptr - pbEncoded - 1 - lenBytes <
|
|
|
|
dataLen; i++)
|
|
|
|
{
|
|
|
|
DWORD itemDecoded;
|
|
|
|
|
|
|
|
if (arrayDesc->hasPointer)
|
|
|
|
*(BYTE **)((BYTE *)rgItems + i * arrayDesc->itemSize
|
|
|
|
+ arrayDesc->pointerOffset) = nextData;
|
|
|
|
ret = arrayDesc->decodeFunc(ptr,
|
2009-10-15 17:57:13 +02:00
|
|
|
itemSizes[i].encodedLen,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG,
|
2009-10-14 21:42:24 +02:00
|
|
|
(BYTE *)rgItems + i * arrayDesc->itemSize,
|
|
|
|
&itemSizes[i].size, &itemDecoded);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
nextData += itemSizes[i].size - arrayDesc->itemSize;
|
|
|
|
ptr += itemDecoded;
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, pvStructInfo);
|
2009-10-14 21:42:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (itemSizes != &itemSize)
|
|
|
|
CryptMemFree(itemSizes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
/* Decodes a DER-encoded BLOB into a CRYPT_DER_BLOB struct pointed to by
|
|
|
|
* pvStructInfo. The BLOB must be non-empty, otherwise the last error is set
|
|
|
|
* to CRYPT_E_ASN1_CORRUPT.
|
|
|
|
* Warning: assumes the CRYPT_DER_BLOB pointed to by pvStructInfo has pbData
|
|
|
|
* set!
|
|
|
|
*/
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeDerBlob(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
DWORD bytesNeeded = sizeof(CRYPT_DER_BLOB);
|
|
|
|
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += 1 + lenBytes + dataLen;
|
|
|
|
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
2007-09-18 02:04:40 +02:00
|
|
|
else if ((ret = CRYPT_DecodeCheckSpace(pcbStructInfo, bytesNeeded)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
CRYPT_DER_BLOB *blob;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
blob->cbData = 1 + lenBytes + dataLen;
|
|
|
|
if (blob->cbData)
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
|
|
|
|
blob->pbData = (BYTE *)pbEncoded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(blob->pbData);
|
|
|
|
memcpy(blob->pbData, pbEncoded, blob->cbData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like CRYPT_AsnDecodeBitsInternal, but swaps the bytes */
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeBitsSwapBytes(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("(%p, %d, 0x%08x, %p, %d, %p)\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
/* Can't use the CRYPT_DECODE_NOCOPY_FLAG, because we modify the bytes in-
|
|
|
|
* place.
|
|
|
|
*/
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeBitsInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_NOCOPY_FLAG, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CRYPT_BIT_BLOB *blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
if (blob->cbData)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
BYTE temp;
|
|
|
|
|
|
|
|
for (i = 0; i < blob->cbData / 2; i++)
|
|
|
|
{
|
|
|
|
temp = blob->pbData[i];
|
|
|
|
blob->pbData[i] = blob->pbData[blob->cbData - i - 1];
|
|
|
|
blob->pbData[blob->cbData - i - 1] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-06-19 21:17:04 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCertSignedContent(DWORD dwCertEncodingType,
|
2006-05-16 02:01:28 +02:00
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ 0, offsetof(CERT_SIGNED_CONTENT_INFO, ToBeSigned),
|
|
|
|
CRYPT_AsnDecodeDerBlob, sizeof(CRYPT_DER_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CERT_SIGNED_CONTENT_INFO, ToBeSigned.pbData), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_SIGNED_CONTENT_INFO,
|
|
|
|
SignatureAlgorithm), CRYPT_AsnDecodeAlgorithmId,
|
|
|
|
sizeof(CRYPT_ALGORITHM_IDENTIFIER), FALSE, TRUE,
|
|
|
|
offsetof(CERT_SIGNED_CONTENT_INFO, SignatureAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_BITSTRING, offsetof(CERT_SIGNED_CONTENT_INFO, Signature),
|
|
|
|
CRYPT_AsnDecodeBitsSwapBytes, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CERT_SIGNED_CONTENT_INFO, Signature.pbData), 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG)
|
|
|
|
items[2].decodeFunc = CRYPT_AsnDecodeBitsInternal;
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2006-06-19 21:17:04 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCertVersion(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeIntInternal(pbEncoded + 1 + lenBytes, dataLen,
|
2007-09-18 02:18:53 +02:00
|
|
|
dwFlags, pvStructInfo, pcbStructInfo, NULL);
|
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeValidity(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ 0, offsetof(CERT_PRIVATE_KEY_VALIDITY, NotBefore),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeChoiceOfTimeInternal, sizeof(FILETIME), FALSE, FALSE, 0 },
|
2006-05-16 02:01:28 +02:00
|
|
|
{ 0, offsetof(CERT_PRIVATE_KEY_VALIDITY, NotAfter),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeChoiceOfTimeInternal, sizeof(FILETIME), FALSE, FALSE, 0 },
|
2006-05-16 02:01:28 +02:00
|
|
|
};
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
2007-09-18 02:13:12 +02:00
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:43:14 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCertExtensionsInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_INFO, cExtension), offsetof(CERT_INFO, rgExtension),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CERT_INFO, cExtension),
|
2009-10-14 21:43:14 +02:00
|
|
|
CRYPT_AsnDecodeExtension, sizeof(CERT_EXTENSION), TRUE,
|
|
|
|
offsetof(CERT_EXTENSION, pszObjId) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:43:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCertExtensions(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
2009-10-14 21:43:14 +02:00
|
|
|
ret = CRYPT_AsnDecodeCertExtensionsInternal(pbEncoded + 1 + lenBytes,
|
2007-09-18 02:18:53 +02:00
|
|
|
dataLen, dwFlags, pvStructInfo, pcbStructInfo, NULL);
|
|
|
|
if (ret && pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnDecodeCertInfo(DWORD dwCertEncodingType,
|
2006-05-16 02:01:28 +02:00
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
2006-06-19 21:17:04 +02:00
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR, offsetof(CERT_INFO, dwVersion),
|
|
|
|
CRYPT_AsnDecodeCertVersion, sizeof(DWORD), TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_INTEGER, offsetof(CERT_INFO, SerialNumber),
|
|
|
|
CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE,
|
|
|
|
TRUE, offsetof(CERT_INFO, SerialNumber.pbData), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_INFO, SignatureAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CERT_INFO, SignatureAlgorithm.pszObjId), 0 },
|
|
|
|
{ 0, offsetof(CERT_INFO, Issuer), CRYPT_AsnDecodeDerBlob,
|
|
|
|
sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_INFO,
|
|
|
|
Issuer.pbData) },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_INFO, NotBefore),
|
|
|
|
CRYPT_AsnDecodeValidity, sizeof(CERT_PRIVATE_KEY_VALIDITY), FALSE,
|
|
|
|
FALSE, 0 },
|
|
|
|
{ 0, offsetof(CERT_INFO, Subject), CRYPT_AsnDecodeDerBlob,
|
|
|
|
sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_INFO,
|
|
|
|
Subject.pbData) },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_INFO, SubjectPublicKeyInfo),
|
|
|
|
CRYPT_AsnDecodePubKeyInfoInternal, sizeof(CERT_PUBLIC_KEY_INFO),
|
|
|
|
FALSE, TRUE, offsetof(CERT_INFO,
|
|
|
|
SubjectPublicKeyInfo.Algorithm.Parameters.pbData), 0 },
|
2009-11-06 20:23:17 +01:00
|
|
|
{ ASN_CONTEXT | 1, offsetof(CERT_INFO, IssuerUniqueId),
|
2006-06-19 21:17:04 +02:00
|
|
|
CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CERT_INFO, IssuerUniqueId.pbData), 0 },
|
2009-11-06 20:23:17 +01:00
|
|
|
{ ASN_CONTEXT | 2, offsetof(CERT_INFO, SubjectUniqueId),
|
2006-06-19 21:17:04 +02:00
|
|
|
CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CERT_INFO, SubjectUniqueId.pbData), 0 },
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 3, offsetof(CERT_INFO, cExtension),
|
2009-10-14 21:43:14 +02:00
|
|
|
CRYPT_AsnDecodeCertExtensions, FINALMEMBERSIZE(CERT_INFO, cExtension),
|
|
|
|
TRUE, TRUE, offsetof(CERT_INFO, rgExtension), 0 },
|
2006-06-19 21:17:04 +02:00
|
|
|
};
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-06-19 21:17:04 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
|
|
|
|
NULL, NULL);
|
2007-07-13 19:35:58 +02:00
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
|
|
|
CERT_INFO *info;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
info = *(CERT_INFO **)pvStructInfo;
|
|
|
|
else
|
2009-01-19 10:23:15 +01:00
|
|
|
info = pvStructInfo;
|
2007-07-13 19:35:58 +02:00
|
|
|
if (!info->SerialNumber.cbData || !info->Issuer.cbData ||
|
|
|
|
!info->Subject.cbData)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
/* Don't need to deallocate, because it should have failed on the
|
|
|
|
* first pass (and no memory was allocated.)
|
|
|
|
*/
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2006-06-19 21:17:04 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
2006-06-19 21:17:04 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCert(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
2007-09-11 01:02:52 +02:00
|
|
|
BOOL ret = FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2006-06-19 21:17:04 +02:00
|
|
|
DWORD size = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-11 01:02:52 +02:00
|
|
|
/* Unless told not to, first try to decode it as a signed cert. */
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_TO_BE_SIGNED_FLAG))
|
2006-06-19 21:17:04 +02:00
|
|
|
{
|
2007-09-11 01:02:52 +02:00
|
|
|
PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeCertSignedContent(dwCertEncodingType,
|
|
|
|
X509_CERT, pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
|
2009-01-19 10:23:15 +01:00
|
|
|
&signedCert, &size);
|
2007-09-11 01:02:52 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
ret = CRYPT_AsnDecodeCertInfo(dwCertEncodingType,
|
|
|
|
X509_CERT_TO_BE_SIGNED, signedCert->ToBeSigned.pbData,
|
|
|
|
signedCert->ToBeSigned.cbData, dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo);
|
|
|
|
LocalFree(signedCert);
|
|
|
|
}
|
2006-06-19 21:17:04 +02:00
|
|
|
}
|
|
|
|
/* Failing that, try it as an unsigned cert */
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
ret = CRYPT_AsnDecodeCertInfo(dwCertEncodingType,
|
|
|
|
X509_CERT_TO_BE_SIGNED, pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, pcbStructInfo);
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2006-06-19 21:17:04 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:43:58 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCRLEntryExtensions(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRL_ENTRY, cExtension), offsetof(CRL_ENTRY, rgExtension),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CRL_ENTRY, cExtension),
|
2009-10-14 21:43:58 +02:00
|
|
|
CRYPT_AsnDecodeExtension, sizeof(CERT_EXTENSION), TRUE,
|
|
|
|
offsetof(CERT_EXTENSION, pszObjId) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:43:58 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCRLEntry(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(CRL_ENTRY, SerialNumber),
|
|
|
|
CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CRL_ENTRY, SerialNumber.pbData), 0 },
|
2007-09-18 02:13:12 +02:00
|
|
|
{ 0, offsetof(CRL_ENTRY, RevocationDate),
|
|
|
|
CRYPT_AsnDecodeChoiceOfTimeInternal, sizeof(FILETIME), FALSE, FALSE, 0 },
|
2006-05-16 02:01:28 +02:00
|
|
|
{ ASN_SEQUENCEOF, offsetof(CRL_ENTRY, cExtension),
|
2009-10-14 21:43:58 +02:00
|
|
|
CRYPT_AsnDecodeCRLEntryExtensions,
|
|
|
|
FINALMEMBERSIZE(CRL_ENTRY, cExtension), TRUE, TRUE,
|
2006-05-16 02:01:28 +02:00
|
|
|
offsetof(CRL_ENTRY, rgExtension), 0 },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
PCRL_ENTRY entry = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags, entry,
|
2006-05-16 02:01:28 +02:00
|
|
|
*pcbStructInfo);
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, entry, pcbStructInfo, pcbDecoded,
|
2007-09-18 00:03:28 +02:00
|
|
|
entry ? entry->SerialNumber.pbData : NULL);
|
2007-11-07 23:00:10 +01:00
|
|
|
if (ret && entry && !entry->SerialNumber.cbData)
|
|
|
|
{
|
|
|
|
WARN("empty CRL entry serial number\n");
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:42:24 +02:00
|
|
|
/* Warning: assumes pvStructInfo points to the cCRLEntry member of a CRL_INFO
|
|
|
|
* whose rgCRLEntry member has been set prior to calling.
|
2006-05-16 02:01:28 +02:00
|
|
|
*/
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCRLEntries(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRL_INFO, cCRLEntry), offsetof(CRL_INFO, rgCRLEntry),
|
2009-10-14 22:01:00 +02:00
|
|
|
MEMBERSIZE(CRL_INFO, cCRLEntry, cExtension),
|
2006-05-16 02:01:28 +02:00
|
|
|
CRYPT_AsnDecodeCRLEntry, sizeof(CRL_ENTRY), TRUE,
|
|
|
|
offsetof(CRL_ENTRY, SerialNumber.pbData) };
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:44:52 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCRLExtensionsInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRL_INFO, cExtension), offsetof(CRL_INFO, rgExtension),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CRL_INFO, cExtension),
|
2009-10-14 21:44:52 +02:00
|
|
|
CRYPT_AsnDecodeExtension, sizeof(CERT_EXTENSION), TRUE,
|
|
|
|
offsetof(CERT_EXTENSION, pszObjId) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:44:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeCRLExtensions(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeCRLExtensionsInternal(pbEncoded + 1 + lenBytes,
|
|
|
|
dataLen, dwFlags, pvStructInfo, pcbStructInfo, NULL);
|
|
|
|
if (ret && pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnDecodeCRLInfo(DWORD dwCertEncodingType,
|
2006-05-16 02:01:28 +02:00
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
2006-06-19 21:17:04 +02:00
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(CRL_INFO, dwVersion),
|
2007-09-18 02:07:35 +02:00
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), TRUE, FALSE, 0, 0 },
|
2006-06-19 21:17:04 +02:00
|
|
|
{ ASN_SEQUENCEOF, offsetof(CRL_INFO, SignatureAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CRL_INFO, SignatureAlgorithm.pszObjId), 0 },
|
|
|
|
{ 0, offsetof(CRL_INFO, Issuer), CRYPT_AsnDecodeDerBlob,
|
|
|
|
sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CRL_INFO,
|
|
|
|
Issuer.pbData) },
|
2007-09-18 02:13:12 +02:00
|
|
|
{ 0, offsetof(CRL_INFO, ThisUpdate), CRYPT_AsnDecodeChoiceOfTimeInternal,
|
2006-06-19 21:17:04 +02:00
|
|
|
sizeof(FILETIME), FALSE, FALSE, 0 },
|
2007-09-18 02:13:12 +02:00
|
|
|
{ 0, offsetof(CRL_INFO, NextUpdate), CRYPT_AsnDecodeChoiceOfTimeInternal,
|
2006-06-19 21:17:04 +02:00
|
|
|
sizeof(FILETIME), TRUE, FALSE, 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CRL_INFO, cCRLEntry),
|
2009-10-14 21:42:24 +02:00
|
|
|
CRYPT_AsnDecodeCRLEntries, MEMBERSIZE(CRL_INFO, cCRLEntry, cExtension),
|
|
|
|
TRUE, TRUE, offsetof(CRL_INFO, rgCRLEntry), 0 },
|
2006-06-19 21:17:04 +02:00
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_INFO, cExtension),
|
2009-10-14 21:44:52 +02:00
|
|
|
CRYPT_AsnDecodeCRLExtensions, FINALMEMBERSIZE(CRL_INFO, cExtension),
|
|
|
|
TRUE, TRUE, offsetof(CRL_INFO, rgExtension), 0 },
|
2006-06-19 21:17:04 +02:00
|
|
|
};
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-06-19 21:17:04 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
|
|
|
|
NULL, NULL);
|
2006-06-19 21:17:04 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
2006-06-19 21:17:04 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCRL(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-11 01:02:52 +02:00
|
|
|
BOOL ret = FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2006-06-19 21:17:04 +02:00
|
|
|
DWORD size = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-11 01:02:52 +02:00
|
|
|
/* Unless told not to, first try to decode it as a signed crl. */
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_TO_BE_SIGNED_FLAG))
|
2006-06-19 21:17:04 +02:00
|
|
|
{
|
2007-09-11 01:02:52 +02:00
|
|
|
PCERT_SIGNED_CONTENT_INFO signedCrl = NULL;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeCertSignedContent(dwCertEncodingType,
|
|
|
|
X509_CERT, pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
|
2009-01-19 10:23:15 +01:00
|
|
|
&signedCrl, &size);
|
2007-09-11 01:02:52 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
ret = CRYPT_AsnDecodeCRLInfo(dwCertEncodingType,
|
|
|
|
X509_CERT_CRL_TO_BE_SIGNED, signedCrl->ToBeSigned.pbData,
|
|
|
|
signedCrl->ToBeSigned.cbData, dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo);
|
|
|
|
LocalFree(signedCrl);
|
|
|
|
}
|
2006-06-19 21:17:04 +02:00
|
|
|
}
|
|
|
|
/* Failing that, try it as an unsigned crl */
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
ret = CRYPT_AsnDecodeCRLInfo(dwCertEncodingType,
|
|
|
|
X509_CERT_CRL_TO_BE_SIGNED, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:04:04 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeOidIgnoreTag(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
2007-08-02 18:48:53 +02:00
|
|
|
DWORD dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 00:04:04 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-08-02 18:48:53 +02:00
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-08-02 18:48:53 +02:00
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
DWORD bytesNeeded = sizeof(LPSTR);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-08-02 18:48:53 +02:00
|
|
|
if (dataLen)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-08-02 18:48:53 +02:00
|
|
|
const BYTE *ptr;
|
2012-01-26 00:21:19 +01:00
|
|
|
char str[32];
|
2007-08-02 18:48:53 +02:00
|
|
|
|
2012-01-26 00:21:19 +01:00
|
|
|
snprintf(str, sizeof(str), "%d.%d",
|
2007-08-02 18:48:53 +02:00
|
|
|
pbEncoded[1 + lenBytes] / 40,
|
|
|
|
pbEncoded[1 + lenBytes] - (pbEncoded[1 + lenBytes] / 40)
|
|
|
|
* 40);
|
2012-01-26 00:21:19 +01:00
|
|
|
bytesNeeded += strlen(str) + 1;
|
2007-08-02 18:48:53 +02:00
|
|
|
for (ptr = pbEncoded + 2 + lenBytes; ret &&
|
|
|
|
ptr - pbEncoded - 1 - lenBytes < dataLen; )
|
|
|
|
{
|
|
|
|
int val = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-08-02 18:48:53 +02:00
|
|
|
while (ptr - pbEncoded - 1 - lenBytes < dataLen &&
|
|
|
|
(*ptr & 0x80))
|
|
|
|
{
|
|
|
|
val <<= 7;
|
|
|
|
val |= *ptr & 0x7f;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
if (ptr - pbEncoded - 1 - lenBytes >= dataLen ||
|
|
|
|
(*ptr & 0x80))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val <<= 7;
|
|
|
|
val |= *ptr++;
|
|
|
|
snprintf(str, sizeof(str), ".%d", val);
|
|
|
|
bytesNeeded += strlen(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 01:05:06 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2007-08-02 18:48:53 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-16 02:01:28 +02:00
|
|
|
if (dataLen)
|
|
|
|
{
|
|
|
|
const BYTE *ptr;
|
2007-08-02 18:48:53 +02:00
|
|
|
LPSTR pszObjId = *(LPSTR *)pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-08-02 18:48:53 +02:00
|
|
|
*pszObjId = 0;
|
|
|
|
sprintf(pszObjId, "%d.%d", pbEncoded[1 + lenBytes] / 40,
|
|
|
|
pbEncoded[1 + lenBytes] - (pbEncoded[1 + lenBytes] /
|
|
|
|
40) * 40);
|
|
|
|
pszObjId += strlen(pszObjId);
|
2006-05-16 02:01:28 +02:00
|
|
|
for (ptr = pbEncoded + 2 + lenBytes; ret &&
|
|
|
|
ptr - pbEncoded - 1 - lenBytes < dataLen; )
|
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
while (ptr - pbEncoded - 1 - lenBytes < dataLen &&
|
|
|
|
(*ptr & 0x80))
|
|
|
|
{
|
|
|
|
val <<= 7;
|
|
|
|
val |= *ptr & 0x7f;
|
|
|
|
ptr++;
|
|
|
|
}
|
2007-08-02 18:48:53 +02:00
|
|
|
val <<= 7;
|
|
|
|
val |= *ptr++;
|
|
|
|
sprintf(pszObjId, ".%d", val);
|
|
|
|
pszObjId += strlen(pszObjId);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-08-02 18:48:53 +02:00
|
|
|
*(LPSTR *)pvStructInfo = NULL;
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeOidInternal(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2007-08-02 18:48:53 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
2007-08-02 18:48:53 +02:00
|
|
|
|
|
|
|
if (pbEncoded[0] == ASN_OBJECTIDENTIFIER)
|
2007-09-18 00:04:04 +02:00
|
|
|
ret = CRYPT_AsnDecodeOidIgnoreTag(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pcbStructInfo, pcbDecoded);
|
2007-08-02 18:48:53 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeExtension(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CERT_EXTENSION, pszObjId),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
|
2006-05-16 02:01:28 +02:00
|
|
|
offsetof(CERT_EXTENSION, pszObjId), 0 },
|
|
|
|
{ ASN_BOOL, offsetof(CERT_EXTENSION, fCritical), CRYPT_AsnDecodeBool,
|
|
|
|
sizeof(BOOL), TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_OCTETSTRING, offsetof(CERT_EXTENSION, Value),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_OBJID_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CERT_EXTENSION, Value.pbData) },
|
|
|
|
};
|
|
|
|
BOOL ret = TRUE;
|
2009-01-19 10:23:15 +01:00
|
|
|
PCERT_EXTENSION ext = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags, ext,
|
2006-05-16 02:01:28 +02:00
|
|
|
*pcbStructInfo);
|
|
|
|
|
|
|
|
if (ext)
|
|
|
|
TRACE("ext->pszObjId is %p\n", ext->pszObjId);
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, ext, pcbStructInfo,
|
|
|
|
pcbDecoded, ext ? ext->pszObjId : NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ext)
|
|
|
|
TRACE("ext->pszObjId is %p (%s)\n", ext->pszObjId,
|
|
|
|
debugstr_a(ext->pszObjId));
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeExtensions(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2009-10-14 21:45:39 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
__TRY
|
|
|
|
{
|
2009-10-14 21:45:39 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_EXTENSIONS, cExtension),
|
|
|
|
offsetof(CERT_EXTENSIONS, rgExtension),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_EXTENSIONS),
|
2009-10-14 21:45:39 +02:00
|
|
|
CRYPT_AsnDecodeExtension, sizeof(CERT_EXTENSION), TRUE,
|
|
|
|
offsetof(CERT_EXTENSION, pszObjId) };
|
2011-11-21 16:52:31 +01:00
|
|
|
CERT_EXTENSIONS *exts = pvStructInfo;
|
2009-10-14 21:45:39 +02:00
|
|
|
|
2011-11-21 16:52:31 +01:00
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
exts->rgExtension = (CERT_EXTENSION *)(exts + 1);
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
2009-10-14 22:09:10 +02:00
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Warning: this assumes the address of value->Value.pbData is already set, in
|
|
|
|
* order to avoid overwriting memory. (In some cases, it may change it, if it
|
|
|
|
* doesn't copy anything to memory.) Be sure to set it correctly!
|
|
|
|
*/
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeNameValueInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
2006-06-02 06:23:26 +02:00
|
|
|
DWORD dataLen;
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_NAME_VALUE *value = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-06-02 06:23:26 +02:00
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-06-02 06:23:26 +02:00
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
2006-07-20 01:46:37 +02:00
|
|
|
DWORD bytesNeeded = sizeof(CERT_NAME_VALUE), valueType;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-06-02 06:23:26 +02:00
|
|
|
switch (pbEncoded[0])
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-07-20 01:46:37 +02:00
|
|
|
case ASN_OCTETSTRING:
|
|
|
|
valueType = CERT_RDN_OCTET_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
2006-06-02 06:23:26 +02:00
|
|
|
case ASN_NUMERICSTRING:
|
2006-07-20 01:46:37 +02:00
|
|
|
valueType = CERT_RDN_NUMERIC_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
2006-06-02 06:23:26 +02:00
|
|
|
case ASN_PRINTABLESTRING:
|
2006-07-20 01:46:37 +02:00
|
|
|
valueType = CERT_RDN_PRINTABLE_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
2006-06-02 06:23:26 +02:00
|
|
|
case ASN_IA5STRING:
|
2006-07-20 01:46:37 +02:00
|
|
|
valueType = CERT_RDN_IA5_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
2006-06-02 22:20:13 +02:00
|
|
|
case ASN_T61STRING:
|
2006-07-20 01:46:37 +02:00
|
|
|
valueType = CERT_RDN_T61_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
|
|
|
case ASN_VIDEOTEXSTRING:
|
|
|
|
valueType = CERT_RDN_VIDEOTEX_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
|
|
|
case ASN_GRAPHICSTRING:
|
|
|
|
valueType = CERT_RDN_GRAPHIC_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
|
|
|
case ASN_VISIBLESTRING:
|
|
|
|
valueType = CERT_RDN_VISIBLE_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
|
|
|
case ASN_GENERALSTRING:
|
|
|
|
valueType = CERT_RDN_GENERAL_STRING;
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
|
|
|
case ASN_UNIVERSALSTRING:
|
|
|
|
FIXME("ASN_UNIVERSALSTRING: unimplemented\n");
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
2006-08-30 23:08:32 +02:00
|
|
|
return FALSE;
|
2006-07-20 01:46:37 +02:00
|
|
|
case ASN_BMPSTRING:
|
|
|
|
valueType = CERT_RDN_BMP_STRING;
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
|
|
|
case ASN_UTF8STRING:
|
|
|
|
valueType = CERT_RDN_UTF8_STRING;
|
|
|
|
bytesNeeded += MultiByteToWideChar(CP_UTF8, 0,
|
2006-10-24 22:54:07 +02:00
|
|
|
(LPCSTR)pbEncoded + 1 + lenBytes, dataLen, NULL, 0) * 2;
|
2006-06-02 06:23:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
2006-07-20 01:46:37 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
2006-08-30 23:08:32 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-08-30 23:08:32 +02:00
|
|
|
if (!value)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
2006-06-02 06:23:26 +02:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-08-30 23:08:32 +02:00
|
|
|
else
|
2006-06-02 06:23:26 +02:00
|
|
|
{
|
2006-08-30 23:08:32 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
value->dwValueType = valueType;
|
|
|
|
if (dataLen)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-08-30 23:08:32 +02:00
|
|
|
DWORD i;
|
2006-07-20 01:46:37 +02:00
|
|
|
|
2006-08-30 23:08:32 +02:00
|
|
|
assert(value->Value.pbData);
|
|
|
|
switch (pbEncoded[0])
|
|
|
|
{
|
|
|
|
case ASN_OCTETSTRING:
|
|
|
|
case ASN_NUMERICSTRING:
|
|
|
|
case ASN_PRINTABLESTRING:
|
|
|
|
case ASN_IA5STRING:
|
|
|
|
case ASN_T61STRING:
|
|
|
|
case ASN_VIDEOTEXSTRING:
|
|
|
|
case ASN_GRAPHICSTRING:
|
|
|
|
case ASN_VISIBLESTRING:
|
|
|
|
case ASN_GENERALSTRING:
|
|
|
|
value->Value.cbData = dataLen;
|
|
|
|
if (dataLen)
|
2006-07-20 01:46:37 +02:00
|
|
|
{
|
2006-08-30 23:08:32 +02:00
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
memcpy(value->Value.pbData,
|
|
|
|
pbEncoded + 1 + lenBytes, dataLen);
|
|
|
|
else
|
|
|
|
value->Value.pbData = (LPBYTE)pbEncoded + 1 +
|
|
|
|
lenBytes;
|
2006-07-20 01:46:37 +02:00
|
|
|
}
|
2006-08-30 23:08:32 +02:00
|
|
|
break;
|
|
|
|
case ASN_BMPSTRING:
|
|
|
|
{
|
|
|
|
LPWSTR str = (LPWSTR)value->Value.pbData;
|
2006-07-20 01:46:37 +02:00
|
|
|
|
2006-08-30 23:08:32 +02:00
|
|
|
value->Value.cbData = dataLen;
|
|
|
|
for (i = 0; i < dataLen / 2; i++)
|
|
|
|
str[i] = (pbEncoded[1 + lenBytes + 2 * i] << 8) |
|
|
|
|
pbEncoded[1 + lenBytes + 2 * i + 1];
|
|
|
|
break;
|
2006-06-02 06:23:26 +02:00
|
|
|
}
|
2006-08-30 23:08:32 +02:00
|
|
|
case ASN_UTF8STRING:
|
2006-06-02 06:23:26 +02:00
|
|
|
{
|
2006-08-30 23:08:32 +02:00
|
|
|
LPWSTR str = (LPWSTR)value->Value.pbData;
|
|
|
|
|
|
|
|
value->Value.cbData = MultiByteToWideChar(CP_UTF8, 0,
|
2006-10-24 22:54:07 +02:00
|
|
|
(LPCSTR)pbEncoded + 1 + lenBytes, dataLen,
|
2006-08-30 23:08:32 +02:00
|
|
|
str, bytesNeeded - sizeof(CERT_NAME_VALUE)) * 2;
|
|
|
|
break;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2006-08-30 23:08:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value->Value.cbData = 0;
|
|
|
|
value->Value.pbData = NULL;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-06-02 22:20:13 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeNameValue(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeNameValueInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pcbStructInfo, NULL);
|
2006-06-02 22:20:13 +02:00
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
|
|
|
ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, *pcbStructInfo);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CERT_NAME_VALUE *value;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
value = pvStructInfo;
|
2006-06-02 22:20:13 +02:00
|
|
|
value->Value.pbData = ((BYTE *)value + sizeof(CERT_NAME_VALUE));
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeNameValueInternal( pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, value);
|
2006-06-02 22:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeUnicodeNameValueInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-07-19 16:44:48 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
DWORD dataLen;
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_NAME_VALUE *value = pvStructInfo;
|
2006-07-19 16:44:48 +02:00
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
DWORD bytesNeeded = sizeof(CERT_NAME_VALUE), valueType;
|
|
|
|
|
|
|
|
switch (pbEncoded[0])
|
|
|
|
{
|
|
|
|
case ASN_NUMERICSTRING:
|
|
|
|
valueType = CERT_RDN_NUMERIC_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_PRINTABLESTRING:
|
|
|
|
valueType = CERT_RDN_PRINTABLE_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_IA5STRING:
|
|
|
|
valueType = CERT_RDN_IA5_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_T61STRING:
|
|
|
|
valueType = CERT_RDN_T61_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_VIDEOTEXSTRING:
|
|
|
|
valueType = CERT_RDN_VIDEOTEX_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_GRAPHICSTRING:
|
|
|
|
valueType = CERT_RDN_GRAPHIC_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_VISIBLESTRING:
|
|
|
|
valueType = CERT_RDN_VISIBLE_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_GENERALSTRING:
|
|
|
|
valueType = CERT_RDN_GENERAL_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_UNIVERSALSTRING:
|
|
|
|
valueType = CERT_RDN_UNIVERSAL_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += dataLen / 2 + sizeof(WCHAR);
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_BMPSTRING:
|
|
|
|
valueType = CERT_RDN_BMP_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += dataLen + sizeof(WCHAR);
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
case ASN_UTF8STRING:
|
|
|
|
valueType = CERT_RDN_UTF8_STRING;
|
2008-11-20 17:23:25 +01:00
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (MultiByteToWideChar(CP_UTF8, 0,
|
|
|
|
(LPCSTR)pbEncoded + 1 + lenBytes, dataLen, NULL, 0) + 1) * 2;
|
2006-07-19 16:44:48 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
2006-08-30 23:08:38 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-08-30 23:08:38 +02:00
|
|
|
if (!value)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
2006-07-19 16:44:48 +02:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-08-30 23:08:38 +02:00
|
|
|
else
|
2006-07-19 16:44:48 +02:00
|
|
|
{
|
2006-08-30 23:08:38 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
value->dwValueType = valueType;
|
|
|
|
if (dataLen)
|
2006-07-19 16:44:48 +02:00
|
|
|
{
|
2006-08-30 23:08:38 +02:00
|
|
|
DWORD i;
|
|
|
|
LPWSTR str = (LPWSTR)value->Value.pbData;
|
2006-07-19 16:44:48 +02:00
|
|
|
|
2006-08-30 23:08:38 +02:00
|
|
|
assert(value->Value.pbData);
|
|
|
|
switch (pbEncoded[0])
|
2006-07-19 16:44:48 +02:00
|
|
|
{
|
2006-08-30 23:08:38 +02:00
|
|
|
case ASN_NUMERICSTRING:
|
|
|
|
case ASN_PRINTABLESTRING:
|
|
|
|
case ASN_IA5STRING:
|
|
|
|
case ASN_T61STRING:
|
|
|
|
case ASN_VIDEOTEXSTRING:
|
|
|
|
case ASN_GRAPHICSTRING:
|
|
|
|
case ASN_VISIBLESTRING:
|
|
|
|
case ASN_GENERALSTRING:
|
|
|
|
value->Value.cbData = dataLen * 2;
|
|
|
|
for (i = 0; i < dataLen; i++)
|
|
|
|
str[i] = pbEncoded[1 + lenBytes + i];
|
2008-11-20 17:23:25 +01:00
|
|
|
str[i] = 0;
|
2006-08-30 23:08:38 +02:00
|
|
|
break;
|
|
|
|
case ASN_UNIVERSALSTRING:
|
|
|
|
value->Value.cbData = dataLen / 2;
|
|
|
|
for (i = 0; i < dataLen / 4; i++)
|
|
|
|
str[i] = (pbEncoded[1 + lenBytes + 2 * i + 2] << 8)
|
|
|
|
| pbEncoded[1 + lenBytes + 2 * i + 3];
|
2008-11-20 17:23:25 +01:00
|
|
|
str[i] = 0;
|
2006-08-30 23:08:38 +02:00
|
|
|
break;
|
|
|
|
case ASN_BMPSTRING:
|
|
|
|
value->Value.cbData = dataLen;
|
|
|
|
for (i = 0; i < dataLen / 2; i++)
|
|
|
|
str[i] = (pbEncoded[1 + lenBytes + 2 * i] << 8) |
|
|
|
|
pbEncoded[1 + lenBytes + 2 * i + 1];
|
2008-11-20 17:23:25 +01:00
|
|
|
str[i] = 0;
|
2006-08-30 23:08:38 +02:00
|
|
|
break;
|
|
|
|
case ASN_UTF8STRING:
|
|
|
|
value->Value.cbData = MultiByteToWideChar(CP_UTF8, 0,
|
2006-10-24 22:54:07 +02:00
|
|
|
(LPCSTR)pbEncoded + 1 + lenBytes, dataLen,
|
2009-02-25 00:26:44 +01:00
|
|
|
str, bytesNeeded - sizeof(CERT_NAME_VALUE)) * sizeof(WCHAR);
|
|
|
|
*(WCHAR *)(value->Value.pbData + value->Value.cbData) = 0;
|
2008-11-20 17:23:25 +01:00
|
|
|
value->Value.cbData += sizeof(WCHAR);
|
2006-08-30 23:08:38 +02:00
|
|
|
break;
|
2006-07-19 16:44:48 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-30 23:08:38 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
value->Value.cbData = 0;
|
|
|
|
value->Value.pbData = NULL;
|
|
|
|
}
|
2006-07-19 16:44:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeUnicodeNameValue(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeUnicodeNameValueInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pcbStructInfo, NULL);
|
2006-07-19 16:44:48 +02:00
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
|
|
|
ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, *pcbStructInfo);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CERT_NAME_VALUE *value;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
value = pvStructInfo;
|
2006-07-19 16:44:48 +02:00
|
|
|
value->Value.pbData = ((BYTE *)value + sizeof(CERT_NAME_VALUE));
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeUnicodeNameValueInternal(pbEncoded,
|
|
|
|
cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, value);
|
2006-07-19 16:44:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeRdnAttr(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2006-06-02 06:23:26 +02:00
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CERT_RDN_ATTR, pszObjId),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
|
2006-06-02 06:23:26 +02:00
|
|
|
offsetof(CERT_RDN_ATTR, pszObjId), 0 },
|
|
|
|
{ 0, offsetof(CERT_RDN_ATTR, dwValueType),
|
2006-06-02 22:20:13 +02:00
|
|
|
CRYPT_AsnDecodeNameValueInternal, sizeof(CERT_NAME_VALUE),
|
2006-06-02 06:23:26 +02:00
|
|
|
FALSE, TRUE, offsetof(CERT_RDN_ATTR, Value.pbData), 0 },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_RDN_ATTR *attr = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pvStructInfo, *pcbStructInfo);
|
|
|
|
|
2006-06-02 06:23:26 +02:00
|
|
|
if (attr)
|
|
|
|
TRACE("attr->pszObjId is %p\n", attr->pszObjId);
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, attr, pcbStructInfo, pcbDecoded,
|
|
|
|
attr ? attr->pszObjId : NULL);
|
2006-06-02 06:23:26 +02:00
|
|
|
if (attr)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-06-02 06:23:26 +02:00
|
|
|
TRACE("attr->pszObjId is %p (%s)\n", attr->pszObjId,
|
|
|
|
debugstr_a(attr->pszObjId));
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("attr->dwValueType is %d\n", attr->dwValueType);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeRdn(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
2006-06-02 06:23:26 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_RDN, cRDNAttr), offsetof(CERT_RDN, rgRDNAttr),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_RDN),
|
2006-06-02 06:23:26 +02:00
|
|
|
CRYPT_AsnDecodeRdnAttr, sizeof(CERT_RDN_ATTR), TRUE,
|
|
|
|
offsetof(CERT_RDN_ATTR, pszObjId) };
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
|
|
|
|
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeName(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_NAME_INFO, cRDN), offsetof(CERT_NAME_INFO, rgRDN),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_NAME_INFO),
|
2006-05-16 02:01:28 +02:00
|
|
|
CRYPT_AsnDecodeRdn, sizeof(CERT_RDN), TRUE,
|
|
|
|
offsetof(CERT_RDN, rgRDNAttr) };
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
2009-11-07 02:59:09 +01:00
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded,
|
|
|
|
NULL);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
CERT_NAME_INFO *info;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
info = pvStructInfo;
|
|
|
|
info->rgRDN = (CERT_RDN *)((BYTE *)pvStructInfo +
|
|
|
|
sizeof(CERT_NAME_INFO));
|
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, info);
|
2009-11-07 02:59:09 +01:00
|
|
|
}
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeUnicodeRdnAttr(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-07-26 16:44:47 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CERT_RDN_ATTR, pszObjId),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
|
2006-07-26 16:44:47 +02:00
|
|
|
offsetof(CERT_RDN_ATTR, pszObjId), 0 },
|
|
|
|
{ 0, offsetof(CERT_RDN_ATTR, dwValueType),
|
|
|
|
CRYPT_AsnDecodeUnicodeNameValueInternal, sizeof(CERT_NAME_VALUE),
|
|
|
|
FALSE, TRUE, offsetof(CERT_RDN_ATTR, Value.pbData), 0 },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_RDN_ATTR *attr = pvStructInfo;
|
2006-07-26 16:44:47 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-07-26 16:44:47 +02:00
|
|
|
pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
if (attr)
|
|
|
|
TRACE("attr->pszObjId is %p\n", attr->pszObjId);
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, attr, pcbStructInfo, pcbDecoded,
|
|
|
|
attr ? attr->pszObjId : NULL);
|
2006-07-26 16:44:47 +02:00
|
|
|
if (attr)
|
|
|
|
{
|
|
|
|
TRACE("attr->pszObjId is %p (%s)\n", attr->pszObjId,
|
|
|
|
debugstr_a(attr->pszObjId));
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("attr->dwValueType is %d\n", attr->dwValueType);
|
2006-07-26 16:44:47 +02:00
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-07-26 16:44:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeUnicodeRdn(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-07-26 16:44:47 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_RDN, cRDNAttr), offsetof(CERT_RDN, rgRDNAttr),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_RDN),
|
2006-07-26 16:44:47 +02:00
|
|
|
CRYPT_AsnDecodeUnicodeRdnAttr, sizeof(CERT_RDN_ATTR), TRUE,
|
|
|
|
offsetof(CERT_RDN_ATTR, pszObjId) };
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
|
|
|
|
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2006-07-26 16:44:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeUnicodeName(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_NAME_INFO, cRDN), offsetof(CERT_NAME_INFO, rgRDN),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_NAME_INFO),
|
2006-07-26 16:44:47 +02:00
|
|
|
CRYPT_AsnDecodeUnicodeRdn, sizeof(CERT_RDN), TRUE,
|
|
|
|
offsetof(CERT_RDN, rgRDNAttr) };
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2009-10-14 22:04:44 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
2009-11-07 02:57:40 +01:00
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded,
|
|
|
|
NULL);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
CERT_NAME_INFO *info;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
info = pvStructInfo;
|
|
|
|
info->rgRDN = (CERT_RDN *)((BYTE *)pvStructInfo +
|
|
|
|
sizeof(CERT_NAME_INFO));
|
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, info);
|
2009-11-07 02:57:40 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-26 16:44:47 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:55:13 +02:00
|
|
|
static BOOL CRYPT_FindEncodedLen(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE, done = FALSE;
|
|
|
|
DWORD indefiniteNestingLevels = 0, decoded = 0;
|
|
|
|
|
|
|
|
TRACE("(%p, %d)\n", pbEncoded, cbEncoded);
|
|
|
|
|
|
|
|
do {
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if (!cbEncoded)
|
|
|
|
done = TRUE;
|
|
|
|
else if ((ret = CRYPT_GetLengthIndefinite(pbEncoded, cbEncoded,
|
|
|
|
&dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
|
|
|
if (dataLen == CMSG_INDEFINITE_LENGTH)
|
|
|
|
{
|
|
|
|
indefiniteNestingLevels++;
|
|
|
|
pbEncoded += 1 + lenBytes;
|
|
|
|
cbEncoded -= 1 + lenBytes;
|
|
|
|
decoded += 1 + lenBytes;
|
|
|
|
TRACE("indefiniteNestingLevels = %d\n",
|
|
|
|
indefiniteNestingLevels);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pbEncoded[0] == 0 && pbEncoded[1] == 0 &&
|
|
|
|
indefiniteNestingLevels)
|
|
|
|
{
|
|
|
|
indefiniteNestingLevels--;
|
|
|
|
TRACE("indefiniteNestingLevels = %d\n",
|
|
|
|
indefiniteNestingLevels);
|
|
|
|
}
|
|
|
|
pbEncoded += 1 + lenBytes + dataLen;
|
|
|
|
cbEncoded -= 1 + lenBytes + dataLen;
|
|
|
|
decoded += 1 + lenBytes + dataLen;
|
|
|
|
if (!indefiniteNestingLevels)
|
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (ret && !done);
|
|
|
|
/* If we haven't found all 0 TLVs, we haven't found the end */
|
|
|
|
if (ret && indefiniteNestingLevels)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
*pcbDecoded = decoded;
|
|
|
|
TRACE("returning %d (%d)\n", ret, ret ? *pcbDecoded : 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCopyBytes(const BYTE *pbEncoded,
|
2007-09-18 00:02:54 +02:00
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
2007-09-18 02:55:13 +02:00
|
|
|
DWORD bytesNeeded = sizeof(CRYPT_OBJID_BLOB), encodedLen = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:55:13 +02:00
|
|
|
if ((ret = CRYPT_FindEncodedLen(pbEncoded, cbEncoded, &encodedLen)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:55:13 +02:00
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += encodedLen;
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
else
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
PCRYPT_OBJID_BLOB blob = pvStructInfo;
|
2007-09-18 02:55:13 +02:00
|
|
|
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
blob->cbData = encodedLen;
|
2008-08-20 21:11:41 +02:00
|
|
|
if (encodedLen)
|
2007-09-18 02:55:13 +02:00
|
|
|
{
|
2008-08-20 21:11:41 +02:00
|
|
|
if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
|
|
|
|
blob->pbData = (LPBYTE)pbEncoded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(blob->pbData);
|
|
|
|
memcpy(blob->pbData, pbEncoded, blob->cbData);
|
|
|
|
}
|
2007-09-18 02:55:13 +02:00
|
|
|
}
|
2008-08-20 21:11:41 +02:00
|
|
|
else
|
|
|
|
blob->pbData = NULL;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2007-09-18 02:55:13 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = encodedLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-27 23:15:57 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCTLUsage(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CTL_USAGE, cUsageIdentifier),
|
|
|
|
offsetof(CTL_USAGE, rgpszUsageIdentifier),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CTL_USAGE),
|
2008-08-27 23:15:57 +02:00
|
|
|
CRYPT_AsnDecodeOidInternal, sizeof(LPSTR), TRUE, 0 };
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
|
|
|
|
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2008-08-27 23:15:57 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:56:38 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCTLEntryAttributes(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CTL_ENTRY, cAttribute), offsetof(CTL_ENTRY, rgAttribute),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CTL_ENTRY, cAttribute),
|
2009-10-14 21:56:38 +02:00
|
|
|
CRYPT_AsnDecodePKCSAttributeInternal, sizeof(CRYPT_ATTRIBUTE), TRUE,
|
|
|
|
offsetof(CRYPT_ATTRIBUTE, pszObjId) };
|
|
|
|
BOOL ret;
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:56:38 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-27 23:15:57 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCTLEntry(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OCTETSTRING, offsetof(CTL_ENTRY, SubjectIdentifier),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DATA_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CTL_ENTRY, SubjectIdentifier.pbData), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_SETOF, offsetof(CTL_ENTRY, cAttribute),
|
2009-10-14 21:56:38 +02:00
|
|
|
CRYPT_AsnDecodeCTLEntryAttributes,
|
|
|
|
FINALMEMBERSIZE(CTL_ENTRY, cAttribute), FALSE, TRUE,
|
|
|
|
offsetof(CTL_ENTRY, rgAttribute), 0 },
|
2008-08-27 23:15:57 +02:00
|
|
|
};
|
|
|
|
BOOL ret = TRUE;
|
2009-01-19 10:23:15 +01:00
|
|
|
CTL_ENTRY *entry = pvStructInfo;
|
2008-08-27 23:15:57 +02:00
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags, entry,
|
|
|
|
*pcbStructInfo);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, entry, pcbStructInfo,
|
|
|
|
pcbDecoded, entry ? entry->SubjectIdentifier.pbData : NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeCTLEntries(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CTL_INFO, cCTLEntry), offsetof(CTL_INFO, rgCTLEntry),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CTL_INFO, cExtension),
|
2008-08-27 23:15:57 +02:00
|
|
|
CRYPT_AsnDecodeCTLEntry, sizeof(CTL_ENTRY), TRUE,
|
|
|
|
offsetof(CTL_ENTRY, SubjectIdentifier.pbData) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2008-08-27 23:15:57 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:52:59 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCTLExtensionsInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CTL_INFO, cExtension), offsetof(CTL_INFO, rgExtension),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CTL_INFO, cExtension),
|
2009-10-14 21:52:59 +02:00
|
|
|
CRYPT_AsnDecodeExtension, sizeof(CERT_EXTENSION), TRUE,
|
|
|
|
offsetof(CERT_EXTENSION, pszObjId) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:52:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeCTLExtensions(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeCTLExtensionsInternal(pbEncoded + 1 + lenBytes,
|
|
|
|
dataLen, dwFlags, pvStructInfo, pcbStructInfo, NULL);
|
|
|
|
if (ret && pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-27 23:15:57 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCTL(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(CTL_INFO, dwVersion),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CTL_INFO, SubjectUsage),
|
|
|
|
CRYPT_AsnDecodeCTLUsage, sizeof(CTL_USAGE), FALSE, TRUE,
|
|
|
|
offsetof(CTL_INFO, SubjectUsage.rgpszUsageIdentifier), 0 },
|
|
|
|
{ ASN_OCTETSTRING, offsetof(CTL_INFO, ListIdentifier),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DATA_BLOB), TRUE,
|
|
|
|
TRUE, offsetof(CTL_INFO, ListIdentifier.pbData), 0 },
|
|
|
|
{ ASN_INTEGER, offsetof(CTL_INFO, SequenceNumber),
|
|
|
|
CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
TRUE, TRUE, offsetof(CTL_INFO, SequenceNumber.pbData), 0 },
|
|
|
|
{ 0, offsetof(CTL_INFO, ThisUpdate),
|
|
|
|
CRYPT_AsnDecodeChoiceOfTimeInternal, sizeof(FILETIME), FALSE, FALSE,
|
|
|
|
0 },
|
|
|
|
{ 0, offsetof(CTL_INFO, NextUpdate),
|
|
|
|
CRYPT_AsnDecodeChoiceOfTimeInternal, sizeof(FILETIME), TRUE, FALSE,
|
|
|
|
0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CTL_INFO, SubjectAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CTL_INFO, SubjectAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CTL_INFO, cCTLEntry),
|
2009-10-14 21:52:14 +02:00
|
|
|
CRYPT_AsnDecodeCTLEntries,
|
|
|
|
MEMBERSIZE(CTL_INFO, cCTLEntry, cExtension),
|
2008-08-27 23:15:57 +02:00
|
|
|
TRUE, TRUE, offsetof(CTL_INFO, rgCTLEntry), 0 },
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CTL_INFO, cExtension),
|
2009-10-14 21:52:59 +02:00
|
|
|
CRYPT_AsnDecodeCTLExtensions, FINALMEMBERSIZE(CTL_INFO, cExtension),
|
|
|
|
TRUE, TRUE, offsetof(CTL_INFO, rgExtension), 0 },
|
2008-08-27 23:15:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-20 21:11:41 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeSMIMECapability(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CRYPT_SMIME_CAPABILITY, pszObjId),
|
|
|
|
CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
|
|
|
|
offsetof(CRYPT_SMIME_CAPABILITY, pszObjId), 0 },
|
|
|
|
{ 0, offsetof(CRYPT_SMIME_CAPABILITY, Parameters),
|
|
|
|
CRYPT_AsnDecodeCopyBytes, sizeof(CRYPT_OBJID_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CRYPT_SMIME_CAPABILITY, Parameters.pbData), 0 },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
PCRYPT_SMIME_CAPABILITY capability = pvStructInfo;
|
2008-08-20 21:11:41 +02:00
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, capability ? capability->pszObjId : NULL);
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeSMIMECapabilities(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-10-14 21:46:53 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRYPT_SMIME_CAPABILITIES, cCapability),
|
|
|
|
offsetof(CRYPT_SMIME_CAPABILITIES, rgCapability),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CRYPT_SMIME_CAPABILITIES),
|
2009-10-14 21:46:53 +02:00
|
|
|
CRYPT_AsnDecodeSMIMECapability, sizeof(CRYPT_SMIME_CAPABILITY), TRUE,
|
|
|
|
offsetof(CRYPT_SMIME_CAPABILITY, pszObjId) };
|
2011-11-21 16:52:31 +01:00
|
|
|
CRYPT_SMIME_CAPABILITIES *capabilities = pvStructInfo;
|
2008-08-20 21:11:41 +02:00
|
|
|
|
2011-11-21 16:52:31 +01:00
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
capabilities->rgCapability = (CRYPT_SMIME_CAPABILITY *)(capabilities + 1);
|
2009-10-14 22:09:10 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
2008-08-20 21:11:41 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-14 23:55:35 +01:00
|
|
|
static BOOL CRYPT_AsnDecodeIA5String(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
DWORD dataLen;
|
|
|
|
LPSTR *pStr = pvStructInfo;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
DWORD bytesNeeded = sizeof(LPSTR) + sizeof(char);
|
|
|
|
|
|
|
|
if (pbEncoded[0] != ASN_IA5STRING)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
if (dataLen)
|
|
|
|
{
|
|
|
|
LPSTR str = *pStr;
|
|
|
|
|
|
|
|
assert(str);
|
|
|
|
memcpy(str, pbEncoded + 1 + lenBytes, dataLen);
|
|
|
|
str[dataLen] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*pStr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:54:23 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeNoticeNumbers(const BYTE *pbEncoded,
|
2008-11-14 23:55:35 +01:00
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE, cNoticeNumbers),
|
|
|
|
offsetof(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE, rgNoticeNumbers),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE, cNoticeNumbers),
|
2008-11-14 23:55:35 +01:00
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(int), FALSE, 0 };
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("(%p, %d, %08x, %p, %d)\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbDecoded : 0);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2008-11-14 23:55:35 +01:00
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeNoticeReference(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_IA5STRING, offsetof(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE,
|
|
|
|
pszOrganization), CRYPT_AsnDecodeIA5String, sizeof(LPSTR), FALSE, TRUE,
|
|
|
|
offsetof(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE, pszOrganization), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE,
|
2009-10-14 21:54:23 +02:00
|
|
|
cNoticeNumbers), CRYPT_AsnDecodeNoticeNumbers,
|
|
|
|
FINALMEMBERSIZE(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE, cNoticeNumbers),
|
2008-11-14 23:55:35 +01:00
|
|
|
FALSE, TRUE, offsetof(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE,
|
|
|
|
rgNoticeNumbers), 0 },
|
|
|
|
};
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2008-11-14 23:55:35 +01:00
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, NULL, &bytesNeeded, pcbDecoded,
|
|
|
|
NULL);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
/* The caller is expecting a pointer to a
|
|
|
|
* CERT_POLICY_QUALIFIER_NOTICE_REFERENCE to be decoded, whereas
|
|
|
|
* CRYPT_AsnDecodeSequence is decoding a
|
|
|
|
* CERT_POLICY_QUALIFIER_NOTICE_REFERENCE. Increment the bytes
|
|
|
|
* needed, and decode again if the requisite space is available.
|
|
|
|
*/
|
|
|
|
bytesNeeded += sizeof(PCERT_POLICY_QUALIFIER_NOTICE_REFERENCE);
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PCERT_POLICY_QUALIFIER_NOTICE_REFERENCE noticeRef;
|
|
|
|
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
/* The pointer (pvStructInfo) passed in points to the first dynamic
|
|
|
|
* pointer, so use it as the pointer to the
|
|
|
|
* CERT_POLICY_QUALIFIER_NOTICE_REFERENCE, and create the
|
|
|
|
* appropriate offset for the first dynamic pointer within the
|
|
|
|
* notice reference by pointing to the first memory location past
|
|
|
|
* the CERT_POLICY_QUALIFIER_NOTICE_REFERENCE.
|
|
|
|
*/
|
|
|
|
noticeRef =
|
|
|
|
*(PCERT_POLICY_QUALIFIER_NOTICE_REFERENCE *)pvStructInfo;
|
|
|
|
noticeRef->pszOrganization = (LPSTR)((LPBYTE)noticeRef +
|
|
|
|
sizeof(CERT_POLICY_QUALIFIER_NOTICE_REFERENCE));
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items,
|
|
|
|
sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
|
|
|
|
NULL, noticeRef, &bytesNeeded, pcbDecoded,
|
|
|
|
noticeRef->pszOrganization);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-22 00:37:46 +01:00
|
|
|
static BOOL CRYPT_AsnDecodeUnicodeString(const BYTE *pbEncoded,
|
2008-11-14 23:55:35 +01:00
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
DWORD dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
2008-11-22 00:37:46 +01:00
|
|
|
DWORD bytesNeeded = sizeof(LPWSTR);
|
2008-11-14 23:55:35 +01:00
|
|
|
|
2008-11-22 00:37:46 +01:00
|
|
|
switch (pbEncoded[0])
|
2008-11-14 23:55:35 +01:00
|
|
|
{
|
2008-11-22 00:37:46 +01:00
|
|
|
case ASN_NUMERICSTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_PRINTABLESTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_IA5STRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_T61STRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_VIDEOTEXSTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_GRAPHICSTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_VISIBLESTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_GENERALSTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (dataLen + 1) * 2;
|
|
|
|
break;
|
|
|
|
case ASN_UNIVERSALSTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += dataLen / 2 + sizeof(WCHAR);
|
|
|
|
break;
|
|
|
|
case ASN_BMPSTRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += dataLen + sizeof(WCHAR);
|
|
|
|
break;
|
|
|
|
case ASN_UTF8STRING:
|
|
|
|
if (dataLen)
|
|
|
|
bytesNeeded += (MultiByteToWideChar(CP_UTF8, 0,
|
|
|
|
(LPCSTR)pbEncoded + 1 + lenBytes, dataLen, NULL, 0) + 1) * 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
2008-11-14 23:55:35 +01:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-22 00:37:46 +01:00
|
|
|
LPWSTR *pStr = pvStructInfo;
|
|
|
|
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
if (dataLen)
|
2008-11-14 23:55:35 +01:00
|
|
|
{
|
2008-11-22 00:37:46 +01:00
|
|
|
DWORD i;
|
|
|
|
LPWSTR str = *(LPWSTR *)pStr;
|
2008-11-14 23:55:35 +01:00
|
|
|
|
2008-11-22 00:37:46 +01:00
|
|
|
assert(str);
|
|
|
|
switch (pbEncoded[0])
|
|
|
|
{
|
|
|
|
case ASN_NUMERICSTRING:
|
|
|
|
case ASN_PRINTABLESTRING:
|
|
|
|
case ASN_IA5STRING:
|
|
|
|
case ASN_T61STRING:
|
|
|
|
case ASN_VIDEOTEXSTRING:
|
|
|
|
case ASN_GRAPHICSTRING:
|
|
|
|
case ASN_VISIBLESTRING:
|
|
|
|
case ASN_GENERALSTRING:
|
|
|
|
for (i = 0; i < dataLen; i++)
|
|
|
|
str[i] = pbEncoded[1 + lenBytes + i];
|
|
|
|
str[i] = 0;
|
|
|
|
break;
|
|
|
|
case ASN_UNIVERSALSTRING:
|
|
|
|
for (i = 0; i < dataLen / 4; i++)
|
|
|
|
str[i] = (pbEncoded[1 + lenBytes + 2 * i + 2] << 8)
|
|
|
|
| pbEncoded[1 + lenBytes + 2 * i + 3];
|
|
|
|
str[i] = 0;
|
|
|
|
break;
|
|
|
|
case ASN_BMPSTRING:
|
2008-11-14 23:55:35 +01:00
|
|
|
for (i = 0; i < dataLen / 2; i++)
|
|
|
|
str[i] = (pbEncoded[1 + lenBytes + 2 * i] << 8) |
|
|
|
|
pbEncoded[1 + lenBytes + 2 * i + 1];
|
|
|
|
str[i] = 0;
|
2008-11-22 00:37:46 +01:00
|
|
|
break;
|
|
|
|
case ASN_UTF8STRING:
|
|
|
|
{
|
|
|
|
int len = MultiByteToWideChar(CP_UTF8, 0,
|
|
|
|
(LPCSTR)pbEncoded + 1 + lenBytes, dataLen,
|
|
|
|
str, bytesNeeded - sizeof(CERT_NAME_VALUE)) * 2;
|
|
|
|
str[len] = 0;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-14 23:55:35 +01:00
|
|
|
}
|
|
|
|
}
|
2008-11-22 00:37:46 +01:00
|
|
|
else
|
|
|
|
*pStr = NULL;
|
2008-11-14 23:55:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodePolicyQualifierUserNoticeInternal(
|
|
|
|
const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo,
|
|
|
|
DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_SEQUENCE, offsetof(CERT_POLICY_QUALIFIER_USER_NOTICE,
|
|
|
|
pNoticeReference), CRYPT_AsnDecodeNoticeReference,
|
|
|
|
sizeof(PCERT_POLICY_QUALIFIER_NOTICE_REFERENCE), TRUE, TRUE,
|
|
|
|
offsetof(CERT_POLICY_QUALIFIER_USER_NOTICE, pNoticeReference), 0 },
|
2008-11-22 00:37:46 +01:00
|
|
|
{ 0, offsetof(CERT_POLICY_QUALIFIER_USER_NOTICE, pszDisplayText),
|
|
|
|
CRYPT_AsnDecodeUnicodeString, sizeof(LPWSTR), TRUE, TRUE,
|
2008-11-14 23:55:35 +01:00
|
|
|
offsetof(CERT_POLICY_QUALIFIER_USER_NOTICE, pszDisplayText), 0 },
|
|
|
|
};
|
|
|
|
PCERT_POLICY_QUALIFIER_USER_NOTICE notice = pvStructInfo;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, notice ? notice->pNoticeReference : NULL);
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodePolicyQualifierUserNotice(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
|
|
|
|
void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2008-11-14 23:55:35 +01:00
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodePolicyQualifierUserNoticeInternal(pbEncoded,
|
|
|
|
cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded,
|
|
|
|
NULL);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
PCERT_POLICY_QUALIFIER_USER_NOTICE notice;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
notice = pvStructInfo;
|
|
|
|
notice->pNoticeReference =
|
|
|
|
(PCERT_POLICY_QUALIFIER_NOTICE_REFERENCE)
|
|
|
|
((BYTE *)pvStructInfo +
|
|
|
|
sizeof(CERT_POLICY_QUALIFIER_USER_NOTICE));
|
|
|
|
ret = CRYPT_AsnDecodePolicyQualifierUserNoticeInternal(
|
|
|
|
pbEncoded, cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG,
|
|
|
|
pvStructInfo, &bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, notice);
|
2008-11-14 23:55:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:47:27 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePKCSAttributeValue(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-10-14 21:58:49 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
|
|
|
offsetof(CRYPT_ATTRIBUTE, cValue), offsetof(CRYPT_ATTRIBUTE, rgValue),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CRYPT_ATTRIBUTE, cValue),
|
2009-10-14 21:58:49 +02:00
|
|
|
CRYPT_AsnDecodeCopyBytes,
|
2009-10-14 21:47:27 +02:00
|
|
|
sizeof(CRYPT_DER_BLOB), TRUE, offsetof(CRYPT_DER_BLOB, pbData) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:47:27 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePKCSAttributeInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CRYPT_ATTRIBUTE, pszObjId),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
|
2007-09-18 00:02:54 +02:00
|
|
|
offsetof(CRYPT_ATTRIBUTE, pszObjId), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_SETOF, offsetof(CRYPT_ATTRIBUTE, cValue),
|
2009-10-14 21:47:27 +02:00
|
|
|
CRYPT_AsnDecodePKCSAttributeValue,
|
|
|
|
FINALMEMBERSIZE(CRYPT_ATTRIBUTE, cValue), FALSE,
|
|
|
|
TRUE, offsetof(CRYPT_ATTRIBUTE, rgValue), 0 },
|
2007-09-18 00:02:54 +02:00
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
PCRYPT_ATTRIBUTE attr = pvStructInfo;
|
2007-09-18 00:02:54 +02:00
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, attr ? attr->pszObjId : NULL);
|
2007-09-18 00:02:54 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-13 00:00:01 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodePKCSAttribute(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
2007-06-13 00:00:01 +02:00
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2007-06-13 00:00:01 +02:00
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
ret = CRYPT_AsnDecodePKCSAttributeInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
PCRYPT_ATTRIBUTE attr;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
attr = pvStructInfo;
|
2007-09-18 00:02:54 +02:00
|
|
|
attr->pszObjId = (LPSTR)((BYTE *)pvStructInfo +
|
|
|
|
sizeof(CRYPT_ATTRIBUTE));
|
|
|
|
ret = CRYPT_AsnDecodePKCSAttributeInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo, &bytesNeeded,
|
|
|
|
NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, attr);
|
2007-09-18 00:02:54 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-13 00:01:34 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePKCSAttributesInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2007-06-13 00:01:34 +02:00
|
|
|
{
|
2007-09-18 00:02:54 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRYPT_ATTRIBUTES, cAttr), offsetof(CRYPT_ATTRIBUTES, rgAttr),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CRYPT_ATTRIBUTES),
|
2007-09-18 00:02:54 +02:00
|
|
|
CRYPT_AsnDecodePKCSAttributeInternal, sizeof(CRYPT_ATTRIBUTE), TRUE,
|
|
|
|
offsetof(CRYPT_ATTRIBUTE, pszObjId) };
|
2007-06-13 00:01:34 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
|
|
|
|
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2007-06-13 00:01:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodePKCSAttributes(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-10-14 21:57:21 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRYPT_ATTRIBUTES, cAttr), offsetof(CRYPT_ATTRIBUTES, rgAttr),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CRYPT_ATTRIBUTES),
|
2009-10-14 21:58:49 +02:00
|
|
|
CRYPT_AsnDecodePKCSAttributeInternal, sizeof(CRYPT_ATTRIBUTE),
|
|
|
|
TRUE, offsetof(CRYPT_ATTRIBUTE, pszObjId) };
|
2011-11-21 16:52:31 +01:00
|
|
|
CRYPT_ATTRIBUTES *attrs = pvStructInfo;
|
2007-06-13 00:01:34 +02:00
|
|
|
|
2011-11-21 16:52:31 +01:00
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
attrs->rgAttr = (CRYPT_ATTRIBUTE *)(attrs + 1);
|
2009-10-14 22:09:10 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
2007-06-13 00:00:01 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2007-06-13 00:01:34 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
2007-06-13 00:00:01 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeAlgorithmId(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
|
2006-05-16 02:01:28 +02:00
|
|
|
offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId), 0 },
|
|
|
|
{ 0, offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters),
|
|
|
|
CRYPT_AsnDecodeCopyBytes, sizeof(CRYPT_OBJID_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters.pbData), 0 },
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
2007-09-18 02:13:12 +02:00
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, algo ? algo->pszObjId : NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
|
|
|
TRACE("pszObjId is %p (%s)\n", algo->pszObjId,
|
|
|
|
debugstr_a(algo->pszObjId));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePubKeyInfoInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_PUBLIC_KEY_INFO, Algorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CERT_PUBLIC_KEY_INFO,
|
|
|
|
Algorithm.pszObjId) },
|
|
|
|
{ ASN_BITSTRING, offsetof(CERT_PUBLIC_KEY_INFO, PublicKey),
|
|
|
|
CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CERT_PUBLIC_KEY_INFO, PublicKey.pbData) },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
PCERT_PUBLIC_KEY_INFO info = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
2007-09-18 02:13:12 +02:00
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, info ? info->Algorithm.Parameters.pbData : NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodePubKeyInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
if ((ret = CRYPT_AsnDecodePubKeyInfoInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
PCERT_PUBLIC_KEY_INFO info;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
info = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
info->Algorithm.Parameters.pbData = (BYTE *)pvStructInfo +
|
|
|
|
sizeof(CERT_PUBLIC_KEY_INFO);
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodePubKeyInfoInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, info);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeBool(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (cbEncoded < 3)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (GET_LEN_BYTES(pbEncoded[1]) > 1)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (pbEncoded[1] > 1)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 3;
|
2006-05-16 02:01:28 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = sizeof(BOOL);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else if (*pcbStructInfo < sizeof(BOOL))
|
|
|
|
{
|
|
|
|
*pcbStructInfo = sizeof(BOOL);
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-14 21:48:59 +02:00
|
|
|
*pcbStructInfo = sizeof(BOOL);
|
2012-08-06 11:19:22 +02:00
|
|
|
*(BOOL *)pvStructInfo = pbEncoded[2] != 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeAltNameEntry(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
PCERT_ALT_NAME_ENTRY entry = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
DWORD dataLen, lenBytes, bytesNeeded = sizeof(CERT_ALT_NAME_ENTRY);
|
|
|
|
BOOL ret;
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
if (cbEncoded < 2)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
if (1 + lenBytes > cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
switch (pbEncoded[0] & ASN_TYPE_MASK)
|
|
|
|
{
|
|
|
|
case 1: /* rfc822Name */
|
|
|
|
case 2: /* dNSName */
|
|
|
|
case 6: /* uniformResourceIdentifier */
|
2009-11-10 01:06:02 +01:00
|
|
|
if (memchr(pbEncoded + 1 + lenBytes, 0, dataLen))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_RULE);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bytesNeeded += (dataLen + 1) * sizeof(WCHAR);
|
2006-05-16 02:01:28 +02:00
|
|
|
break;
|
2007-08-02 18:49:40 +02:00
|
|
|
case 4: /* directoryName */
|
2006-05-16 02:01:28 +02:00
|
|
|
case 7: /* iPAddress */
|
|
|
|
bytesNeeded += dataLen;
|
|
|
|
break;
|
|
|
|
case 8: /* registeredID */
|
2007-09-18 00:04:04 +02:00
|
|
|
ret = CRYPT_AsnDecodeOidIgnoreTag(pbEncoded, cbEncoded, 0, NULL,
|
|
|
|
&dataLen, NULL);
|
2007-08-02 18:49:40 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
/* FIXME: ugly, shouldn't need to know internals of OID decode
|
|
|
|
* function to use it.
|
|
|
|
*/
|
|
|
|
bytesNeeded += dataLen - sizeof(LPSTR);
|
|
|
|
}
|
|
|
|
break;
|
2006-05-16 02:01:28 +02:00
|
|
|
case 0: /* otherName */
|
2007-08-02 18:47:19 +02:00
|
|
|
FIXME("%d: stub\n", pbEncoded[0] & ASN_TYPE_MASK);
|
2006-05-16 02:01:28 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
case 3: /* x400Address, unimplemented */
|
|
|
|
case 5: /* ediPartyName, unimplemented */
|
2007-08-02 18:47:19 +02:00
|
|
|
TRACE("type %d unimplemented\n", pbEncoded[0] & ASN_TYPE_MASK);
|
2006-05-16 02:01:28 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
2007-08-02 18:47:19 +02:00
|
|
|
TRACE("type %d bad\n", pbEncoded[0] & ASN_TYPE_MASK);
|
2006-05-16 02:01:28 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
2007-09-18 00:06:27 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
if (!entry)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
/* MS used values one greater than the asn1 ones.. sigh */
|
2008-09-09 21:26:31 +02:00
|
|
|
entry->dwAltNameChoice = (pbEncoded[0] & ASN_TYPE_MASK) + 1;
|
2006-05-16 02:01:28 +02:00
|
|
|
switch (pbEncoded[0] & ASN_TYPE_MASK)
|
|
|
|
{
|
|
|
|
case 1: /* rfc822Name */
|
|
|
|
case 2: /* dNSName */
|
|
|
|
case 6: /* uniformResourceIdentifier */
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < dataLen; i++)
|
|
|
|
entry->u.pwszURL[i] =
|
|
|
|
(WCHAR)pbEncoded[1 + lenBytes + i];
|
|
|
|
entry->u.pwszURL[i] = 0;
|
|
|
|
TRACE("URL is %p (%s)\n", entry->u.pwszURL,
|
|
|
|
debugstr_w(entry->u.pwszURL));
|
|
|
|
break;
|
|
|
|
}
|
2007-08-02 18:49:40 +02:00
|
|
|
case 4: /* directoryName */
|
|
|
|
/* The data are memory-equivalent with the IPAddress case,
|
|
|
|
* fall-through
|
|
|
|
*/
|
2006-05-16 02:01:28 +02:00
|
|
|
case 7: /* iPAddress */
|
|
|
|
/* The next data pointer is in the pwszURL spot, that is,
|
|
|
|
* the first 4 bytes. Need to move it to the next spot.
|
|
|
|
*/
|
|
|
|
entry->u.IPAddress.pbData = (LPBYTE)entry->u.pwszURL;
|
|
|
|
entry->u.IPAddress.cbData = dataLen;
|
|
|
|
memcpy(entry->u.IPAddress.pbData, pbEncoded + 1 + lenBytes,
|
|
|
|
dataLen);
|
|
|
|
break;
|
2007-08-02 18:49:40 +02:00
|
|
|
case 8: /* registeredID */
|
2007-09-18 00:04:04 +02:00
|
|
|
ret = CRYPT_AsnDecodeOidIgnoreTag(pbEncoded, cbEncoded, 0,
|
|
|
|
&entry->u.pszRegisteredID, &dataLen, NULL);
|
2007-08-02 18:49:40 +02:00
|
|
|
break;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeAltNameInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2009-10-14 22:02:53 +02:00
|
|
|
BOOL ret;
|
2007-06-12 23:39:19 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_ALT_NAME_INFO, cAltEntry),
|
|
|
|
offsetof(CERT_ALT_NAME_INFO, rgAltEntry),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_ALT_NAME_INFO),
|
2006-05-16 02:01:28 +02:00
|
|
|
CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
|
|
|
|
offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
|
|
|
|
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-09-27 06:20:35 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeAuthorityKeyId(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_CONTEXT | 0, offsetof(CERT_AUTHORITY_KEY_ID_INFO, KeyId),
|
2009-12-02 22:05:28 +01:00
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DATA_BLOB),
|
2006-09-27 06:20:35 +02:00
|
|
|
TRUE, TRUE, offsetof(CERT_AUTHORITY_KEY_ID_INFO, KeyId.pbData), 0 },
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR| 1,
|
|
|
|
offsetof(CERT_AUTHORITY_KEY_ID_INFO, CertIssuer),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CERT_NAME_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CERT_AUTHORITY_KEY_ID_INFO, CertIssuer.pbData), 0 },
|
|
|
|
{ ASN_CONTEXT | 2, offsetof(CERT_AUTHORITY_KEY_ID_INFO,
|
|
|
|
CertSerialNumber), CRYPT_AsnDecodeIntegerInternal,
|
|
|
|
sizeof(CRYPT_INTEGER_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CERT_AUTHORITY_KEY_ID_INFO, CertSerialNumber.pbData), 0 },
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
2006-09-27 06:20:35 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-12 23:39:19 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeAuthorityKeyId2(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_CONTEXT | 0, offsetof(CERT_AUTHORITY_KEY_ID2_INFO, KeyId),
|
2009-12-02 22:05:28 +01:00
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DATA_BLOB),
|
2007-06-12 23:39:19 +02:00
|
|
|
TRUE, TRUE, offsetof(CERT_AUTHORITY_KEY_ID2_INFO, KeyId.pbData), 0 },
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR| 1,
|
|
|
|
offsetof(CERT_AUTHORITY_KEY_ID2_INFO, AuthorityCertIssuer),
|
|
|
|
CRYPT_AsnDecodeAltNameInternal, sizeof(CERT_ALT_NAME_INFO), TRUE,
|
|
|
|
TRUE, offsetof(CERT_AUTHORITY_KEY_ID2_INFO,
|
|
|
|
AuthorityCertIssuer.rgAltEntry), 0 },
|
|
|
|
{ ASN_CONTEXT | 2, offsetof(CERT_AUTHORITY_KEY_ID2_INFO,
|
|
|
|
AuthorityCertSerialNumber), CRYPT_AsnDecodeIntegerInternal,
|
|
|
|
sizeof(CRYPT_INTEGER_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CERT_AUTHORITY_KEY_ID2_INFO,
|
|
|
|
AuthorityCertSerialNumber.pbData), 0 },
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
2007-06-12 23:39:19 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-27 19:56:56 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeAccessDescription(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ 0, offsetof(CERT_ACCESS_DESCRIPTION, pszAccessMethod),
|
|
|
|
CRYPT_AsnDecodeOidInternal, sizeof(LPSTR), FALSE, TRUE,
|
|
|
|
offsetof(CERT_ACCESS_DESCRIPTION, pszAccessMethod), 0 },
|
|
|
|
{ 0, offsetof(CERT_ACCESS_DESCRIPTION, AccessLocation),
|
|
|
|
CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), FALSE,
|
|
|
|
TRUE, offsetof(CERT_ACCESS_DESCRIPTION, AccessLocation.u.pwszURL), 0 },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_ACCESS_DESCRIPTION *descr = pvStructInfo;
|
2008-08-27 19:56:56 +02:00
|
|
|
|
|
|
|
return CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, descr ? descr->pszAccessMethod : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeAuthorityInfoAccess(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_AUTHORITY_INFO_ACCESS, cAccDescr),
|
|
|
|
offsetof(CERT_AUTHORITY_INFO_ACCESS, rgAccDescr),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_AUTHORITY_INFO_ACCESS),
|
2008-08-27 19:56:56 +02:00
|
|
|
CRYPT_AsnDecodeAccessDescription, sizeof(CERT_ACCESS_DESCRIPTION),
|
|
|
|
TRUE, offsetof(CERT_ACCESS_DESCRIPTION, pszAccessMethod) };
|
2011-11-21 16:52:31 +01:00
|
|
|
CERT_AUTHORITY_INFO_ACCESS *info = pvStructInfo;
|
2008-08-27 19:56:56 +02:00
|
|
|
|
2011-11-21 16:52:31 +01:00
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
info->rgAccDescr = (CERT_ACCESS_DESCRIPTION *)(info + 1);
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
2009-10-14 22:09:10 +02:00
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
2008-08-27 19:56:56 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePKCSContent(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2007-06-12 23:40:04 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD dataLen;
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2007-06-12 23:40:04 +02:00
|
|
|
|
|
|
|
/* The caller has already checked the tag, no need to check it again.
|
2007-09-18 02:29:37 +02:00
|
|
|
* Check the outer length is valid:
|
2007-06-12 23:40:04 +02:00
|
|
|
*/
|
2007-09-18 02:29:37 +02:00
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(pbEncoded, cbEncoded, &dataLen)))
|
2007-06-12 23:40:04 +02:00
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
DWORD innerLen;
|
|
|
|
|
|
|
|
pbEncoded += 1 + lenBytes;
|
2007-09-18 02:29:37 +02:00
|
|
|
cbEncoded -= 1 + lenBytes;
|
|
|
|
if (dataLen == CMSG_INDEFINITE_LENGTH)
|
|
|
|
cbEncoded -= 2; /* space for 0 TLV */
|
|
|
|
/* Check the inner length is valid: */
|
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(pbEncoded, cbEncoded, &innerLen)))
|
2007-06-12 23:40:04 +02:00
|
|
|
{
|
2007-09-18 02:29:37 +02:00
|
|
|
DWORD decodedLen;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeCopyBytes(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pcbStructInfo, &decodedLen);
|
|
|
|
if (dataLen == CMSG_INDEFINITE_LENGTH)
|
|
|
|
{
|
|
|
|
if (*(pbEncoded + decodedLen) != 0 ||
|
|
|
|
*(pbEncoded + decodedLen + 1) != 0)
|
|
|
|
{
|
|
|
|
TRACE("expected 0 TLV, got {%02x,%02x}\n",
|
|
|
|
*(pbEncoded + decodedLen),
|
|
|
|
*(pbEncoded + decodedLen + 1));
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
decodedLen += 2;
|
|
|
|
}
|
|
|
|
if (ret && pcbDecoded)
|
|
|
|
{
|
|
|
|
*pcbDecoded = 1 + lenBytes + decodedLen;
|
|
|
|
TRACE("decoded %d bytes\n", *pcbDecoded);
|
|
|
|
}
|
2007-06-12 23:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePKCSContentInfoInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2007-07-17 16:25:31 +02:00
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CRYPT_CONTENT_INFO *info = pvStructInfo;
|
2007-07-17 16:25:31 +02:00
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CRYPT_CONTENT_INFO, pszObjId),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
|
2007-07-17 16:25:31 +02:00
|
|
|
offsetof(CRYPT_CONTENT_INFO, pszObjId), 0 },
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 0,
|
|
|
|
offsetof(CRYPT_CONTENT_INFO, Content), CRYPT_AsnDecodePKCSContent,
|
|
|
|
sizeof(CRYPT_DER_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CRYPT_CONTENT_INFO, Content.pbData), 0 },
|
|
|
|
};
|
2007-07-30 21:08:57 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2007-07-17 16:25:31 +02:00
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
2007-09-18 02:13:12 +02:00
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, info ? info->pszObjId : NULL);
|
2007-07-30 21:08:57 +02:00
|
|
|
return ret;
|
2007-07-17 16:25:31 +02:00
|
|
|
}
|
|
|
|
|
2007-06-12 23:40:04 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodePKCSContentInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodePKCSContentInfoInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pcbStructInfo, NULL);
|
2007-07-17 16:25:31 +02:00
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
|
|
|
ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, *pcbStructInfo);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_CONTENT_INFO *info;
|
2007-06-12 23:40:04 +02:00
|
|
|
|
2007-07-17 16:25:31 +02:00
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
info = pvStructInfo;
|
2007-07-17 16:25:31 +02:00
|
|
|
info->pszObjId = (LPSTR)((BYTE *)info +
|
|
|
|
sizeof(CRYPT_CONTENT_INFO));
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodePKCSContentInfoInternal(pbEncoded,
|
|
|
|
cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, info);
|
2007-07-17 16:25:31 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-12 23:40:04 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-17 16:25:31 +02:00
|
|
|
BOOL CRYPT_AsnDecodePKCSDigestedData(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
|
|
|
|
CRYPT_DIGESTED_DATA *digestedData, DWORD *pcbDigestedData)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
2007-09-18 02:07:35 +02:00
|
|
|
{ ASN_INTEGER, offsetof(CRYPT_DIGESTED_DATA, version),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
2007-07-17 16:25:31 +02:00
|
|
|
{ ASN_SEQUENCEOF, offsetof(CRYPT_DIGESTED_DATA, DigestAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CRYPT_DIGESTED_DATA, DigestAlgorithm.pszObjId),
|
|
|
|
0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CRYPT_DIGESTED_DATA, ContentInfo),
|
|
|
|
CRYPT_AsnDecodePKCSContentInfoInternal,
|
|
|
|
sizeof(CRYPT_CONTENT_INFO), FALSE, TRUE, offsetof(CRYPT_DIGESTED_DATA,
|
|
|
|
ContentInfo.pszObjId), 0 },
|
|
|
|
{ ASN_OCTETSTRING, offsetof(CRYPT_DIGESTED_DATA, hash),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_HASH_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CRYPT_DIGESTED_DATA, hash.pbData), 0 },
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, digestedData, pcbDigestedData,
|
|
|
|
NULL, NULL);
|
2007-07-17 16:25:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeAltName(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
if ((ret = CRYPT_AsnDecodeAltNameInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL)))
|
2007-09-18 02:08:24 +02:00
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
CERT_ALT_NAME_INFO *name;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
name = pvStructInfo;
|
2007-09-18 02:08:24 +02:00
|
|
|
name->rgAltEntry = (PCERT_ALT_NAME_ENTRY)
|
|
|
|
((BYTE *)pvStructInfo + sizeof(CERT_ALT_NAME_INFO));
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeAltNameInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, name);
|
2007-09-18 02:08:24 +02:00
|
|
|
}
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PATH_LEN_CONSTRAINT
|
|
|
|
{
|
|
|
|
BOOL fPathLenConstraint;
|
|
|
|
DWORD dwPathLenConstraint;
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePathLenConstraint(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
2007-09-18 02:21:43 +02:00
|
|
|
DWORD bytesNeeded = sizeof(struct PATH_LEN_CONSTRAINT), size;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:21:43 +02:00
|
|
|
if (!pvStructInfo)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:21:43 +02:00
|
|
|
ret = CRYPT_AsnDecodeIntInternal(pbEncoded, cbEncoded, dwFlags, NULL,
|
|
|
|
&size, pcbDecoded);
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
}
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
struct PATH_LEN_CONSTRAINT *constraint = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2008-08-14 21:48:59 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
2007-09-18 02:21:43 +02:00
|
|
|
size = sizeof(constraint->dwPathLenConstraint);
|
|
|
|
ret = CRYPT_AsnDecodeIntInternal(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
&constraint->dwPathLenConstraint, &size, pcbDecoded);
|
|
|
|
if (ret)
|
|
|
|
constraint->fPathLenConstraint = TRUE;
|
|
|
|
TRACE("got an int, dwPathLenConstraint is %d\n",
|
|
|
|
constraint->dwPathLenConstraint);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeSubtreeConstraints(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_BASIC_CONSTRAINTS_INFO, cSubtreesConstraint),
|
|
|
|
offsetof(CERT_BASIC_CONSTRAINTS_INFO, rgSubtreesConstraint),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CERT_BASIC_CONSTRAINTS_INFO, cSubtreesConstraint),
|
2007-09-18 02:13:12 +02:00
|
|
|
CRYPT_AsnDecodeCopyBytes, sizeof(CERT_NAME_BLOB), TRUE,
|
2006-05-16 02:01:28 +02:00
|
|
|
offsetof(CERT_NAME_BLOB, pbData) };
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeBasicConstraints(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_BITSTRING, offsetof(CERT_BASIC_CONSTRAINTS_INFO, SubjectType),
|
|
|
|
CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CERT_BASIC_CONSTRAINTS_INFO, SubjectType.pbData), 0 },
|
|
|
|
{ ASN_INTEGER, offsetof(CERT_BASIC_CONSTRAINTS_INFO,
|
|
|
|
fPathLenConstraint), CRYPT_AsnDecodePathLenConstraint,
|
|
|
|
sizeof(struct PATH_LEN_CONSTRAINT), TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_BASIC_CONSTRAINTS_INFO,
|
|
|
|
cSubtreesConstraint), CRYPT_AsnDecodeSubtreeConstraints,
|
2009-10-14 21:55:05 +02:00
|
|
|
FINALMEMBERSIZE(CERT_BASIC_CONSTRAINTS_INFO, cSubtreesConstraint),
|
|
|
|
TRUE, TRUE,
|
2006-05-16 02:01:28 +02:00
|
|
|
offsetof(CERT_BASIC_CONSTRAINTS_INFO, rgSubtreesConstraint), 0 },
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeBasicConstraints2(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_BOOL, offsetof(CERT_BASIC_CONSTRAINTS2_INFO, fCA),
|
|
|
|
CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_INTEGER, offsetof(CERT_BASIC_CONSTRAINTS2_INFO,
|
|
|
|
fPathLenConstraint), CRYPT_AsnDecodePathLenConstraint,
|
|
|
|
sizeof(struct PATH_LEN_CONSTRAINT), TRUE, FALSE, 0, 0 },
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-15 03:00:01 +01:00
|
|
|
static BOOL CRYPT_AsnDecodePolicyQualifier(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CERT_POLICY_QUALIFIER_INFO,
|
|
|
|
pszPolicyQualifierId), CRYPT_AsnDecodeOidInternal, sizeof(LPSTR),
|
|
|
|
FALSE, TRUE, offsetof(CERT_POLICY_QUALIFIER_INFO, pszPolicyQualifierId),
|
|
|
|
0 },
|
|
|
|
{ 0, offsetof(CERT_POLICY_QUALIFIER_INFO, Qualifier),
|
|
|
|
CRYPT_AsnDecodeDerBlob, sizeof(CRYPT_OBJID_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CERT_POLICY_QUALIFIER_INFO, Qualifier.pbData), 0 },
|
|
|
|
};
|
|
|
|
BOOL ret;
|
|
|
|
CERT_POLICY_QUALIFIER_INFO *qualifier = pvStructInfo;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, qualifier ? qualifier->pszPolicyQualifierId : NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodePolicyQualifiers(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_POLICY_INFO, cPolicyQualifier),
|
|
|
|
offsetof(CERT_POLICY_INFO, rgPolicyQualifier),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CERT_POLICY_INFO, cPolicyQualifier),
|
2008-11-15 03:00:01 +01:00
|
|
|
CRYPT_AsnDecodePolicyQualifier, sizeof(CERT_POLICY_QUALIFIER_INFO), TRUE,
|
|
|
|
offsetof(CERT_POLICY_QUALIFIER_INFO, pszPolicyQualifierId) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2008-11-15 03:00:01 +01:00
|
|
|
TRACE("Returning %d (%08x)\n", ret, GetLastError());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeCertPolicy(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CERT_POLICY_INFO, pszPolicyIdentifier),
|
|
|
|
CRYPT_AsnDecodeOidInternal, sizeof(LPSTR), FALSE, TRUE,
|
|
|
|
offsetof(CERT_POLICY_INFO, pszPolicyIdentifier), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CERT_POLICY_INFO, cPolicyQualifier),
|
2009-10-14 22:03:25 +02:00
|
|
|
CRYPT_AsnDecodePolicyQualifiers,
|
|
|
|
FINALMEMBERSIZE(CERT_POLICY_INFO, cPolicyQualifier), TRUE,
|
2008-11-15 03:00:01 +01:00
|
|
|
TRUE, offsetof(CERT_POLICY_INFO, rgPolicyQualifier), 0 },
|
|
|
|
};
|
|
|
|
CERT_POLICY_INFO *info = pvStructInfo;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, info ? info->pszPolicyIdentifier : NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCertPolicies(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_POLICIES_INFO, cPolicyInfo),
|
|
|
|
offsetof(CERT_POLICIES_INFO, rgPolicyInfo),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_POLICIES_INFO),
|
2008-11-15 03:00:01 +01:00
|
|
|
CRYPT_AsnDecodeCertPolicy, sizeof(CERT_POLICY_INFO), TRUE,
|
|
|
|
offsetof(CERT_POLICY_INFO, pszPolicyIdentifier) };
|
2011-11-21 16:52:31 +01:00
|
|
|
CERT_POLICIES_INFO *info = pvStructInfo;
|
|
|
|
|
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
info->rgPolicyInfo = (CERT_POLICY_INFO *)(info + 1);
|
2009-10-14 22:05:44 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
2009-10-14 22:09:10 +02:00
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
2008-11-15 03:00:01 +01:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-06 21:26:59 +01:00
|
|
|
static BOOL CRYPT_AsnDecodeCertPolicyMapping(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CERT_POLICY_MAPPING,
|
|
|
|
pszIssuerDomainPolicy), CRYPT_AsnDecodeOidInternal, sizeof(LPSTR),
|
|
|
|
FALSE, TRUE, offsetof(CERT_POLICY_MAPPING, pszIssuerDomainPolicy), 0 },
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CERT_POLICY_MAPPING,
|
|
|
|
pszSubjectDomainPolicy), CRYPT_AsnDecodeOidInternal, sizeof(LPSTR),
|
|
|
|
FALSE, TRUE, offsetof(CERT_POLICY_MAPPING, pszSubjectDomainPolicy), 0 },
|
|
|
|
};
|
|
|
|
CERT_POLICY_MAPPING *mapping = pvStructInfo;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, mapping ? mapping->pszIssuerDomainPolicy : NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCertPolicyMappings(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
|
|
|
offsetof(CERT_POLICY_MAPPINGS_INFO, cPolicyMapping),
|
|
|
|
offsetof(CERT_POLICY_MAPPINGS_INFO, rgPolicyMapping),
|
|
|
|
sizeof(CERT_POLICY_MAPPING),
|
|
|
|
CRYPT_AsnDecodeCertPolicyMapping, sizeof(CERT_POLICY_MAPPING), TRUE,
|
|
|
|
offsetof(CERT_POLICY_MAPPING, pszIssuerDomainPolicy) };
|
2011-11-21 16:52:31 +01:00
|
|
|
CERT_POLICY_MAPPINGS_INFO *info = pvStructInfo;
|
2009-11-06 21:26:59 +01:00
|
|
|
|
2011-11-21 16:52:31 +01:00
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
info->rgPolicyMapping = (CERT_POLICY_MAPPING *)(info + 1);
|
2009-11-06 21:26:59 +01:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-06 21:47:15 +01:00
|
|
|
static BOOL CRYPT_AsnDecodeRequireExplicit(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD skip, size = sizeof(skip);
|
|
|
|
|
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (pbEncoded[0] != (ASN_CONTEXT | 0))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((ret = CRYPT_AsnDecodeIntInternal(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
&skip, &size, pcbDecoded)))
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded = MEMBERSIZE(CERT_POLICY_CONSTRAINTS_INFO,
|
|
|
|
fRequireExplicitPolicy, fInhibitPolicyMapping);
|
|
|
|
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-29 02:39:42 +01:00
|
|
|
CERT_POLICY_CONSTRAINTS_INFO *info = CONTAINING_RECORD(pvStructInfo,
|
|
|
|
CERT_POLICY_CONSTRAINTS_INFO, fRequireExplicitPolicy);
|
2009-11-06 21:47:15 +01:00
|
|
|
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
/* The BOOL is implicit: if the integer is present, then it's
|
|
|
|
* TRUE.
|
|
|
|
*/
|
|
|
|
info->fRequireExplicitPolicy = TRUE;
|
|
|
|
info->dwRequireExplicitPolicySkipCerts = skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeInhibitMapping(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD skip, size = sizeof(skip);
|
|
|
|
|
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (pbEncoded[0] != (ASN_CONTEXT | 1))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((ret = CRYPT_AsnDecodeIntInternal(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
&skip, &size, pcbDecoded)))
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded = FINALMEMBERSIZE(CERT_POLICY_CONSTRAINTS_INFO,
|
|
|
|
fInhibitPolicyMapping);
|
|
|
|
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-29 02:39:42 +01:00
|
|
|
CERT_POLICY_CONSTRAINTS_INFO *info = CONTAINING_RECORD(pvStructInfo,
|
|
|
|
CERT_POLICY_CONSTRAINTS_INFO, fInhibitPolicyMapping);
|
2009-11-06 21:47:15 +01:00
|
|
|
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
/* The BOOL is implicit: if the integer is present, then it's
|
|
|
|
* TRUE.
|
|
|
|
*/
|
|
|
|
info->fInhibitPolicyMapping = TRUE;
|
|
|
|
info->dwInhibitPolicyMappingSkipCerts = skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCertPolicyConstraints(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
|
|
|
|
void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_CONTEXT | 0,
|
|
|
|
offsetof(CERT_POLICY_CONSTRAINTS_INFO, fRequireExplicitPolicy),
|
|
|
|
CRYPT_AsnDecodeRequireExplicit,
|
|
|
|
MEMBERSIZE(CERT_POLICY_CONSTRAINTS_INFO, fRequireExplicitPolicy,
|
|
|
|
fInhibitPolicyMapping), TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_CONTEXT | 1,
|
|
|
|
offsetof(CERT_POLICY_CONSTRAINTS_INFO, fInhibitPolicyMapping),
|
|
|
|
CRYPT_AsnDecodeInhibitMapping,
|
|
|
|
FINALMEMBERSIZE(CERT_POLICY_CONSTRAINTS_INFO, fInhibitPolicyMapping),
|
|
|
|
TRUE, FALSE, 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
#define RSA1_MAGIC 0x31415352
|
|
|
|
|
|
|
|
struct DECODED_RSA_PUB_KEY
|
|
|
|
{
|
|
|
|
DWORD pubexp;
|
|
|
|
CRYPT_INTEGER_BLOB modulus;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeRsaPubKey(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, modulus),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PUB_KEY, modulus.pbData),
|
|
|
|
0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, pubexp),
|
2007-09-18 02:07:35 +02:00
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
2006-05-16 02:01:28 +02:00
|
|
|
};
|
|
|
|
struct DECODED_RSA_PUB_KEY *decodedKey = NULL;
|
|
|
|
DWORD size = 0;
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, &decodedKey,
|
|
|
|
&size, NULL, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
|
|
|
|
decodedKey->modulus.cbData;
|
|
|
|
|
|
|
|
if (!pvStructInfo)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
BLOBHEADER *hdr;
|
|
|
|
RSAPUBKEY *rsaPubKey;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
hdr = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
hdr->bType = PUBLICKEYBLOB;
|
|
|
|
hdr->bVersion = CUR_BLOB_VERSION;
|
|
|
|
hdr->reserved = 0;
|
|
|
|
hdr->aiKeyAlg = CALG_RSA_KEYX;
|
|
|
|
rsaPubKey = (RSAPUBKEY *)((BYTE *)pvStructInfo +
|
|
|
|
sizeof(BLOBHEADER));
|
|
|
|
rsaPubKey->magic = RSA1_MAGIC;
|
|
|
|
rsaPubKey->pubexp = decodedKey->pubexp;
|
|
|
|
rsaPubKey->bitlen = decodedKey->modulus.cbData * 8;
|
|
|
|
memcpy((BYTE *)pvStructInfo + sizeof(BLOBHEADER) +
|
|
|
|
sizeof(RSAPUBKEY), decodedKey->modulus.pbData,
|
|
|
|
decodedKey->modulus.cbData);
|
|
|
|
}
|
|
|
|
LocalFree(decodedKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:48:05 +01:00
|
|
|
#define RSA2_MAGIC 0x32415352
|
|
|
|
|
|
|
|
struct DECODED_RSA_PRIV_KEY
|
|
|
|
{
|
|
|
|
DWORD version;
|
|
|
|
DWORD pubexp;
|
|
|
|
CRYPT_INTEGER_BLOB modulus;
|
|
|
|
CRYPT_INTEGER_BLOB privexp;
|
|
|
|
CRYPT_INTEGER_BLOB prime1;
|
|
|
|
CRYPT_INTEGER_BLOB prime2;
|
|
|
|
CRYPT_INTEGER_BLOB exponent1;
|
|
|
|
CRYPT_INTEGER_BLOB exponent2;
|
|
|
|
CRYPT_INTEGER_BLOB coefficient;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeRsaPrivKey(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD halflen;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, version),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, modulus),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PRIV_KEY, modulus.pbData),
|
|
|
|
0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, pubexp),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, privexp),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PRIV_KEY, privexp.pbData),
|
|
|
|
0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, prime1),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PRIV_KEY, prime1.pbData),
|
|
|
|
0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, prime2),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PRIV_KEY, prime2.pbData),
|
|
|
|
0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, exponent1),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PRIV_KEY, exponent1.pbData),
|
|
|
|
0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, exponent2),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PRIV_KEY, exponent2.pbData),
|
|
|
|
0 },
|
|
|
|
{ ASN_INTEGER, offsetof(struct DECODED_RSA_PRIV_KEY, coefficient),
|
|
|
|
CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(struct DECODED_RSA_PRIV_KEY, coefficient.pbData),
|
|
|
|
0 },
|
|
|
|
};
|
|
|
|
struct DECODED_RSA_PRIV_KEY *decodedKey = NULL;
|
|
|
|
DWORD size = 0;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, &decodedKey,
|
|
|
|
&size, NULL, NULL);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
halflen = decodedKey->modulus.cbData / 2;
|
|
|
|
if ((decodedKey->modulus.cbData != halflen * 2) ||
|
|
|
|
(decodedKey->prime1.cbData != halflen) ||
|
|
|
|
(decodedKey->prime2.cbData != halflen) ||
|
|
|
|
(decodedKey->exponent1.cbData != halflen) ||
|
|
|
|
(decodedKey->exponent2.cbData != halflen) ||
|
|
|
|
(decodedKey->coefficient.cbData != halflen) ||
|
|
|
|
(decodedKey->privexp.cbData != halflen * 2))
|
|
|
|
{
|
|
|
|
ret = FALSE;
|
|
|
|
SetLastError(CRYPT_E_BAD_ENCODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
|
|
|
|
(halflen * 9);
|
|
|
|
|
|
|
|
if (!pvStructInfo)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
BLOBHEADER *hdr;
|
|
|
|
RSAPUBKEY *rsaPubKey;
|
|
|
|
BYTE *vardata;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
|
|
|
|
hdr = pvStructInfo;
|
|
|
|
hdr->bType = PRIVATEKEYBLOB;
|
|
|
|
hdr->bVersion = CUR_BLOB_VERSION;
|
|
|
|
hdr->reserved = 0;
|
|
|
|
hdr->aiKeyAlg = CALG_RSA_KEYX;
|
|
|
|
|
|
|
|
rsaPubKey = (RSAPUBKEY *)((BYTE *)pvStructInfo +
|
|
|
|
sizeof(BLOBHEADER));
|
|
|
|
rsaPubKey->magic = RSA2_MAGIC;
|
|
|
|
rsaPubKey->pubexp = decodedKey->pubexp;
|
|
|
|
rsaPubKey->bitlen = halflen * 16;
|
|
|
|
|
|
|
|
vardata = (BYTE*)(rsaPubKey + 1);
|
|
|
|
memcpy(vardata,
|
|
|
|
decodedKey->modulus.pbData, halflen * 2);
|
|
|
|
memcpy(vardata + halflen * 2,
|
|
|
|
decodedKey->prime1.pbData, halflen);
|
|
|
|
memcpy(vardata + halflen * 3,
|
|
|
|
decodedKey->prime2.pbData, halflen);
|
|
|
|
memcpy(vardata + halflen * 4,
|
|
|
|
decodedKey->exponent1.pbData, halflen);
|
|
|
|
memcpy(vardata + halflen * 5,
|
|
|
|
decodedKey->exponent2.pbData, halflen);
|
|
|
|
memcpy(vardata + halflen * 6,
|
|
|
|
decodedKey->coefficient.pbData, halflen);
|
|
|
|
memcpy(vardata + halflen * 7,
|
|
|
|
decodedKey->privexp.pbData, halflen * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalFree(decodedKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeOctetsInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD bytesNeeded, dataLen;
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
2007-09-18 02:18:53 +02:00
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
|
|
|
|
bytesNeeded = sizeof(CRYPT_DATA_BLOB);
|
|
|
|
else
|
|
|
|
bytesNeeded = dataLen + sizeof(CRYPT_DATA_BLOB);
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB *blob;
|
|
|
|
|
2008-08-14 21:48:59 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
2009-01-19 10:23:15 +01:00
|
|
|
blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
blob->cbData = dataLen;
|
|
|
|
if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
|
|
|
|
blob->pbData = (BYTE *)pbEncoded + 1 + lenBytes;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(blob->pbData);
|
|
|
|
if (blob->cbData)
|
|
|
|
memcpy(blob->pbData, pbEncoded + 1 + lenBytes,
|
|
|
|
blob->cbData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeOctets(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (pbEncoded[0] != ASN_OCTETSTRING)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2007-09-18 02:13:12 +02:00
|
|
|
else if ((ret = CRYPT_AsnDecodeOctetsInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB *blob;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_DATA_BLOB);
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeOctetsInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, blob);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeBitsInternal(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2008-11-05 20:47:38 +01:00
|
|
|
DWORD bytesNeeded, dataLen;
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("(%p, %d, 0x%08x, %p, %d, %p)\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2008-11-05 20:47:38 +01:00
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2008-11-05 20:47:38 +01:00
|
|
|
if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
|
|
|
|
bytesNeeded = sizeof(CRYPT_BIT_BLOB);
|
|
|
|
else
|
|
|
|
bytesNeeded = dataLen - 1 + sizeof(CRYPT_BIT_BLOB);
|
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2008-11-05 20:47:38 +01:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRYPT_BIT_BLOB *blob;
|
|
|
|
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
2009-01-19 10:23:15 +01:00
|
|
|
blob = pvStructInfo;
|
2008-11-05 20:47:38 +01:00
|
|
|
blob->cbData = dataLen - 1;
|
|
|
|
blob->cUnusedBits = *(pbEncoded + 1 + lenBytes);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
|
|
|
|
{
|
2008-11-05 20:47:38 +01:00
|
|
|
blob->pbData = (BYTE *)pbEncoded + 2 + lenBytes;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-05 20:47:38 +01:00
|
|
|
assert(blob->pbData);
|
|
|
|
if (blob->cbData)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2008-11-05 20:47:38 +01:00
|
|
|
BYTE mask = 0xff << blob->cUnusedBits;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2008-11-05 20:47:38 +01:00
|
|
|
memcpy(blob->pbData, pbEncoded + 2 + lenBytes,
|
|
|
|
blob->cbData);
|
|
|
|
blob->pbData[blob->cbData - 1] &= mask;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeBits(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %d, 0x%08x, %p, %p, %p)\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2008-11-05 20:47:38 +01:00
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (pbEncoded[0] != ASN_BITSTRING)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if ((ret = CRYPT_AsnDecodeBitsInternal(pbEncoded, cbEncoded,
|
2007-09-18 02:13:12 +02:00
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
CRYPT_BIT_BLOB *blob;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_BIT_BLOB);
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeBitsInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, blob);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-11 02:49:33 +02:00
|
|
|
/* Ignores tag. Only allows integers 4 bytes or smaller in size. */
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeIntInternal(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2007-09-18 02:03:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-10-16 00:12:40 +02:00
|
|
|
DWORD dataLen;
|
2007-09-18 02:03:28 +02:00
|
|
|
|
2009-10-16 00:12:40 +02:00
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
2007-09-18 02:03:28 +02:00
|
|
|
{
|
2009-10-16 00:12:40 +02:00
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
|
|
|
if (dataLen > sizeof(int))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_LARGE);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (!pvStructInfo)
|
2007-09-18 02:03:28 +02:00
|
|
|
*pcbStructInfo = sizeof(int);
|
2007-09-18 02:04:40 +02:00
|
|
|
else if ((ret = CRYPT_DecodeCheckSpace(pcbStructInfo, sizeof(int))))
|
2007-09-18 02:03:28 +02:00
|
|
|
{
|
|
|
|
int val, i;
|
|
|
|
|
2009-10-16 00:12:40 +02:00
|
|
|
if (dataLen && pbEncoded[1 + lenBytes] & 0x80)
|
2007-09-18 02:03:28 +02:00
|
|
|
{
|
|
|
|
/* initialize to a negative value to sign-extend */
|
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
val = 0;
|
2009-10-16 00:12:40 +02:00
|
|
|
for (i = 0; i < dataLen; i++)
|
2007-09-18 02:03:28 +02:00
|
|
|
{
|
|
|
|
val <<= 8;
|
2009-10-16 00:12:40 +02:00
|
|
|
val |= pbEncoded[1 + lenBytes + i];
|
2007-09-18 02:03:28 +02:00
|
|
|
}
|
|
|
|
memcpy(pvStructInfo, &val, sizeof(int));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-16 02:01:28 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeInt(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-10-11 02:49:33 +02:00
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
2008-10-16 18:09:37 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
2007-10-11 02:49:33 +02:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (pbEncoded[0] != ASN_INTEGER)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = CRYPT_AsnDecodeIntInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2007-09-18 02:03:28 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeIntInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, pvStructInfo);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeIntegerInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2006-09-27 06:20:35 +02:00
|
|
|
DWORD bytesNeeded, dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-09-27 06:20:35 +02:00
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-09-27 06:20:35 +02:00
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-09-27 06:20:35 +02:00
|
|
|
bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-09-27 06:20:35 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-09-27 06:20:35 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CRYPT_INTEGER_BLOB *blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2008-08-14 21:48:59 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
2006-09-27 06:20:35 +02:00
|
|
|
blob->cbData = dataLen;
|
|
|
|
assert(blob->pbData);
|
|
|
|
if (blob->cbData)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-09-27 06:20:35 +02:00
|
|
|
DWORD i;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-09-27 06:20:35 +02:00
|
|
|
for (i = 0; i < blob->cbData; i++)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2006-09-27 06:20:35 +02:00
|
|
|
blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
|
|
|
|
dataLen - i - 1);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeInteger(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2006-09-27 06:20:35 +02:00
|
|
|
if (pbEncoded[0] != ASN_INTEGER)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeIntegerInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL);
|
2006-09-27 06:20:35 +02:00
|
|
|
if (ret)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
CRYPT_INTEGER_BLOB *blob;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
blob->pbData = (BYTE *)pvStructInfo +
|
|
|
|
sizeof(CRYPT_INTEGER_BLOB);
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeIntegerInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, blob);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeUnsignedIntegerInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (pbEncoded[0] == ASN_INTEGER)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, dataLen;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-05-16 02:01:28 +02:00
|
|
|
bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CRYPT_INTEGER_BLOB *blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2008-08-14 21:48:59 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
2006-05-16 02:01:28 +02:00
|
|
|
blob->cbData = dataLen;
|
|
|
|
assert(blob->pbData);
|
|
|
|
/* remove leading zero byte if it exists */
|
|
|
|
if (blob->cbData && *(pbEncoded + 1 + lenBytes) == 0)
|
|
|
|
{
|
|
|
|
blob->cbData--;
|
|
|
|
blob->pbData++;
|
|
|
|
}
|
|
|
|
if (blob->cbData)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < blob->cbData; i++)
|
|
|
|
{
|
|
|
|
blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
|
|
|
|
dataLen - i - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeUnsignedInteger(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
if ((ret = CRYPT_AsnDecodeUnsignedIntegerInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL)))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
CRYPT_INTEGER_BLOB *blob;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
blob = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
blob->pbData = (BYTE *)pvStructInfo +
|
|
|
|
sizeof(CRYPT_INTEGER_BLOB);
|
2007-09-18 02:13:12 +02:00
|
|
|
ret = CRYPT_AsnDecodeUnsignedIntegerInternal(pbEncoded,
|
|
|
|
cbEncoded, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, blob);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeEnumerated(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (!pvStructInfo)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = sizeof(int);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
if (pbEncoded[0] == ASN_ENUMERATED)
|
|
|
|
{
|
|
|
|
unsigned int val = 0, i;
|
|
|
|
|
|
|
|
if (cbEncoded <= 1)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (pbEncoded[1] == 0)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A little strange looking, but we have to accept a sign byte:
|
|
|
|
* 0xffffffff gets encoded as 0a 05 00 ff ff ff ff. Also,
|
|
|
|
* assuming a small length is okay here, it has to be in short
|
|
|
|
* form.
|
|
|
|
*/
|
|
|
|
if (pbEncoded[1] > sizeof(unsigned int) + 1)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_LARGE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (i = 0; i < pbEncoded[1]; i++)
|
|
|
|
{
|
|
|
|
val <<= 8;
|
|
|
|
val |= pbEncoded[2 + i];
|
|
|
|
}
|
|
|
|
if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, sizeof(unsigned int))))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
memcpy(pvStructInfo, &val, sizeof(unsigned int));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Modifies word, pbEncoded, and len, and magically sets a value ret to FALSE
|
|
|
|
* if it fails.
|
|
|
|
*/
|
|
|
|
#define CRYPT_TIME_GET_DIGITS(pbEncoded, len, numDigits, word) \
|
|
|
|
do { \
|
|
|
|
BYTE i; \
|
|
|
|
\
|
|
|
|
(word) = 0; \
|
|
|
|
for (i = 0; (len) > 0 && i < (numDigits); i++, (len)--) \
|
|
|
|
{ \
|
|
|
|
if (!isdigit(*(pbEncoded))) \
|
|
|
|
{ \
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT); \
|
|
|
|
ret = FALSE; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
(word) *= 10; \
|
|
|
|
(word) += *(pbEncoded)++ - '0'; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeTimeZone(const BYTE *pbEncoded, DWORD len,
|
|
|
|
SYSTEMTIME *sysTime)
|
|
|
|
{
|
2007-09-18 02:08:52 +02:00
|
|
|
BOOL ret = TRUE;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:08:52 +02:00
|
|
|
if (len >= 3 && (*pbEncoded == '+' || *pbEncoded == '-'))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:08:52 +02:00
|
|
|
WORD hours, minutes = 0;
|
|
|
|
BYTE sign = *pbEncoded++;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:08:52 +02:00
|
|
|
len--;
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, hours);
|
|
|
|
if (ret && hours >= 24)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (len >= 2)
|
|
|
|
{
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, minutes);
|
|
|
|
if (ret && minutes >= 60)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2007-09-18 02:08:52 +02:00
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (sign == '+')
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:08:52 +02:00
|
|
|
sysTime->wHour += hours;
|
|
|
|
sysTime->wMinute += minutes;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2007-09-18 02:08:52 +02:00
|
|
|
else
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:08:52 +02:00
|
|
|
if (hours > sysTime->wHour)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:08:52 +02:00
|
|
|
sysTime->wDay--;
|
|
|
|
sysTime->wHour = 24 - (hours - sysTime->wHour);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
else
|
2007-09-18 02:08:52 +02:00
|
|
|
sysTime->wHour -= hours;
|
|
|
|
if (minutes > sysTime->wMinute)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:08:52 +02:00
|
|
|
sysTime->wHour--;
|
|
|
|
sysTime->wMinute = 60 - (minutes - sysTime->wMinute);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2007-09-18 02:08:52 +02:00
|
|
|
else
|
|
|
|
sysTime->wMinute -= minutes;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MIN_ENCODED_TIME_LENGTH 10
|
|
|
|
|
2007-09-18 02:02:58 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeUtcTimeInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
BOOL ret = FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:02:58 +02:00
|
|
|
if (pbEncoded[0] == ASN_UTCTIME)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
if (cbEncoded <= 1)
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
else if (pbEncoded[1] > 0x7f)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
/* long-form date strings really can't be valid */
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SYSTEMTIME sysTime = { 0 };
|
|
|
|
BYTE len = pbEncoded[1];
|
|
|
|
|
|
|
|
if (len < MIN_ENCODED_TIME_LENGTH)
|
2006-05-16 02:01:28 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
else
|
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
ret = TRUE;
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 2 + len;
|
2007-09-18 02:02:58 +02:00
|
|
|
pbEncoded += 2;
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wYear);
|
|
|
|
if (sysTime.wYear >= 50)
|
|
|
|
sysTime.wYear += 1900;
|
|
|
|
else
|
|
|
|
sysTime.wYear += 2000;
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMinute);
|
|
|
|
if (ret && len > 0)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
if (len >= 2 && isdigit(*pbEncoded) &&
|
|
|
|
isdigit(*(pbEncoded + 1)))
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
|
|
|
|
sysTime.wSecond);
|
|
|
|
else if (isdigit(*pbEncoded))
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 1,
|
|
|
|
sysTime.wSecond);
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
|
|
|
|
&sysTime);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2007-09-18 02:02:58 +02:00
|
|
|
if (ret)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = sizeof(FILETIME);
|
2007-09-18 02:04:40 +02:00
|
|
|
else if ((ret = CRYPT_DecodeCheckSpace(pcbStructInfo,
|
|
|
|
sizeof(FILETIME))))
|
2009-01-19 10:23:15 +01:00
|
|
|
ret = SystemTimeToFileTime(&sysTime, pvStructInfo);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 02:02:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeUtcTime(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2007-09-18 02:02:58 +02:00
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeUtcTimeInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL);
|
|
|
|
if (ret)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
ret = CRYPT_AsnDecodeUtcTimeInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, pvStructInfo);
|
2007-09-18 02:02:58 +02:00
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:02:58 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeGeneralizedTime(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
BOOL ret = FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:02:58 +02:00
|
|
|
if (pbEncoded[0] == ASN_GENERALTIME)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
if (cbEncoded <= 1)
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
else if (pbEncoded[1] > 0x7f)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
/* long-form date strings really can't be valid */
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BYTE len = pbEncoded[1];
|
|
|
|
|
|
|
|
if (len < MIN_ENCODED_TIME_LENGTH)
|
2006-05-16 02:01:28 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
else
|
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
SYSTEMTIME sysTime = { 0 };
|
2006-05-16 02:01:28 +02:00
|
|
|
|
2007-09-18 02:02:58 +02:00
|
|
|
ret = TRUE;
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 2 + len;
|
2007-09-18 02:02:58 +02:00
|
|
|
pbEncoded += 2;
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 4, sysTime.wYear);
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
|
|
|
|
if (ret && len > 0)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
|
|
|
|
sysTime.wMinute);
|
2006-05-16 02:01:28 +02:00
|
|
|
if (ret && len > 0)
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
|
2007-09-18 02:02:58 +02:00
|
|
|
sysTime.wSecond);
|
|
|
|
if (ret && len > 0 && (*pbEncoded == '.' ||
|
|
|
|
*pbEncoded == ','))
|
|
|
|
{
|
|
|
|
BYTE digits;
|
|
|
|
|
|
|
|
pbEncoded++;
|
|
|
|
len--;
|
|
|
|
/* workaround macro weirdness */
|
|
|
|
digits = min(len, 3);
|
|
|
|
CRYPT_TIME_GET_DIGITS(pbEncoded, len, digits,
|
|
|
|
sysTime.wMilliseconds);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2007-09-18 02:02:58 +02:00
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
|
|
|
|
&sysTime);
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = sizeof(FILETIME);
|
2007-09-18 02:04:40 +02:00
|
|
|
else if ((ret = CRYPT_DecodeCheckSpace(pcbStructInfo,
|
|
|
|
sizeof(FILETIME))))
|
2009-01-19 10:23:15 +01:00
|
|
|
ret = SystemTimeToFileTime(&sysTime, pvStructInfo);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 02:02:58 +02:00
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeChoiceOfTimeInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
InternalDecodeFunc decode = NULL;
|
|
|
|
|
|
|
|
if (pbEncoded[0] == ASN_UTCTIME)
|
|
|
|
decode = CRYPT_AsnDecodeUtcTimeInternal;
|
|
|
|
else if (pbEncoded[0] == ASN_GENERALTIME)
|
|
|
|
decode = CRYPT_AsnDecodeGeneralizedTime;
|
|
|
|
if (decode)
|
|
|
|
ret = decode(pbEncoded, cbEncoded, dwFlags, pvStructInfo,
|
|
|
|
pcbStructInfo, pcbDecoded);
|
|
|
|
else
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
2006-05-16 02:01:28 +02:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeChoiceOfTime(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0;
|
2007-09-18 02:02:58 +02:00
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeChoiceOfTimeInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, &bytesNeeded, NULL);
|
|
|
|
if (ret)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-09-18 02:02:58 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
|
|
|
ret = CRYPT_AsnDecodeChoiceOfTimeInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
&bytesNeeded, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, pvStructInfo);
|
2007-09-18 02:02:58 +02:00
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeSequenceOfAny(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
if (pbEncoded[0] == ASN_SEQUENCEOF)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, dataLen, remainingLen, cValue;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
BYTE lenBytes;
|
|
|
|
const BYTE *ptr;
|
|
|
|
|
|
|
|
lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
|
|
|
bytesNeeded = sizeof(CRYPT_SEQUENCE_OF_ANY);
|
|
|
|
cValue = 0;
|
|
|
|
ptr = pbEncoded + 1 + lenBytes;
|
|
|
|
remainingLen = dataLen;
|
|
|
|
while (ret && remainingLen)
|
|
|
|
{
|
|
|
|
DWORD nextLen;
|
|
|
|
|
|
|
|
ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
|
|
|
|
|
|
|
|
remainingLen -= 1 + nextLenBytes + nextLen;
|
|
|
|
ptr += 1 + nextLenBytes + nextLen;
|
|
|
|
bytesNeeded += sizeof(CRYPT_DER_BLOB);
|
|
|
|
if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
|
|
|
|
bytesNeeded += 1 + nextLenBytes + nextLen;
|
|
|
|
cValue++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_SEQUENCE_OF_ANY *seq;
|
|
|
|
BYTE *nextPtr;
|
|
|
|
DWORD i;
|
|
|
|
|
2008-08-20 12:21:24 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
2006-05-16 02:01:28 +02:00
|
|
|
pvStructInfo, pcbStructInfo, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
seq = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
seq->cValue = cValue;
|
|
|
|
seq->rgValue = (CRYPT_DER_BLOB *)((BYTE *)seq +
|
|
|
|
sizeof(*seq));
|
|
|
|
nextPtr = (BYTE *)seq->rgValue +
|
|
|
|
cValue * sizeof(CRYPT_DER_BLOB);
|
|
|
|
ptr = pbEncoded + 1 + lenBytes;
|
|
|
|
remainingLen = dataLen;
|
|
|
|
i = 0;
|
|
|
|
while (ret && remainingLen)
|
|
|
|
{
|
|
|
|
DWORD nextLen;
|
|
|
|
|
|
|
|
ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
|
|
|
|
|
|
|
|
seq->rgValue[i].cbData = 1 + nextLenBytes +
|
|
|
|
nextLen;
|
|
|
|
if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
|
|
|
|
seq->rgValue[i].pbData = (BYTE *)ptr;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
seq->rgValue[i].pbData = nextPtr;
|
|
|
|
memcpy(nextPtr, ptr, 1 + nextLenBytes +
|
|
|
|
nextLen);
|
|
|
|
nextPtr += 1 + nextLenBytes + nextLen;
|
|
|
|
}
|
|
|
|
remainingLen -= 1 + nextLenBytes + nextLen;
|
|
|
|
ptr += 1 + nextLenBytes + nextLen;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, seq);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
2006-06-19 21:17:04 +02:00
|
|
|
ret = FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeDistPointName(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2006-06-19 21:17:04 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (pbEncoded[0] == (ASN_CONTEXT | ASN_CONSTRUCTOR | 0))
|
|
|
|
{
|
2012-10-11 22:58:33 +02:00
|
|
|
DWORD bytesNeeded = 0, dataLen;
|
2006-06-19 21:17:04 +02:00
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = {
|
2009-10-14 21:58:49 +02:00
|
|
|
ASN_CONTEXT | ASN_CONSTRUCTOR | 0,
|
|
|
|
offsetof(CRL_DIST_POINT_NAME, u.FullName.cAltEntry),
|
|
|
|
offsetof(CRL_DIST_POINT_NAME, u.FullName.rgAltEntry),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CRL_DIST_POINT_NAME, u),
|
2009-10-14 21:58:49 +02:00
|
|
|
CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
|
2006-06-19 21:17:04 +02:00
|
|
|
offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
|
|
|
|
BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
|
2008-08-16 01:19:11 +02:00
|
|
|
DWORD nameLen;
|
2006-06-19 21:17:04 +02:00
|
|
|
|
|
|
|
if (dataLen)
|
|
|
|
{
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc,
|
2006-06-19 21:17:04 +02:00
|
|
|
pbEncoded + 1 + lenBytes, cbEncoded - 1 - lenBytes,
|
2009-10-14 22:07:36 +02:00
|
|
|
dwFlags, NULL, NULL, &nameLen, NULL);
|
2009-10-14 22:01:00 +02:00
|
|
|
bytesNeeded = sizeof(CRL_DIST_POINT_NAME) + nameLen -
|
|
|
|
FINALMEMBERSIZE(CRL_DIST_POINT_NAME, u);
|
2006-06-19 21:17:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
bytesNeeded = sizeof(CRL_DIST_POINT_NAME);
|
2007-09-18 02:18:53 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
*pcbDecoded = 1 + lenBytes + dataLen;
|
2006-06-19 21:17:04 +02:00
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CRL_DIST_POINT_NAME *name = pvStructInfo;
|
2006-06-19 21:17:04 +02:00
|
|
|
|
2008-08-14 21:48:59 +02:00
|
|
|
*pcbStructInfo = bytesNeeded;
|
2006-06-19 21:17:04 +02:00
|
|
|
if (dataLen)
|
|
|
|
{
|
|
|
|
name->dwDistPointNameChoice = CRL_DIST_POINT_FULL_NAME;
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc,
|
2006-06-19 21:17:04 +02:00
|
|
|
pbEncoded + 1 + lenBytes, cbEncoded - 1 - lenBytes,
|
2009-10-14 22:07:36 +02:00
|
|
|
dwFlags, NULL, &name->u.FullName.cAltEntry, &nameLen,
|
|
|
|
NULL);
|
2006-06-19 21:17:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
name->dwDistPointNameChoice = CRL_DIST_POINT_NO_NAME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeDistPoint(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_DIST_POINT,
|
2006-06-19 21:17:04 +02:00
|
|
|
DistPointName), CRYPT_AsnDecodeDistPointName,
|
2006-05-16 02:01:28 +02:00
|
|
|
sizeof(CRL_DIST_POINT_NAME), TRUE, TRUE, offsetof(CRL_DIST_POINT,
|
|
|
|
DistPointName.u.FullName.rgAltEntry), 0 },
|
|
|
|
{ ASN_CONTEXT | 1, offsetof(CRL_DIST_POINT, ReasonFlags),
|
|
|
|
CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CRL_DIST_POINT, ReasonFlags.pbData), 0 },
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 2, offsetof(CRL_DIST_POINT, CRLIssuer),
|
|
|
|
CRYPT_AsnDecodeAltNameInternal, sizeof(CERT_ALT_NAME_INFO), TRUE, TRUE,
|
|
|
|
offsetof(CRL_DIST_POINT, CRLIssuer.rgAltEntry), 0 },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
CRL_DIST_POINT *point = pvStructInfo;
|
2006-05-16 02:01:28 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
2008-11-20 23:17:59 +01:00
|
|
|
pcbDecoded, point ? point->DistPointName.u.FullName.rgAltEntry : NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCRLDistPoints(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRL_DIST_POINTS_INFO, cDistPoint),
|
|
|
|
offsetof(CRL_DIST_POINTS_INFO, rgDistPoint),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CRL_DIST_POINTS_INFO),
|
2006-05-16 02:01:28 +02:00
|
|
|
CRYPT_AsnDecodeDistPoint, sizeof(CRL_DIST_POINT), TRUE,
|
|
|
|
offsetof(CRL_DIST_POINT, DistPointName.u.FullName.rgAltEntry) };
|
2011-11-21 16:52:31 +01:00
|
|
|
CRL_DIST_POINTS_INFO *info = pvStructInfo;
|
2009-10-14 22:06:20 +02:00
|
|
|
|
2011-11-21 16:52:31 +01:00
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
info->rgDistPoint = (CRL_DIST_POINT *)(info + 1);
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
2009-10-14 22:09:10 +02:00
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeEnhancedKeyUsage(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-05-16 02:01:28 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_ENHKEY_USAGE, cUsageIdentifier),
|
|
|
|
offsetof(CERT_ENHKEY_USAGE, rgpszUsageIdentifier),
|
2009-10-14 22:01:00 +02:00
|
|
|
sizeof(CERT_ENHKEY_USAGE),
|
2006-05-16 02:01:28 +02:00
|
|
|
CRYPT_AsnDecodeOidInternal, sizeof(LPSTR), TRUE, 0 };
|
2011-11-21 16:52:31 +01:00
|
|
|
CERT_ENHKEY_USAGE *usage = pvStructInfo;
|
2009-10-14 22:06:47 +02:00
|
|
|
|
2011-11-21 16:52:31 +01:00
|
|
|
if (pvStructInfo && !(dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
usage->rgpszUsageIdentifier = (LPSTR *)(usage + 1);
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
2009-10-14 22:09:10 +02:00
|
|
|
dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-06-20 00:12:51 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeIssuingDistPoint(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
2006-06-20 00:12:51 +02:00
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_ISSUING_DIST_POINT,
|
|
|
|
DistPointName), CRYPT_AsnDecodeDistPointName,
|
|
|
|
sizeof(CRL_DIST_POINT_NAME), TRUE, TRUE,
|
|
|
|
offsetof(CRL_ISSUING_DIST_POINT,
|
|
|
|
DistPointName.u.FullName.rgAltEntry), 0 },
|
|
|
|
{ ASN_CONTEXT | 1, offsetof(CRL_ISSUING_DIST_POINT,
|
|
|
|
fOnlyContainsUserCerts), CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE,
|
|
|
|
FALSE, 0 },
|
|
|
|
{ ASN_CONTEXT | 2, offsetof(CRL_ISSUING_DIST_POINT,
|
|
|
|
fOnlyContainsCACerts), CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE,
|
|
|
|
FALSE, 0 },
|
|
|
|
{ ASN_CONTEXT | 3, offsetof(CRL_ISSUING_DIST_POINT,
|
|
|
|
OnlySomeReasonFlags), CRYPT_AsnDecodeBitsInternal,
|
|
|
|
sizeof(CRYPT_BIT_BLOB), TRUE, TRUE, offsetof(CRL_ISSUING_DIST_POINT,
|
|
|
|
OnlySomeReasonFlags.pbData), 0 },
|
|
|
|
{ ASN_CONTEXT | 4, offsetof(CRL_ISSUING_DIST_POINT,
|
|
|
|
fIndirectCRL), CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE, FALSE, 0 },
|
|
|
|
};
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
2006-06-20 00:12:51 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-11 02:50:36 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeMaximum(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
2009-10-16 00:27:04 +02:00
|
|
|
BOOL ret;
|
|
|
|
DWORD max, size = sizeof(max);
|
2007-10-11 02:50:36 +02:00
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
|
|
|
if (!cbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_EOD);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (pbEncoded[0] != (ASN_CONTEXT | 1))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-10-16 00:27:04 +02:00
|
|
|
if ((ret = CRYPT_AsnDecodeIntInternal(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
&max, &size, pcbDecoded)))
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded = FINALMEMBERSIZE(CERT_GENERAL_SUBTREE, fMaximum);
|
|
|
|
|
|
|
|
if (!pvStructInfo)
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
else if (*pcbStructInfo < bytesNeeded)
|
|
|
|
{
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-29 02:39:42 +01:00
|
|
|
CERT_GENERAL_SUBTREE *subtree = CONTAINING_RECORD(pvStructInfo,
|
|
|
|
CERT_GENERAL_SUBTREE, fMaximum);
|
2009-10-16 00:27:04 +02:00
|
|
|
|
|
|
|
*pcbStructInfo = bytesNeeded;
|
|
|
|
/* The BOOL is implicit: if the integer is present, then it's
|
|
|
|
* TRUE.
|
|
|
|
*/
|
|
|
|
subtree->fMaximum = TRUE;
|
|
|
|
subtree->dwMaximum = max;
|
|
|
|
}
|
|
|
|
}
|
2007-10-11 02:50:36 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeSubtree(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ 0, offsetof(CERT_GENERAL_SUBTREE, Base),
|
|
|
|
CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE, TRUE,
|
|
|
|
offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL), 0 },
|
|
|
|
{ ASN_CONTEXT | 0, offsetof(CERT_GENERAL_SUBTREE, dwMinimum),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_CONTEXT | 1, offsetof(CERT_GENERAL_SUBTREE, fMaximum),
|
2009-10-16 00:27:04 +02:00
|
|
|
CRYPT_AsnDecodeMaximum, FINALMEMBERSIZE(CERT_GENERAL_SUBTREE, fMaximum),
|
|
|
|
TRUE, FALSE, 0, 0 },
|
2007-10-11 02:50:36 +02:00
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_GENERAL_SUBTREE *subtree = pvStructInfo;
|
2007-10-11 02:50:36 +02:00
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
2009-01-19 10:23:15 +01:00
|
|
|
pcbDecoded, subtree ? subtree->Base.u.pwszURL : NULL);
|
2007-10-11 02:50:36 +02:00
|
|
|
if (pcbDecoded)
|
|
|
|
{
|
|
|
|
TRACE("%d\n", *pcbDecoded);
|
|
|
|
if (*pcbDecoded < cbEncoded)
|
|
|
|
TRACE("%02x %02x\n", *(pbEncoded + *pcbDecoded),
|
|
|
|
*(pbEncoded + *pcbDecoded + 1));
|
|
|
|
}
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:49:13 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePermittedSubtree(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, cPermittedSubtree),
|
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, rgPermittedSubtree),
|
2009-10-14 22:01:00 +02:00
|
|
|
MEMBERSIZE(CERT_NAME_CONSTRAINTS_INFO, cPermittedSubtree,
|
|
|
|
cExcludedSubtree),
|
2009-10-14 21:49:13 +02:00
|
|
|
CRYPT_AsnDecodeSubtree, sizeof(CERT_GENERAL_SUBTREE), TRUE,
|
|
|
|
offsetof(CERT_GENERAL_SUBTREE, Base.u.pwszURL) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:49:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:49:49 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeExcludedSubtree(const BYTE *pbEncoded,
|
2007-10-11 02:50:36 +02:00
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, cExcludedSubtree),
|
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, rgExcludedSubtree),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CERT_NAME_CONSTRAINTS_INFO, cExcludedSubtree),
|
2007-10-11 02:50:36 +02:00
|
|
|
CRYPT_AsnDecodeSubtree, sizeof(CERT_GENERAL_SUBTREE), TRUE,
|
|
|
|
offsetof(CERT_GENERAL_SUBTREE, Base.u.pwszURL) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2007-10-11 02:50:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeNameConstraints(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 0,
|
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, cPermittedSubtree),
|
2009-10-14 21:49:13 +02:00
|
|
|
CRYPT_AsnDecodePermittedSubtree,
|
|
|
|
MEMBERSIZE(CERT_NAME_CONSTRAINTS_INFO, cPermittedSubtree,
|
|
|
|
cExcludedSubtree), TRUE, TRUE,
|
2007-10-11 02:50:36 +02:00
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, rgPermittedSubtree), 0 },
|
|
|
|
{ ASN_CONTEXT | ASN_CONSTRUCTOR | 1,
|
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, cExcludedSubtree),
|
2009-10-14 21:49:49 +02:00
|
|
|
CRYPT_AsnDecodeExcludedSubtree,
|
|
|
|
FINALMEMBERSIZE(CERT_NAME_CONSTRAINTS_INFO, cExcludedSubtree),
|
|
|
|
TRUE, TRUE,
|
2007-10-11 02:50:36 +02:00
|
|
|
offsetof(CERT_NAME_CONSTRAINTS_INFO, rgExcludedSubtree), 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL, NULL);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeIssuerSerialNumber(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2007-06-13 00:02:57 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ 0, offsetof(CERT_ISSUER_SERIAL_NUMBER, Issuer), CRYPT_AsnDecodeDerBlob,
|
|
|
|
sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_ISSUER_SERIAL_NUMBER,
|
|
|
|
Issuer.pbData) },
|
|
|
|
{ ASN_INTEGER, offsetof(CERT_ISSUER_SERIAL_NUMBER, SerialNumber),
|
|
|
|
CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE,
|
|
|
|
TRUE, offsetof(CERT_ISSUER_SERIAL_NUMBER, SerialNumber.pbData), 0 },
|
|
|
|
};
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_ISSUER_SERIAL_NUMBER *issuerSerial = pvStructInfo;
|
2007-06-13 00:02:57 +02:00
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2007-06-13 00:02:57 +02:00
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
2007-09-18 02:13:12 +02:00
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, issuerSerial ? issuerSerial->Issuer.pbData : NULL);
|
2007-06-13 00:02:57 +02:00
|
|
|
if (ret && issuerSerial && !issuerSerial->SerialNumber.cbData)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_CORRUPT);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
static BOOL CRYPT_AsnDecodePKCSSignerInfoInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
2007-07-30 21:01:18 +02:00
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CMSG_SIGNER_INFO *info = pvStructInfo;
|
2007-07-30 21:01:18 +02:00
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(CMSG_SIGNER_INFO, dwVersion),
|
2007-09-18 02:07:35 +02:00
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
2007-07-30 21:01:18 +02:00
|
|
|
{ ASN_SEQUENCEOF, offsetof(CMSG_SIGNER_INFO, Issuer),
|
|
|
|
CRYPT_AsnDecodeIssuerSerialNumber, sizeof(CERT_ISSUER_SERIAL_NUMBER),
|
|
|
|
FALSE, TRUE, offsetof(CMSG_SIGNER_INFO, Issuer.pbData), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CMSG_SIGNER_INFO, HashAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CMSG_SIGNER_INFO, HashAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_CONTEXT | 0,
|
|
|
|
offsetof(CMSG_SIGNER_INFO, AuthAttrs),
|
|
|
|
CRYPT_AsnDecodePKCSAttributesInternal, sizeof(CRYPT_ATTRIBUTES),
|
|
|
|
TRUE, TRUE, offsetof(CMSG_SIGNER_INFO, AuthAttrs.rgAttr), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CMSG_SIGNER_INFO, HashEncryptionAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CMSG_SIGNER_INFO,
|
|
|
|
HashEncryptionAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_OCTETSTRING, offsetof(CMSG_SIGNER_INFO, EncryptedHash),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(CMSG_SIGNER_INFO, EncryptedHash.pbData), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_CONTEXT | 1,
|
|
|
|
offsetof(CMSG_SIGNER_INFO, UnauthAttrs),
|
|
|
|
CRYPT_AsnDecodePKCSAttributesInternal, sizeof(CRYPT_ATTRIBUTES),
|
|
|
|
TRUE, TRUE, offsetof(CMSG_SIGNER_INFO, UnauthAttrs.rgAttr), 0 },
|
|
|
|
};
|
|
|
|
BOOL ret;
|
|
|
|
|
2007-09-18 00:02:54 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
2007-07-30 21:01:18 +02:00
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, info ? info->Issuer.pbData : NULL);
|
2007-07-30 21:01:18 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-13 00:02:57 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnDecodePKCSSignerInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2007-09-18 00:02:54 +02:00
|
|
|
ret = CRYPT_AsnDecodePKCSSignerInfoInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pcbStructInfo, NULL);
|
2007-07-30 21:01:18 +02:00
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
|
|
|
ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, *pcbStructInfo);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CMSG_SIGNER_INFO *info;
|
2007-06-13 00:02:57 +02:00
|
|
|
|
2007-07-30 21:01:18 +02:00
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
info = pvStructInfo;
|
2007-07-30 21:01:18 +02:00
|
|
|
info->Issuer.pbData = ((BYTE *)info +
|
|
|
|
sizeof(CMSG_SIGNER_INFO));
|
2007-09-18 00:02:54 +02:00
|
|
|
ret = CRYPT_AsnDecodePKCSSignerInfoInternal(pbEncoded,
|
|
|
|
cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, info);
|
2007-07-30 21:01:18 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-13 00:02:57 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:48:03 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCMSCertEncoded(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-10-14 21:58:49 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
|
|
|
offsetof(CRYPT_SIGNED_INFO, cCertEncoded),
|
|
|
|
offsetof(CRYPT_SIGNED_INFO, rgCertEncoded),
|
2009-10-14 22:01:00 +02:00
|
|
|
MEMBERSIZE(CRYPT_SIGNED_INFO, cCertEncoded, cCrlEncoded),
|
2009-10-14 21:58:49 +02:00
|
|
|
CRYPT_AsnDecodeCopyBytes,
|
2009-10-14 21:48:03 +02:00
|
|
|
sizeof(CRYPT_DER_BLOB), TRUE, offsetof(CRYPT_DER_BLOB, pbData) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:48:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:48:38 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCMSCrlEncoded(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-10-14 21:58:49 +02:00
|
|
|
struct AsnArrayDescriptor arrayDesc = { 0,
|
|
|
|
offsetof(CRYPT_SIGNED_INFO, cCrlEncoded),
|
|
|
|
offsetof(CRYPT_SIGNED_INFO, rgCrlEncoded),
|
2009-10-14 22:01:00 +02:00
|
|
|
MEMBERSIZE(CRYPT_SIGNED_INFO, cCrlEncoded, content),
|
2009-10-14 21:58:49 +02:00
|
|
|
CRYPT_AsnDecodeCopyBytes, sizeof(CRYPT_DER_BLOB),
|
|
|
|
TRUE, offsetof(CRYPT_DER_BLOB, pbData) };
|
2009-10-14 21:48:38 +02:00
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pvStructInfo ? *pcbStructInfo : 0, pcbDecoded);
|
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2009-10-14 21:48:38 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-14 21:51:37 +02:00
|
|
|
static BOOL CRYPT_AsnDecodeCMSSignerId(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CERT_ID *id = pvStructInfo;
|
2008-08-14 21:51:37 +02:00
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (*pbEncoded == ASN_SEQUENCEOF)
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnDecodeIssuerSerialNumber(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
id ? &id->u.IssuerSerialNumber : NULL, pcbStructInfo, pcbDecoded);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (id)
|
|
|
|
id->dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
|
|
|
|
if (*pcbStructInfo > sizeof(CERT_ISSUER_SERIAL_NUMBER))
|
|
|
|
*pcbStructInfo = sizeof(CERT_ID) + *pcbStructInfo -
|
|
|
|
sizeof(CERT_ISSUER_SERIAL_NUMBER);
|
|
|
|
else
|
|
|
|
*pcbStructInfo = sizeof(CERT_ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (*pbEncoded == (ASN_CONTEXT | 0))
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnDecodeOctetsInternal(pbEncoded, cbEncoded, dwFlags,
|
|
|
|
id ? &id->u.KeyId : NULL, pcbStructInfo, pcbDecoded);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (id)
|
|
|
|
id->dwIdChoice = CERT_ID_KEY_IDENTIFIER;
|
|
|
|
if (*pcbStructInfo > sizeof(CRYPT_DATA_BLOB))
|
|
|
|
*pcbStructInfo = sizeof(CERT_ID) + *pcbStructInfo -
|
|
|
|
sizeof(CRYPT_DATA_BLOB);
|
|
|
|
else
|
|
|
|
*pcbStructInfo = sizeof(CERT_ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeCMSSignerInfoInternal(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
2009-01-19 10:23:15 +01:00
|
|
|
CMSG_CMS_SIGNER_INFO *info = pvStructInfo;
|
2008-08-14 21:51:37 +02:00
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(CMSG_CMS_SIGNER_INFO, dwVersion),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
|
|
|
{ 0, offsetof(CMSG_CMS_SIGNER_INFO, SignerId),
|
|
|
|
CRYPT_AsnDecodeCMSSignerId, sizeof(CERT_ID), FALSE, TRUE,
|
|
|
|
offsetof(CMSG_CMS_SIGNER_INFO, SignerId.u.KeyId.pbData), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CMSG_CMS_SIGNER_INFO, HashAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CMSG_CMS_SIGNER_INFO, HashAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_CONTEXT | 0,
|
|
|
|
offsetof(CMSG_CMS_SIGNER_INFO, AuthAttrs),
|
|
|
|
CRYPT_AsnDecodePKCSAttributesInternal, sizeof(CRYPT_ATTRIBUTES),
|
|
|
|
TRUE, TRUE, offsetof(CMSG_CMS_SIGNER_INFO, AuthAttrs.rgAttr), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CMSG_CMS_SIGNER_INFO, HashEncryptionAlgorithm),
|
|
|
|
CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
|
|
|
|
FALSE, TRUE, offsetof(CMSG_CMS_SIGNER_INFO,
|
|
|
|
HashEncryptionAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_OCTETSTRING, offsetof(CMSG_CMS_SIGNER_INFO, EncryptedHash),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DER_BLOB),
|
|
|
|
FALSE, TRUE, offsetof(CMSG_CMS_SIGNER_INFO, EncryptedHash.pbData), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_CONTEXT | 1,
|
|
|
|
offsetof(CMSG_CMS_SIGNER_INFO, UnauthAttrs),
|
|
|
|
CRYPT_AsnDecodePKCSAttributesInternal, sizeof(CRYPT_ATTRIBUTES),
|
|
|
|
TRUE, TRUE, offsetof(CMSG_CMS_SIGNER_INFO, UnauthAttrs.rgAttr), 0 },
|
|
|
|
};
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, info ? info->SignerId.u.KeyId.pbData : NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnDecodeCMSSignerInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, pvStructInfo, *pcbStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnDecodeCMSSignerInfoInternal(pbEncoded, cbEncoded,
|
|
|
|
dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pcbStructInfo, NULL);
|
|
|
|
if (ret && pvStructInfo)
|
|
|
|
{
|
|
|
|
ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
|
|
|
|
pcbStructInfo, *pcbStructInfo);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CMSG_CMS_SIGNER_INFO *info;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
pvStructInfo = *(BYTE **)pvStructInfo;
|
2009-01-19 10:23:15 +01:00
|
|
|
info = pvStructInfo;
|
2008-08-14 21:51:37 +02:00
|
|
|
info->SignerId.u.KeyId.pbData = ((BYTE *)info +
|
|
|
|
sizeof(CMSG_CMS_SIGNER_INFO));
|
|
|
|
ret = CRYPT_AsnDecodeCMSSignerInfoInternal(pbEncoded,
|
|
|
|
cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, pvStructInfo,
|
|
|
|
pcbStructInfo, NULL);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_DECODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pDecodePara, info);
|
2008-08-14 21:51:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
static BOOL CRYPT_DecodeSignerArray(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
2007-07-26 03:12:16 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
|
2009-10-14 21:58:49 +02:00
|
|
|
offsetof(CRYPT_SIGNED_INFO, cSignerInfo),
|
|
|
|
offsetof(CRYPT_SIGNED_INFO, rgSignerInfo),
|
2009-10-14 22:01:00 +02:00
|
|
|
FINALMEMBERSIZE(CRYPT_SIGNED_INFO, cSignerInfo),
|
2008-08-20 20:57:21 +02:00
|
|
|
CRYPT_AsnDecodeCMSSignerInfoInternal, sizeof(CMSG_CMS_SIGNER_INFO), TRUE,
|
|
|
|
offsetof(CMSG_CMS_SIGNER_INFO, SignerId.u.KeyId.pbData) };
|
2007-07-26 03:12:16 +02:00
|
|
|
|
2007-09-18 02:13:12 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
2007-07-26 03:12:16 +02:00
|
|
|
|
2009-10-14 22:07:36 +02:00
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
2007-07-26 03:12:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-20 20:57:21 +02:00
|
|
|
BOOL CRYPT_AsnDecodeCMSSignedInfo(const BYTE *pbEncoded, DWORD cbEncoded,
|
2007-07-26 03:12:16 +02:00
|
|
|
DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
|
|
|
|
CRYPT_SIGNED_INFO *signedInfo, DWORD *pcbSignedInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
2007-09-18 02:07:35 +02:00
|
|
|
{ ASN_INTEGER, offsetof(CRYPT_SIGNED_INFO, version),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
2007-07-26 03:12:16 +02:00
|
|
|
/* Placeholder for the hash algorithms - redundant with those in the
|
|
|
|
* signers, so just ignore them.
|
|
|
|
*/
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_SETOF, 0, NULL, 0, TRUE, FALSE, 0, 0 },
|
|
|
|
{ ASN_SEQUENCE, offsetof(CRYPT_SIGNED_INFO, content),
|
|
|
|
CRYPT_AsnDecodePKCSContentInfoInternal, sizeof(CRYPT_CONTENT_INFO),
|
|
|
|
FALSE, TRUE, offsetof(CRYPT_SIGNED_INFO, content.pszObjId), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_CONTEXT | 0,
|
2009-10-14 21:48:03 +02:00
|
|
|
offsetof(CRYPT_SIGNED_INFO, cCertEncoded), CRYPT_AsnDecodeCMSCertEncoded,
|
|
|
|
MEMBERSIZE(CRYPT_SIGNED_INFO, cCertEncoded, cCrlEncoded), TRUE, TRUE,
|
2007-07-26 03:12:16 +02:00
|
|
|
offsetof(CRYPT_SIGNED_INFO, rgCertEncoded), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_CONTEXT | 1,
|
2009-10-14 21:48:38 +02:00
|
|
|
offsetof(CRYPT_SIGNED_INFO, cCrlEncoded), CRYPT_AsnDecodeCMSCrlEncoded,
|
|
|
|
MEMBERSIZE(CRYPT_SIGNED_INFO, cCrlEncoded, content), TRUE, TRUE,
|
2007-07-26 03:12:16 +02:00
|
|
|
offsetof(CRYPT_SIGNED_INFO, rgCrlEncoded), 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_SETOF, offsetof(CRYPT_SIGNED_INFO, cSignerInfo),
|
2009-10-14 21:51:30 +02:00
|
|
|
CRYPT_DecodeSignerArray,
|
|
|
|
FINALMEMBERSIZE(CRYPT_SIGNED_INFO, cSignerInfo), TRUE, TRUE,
|
2007-07-26 03:12:16 +02:00
|
|
|
offsetof(CRYPT_SIGNED_INFO, rgSignerInfo), 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, signedInfo, *pcbSignedInfo);
|
|
|
|
|
2007-09-18 02:12:27 +02:00
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, signedInfo, pcbSignedInfo,
|
|
|
|
NULL, NULL);
|
2007-07-26 03:12:16 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-15 15:35:29 +01:00
|
|
|
static BOOL CRYPT_AsnDecodeRecipientInfo(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo, DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
CMSG_KEY_TRANS_RECIPIENT_INFO *info = pvStructInfo;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO, dwVersion),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO,
|
|
|
|
RecipientId.u.IssuerSerialNumber), CRYPT_AsnDecodeIssuerSerialNumber,
|
|
|
|
sizeof(CERT_ISSUER_SERIAL_NUMBER), FALSE, TRUE,
|
|
|
|
offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO,
|
|
|
|
RecipientId.u.IssuerSerialNumber.Issuer.pbData), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO,
|
|
|
|
KeyEncryptionAlgorithm), CRYPT_AsnDecodeAlgorithmId,
|
|
|
|
sizeof(CRYPT_ALGORITHM_IDENTIFIER), FALSE, TRUE,
|
|
|
|
offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO,
|
|
|
|
KeyEncryptionAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_OCTETSTRING, offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO, EncryptedKey),
|
|
|
|
CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DATA_BLOB), FALSE, TRUE,
|
|
|
|
offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO, EncryptedKey.pbData), 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, info ? info->RecipientId.u.IssuerSerialNumber.Issuer.pbData :
|
|
|
|
NULL);
|
|
|
|
if (info)
|
|
|
|
info->RecipientId.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_DecodeRecipientInfoArray(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
|
|
|
|
offsetof(CRYPT_ENVELOPED_DATA, cRecipientInfo),
|
|
|
|
offsetof(CRYPT_ENVELOPED_DATA, rgRecipientInfo),
|
|
|
|
MEMBERSIZE(CRYPT_ENVELOPED_DATA, cRecipientInfo, encryptedContentInfo),
|
|
|
|
CRYPT_AsnDecodeRecipientInfo, sizeof(CMSG_KEY_TRANS_RECIPIENT_INFO), TRUE,
|
|
|
|
offsetof(CMSG_KEY_TRANS_RECIPIENT_INFO,
|
|
|
|
RecipientId.u.IssuerSerialNumber.Issuer.pbData) };
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded,
|
|
|
|
dwFlags, NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnDecodeEncryptedContentInfo(const BYTE *pbEncoded,
|
|
|
|
DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, DWORD *pcbStructInfo,
|
|
|
|
DWORD *pcbDecoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
CRYPT_ENCRYPTED_CONTENT_INFO *info = pvStructInfo;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_OBJECTIDENTIFIER, offsetof(CRYPT_ENCRYPTED_CONTENT_INFO,
|
|
|
|
contentType), CRYPT_AsnDecodeOidInternal, sizeof(LPSTR),
|
|
|
|
FALSE, TRUE, offsetof(CRYPT_ENCRYPTED_CONTENT_INFO,
|
|
|
|
contentType), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CRYPT_ENCRYPTED_CONTENT_INFO,
|
|
|
|
contentEncryptionAlgorithm), CRYPT_AsnDecodeAlgorithmId,
|
|
|
|
sizeof(CRYPT_ALGORITHM_IDENTIFIER), FALSE, TRUE,
|
|
|
|
offsetof(CRYPT_ENCRYPTED_CONTENT_INFO,
|
|
|
|
contentEncryptionAlgorithm.pszObjId), 0 },
|
|
|
|
{ ASN_CONTEXT | 0, offsetof(CRYPT_ENCRYPTED_CONTENT_INFO,
|
|
|
|
encryptedContent), CRYPT_AsnDecodeOctetsInternal,
|
|
|
|
sizeof(CRYPT_DATA_BLOB), TRUE, TRUE,
|
|
|
|
offsetof(CRYPT_ENCRYPTED_CONTENT_INFO, encryptedContent.pbData) },
|
|
|
|
};
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %d, %p\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, *pcbStructInfo, pcbDecoded);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo,
|
|
|
|
pcbDecoded, info ? info->contentType : NULL);
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CRYPT_AsnDecodePKCSEnvelopedData(const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
|
|
|
|
CRYPT_ENVELOPED_DATA *envelopedData, DWORD *pcbEnvelopedData)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnDecodeSequenceItem items[] = {
|
|
|
|
{ ASN_INTEGER, offsetof(CRYPT_ENVELOPED_DATA, version),
|
|
|
|
CRYPT_AsnDecodeIntInternal, sizeof(DWORD), FALSE, FALSE, 0, 0 },
|
|
|
|
{ ASN_CONSTRUCTOR | ASN_SETOF, offsetof(CRYPT_ENVELOPED_DATA,
|
|
|
|
cRecipientInfo), CRYPT_DecodeRecipientInfoArray,
|
|
|
|
MEMBERSIZE(CRYPT_ENVELOPED_DATA, cRecipientInfo, encryptedContentInfo),
|
|
|
|
FALSE, TRUE, offsetof(CRYPT_ENVELOPED_DATA, rgRecipientInfo), 0 },
|
|
|
|
{ ASN_SEQUENCEOF, offsetof(CRYPT_ENVELOPED_DATA, encryptedContentInfo),
|
|
|
|
CRYPT_AsnDecodeEncryptedContentInfo,
|
|
|
|
sizeof(CRYPT_ENCRYPTED_CONTENT_INFO), FALSE, TRUE,
|
|
|
|
offsetof(CRYPT_ENVELOPED_DATA, encryptedContentInfo.contentType), 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pDecodePara, envelopedData, *pcbEnvelopedData);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnDecodeSequence(items, sizeof(items) / sizeof(items[0]),
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pDecodePara, envelopedData,
|
|
|
|
pcbEnvelopedData, NULL, NULL);
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-10 20:19:42 +02:00
|
|
|
static CryptDecodeObjectExFunc CRYPT_GetBuiltinDecoder(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
CryptDecodeObjectExFunc decodeFunc = NULL;
|
|
|
|
|
|
|
|
if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
|
|
|
|
&& (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_FILE_NOT_FOUND);
|
2007-08-10 20:19:42 +02:00
|
|
|
return NULL;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2010-01-29 01:39:26 +01:00
|
|
|
if (IS_INTOID(lpszStructType))
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
|
|
|
switch (LOWORD(lpszStructType))
|
|
|
|
{
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_CERT):
|
2006-06-19 21:17:04 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeCertSignedContent;
|
2006-05-16 02:01:28 +02:00
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_CERT_TO_BE_SIGNED):
|
2006-06-19 21:17:04 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeCert;
|
2006-05-16 02:01:28 +02:00
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_CERT_CRL_TO_BE_SIGNED):
|
2006-06-19 21:17:04 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeCRL;
|
2006-05-16 02:01:28 +02:00
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_EXTENSIONS):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeExtensions;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_NAME_VALUE):
|
2006-06-02 22:20:13 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeNameValue;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_NAME):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeName;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_PUBLIC_KEY_INFO):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodePubKeyInfo;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_AUTHORITY_KEY_ID):
|
2006-09-27 06:20:35 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeAuthorityKeyId;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_ALTERNATE_NAME):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeAltName;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_BASIC_CONSTRAINTS):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeBasicConstraints;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_BASIC_CONSTRAINTS2):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
|
|
|
|
break;
|
2008-11-15 03:00:01 +01:00
|
|
|
case LOWORD(X509_CERT_POLICIES):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCertPolicies;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(RSA_CSP_PUBLICKEYBLOB):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeRsaPubKey;
|
|
|
|
break;
|
2015-02-26 21:48:05 +01:00
|
|
|
case LOWORD(PKCS_RSA_PRIVATE_KEY):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeRsaPrivKey;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_UNICODE_NAME):
|
2006-07-26 16:44:47 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeUnicodeName;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(PKCS_ATTRIBUTE):
|
2007-06-13 00:00:01 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodePKCSAttribute;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_UNICODE_NAME_VALUE):
|
2006-07-19 16:44:48 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeUnicodeNameValue;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_OCTET_STRING):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeOctets;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_BITS):
|
|
|
|
case LOWORD(X509_KEY_USAGE):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeBits;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_INTEGER):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeInt;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_MULTI_BYTE_INTEGER):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeInteger;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_MULTI_BYTE_UINT):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeUnsignedInteger;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_ENUMERATED):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeEnumerated;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_CHOICE_OF_TIME):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeChoiceOfTime;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_AUTHORITY_KEY_ID2):
|
2007-06-12 23:39:19 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeAuthorityKeyId2;
|
|
|
|
break;
|
2008-08-27 19:56:56 +02:00
|
|
|
case LOWORD(X509_AUTHORITY_INFO_ACCESS):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAuthorityInfoAccess;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(PKCS_CONTENT_INFO):
|
2007-06-12 23:40:04 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodePKCSContentInfo;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_SEQUENCE_OF_ANY):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeSequenceOfAny;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(PKCS_UTC_TIME):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeUtcTime;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_CRL_DIST_POINTS):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeCRLDistPoints;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_ENHANCED_KEY_USAGE):
|
2006-05-16 02:01:28 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeEnhancedKeyUsage;
|
|
|
|
break;
|
2008-08-27 23:15:57 +02:00
|
|
|
case LOWORD(PKCS_CTL):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCTL;
|
|
|
|
break;
|
2008-08-20 21:11:41 +02:00
|
|
|
case LOWORD(PKCS_SMIME_CAPABILITIES):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeSMIMECapabilities;
|
|
|
|
break;
|
2008-11-14 23:55:35 +01:00
|
|
|
case LOWORD(X509_PKIX_POLICY_QUALIFIER_USERNOTICE):
|
|
|
|
decodeFunc = CRYPT_AsnDecodePolicyQualifierUserNotice;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(PKCS_ATTRIBUTES):
|
2007-06-13 00:01:34 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodePKCSAttributes;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_ISSUING_DIST_POINT):
|
2006-06-20 00:12:51 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeIssuingDistPoint;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(X509_NAME_CONSTRAINTS):
|
2007-10-11 02:50:36 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodeNameConstraints;
|
|
|
|
break;
|
2009-11-06 21:26:59 +01:00
|
|
|
case LOWORD(X509_POLICY_MAPPINGS):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCertPolicyMappings;
|
|
|
|
break;
|
2009-11-06 21:47:15 +01:00
|
|
|
case LOWORD(X509_POLICY_CONSTRAINTS):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCertPolicyConstraints;
|
|
|
|
break;
|
2007-10-17 14:14:36 +02:00
|
|
|
case LOWORD(PKCS7_SIGNER_INFO):
|
2007-06-13 00:02:57 +02:00
|
|
|
decodeFunc = CRYPT_AsnDecodePKCSSignerInfo;
|
|
|
|
break;
|
2008-08-14 21:51:37 +02:00
|
|
|
case LOWORD(CMS_SIGNER_INFO):
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCMSSignerInfo;
|
|
|
|
break;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeExtensions;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeUtcTime;
|
2008-08-20 21:11:41 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_RSA_SMIMECapabilities))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeSMIMECapabilities;
|
2006-09-27 06:20:35 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAuthorityKeyId;
|
2009-11-06 21:26:59 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_LEGACY_POLICY_MAPPINGS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCertPolicyMappings;
|
2007-06-12 23:39:19 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER2))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAuthorityKeyId2;
|
2006-05-16 02:01:28 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeEnumerated;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeBits;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeOctets;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeBasicConstraints;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_CRL_DIST_POINTS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCRLDistPoints;
|
2008-11-15 03:00:01 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CERT_POLICIES))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCertPolicies;
|
2009-11-06 21:26:59 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_POLICY_MAPPINGS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCertPolicyMappings;
|
2009-11-06 21:47:15 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_POLICY_CONSTRAINTS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCertPolicyConstraints;
|
2006-05-16 02:01:28 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_ENHANCED_KEY_USAGE))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeEnhancedKeyUsage;
|
2006-06-20 00:12:51 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_ISSUING_DIST_POINT))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeIssuingDistPoint;
|
2007-10-11 02:50:36 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_NAME_CONSTRAINTS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeNameConstraints;
|
2008-08-27 19:56:56 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_AUTHORITY_INFO_ACCESS))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeAuthorityInfoAccess;
|
2008-11-14 23:55:35 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_PKIX_POLICY_QUALIFIER_USERNOTICE))
|
|
|
|
decodeFunc = CRYPT_AsnDecodePolicyQualifierUserNotice;
|
2008-08-27 23:15:57 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CTL))
|
|
|
|
decodeFunc = CRYPT_AsnDecodeCTL;
|
2007-08-10 20:19:42 +02:00
|
|
|
return decodeFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CryptDecodeObjectFunc CRYPT_LoadDecoderFunc(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
|
|
|
|
{
|
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
|
|
|
CryptDecodeObjectFunc decodeFunc = NULL;
|
|
|
|
|
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_DECODE_OBJECT_FUNC, 0);
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
|
|
|
|
(void **)&decodeFunc, hFunc);
|
|
|
|
return decodeFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CryptDecodeObjectExFunc CRYPT_LoadDecoderExFunc(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
|
|
|
|
{
|
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
|
|
|
CryptDecodeObjectExFunc decodeFunc = NULL;
|
|
|
|
|
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_DECODE_OBJECT_EX_FUNC, 0);
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
|
|
|
|
(void **)&decodeFunc, hFunc);
|
|
|
|
return decodeFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptDecodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
|
|
|
|
const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo,
|
|
|
|
DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
CryptDecodeObjectFunc pCryptDecodeObject = NULL;
|
|
|
|
CryptDecodeObjectExFunc pCryptDecodeObjectEx = NULL;
|
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
|
|
|
|
|
|
|
TRACE_(crypt)("(0x%08x, %s, %p, %d, 0x%08x, %p, %p)\n", dwCertEncodingType,
|
|
|
|
debugstr_a(lpszStructType), pbEncoded, cbEncoded, dwFlags,
|
|
|
|
pvStructInfo, pcbStructInfo);
|
|
|
|
|
|
|
|
if (!pvStructInfo && !pcbStructInfo)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-08-10 20:20:24 +02:00
|
|
|
if (cbEncoded > MAX_ENCODED_LEN)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_LARGE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-08-10 20:19:42 +02:00
|
|
|
|
|
|
|
if (!(pCryptDecodeObjectEx = CRYPT_GetBuiltinDecoder(dwCertEncodingType,
|
|
|
|
lpszStructType)))
|
|
|
|
{
|
2007-08-10 20:21:37 +02:00
|
|
|
TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
|
|
|
|
debugstr_a(lpszStructType));
|
2007-08-10 20:19:42 +02:00
|
|
|
pCryptDecodeObject = CRYPT_LoadDecoderFunc(dwCertEncodingType,
|
|
|
|
lpszStructType, &hFunc);
|
|
|
|
if (!pCryptDecodeObject)
|
|
|
|
pCryptDecodeObjectEx = CRYPT_LoadDecoderExFunc(dwCertEncodingType,
|
|
|
|
lpszStructType, &hFunc);
|
|
|
|
}
|
|
|
|
if (pCryptDecodeObject)
|
|
|
|
ret = pCryptDecodeObject(dwCertEncodingType, lpszStructType,
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pvStructInfo, pcbStructInfo);
|
|
|
|
else if (pCryptDecodeObjectEx)
|
|
|
|
ret = pCryptDecodeObjectEx(dwCertEncodingType, lpszStructType,
|
|
|
|
pbEncoded, cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL,
|
|
|
|
pvStructInfo, pcbStructInfo);
|
|
|
|
if (hFunc)
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
|
|
|
TRACE_(crypt)("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptDecodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
|
|
|
|
const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
|
|
|
|
PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
CryptDecodeObjectExFunc decodeFunc;
|
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
|
|
|
|
|
|
|
TRACE_(crypt)("(0x%08x, %s, %p, %d, 0x%08x, %p, %p, %p)\n",
|
|
|
|
dwCertEncodingType, debugstr_a(lpszStructType), pbEncoded,
|
|
|
|
cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
|
|
|
|
|
|
|
|
if (!pvStructInfo && !pcbStructInfo)
|
2006-05-16 02:01:28 +02:00
|
|
|
{
|
2007-08-10 20:19:42 +02:00
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
2006-05-16 02:01:28 +02:00
|
|
|
}
|
2007-08-10 20:19:42 +02:00
|
|
|
if (cbEncoded > MAX_ENCODED_LEN)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_LARGE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastError(NOERROR);
|
2010-12-31 12:50:36 +01:00
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
{
|
|
|
|
if (!pvStructInfo)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-08-10 20:19:42 +02:00
|
|
|
*(BYTE **)pvStructInfo = NULL;
|
2010-12-31 12:50:36 +01:00
|
|
|
}
|
2007-08-10 20:19:42 +02:00
|
|
|
decodeFunc = CRYPT_GetBuiltinDecoder(dwCertEncodingType, lpszStructType);
|
|
|
|
if (!decodeFunc)
|
2007-08-10 20:21:37 +02:00
|
|
|
{
|
|
|
|
TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
|
|
|
|
debugstr_a(lpszStructType));
|
2007-08-10 20:19:42 +02:00
|
|
|
decodeFunc = CRYPT_LoadDecoderExFunc(dwCertEncodingType, lpszStructType,
|
|
|
|
&hFunc);
|
2007-08-10 20:21:37 +02:00
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
if (decodeFunc)
|
|
|
|
ret = decodeFunc(dwCertEncodingType, lpszStructType, pbEncoded,
|
|
|
|
cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
|
|
|
|
else
|
2007-08-10 20:18:33 +02:00
|
|
|
{
|
2007-08-10 20:19:42 +02:00
|
|
|
CryptDecodeObjectFunc pCryptDecodeObject =
|
|
|
|
CRYPT_LoadDecoderFunc(dwCertEncodingType, lpszStructType, &hFunc);
|
2007-08-10 20:18:33 +02:00
|
|
|
|
|
|
|
/* Try CryptDecodeObject function. Don't call CryptDecodeObject
|
|
|
|
* directly, as that could cause an infinite loop.
|
|
|
|
*/
|
|
|
|
if (pCryptDecodeObject)
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
|
|
|
|
{
|
|
|
|
ret = pCryptDecodeObject(dwCertEncodingType, lpszStructType,
|
|
|
|
pbEncoded, cbEncoded, dwFlags, NULL, pcbStructInfo);
|
|
|
|
if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
|
|
|
|
pvStructInfo, pcbStructInfo, *pcbStructInfo)))
|
2011-01-18 19:29:58 +01:00
|
|
|
{
|
2007-08-10 20:19:42 +02:00
|
|
|
ret = pCryptDecodeObject(dwCertEncodingType,
|
|
|
|
lpszStructType, pbEncoded, cbEncoded, dwFlags,
|
|
|
|
*(BYTE **)pvStructInfo, pcbStructInfo);
|
2011-01-18 19:29:58 +01:00
|
|
|
if (!ret)
|
|
|
|
CRYPT_FreeSpace(pDecodePara, *(BYTE **)pvStructInfo);
|
|
|
|
}
|
2007-08-10 20:18:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = pCryptDecodeObject(dwCertEncodingType, lpszStructType,
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pvStructInfo, pcbStructInfo);
|
|
|
|
}
|
|
|
|
}
|
2006-05-16 02:01:28 +02:00
|
|
|
if (hFunc)
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
2007-08-10 20:19:42 +02:00
|
|
|
TRACE_(crypt)("returning %d\n", ret);
|
2006-05-16 02:01:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2009-01-27 18:00:03 +01:00
|
|
|
|
|
|
|
BOOL WINAPI PFXIsPFXBlob(CRYPT_DATA_BLOB *pPFX)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2009-01-29 11:07:20 +01:00
|
|
|
TRACE_(crypt)("(%p)\n", pPFX);
|
2009-01-27 18:00:03 +01:00
|
|
|
|
|
|
|
/* A PFX blob is an asn.1-encoded sequence, consisting of at least a
|
|
|
|
* version integer of length 1 (3 encoded byes) and at least one other
|
|
|
|
* datum (two encoded bytes), plus at least two bytes for the outer
|
|
|
|
* sequence. Thus, even an empty PFX blob is at least 7 bytes in length.
|
|
|
|
*/
|
|
|
|
if (pPFX->cbData < 7)
|
|
|
|
ret = FALSE;
|
|
|
|
else if (pPFX->pbData[0] == ASN_SEQUENCE)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
if ((ret = CRYPT_GetLengthIndefinite(pPFX->pbData, pPFX->cbData, &len)))
|
|
|
|
{
|
|
|
|
BYTE lenLen = GET_LEN_BYTES(pPFX->pbData[1]);
|
|
|
|
|
|
|
|
/* Need at least three bytes for the integer version */
|
|
|
|
if (pPFX->cbData < 1 + lenLen + 3)
|
|
|
|
ret = FALSE;
|
|
|
|
else if (pPFX->pbData[1 + lenLen] != ASN_INTEGER || /* Tag */
|
|
|
|
pPFX->pbData[1 + lenLen + 1] != 1 || /* Definite length */
|
|
|
|
pPFX->pbData[1 + lenLen + 2] != 3) /* PFX version */
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-27 18:00:25 +01:00
|
|
|
|
|
|
|
HCERTSTORE WINAPI PFXImportCertStore(CRYPT_DATA_BLOB *pPFX, LPCWSTR szPassword,
|
|
|
|
DWORD dwFlags)
|
|
|
|
{
|
2009-01-29 11:07:20 +01:00
|
|
|
FIXME_(crypt)("(%p, %p, %08x): stub\n", pPFX, szPassword, dwFlags);
|
2009-01-27 18:00:25 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-03 13:50:38 +02:00
|
|
|
|
|
|
|
BOOL WINAPI PFXVerifyPassword(CRYPT_DATA_BLOB *pPFX, LPCWSTR szPassword,
|
|
|
|
DWORD dwFlags)
|
|
|
|
{
|
|
|
|
FIXME_(crypt)("(%p, %p, %08x): stub\n", pPFX, szPassword, dwFlags);
|
|
|
|
return FALSE;
|
|
|
|
}
|