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);
3558 TRACE("attr->pszObjId is %p (%s)\n", attr->pszObjId,
3559 debugstr_a(attr->pszObjId));
3560 TRACE("attr->dwValueType is %ld\n", attr->dwValueType);
3562 TRACE("returning %d (%08lx)\n", ret, GetLastError());
3566 SetLastError(STATUS_ACCESS_VIOLATION);
3573 static BOOL WINAPI CRYPT_AsnDecodeRdn(DWORD dwCertEncodingType,
3574 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3575 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3581 struct AsnArrayDescriptor arrayDesc = { ASN_CONSTRUCTOR | ASN_SETOF,
3582 CRYPT_AsnDecodeRdnAttr, sizeof(CERT_RDN_ATTR), TRUE,
3583 offsetof(CERT_RDN_ATTR, pszObjId) };
3584 PCERT_RDN rdn = (PCERT_RDN)pvStructInfo;
3586 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3587 pDecodePara, pvStructInfo, pcbStructInfo, rdn ? rdn->rgRDNAttr : NULL);
3591 SetLastError(STATUS_ACCESS_VIOLATION);
3598 static BOOL WINAPI CRYPT_AsnDecodeName(DWORD dwCertEncodingType,
3599 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3600 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3606 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3607 CRYPT_AsnDecodeRdn, sizeof(CERT_RDN), TRUE,
3608 offsetof(CERT_RDN, rgRDNAttr) };
3610 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3611 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3615 SetLastError(STATUS_ACCESS_VIOLATION);
3622 static BOOL WINAPI CRYPT_AsnDecodeCopyBytes(DWORD dwCertEncodingType,
3623 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3624 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3627 DWORD bytesNeeded = sizeof(CRYPT_OBJID_BLOB);
3629 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3630 pDecodePara, pvStructInfo, *pcbStructInfo);
3632 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
3633 bytesNeeded += cbEncoded;
3635 *pcbStructInfo = bytesNeeded;
3636 else if (*pcbStructInfo < bytesNeeded)
3638 SetLastError(ERROR_MORE_DATA);
3639 *pcbStructInfo = bytesNeeded;
3644 PCRYPT_OBJID_BLOB blob = (PCRYPT_OBJID_BLOB)pvStructInfo;
3646 *pcbStructInfo = bytesNeeded;
3647 blob->cbData = cbEncoded;
3648 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
3649 blob->pbData = (LPBYTE)pbEncoded;
3652 assert(blob->pbData);
3653 memcpy(blob->pbData, pbEncoded, blob->cbData);
3659 static BOOL WINAPI CRYPT_AsnDecodeAlgorithmId(DWORD dwCertEncodingType,
3660 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3661 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3663 CRYPT_ALGORITHM_IDENTIFIER *algo =
3664 (CRYPT_ALGORITHM_IDENTIFIER *)pvStructInfo;
3666 struct AsnDecodeSequenceItem items[] = {
3667 { ASN_OBJECTIDENTIFIER, offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId),
3668 CRYPT_AsnDecodeOidWrapper, sizeof(LPSTR), FALSE, TRUE,
3669 offsetof(CRYPT_ALGORITHM_IDENTIFIER, pszObjId), 0 },
3670 { 0, offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters),
3671 CRYPT_AsnDecodeCopyBytes, sizeof(CRYPT_OBJID_BLOB), TRUE, TRUE,
3672 offsetof(CRYPT_ALGORITHM_IDENTIFIER, Parameters.pbData), 0 },
3675 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3676 pDecodePara, pvStructInfo, *pcbStructInfo);
3678 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3679 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3680 pDecodePara, pvStructInfo, pcbStructInfo, algo ? algo->pszObjId : NULL);
3681 if (ret && pvStructInfo)
3683 TRACE("pszObjId is %p (%s)\n", algo->pszObjId,
3684 debugstr_a(algo->pszObjId));
3689 static BOOL WINAPI CRYPT_AsnDecodePubKeyInfo(DWORD dwCertEncodingType,
3690 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3691 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3697 struct AsnDecodeSequenceItem items[] = {
3698 { ASN_SEQUENCEOF, offsetof(CERT_PUBLIC_KEY_INFO, Algorithm),
3699 CRYPT_AsnDecodeAlgorithmId, sizeof(CRYPT_ALGORITHM_IDENTIFIER),
3700 FALSE, TRUE, offsetof(CERT_PUBLIC_KEY_INFO,
3701 Algorithm.pszObjId) },
3702 { ASN_BITSTRING, offsetof(CERT_PUBLIC_KEY_INFO, PublicKey),
3703 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), FALSE, TRUE,
3704 offsetof(CERT_PUBLIC_KEY_INFO, PublicKey.pbData) },
3707 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3708 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3709 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3713 SetLastError(STATUS_ACCESS_VIOLATION);
3720 static BOOL WINAPI CRYPT_AsnDecodeBool(DWORD dwCertEncodingType,
3721 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3722 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3728 SetLastError(CRYPT_E_ASN1_CORRUPT);
3731 if (GET_LEN_BYTES(pbEncoded[1]) > 1)
3733 SetLastError(CRYPT_E_ASN1_CORRUPT);
3736 if (pbEncoded[1] > 1)
3738 SetLastError(CRYPT_E_ASN1_CORRUPT);
3743 *pcbStructInfo = sizeof(BOOL);
3746 else if (*pcbStructInfo < sizeof(BOOL))
3748 *pcbStructInfo = sizeof(BOOL);
3749 SetLastError(ERROR_MORE_DATA);
3754 *(BOOL *)pvStructInfo = pbEncoded[2] ? TRUE : FALSE;
3757 TRACE("returning %d (%08lx)\n", ret, GetLastError());
3761 static BOOL WINAPI CRYPT_AsnDecodeAltNameEntry(DWORD dwCertEncodingType,
3762 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3763 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3765 PCERT_ALT_NAME_ENTRY entry = (PCERT_ALT_NAME_ENTRY)pvStructInfo;
3766 DWORD dataLen, lenBytes, bytesNeeded = sizeof(CERT_ALT_NAME_ENTRY);
3769 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3770 pDecodePara, pvStructInfo, *pcbStructInfo);
3774 SetLastError(CRYPT_E_ASN1_CORRUPT);
3777 if ((pbEncoded[0] & ASN_FLAGS_MASK) != ASN_CONTEXT)
3779 SetLastError(CRYPT_E_ASN1_BADTAG);
3782 lenBytes = GET_LEN_BYTES(pbEncoded[1]);
3783 if (1 + lenBytes > cbEncoded)
3785 SetLastError(CRYPT_E_ASN1_CORRUPT);
3788 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
3790 switch (pbEncoded[0] & ASN_TYPE_MASK)
3792 case 1: /* rfc822Name */
3793 case 2: /* dNSName */
3794 case 6: /* uniformResourceIdentifier */
3795 bytesNeeded += (dataLen + 1) * sizeof(WCHAR);
3797 case 7: /* iPAddress */
3798 bytesNeeded += dataLen;
3800 case 8: /* registeredID */
3801 /* FIXME: decode as OID */
3802 case 0: /* otherName */
3803 case 4: /* directoryName */
3805 SetLastError(CRYPT_E_ASN1_BADTAG);
3808 case 3: /* x400Address, unimplemented */
3809 case 5: /* ediPartyName, unimplemented */
3810 SetLastError(CRYPT_E_ASN1_BADTAG);
3814 SetLastError(CRYPT_E_ASN1_CORRUPT);
3820 *pcbStructInfo = bytesNeeded;
3821 else if (*pcbStructInfo < bytesNeeded)
3823 *pcbStructInfo = bytesNeeded;
3824 SetLastError(ERROR_MORE_DATA);
3829 *pcbStructInfo = bytesNeeded;
3830 /* MS used values one greater than the asn1 ones.. sigh */
3831 entry->dwAltNameChoice = (pbEncoded[0] & 0x7f) + 1;
3832 switch (pbEncoded[0] & ASN_TYPE_MASK)
3834 case 1: /* rfc822Name */
3835 case 2: /* dNSName */
3836 case 6: /* uniformResourceIdentifier */
3840 for (i = 0; i < dataLen; i++)
3841 entry->u.pwszURL[i] =
3842 (WCHAR)pbEncoded[1 + lenBytes + i];
3843 entry->u.pwszURL[i] = 0;
3844 TRACE("URL is %p (%s)\n", entry->u.pwszURL,
3845 debugstr_w(entry->u.pwszURL));
3848 case 7: /* iPAddress */
3849 /* The next data pointer is in the pwszURL spot, that is,
3850 * the first 4 bytes. Need to move it to the next spot.
3852 entry->u.IPAddress.pbData = (LPBYTE)entry->u.pwszURL;
3853 entry->u.IPAddress.cbData = dataLen;
3854 memcpy(entry->u.IPAddress.pbData, pbEncoded + 1 + lenBytes,
3864 static BOOL WINAPI CRYPT_AsnDecodeAltNameInternal(DWORD dwCertEncodingType,
3865 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3866 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3869 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3870 CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
3871 offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
3872 PCERT_ALT_NAME_INFO info = (PCERT_ALT_NAME_INFO)pvStructInfo;
3874 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3875 pDecodePara, pvStructInfo, *pcbStructInfo);
3878 TRACE("info->rgAltEntry is %p\n", info->rgAltEntry);
3879 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3880 pDecodePara, pvStructInfo, pcbStructInfo, info ? info->rgAltEntry : NULL);
3884 static BOOL WINAPI CRYPT_AsnDecodeAltName(DWORD dwCertEncodingType,
3885 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3886 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3890 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3891 pDecodePara, pvStructInfo, *pcbStructInfo);
3895 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
3896 CRYPT_AsnDecodeAltNameEntry, sizeof(CERT_ALT_NAME_ENTRY), TRUE,
3897 offsetof(CERT_ALT_NAME_ENTRY, u.pwszURL) };
3899 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
3900 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3904 SetLastError(STATUS_ACCESS_VIOLATION);
3911 struct PATH_LEN_CONSTRAINT
3913 BOOL fPathLenConstraint;
3914 DWORD dwPathLenConstraint;
3917 static BOOL WINAPI CRYPT_AsnDecodePathLenConstraint(DWORD dwCertEncodingType,
3918 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3919 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3923 TRACE("%p, %ld, %08lx, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
3924 pvStructInfo, *pcbStructInfo);
3928 if (pbEncoded[0] == ASN_INTEGER)
3930 DWORD bytesNeeded = sizeof(struct PATH_LEN_CONSTRAINT);
3933 *pcbStructInfo = bytesNeeded;
3934 else if (*pcbStructInfo < bytesNeeded)
3936 SetLastError(ERROR_MORE_DATA);
3937 *pcbStructInfo = bytesNeeded;
3942 struct PATH_LEN_CONSTRAINT *constraint =
3943 (struct PATH_LEN_CONSTRAINT *)pvStructInfo;
3944 DWORD size = sizeof(constraint->dwPathLenConstraint);
3946 ret = CRYPT_AsnDecodeInt(dwCertEncodingType, X509_INTEGER,
3947 pbEncoded, cbEncoded, 0, NULL,
3948 &constraint->dwPathLenConstraint, &size);
3950 constraint->fPathLenConstraint = TRUE;
3951 TRACE("got an int, dwPathLenConstraint is %ld\n",
3952 constraint->dwPathLenConstraint);
3957 SetLastError(CRYPT_E_ASN1_CORRUPT);
3961 TRACE("returning %d (%08lx)\n", ret, GetLastError());
3965 static BOOL WINAPI CRYPT_AsnDecodeBasicConstraints2(DWORD dwCertEncodingType,
3966 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
3967 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
3973 struct AsnDecodeSequenceItem items[] = {
3974 { ASN_BOOL, offsetof(CERT_BASIC_CONSTRAINTS2_INFO, fCA),
3975 CRYPT_AsnDecodeBool, sizeof(BOOL), TRUE, FALSE, 0, 0 },
3976 { ASN_INTEGER, offsetof(CERT_BASIC_CONSTRAINTS2_INFO,
3977 fPathLenConstraint), CRYPT_AsnDecodePathLenConstraint,
3978 sizeof(struct PATH_LEN_CONSTRAINT), TRUE, FALSE, 0, 0 },
3981 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
3982 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded, dwFlags,
3983 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
3987 SetLastError(STATUS_ACCESS_VIOLATION);
3994 #define RSA1_MAGIC 0x31415352
3996 struct DECODED_RSA_PUB_KEY
3999 CRYPT_INTEGER_BLOB modulus;
4002 static BOOL WINAPI CRYPT_AsnDecodeRsaPubKey(DWORD dwCertEncodingType,
4003 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4004 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4010 struct AsnDecodeSequenceItem items[] = {
4011 { ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, modulus),
4012 CRYPT_AsnDecodeUnsignedIntegerInternal, sizeof(CRYPT_INTEGER_BLOB),
4013 FALSE, TRUE, offsetof(struct DECODED_RSA_PUB_KEY, modulus.pbData),
4015 { ASN_INTEGER, offsetof(struct DECODED_RSA_PUB_KEY, pubexp),
4016 CRYPT_AsnDecodeInt, sizeof(DWORD), FALSE, FALSE, 0, 0 },
4018 struct DECODED_RSA_PUB_KEY *decodedKey = NULL;
4021 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
4022 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
4023 CRYPT_DECODE_ALLOC_FLAG, NULL, &decodedKey, &size, NULL);
4026 DWORD bytesNeeded = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
4027 decodedKey->modulus.cbData;
4031 *pcbStructInfo = bytesNeeded;
4034 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4035 pvStructInfo, pcbStructInfo, bytesNeeded)))
4038 RSAPUBKEY *rsaPubKey;
4040 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4041 pvStructInfo = *(BYTE **)pvStructInfo;
4042 hdr = (BLOBHEADER *)pvStructInfo;
4043 hdr->bType = PUBLICKEYBLOB;
4044 hdr->bVersion = CUR_BLOB_VERSION;
4046 hdr->aiKeyAlg = CALG_RSA_KEYX;
4047 rsaPubKey = (RSAPUBKEY *)((BYTE *)pvStructInfo +
4048 sizeof(BLOBHEADER));
4049 rsaPubKey->magic = RSA1_MAGIC;
4050 rsaPubKey->pubexp = decodedKey->pubexp;
4051 rsaPubKey->bitlen = decodedKey->modulus.cbData * 8;
4052 memcpy((BYTE *)pvStructInfo + sizeof(BLOBHEADER) +
4053 sizeof(RSAPUBKEY), decodedKey->modulus.pbData,
4054 decodedKey->modulus.cbData);
4056 LocalFree(decodedKey);
4061 SetLastError(STATUS_ACCESS_VIOLATION);
4068 static BOOL WINAPI CRYPT_AsnDecodeOctetsInternal(DWORD dwCertEncodingType,
4069 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4070 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4073 DWORD bytesNeeded, dataLen;
4075 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
4076 pDecodePara, pvStructInfo, *pcbStructInfo);
4078 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4080 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4081 bytesNeeded = sizeof(CRYPT_DATA_BLOB);
4083 bytesNeeded = dataLen + sizeof(CRYPT_DATA_BLOB);
4085 *pcbStructInfo = bytesNeeded;
4086 else if (*pcbStructInfo < bytesNeeded)
4088 SetLastError(ERROR_MORE_DATA);
4089 *pcbStructInfo = bytesNeeded;
4094 CRYPT_DATA_BLOB *blob;
4095 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4097 blob = (CRYPT_DATA_BLOB *)pvStructInfo;
4098 blob->cbData = dataLen;
4099 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4100 blob->pbData = (BYTE *)pbEncoded + 1 + lenBytes;
4103 assert(blob->pbData);
4105 memcpy(blob->pbData, pbEncoded + 1 + lenBytes,
4113 static BOOL WINAPI CRYPT_AsnDecodeOctets(DWORD dwCertEncodingType,
4114 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4115 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4119 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
4120 pDecodePara, pvStructInfo, *pcbStructInfo);
4128 SetLastError(CRYPT_E_ASN1_CORRUPT);
4131 else if (pbEncoded[0] != ASN_OCTETSTRING)
4133 SetLastError(CRYPT_E_ASN1_BADTAG);
4136 else if ((ret = CRYPT_AsnDecodeOctetsInternal(dwCertEncodingType,
4137 lpszStructType, pbEncoded, cbEncoded,
4138 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4141 *pcbStructInfo = bytesNeeded;
4142 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4143 pvStructInfo, pcbStructInfo, bytesNeeded)))
4145 CRYPT_DATA_BLOB *blob;
4147 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4148 pvStructInfo = *(BYTE **)pvStructInfo;
4149 blob = (CRYPT_DATA_BLOB *)pvStructInfo;
4150 blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_DATA_BLOB);
4151 ret = CRYPT_AsnDecodeOctetsInternal(dwCertEncodingType,
4152 lpszStructType, pbEncoded, cbEncoded,
4153 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
4160 SetLastError(STATUS_ACCESS_VIOLATION);
4167 static BOOL WINAPI CRYPT_AsnDecodeBitsInternal(DWORD dwCertEncodingType,
4168 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4169 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4173 TRACE("(%p, %ld, 0x%08lx, %p, %p, %ld)\n", pbEncoded, cbEncoded, dwFlags,
4174 pDecodePara, pvStructInfo, *pcbStructInfo);
4176 if (pbEncoded[0] == ASN_BITSTRING)
4178 DWORD bytesNeeded, dataLen;
4180 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4182 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4183 bytesNeeded = sizeof(CRYPT_BIT_BLOB);
4185 bytesNeeded = dataLen - 1 + sizeof(CRYPT_BIT_BLOB);
4187 *pcbStructInfo = bytesNeeded;
4188 else if (*pcbStructInfo < bytesNeeded)
4190 *pcbStructInfo = bytesNeeded;
4191 SetLastError(ERROR_MORE_DATA);
4196 CRYPT_BIT_BLOB *blob;
4198 blob = (CRYPT_BIT_BLOB *)pvStructInfo;
4199 blob->cbData = dataLen - 1;
4200 blob->cUnusedBits = *(pbEncoded + 1 +
4201 GET_LEN_BYTES(pbEncoded[1]));
4202 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4204 blob->pbData = (BYTE *)pbEncoded + 2 +
4205 GET_LEN_BYTES(pbEncoded[1]);
4209 assert(blob->pbData);
4212 BYTE mask = 0xff << blob->cUnusedBits;
4214 memcpy(blob->pbData, pbEncoded + 2 +
4215 GET_LEN_BYTES(pbEncoded[1]), blob->cbData);
4216 blob->pbData[blob->cbData - 1] &= mask;
4224 SetLastError(CRYPT_E_ASN1_BADTAG);
4227 TRACE("returning %d (%08lx)\n", ret, GetLastError());
4231 static BOOL WINAPI CRYPT_AsnDecodeBits(DWORD dwCertEncodingType,
4232 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4233 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4237 TRACE("(%p, %ld, 0x%08lx, %p, %p, %p)\n", pbEncoded, cbEncoded, dwFlags,
4238 pDecodePara, pvStructInfo, pcbStructInfo);
4244 if ((ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType,
4245 lpszStructType, pbEncoded, cbEncoded,
4246 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4249 *pcbStructInfo = bytesNeeded;
4250 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4251 pvStructInfo, pcbStructInfo, bytesNeeded)))
4253 CRYPT_BIT_BLOB *blob;
4255 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4256 pvStructInfo = *(BYTE **)pvStructInfo;
4257 blob = (CRYPT_BIT_BLOB *)pvStructInfo;
4258 blob->pbData = (BYTE *)pvStructInfo + sizeof(CRYPT_BIT_BLOB);
4259 ret = CRYPT_AsnDecodeBitsInternal(dwCertEncodingType,
4260 lpszStructType, pbEncoded, cbEncoded,
4261 dwFlags & ~CRYPT_DECODE_ALLOC_FLAG, NULL, pvStructInfo,
4268 SetLastError(STATUS_ACCESS_VIOLATION);
4272 TRACE("returning %d (%08lx)\n", ret, GetLastError());
4276 static BOOL WINAPI CRYPT_AsnDecodeInt(DWORD dwCertEncodingType,
4277 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4278 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4284 *pcbStructInfo = sizeof(int);
4289 BYTE buf[sizeof(CRYPT_INTEGER_BLOB) + sizeof(int)];
4290 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)buf;
4291 DWORD size = sizeof(buf);
4293 blob->pbData = buf + sizeof(CRYPT_INTEGER_BLOB);
4294 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
4295 X509_MULTI_BYTE_INTEGER, pbEncoded, cbEncoded, 0, NULL, &buf, &size);
4298 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4299 pvStructInfo, pcbStructInfo, sizeof(int))))
4303 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4304 pvStructInfo = *(BYTE **)pvStructInfo;
4305 if (blob->pbData[blob->cbData - 1] & 0x80)
4307 /* initialize to a negative value to sign-extend */
4312 for (i = 0; i < blob->cbData; i++)
4315 val |= blob->pbData[blob->cbData - i - 1];
4317 memcpy(pvStructInfo, &val, sizeof(int));
4320 else if (GetLastError() == ERROR_MORE_DATA)
4321 SetLastError(CRYPT_E_ASN1_LARGE);
4325 SetLastError(STATUS_ACCESS_VIOLATION);
4332 static BOOL WINAPI CRYPT_AsnDecodeIntegerInternal(DWORD dwCertEncodingType,
4333 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4334 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4338 if (pbEncoded[0] == ASN_INTEGER)
4340 DWORD bytesNeeded, dataLen;
4342 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4344 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4346 bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
4348 *pcbStructInfo = bytesNeeded;
4349 else if (*pcbStructInfo < bytesNeeded)
4351 *pcbStructInfo = bytesNeeded;
4352 SetLastError(ERROR_MORE_DATA);
4357 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4359 blob->cbData = dataLen;
4360 assert(blob->pbData);
4365 for (i = 0; i < blob->cbData; i++)
4367 blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
4376 SetLastError(CRYPT_E_ASN1_BADTAG);
4382 static BOOL WINAPI CRYPT_AsnDecodeInteger(DWORD dwCertEncodingType,
4383 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4384 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4392 if ((ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
4393 lpszStructType, pbEncoded, cbEncoded,
4394 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4397 *pcbStructInfo = bytesNeeded;
4398 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4399 pvStructInfo, pcbStructInfo, bytesNeeded)))
4401 CRYPT_INTEGER_BLOB *blob;
4403 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4404 pvStructInfo = *(BYTE **)pvStructInfo;
4405 blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4406 blob->pbData = (BYTE *)pvStructInfo +
4407 sizeof(CRYPT_INTEGER_BLOB);
4408 ret = CRYPT_AsnDecodeIntegerInternal(dwCertEncodingType,
4409 lpszStructType, pbEncoded, cbEncoded,
4410 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pvStructInfo,
4417 SetLastError(STATUS_ACCESS_VIOLATION);
4424 static BOOL WINAPI CRYPT_AsnDecodeUnsignedIntegerInternal(
4425 DWORD dwCertEncodingType, LPCSTR lpszStructType, const BYTE *pbEncoded,
4426 DWORD cbEncoded, DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
4427 void *pvStructInfo, DWORD *pcbStructInfo)
4431 if (pbEncoded[0] == ASN_INTEGER)
4433 DWORD bytesNeeded, dataLen;
4435 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4437 BYTE lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4439 bytesNeeded = dataLen + sizeof(CRYPT_INTEGER_BLOB);
4441 *pcbStructInfo = bytesNeeded;
4442 else if (*pcbStructInfo < bytesNeeded)
4444 *pcbStructInfo = bytesNeeded;
4445 SetLastError(ERROR_MORE_DATA);
4450 CRYPT_INTEGER_BLOB *blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4452 blob->cbData = dataLen;
4453 assert(blob->pbData);
4454 /* remove leading zero byte if it exists */
4455 if (blob->cbData && *(pbEncoded + 1 + lenBytes) == 0)
4464 for (i = 0; i < blob->cbData; i++)
4466 blob->pbData[i] = *(pbEncoded + 1 + lenBytes +
4475 SetLastError(CRYPT_E_ASN1_BADTAG);
4481 static BOOL WINAPI CRYPT_AsnDecodeUnsignedInteger(DWORD dwCertEncodingType,
4482 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4483 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4491 if ((ret = CRYPT_AsnDecodeUnsignedIntegerInternal(dwCertEncodingType,
4492 lpszStructType, pbEncoded, cbEncoded,
4493 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &bytesNeeded)))
4496 *pcbStructInfo = bytesNeeded;
4497 else if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4498 pvStructInfo, pcbStructInfo, bytesNeeded)))
4500 CRYPT_INTEGER_BLOB *blob;
4502 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4503 pvStructInfo = *(BYTE **)pvStructInfo;
4504 blob = (CRYPT_INTEGER_BLOB *)pvStructInfo;
4505 blob->pbData = (BYTE *)pvStructInfo +
4506 sizeof(CRYPT_INTEGER_BLOB);
4507 ret = CRYPT_AsnDecodeUnsignedIntegerInternal(dwCertEncodingType,
4508 lpszStructType, pbEncoded, cbEncoded,
4509 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pvStructInfo,
4516 SetLastError(STATUS_ACCESS_VIOLATION);
4523 static BOOL WINAPI CRYPT_AsnDecodeEnumerated(DWORD dwCertEncodingType,
4524 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4525 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4531 *pcbStructInfo = sizeof(int);
4536 if (pbEncoded[0] == ASN_ENUMERATED)
4538 unsigned int val = 0, i;
4542 SetLastError(CRYPT_E_ASN1_EOD);
4545 else if (pbEncoded[1] == 0)
4547 SetLastError(CRYPT_E_ASN1_CORRUPT);
4552 /* A little strange looking, but we have to accept a sign byte:
4553 * 0xffffffff gets encoded as 0a 05 00 ff ff ff ff. Also,
4554 * assuming a small length is okay here, it has to be in short
4557 if (pbEncoded[1] > sizeof(unsigned int) + 1)
4559 SetLastError(CRYPT_E_ASN1_LARGE);
4562 for (i = 0; i < pbEncoded[1]; i++)
4565 val |= pbEncoded[2 + i];
4567 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4568 pvStructInfo, pcbStructInfo, sizeof(unsigned int))))
4570 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4571 pvStructInfo = *(BYTE **)pvStructInfo;
4572 memcpy(pvStructInfo, &val, sizeof(unsigned int));
4578 SetLastError(CRYPT_E_ASN1_BADTAG);
4584 SetLastError(STATUS_ACCESS_VIOLATION);
4591 /* Modifies word, pbEncoded, and len, and magically sets a value ret to FALSE
4594 #define CRYPT_TIME_GET_DIGITS(pbEncoded, len, numDigits, word) \
4599 for (i = 0; (len) > 0 && i < (numDigits); i++, (len)--) \
4601 if (!isdigit(*(pbEncoded))) \
4603 SetLastError(CRYPT_E_ASN1_CORRUPT); \
4609 (word) += *(pbEncoded)++ - '0'; \
4614 static BOOL CRYPT_AsnDecodeTimeZone(const BYTE *pbEncoded, DWORD len,
4615 SYSTEMTIME *sysTime)
4622 if (len >= 3 && (*pbEncoded == '+' || *pbEncoded == '-'))
4624 WORD hours, minutes = 0;
4625 BYTE sign = *pbEncoded++;
4628 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, hours);
4629 if (ret && hours >= 24)
4631 SetLastError(CRYPT_E_ASN1_CORRUPT);
4636 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, minutes);
4637 if (ret && minutes >= 60)
4639 SetLastError(CRYPT_E_ASN1_CORRUPT);
4647 sysTime->wHour += hours;
4648 sysTime->wMinute += minutes;
4652 if (hours > sysTime->wHour)
4655 sysTime->wHour = 24 - (hours - sysTime->wHour);
4658 sysTime->wHour -= hours;
4659 if (minutes > sysTime->wMinute)
4662 sysTime->wMinute = 60 - (minutes - sysTime->wMinute);
4665 sysTime->wMinute -= minutes;
4672 SetLastError(STATUS_ACCESS_VIOLATION);
4679 #define MIN_ENCODED_TIME_LENGTH 10
4681 static BOOL WINAPI CRYPT_AsnDecodeUtcTime(DWORD dwCertEncodingType,
4682 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4683 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4689 *pcbStructInfo = sizeof(FILETIME);
4695 if (pbEncoded[0] == ASN_UTCTIME)
4699 SetLastError(CRYPT_E_ASN1_EOD);
4702 else if (pbEncoded[1] > 0x7f)
4704 /* long-form date strings really can't be valid */
4705 SetLastError(CRYPT_E_ASN1_CORRUPT);
4710 SYSTEMTIME sysTime = { 0 };
4711 BYTE len = pbEncoded[1];
4713 if (len < MIN_ENCODED_TIME_LENGTH)
4715 SetLastError(CRYPT_E_ASN1_CORRUPT);
4721 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wYear);
4722 if (sysTime.wYear >= 50)
4723 sysTime.wYear += 1900;
4725 sysTime.wYear += 2000;
4726 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
4727 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
4728 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
4729 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMinute);
4732 if (len >= 2 && isdigit(*pbEncoded) &&
4733 isdigit(*(pbEncoded + 1)))
4734 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
4736 else if (isdigit(*pbEncoded))
4737 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 1,
4740 ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
4743 if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags,
4744 pDecodePara, pvStructInfo, pcbStructInfo,
4747 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4748 pvStructInfo = *(BYTE **)pvStructInfo;
4749 ret = SystemTimeToFileTime(&sysTime,
4750 (FILETIME *)pvStructInfo);
4757 SetLastError(CRYPT_E_ASN1_BADTAG);
4763 SetLastError(STATUS_ACCESS_VIOLATION);
4770 static BOOL WINAPI CRYPT_AsnDecodeGeneralizedTime(DWORD dwCertEncodingType,
4771 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4772 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4778 *pcbStructInfo = sizeof(FILETIME);
4784 if (pbEncoded[0] == ASN_GENERALTIME)
4788 SetLastError(CRYPT_E_ASN1_EOD);
4791 else if (pbEncoded[1] > 0x7f)
4793 /* long-form date strings really can't be valid */
4794 SetLastError(CRYPT_E_ASN1_CORRUPT);
4799 BYTE len = pbEncoded[1];
4801 if (len < MIN_ENCODED_TIME_LENGTH)
4803 SetLastError(CRYPT_E_ASN1_CORRUPT);
4808 SYSTEMTIME sysTime = { 0 };
4811 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 4, sysTime.wYear);
4812 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wMonth);
4813 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wDay);
4814 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2, sysTime.wHour);
4817 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
4820 CRYPT_TIME_GET_DIGITS(pbEncoded, len, 2,
4822 if (ret && len > 0 && (*pbEncoded == '.' ||
4829 /* workaround macro weirdness */
4830 digits = min(len, 3);
4831 CRYPT_TIME_GET_DIGITS(pbEncoded, len, digits,
4832 sysTime.wMilliseconds);
4835 ret = CRYPT_AsnDecodeTimeZone(pbEncoded, len,
4838 if (ret && (ret = CRYPT_DecodeEnsureSpace(dwFlags,
4839 pDecodePara, pvStructInfo, pcbStructInfo,
4842 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4843 pvStructInfo = *(BYTE **)pvStructInfo;
4844 ret = SystemTimeToFileTime(&sysTime,
4845 (FILETIME *)pvStructInfo);
4852 SetLastError(CRYPT_E_ASN1_BADTAG);
4858 SetLastError(STATUS_ACCESS_VIOLATION);
4865 static BOOL WINAPI CRYPT_AsnDecodeChoiceOfTime(DWORD dwCertEncodingType,
4866 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4867 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4873 if (pbEncoded[0] == ASN_UTCTIME)
4874 ret = CRYPT_AsnDecodeUtcTime(dwCertEncodingType, lpszStructType,
4875 pbEncoded, cbEncoded, dwFlags, pDecodePara, pvStructInfo,
4877 else if (pbEncoded[0] == ASN_GENERALTIME)
4878 ret = CRYPT_AsnDecodeGeneralizedTime(dwCertEncodingType,
4879 lpszStructType, pbEncoded, cbEncoded, dwFlags, pDecodePara,
4880 pvStructInfo, pcbStructInfo);
4883 SetLastError(CRYPT_E_ASN1_BADTAG);
4889 SetLastError(STATUS_ACCESS_VIOLATION);
4896 static BOOL WINAPI CRYPT_AsnDecodeSequenceOfAny(DWORD dwCertEncodingType,
4897 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
4898 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
4904 if (pbEncoded[0] == ASN_SEQUENCEOF)
4906 DWORD bytesNeeded, dataLen, remainingLen, cValue;
4908 if ((ret = CRYPT_GetLen(pbEncoded, cbEncoded, &dataLen)))
4913 lenBytes = GET_LEN_BYTES(pbEncoded[1]);
4914 bytesNeeded = sizeof(CRYPT_SEQUENCE_OF_ANY);
4916 ptr = pbEncoded + 1 + lenBytes;
4917 remainingLen = dataLen;
4918 while (ret && remainingLen)
4922 ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
4925 DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
4927 remainingLen -= 1 + nextLenBytes + nextLen;
4928 ptr += 1 + nextLenBytes + nextLen;
4929 bytesNeeded += sizeof(CRYPT_DER_BLOB);
4930 if (!(dwFlags & CRYPT_DECODE_NOCOPY_FLAG))
4931 bytesNeeded += 1 + nextLenBytes + nextLen;
4937 CRYPT_SEQUENCE_OF_ANY *seq;
4941 if ((ret = CRYPT_DecodeEnsureSpace(dwFlags, pDecodePara,
4942 pvStructInfo, pcbStructInfo, bytesNeeded)))
4944 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG)
4945 pvStructInfo = *(BYTE **)pvStructInfo;
4946 seq = (CRYPT_SEQUENCE_OF_ANY *)pvStructInfo;
4947 seq->cValue = cValue;
4948 seq->rgValue = (CRYPT_DER_BLOB *)((BYTE *)seq +
4950 nextPtr = (BYTE *)seq->rgValue +
4951 cValue * sizeof(CRYPT_DER_BLOB);
4952 ptr = pbEncoded + 1 + lenBytes;
4953 remainingLen = dataLen;
4955 while (ret && remainingLen)
4959 ret = CRYPT_GetLen(ptr, remainingLen, &nextLen);
4962 DWORD nextLenBytes = GET_LEN_BYTES(ptr[1]);
4964 seq->rgValue[i].cbData = 1 + nextLenBytes +
4966 if (dwFlags & CRYPT_DECODE_NOCOPY_FLAG)
4967 seq->rgValue[i].pbData = (BYTE *)ptr;
4970 seq->rgValue[i].pbData = nextPtr;
4971 memcpy(nextPtr, ptr, 1 + nextLenBytes +
4973 nextPtr += 1 + nextLenBytes + nextLen;
4975 remainingLen -= 1 + nextLenBytes + nextLen;
4976 ptr += 1 + nextLenBytes + nextLen;
4986 SetLastError(CRYPT_E_ASN1_BADTAG);
4992 SetLastError(STATUS_ACCESS_VIOLATION);
4999 static BOOL WINAPI CRYPT_AsnDecodeDistPoint(DWORD dwCertEncodingType,
5000 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
5001 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
5003 struct AsnDecodeSequenceItem items[] = {
5004 { ASN_CONTEXT | ASN_CONSTRUCTOR | 0, offsetof(CRL_DIST_POINT,
5005 DistPointName), CRYPT_AsnDecodeAltNameInternal,
5006 sizeof(CRL_DIST_POINT_NAME), TRUE, TRUE, offsetof(CRL_DIST_POINT,
5007 DistPointName.u.FullName.rgAltEntry), 0 },
5008 { ASN_CONTEXT | 1, offsetof(CRL_DIST_POINT, ReasonFlags),
5009 CRYPT_AsnDecodeBitsInternal, sizeof(CRYPT_BIT_BLOB), TRUE, TRUE,
5010 offsetof(CRL_DIST_POINT, ReasonFlags.pbData), 0 },
5011 { ASN_CONTEXT | ASN_CONSTRUCTOR | 2, offsetof(CRL_DIST_POINT, CRLIssuer),
5012 CRYPT_AsnDecodeAltNameInternal, sizeof(CERT_ALT_NAME_INFO), TRUE, TRUE,
5013 offsetof(CRL_DIST_POINT, CRLIssuer.rgAltEntry), 0 },
5017 ret = CRYPT_AsnDecodeSequence(dwCertEncodingType, items,
5018 sizeof(items) / sizeof(items[0]), pbEncoded, cbEncoded,
5019 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo, NULL);
5023 static BOOL WINAPI CRYPT_AsnDecodeCRLDistPoints(DWORD dwCertEncodingType,
5024 LPCSTR lpszStructType, const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
5025 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
5029 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", pbEncoded, cbEncoded, dwFlags,
5030 pDecodePara, pvStructInfo, *pcbStructInfo);
5034 struct AsnArrayDescriptor arrayDesc = { ASN_SEQUENCEOF,
5035 CRYPT_AsnDecodeDistPoint, sizeof(CRL_DIST_POINT), TRUE,
5036 offsetof(CRL_DIST_POINT, DistPointName.u.FullName.rgAltEntry) };
5038 ret = CRYPT_AsnDecodeArray(&arrayDesc, pbEncoded, cbEncoded, dwFlags,
5039 pDecodePara, pvStructInfo, pcbStructInfo, NULL);
5043 SetLastError(STATUS_ACCESS_VIOLATION);
5050 BOOL WINAPI CryptDecodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
5051 const BYTE *pbEncoded, DWORD cbEncoded, DWORD dwFlags,
5052 PCRYPT_DECODE_PARA pDecodePara, void *pvStructInfo, DWORD *pcbStructInfo)
5054 static HCRYPTOIDFUNCSET set = NULL;
5056 CryptDecodeObjectExFunc decodeFunc = NULL;
5057 HCRYPTOIDFUNCADDR hFunc = NULL;
5059 TRACE("(0x%08lx, %s, %p, %ld, 0x%08lx, %p, %p, %p)\n",
5060 dwCertEncodingType, debugstr_a(lpszStructType), pbEncoded,
5061 cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
5063 if (!pvStructInfo && !pcbStructInfo)
5065 SetLastError(ERROR_INVALID_PARAMETER);
5068 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
5069 && (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
5071 SetLastError(ERROR_FILE_NOT_FOUND);
5076 SetLastError(CRYPT_E_ASN1_EOD);
5079 if (cbEncoded > MAX_ENCODED_LEN)
5081 SetLastError(CRYPT_E_ASN1_LARGE);
5085 SetLastError(NOERROR);
5086 if (dwFlags & CRYPT_DECODE_ALLOC_FLAG && pvStructInfo)
5087 *(BYTE **)pvStructInfo = NULL;
5088 if (!HIWORD(lpszStructType))
5090 switch (LOWORD(lpszStructType))
5092 case (WORD)X509_CERT:
5093 decodeFunc = CRYPT_AsnDecodeCert;
5095 case (WORD)X509_CERT_TO_BE_SIGNED:
5096 decodeFunc = CRYPT_AsnDecodeCertInfo;
5098 case (WORD)X509_CERT_CRL_TO_BE_SIGNED:
5099 decodeFunc = CRYPT_AsnDecodeCRLInfo;
5101 case (WORD)X509_EXTENSIONS:
5102 decodeFunc = CRYPT_AsnDecodeExtensions;
5104 case (WORD)X509_NAME:
5105 decodeFunc = CRYPT_AsnDecodeName;
5107 case (WORD)X509_PUBLIC_KEY_INFO:
5108 decodeFunc = CRYPT_AsnDecodePubKeyInfo;
5110 case (WORD)X509_ALTERNATE_NAME:
5111 decodeFunc = CRYPT_AsnDecodeAltName;
5113 case (WORD)X509_BASIC_CONSTRAINTS2:
5114 decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
5116 case (WORD)RSA_CSP_PUBLICKEYBLOB:
5117 decodeFunc = CRYPT_AsnDecodeRsaPubKey;
5119 case (WORD)X509_OCTET_STRING:
5120 decodeFunc = CRYPT_AsnDecodeOctets;
5122 case (WORD)X509_BITS:
5123 case (WORD)X509_KEY_USAGE:
5124 decodeFunc = CRYPT_AsnDecodeBits;
5126 case (WORD)X509_INTEGER:
5127 decodeFunc = CRYPT_AsnDecodeInt;
5129 case (WORD)X509_MULTI_BYTE_INTEGER:
5130 decodeFunc = CRYPT_AsnDecodeInteger;
5132 case (WORD)X509_MULTI_BYTE_UINT:
5133 decodeFunc = CRYPT_AsnDecodeUnsignedInteger;
5135 case (WORD)X509_ENUMERATED:
5136 decodeFunc = CRYPT_AsnDecodeEnumerated;
5138 case (WORD)X509_CHOICE_OF_TIME:
5139 decodeFunc = CRYPT_AsnDecodeChoiceOfTime;
5141 case (WORD)X509_SEQUENCE_OF_ANY:
5142 decodeFunc = CRYPT_AsnDecodeSequenceOfAny;
5144 case (WORD)PKCS_UTC_TIME:
5145 decodeFunc = CRYPT_AsnDecodeUtcTime;
5147 case (WORD)X509_CRL_DIST_POINTS:
5148 decodeFunc = CRYPT_AsnDecodeCRLDistPoints;
5151 FIXME("%d: unimplemented\n", LOWORD(lpszStructType));
5154 else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
5155 decodeFunc = CRYPT_AsnDecodeExtensions;
5156 else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
5157 decodeFunc = CRYPT_AsnDecodeUtcTime;
5158 else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
5159 decodeFunc = CRYPT_AsnDecodeEnumerated;
5160 else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
5161 decodeFunc = CRYPT_AsnDecodeBits;
5162 else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
5163 decodeFunc = CRYPT_AsnDecodeOctets;
5164 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
5165 decodeFunc = CRYPT_AsnDecodeBasicConstraints2;
5166 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
5167 decodeFunc = CRYPT_AsnDecodeAltName;
5168 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
5169 decodeFunc = CRYPT_AsnDecodeAltName;
5170 else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
5171 decodeFunc = CRYPT_AsnDecodeAltName;
5172 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
5173 decodeFunc = CRYPT_AsnDecodeAltName;
5174 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
5175 decodeFunc = CRYPT_AsnDecodeAltName;
5177 TRACE("OID %s not found or unimplemented, looking for DLL\n",
5178 debugstr_a(lpszStructType));
5182 set = CryptInitOIDFunctionSet(CRYPT_OID_DECODE_OBJECT_EX_FUNC, 0);
5183 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
5184 (void **)&decodeFunc, &hFunc);
5187 ret = decodeFunc(dwCertEncodingType, lpszStructType, pbEncoded,
5188 cbEncoded, dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
5190 SetLastError(ERROR_FILE_NOT_FOUND);
5192 CryptFreeOIDFunctionAddress(hFunc, 0);
5196 BOOL WINAPI CryptExportPublicKeyInfo(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
5197 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
5199 return CryptExportPublicKeyInfoEx(hCryptProv, dwKeySpec, dwCertEncodingType,
5200 NULL, 0, NULL, pInfo, pcbInfo);
5203 static BOOL WINAPI CRYPT_ExportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
5204 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
5205 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
5210 TRACE("(%ld, %ld, %08lx, %s, %08lx, %p, %p, %p)\n", hCryptProv, dwKeySpec,
5211 dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags, pvAuxInfo,
5214 if (!pszPublicKeyObjId)
5215 pszPublicKeyObjId = szOID_RSA_RSA;
5216 if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key)))
5220 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, NULL, &keySize);
5223 LPBYTE pubKey = CryptMemAlloc(keySize);
5227 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, pubKey,
5231 DWORD encodedLen = 0;
5233 ret = CryptEncodeObject(dwCertEncodingType,
5234 RSA_CSP_PUBLICKEYBLOB, pubKey, NULL, &encodedLen);
5237 DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) +
5238 strlen(pszPublicKeyObjId) + 1 + encodedLen;
5241 *pcbInfo = sizeNeeded;
5242 else if (*pcbInfo < sizeNeeded)
5244 SetLastError(ERROR_MORE_DATA);
5245 *pcbInfo = sizeNeeded;
5250 pInfo->Algorithm.pszObjId = (char *)pInfo +
5251 sizeof(CERT_PUBLIC_KEY_INFO);
5252 lstrcpyA(pInfo->Algorithm.pszObjId,
5254 pInfo->Algorithm.Parameters.cbData = 0;
5255 pInfo->Algorithm.Parameters.pbData = NULL;
5256 pInfo->PublicKey.pbData =
5257 (BYTE *)pInfo->Algorithm.pszObjId
5258 + lstrlenA(pInfo->Algorithm.pszObjId) + 1;
5259 pInfo->PublicKey.cbData = encodedLen;
5260 pInfo->PublicKey.cUnusedBits = 0;
5261 ret = CryptEncodeObject(dwCertEncodingType,
5262 RSA_CSP_PUBLICKEYBLOB, pubKey,
5263 pInfo->PublicKey.pbData, &pInfo->PublicKey.cbData);
5267 CryptMemFree(pubKey);
5272 CryptDestroyKey(key);
5277 typedef BOOL (WINAPI *ExportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
5278 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
5279 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo);
5281 BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
5282 DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags,
5283 void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
5285 static HCRYPTOIDFUNCSET set = NULL;
5287 ExportPublicKeyInfoExFunc exportFunc = NULL;
5288 HCRYPTOIDFUNCADDR hFunc = NULL;
5290 TRACE("(%ld, %ld, %08lx, %s, %08lx, %p, %p, %p)\n", hCryptProv, dwKeySpec,
5291 dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags, pvAuxInfo,
5296 SetLastError(ERROR_INVALID_PARAMETER);
5300 if (pszPublicKeyObjId)
5303 set = CryptInitOIDFunctionSet(CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC,
5305 CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId,
5306 0, (void **)&exportFunc, &hFunc);
5309 exportFunc = CRYPT_ExportRsaPublicKeyInfoEx;
5310 ret = exportFunc(hCryptProv, dwKeySpec, dwCertEncodingType,
5311 pszPublicKeyObjId, dwFlags, pvAuxInfo, pInfo, pcbInfo);
5313 CryptFreeOIDFunctionAddress(hFunc, 0);
5317 BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
5318 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
5320 return CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo,
5324 static BOOL WINAPI CRYPT_ImportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
5325 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5326 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
5329 DWORD pubKeySize = 0;
5331 TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv,
5332 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
5334 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
5335 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize);
5338 LPBYTE pubKey = CryptMemAlloc(pubKeySize);
5342 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
5343 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, pubKey,
5346 ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0,
5348 CryptMemFree(pubKey);
5356 typedef BOOL (WINAPI *ImportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
5357 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5358 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey);
5360 BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
5361 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5362 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
5364 static HCRYPTOIDFUNCSET set = NULL;
5366 ImportPublicKeyInfoExFunc importFunc = NULL;
5367 HCRYPTOIDFUNCADDR hFunc = NULL;
5369 TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv,
5370 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
5373 set = CryptInitOIDFunctionSet(CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC, 0);
5374 CryptGetOIDFunctionAddress(set, dwCertEncodingType,
5375 pInfo->Algorithm.pszObjId, 0, (void **)&importFunc, &hFunc);
5377 importFunc = CRYPT_ImportRsaPublicKeyInfoEx;
5378 ret = importFunc(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags,
5381 CryptFreeOIDFunctionAddress(hFunc, 0);