2 * Copyright 2005 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * This file implements ASN.1 DER decoding of a limited set of types.
19 * It isn't a full ASN.1 implementation. Microsoft implements BER
20 * encoding of many of the basic types in msasn1.dll, but that interface is
21 * undocumented, so I implement them here.
24 * "A Layman's Guide to a Subset of ASN.1, BER, and DER", by Burton Kaliski
25 * (available online, look for a PDF copy as the HTML versions tend to have
26 * translation errors.)
28 * RFC3280, http://www.faqs.org/rfcs/rfc3280.html
31 * http://msdn.microsoft.com/library/en-us/seccrypto/security/constants_for_cryptencodeobject_and_cryptdecodeobject.asp
39 #define NONAMELESSUNION
46 #include "wine/debug.h"
47 #include "wine/exception.h"
48 #include "crypt32_private.h"
50 /* This is a bit arbitrary, but to set some limit: */
51 #define MAX_ENCODED_LEN 0x02000000
53 #define ASN_FLAGS_MASK 0xe0
54 #define ASN_TYPE_MASK 0x1f
56 WINE_DEFAULT_DEBUG_CHANNEL(cryptasn);
57 WINE_DECLARE_DEBUG_CHANNEL(crypt);
65 typedef BOOL (WINAPI *CryptDecodeObjectFunc)(DWORD, LPCSTR, const BYTE *,
66 DWORD, DWORD, void *, DWORD *);
67 typedef BOOL (WINAPI *CryptDecodeObjectExFunc)(DWORD, LPCSTR, const BYTE *,
68 DWORD, DWORD, PCRYPT_DECODE_PARA, void *, DWORD *);
70 /* Prototypes for built-in decoders. They follow the Ex style prototypes.
71 * The dwCertEncodingType and lpszStructType are ignored by the built-in
72 * functions, but the parameters are retained to simplify CryptDecodeObjectEx,
73 * since it must call functions in external DLLs that follow these signatures.
75 static BOOL WINAPI CRYPT_AsnDecodeChoiceOfTime(DWORD dwCertEncodingType,
76 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
77 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
78 static BOOL WINAPI CRYPT_AsnDecodePubKeyInfoInternal(DWORD dwCertEncodingType,
79 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
80 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
81 /* Like CRYPT_AsnDecodeExtensions, except assumes rgExtension is set ahead of
82 * time, doesn't do memory allocation, and doesn't do exception handling.
83 * (This isn't intended to be the externally-called one.)
85 static BOOL WINAPI CRYPT_AsnDecodeExtensionsInternal(DWORD dwCertEncodingType,
86 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
87 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
88 /* Assumes algo->Parameters.pbData is set ahead of time. Internal func. */
89 static BOOL WINAPI CRYPT_AsnDecodeAlgorithmId(DWORD dwCertEncodingType,
90 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
91 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
92 /* Internal function */
93 static BOOL WINAPI CRYPT_AsnDecodeBool(DWORD dwCertEncodingType,
94 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
95 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
96 /* Assumes the CRYPT_DATA_BLOB's pbData member has been initialized */
97 static BOOL WINAPI CRYPT_AsnDecodeOctetsInternal(DWORD dwCertEncodingType,
98 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
99 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
100 /* Like CRYPT_AsnDecodeBits, but assumes the CRYPT_INTEGER_BLOB's pbData
101 * member has been initialized, doesn't do exception handling, and doesn't do
104 static BOOL WINAPI CRYPT_AsnDecodeBitsInternal(DWORD dwCertEncodingType,
105 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
106 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
107 static BOOL WINAPI CRYPT_AsnDecodeBits(DWORD dwCertEncodingType,
108 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
109 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
110 static BOOL WINAPI CRYPT_AsnDecodeInt(DWORD dwCertEncodingType,
111 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
112 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
113 /* Like CRYPT_AsnDecodeInteger, but assumes the CRYPT_INTEGER_BLOB's pbData
114 * member has been initialized, doesn't do exception handling, and doesn't do
115 * memory allocation. Also doesn't check tag, assumes the caller has checked
118 static BOOL WINAPI CRYPT_AsnDecodeIntegerInternal(DWORD dwCertEncodingType,
119 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
120 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
121 /* Like CRYPT_AsnDecodeInteger, but unsigned. */
122 static BOOL WINAPI CRYPT_AsnDecodeUnsignedIntegerInternal(
123 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
124 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
125 void *pvStructInfo, DWORD *pcbStructInfo);
127 /* Gets the number of length bytes from the given (leading) length byte */
128 #define GET_LEN_BYTES(b) ((b) <= 0x7f ? 1 : 1 + ((b) & 0x7f))
130 /* Helper function to get the encoded length of the data starting at pbEncoded,
131 * where pbEncoded[0] is the tag. If the data are too short to contain a
132 * length or if the length is too large for cbEncoded, sets an appropriate
133 * error code and returns FALSE.
135 static BOOL WINAPI CRYPT_GetLen(const BYTE *pbEncoded, DWORD cbEncoded,
142 SetLastError(CRYPT_E_ASN1_CORRUPT);
145 else if (pbEncoded[1] <= 0x7f)
147 if (pbEncoded[1] + 1 > cbEncoded)
149 SetLastError(CRYPT_E_ASN1_EOD);
158 else if (pbEncoded[1] == 0x80)
160 FIXME("unimplemented for indefinite-length encoding\n");
161 SetLastError(CRYPT_E_ASN1_CORRUPT);
166 BYTE lenLen = GET_LEN_BYTES(pbEncoded[1]);
168 if (lenLen > sizeof(DWORD) + 1)
170 SetLastError(CRYPT_E_ASN1_LARGE);
173 else if (lenLen + 2 > cbEncoded)
175 SetLastError(CRYPT_E_ASN1_CORRUPT);
188 if (out + lenLen + 1 > cbEncoded)
190 SetLastError(CRYPT_E_ASN1_EOD);
203 /* Helper function to check *pcbStructInfo, set it to the required size, and
204 * optionally to allocate memory. Assumes pvStructInfo is not NULL.
205 * If CRYPT_DECODE_ALLOC_FLAG is set in dwFlags, *pvStructInfo will be set to a
206 * pointer to the newly allocated memory.
208 static BOOL CRYPT_DecodeEnsureSpace(DWORD dwFlags,
209 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo,
214 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
216 if (pDecodePara && pDecodePara->pfnAlloc)
217 *(BYTE **)pvStructInfo = pDecodePara->pfnAlloc(bytesNeeded);
219 *(BYTE **)pvStructInfo = LocalAlloc(0, bytesNeeded);
220 if (!*(BYTE **)pvStructInfo)
223 *pcbStructInfo = bytesNeeded;
225 else if (*pcbStructInfo < bytesNeeded)
227 *pcbStructInfo = bytesNeeded;
228 SetLastError(ERROR_MORE_DATA);
235 * The expected tag of the item. If tag is 0, decodeFunc is called
236 * regardless of the tag value seen.
238 * A sequence is decoded into a struct. The offset member is the
239 * offset of this item within that struct.
241 * The decoder function to use. If this is NULL, then the member isn't
242 * decoded, but minSize space is reserved for it.
244 * The minimum amount of space occupied after decoding. You must set this.
246 * If true, and the tag doesn't match the expected tag for this item,
247 * or the decodeFunc fails with CRYPT_E_ASN1_BADTAG, then minSize space is
248 * filled with 0 for this member.
249 * hasPointer, pointerOffset:
250 * If the item has dynamic data, set hasPointer to TRUE, pointerOffset to
251 * the offset within the struct of the data pointer (or to the
252 * first data pointer, if more than one exist).
254 * Used by CRYPT_AsnDecodeSequence, not for your use.
256 struct AsnDecodeSequenceItem
260 CryptDecodeObjectExFunc decodeFunc;
268 /* Decodes the items in a sequence, where the items are described in items,
269 * the encoded data are in pbEncoded with length cbEncoded. Decodes into
270 * pvStructInfo. nextData is a pointer to the memory location at which the
271 * first decoded item with a dynamic pointer should point.
272 * Upon decoding, *cbDecoded is the total number of bytes decoded.
274 static BOOL CRYPT_AsnDecodeSequenceItems(DWORD dwCertEncodingType,
275 struct AsnDecodeSequenceItem items[], DWORD cItem, const BYTE *pbEncoded,
276 DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, BYTE *nextData,
280 DWORD i, decoded = 0;
281 const BYTE *ptr = pbEncoded;
283 TRACE("%p, %d, %p, %d, %08x, %p, %p, %p\n", items, cItem, pbEncoded,
284 cbEncoded, dwFlags, pvStructInfo, nextData, cbDecoded);
286 for (i = 0, ret = TRUE; ret && i < cItem; i++)
288 if (cbEncoded - (ptr - pbEncoded) != 0)
292 if ((ret = CRYPT_GetLen(ptr, cbEncoded - (ptr - pbEncoded),
295 BYTE nextItemLenBytes = GET_LEN_BYTES(ptr[1]);
297 if (ptr[0] == items[i].tag || !items[i].tag)
299 if (nextData && pvStructInfo && items[i].hasPointer)
301 TRACE("Setting next pointer to %p\n",
303 *(BYTE **)((BYTE *)pvStructInfo +
304 items[i].pointerOffset) = nextData;
306 if (items[i].decodeFunc)
309 TRACE("decoding item %d\n", i);
311 TRACE("sizing item %d\n", i);
312 ret = items[i].decodeFunc(dwCertEncodingType,
313 NULL, ptr, 1 + nextItemLenBytes + nextItemLen,
314 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL,
315 pvStructInfo ? (BYTE *)pvStructInfo + items[i].offset
316 : NULL, &items[i].size);
319 /* Account for alignment padding */
320 if (items[i].size % sizeof(DWORD))
321 items[i].size += sizeof(DWORD) -
322 items[i].size % sizeof(DWORD);
323 TRACE("item %d size: %d\n", i, items[i].size);
324 if (nextData && items[i].hasPointer &&
325 items[i].size > items[i].minSize)
326 nextData += items[i].size - items[i].minSize;
327 ptr += 1 + nextItemLenBytes + nextItemLen;
328 decoded += 1 + nextItemLenBytes + nextItemLen;
329 TRACE("item %d: decoded %d bytes\n", i,
330 1 + nextItemLenBytes + nextItemLen);
332 else if (items[i].optional &&
333 GetLastError() == CRYPT_E_ASN1_BADTAG)
335 TRACE("skipping optional item %d\n", i);
336 items[i].size = items[i].minSize;
337 SetLastError(NOERROR);
341 TRACE("item %d failed: %08x\n", i,
346 TRACE("item %d: decoded %d bytes\n", i,
347 1 + nextItemLenBytes + nextItemLen);
348 ptr += 1 + nextItemLenBytes + nextItemLen;
349 decoded += 1 + nextItemLenBytes + nextItemLen;
350 items[i].size = items[i].minSize;
353 else if (items[i].optional)
355 TRACE("skipping optional item %d\n", i);
356 items[i].size = items[i].minSize;
360 TRACE("item %d: tag %02x doesn't match expected %02x\n",
361 i, ptr[0], items[i].tag);
362 SetLastError(CRYPT_E_ASN1_BADTAG);
367 else if (items[i].optional)
369 TRACE("missing optional item %d, skipping\n", i);
370 items[i].size = items[i].minSize;
374 TRACE("not enough bytes for item %d, failing\n", i);
375 SetLastError(CRYPT_E_ASN1_CORRUPT);
380 *cbDecoded = decoded;
381 TRACE("returning %d\n", ret);
385 /* This decodes an arbitrary sequence into a contiguous block of memory
386 * (basically, a struct.) Each element being decoded is described by a struct
387 * AsnDecodeSequenceItem, see above.
388 * startingPointer is an optional pointer to the first place where dynamic
389 * data will be stored. If you know the starting offset, you may pass it
390 * here. Otherwise, pass NULL, and one will be inferred from the items.
391 * Each item decoder is never called with CRYPT_DECODE_ALLOC_FLAG set.
393 static BOOL CRYPT_AsnDecodeSequence(DWORD dwCertEncodingType,
394 struct AsnDecodeSequenceItem items[], DWORD cItem, const BYTE *pbEncoded,
395 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
396 void *pvStructInfo, DWORD *pcbStructInfo, void *startingPointer)
400 TRACE("%p, %d, %p, %d, %08x, %p, %p, %d, %p\n", items, cItem, pbEncoded,
401 cbEncoded, dwFlags, pDecodePara, pvStructInfo, *pcbStructInfo,
404 if (pbEncoded[0] == ASN_SEQUENCE)
408 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
410 DWORD lenBytes = GET_LEN_BYTES(pbEncoded[1]), cbDecoded;
411 const BYTE *ptr = pbEncoded + 1 + lenBytes;
413 cbEncoded -= 1 + lenBytes;
414 if (cbEncoded < dataLen)
416 TRACE("dataLen %d exceeds cbEncoded %d, failing\n", dataLen,
418 SetLastError(CRYPT_E_ASN1_CORRUPT);
422 ret = CRYPT_AsnDecodeSequenceItems(dwFlags, items, cItem, ptr,
423 cbEncoded, dwFlags, NULL, NULL, &cbDecoded);
424 if (ret && cbDecoded != dataLen)
426 TRACE("expected %d decoded, got %d, failing\n", dataLen,
428 SetLastError(CRYPT_E_ASN1_CORRUPT);
433 DWORD i, bytesNeeded = 0, structSize = 0;
435 for (i = 0; i < cItem; i++)
437 bytesNeeded += items[i].size;
438 structSize += items[i].minSize;
441 *pcbStructInfo = bytesNeeded;
442 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags,
443 pDecodePara, pvStructInfo, pcbStructInfo, bytesNeeded)))
447 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
448 pvStructInfo = *(BYTE **)pvStructInfo;
450 nextData = (BYTE *)startingPointer;
452 nextData = (BYTE *)pvStructInfo + structSize;
453 memset(pvStructInfo, 0, structSize);
454 ret = CRYPT_AsnDecodeSequenceItems(dwFlags, items, cItem,
455 ptr, cbEncoded, dwFlags, pvStructInfo, nextData,
463 SetLastError(CRYPT_E_ASN1_BADTAG);
466 TRACE("returning %d (%08x)\n", ret, GetLastError());
471 * The expected tag of the entire encoded array (usually a variant
472 * of ASN_SETOF or ASN_SEQUENCEOF.) If tag is 0, decodeFunc is called
473 * regardless of the tag seen.
475 * used to decode each item in the array
477 * is the minimum size of each decoded item
479 * indicates whether each item has a dynamic pointer
481 * indicates the offset within itemSize at which the pointer exists
483 struct AsnArrayDescriptor
486 CryptDecodeObjectExFunc decodeFunc;
492 struct AsnArrayItemSize
498 /* Decodes an array of like types into a struct GenericArray.
499 * The layout and decoding of the array are described by a struct
500 * AsnArrayDescriptor.
502 static BOOL CRYPT_AsnDecodeArray(const struct AsnArrayDescriptor *arrayDesc,
503 const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
504 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo,
505 void *startingPointer)
509 TRACE("%p, %p, %d, %08x, %p, %p, %d, %p\n", arrayDesc, pbEncoded,
510 cbEncoded, dwFlags, pDecodePara, pvStructInfo, *pcbStructInfo,
513 if (!arrayDesc->tag || pbEncoded[0] == arrayDesc->tag)
517 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
519 DWORD bytesNeeded, cItems = 0;
520 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
521 /* There can be arbitrarily many items, but there is often only one.
523 struct AsnArrayItemSize itemSize = { 0 }, *itemSizes = &itemSize;
525 bytesNeeded = sizeof(struct GenericArray);
530 for (ptr = pbEncoded + 1 + lenBytes; ret &&
531 ptr - pbEncoded - 1 - lenBytes < dataLen; )
533 DWORD itemLenBytes, itemDataLen, size = 0;
535 itemLenBytes = GET_LEN_BYTES(ptr[1]);
536 /* Each item decoded may not tolerate extraneous bytes, so
537 * get the length of the next element and pass it directly.
539 ret = CRYPT_GetLen(ptr, cbEncoded - (ptr - pbEncoded),
542 ret = arrayDesc->decodeFunc(X509_ASN_ENCODING, 0, ptr,
543 1 + itemLenBytes + itemDataLen,
544 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL,
551 if (itemSizes != &itemSize)
552 itemSizes = CryptMemRealloc(itemSizes,
553 cItems * sizeof(struct AsnArrayItemSize));
558 cItems * sizeof(struct AsnArrayItemSize));
560 memcpy(itemSizes, &itemSize, sizeof(itemSize));
564 itemSizes[cItems - 1].encodedLen = 1 + itemLenBytes
566 itemSizes[cItems - 1].size = size;
568 ret = CRYPT_GetLen(ptr,
569 cbEncoded - (ptr - pbEncoded), &nextLen);
571 ptr += nextLen + 1 + GET_LEN_BYTES(ptr[1]);
581 *pcbStructInfo = bytesNeeded;
582 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags,
583 pDecodePara, pvStructInfo, pcbStructInfo, bytesNeeded)))
588 struct GenericArray *array;
590 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
591 pvStructInfo = *(BYTE **)pvStructInfo;
592 array = (struct GenericArray *)pvStructInfo;
593 array->cItems = cItems;
595 array->rgItems = startingPointer;
597 array->rgItems = (BYTE *)array +
598 sizeof(struct GenericArray);
599 nextData = (BYTE *)array->rgItems +
600 array->cItems * arrayDesc->itemSize;
601 for (i = 0, ptr = pbEncoded + 1 + lenBytes; ret &&
602 i < cItems && ptr - pbEncoded - 1 - lenBytes <
605 if (arrayDesc->hasPointer)
606 *(BYTE **)(array->rgItems + i * arrayDesc->itemSize
607 + arrayDesc->pointerOffset) = nextData;
608 ret = arrayDesc->decodeFunc(X509_ASN_ENCODING, 0, ptr,
609 itemSizes[i].encodedLen,
610 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL,
611 array->rgItems + i * arrayDesc->itemSize,
617 nextData += itemSizes[i].size - arrayDesc->itemSize;
618 ret = CRYPT_GetLen(ptr,
619 cbEncoded - (ptr - pbEncoded), &nextLen);
621 ptr += nextLen + 1 + GET_LEN_BYTES(ptr[1]);
626 if (itemSizes != &itemSize)
627 CryptMemFree(itemSizes);
632 SetLastError(CRYPT_E_ASN1_BADTAG);
638 /* Decodes a DER-encoded BLOB into a CRYPT_DER_BLOB struct pointed to by
639 * pvStructInfo. The BLOB must be non-empty, otherwise the last error is set
640 * to CRYPT_E_ASN1_CORRUPT.
641 * Warning: assumes the CRYPT_DER_BLOB pointed to by pvStructInfo has pbData
644 static BOOL WINAPI CRYPT_AsnDecodeDerBlob(DWORD dwCertEncodingType,
645 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
646 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
651 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
653 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
654 DWORD bytesNeeded = sizeof(CRYPT_DER_BLOB);
656 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
657 bytesNeeded += 1 + lenBytes + dataLen;
660 *pcbStructInfo = bytesNeeded;
661 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
662 pvStructInfo, pcbStructInfo, bytesNeeded)))
664 CRYPT_DER_BLOB *blob;
666 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
667 pvStructInfo = *(BYTE **)pvStructInfo;
668 blob = (CRYPT_DER_BLOB *)pvStructInfo;
669 blob->cbData = 1 + lenBytes + dataLen;
672 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
673 blob->pbData = (BYTE *)pbEncoded;
676 assert(blob->pbData);
677 memcpy(blob->pbData, pbEncoded, blob->cbData);
682 SetLastError(CRYPT_E_ASN1_CORRUPT);
690 /* Like CRYPT_AsnDecodeBitsInternal, but swaps the bytes */
691 static BOOL WINAPI CRYPT_AsnDecodeBitsSwapBytes(DWORD dwCertEncodingType,
692 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
693 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
697 TRACE("(%p, %d, 0x%08x, %p, %p, %d)\n", pbEncoded, cbEncoded, dwFlags,
698 pDecodePara, pvStructInfo, *pcbStructInfo);
700 /* Can't use the CRYPT_DECODE_NOCOPY_FLAG, because we modify the bytes in-
703 ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType, lpszStructType,
704 pbEncoded, cbEncoded, dwFlags & ~CRYPT_DECODE_NOCOPY_FLAG, pDecodePara,
705 pvStructInfo, pcbStructInfo);
706 if (ret && pvStructInfo)
708 CRYPT_BIT_BLOB *blob = (CRYPT_BIT_BLOB *)pvStructInfo;
715 for (i = 0; i < blob->cbData / 2; i++)
717 temp = blob->pbData[i];
718 blob->pbData[i] = blob->pbData[blob->cbData - i - 1];
719 blob->pbData[blob->cbData - i - 1] = temp;
723 TRACE("returning %d (%08x)\n", ret, GetLastError());
727 static BOOL WINAPI CRYPT_AsnDecodeCertSignedContent(DWORD dwCertEncodingType,
728 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
729 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
733 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
734 pDecodePara, pvStructInfo, *pcbStructInfo);
738 struct AsnDecodeSequenceItem items[] = {
739 { 0, offsetof(CERT_SIGNED_CONTENT_INFO, ToBeSigned),
740 CRYPT_AsnDecodeDerBlob, sizeof(CRYPT_DER_BLOB), FALSE, TRUE,
741 offsetof(CERT_SIGNED_CONTENT_INFO, ToBeSigned.pbData), 0 },
742 { ASN_SEQUENCEOF, offsetof(CERT_SIGNED_CONTENT_INFO,
743 SignatureAlgorithm), CRYPT_AsnDecodeAlgorithmId,
744 sizeof(CRYPT_ALGORITHM_IDENTIFIER), FALSE, TRUE,
745 offsetof(CERT_SIGNED_CONTENT_INFO, SignatureAlgorithm.pszObjId), 0 },
746 { ASN_BITSTRING, offsetof(CERT_SIGNED_CONTENT_INFO, Signature),
747 CRYPT_AsnDecodeBitsSwapBytes, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
748 offsetof(CERT_SIGNED_CONTENT_INFO, Signature.pbData), 0 },
751 if (dwFlags & CRYPT_DECODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG)
752 items[2].decodeFunc = CRYPT_AsnDecodeBitsInternal;
753 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
754 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
755 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
759 SetLastError(STATUS_ACCESS_VIOLATION);
764 TRACE("Returning %d (%08x)\n", ret, GetLastError());
768 /* Internal function */
769 static BOOL WINAPI CRYPT_AsnDecodeCertVersion(DWORD dwCertEncodingType,
770 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
771 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
776 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
778 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
780 ret = CRYPT_AsnDecodeInt(dwCertEncodingType, X509_INTEGER,
781 pbEncoded + 1 + lenBytes, dataLen, dwFlags, pDecodePara,
782 pvStructInfo, pcbStructInfo);
787 static BOOL WINAPI CRYPT_AsnDecodeValidity(DWORD dwCertEncodingType,
788 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
789 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
793 struct AsnDecodeSequenceItem items[] = {
794 { 0, offsetof(CERT_PRIVATE_KEY_VALIDITY, NotBefore),
795 CRYPT_AsnDecodeChoiceOfTime, sizeof(FILETIME), FALSE, FALSE, 0 },
796 { 0, offsetof(CERT_PRIVATE_KEY_VALIDITY, NotAfter),
797 CRYPT_AsnDecodeChoiceOfTime, sizeof(FILETIME), FALSE, FALSE, 0 },
800 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
801 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
802 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
806 /* Internal function */
807 static BOOL WINAPI CRYPT_AsnDecodeCertExtensions(DWORD dwCertEncodingType,
808 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
809 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
814 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
816 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
818 ret = CRYPT_AsnDecodeExtensionsInternal(dwCertEncodingType,
819 X509_EXTENSIONS, pbEncoded + 1 + lenBytes, dataLen, dwFlags,
820 pDecodePara, pvStructInfo, pcbStructInfo);
825 static BOOL WINAPI CRYPT_AsnDecodeCertInfo(DWORD dwCertEncodingType,
826 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
827 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
830 struct AsnDecodeSequenceItem items[] = {
831 { ASN_CONTEXT | ASN_CONSTRUCTOR, offsetof(CERT_INFO, dwVersion),
832 CRYPT_AsnDecodeCertVersion, sizeof(DWORD), TRUE, FALSE, 0, 0 },
833 { ASN_INTEGER, offsetof(CERT_INFO, SerialNumber),
834 CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE,
835 TRUE, offsetof(CERT_INFO, SerialNumber.pbData), 0 },
836 { ASN_SEQUENCEOF, offsetof(CERT_INFO, SignatureAlgorithm),
837 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
838 FALSE, TRUE, offsetof(CERT_INFO, SignatureAlgorithm.pszObjId), 0 },
839 { 0, offsetof(CERT_INFO, Issuer), CRYPT_AsnDecodeDerBlob,
840 sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_INFO,
842 { ASN_SEQUENCEOF, offsetof(CERT_INFO, NotBefore),
843 CRYPT_AsnDecodeValidity, sizeof(CERT_PRIVATE_KEY_VALIDITY), FALSE,
845 { 0, offsetof(CERT_INFO, Subject), CRYPT_AsnDecodeDerBlob,
846 sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_INFO,
848 { ASN_SEQUENCEOF, offsetof(CERT_INFO, SubjectPublicKeyInfo),
849 CRYPT_AsnDecodePubKeyInfoInternal, sizeof(CERT_PUBLIC_KEY_INFO),
850 FALSE, TRUE, offsetof(CERT_INFO,
851 SubjectPublicKeyInfo.Algorithm.Parameters.pbData), 0 },
852 { ASN_BITSTRING, offsetof(CERT_INFO, IssuerUniqueId),
853 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
854 offsetof(CERT_INFO, IssuerUniqueId.pbData), 0 },
855 { ASN_BITSTRING, offsetof(CERT_INFO, SubjectUniqueId),
856 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
857 offsetof(CERT_INFO, SubjectUniqueId.pbData), 0 },
858 { ASN_CONTEXT | ASN_CONSTRUCTOR | 3, offsetof(CERT_INFO, cExtension),
859 CRYPT_AsnDecodeCertExtensions, sizeof(CERT_EXTENSIONS), TRUE, TRUE,
860 offsetof(CERT_INFO, rgExtension), 0 },
863 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
864 pDecodePara, pvStructInfo, *pcbStructInfo);
866 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
867 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
868 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
869 if (ret && pvStructInfo)
873 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
874 info = *(CERT_INFO **)pvStructInfo;
876 info = (CERT_INFO *)pvStructInfo;
877 if (!info->SerialNumber.cbData || !info->Issuer.cbData ||
878 !info->Subject.cbData)
880 SetLastError(CRYPT_E_ASN1_CORRUPT);
881 /* Don't need to deallocate, because it should have failed on the
882 * first pass (and no memory was allocated.)
888 TRACE("Returning %d (%08x)\n", ret, GetLastError());
892 static BOOL WINAPI CRYPT_AsnDecodeCert(DWORD dwCertEncodingType,
893 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
894 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
898 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
899 pDecodePara, pvStructInfo, *pcbStructInfo);
905 /* Unless told not to, first try to decode it as a signed cert. */
906 if (!(dwFlags & CRYPT_DECODE_TO_BE_SIGNED_FLAG))
908 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
910 ret = CRYPT_AsnDecodeCertSignedContent(dwCertEncodingType,
911 X509_CERT, pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
912 (BYTE *)&signedCert, &size);
916 ret = CRYPT_AsnDecodeCertInfo(dwCertEncodingType,
917 X509_CERT_TO_BE_SIGNED, signedCert->ToBeSigned.pbData,
918 signedCert->ToBeSigned.cbData, dwFlags, pDecodePara,
919 pvStructInfo, pcbStructInfo);
920 LocalFree(signedCert);
923 /* Failing that, try it as an unsigned cert */
927 ret = CRYPT_AsnDecodeCertInfo(dwCertEncodingType,
928 X509_CERT_TO_BE_SIGNED, pbEncoded, cbEncoded, dwFlags,
929 pDecodePara, pvStructInfo, pcbStructInfo);
934 SetLastError(STATUS_ACCESS_VIOLATION);
938 TRACE("Returning %d (%08x)\n", ret, GetLastError());
942 static BOOL WINAPI CRYPT_AsnDecodeCRLEntry(DWORD dwCertEncodingType,
943 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
944 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
947 struct AsnDecodeSequenceItem items[] = {
948 { ASN_INTEGER, offsetof(CRL_ENTRY, SerialNumber),
949 CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE, TRUE,
950 offsetof(CRL_ENTRY, SerialNumber.pbData), 0 },
951 { 0, offsetof(CRL_ENTRY, RevocationDate), CRYPT_AsnDecodeChoiceOfTime,
952 sizeof(FILETIME), FALSE, FALSE, 0 },
953 { ASN_SEQUENCEOF, offsetof(CRL_ENTRY, cExtension),
954 CRYPT_AsnDecodeExtensionsInternal, sizeof(CERT_EXTENSIONS), TRUE, TRUE,
955 offsetof(CRL_ENTRY, rgExtension), 0 },
957 PCRL_ENTRY entry = (PCRL_ENTRY)pvStructInfo;
959 TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags, entry,
962 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
963 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
964 NULL, entry, pcbStructInfo, entry ? entry->SerialNumber.pbData : NULL);
968 /* Warning: assumes pvStructInfo is a struct GenericArray whose rgItems has
969 * been set prior to calling.
971 static BOOL WINAPI CRYPT_AsnDecodeCRLEntries(DWORD dwCertEncodingType,
972 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
973 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
976 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
977 CRYPT_AsnDecodeCRLEntry, sizeof(CRL_ENTRY), TRUE,
978 offsetof(CRL_ENTRY, SerialNumber.pbData) };
979 struct GenericArray *entries = (struct GenericArray *)pvStructInfo;
981 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
982 pDecodePara, pvStructInfo, *pcbStructInfo);
984 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
985 pDecodePara, pvStructInfo, pcbStructInfo,
986 entries ? entries->rgItems : NULL);
987 TRACE("Returning %d (%08x)\n", ret, GetLastError());
991 static BOOL WINAPI CRYPT_AsnDecodeCRLInfo(DWORD dwCertEncodingType,
992 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
993 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
995 struct AsnDecodeSequenceItem items[] = {
996 { ASN_INTEGER, offsetof(CRL_INFO, dwVersion),
997 CRYPT_AsnDecodeInt, sizeof(DWORD), TRUE, FALSE, 0, 0 },
998 { ASN_SEQUENCEOF, offsetof(CRL_INFO, SignatureAlgorithm),
999 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
1000 FALSE, TRUE, offsetof(CRL_INFO, SignatureAlgorithm.pszObjId), 0 },
1001 { 0, offsetof(CRL_INFO, Issuer), CRYPT_AsnDecodeDerBlob,
1002 sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CRL_INFO,
1004 { 0, offsetof(CRL_INFO, ThisUpdate), CRYPT_AsnDecodeChoiceOfTime,
1005 sizeof(FILETIME), FALSE, FALSE, 0 },
1006 { 0, offsetof(CRL_INFO, NextUpdate), CRYPT_AsnDecodeChoiceOfTime,
1007 sizeof(FILETIME), TRUE, FALSE, 0 },
1008 { ASN_SEQUENCEOF, offsetof(CRL_INFO, cCRLEntry),
1009 CRYPT_AsnDecodeCRLEntries, sizeof(struct GenericArray), TRUE, TRUE,
1010 offsetof(CRL_INFO, rgCRLEntry), 0 },
1011 { ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_INFO, cExtension),
1012 CRYPT_AsnDecodeCertExtensions, sizeof(CERT_EXTENSIONS), TRUE, TRUE,
1013 offsetof(CRL_INFO, rgExtension), 0 },
1017 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1018 pDecodePara, pvStructInfo, *pcbStructInfo);
1020 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
1021 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
1022 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
1024 TRACE("Returning %d (%08x)\n", ret, GetLastError());
1028 static BOOL WINAPI CRYPT_AsnDecodeCRL(DWORD dwCertEncodingType,
1029 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1030 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1034 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1035 pDecodePara, pvStructInfo, *pcbStructInfo);
1041 /* Unless told not to, first try to decode it as a signed crl. */
1042 if (!(dwFlags & CRYPT_DECODE_TO_BE_SIGNED_FLAG))
1044 PCERT_SIGNED_CONTENT_INFO signedCrl = NULL;
1046 ret = CRYPT_AsnDecodeCertSignedContent(dwCertEncodingType,
1047 X509_CERT, pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
1048 (BYTE *)&signedCrl, &size);
1052 ret = CRYPT_AsnDecodeCRLInfo(dwCertEncodingType,
1053 X509_CERT_CRL_TO_BE_SIGNED, signedCrl->ToBeSigned.pbData,
1054 signedCrl->ToBeSigned.cbData, dwFlags, pDecodePara,
1055 pvStructInfo, pcbStructInfo);
1056 LocalFree(signedCrl);
1059 /* Failing that, try it as an unsigned crl */
1063 ret = CRYPT_AsnDecodeCRLInfo(dwCertEncodingType,
1064 X509_CERT_CRL_TO_BE_SIGNED, pbEncoded, cbEncoded,
1065 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
1070 SetLastError(STATUS_ACCESS_VIOLATION);
1074 TRACE("Returning %d (%08x)\n", ret, GetLastError());
1078 static BOOL WINAPI CRYPT_AsnDecodeOidIgnoreTag(DWORD dwCertEncodingType,
1079 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1080 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1085 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1086 pDecodePara, pvStructInfo, *pcbStructInfo);
1088 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
1090 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
1091 DWORD bytesNeeded = sizeof(LPSTR);
1095 /* The largest possible string for the first two components
1096 * is 2.175 (= 2 * 40 + 175 = 255), so this is big enough.
1101 snprintf(firstTwo, sizeof(firstTwo), "%d.%d",
1102 pbEncoded[1 + lenBytes] / 40,
1103 pbEncoded[1 + lenBytes] - (pbEncoded[1 + lenBytes] / 40)
1105 bytesNeeded += strlen(firstTwo) + 1;
1106 for (ptr = pbEncoded + 2 + lenBytes; ret &&
1107 ptr - pbEncoded - 1 - lenBytes < dataLen; )
1109 /* large enough for ".4000000" */
1113 while (ptr - pbEncoded - 1 - lenBytes < dataLen &&
1120 if (ptr - pbEncoded - 1 - lenBytes >= dataLen ||
1123 SetLastError(CRYPT_E_ASN1_CORRUPT);
1130 snprintf(str, sizeof(str), ".%d", val);
1131 bytesNeeded += strlen(str);
1136 *pcbStructInfo = bytesNeeded;
1137 else if (*pcbStructInfo < bytesNeeded)
1139 *pcbStructInfo = bytesNeeded;
1140 SetLastError(ERROR_MORE_DATA);
1148 LPSTR pszObjId = *(LPSTR *)pvStructInfo;
1151 sprintf(pszObjId, "%d.%d", pbEncoded[1 + lenBytes] / 40,
1152 pbEncoded[1 + lenBytes] - (pbEncoded[1 + lenBytes] /
1154 pszObjId += strlen(pszObjId);
1155 for (ptr = pbEncoded + 2 + lenBytes; ret &&
1156 ptr - pbEncoded - 1 - lenBytes < dataLen; )
1160 while (ptr - pbEncoded - 1 - lenBytes < dataLen &&
1169 sprintf(pszObjId, ".%d", val);
1170 pszObjId += strlen(pszObjId);
1174 *(LPSTR *)pvStructInfo = NULL;
1175 *pcbStructInfo = bytesNeeded;
1181 static BOOL WINAPI CRYPT_AsnDecodeOidInternal(DWORD dwCertEncodingType,
1182 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1183 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1187 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1188 pDecodePara, pvStructInfo, *pcbStructInfo);
1190 if (pbEncoded[0] == ASN_OBJECTIDENTIFIER)
1191 ret = CRYPT_AsnDecodeOidIgnoreTag(dwCertEncodingType, lpszStructType,
1192 pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
1196 SetLastError(CRYPT_E_ASN1_BADTAG);
1202 /* Warning: assumes pvStructInfo is a CERT_EXTENSION whose pszObjId is set
1205 static BOOL WINAPI CRYPT_AsnDecodeExtension(DWORD dwCertEncodingType,
1206 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1207 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1209 struct AsnDecodeSequenceItem items[] = {
1210 { ASN_OBJECTIDENTIFIER, offsetof(CERT_EXTENSION, pszObjId),
1211 CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
1212 offsetof(CERT_EXTENSION, pszObjId), 0 },
1213 { ASN_BOOL, offsetof(CERT_EXTENSION, fCritical), CRYPT_AsnDecodeBool,
1214 sizeof(BOOL), TRUE, FALSE, 0, 0 },
1215 { ASN_OCTETSTRING, offsetof(CERT_EXTENSION, Value),
1216 CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_OBJID_BLOB), FALSE, TRUE,
1217 offsetof(CERT_EXTENSION, Value.pbData) },
1220 PCERT_EXTENSION ext = (PCERT_EXTENSION)pvStructInfo;
1222 TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags, ext,
1226 TRACE("ext->pszObjId is %p\n", ext->pszObjId);
1227 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
1228 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags, NULL,
1229 ext, pcbStructInfo, ext ? ext->pszObjId : NULL);
1231 TRACE("ext->pszObjId is %p (%s)\n", ext->pszObjId,
1232 debugstr_a(ext->pszObjId));
1233 TRACE("returning %d (%08x)\n", ret, GetLastError());
1237 static BOOL WINAPI CRYPT_AsnDecodeExtensionsInternal(DWORD dwCertEncodingType,
1238 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1239 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1242 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
1243 CRYPT_AsnDecodeExtension, sizeof(CERT_EXTENSION), TRUE,
1244 offsetof(CERT_EXTENSION, pszObjId) };
1245 PCERT_EXTENSIONS exts = (PCERT_EXTENSIONS)pvStructInfo;
1247 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1248 pDecodePara, pvStructInfo, *pcbStructInfo);
1250 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
1251 pDecodePara, pvStructInfo, pcbStructInfo, exts ? exts->rgExtension : NULL);
1255 static BOOL WINAPI CRYPT_AsnDecodeExtensions(DWORD dwCertEncodingType,
1256 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1257 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1263 ret = CRYPT_AsnDecodeExtensionsInternal(dwCertEncodingType,
1264 lpszStructType, pbEncoded, cbEncoded,
1265 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, pcbStructInfo);
1266 if (ret && pvStructInfo)
1268 ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
1269 pcbStructInfo, *pcbStructInfo);
1272 CERT_EXTENSIONS *exts;
1274 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
1275 pvStructInfo = *(BYTE **)pvStructInfo;
1276 exts = (CERT_EXTENSIONS *)pvStructInfo;
1277 exts->rgExtension = (CERT_EXTENSION *)((BYTE *)exts +
1278 sizeof(CERT_EXTENSIONS));
1279 ret = CRYPT_AsnDecodeExtensionsInternal(dwCertEncodingType,
1280 lpszStructType, pbEncoded, cbEncoded,
1281 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
1288 SetLastError(STATUS_ACCESS_VIOLATION);
1295 /* Warning: this assumes the address of value->Value.pbData is already set, in
1296 * order to avoid overwriting memory. (In some cases, it may change it, if it
1297 * doesn't copy anything to memory.) Be sure to set it correctly!
1299 static BOOL WINAPI CRYPT_AsnDecodeNameValueInternal(DWORD dwCertEncodingType,
1300 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1301 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1305 CERT_NAME_VALUE *value = (CERT_NAME_VALUE *)pvStructInfo;
1307 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
1309 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
1310 DWORD bytesNeeded = sizeof(CERT_NAME_VALUE), valueType;
1312 switch (pbEncoded[0])
1314 case ASN_OCTETSTRING:
1315 valueType = CERT_RDN_OCTET_STRING;
1316 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1317 bytesNeeded += dataLen;
1319 case ASN_NUMERICSTRING:
1320 valueType = CERT_RDN_NUMERIC_STRING;
1321 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1322 bytesNeeded += dataLen;
1324 case ASN_PRINTABLESTRING:
1325 valueType = CERT_RDN_PRINTABLE_STRING;
1326 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1327 bytesNeeded += dataLen;
1330 valueType = CERT_RDN_IA5_STRING;
1331 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1332 bytesNeeded += dataLen;
1335 valueType = CERT_RDN_T61_STRING;
1336 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1337 bytesNeeded += dataLen;
1339 case ASN_VIDEOTEXSTRING:
1340 valueType = CERT_RDN_VIDEOTEX_STRING;
1341 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1342 bytesNeeded += dataLen;
1344 case ASN_GRAPHICSTRING:
1345 valueType = CERT_RDN_GRAPHIC_STRING;
1346 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1347 bytesNeeded += dataLen;
1349 case ASN_VISIBLESTRING:
1350 valueType = CERT_RDN_VISIBLE_STRING;
1351 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1352 bytesNeeded += dataLen;
1354 case ASN_GENERALSTRING:
1355 valueType = CERT_RDN_GENERAL_STRING;
1356 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1357 bytesNeeded += dataLen;
1359 case ASN_UNIVERSALSTRING:
1360 FIXME("ASN_UNIVERSALSTRING: unimplemented\n");
1361 SetLastError(CRYPT_E_ASN1_BADTAG);
1364 valueType = CERT_RDN_BMP_STRING;
1365 bytesNeeded += dataLen;
1367 case ASN_UTF8STRING:
1368 valueType = CERT_RDN_UTF8_STRING;
1369 bytesNeeded += MultiByteToWideChar(CP_UTF8, 0,
1370 (LPCSTR)pbEncoded + 1 + lenBytes, dataLen, NULL, 0) * 2;
1373 SetLastError(CRYPT_E_ASN1_BADTAG);
1378 *pcbStructInfo = bytesNeeded;
1379 else if (*pcbStructInfo < bytesNeeded)
1381 *pcbStructInfo = bytesNeeded;
1382 SetLastError(ERROR_MORE_DATA);
1387 *pcbStructInfo = bytesNeeded;
1388 value->dwValueType = valueType;
1393 assert(value->Value.pbData);
1394 switch (pbEncoded[0])
1396 case ASN_OCTETSTRING:
1397 case ASN_NUMERICSTRING:
1398 case ASN_PRINTABLESTRING:
1401 case ASN_VIDEOTEXSTRING:
1402 case ASN_GRAPHICSTRING:
1403 case ASN_VISIBLESTRING:
1404 case ASN_GENERALSTRING:
1405 value->Value.cbData = dataLen;
1408 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1409 memcpy(value->Value.pbData,
1410 pbEncoded + 1 + lenBytes, dataLen);
1412 value->Value.pbData = (LPBYTE)pbEncoded + 1 +
1418 LPWSTR str = (LPWSTR)value->Value.pbData;
1420 value->Value.cbData = dataLen;
1421 for (i = 0; i < dataLen / 2; i++)
1422 str[i] = (pbEncoded[1 + lenBytes + 2 * i] << 8) |
1423 pbEncoded[1 + lenBytes + 2 * i + 1];
1426 case ASN_UTF8STRING:
1428 LPWSTR str = (LPWSTR)value->Value.pbData;
1430 value->Value.cbData = MultiByteToWideChar(CP_UTF8, 0,
1431 (LPCSTR)pbEncoded + 1 + lenBytes, dataLen,
1432 str, bytesNeeded - sizeof(CERT_NAME_VALUE)) * 2;
1439 value->Value.cbData = 0;
1440 value->Value.pbData = NULL;
1447 static BOOL WINAPI CRYPT_AsnDecodeNameValue(DWORD dwCertEncodingType,
1448 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1449 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1455 ret = CRYPT_AsnDecodeNameValueInternal(dwCertEncodingType,
1456 lpszStructType, pbEncoded, cbEncoded,
1457 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, pcbStructInfo);
1458 if (ret && pvStructInfo)
1460 ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
1461 pcbStructInfo, *pcbStructInfo);
1464 CERT_NAME_VALUE *value;
1466 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
1467 pvStructInfo = *(BYTE **)pvStructInfo;
1468 value = (CERT_NAME_VALUE *)pvStructInfo;
1469 value->Value.pbData = ((BYTE *)value + sizeof(CERT_NAME_VALUE));
1470 ret = CRYPT_AsnDecodeNameValueInternal(dwCertEncodingType,
1471 lpszStructType, pbEncoded, cbEncoded,
1472 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
1479 SetLastError(STATUS_ACCESS_VIOLATION);
1486 static BOOL WINAPI CRYPT_AsnDecodeUnicodeNameValueInternal(
1487 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
1488 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
1489 void *pvStructInfo, DWORD *pcbStructInfo)
1493 CERT_NAME_VALUE *value = (CERT_NAME_VALUE *)pvStructInfo;
1495 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
1497 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
1498 DWORD bytesNeeded = sizeof(CERT_NAME_VALUE), valueType;
1500 switch (pbEncoded[0])
1502 case ASN_NUMERICSTRING:
1503 valueType = CERT_RDN_NUMERIC_STRING;
1504 bytesNeeded += dataLen * 2;
1506 case ASN_PRINTABLESTRING:
1507 valueType = CERT_RDN_PRINTABLE_STRING;
1508 bytesNeeded += dataLen * 2;
1511 valueType = CERT_RDN_IA5_STRING;
1512 bytesNeeded += dataLen * 2;
1515 valueType = CERT_RDN_T61_STRING;
1516 bytesNeeded += dataLen * 2;
1518 case ASN_VIDEOTEXSTRING:
1519 valueType = CERT_RDN_VIDEOTEX_STRING;
1520 bytesNeeded += dataLen * 2;
1522 case ASN_GRAPHICSTRING:
1523 valueType = CERT_RDN_GRAPHIC_STRING;
1524 bytesNeeded += dataLen * 2;
1526 case ASN_VISIBLESTRING:
1527 valueType = CERT_RDN_VISIBLE_STRING;
1528 bytesNeeded += dataLen * 2;
1530 case ASN_GENERALSTRING:
1531 valueType = CERT_RDN_GENERAL_STRING;
1532 bytesNeeded += dataLen * 2;
1534 case ASN_UNIVERSALSTRING:
1535 valueType = CERT_RDN_UNIVERSAL_STRING;
1536 bytesNeeded += dataLen / 2;
1539 valueType = CERT_RDN_BMP_STRING;
1540 bytesNeeded += dataLen;
1542 case ASN_UTF8STRING:
1543 valueType = CERT_RDN_UTF8_STRING;
1544 bytesNeeded += MultiByteToWideChar(CP_UTF8, 0,
1545 (LPCSTR)pbEncoded + 1 + lenBytes, dataLen, NULL, 0) * 2;
1548 SetLastError(CRYPT_E_ASN1_BADTAG);
1553 *pcbStructInfo = bytesNeeded;
1554 else if (*pcbStructInfo < bytesNeeded)
1556 *pcbStructInfo = bytesNeeded;
1557 SetLastError(ERROR_MORE_DATA);
1562 *pcbStructInfo = bytesNeeded;
1563 value->dwValueType = valueType;
1567 LPWSTR str = (LPWSTR)value->Value.pbData;
1569 assert(value->Value.pbData);
1570 switch (pbEncoded[0])
1572 case ASN_NUMERICSTRING:
1573 case ASN_PRINTABLESTRING:
1576 case ASN_VIDEOTEXSTRING:
1577 case ASN_GRAPHICSTRING:
1578 case ASN_VISIBLESTRING:
1579 case ASN_GENERALSTRING:
1580 value->Value.cbData = dataLen * 2;
1581 for (i = 0; i < dataLen; i++)
1582 str[i] = pbEncoded[1 + lenBytes + i];
1584 case ASN_UNIVERSALSTRING:
1585 value->Value.cbData = dataLen / 2;
1586 for (i = 0; i < dataLen / 4; i++)
1587 str[i] = (pbEncoded[1 + lenBytes + 2 * i + 2] << 8)
1588 | pbEncoded[1 + lenBytes + 2 * i + 3];
1591 value->Value.cbData = dataLen;
1592 for (i = 0; i < dataLen / 2; i++)
1593 str[i] = (pbEncoded[1 + lenBytes + 2 * i] << 8) |
1594 pbEncoded[1 + lenBytes + 2 * i + 1];
1596 case ASN_UTF8STRING:
1597 value->Value.cbData = MultiByteToWideChar(CP_UTF8, 0,
1598 (LPCSTR)pbEncoded + 1 + lenBytes, dataLen,
1599 str, bytesNeeded - sizeof(CERT_NAME_VALUE)) * 2;
1605 value->Value.cbData = 0;
1606 value->Value.pbData = NULL;
1613 static BOOL WINAPI CRYPT_AsnDecodeUnicodeNameValue(DWORD dwCertEncodingType,
1614 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1615 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1621 ret = CRYPT_AsnDecodeUnicodeNameValueInternal(dwCertEncodingType,
1622 lpszStructType, pbEncoded, cbEncoded,
1623 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, pcbStructInfo);
1624 if (ret && pvStructInfo)
1626 ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
1627 pcbStructInfo, *pcbStructInfo);
1630 CERT_NAME_VALUE *value;
1632 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
1633 pvStructInfo = *(BYTE **)pvStructInfo;
1634 value = (CERT_NAME_VALUE *)pvStructInfo;
1635 value->Value.pbData = ((BYTE *)value + sizeof(CERT_NAME_VALUE));
1636 ret = CRYPT_AsnDecodeUnicodeNameValueInternal(
1637 dwCertEncodingType, lpszStructType, pbEncoded, cbEncoded,
1638 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
1645 SetLastError(STATUS_ACCESS_VIOLATION);
1652 static BOOL WINAPI CRYPT_AsnDecodeRdnAttr(DWORD dwCertEncodingType,
1653 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1654 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1657 struct AsnDecodeSequenceItem items[] = {
1658 { ASN_OBJECTIDENTIFIER, offsetof(CERT_RDN_ATTR, pszObjId),
1659 CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
1660 offsetof(CERT_RDN_ATTR, pszObjId), 0 },
1661 { 0, offsetof(CERT_RDN_ATTR, dwValueType),
1662 CRYPT_AsnDecodeNameValueInternal, sizeof(CERT_NAME_VALUE),
1663 FALSE, TRUE, offsetof(CERT_RDN_ATTR, Value.pbData), 0 },
1665 CERT_RDN_ATTR *attr = (CERT_RDN_ATTR *)pvStructInfo;
1667 TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1668 pvStructInfo, *pcbStructInfo);
1671 TRACE("attr->pszObjId is %p\n", attr->pszObjId);
1672 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
1673 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags, NULL,
1674 attr, pcbStructInfo, attr ? attr->pszObjId : NULL);
1677 TRACE("attr->pszObjId is %p (%s)\n", attr->pszObjId,
1678 debugstr_a(attr->pszObjId));
1679 TRACE("attr->dwValueType is %d\n", attr->dwValueType);
1681 TRACE("returning %d (%08x)\n", ret, GetLastError());
1685 static BOOL WINAPI CRYPT_AsnDecodeRdn(DWORD dwCertEncodingType,
1686 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1687 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1690 struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
1691 CRYPT_AsnDecodeRdnAttr, sizeof(CERT_RDN_ATTR), TRUE,
1692 offsetof(CERT_RDN_ATTR, pszObjId) };
1693 PCERT_RDN rdn = (PCERT_RDN)pvStructInfo;
1695 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
1696 pDecodePara, pvStructInfo, pcbStructInfo, rdn ? rdn->rgRDNAttr : NULL);
1700 static BOOL WINAPI CRYPT_AsnDecodeName(DWORD dwCertEncodingType,
1701 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1702 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1708 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
1709 CRYPT_AsnDecodeRdn, sizeof(CERT_RDN), TRUE,
1710 offsetof(CERT_RDN, rgRDNAttr) };
1712 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
1713 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
1717 SetLastError(STATUS_ACCESS_VIOLATION);
1724 static BOOL WINAPI CRYPT_AsnDecodeUnicodeRdnAttr(DWORD dwCertEncodingType,
1725 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1726 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1729 struct AsnDecodeSequenceItem items[] = {
1730 { ASN_OBJECTIDENTIFIER, offsetof(CERT_RDN_ATTR, pszObjId),
1731 CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
1732 offsetof(CERT_RDN_ATTR, pszObjId), 0 },
1733 { 0, offsetof(CERT_RDN_ATTR, dwValueType),
1734 CRYPT_AsnDecodeUnicodeNameValueInternal, sizeof(CERT_NAME_VALUE),
1735 FALSE, TRUE, offsetof(CERT_RDN_ATTR, Value.pbData), 0 },
1737 CERT_RDN_ATTR *attr = (CERT_RDN_ATTR *)pvStructInfo;
1739 TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1740 pvStructInfo, *pcbStructInfo);
1743 TRACE("attr->pszObjId is %p\n", attr->pszObjId);
1744 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
1745 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags, NULL,
1746 attr, pcbStructInfo, attr ? attr->pszObjId : NULL);
1749 TRACE("attr->pszObjId is %p (%s)\n", attr->pszObjId,
1750 debugstr_a(attr->pszObjId));
1751 TRACE("attr->dwValueType is %d\n", attr->dwValueType);
1753 TRACE("returning %d (%08x)\n", ret, GetLastError());
1757 static BOOL WINAPI CRYPT_AsnDecodeUnicodeRdn(DWORD dwCertEncodingType,
1758 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1759 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1762 struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
1763 CRYPT_AsnDecodeUnicodeRdnAttr, sizeof(CERT_RDN_ATTR), TRUE,
1764 offsetof(CERT_RDN_ATTR, pszObjId) };
1765 PCERT_RDN rdn = (PCERT_RDN)pvStructInfo;
1767 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
1768 pDecodePara, pvStructInfo, pcbStructInfo, rdn ? rdn->rgRDNAttr : NULL);
1772 static BOOL WINAPI CRYPT_AsnDecodeUnicodeName(DWORD dwCertEncodingType,
1773 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1774 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1780 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
1781 CRYPT_AsnDecodeUnicodeRdn, sizeof(CERT_RDN), TRUE,
1782 offsetof(CERT_RDN, rgRDNAttr) };
1784 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
1785 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
1789 SetLastError(STATUS_ACCESS_VIOLATION);
1796 static BOOL WINAPI CRYPT_AsnDecodeCopyBytes(DWORD dwCertEncodingType,
1797 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1798 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1801 DWORD bytesNeeded = sizeof(CRYPT_OBJID_BLOB);
1803 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1804 pDecodePara, pvStructInfo, *pcbStructInfo);
1806 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
1807 bytesNeeded += cbEncoded;
1809 *pcbStructInfo = bytesNeeded;
1810 else if (*pcbStructInfo < bytesNeeded)
1812 SetLastError(ERROR_MORE_DATA);
1813 *pcbStructInfo = bytesNeeded;
1818 PCRYPT_OBJID_BLOB blob = (PCRYPT_OBJID_BLOB)pvStructInfo;
1820 *pcbStructInfo = bytesNeeded;
1821 blob->cbData = cbEncoded;
1822 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
1823 blob->pbData = (LPBYTE)pbEncoded;
1826 assert(blob->pbData);
1827 memcpy(blob->pbData, pbEncoded, blob->cbData);
1833 static BOOL WINAPI CRYPT_DecodeDERArray(DWORD dwCertEncodingType,
1834 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1835 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1838 struct AsnArrayDescriptor arrayDesc = { 0, CRYPT_AsnDecodeCopyBytes,
1839 sizeof(CRYPT_DER_BLOB), TRUE, offsetof(CRYPT_DER_BLOB, pbData) };
1840 struct GenericArray *array = (struct GenericArray *)pvStructInfo;
1842 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1843 pDecodePara, pvStructInfo, *pcbStructInfo);
1845 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
1846 pDecodePara, pvStructInfo, pcbStructInfo, array ? array->rgItems : NULL);
1850 static BOOL WINAPI CRYPT_AsnDecodePKCSAttribute(DWORD dwCertEncodingType,
1851 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1852 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1858 struct AsnDecodeSequenceItem items[] = {
1859 { ASN_OBJECTIDENTIFIER, offsetof(CRYPT_ATTRIBUTE, pszObjId),
1860 CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
1861 offsetof(CRYPT_ATTRIBUTE, pszObjId), 0 },
1862 { ASN_CONSTRUCTOR | ASN_SETOF, offsetof(CRYPT_ATTRIBUTE, cValue),
1863 CRYPT_DecodeDERArray, sizeof(struct GenericArray), FALSE, TRUE,
1864 offsetof(CRYPT_ATTRIBUTE, rgValue), 0 },
1866 PCRYPT_ATTRIBUTE attr = (PCRYPT_ATTRIBUTE)pvStructInfo;
1868 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
1869 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
1870 pDecodePara, pvStructInfo, pcbStructInfo, attr ? attr->pszObjId :
1875 SetLastError(STATUS_ACCESS_VIOLATION);
1878 TRACE("returning %d\n", ret);
1882 static BOOL WINAPI CRYPT_AsnDecodePKCSAttributesInternal(
1883 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
1884 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
1885 void *pvStructInfo, DWORD *pcbStructInfo)
1887 struct AsnArrayDescriptor arrayDesc = { 0, CRYPT_AsnDecodePKCSAttribute,
1888 sizeof(CRYPT_ATTRIBUTE), TRUE, offsetof(CRYPT_ATTRIBUTE, pszObjId) };
1889 PCRYPT_ATTRIBUTES attrs = (PCRYPT_ATTRIBUTES)pvStructInfo;
1892 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
1893 pDecodePara, pvStructInfo, pcbStructInfo, attrs ? attrs->rgAttr :
1898 static BOOL WINAPI CRYPT_AsnDecodePKCSAttributes(DWORD dwCertEncodingType,
1899 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1900 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1904 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1905 pDecodePara, pvStructInfo, *pcbStructInfo);
1912 SetLastError(CRYPT_E_ASN1_EOD);
1913 else if (pbEncoded[0] != (ASN_CONSTRUCTOR | ASN_SETOF))
1914 SetLastError(CRYPT_E_ASN1_CORRUPT);
1915 else if ((ret = CRYPT_AsnDecodePKCSAttributesInternal(
1916 dwCertEncodingType, lpszStructType, pbEncoded, cbEncoded,
1917 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
1920 *pcbStructInfo = bytesNeeded;
1921 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
1922 pvStructInfo, pcbStructInfo, bytesNeeded)))
1924 PCRYPT_ATTRIBUTES attrs;
1926 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
1927 pvStructInfo = *(BYTE **)pvStructInfo;
1928 attrs = (PCRYPT_ATTRIBUTES)pvStructInfo;
1929 attrs->rgAttr = (PCRYPT_ATTRIBUTE)((BYTE *)pvStructInfo +
1930 sizeof(CRYPT_ATTRIBUTES));
1931 ret = CRYPT_AsnDecodePKCSAttributesInternal(dwCertEncodingType,
1932 lpszStructType, pbEncoded, cbEncoded,
1933 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
1940 SetLastError(STATUS_ACCESS_VIOLATION);
1943 TRACE("returning %d\n", ret);
1947 static BOOL WINAPI CRYPT_AsnDecodeAlgorithmId(DWORD dwCertEncodingType,
1948 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1949 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1951 CRYPT_ALGORITHM_IDENTIFIER *algo =
1952 (CRYPT_ALGORITHM_IDENTIFIER *)pvStructInfo;
1954 struct AsnDecodeSequenceItem items[] = {
1955 { ASN_OBJECTIDENTIFIER, offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId),
1956 CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
1957 offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId), 0 },
1958 { 0, offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters),
1959 CRYPT_AsnDecodeCopyBytes, sizeof(CRYPT_OBJID_BLOB), TRUE, TRUE,
1960 offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters.pbData), 0 },
1963 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
1964 pDecodePara, pvStructInfo, *pcbStructInfo);
1966 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
1967 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
1968 pDecodePara, pvStructInfo, pcbStructInfo, algo ? algo->pszObjId : NULL);
1969 if (ret && pvStructInfo)
1971 TRACE("pszObjId is %p (%s)\n", algo->pszObjId,
1972 debugstr_a(algo->pszObjId));
1977 static BOOL WINAPI CRYPT_AsnDecodePubKeyInfoInternal(DWORD dwCertEncodingType,
1978 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
1979 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
1982 struct AsnDecodeSequenceItem items[] = {
1983 { ASN_SEQUENCEOF, offsetof(CERT_PUBLIC_KEY_INFO, Algorithm),
1984 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
1985 FALSE, TRUE, offsetof(CERT_PUBLIC_KEY_INFO,
1986 Algorithm.pszObjId) },
1987 { ASN_BITSTRING, offsetof(CERT_PUBLIC_KEY_INFO, PublicKey),
1988 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
1989 offsetof(CERT_PUBLIC_KEY_INFO, PublicKey.pbData) },
1991 PCERT_PUBLIC_KEY_INFO info = (PCERT_PUBLIC_KEY_INFO)pvStructInfo;
1993 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
1994 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
1995 pDecodePara, pvStructInfo, pcbStructInfo, info ?
1996 info->Algorithm.Parameters.pbData : NULL);
2000 static BOOL WINAPI CRYPT_AsnDecodePubKeyInfo(DWORD dwCertEncodingType,
2001 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2002 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2010 if ((ret = CRYPT_AsnDecodePubKeyInfoInternal(dwCertEncodingType,
2011 lpszStructType, pbEncoded, cbEncoded,
2012 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
2015 *pcbStructInfo = bytesNeeded;
2016 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
2017 pvStructInfo, pcbStructInfo, bytesNeeded)))
2019 PCERT_PUBLIC_KEY_INFO info;
2021 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2022 pvStructInfo = *(BYTE **)pvStructInfo;
2023 info = (PCERT_PUBLIC_KEY_INFO)pvStructInfo;
2024 info->Algorithm.Parameters.pbData = (BYTE *)pvStructInfo +
2025 sizeof(CERT_PUBLIC_KEY_INFO);
2026 ret = CRYPT_AsnDecodePubKeyInfoInternal(dwCertEncodingType,
2027 lpszStructType, pbEncoded, cbEncoded,
2028 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
2035 SetLastError(STATUS_ACCESS_VIOLATION);
2042 static BOOL WINAPI CRYPT_AsnDecodeBool(DWORD dwCertEncodingType,
2043 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2044 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2050 SetLastError(CRYPT_E_ASN1_CORRUPT);
2053 if (GET_LEN_BYTES(pbEncoded[1]) > 1)
2055 SetLastError(CRYPT_E_ASN1_CORRUPT);
2058 if (pbEncoded[1] > 1)
2060 SetLastError(CRYPT_E_ASN1_CORRUPT);
2065 *pcbStructInfo = sizeof(BOOL);
2068 else if (*pcbStructInfo < sizeof(BOOL))
2070 *pcbStructInfo = sizeof(BOOL);
2071 SetLastError(ERROR_MORE_DATA);
2076 *(BOOL *)pvStructInfo = pbEncoded[2] ? TRUE : FALSE;
2079 TRACE("returning %d (%08x)\n", ret, GetLastError());
2083 static BOOL WINAPI CRYPT_AsnDecodeAltNameEntry(DWORD dwCertEncodingType,
2084 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2085 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2087 PCERT_ALT_NAME_ENTRY entry = (PCERT_ALT_NAME_ENTRY)pvStructInfo;
2088 DWORD dataLen, lenBytes, bytesNeeded = sizeof(CERT_ALT_NAME_ENTRY);
2091 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2092 pDecodePara, pvStructInfo, *pcbStructInfo);
2096 SetLastError(CRYPT_E_ASN1_CORRUPT);
2099 lenBytes = GET_LEN_BYTES(pbEncoded[1]);
2100 if (1 + lenBytes > cbEncoded)
2102 SetLastError(CRYPT_E_ASN1_CORRUPT);
2105 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2107 switch (pbEncoded[0] & ASN_TYPE_MASK)
2109 case 1: /* rfc822Name */
2110 case 2: /* dNSName */
2111 case 6: /* uniformResourceIdentifier */
2112 bytesNeeded += (dataLen + 1) * sizeof(WCHAR);
2114 case 4: /* directoryName */
2115 case 7: /* iPAddress */
2116 bytesNeeded += dataLen;
2118 case 8: /* registeredID */
2119 ret = CRYPT_AsnDecodeOidIgnoreTag(dwCertEncodingType, NULL,
2120 pbEncoded, cbEncoded, 0, NULL, NULL, &dataLen);
2123 /* FIXME: ugly, shouldn't need to know internals of OID decode
2124 * function to use it.
2126 bytesNeeded += dataLen - sizeof(LPSTR);
2129 case 0: /* otherName */
2130 FIXME("%d: stub\n", pbEncoded[0] & ASN_TYPE_MASK);
2131 SetLastError(CRYPT_E_ASN1_BADTAG);
2134 case 3: /* x400Address, unimplemented */
2135 case 5: /* ediPartyName, unimplemented */
2136 TRACE("type %d unimplemented\n", pbEncoded[0] & ASN_TYPE_MASK);
2137 SetLastError(CRYPT_E_ASN1_BADTAG);
2141 TRACE("type %d bad\n", pbEncoded[0] & ASN_TYPE_MASK);
2142 SetLastError(CRYPT_E_ASN1_CORRUPT);
2148 *pcbStructInfo = bytesNeeded;
2149 else if (*pcbStructInfo < bytesNeeded)
2151 *pcbStructInfo = bytesNeeded;
2152 SetLastError(ERROR_MORE_DATA);
2157 *pcbStructInfo = bytesNeeded;
2158 /* MS used values one greater than the asn1 ones.. sigh */
2159 entry->dwAltNameChoice = (pbEncoded[0] & 0x7f) + 1;
2160 switch (pbEncoded[0] & ASN_TYPE_MASK)
2162 case 1: /* rfc822Name */
2163 case 2: /* dNSName */
2164 case 6: /* uniformResourceIdentifier */
2168 for (i = 0; i < dataLen; i++)
2169 entry->u.pwszURL[i] =
2170 (WCHAR)pbEncoded[1 + lenBytes + i];
2171 entry->u.pwszURL[i] = 0;
2172 TRACE("URL is %p (%s)\n", entry->u.pwszURL,
2173 debugstr_w(entry->u.pwszURL));
2176 case 4: /* directoryName */
2177 entry->dwAltNameChoice = CERT_ALT_NAME_DIRECTORY_NAME;
2178 /* The data are memory-equivalent with the IPAddress case,
2181 case 7: /* iPAddress */
2182 /* The next data pointer is in the pwszURL spot, that is,
2183 * the first 4 bytes. Need to move it to the next spot.
2185 entry->u.IPAddress.pbData = (LPBYTE)entry->u.pwszURL;
2186 entry->u.IPAddress.cbData = dataLen;
2187 memcpy(entry->u.IPAddress.pbData, pbEncoded + 1 + lenBytes,
2190 case 8: /* registeredID */
2191 ret = CRYPT_AsnDecodeOidIgnoreTag(dwCertEncodingType, NULL,
2192 pbEncoded, cbEncoded, 0, NULL, &entry->u.pszRegisteredID,
2202 static BOOL WINAPI CRYPT_AsnDecodeAltNameInternal(DWORD dwCertEncodingType,
2203 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2204 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2207 struct AsnArrayDescriptor arrayDesc = { 0,
2208 CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
2209 offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
2210 PCERT_ALT_NAME_INFO info = (PCERT_ALT_NAME_INFO)pvStructInfo;
2212 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2213 pDecodePara, pvStructInfo, *pcbStructInfo);
2216 TRACE("info->rgAltEntry is %p\n", info->rgAltEntry);
2217 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
2218 pDecodePara, pvStructInfo, pcbStructInfo, info ? info->rgAltEntry : NULL);
2222 /* Like CRYPT_AsnDecodeIntegerInternal, but swaps the bytes */
2223 static BOOL WINAPI CRYPT_AsnDecodeIntegerSwapBytes(DWORD dwCertEncodingType,
2224 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2225 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2229 TRACE("(%p, %d, 0x%08x, %p, %p, %d)\n", pbEncoded, cbEncoded, dwFlags,
2230 pDecodePara, pvStructInfo, *pcbStructInfo);
2232 /* Can't use the CRYPT_DECODE_NOCOPY_FLAG, because we modify the bytes in-
2235 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType, lpszStructType,
2236 pbEncoded, cbEncoded, dwFlags & ~CRYPT_DECODE_NOCOPY_FLAG, pDecodePara,
2237 pvStructInfo, pcbStructInfo);
2238 if (ret && pvStructInfo)
2240 CRYPT_DATA_BLOB *blob = (CRYPT_DATA_BLOB *)pvStructInfo;
2247 for (i = 0; i < blob->cbData / 2; i++)
2249 temp = blob->pbData[i];
2250 blob->pbData[i] = blob->pbData[blob->cbData - i - 1];
2251 blob->pbData[blob->cbData - i - 1] = temp;
2255 TRACE("returning %d (%08x)\n", ret, GetLastError());
2259 static BOOL WINAPI CRYPT_AsnDecodeAuthorityKeyId(DWORD dwCertEncodingType,
2260 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2261 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2267 struct AsnDecodeSequenceItem items[] = {
2268 { ASN_CONTEXT | 0, offsetof(CERT_AUTHORITY_KEY_ID_INFO, KeyId),
2269 CRYPT_AsnDecodeIntegerSwapBytes, sizeof(CRYPT_DATA_BLOB),
2270 TRUE, TRUE, offsetof(CERT_AUTHORITY_KEY_ID_INFO, KeyId.pbData), 0 },
2271 { ASN_CONTEXT | ASN_CONSTRUCTOR| 1,
2272 offsetof(CERT_AUTHORITY_KEY_ID_INFO, CertIssuer),
2273 CRYPT_AsnDecodeOctetsInternal, sizeof(CERT_NAME_BLOB), TRUE, TRUE,
2274 offsetof(CERT_AUTHORITY_KEY_ID_INFO, CertIssuer.pbData), 0 },
2275 { ASN_CONTEXT | 2, offsetof(CERT_AUTHORITY_KEY_ID_INFO,
2276 CertSerialNumber), CRYPT_AsnDecodeIntegerInternal,
2277 sizeof(CRYPT_INTEGER_BLOB), TRUE, TRUE,
2278 offsetof(CERT_AUTHORITY_KEY_ID_INFO, CertSerialNumber.pbData), 0 },
2281 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
2282 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
2283 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
2287 SetLastError(STATUS_ACCESS_VIOLATION);
2294 static BOOL WINAPI CRYPT_AsnDecodeAuthorityKeyId2(DWORD dwCertEncodingType,
2295 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2296 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2302 struct AsnDecodeSequenceItem items[] = {
2303 { ASN_CONTEXT | 0, offsetof(CERT_AUTHORITY_KEY_ID2_INFO, KeyId),
2304 CRYPT_AsnDecodeIntegerSwapBytes, sizeof(CRYPT_DATA_BLOB),
2305 TRUE, TRUE, offsetof(CERT_AUTHORITY_KEY_ID2_INFO, KeyId.pbData), 0 },
2306 { ASN_CONTEXT | ASN_CONSTRUCTOR| 1,
2307 offsetof(CERT_AUTHORITY_KEY_ID2_INFO, AuthorityCertIssuer),
2308 CRYPT_AsnDecodeAltNameInternal, sizeof(CERT_ALT_NAME_INFO), TRUE,
2309 TRUE, offsetof(CERT_AUTHORITY_KEY_ID2_INFO,
2310 AuthorityCertIssuer.rgAltEntry), 0 },
2311 { ASN_CONTEXT | 2, offsetof(CERT_AUTHORITY_KEY_ID2_INFO,
2312 AuthorityCertSerialNumber), CRYPT_AsnDecodeIntegerInternal,
2313 sizeof(CRYPT_INTEGER_BLOB), TRUE, TRUE,
2314 offsetof(CERT_AUTHORITY_KEY_ID2_INFO,
2315 AuthorityCertSerialNumber.pbData), 0 },
2318 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
2319 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
2320 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
2324 SetLastError(STATUS_ACCESS_VIOLATION);
2331 static BOOL WINAPI CRYPT_AsnDecodePKCSContent(DWORD dwCertEncodingType,
2332 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2333 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2338 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2339 pDecodePara, pvStructInfo, *pcbStructInfo);
2341 /* The caller has already checked the tag, no need to check it again.
2342 * Check the outer length is valid by calling CRYPT_GetLen:
2344 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2346 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
2349 pbEncoded += 1 + lenBytes;
2350 /* Check the inner length is valid by calling CRYPT_GetLen again: */
2351 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &innerLen)))
2353 ret = CRYPT_AsnDecodeCopyBytes(dwCertEncodingType, NULL,
2354 pbEncoded, dataLen, dwFlags, pDecodePara, pvStructInfo,
2361 static BOOL WINAPI CRYPT_AsnDecodePKCSContentInfoInternal(
2362 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
2363 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
2364 void *pvStructInfo, DWORD *pcbStructInfo)
2366 CRYPT_CONTENT_INFO *info = (CRYPT_CONTENT_INFO *)pvStructInfo;
2367 struct AsnDecodeSequenceItem items[] = {
2368 { ASN_OBJECTIDENTIFIER, offsetof(CRYPT_CONTENT_INFO, pszObjId),
2369 CRYPT_AsnDecodeOidIgnoreTag, sizeof(LPSTR), FALSE, TRUE,
2370 offsetof(CRYPT_CONTENT_INFO, pszObjId), 0 },
2371 { ASN_CONTEXT | ASN_CONSTRUCTOR | 0,
2372 offsetof(CRYPT_CONTENT_INFO, Content), CRYPT_AsnDecodePKCSContent,
2373 sizeof(CRYPT_DER_BLOB), TRUE, TRUE,
2374 offsetof(CRYPT_CONTENT_INFO, Content.pbData), 0 },
2378 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2379 pDecodePara, pvStructInfo, *pcbStructInfo);
2381 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
2382 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
2383 pDecodePara, pvStructInfo, pcbStructInfo, info ? info->pszObjId : NULL);
2387 static BOOL WINAPI CRYPT_AsnDecodePKCSContentInfo(DWORD dwCertEncodingType,
2388 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2389 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2393 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2394 pDecodePara, pvStructInfo, *pcbStructInfo);
2398 ret = CRYPT_AsnDecodePKCSContentInfoInternal(dwCertEncodingType,
2399 lpszStructType, pbEncoded, cbEncoded,
2400 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, pcbStructInfo);
2401 if (ret && pvStructInfo)
2403 ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
2404 pcbStructInfo, *pcbStructInfo);
2407 CRYPT_CONTENT_INFO *info;
2409 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2410 pvStructInfo = *(BYTE **)pvStructInfo;
2411 info = (CRYPT_CONTENT_INFO *)pvStructInfo;
2412 info->pszObjId = (LPSTR)((BYTE *)info +
2413 sizeof(CRYPT_CONTENT_INFO));
2414 ret = CRYPT_AsnDecodePKCSContentInfoInternal(dwCertEncodingType,
2415 lpszStructType, pbEncoded, cbEncoded,
2416 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
2423 SetLastError(STATUS_ACCESS_VIOLATION);
2429 BOOL CRYPT_AsnDecodePKCSDigestedData(const BYTE *pbEncoded, DWORD cbEncoded,
2430 DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
2431 CRYPT_DIGESTED_DATA *digestedData, DWORD *pcbDigestedData)
2434 struct AsnDecodeSequenceItem items[] = {
2435 { ASN_INTEGER, offsetof(CRYPT_DIGESTED_DATA, version), CRYPT_AsnDecodeInt,
2436 sizeof(DWORD), FALSE, FALSE, 0, 0 },
2437 { ASN_SEQUENCEOF, offsetof(CRYPT_DIGESTED_DATA, DigestAlgorithm),
2438 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
2439 FALSE, TRUE, offsetof(CRYPT_DIGESTED_DATA, DigestAlgorithm.pszObjId),
2441 { ASN_SEQUENCEOF, offsetof(CRYPT_DIGESTED_DATA, ContentInfo),
2442 CRYPT_AsnDecodePKCSContentInfoInternal,
2443 sizeof(CRYPT_CONTENT_INFO), FALSE, TRUE, offsetof(CRYPT_DIGESTED_DATA,
2444 ContentInfo.pszObjId), 0 },
2445 { ASN_OCTETSTRING, offsetof(CRYPT_DIGESTED_DATA, hash),
2446 CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_HASH_BLOB), FALSE, TRUE,
2447 offsetof(CRYPT_DIGESTED_DATA, hash.pbData), 0 },
2450 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
2451 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
2452 pDecodePara, digestedData, pcbDigestedData, NULL);
2456 static BOOL WINAPI CRYPT_AsnDecodeAltName(DWORD dwCertEncodingType,
2457 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2458 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2462 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2463 pDecodePara, pvStructInfo, *pcbStructInfo);
2467 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
2468 CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
2469 offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
2471 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
2472 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
2476 SetLastError(STATUS_ACCESS_VIOLATION);
2483 struct PATH_LEN_CONSTRAINT
2485 BOOL fPathLenConstraint;
2486 DWORD dwPathLenConstraint;
2489 static BOOL WINAPI CRYPT_AsnDecodePathLenConstraint(DWORD dwCertEncodingType,
2490 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2491 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2495 TRACE("%p, %d, %08x, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2496 pvStructInfo, *pcbStructInfo);
2500 if (pbEncoded[0] == ASN_INTEGER)
2502 DWORD bytesNeeded = sizeof(struct PATH_LEN_CONSTRAINT);
2505 *pcbStructInfo = bytesNeeded;
2506 else if (*pcbStructInfo < bytesNeeded)
2508 SetLastError(ERROR_MORE_DATA);
2509 *pcbStructInfo = bytesNeeded;
2514 struct PATH_LEN_CONSTRAINT *constraint =
2515 (struct PATH_LEN_CONSTRAINT *)pvStructInfo;
2516 DWORD size = sizeof(constraint->dwPathLenConstraint);
2518 ret = CRYPT_AsnDecodeInt(dwCertEncodingType, X509_INTEGER,
2519 pbEncoded, cbEncoded, 0, NULL,
2520 &constraint->dwPathLenConstraint, &size);
2522 constraint->fPathLenConstraint = TRUE;
2523 TRACE("got an int, dwPathLenConstraint is %d\n",
2524 constraint->dwPathLenConstraint);
2529 SetLastError(CRYPT_E_ASN1_CORRUPT);
2533 TRACE("returning %d (%08x)\n", ret, GetLastError());
2537 static BOOL WINAPI CRYPT_AsnDecodeSubtreeConstraints(DWORD dwCertEncodingType,
2538 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2539 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2542 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
2543 CRYPT_AsnDecodeCopyBytes, sizeof(CERT_NAME_BLOB), TRUE,
2544 offsetof(CERT_NAME_BLOB, pbData) };
2545 struct GenericArray *entries = (struct GenericArray *)pvStructInfo;
2547 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2548 pDecodePara, pvStructInfo, *pcbStructInfo);
2550 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
2551 pDecodePara, pvStructInfo, pcbStructInfo,
2552 entries ? entries->rgItems : NULL);
2553 TRACE("Returning %d (%08x)\n", ret, GetLastError());
2557 static BOOL WINAPI CRYPT_AsnDecodeBasicConstraints(DWORD dwCertEncodingType,
2558 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2559 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2565 struct AsnDecodeSequenceItem items[] = {
2566 { ASN_BITSTRING, offsetof(CERT_BASIC_CONSTRAINTS_INFO, SubjectType),
2567 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
2568 offsetof(CERT_BASIC_CONSTRAINTS_INFO, SubjectType.pbData), 0 },
2569 { ASN_INTEGER, offsetof(CERT_BASIC_CONSTRAINTS_INFO,
2570 fPathLenConstraint), CRYPT_AsnDecodePathLenConstraint,
2571 sizeof(struct PATH_LEN_CONSTRAINT), TRUE, FALSE, 0, 0 },
2572 { ASN_SEQUENCEOF, offsetof(CERT_BASIC_CONSTRAINTS_INFO,
2573 cSubtreesConstraint), CRYPT_AsnDecodeSubtreeConstraints,
2574 sizeof(struct GenericArray), TRUE, TRUE,
2575 offsetof(CERT_BASIC_CONSTRAINTS_INFO, rgSubtreesConstraint), 0 },
2578 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
2579 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
2580 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
2584 SetLastError(STATUS_ACCESS_VIOLATION);
2591 static BOOL WINAPI CRYPT_AsnDecodeBasicConstraints2(DWORD dwCertEncodingType,
2592 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2593 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2599 struct AsnDecodeSequenceItem items[] = {
2600 { ASN_BOOL, offsetof(CERT_BASIC_CONSTRAINTS2_INFO, fCA),
2601 CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE, FALSE, 0, 0 },
2602 { ASN_INTEGER, offsetof(CERT_BASIC_CONSTRAINTS2_INFO,
2603 fPathLenConstraint), CRYPT_AsnDecodePathLenConstraint,
2604 sizeof(struct PATH_LEN_CONSTRAINT), TRUE, FALSE, 0, 0 },
2607 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
2608 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
2609 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
2613 SetLastError(STATUS_ACCESS_VIOLATION);
2620 #define RSA1_MAGIC 0x31415352
2622 struct DECODED_RSA_PUB_KEY
2625 CRYPT_INTEGER_BLOB modulus;
2628 static BOOL WINAPI CRYPT_AsnDecodeRsaPubKey(DWORD dwCertEncodingType,
2629 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2630 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2636 struct AsnDecodeSequenceItem items[] = {
2637 { ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, modulus),
2638 CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
2639 FALSE, TRUE, offsetof(struct DECODED_RSA_PUB_KEY, modulus.pbData),
2641 { ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, pubexp),
2642 CRYPT_AsnDecodeInt, sizeof(DWORD), FALSE, FALSE, 0, 0 },
2644 struct DECODED_RSA_PUB_KEY *decodedKey = NULL;
2647 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
2648 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
2649 CRYPT_DECODE_ALLOC_FLAG, NULL, &decodedKey, &size, NULL);
2652 DWORD bytesNeeded = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
2653 decodedKey->modulus.cbData;
2657 *pcbStructInfo = bytesNeeded;
2660 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
2661 pvStructInfo, pcbStructInfo, bytesNeeded)))
2664 RSAPUBKEY *rsaPubKey;
2666 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2667 pvStructInfo = *(BYTE **)pvStructInfo;
2668 hdr = (BLOBHEADER *)pvStructInfo;
2669 hdr->bType = PUBLICKEYBLOB;
2670 hdr->bVersion = CUR_BLOB_VERSION;
2672 hdr->aiKeyAlg = CALG_RSA_KEYX;
2673 rsaPubKey = (RSAPUBKEY *)((BYTE *)pvStructInfo +
2674 sizeof(BLOBHEADER));
2675 rsaPubKey->magic = RSA1_MAGIC;
2676 rsaPubKey->pubexp = decodedKey->pubexp;
2677 rsaPubKey->bitlen = decodedKey->modulus.cbData * 8;
2678 memcpy((BYTE *)pvStructInfo + sizeof(BLOBHEADER) +
2679 sizeof(RSAPUBKEY), decodedKey->modulus.pbData,
2680 decodedKey->modulus.cbData);
2682 LocalFree(decodedKey);
2687 SetLastError(STATUS_ACCESS_VIOLATION);
2694 static BOOL WINAPI CRYPT_AsnDecodeOctetsInternal(DWORD dwCertEncodingType,
2695 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2696 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2699 DWORD bytesNeeded, dataLen;
2701 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2702 pDecodePara, pvStructInfo, *pcbStructInfo);
2704 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2706 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
2707 bytesNeeded = sizeof(CRYPT_DATA_BLOB);
2709 bytesNeeded = dataLen + sizeof(CRYPT_DATA_BLOB);
2711 *pcbStructInfo = bytesNeeded;
2712 else if (*pcbStructInfo < bytesNeeded)
2714 SetLastError(ERROR_MORE_DATA);
2715 *pcbStructInfo = bytesNeeded;
2720 CRYPT_DATA_BLOB *blob;
2721 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
2723 blob = (CRYPT_DATA_BLOB *)pvStructInfo;
2724 blob->cbData = dataLen;
2725 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
2726 blob->pbData = (BYTE *)pbEncoded + 1 + lenBytes;
2729 assert(blob->pbData);
2731 memcpy(blob->pbData, pbEncoded + 1 + lenBytes,
2739 static BOOL WINAPI CRYPT_AsnDecodeOctets(DWORD dwCertEncodingType,
2740 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2741 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2745 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
2746 pDecodePara, pvStructInfo, *pcbStructInfo);
2754 SetLastError(CRYPT_E_ASN1_CORRUPT);
2757 else if (pbEncoded[0] != ASN_OCTETSTRING)
2759 SetLastError(CRYPT_E_ASN1_BADTAG);
2762 else if ((ret = CRYPT_AsnDecodeOctetsInternal(dwCertEncodingType,
2763 lpszStructType, pbEncoded, cbEncoded,
2764 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
2767 *pcbStructInfo = bytesNeeded;
2768 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
2769 pvStructInfo, pcbStructInfo, bytesNeeded)))
2771 CRYPT_DATA_BLOB *blob;
2773 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2774 pvStructInfo = *(BYTE **)pvStructInfo;
2775 blob = (CRYPT_DATA_BLOB *)pvStructInfo;
2776 blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_DATA_BLOB);
2777 ret = CRYPT_AsnDecodeOctetsInternal(dwCertEncodingType,
2778 lpszStructType, pbEncoded, cbEncoded,
2779 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
2786 SetLastError(STATUS_ACCESS_VIOLATION);
2793 static BOOL WINAPI CRYPT_AsnDecodeBitsInternal(DWORD dwCertEncodingType,
2794 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2795 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2799 TRACE("(%p, %d, 0x%08x, %p, %p, %d)\n", pbEncoded, cbEncoded, dwFlags,
2800 pDecodePara, pvStructInfo, *pcbStructInfo);
2802 if (pbEncoded[0] == ASN_BITSTRING)
2804 DWORD bytesNeeded, dataLen;
2806 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2808 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
2809 bytesNeeded = sizeof(CRYPT_BIT_BLOB);
2811 bytesNeeded = dataLen - 1 + sizeof(CRYPT_BIT_BLOB);
2813 *pcbStructInfo = bytesNeeded;
2814 else if (*pcbStructInfo < bytesNeeded)
2816 *pcbStructInfo = bytesNeeded;
2817 SetLastError(ERROR_MORE_DATA);
2822 CRYPT_BIT_BLOB *blob;
2824 blob = (CRYPT_BIT_BLOB *)pvStructInfo;
2825 blob->cbData = dataLen - 1;
2826 blob->cUnusedBits = *(pbEncoded + 1 +
2827 GET_LEN_BYTES(pbEncoded[1]));
2828 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
2830 blob->pbData = (BYTE *)pbEncoded + 2 +
2831 GET_LEN_BYTES(pbEncoded[1]);
2835 assert(blob->pbData);
2838 BYTE mask = 0xff << blob->cUnusedBits;
2840 memcpy(blob->pbData, pbEncoded + 2 +
2841 GET_LEN_BYTES(pbEncoded[1]), blob->cbData);
2842 blob->pbData[blob->cbData - 1] &= mask;
2850 SetLastError(CRYPT_E_ASN1_BADTAG);
2853 TRACE("returning %d (%08x)\n", ret, GetLastError());
2857 static BOOL WINAPI CRYPT_AsnDecodeBits(DWORD dwCertEncodingType,
2858 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2859 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2863 TRACE("(%p, %d, 0x%08x, %p, %p, %p)\n", pbEncoded, cbEncoded, dwFlags,
2864 pDecodePara, pvStructInfo, pcbStructInfo);
2870 if ((ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType,
2871 lpszStructType, pbEncoded, cbEncoded,
2872 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
2875 *pcbStructInfo = bytesNeeded;
2876 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
2877 pvStructInfo, pcbStructInfo, bytesNeeded)))
2879 CRYPT_BIT_BLOB *blob;
2881 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2882 pvStructInfo = *(BYTE **)pvStructInfo;
2883 blob = (CRYPT_BIT_BLOB *)pvStructInfo;
2884 blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_BIT_BLOB);
2885 ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType,
2886 lpszStructType, pbEncoded, cbEncoded,
2887 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
2894 SetLastError(STATUS_ACCESS_VIOLATION);
2898 TRACE("returning %d (%08x)\n", ret, GetLastError());
2902 static BOOL WINAPI CRYPT_AsnDecodeInt(DWORD dwCertEncodingType,
2903 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2904 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2910 *pcbStructInfo = sizeof(int);
2915 BYTE buf[sizeof(CRYPT_INTEGER_BLOB) + sizeof(int)];
2916 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)buf;
2917 DWORD size = sizeof(buf);
2919 blob->pbData = buf + sizeof(CRYPT_INTEGER_BLOB);
2920 if (pbEncoded[0] != ASN_INTEGER)
2922 SetLastError(CRYPT_E_ASN1_BADTAG);
2926 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
2927 X509_MULTI_BYTE_INTEGER, pbEncoded, cbEncoded, 0, NULL, &buf,
2931 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
2932 pvStructInfo, pcbStructInfo, sizeof(int))))
2936 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2937 pvStructInfo = *(BYTE **)pvStructInfo;
2938 if (blob->pbData[blob->cbData - 1] & 0x80)
2940 /* initialize to a negative value to sign-extend */
2945 for (i = 0; i < blob->cbData; i++)
2948 val |= blob->pbData[blob->cbData - i - 1];
2950 memcpy(pvStructInfo, &val, sizeof(int));
2953 else if (GetLastError() == ERROR_MORE_DATA)
2954 SetLastError(CRYPT_E_ASN1_LARGE);
2958 SetLastError(STATUS_ACCESS_VIOLATION);
2965 static BOOL WINAPI CRYPT_AsnDecodeIntegerInternal(DWORD dwCertEncodingType,
2966 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2967 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2970 DWORD bytesNeeded, dataLen;
2972 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2974 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
2976 bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
2978 *pcbStructInfo = bytesNeeded;
2979 else if (*pcbStructInfo < bytesNeeded)
2981 *pcbStructInfo = bytesNeeded;
2982 SetLastError(ERROR_MORE_DATA);
2987 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
2989 blob->cbData = dataLen;
2990 assert(blob->pbData);
2995 for (i = 0; i < blob->cbData; i++)
2997 blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
3006 static BOOL WINAPI CRYPT_AsnDecodeInteger(DWORD dwCertEncodingType,
3007 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3008 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3016 if (pbEncoded[0] != ASN_INTEGER)
3018 SetLastError(CRYPT_E_ASN1_BADTAG);
3022 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
3023 lpszStructType, pbEncoded, cbEncoded,
3024 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded);
3028 *pcbStructInfo = bytesNeeded;
3029 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
3030 pvStructInfo, pcbStructInfo, bytesNeeded)))
3032 CRYPT_INTEGER_BLOB *blob;
3034 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3035 pvStructInfo = *(BYTE **)pvStructInfo;
3036 blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
3037 blob->pbData = (BYTE *)pvStructInfo +
3038 sizeof(CRYPT_INTEGER_BLOB);
3039 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
3040 lpszStructType, pbEncoded, cbEncoded,
3041 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pvStructInfo,
3048 SetLastError(STATUS_ACCESS_VIOLATION);
3055 static BOOL WINAPI CRYPT_AsnDecodeUnsignedIntegerInternal(
3056 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
3057 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
3058 void *pvStructInfo, DWORD *pcbStructInfo)
3062 if (pbEncoded[0] == ASN_INTEGER)
3064 DWORD bytesNeeded, dataLen;
3066 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3068 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3070 bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
3072 *pcbStructInfo = bytesNeeded;
3073 else if (*pcbStructInfo < bytesNeeded)
3075 *pcbStructInfo = bytesNeeded;
3076 SetLastError(ERROR_MORE_DATA);
3081 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
3083 blob->cbData = dataLen;
3084 assert(blob->pbData);
3085 /* remove leading zero byte if it exists */
3086 if (blob->cbData && *(pbEncoded + 1 + lenBytes) == 0)
3095 for (i = 0; i < blob->cbData; i++)
3097 blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
3106 SetLastError(CRYPT_E_ASN1_BADTAG);
3112 static BOOL WINAPI CRYPT_AsnDecodeUnsignedInteger(DWORD dwCertEncodingType,
3113 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3114 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3122 if ((ret = CRYPT_AsnDecodeUnsignedIntegerInternal(dwCertEncodingType,
3123 lpszStructType, pbEncoded, cbEncoded,
3124 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
3127 *pcbStructInfo = bytesNeeded;
3128 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
3129 pvStructInfo, pcbStructInfo, bytesNeeded)))
3131 CRYPT_INTEGER_BLOB *blob;
3133 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3134 pvStructInfo = *(BYTE **)pvStructInfo;
3135 blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
3136 blob->pbData = (BYTE *)pvStructInfo +
3137 sizeof(CRYPT_INTEGER_BLOB);
3138 ret = CRYPT_AsnDecodeUnsignedIntegerInternal(dwCertEncodingType,
3139 lpszStructType, pbEncoded, cbEncoded,
3140 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pvStructInfo,
3147 SetLastError(STATUS_ACCESS_VIOLATION);
3154 static BOOL WINAPI CRYPT_AsnDecodeEnumerated(DWORD dwCertEncodingType,
3155 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3156 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3162 *pcbStructInfo = sizeof(int);
3167 if (pbEncoded[0] == ASN_ENUMERATED)
3169 unsigned int val = 0, i;
3173 SetLastError(CRYPT_E_ASN1_EOD);
3176 else if (pbEncoded[1] == 0)
3178 SetLastError(CRYPT_E_ASN1_CORRUPT);
3183 /* A little strange looking, but we have to accept a sign byte:
3184 * 0xffffffff gets encoded as 0a 05 00 ff ff ff ff. Also,
3185 * assuming a small length is okay here, it has to be in short
3188 if (pbEncoded[1] > sizeof(unsigned int) + 1)
3190 SetLastError(CRYPT_E_ASN1_LARGE);
3193 for (i = 0; i < pbEncoded[1]; i++)
3196 val |= pbEncoded[2 + i];
3198 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
3199 pvStructInfo, pcbStructInfo, sizeof(unsigned int))))
3201 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3202 pvStructInfo = *(BYTE **)pvStructInfo;
3203 memcpy(pvStructInfo, &val, sizeof(unsigned int));
3209 SetLastError(CRYPT_E_ASN1_BADTAG);
3215 SetLastError(STATUS_ACCESS_VIOLATION);
3222 /* Modifies word, pbEncoded, and len, and magically sets a value ret to FALSE
3225 #define CRYPT_TIME_GET_DIGITS(pbEncoded, len, numDigits, word) \
3230 for (i = 0; (len) > 0 && i < (numDigits); i++, (len)--) \
3232 if (!isdigit(*(pbEncoded))) \
3234 SetLastError(CRYPT_E_ASN1_CORRUPT); \
3240 (word) += *(pbEncoded)++ - '0'; \
3245 static BOOL CRYPT_AsnDecodeTimeZone(const BYTE *pbEncoded, DWORD len,
3246 SYSTEMTIME *sysTime)
3253 if (len >= 3 && (*pbEncoded == '+' || *pbEncoded == '-'))
3255 WORD hours, minutes = 0;
3256 BYTE sign = *pbEncoded++;
3259 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, hours);
3260 if (ret && hours >= 24)
3262 SetLastError(CRYPT_E_ASN1_CORRUPT);
3267 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, minutes);
3268 if (ret && minutes >= 60)
3270 SetLastError(CRYPT_E_ASN1_CORRUPT);
3278 sysTime->wHour += hours;
3279 sysTime->wMinute += minutes;
3283 if (hours > sysTime->wHour)
3286 sysTime->wHour = 24 - (hours - sysTime->wHour);
3289 sysTime->wHour -= hours;
3290 if (minutes > sysTime->wMinute)
3293 sysTime->wMinute = 60 - (minutes - sysTime->wMinute);
3296 sysTime->wMinute -= minutes;
3303 SetLastError(STATUS_ACCESS_VIOLATION);
3310 #define MIN_ENCODED_TIME_LENGTH 10
3312 static BOOL WINAPI CRYPT_AsnDecodeUtcTime(DWORD dwCertEncodingType,
3313 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3314 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3320 *pcbStructInfo = sizeof(FILETIME);
3326 if (pbEncoded[0] == ASN_UTCTIME)
3330 SetLastError(CRYPT_E_ASN1_EOD);
3333 else if (pbEncoded[1] > 0x7f)
3335 /* long-form date strings really can't be valid */
3336 SetLastError(CRYPT_E_ASN1_CORRUPT);
3341 SYSTEMTIME sysTime = { 0 };
3342 BYTE len = pbEncoded[1];
3344 if (len < MIN_ENCODED_TIME_LENGTH)
3346 SetLastError(CRYPT_E_ASN1_CORRUPT);
3352 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wYear);
3353 if (sysTime.wYear >= 50)
3354 sysTime.wYear += 1900;
3356 sysTime.wYear += 2000;
3357 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
3358 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
3359 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
3360 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMinute);
3363 if (len >= 2 && isdigit(*pbEncoded) &&
3364 isdigit(*(pbEncoded + 1)))
3365 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
3367 else if (isdigit(*pbEncoded))
3368 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 1,
3371 ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
3374 if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags,
3375 pDecodePara, pvStructInfo, pcbStructInfo,
3378 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3379 pvStructInfo = *(BYTE **)pvStructInfo;
3380 ret = SystemTimeToFileTime(&sysTime,
3381 (FILETIME *)pvStructInfo);
3388 SetLastError(CRYPT_E_ASN1_BADTAG);
3394 SetLastError(STATUS_ACCESS_VIOLATION);
3401 static BOOL WINAPI CRYPT_AsnDecodeGeneralizedTime(DWORD dwCertEncodingType,
3402 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3403 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3409 *pcbStructInfo = sizeof(FILETIME);
3415 if (pbEncoded[0] == ASN_GENERALTIME)
3419 SetLastError(CRYPT_E_ASN1_EOD);
3422 else if (pbEncoded[1] > 0x7f)
3424 /* long-form date strings really can't be valid */
3425 SetLastError(CRYPT_E_ASN1_CORRUPT);
3430 BYTE len = pbEncoded[1];
3432 if (len < MIN_ENCODED_TIME_LENGTH)
3434 SetLastError(CRYPT_E_ASN1_CORRUPT);
3439 SYSTEMTIME sysTime = { 0 };
3442 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 4, sysTime.wYear);
3443 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
3444 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
3445 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
3448 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
3451 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
3453 if (ret && len > 0 && (*pbEncoded == '.' ||
3460 /* workaround macro weirdness */
3461 digits = min(len, 3);
3462 CRYPT_TIME_GET_DIGITS(pbEncoded, len, digits,
3463 sysTime.wMilliseconds);
3466 ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
3469 if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags,
3470 pDecodePara, pvStructInfo, pcbStructInfo,
3473 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3474 pvStructInfo = *(BYTE **)pvStructInfo;
3475 ret = SystemTimeToFileTime(&sysTime,
3476 (FILETIME *)pvStructInfo);
3483 SetLastError(CRYPT_E_ASN1_BADTAG);
3489 SetLastError(STATUS_ACCESS_VIOLATION);
3496 static BOOL WINAPI CRYPT_AsnDecodeChoiceOfTime(DWORD dwCertEncodingType,
3497 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3498 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3504 if (pbEncoded[0] == ASN_UTCTIME)
3505 ret = CRYPT_AsnDecodeUtcTime(dwCertEncodingType, lpszStructType,
3506 pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
3508 else if (pbEncoded[0] == ASN_GENERALTIME)
3509 ret = CRYPT_AsnDecodeGeneralizedTime(dwCertEncodingType,
3510 lpszStructType, pbEncoded, cbEncoded, dwFlags, pDecodePara,
3511 pvStructInfo, pcbStructInfo);
3514 SetLastError(CRYPT_E_ASN1_BADTAG);
3520 SetLastError(STATUS_ACCESS_VIOLATION);
3527 static BOOL WINAPI CRYPT_AsnDecodeSequenceOfAny(DWORD dwCertEncodingType,
3528 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3529 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3535 if (pbEncoded[0] == ASN_SEQUENCEOF)
3537 DWORD bytesNeeded, dataLen, remainingLen, cValue;
3539 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3544 lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3545 bytesNeeded = sizeof(CRYPT_SEQUENCE_OF_ANY);
3547 ptr = pbEncoded + 1 + lenBytes;
3548 remainingLen = dataLen;
3549 while (ret && remainingLen)
3553 ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
3556 DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
3558 remainingLen -= 1 + nextLenBytes + nextLen;
3559 ptr += 1 + nextLenBytes + nextLen;
3560 bytesNeeded += sizeof(CRYPT_DER_BLOB);
3561 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
3562 bytesNeeded += 1 + nextLenBytes + nextLen;
3568 CRYPT_SEQUENCE_OF_ANY *seq;
3572 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
3573 pvStructInfo, pcbStructInfo, bytesNeeded)))
3575 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3576 pvStructInfo = *(BYTE **)pvStructInfo;
3577 seq = (CRYPT_SEQUENCE_OF_ANY *)pvStructInfo;
3578 seq->cValue = cValue;
3579 seq->rgValue = (CRYPT_DER_BLOB *)((BYTE *)seq +
3581 nextPtr = (BYTE *)seq->rgValue +
3582 cValue * sizeof(CRYPT_DER_BLOB);
3583 ptr = pbEncoded + 1 + lenBytes;
3584 remainingLen = dataLen;
3586 while (ret && remainingLen)
3590 ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
3593 DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
3595 seq->rgValue[i].cbData = 1 + nextLenBytes +
3597 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
3598 seq->rgValue[i].pbData = (BYTE *)ptr;
3601 seq->rgValue[i].pbData = nextPtr;
3602 memcpy(nextPtr, ptr, 1 + nextLenBytes +
3604 nextPtr += 1 + nextLenBytes + nextLen;
3606 remainingLen -= 1 + nextLenBytes + nextLen;
3607 ptr += 1 + nextLenBytes + nextLen;
3617 SetLastError(CRYPT_E_ASN1_BADTAG);
3623 SetLastError(STATUS_ACCESS_VIOLATION);
3630 static BOOL WINAPI CRYPT_AsnDecodeDistPointName(DWORD dwCertEncodingType,
3631 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3632 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3636 if (pbEncoded[0] == (ASN_CONTEXT | ASN_CONSTRUCTOR | 0))
3638 DWORD bytesNeeded, dataLen;
3640 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3642 struct AsnArrayDescriptor arrayDesc = {
3643 ASN_CONTEXT | ASN_CONSTRUCTOR | 0, CRYPT_AsnDecodeAltNameEntry,
3644 sizeof(CERT_ALT_NAME_ENTRY), TRUE,
3645 offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
3646 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3652 ret = CRYPT_AsnDecodeArray(&arrayDesc,
3653 pbEncoded + 1 + lenBytes, cbEncoded - 1 - lenBytes,
3654 0, NULL, NULL, &nameLen, NULL);
3655 bytesNeeded = sizeof(CRL_DIST_POINT_NAME) + nameLen;
3658 bytesNeeded = sizeof(CRL_DIST_POINT_NAME);
3660 *pcbStructInfo = bytesNeeded;
3661 else if (*pcbStructInfo < bytesNeeded)
3663 *pcbStructInfo = bytesNeeded;
3664 SetLastError(ERROR_MORE_DATA);
3669 CRL_DIST_POINT_NAME *name = (CRL_DIST_POINT_NAME *)pvStructInfo;
3673 name->dwDistPointNameChoice = CRL_DIST_POINT_FULL_NAME;
3674 ret = CRYPT_AsnDecodeArray(&arrayDesc,
3675 pbEncoded + 1 + lenBytes, cbEncoded - 1 - lenBytes,
3676 0, NULL, &name->u.FullName, pcbStructInfo,
3677 name->u.FullName.rgAltEntry);
3680 name->dwDistPointNameChoice = CRL_DIST_POINT_NO_NAME;
3686 SetLastError(CRYPT_E_ASN1_BADTAG);
3692 static BOOL WINAPI CRYPT_AsnDecodeDistPoint(DWORD dwCertEncodingType,
3693 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3694 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3696 struct AsnDecodeSequenceItem items[] = {
3697 { ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_DIST_POINT,
3698 DistPointName), CRYPT_AsnDecodeDistPointName,
3699 sizeof(CRL_DIST_POINT_NAME), TRUE, TRUE, offsetof(CRL_DIST_POINT,
3700 DistPointName.u.FullName.rgAltEntry), 0 },
3701 { ASN_CONTEXT | 1, offsetof(CRL_DIST_POINT, ReasonFlags),
3702 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
3703 offsetof(CRL_DIST_POINT, ReasonFlags.pbData), 0 },
3704 { ASN_CONTEXT | ASN_CONSTRUCTOR | 2, offsetof(CRL_DIST_POINT, CRLIssuer),
3705 CRYPT_AsnDecodeAltNameInternal, sizeof(CERT_ALT_NAME_INFO), TRUE, TRUE,
3706 offsetof(CRL_DIST_POINT, CRLIssuer.rgAltEntry), 0 },
3710 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3711 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
3712 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3716 static BOOL WINAPI CRYPT_AsnDecodeCRLDistPoints(DWORD dwCertEncodingType,
3717 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3718 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3722 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3723 pDecodePara, pvStructInfo, *pcbStructInfo);
3727 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3728 CRYPT_AsnDecodeDistPoint, sizeof(CRL_DIST_POINT), TRUE,
3729 offsetof(CRL_DIST_POINT, DistPointName.u.FullName.rgAltEntry) };
3731 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3732 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3736 SetLastError(STATUS_ACCESS_VIOLATION);
3743 static BOOL WINAPI CRYPT_AsnDecodeEnhancedKeyUsage(DWORD dwCertEncodingType,
3744 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3745 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3749 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3750 pDecodePara, pvStructInfo, *pcbStructInfo);
3754 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3755 CRYPT_AsnDecodeOidInternal, sizeof(LPSTR), TRUE, 0 };
3757 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3758 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3762 SetLastError(STATUS_ACCESS_VIOLATION);
3769 static BOOL WINAPI CRYPT_AsnDecodeIssuingDistPoint(DWORD dwCertEncodingType,
3770 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3771 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3775 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3776 pDecodePara, pvStructInfo, *pcbStructInfo);
3780 struct AsnDecodeSequenceItem items[] = {
3781 { ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_ISSUING_DIST_POINT,
3782 DistPointName), CRYPT_AsnDecodeDistPointName,
3783 sizeof(CRL_DIST_POINT_NAME), TRUE, TRUE,
3784 offsetof(CRL_ISSUING_DIST_POINT,
3785 DistPointName.u.FullName.rgAltEntry), 0 },
3786 { ASN_CONTEXT | 1, offsetof(CRL_ISSUING_DIST_POINT,
3787 fOnlyContainsUserCerts), CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE,
3789 { ASN_CONTEXT | 2, offsetof(CRL_ISSUING_DIST_POINT,
3790 fOnlyContainsCACerts), CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE,
3792 { ASN_CONTEXT | 3, offsetof(CRL_ISSUING_DIST_POINT,
3793 OnlySomeReasonFlags), CRYPT_AsnDecodeBitsInternal,
3794 sizeof(CRYPT_BIT_BLOB), TRUE, TRUE, offsetof(CRL_ISSUING_DIST_POINT,
3795 OnlySomeReasonFlags.pbData), 0 },
3796 { ASN_CONTEXT | 4, offsetof(CRL_ISSUING_DIST_POINT,
3797 fIndirectCRL), CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE, FALSE, 0 },
3800 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3801 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
3802 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3806 SetLastError(STATUS_ACCESS_VIOLATION);
3813 static BOOL WINAPI CRYPT_AsnDecodeIssuerSerialNumber(DWORD dwCertEncodingType,
3814 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3815 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3818 struct AsnDecodeSequenceItem items[] = {
3819 { 0, offsetof(CERT_ISSUER_SERIAL_NUMBER, Issuer), CRYPT_AsnDecodeDerBlob,
3820 sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_ISSUER_SERIAL_NUMBER,
3822 { ASN_INTEGER, offsetof(CERT_ISSUER_SERIAL_NUMBER, SerialNumber),
3823 CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE,
3824 TRUE, offsetof(CERT_ISSUER_SERIAL_NUMBER, SerialNumber.pbData), 0 },
3826 CERT_ISSUER_SERIAL_NUMBER *issuerSerial =
3827 (CERT_ISSUER_SERIAL_NUMBER *)pvStructInfo;
3829 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3830 pDecodePara, pvStructInfo, *pcbStructInfo);
3832 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3833 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
3834 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
3835 issuerSerial ? issuerSerial->Issuer.pbData : NULL);
3836 if (ret && issuerSerial && !issuerSerial->SerialNumber.cbData)
3838 SetLastError(CRYPT_E_ASN1_CORRUPT);
3841 TRACE("returning %d\n", ret);
3845 static BOOL WINAPI CRYPT_AsnDecodePKCSSignerInfoInternal(
3846 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
3847 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
3848 void *pvStructInfo, DWORD *pcbStructInfo)
3850 CMSG_SIGNER_INFO *info = (CMSG_SIGNER_INFO *)pvStructInfo;
3851 struct AsnDecodeSequenceItem items[] = {
3852 { ASN_INTEGER, offsetof(CMSG_SIGNER_INFO, dwVersion),
3853 CRYPT_AsnDecodeInt, sizeof(DWORD), FALSE, FALSE, 0, 0 },
3854 { ASN_SEQUENCEOF, offsetof(CMSG_SIGNER_INFO, Issuer),
3855 CRYPT_AsnDecodeIssuerSerialNumber, sizeof(CERT_ISSUER_SERIAL_NUMBER),
3856 FALSE, TRUE, offsetof(CMSG_SIGNER_INFO, Issuer.pbData), 0 },
3857 { ASN_SEQUENCEOF, offsetof(CMSG_SIGNER_INFO, HashAlgorithm),
3858 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
3859 FALSE, TRUE, offsetof(CMSG_SIGNER_INFO, HashAlgorithm.pszObjId), 0 },
3860 { ASN_CONSTRUCTOR | ASN_CONTEXT | 0,
3861 offsetof(CMSG_SIGNER_INFO, AuthAttrs),
3862 CRYPT_AsnDecodePKCSAttributesInternal, sizeof(CRYPT_ATTRIBUTES),
3863 TRUE, TRUE, offsetof(CMSG_SIGNER_INFO, AuthAttrs.rgAttr), 0 },
3864 { ASN_SEQUENCEOF, offsetof(CMSG_SIGNER_INFO, HashEncryptionAlgorithm),
3865 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
3866 FALSE, TRUE, offsetof(CMSG_SIGNER_INFO,
3867 HashEncryptionAlgorithm.pszObjId), 0 },
3868 { ASN_OCTETSTRING, offsetof(CMSG_SIGNER_INFO, EncryptedHash),
3869 CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_DER_BLOB),
3870 FALSE, TRUE, offsetof(CMSG_SIGNER_INFO, EncryptedHash.pbData), 0 },
3871 { ASN_CONSTRUCTOR | ASN_CONTEXT | 1,
3872 offsetof(CMSG_SIGNER_INFO, UnauthAttrs),
3873 CRYPT_AsnDecodePKCSAttributesInternal, sizeof(CRYPT_ATTRIBUTES),
3874 TRUE, TRUE, offsetof(CMSG_SIGNER_INFO, UnauthAttrs.rgAttr), 0 },
3878 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3879 pDecodePara, pvStructInfo, *pcbStructInfo);
3881 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3882 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
3883 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
3884 info ? info->Issuer.pbData : NULL);
3888 static BOOL WINAPI CRYPT_AsnDecodePKCSSignerInfo(DWORD dwCertEncodingType,
3889 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3890 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3894 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3895 pDecodePara, pvStructInfo, *pcbStructInfo);
3899 ret = CRYPT_AsnDecodePKCSSignerInfoInternal(dwCertEncodingType,
3900 lpszStructType, pbEncoded, cbEncoded,
3901 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, pcbStructInfo);
3902 if (ret && pvStructInfo)
3904 ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
3905 pcbStructInfo, *pcbStructInfo);
3908 CMSG_SIGNER_INFO *info;
3910 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3911 pvStructInfo = *(BYTE **)pvStructInfo;
3912 info = (CMSG_SIGNER_INFO *)pvStructInfo;
3913 info->Issuer.pbData = ((BYTE *)info +
3914 sizeof(CMSG_SIGNER_INFO));
3915 ret = CRYPT_AsnDecodePKCSSignerInfoInternal(dwCertEncodingType,
3916 lpszStructType, pbEncoded, cbEncoded,
3917 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
3924 SetLastError(STATUS_ACCESS_VIOLATION);
3927 TRACE("returning %d\n", ret);
3931 static BOOL WINAPI CRYPT_DecodeSignerArray(DWORD dwCertEncodingType,
3932 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3933 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3936 struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
3937 CRYPT_AsnDecodePKCSSignerInfoInternal, sizeof(CMSG_SIGNER_INFO), TRUE,
3938 offsetof(CMSG_SIGNER_INFO, Issuer.pbData) };
3939 struct GenericArray *array = (struct GenericArray *)pvStructInfo;
3941 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3942 pDecodePara, pvStructInfo, *pcbStructInfo);
3944 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3945 pDecodePara, pvStructInfo, pcbStructInfo, array ? array->rgItems : NULL);
3949 BOOL CRYPT_AsnDecodePKCSSignedInfo(const BYTE *pbEncoded, DWORD cbEncoded,
3950 DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
3951 CRYPT_SIGNED_INFO *signedInfo, DWORD *pcbSignedInfo)
3954 struct AsnDecodeSequenceItem items[] = {
3955 { ASN_INTEGER, offsetof(CRYPT_SIGNED_INFO, version), CRYPT_AsnDecodeInt,
3956 sizeof(DWORD), FALSE, FALSE, 0, 0 },
3957 /* Placeholder for the hash algorithms - redundant with those in the
3958 * signers, so just ignore them.
3960 { ASN_CONSTRUCTOR | ASN_SETOF, 0, NULL, 0, TRUE, FALSE, 0, 0 },
3961 { ASN_SEQUENCE, offsetof(CRYPT_SIGNED_INFO, content),
3962 CRYPT_AsnDecodePKCSContentInfoInternal, sizeof(CRYPT_CONTENT_INFO),
3963 FALSE, TRUE, offsetof(CRYPT_SIGNED_INFO, content.pszObjId), 0 },
3964 { ASN_CONSTRUCTOR | ASN_CONTEXT | 0,
3965 offsetof(CRYPT_SIGNED_INFO, cCertEncoded),
3966 CRYPT_DecodeDERArray, sizeof(struct GenericArray), TRUE, TRUE,
3967 offsetof(CRYPT_SIGNED_INFO, rgCertEncoded), 0 },
3968 { ASN_CONSTRUCTOR | ASN_CONTEXT | 1,
3969 offsetof(CRYPT_SIGNED_INFO, cCrlEncoded), CRYPT_DecodeDERArray,
3970 sizeof(struct GenericArray), TRUE, TRUE,
3971 offsetof(CRYPT_SIGNED_INFO, rgCrlEncoded), 0 },
3972 { ASN_CONSTRUCTOR | ASN_SETOF, offsetof(CRYPT_SIGNED_INFO, cSignerInfo),
3973 CRYPT_DecodeSignerArray, sizeof(struct GenericArray), TRUE, TRUE,
3974 offsetof(CRYPT_SIGNED_INFO, rgSignerInfo), 0 },
3977 TRACE("%p, %d, %08x, %p, %p, %d\n", pbEncoded, cbEncoded, dwFlags,
3978 pDecodePara, signedInfo, *pcbSignedInfo);
3980 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
3981 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3982 pDecodePara, signedInfo, pcbSignedInfo, NULL);
3983 TRACE("returning %d\n", ret);
3987 static CryptDecodeObjectExFunc CRYPT_GetBuiltinDecoder(DWORD dwCertEncodingType,
3988 LPCSTR lpszStructType)
3990 CryptDecodeObjectExFunc decodeFunc = NULL;
3992 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
3993 && (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
3995 SetLastError(ERROR_FILE_NOT_FOUND);
3998 if (!HIWORD(lpszStructType))
4000 switch (LOWORD(lpszStructType))
4002 case (WORD)X509_CERT:
4003 decodeFunc = CRYPT_AsnDecodeCertSignedContent;
4005 case (WORD)X509_CERT_TO_BE_SIGNED:
4006 decodeFunc = CRYPT_AsnDecodeCert;
4008 case (WORD)X509_CERT_CRL_TO_BE_SIGNED:
4009 decodeFunc = CRYPT_AsnDecodeCRL;
4011 case (WORD)X509_EXTENSIONS:
4012 decodeFunc = CRYPT_AsnDecodeExtensions;
4014 case (WORD)X509_NAME_VALUE:
4015 decodeFunc = CRYPT_AsnDecodeNameValue;
4017 case (WORD)X509_NAME:
4018 decodeFunc = CRYPT_AsnDecodeName;
4020 case (WORD)X509_PUBLIC_KEY_INFO:
4021 decodeFunc = CRYPT_AsnDecodePubKeyInfo;
4023 case (WORD)X509_AUTHORITY_KEY_ID:
4024 decodeFunc = CRYPT_AsnDecodeAuthorityKeyId;
4026 case (WORD)X509_ALTERNATE_NAME:
4027 decodeFunc = CRYPT_AsnDecodeAltName;
4029 case (WORD)X509_BASIC_CONSTRAINTS:
4030 decodeFunc = CRYPT_AsnDecodeBasicConstraints;
4032 case (WORD)X509_BASIC_CONSTRAINTS2:
4033 decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
4035 case (WORD)RSA_CSP_PUBLICKEYBLOB:
4036 decodeFunc = CRYPT_AsnDecodeRsaPubKey;
4038 case (WORD)X509_UNICODE_NAME:
4039 decodeFunc = CRYPT_AsnDecodeUnicodeName;
4041 case (WORD)PKCS_ATTRIBUTE:
4042 decodeFunc = CRYPT_AsnDecodePKCSAttribute;
4044 case (WORD)X509_UNICODE_NAME_VALUE:
4045 decodeFunc = CRYPT_AsnDecodeUnicodeNameValue;
4047 case (WORD)X509_OCTET_STRING:
4048 decodeFunc = CRYPT_AsnDecodeOctets;
4050 case (WORD)X509_BITS:
4051 case (WORD)X509_KEY_USAGE:
4052 decodeFunc = CRYPT_AsnDecodeBits;
4054 case (WORD)X509_INTEGER:
4055 decodeFunc = CRYPT_AsnDecodeInt;
4057 case (WORD)X509_MULTI_BYTE_INTEGER:
4058 decodeFunc = CRYPT_AsnDecodeInteger;
4060 case (WORD)X509_MULTI_BYTE_UINT:
4061 decodeFunc = CRYPT_AsnDecodeUnsignedInteger;
4063 case (WORD)X509_ENUMERATED:
4064 decodeFunc = CRYPT_AsnDecodeEnumerated;
4066 case (WORD)X509_CHOICE_OF_TIME:
4067 decodeFunc = CRYPT_AsnDecodeChoiceOfTime;
4069 case (WORD)X509_AUTHORITY_KEY_ID2:
4070 decodeFunc = CRYPT_AsnDecodeAuthorityKeyId2;
4072 case (WORD)PKCS_CONTENT_INFO:
4073 decodeFunc = CRYPT_AsnDecodePKCSContentInfo;
4075 case (WORD)X509_SEQUENCE_OF_ANY:
4076 decodeFunc = CRYPT_AsnDecodeSequenceOfAny;
4078 case (WORD)PKCS_UTC_TIME:
4079 decodeFunc = CRYPT_AsnDecodeUtcTime;
4081 case (WORD)X509_CRL_DIST_POINTS:
4082 decodeFunc = CRYPT_AsnDecodeCRLDistPoints;
4084 case (WORD)X509_ENHANCED_KEY_USAGE:
4085 decodeFunc = CRYPT_AsnDecodeEnhancedKeyUsage;
4087 case (WORD)PKCS_ATTRIBUTES:
4088 decodeFunc = CRYPT_AsnDecodePKCSAttributes;
4090 case (WORD)X509_ISSUING_DIST_POINT:
4091 decodeFunc = CRYPT_AsnDecodeIssuingDistPoint;
4093 case (WORD)PKCS7_SIGNER_INFO:
4094 decodeFunc = CRYPT_AsnDecodePKCSSignerInfo;
4098 else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
4099 decodeFunc = CRYPT_AsnDecodeExtensions;
4100 else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
4101 decodeFunc = CRYPT_AsnDecodeUtcTime;
4102 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER))
4103 decodeFunc = CRYPT_AsnDecodeAuthorityKeyId;
4104 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER2))
4105 decodeFunc = CRYPT_AsnDecodeAuthorityKeyId2;
4106 else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
4107 decodeFunc = CRYPT_AsnDecodeEnumerated;
4108 else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
4109 decodeFunc = CRYPT_AsnDecodeBits;
4110 else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
4111 decodeFunc = CRYPT_AsnDecodeOctets;
4112 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS))
4113 decodeFunc = CRYPT_AsnDecodeBasicConstraints;
4114 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
4115 decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
4116 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
4117 decodeFunc = CRYPT_AsnDecodeAltName;
4118 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
4119 decodeFunc = CRYPT_AsnDecodeAltName;
4120 else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
4121 decodeFunc = CRYPT_AsnDecodeAltName;
4122 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
4123 decodeFunc = CRYPT_AsnDecodeAltName;
4124 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
4125 decodeFunc = CRYPT_AsnDecodeAltName;
4126 else if (!strcmp(lpszStructType, szOID_CRL_DIST_POINTS))
4127 decodeFunc = CRYPT_AsnDecodeCRLDistPoints;
4128 else if (!strcmp(lpszStructType, szOID_ENHANCED_KEY_USAGE))
4129 decodeFunc = CRYPT_AsnDecodeEnhancedKeyUsage;
4130 else if (!strcmp(lpszStructType, szOID_ISSUING_DIST_POINT))
4131 decodeFunc = CRYPT_AsnDecodeIssuingDistPoint;
4135 static CryptDecodeObjectFunc CRYPT_LoadDecoderFunc(DWORD dwCertEncodingType,
4136 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
4138 static HCRYPTOIDFUNCSET set = NULL;
4139 CryptDecodeObjectFunc decodeFunc = NULL;
4142 set = CryptInitOIDFunctionSet(CRYPT_OID_DECODE_OBJECT_FUNC, 0);
4143 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
4144 (void **)&decodeFunc, hFunc);
4148 static CryptDecodeObjectExFunc CRYPT_LoadDecoderExFunc(DWORD dwCertEncodingType,
4149 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
4151 static HCRYPTOIDFUNCSET set = NULL;
4152 CryptDecodeObjectExFunc decodeFunc = NULL;
4155 set = CryptInitOIDFunctionSet(CRYPT_OID_DECODE_OBJECT_EX_FUNC, 0);
4156 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
4157 (void **)&decodeFunc, hFunc);
4161 BOOL WINAPI CryptDecodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
4162 const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo,
4163 DWORD *pcbStructInfo)
4166 CryptDecodeObjectFunc pCryptDecodeObject = NULL;
4167 CryptDecodeObjectExFunc pCryptDecodeObjectEx = NULL;
4168 HCRYPTOIDFUNCADDR hFunc = NULL;
4170 TRACE_(crypt)("(0x%08x, %s, %p, %d, 0x%08x, %p, %p)\n", dwCertEncodingType,
4171 debugstr_a(lpszStructType), pbEncoded, cbEncoded, dwFlags,
4172 pvStructInfo, pcbStructInfo);
4174 if (!pvStructInfo && !pcbStructInfo)
4176 SetLastError(ERROR_INVALID_PARAMETER);
4181 SetLastError(CRYPT_E_ASN1_EOD);
4184 if (cbEncoded > MAX_ENCODED_LEN)
4186 SetLastError(CRYPT_E_ASN1_LARGE);
4190 if (!(pCryptDecodeObjectEx = CRYPT_GetBuiltinDecoder(dwCertEncodingType,
4193 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
4194 debugstr_a(lpszStructType));
4195 pCryptDecodeObject = CRYPT_LoadDecoderFunc(dwCertEncodingType,
4196 lpszStructType, &hFunc);
4197 if (!pCryptDecodeObject)
4198 pCryptDecodeObjectEx = CRYPT_LoadDecoderExFunc(dwCertEncodingType,
4199 lpszStructType, &hFunc);
4201 if (pCryptDecodeObject)
4202 ret = pCryptDecodeObject(dwCertEncodingType, lpszStructType,
4203 pbEncoded, cbEncoded, dwFlags, pvStructInfo, pcbStructInfo);
4204 else if (pCryptDecodeObjectEx)
4205 ret = pCryptDecodeObjectEx(dwCertEncodingType, lpszStructType,
4206 pbEncoded, cbEncoded, dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL,
4207 pvStructInfo, pcbStructInfo);
4209 CryptFreeOIDFunctionAddress(hFunc, 0);
4210 TRACE_(crypt)("returning %d\n", ret);
4214 BOOL WINAPI CryptDecodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
4215 const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4216 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4219 CryptDecodeObjectExFunc decodeFunc;
4220 HCRYPTOIDFUNCADDR hFunc = NULL;
4222 TRACE_(crypt)("(0x%08x, %s, %p, %d, 0x%08x, %p, %p, %p)\n",
4223 dwCertEncodingType, debugstr_a(lpszStructType), pbEncoded,
4224 cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
4226 if (!pvStructInfo && !pcbStructInfo)
4228 SetLastError(ERROR_INVALID_PARAMETER);
4233 SetLastError(CRYPT_E_ASN1_EOD);
4236 if (cbEncoded > MAX_ENCODED_LEN)
4238 SetLastError(CRYPT_E_ASN1_LARGE);
4242 SetLastError(NOERROR);
4243 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG && pvStructInfo)
4244 *(BYTE **)pvStructInfo = NULL;
4245 decodeFunc = CRYPT_GetBuiltinDecoder(dwCertEncodingType, lpszStructType);
4248 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
4249 debugstr_a(lpszStructType));
4250 decodeFunc = CRYPT_LoadDecoderExFunc(dwCertEncodingType, lpszStructType,
4254 ret = decodeFunc(dwCertEncodingType, lpszStructType, pbEncoded,
4255 cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
4258 CryptDecodeObjectFunc pCryptDecodeObject =
4259 CRYPT_LoadDecoderFunc(dwCertEncodingType, lpszStructType, &hFunc);
4261 /* Try CryptDecodeObject function. Don't call CryptDecodeObject
4262 * directly, as that could cause an infinite loop.
4264 if (pCryptDecodeObject)
4266 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4268 ret = pCryptDecodeObject(dwCertEncodingType, lpszStructType,
4269 pbEncoded, cbEncoded, dwFlags, NULL, pcbStructInfo);
4270 if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4271 pvStructInfo, pcbStructInfo, *pcbStructInfo)))
4272 ret = pCryptDecodeObject(dwCertEncodingType,
4273 lpszStructType, pbEncoded, cbEncoded, dwFlags,
4274 *(BYTE **)pvStructInfo, pcbStructInfo);
4277 ret = pCryptDecodeObject(dwCertEncodingType, lpszStructType,
4278 pbEncoded, cbEncoded, dwFlags, pvStructInfo, pcbStructInfo);
4282 CryptFreeOIDFunctionAddress(hFunc, 0);
4283 TRACE_(crypt)("returning %d\n", ret);