2005-05-20 21:37:26 +02:00
|
|
|
/*
|
2008-08-14 21:50:57 +02:00
|
|
|
* Copyright 2005-2008 Juan Lang
|
2005-05-20 21:37:26 +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
|
2005-06-17 11:50:37 +02:00
|
|
|
*
|
2006-05-16 02:01:28 +02:00
|
|
|
* This file implements ASN.1 DER encoding 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.
|
2005-06-17 11:50:37 +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"
|
2005-05-20 21:37:26 +02:00
|
|
|
*/
|
2006-02-07 16:31:21 +01:00
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
#include <assert.h>
|
2005-05-20 21:37:26 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2005-06-13 13:39:35 +02:00
|
|
|
#include <stdlib.h>
|
2005-08-29 16:12:44 +02:00
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
|
2005-05-20 21:37:26 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wincrypt.h"
|
2005-06-02 12:34:03 +02:00
|
|
|
#include "snmp.h"
|
2005-05-20 21:37:26 +02:00
|
|
|
#include "wine/debug.h"
|
2005-06-27 21:48:24 +02:00
|
|
|
#include "wine/exception.h"
|
2005-11-23 15:12:56 +01:00
|
|
|
#include "crypt32_private.h"
|
2005-05-20 21:37:26 +02:00
|
|
|
|
2007-08-07 18:19:17 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(cryptasn);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(crypt);
|
2005-05-20 21:37:26 +02:00
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
typedef BOOL (WINAPI *CryptEncodeObjectFunc)(DWORD, LPCSTR, const void *,
|
|
|
|
BYTE *, DWORD *);
|
2006-05-16 02:01:28 +02:00
|
|
|
|
|
|
|
/* Prototypes for built-in encoders. They follow the Ex style prototypes.
|
|
|
|
* The dwCertEncodingType and lpszStructType are ignored by the built-in
|
|
|
|
* functions, but the parameters are retained to simplify CryptEncodeObjectEx,
|
|
|
|
* since it must call functions in external DLLs that follow these signatures.
|
2005-07-12 19:00:23 +02:00
|
|
|
*/
|
2007-07-12 23:09:00 +02:00
|
|
|
BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
|
2005-07-12 19:00:23 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2006-02-16 19:39:27 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2005-07-12 19:00:23 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2005-07-07 13:26:50 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2005-07-14 12:14:22 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2007-07-17 16:22:50 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2005-06-27 21:48:24 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2005-09-28 20:12:29 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2005-07-12 19:00:23 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2008-08-27 22:21:50 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePKCSAttributes(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
2005-07-12 19:00:23 +02:00
|
|
|
|
2010-08-15 21:15:26 +02:00
|
|
|
BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara,
|
2007-07-12 23:09:00 +02:00
|
|
|
BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded)
|
2005-06-08 20:31:21 +02:00
|
|
|
{
|
2005-06-10 21:28:58 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2005-06-08 20:31:21 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
{
|
|
|
|
if (pEncodePara && pEncodePara->pfnAlloc)
|
|
|
|
*(BYTE **)pbEncoded = pEncodePara->pfnAlloc(bytesNeeded);
|
|
|
|
else
|
|
|
|
*(BYTE **)pbEncoded = LocalAlloc(0, bytesNeeded);
|
|
|
|
if (!*(BYTE **)pbEncoded)
|
2005-06-10 21:28:58 +02:00
|
|
|
ret = FALSE;
|
2005-06-13 13:39:35 +02:00
|
|
|
else
|
|
|
|
*pcbEncoded = bytesNeeded;
|
2005-06-08 20:31:21 +02:00
|
|
|
}
|
2005-06-10 21:28:58 +02:00
|
|
|
else if (bytesNeeded > *pcbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2006-07-18 04:44:54 +02:00
|
|
|
else
|
|
|
|
*pcbEncoded = bytesNeeded;
|
2005-06-10 21:28:58 +02:00
|
|
|
return ret;
|
2005-06-08 20:31:21 +02:00
|
|
|
}
|
|
|
|
|
2011-01-19 04:17:46 +01:00
|
|
|
static void CRYPT_FreeSpace(const CRYPT_ENCODE_PARA *pEncodePara, LPVOID pv)
|
|
|
|
{
|
|
|
|
if (pEncodePara && pEncodePara->pfnFree)
|
|
|
|
pEncodePara->pfnFree(pv);
|
|
|
|
else
|
|
|
|
LocalFree(pv);
|
|
|
|
}
|
|
|
|
|
2007-07-12 23:09:00 +02:00
|
|
|
BOOL CRYPT_EncodeLen(DWORD len, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
|
|
|
DWORD bytesNeeded, significantBytes = 0;
|
|
|
|
|
|
|
|
if (len <= 0x7f)
|
|
|
|
bytesNeeded = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD temp;
|
|
|
|
|
|
|
|
for (temp = len, significantBytes = sizeof(temp); !(temp & 0xff000000);
|
|
|
|
temp <<= 8, significantBytes--)
|
|
|
|
;
|
|
|
|
bytesNeeded = significantBytes + 1;
|
|
|
|
}
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (*pcbEncoded < bytesNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (len <= 0x7f)
|
|
|
|
*pbEncoded = (BYTE)len;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
*pbEncoded++ = significantBytes | 0x80;
|
|
|
|
for (i = 0; i < significantBytes; i++)
|
|
|
|
{
|
|
|
|
*(pbEncoded + significantBytes - i - 1) = (BYTE)(len & 0xff);
|
|
|
|
len >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-07-12 23:09:00 +02:00
|
|
|
BOOL WINAPI CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType,
|
2005-07-12 19:00:23 +02:00
|
|
|
struct AsnEncodeSequenceItem items[], DWORD cItem, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2005-07-12 19:00:23 +02:00
|
|
|
DWORD i, dataLen = 0;
|
2005-07-01 17:40:45 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%p, %d, %08x, %p, %p, %d\n", items, cItem, dwFlags, pEncodePara,
|
2011-12-16 13:35:29 +01:00
|
|
|
pbEncoded, pbEncoded ? *pcbEncoded : 0);
|
2005-07-12 19:00:23 +02:00
|
|
|
for (i = 0, ret = TRUE; ret && i < cItem; i++)
|
|
|
|
{
|
|
|
|
ret = items[i].encodeFunc(dwCertEncodingType, NULL,
|
|
|
|
items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
|
|
|
NULL, &items[i].size);
|
2005-12-13 11:11:25 +01:00
|
|
|
/* Some functions propagate their errors through the size */
|
|
|
|
if (!ret)
|
|
|
|
*pcbEncoded = items[i].size;
|
2005-07-12 19:00:23 +02:00
|
|
|
dataLen += items[i].size;
|
|
|
|
}
|
2005-07-01 17:40:45 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2005-07-12 19:00:23 +02:00
|
|
|
DWORD lenBytes, bytesNeeded;
|
|
|
|
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
2005-07-12 19:00:23 +02:00
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCE;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2005-07-12 19:00:23 +02:00
|
|
|
for (i = 0; ret && i < cItem; i++)
|
|
|
|
{
|
|
|
|
ret = items[i].encodeFunc(dwCertEncodingType, NULL,
|
|
|
|
items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG,
|
2011-01-19 04:17:46 +01:00
|
|
|
NULL, out, &items[i].size);
|
2005-12-13 11:11:25 +01:00
|
|
|
/* Some functions propagate their errors through the size */
|
|
|
|
if (!ret)
|
|
|
|
*pcbEncoded = items[i].size;
|
2011-01-19 04:17:46 +01:00
|
|
|
out += items[i].size;
|
2005-07-12 19:00:23 +02:00
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2005-07-12 19:00:23 +02:00
|
|
|
}
|
2005-07-01 17:40:45 +02:00
|
|
|
}
|
2005-07-12 19:00:23 +02:00
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2005-07-12 19:00:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-12 23:09:00 +02:00
|
|
|
BOOL WINAPI CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType,
|
2005-07-12 19:00:23 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-01-26 11:01:47 +01:00
|
|
|
const struct AsnConstructedItem *item = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
if ((ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
|
|
|
|
item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &len)))
|
|
|
|
{
|
|
|
|
DWORD dataLen, bytesNeeded;
|
|
|
|
|
|
|
|
CRYPT_EncodeLen(len, NULL, &dataLen);
|
|
|
|
bytesNeeded = 1 + dataLen + len;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_CONTEXT | ASN_CONSTRUCTOR | item->tag;
|
|
|
|
CRYPT_EncodeLen(len, out, &dataLen);
|
|
|
|
out += dataLen;
|
2005-07-12 19:00:23 +02:00
|
|
|
ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
|
|
|
|
item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
2011-01-19 04:17:46 +01:00
|
|
|
out, &len);
|
2005-12-13 11:11:25 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
/* Some functions propagate their errors through the size */
|
|
|
|
*pcbEncoded = len;
|
2011-01-19 04:17:46 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2005-12-13 11:11:25 +01:00
|
|
|
}
|
2005-07-01 17:40:45 +02:00
|
|
|
}
|
|
|
|
}
|
2005-12-13 11:11:25 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Some functions propagate their errors through the size */
|
|
|
|
*pcbEncoded = len;
|
|
|
|
}
|
2005-07-12 19:00:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-12-16 12:41:28 +01:00
|
|
|
struct AsnEncodeTagSwappedItem
|
|
|
|
{
|
|
|
|
BYTE tag;
|
|
|
|
const void *pvStructInfo;
|
|
|
|
CryptEncodeObjectExFunc encodeFunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Sort of a wacky hack, it encodes something using the struct
|
|
|
|
* AsnEncodeTagSwappedItem's encodeFunc, then replaces the tag byte with the tag
|
|
|
|
* given in the struct AsnEncodeTagSwappedItem.
|
|
|
|
*/
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeSwapTag(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-01-26 11:01:47 +01:00
|
|
|
const struct AsnEncodeTagSwappedItem *item = pvStructInfo;
|
2005-12-16 12:41:28 +01:00
|
|
|
|
|
|
|
ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
|
|
|
|
item->pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
if (ret && pbEncoded)
|
|
|
|
*pbEncoded = item->tag;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCertVersion(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const DWORD *ver = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
/* CERT_V1 is not encoded */
|
|
|
|
if (*ver == CERT_V1)
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
2005-07-12 19:00:23 +02:00
|
|
|
*pcbEncoded = 0;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct AsnConstructedItem item = { 0, ver, CRYPT_AsnEncodeInt };
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeConstructed(dwCertEncodingType, X509_INTEGER,
|
|
|
|
&item, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_CopyEncodedBlob(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_DER_BLOB *blob = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = blob->cbData;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-20 01:46:37 +02:00
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, blob->cbData)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
if (blob->cbData)
|
|
|
|
memcpy(pbEncoded, blob->pbData, blob->cbData);
|
|
|
|
*pcbEncoded = blob->cbData;
|
|
|
|
}
|
2005-07-12 19:00:23 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeValidity(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
/* This has two filetimes in a row, a NotBefore and a NotAfter */
|
2009-01-26 11:01:47 +01:00
|
|
|
const FILETIME *timePtr = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
2008-05-25 17:38:54 +02:00
|
|
|
{ timePtr, CRYPT_AsnEncodeChoiceOfTime, 0 },
|
|
|
|
{ timePtr + 1, CRYPT_AsnEncodeChoiceOfTime, 0 },
|
2005-07-12 19:00:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2005-07-12 19:00:23 +02:00
|
|
|
pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-17 16:22:50 +02:00
|
|
|
/* Like CRYPT_AsnEncodeAlgorithmId, but encodes parameters as an asn.1 NULL
|
|
|
|
* if they are empty.
|
|
|
|
*/
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeAlgorithmIdWithNullParams(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
|
|
|
|
DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
2007-07-12 23:58:08 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
|
2007-07-12 23:58:08 +02:00
|
|
|
static const BYTE asn1Null[] = { ASN_NULL, 0 };
|
|
|
|
static const CRYPT_DATA_BLOB nullBlob = { sizeof(asn1Null),
|
|
|
|
(LPBYTE)asn1Null };
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ NULL, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (algo->Parameters.cbData)
|
|
|
|
items[1].pvStructInfo = &algo->Parameters;
|
|
|
|
else
|
|
|
|
items[1].pvStructInfo = &nullBlob;
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2007-07-12 23:58:08 +02:00
|
|
|
pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeAlgorithmId(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2005-07-12 19:00:23 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
BOOL ret;
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ &algo->Parameters, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2005-07-12 19:00:23 +02:00
|
|
|
pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_PUBLIC_KEY_INFO *info = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
2009-10-20 22:33:15 +02:00
|
|
|
{ &info->Algorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams, 0 },
|
2005-07-12 19:00:23 +02:00
|
|
|
{ &info->PublicKey, CRYPT_AsnEncodeBits, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
TRACE("Encoding public key with OID %s\n",
|
|
|
|
debugstr_a(info->Algorithm.pszObjId));
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2005-07-12 19:00:23 +02:00
|
|
|
pcbEncoded);
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-07-12 19:00:23 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-07-14 12:14:22 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCert(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_SIGNED_CONTENT_INFO *info = pvStructInfo;
|
2005-07-14 12:14:22 +02:00
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &info->ToBeSigned, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
{ &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
|
|
|
|
{ &info->Signature, CRYPT_AsnEncodeBitsSwapBytes, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ENCODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG)
|
|
|
|
items[2].encodeFunc = CRYPT_AsnEncodeBits;
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2005-07-14 12:14:22 +02:00
|
|
|
pcbEncoded);
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-07-14 12:14:22 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:29:03 +02:00
|
|
|
BOOL WINAPI CRYPT_AsnEncodePubKeyInfoNoNull(DWORD dwCertEncodingType,
|
2009-10-20 22:20:59 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
const CERT_PUBLIC_KEY_INFO *info = pvStructInfo;
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &info->Algorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
|
|
|
|
{ &info->PublicKey, CRYPT_AsnEncodeBits, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
TRACE("Encoding public key with OID %s\n",
|
|
|
|
debugstr_a(info->Algorithm.pszObjId));
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2009-10-20 22:20:59 +02:00
|
|
|
pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
/* Like in Windows, this blithely ignores the validity of the passed-in
|
|
|
|
* CERT_INFO, and just encodes it as-is. The resulting encoded data may not
|
|
|
|
* decode properly, see CRYPT_AsnDecodeCertInfo.
|
|
|
|
*/
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCertInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_INFO *info = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
struct AsnEncodeSequenceItem items[10] = {
|
|
|
|
{ &info->dwVersion, CRYPT_AsnEncodeCertVersion, 0 },
|
|
|
|
{ &info->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
|
|
|
|
{ &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
|
|
|
|
{ &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
{ &info->NotBefore, CRYPT_AsnEncodeValidity, 0 },
|
|
|
|
{ &info->Subject, CRYPT_CopyEncodedBlob, 0 },
|
2009-10-20 22:20:59 +02:00
|
|
|
{ &info->SubjectPublicKeyInfo, CRYPT_AsnEncodePubKeyInfoNoNull, 0 },
|
2005-07-12 19:00:23 +02:00
|
|
|
{ 0 }
|
|
|
|
};
|
2009-11-06 20:21:51 +01:00
|
|
|
struct AsnConstructedItem constructed = { 0 };
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
|
|
|
|
DWORD cItem = 7, cSwapped = 0;
|
2005-07-12 19:00:23 +02:00
|
|
|
|
|
|
|
if (info->IssuerUniqueId.cbData)
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
2009-11-06 20:21:51 +01:00
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->IssuerUniqueId;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
2005-07-12 19:00:23 +02:00
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->SubjectUniqueId.cbData)
|
|
|
|
{
|
2009-11-06 20:21:51 +01:00
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 2;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->SubjectUniqueId;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
2005-07-12 19:00:23 +02:00
|
|
|
cItem++;
|
2005-07-01 17:40:45 +02:00
|
|
|
}
|
2005-07-12 19:00:23 +02:00
|
|
|
if (info->cExtension)
|
|
|
|
{
|
2009-11-06 20:21:51 +01:00
|
|
|
constructed.tag = 3;
|
|
|
|
constructed.pvStructInfo = &info->cExtension;
|
|
|
|
constructed.encodeFunc = CRYPT_AsnEncodeExtensions;
|
|
|
|
items[cItem].pvStructInfo = &constructed;
|
2005-07-12 19:00:23 +02:00
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2005-07-01 17:40:45 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-07-12 19:00:23 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeCRLEntry(const CRL_ENTRY *entry,
|
2005-08-29 16:12:44 +02:00
|
|
|
BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[3] = {
|
|
|
|
{ &entry->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
|
|
|
|
{ &entry->RevocationDate, CRYPT_AsnEncodeChoiceOfTime, 0 },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
DWORD cItem = 2;
|
|
|
|
BOOL ret;
|
|
|
|
|
2011-12-16 13:35:29 +01:00
|
|
|
TRACE("%p, %p, %d\n", entry, pbEncoded, pbEncoded ? *pcbEncoded : 0);
|
2005-08-29 16:12:44 +02:00
|
|
|
|
|
|
|
if (entry->cExtension)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &entry->cExtension;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeExtensions;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2005-08-29 16:12:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCRLEntries(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
2009-06-22 14:53:24 +02:00
|
|
|
const CRL_INFO *info = pvStructInfo;
|
|
|
|
const CRL_ENTRY *rgCRLEntry = info->rgCRLEntry;
|
2005-08-29 16:12:44 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2009-06-22 14:53:24 +02:00
|
|
|
for (i = 0, dataLen = 0; ret && i < info->cCRLEntry; i++)
|
2005-08-29 16:12:44 +02:00
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
dataLen += size;
|
|
|
|
}
|
2008-08-27 20:02:13 +02:00
|
|
|
if (ret)
|
2005-08-29 16:12:44 +02:00
|
|
|
{
|
2008-08-27 20:02:13 +02:00
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
2005-08-29 16:12:44 +02:00
|
|
|
{
|
2008-08-27 20:02:13 +02:00
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
2005-08-29 16:12:44 +02:00
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-08-27 20:02:13 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2009-06-22 14:53:24 +02:00
|
|
|
for (i = 0; i < info->cCRLEntry; i++)
|
2008-08-27 20:02:13 +02:00
|
|
|
{
|
|
|
|
DWORD size = dataLen;
|
2005-08-29 16:12:44 +02:00
|
|
|
|
2011-01-19 04:17:46 +01:00
|
|
|
ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], out, &size);
|
|
|
|
out += size;
|
2008-08-27 20:02:13 +02:00
|
|
|
dataLen -= size;
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2005-08-29 16:12:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCRLVersion(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const DWORD *ver = pvStructInfo;
|
2005-08-29 16:12:44 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
/* CRL_V1 is not encoded */
|
|
|
|
if (*ver == CRL_V1)
|
|
|
|
{
|
|
|
|
*pcbEncoded = 0;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like in Windows, this blithely ignores the validity of the passed-in
|
|
|
|
* CRL_INFO, and just encodes it as-is. The resulting encoded data may not
|
|
|
|
* decode properly, see CRYPT_AsnDecodeCRLInfo.
|
|
|
|
*/
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCRLInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRL_INFO *info = pvStructInfo;
|
2005-08-29 16:12:44 +02:00
|
|
|
struct AsnEncodeSequenceItem items[7] = {
|
|
|
|
{ &info->dwVersion, CRYPT_AsnEncodeCRLVersion, 0 },
|
|
|
|
{ &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
|
|
|
|
{ &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
{ &info->ThisUpdate, CRYPT_AsnEncodeChoiceOfTime, 0 },
|
|
|
|
{ 0 }
|
|
|
|
};
|
2006-06-15 18:48:12 +02:00
|
|
|
struct AsnConstructedItem constructed[1] = { { 0 } };
|
|
|
|
DWORD cItem = 4, cConstructed = 0;
|
2005-08-29 16:12:44 +02:00
|
|
|
|
|
|
|
if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &info->NextUpdate;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->cCRLEntry)
|
|
|
|
{
|
2009-06-22 14:53:24 +02:00
|
|
|
items[cItem].pvStructInfo = info;
|
2005-08-29 16:12:44 +02:00
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeCRLEntries;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->cExtension)
|
|
|
|
{
|
2006-06-15 18:48:12 +02:00
|
|
|
constructed[cConstructed].tag = 0;
|
|
|
|
constructed[cConstructed].pvStructInfo = &info->cExtension;
|
|
|
|
constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeExtensions;
|
|
|
|
items[cItem].pvStructInfo = &constructed[cConstructed];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
|
|
|
|
cConstructed++;
|
2005-08-29 16:12:44 +02:00
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-08-29 16:12:44 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
static BOOL CRYPT_AsnEncodeExtension(CERT_EXTENSION *ext, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
struct AsnEncodeSequenceItem items[3] = {
|
|
|
|
{ ext->pszObjId, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ NULL, NULL, 0 },
|
|
|
|
{ NULL, NULL, 0 },
|
|
|
|
};
|
|
|
|
DWORD cItem = 1;
|
|
|
|
|
2011-12-16 13:35:29 +01:00
|
|
|
TRACE("%p, %p, %d\n", ext, pbEncoded, pbEncoded ? *pcbEncoded : 0);
|
2005-07-12 19:00:23 +02:00
|
|
|
|
|
|
|
if (ext->fCritical)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &ext->fCritical;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
items[cItem].pvStructInfo = &ext->Value;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
|
|
|
|
cItem++;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
|
|
|
|
pbEncoded, pcbEncoded);
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2005-07-01 17:40:45 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_EXTENSIONS *exts = pvStructInfo;
|
2005-07-01 17:40:45 +02:00
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0, dataLen = 0; ret && i < exts->cExtension; i++)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i], NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
dataLen += size;
|
|
|
|
}
|
2008-08-27 20:02:13 +02:00
|
|
|
if (ret)
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
2008-08-27 20:02:13 +02:00
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
2008-08-27 20:02:13 +02:00
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-08-27 20:02:13 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2008-08-27 20:02:13 +02:00
|
|
|
for (i = 0; i < exts->cExtension; i++)
|
|
|
|
{
|
|
|
|
DWORD size = dataLen;
|
2005-07-01 17:40:45 +02:00
|
|
|
|
2008-08-27 20:02:13 +02:00
|
|
|
ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i],
|
2011-01-19 04:17:46 +01:00
|
|
|
out, &size);
|
|
|
|
out += size;
|
2008-08-27 20:02:13 +02:00
|
|
|
dataLen -= size;
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2005-07-01 17:40:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-07-01 17:40:45 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-12 23:09:00 +02:00
|
|
|
BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
|
2005-07-12 19:00:23 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
LPCSTR pszObjId = pvStructInfo;
|
2005-06-16 18:08:50 +02:00
|
|
|
DWORD bytesNeeded = 0, lenBytes;
|
2005-06-13 13:39:35 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
int firstPos = 0;
|
|
|
|
BYTE firstByte = 0;
|
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
TRACE("%s\n", debugstr_a(pszObjId));
|
|
|
|
|
2005-06-13 13:39:35 +02:00
|
|
|
if (pszObjId)
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
int val1, val2;
|
|
|
|
|
2008-12-20 00:40:35 +01:00
|
|
|
if (sscanf(pszObjId, "%d.%d%n", &val1, &val2, &firstPos) != 2)
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_ERROR);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
bytesNeeded++;
|
|
|
|
firstByte = val1 * 40 + val2;
|
|
|
|
ptr = pszObjId + firstPos;
|
2008-12-20 00:40:35 +01:00
|
|
|
if (*ptr == '.')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
firstPos++;
|
|
|
|
}
|
2005-06-13 13:39:35 +02:00
|
|
|
while (ret && *ptr)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
/* note I assume each component is at most 32-bits long in base 2 */
|
|
|
|
if (sscanf(ptr, "%d%n", &val1, &pos) == 1)
|
|
|
|
{
|
|
|
|
if (val1 >= 0x10000000)
|
|
|
|
bytesNeeded += 5;
|
|
|
|
else if (val1 >= 0x200000)
|
|
|
|
bytesNeeded += 4;
|
|
|
|
else if (val1 >= 0x4000)
|
|
|
|
bytesNeeded += 3;
|
|
|
|
else if (val1 >= 0x80)
|
|
|
|
bytesNeeded += 2;
|
|
|
|
else
|
|
|
|
bytesNeeded += 1;
|
|
|
|
ptr += pos;
|
|
|
|
if (*ptr == '.')
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_ASN1_ERROR);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2005-06-16 18:08:50 +02:00
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
2005-06-13 13:39:35 +02:00
|
|
|
}
|
2005-06-16 18:08:50 +02:00
|
|
|
else
|
|
|
|
lenBytes = 1;
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
2005-06-13 13:39:35 +02:00
|
|
|
if (pbEncoded)
|
|
|
|
{
|
2005-07-12 19:00:23 +02:00
|
|
|
if (*pcbEncoded < bytesNeeded)
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pbEncoded++ = ASN_OBJECTIDENTIFIER;
|
2005-06-16 18:08:50 +02:00
|
|
|
CRYPT_EncodeLen(bytesNeeded - 1 - lenBytes, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
2005-06-13 13:39:35 +02:00
|
|
|
if (pszObjId)
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
int val, pos;
|
|
|
|
|
|
|
|
*pbEncoded++ = firstByte;
|
|
|
|
ptr = pszObjId + firstPos;
|
|
|
|
while (ret && *ptr)
|
|
|
|
{
|
|
|
|
sscanf(ptr, "%d%n", &val, &pos);
|
|
|
|
{
|
|
|
|
unsigned char outBytes[5];
|
|
|
|
int numBytes, i;
|
|
|
|
|
|
|
|
if (val >= 0x10000000)
|
|
|
|
numBytes = 5;
|
|
|
|
else if (val >= 0x200000)
|
|
|
|
numBytes = 4;
|
|
|
|
else if (val >= 0x4000)
|
|
|
|
numBytes = 3;
|
|
|
|
else if (val >= 0x80)
|
|
|
|
numBytes = 2;
|
|
|
|
else
|
|
|
|
numBytes = 1;
|
|
|
|
for (i = numBytes; i > 0; i--)
|
|
|
|
{
|
|
|
|
outBytes[i - 1] = val & 0x7f;
|
|
|
|
val >>= 7;
|
|
|
|
}
|
|
|
|
for (i = 0; i < numBytes - 1; i++)
|
|
|
|
*pbEncoded++ = outBytes[i] | 0x80;
|
|
|
|
*pbEncoded++ = outBytes[i];
|
|
|
|
ptr += pos;
|
|
|
|
if (*ptr == '.')
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-20 01:46:37 +02:00
|
|
|
static BOOL CRYPT_AsnEncodeStringCoerce(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
BYTE tag, DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-20 01:46:37 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCSTR str = (LPCSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, encodedLen;
|
|
|
|
|
2008-09-23 23:38:33 +02:00
|
|
|
encodedLen = value->Value.cbData ? value->Value.cbData : strlen(str);
|
2006-07-20 01:46:37 +02:00
|
|
|
CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + encodedLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = tag;
|
|
|
|
CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
memcpy(pbEncoded, str, encodedLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnEncodeBMPString(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-20 01:46:37 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCWSTR str = (LPCWSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, strLen;
|
|
|
|
|
2007-10-20 23:24:10 +02:00
|
|
|
if (value->Value.cbData)
|
|
|
|
strLen = value->Value.cbData / sizeof(WCHAR);
|
|
|
|
else if (value->Value.pbData)
|
|
|
|
strLen = lstrlenW(str);
|
|
|
|
else
|
|
|
|
strLen = 0;
|
2006-07-20 01:46:37 +02:00
|
|
|
CRYPT_EncodeLen(strLen * 2, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + strLen * 2;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_BMPSTRING;
|
|
|
|
CRYPT_EncodeLen(strLen * 2, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
for (i = 0; i < strLen; i++)
|
|
|
|
{
|
|
|
|
*pbEncoded++ = (str[i] & 0xff00) >> 8;
|
|
|
|
*pbEncoded++ = str[i] & 0x00ff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnEncodeUTF8String(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-20 01:46:37 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCWSTR str = (LPCWSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, encodedLen, strLen;
|
|
|
|
|
2011-02-10 17:52:21 +01:00
|
|
|
if (value->Value.cbData)
|
|
|
|
strLen = value->Value.cbData / sizeof(WCHAR);
|
|
|
|
else if (str)
|
2020-11-09 11:41:21 +01:00
|
|
|
strLen = lstrlenW(str);
|
2011-02-10 17:52:21 +01:00
|
|
|
else
|
|
|
|
strLen = 0;
|
2006-07-20 01:46:37 +02:00
|
|
|
encodedLen = WideCharToMultiByte(CP_UTF8, 0, str, strLen, NULL, 0, NULL,
|
|
|
|
NULL);
|
|
|
|
CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + encodedLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_UTF8STRING;
|
|
|
|
CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, str, strLen, (LPSTR)pbEncoded,
|
|
|
|
bytesNeeded - lenBytes - 1, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-13 13:39:35 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeNameValue(DWORD dwCertEncodingType,
|
2006-06-02 22:20:13 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2006-06-02 22:20:13 +02:00
|
|
|
__TRY
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_NAME_VALUE *value = pvStructInfo;
|
2006-06-02 22:20:13 +02:00
|
|
|
|
|
|
|
switch (value->dwValueType)
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
2006-07-20 01:46:37 +02:00
|
|
|
case CERT_RDN_ANY_TYPE:
|
|
|
|
/* explicitly disallowed */
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
case CERT_RDN_ENCODED_BLOB:
|
|
|
|
ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL,
|
|
|
|
&value->Value, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_OCTET_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_OCTETSTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
2006-06-02 22:20:13 +02:00
|
|
|
case CERT_RDN_NUMERIC_STRING:
|
2006-07-20 01:46:37 +02:00
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_NUMERICSTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2006-06-02 22:20:13 +02:00
|
|
|
break;
|
|
|
|
case CERT_RDN_PRINTABLE_STRING:
|
2006-07-20 01:46:37 +02:00
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_PRINTABLESTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_TELETEX_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_T61STRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2006-06-02 22:20:13 +02:00
|
|
|
break;
|
2006-07-20 01:46:37 +02:00
|
|
|
case CERT_RDN_VIDEOTEX_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value,
|
|
|
|
ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2006-06-02 22:20:13 +02:00
|
|
|
break;
|
|
|
|
case CERT_RDN_IA5_STRING:
|
2006-07-20 01:46:37 +02:00
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_IA5STRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2006-06-02 22:20:13 +02:00
|
|
|
break;
|
2006-07-20 01:46:37 +02:00
|
|
|
case CERT_RDN_GRAPHIC_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GRAPHICSTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_VISIBLE_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_VISIBLESTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_GENERAL_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GENERALSTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_UNIVERSAL_STRING:
|
|
|
|
FIXME("CERT_RDN_UNIVERSAL_STRING: unimplemented\n");
|
|
|
|
SetLastError(CRYPT_E_ASN1_CHOICE);
|
2006-07-18 15:41:21 +02:00
|
|
|
ret = FALSE;
|
|
|
|
break;
|
2006-07-20 01:46:37 +02:00
|
|
|
case CERT_RDN_BMP_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_UTF8_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
2006-06-02 22:20:13 +02:00
|
|
|
default:
|
2006-07-20 01:46:37 +02:00
|
|
|
SetLastError(CRYPT_E_ASN1_CHOICE);
|
2006-07-18 15:41:21 +02:00
|
|
|
ret = FALSE;
|
2005-06-13 13:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
2006-06-02 22:20:13 +02:00
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2005-06-13 13:39:35 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeRdnAttr(DWORD dwCertEncodingType,
|
2009-06-12 19:27:56 +02:00
|
|
|
const CERT_RDN_ATTR *attr, CryptEncodeObjectExFunc nameValueEncodeFunc,
|
2006-07-26 16:44:47 +02:00
|
|
|
BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded = 0, lenBytes, size;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL, attr->pszObjId,
|
|
|
|
0, NULL, NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
bytesNeeded += size;
|
|
|
|
/* hack: a CERT_RDN_ATTR is identical to a CERT_NAME_VALUE beginning
|
|
|
|
* with dwValueType, so "cast" it to get its encoded size
|
|
|
|
*/
|
2009-01-26 11:01:47 +01:00
|
|
|
ret = nameValueEncodeFunc(dwCertEncodingType, NULL, &attr->dwValueType,
|
|
|
|
0, NULL, NULL, &size);
|
2006-07-26 16:44:47 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
bytesNeeded += size;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (pbEncoded)
|
|
|
|
{
|
|
|
|
if (*pcbEncoded < bytesNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pbEncoded++ = ASN_SEQUENCE;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
|
|
|
|
&lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
size = bytesNeeded - 1 - lenBytes;
|
|
|
|
ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
|
|
|
|
attr->pszObjId, 0, NULL, pbEncoded, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
pbEncoded += size;
|
|
|
|
size = bytesNeeded - 1 - lenBytes - size;
|
2009-01-26 11:01:47 +01:00
|
|
|
ret = nameValueEncodeFunc(dwCertEncodingType, NULL,
|
|
|
|
&attr->dwValueType, 0, NULL, pbEncoded, &size);
|
2006-07-26 16:44:47 +02:00
|
|
|
if (!ret)
|
|
|
|
*pcbEncoded = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Have to propagate index of failing character */
|
|
|
|
*pcbEncoded = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-11-09 11:41:21 +01:00
|
|
|
static int __cdecl BLOBComp(const void *l, const void *r)
|
2006-07-26 16:44:47 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_DER_BLOB *a = l, *b = r;
|
2006-07-26 16:44:47 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!(ret = memcmp(a->pbData, b->pbData, min(a->cbData, b->cbData))))
|
|
|
|
ret = a->cbData - b->cbData;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-12 23:40:52 +02:00
|
|
|
/* This encodes a SET OF, which in DER must be lexicographically sorted.
|
2006-07-26 16:44:47 +02:00
|
|
|
*/
|
2007-06-12 23:40:52 +02:00
|
|
|
static BOOL WINAPI CRYPT_DEREncodeSet(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_BLOB_ARRAY *set = pvStructInfo;
|
2007-06-12 23:40:52 +02:00
|
|
|
DWORD bytesNeeded = 0, lenBytes, i;
|
2007-06-13 00:01:04 +02:00
|
|
|
BOOL ret;
|
2007-06-12 23:40:52 +02:00
|
|
|
|
2007-10-19 21:36:06 +02:00
|
|
|
for (i = 0; i < set->cBlob; i++)
|
|
|
|
bytesNeeded += set->rgBlob[i].cbData;
|
2007-06-12 23:40:52 +02:00
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
2007-06-13 00:01:04 +02:00
|
|
|
if (!pbEncoded)
|
2007-06-12 23:40:52 +02:00
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2007-06-13 00:01:04 +02:00
|
|
|
else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2007-10-19 21:36:06 +02:00
|
|
|
qsort(set->rgBlob, set->cBlob, sizeof(CRYPT_DER_BLOB), BLOBComp);
|
2007-06-13 00:01:04 +02:00
|
|
|
*pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
2011-01-19 04:19:13 +01:00
|
|
|
for (i = 0; i < set->cBlob; i++)
|
2007-06-13 00:01:04 +02:00
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
memcpy(pbEncoded, set->rgBlob[i].pbData, set->rgBlob[i].cbData);
|
|
|
|
pbEncoded += set->rgBlob[i].cbData;
|
2007-06-13 00:01:04 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-12 23:40:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-24 00:29:15 +02:00
|
|
|
struct DERSetDescriptor
|
|
|
|
{
|
|
|
|
DWORD cItems;
|
|
|
|
const void *items;
|
|
|
|
size_t itemSize;
|
2007-07-26 11:52:23 +02:00
|
|
|
size_t itemOffset;
|
2007-07-24 00:29:15 +02:00
|
|
|
CryptEncodeObjectExFunc encode;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_DEREncodeItemsAsSet(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const struct DERSetDescriptor *desc = pvStructInfo;
|
2007-10-19 21:36:06 +02:00
|
|
|
CRYPT_BLOB_ARRAY setOf = { 0, NULL };
|
2007-07-24 00:29:15 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (desc->cItems)
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
setOf.rgBlob = CryptMemAlloc(desc->cItems * sizeof(CRYPT_DER_BLOB));
|
|
|
|
if (!setOf.rgBlob)
|
2007-07-24 00:29:15 +02:00
|
|
|
ret = FALSE;
|
|
|
|
else
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
setOf.cBlob = desc->cItems;
|
|
|
|
memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
|
2007-07-24 00:29:15 +02:00
|
|
|
}
|
|
|
|
}
|
2007-10-19 21:36:06 +02:00
|
|
|
for (i = 0; ret && i < setOf.cBlob; i++)
|
2007-07-24 00:29:15 +02:00
|
|
|
{
|
|
|
|
ret = desc->encode(dwCertEncodingType, lpszStructType,
|
|
|
|
(const BYTE *)desc->items + i * desc->itemSize + desc->itemOffset,
|
2007-10-19 21:36:06 +02:00
|
|
|
0, NULL, NULL, &setOf.rgBlob[i].cbData);
|
2007-07-24 00:29:15 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
|
|
|
|
if (!setOf.rgBlob[i].pbData)
|
2007-07-24 00:29:15 +02:00
|
|
|
ret = FALSE;
|
|
|
|
else
|
|
|
|
ret = desc->encode(dwCertEncodingType, lpszStructType,
|
|
|
|
(const BYTE *)desc->items + i * desc->itemSize +
|
2007-10-19 21:36:06 +02:00
|
|
|
desc->itemOffset, 0, NULL, setOf.rgBlob[i].pbData,
|
|
|
|
&setOf.rgBlob[i].cbData);
|
2007-07-24 00:29:15 +02:00
|
|
|
}
|
|
|
|
/* Some functions propagate their errors through the size */
|
|
|
|
if (!ret)
|
2007-10-19 21:36:06 +02:00
|
|
|
*pcbEncoded = setOf.rgBlob[i].cbData;
|
2007-07-24 00:29:15 +02:00
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded = 0, lenBytes;
|
|
|
|
|
2007-10-19 21:36:06 +02:00
|
|
|
for (i = 0; i < setOf.cBlob; i++)
|
|
|
|
bytesNeeded += setOf.rgBlob[i].cbData;
|
2007-07-24 00:29:15 +02:00
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2007-10-19 21:36:06 +02:00
|
|
|
qsort(setOf.rgBlob, setOf.cBlob, sizeof(CRYPT_DER_BLOB),
|
2007-07-24 00:29:15 +02:00
|
|
|
BLOBComp);
|
|
|
|
*pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
2007-10-19 21:36:06 +02:00
|
|
|
for (i = 0; i < setOf.cBlob; i++)
|
2007-07-24 00:29:15 +02:00
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
memcpy(pbEncoded, setOf.rgBlob[i].pbData,
|
|
|
|
setOf.rgBlob[i].cbData);
|
|
|
|
pbEncoded += setOf.rgBlob[i].cbData;
|
2007-07-24 00:29:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 21:36:06 +02:00
|
|
|
for (i = 0; i < setOf.cBlob; i++)
|
|
|
|
CryptMemFree(setOf.rgBlob[i].pbData);
|
|
|
|
CryptMemFree(setOf.rgBlob);
|
2007-07-24 00:29:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-06-12 19:27:56 +02:00
|
|
|
static BOOL CRYPT_AsnEncodeRdn(DWORD dwCertEncodingType, const CERT_RDN *rdn,
|
2006-07-26 16:44:47 +02:00
|
|
|
CryptEncodeObjectExFunc nameValueEncodeFunc, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2007-10-19 21:36:06 +02:00
|
|
|
CRYPT_BLOB_ARRAY setOf = { 0, NULL };
|
2006-07-26 16:44:47 +02:00
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2007-06-12 23:40:52 +02:00
|
|
|
DWORD i;
|
2006-07-26 16:44:47 +02:00
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
if (rdn->cRDNAttr)
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
setOf.rgBlob = CryptMemAlloc(rdn->cRDNAttr *
|
2007-06-12 23:40:52 +02:00
|
|
|
sizeof(CRYPT_DER_BLOB));
|
2007-10-19 21:36:06 +02:00
|
|
|
if (!setOf.rgBlob)
|
2006-07-26 16:44:47 +02:00
|
|
|
ret = FALSE;
|
|
|
|
else
|
2011-02-15 20:31:16 +01:00
|
|
|
{
|
|
|
|
setOf.cBlob = rdn->cRDNAttr;
|
2007-10-19 21:36:06 +02:00
|
|
|
memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
|
2011-02-15 20:31:16 +01:00
|
|
|
}
|
2006-07-26 16:44:47 +02:00
|
|
|
}
|
|
|
|
for (i = 0; ret && i < rdn->cRDNAttr; i++)
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
setOf.rgBlob[i].cbData = 0;
|
2006-07-26 16:44:47 +02:00
|
|
|
ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType, &rdn->rgRDNAttr[i],
|
2007-10-19 21:36:06 +02:00
|
|
|
nameValueEncodeFunc, NULL, &setOf.rgBlob[i].cbData);
|
2006-07-26 16:44:47 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
|
|
|
|
if (!setOf.rgBlob[i].pbData)
|
2006-07-26 16:44:47 +02:00
|
|
|
ret = FALSE;
|
|
|
|
else
|
2007-06-12 23:40:52 +02:00
|
|
|
ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType,
|
|
|
|
&rdn->rgRDNAttr[i], nameValueEncodeFunc,
|
2007-10-19 21:36:06 +02:00
|
|
|
setOf.rgBlob[i].pbData, &setOf.rgBlob[i].cbData);
|
2007-06-12 23:40:52 +02:00
|
|
|
}
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
/* Have to propagate index of failing character */
|
2007-10-19 21:36:06 +02:00
|
|
|
*pcbEncoded = setOf.rgBlob[i].cbData;
|
2006-07-26 16:44:47 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-12 23:40:52 +02:00
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_DEREncodeSet(X509_ASN_ENCODING, NULL, &setOf, 0, NULL,
|
|
|
|
pbEncoded, pcbEncoded);
|
2007-10-19 21:36:06 +02:00
|
|
|
for (i = 0; i < setOf.cBlob; i++)
|
|
|
|
CryptMemFree(setOf.rgBlob[i].pbData);
|
2006-07-26 16:44:47 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2007-10-19 21:36:06 +02:00
|
|
|
CryptMemFree(setOf.rgBlob);
|
2006-07-26 16:44:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeOrCopyUnicodeNameValue(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
|
|
|
|
DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_NAME_VALUE *value = pvStructInfo;
|
2006-07-26 16:44:47 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (value->dwValueType == CERT_RDN_ENCODED_BLOB)
|
|
|
|
ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL, &value->Value,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
else
|
|
|
|
ret = CRYPT_AsnEncodeUnicodeNameValue(dwCertEncodingType, NULL, value,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeUnicodeName(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_NAME_INFO *info = pvStructInfo;
|
2006-07-26 16:44:47 +02:00
|
|
|
DWORD bytesNeeded = 0, lenBytes, size, i;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("encoding name with %d RDNs\n", info->cRDN);
|
2006-07-26 16:44:47 +02:00
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0; ret && i < info->cRDN; i++)
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
|
|
|
|
CRYPT_AsnEncodeOrCopyUnicodeNameValue, NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
bytesNeeded += size;
|
|
|
|
else
|
|
|
|
*pcbEncoded = size;
|
|
|
|
}
|
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2006-07-26 16:44:47 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2006-07-26 16:44:47 +02:00
|
|
|
for (i = 0; ret && i < info->cRDN; i++)
|
|
|
|
{
|
|
|
|
size = bytesNeeded;
|
|
|
|
ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
|
|
|
|
&info->rgRDN[i], CRYPT_AsnEncodeOrCopyUnicodeNameValue,
|
2011-01-19 04:17:46 +01:00
|
|
|
out, &size);
|
2006-07-26 16:44:47 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += size;
|
2006-07-26 16:44:47 +02:00
|
|
|
bytesNeeded -= size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*pcbEncoded = size;
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2006-07-26 16:44:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-27 22:21:50 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCTLVersion(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const DWORD *ver = pvStructInfo;
|
2008-08-27 22:21:50 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
/* CTL_V1 is not encoded */
|
|
|
|
if (*ver == CTL_V1)
|
|
|
|
{
|
|
|
|
*pcbEncoded = 0;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like CRYPT_AsnEncodeAlgorithmId, but encodes parameters as an asn.1 NULL
|
|
|
|
* if they are empty and the OID is not empty (otherwise omits them.)
|
|
|
|
*/
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCTLSubjectAlgorithm(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
|
|
|
|
DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
|
2008-08-27 22:21:50 +02:00
|
|
|
BOOL ret;
|
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
};
|
|
|
|
DWORD cItem = 1;
|
|
|
|
|
|
|
|
if (algo->pszObjId)
|
|
|
|
{
|
|
|
|
static const BYTE asn1Null[] = { ASN_NULL, 0 };
|
|
|
|
static const CRYPT_DATA_BLOB nullBlob = { sizeof(asn1Null),
|
|
|
|
(LPBYTE)asn1Null };
|
|
|
|
|
|
|
|
if (algo->Parameters.cbData)
|
|
|
|
items[cItem].pvStructInfo = &algo->Parameters;
|
|
|
|
else
|
|
|
|
items[cItem].pvStructInfo = &nullBlob;
|
|
|
|
items[cItem].encodeFunc = CRYPT_CopyEncodedBlob;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeCTLEntry(const CTL_ENTRY *entry,
|
2008-08-27 22:21:50 +02:00
|
|
|
BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ &entry->SubjectIdentifier, CRYPT_AsnEncodeOctets, 0 },
|
|
|
|
{ &entry->cAttribute, CRYPT_AsnEncodePKCSAttributes, 0 },
|
|
|
|
};
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), 0, NULL, pbEncoded, pcbEncoded);
|
2008-08-27 22:21:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CTLEntries
|
|
|
|
{
|
|
|
|
DWORD cEntry;
|
|
|
|
CTL_ENTRY *rgEntry;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCTLEntries(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
2009-01-26 11:01:47 +01:00
|
|
|
const struct CTLEntries *entries = pvStructInfo;
|
2008-08-27 22:21:50 +02:00
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0, dataLen = 0; ret && i < entries->cEntry; i++)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeCTLEntry(&entries->rgEntry[i], NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
dataLen += size;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-08-27 22:21:50 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2008-08-27 22:21:50 +02:00
|
|
|
for (i = 0; ret && i < entries->cEntry; i++)
|
|
|
|
{
|
|
|
|
DWORD size = dataLen;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeCTLEntry(&entries->rgEntry[i],
|
2011-01-19 04:17:46 +01:00
|
|
|
out, &size);
|
|
|
|
out += size;
|
2008-08-27 22:21:50 +02:00
|
|
|
dataLen -= size;
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2008-08-27 22:21:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCTL(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CTL_INFO *info = pvStructInfo;
|
2008-08-27 22:21:50 +02:00
|
|
|
struct AsnEncodeSequenceItem items[9] = {
|
|
|
|
{ &info->dwVersion, CRYPT_AsnEncodeCTLVersion, 0 },
|
|
|
|
{ &info->SubjectUsage, CRYPT_AsnEncodeEnhancedKeyUsage, 0 },
|
|
|
|
};
|
|
|
|
struct AsnConstructedItem constructed = { 0 };
|
|
|
|
DWORD cItem = 2;
|
|
|
|
|
|
|
|
if (info->ListIdentifier.cbData)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &info->ListIdentifier;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->SequenceNumber.cbData)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &info->SequenceNumber;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeInteger;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
items[cItem].pvStructInfo = &info->ThisUpdate;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
|
|
|
|
cItem++;
|
|
|
|
if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &info->NextUpdate;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
items[cItem].pvStructInfo = &info->SubjectAlgorithm;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeCTLSubjectAlgorithm;
|
|
|
|
cItem++;
|
|
|
|
if (info->cCTLEntry)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &info->cCTLEntry;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeCTLEntries;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->cExtension)
|
|
|
|
{
|
|
|
|
constructed.tag = 0;
|
|
|
|
constructed.pvStructInfo = &info->cExtension;
|
|
|
|
constructed.encodeFunc = CRYPT_AsnEncodeExtensions;
|
|
|
|
items[cItem].pvStructInfo = &constructed;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeSMIMECapability(DWORD dwCertEncodingType,
|
2008-08-20 21:11:15 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_SMIME_CAPABILITY *capability = pvStructInfo;
|
2008-08-20 21:11:15 +02:00
|
|
|
|
|
|
|
if (!capability->pszObjId)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ capability->pszObjId, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ &capability->Parameters, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2008-08-20 21:11:15 +02:00
|
|
|
pcbEncoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeSMIMECapabilities(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_SMIME_CAPABILITIES *capabilities = pvStructInfo;
|
2008-08-20 21:11:15 +02:00
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0, dataLen = 0; ret && i < capabilities->cCapability; i++)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSMIMECapability(dwCertEncodingType, NULL,
|
|
|
|
&capabilities->rgCapability[i], 0, NULL, NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
dataLen += size;
|
|
|
|
}
|
2008-08-27 20:02:13 +02:00
|
|
|
if (ret)
|
2008-08-20 21:11:15 +02:00
|
|
|
{
|
2008-08-27 20:02:13 +02:00
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
2008-08-20 21:11:15 +02:00
|
|
|
{
|
2008-08-27 20:02:13 +02:00
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
2008-08-20 21:11:15 +02:00
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-08-27 20:02:13 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2008-08-27 20:02:13 +02:00
|
|
|
for (i = 0; i < capabilities->cCapability; i++)
|
|
|
|
{
|
|
|
|
DWORD size = dataLen;
|
2008-08-20 21:11:15 +02:00
|
|
|
|
2008-08-27 20:02:13 +02:00
|
|
|
ret = CRYPT_AsnEncodeSMIMECapability(dwCertEncodingType,
|
|
|
|
NULL, &capabilities->rgCapability[i], 0, NULL,
|
2011-01-19 04:17:46 +01:00
|
|
|
out, &size);
|
|
|
|
out += size;
|
2008-08-27 20:02:13 +02:00
|
|
|
dataLen -= size;
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2008-08-20 21:11:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-13 22:45:29 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeNoticeNumbers(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
const CERT_POLICY_QUALIFIER_NOTICE_REFERENCE *noticeRef = pvStructInfo;
|
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
for (i = 0, dataLen = 0; ret && i < noticeRef->cNoticeNumbers; i++)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER,
|
|
|
|
¬iceRef->rgNoticeNumbers[i], 0, NULL, NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
dataLen += size;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-11-13 22:45:29 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCE;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2008-11-13 22:45:29 +01:00
|
|
|
for (i = 0; i < noticeRef->cNoticeNumbers; i++)
|
|
|
|
{
|
|
|
|
DWORD size = dataLen;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER,
|
2011-01-19 04:17:46 +01:00
|
|
|
¬iceRef->rgNoticeNumbers[i], 0, NULL, out, &size);
|
|
|
|
out += size;
|
2008-11-13 22:45:29 +01:00
|
|
|
dataLen -= size;
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2008-11-13 22:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeNoticeReference(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
const CERT_POLICY_QUALIFIER_NOTICE_REFERENCE *noticeRef = pvStructInfo;
|
|
|
|
BOOL ret;
|
|
|
|
CERT_NAME_VALUE orgValue = { CERT_RDN_IA5_STRING,
|
|
|
|
{ 0, (LPBYTE)noticeRef->pszOrganization } };
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &orgValue, CRYPT_AsnEncodeNameValue, 0 },
|
|
|
|
{ noticeRef, CRYPT_AsnEncodeNoticeNumbers, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2008-11-13 22:45:29 +01:00
|
|
|
pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePolicyQualifierUserNotice(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
|
|
|
|
DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
const CERT_POLICY_QUALIFIER_USER_NOTICE *notice = pvStructInfo;
|
|
|
|
struct AsnEncodeSequenceItem items[2];
|
|
|
|
CERT_NAME_VALUE displayTextValue;
|
|
|
|
DWORD cItem = 0;
|
|
|
|
|
|
|
|
if (notice->pNoticeReference)
|
|
|
|
{
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeNoticeReference;
|
|
|
|
items[cItem].pvStructInfo = notice->pNoticeReference;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (notice->pszDisplayText)
|
|
|
|
{
|
|
|
|
displayTextValue.dwValueType = CERT_RDN_BMP_STRING;
|
|
|
|
displayTextValue.Value.cbData = 0;
|
|
|
|
displayTextValue.Value.pbData = (LPBYTE)notice->pszDisplayText;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeNameValue;
|
|
|
|
items[cItem].pvStructInfo = &displayTextValue;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-13 00:00:01 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePKCSAttribute(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_ATTRIBUTE *attr = pvStructInfo;
|
2007-06-13 00:00:01 +02:00
|
|
|
|
|
|
|
if (!attr->pszObjId)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ attr->pszObjId, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ &attr->cValue, CRYPT_DEREncodeSet, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
|
2007-06-13 00:00:01 +02:00
|
|
|
pcbEncoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-13 00:01:34 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePKCSAttributes(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_ATTRIBUTES *attributes = pvStructInfo;
|
2007-07-24 00:29:15 +02:00
|
|
|
struct DERSetDescriptor desc = { attributes->cAttr, attributes->rgAttr,
|
|
|
|
sizeof(CRYPT_ATTRIBUTE), 0, CRYPT_AsnEncodePKCSAttribute };
|
2007-06-13 00:01:34 +02:00
|
|
|
|
2007-07-24 00:29:15 +02:00
|
|
|
ret = CRYPT_DEREncodeItemsAsSet(X509_ASN_ENCODING, lpszStructType,
|
|
|
|
&desc, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2007-06-13 00:01:34 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-17 16:22:50 +02:00
|
|
|
/* Like CRYPT_AsnEncodePKCSContentInfo, but allows the OID to be NULL */
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfoInternal(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
|
|
|
|
DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
2007-07-12 23:59:27 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_CONTENT_INFO *info = pvStructInfo;
|
2007-07-12 23:59:27 +02:00
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ info->pszObjId, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ NULL, NULL, 0 },
|
|
|
|
};
|
|
|
|
struct AsnConstructedItem constructed = { 0 };
|
|
|
|
DWORD cItem = 1;
|
|
|
|
|
|
|
|
if (info->Content.cbData)
|
|
|
|
{
|
|
|
|
constructed.tag = 0;
|
|
|
|
constructed.pvStructInfo = &info->Content;
|
|
|
|
constructed.encodeFunc = CRYPT_CopyEncodedBlob;
|
|
|
|
items[cItem].pvStructInfo = &constructed;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
|
|
|
cItem, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
|
2009-06-12 19:27:56 +02:00
|
|
|
BOOL CRYPT_AsnEncodePKCSDigestedData(const CRYPT_DIGESTED_DATA *digestedData,
|
2007-07-17 16:22:50 +02:00
|
|
|
void *pvData, DWORD *pcbData)
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &digestedData->version, CRYPT_AsnEncodeInt, 0 },
|
|
|
|
{ &digestedData->DigestAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
|
|
|
|
0 },
|
|
|
|
{ &digestedData->ContentInfo, CRYPT_AsnEncodePKCSContentInfoInternal, 0 },
|
|
|
|
{ &digestedData->hash, CRYPT_AsnEncodeOctets, 0 },
|
|
|
|
};
|
|
|
|
|
2018-06-20 09:37:55 +02:00
|
|
|
return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, ARRAY_SIZE(items), 0, NULL, pvData, pcbData);
|
2007-07-17 16:22:50 +02:00
|
|
|
}
|
|
|
|
|
2007-06-12 23:40:04 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_CONTENT_INFO *info = pvStructInfo;
|
2007-06-12 23:40:04 +02:00
|
|
|
|
|
|
|
if (!info->pszObjId)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
2007-07-12 23:59:27 +02:00
|
|
|
ret = CRYPT_AsnEncodePKCSContentInfoInternal(dwCertEncodingType,
|
|
|
|
lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded);
|
2007-06-12 23:40:04 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-19 16:44:48 +02:00
|
|
|
static BOOL CRYPT_AsnEncodeUnicodeStringCoerce(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
BYTE tag, DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-19 16:44:48 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCWSTR str = (LPCWSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, encodedLen;
|
|
|
|
|
2011-02-10 17:52:21 +01:00
|
|
|
if (value->Value.cbData)
|
|
|
|
encodedLen = value->Value.cbData / sizeof(WCHAR);
|
|
|
|
else if (str)
|
2020-11-09 11:41:21 +01:00
|
|
|
encodedLen = lstrlenW(str);
|
2011-02-10 17:52:21 +01:00
|
|
|
else
|
|
|
|
encodedLen = 0;
|
2006-07-19 16:44:48 +02:00
|
|
|
CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + encodedLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = tag;
|
|
|
|
CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
for (i = 0; i < encodedLen; i++)
|
|
|
|
*pbEncoded++ = (BYTE)str[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnEncodeNumericString(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-19 16:44:48 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCWSTR str = (LPCWSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, encodedLen;
|
|
|
|
|
2011-02-10 17:52:21 +01:00
|
|
|
if (value->Value.cbData)
|
|
|
|
encodedLen = value->Value.cbData / sizeof(WCHAR);
|
|
|
|
else if (str)
|
2020-11-09 11:41:21 +01:00
|
|
|
encodedLen = lstrlenW(str);
|
2011-02-10 17:52:21 +01:00
|
|
|
else
|
|
|
|
encodedLen = 0;
|
2006-07-19 16:44:48 +02:00
|
|
|
CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + encodedLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
DWORD i;
|
2007-11-01 18:07:11 +01:00
|
|
|
BYTE *ptr;
|
2006-07-19 16:44:48 +02:00
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
2007-11-01 18:07:11 +01:00
|
|
|
ptr = *(BYTE **)pbEncoded;
|
|
|
|
else
|
|
|
|
ptr = pbEncoded;
|
|
|
|
*ptr++ = ASN_NUMERICSTRING;
|
|
|
|
CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
|
|
|
|
ptr += lenBytes;
|
2006-07-19 16:44:48 +02:00
|
|
|
for (i = 0; ret && i < encodedLen; i++)
|
|
|
|
{
|
2020-02-06 14:00:20 +01:00
|
|
|
if ('0' <= str[i] && str[i] <= '9')
|
2007-11-01 18:07:11 +01:00
|
|
|
*ptr++ = (BYTE)str[i];
|
2006-07-19 16:44:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbEncoded = i;
|
|
|
|
SetLastError(CRYPT_E_INVALID_NUMERIC_STRING);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-11-01 18:07:11 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
2008-08-16 01:39:09 +02:00
|
|
|
CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
|
2006-07-19 16:44:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-08 03:37:51 +01:00
|
|
|
static inline BOOL isprintableW(WCHAR wc)
|
2006-07-19 16:44:48 +02:00
|
|
|
{
|
2020-11-09 11:44:20 +01:00
|
|
|
return wc && wcschr( L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '()+,-./:=?", wc );
|
2006-07-19 16:44:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnEncodePrintableString(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-19 16:44:48 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCWSTR str = (LPCWSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, encodedLen;
|
|
|
|
|
2011-02-10 17:52:21 +01:00
|
|
|
if (value->Value.cbData)
|
|
|
|
encodedLen = value->Value.cbData / sizeof(WCHAR);
|
|
|
|
else if (str)
|
2020-11-09 11:41:21 +01:00
|
|
|
encodedLen = lstrlenW(str);
|
2011-02-10 17:52:21 +01:00
|
|
|
else
|
|
|
|
encodedLen = 0;
|
2006-07-19 16:44:48 +02:00
|
|
|
CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + encodedLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
DWORD i;
|
2007-11-01 18:07:11 +01:00
|
|
|
BYTE *ptr;
|
2006-07-19 16:44:48 +02:00
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
2007-11-01 18:07:11 +01:00
|
|
|
ptr = *(BYTE **)pbEncoded;
|
|
|
|
else
|
|
|
|
ptr = pbEncoded;
|
|
|
|
*ptr++ = ASN_PRINTABLESTRING;
|
|
|
|
CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
|
|
|
|
ptr += lenBytes;
|
2006-07-19 16:44:48 +02:00
|
|
|
for (i = 0; ret && i < encodedLen; i++)
|
|
|
|
{
|
|
|
|
if (isprintableW(str[i]))
|
2007-11-01 18:07:11 +01:00
|
|
|
*ptr++ = (BYTE)str[i];
|
2006-07-19 16:44:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbEncoded = i;
|
|
|
|
SetLastError(CRYPT_E_INVALID_PRINTABLE_STRING);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-11-01 18:07:11 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
2008-08-16 01:39:09 +02:00
|
|
|
CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
|
2006-07-19 16:44:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnEncodeIA5String(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-19 16:44:48 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCWSTR str = (LPCWSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, encodedLen;
|
|
|
|
|
2011-02-10 17:52:21 +01:00
|
|
|
if (value->Value.cbData)
|
|
|
|
encodedLen = value->Value.cbData / sizeof(WCHAR);
|
|
|
|
else if (str)
|
2020-11-09 11:41:21 +01:00
|
|
|
encodedLen = lstrlenW(str);
|
2011-02-10 17:52:21 +01:00
|
|
|
else
|
|
|
|
encodedLen = 0;
|
2006-07-19 16:44:48 +02:00
|
|
|
CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + encodedLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
DWORD i;
|
2007-11-01 18:07:11 +01:00
|
|
|
BYTE *ptr;
|
2006-07-19 16:44:48 +02:00
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
2007-11-01 18:07:11 +01:00
|
|
|
ptr = *(BYTE **)pbEncoded;
|
|
|
|
else
|
|
|
|
ptr = pbEncoded;
|
|
|
|
*ptr++ = ASN_IA5STRING;
|
|
|
|
CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
|
|
|
|
ptr += lenBytes;
|
2006-07-19 16:44:48 +02:00
|
|
|
for (i = 0; ret && i < encodedLen; i++)
|
|
|
|
{
|
|
|
|
if (str[i] <= 0x7f)
|
2007-11-01 18:07:11 +01:00
|
|
|
*ptr++ = (BYTE)str[i];
|
2006-07-19 16:44:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbEncoded = i;
|
|
|
|
SetLastError(CRYPT_E_INVALID_IA5_STRING);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-11-01 18:07:11 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
2008-08-16 01:39:09 +02:00
|
|
|
CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
|
2006-07-19 16:44:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnEncodeUniversalString(const CERT_NAME_VALUE *value,
|
2010-08-15 21:15:26 +02:00
|
|
|
DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
|
2006-07-19 16:44:48 +02:00
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
LPCWSTR str = (LPCWSTR)value->Value.pbData;
|
|
|
|
DWORD bytesNeeded, lenBytes, strLen;
|
|
|
|
|
|
|
|
/* FIXME: doesn't handle composite characters */
|
2011-02-10 17:52:21 +01:00
|
|
|
if (value->Value.cbData)
|
|
|
|
strLen = value->Value.cbData / sizeof(WCHAR);
|
|
|
|
else if (str)
|
2020-11-09 11:41:21 +01:00
|
|
|
strLen = lstrlenW(str);
|
2011-02-10 17:52:21 +01:00
|
|
|
else
|
|
|
|
strLen = 0;
|
2006-07-19 16:44:48 +02:00
|
|
|
CRYPT_EncodeLen(strLen * 4, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + strLen * 4;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_UNIVERSALSTRING;
|
|
|
|
CRYPT_EncodeLen(strLen * 4, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
for (i = 0; i < strLen; i++)
|
|
|
|
{
|
|
|
|
*pbEncoded++ = 0;
|
|
|
|
*pbEncoded++ = 0;
|
|
|
|
*pbEncoded++ = (BYTE)((str[i] & 0xff00) >> 8);
|
|
|
|
*pbEncoded++ = (BYTE)(str[i] & 0x00ff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_NAME_VALUE *value = pvStructInfo;
|
2006-07-19 16:44:48 +02:00
|
|
|
|
|
|
|
switch (value->dwValueType)
|
|
|
|
{
|
|
|
|
case CERT_RDN_ANY_TYPE:
|
|
|
|
case CERT_RDN_ENCODED_BLOB:
|
|
|
|
case CERT_RDN_OCTET_STRING:
|
|
|
|
SetLastError(CRYPT_E_NOT_CHAR_STRING);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_NUMERIC_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeNumericString(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_PRINTABLE_STRING:
|
|
|
|
ret = CRYPT_AsnEncodePrintableString(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_TELETEX_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_T61STRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_VIDEOTEX_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUnicodeStringCoerce(value,
|
|
|
|
ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_IA5_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeIA5String(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_GRAPHIC_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GRAPHICSTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_VISIBLE_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_VISIBLESTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_GENERAL_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GENERALSTRING,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_UNIVERSAL_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUniversalString(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_BMP_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
case CERT_RDN_UTF8_STRING:
|
|
|
|
ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SetLastError(CRYPT_E_ASN1_CHOICE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-13 13:39:35 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeName(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_NAME_INFO *info = pvStructInfo;
|
2005-06-27 21:48:24 +02:00
|
|
|
DWORD bytesNeeded = 0, lenBytes, size, i;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("encoding name with %d RDNs\n", info->cRDN);
|
2005-06-27 21:48:24 +02:00
|
|
|
ret = TRUE;
|
2005-06-13 13:39:35 +02:00
|
|
|
for (i = 0; ret && i < info->cRDN; i++)
|
|
|
|
{
|
2006-07-26 16:44:47 +02:00
|
|
|
ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
|
|
|
|
CRYPT_AsnEncodeNameValue, NULL, &size);
|
2005-06-13 13:39:35 +02:00
|
|
|
if (ret)
|
2005-06-27 21:48:24 +02:00
|
|
|
bytesNeeded += size;
|
|
|
|
}
|
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
2005-06-13 13:39:35 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2005-06-27 21:48:24 +02:00
|
|
|
for (i = 0; ret && i < info->cRDN; i++)
|
|
|
|
{
|
|
|
|
size = bytesNeeded;
|
|
|
|
ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
|
2011-01-19 04:17:46 +01:00
|
|
|
&info->rgRDN[i], CRYPT_AsnEncodeNameValue, out, &size);
|
2005-06-27 21:48:24 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += size;
|
2005-06-27 21:48:24 +02:00
|
|
|
bytesNeeded -= size;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2005-06-27 21:48:24 +02:00
|
|
|
}
|
2005-06-13 13:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-27 21:48:24 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2005-06-13 13:39:35 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
|
2005-07-07 13:26:50 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-07-12 19:00:23 +02:00
|
|
|
BOOL val = *(const BOOL *)pvStructInfo, ret;
|
2005-07-07 13:26:50 +02:00
|
|
|
|
2005-07-12 19:00:23 +02:00
|
|
|
TRACE("%d\n", val);
|
2005-07-07 13:26:50 +02:00
|
|
|
|
2005-06-27 11:42:15 +02:00
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = 3;
|
2005-07-12 19:00:23 +02:00
|
|
|
ret = TRUE;
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
2005-07-12 19:00:23 +02:00
|
|
|
else if (*pcbEncoded < 3)
|
2005-06-27 11:42:15 +02:00
|
|
|
{
|
|
|
|
*pcbEncoded = 3;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
2005-07-12 19:00:23 +02:00
|
|
|
ret = FALSE;
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
2005-07-12 19:00:23 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbEncoded = 3;
|
|
|
|
*pbEncoded++ = ASN_BOOL;
|
|
|
|
*pbEncoded++ = 1;
|
|
|
|
*pbEncoded++ = val ? 0xff : 0;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2005-07-12 19:00:23 +02:00
|
|
|
return ret;
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
|
|
|
|
2007-10-11 01:05:12 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeAltNameEntry(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2005-08-29 16:12:44 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_ALT_NAME_ENTRY *entry = pvStructInfo;
|
2005-08-29 16:12:44 +02:00
|
|
|
BOOL ret;
|
|
|
|
DWORD dataLen;
|
2007-08-02 18:48:22 +02:00
|
|
|
BYTE tag;
|
2005-08-29 16:12:44 +02:00
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
switch (entry->dwAltNameChoice)
|
|
|
|
{
|
|
|
|
case CERT_ALT_NAME_RFC822_NAME:
|
|
|
|
case CERT_ALT_NAME_DNS_NAME:
|
|
|
|
case CERT_ALT_NAME_URL:
|
2007-08-02 18:48:22 +02:00
|
|
|
tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
|
2005-08-29 16:12:44 +02:00
|
|
|
if (entry->u.pwszURL)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
/* Not + 1: don't encode the NULL-terminator */
|
|
|
|
dataLen = lstrlenW(entry->u.pwszURL);
|
|
|
|
for (i = 0; ret && i < dataLen; i++)
|
|
|
|
{
|
|
|
|
if (entry->u.pwszURL[i] > 0x7f)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_INVALID_IA5_STRING);
|
|
|
|
ret = FALSE;
|
|
|
|
*pcbEncoded = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dataLen = 0;
|
|
|
|
break;
|
2007-08-02 18:48:22 +02:00
|
|
|
case CERT_ALT_NAME_DIRECTORY_NAME:
|
|
|
|
tag = ASN_CONTEXT | ASN_CONSTRUCTOR | (entry->dwAltNameChoice - 1);
|
|
|
|
dataLen = entry->u.DirectoryName.cbData;
|
|
|
|
break;
|
2005-08-29 16:12:44 +02:00
|
|
|
case CERT_ALT_NAME_IP_ADDRESS:
|
2007-08-02 18:48:22 +02:00
|
|
|
tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
|
2005-08-29 16:12:44 +02:00
|
|
|
dataLen = entry->u.IPAddress.cbData;
|
|
|
|
break;
|
|
|
|
case CERT_ALT_NAME_REGISTERED_ID:
|
2007-08-02 18:48:22 +02:00
|
|
|
{
|
|
|
|
struct AsnEncodeTagSwappedItem swapped =
|
|
|
|
{ ASN_CONTEXT | (entry->dwAltNameChoice - 1), entry->u.pszRegisteredID,
|
|
|
|
CRYPT_AsnEncodeOid };
|
|
|
|
|
|
|
|
return CRYPT_AsnEncodeSwapTag(0, NULL, &swapped, 0, NULL, pbEncoded,
|
|
|
|
pcbEncoded);
|
|
|
|
}
|
2005-08-29 16:12:44 +02:00
|
|
|
case CERT_ALT_NAME_OTHER_NAME:
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("name type %d unimplemented\n", entry->dwAltNameChoice);
|
2005-08-29 16:12:44 +02:00
|
|
|
return FALSE;
|
|
|
|
default:
|
2006-05-11 18:48:01 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2005-08-29 16:12:44 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, lenBytes;
|
|
|
|
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + dataLen + lenBytes;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else if (*pcbEncoded < bytesNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-08-02 18:48:22 +02:00
|
|
|
*pbEncoded++ = tag;
|
2005-08-29 16:12:44 +02:00
|
|
|
CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
switch (entry->dwAltNameChoice)
|
|
|
|
{
|
|
|
|
case CERT_ALT_NAME_RFC822_NAME:
|
|
|
|
case CERT_ALT_NAME_DNS_NAME:
|
|
|
|
case CERT_ALT_NAME_URL:
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < dataLen; i++)
|
|
|
|
*pbEncoded++ = (BYTE)entry->u.pwszURL[i];
|
|
|
|
break;
|
|
|
|
}
|
2007-08-02 18:48:22 +02:00
|
|
|
case CERT_ALT_NAME_DIRECTORY_NAME:
|
|
|
|
memcpy(pbEncoded, entry->u.DirectoryName.pbData, dataLen);
|
|
|
|
break;
|
2005-08-29 16:12:44 +02:00
|
|
|
case CERT_ALT_NAME_IP_ADDRESS:
|
|
|
|
memcpy(pbEncoded, entry->u.IPAddress.pbData, dataLen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2005-08-29 16:12:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-09-27 06:20:35 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_AUTHORITY_KEY_ID_INFO *info = pvStructInfo;
|
2006-09-27 06:20:35 +02:00
|
|
|
struct AsnEncodeSequenceItem items[3] = { { 0 } };
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
|
|
|
|
struct AsnConstructedItem constructed = { 0 };
|
|
|
|
DWORD cItem = 0, cSwapped = 0;
|
|
|
|
|
|
|
|
if (info->KeyId.cbData)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->KeyId;
|
2009-12-02 22:05:28 +01:00
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeOctets;
|
2006-09-27 06:20:35 +02:00
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->CertIssuer.cbData)
|
|
|
|
{
|
|
|
|
constructed.tag = 1;
|
|
|
|
constructed.pvStructInfo = &info->CertIssuer;
|
|
|
|
constructed.encodeFunc = CRYPT_CopyEncodedBlob;
|
|
|
|
items[cItem].pvStructInfo = &constructed;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->CertSerialNumber.cbData)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 2;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->CertSerialNumber;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
|
|
|
|
pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-08-29 16:12:44 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeAltName(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_ALT_NAME_INFO *info = pvStructInfo;
|
2005-08-29 16:12:44 +02:00
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
/* FIXME: should check that cAltEntry is not bigger than 0xff, since we
|
|
|
|
* can't encode an erroneous entry index if it's bigger than this.
|
|
|
|
*/
|
|
|
|
for (i = 0, dataLen = 0; ret && i < info->cAltEntry; i++)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
|
2007-10-11 01:05:12 +02:00
|
|
|
ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType, NULL,
|
|
|
|
&info->rgAltEntry[i], 0, NULL, NULL, &len);
|
2005-08-29 16:12:44 +02:00
|
|
|
if (ret)
|
|
|
|
dataLen += len;
|
2005-09-03 11:36:02 +02:00
|
|
|
else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
|
|
|
|
{
|
|
|
|
/* CRYPT_AsnEncodeAltNameEntry encoded the index of
|
|
|
|
* the bad character, now set the index of the bad
|
|
|
|
* entry
|
|
|
|
*/
|
|
|
|
*pcbEncoded = (BYTE)i <<
|
|
|
|
CERT_ALT_NAME_ENTRY_ERR_INDEX_SHIFT | len;
|
|
|
|
}
|
2005-08-29 16:12:44 +02:00
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2005-08-29 16:12:44 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2005-08-29 16:12:44 +02:00
|
|
|
for (i = 0; ret && i < info->cAltEntry; i++)
|
|
|
|
{
|
|
|
|
DWORD len = dataLen;
|
|
|
|
|
2007-10-11 01:05:12 +02:00
|
|
|
ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType,
|
2011-01-19 04:17:46 +01:00
|
|
|
NULL, &info->rgAltEntry[i], 0, NULL, out, &len);
|
2005-08-29 16:12:44 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += len;
|
2005-08-29 16:12:44 +02:00
|
|
|
dataLen -= len;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2005-08-29 16:12:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-08-29 16:12:44 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-12 23:39:19 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId2(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_AUTHORITY_KEY_ID2_INFO *info = pvStructInfo;
|
2007-06-12 23:39:19 +02:00
|
|
|
struct AsnEncodeSequenceItem items[3] = { { 0 } };
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
|
|
|
|
DWORD cItem = 0, cSwapped = 0;
|
|
|
|
|
|
|
|
if (info->KeyId.cbData)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->KeyId;
|
2009-12-02 22:05:28 +01:00
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeOctets;
|
2007-06-12 23:39:19 +02:00
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->AuthorityCertIssuer.cAltEntry)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->AuthorityCertIssuer;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->AuthorityCertSerialNumber.cbData)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 2;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->AuthorityCertSerialNumber;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
|
|
|
|
pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeAccessDescription(
|
2008-08-27 19:17:09 +02:00
|
|
|
const CERT_ACCESS_DESCRIPTION *descr, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ descr->pszAccessMethod, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ &descr->AccessLocation, CRYPT_AsnEncodeAltNameEntry, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!descr->pszAccessMethod)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-06-20 09:37:55 +02:00
|
|
|
return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, ARRAY_SIZE(items), 0, NULL, pbEncoded, pcbEncoded);
|
2008-08-27 19:17:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeAuthorityInfoAccess(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_AUTHORITY_INFO_ACCESS *info = pvStructInfo;
|
2008-08-27 19:17:09 +02:00
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0, dataLen = 0; ret && i < info->cAccDescr; i++)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeAccessDescription(&info->rgAccDescr[i], NULL,
|
|
|
|
&size);
|
|
|
|
if (ret)
|
|
|
|
dataLen += size;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-08-27 19:17:09 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2008-08-27 19:17:09 +02:00
|
|
|
for (i = 0; i < info->cAccDescr; i++)
|
|
|
|
{
|
|
|
|
DWORD size = dataLen;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeAccessDescription(
|
2011-01-19 04:17:46 +01:00
|
|
|
&info->rgAccDescr[i], out, &size);
|
|
|
|
out += size;
|
2008-08-27 19:17:09 +02:00
|
|
|
dataLen -= size;
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2008-08-27 19:17:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:39:27 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_BASIC_CONSTRAINTS_INFO *info = pvStructInfo;
|
2006-02-16 19:39:27 +01:00
|
|
|
struct AsnEncodeSequenceItem items[3] = {
|
|
|
|
{ &info->SubjectType, CRYPT_AsnEncodeBits, 0 },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
DWORD cItem = 1;
|
|
|
|
|
|
|
|
if (info->fPathLenConstraint)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &info->dwPathLenConstraint;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->cSubtreesConstraint)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo = &info->cSubtreesConstraint;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-27 11:42:15 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints2(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-27 11:42:15 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-27 11:42:15 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_BASIC_CONSTRAINTS2_INFO *info = pvStructInfo;
|
2005-07-12 19:00:23 +02:00
|
|
|
struct AsnEncodeSequenceItem items[2] = { { 0 } };
|
|
|
|
DWORD cItem = 0;
|
2005-06-27 21:48:24 +02:00
|
|
|
|
|
|
|
if (info->fCA)
|
|
|
|
{
|
2005-07-12 19:00:23 +02:00
|
|
|
items[cItem].pvStructInfo = &info->fCA;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
|
|
|
|
cItem++;
|
2005-06-27 21:48:24 +02:00
|
|
|
}
|
|
|
|
if (info->fPathLenConstraint)
|
|
|
|
{
|
2005-07-12 19:00:23 +02:00
|
|
|
items[cItem].pvStructInfo = &info->dwPathLenConstraint;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
|
|
|
|
cItem++;
|
2005-06-27 21:48:24 +02:00
|
|
|
}
|
2005-07-12 19:00:23 +02:00
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-27 11:42:15 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
2005-06-27 11:42:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-15 01:06:13 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCertPolicyQualifiers(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2009-10-09 23:06:59 +02:00
|
|
|
const CERT_POLICY_INFO *info = pvStructInfo;
|
2008-11-15 01:06:13 +01:00
|
|
|
BOOL ret;
|
|
|
|
|
2009-10-09 23:06:59 +02:00
|
|
|
if (!info->cPolicyQualifier)
|
2008-11-15 01:06:13 +01:00
|
|
|
{
|
|
|
|
*pcbEncoded = 0;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ NULL, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ NULL, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
};
|
|
|
|
DWORD bytesNeeded = 0, lenBytes, size, i;
|
|
|
|
|
|
|
|
ret = TRUE;
|
2009-10-09 23:06:59 +02:00
|
|
|
for (i = 0; ret && i < info->cPolicyQualifier; i++)
|
2008-11-15 01:06:13 +01:00
|
|
|
{
|
2009-10-09 23:06:59 +02:00
|
|
|
items[0].pvStructInfo =
|
|
|
|
info->rgPolicyQualifier[i].pszPolicyQualifierId;
|
|
|
|
items[1].pvStructInfo = &info->rgPolicyQualifier[i].Qualifier;
|
2018-06-20 09:37:55 +02:00
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items),
|
2008-11-15 01:06:13 +01:00
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
bytesNeeded += size;
|
|
|
|
}
|
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-11-15 01:06:13 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2009-10-09 23:06:59 +02:00
|
|
|
for (i = 0; ret && i < info->cPolicyQualifier; i++)
|
2008-11-15 01:06:13 +01:00
|
|
|
{
|
|
|
|
items[0].pvStructInfo =
|
2009-10-09 23:06:59 +02:00
|
|
|
info->rgPolicyQualifier[i].pszPolicyQualifierId;
|
2008-11-15 01:06:13 +01:00
|
|
|
items[1].pvStructInfo =
|
2009-10-09 23:06:59 +02:00
|
|
|
&info->rgPolicyQualifier[i].Qualifier;
|
2008-11-15 01:06:13 +01:00
|
|
|
size = bytesNeeded;
|
2018-06-20 09:37:55 +02:00
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items),
|
2011-01-19 04:17:46 +01:00
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, out, &size);
|
2008-11-15 01:06:13 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += size;
|
2008-11-15 01:06:13 +01:00
|
|
|
bytesNeeded -= size;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2008-11-15 01:06:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_AsnEncodeCertPolicy(DWORD dwCertEncodingType,
|
|
|
|
const CERT_POLICY_INFO *info, DWORD dwFlags, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[2] = {
|
|
|
|
{ info->pszPolicyIdentifier, CRYPT_AsnEncodeOid, 0 },
|
2009-10-09 23:06:59 +02:00
|
|
|
{ info, CRYPT_AsnEncodeCertPolicyQualifiers, 0 },
|
2008-11-15 01:06:13 +01:00
|
|
|
};
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (!info->pszPolicyIdentifier)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-06-20 09:37:55 +02:00
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items), dwFlags, NULL, pbEncoded, pcbEncoded);
|
2008-11-15 01:06:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCertPolicies(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
const CERT_POLICIES_INFO *info = pvStructInfo;
|
|
|
|
DWORD bytesNeeded = 0, lenBytes, size, i;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0; ret && i < info->cPolicyInfo; i++)
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnEncodeCertPolicy(dwCertEncodingType,
|
|
|
|
&info->rgPolicyInfo[i], dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
|
|
|
&size);
|
|
|
|
if (ret)
|
|
|
|
bytesNeeded += size;
|
|
|
|
}
|
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2008-11-15 01:06:13 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2008-11-15 01:06:13 +01:00
|
|
|
for (i = 0; ret && i < info->cPolicyInfo; i++)
|
|
|
|
{
|
|
|
|
size = bytesNeeded;
|
|
|
|
ret = CRYPT_AsnEncodeCertPolicy(dwCertEncodingType,
|
|
|
|
&info->rgPolicyInfo[i],
|
2011-01-19 04:17:46 +01:00
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, out, &size);
|
2008-11-15 01:06:13 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += size;
|
2008-11-15 01:06:13 +01:00
|
|
|
bytesNeeded -= size;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2008-11-15 01:06:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-06 20:52:19 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeCertPolicyMapping(DWORD dwCertEncodingType,
|
|
|
|
const CERT_POLICY_MAPPING *mapping, DWORD dwFlags, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ mapping->pszIssuerDomainPolicy, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ mapping->pszSubjectDomainPolicy, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!mapping->pszIssuerDomainPolicy || !mapping->pszSubjectDomainPolicy)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-06-20 09:37:55 +02:00
|
|
|
return CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items), dwFlags, NULL, pbEncoded, pcbEncoded);
|
2009-11-06 20:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCertPolicyMappings(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
const CERT_POLICY_MAPPINGS_INFO *info = pvStructInfo;
|
|
|
|
DWORD bytesNeeded = 0, lenBytes, size, i;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0; ret && i < info->cPolicyMapping; i++)
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnEncodeCertPolicyMapping(dwCertEncodingType,
|
|
|
|
&info->rgPolicyMapping[i], dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG,
|
|
|
|
NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
bytesNeeded += size;
|
|
|
|
}
|
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2009-11-06 20:52:19 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2009-11-06 20:52:19 +01:00
|
|
|
for (i = 0; ret && i < info->cPolicyMapping; i++)
|
|
|
|
{
|
|
|
|
size = bytesNeeded;
|
|
|
|
ret = CRYPT_AsnEncodeCertPolicyMapping(
|
|
|
|
dwCertEncodingType, &info->rgPolicyMapping[i],
|
2011-01-19 04:17:46 +01:00
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, out, &size);
|
2009-11-06 20:52:19 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += size;
|
2009-11-06 20:52:19 +01:00
|
|
|
bytesNeeded -= size;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2009-11-06 20:52:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-06 21:35:49 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCertPolicyConstraints(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
|
|
|
|
DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
const CERT_POLICY_CONSTRAINTS_INFO *info = pvStructInfo;
|
|
|
|
struct AsnEncodeSequenceItem items[2];
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[2];
|
|
|
|
DWORD cItem = 0, cSwapped = 0;
|
|
|
|
|
|
|
|
if (info->fRequireExplicitPolicy)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo =
|
|
|
|
&info->dwRequireExplicitPolicySkipCerts;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (info->fInhibitPolicyMapping)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo =
|
|
|
|
&info->dwInhibitPolicyMappingSkipCerts;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, NULL, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-12 11:40:50 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeRsaPubKey_Bcrypt(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
const BCRYPT_RSAKEY_BLOB *hdr = pvStructInfo;
|
|
|
|
|
|
|
|
BYTE *pubexp = (BYTE*)pvStructInfo + sizeof(BCRYPT_RSAKEY_BLOB);
|
|
|
|
BYTE *modulus = (BYTE*)pvStructInfo + sizeof(BCRYPT_RSAKEY_BLOB) + hdr->cbPublicExp;
|
|
|
|
BYTE *pubexp_be = CryptMemAlloc(hdr->cbPublicExp);
|
|
|
|
BYTE *modulus_be = CryptMemAlloc(hdr->cbModulus);
|
|
|
|
CRYPT_INTEGER_BLOB pubexp_int = { hdr->cbPublicExp, pubexp_be };
|
|
|
|
CRYPT_INTEGER_BLOB modulus_int = { hdr->cbModulus, modulus_be};
|
|
|
|
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &modulus_int, CRYPT_AsnEncodeUnsignedInteger, 0 },
|
|
|
|
{ &pubexp_int, CRYPT_AsnEncodeInteger, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
/* CNG_RSA_PUBLIC_KEY_BLOB stores the exponent and modulus
|
|
|
|
* in big-endian format, so we need to convert them
|
|
|
|
* to little-endian format before encoding
|
|
|
|
*/
|
|
|
|
CRYPT_CopyReversed(pubexp_be, pubexp, hdr->cbPublicExp);
|
|
|
|
CRYPT_CopyReversed(modulus_be, modulus, hdr->cbModulus);
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
|
|
|
|
CryptMemFree(pubexp_be);
|
|
|
|
CryptMemFree(modulus_be);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-02 16:38:39 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeRsaPubKey(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const BLOBHEADER *hdr = pvStructInfo;
|
2005-09-02 16:38:39 +02:00
|
|
|
|
|
|
|
if (hdr->bType != PUBLICKEYBLOB)
|
|
|
|
{
|
2006-05-11 18:48:01 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2005-09-02 16:38:39 +02:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const RSAPUBKEY *rsaPubKey = (const RSAPUBKEY *)
|
|
|
|
((const BYTE *)pvStructInfo + sizeof(BLOBHEADER));
|
|
|
|
CRYPT_INTEGER_BLOB blob = { rsaPubKey->bitlen / 8,
|
|
|
|
(BYTE *)pvStructInfo + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) };
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
2005-09-28 20:12:29 +02:00
|
|
|
{ &blob, CRYPT_AsnEncodeUnsignedInteger, 0 },
|
2005-09-02 16:38:39 +02:00
|
|
|
{ &rsaPubKey->pubexp, CRYPT_AsnEncodeInt, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2005-09-02 16:38:39 +02:00
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-09-02 16:38:39 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-12 23:59:27 +02:00
|
|
|
BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
|
2005-06-16 18:08:50 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-16 18:08:50 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_DATA_BLOB *blob = pvStructInfo;
|
2005-06-27 21:48:24 +02:00
|
|
|
DWORD bytesNeeded, lenBytes;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%d, %p), %08x, %p, %p, %d\n", blob->cbData, blob->pbData,
|
2011-12-16 13:35:29 +01:00
|
|
|
dwFlags, pEncodePara, pbEncoded, pbEncoded ? *pcbEncoded : 0);
|
2005-07-12 19:00:23 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
CRYPT_EncodeLen(blob->cbData, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + blob->cbData;
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_OCTETSTRING;
|
|
|
|
CRYPT_EncodeLen(blob->cbData, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
if (blob->cbData)
|
|
|
|
memcpy(pbEncoded, blob->pbData, blob->cbData);
|
|
|
|
}
|
|
|
|
}
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%08x)\n", ret, GetLastError());
|
2005-06-27 21:48:24 +02:00
|
|
|
return ret;
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-16 18:08:50 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_BIT_BLOB *blob = pvStructInfo;
|
2005-06-27 21:48:24 +02:00
|
|
|
DWORD bytesNeeded, lenBytes, dataBytes;
|
|
|
|
BYTE unusedBits;
|
2005-06-16 18:08:50 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
/* yep, MS allows cUnusedBits to be >= 8 */
|
2005-07-07 13:26:50 +02:00
|
|
|
if (!blob->cUnusedBits)
|
|
|
|
{
|
|
|
|
dataBytes = blob->cbData;
|
|
|
|
unusedBits = 0;
|
|
|
|
}
|
|
|
|
else if (blob->cbData * 8 > blob->cUnusedBits)
|
2005-06-27 21:48:24 +02:00
|
|
|
{
|
|
|
|
dataBytes = (blob->cbData * 8 - blob->cUnusedBits) / 8 + 1;
|
|
|
|
unusedBits = blob->cUnusedBits >= 8 ? blob->cUnusedBits / 8 :
|
|
|
|
blob->cUnusedBits;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dataBytes = 0;
|
|
|
|
unusedBits = 0;
|
|
|
|
}
|
|
|
|
CRYPT_EncodeLen(dataBytes + 1, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataBytes + 1;
|
|
|
|
if (!pbEncoded)
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_BITSTRING;
|
|
|
|
CRYPT_EncodeLen(dataBytes + 1, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
*pbEncoded++ = unusedBits;
|
|
|
|
if (dataBytes)
|
|
|
|
{
|
|
|
|
BYTE mask = 0xff << unusedBits;
|
|
|
|
|
|
|
|
if (dataBytes > 1)
|
|
|
|
{
|
|
|
|
memcpy(pbEncoded, blob->pbData, dataBytes - 1);
|
|
|
|
pbEncoded += dataBytes - 1;
|
|
|
|
}
|
|
|
|
*pbEncoded = *(blob->pbData + dataBytes - 1) & mask;
|
|
|
|
}
|
|
|
|
}
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-27 21:48:24 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
2005-07-14 12:14:22 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_BIT_BLOB *blob = pvStructInfo;
|
2005-07-14 12:14:22 +02:00
|
|
|
CRYPT_BIT_BLOB newBlob = { blob->cbData, NULL, blob->cUnusedBits };
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
if (newBlob.cbData)
|
|
|
|
{
|
2005-10-28 12:09:26 +02:00
|
|
|
newBlob.pbData = CryptMemAlloc(newBlob.cbData);
|
2005-07-14 12:14:22 +02:00
|
|
|
if (newBlob.pbData)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < newBlob.cbData; i++)
|
|
|
|
newBlob.pbData[newBlob.cbData - i - 1] = blob->pbData[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_AsnEncodeBits(dwCertEncodingType, lpszStructType,
|
|
|
|
&newBlob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2005-10-28 12:09:26 +02:00
|
|
|
CryptMemFree(newBlob.pbData);
|
2005-07-14 12:14:22 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-07-14 12:14:22 +02:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-02 15:27:40 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
|
2005-06-03 13:25:24 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2005-06-02 12:34:03 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
CRYPT_INTEGER_BLOB blob = { sizeof(INT), (BYTE *)pvStructInfo };
|
2005-06-02 12:34:03 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
return CRYPT_AsnEncodeInteger(dwCertEncodingType, X509_MULTI_BYTE_INTEGER,
|
|
|
|
&blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2005-06-02 12:34:03 +02:00
|
|
|
}
|
|
|
|
|
2005-06-15 12:21:32 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-15 12:21:32 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-15 12:21:32 +02:00
|
|
|
{
|
2008-05-25 14:32:22 +02:00
|
|
|
DWORD significantBytes, lenBytes, bytesNeeded;
|
|
|
|
BYTE padByte = 0;
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL pad = FALSE;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_INTEGER_BLOB *blob = pvStructInfo;
|
2005-06-15 12:21:32 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
significantBytes = blob->cbData;
|
|
|
|
if (significantBytes)
|
2005-06-15 12:21:32 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
if (blob->pbData[significantBytes - 1] & 0x80)
|
2005-06-15 12:21:32 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
/* negative, lop off leading (little-endian) 0xffs */
|
|
|
|
for (; significantBytes > 0 &&
|
|
|
|
blob->pbData[significantBytes - 1] == 0xff; significantBytes--)
|
|
|
|
;
|
|
|
|
if (blob->pbData[significantBytes - 1] < 0x80)
|
|
|
|
{
|
|
|
|
padByte = 0xff;
|
|
|
|
pad = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* positive, lop off leading (little-endian) zeroes */
|
|
|
|
for (; significantBytes > 0 &&
|
|
|
|
!blob->pbData[significantBytes - 1]; significantBytes--)
|
|
|
|
;
|
|
|
|
if (significantBytes == 0)
|
|
|
|
significantBytes = 1;
|
|
|
|
if (blob->pbData[significantBytes - 1] > 0x7f)
|
|
|
|
{
|
|
|
|
padByte = 0;
|
|
|
|
pad = TRUE;
|
|
|
|
}
|
2005-06-15 12:21:32 +02:00
|
|
|
}
|
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
if (pad)
|
|
|
|
CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
|
2005-06-15 12:21:32 +02:00
|
|
|
else
|
2005-06-27 21:48:24 +02:00
|
|
|
CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + significantBytes;
|
|
|
|
if (pad)
|
|
|
|
bytesNeeded++;
|
|
|
|
if (!pbEncoded)
|
2005-06-15 12:21:32 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
2005-06-15 12:21:32 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_INTEGER;
|
|
|
|
if (pad)
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
*pbEncoded++ = padByte;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
}
|
|
|
|
for (; significantBytes > 0; significantBytes--)
|
|
|
|
*(pbEncoded++) = blob->pbData[significantBytes - 1];
|
2005-06-15 12:21:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-16 18:08:50 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
2008-05-25 14:32:22 +02:00
|
|
|
DWORD significantBytes, lenBytes, bytesNeeded;
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL pad = FALSE;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_INTEGER_BLOB *blob = pvStructInfo;
|
2005-06-16 18:08:50 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
significantBytes = blob->cbData;
|
|
|
|
if (significantBytes)
|
|
|
|
{
|
|
|
|
/* positive, lop off leading (little-endian) zeroes */
|
|
|
|
for (; significantBytes > 0 && !blob->pbData[significantBytes - 1];
|
|
|
|
significantBytes--)
|
|
|
|
;
|
|
|
|
if (significantBytes == 0)
|
|
|
|
significantBytes = 1;
|
|
|
|
if (blob->pbData[significantBytes - 1] > 0x7f)
|
|
|
|
pad = TRUE;
|
|
|
|
}
|
|
|
|
if (pad)
|
|
|
|
CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
|
|
|
|
else
|
|
|
|
CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + significantBytes;
|
|
|
|
if (pad)
|
|
|
|
bytesNeeded++;
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_INTEGER;
|
|
|
|
if (pad)
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
*pbEncoded++ = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
}
|
|
|
|
for (; significantBytes > 0; significantBytes--)
|
|
|
|
*(pbEncoded++) = blob->pbData[significantBytes - 1];
|
|
|
|
}
|
|
|
|
}
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-16 18:08:50 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
2005-06-16 18:08:50 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
2005-06-15 12:21:32 +02:00
|
|
|
}
|
|
|
|
|
2005-06-16 18:08:50 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeEnumerated(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
CRYPT_INTEGER_BLOB blob;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
/* Encode as an unsigned integer, then change the tag to enumerated */
|
|
|
|
blob.cbData = sizeof(DWORD);
|
|
|
|
blob.pbData = (BYTE *)pvStructInfo;
|
|
|
|
ret = CRYPT_AsnEncodeUnsignedInteger(dwCertEncodingType,
|
|
|
|
X509_MULTI_BYTE_UINT, &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
if (ret && pbEncoded)
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
pbEncoded[0] = ASN_ENUMERATED;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-08 20:31:21 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeUtcTime(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-08 20:31:21 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-08 20:31:21 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SYSTEMTIME sysTime;
|
2017-08-30 09:42:17 +02:00
|
|
|
/* sorry, magic number: enough for tag, len, YYMMDDHHMMSSZ. I use a
|
2005-06-27 21:48:24 +02:00
|
|
|
* temporary buffer because the output buffer is not NULL-terminated.
|
|
|
|
*/
|
2017-08-30 09:42:17 +02:00
|
|
|
static const DWORD bytesNeeded = 15;
|
|
|
|
char buf[40];
|
2005-06-27 21:48:24 +02:00
|
|
|
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Sanity check the year, this is a two-digit year format */
|
2009-01-26 11:01:47 +01:00
|
|
|
ret = FileTimeToSystemTime(pvStructInfo, &sysTime);
|
2005-06-27 21:48:24 +02:00
|
|
|
if (ret && (sysTime.wYear < 1950 || sysTime.wYear > 2050))
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_BAD_ENCODE);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
buf[0] = ASN_UTCTIME;
|
|
|
|
buf[1] = bytesNeeded - 2;
|
|
|
|
snprintf(buf + 2, sizeof(buf) - 2,
|
|
|
|
"%02d%02d%02d%02d%02d%02dZ", sysTime.wYear >= 2000 ?
|
|
|
|
sysTime.wYear - 2000 : sysTime.wYear - 1900,
|
2006-04-13 11:45:12 +02:00
|
|
|
sysTime.wMonth, sysTime.wDay, sysTime.wHour,
|
2005-06-27 21:48:24 +02:00
|
|
|
sysTime.wMinute, sysTime.wSecond);
|
|
|
|
memcpy(pbEncoded, buf, bytesNeeded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-08 20:31:21 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-10 21:28:58 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
2005-06-10 21:28:58 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
2005-06-08 20:31:21 +02:00
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeGeneralizedTime(DWORD dwCertEncodingType,
|
2005-06-08 20:31:21 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-08 20:31:21 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-08 20:31:21 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SYSTEMTIME sysTime;
|
2017-08-30 09:42:17 +02:00
|
|
|
/* sorry, magic number: enough for tag, len, YYYYMMDDHHMMSSZ. I use a
|
2005-06-27 21:48:24 +02:00
|
|
|
* temporary buffer because the output buffer is not NULL-terminated.
|
|
|
|
*/
|
2017-08-30 09:42:17 +02:00
|
|
|
static const DWORD bytesNeeded = 17;
|
|
|
|
char buf[40];
|
2005-06-27 21:48:24 +02:00
|
|
|
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
ret = FileTimeToSystemTime(pvStructInfo, &sysTime);
|
2005-06-27 21:48:24 +02:00
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
buf[0] = ASN_GENERALTIME;
|
|
|
|
buf[1] = bytesNeeded - 2;
|
|
|
|
snprintf(buf + 2, sizeof(buf) - 2, "%04d%02d%02d%02d%02d%02dZ",
|
2006-04-13 11:45:12 +02:00
|
|
|
sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour,
|
2005-06-27 21:48:24 +02:00
|
|
|
sysTime.wMinute, sysTime.wSecond);
|
|
|
|
memcpy(pbEncoded, buf, bytesNeeded);
|
|
|
|
}
|
|
|
|
}
|
2005-06-08 20:31:21 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-10 21:28:58 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
2005-06-10 21:28:58 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
2005-06-08 20:31:21 +02:00
|
|
|
}
|
|
|
|
|
2005-06-10 21:28:58 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
SYSTEMTIME sysTime;
|
|
|
|
|
|
|
|
/* Check the year, if it's in the UTCTime range call that encode func */
|
2009-01-26 11:01:47 +01:00
|
|
|
if (!FileTimeToSystemTime(pvStructInfo, &sysTime))
|
2005-06-27 21:48:24 +02:00
|
|
|
return FALSE;
|
|
|
|
if (sysTime.wYear >= 1950 && sysTime.wYear <= 2050)
|
|
|
|
ret = CRYPT_AsnEncodeUtcTime(dwCertEncodingType, lpszStructType,
|
|
|
|
pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
else
|
|
|
|
ret = CRYPT_AsnEncodeGeneralizedTime(dwCertEncodingType,
|
|
|
|
lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded);
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-10 21:28:58 +02:00
|
|
|
{
|
2005-06-13 13:39:35 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
2005-06-27 21:48:24 +02:00
|
|
|
ret = FALSE;
|
2005-06-10 21:28:58 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
2005-06-10 21:28:58 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-27 11:42:15 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
BOOL ret;
|
2005-06-27 11:42:15 +02:00
|
|
|
|
2005-06-27 21:48:24 +02:00
|
|
|
__TRY
|
2005-06-27 11:42:15 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_SEQUENCE_OF_ANY *seq = pvStructInfo;
|
2005-06-27 21:48:24 +02:00
|
|
|
|
|
|
|
for (i = 0, dataLen = 0; i < seq->cValue; i++)
|
|
|
|
dataLen += seq->rgValue[i].cbData;
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
|
|
|
|
pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
|
|
|
*pbEncoded++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
|
|
|
|
pbEncoded += lenBytes;
|
|
|
|
for (i = 0; i < seq->cValue; i++)
|
|
|
|
{
|
|
|
|
memcpy(pbEncoded, seq->rgValue[i].pbData,
|
|
|
|
seq->rgValue[i].cbData);
|
|
|
|
pbEncoded += seq->rgValue[i].cbData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-06-27 11:42:15 +02:00
|
|
|
{
|
2005-06-27 21:48:24 +02:00
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
2005-06-27 21:48:24 +02:00
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
2005-06-27 11:42:15 +02:00
|
|
|
}
|
|
|
|
|
2005-12-16 12:41:28 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeDistPoint(const CRL_DIST_POINT *distPoint,
|
|
|
|
BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
struct AsnEncodeSequenceItem items[3] = { { 0 } };
|
|
|
|
struct AsnConstructedItem constructed = { 0 };
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
|
|
|
|
DWORD cItem = 0, cSwapped = 0;
|
|
|
|
|
|
|
|
switch (distPoint->DistPointName.dwDistPointNameChoice)
|
|
|
|
{
|
|
|
|
case CRL_DIST_POINT_NO_NAME:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
case CRL_DIST_POINT_FULL_NAME:
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &distPoint->DistPointName.u.FullName;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
constructed.tag = 0;
|
|
|
|
constructed.pvStructInfo = &swapped[cSwapped];
|
|
|
|
constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
items[cItem].pvStructInfo = &constructed;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
break;
|
|
|
|
case CRL_DIST_POINT_ISSUER_RDN_NAME:
|
|
|
|
FIXME("unimplemented for CRL_DIST_POINT_ISSUER_RDN_NAME\n");
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (ret && distPoint->ReasonFlags.cbData)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &distPoint->ReasonFlags;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (ret && distPoint->CRLIssuer.cAltEntry)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 2;
|
|
|
|
swapped[cSwapped].pvStructInfo = &distPoint->CRLIssuer;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
|
|
|
|
pbEncoded, pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCRLDistPoints(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRL_DIST_POINTS_INFO *info = pvStructInfo;
|
2005-12-16 12:41:28 +01:00
|
|
|
|
|
|
|
if (!info->cDistPoint)
|
|
|
|
{
|
2006-05-11 18:48:01 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2005-12-16 12:41:28 +01:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD bytesNeeded, dataLen, lenBytes, i;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0, dataLen = 0; ret && i < info->cDistPoint; i++)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeDistPoint(&info->rgDistPoint[i], NULL,
|
|
|
|
&len);
|
|
|
|
if (ret)
|
|
|
|
dataLen += len;
|
|
|
|
else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
|
|
|
|
{
|
|
|
|
/* Have to propagate index of failing character */
|
|
|
|
*pcbEncoded = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
|
|
|
|
bytesNeeded = 1 + lenBytes + dataLen;
|
|
|
|
if (!pbEncoded)
|
|
|
|
{
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2005-12-16 12:41:28 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(dataLen, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2005-12-16 12:41:28 +01:00
|
|
|
for (i = 0; ret && i < info->cDistPoint; i++)
|
|
|
|
{
|
|
|
|
DWORD len = dataLen;
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeDistPoint(
|
2011-01-19 04:17:46 +01:00
|
|
|
&info->rgDistPoint[i], out, &len);
|
2005-12-16 12:41:28 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += len;
|
2005-12-16 12:41:28 +01:00
|
|
|
dataLen -= len;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2005-12-16 12:41:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2005-12-16 12:41:28 +01:00
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:39:27 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_ENHKEY_USAGE *usage = pvStructInfo;
|
2006-02-16 19:39:27 +01:00
|
|
|
DWORD bytesNeeded = 0, lenBytes, size, i;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
for (i = 0; ret && i < usage->cUsageIdentifier; i++)
|
|
|
|
{
|
|
|
|
ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
|
|
|
|
usage->rgpszUsageIdentifier[i],
|
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
bytesNeeded += size;
|
|
|
|
}
|
|
|
|
CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
|
|
|
|
bytesNeeded += 1 + lenBytes;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pbEncoded)
|
|
|
|
*pcbEncoded = bytesNeeded;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pbEncoded, pcbEncoded, bytesNeeded)))
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
BYTE *out;
|
|
|
|
|
2006-02-16 19:39:27 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
pbEncoded = *(BYTE **)pbEncoded;
|
2011-01-19 04:17:46 +01:00
|
|
|
out = pbEncoded;
|
|
|
|
*out++ = ASN_SEQUENCEOF;
|
|
|
|
CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
|
|
|
|
out += lenBytes;
|
2006-02-16 19:39:27 +01:00
|
|
|
for (i = 0; ret && i < usage->cUsageIdentifier; i++)
|
|
|
|
{
|
|
|
|
size = bytesNeeded;
|
|
|
|
ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
|
|
|
|
usage->rgpszUsageIdentifier[i],
|
2011-01-19 04:17:46 +01:00
|
|
|
dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, out, &size);
|
2006-02-16 19:39:27 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2011-01-19 04:17:46 +01:00
|
|
|
out += size;
|
2006-02-16 19:39:27 +01:00
|
|
|
bytesNeeded -= size;
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 04:17:46 +01:00
|
|
|
if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
|
|
|
|
CRYPT_FreeSpace(pEncodePara, pbEncoded);
|
2006-02-16 19:39:27 +01: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_AsnEncodeIssuingDistPoint(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRL_ISSUING_DIST_POINT *point = pvStructInfo;
|
2006-06-20 00:12:51 +02:00
|
|
|
struct AsnEncodeSequenceItem items[6] = { { 0 } };
|
|
|
|
struct AsnConstructedItem constructed = { 0 };
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[5] = { { 0 } };
|
|
|
|
DWORD cItem = 0, cSwapped = 0;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
switch (point->DistPointName.dwDistPointNameChoice)
|
|
|
|
{
|
|
|
|
case CRL_DIST_POINT_NO_NAME:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
case CRL_DIST_POINT_FULL_NAME:
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &point->DistPointName.u.FullName;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
constructed.tag = 0;
|
|
|
|
constructed.pvStructInfo = &swapped[cSwapped];
|
|
|
|
constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
items[cItem].pvStructInfo = &constructed;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (ret && point->fOnlyContainsUserCerts)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &point->fOnlyContainsUserCerts;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (ret && point->fOnlyContainsCACerts)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 2;
|
|
|
|
swapped[cSwapped].pvStructInfo = &point->fOnlyContainsCACerts;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (ret && point->OnlySomeReasonFlags.cbData)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 3;
|
|
|
|
swapped[cSwapped].pvStructInfo = &point->OnlySomeReasonFlags;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (ret && point->fIndirectCRL)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 4;
|
|
|
|
swapped[cSwapped].pvStructInfo = &point->fIndirectCRL;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-24 00:28:38 +01:00
|
|
|
static BOOL CRYPT_AsnEncodeGeneralSubtree(DWORD dwCertEncodingType,
|
2007-10-11 01:05:12 +02:00
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_GENERAL_SUBTREE *subtree = pvStructInfo;
|
2007-10-11 01:05:12 +02:00
|
|
|
struct AsnEncodeSequenceItem items[3] = {
|
|
|
|
{ &subtree->Base, CRYPT_AsnEncodeAltNameEntry, 0 },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
|
|
|
|
DWORD cItem = 1, cSwapped = 0;
|
|
|
|
|
|
|
|
if (subtree->dwMinimum)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &subtree->dwMinimum;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (subtree->fMaximum)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &subtree->dwMaximum;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem, dwFlags,
|
|
|
|
pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeNameConstraints(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
2007-10-19 21:36:06 +02:00
|
|
|
CRYPT_BLOB_ARRAY permitted = { 0, NULL }, excluded = { 0, NULL };
|
2007-10-11 01:05:12 +02:00
|
|
|
|
|
|
|
TRACE("%p\n", pvStructInfo);
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_NAME_CONSTRAINTS_INFO *constraints = pvStructInfo;
|
2007-10-11 01:05:12 +02:00
|
|
|
struct AsnEncodeSequenceItem items[2] = { { 0 } };
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
|
|
|
|
DWORD i, cItem = 0, cSwapped = 0;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
if (constraints->cPermittedSubtree)
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
permitted.rgBlob = CryptMemAlloc(
|
2007-10-11 01:05:12 +02:00
|
|
|
constraints->cPermittedSubtree * sizeof(CRYPT_DER_BLOB));
|
2007-10-19 21:36:06 +02:00
|
|
|
if (permitted.rgBlob)
|
2007-10-11 01:05:12 +02:00
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
permitted.cBlob = constraints->cPermittedSubtree;
|
|
|
|
memset(permitted.rgBlob, 0,
|
|
|
|
permitted.cBlob * sizeof(CRYPT_DER_BLOB));
|
|
|
|
for (i = 0; ret && i < permitted.cBlob; i++)
|
2007-10-11 01:05:12 +02:00
|
|
|
ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
|
|
|
|
NULL, &constraints->rgPermittedSubtree[i],
|
|
|
|
CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
2007-10-19 21:36:06 +02:00
|
|
|
(BYTE *)&permitted.rgBlob[i].pbData,
|
|
|
|
&permitted.rgBlob[i].cbData);
|
2007-10-11 01:05:12 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &permitted;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (constraints->cExcludedSubtree)
|
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
excluded.rgBlob = CryptMemAlloc(
|
2007-10-11 01:05:12 +02:00
|
|
|
constraints->cExcludedSubtree * sizeof(CRYPT_DER_BLOB));
|
2007-10-19 21:36:06 +02:00
|
|
|
if (excluded.rgBlob)
|
2007-10-11 01:05:12 +02:00
|
|
|
{
|
2007-10-19 21:36:06 +02:00
|
|
|
excluded.cBlob = constraints->cExcludedSubtree;
|
|
|
|
memset(excluded.rgBlob, 0,
|
|
|
|
excluded.cBlob * sizeof(CRYPT_DER_BLOB));
|
|
|
|
for (i = 0; ret && i < excluded.cBlob; i++)
|
2007-10-11 01:05:12 +02:00
|
|
|
ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
|
|
|
|
NULL, &constraints->rgExcludedSubtree[i],
|
|
|
|
CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
2007-10-19 21:36:06 +02:00
|
|
|
(BYTE *)&excluded.rgBlob[i].pbData,
|
|
|
|
&excluded.rgBlob[i].cbData);
|
2007-10-11 01:05:12 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &excluded;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2007-10-19 21:36:06 +02:00
|
|
|
for (i = 0; i < permitted.cBlob; i++)
|
|
|
|
LocalFree(permitted.rgBlob[i].pbData);
|
|
|
|
for (i = 0; i < excluded.cBlob; i++)
|
|
|
|
LocalFree(excluded.rgBlob[i].pbData);
|
2007-10-11 01:05:12 +02:00
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2007-10-19 21:36:06 +02:00
|
|
|
CryptMemFree(permitted.rgBlob);
|
|
|
|
CryptMemFree(excluded.rgBlob);
|
2007-10-11 01:05:12 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-13 00:02:57 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeIssuerSerialNumber(
|
|
|
|
DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
|
|
|
|
DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
|
|
|
|
DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2009-01-26 11:01:47 +01:00
|
|
|
const CERT_ISSUER_SERIAL_NUMBER *issuerSerial = pvStructInfo;
|
2007-06-13 00:02:57 +02:00
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &issuerSerial->Issuer, CRYPT_CopyEncodedBlob, 0 },
|
|
|
|
{ &issuerSerial->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2007-06-13 00:02:57 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodePKCSSignerInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (!(dwCertEncodingType & PKCS_7_ASN_ENCODING))
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CMSG_SIGNER_INFO *info = pvStructInfo;
|
2007-06-13 00:02:57 +02:00
|
|
|
|
|
|
|
if (!info->Issuer.cbData)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[7] = {
|
|
|
|
{ &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
|
|
|
|
{ &info->Issuer, CRYPT_AsnEncodeIssuerSerialNumber, 0 },
|
2007-07-12 23:58:08 +02:00
|
|
|
{ &info->HashAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
|
|
|
|
0 },
|
2007-06-13 00:02:57 +02:00
|
|
|
};
|
2007-07-26 03:13:45 +02:00
|
|
|
struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
|
|
|
|
DWORD cItem = 3, cSwapped = 0;
|
2007-06-13 00:02:57 +02:00
|
|
|
|
|
|
|
if (info->AuthAttrs.cAttr)
|
|
|
|
{
|
2007-07-26 03:13:45 +02:00
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->AuthAttrs;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
2007-06-13 00:02:57 +02:00
|
|
|
cItem++;
|
|
|
|
}
|
2007-07-26 03:13:45 +02:00
|
|
|
items[cItem].pvStructInfo = &info->HashEncryptionAlgorithm;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
|
|
|
|
cItem++;
|
|
|
|
items[cItem].pvStructInfo = &info->EncryptedHash;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
|
|
|
|
cItem++;
|
2007-06-13 00:02:57 +02:00
|
|
|
if (info->UnauthAttrs.cAttr)
|
|
|
|
{
|
2007-07-26 03:13:45 +02:00
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->UnauthAttrs;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
2007-06-13 00:02:57 +02:00
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-14 21:50:57 +02:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeCMSSignerInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (!(dwCertEncodingType & PKCS_7_ASN_ENCODING))
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
__TRY
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CMSG_CMS_SIGNER_INFO *info = pvStructInfo;
|
2008-08-14 21:50:57 +02:00
|
|
|
|
|
|
|
if (info->SignerId.dwIdChoice != CERT_ID_ISSUER_SERIAL_NUMBER &&
|
|
|
|
info->SignerId.dwIdChoice != CERT_ID_KEY_IDENTIFIER)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER &&
|
|
|
|
!info->SignerId.u.IssuerSerialNumber.Issuer.cbData)
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[7] = {
|
|
|
|
{ &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
|
|
|
|
};
|
|
|
|
struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
|
|
|
|
DWORD cItem = 1, cSwapped = 0;
|
|
|
|
|
|
|
|
if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
|
|
|
|
{
|
|
|
|
items[cItem].pvStructInfo =
|
|
|
|
&info->SignerId.u.IssuerSerialNumber.Issuer;
|
|
|
|
items[cItem].encodeFunc =
|
|
|
|
CRYPT_AsnEncodeIssuerSerialNumber;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->SignerId.u.KeyId;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeOctets;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
items[cItem].pvStructInfo = &info->HashAlgorithm;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
|
|
|
|
cItem++;
|
|
|
|
if (info->AuthAttrs.cAttr)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->AuthAttrs;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
items[cItem].pvStructInfo = &info->HashEncryptionAlgorithm;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
|
|
|
|
cItem++;
|
|
|
|
items[cItem].pvStructInfo = &info->EncryptedHash;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
|
|
|
|
cItem++;
|
|
|
|
if (info->UnauthAttrs.cAttr)
|
|
|
|
{
|
|
|
|
swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &info->UnauthAttrs;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
|
|
|
|
dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__EXCEPT_PAGE_FAULT
|
|
|
|
{
|
|
|
|
SetLastError(STATUS_ACCESS_VIOLATION);
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-20 20:57:21 +02:00
|
|
|
BOOL CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO *signedInfo, void *pvData,
|
2007-07-24 03:23:00 +02:00
|
|
|
DWORD *pcbData)
|
|
|
|
{
|
|
|
|
struct AsnEncodeSequenceItem items[7] = {
|
|
|
|
{ &signedInfo->version, CRYPT_AsnEncodeInt, 0 },
|
|
|
|
};
|
2007-07-24 03:23:43 +02:00
|
|
|
struct DERSetDescriptor digestAlgorithmsSet = { 0 }, certSet = { 0 };
|
2007-07-24 03:24:11 +02:00
|
|
|
struct DERSetDescriptor crlSet = { 0 }, signerSet = { 0 };
|
2007-07-24 03:23:43 +02:00
|
|
|
struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
|
|
|
|
DWORD cItem = 1, cSwapped = 0;
|
2007-07-24 03:23:00 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
if (signedInfo->cSignerInfo)
|
|
|
|
{
|
|
|
|
digestAlgorithmsSet.cItems = signedInfo->cSignerInfo;
|
|
|
|
digestAlgorithmsSet.items = signedInfo->rgSignerInfo;
|
2008-08-20 20:57:21 +02:00
|
|
|
digestAlgorithmsSet.itemSize = sizeof(CMSG_CMS_SIGNER_INFO);
|
2007-07-24 03:23:00 +02:00
|
|
|
digestAlgorithmsSet.itemOffset =
|
2008-08-20 20:57:21 +02:00
|
|
|
offsetof(CMSG_CMS_SIGNER_INFO, HashAlgorithm);
|
2007-07-24 03:23:00 +02:00
|
|
|
digestAlgorithmsSet.encode = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
|
|
|
|
items[cItem].pvStructInfo = &digestAlgorithmsSet;
|
|
|
|
items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
items[cItem].pvStructInfo = &signedInfo->content;
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodePKCSContentInfoInternal;
|
|
|
|
cItem++;
|
2007-07-24 03:23:43 +02:00
|
|
|
if (signedInfo->cCertEncoded)
|
|
|
|
{
|
|
|
|
certSet.cItems = signedInfo->cCertEncoded;
|
|
|
|
certSet.items = signedInfo->rgCertEncoded;
|
|
|
|
certSet.itemSize = sizeof(CERT_BLOB);
|
|
|
|
certSet.itemOffset = 0;
|
|
|
|
certSet.encode = CRYPT_CopyEncodedBlob;
|
|
|
|
swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 0;
|
|
|
|
swapped[cSwapped].pvStructInfo = &certSet;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
2007-07-24 03:24:11 +02:00
|
|
|
if (signedInfo->cCrlEncoded)
|
|
|
|
{
|
|
|
|
crlSet.cItems = signedInfo->cCrlEncoded;
|
|
|
|
crlSet.items = signedInfo->rgCrlEncoded;
|
|
|
|
crlSet.itemSize = sizeof(CRL_BLOB);
|
|
|
|
crlSet.itemOffset = 0;
|
|
|
|
crlSet.encode = CRYPT_CopyEncodedBlob;
|
|
|
|
swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 1;
|
|
|
|
swapped[cSwapped].pvStructInfo = &crlSet;
|
|
|
|
swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
|
|
|
|
items[cItem].pvStructInfo = &swapped[cSwapped];
|
|
|
|
items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
|
|
|
|
cSwapped++;
|
|
|
|
cItem++;
|
|
|
|
}
|
2007-07-24 03:23:00 +02:00
|
|
|
if (ret && signedInfo->cSignerInfo)
|
|
|
|
{
|
|
|
|
signerSet.cItems = signedInfo->cSignerInfo;
|
|
|
|
signerSet.items = signedInfo->rgSignerInfo;
|
2008-08-20 20:57:21 +02:00
|
|
|
signerSet.itemSize = sizeof(CMSG_CMS_SIGNER_INFO);
|
2007-07-24 03:23:00 +02:00
|
|
|
signerSet.itemOffset = 0;
|
2008-08-20 20:57:21 +02:00
|
|
|
signerSet.encode = CRYPT_AsnEncodeCMSSignerInfo;
|
2007-07-24 03:23:00 +02:00
|
|
|
items[cItem].pvStructInfo = &signerSet;
|
|
|
|
items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
|
|
|
|
cItem++;
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
|
|
|
|
items, cItem, 0, NULL, pvData, pcbData);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-01 12:40:27 +01:00
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeRecipientInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
const CMSG_KEY_TRANS_RECIPIENT_INFO *info = pvStructInfo;
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
|
|
|
|
{ &info->RecipientId.u.IssuerSerialNumber,
|
|
|
|
CRYPT_AsnEncodeIssuerSerialNumber, 0 },
|
|
|
|
{ &info->KeyEncryptionAlgorithm,
|
|
|
|
CRYPT_AsnEncodeAlgorithmIdWithNullParams, 0 },
|
|
|
|
{ &info->EncryptedKey, CRYPT_AsnEncodeOctets, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2010-12-01 12:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_AsnEncodeEncryptedContentInfo(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
|
|
|
|
PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
const CRYPT_ENCRYPTED_CONTENT_INFO *info = pvStructInfo;
|
|
|
|
struct AsnEncodeTagSwappedItem swapped = { ASN_CONTEXT | 0,
|
|
|
|
&info->encryptedContent, CRYPT_AsnEncodeOctets };
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ info->contentType, CRYPT_AsnEncodeOid, 0 },
|
|
|
|
{ &info->contentEncryptionAlgorithm,
|
|
|
|
CRYPT_AsnEncodeAlgorithmIdWithNullParams, 0 },
|
|
|
|
{ &swapped, CRYPT_AsnEncodeSwapTag, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
|
2010-12-01 12:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CRYPT_AsnEncodePKCSEnvelopedData(const CRYPT_ENVELOPED_DATA *envelopedData,
|
|
|
|
void *pvData, DWORD *pcbData)
|
|
|
|
{
|
|
|
|
struct DERSetDescriptor recipientInfosSet = { envelopedData->cRecipientInfo,
|
|
|
|
envelopedData->rgRecipientInfo, sizeof(CMSG_KEY_TRANS_RECIPIENT_INFO), 0,
|
|
|
|
CRYPT_AsnEncodeRecipientInfo };
|
|
|
|
struct AsnEncodeSequenceItem items[] = {
|
|
|
|
{ &envelopedData->version, CRYPT_AsnEncodeInt, 0 },
|
|
|
|
{ &recipientInfosSet, CRYPT_DEREncodeItemsAsSet, 0 },
|
|
|
|
{ &envelopedData->encryptedContentInfo,
|
|
|
|
CRYPT_AsnEncodeEncryptedContentInfo, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
|
2018-06-20 09:37:55 +02:00
|
|
|
ARRAY_SIZE(items), 0, NULL, pvData, pcbData);
|
2010-12-01 12:40:27 +01:00
|
|
|
}
|
|
|
|
|
2007-08-10 20:21:03 +02:00
|
|
|
static CryptEncodeObjectExFunc CRYPT_GetBuiltinEncoder(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType)
|
2005-05-20 21:37:26 +02:00
|
|
|
{
|
2005-06-03 13:25:24 +02:00
|
|
|
CryptEncodeObjectExFunc encodeFunc = NULL;
|
2005-05-29 21:59:33 +02:00
|
|
|
|
2005-06-08 20:31:21 +02:00
|
|
|
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:21:03 +02:00
|
|
|
return NULL;
|
2005-06-08 20:31:21 +02:00
|
|
|
}
|
2005-05-29 21:59:33 +02:00
|
|
|
|
2010-01-29 01:39:26 +01:00
|
|
|
if (IS_INTOID(lpszStructType))
|
2005-05-29 21:59:33 +02:00
|
|
|
{
|
2005-06-08 20:31:21 +02:00
|
|
|
switch (LOWORD(lpszStructType))
|
2005-05-29 21:59:33 +02:00
|
|
|
{
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_CERT):
|
2005-07-14 12:14:22 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeCert;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_CERT_TO_BE_SIGNED):
|
2005-07-12 19:00:23 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeCertInfo;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_CERT_CRL_TO_BE_SIGNED):
|
2005-08-29 16:12:44 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeCRLInfo;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_EXTENSIONS):
|
2005-07-01 17:40:45 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeExtensions;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_NAME_VALUE):
|
2006-06-02 22:20:13 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeNameValue;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_NAME):
|
2005-06-13 13:39:35 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeName;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_PUBLIC_KEY_INFO):
|
2005-07-07 13:26:50 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodePubKeyInfo;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_AUTHORITY_KEY_ID):
|
2006-09-27 06:20:35 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_ALTERNATE_NAME):
|
2005-08-29 16:12:44 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_BASIC_CONSTRAINTS):
|
2006-02-16 19:39:27 +01:00
|
|
|
encodeFunc = CRYPT_AsnEncodeBasicConstraints;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_BASIC_CONSTRAINTS2):
|
2005-06-27 11:42:15 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
|
|
|
|
break;
|
2008-11-15 01:06:13 +01:00
|
|
|
case LOWORD(X509_CERT_POLICIES):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCertPolicies;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(RSA_CSP_PUBLICKEYBLOB):
|
2005-09-02 16:38:39 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeRsaPubKey;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_UNICODE_NAME):
|
2006-07-26 16:44:47 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeUnicodeName;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(PKCS_CONTENT_INFO):
|
2007-06-12 23:40:04 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodePKCSContentInfo;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(PKCS_ATTRIBUTE):
|
2007-06-13 00:00:01 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodePKCSAttribute;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_UNICODE_NAME_VALUE):
|
2006-07-19 16:44:48 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeUnicodeNameValue;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_OCTET_STRING):
|
2005-06-16 18:08:50 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeOctets;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_BITS):
|
|
|
|
case LOWORD(X509_KEY_USAGE):
|
2005-06-16 18:08:50 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeBits;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_INTEGER):
|
2005-06-08 20:31:21 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeInt;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_MULTI_BYTE_INTEGER):
|
2005-06-15 12:21:32 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeInteger;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_MULTI_BYTE_UINT):
|
2005-06-16 18:08:50 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeUnsignedInteger;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_ENUMERATED):
|
2005-06-16 18:08:50 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeEnumerated;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_CHOICE_OF_TIME):
|
2005-06-08 20:31:21 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_AUTHORITY_KEY_ID2):
|
2007-06-12 23:39:19 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
|
|
|
|
break;
|
2008-08-27 19:17:09 +02:00
|
|
|
case LOWORD(X509_AUTHORITY_INFO_ACCESS):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAuthorityInfoAccess;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_SEQUENCE_OF_ANY):
|
2005-06-27 11:42:15 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(PKCS_UTC_TIME):
|
2005-06-08 20:31:21 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeUtcTime;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_CRL_DIST_POINTS):
|
2005-12-16 12:41:28 +01:00
|
|
|
encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_ENHANCED_KEY_USAGE):
|
2006-02-16 19:39:27 +01:00
|
|
|
encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
|
|
|
|
break;
|
2008-08-27 22:21:50 +02:00
|
|
|
case LOWORD(PKCS_CTL):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCTL;
|
|
|
|
break;
|
2008-08-20 21:11:15 +02:00
|
|
|
case LOWORD(PKCS_SMIME_CAPABILITIES):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeSMIMECapabilities;
|
|
|
|
break;
|
2008-11-13 22:45:29 +01:00
|
|
|
case LOWORD(X509_PKIX_POLICY_QUALIFIER_USERNOTICE):
|
|
|
|
encodeFunc = CRYPT_AsnEncodePolicyQualifierUserNotice;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(PKCS_ATTRIBUTES):
|
2007-06-13 00:01:34 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodePKCSAttributes;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_ISSUING_DIST_POINT):
|
2006-06-20 00:12:51 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(X509_NAME_CONSTRAINTS):
|
2007-10-11 01:05:12 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodeNameConstraints;
|
|
|
|
break;
|
2009-11-06 20:52:19 +01:00
|
|
|
case LOWORD(X509_POLICY_MAPPINGS):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCertPolicyMappings;
|
|
|
|
break;
|
2009-11-06 21:35:49 +01:00
|
|
|
case LOWORD(X509_POLICY_CONSTRAINTS):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCertPolicyConstraints;
|
|
|
|
break;
|
2007-10-15 13:58:45 +02:00
|
|
|
case LOWORD(PKCS7_SIGNER_INFO):
|
2007-06-13 00:02:57 +02:00
|
|
|
encodeFunc = CRYPT_AsnEncodePKCSSignerInfo;
|
|
|
|
break;
|
2008-08-14 21:50:57 +02:00
|
|
|
case LOWORD(CMS_SIGNER_INFO):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCMSSignerInfo;
|
|
|
|
break;
|
2021-07-12 11:40:50 +02:00
|
|
|
case LOWORD(CNG_RSA_PUBLIC_KEY_BLOB):
|
|
|
|
encodeFunc = CRYPT_AsnEncodeRsaPubKey_Bcrypt;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("Unimplemented encoder for lpszStructType OID %d\n", LOWORD(lpszStructType));
|
2005-05-29 21:59:33 +02:00
|
|
|
}
|
|
|
|
}
|
2005-07-01 17:40:45 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeExtensions;
|
2005-06-08 20:31:21 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeUtcTime;
|
2008-08-20 21:11:15 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_RSA_SMIMECapabilities))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeUtcTime;
|
2006-09-27 06:20:35 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
|
2009-11-06 20:52:19 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_LEGACY_POLICY_MAPPINGS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCertPolicyMappings;
|
2007-06-12 23:39:19 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER2))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
|
2005-06-16 18:08:50 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeEnumerated;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeBits;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeOctets;
|
2006-02-16 19:39:27 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeBasicConstraints;
|
2005-06-27 11:42:15 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
|
2005-08-29 16:12:44 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAltName;
|
|
|
|
else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAltName;
|
2006-02-16 19:39:27 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CRL_DIST_POINTS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
|
2008-11-15 01:06:13 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CERT_POLICIES))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCertPolicies;
|
2009-11-06 20:52:19 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_POLICY_MAPPINGS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCertPolicyMappings;
|
2009-11-06 21:35:49 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_POLICY_CONSTRAINTS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCertPolicyConstraints;
|
2006-02-16 19:39:27 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_ENHANCED_KEY_USAGE))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
|
2006-06-20 00:12:51 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_ISSUING_DIST_POINT))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
|
2007-10-11 01:05:12 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_NAME_CONSTRAINTS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeNameConstraints;
|
2008-08-27 19:17:09 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_AUTHORITY_INFO_ACCESS))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeAuthorityInfoAccess;
|
2008-11-13 22:45:29 +01:00
|
|
|
else if (!strcmp(lpszStructType, szOID_PKIX_POLICY_QUALIFIER_USERNOTICE))
|
|
|
|
encodeFunc = CRYPT_AsnEncodePolicyQualifierUserNotice;
|
2008-08-27 22:21:50 +02:00
|
|
|
else if (!strcmp(lpszStructType, szOID_CTL))
|
|
|
|
encodeFunc = CRYPT_AsnEncodeCTL;
|
2021-07-12 11:40:50 +02:00
|
|
|
else
|
|
|
|
FIXME("Unsupported encoder for lpszStructType %s\n", lpszStructType);
|
2007-08-10 20:21:03 +02:00
|
|
|
return encodeFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CryptEncodeObjectFunc CRYPT_LoadEncoderFunc(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
|
|
|
|
{
|
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
|
|
|
CryptEncodeObjectFunc encodeFunc = NULL;
|
|
|
|
|
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_FUNC, 0);
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
|
|
|
|
(void **)&encodeFunc, hFunc);
|
|
|
|
return encodeFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CryptEncodeObjectExFunc CRYPT_LoadEncoderExFunc(DWORD dwCertEncodingType,
|
|
|
|
LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
|
|
|
|
{
|
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
|
|
|
CryptEncodeObjectExFunc encodeFunc = NULL;
|
|
|
|
|
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_EX_FUNC, 0);
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
|
|
|
|
(void **)&encodeFunc, hFunc);
|
|
|
|
return encodeFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptEncodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
|
|
|
|
const void *pvStructInfo, BYTE *pbEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
|
|
|
CryptEncodeObjectFunc pCryptEncodeObject = NULL;
|
|
|
|
CryptEncodeObjectExFunc pCryptEncodeObjectEx = NULL;
|
|
|
|
|
|
|
|
TRACE_(crypt)("(0x%08x, %s, %p, %p, %p)\n", dwCertEncodingType,
|
|
|
|
debugstr_a(lpszStructType), pvStructInfo, pbEncoded,
|
|
|
|
pcbEncoded);
|
|
|
|
|
|
|
|
if (!pbEncoded && !pcbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pCryptEncodeObjectEx = CRYPT_GetBuiltinEncoder(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:21:03 +02:00
|
|
|
pCryptEncodeObject = CRYPT_LoadEncoderFunc(dwCertEncodingType,
|
|
|
|
lpszStructType, &hFunc);
|
|
|
|
if (!pCryptEncodeObject)
|
|
|
|
pCryptEncodeObjectEx = CRYPT_LoadEncoderExFunc(dwCertEncodingType,
|
|
|
|
lpszStructType, &hFunc);
|
|
|
|
}
|
|
|
|
if (pCryptEncodeObject)
|
|
|
|
ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
|
|
|
|
pvStructInfo, pbEncoded, pcbEncoded);
|
|
|
|
else if (pCryptEncodeObjectEx)
|
|
|
|
ret = pCryptEncodeObjectEx(dwCertEncodingType, lpszStructType,
|
|
|
|
pvStructInfo, 0, NULL, pbEncoded, pcbEncoded);
|
|
|
|
if (hFunc)
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
|
|
|
TRACE_(crypt)("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptEncodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
|
|
|
|
const void *pvStructInfo, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
|
|
|
|
void *pvEncoded, DWORD *pcbEncoded)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
|
|
|
CryptEncodeObjectExFunc encodeFunc = NULL;
|
|
|
|
|
|
|
|
TRACE_(crypt)("(0x%08x, %s, %p, 0x%08x, %p, %p, %p)\n", dwCertEncodingType,
|
|
|
|
debugstr_a(lpszStructType), pvStructInfo, dwFlags, pEncodePara,
|
|
|
|
pvEncoded, pcbEncoded);
|
|
|
|
|
|
|
|
if (!pvEncoded && !pcbEncoded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastError(NOERROR);
|
2010-12-31 12:00:22 +01:00
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG) {
|
|
|
|
if (!pvEncoded) {
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-08-10 20:21:03 +02:00
|
|
|
*(BYTE **)pvEncoded = NULL;
|
2010-12-31 12:00:22 +01:00
|
|
|
}
|
2007-08-10 20:21:03 +02:00
|
|
|
encodeFunc = CRYPT_GetBuiltinEncoder(dwCertEncodingType, lpszStructType);
|
2005-06-03 13:25:24 +02:00
|
|
|
if (!encodeFunc)
|
2005-12-02 11:28:09 +01:00
|
|
|
{
|
2007-08-10 20:21:03 +02:00
|
|
|
TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
|
|
|
|
debugstr_a(lpszStructType));
|
|
|
|
encodeFunc = CRYPT_LoadEncoderExFunc(dwCertEncodingType, lpszStructType,
|
|
|
|
&hFunc);
|
2005-12-02 11:28:09 +01:00
|
|
|
}
|
2005-06-03 13:25:24 +02:00
|
|
|
if (encodeFunc)
|
|
|
|
ret = encodeFunc(dwCertEncodingType, lpszStructType, pvStructInfo,
|
2005-06-17 23:26:53 +02:00
|
|
|
dwFlags, pEncodePara, pvEncoded, pcbEncoded);
|
2005-06-03 13:25:24 +02:00
|
|
|
else
|
2007-08-10 20:18:33 +02:00
|
|
|
{
|
2007-08-10 20:21:03 +02:00
|
|
|
CryptEncodeObjectFunc pCryptEncodeObject =
|
|
|
|
CRYPT_LoadEncoderFunc(dwCertEncodingType, lpszStructType, &hFunc);
|
2007-08-10 20:18:33 +02:00
|
|
|
|
|
|
|
if (pCryptEncodeObject)
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
|
|
|
|
{
|
|
|
|
ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
|
|
|
|
pvStructInfo, NULL, pcbEncoded);
|
|
|
|
if (ret && (ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
|
|
|
|
pvEncoded, pcbEncoded, *pcbEncoded)))
|
|
|
|
ret = pCryptEncodeObject(dwCertEncodingType,
|
|
|
|
lpszStructType, pvStructInfo, *(BYTE **)pvEncoded,
|
|
|
|
pcbEncoded);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
|
|
|
|
pvStructInfo, pvEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
}
|
2005-12-02 11:28:09 +01:00
|
|
|
if (hFunc)
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
2007-08-10 20:21:03 +02:00
|
|
|
TRACE_(crypt)("returning %d\n", ret);
|
2005-05-29 21:59:33 +02:00
|
|
|
return ret;
|
2005-05-20 21:37:26 +02:00
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptExportPublicKeyInfo(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
|
2005-09-28 12:15:37 +02:00
|
|
|
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
|
|
|
|
{
|
|
|
|
return CryptExportPublicKeyInfoEx(hCryptProv, dwKeySpec, dwCertEncodingType,
|
|
|
|
NULL, 0, NULL, pInfo, pcbInfo);
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:17:56 +02:00
|
|
|
typedef BOOL (WINAPI *EncodePublicKeyAndParametersFunc)(DWORD dwCertEncodingType,
|
|
|
|
LPSTR pszPublicKeyObjId, BYTE *pbPubKey, DWORD cbPubKey, DWORD dwFlags, void *pvAuxInfo,
|
|
|
|
BYTE **ppPublicKey, DWORD *pcbPublicKey, BYTE **ppbParams, DWORD *pcbParams);
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_ExportPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
|
2005-09-30 12:35:31 +02:00
|
|
|
DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
|
|
|
|
DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
|
2005-09-28 12:15:37 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
HCRYPTKEY key;
|
2018-10-22 13:17:56 +02:00
|
|
|
static CHAR rsa_oid[] = szOID_RSA_RSA;
|
2005-09-28 12:15:37 +02:00
|
|
|
|
2007-09-27 20:08:36 +02:00
|
|
|
TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
|
2007-08-22 00:11:09 +02:00
|
|
|
dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
|
2007-09-27 20:08:36 +02:00
|
|
|
pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
|
2005-09-28 12:15:37 +02:00
|
|
|
|
|
|
|
if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key)))
|
|
|
|
{
|
|
|
|
DWORD keySize = 0;
|
|
|
|
|
|
|
|
ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, NULL, &keySize);
|
|
|
|
if (ret)
|
|
|
|
{
|
2018-10-22 13:17:56 +02:00
|
|
|
PUBLICKEYSTRUC *pubKey = CryptMemAlloc(keySize);
|
2005-09-28 12:15:37 +02:00
|
|
|
|
|
|
|
if (pubKey)
|
|
|
|
{
|
2018-10-22 13:17:56 +02:00
|
|
|
ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, (BYTE *)pubKey, &keySize);
|
2005-09-28 12:15:37 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2018-10-22 13:17:56 +02:00
|
|
|
DWORD encodedLen;
|
|
|
|
|
|
|
|
if (!pszPublicKeyObjId)
|
|
|
|
{
|
|
|
|
static HCRYPTOIDFUNCSET set;
|
|
|
|
EncodePublicKeyAndParametersFunc encodeFunc = NULL;
|
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
|
|
|
|
|
|
|
pszPublicKeyObjId = (LPSTR)CertAlgIdToOID(pubKey->aiKeyAlg);
|
|
|
|
TRACE("public key algid %#x (%s)\n", pubKey->aiKeyAlg, debugstr_a(pszPublicKeyObjId));
|
|
|
|
|
|
|
|
if (!set) /* FIXME: there is no a public macro */
|
|
|
|
set = CryptInitOIDFunctionSet("CryptDllEncodePublicKeyAndParameters", 0);
|
|
|
|
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId, 0, (void **)&encodeFunc, &hFunc);
|
|
|
|
if (encodeFunc)
|
|
|
|
{
|
|
|
|
BYTE *key_data = NULL;
|
|
|
|
DWORD key_size = 0;
|
|
|
|
BYTE *params = NULL;
|
|
|
|
DWORD params_size = 0;
|
|
|
|
|
|
|
|
ret = encodeFunc(dwCertEncodingType, pszPublicKeyObjId, (BYTE *)pubKey, keySize,
|
|
|
|
dwFlags, pvAuxInfo, &key_data, &key_size, ¶ms, ¶ms_size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD oid_size = strlen(pszPublicKeyObjId) + 1;
|
|
|
|
DWORD size_needed = sizeof(*pInfo) + oid_size + key_size + params_size;
|
|
|
|
|
|
|
|
if (!pInfo)
|
|
|
|
*pcbInfo = size_needed;
|
|
|
|
else if (*pcbInfo < size_needed)
|
|
|
|
{
|
|
|
|
*pcbInfo = size_needed;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbInfo = size_needed;
|
|
|
|
pInfo->Algorithm.pszObjId = (char *)(pInfo + 1);
|
|
|
|
lstrcpyA(pInfo->Algorithm.pszObjId, pszPublicKeyObjId);
|
|
|
|
if (params)
|
|
|
|
{
|
|
|
|
pInfo->Algorithm.Parameters.cbData = params_size;
|
|
|
|
pInfo->Algorithm.Parameters.pbData = (BYTE *)pInfo->Algorithm.pszObjId + oid_size;
|
|
|
|
memcpy(pInfo->Algorithm.Parameters.pbData, params, params_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pInfo->Algorithm.Parameters.cbData = 0;
|
|
|
|
pInfo->Algorithm.Parameters.pbData = NULL;
|
|
|
|
}
|
|
|
|
pInfo->PublicKey.pbData = (BYTE *)pInfo->Algorithm.pszObjId + oid_size + params_size;
|
|
|
|
pInfo->PublicKey.cbData = key_size;
|
|
|
|
memcpy(pInfo->PublicKey.pbData, key_data, key_size);
|
|
|
|
pInfo->PublicKey.cUnusedBits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptMemFree(key_data);
|
|
|
|
CryptMemFree(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptMemFree(pubKey);
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fallback to RSA */
|
|
|
|
pszPublicKeyObjId = rsa_oid;
|
|
|
|
}
|
2005-09-28 12:15:37 +02:00
|
|
|
|
2018-10-22 13:17:56 +02:00
|
|
|
encodedLen = 0;
|
2005-09-28 12:15:37 +02:00
|
|
|
ret = CryptEncodeObject(dwCertEncodingType,
|
2005-09-30 12:35:31 +02:00
|
|
|
RSA_CSP_PUBLICKEYBLOB, pubKey, NULL, &encodedLen);
|
2005-09-28 12:15:37 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) +
|
|
|
|
strlen(pszPublicKeyObjId) + 1 + encodedLen;
|
|
|
|
|
|
|
|
if (!pInfo)
|
|
|
|
*pcbInfo = sizeNeeded;
|
|
|
|
else if (*pcbInfo < sizeNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
*pcbInfo = sizeNeeded;
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-20 21:48:48 +02:00
|
|
|
*pcbInfo = sizeNeeded;
|
2005-09-28 12:15:37 +02:00
|
|
|
pInfo->Algorithm.pszObjId = (char *)pInfo +
|
|
|
|
sizeof(CERT_PUBLIC_KEY_INFO);
|
|
|
|
lstrcpyA(pInfo->Algorithm.pszObjId,
|
|
|
|
pszPublicKeyObjId);
|
|
|
|
pInfo->Algorithm.Parameters.cbData = 0;
|
|
|
|
pInfo->Algorithm.Parameters.pbData = NULL;
|
2005-09-30 12:35:31 +02:00
|
|
|
pInfo->PublicKey.pbData =
|
|
|
|
(BYTE *)pInfo->Algorithm.pszObjId
|
2005-09-28 12:15:37 +02:00
|
|
|
+ lstrlenA(pInfo->Algorithm.pszObjId) + 1;
|
|
|
|
pInfo->PublicKey.cbData = encodedLen;
|
|
|
|
pInfo->PublicKey.cUnusedBits = 0;
|
|
|
|
ret = CryptEncodeObject(dwCertEncodingType,
|
2005-09-30 12:35:31 +02:00
|
|
|
RSA_CSP_PUBLICKEYBLOB, pubKey,
|
|
|
|
pInfo->PublicKey.pbData, &pInfo->PublicKey.cbData);
|
2005-09-28 12:15:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-28 12:09:26 +02:00
|
|
|
CryptMemFree(pubKey);
|
2005-09-28 12:15:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
CryptDestroyKey(key);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
typedef BOOL (WINAPI *ExportPublicKeyInfoExFunc)(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
|
2005-09-30 12:35:31 +02:00
|
|
|
DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
|
|
|
|
DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo);
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
|
2005-09-30 12:35:31 +02:00
|
|
|
DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags,
|
|
|
|
void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
|
|
|
|
{
|
2005-12-02 11:28:09 +01:00
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
2005-09-30 12:35:31 +02:00
|
|
|
BOOL ret;
|
|
|
|
ExportPublicKeyInfoExFunc exportFunc = NULL;
|
2005-12-02 11:28:09 +01:00
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
2005-09-30 12:35:31 +02:00
|
|
|
|
2007-09-27 20:08:36 +02:00
|
|
|
TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
|
2007-08-07 18:19:17 +02:00
|
|
|
dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
|
2007-09-27 20:08:36 +02:00
|
|
|
pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
|
2005-09-30 12:35:31 +02:00
|
|
|
|
|
|
|
if (!hCryptProv)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pszPublicKeyObjId)
|
2005-12-02 11:28:09 +01:00
|
|
|
{
|
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC,
|
|
|
|
0);
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId,
|
|
|
|
0, (void **)&exportFunc, &hFunc);
|
|
|
|
}
|
2005-09-30 12:35:31 +02:00
|
|
|
if (!exportFunc)
|
2018-10-22 13:17:56 +02:00
|
|
|
exportFunc = CRYPT_ExportPublicKeyInfoEx;
|
2005-09-30 12:35:31 +02:00
|
|
|
ret = exportFunc(hCryptProv, dwKeySpec, dwCertEncodingType,
|
|
|
|
pszPublicKeyObjId, dwFlags, pvAuxInfo, pInfo, pcbInfo);
|
2005-12-02 11:28:09 +01:00
|
|
|
if (hFunc)
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
2005-09-30 12:35:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-28 12:15:37 +02:00
|
|
|
BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
|
|
|
|
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
|
|
|
|
{
|
|
|
|
return CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo,
|
|
|
|
0, 0, NULL, phKey);
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:17:59 +02:00
|
|
|
typedef BOOL (WINAPI *ConvertPublicKeyInfoFunc)(DWORD dwCertEncodingType,
|
|
|
|
PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg, DWORD dwFlags,
|
|
|
|
BYTE **ppbData, DWORD *dwDataLen);
|
|
|
|
|
|
|
|
static BOOL WINAPI CRYPT_ImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
|
2005-09-28 12:15:37 +02:00
|
|
|
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
|
|
|
|
DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
|
|
|
|
{
|
2018-10-22 13:17:59 +02:00
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
|
|
|
ConvertPublicKeyInfoFunc convertFunc = NULL;
|
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
2005-09-28 12:15:37 +02:00
|
|
|
BOOL ret;
|
2018-10-22 13:17:59 +02:00
|
|
|
DWORD pubKeySize;
|
|
|
|
LPBYTE pubKey;
|
2005-09-28 12:15:37 +02:00
|
|
|
|
2007-09-27 20:08:36 +02:00
|
|
|
TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
|
2005-09-28 12:15:37 +02:00
|
|
|
dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
|
|
|
|
|
2018-10-22 13:17:59 +02:00
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_CONVERT_PUBLIC_KEY_INFO_FUNC, 0);
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType, pInfo->Algorithm.pszObjId,
|
|
|
|
0, (void **)&convertFunc, &hFunc);
|
|
|
|
if (convertFunc)
|
|
|
|
{
|
|
|
|
pubKey = NULL;
|
|
|
|
pubKeySize = 0;
|
|
|
|
ret = convertFunc(dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, &pubKey, &pubKeySize);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0, phKey);
|
|
|
|
CryptMemFree(pubKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-30 12:35:31 +02:00
|
|
|
ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
|
2005-09-28 12:15:37 +02:00
|
|
|
pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize);
|
|
|
|
if (ret)
|
|
|
|
{
|
2018-10-22 13:17:59 +02:00
|
|
|
pubKey = CryptMemAlloc(pubKeySize);
|
2005-09-28 12:15:37 +02:00
|
|
|
|
|
|
|
if (pubKey)
|
|
|
|
{
|
2005-09-30 12:35:31 +02:00
|
|
|
ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
|
|
|
|
pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, pubKey,
|
|
|
|
&pubKeySize);
|
2005-09-28 12:15:37 +02:00
|
|
|
if (ret)
|
2008-05-25 20:16:14 +02:00
|
|
|
{
|
|
|
|
if(aiKeyAlg)
|
|
|
|
((BLOBHEADER*)pubKey)->aiKeyAlg = aiKeyAlg;
|
2005-09-28 12:15:37 +02:00
|
|
|
ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0,
|
|
|
|
phKey);
|
2008-05-25 20:16:14 +02:00
|
|
|
}
|
2005-10-28 12:09:26 +02:00
|
|
|
CryptMemFree(pubKey);
|
2005-09-28 12:15:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2005-09-30 12:35:31 +02:00
|
|
|
|
|
|
|
typedef BOOL (WINAPI *ImportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
|
|
|
|
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
|
|
|
|
DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey);
|
|
|
|
|
|
|
|
BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
|
|
|
|
DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
|
|
|
|
DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
|
|
|
|
{
|
2005-12-02 11:28:09 +01:00
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
2005-09-30 12:35:31 +02:00
|
|
|
BOOL ret;
|
|
|
|
ImportPublicKeyInfoExFunc importFunc = NULL;
|
2005-12-02 11:28:09 +01:00
|
|
|
HCRYPTOIDFUNCADDR hFunc = NULL;
|
2005-09-30 12:35:31 +02:00
|
|
|
|
2007-09-27 20:08:36 +02:00
|
|
|
TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
|
2005-09-30 12:35:31 +02:00
|
|
|
dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
|
|
|
|
|
2005-12-02 11:28:09 +01:00
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC, 0);
|
|
|
|
CryptGetOIDFunctionAddress(set, dwCertEncodingType,
|
|
|
|
pInfo->Algorithm.pszObjId, 0, (void **)&importFunc, &hFunc);
|
2005-09-30 12:35:31 +02:00
|
|
|
if (!importFunc)
|
2018-10-22 13:17:59 +02:00
|
|
|
importFunc = CRYPT_ImportPublicKeyInfoEx;
|
2005-09-30 12:35:31 +02:00
|
|
|
ret = importFunc(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags,
|
|
|
|
pvAuxInfo, phKey);
|
2005-12-02 11:28:09 +01:00
|
|
|
if (hFunc)
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
2005-09-30 12:35:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2019-01-25 19:13:54 +01:00
|
|
|
|
|
|
|
BOOL WINAPI CryptImportPublicKeyInfoEx2(DWORD dwCertEncodingType,
|
|
|
|
PCERT_PUBLIC_KEY_INFO pInfo, DWORD dwFlags, void *pvAuxInfo,
|
|
|
|
BCRYPT_KEY_HANDLE *phKey)
|
|
|
|
{
|
2021-04-12 14:20:57 +02:00
|
|
|
TRACE_(crypt)("(%d, %p, %08x, %p, %p)\n", dwCertEncodingType, pInfo,
|
2019-01-25 19:13:54 +01:00
|
|
|
dwFlags, pvAuxInfo, phKey);
|
2021-04-12 14:20:57 +02:00
|
|
|
|
|
|
|
if (dwFlags)
|
|
|
|
FIXME("flags %#x ignored\n", dwFlags);
|
|
|
|
|
|
|
|
return CNG_ImportPubKey(pInfo, phKey);
|
2019-01-25 19:13:54 +01:00
|
|
|
}
|