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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * This file implements ASN.1 DER encoding and decoding of a limited set of
19 * types. 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
38 #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 /* a few asn.1 tags we need */
54 #define ASN_BOOL (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x01)
55 #define ASN_BITSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x03)
56 #define ASN_ENUMERATED (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0a)
57 #define ASN_SETOF (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x11)
58 #define ASN_NUMERICSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x12)
59 #define ASN_PRINTABLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x13)
60 #define ASN_IA5STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x16)
61 #define ASN_UTCTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x17)
62 #define ASN_GENERALTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x18)
64 #define ASN_FLAGS_MASK 0xe0
65 #define ASN_TYPE_MASK 0x1f
67 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
69 typedef BOOL (WINAPI *CryptEncodeObjectFunc)(DWORD, LPCSTR, const void *,
71 typedef BOOL (WINAPI *CryptEncodeObjectExFunc)(DWORD, LPCSTR, const void *,
72 DWORD, PCRYPT_ENCODE_PARA, BYTE *, DWORD *);
73 typedef BOOL (WINAPI *CryptDecodeObjectFunc)(DWORD, LPCSTR, const BYTE *,
74 DWORD, DWORD, void *, DWORD *);
75 typedef BOOL (WINAPI *CryptDecodeObjectExFunc)(DWORD, LPCSTR, const BYTE *,
76 DWORD, DWORD, PCRYPT_DECODE_PARA, void *, DWORD *);
78 /* Prototypes for built-in encoders/decoders. They follow the Ex style
79 * prototypes. The dwCertEncodingType and lpszStructType are ignored by the
80 * built-in functions, but the parameters are retained to simplify
81 * CryptEncodeObjectEx/CryptDecodeObjectEx, since they must call functions in
82 * external DLLs that follow these signatures.
83 * FIXME: some built-in functions are suitable to be called directly by
84 * CryptEncodeObjectEx/CryptDecodeObjectEx (they implement exception handling
85 * and memory allocation if requested), others are only suitable to be called
86 * internally. Comment which are which.
88 static BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
89 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
90 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
91 static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
92 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
93 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
94 static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
95 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
96 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
97 static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
98 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
99 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
100 static BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
101 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
102 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
103 static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
104 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
105 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
106 static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
107 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
108 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
109 static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
110 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
111 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
112 static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
113 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
114 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
115 static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
116 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
117 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
118 static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
119 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
120 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
122 static BOOL WINAPI CRYPT_AsnDecodeChoiceOfTime(DWORD dwCertEncodingType,
123 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
124 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
125 static BOOL WINAPI CRYPT_AsnDecodePubKeyInfo(DWORD dwCertEncodingType,
126 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
127 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
128 /* Like CRYPT_AsnDecodeExtensions, except assumes rgExtension is set ahead of
129 * time, doesn't do memory allocation, and doesn't do exception handling.
130 * (This isn't intended to be the externally-called one.)
132 static BOOL WINAPI CRYPT_AsnDecodeExtensionsInternal(DWORD dwCertEncodingType,
133 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
134 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
135 static BOOL WINAPI CRYPT_AsnDecodeOid(const BYTE *pbEncoded, DWORD cbEncoded,
136 DWORD dwFlags, LPSTR pszObjId, DWORD *pcbObjId);
137 /* Assumes algo->Parameters.pbData is set ahead of time. Internal func. */
138 static BOOL WINAPI CRYPT_AsnDecodeAlgorithmId(DWORD dwCertEncodingType,
139 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
140 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
141 /* Internal function */
142 static BOOL WINAPI CRYPT_AsnDecodeBool(DWORD dwCertEncodingType,
143 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
144 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
145 /* Assumes the CRYPT_DATA_BLOB's pbData member has been initialized */
146 static BOOL WINAPI CRYPT_AsnDecodeOctetsInternal(DWORD dwCertEncodingType,
147 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
148 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
149 /* Like CRYPT_AsnDecodeBits, but assumes the CRYPT_INTEGER_BLOB's pbData
150 * member has been initialized, doesn't do exception handling, and doesn't do
153 static BOOL WINAPI CRYPT_AsnDecodeBitsInternal(DWORD dwCertEncodingType,
154 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
155 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
156 static BOOL WINAPI CRYPT_AsnDecodeBits(DWORD dwCertEncodingType,
157 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
158 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
159 static BOOL WINAPI CRYPT_AsnDecodeInt(DWORD dwCertEncodingType,
160 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
161 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
162 /* Like CRYPT_AsnDecodeInteger, but assumes the CRYPT_INTEGER_BLOB's pbData
163 * member has been initialized, doesn't do exception handling, and doesn't do
166 static BOOL WINAPI CRYPT_AsnDecodeIntegerInternal(DWORD dwCertEncodingType,
167 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
168 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo);
169 /* Like CRYPT_AsnDecodeInteger, but unsigned. */
170 static BOOL WINAPI CRYPT_AsnDecodeUnsignedIntegerInternal(
171 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
172 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
173 void *pvStructInfo, DWORD *pcbStructInfo);
175 BOOL WINAPI CryptEncodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
176 const void *pvStructInfo, BYTE *pbEncoded, DWORD *pcbEncoded)
178 static HCRYPTOIDFUNCSET set = NULL;
180 HCRYPTOIDFUNCADDR hFunc;
181 CryptEncodeObjectFunc pCryptEncodeObject;
183 TRACE("(0x%08lx, %s, %p, %p, %p)\n", dwCertEncodingType,
184 debugstr_a(lpszStructType), pvStructInfo, pbEncoded,
187 if (!pbEncoded && !pcbEncoded)
189 SetLastError(ERROR_INVALID_PARAMETER);
193 /* Try registered DLL first.. */
195 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_FUNC, 0);
196 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
197 (void **)&pCryptEncodeObject, &hFunc);
198 if (pCryptEncodeObject)
200 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
201 pvStructInfo, pbEncoded, pcbEncoded);
202 CryptFreeOIDFunctionAddress(hFunc, 0);
206 /* If not, use CryptEncodeObjectEx */
207 ret = CryptEncodeObjectEx(dwCertEncodingType, lpszStructType,
208 pvStructInfo, 0, NULL, pbEncoded, pcbEncoded);
213 /* Helper function to check *pcbEncoded, set it to the required size, and
214 * optionally to allocate memory. Assumes pbEncoded is not NULL.
215 * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
216 * pointer to the newly allocated memory.
218 static BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags,
219 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded,
224 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
226 if (pEncodePara && pEncodePara->pfnAlloc)
227 *(BYTE **)pbEncoded = pEncodePara->pfnAlloc(bytesNeeded);
229 *(BYTE **)pbEncoded = LocalAlloc(0, bytesNeeded);
230 if (!*(BYTE **)pbEncoded)
233 *pcbEncoded = bytesNeeded;
235 else if (bytesNeeded > *pcbEncoded)
237 *pcbEncoded = bytesNeeded;
238 SetLastError(ERROR_MORE_DATA);
244 static BOOL CRYPT_EncodeLen(DWORD len, BYTE *pbEncoded, DWORD *pcbEncoded)
246 DWORD bytesNeeded, significantBytes = 0;
254 for (temp = len, significantBytes = sizeof(temp); !(temp & 0xff000000);
255 temp <<= 8, significantBytes--)
257 bytesNeeded = significantBytes + 1;
261 *pcbEncoded = bytesNeeded;
264 if (*pcbEncoded < bytesNeeded)
266 SetLastError(ERROR_MORE_DATA);
270 *pbEncoded = (BYTE)len;
275 *pbEncoded++ = significantBytes | 0x80;
276 for (i = 0; i < significantBytes; i++)
278 *(pbEncoded + significantBytes - i - 1) = (BYTE)(len & 0xff);
282 *pcbEncoded = bytesNeeded;
286 struct AsnEncodeSequenceItem
288 const void *pvStructInfo;
289 CryptEncodeObjectExFunc encodeFunc;
290 DWORD size; /* used during encoding, not for your use */
293 static BOOL WINAPI CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType,
294 struct AsnEncodeSequenceItem items[], DWORD cItem, DWORD dwFlags,
295 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
298 DWORD i, dataLen = 0;
300 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", items, cItem, dwFlags, pEncodePara,
301 pbEncoded, *pcbEncoded);
302 for (i = 0, ret = TRUE; ret && i < cItem; i++)
304 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
305 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
306 NULL, &items[i].size);
307 /* Some functions propagate their errors through the size */
309 *pcbEncoded = items[i].size;
310 dataLen += items[i].size;
314 DWORD lenBytes, bytesNeeded;
316 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
317 bytesNeeded = 1 + lenBytes + dataLen;
319 *pcbEncoded = bytesNeeded;
322 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
323 pcbEncoded, bytesNeeded)))
325 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
326 pbEncoded = *(BYTE **)pbEncoded;
327 *pbEncoded++ = ASN_SEQUENCE;
328 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
329 pbEncoded += lenBytes;
330 for (i = 0; ret && i < cItem; i++)
332 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
333 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG,
334 NULL, pbEncoded, &items[i].size);
335 /* Some functions propagate their errors through the size */
337 *pcbEncoded = items[i].size;
338 pbEncoded += items[i].size;
343 TRACE("returning %d (%08lx)\n", ret, GetLastError());
347 struct AsnConstructedItem
350 const void *pvStructInfo;
351 CryptEncodeObjectExFunc encodeFunc;
354 static BOOL WINAPI CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType,
355 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
356 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
359 const struct AsnConstructedItem *item =
360 (const struct AsnConstructedItem *)pvStructInfo;
363 if ((ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
364 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &len)))
366 DWORD dataLen, bytesNeeded;
368 CRYPT_EncodeLen(len, NULL, &dataLen);
369 bytesNeeded = 1 + dataLen + len;
371 *pcbEncoded = bytesNeeded;
372 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
373 pbEncoded, pcbEncoded, bytesNeeded)))
375 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
376 pbEncoded = *(BYTE **)pbEncoded;
377 *pbEncoded++ = ASN_CONTEXT | ASN_CONSTRUCTOR | item->tag;
378 CRYPT_EncodeLen(len, pbEncoded, &dataLen);
379 pbEncoded += dataLen;
380 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
381 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
385 /* Some functions propagate their errors through the size */
392 /* Some functions propagate their errors through the size */
398 struct AsnEncodeTagSwappedItem
401 const void *pvStructInfo;
402 CryptEncodeObjectExFunc encodeFunc;
405 /* Sort of a wacky hack, it encodes something using the struct
406 * AsnEncodeTagSwappedItem's encodeFunc, then replaces the tag byte with the tag
407 * given in the struct AsnEncodeTagSwappedItem.
409 static BOOL WINAPI CRYPT_AsnEncodeSwapTag(DWORD dwCertEncodingType,
410 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
411 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
414 const struct AsnEncodeTagSwappedItem *item =
415 (const struct AsnEncodeTagSwappedItem *)pvStructInfo;
417 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
418 item->pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
419 if (ret && pbEncoded)
420 *pbEncoded = item->tag;
424 static BOOL WINAPI CRYPT_AsnEncodeCertVersion(DWORD dwCertEncodingType,
425 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
426 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
428 const DWORD *ver = (const DWORD *)pvStructInfo;
431 /* CERT_V1 is not encoded */
439 struct AsnConstructedItem item = { 0, ver, CRYPT_AsnEncodeInt };
441 ret = CRYPT_AsnEncodeConstructed(dwCertEncodingType, X509_INTEGER,
442 &item, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
447 static BOOL WINAPI CRYPT_CopyEncodedBlob(DWORD dwCertEncodingType,
448 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
449 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
451 const CRYPT_DER_BLOB *blob = (const CRYPT_DER_BLOB *)pvStructInfo;
456 *pcbEncoded = blob->cbData;
459 else if (*pcbEncoded < blob->cbData)
461 *pcbEncoded = blob->cbData;
462 SetLastError(ERROR_MORE_DATA);
468 memcpy(pbEncoded, blob->pbData, blob->cbData);
469 *pcbEncoded = blob->cbData;
475 static BOOL WINAPI CRYPT_AsnEncodeValidity(DWORD dwCertEncodingType,
476 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
477 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
480 /* This has two filetimes in a row, a NotBefore and a NotAfter */
481 const FILETIME *timePtr = (const FILETIME *)pvStructInfo;
482 struct AsnEncodeSequenceItem items[] = {
483 { timePtr++, CRYPT_AsnEncodeChoiceOfTime, 0 },
484 { timePtr, CRYPT_AsnEncodeChoiceOfTime, 0 },
487 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
488 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
493 static BOOL WINAPI CRYPT_AsnEncodeAlgorithmId(
494 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
495 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
498 const CRYPT_ALGORITHM_IDENTIFIER *algo =
499 (const CRYPT_ALGORITHM_IDENTIFIER *)pvStructInfo;
501 struct AsnEncodeSequenceItem items[] = {
502 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
503 { &algo->Parameters, CRYPT_CopyEncodedBlob, 0 },
506 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
507 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
512 static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
513 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
514 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
520 const CERT_PUBLIC_KEY_INFO *info =
521 (const CERT_PUBLIC_KEY_INFO *)pvStructInfo;
522 struct AsnEncodeSequenceItem items[] = {
523 { &info->Algorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
524 { &info->PublicKey, CRYPT_AsnEncodeBits, 0 },
527 TRACE("Encoding public key with OID %s\n",
528 debugstr_a(info->Algorithm.pszObjId));
529 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
530 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
535 SetLastError(STATUS_ACCESS_VIOLATION);
542 static BOOL WINAPI CRYPT_AsnEncodeCert(DWORD dwCertEncodingType,
543 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
544 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
550 const CERT_SIGNED_CONTENT_INFO *info =
551 (const CERT_SIGNED_CONTENT_INFO *)pvStructInfo;
552 struct AsnEncodeSequenceItem items[] = {
553 { &info->ToBeSigned, CRYPT_CopyEncodedBlob, 0 },
554 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
555 { &info->Signature, CRYPT_AsnEncodeBitsSwapBytes, 0 },
558 if (dwFlags & CRYPT_ENCODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG)
559 items[2].encodeFunc = CRYPT_AsnEncodeBits;
560 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
561 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
566 SetLastError(STATUS_ACCESS_VIOLATION);
573 /* Like in Windows, this blithely ignores the validity of the passed-in
574 * CERT_INFO, and just encodes it as-is. The resulting encoded data may not
575 * decode properly, see CRYPT_AsnDecodeCertInfo.
577 static BOOL WINAPI CRYPT_AsnEncodeCertInfo(DWORD dwCertEncodingType,
578 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
579 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
585 const CERT_INFO *info = (const CERT_INFO *)pvStructInfo;
586 struct AsnEncodeSequenceItem items[10] = {
587 { &info->dwVersion, CRYPT_AsnEncodeCertVersion, 0 },
588 { &info->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
589 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
590 { &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
591 { &info->NotBefore, CRYPT_AsnEncodeValidity, 0 },
592 { &info->Subject, CRYPT_CopyEncodedBlob, 0 },
593 { &info->SubjectPublicKeyInfo, CRYPT_AsnEncodePubKeyInfo, 0 },
596 struct AsnConstructedItem constructed[3] = { { 0 } };
597 DWORD cItem = 7, cConstructed = 0;
599 if (info->IssuerUniqueId.cbData)
601 constructed[cConstructed].tag = 1;
602 constructed[cConstructed].pvStructInfo = &info->IssuerUniqueId;
603 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeBits;
604 items[cItem].pvStructInfo = &constructed[cConstructed];
605 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
609 if (info->SubjectUniqueId.cbData)
611 constructed[cConstructed].tag = 2;
612 constructed[cConstructed].pvStructInfo = &info->SubjectUniqueId;
613 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeBits;
614 items[cItem].pvStructInfo = &constructed[cConstructed];
615 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
619 if (info->cExtension)
621 constructed[cConstructed].tag = 3;
622 constructed[cConstructed].pvStructInfo = &info->cExtension;
623 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeExtensions;
624 items[cItem].pvStructInfo = &constructed[cConstructed];
625 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
630 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
631 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
635 SetLastError(STATUS_ACCESS_VIOLATION);
642 static BOOL WINAPI CRYPT_AsnEncodeCRLEntry(const CRL_ENTRY *entry,
643 BYTE *pbEncoded, DWORD *pcbEncoded)
645 struct AsnEncodeSequenceItem items[3] = {
646 { &entry->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
647 { &entry->RevocationDate, CRYPT_AsnEncodeChoiceOfTime, 0 },
653 TRACE("%p, %p, %p\n", entry, pbEncoded, pcbEncoded);
655 if (entry->cExtension)
657 items[cItem].pvStructInfo = &entry->cExtension;
658 items[cItem].encodeFunc = CRYPT_AsnEncodeExtensions;
662 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
663 pbEncoded, pcbEncoded);
665 TRACE("returning %d (%08lx)\n", ret, GetLastError());
669 static BOOL WINAPI CRYPT_AsnEncodeCRLEntries(DWORD dwCertEncodingType,
670 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
671 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
673 DWORD cCRLEntry = *(const DWORD *)pvStructInfo;
674 DWORD bytesNeeded, dataLen, lenBytes, i;
675 const CRL_ENTRY *rgCRLEntry = *(const CRL_ENTRY **)
676 ((const BYTE *)pvStructInfo + sizeof(DWORD));
679 for (i = 0, dataLen = 0; ret && i < cCRLEntry; i++)
683 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], NULL, &size);
687 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
688 bytesNeeded = 1 + lenBytes + dataLen;
690 *pcbEncoded = bytesNeeded;
693 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
694 pcbEncoded, bytesNeeded)))
696 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
697 pbEncoded = *(BYTE **)pbEncoded;
698 *pbEncoded++ = ASN_SEQUENCEOF;
699 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
700 pbEncoded += lenBytes;
701 for (i = 0; i < cCRLEntry; i++)
703 DWORD size = dataLen;
705 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], pbEncoded, &size);
714 static BOOL WINAPI CRYPT_AsnEncodeCRLVersion(DWORD dwCertEncodingType,
715 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
716 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
718 const DWORD *ver = (const DWORD *)pvStructInfo;
721 /* CRL_V1 is not encoded */
728 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
729 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
733 /* Like in Windows, this blithely ignores the validity of the passed-in
734 * CRL_INFO, and just encodes it as-is. The resulting encoded data may not
735 * decode properly, see CRYPT_AsnDecodeCRLInfo.
737 static BOOL WINAPI CRYPT_AsnEncodeCRLInfo(DWORD dwCertEncodingType,
738 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
739 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
745 const CRL_INFO *info = (const CRL_INFO *)pvStructInfo;
746 struct AsnEncodeSequenceItem items[7] = {
747 { &info->dwVersion, CRYPT_AsnEncodeCRLVersion, 0 },
748 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
749 { &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
750 { &info->ThisUpdate, CRYPT_AsnEncodeChoiceOfTime, 0 },
755 if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
757 items[cItem].pvStructInfo = &info->NextUpdate;
758 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
763 items[cItem].pvStructInfo = &info->cCRLEntry;
764 items[cItem].encodeFunc = CRYPT_AsnEncodeCRLEntries;
767 if (info->cExtension)
769 items[cItem].pvStructInfo = &info->cExtension;
770 items[cItem].encodeFunc = CRYPT_AsnEncodeExtensions;
774 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
775 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
779 SetLastError(STATUS_ACCESS_VIOLATION);
786 static BOOL CRYPT_AsnEncodeExtension(CERT_EXTENSION *ext, BYTE *pbEncoded,
790 struct AsnEncodeSequenceItem items[3] = {
791 { ext->pszObjId, CRYPT_AsnEncodeOid, 0 },
797 TRACE("%p, %p, %ld\n", ext, pbEncoded, *pcbEncoded);
801 items[cItem].pvStructInfo = &ext->fCritical;
802 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
805 items[cItem].pvStructInfo = &ext->Value;
806 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
809 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
810 pbEncoded, pcbEncoded);
811 TRACE("returning %d (%08lx)\n", ret, GetLastError());
815 static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
816 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
817 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
823 DWORD bytesNeeded, dataLen, lenBytes, i;
824 const CERT_EXTENSIONS *exts = (const CERT_EXTENSIONS *)pvStructInfo;
827 for (i = 0, dataLen = 0; ret && i < exts->cExtension; i++)
831 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i], NULL, &size);
835 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
836 bytesNeeded = 1 + lenBytes + dataLen;
838 *pcbEncoded = bytesNeeded;
841 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
842 pcbEncoded, bytesNeeded)))
844 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
845 pbEncoded = *(BYTE **)pbEncoded;
846 *pbEncoded++ = ASN_SEQUENCEOF;
847 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
848 pbEncoded += lenBytes;
849 for (i = 0; i < exts->cExtension; i++)
851 DWORD size = dataLen;
853 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i],
863 SetLastError(STATUS_ACCESS_VIOLATION);
870 static BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
871 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
872 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
874 LPCSTR pszObjId = (LPCSTR)pvStructInfo;
875 DWORD bytesNeeded = 0, lenBytes;
880 TRACE("%s\n", debugstr_a(pszObjId));
887 if (sscanf(pszObjId, "%d.%d.%n", &val1, &val2, &firstPos) != 2)
889 SetLastError(CRYPT_E_ASN1_ERROR);
893 firstByte = val1 * 40 + val2;
894 ptr = pszObjId + firstPos;
899 /* note I assume each component is at most 32-bits long in base 2 */
900 if (sscanf(ptr, "%d%n", &val1, &pos) == 1)
902 if (val1 >= 0x10000000)
904 else if (val1 >= 0x200000)
906 else if (val1 >= 0x4000)
908 else if (val1 >= 0x80)
918 SetLastError(CRYPT_E_ASN1_ERROR);
922 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
926 bytesNeeded += 1 + lenBytes;
929 if (*pcbEncoded < bytesNeeded)
931 SetLastError(ERROR_MORE_DATA);
936 *pbEncoded++ = ASN_OBJECTIDENTIFIER;
937 CRYPT_EncodeLen(bytesNeeded - 1 - lenBytes, pbEncoded, &lenBytes);
938 pbEncoded += lenBytes;
944 *pbEncoded++ = firstByte;
945 ptr = pszObjId + firstPos;
948 sscanf(ptr, "%d%n", &val, &pos);
950 unsigned char outBytes[5];
953 if (val >= 0x10000000)
955 else if (val >= 0x200000)
957 else if (val >= 0x4000)
959 else if (val >= 0x80)
963 for (i = numBytes; i > 0; i--)
965 outBytes[i - 1] = val & 0x7f;
968 for (i = 0; i < numBytes - 1; i++)
969 *pbEncoded++ = outBytes[i] | 0x80;
970 *pbEncoded++ = outBytes[i];
979 *pcbEncoded = bytesNeeded;
983 static BOOL WINAPI CRYPT_AsnEncodeNameValue(DWORD dwCertEncodingType,
984 CERT_NAME_VALUE *value, BYTE *pbEncoded, DWORD *pcbEncoded)
987 DWORD bytesNeeded, lenBytes, encodedLen;
990 switch (value->dwValueType)
992 case CERT_RDN_NUMERIC_STRING:
993 tag = ASN_NUMERICSTRING;
994 encodedLen = value->Value.cbData;
996 case CERT_RDN_PRINTABLE_STRING:
997 tag = ASN_PRINTABLESTRING;
998 encodedLen = value->Value.cbData;
1000 case CERT_RDN_IA5_STRING:
1001 tag = ASN_IA5STRING;
1002 encodedLen = value->Value.cbData;
1004 case CERT_RDN_ANY_TYPE:
1005 /* explicitly disallowed */
1006 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1009 FIXME("String type %ld unimplemented\n", value->dwValueType);
1012 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1013 bytesNeeded = 1 + lenBytes + encodedLen;
1016 if (*pcbEncoded < bytesNeeded)
1018 SetLastError(ERROR_MORE_DATA);
1024 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
1025 pbEncoded += lenBytes;
1026 switch (value->dwValueType)
1028 case CERT_RDN_NUMERIC_STRING:
1029 case CERT_RDN_PRINTABLE_STRING:
1030 case CERT_RDN_IA5_STRING:
1031 memcpy(pbEncoded, value->Value.pbData, value->Value.cbData);
1035 *pcbEncoded = bytesNeeded;
1039 static BOOL WINAPI CRYPT_AsnEncodeRdnAttr(DWORD dwCertEncodingType,
1040 CERT_RDN_ATTR *attr, BYTE *pbEncoded, DWORD *pcbEncoded)
1042 DWORD bytesNeeded = 0, lenBytes, size;
1045 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL, attr->pszObjId,
1046 0, NULL, NULL, &size);
1049 bytesNeeded += size;
1050 /* hack: a CERT_RDN_ATTR is identical to a CERT_NAME_VALUE beginning
1051 * with dwValueType, so "cast" it to get its encoded size
1053 ret = CRYPT_AsnEncodeNameValue(dwCertEncodingType,
1054 (CERT_NAME_VALUE *)&attr->dwValueType, NULL, &size);
1057 bytesNeeded += size;
1058 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1059 bytesNeeded += 1 + lenBytes;
1062 if (*pcbEncoded < bytesNeeded)
1064 SetLastError(ERROR_MORE_DATA);
1069 *pbEncoded++ = ASN_SEQUENCE;
1070 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1072 pbEncoded += lenBytes;
1073 size = bytesNeeded - 1 - lenBytes;
1074 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
1075 attr->pszObjId, 0, NULL, pbEncoded, &size);
1079 size = bytesNeeded - 1 - lenBytes - size;
1080 ret = CRYPT_AsnEncodeNameValue(dwCertEncodingType,
1081 (CERT_NAME_VALUE *)&attr->dwValueType, pbEncoded,
1086 *pcbEncoded = bytesNeeded;
1092 static int BLOBComp(const void *l, const void *r)
1094 CRYPT_DER_BLOB *a = (CRYPT_DER_BLOB *)l, *b = (CRYPT_DER_BLOB *)r;
1097 if (!(ret = memcmp(a->pbData, b->pbData, min(a->cbData, b->cbData))))
1098 ret = a->cbData - b->cbData;
1102 /* This encodes as a SET OF, which in DER must be lexicographically sorted.
1104 static BOOL WINAPI CRYPT_AsnEncodeRdn(DWORD dwCertEncodingType, CERT_RDN *rdn,
1105 BYTE *pbEncoded, DWORD *pcbEncoded)
1108 CRYPT_DER_BLOB *blobs = NULL;
1112 DWORD bytesNeeded = 0, lenBytes, i;
1118 blobs = CryptMemAlloc(rdn->cRDNAttr * sizeof(CRYPT_DER_BLOB));
1122 memset(blobs, 0, rdn->cRDNAttr * sizeof(CRYPT_DER_BLOB));
1124 for (i = 0; ret && i < rdn->cRDNAttr; i++)
1126 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType, &rdn->rgRDNAttr[i],
1127 NULL, &blobs[i].cbData);
1129 bytesNeeded += blobs[i].cbData;
1133 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1134 bytesNeeded += 1 + lenBytes;
1137 if (*pcbEncoded < bytesNeeded)
1139 SetLastError(ERROR_MORE_DATA);
1144 for (i = 0; ret && i < rdn->cRDNAttr; i++)
1146 blobs[i].pbData = CryptMemAlloc(blobs[i].cbData);
1147 if (!blobs[i].pbData)
1150 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType,
1151 &rdn->rgRDNAttr[i], blobs[i].pbData,
1156 qsort(blobs, rdn->cRDNAttr, sizeof(CRYPT_DER_BLOB),
1158 *pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
1159 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1161 pbEncoded += lenBytes;
1162 for (i = 0; ret && i < rdn->cRDNAttr; i++)
1164 memcpy(pbEncoded, blobs[i].pbData, blobs[i].cbData);
1165 pbEncoded += blobs[i].cbData;
1170 *pcbEncoded = bytesNeeded;
1174 for (i = 0; i < rdn->cRDNAttr; i++)
1175 CryptMemFree(blobs[i].pbData);
1180 SetLastError(STATUS_ACCESS_VIOLATION);
1184 CryptMemFree(blobs);
1188 static BOOL WINAPI CRYPT_AsnEncodeName(DWORD dwCertEncodingType,
1189 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1190 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1196 const CERT_NAME_INFO *info = (const CERT_NAME_INFO *)pvStructInfo;
1197 DWORD bytesNeeded = 0, lenBytes, size, i;
1199 TRACE("encoding name with %ld RDNs\n", info->cRDN);
1201 for (i = 0; ret && i < info->cRDN; i++)
1203 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i], NULL,
1206 bytesNeeded += size;
1208 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1209 bytesNeeded += 1 + lenBytes;
1213 *pcbEncoded = bytesNeeded;
1216 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1217 pbEncoded, pcbEncoded, bytesNeeded)))
1219 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1220 pbEncoded = *(BYTE **)pbEncoded;
1221 *pbEncoded++ = ASN_SEQUENCEOF;
1222 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1224 pbEncoded += lenBytes;
1225 for (i = 0; ret && i < info->cRDN; i++)
1228 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
1229 &info->rgRDN[i], pbEncoded, &size);
1233 bytesNeeded -= size;
1242 SetLastError(STATUS_ACCESS_VIOLATION);
1249 static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
1250 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1251 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1253 BOOL val = *(const BOOL *)pvStructInfo, ret;
1262 else if (*pcbEncoded < 3)
1265 SetLastError(ERROR_MORE_DATA);
1271 *pbEncoded++ = ASN_BOOL;
1273 *pbEncoded++ = val ? 0xff : 0;
1276 TRACE("returning %d (%08lx)\n", ret, GetLastError());
1280 static BOOL CRYPT_AsnEncodeAltNameEntry(const CERT_ALT_NAME_ENTRY *entry,
1281 BYTE *pbEncoded, DWORD *pcbEncoded)
1287 switch (entry->dwAltNameChoice)
1289 case CERT_ALT_NAME_RFC822_NAME:
1290 case CERT_ALT_NAME_DNS_NAME:
1291 case CERT_ALT_NAME_URL:
1292 if (entry->u.pwszURL)
1296 /* Not + 1: don't encode the NULL-terminator */
1297 dataLen = lstrlenW(entry->u.pwszURL);
1298 for (i = 0; ret && i < dataLen; i++)
1300 if (entry->u.pwszURL[i] > 0x7f)
1302 SetLastError(CRYPT_E_INVALID_IA5_STRING);
1311 case CERT_ALT_NAME_IP_ADDRESS:
1312 dataLen = entry->u.IPAddress.cbData;
1314 case CERT_ALT_NAME_REGISTERED_ID:
1315 /* FIXME: encode OID */
1316 case CERT_ALT_NAME_OTHER_NAME:
1317 case CERT_ALT_NAME_DIRECTORY_NAME:
1318 FIXME("name type %ld unimplemented\n", entry->dwAltNameChoice);
1321 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1326 DWORD bytesNeeded, lenBytes;
1328 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1329 bytesNeeded = 1 + dataLen + lenBytes;
1331 *pcbEncoded = bytesNeeded;
1332 else if (*pcbEncoded < bytesNeeded)
1334 SetLastError(ERROR_MORE_DATA);
1335 *pcbEncoded = bytesNeeded;
1340 *pbEncoded++ = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
1341 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
1342 pbEncoded += lenBytes;
1343 switch (entry->dwAltNameChoice)
1345 case CERT_ALT_NAME_RFC822_NAME:
1346 case CERT_ALT_NAME_DNS_NAME:
1347 case CERT_ALT_NAME_URL:
1351 for (i = 0; i < dataLen; i++)
1352 *pbEncoded++ = (BYTE)entry->u.pwszURL[i];
1355 case CERT_ALT_NAME_IP_ADDRESS:
1356 memcpy(pbEncoded, entry->u.IPAddress.pbData, dataLen);
1360 *pcbEncoded = bytesNeeded;
1363 TRACE("returning %d (%08lx)\n", ret, GetLastError());
1367 static BOOL WINAPI CRYPT_AsnEncodeAltName(DWORD dwCertEncodingType,
1368 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1369 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1375 const CERT_ALT_NAME_INFO *info =
1376 (const CERT_ALT_NAME_INFO *)pvStructInfo;
1377 DWORD bytesNeeded, dataLen, lenBytes, i;
1380 /* FIXME: should check that cAltEntry is not bigger than 0xff, since we
1381 * can't encode an erroneous entry index if it's bigger than this.
1383 for (i = 0, dataLen = 0; ret && i < info->cAltEntry; i++)
1387 ret = CRYPT_AsnEncodeAltNameEntry(&info->rgAltEntry[i], NULL,
1391 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
1393 /* CRYPT_AsnEncodeAltNameEntry encoded the index of
1394 * the bad character, now set the index of the bad
1397 *pcbEncoded = (BYTE)i <<
1398 CERT_ALT_NAME_ENTRY_ERR_INDEX_SHIFT | len;
1403 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1404 bytesNeeded = 1 + lenBytes + dataLen;
1407 *pcbEncoded = bytesNeeded;
1412 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1413 pbEncoded, pcbEncoded, bytesNeeded)))
1415 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1416 pbEncoded = *(BYTE **)pbEncoded;
1417 *pbEncoded++ = ASN_SEQUENCEOF;
1418 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
1419 pbEncoded += lenBytes;
1420 for (i = 0; ret && i < info->cAltEntry; i++)
1422 DWORD len = dataLen;
1424 ret = CRYPT_AsnEncodeAltNameEntry(&info->rgAltEntry[i],
1438 SetLastError(STATUS_ACCESS_VIOLATION);
1445 static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints2(DWORD dwCertEncodingType,
1446 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1447 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1453 const CERT_BASIC_CONSTRAINTS2_INFO *info =
1454 (const CERT_BASIC_CONSTRAINTS2_INFO *)pvStructInfo;
1455 struct AsnEncodeSequenceItem items[2] = { { 0 } };
1460 items[cItem].pvStructInfo = &info->fCA;
1461 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
1464 if (info->fPathLenConstraint)
1466 items[cItem].pvStructInfo = &info->dwPathLenConstraint;
1467 items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
1470 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
1471 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1475 SetLastError(STATUS_ACCESS_VIOLATION);
1482 static BOOL WINAPI CRYPT_AsnEncodeRsaPubKey(DWORD dwCertEncodingType,
1483 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1484 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1490 const BLOBHEADER *hdr =
1491 (const BLOBHEADER *)pvStructInfo;
1493 if (hdr->bType != PUBLICKEYBLOB)
1495 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1500 const RSAPUBKEY *rsaPubKey = (const RSAPUBKEY *)
1501 ((const BYTE *)pvStructInfo + sizeof(BLOBHEADER));
1502 CRYPT_INTEGER_BLOB blob = { rsaPubKey->bitlen / 8,
1503 (BYTE *)pvStructInfo + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) };
1504 struct AsnEncodeSequenceItem items[] = {
1505 { &blob, CRYPT_AsnEncodeUnsignedInteger, 0 },
1506 { &rsaPubKey->pubexp, CRYPT_AsnEncodeInt, 0 },
1509 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1510 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
1516 SetLastError(STATUS_ACCESS_VIOLATION);
1523 static BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
1524 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1525 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1531 const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvStructInfo;
1532 DWORD bytesNeeded, lenBytes;
1534 TRACE("(%ld, %p), %08lx, %p, %p, %ld\n", blob->cbData, blob->pbData,
1535 dwFlags, pEncodePara, pbEncoded, *pcbEncoded);
1537 CRYPT_EncodeLen(blob->cbData, NULL, &lenBytes);
1538 bytesNeeded = 1 + lenBytes + blob->cbData;
1541 *pcbEncoded = bytesNeeded;
1546 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
1547 pcbEncoded, bytesNeeded)))
1549 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1550 pbEncoded = *(BYTE **)pbEncoded;
1551 *pbEncoded++ = ASN_OCTETSTRING;
1552 CRYPT_EncodeLen(blob->cbData, pbEncoded, &lenBytes);
1553 pbEncoded += lenBytes;
1555 memcpy(pbEncoded, blob->pbData, blob->cbData);
1561 SetLastError(STATUS_ACCESS_VIOLATION);
1565 TRACE("returning %d (%08lx)\n", ret, GetLastError());
1569 static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
1570 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1571 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1577 const CRYPT_BIT_BLOB *blob = (const CRYPT_BIT_BLOB *)pvStructInfo;
1578 DWORD bytesNeeded, lenBytes, dataBytes;
1581 /* yep, MS allows cUnusedBits to be >= 8 */
1582 if (!blob->cUnusedBits)
1584 dataBytes = blob->cbData;
1587 else if (blob->cbData * 8 > blob->cUnusedBits)
1589 dataBytes = (blob->cbData * 8 - blob->cUnusedBits) / 8 + 1;
1590 unusedBits = blob->cUnusedBits >= 8 ? blob->cUnusedBits / 8 :
1598 CRYPT_EncodeLen(dataBytes + 1, NULL, &lenBytes);
1599 bytesNeeded = 1 + lenBytes + dataBytes + 1;
1602 *pcbEncoded = bytesNeeded;
1607 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
1608 pcbEncoded, bytesNeeded)))
1610 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1611 pbEncoded = *(BYTE **)pbEncoded;
1612 *pbEncoded++ = ASN_BITSTRING;
1613 CRYPT_EncodeLen(dataBytes + 1, pbEncoded, &lenBytes);
1614 pbEncoded += lenBytes;
1615 *pbEncoded++ = unusedBits;
1618 BYTE mask = 0xff << unusedBits;
1622 memcpy(pbEncoded, blob->pbData, dataBytes - 1);
1623 pbEncoded += dataBytes - 1;
1625 *pbEncoded = *(blob->pbData + dataBytes - 1) & mask;
1632 SetLastError(STATUS_ACCESS_VIOLATION);
1639 static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
1640 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1641 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1647 const CRYPT_BIT_BLOB *blob = (const CRYPT_BIT_BLOB *)pvStructInfo;
1648 CRYPT_BIT_BLOB newBlob = { blob->cbData, NULL, blob->cUnusedBits };
1653 newBlob.pbData = CryptMemAlloc(newBlob.cbData);
1658 for (i = 0; i < newBlob.cbData; i++)
1659 newBlob.pbData[newBlob.cbData - i - 1] = blob->pbData[i];
1665 ret = CRYPT_AsnEncodeBits(dwCertEncodingType, lpszStructType,
1666 &newBlob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1667 CryptMemFree(newBlob.pbData);
1671 SetLastError(STATUS_ACCESS_VIOLATION);
1678 static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
1679 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1680 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1682 CRYPT_INTEGER_BLOB blob = { sizeof(INT), (BYTE *)pvStructInfo };
1684 return CRYPT_AsnEncodeInteger(dwCertEncodingType, X509_MULTI_BYTE_INTEGER,
1685 &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1688 static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
1689 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1690 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1696 DWORD significantBytes, lenBytes;
1697 BYTE padByte = 0, bytesNeeded;
1699 const CRYPT_INTEGER_BLOB *blob =
1700 (const CRYPT_INTEGER_BLOB *)pvStructInfo;
1702 significantBytes = blob->cbData;
1703 if (significantBytes)
1705 if (blob->pbData[significantBytes - 1] & 0x80)
1707 /* negative, lop off leading (little-endian) 0xffs */
1708 for (; significantBytes > 0 &&
1709 blob->pbData[significantBytes - 1] == 0xff; significantBytes--)
1711 if (blob->pbData[significantBytes - 1] < 0x80)
1719 /* positive, lop off leading (little-endian) zeroes */
1720 for (; significantBytes > 0 &&
1721 !blob->pbData[significantBytes - 1]; significantBytes--)
1723 if (significantBytes == 0)
1724 significantBytes = 1;
1725 if (blob->pbData[significantBytes - 1] > 0x7f)
1733 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
1735 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
1736 bytesNeeded = 1 + lenBytes + significantBytes;
1741 *pcbEncoded = bytesNeeded;
1746 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
1747 pcbEncoded, bytesNeeded)))
1749 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1750 pbEncoded = *(BYTE **)pbEncoded;
1751 *pbEncoded++ = ASN_INTEGER;
1754 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
1755 pbEncoded += lenBytes;
1756 *pbEncoded++ = padByte;
1760 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
1761 pbEncoded += lenBytes;
1763 for (; significantBytes > 0; significantBytes--)
1764 *(pbEncoded++) = blob->pbData[significantBytes - 1];
1770 SetLastError(STATUS_ACCESS_VIOLATION);
1777 static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
1778 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1779 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1785 DWORD significantBytes, lenBytes;
1788 const CRYPT_INTEGER_BLOB *blob =
1789 (const CRYPT_INTEGER_BLOB *)pvStructInfo;
1791 significantBytes = blob->cbData;
1792 if (significantBytes)
1794 /* positive, lop off leading (little-endian) zeroes */
1795 for (; significantBytes > 0 && !blob->pbData[significantBytes - 1];
1798 if (significantBytes == 0)
1799 significantBytes = 1;
1800 if (blob->pbData[significantBytes - 1] > 0x7f)
1804 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
1806 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
1807 bytesNeeded = 1 + lenBytes + significantBytes;
1812 *pcbEncoded = bytesNeeded;
1817 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
1818 pcbEncoded, bytesNeeded)))
1820 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1821 pbEncoded = *(BYTE **)pbEncoded;
1822 *pbEncoded++ = ASN_INTEGER;
1825 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
1826 pbEncoded += lenBytes;
1831 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
1832 pbEncoded += lenBytes;
1834 for (; significantBytes > 0; significantBytes--)
1835 *(pbEncoded++) = blob->pbData[significantBytes - 1];
1841 SetLastError(STATUS_ACCESS_VIOLATION);
1848 static BOOL WINAPI CRYPT_AsnEncodeEnumerated(DWORD dwCertEncodingType,
1849 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1850 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1852 CRYPT_INTEGER_BLOB blob;
1855 /* Encode as an unsigned integer, then change the tag to enumerated */
1856 blob.cbData = sizeof(DWORD);
1857 blob.pbData = (BYTE *)pvStructInfo;
1858 ret = CRYPT_AsnEncodeUnsignedInteger(dwCertEncodingType,
1859 X509_MULTI_BYTE_UINT, &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1860 if (ret && pbEncoded)
1862 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1863 pbEncoded = *(BYTE **)pbEncoded;
1864 pbEncoded[0] = ASN_ENUMERATED;
1869 static BOOL WINAPI CRYPT_AsnEncodeUtcTime(DWORD dwCertEncodingType,
1870 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1871 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1878 /* sorry, magic number: enough for tag, len, YYMMDDHHMMSSZ\0. I use a
1879 * temporary buffer because the output buffer is not NULL-terminated.
1882 static const DWORD bytesNeeded = sizeof(buf) - 1;
1886 *pcbEncoded = bytesNeeded;
1891 /* Sanity check the year, this is a two-digit year format */
1892 ret = FileTimeToSystemTime((const FILETIME *)pvStructInfo,
1894 if (ret && (sysTime.wYear < 1950 || sysTime.wYear > 2050))
1896 SetLastError(CRYPT_E_BAD_ENCODE);
1901 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1902 pbEncoded, pcbEncoded, bytesNeeded)))
1904 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1905 pbEncoded = *(BYTE **)pbEncoded;
1906 buf[0] = ASN_UTCTIME;
1907 buf[1] = bytesNeeded - 2;
1908 snprintf(buf + 2, sizeof(buf) - 2,
1909 "%02d%02d%02d%02d%02d%02dZ", sysTime.wYear >= 2000 ?
1910 sysTime.wYear - 2000 : sysTime.wYear - 1900,
1911 sysTime.wDay, sysTime.wMonth, sysTime.wHour,
1912 sysTime.wMinute, sysTime.wSecond);
1913 memcpy(pbEncoded, buf, bytesNeeded);
1920 SetLastError(STATUS_ACCESS_VIOLATION);
1927 static BOOL WINAPI CRYPT_AsnEncodeGeneralizedTime(DWORD dwCertEncodingType,
1928 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1929 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1936 /* sorry, magic number: enough for tag, len, YYYYMMDDHHMMSSZ\0. I use a
1937 * temporary buffer because the output buffer is not NULL-terminated.
1940 static const DWORD bytesNeeded = sizeof(buf) - 1;
1944 *pcbEncoded = bytesNeeded;
1949 ret = FileTimeToSystemTime((const FILETIME *)pvStructInfo,
1952 ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
1953 pcbEncoded, bytesNeeded);
1956 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1957 pbEncoded = *(BYTE **)pbEncoded;
1958 buf[0] = ASN_GENERALTIME;
1959 buf[1] = bytesNeeded - 2;
1960 snprintf(buf + 2, sizeof(buf) - 2, "%04d%02d%02d%02d%02d%02dZ",
1961 sysTime.wYear, sysTime.wDay, sysTime.wMonth, sysTime.wHour,
1962 sysTime.wMinute, sysTime.wSecond);
1963 memcpy(pbEncoded, buf, bytesNeeded);
1969 SetLastError(STATUS_ACCESS_VIOLATION);
1976 static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
1977 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1978 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1986 /* Check the year, if it's in the UTCTime range call that encode func */
1987 if (!FileTimeToSystemTime((const FILETIME *)pvStructInfo, &sysTime))
1989 if (sysTime.wYear >= 1950 && sysTime.wYear <= 2050)
1990 ret = CRYPT_AsnEncodeUtcTime(dwCertEncodingType, lpszStructType,
1991 pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1993 ret = CRYPT_AsnEncodeGeneralizedTime(dwCertEncodingType,
1994 lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
1999 SetLastError(STATUS_ACCESS_VIOLATION);
2006 static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
2007 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2008 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2014 DWORD bytesNeeded, dataLen, lenBytes, i;
2015 const CRYPT_SEQUENCE_OF_ANY *seq =
2016 (const CRYPT_SEQUENCE_OF_ANY *)pvStructInfo;
2018 for (i = 0, dataLen = 0; i < seq->cValue; i++)
2019 dataLen += seq->rgValue[i].cbData;
2020 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2021 bytesNeeded = 1 + lenBytes + dataLen;
2024 *pcbEncoded = bytesNeeded;
2029 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
2030 pcbEncoded, bytesNeeded)))
2032 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2033 pbEncoded = *(BYTE **)pbEncoded;
2034 *pbEncoded++ = ASN_SEQUENCEOF;
2035 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2036 pbEncoded += lenBytes;
2037 for (i = 0; i < seq->cValue; i++)
2039 memcpy(pbEncoded, seq->rgValue[i].pbData,
2040 seq->rgValue[i].cbData);
2041 pbEncoded += seq->rgValue[i].cbData;
2048 SetLastError(STATUS_ACCESS_VIOLATION);
2055 static BOOL CRYPT_AsnEncodeDistPoint(const CRL_DIST_POINT *distPoint,
2056 BYTE *pbEncoded, DWORD *pcbEncoded)
2059 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2060 struct AsnConstructedItem constructed = { 0 };
2061 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2062 DWORD cItem = 0, cSwapped = 0;
2064 switch (distPoint->DistPointName.dwDistPointNameChoice)
2066 case CRL_DIST_POINT_NO_NAME:
2069 case CRL_DIST_POINT_FULL_NAME:
2070 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
2071 swapped[cSwapped].pvStructInfo = &distPoint->DistPointName.u.FullName;
2072 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
2073 constructed.tag = 0;
2074 constructed.pvStructInfo = &swapped[cSwapped];
2075 constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
2076 items[cItem].pvStructInfo = &constructed;
2077 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
2081 case CRL_DIST_POINT_ISSUER_RDN_NAME:
2082 FIXME("unimplemented for CRL_DIST_POINT_ISSUER_RDN_NAME\n");
2088 if (ret && distPoint->ReasonFlags.cbData)
2090 swapped[cSwapped].tag = ASN_CONTEXT | 1;
2091 swapped[cSwapped].pvStructInfo = &distPoint->ReasonFlags;
2092 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
2093 items[cItem].pvStructInfo = &swapped[cSwapped];
2094 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2098 if (ret && distPoint->CRLIssuer.cAltEntry)
2100 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 2;
2101 swapped[cSwapped].pvStructInfo = &distPoint->CRLIssuer;
2102 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
2103 items[cItem].pvStructInfo = &swapped[cSwapped];
2104 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2109 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
2110 pbEncoded, pcbEncoded);
2114 static BOOL WINAPI CRYPT_AsnEncodeCRLDistPoints(DWORD dwCertEncodingType,
2115 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2116 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2122 const CRL_DIST_POINTS_INFO *info =
2123 (const CRL_DIST_POINTS_INFO *)pvStructInfo;
2125 if (!info->cDistPoint)
2127 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2132 DWORD bytesNeeded, dataLen, lenBytes, i;
2135 for (i = 0, dataLen = 0; ret && i < info->cDistPoint; i++)
2139 ret = CRYPT_AsnEncodeDistPoint(&info->rgDistPoint[i], NULL,
2143 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
2145 /* Have to propagate index of failing character */
2151 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2152 bytesNeeded = 1 + lenBytes + dataLen;
2155 *pcbEncoded = bytesNeeded;
2160 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2161 pbEncoded, pcbEncoded, bytesNeeded)))
2163 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2164 pbEncoded = *(BYTE **)pbEncoded;
2165 *pbEncoded++ = ASN_SEQUENCEOF;
2166 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2167 pbEncoded += lenBytes;
2168 for (i = 0; ret && i < info->cDistPoint; i++)
2170 DWORD len = dataLen;
2172 ret = CRYPT_AsnEncodeDistPoint(
2173 &info->rgDistPoint[i], pbEncoded, &len);
2187 SetLastError(STATUS_ACCESS_VIOLATION);
2194 BOOL WINAPI CryptEncodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
2195 const void *pvStructInfo, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
2196 void *pvEncoded, DWORD *pcbEncoded)
2198 static HCRYPTOIDFUNCSET set = NULL;
2200 CryptEncodeObjectExFunc encodeFunc = NULL;
2201 HCRYPTOIDFUNCADDR hFunc = NULL;
2203 TRACE("(0x%08lx, %s, %p, 0x%08lx, %p, %p, %p)\n", dwCertEncodingType,
2204 debugstr_a(lpszStructType), pvStructInfo, dwFlags, pEncodePara,
2205 pvEncoded, pcbEncoded);
2207 if (!pvEncoded && !pcbEncoded)
2209 SetLastError(ERROR_INVALID_PARAMETER);
2212 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
2213 && (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
2215 SetLastError(ERROR_FILE_NOT_FOUND);
2219 SetLastError(NOERROR);
2220 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG && pvEncoded)
2221 *(BYTE **)pvEncoded = NULL;
2222 if (!HIWORD(lpszStructType))
2224 switch (LOWORD(lpszStructType))
2226 case (WORD)X509_CERT:
2227 encodeFunc = CRYPT_AsnEncodeCert;
2229 case (WORD)X509_CERT_TO_BE_SIGNED:
2230 encodeFunc = CRYPT_AsnEncodeCertInfo;
2232 case (WORD)X509_CERT_CRL_TO_BE_SIGNED:
2233 encodeFunc = CRYPT_AsnEncodeCRLInfo;
2235 case (WORD)X509_EXTENSIONS:
2236 encodeFunc = CRYPT_AsnEncodeExtensions;
2238 case (WORD)X509_NAME:
2239 encodeFunc = CRYPT_AsnEncodeName;
2241 case (WORD)X509_PUBLIC_KEY_INFO:
2242 encodeFunc = CRYPT_AsnEncodePubKeyInfo;
2244 case (WORD)X509_ALTERNATE_NAME:
2245 encodeFunc = CRYPT_AsnEncodeAltName;
2247 case (WORD)X509_BASIC_CONSTRAINTS2:
2248 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
2250 case (WORD)RSA_CSP_PUBLICKEYBLOB:
2251 encodeFunc = CRYPT_AsnEncodeRsaPubKey;
2253 case (WORD)X509_OCTET_STRING:
2254 encodeFunc = CRYPT_AsnEncodeOctets;
2256 case (WORD)X509_BITS:
2257 case (WORD)X509_KEY_USAGE:
2258 encodeFunc = CRYPT_AsnEncodeBits;
2260 case (WORD)X509_INTEGER:
2261 encodeFunc = CRYPT_AsnEncodeInt;
2263 case (WORD)X509_MULTI_BYTE_INTEGER:
2264 encodeFunc = CRYPT_AsnEncodeInteger;
2266 case (WORD)X509_MULTI_BYTE_UINT:
2267 encodeFunc = CRYPT_AsnEncodeUnsignedInteger;
2269 case (WORD)X509_ENUMERATED:
2270 encodeFunc = CRYPT_AsnEncodeEnumerated;
2272 case (WORD)X509_CHOICE_OF_TIME:
2273 encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
2275 case (WORD)X509_SEQUENCE_OF_ANY:
2276 encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
2278 case (WORD)PKCS_UTC_TIME:
2279 encodeFunc = CRYPT_AsnEncodeUtcTime;
2281 case (WORD)X509_CRL_DIST_POINTS:
2282 encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
2285 FIXME("%d: unimplemented\n", LOWORD(lpszStructType));
2288 else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
2289 encodeFunc = CRYPT_AsnEncodeExtensions;
2290 else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
2291 encodeFunc = CRYPT_AsnEncodeUtcTime;
2292 else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
2293 encodeFunc = CRYPT_AsnEncodeEnumerated;
2294 else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
2295 encodeFunc = CRYPT_AsnEncodeBits;
2296 else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
2297 encodeFunc = CRYPT_AsnEncodeOctets;
2298 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
2299 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
2300 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
2301 encodeFunc = CRYPT_AsnEncodeAltName;
2302 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
2303 encodeFunc = CRYPT_AsnEncodeAltName;
2304 else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
2305 encodeFunc = CRYPT_AsnEncodeAltName;
2306 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
2307 encodeFunc = CRYPT_AsnEncodeAltName;
2308 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
2309 encodeFunc = CRYPT_AsnEncodeAltName;
2311 TRACE("OID %s not found or unimplemented, looking for DLL\n",
2312 debugstr_a(lpszStructType));
2316 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_EX_FUNC, 0);
2317 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
2318 (void **)&encodeFunc, &hFunc);
2321 ret = encodeFunc(dwCertEncodingType, lpszStructType, pvStructInfo,
2322 dwFlags, pEncodePara, pvEncoded, pcbEncoded);
2324 SetLastError(ERROR_FILE_NOT_FOUND);
2326 CryptFreeOIDFunctionAddress(hFunc, 0);
2330 BOOL WINAPI CryptDecodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
2331 const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo,
2332 DWORD *pcbStructInfo)
2334 static HCRYPTOIDFUNCSET set = NULL;
2336 CryptDecodeObjectFunc pCryptDecodeObject;
2337 HCRYPTOIDFUNCADDR hFunc;
2339 TRACE("(0x%08lx, %s, %p, %ld, 0x%08lx, %p, %p)\n", dwCertEncodingType,
2340 debugstr_a(lpszStructType), pbEncoded, cbEncoded, dwFlags,
2341 pvStructInfo, pcbStructInfo);
2343 if (!pvStructInfo && !pcbStructInfo)
2345 SetLastError(ERROR_INVALID_PARAMETER);
2349 /* Try registered DLL first.. */
2351 set = CryptInitOIDFunctionSet(CRYPT_OID_DECODE_OBJECT_FUNC, 0);
2352 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
2353 (void **)&pCryptDecodeObject, &hFunc);
2354 if (pCryptDecodeObject)
2356 ret = pCryptDecodeObject(dwCertEncodingType, lpszStructType,
2357 pbEncoded, cbEncoded, dwFlags, pvStructInfo, pcbStructInfo);
2358 CryptFreeOIDFunctionAddress(hFunc, 0);
2362 /* If not, use CryptDecodeObjectEx */
2363 ret = CryptDecodeObjectEx(dwCertEncodingType, lpszStructType, pbEncoded,
2364 cbEncoded, dwFlags, NULL, pvStructInfo, pcbStructInfo);
2369 /* Gets the number of length bytes from the given (leading) length byte */
2370 #define GET_LEN_BYTES(b) ((b) <= 0x7f ? 1 : 1 + ((b) & 0x7f))
2372 /* Helper function to get the encoded length of the data starting at pbEncoded,
2373 * where pbEncoded[0] is the tag. If the data are too short to contain a
2374 * length or if the length is too large for cbEncoded, sets an appropriate
2375 * error code and returns FALSE.
2377 static BOOL WINAPI CRYPT_GetLen(const BYTE *pbEncoded, DWORD cbEncoded,
2384 SetLastError(CRYPT_E_ASN1_CORRUPT);
2387 else if (pbEncoded[1] <= 0x7f)
2389 if (pbEncoded[1] + 1 > cbEncoded)
2391 SetLastError(CRYPT_E_ASN1_EOD);
2396 *len = pbEncoded[1];
2402 BYTE lenLen = GET_LEN_BYTES(pbEncoded[1]);
2404 if (lenLen > sizeof(DWORD) + 1)
2406 SetLastError(CRYPT_E_ASN1_LARGE);
2409 else if (lenLen + 2 > cbEncoded)
2411 SetLastError(CRYPT_E_ASN1_CORRUPT);
2422 out |= *pbEncoded++;
2424 if (out + lenLen + 1 > cbEncoded)
2426 SetLastError(CRYPT_E_ASN1_EOD);
2439 /* Helper function to check *pcbStructInfo, set it to the required size, and
2440 * optionally to allocate memory. Assumes pvStructInfo is not NULL.
2441 * If CRYPT_DECODE_ALLOC_FLAG is set in dwFlags, *pvStructInfo will be set to a
2442 * pointer to the newly allocated memory.
2444 static BOOL CRYPT_DecodeEnsureSpace(DWORD dwFlags,
2445 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo,
2450 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2452 if (pDecodePara && pDecodePara->pfnAlloc)
2453 *(BYTE **)pvStructInfo = pDecodePara->pfnAlloc(bytesNeeded);
2455 *(BYTE **)pvStructInfo = LocalAlloc(0, bytesNeeded);
2456 if (!*(BYTE **)pvStructInfo)
2459 *pcbStructInfo = bytesNeeded;
2461 else if (*pcbStructInfo < bytesNeeded)
2463 *pcbStructInfo = bytesNeeded;
2464 SetLastError(ERROR_MORE_DATA);
2471 * The expected tag of the item. If tag is 0, decodeFunc is called
2472 * regardless of the tag value seen.
2474 * A sequence is decoded into a struct. The offset member is the
2475 * offset of this item within that struct.
2477 * The decoder function to use. If this is NULL, then the member isn't
2478 * decoded, but minSize space is reserved for it.
2480 * The minimum amount of space occupied after decoding. You must set this.
2482 * If true, and the tag doesn't match the expected tag for this item,
2483 * or the decodeFunc fails with CRYPT_E_ASN1_BADTAG, then minSize space is
2484 * filled with 0 for this member.
2485 * hasPointer, pointerOffset, minSize:
2486 * If the item has dynamic data, set hasPointer to TRUE, pointerOffset to
2487 * the offset within the (outer) struct of the data pointer (or to the
2488 * first data pointer, if more than one exist).
2490 * Used by CRYPT_AsnDecodeSequence, not for your use.
2492 struct AsnDecodeSequenceItem
2496 CryptDecodeObjectExFunc decodeFunc;
2500 DWORD pointerOffset;
2504 static BOOL CRYPT_AsnDecodeSequenceItems(DWORD dwCertEncodingType,
2505 struct AsnDecodeSequenceItem items[], DWORD cItem, const BYTE *pbEncoded,
2506 DWORD cbEncoded, DWORD dwFlags, void *pvStructInfo, BYTE *nextData)
2512 ptr = pbEncoded + 1 + GET_LEN_BYTES(pbEncoded[1]);
2513 for (i = 0, ret = TRUE; ret && i < cItem; i++)
2515 if (cbEncoded - (ptr - pbEncoded) != 0)
2519 if ((ret = CRYPT_GetLen(ptr, cbEncoded - (ptr - pbEncoded),
2522 BYTE nextItemLenBytes = GET_LEN_BYTES(ptr[1]);
2524 if (ptr[0] == items[i].tag || !items[i].tag)
2526 if (nextData && pvStructInfo && items[i].hasPointer)
2528 TRACE("Setting next pointer to %p\n",
2530 *(BYTE **)((BYTE *)pvStructInfo +
2531 items[i].pointerOffset) = nextData;
2533 if (items[i].decodeFunc)
2536 TRACE("decoding item %ld\n", i);
2538 TRACE("sizing item %ld\n", i);
2539 ret = items[i].decodeFunc(dwCertEncodingType,
2540 NULL, ptr, 1 + nextItemLenBytes + nextItemLen,
2541 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL,
2542 pvStructInfo ? (BYTE *)pvStructInfo + items[i].offset
2543 : NULL, &items[i].size);
2546 if (nextData && items[i].hasPointer &&
2547 items[i].size > items[i].minSize)
2549 nextData += items[i].size - items[i].minSize;
2550 /* align nextData to DWORD boundaries */
2551 if (items[i].size % sizeof(DWORD))
2552 nextData += sizeof(DWORD) - items[i].size %
2555 /* Account for alignment padding */
2556 if (items[i].size % sizeof(DWORD))
2557 items[i].size += sizeof(DWORD) -
2558 items[i].size % sizeof(DWORD);
2559 ptr += 1 + nextItemLenBytes + nextItemLen;
2561 else if (items[i].optional &&
2562 GetLastError() == CRYPT_E_ASN1_BADTAG)
2564 TRACE("skipping optional item %ld\n", i);
2565 items[i].size = items[i].minSize;
2566 SetLastError(NOERROR);
2570 TRACE("item %ld failed: %08lx\n", i,
2574 items[i].size = items[i].minSize;
2576 else if (items[i].optional)
2578 TRACE("skipping optional item %ld\n", i);
2579 items[i].size = items[i].minSize;
2583 TRACE("tag %02x doesn't match expected %02x\n",
2584 ptr[0], items[i].tag);
2585 SetLastError(CRYPT_E_ASN1_BADTAG);
2590 else if (items[i].optional)
2592 TRACE("missing optional item %ld, skipping\n", i);
2593 items[i].size = items[i].minSize;
2597 TRACE("not enough bytes for item %ld, failing\n", i);
2598 SetLastError(CRYPT_E_ASN1_CORRUPT);
2602 if (cbEncoded - (ptr - pbEncoded) != 0)
2604 TRACE("%ld remaining bytes, failing\n", cbEncoded -
2606 SetLastError(CRYPT_E_ASN1_CORRUPT);
2612 /* This decodes an arbitrary sequence into a contiguous block of memory
2613 * (basically, a struct.) Each element being decoded is described by a struct
2614 * AsnDecodeSequenceItem, see above.
2615 * startingPointer is an optional pointer to the first place where dynamic
2616 * data will be stored. If you know the starting offset, you may pass it
2617 * here. Otherwise, pass NULL, and one will be inferred from the items.
2618 * Each item decoder is never called with CRYPT_DECODE_ALLOC_FLAG set.
2619 * If any undecoded data are left over, fails with CRYPT_E_ASN1_CORRUPT.
2621 static BOOL CRYPT_AsnDecodeSequence(DWORD dwCertEncodingType,
2622 struct AsnDecodeSequenceItem items[], DWORD cItem, const BYTE *pbEncoded,
2623 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
2624 void *pvStructInfo, DWORD *pcbStructInfo, void *startingPointer)
2628 TRACE("%p, %ld, %p, %ld, %08lx, %p, %p, %ld, %p\n", items, cItem, pbEncoded,
2629 cbEncoded, dwFlags, pDecodePara, pvStructInfo, *pcbStructInfo,
2632 if (pbEncoded[0] == ASN_SEQUENCE)
2636 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2640 ret = CRYPT_AsnDecodeSequenceItems(dwFlags, items, cItem, pbEncoded,
2641 cbEncoded, dwFlags, NULL, NULL);
2644 DWORD bytesNeeded = 0, structSize = 0;
2646 for (i = 0; i < cItem; i++)
2648 bytesNeeded += items[i].size;
2649 structSize += items[i].minSize;
2652 *pcbStructInfo = bytesNeeded;
2653 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags,
2654 pDecodePara, pvStructInfo, pcbStructInfo, bytesNeeded)))
2658 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2659 pvStructInfo = *(BYTE **)pvStructInfo;
2660 if (startingPointer)
2661 nextData = (BYTE *)startingPointer;
2663 nextData = (BYTE *)pvStructInfo + structSize;
2664 memset(pvStructInfo, 0, structSize);
2665 ret = CRYPT_AsnDecodeSequenceItems(dwFlags, items, cItem,
2666 pbEncoded, cbEncoded, dwFlags, pvStructInfo, nextData);
2673 SetLastError(CRYPT_E_ASN1_BADTAG);
2676 TRACE("returning %d (%08lx)\n", ret, GetLastError());
2681 * The expected tag of the entire encoded array (usually a variant
2682 * of ASN_SETOF or ASN_SEQUENCEOF.)
2684 * used to decode each item in the array
2686 * is the minimum size of each decoded item
2688 * indicates whether each item has a dynamic pointer
2690 * indicates the offset within itemSize at which the pointer exists
2692 struct AsnArrayDescriptor
2695 CryptDecodeObjectExFunc decodeFunc;
2698 DWORD pointerOffset;
2701 struct AsnArrayItemSize
2713 /* Decodes an array of like types into a struct GenericArray.
2714 * The layout and decoding of the array are described by a struct
2715 * AsnArrayDescriptor.
2717 static BOOL CRYPT_AsnDecodeArray(const struct AsnArrayDescriptor *arrayDesc,
2718 const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2719 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo,
2720 void *startingPointer)
2724 TRACE("%p, %p, %ld, %08lx, %p, %p, %ld, %p\n", arrayDesc, pbEncoded,
2725 cbEncoded, dwFlags, pDecodePara, pvStructInfo, *pcbStructInfo,
2728 if (pbEncoded[0] == arrayDesc->tag)
2732 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2734 DWORD bytesNeeded, cItems = 0;
2735 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
2736 /* There can be arbitrarily many items, but there is often only one.
2738 struct AsnArrayItemSize itemSize = { 0 }, *itemSizes = &itemSize;
2740 bytesNeeded = sizeof(struct GenericArray);
2745 for (ptr = pbEncoded + 1 + lenBytes; ret &&
2746 ptr - pbEncoded - 1 - lenBytes < dataLen; )
2748 DWORD itemLenBytes, itemDataLen, size;
2750 itemLenBytes = GET_LEN_BYTES(ptr[1]);
2751 /* Each item decoded may not tolerate extraneous bytes, so
2752 * get the length of the next element and pass it directly.
2754 ret = CRYPT_GetLen(ptr, cbEncoded - (ptr - pbEncoded),
2757 ret = arrayDesc->decodeFunc(X509_ASN_ENCODING, 0, ptr,
2758 1 + itemLenBytes + itemDataLen,
2759 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL,
2766 if (itemSizes != &itemSize)
2767 itemSizes = CryptMemRealloc(itemSizes,
2768 cItems * sizeof(struct AsnArrayItemSize));
2773 cItems * sizeof(struct AsnArrayItemSize));
2774 memcpy(itemSizes, &itemSize, sizeof(itemSize));
2778 itemSizes[cItems - 1].encodedLen = 1 + itemLenBytes
2780 itemSizes[cItems - 1].size = size;
2781 bytesNeeded += size;
2782 ret = CRYPT_GetLen(ptr,
2783 cbEncoded - (ptr - pbEncoded), &nextLen);
2785 ptr += nextLen + 1 + GET_LEN_BYTES(ptr[1]);
2795 *pcbStructInfo = bytesNeeded;
2796 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags,
2797 pDecodePara, pvStructInfo, pcbStructInfo, bytesNeeded)))
2802 struct GenericArray *array;
2804 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2805 pvStructInfo = *(BYTE **)pvStructInfo;
2806 array = (struct GenericArray *)pvStructInfo;
2807 array->cItems = cItems;
2808 if (startingPointer)
2809 array->rgItems = startingPointer;
2811 array->rgItems = (BYTE *)array +
2812 sizeof(struct GenericArray);
2813 nextData = (BYTE *)array->rgItems +
2814 array->cItems * arrayDesc->itemSize;
2815 for (i = 0, ptr = pbEncoded + 1 + lenBytes; ret &&
2816 i < cItems && ptr - pbEncoded - 1 - lenBytes <
2819 if (arrayDesc->hasPointer)
2820 *(BYTE **)(array->rgItems + i * arrayDesc->itemSize
2821 + arrayDesc->pointerOffset) = nextData;
2822 ret = arrayDesc->decodeFunc(X509_ASN_ENCODING, 0, ptr,
2823 itemSizes[i].encodedLen,
2824 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL,
2825 array->rgItems + i * arrayDesc->itemSize,
2826 &itemSizes[i].size);
2831 nextData += itemSizes[i].size - arrayDesc->itemSize;
2832 ret = CRYPT_GetLen(ptr,
2833 cbEncoded - (ptr - pbEncoded), &nextLen);
2835 ptr += nextLen + 1 + GET_LEN_BYTES(ptr[1]);
2840 if (itemSizes != &itemSize)
2841 CryptMemFree(itemSizes);
2846 SetLastError(CRYPT_E_ASN1_BADTAG);
2852 /* Decodes a DER-encoded BLOB into a CRYPT_DER_BLOB struct pointed to by
2853 * pvStructInfo. The BLOB must be non-empty, otherwise the last error is set
2854 * to CRYPT_E_ASN1_CORRUPT.
2855 * Warning: assumes the CRYPT_DER_BLOB pointed to by pvStructInfo has pbData
2858 static BOOL WINAPI CRYPT_AsnDecodeDerBlob(DWORD dwCertEncodingType,
2859 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2860 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2865 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2867 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
2868 DWORD bytesNeeded = sizeof(CRYPT_DER_BLOB);
2870 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
2871 bytesNeeded += 1 + lenBytes + dataLen;
2874 *pcbStructInfo = bytesNeeded;
2875 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
2876 pvStructInfo, pcbStructInfo, bytesNeeded)))
2878 CRYPT_DER_BLOB *blob;
2880 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
2881 pvStructInfo = *(BYTE **)pvStructInfo;
2882 blob = (CRYPT_DER_BLOB *)pvStructInfo;
2883 blob->cbData = 1 + lenBytes + dataLen;
2886 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
2887 blob->pbData = (BYTE *)pbEncoded;
2890 assert(blob->pbData);
2891 memcpy(blob->pbData, pbEncoded, blob->cbData);
2896 SetLastError(CRYPT_E_ASN1_CORRUPT);
2904 /* Like CRYPT_AsnDecodeBitsInternal, but swaps the bytes */
2905 static BOOL WINAPI CRYPT_AsnDecodeBitsSwapBytes(DWORD dwCertEncodingType,
2906 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2907 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2911 TRACE("(%p, %ld, 0x%08lx, %p, %p, %ld)\n", pbEncoded, cbEncoded, dwFlags,
2912 pDecodePara, pvStructInfo, *pcbStructInfo);
2914 /* Can't use the CRYPT_DECODE_NOCOPY_FLAG, because we modify the bytes in-
2917 ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType, lpszStructType,
2918 pbEncoded, cbEncoded, dwFlags & ~CRYPT_DECODE_NOCOPY_FLAG, pDecodePara,
2919 pvStructInfo, pcbStructInfo);
2920 if (ret && pvStructInfo)
2922 CRYPT_BIT_BLOB *blob = (CRYPT_BIT_BLOB *)pvStructInfo;
2929 for (i = 0; i < blob->cbData / 2; i++)
2931 temp = blob->pbData[i];
2932 blob->pbData[i] = blob->pbData[blob->cbData - i - 1];
2933 blob->pbData[blob->cbData - i - 1] = temp;
2937 TRACE("returning %d (%08lx)\n", ret, GetLastError());
2941 static BOOL WINAPI CRYPT_AsnDecodeCert(DWORD dwCertEncodingType,
2942 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2943 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2947 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
2948 pDecodePara, pvStructInfo, *pcbStructInfo);
2952 struct AsnDecodeSequenceItem items[] = {
2953 { 0, offsetof(CERT_SIGNED_CONTENT_INFO, ToBeSigned),
2954 CRYPT_AsnDecodeDerBlob, sizeof(CRYPT_DER_BLOB), FALSE, TRUE,
2955 offsetof(CERT_SIGNED_CONTENT_INFO, ToBeSigned.pbData), 0 },
2956 { ASN_SEQUENCEOF, offsetof(CERT_SIGNED_CONTENT_INFO,
2957 SignatureAlgorithm), CRYPT_AsnDecodeAlgorithmId,
2958 sizeof(CRYPT_ALGORITHM_IDENTIFIER), FALSE, TRUE,
2959 offsetof(CERT_SIGNED_CONTENT_INFO, SignatureAlgorithm.pszObjId), 0 },
2960 { ASN_BITSTRING, offsetof(CERT_SIGNED_CONTENT_INFO, Signature),
2961 CRYPT_AsnDecodeBitsSwapBytes, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
2962 offsetof(CERT_SIGNED_CONTENT_INFO, Signature.pbData), 0 },
2965 if (dwFlags & CRYPT_DECODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG)
2966 items[2].decodeFunc = CRYPT_AsnDecodeBitsInternal;
2967 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
2968 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
2969 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
2973 SetLastError(STATUS_ACCESS_VIOLATION);
2980 /* Internal function */
2981 static BOOL WINAPI CRYPT_AsnDecodeCertVersion(DWORD dwCertEncodingType,
2982 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
2983 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
2988 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
2990 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
2992 ret = CRYPT_AsnDecodeInt(dwCertEncodingType, X509_INTEGER,
2993 pbEncoded + 1 + lenBytes, dataLen, dwFlags, pDecodePara,
2994 pvStructInfo, pcbStructInfo);
2999 static BOOL WINAPI CRYPT_AsnDecodeValidity(DWORD dwCertEncodingType,
3000 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3001 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3005 struct AsnDecodeSequenceItem items[] = {
3006 { 0, offsetof(CERT_PRIVATE_KEY_VALIDITY, NotBefore),
3007 CRYPT_AsnDecodeChoiceOfTime, sizeof(FILETIME), FALSE, FALSE, 0 },
3008 { 0, offsetof(CERT_PRIVATE_KEY_VALIDITY, NotAfter),
3009 CRYPT_AsnDecodeChoiceOfTime, sizeof(FILETIME), FALSE, FALSE, 0 },
3012 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3013 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3014 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3018 /* Internal function */
3019 static BOOL WINAPI CRYPT_AsnDecodeCertExtensions(DWORD dwCertEncodingType,
3020 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3021 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3026 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3028 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3030 ret = CRYPT_AsnDecodeExtensionsInternal(dwCertEncodingType,
3031 X509_EXTENSIONS, pbEncoded + 1 + lenBytes, dataLen, dwFlags,
3032 pDecodePara, pvStructInfo, pcbStructInfo);
3037 static BOOL WINAPI CRYPT_AsnDecodeCertInfo(DWORD dwCertEncodingType,
3038 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3039 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3043 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3044 pDecodePara, pvStructInfo, *pcbStructInfo);
3048 struct AsnDecodeSequenceItem items[] = {
3049 { ASN_CONTEXT | ASN_CONSTRUCTOR, offsetof(CERT_INFO, dwVersion),
3050 CRYPT_AsnDecodeCertVersion, sizeof(DWORD), TRUE, FALSE, 0, 0 },
3051 { ASN_INTEGER, offsetof(CERT_INFO, SerialNumber),
3052 CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE,
3053 TRUE, offsetof(CERT_INFO, SerialNumber.pbData), 0 },
3054 { ASN_SEQUENCEOF, offsetof(CERT_INFO, SignatureAlgorithm),
3055 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
3056 FALSE, TRUE, offsetof(CERT_INFO, SignatureAlgorithm.pszObjId), 0 },
3057 { 0, offsetof(CERT_INFO, Issuer), CRYPT_AsnDecodeDerBlob,
3058 sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_INFO,
3060 { ASN_SEQUENCEOF, offsetof(CERT_INFO, NotBefore),
3061 CRYPT_AsnDecodeValidity, sizeof(CERT_PRIVATE_KEY_VALIDITY), FALSE,
3063 { 0, offsetof(CERT_INFO, Subject), CRYPT_AsnDecodeDerBlob,
3064 sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CERT_INFO,
3066 /* jil FIXME: shouldn't this have an internal version, which expects
3067 * the pbData to be set?
3069 { ASN_SEQUENCEOF, offsetof(CERT_INFO, SubjectPublicKeyInfo),
3070 CRYPT_AsnDecodePubKeyInfo, sizeof(CERT_PUBLIC_KEY_INFO), FALSE,
3071 TRUE, offsetof(CERT_INFO,
3072 SubjectPublicKeyInfo.Algorithm.Parameters.pbData), 0 },
3073 { ASN_BITSTRING, offsetof(CERT_INFO, IssuerUniqueId),
3074 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
3075 offsetof(CERT_INFO, IssuerUniqueId.pbData), 0 },
3076 { ASN_BITSTRING, offsetof(CERT_INFO, SubjectUniqueId),
3077 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
3078 offsetof(CERT_INFO, SubjectUniqueId.pbData), 0 },
3079 { ASN_CONTEXT | ASN_CONSTRUCTOR | 3, offsetof(CERT_INFO, cExtension),
3080 CRYPT_AsnDecodeCertExtensions, sizeof(CERT_EXTENSIONS), TRUE, TRUE,
3081 offsetof(CERT_INFO, rgExtension), 0 },
3084 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3085 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3086 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3090 SetLastError(STATUS_ACCESS_VIOLATION);
3097 static BOOL WINAPI CRYPT_AsnDecodeCRLEntry(DWORD dwCertEncodingType,
3098 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3099 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3102 struct AsnDecodeSequenceItem items[] = {
3103 { ASN_INTEGER, offsetof(CRL_ENTRY, SerialNumber),
3104 CRYPT_AsnDecodeIntegerInternal, sizeof(CRYPT_INTEGER_BLOB), FALSE, TRUE,
3105 offsetof(CRL_ENTRY, SerialNumber.pbData), 0 },
3106 { 0, offsetof(CRL_ENTRY, RevocationDate), CRYPT_AsnDecodeChoiceOfTime,
3107 sizeof(FILETIME), FALSE, FALSE, 0 },
3108 { ASN_SEQUENCEOF, offsetof(CRL_ENTRY, cExtension),
3109 CRYPT_AsnDecodeExtensionsInternal, sizeof(CERT_EXTENSIONS), TRUE, TRUE,
3110 offsetof(CRL_ENTRY, rgExtension), 0 },
3112 PCRL_ENTRY entry = (PCRL_ENTRY)pvStructInfo;
3114 TRACE("%p, %ld, %08lx, %p, %ld\n", pbEncoded, cbEncoded, dwFlags, entry,
3117 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
3118 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3119 NULL, entry, pcbStructInfo, entry ? entry->SerialNumber.pbData : NULL);
3123 /* Warning: assumes pvStructInfo is a struct GenericArray whose rgItems has
3124 * been set prior to calling.
3126 static BOOL WINAPI CRYPT_AsnDecodeCRLEntries(DWORD dwCertEncodingType,
3127 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3128 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3131 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3132 CRYPT_AsnDecodeCRLEntry, sizeof(CRL_ENTRY), TRUE,
3133 offsetof(CRL_ENTRY, SerialNumber.pbData) };
3134 struct GenericArray *entries = (struct GenericArray *)pvStructInfo;
3136 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3137 pDecodePara, pvStructInfo, *pcbStructInfo);
3139 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3140 pDecodePara, pvStructInfo, pcbStructInfo,
3141 entries ? entries->rgItems : NULL);
3142 TRACE("Returning %d (%08lx)\n", ret, GetLastError());
3146 static BOOL WINAPI CRYPT_AsnDecodeCRLInfo(DWORD dwCertEncodingType,
3147 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3148 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3152 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3153 pDecodePara, pvStructInfo, *pcbStructInfo);
3157 struct AsnDecodeSequenceItem items[] = {
3158 { ASN_CONTEXT | ASN_CONSTRUCTOR, offsetof(CRL_INFO, dwVersion),
3159 CRYPT_AsnDecodeCertVersion, sizeof(DWORD), TRUE, FALSE, 0, 0 },
3160 { ASN_SEQUENCEOF, offsetof(CRL_INFO, SignatureAlgorithm),
3161 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
3162 FALSE, TRUE, offsetof(CRL_INFO, SignatureAlgorithm.pszObjId), 0 },
3163 { 0, offsetof(CRL_INFO, Issuer), CRYPT_AsnDecodeDerBlob,
3164 sizeof(CRYPT_DER_BLOB), FALSE, TRUE, offsetof(CRL_INFO,
3166 { 0, offsetof(CRL_INFO, ThisUpdate), CRYPT_AsnDecodeChoiceOfTime,
3167 sizeof(FILETIME), FALSE, FALSE, 0 },
3168 { 0, offsetof(CRL_INFO, NextUpdate), CRYPT_AsnDecodeChoiceOfTime,
3169 sizeof(FILETIME), TRUE, FALSE, 0 },
3170 { ASN_SEQUENCEOF, offsetof(CRL_INFO, cCRLEntry),
3171 CRYPT_AsnDecodeCRLEntries, sizeof(struct GenericArray), TRUE, TRUE,
3172 offsetof(CRL_INFO, rgCRLEntry), 0 },
3173 /* Note that the extensions are ignored by MS, so I'll ignore them too
3175 { 0, offsetof(CRL_INFO, cExtension), NULL,
3176 sizeof(CERT_EXTENSIONS), TRUE, FALSE, 0 },
3179 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3180 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3181 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3185 SetLastError(STATUS_ACCESS_VIOLATION);
3190 TRACE("Returning %d (%08lx)\n", ret, GetLastError());
3194 /* Differences between this and CRYPT_AsnDecodeOid:
3195 * - pvStructInfo is a LPSTR *, not an LPSTR
3196 * - CRYPT_AsnDecodeOid doesn't account for the size of an LPSTR in its byte
3197 * count, whereas our callers (typically CRYPT_AsnDecodeSequence) expect this
3200 static BOOL WINAPI CRYPT_AsnDecodeOidWrapper(DWORD dwCertEncodingType,
3201 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3202 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3206 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3207 pDecodePara, pvStructInfo, *pcbStructInfo);
3209 ret = CRYPT_AsnDecodeOid(pbEncoded, cbEncoded, dwFlags,
3210 pvStructInfo ? *(LPSTR *)pvStructInfo : NULL, pcbStructInfo);
3211 if (ret || GetLastError() == ERROR_MORE_DATA)
3212 *pcbStructInfo += sizeof(LPSTR);
3213 if (ret && pvStructInfo)
3214 TRACE("returning %s\n", debugstr_a(*(LPSTR *)pvStructInfo));
3218 /* Warning: assumes pvStructInfo is a CERT_EXTENSION whose pszObjId is set
3221 static BOOL WINAPI CRYPT_AsnDecodeExtension(DWORD dwCertEncodingType,
3222 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3223 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3225 struct AsnDecodeSequenceItem items[] = {
3226 { ASN_OBJECTIDENTIFIER, offsetof(CERT_EXTENSION, pszObjId),
3227 CRYPT_AsnDecodeOidWrapper, sizeof(LPSTR), FALSE, TRUE,
3228 offsetof(CERT_EXTENSION, pszObjId), 0 },
3229 { ASN_BOOL, offsetof(CERT_EXTENSION, fCritical), CRYPT_AsnDecodeBool,
3230 sizeof(BOOL), TRUE, FALSE, 0, 0 },
3231 { ASN_OCTETSTRING, offsetof(CERT_EXTENSION, Value),
3232 CRYPT_AsnDecodeOctetsInternal, sizeof(CRYPT_OBJID_BLOB), FALSE, TRUE,
3233 offsetof(CERT_EXTENSION, Value.pbData) },
3236 PCERT_EXTENSION ext = (PCERT_EXTENSION)pvStructInfo;
3238 TRACE("%p, %ld, %08lx, %p, %ld\n", pbEncoded, cbEncoded, dwFlags, ext,
3242 TRACE("ext->pszObjId is %p\n", ext->pszObjId);
3243 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
3244 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags, NULL,
3245 ext, pcbStructInfo, ext ? ext->pszObjId : NULL);
3247 TRACE("ext->pszObjId is %p (%s)\n", ext->pszObjId,
3248 debugstr_a(ext->pszObjId));
3249 TRACE("returning %d (%08lx)\n", ret, GetLastError());
3253 static BOOL WINAPI CRYPT_AsnDecodeExtensionsInternal(DWORD dwCertEncodingType,
3254 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3255 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3258 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3259 CRYPT_AsnDecodeExtension, sizeof(CERT_EXTENSION), TRUE,
3260 offsetof(CERT_EXTENSION, pszObjId) };
3261 PCERT_EXTENSIONS exts = (PCERT_EXTENSIONS)pvStructInfo;
3263 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3264 pDecodePara, pvStructInfo, *pcbStructInfo);
3266 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3267 pDecodePara, pvStructInfo, pcbStructInfo, exts ? exts->rgExtension : NULL);
3271 static BOOL WINAPI CRYPT_AsnDecodeExtensions(DWORD dwCertEncodingType,
3272 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3273 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3279 ret = CRYPT_AsnDecodeExtensionsInternal(dwCertEncodingType,
3280 lpszStructType, pbEncoded, cbEncoded,
3281 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, pcbStructInfo);
3282 if (ret && pvStructInfo)
3284 ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara, pvStructInfo,
3285 pcbStructInfo, *pcbStructInfo);
3288 CERT_EXTENSIONS *exts;
3290 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
3291 pvStructInfo = *(BYTE **)pvStructInfo;
3292 exts = (CERT_EXTENSIONS *)pvStructInfo;
3293 exts->rgExtension = (CERT_EXTENSION *)((BYTE *)exts +
3294 sizeof(CERT_EXTENSIONS));
3295 ret = CRYPT_AsnDecodeExtensionsInternal(dwCertEncodingType,
3296 lpszStructType, pbEncoded, cbEncoded,
3297 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
3304 SetLastError(STATUS_ACCESS_VIOLATION);
3311 /* FIXME: honor the CRYPT_DECODE_SHARE_OID_STRING_FLAG. */
3312 static BOOL WINAPI CRYPT_AsnDecodeOid(const BYTE *pbEncoded, DWORD cbEncoded,
3313 DWORD dwFlags, LPSTR pszObjId, DWORD *pcbObjId)
3317 TRACE("%p, %ld, %08lx, %p, %ld\n", pbEncoded, cbEncoded, dwFlags, pszObjId,
3322 if (pbEncoded[0] == ASN_OBJECTIDENTIFIER)
3326 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3328 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3333 /* The largest possible string for the first two components
3334 * is 2.175 (= 2 * 40 + 175 = 255), so this is big enough.
3339 snprintf(firstTwo, sizeof(firstTwo), "%d.%d",
3340 pbEncoded[1 + lenBytes] / 40,
3341 pbEncoded[1 + lenBytes] - (pbEncoded[1 + lenBytes] / 40)
3343 bytesNeeded = strlen(firstTwo) + 1;
3344 for (ptr = pbEncoded + 2 + lenBytes; ret &&
3345 ptr - pbEncoded - 1 - lenBytes < dataLen; )
3347 /* large enough for ".4000000" */
3351 while (ptr - pbEncoded - 1 - lenBytes < dataLen &&
3358 if (ptr - pbEncoded - 1 - lenBytes >= dataLen ||
3361 SetLastError(CRYPT_E_ASN1_CORRUPT);
3368 snprintf(str, sizeof(str), ".%d", val);
3369 bytesNeeded += strlen(str);
3373 *pcbObjId = bytesNeeded;
3374 else if (*pcbObjId < bytesNeeded)
3376 *pcbObjId = bytesNeeded;
3377 SetLastError(ERROR_MORE_DATA);
3383 sprintf(pszObjId, "%d.%d", pbEncoded[1 + lenBytes] / 40,
3384 pbEncoded[1 + lenBytes] - (pbEncoded[1 + lenBytes] /
3386 pszObjId += strlen(pszObjId);
3387 for (ptr = pbEncoded + 2 + lenBytes; ret &&
3388 ptr - pbEncoded - 1 - lenBytes < dataLen; )
3392 while (ptr - pbEncoded - 1 - lenBytes < dataLen &&
3401 sprintf(pszObjId, ".%d", val);
3402 pszObjId += strlen(pszObjId);
3408 *pcbObjId = bytesNeeded;
3413 SetLastError(CRYPT_E_ASN1_BADTAG);
3419 SetLastError(STATUS_ACCESS_VIOLATION);
3426 /* Warning: this assumes the address of value->Value.pbData is already set, in
3427 * order to avoid overwriting memory. (In some cases, it may change it, if it
3428 * doesn't copy anything to memory.) Be sure to set it correctly!
3430 static BOOL WINAPI CRYPT_AsnDecodeNameValue(DWORD dwCertEncodingType,
3431 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3432 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3439 CERT_NAME_VALUE *value = (CERT_NAME_VALUE *)pvStructInfo;
3441 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3443 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3445 switch (pbEncoded[0])
3447 case ASN_NUMERICSTRING:
3448 case ASN_PRINTABLESTRING:
3452 FIXME("Unimplemented string type %02x\n", pbEncoded[0]);
3453 SetLastError(OSS_UNIMPLEMENTED);
3458 DWORD bytesNeeded = sizeof(CERT_NAME_VALUE);
3460 switch (pbEncoded[0])
3462 case ASN_NUMERICSTRING:
3463 case ASN_PRINTABLESTRING:
3465 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
3466 bytesNeeded += dataLen;
3470 *pcbStructInfo = bytesNeeded;
3471 else if (*pcbStructInfo < bytesNeeded)
3473 *pcbStructInfo = bytesNeeded;
3474 SetLastError(ERROR_MORE_DATA);
3479 *pcbStructInfo = bytesNeeded;
3480 switch (pbEncoded[0])
3482 case ASN_NUMERICSTRING:
3483 value->dwValueType = CERT_RDN_NUMERIC_STRING;
3485 case ASN_PRINTABLESTRING:
3486 value->dwValueType = CERT_RDN_PRINTABLE_STRING;
3489 value->dwValueType = CERT_RDN_IA5_STRING;
3494 switch (pbEncoded[0])
3496 case ASN_NUMERICSTRING:
3497 case ASN_PRINTABLESTRING:
3499 value->Value.cbData = dataLen;
3500 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
3501 value->Value.pbData = (BYTE *)pbEncoded + 1 +
3505 assert(value->Value.pbData);
3506 memcpy(value->Value.pbData,
3507 pbEncoded + 1 + lenBytes, dataLen);
3514 value->Value.cbData = 0;
3515 value->Value.pbData = NULL;
3523 SetLastError(STATUS_ACCESS_VIOLATION);
3530 static BOOL WINAPI CRYPT_AsnDecodeRdnAttr(DWORD dwCertEncodingType,
3531 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3532 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3536 TRACE("%p, %ld, %08lx, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3537 pvStructInfo, *pcbStructInfo);
3541 struct AsnDecodeSequenceItem items[] = {
3542 { ASN_OBJECTIDENTIFIER, offsetof(CERT_RDN_ATTR, pszObjId),
3543 CRYPT_AsnDecodeOidWrapper, sizeof(LPSTR), FALSE, TRUE,
3544 offsetof(CERT_RDN_ATTR, pszObjId), 0 },
3545 { 0, offsetof(CERT_RDN_ATTR, dwValueType), CRYPT_AsnDecodeNameValue,
3546 sizeof(CERT_NAME_VALUE), FALSE, TRUE, offsetof(CERT_RDN_ATTR,
3549 CERT_RDN_ATTR *attr = (CERT_RDN_ATTR *)pvStructInfo;
3552 TRACE("attr->pszObjId is %p\n", attr->pszObjId);
3553 ret = CRYPT_AsnDecodeSequence(X509_ASN_ENCODING, items,
3554 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags, NULL,
3555 attr, pcbStructInfo, attr ? attr->pszObjId : NULL);
3557 TRACE("attr->pszObjId is %p (%s)\n", attr->pszObjId,
3558 debugstr_a(attr->pszObjId));
3559 TRACE("returning %d (%08lx)\n", ret, GetLastError());
3563 SetLastError(STATUS_ACCESS_VIOLATION);
3570 static BOOL WINAPI CRYPT_AsnDecodeRdn(DWORD dwCertEncodingType,
3571 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3572 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3578 struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
3579 CRYPT_AsnDecodeRdnAttr, sizeof(CERT_RDN_ATTR), TRUE,
3580 offsetof(CERT_RDN_ATTR, pszObjId) };
3582 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3583 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3587 SetLastError(STATUS_ACCESS_VIOLATION);
3594 static BOOL WINAPI CRYPT_AsnDecodeName(DWORD dwCertEncodingType,
3595 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3596 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3602 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3603 CRYPT_AsnDecodeRdn, sizeof(CERT_RDN), TRUE,
3604 offsetof(CERT_RDN, rgRDNAttr) };
3606 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3607 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3611 SetLastError(STATUS_ACCESS_VIOLATION);
3618 static BOOL WINAPI CRYPT_AsnDecodeCopyBytes(DWORD dwCertEncodingType,
3619 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3620 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3623 DWORD bytesNeeded = sizeof(CRYPT_OBJID_BLOB);
3625 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3626 pDecodePara, pvStructInfo, *pcbStructInfo);
3628 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
3629 bytesNeeded += cbEncoded;
3631 *pcbStructInfo = bytesNeeded;
3632 else if (*pcbStructInfo < bytesNeeded)
3634 SetLastError(ERROR_MORE_DATA);
3635 *pcbStructInfo = bytesNeeded;
3640 PCRYPT_OBJID_BLOB blob = (PCRYPT_OBJID_BLOB)pvStructInfo;
3642 *pcbStructInfo = bytesNeeded;
3643 blob->cbData = cbEncoded;
3644 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
3645 blob->pbData = (LPBYTE)pbEncoded;
3648 assert(blob->pbData);
3649 memcpy(blob->pbData, pbEncoded, blob->cbData);
3655 static BOOL WINAPI CRYPT_AsnDecodeAlgorithmId(DWORD dwCertEncodingType,
3656 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3657 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3659 CRYPT_ALGORITHM_IDENTIFIER *algo =
3660 (CRYPT_ALGORITHM_IDENTIFIER *)pvStructInfo;
3662 struct AsnDecodeSequenceItem items[] = {
3663 { ASN_OBJECTIDENTIFIER, offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId),
3664 CRYPT_AsnDecodeOidWrapper, sizeof(LPSTR), FALSE, TRUE,
3665 offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId), 0 },
3666 { 0, offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters),
3667 CRYPT_AsnDecodeCopyBytes, sizeof(CRYPT_OBJID_BLOB), TRUE, TRUE,
3668 offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters.pbData), 0 },
3671 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3672 pDecodePara, pvStructInfo, *pcbStructInfo);
3674 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3675 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3676 pDecodePara, pvStructInfo, pcbStructInfo, algo ? algo->pszObjId : NULL);
3677 if (ret && pvStructInfo)
3679 TRACE("pszObjId is %p (%s)\n", algo->pszObjId,
3680 debugstr_a(algo->pszObjId));
3685 static BOOL WINAPI CRYPT_AsnDecodePubKeyInfo(DWORD dwCertEncodingType,
3686 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3687 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3693 struct AsnDecodeSequenceItem items[] = {
3694 { ASN_SEQUENCEOF, offsetof(CERT_PUBLIC_KEY_INFO, Algorithm),
3695 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
3696 FALSE, TRUE, offsetof(CERT_PUBLIC_KEY_INFO,
3697 Algorithm.pszObjId) },
3698 { ASN_BITSTRING, offsetof(CERT_PUBLIC_KEY_INFO, PublicKey),
3699 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
3700 offsetof(CERT_PUBLIC_KEY_INFO, PublicKey.pbData) },
3703 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3704 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3705 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3709 SetLastError(STATUS_ACCESS_VIOLATION);
3716 static BOOL WINAPI CRYPT_AsnDecodeBool(DWORD dwCertEncodingType,
3717 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3718 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3724 SetLastError(CRYPT_E_ASN1_CORRUPT);
3727 if (GET_LEN_BYTES(pbEncoded[1]) > 1)
3729 SetLastError(CRYPT_E_ASN1_CORRUPT);
3732 if (pbEncoded[1] > 1)
3734 SetLastError(CRYPT_E_ASN1_CORRUPT);
3739 *pcbStructInfo = sizeof(BOOL);
3742 else if (*pcbStructInfo < sizeof(BOOL))
3744 *pcbStructInfo = sizeof(BOOL);
3745 SetLastError(ERROR_MORE_DATA);
3750 *(BOOL *)pvStructInfo = pbEncoded[2] ? TRUE : FALSE;
3753 TRACE("returning %d (%08lx)\n", ret, GetLastError());
3757 static BOOL WINAPI CRYPT_AsnDecodeAltNameEntry(DWORD dwCertEncodingType,
3758 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3759 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3761 PCERT_ALT_NAME_ENTRY entry = (PCERT_ALT_NAME_ENTRY)pvStructInfo;
3762 DWORD dataLen, lenBytes, bytesNeeded = sizeof(CERT_ALT_NAME_ENTRY);
3765 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3766 pDecodePara, pvStructInfo, *pcbStructInfo);
3770 SetLastError(CRYPT_E_ASN1_CORRUPT);
3773 if ((pbEncoded[0] & ASN_FLAGS_MASK) != ASN_CONTEXT)
3775 SetLastError(CRYPT_E_ASN1_BADTAG);
3778 lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3779 if (1 + lenBytes > cbEncoded)
3781 SetLastError(CRYPT_E_ASN1_CORRUPT);
3784 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3786 switch (pbEncoded[0] & ASN_TYPE_MASK)
3788 case 1: /* rfc822Name */
3789 case 2: /* dNSName */
3790 case 6: /* uniformResourceIdentifier */
3791 bytesNeeded += (dataLen + 1) * sizeof(WCHAR);
3793 case 7: /* iPAddress */
3794 bytesNeeded += dataLen;
3796 case 8: /* registeredID */
3797 /* FIXME: decode as OID */
3798 case 0: /* otherName */
3799 case 4: /* directoryName */
3801 SetLastError(CRYPT_E_ASN1_BADTAG);
3804 case 3: /* x400Address, unimplemented */
3805 case 5: /* ediPartyName, unimplemented */
3806 SetLastError(CRYPT_E_ASN1_BADTAG);
3810 SetLastError(CRYPT_E_ASN1_CORRUPT);
3816 *pcbStructInfo = bytesNeeded;
3817 else if (*pcbStructInfo < bytesNeeded)
3819 *pcbStructInfo = bytesNeeded;
3820 SetLastError(ERROR_MORE_DATA);
3825 *pcbStructInfo = bytesNeeded;
3826 /* MS used values one greater than the asn1 ones.. sigh */
3827 entry->dwAltNameChoice = (pbEncoded[0] & 0x7f) + 1;
3828 switch (pbEncoded[0] & ASN_TYPE_MASK)
3830 case 1: /* rfc822Name */
3831 case 2: /* dNSName */
3832 case 6: /* uniformResourceIdentifier */
3836 for (i = 0; i < dataLen; i++)
3837 entry->u.pwszURL[i] =
3838 (WCHAR)pbEncoded[1 + lenBytes + i];
3839 entry->u.pwszURL[i] = 0;
3840 TRACE("URL is %p (%s)\n", entry->u.pwszURL,
3841 debugstr_w(entry->u.pwszURL));
3844 case 7: /* iPAddress */
3845 /* The next data pointer is in the pwszURL spot, that is,
3846 * the first 4 bytes. Need to move it to the next spot.
3848 entry->u.IPAddress.pbData = (LPBYTE)entry->u.pwszURL;
3849 entry->u.IPAddress.cbData = dataLen;
3850 memcpy(entry->u.IPAddress.pbData, pbEncoded + 1 + lenBytes,
3860 static BOOL WINAPI CRYPT_AsnDecodeAltNameInternal(DWORD dwCertEncodingType,
3861 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3862 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3865 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3866 CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
3867 offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
3868 PCERT_ALT_NAME_INFO info = (PCERT_ALT_NAME_INFO)pvStructInfo;
3870 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3871 pDecodePara, pvStructInfo, *pcbStructInfo);
3874 TRACE("info->rgAltEntry is %p\n", info->rgAltEntry);
3875 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3876 pDecodePara, pvStructInfo, pcbStructInfo, info ? info->rgAltEntry : NULL);
3880 static BOOL WINAPI CRYPT_AsnDecodeAltName(DWORD dwCertEncodingType,
3881 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3882 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3886 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3887 pDecodePara, pvStructInfo, *pcbStructInfo);
3891 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3892 CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
3893 offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
3895 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3896 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3900 SetLastError(STATUS_ACCESS_VIOLATION);
3907 struct PATH_LEN_CONSTRAINT
3909 BOOL fPathLenConstraint;
3910 DWORD dwPathLenConstraint;
3913 static BOOL WINAPI CRYPT_AsnDecodePathLenConstraint(DWORD dwCertEncodingType,
3914 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3915 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3919 TRACE("%p, %ld, %08lx, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3920 pvStructInfo, *pcbStructInfo);
3924 if (pbEncoded[0] == ASN_INTEGER)
3926 DWORD bytesNeeded = sizeof(struct PATH_LEN_CONSTRAINT);
3929 *pcbStructInfo = bytesNeeded;
3930 else if (*pcbStructInfo < bytesNeeded)
3932 SetLastError(ERROR_MORE_DATA);
3933 *pcbStructInfo = bytesNeeded;
3938 struct PATH_LEN_CONSTRAINT *constraint =
3939 (struct PATH_LEN_CONSTRAINT *)pvStructInfo;
3940 DWORD size = sizeof(constraint->dwPathLenConstraint);
3942 ret = CRYPT_AsnDecodeInt(dwCertEncodingType, X509_INTEGER,
3943 pbEncoded, cbEncoded, 0, NULL,
3944 &constraint->dwPathLenConstraint, &size);
3946 constraint->fPathLenConstraint = TRUE;
3947 TRACE("got an int, dwPathLenConstraint is %ld\n",
3948 constraint->dwPathLenConstraint);
3953 SetLastError(CRYPT_E_ASN1_CORRUPT);
3957 TRACE("returning %d (%08lx)\n", ret, GetLastError());
3961 static BOOL WINAPI CRYPT_AsnDecodeBasicConstraints2(DWORD dwCertEncodingType,
3962 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3963 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3969 struct AsnDecodeSequenceItem items[] = {
3970 { ASN_BOOL, offsetof(CERT_BASIC_CONSTRAINTS2_INFO, fCA),
3971 CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE, FALSE, 0, 0 },
3972 { ASN_INTEGER, offsetof(CERT_BASIC_CONSTRAINTS2_INFO,
3973 fPathLenConstraint), CRYPT_AsnDecodePathLenConstraint,
3974 sizeof(struct PATH_LEN_CONSTRAINT), TRUE, FALSE, 0, 0 },
3977 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3978 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3979 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3983 SetLastError(STATUS_ACCESS_VIOLATION);
3990 #define RSA1_MAGIC 0x31415352
3992 struct DECODED_RSA_PUB_KEY
3995 CRYPT_INTEGER_BLOB modulus;
3998 static BOOL WINAPI CRYPT_AsnDecodeRsaPubKey(DWORD dwCertEncodingType,
3999 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4000 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4006 struct AsnDecodeSequenceItem items[] = {
4007 { ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, modulus),
4008 CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
4009 FALSE, TRUE, offsetof(struct DECODED_RSA_PUB_KEY, modulus.pbData),
4011 { ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, pubexp),
4012 CRYPT_AsnDecodeInt, sizeof(DWORD), FALSE, FALSE, 0, 0 },
4014 struct DECODED_RSA_PUB_KEY *decodedKey = NULL;
4017 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
4018 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
4019 CRYPT_DECODE_ALLOC_FLAG, NULL, &decodedKey, &size, NULL);
4022 DWORD bytesNeeded = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
4023 decodedKey->modulus.cbData;
4027 *pcbStructInfo = bytesNeeded;
4030 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4031 pvStructInfo, pcbStructInfo, bytesNeeded)))
4034 RSAPUBKEY *rsaPubKey;
4036 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4037 pvStructInfo = *(BYTE **)pvStructInfo;
4038 hdr = (BLOBHEADER *)pvStructInfo;
4039 hdr->bType = PUBLICKEYBLOB;
4040 hdr->bVersion = CUR_BLOB_VERSION;
4042 hdr->aiKeyAlg = CALG_RSA_KEYX;
4043 rsaPubKey = (RSAPUBKEY *)((BYTE *)pvStructInfo +
4044 sizeof(BLOBHEADER));
4045 rsaPubKey->magic = RSA1_MAGIC;
4046 rsaPubKey->pubexp = decodedKey->pubexp;
4047 rsaPubKey->bitlen = decodedKey->modulus.cbData * 8;
4048 memcpy((BYTE *)pvStructInfo + sizeof(BLOBHEADER) +
4049 sizeof(RSAPUBKEY), decodedKey->modulus.pbData,
4050 decodedKey->modulus.cbData);
4052 LocalFree(decodedKey);
4057 SetLastError(STATUS_ACCESS_VIOLATION);
4064 static BOOL WINAPI CRYPT_AsnDecodeOctetsInternal(DWORD dwCertEncodingType,
4065 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4066 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4069 DWORD bytesNeeded, dataLen;
4071 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
4072 pDecodePara, pvStructInfo, *pcbStructInfo);
4074 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4076 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4077 bytesNeeded = sizeof(CRYPT_DATA_BLOB);
4079 bytesNeeded = dataLen + sizeof(CRYPT_DATA_BLOB);
4081 *pcbStructInfo = bytesNeeded;
4082 else if (*pcbStructInfo < bytesNeeded)
4084 SetLastError(ERROR_MORE_DATA);
4085 *pcbStructInfo = bytesNeeded;
4090 CRYPT_DATA_BLOB *blob;
4091 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4093 blob = (CRYPT_DATA_BLOB *)pvStructInfo;
4094 blob->cbData = dataLen;
4095 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4096 blob->pbData = (BYTE *)pbEncoded + 1 + lenBytes;
4099 assert(blob->pbData);
4101 memcpy(blob->pbData, pbEncoded + 1 + lenBytes,
4109 static BOOL WINAPI CRYPT_AsnDecodeOctets(DWORD dwCertEncodingType,
4110 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4111 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4115 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
4116 pDecodePara, pvStructInfo, *pcbStructInfo);
4124 SetLastError(CRYPT_E_ASN1_CORRUPT);
4127 else if (pbEncoded[0] != ASN_OCTETSTRING)
4129 SetLastError(CRYPT_E_ASN1_BADTAG);
4132 else if ((ret = CRYPT_AsnDecodeOctetsInternal(dwCertEncodingType,
4133 lpszStructType, pbEncoded, cbEncoded,
4134 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4137 *pcbStructInfo = bytesNeeded;
4138 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4139 pvStructInfo, pcbStructInfo, bytesNeeded)))
4141 CRYPT_DATA_BLOB *blob;
4143 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4144 pvStructInfo = *(BYTE **)pvStructInfo;
4145 blob = (CRYPT_DATA_BLOB *)pvStructInfo;
4146 blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_DATA_BLOB);
4147 ret = CRYPT_AsnDecodeOctetsInternal(dwCertEncodingType,
4148 lpszStructType, pbEncoded, cbEncoded,
4149 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
4156 SetLastError(STATUS_ACCESS_VIOLATION);
4163 static BOOL WINAPI CRYPT_AsnDecodeBitsInternal(DWORD dwCertEncodingType,
4164 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4165 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4169 TRACE("(%p, %ld, 0x%08lx, %p, %p, %ld)\n", pbEncoded, cbEncoded, dwFlags,
4170 pDecodePara, pvStructInfo, *pcbStructInfo);
4172 if (pbEncoded[0] == ASN_BITSTRING)
4174 DWORD bytesNeeded, dataLen;
4176 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4178 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4179 bytesNeeded = sizeof(CRYPT_BIT_BLOB);
4181 bytesNeeded = dataLen - 1 + sizeof(CRYPT_BIT_BLOB);
4183 *pcbStructInfo = bytesNeeded;
4184 else if (*pcbStructInfo < bytesNeeded)
4186 *pcbStructInfo = bytesNeeded;
4187 SetLastError(ERROR_MORE_DATA);
4192 CRYPT_BIT_BLOB *blob;
4194 blob = (CRYPT_BIT_BLOB *)pvStructInfo;
4195 blob->cbData = dataLen - 1;
4196 blob->cUnusedBits = *(pbEncoded + 1 +
4197 GET_LEN_BYTES(pbEncoded[1]));
4198 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4200 blob->pbData = (BYTE *)pbEncoded + 2 +
4201 GET_LEN_BYTES(pbEncoded[1]);
4205 assert(blob->pbData);
4208 BYTE mask = 0xff << blob->cUnusedBits;
4210 memcpy(blob->pbData, pbEncoded + 2 +
4211 GET_LEN_BYTES(pbEncoded[1]), blob->cbData);
4212 blob->pbData[blob->cbData - 1] &= mask;
4220 SetLastError(CRYPT_E_ASN1_BADTAG);
4223 TRACE("returning %d (%08lx)\n", ret, GetLastError());
4227 static BOOL WINAPI CRYPT_AsnDecodeBits(DWORD dwCertEncodingType,
4228 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4229 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4233 TRACE("(%p, %ld, 0x%08lx, %p, %p, %p)\n", pbEncoded, cbEncoded, dwFlags,
4234 pDecodePara, pvStructInfo, pcbStructInfo);
4240 if ((ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType,
4241 lpszStructType, pbEncoded, cbEncoded,
4242 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4245 *pcbStructInfo = bytesNeeded;
4246 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4247 pvStructInfo, pcbStructInfo, bytesNeeded)))
4249 CRYPT_BIT_BLOB *blob;
4251 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4252 pvStructInfo = *(BYTE **)pvStructInfo;
4253 blob = (CRYPT_BIT_BLOB *)pvStructInfo;
4254 blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_BIT_BLOB);
4255 ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType,
4256 lpszStructType, pbEncoded, cbEncoded,
4257 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
4264 SetLastError(STATUS_ACCESS_VIOLATION);
4268 TRACE("returning %d (%08lx)\n", ret, GetLastError());
4272 static BOOL WINAPI CRYPT_AsnDecodeInt(DWORD dwCertEncodingType,
4273 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4274 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4280 *pcbStructInfo = sizeof(int);
4285 BYTE buf[sizeof(CRYPT_INTEGER_BLOB) + sizeof(int)];
4286 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)buf;
4287 DWORD size = sizeof(buf);
4289 blob->pbData = buf + sizeof(CRYPT_INTEGER_BLOB);
4290 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
4291 X509_MULTI_BYTE_INTEGER, pbEncoded, cbEncoded, 0, NULL, &buf, &size);
4294 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4295 pvStructInfo, pcbStructInfo, sizeof(int))))
4299 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4300 pvStructInfo = *(BYTE **)pvStructInfo;
4301 if (blob->pbData[blob->cbData - 1] & 0x80)
4303 /* initialize to a negative value to sign-extend */
4308 for (i = 0; i < blob->cbData; i++)
4311 val |= blob->pbData[blob->cbData - i - 1];
4313 memcpy(pvStructInfo, &val, sizeof(int));
4316 else if (GetLastError() == ERROR_MORE_DATA)
4317 SetLastError(CRYPT_E_ASN1_LARGE);
4321 SetLastError(STATUS_ACCESS_VIOLATION);
4328 static BOOL WINAPI CRYPT_AsnDecodeIntegerInternal(DWORD dwCertEncodingType,
4329 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4330 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4334 if (pbEncoded[0] == ASN_INTEGER)
4336 DWORD bytesNeeded, dataLen;
4338 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4340 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4342 bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
4344 *pcbStructInfo = bytesNeeded;
4345 else if (*pcbStructInfo < bytesNeeded)
4347 *pcbStructInfo = bytesNeeded;
4348 SetLastError(ERROR_MORE_DATA);
4353 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4355 blob->cbData = dataLen;
4356 assert(blob->pbData);
4361 for (i = 0; i < blob->cbData; i++)
4363 blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
4372 SetLastError(CRYPT_E_ASN1_BADTAG);
4378 static BOOL WINAPI CRYPT_AsnDecodeInteger(DWORD dwCertEncodingType,
4379 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4380 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4388 if ((ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
4389 lpszStructType, pbEncoded, cbEncoded,
4390 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4393 *pcbStructInfo = bytesNeeded;
4394 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4395 pvStructInfo, pcbStructInfo, bytesNeeded)))
4397 CRYPT_INTEGER_BLOB *blob;
4399 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4400 pvStructInfo = *(BYTE **)pvStructInfo;
4401 blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4402 blob->pbData = (BYTE *)pvStructInfo +
4403 sizeof(CRYPT_INTEGER_BLOB);
4404 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
4405 lpszStructType, pbEncoded, cbEncoded,
4406 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pvStructInfo,
4413 SetLastError(STATUS_ACCESS_VIOLATION);
4420 static BOOL WINAPI CRYPT_AsnDecodeUnsignedIntegerInternal(
4421 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
4422 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
4423 void *pvStructInfo, DWORD *pcbStructInfo)
4427 if (pbEncoded[0] == ASN_INTEGER)
4429 DWORD bytesNeeded, dataLen;
4431 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4433 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4435 bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
4437 *pcbStructInfo = bytesNeeded;
4438 else if (*pcbStructInfo < bytesNeeded)
4440 *pcbStructInfo = bytesNeeded;
4441 SetLastError(ERROR_MORE_DATA);
4446 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4448 blob->cbData = dataLen;
4449 assert(blob->pbData);
4450 /* remove leading zero byte if it exists */
4451 if (blob->cbData && *(pbEncoded + 1 + lenBytes) == 0)
4460 for (i = 0; i < blob->cbData; i++)
4462 blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
4471 SetLastError(CRYPT_E_ASN1_BADTAG);
4477 static BOOL WINAPI CRYPT_AsnDecodeUnsignedInteger(DWORD dwCertEncodingType,
4478 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4479 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4487 if ((ret = CRYPT_AsnDecodeUnsignedIntegerInternal(dwCertEncodingType,
4488 lpszStructType, pbEncoded, cbEncoded,
4489 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4492 *pcbStructInfo = bytesNeeded;
4493 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4494 pvStructInfo, pcbStructInfo, bytesNeeded)))
4496 CRYPT_INTEGER_BLOB *blob;
4498 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4499 pvStructInfo = *(BYTE **)pvStructInfo;
4500 blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4501 blob->pbData = (BYTE *)pvStructInfo +
4502 sizeof(CRYPT_INTEGER_BLOB);
4503 ret = CRYPT_AsnDecodeUnsignedIntegerInternal(dwCertEncodingType,
4504 lpszStructType, pbEncoded, cbEncoded,
4505 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pvStructInfo,
4512 SetLastError(STATUS_ACCESS_VIOLATION);
4519 static BOOL WINAPI CRYPT_AsnDecodeEnumerated(DWORD dwCertEncodingType,
4520 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4521 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4527 *pcbStructInfo = sizeof(int);
4532 if (pbEncoded[0] == ASN_ENUMERATED)
4534 unsigned int val = 0, i;
4538 SetLastError(CRYPT_E_ASN1_EOD);
4541 else if (pbEncoded[1] == 0)
4543 SetLastError(CRYPT_E_ASN1_CORRUPT);
4548 /* A little strange looking, but we have to accept a sign byte:
4549 * 0xffffffff gets encoded as 0a 05 00 ff ff ff ff. Also,
4550 * assuming a small length is okay here, it has to be in short
4553 if (pbEncoded[1] > sizeof(unsigned int) + 1)
4555 SetLastError(CRYPT_E_ASN1_LARGE);
4558 for (i = 0; i < pbEncoded[1]; i++)
4561 val |= pbEncoded[2 + i];
4563 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4564 pvStructInfo, pcbStructInfo, sizeof(unsigned int))))
4566 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4567 pvStructInfo = *(BYTE **)pvStructInfo;
4568 memcpy(pvStructInfo, &val, sizeof(unsigned int));
4574 SetLastError(CRYPT_E_ASN1_BADTAG);
4580 SetLastError(STATUS_ACCESS_VIOLATION);
4587 /* Modifies word, pbEncoded, and len, and magically sets a value ret to FALSE
4590 #define CRYPT_TIME_GET_DIGITS(pbEncoded, len, numDigits, word) \
4595 for (i = 0; (len) > 0 && i < (numDigits); i++, (len)--) \
4597 if (!isdigit(*(pbEncoded))) \
4599 SetLastError(CRYPT_E_ASN1_CORRUPT); \
4605 (word) += *(pbEncoded)++ - '0'; \
4610 static BOOL CRYPT_AsnDecodeTimeZone(const BYTE *pbEncoded, DWORD len,
4611 SYSTEMTIME *sysTime)
4618 if (len >= 3 && (*pbEncoded == '+' || *pbEncoded == '-'))
4620 WORD hours, minutes = 0;
4621 BYTE sign = *pbEncoded++;
4624 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, hours);
4625 if (ret && hours >= 24)
4627 SetLastError(CRYPT_E_ASN1_CORRUPT);
4632 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, minutes);
4633 if (ret && minutes >= 60)
4635 SetLastError(CRYPT_E_ASN1_CORRUPT);
4643 sysTime->wHour += hours;
4644 sysTime->wMinute += minutes;
4648 if (hours > sysTime->wHour)
4651 sysTime->wHour = 24 - (hours - sysTime->wHour);
4654 sysTime->wHour -= hours;
4655 if (minutes > sysTime->wMinute)
4658 sysTime->wMinute = 60 - (minutes - sysTime->wMinute);
4661 sysTime->wMinute -= minutes;
4668 SetLastError(STATUS_ACCESS_VIOLATION);
4675 #define MIN_ENCODED_TIME_LENGTH 10
4677 static BOOL WINAPI CRYPT_AsnDecodeUtcTime(DWORD dwCertEncodingType,
4678 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4679 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4685 *pcbStructInfo = sizeof(FILETIME);
4691 if (pbEncoded[0] == ASN_UTCTIME)
4695 SetLastError(CRYPT_E_ASN1_EOD);
4698 else if (pbEncoded[1] > 0x7f)
4700 /* long-form date strings really can't be valid */
4701 SetLastError(CRYPT_E_ASN1_CORRUPT);
4706 SYSTEMTIME sysTime = { 0 };
4707 BYTE len = pbEncoded[1];
4709 if (len < MIN_ENCODED_TIME_LENGTH)
4711 SetLastError(CRYPT_E_ASN1_CORRUPT);
4717 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wYear);
4718 if (sysTime.wYear >= 50)
4719 sysTime.wYear += 1900;
4721 sysTime.wYear += 2000;
4722 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
4723 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
4724 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
4725 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMinute);
4728 if (len >= 2 && isdigit(*pbEncoded) &&
4729 isdigit(*(pbEncoded + 1)))
4730 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
4732 else if (isdigit(*pbEncoded))
4733 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 1,
4736 ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
4739 if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags,
4740 pDecodePara, pvStructInfo, pcbStructInfo,
4743 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4744 pvStructInfo = *(BYTE **)pvStructInfo;
4745 ret = SystemTimeToFileTime(&sysTime,
4746 (FILETIME *)pvStructInfo);
4753 SetLastError(CRYPT_E_ASN1_BADTAG);
4759 SetLastError(STATUS_ACCESS_VIOLATION);
4766 static BOOL WINAPI CRYPT_AsnDecodeGeneralizedTime(DWORD dwCertEncodingType,
4767 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4768 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4774 *pcbStructInfo = sizeof(FILETIME);
4780 if (pbEncoded[0] == ASN_GENERALTIME)
4784 SetLastError(CRYPT_E_ASN1_EOD);
4787 else if (pbEncoded[1] > 0x7f)
4789 /* long-form date strings really can't be valid */
4790 SetLastError(CRYPT_E_ASN1_CORRUPT);
4795 BYTE len = pbEncoded[1];
4797 if (len < MIN_ENCODED_TIME_LENGTH)
4799 SetLastError(CRYPT_E_ASN1_CORRUPT);
4804 SYSTEMTIME sysTime = { 0 };
4807 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 4, sysTime.wYear);
4808 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
4809 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
4810 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
4813 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
4816 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
4818 if (ret && len > 0 && (*pbEncoded == '.' ||
4825 /* workaround macro weirdness */
4826 digits = min(len, 3);
4827 CRYPT_TIME_GET_DIGITS(pbEncoded, len, digits,
4828 sysTime.wMilliseconds);
4831 ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
4834 if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags,
4835 pDecodePara, pvStructInfo, pcbStructInfo,
4838 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4839 pvStructInfo = *(BYTE **)pvStructInfo;
4840 ret = SystemTimeToFileTime(&sysTime,
4841 (FILETIME *)pvStructInfo);
4848 SetLastError(CRYPT_E_ASN1_BADTAG);
4854 SetLastError(STATUS_ACCESS_VIOLATION);
4861 static BOOL WINAPI CRYPT_AsnDecodeChoiceOfTime(DWORD dwCertEncodingType,
4862 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4863 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4869 if (pbEncoded[0] == ASN_UTCTIME)
4870 ret = CRYPT_AsnDecodeUtcTime(dwCertEncodingType, lpszStructType,
4871 pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
4873 else if (pbEncoded[0] == ASN_GENERALTIME)
4874 ret = CRYPT_AsnDecodeGeneralizedTime(dwCertEncodingType,
4875 lpszStructType, pbEncoded, cbEncoded, dwFlags, pDecodePara,
4876 pvStructInfo, pcbStructInfo);
4879 SetLastError(CRYPT_E_ASN1_BADTAG);
4885 SetLastError(STATUS_ACCESS_VIOLATION);
4892 static BOOL WINAPI CRYPT_AsnDecodeSequenceOfAny(DWORD dwCertEncodingType,
4893 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4894 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4900 if (pbEncoded[0] == ASN_SEQUENCEOF)
4902 DWORD bytesNeeded, dataLen, remainingLen, cValue;
4904 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4909 lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4910 bytesNeeded = sizeof(CRYPT_SEQUENCE_OF_ANY);
4912 ptr = pbEncoded + 1 + lenBytes;
4913 remainingLen = dataLen;
4914 while (ret && remainingLen)
4918 ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
4921 DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
4923 remainingLen -= 1 + nextLenBytes + nextLen;
4924 ptr += 1 + nextLenBytes + nextLen;
4925 bytesNeeded += sizeof(CRYPT_DER_BLOB);
4926 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
4927 bytesNeeded += 1 + nextLenBytes + nextLen;
4933 CRYPT_SEQUENCE_OF_ANY *seq;
4937 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4938 pvStructInfo, pcbStructInfo, bytesNeeded)))
4940 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4941 pvStructInfo = *(BYTE **)pvStructInfo;
4942 seq = (CRYPT_SEQUENCE_OF_ANY *)pvStructInfo;
4943 seq->cValue = cValue;
4944 seq->rgValue = (CRYPT_DER_BLOB *)((BYTE *)seq +
4946 nextPtr = (BYTE *)seq->rgValue +
4947 cValue * sizeof(CRYPT_DER_BLOB);
4948 ptr = pbEncoded + 1 + lenBytes;
4949 remainingLen = dataLen;
4951 while (ret && remainingLen)
4955 ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
4958 DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
4960 seq->rgValue[i].cbData = 1 + nextLenBytes +
4962 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4963 seq->rgValue[i].pbData = (BYTE *)ptr;
4966 seq->rgValue[i].pbData = nextPtr;
4967 memcpy(nextPtr, ptr, 1 + nextLenBytes +
4969 nextPtr += 1 + nextLenBytes + nextLen;
4971 remainingLen -= 1 + nextLenBytes + nextLen;
4972 ptr += 1 + nextLenBytes + nextLen;
4982 SetLastError(CRYPT_E_ASN1_BADTAG);
4988 SetLastError(STATUS_ACCESS_VIOLATION);
4995 static BOOL WINAPI CRYPT_AsnDecodeDistPoint(DWORD dwCertEncodingType,
4996 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4997 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4999 struct AsnDecodeSequenceItem items[] = {
5000 { ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_DIST_POINT,
5001 DistPointName), CRYPT_AsnDecodeAltNameInternal,
5002 sizeof(CRL_DIST_POINT_NAME), TRUE, TRUE, offsetof(CRL_DIST_POINT,
5003 DistPointName.u.FullName.rgAltEntry), 0 },
5004 { ASN_CONTEXT | 1, offsetof(CRL_DIST_POINT, ReasonFlags),
5005 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
5006 offsetof(CRL_DIST_POINT, ReasonFlags.pbData), 0 },
5007 { ASN_CONTEXT | ASN_CONSTRUCTOR | 2, offsetof(CRL_DIST_POINT, CRLIssuer),
5008 CRYPT_AsnDecodeAltNameInternal, sizeof(CERT_ALT_NAME_INFO), TRUE, TRUE,
5009 offsetof(CRL_DIST_POINT, CRLIssuer.rgAltEntry), 0 },
5013 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
5014 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
5015 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
5019 static BOOL WINAPI CRYPT_AsnDecodeCRLDistPoints(DWORD dwCertEncodingType,
5020 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
5021 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
5025 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
5026 pDecodePara, pvStructInfo, *pcbStructInfo);
5030 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
5031 CRYPT_AsnDecodeDistPoint, sizeof(CRL_DIST_POINT), TRUE,
5032 offsetof(CRL_DIST_POINT, DistPointName.u.FullName.rgAltEntry) };
5034 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
5035 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
5039 SetLastError(STATUS_ACCESS_VIOLATION);
5046 BOOL WINAPI CryptDecodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
5047 const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
5048 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
5050 static HCRYPTOIDFUNCSET set = NULL;
5052 CryptDecodeObjectExFunc decodeFunc = NULL;
5053 HCRYPTOIDFUNCADDR hFunc = NULL;
5055 TRACE("(0x%08lx, %s, %p, %ld, 0x%08lx, %p, %p, %p)\n",
5056 dwCertEncodingType, debugstr_a(lpszStructType), pbEncoded,
5057 cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
5059 if (!pvStructInfo && !pcbStructInfo)
5061 SetLastError(ERROR_INVALID_PARAMETER);
5064 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
5065 && (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
5067 SetLastError(ERROR_FILE_NOT_FOUND);
5072 SetLastError(CRYPT_E_ASN1_EOD);
5075 if (cbEncoded > MAX_ENCODED_LEN)
5077 SetLastError(CRYPT_E_ASN1_LARGE);
5081 SetLastError(NOERROR);
5082 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG && pvStructInfo)
5083 *(BYTE **)pvStructInfo = NULL;
5084 if (!HIWORD(lpszStructType))
5086 switch (LOWORD(lpszStructType))
5088 case (WORD)X509_CERT:
5089 decodeFunc = CRYPT_AsnDecodeCert;
5091 case (WORD)X509_CERT_TO_BE_SIGNED:
5092 decodeFunc = CRYPT_AsnDecodeCertInfo;
5094 case (WORD)X509_CERT_CRL_TO_BE_SIGNED:
5095 decodeFunc = CRYPT_AsnDecodeCRLInfo;
5097 case (WORD)X509_EXTENSIONS:
5098 decodeFunc = CRYPT_AsnDecodeExtensions;
5100 case (WORD)X509_NAME:
5101 decodeFunc = CRYPT_AsnDecodeName;
5103 case (WORD)X509_PUBLIC_KEY_INFO:
5104 decodeFunc = CRYPT_AsnDecodePubKeyInfo;
5106 case (WORD)X509_ALTERNATE_NAME:
5107 decodeFunc = CRYPT_AsnDecodeAltName;
5109 case (WORD)X509_BASIC_CONSTRAINTS2:
5110 decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
5112 case (WORD)RSA_CSP_PUBLICKEYBLOB:
5113 decodeFunc = CRYPT_AsnDecodeRsaPubKey;
5115 case (WORD)X509_OCTET_STRING:
5116 decodeFunc = CRYPT_AsnDecodeOctets;
5118 case (WORD)X509_BITS:
5119 case (WORD)X509_KEY_USAGE:
5120 decodeFunc = CRYPT_AsnDecodeBits;
5122 case (WORD)X509_INTEGER:
5123 decodeFunc = CRYPT_AsnDecodeInt;
5125 case (WORD)X509_MULTI_BYTE_INTEGER:
5126 decodeFunc = CRYPT_AsnDecodeInteger;
5128 case (WORD)X509_MULTI_BYTE_UINT:
5129 decodeFunc = CRYPT_AsnDecodeUnsignedInteger;
5131 case (WORD)X509_ENUMERATED:
5132 decodeFunc = CRYPT_AsnDecodeEnumerated;
5134 case (WORD)X509_CHOICE_OF_TIME:
5135 decodeFunc = CRYPT_AsnDecodeChoiceOfTime;
5137 case (WORD)X509_SEQUENCE_OF_ANY:
5138 decodeFunc = CRYPT_AsnDecodeSequenceOfAny;
5140 case (WORD)PKCS_UTC_TIME:
5141 decodeFunc = CRYPT_AsnDecodeUtcTime;
5143 case (WORD)X509_CRL_DIST_POINTS:
5144 decodeFunc = CRYPT_AsnDecodeCRLDistPoints;
5147 FIXME("%d: unimplemented\n", LOWORD(lpszStructType));
5150 else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
5151 decodeFunc = CRYPT_AsnDecodeExtensions;
5152 else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
5153 decodeFunc = CRYPT_AsnDecodeUtcTime;
5154 else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
5155 decodeFunc = CRYPT_AsnDecodeEnumerated;
5156 else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
5157 decodeFunc = CRYPT_AsnDecodeBits;
5158 else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
5159 decodeFunc = CRYPT_AsnDecodeOctets;
5160 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
5161 decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
5162 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
5163 decodeFunc = CRYPT_AsnDecodeAltName;
5164 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
5165 decodeFunc = CRYPT_AsnDecodeAltName;
5166 else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
5167 decodeFunc = CRYPT_AsnDecodeAltName;
5168 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
5169 decodeFunc = CRYPT_AsnDecodeAltName;
5170 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
5171 decodeFunc = CRYPT_AsnDecodeAltName;
5173 TRACE("OID %s not found or unimplemented, looking for DLL\n",
5174 debugstr_a(lpszStructType));
5178 set = CryptInitOIDFunctionSet(CRYPT_OID_DECODE_OBJECT_EX_FUNC, 0);
5179 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
5180 (void **)&decodeFunc, &hFunc);
5183 ret = decodeFunc(dwCertEncodingType, lpszStructType, pbEncoded,
5184 cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
5186 SetLastError(ERROR_FILE_NOT_FOUND);
5188 CryptFreeOIDFunctionAddress(hFunc, 0);
5192 BOOL WINAPI CryptExportPublicKeyInfo(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
5193 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
5195 return CryptExportPublicKeyInfoEx(hCryptProv, dwKeySpec, dwCertEncodingType,
5196 NULL, 0, NULL, pInfo, pcbInfo);
5199 static BOOL WINAPI CRYPT_ExportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
5200 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
5201 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
5206 TRACE("(%ld, %ld, %08lx, %s, %08lx, %p, %p, %p)\n", hCryptProv, dwKeySpec,
5207 dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags, pvAuxInfo,
5210 if (!pszPublicKeyObjId)
5211 pszPublicKeyObjId = szOID_RSA_RSA;
5212 if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key)))
5216 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, NULL, &keySize);
5219 LPBYTE pubKey = CryptMemAlloc(keySize);
5223 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, pubKey,
5227 DWORD encodedLen = 0;
5229 ret = CryptEncodeObject(dwCertEncodingType,
5230 RSA_CSP_PUBLICKEYBLOB, pubKey, NULL, &encodedLen);
5233 DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) +
5234 strlen(pszPublicKeyObjId) + 1 + encodedLen;
5237 *pcbInfo = sizeNeeded;
5238 else if (*pcbInfo < sizeNeeded)
5240 SetLastError(ERROR_MORE_DATA);
5241 *pcbInfo = sizeNeeded;
5246 pInfo->Algorithm.pszObjId = (char *)pInfo +
5247 sizeof(CERT_PUBLIC_KEY_INFO);
5248 lstrcpyA(pInfo->Algorithm.pszObjId,
5250 pInfo->Algorithm.Parameters.cbData = 0;
5251 pInfo->Algorithm.Parameters.pbData = NULL;
5252 pInfo->PublicKey.pbData =
5253 (BYTE *)pInfo->Algorithm.pszObjId
5254 + lstrlenA(pInfo->Algorithm.pszObjId) + 1;
5255 pInfo->PublicKey.cbData = encodedLen;
5256 pInfo->PublicKey.cUnusedBits = 0;
5257 ret = CryptEncodeObject(dwCertEncodingType,
5258 RSA_CSP_PUBLICKEYBLOB, pubKey,
5259 pInfo->PublicKey.pbData, &pInfo->PublicKey.cbData);
5263 CryptMemFree(pubKey);
5268 CryptDestroyKey(key);
5273 typedef BOOL (WINAPI *ExportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
5274 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
5275 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo);
5277 BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
5278 DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags,
5279 void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
5281 static HCRYPTOIDFUNCSET set = NULL;
5283 ExportPublicKeyInfoExFunc exportFunc = NULL;
5284 HCRYPTOIDFUNCADDR hFunc = NULL;
5286 TRACE("(%ld, %ld, %08lx, %s, %08lx, %p, %p, %p)\n", hCryptProv, dwKeySpec,
5287 dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags, pvAuxInfo,
5292 SetLastError(ERROR_INVALID_PARAMETER);
5296 if (pszPublicKeyObjId)
5299 set = CryptInitOIDFunctionSet(CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC,
5301 CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId,
5302 0, (void **)&exportFunc, &hFunc);
5305 exportFunc = CRYPT_ExportRsaPublicKeyInfoEx;
5306 ret = exportFunc(hCryptProv, dwKeySpec, dwCertEncodingType,
5307 pszPublicKeyObjId, dwFlags, pvAuxInfo, pInfo, pcbInfo);
5309 CryptFreeOIDFunctionAddress(hFunc, 0);
5313 BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
5314 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
5316 return CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo,
5320 static BOOL WINAPI CRYPT_ImportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
5321 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5322 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
5325 DWORD pubKeySize = 0;
5327 TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv,
5328 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
5330 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
5331 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize);
5334 LPBYTE pubKey = CryptMemAlloc(pubKeySize);
5338 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
5339 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, pubKey,
5342 ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0,
5344 CryptMemFree(pubKey);
5352 typedef BOOL (WINAPI *ImportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
5353 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5354 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey);
5356 BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
5357 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5358 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
5360 static HCRYPTOIDFUNCSET set = NULL;
5362 ImportPublicKeyInfoExFunc importFunc = NULL;
5363 HCRYPTOIDFUNCADDR hFunc = NULL;
5365 TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv,
5366 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
5369 set = CryptInitOIDFunctionSet(CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC, 0);
5370 CryptGetOIDFunctionAddress(set, dwCertEncodingType,
5371 pInfo->Algorithm.pszObjId, 0, (void **)&importFunc, &hFunc);
5373 importFunc = CRYPT_ImportRsaPublicKeyInfoEx;
5374 ret = importFunc(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags,
5377 CryptFreeOIDFunctionAddress(hFunc, 0);