2 * Copyright 2005-2008 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * This file implements ASN.1 DER encoding of a limited set of types.
19 * It isn't a full ASN.1 implementation. Microsoft implements BER
20 * encoding of many of the basic types in msasn1.dll, but that interface isn't
21 * implemented, 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
30 * MSDN, especially "Constants for CryptEncodeObject and CryptDecodeObject"
34 #include "wine/port.h"
41 #define NONAMELESSUNION
47 #include "wine/debug.h"
48 #include "wine/exception.h"
49 #include "wine/unicode.h"
50 #include "crypt32_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(cryptasn);
53 WINE_DECLARE_DEBUG_CHANNEL(crypt);
55 typedef BOOL (WINAPI *CryptEncodeObjectFunc)(DWORD, LPCSTR, const void *,
58 /* Prototypes for built-in encoders. They follow the Ex style prototypes.
59 * The dwCertEncodingType and lpszStructType are ignored by the built-in
60 * functions, but the parameters are retained to simplify CryptEncodeObjectEx,
61 * since it must call functions in external DLLs that follow these signatures.
63 BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
64 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
65 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
66 static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
67 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
68 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
69 static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
70 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
71 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
72 static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
73 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
74 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
75 static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
76 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
77 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
78 static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
79 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
80 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
81 static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
82 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
83 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
84 static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
85 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
86 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
87 static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
88 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
89 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
90 static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
91 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
92 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
93 static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
94 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
95 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
96 static BOOL WINAPI CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType,
97 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
98 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
99 static BOOL WINAPI CRYPT_AsnEncodePKCSAttributes(DWORD dwCertEncodingType,
100 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
101 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
103 BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
104 BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded)
108 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
110 if (pEncodePara && pEncodePara->pfnAlloc)
111 *(BYTE **)pbEncoded = pEncodePara->pfnAlloc(bytesNeeded);
113 *(BYTE **)pbEncoded = LocalAlloc(0, bytesNeeded);
114 if (!*(BYTE **)pbEncoded)
117 *pcbEncoded = bytesNeeded;
119 else if (bytesNeeded > *pcbEncoded)
121 *pcbEncoded = bytesNeeded;
122 SetLastError(ERROR_MORE_DATA);
126 *pcbEncoded = bytesNeeded;
130 BOOL CRYPT_EncodeLen(DWORD len, BYTE *pbEncoded, DWORD *pcbEncoded)
132 DWORD bytesNeeded, significantBytes = 0;
140 for (temp = len, significantBytes = sizeof(temp); !(temp & 0xff000000);
141 temp <<= 8, significantBytes--)
143 bytesNeeded = significantBytes + 1;
147 *pcbEncoded = bytesNeeded;
150 if (*pcbEncoded < bytesNeeded)
152 SetLastError(ERROR_MORE_DATA);
156 *pbEncoded = (BYTE)len;
161 *pbEncoded++ = significantBytes | 0x80;
162 for (i = 0; i < significantBytes; i++)
164 *(pbEncoded + significantBytes - i - 1) = (BYTE)(len & 0xff);
168 *pcbEncoded = bytesNeeded;
172 BOOL WINAPI CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType,
173 struct AsnEncodeSequenceItem items[], DWORD cItem, DWORD dwFlags,
174 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
177 DWORD i, dataLen = 0;
179 TRACE("%p, %d, %08x, %p, %p, %d\n", items, cItem, dwFlags, pEncodePara,
180 pbEncoded, *pcbEncoded);
181 for (i = 0, ret = TRUE; ret && i < cItem; i++)
183 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
184 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
185 NULL, &items[i].size);
186 /* Some functions propagate their errors through the size */
188 *pcbEncoded = items[i].size;
189 dataLen += items[i].size;
193 DWORD lenBytes, bytesNeeded;
195 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
196 bytesNeeded = 1 + lenBytes + dataLen;
198 *pcbEncoded = bytesNeeded;
201 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
202 pcbEncoded, bytesNeeded)))
204 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
205 pbEncoded = *(BYTE **)pbEncoded;
206 *pbEncoded++ = ASN_SEQUENCE;
207 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
208 pbEncoded += lenBytes;
209 for (i = 0; ret && i < cItem; i++)
211 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
212 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG,
213 NULL, pbEncoded, &items[i].size);
214 /* Some functions propagate their errors through the size */
216 *pcbEncoded = items[i].size;
217 pbEncoded += items[i].size;
222 TRACE("returning %d (%08x)\n", ret, GetLastError());
226 BOOL WINAPI CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType,
227 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
228 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
231 const struct AsnConstructedItem *item = pvStructInfo;
234 if ((ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
235 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &len)))
237 DWORD dataLen, bytesNeeded;
239 CRYPT_EncodeLen(len, NULL, &dataLen);
240 bytesNeeded = 1 + dataLen + len;
242 *pcbEncoded = bytesNeeded;
243 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
244 pbEncoded, pcbEncoded, bytesNeeded)))
246 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
247 pbEncoded = *(BYTE **)pbEncoded;
248 *pbEncoded++ = ASN_CONTEXT | ASN_CONSTRUCTOR | item->tag;
249 CRYPT_EncodeLen(len, pbEncoded, &dataLen);
250 pbEncoded += dataLen;
251 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
252 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
256 /* Some functions propagate their errors through the size */
263 /* Some functions propagate their errors through the size */
269 struct AsnEncodeTagSwappedItem
272 const void *pvStructInfo;
273 CryptEncodeObjectExFunc encodeFunc;
276 /* Sort of a wacky hack, it encodes something using the struct
277 * AsnEncodeTagSwappedItem's encodeFunc, then replaces the tag byte with the tag
278 * given in the struct AsnEncodeTagSwappedItem.
280 static BOOL WINAPI CRYPT_AsnEncodeSwapTag(DWORD dwCertEncodingType,
281 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
282 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
285 const struct AsnEncodeTagSwappedItem *item = pvStructInfo;
287 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
288 item->pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
289 if (ret && pbEncoded)
290 *pbEncoded = item->tag;
294 static BOOL WINAPI CRYPT_AsnEncodeCertVersion(DWORD dwCertEncodingType,
295 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
296 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
298 const DWORD *ver = pvStructInfo;
301 /* CERT_V1 is not encoded */
309 struct AsnConstructedItem item = { 0, ver, CRYPT_AsnEncodeInt };
311 ret = CRYPT_AsnEncodeConstructed(dwCertEncodingType, X509_INTEGER,
312 &item, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
317 static BOOL WINAPI CRYPT_CopyEncodedBlob(DWORD dwCertEncodingType,
318 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
319 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
321 const CRYPT_DER_BLOB *blob = pvStructInfo;
326 *pcbEncoded = blob->cbData;
331 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
332 pcbEncoded, blob->cbData)))
334 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
335 pbEncoded = *(BYTE **)pbEncoded;
337 memcpy(pbEncoded, blob->pbData, blob->cbData);
338 *pcbEncoded = blob->cbData;
345 static BOOL WINAPI CRYPT_AsnEncodeValidity(DWORD dwCertEncodingType,
346 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
347 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
350 /* This has two filetimes in a row, a NotBefore and a NotAfter */
351 const FILETIME *timePtr = pvStructInfo;
352 struct AsnEncodeSequenceItem items[] = {
353 { timePtr, CRYPT_AsnEncodeChoiceOfTime, 0 },
354 { timePtr + 1, CRYPT_AsnEncodeChoiceOfTime, 0 },
357 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
358 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
363 /* Like CRYPT_AsnEncodeAlgorithmId, but encodes parameters as an asn.1 NULL
366 static BOOL WINAPI CRYPT_AsnEncodeAlgorithmIdWithNullParams(
367 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
368 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
371 const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
372 static const BYTE asn1Null[] = { ASN_NULL, 0 };
373 static const CRYPT_DATA_BLOB nullBlob = { sizeof(asn1Null),
376 struct AsnEncodeSequenceItem items[2] = {
377 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
378 { NULL, CRYPT_CopyEncodedBlob, 0 },
381 if (algo->Parameters.cbData)
382 items[1].pvStructInfo = &algo->Parameters;
384 items[1].pvStructInfo = &nullBlob;
385 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
386 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
391 static BOOL WINAPI CRYPT_AsnEncodeAlgorithmId(DWORD dwCertEncodingType,
392 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
393 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
395 const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
397 struct AsnEncodeSequenceItem items[] = {
398 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
399 { &algo->Parameters, CRYPT_CopyEncodedBlob, 0 },
402 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
403 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
408 static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
409 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
410 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
416 const CERT_PUBLIC_KEY_INFO *info = pvStructInfo;
417 struct AsnEncodeSequenceItem items[] = {
418 { &info->Algorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
419 { &info->PublicKey, CRYPT_AsnEncodeBits, 0 },
422 TRACE("Encoding public key with OID %s\n",
423 debugstr_a(info->Algorithm.pszObjId));
424 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
425 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
430 SetLastError(STATUS_ACCESS_VIOLATION);
437 static BOOL WINAPI CRYPT_AsnEncodeCert(DWORD dwCertEncodingType,
438 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
439 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
445 const CERT_SIGNED_CONTENT_INFO *info = pvStructInfo;
446 struct AsnEncodeSequenceItem items[] = {
447 { &info->ToBeSigned, CRYPT_CopyEncodedBlob, 0 },
448 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
449 { &info->Signature, CRYPT_AsnEncodeBitsSwapBytes, 0 },
452 if (dwFlags & CRYPT_ENCODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG)
453 items[2].encodeFunc = CRYPT_AsnEncodeBits;
454 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
455 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
460 SetLastError(STATUS_ACCESS_VIOLATION);
467 /* Like in Windows, this blithely ignores the validity of the passed-in
468 * CERT_INFO, and just encodes it as-is. The resulting encoded data may not
469 * decode properly, see CRYPT_AsnDecodeCertInfo.
471 static BOOL WINAPI CRYPT_AsnEncodeCertInfo(DWORD dwCertEncodingType,
472 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
473 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
479 const CERT_INFO *info = pvStructInfo;
480 struct AsnEncodeSequenceItem items[10] = {
481 { &info->dwVersion, CRYPT_AsnEncodeCertVersion, 0 },
482 { &info->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
483 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
484 { &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
485 { &info->NotBefore, CRYPT_AsnEncodeValidity, 0 },
486 { &info->Subject, CRYPT_CopyEncodedBlob, 0 },
487 { &info->SubjectPublicKeyInfo, CRYPT_AsnEncodePubKeyInfo, 0 },
490 struct AsnConstructedItem constructed[3] = { { 0 } };
491 DWORD cItem = 7, cConstructed = 0;
493 if (info->IssuerUniqueId.cbData)
495 constructed[cConstructed].tag = 1;
496 constructed[cConstructed].pvStructInfo = &info->IssuerUniqueId;
497 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeBits;
498 items[cItem].pvStructInfo = &constructed[cConstructed];
499 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
503 if (info->SubjectUniqueId.cbData)
505 constructed[cConstructed].tag = 2;
506 constructed[cConstructed].pvStructInfo = &info->SubjectUniqueId;
507 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeBits;
508 items[cItem].pvStructInfo = &constructed[cConstructed];
509 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
513 if (info->cExtension)
515 constructed[cConstructed].tag = 3;
516 constructed[cConstructed].pvStructInfo = &info->cExtension;
517 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeExtensions;
518 items[cItem].pvStructInfo = &constructed[cConstructed];
519 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
524 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
525 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
529 SetLastError(STATUS_ACCESS_VIOLATION);
536 static BOOL CRYPT_AsnEncodeCRLEntry(const CRL_ENTRY *entry,
537 BYTE *pbEncoded, DWORD *pcbEncoded)
539 struct AsnEncodeSequenceItem items[3] = {
540 { &entry->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
541 { &entry->RevocationDate, CRYPT_AsnEncodeChoiceOfTime, 0 },
547 TRACE("%p, %p, %p\n", entry, pbEncoded, pcbEncoded);
549 if (entry->cExtension)
551 items[cItem].pvStructInfo = &entry->cExtension;
552 items[cItem].encodeFunc = CRYPT_AsnEncodeExtensions;
556 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
557 pbEncoded, pcbEncoded);
559 TRACE("returning %d (%08x)\n", ret, GetLastError());
563 static BOOL WINAPI CRYPT_AsnEncodeCRLEntries(DWORD dwCertEncodingType,
564 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
565 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
567 DWORD bytesNeeded, dataLen, lenBytes, i;
568 const CRL_INFO *info = pvStructInfo;
569 const CRL_ENTRY *rgCRLEntry = info->rgCRLEntry;
572 for (i = 0, dataLen = 0; ret && i < info->cCRLEntry; i++)
576 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], NULL, &size);
582 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
583 bytesNeeded = 1 + lenBytes + dataLen;
585 *pcbEncoded = bytesNeeded;
588 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
589 pcbEncoded, bytesNeeded)))
591 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
592 pbEncoded = *(BYTE **)pbEncoded;
593 *pbEncoded++ = ASN_SEQUENCEOF;
594 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
595 pbEncoded += lenBytes;
596 for (i = 0; i < info->cCRLEntry; i++)
598 DWORD size = dataLen;
600 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], pbEncoded,
611 static BOOL WINAPI CRYPT_AsnEncodeCRLVersion(DWORD dwCertEncodingType,
612 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
613 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
615 const DWORD *ver = pvStructInfo;
618 /* CRL_V1 is not encoded */
625 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
626 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
630 /* Like in Windows, this blithely ignores the validity of the passed-in
631 * CRL_INFO, and just encodes it as-is. The resulting encoded data may not
632 * decode properly, see CRYPT_AsnDecodeCRLInfo.
634 static BOOL WINAPI CRYPT_AsnEncodeCRLInfo(DWORD dwCertEncodingType,
635 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
636 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
642 const CRL_INFO *info = pvStructInfo;
643 struct AsnEncodeSequenceItem items[7] = {
644 { &info->dwVersion, CRYPT_AsnEncodeCRLVersion, 0 },
645 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
646 { &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
647 { &info->ThisUpdate, CRYPT_AsnEncodeChoiceOfTime, 0 },
650 struct AsnConstructedItem constructed[1] = { { 0 } };
651 DWORD cItem = 4, cConstructed = 0;
653 if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
655 items[cItem].pvStructInfo = &info->NextUpdate;
656 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
661 items[cItem].pvStructInfo = info;
662 items[cItem].encodeFunc = CRYPT_AsnEncodeCRLEntries;
665 if (info->cExtension)
667 constructed[cConstructed].tag = 0;
668 constructed[cConstructed].pvStructInfo = &info->cExtension;
669 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeExtensions;
670 items[cItem].pvStructInfo = &constructed[cConstructed];
671 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
676 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
677 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
681 SetLastError(STATUS_ACCESS_VIOLATION);
688 static BOOL CRYPT_AsnEncodeExtension(CERT_EXTENSION *ext, BYTE *pbEncoded,
692 struct AsnEncodeSequenceItem items[3] = {
693 { ext->pszObjId, CRYPT_AsnEncodeOid, 0 },
699 TRACE("%p, %p, %d\n", ext, pbEncoded, *pcbEncoded);
703 items[cItem].pvStructInfo = &ext->fCritical;
704 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
707 items[cItem].pvStructInfo = &ext->Value;
708 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
711 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
712 pbEncoded, pcbEncoded);
713 TRACE("returning %d (%08x)\n", ret, GetLastError());
717 static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
718 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
719 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
725 DWORD bytesNeeded, dataLen, lenBytes, i;
726 const CERT_EXTENSIONS *exts = pvStructInfo;
729 for (i = 0, dataLen = 0; ret && i < exts->cExtension; i++)
733 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i], NULL, &size);
739 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
740 bytesNeeded = 1 + lenBytes + dataLen;
742 *pcbEncoded = bytesNeeded;
745 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
746 pbEncoded, pcbEncoded, bytesNeeded)))
748 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
749 pbEncoded = *(BYTE **)pbEncoded;
750 *pbEncoded++ = ASN_SEQUENCEOF;
751 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
752 pbEncoded += lenBytes;
753 for (i = 0; i < exts->cExtension; i++)
755 DWORD size = dataLen;
757 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i],
768 SetLastError(STATUS_ACCESS_VIOLATION);
775 BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
776 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
777 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
779 LPCSTR pszObjId = pvStructInfo;
780 DWORD bytesNeeded = 0, lenBytes;
785 TRACE("%s\n", debugstr_a(pszObjId));
792 if (sscanf(pszObjId, "%d.%d%n", &val1, &val2, &firstPos) != 2)
794 SetLastError(CRYPT_E_ASN1_ERROR);
798 firstByte = val1 * 40 + val2;
799 ptr = pszObjId + firstPos;
809 /* note I assume each component is at most 32-bits long in base 2 */
810 if (sscanf(ptr, "%d%n", &val1, &pos) == 1)
812 if (val1 >= 0x10000000)
814 else if (val1 >= 0x200000)
816 else if (val1 >= 0x4000)
818 else if (val1 >= 0x80)
828 SetLastError(CRYPT_E_ASN1_ERROR);
832 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
836 bytesNeeded += 1 + lenBytes;
839 if (*pcbEncoded < bytesNeeded)
841 SetLastError(ERROR_MORE_DATA);
846 *pbEncoded++ = ASN_OBJECTIDENTIFIER;
847 CRYPT_EncodeLen(bytesNeeded - 1 - lenBytes, pbEncoded, &lenBytes);
848 pbEncoded += lenBytes;
854 *pbEncoded++ = firstByte;
855 ptr = pszObjId + firstPos;
858 sscanf(ptr, "%d%n", &val, &pos);
860 unsigned char outBytes[5];
863 if (val >= 0x10000000)
865 else if (val >= 0x200000)
867 else if (val >= 0x4000)
869 else if (val >= 0x80)
873 for (i = numBytes; i > 0; i--)
875 outBytes[i - 1] = val & 0x7f;
878 for (i = 0; i < numBytes - 1; i++)
879 *pbEncoded++ = outBytes[i] | 0x80;
880 *pbEncoded++ = outBytes[i];
889 *pcbEncoded = bytesNeeded;
893 static BOOL CRYPT_AsnEncodeStringCoerce(const CERT_NAME_VALUE *value,
894 BYTE tag, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
898 LPCSTR str = (LPCSTR)value->Value.pbData;
899 DWORD bytesNeeded, lenBytes, encodedLen;
901 encodedLen = value->Value.cbData ? value->Value.cbData : strlen(str);
902 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
903 bytesNeeded = 1 + lenBytes + encodedLen;
905 *pcbEncoded = bytesNeeded;
908 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
909 pbEncoded, pcbEncoded, bytesNeeded)))
911 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
912 pbEncoded = *(BYTE **)pbEncoded;
914 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
915 pbEncoded += lenBytes;
916 memcpy(pbEncoded, str, encodedLen);
922 static BOOL CRYPT_AsnEncodeBMPString(const CERT_NAME_VALUE *value,
923 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
927 LPCWSTR str = (LPCWSTR)value->Value.pbData;
928 DWORD bytesNeeded, lenBytes, strLen;
930 if (value->Value.cbData)
931 strLen = value->Value.cbData / sizeof(WCHAR);
932 else if (value->Value.pbData)
933 strLen = lstrlenW(str);
936 CRYPT_EncodeLen(strLen * 2, NULL, &lenBytes);
937 bytesNeeded = 1 + lenBytes + strLen * 2;
939 *pcbEncoded = bytesNeeded;
942 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
943 pbEncoded, pcbEncoded, bytesNeeded)))
947 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
948 pbEncoded = *(BYTE **)pbEncoded;
949 *pbEncoded++ = ASN_BMPSTRING;
950 CRYPT_EncodeLen(strLen * 2, pbEncoded, &lenBytes);
951 pbEncoded += lenBytes;
952 for (i = 0; i < strLen; i++)
954 *pbEncoded++ = (str[i] & 0xff00) >> 8;
955 *pbEncoded++ = str[i] & 0x00ff;
962 static BOOL CRYPT_AsnEncodeUTF8String(const CERT_NAME_VALUE *value,
963 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
967 LPCWSTR str = (LPCWSTR)value->Value.pbData;
968 DWORD bytesNeeded, lenBytes, encodedLen, strLen;
970 strLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
972 encodedLen = WideCharToMultiByte(CP_UTF8, 0, str, strLen, NULL, 0, NULL,
974 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
975 bytesNeeded = 1 + lenBytes + encodedLen;
977 *pcbEncoded = bytesNeeded;
980 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
981 pbEncoded, pcbEncoded, bytesNeeded)))
983 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
984 pbEncoded = *(BYTE **)pbEncoded;
985 *pbEncoded++ = ASN_UTF8STRING;
986 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
987 pbEncoded += lenBytes;
988 WideCharToMultiByte(CP_UTF8, 0, str, strLen, (LPSTR)pbEncoded,
989 bytesNeeded - lenBytes - 1, NULL, NULL);
995 static BOOL WINAPI CRYPT_AsnEncodeNameValue(DWORD dwCertEncodingType,
996 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
997 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1003 const CERT_NAME_VALUE *value = pvStructInfo;
1005 switch (value->dwValueType)
1007 case CERT_RDN_ANY_TYPE:
1008 /* explicitly disallowed */
1009 SetLastError(E_INVALIDARG);
1012 case CERT_RDN_ENCODED_BLOB:
1013 ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL,
1014 &value->Value, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1016 case CERT_RDN_OCTET_STRING:
1017 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_OCTETSTRING,
1018 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1020 case CERT_RDN_NUMERIC_STRING:
1021 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_NUMERICSTRING,
1022 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1024 case CERT_RDN_PRINTABLE_STRING:
1025 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_PRINTABLESTRING,
1026 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1028 case CERT_RDN_TELETEX_STRING:
1029 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_T61STRING,
1030 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1032 case CERT_RDN_VIDEOTEX_STRING:
1033 ret = CRYPT_AsnEncodeStringCoerce(value,
1034 ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1036 case CERT_RDN_IA5_STRING:
1037 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_IA5STRING,
1038 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1040 case CERT_RDN_GRAPHIC_STRING:
1041 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GRAPHICSTRING,
1042 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1044 case CERT_RDN_VISIBLE_STRING:
1045 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_VISIBLESTRING,
1046 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1048 case CERT_RDN_GENERAL_STRING:
1049 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GENERALSTRING,
1050 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1052 case CERT_RDN_UNIVERSAL_STRING:
1053 FIXME("CERT_RDN_UNIVERSAL_STRING: unimplemented\n");
1054 SetLastError(CRYPT_E_ASN1_CHOICE);
1057 case CERT_RDN_BMP_STRING:
1058 ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
1059 pbEncoded, pcbEncoded);
1061 case CERT_RDN_UTF8_STRING:
1062 ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
1063 pbEncoded, pcbEncoded);
1066 SetLastError(CRYPT_E_ASN1_CHOICE);
1072 SetLastError(STATUS_ACCESS_VIOLATION);
1079 static BOOL CRYPT_AsnEncodeRdnAttr(DWORD dwCertEncodingType,
1080 const CERT_RDN_ATTR *attr, CryptEncodeObjectExFunc nameValueEncodeFunc,
1081 BYTE *pbEncoded, DWORD *pcbEncoded)
1083 DWORD bytesNeeded = 0, lenBytes, size;
1086 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL, attr->pszObjId,
1087 0, NULL, NULL, &size);
1090 bytesNeeded += size;
1091 /* hack: a CERT_RDN_ATTR is identical to a CERT_NAME_VALUE beginning
1092 * with dwValueType, so "cast" it to get its encoded size
1094 ret = nameValueEncodeFunc(dwCertEncodingType, NULL, &attr->dwValueType,
1095 0, NULL, NULL, &size);
1098 bytesNeeded += size;
1099 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1100 bytesNeeded += 1 + lenBytes;
1103 if (*pcbEncoded < bytesNeeded)
1105 SetLastError(ERROR_MORE_DATA);
1110 *pbEncoded++ = ASN_SEQUENCE;
1111 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1113 pbEncoded += lenBytes;
1114 size = bytesNeeded - 1 - lenBytes;
1115 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
1116 attr->pszObjId, 0, NULL, pbEncoded, &size);
1120 size = bytesNeeded - 1 - lenBytes - size;
1121 ret = nameValueEncodeFunc(dwCertEncodingType, NULL,
1122 &attr->dwValueType, 0, NULL, pbEncoded, &size);
1129 *pcbEncoded = bytesNeeded;
1133 /* Have to propagate index of failing character */
1140 static int BLOBComp(const void *l, const void *r)
1142 const CRYPT_DER_BLOB *a = l, *b = r;
1145 if (!(ret = memcmp(a->pbData, b->pbData, min(a->cbData, b->cbData))))
1146 ret = a->cbData - b->cbData;
1150 /* This encodes a SET OF, which in DER must be lexicographically sorted.
1152 static BOOL WINAPI CRYPT_DEREncodeSet(DWORD dwCertEncodingType,
1153 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1154 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1156 const CRYPT_BLOB_ARRAY *set = pvStructInfo;
1157 DWORD bytesNeeded = 0, lenBytes, i;
1160 for (i = 0; i < set->cBlob; i++)
1161 bytesNeeded += set->rgBlob[i].cbData;
1162 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1163 bytesNeeded += 1 + lenBytes;
1166 *pcbEncoded = bytesNeeded;
1169 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1170 pbEncoded, pcbEncoded, bytesNeeded)))
1172 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1173 pbEncoded = *(BYTE **)pbEncoded;
1174 qsort(set->rgBlob, set->cBlob, sizeof(CRYPT_DER_BLOB), BLOBComp);
1175 *pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
1176 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
1177 pbEncoded += lenBytes;
1178 for (i = 0; ret && i < set->cBlob; i++)
1180 memcpy(pbEncoded, set->rgBlob[i].pbData, set->rgBlob[i].cbData);
1181 pbEncoded += set->rgBlob[i].cbData;
1187 struct DERSetDescriptor
1193 CryptEncodeObjectExFunc encode;
1196 static BOOL WINAPI CRYPT_DEREncodeItemsAsSet(DWORD dwCertEncodingType,
1197 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1198 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1200 const struct DERSetDescriptor *desc = pvStructInfo;
1201 CRYPT_BLOB_ARRAY setOf = { 0, NULL };
1207 setOf.rgBlob = CryptMemAlloc(desc->cItems * sizeof(CRYPT_DER_BLOB));
1212 setOf.cBlob = desc->cItems;
1213 memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
1216 for (i = 0; ret && i < setOf.cBlob; i++)
1218 ret = desc->encode(dwCertEncodingType, lpszStructType,
1219 (const BYTE *)desc->items + i * desc->itemSize + desc->itemOffset,
1220 0, NULL, NULL, &setOf.rgBlob[i].cbData);
1223 setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
1224 if (!setOf.rgBlob[i].pbData)
1227 ret = desc->encode(dwCertEncodingType, lpszStructType,
1228 (const BYTE *)desc->items + i * desc->itemSize +
1229 desc->itemOffset, 0, NULL, setOf.rgBlob[i].pbData,
1230 &setOf.rgBlob[i].cbData);
1232 /* Some functions propagate their errors through the size */
1234 *pcbEncoded = setOf.rgBlob[i].cbData;
1238 DWORD bytesNeeded = 0, lenBytes;
1240 for (i = 0; i < setOf.cBlob; i++)
1241 bytesNeeded += setOf.rgBlob[i].cbData;
1242 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1243 bytesNeeded += 1 + lenBytes;
1245 *pcbEncoded = bytesNeeded;
1246 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1247 pbEncoded, pcbEncoded, bytesNeeded)))
1249 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1250 pbEncoded = *(BYTE **)pbEncoded;
1251 qsort(setOf.rgBlob, setOf.cBlob, sizeof(CRYPT_DER_BLOB),
1253 *pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
1254 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
1255 pbEncoded += lenBytes;
1256 for (i = 0; i < setOf.cBlob; i++)
1258 memcpy(pbEncoded, setOf.rgBlob[i].pbData,
1259 setOf.rgBlob[i].cbData);
1260 pbEncoded += setOf.rgBlob[i].cbData;
1264 for (i = 0; i < setOf.cBlob; i++)
1265 CryptMemFree(setOf.rgBlob[i].pbData);
1266 CryptMemFree(setOf.rgBlob);
1270 static BOOL CRYPT_AsnEncodeRdn(DWORD dwCertEncodingType, const CERT_RDN *rdn,
1271 CryptEncodeObjectExFunc nameValueEncodeFunc, BYTE *pbEncoded,
1275 CRYPT_BLOB_ARRAY setOf = { 0, NULL };
1284 setOf.cBlob = rdn->cRDNAttr;
1285 setOf.rgBlob = CryptMemAlloc(rdn->cRDNAttr *
1286 sizeof(CRYPT_DER_BLOB));
1290 memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
1292 for (i = 0; ret && i < rdn->cRDNAttr; i++)
1294 setOf.rgBlob[i].cbData = 0;
1295 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType, &rdn->rgRDNAttr[i],
1296 nameValueEncodeFunc, NULL, &setOf.rgBlob[i].cbData);
1299 setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
1300 if (!setOf.rgBlob[i].pbData)
1303 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType,
1304 &rdn->rgRDNAttr[i], nameValueEncodeFunc,
1305 setOf.rgBlob[i].pbData, &setOf.rgBlob[i].cbData);
1309 /* Have to propagate index of failing character */
1310 *pcbEncoded = setOf.rgBlob[i].cbData;
1314 ret = CRYPT_DEREncodeSet(X509_ASN_ENCODING, NULL, &setOf, 0, NULL,
1315 pbEncoded, pcbEncoded);
1316 for (i = 0; i < setOf.cBlob; i++)
1317 CryptMemFree(setOf.rgBlob[i].pbData);
1321 SetLastError(STATUS_ACCESS_VIOLATION);
1325 CryptMemFree(setOf.rgBlob);
1329 static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
1330 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1331 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
1333 static BOOL WINAPI CRYPT_AsnEncodeOrCopyUnicodeNameValue(
1334 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1335 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1338 const CERT_NAME_VALUE *value = pvStructInfo;
1341 if (value->dwValueType == CERT_RDN_ENCODED_BLOB)
1342 ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL, &value->Value,
1343 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1345 ret = CRYPT_AsnEncodeUnicodeNameValue(dwCertEncodingType, NULL, value,
1346 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1350 static BOOL WINAPI CRYPT_AsnEncodeUnicodeName(DWORD dwCertEncodingType,
1351 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1352 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1358 const CERT_NAME_INFO *info = pvStructInfo;
1359 DWORD bytesNeeded = 0, lenBytes, size, i;
1361 TRACE("encoding name with %d RDNs\n", info->cRDN);
1363 for (i = 0; ret && i < info->cRDN; i++)
1365 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
1366 CRYPT_AsnEncodeOrCopyUnicodeNameValue, NULL, &size);
1368 bytesNeeded += size;
1372 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1373 bytesNeeded += 1 + lenBytes;
1377 *pcbEncoded = bytesNeeded;
1380 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1381 pbEncoded, pcbEncoded, bytesNeeded)))
1383 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1384 pbEncoded = *(BYTE **)pbEncoded;
1385 *pbEncoded++ = ASN_SEQUENCEOF;
1386 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1388 pbEncoded += lenBytes;
1389 for (i = 0; ret && i < info->cRDN; i++)
1392 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
1393 &info->rgRDN[i], CRYPT_AsnEncodeOrCopyUnicodeNameValue,
1398 bytesNeeded -= size;
1409 SetLastError(STATUS_ACCESS_VIOLATION);
1416 static BOOL WINAPI CRYPT_AsnEncodeCTLVersion(DWORD dwCertEncodingType,
1417 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1418 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1420 const DWORD *ver = pvStructInfo;
1423 /* CTL_V1 is not encoded */
1430 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
1431 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1435 /* Like CRYPT_AsnEncodeAlgorithmId, but encodes parameters as an asn.1 NULL
1436 * if they are empty and the OID is not empty (otherwise omits them.)
1438 static BOOL WINAPI CRYPT_AsnEncodeCTLSubjectAlgorithm(
1439 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1440 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1443 const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
1445 struct AsnEncodeSequenceItem items[2] = {
1446 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
1452 static const BYTE asn1Null[] = { ASN_NULL, 0 };
1453 static const CRYPT_DATA_BLOB nullBlob = { sizeof(asn1Null),
1456 if (algo->Parameters.cbData)
1457 items[cItem].pvStructInfo = &algo->Parameters;
1459 items[cItem].pvStructInfo = &nullBlob;
1460 items[cItem].encodeFunc = CRYPT_CopyEncodedBlob;
1463 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
1464 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1468 static BOOL CRYPT_AsnEncodeCTLEntry(const CTL_ENTRY *entry,
1469 BYTE *pbEncoded, DWORD *pcbEncoded)
1471 struct AsnEncodeSequenceItem items[2] = {
1472 { &entry->SubjectIdentifier, CRYPT_AsnEncodeOctets, 0 },
1473 { &entry->cAttribute, CRYPT_AsnEncodePKCSAttributes, 0 },
1477 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
1478 sizeof(items) / sizeof(items[0]), 0, NULL, pbEncoded, pcbEncoded);
1488 static BOOL WINAPI CRYPT_AsnEncodeCTLEntries(DWORD dwCertEncodingType,
1489 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1490 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1493 DWORD bytesNeeded, dataLen, lenBytes, i;
1494 const struct CTLEntries *entries = pvStructInfo;
1497 for (i = 0, dataLen = 0; ret && i < entries->cEntry; i++)
1501 ret = CRYPT_AsnEncodeCTLEntry(&entries->rgEntry[i], NULL, &size);
1507 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1508 bytesNeeded = 1 + lenBytes + dataLen;
1510 *pcbEncoded = bytesNeeded;
1513 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1514 pbEncoded, pcbEncoded, bytesNeeded)))
1516 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1517 pbEncoded = *(BYTE **)pbEncoded;
1518 *pbEncoded++ = ASN_SEQUENCEOF;
1519 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
1520 pbEncoded += lenBytes;
1521 for (i = 0; ret && i < entries->cEntry; i++)
1523 DWORD size = dataLen;
1525 ret = CRYPT_AsnEncodeCTLEntry(&entries->rgEntry[i],
1536 static BOOL WINAPI CRYPT_AsnEncodeCTL(DWORD dwCertEncodingType,
1537 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1538 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1544 const CTL_INFO *info = pvStructInfo;
1545 struct AsnEncodeSequenceItem items[9] = {
1546 { &info->dwVersion, CRYPT_AsnEncodeCTLVersion, 0 },
1547 { &info->SubjectUsage, CRYPT_AsnEncodeEnhancedKeyUsage, 0 },
1549 struct AsnConstructedItem constructed = { 0 };
1552 if (info->ListIdentifier.cbData)
1554 items[cItem].pvStructInfo = &info->ListIdentifier;
1555 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
1558 if (info->SequenceNumber.cbData)
1560 items[cItem].pvStructInfo = &info->SequenceNumber;
1561 items[cItem].encodeFunc = CRYPT_AsnEncodeInteger;
1564 items[cItem].pvStructInfo = &info->ThisUpdate;
1565 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
1567 if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
1569 items[cItem].pvStructInfo = &info->NextUpdate;
1570 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
1573 items[cItem].pvStructInfo = &info->SubjectAlgorithm;
1574 items[cItem].encodeFunc = CRYPT_AsnEncodeCTLSubjectAlgorithm;
1576 if (info->cCTLEntry)
1578 items[cItem].pvStructInfo = &info->cCTLEntry;
1579 items[cItem].encodeFunc = CRYPT_AsnEncodeCTLEntries;
1582 if (info->cExtension)
1584 constructed.tag = 0;
1585 constructed.pvStructInfo = &info->cExtension;
1586 constructed.encodeFunc = CRYPT_AsnEncodeExtensions;
1587 items[cItem].pvStructInfo = &constructed;
1588 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
1591 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
1592 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1596 SetLastError(STATUS_ACCESS_VIOLATION);
1602 static BOOL CRYPT_AsnEncodeSMIMECapability(DWORD dwCertEncodingType,
1603 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1604 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1610 const CRYPT_SMIME_CAPABILITY *capability = pvStructInfo;
1612 if (!capability->pszObjId)
1613 SetLastError(E_INVALIDARG);
1616 struct AsnEncodeSequenceItem items[] = {
1617 { capability->pszObjId, CRYPT_AsnEncodeOid, 0 },
1618 { &capability->Parameters, CRYPT_CopyEncodedBlob, 0 },
1621 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1622 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
1628 SetLastError(STATUS_ACCESS_VIOLATION);
1634 static BOOL WINAPI CRYPT_AsnEncodeSMIMECapabilities(DWORD dwCertEncodingType,
1635 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1636 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1642 DWORD bytesNeeded, dataLen, lenBytes, i;
1643 const CRYPT_SMIME_CAPABILITIES *capabilities = pvStructInfo;
1646 for (i = 0, dataLen = 0; ret && i < capabilities->cCapability; i++)
1650 ret = CRYPT_AsnEncodeSMIMECapability(dwCertEncodingType, NULL,
1651 &capabilities->rgCapability[i], 0, NULL, NULL, &size);
1657 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1658 bytesNeeded = 1 + lenBytes + dataLen;
1660 *pcbEncoded = bytesNeeded;
1663 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1664 pbEncoded, pcbEncoded, bytesNeeded)))
1666 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1667 pbEncoded = *(BYTE **)pbEncoded;
1668 *pbEncoded++ = ASN_SEQUENCEOF;
1669 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
1670 pbEncoded += lenBytes;
1671 for (i = 0; i < capabilities->cCapability; i++)
1673 DWORD size = dataLen;
1675 ret = CRYPT_AsnEncodeSMIMECapability(dwCertEncodingType,
1676 NULL, &capabilities->rgCapability[i], 0, NULL,
1687 SetLastError(STATUS_ACCESS_VIOLATION);
1693 static BOOL WINAPI CRYPT_AsnEncodeNoticeNumbers(DWORD dwCertEncodingType,
1694 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1695 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1697 const CERT_POLICY_QUALIFIER_NOTICE_REFERENCE *noticeRef = pvStructInfo;
1698 DWORD bytesNeeded, dataLen, lenBytes, i;
1701 for (i = 0, dataLen = 0; ret && i < noticeRef->cNoticeNumbers; i++)
1705 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER,
1706 ¬iceRef->rgNoticeNumbers[i], 0, NULL, NULL, &size);
1712 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1713 bytesNeeded = 1 + lenBytes + dataLen;
1715 *pcbEncoded = bytesNeeded;
1718 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
1719 pcbEncoded, bytesNeeded)))
1721 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1722 pbEncoded = *(BYTE **)pbEncoded;
1723 *pbEncoded++ = ASN_SEQUENCE;
1724 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
1725 pbEncoded += lenBytes;
1726 for (i = 0; i < noticeRef->cNoticeNumbers; i++)
1728 DWORD size = dataLen;
1730 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER,
1731 ¬iceRef->rgNoticeNumbers[i], 0, NULL, pbEncoded, &size);
1741 static BOOL WINAPI CRYPT_AsnEncodeNoticeReference(DWORD dwCertEncodingType,
1742 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1743 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1745 const CERT_POLICY_QUALIFIER_NOTICE_REFERENCE *noticeRef = pvStructInfo;
1747 CERT_NAME_VALUE orgValue = { CERT_RDN_IA5_STRING,
1748 { 0, (LPBYTE)noticeRef->pszOrganization } };
1749 struct AsnEncodeSequenceItem items[] = {
1750 { &orgValue, CRYPT_AsnEncodeNameValue, 0 },
1751 { noticeRef, CRYPT_AsnEncodeNoticeNumbers, 0 },
1754 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1755 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
1760 static BOOL WINAPI CRYPT_AsnEncodePolicyQualifierUserNotice(
1761 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1762 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1769 const CERT_POLICY_QUALIFIER_USER_NOTICE *notice = pvStructInfo;
1770 struct AsnEncodeSequenceItem items[2];
1771 CERT_NAME_VALUE displayTextValue;
1775 if (notice->pNoticeReference)
1777 items[cItem].encodeFunc = CRYPT_AsnEncodeNoticeReference;
1778 items[cItem].pvStructInfo = notice->pNoticeReference;
1781 if (notice->pszDisplayText)
1783 displayTextValue.dwValueType = CERT_RDN_BMP_STRING;
1784 displayTextValue.Value.cbData = 0;
1785 displayTextValue.Value.pbData = (LPBYTE)notice->pszDisplayText;
1786 items[cItem].encodeFunc = CRYPT_AsnEncodeNameValue;
1787 items[cItem].pvStructInfo = &displayTextValue;
1790 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
1791 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1795 SetLastError(STATUS_ACCESS_VIOLATION);
1801 static BOOL WINAPI CRYPT_AsnEncodePKCSAttribute(DWORD dwCertEncodingType,
1802 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1803 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1809 const CRYPT_ATTRIBUTE *attr = pvStructInfo;
1811 if (!attr->pszObjId)
1812 SetLastError(E_INVALIDARG);
1815 struct AsnEncodeSequenceItem items[2] = {
1816 { attr->pszObjId, CRYPT_AsnEncodeOid, 0 },
1817 { &attr->cValue, CRYPT_DEREncodeSet, 0 },
1820 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1821 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
1827 SetLastError(STATUS_ACCESS_VIOLATION);
1833 static BOOL WINAPI CRYPT_AsnEncodePKCSAttributes(DWORD dwCertEncodingType,
1834 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1835 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1841 const CRYPT_ATTRIBUTES *attributes = pvStructInfo;
1842 struct DERSetDescriptor desc = { attributes->cAttr, attributes->rgAttr,
1843 sizeof(CRYPT_ATTRIBUTE), 0, CRYPT_AsnEncodePKCSAttribute };
1845 ret = CRYPT_DEREncodeItemsAsSet(X509_ASN_ENCODING, lpszStructType,
1846 &desc, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1850 SetLastError(STATUS_ACCESS_VIOLATION);
1856 /* Like CRYPT_AsnEncodePKCSContentInfo, but allows the OID to be NULL */
1857 static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfoInternal(
1858 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1859 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1862 const CRYPT_CONTENT_INFO *info = pvStructInfo;
1863 struct AsnEncodeSequenceItem items[2] = {
1864 { info->pszObjId, CRYPT_AsnEncodeOid, 0 },
1867 struct AsnConstructedItem constructed = { 0 };
1870 if (info->Content.cbData)
1872 constructed.tag = 0;
1873 constructed.pvStructInfo = &info->Content;
1874 constructed.encodeFunc = CRYPT_CopyEncodedBlob;
1875 items[cItem].pvStructInfo = &constructed;
1876 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
1879 return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1880 cItem, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1883 BOOL CRYPT_AsnEncodePKCSDigestedData(const CRYPT_DIGESTED_DATA *digestedData,
1884 void *pvData, DWORD *pcbData)
1886 struct AsnEncodeSequenceItem items[] = {
1887 { &digestedData->version, CRYPT_AsnEncodeInt, 0 },
1888 { &digestedData->DigestAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
1890 { &digestedData->ContentInfo, CRYPT_AsnEncodePKCSContentInfoInternal, 0 },
1891 { &digestedData->hash, CRYPT_AsnEncodeOctets, 0 },
1894 return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
1895 sizeof(items) / sizeof(items[0]), 0, NULL, pvData, pcbData);
1898 static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfo(DWORD dwCertEncodingType,
1899 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1900 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1906 const CRYPT_CONTENT_INFO *info = pvStructInfo;
1908 if (!info->pszObjId)
1909 SetLastError(E_INVALIDARG);
1911 ret = CRYPT_AsnEncodePKCSContentInfoInternal(dwCertEncodingType,
1912 lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
1917 SetLastError(STATUS_ACCESS_VIOLATION);
1923 static BOOL CRYPT_AsnEncodeUnicodeStringCoerce(const CERT_NAME_VALUE *value,
1924 BYTE tag, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1928 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1929 DWORD bytesNeeded, lenBytes, encodedLen;
1931 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
1933 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1934 bytesNeeded = 1 + lenBytes + encodedLen;
1936 *pcbEncoded = bytesNeeded;
1939 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1940 pbEncoded, pcbEncoded, bytesNeeded)))
1944 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1945 pbEncoded = *(BYTE **)pbEncoded;
1947 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
1948 pbEncoded += lenBytes;
1949 for (i = 0; i < encodedLen; i++)
1950 *pbEncoded++ = (BYTE)str[i];
1956 static void CRYPT_FreeSpace(PCRYPT_ENCODE_PARA pEncodePara, LPVOID pv)
1958 if (pEncodePara && pEncodePara->pfnFree)
1959 pEncodePara->pfnFree(pv);
1964 static BOOL CRYPT_AsnEncodeNumericString(const CERT_NAME_VALUE *value,
1965 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1969 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1970 DWORD bytesNeeded, lenBytes, encodedLen;
1972 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
1974 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1975 bytesNeeded = 1 + lenBytes + encodedLen;
1977 *pcbEncoded = bytesNeeded;
1980 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1981 pbEncoded, pcbEncoded, bytesNeeded)))
1986 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1987 ptr = *(BYTE **)pbEncoded;
1990 *ptr++ = ASN_NUMERICSTRING;
1991 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
1993 for (i = 0; ret && i < encodedLen; i++)
1995 if (isdigitW(str[i]))
1996 *ptr++ = (BYTE)str[i];
2000 SetLastError(CRYPT_E_INVALID_NUMERIC_STRING);
2004 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2005 CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
2011 static inline int isprintableW(WCHAR wc)
2013 return isalnumW(wc) || isspaceW(wc) || wc == '\'' || wc == '(' ||
2014 wc == ')' || wc == '+' || wc == ',' || wc == '-' || wc == '.' ||
2015 wc == '/' || wc == ':' || wc == '=' || wc == '?';
2018 static BOOL CRYPT_AsnEncodePrintableString(const CERT_NAME_VALUE *value,
2019 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
2023 LPCWSTR str = (LPCWSTR)value->Value.pbData;
2024 DWORD bytesNeeded, lenBytes, encodedLen;
2026 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
2028 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
2029 bytesNeeded = 1 + lenBytes + encodedLen;
2031 *pcbEncoded = bytesNeeded;
2034 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2035 pbEncoded, pcbEncoded, bytesNeeded)))
2040 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2041 ptr = *(BYTE **)pbEncoded;
2044 *ptr++ = ASN_PRINTABLESTRING;
2045 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
2047 for (i = 0; ret && i < encodedLen; i++)
2049 if (isprintableW(str[i]))
2050 *ptr++ = (BYTE)str[i];
2054 SetLastError(CRYPT_E_INVALID_PRINTABLE_STRING);
2058 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2059 CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
2065 static BOOL CRYPT_AsnEncodeIA5String(const CERT_NAME_VALUE *value,
2066 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
2070 LPCWSTR str = (LPCWSTR)value->Value.pbData;
2071 DWORD bytesNeeded, lenBytes, encodedLen;
2073 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
2075 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
2076 bytesNeeded = 1 + lenBytes + encodedLen;
2078 *pcbEncoded = bytesNeeded;
2081 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2082 pbEncoded, pcbEncoded, bytesNeeded)))
2087 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2088 ptr = *(BYTE **)pbEncoded;
2091 *ptr++ = ASN_IA5STRING;
2092 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
2094 for (i = 0; ret && i < encodedLen; i++)
2097 *ptr++ = (BYTE)str[i];
2101 SetLastError(CRYPT_E_INVALID_IA5_STRING);
2105 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2106 CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
2112 static BOOL CRYPT_AsnEncodeUniversalString(const CERT_NAME_VALUE *value,
2113 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
2117 LPCWSTR str = (LPCWSTR)value->Value.pbData;
2118 DWORD bytesNeeded, lenBytes, strLen;
2120 /* FIXME: doesn't handle composite characters */
2121 strLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
2123 CRYPT_EncodeLen(strLen * 4, NULL, &lenBytes);
2124 bytesNeeded = 1 + lenBytes + strLen * 4;
2126 *pcbEncoded = bytesNeeded;
2129 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2130 pbEncoded, pcbEncoded, bytesNeeded)))
2134 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2135 pbEncoded = *(BYTE **)pbEncoded;
2136 *pbEncoded++ = ASN_UNIVERSALSTRING;
2137 CRYPT_EncodeLen(strLen * 4, pbEncoded, &lenBytes);
2138 pbEncoded += lenBytes;
2139 for (i = 0; i < strLen; i++)
2143 *pbEncoded++ = (BYTE)((str[i] & 0xff00) >> 8);
2144 *pbEncoded++ = (BYTE)(str[i] & 0x00ff);
2151 static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
2152 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2153 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2159 const CERT_NAME_VALUE *value = pvStructInfo;
2161 switch (value->dwValueType)
2163 case CERT_RDN_ANY_TYPE:
2164 case CERT_RDN_ENCODED_BLOB:
2165 case CERT_RDN_OCTET_STRING:
2166 SetLastError(CRYPT_E_NOT_CHAR_STRING);
2168 case CERT_RDN_NUMERIC_STRING:
2169 ret = CRYPT_AsnEncodeNumericString(value, dwFlags, pEncodePara,
2170 pbEncoded, pcbEncoded);
2172 case CERT_RDN_PRINTABLE_STRING:
2173 ret = CRYPT_AsnEncodePrintableString(value, dwFlags, pEncodePara,
2174 pbEncoded, pcbEncoded);
2176 case CERT_RDN_TELETEX_STRING:
2177 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_T61STRING,
2178 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2180 case CERT_RDN_VIDEOTEX_STRING:
2181 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value,
2182 ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2184 case CERT_RDN_IA5_STRING:
2185 ret = CRYPT_AsnEncodeIA5String(value, dwFlags, pEncodePara,
2186 pbEncoded, pcbEncoded);
2188 case CERT_RDN_GRAPHIC_STRING:
2189 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GRAPHICSTRING,
2190 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2192 case CERT_RDN_VISIBLE_STRING:
2193 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_VISIBLESTRING,
2194 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2196 case CERT_RDN_GENERAL_STRING:
2197 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GENERALSTRING,
2198 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2200 case CERT_RDN_UNIVERSAL_STRING:
2201 ret = CRYPT_AsnEncodeUniversalString(value, dwFlags, pEncodePara,
2202 pbEncoded, pcbEncoded);
2204 case CERT_RDN_BMP_STRING:
2205 ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
2206 pbEncoded, pcbEncoded);
2208 case CERT_RDN_UTF8_STRING:
2209 ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
2210 pbEncoded, pcbEncoded);
2213 SetLastError(CRYPT_E_ASN1_CHOICE);
2218 SetLastError(STATUS_ACCESS_VIOLATION);
2224 static BOOL WINAPI CRYPT_AsnEncodeName(DWORD dwCertEncodingType,
2225 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2226 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2232 const CERT_NAME_INFO *info = pvStructInfo;
2233 DWORD bytesNeeded = 0, lenBytes, size, i;
2235 TRACE("encoding name with %d RDNs\n", info->cRDN);
2237 for (i = 0; ret && i < info->cRDN; i++)
2239 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
2240 CRYPT_AsnEncodeNameValue, NULL, &size);
2242 bytesNeeded += size;
2244 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
2245 bytesNeeded += 1 + lenBytes;
2249 *pcbEncoded = bytesNeeded;
2252 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2253 pbEncoded, pcbEncoded, bytesNeeded)))
2255 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2256 pbEncoded = *(BYTE **)pbEncoded;
2257 *pbEncoded++ = ASN_SEQUENCEOF;
2258 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
2260 pbEncoded += lenBytes;
2261 for (i = 0; ret && i < info->cRDN; i++)
2264 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
2265 &info->rgRDN[i], CRYPT_AsnEncodeNameValue, pbEncoded,
2270 bytesNeeded -= size;
2279 SetLastError(STATUS_ACCESS_VIOLATION);
2286 static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
2287 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2288 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2290 BOOL val = *(const BOOL *)pvStructInfo, ret;
2299 else if (*pcbEncoded < 3)
2302 SetLastError(ERROR_MORE_DATA);
2308 *pbEncoded++ = ASN_BOOL;
2310 *pbEncoded++ = val ? 0xff : 0;
2313 TRACE("returning %d (%08x)\n", ret, GetLastError());
2317 static BOOL WINAPI CRYPT_AsnEncodeAltNameEntry(DWORD dwCertEncodingType,
2318 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2319 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2321 const CERT_ALT_NAME_ENTRY *entry = pvStructInfo;
2327 switch (entry->dwAltNameChoice)
2329 case CERT_ALT_NAME_RFC822_NAME:
2330 case CERT_ALT_NAME_DNS_NAME:
2331 case CERT_ALT_NAME_URL:
2332 tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
2333 if (entry->u.pwszURL)
2337 /* Not + 1: don't encode the NULL-terminator */
2338 dataLen = lstrlenW(entry->u.pwszURL);
2339 for (i = 0; ret && i < dataLen; i++)
2341 if (entry->u.pwszURL[i] > 0x7f)
2343 SetLastError(CRYPT_E_INVALID_IA5_STRING);
2352 case CERT_ALT_NAME_DIRECTORY_NAME:
2353 tag = ASN_CONTEXT | ASN_CONSTRUCTOR | (entry->dwAltNameChoice - 1);
2354 dataLen = entry->u.DirectoryName.cbData;
2356 case CERT_ALT_NAME_IP_ADDRESS:
2357 tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
2358 dataLen = entry->u.IPAddress.cbData;
2360 case CERT_ALT_NAME_REGISTERED_ID:
2362 struct AsnEncodeTagSwappedItem swapped =
2363 { ASN_CONTEXT | (entry->dwAltNameChoice - 1), entry->u.pszRegisteredID,
2364 CRYPT_AsnEncodeOid };
2366 return CRYPT_AsnEncodeSwapTag(0, NULL, &swapped, 0, NULL, pbEncoded,
2369 case CERT_ALT_NAME_OTHER_NAME:
2370 FIXME("name type %d unimplemented\n", entry->dwAltNameChoice);
2373 SetLastError(E_INVALIDARG);
2378 DWORD bytesNeeded, lenBytes;
2380 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2381 bytesNeeded = 1 + dataLen + lenBytes;
2383 *pcbEncoded = bytesNeeded;
2384 else if (*pcbEncoded < bytesNeeded)
2386 SetLastError(ERROR_MORE_DATA);
2387 *pcbEncoded = bytesNeeded;
2393 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2394 pbEncoded += lenBytes;
2395 switch (entry->dwAltNameChoice)
2397 case CERT_ALT_NAME_RFC822_NAME:
2398 case CERT_ALT_NAME_DNS_NAME:
2399 case CERT_ALT_NAME_URL:
2403 for (i = 0; i < dataLen; i++)
2404 *pbEncoded++ = (BYTE)entry->u.pwszURL[i];
2407 case CERT_ALT_NAME_DIRECTORY_NAME:
2408 memcpy(pbEncoded, entry->u.DirectoryName.pbData, dataLen);
2410 case CERT_ALT_NAME_IP_ADDRESS:
2411 memcpy(pbEncoded, entry->u.IPAddress.pbData, dataLen);
2415 *pcbEncoded = bytesNeeded;
2418 TRACE("returning %d (%08x)\n", ret, GetLastError());
2422 static BOOL WINAPI CRYPT_AsnEncodeIntegerSwapBytes(DWORD dwCertEncodingType,
2423 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2424 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2430 const CRYPT_DATA_BLOB *blob = pvStructInfo;
2431 CRYPT_DATA_BLOB newBlob = { blob->cbData, NULL };
2436 newBlob.pbData = CryptMemAlloc(newBlob.cbData);
2441 for (i = 0; i < newBlob.cbData; i++)
2442 newBlob.pbData[newBlob.cbData - i - 1] = blob->pbData[i];
2448 ret = CRYPT_AsnEncodeInteger(dwCertEncodingType, lpszStructType,
2449 &newBlob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2450 CryptMemFree(newBlob.pbData);
2454 SetLastError(STATUS_ACCESS_VIOLATION);
2461 static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId(DWORD dwCertEncodingType,
2462 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2463 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2469 const CERT_AUTHORITY_KEY_ID_INFO *info = pvStructInfo;
2470 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2471 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2472 struct AsnConstructedItem constructed = { 0 };
2473 DWORD cItem = 0, cSwapped = 0;
2475 if (info->KeyId.cbData)
2477 swapped[cSwapped].tag = ASN_CONTEXT | 0;
2478 swapped[cSwapped].pvStructInfo = &info->KeyId;
2479 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeIntegerSwapBytes;
2480 items[cItem].pvStructInfo = &swapped[cSwapped];
2481 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2485 if (info->CertIssuer.cbData)
2487 constructed.tag = 1;
2488 constructed.pvStructInfo = &info->CertIssuer;
2489 constructed.encodeFunc = CRYPT_CopyEncodedBlob;
2490 items[cItem].pvStructInfo = &constructed;
2491 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
2494 if (info->CertSerialNumber.cbData)
2496 swapped[cSwapped].tag = ASN_CONTEXT | 2;
2497 swapped[cSwapped].pvStructInfo = &info->CertSerialNumber;
2498 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
2499 items[cItem].pvStructInfo = &swapped[cSwapped];
2500 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2504 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
2505 pEncodePara, pbEncoded, pcbEncoded);
2509 SetLastError(STATUS_ACCESS_VIOLATION);
2516 static BOOL WINAPI CRYPT_AsnEncodeAltName(DWORD dwCertEncodingType,
2517 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2518 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2524 const CERT_ALT_NAME_INFO *info = pvStructInfo;
2525 DWORD bytesNeeded, dataLen, lenBytes, i;
2528 /* FIXME: should check that cAltEntry is not bigger than 0xff, since we
2529 * can't encode an erroneous entry index if it's bigger than this.
2531 for (i = 0, dataLen = 0; ret && i < info->cAltEntry; i++)
2535 ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType, NULL,
2536 &info->rgAltEntry[i], 0, NULL, NULL, &len);
2539 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
2541 /* CRYPT_AsnEncodeAltNameEntry encoded the index of
2542 * the bad character, now set the index of the bad
2545 *pcbEncoded = (BYTE)i <<
2546 CERT_ALT_NAME_ENTRY_ERR_INDEX_SHIFT | len;
2551 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2552 bytesNeeded = 1 + lenBytes + dataLen;
2555 *pcbEncoded = bytesNeeded;
2560 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2561 pbEncoded, pcbEncoded, bytesNeeded)))
2563 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2564 pbEncoded = *(BYTE **)pbEncoded;
2565 *pbEncoded++ = ASN_SEQUENCEOF;
2566 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2567 pbEncoded += lenBytes;
2568 for (i = 0; ret && i < info->cAltEntry; i++)
2570 DWORD len = dataLen;
2572 ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType,
2573 NULL, &info->rgAltEntry[i], 0, NULL, pbEncoded, &len);
2586 SetLastError(STATUS_ACCESS_VIOLATION);
2593 static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId2(DWORD dwCertEncodingType,
2594 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2595 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2601 const CERT_AUTHORITY_KEY_ID2_INFO *info = pvStructInfo;
2602 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2603 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2604 DWORD cItem = 0, cSwapped = 0;
2606 if (info->KeyId.cbData)
2608 swapped[cSwapped].tag = ASN_CONTEXT | 0;
2609 swapped[cSwapped].pvStructInfo = &info->KeyId;
2610 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeIntegerSwapBytes;
2611 items[cItem].pvStructInfo = &swapped[cSwapped];
2612 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2616 if (info->AuthorityCertIssuer.cAltEntry)
2618 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
2619 swapped[cSwapped].pvStructInfo = &info->AuthorityCertIssuer;
2620 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
2621 items[cItem].pvStructInfo = &swapped[cSwapped];
2622 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2626 if (info->AuthorityCertSerialNumber.cbData)
2628 swapped[cSwapped].tag = ASN_CONTEXT | 2;
2629 swapped[cSwapped].pvStructInfo = &info->AuthorityCertSerialNumber;
2630 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
2631 items[cItem].pvStructInfo = &swapped[cSwapped];
2632 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2636 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
2637 pEncodePara, pbEncoded, pcbEncoded);
2641 SetLastError(STATUS_ACCESS_VIOLATION);
2648 static BOOL CRYPT_AsnEncodeAccessDescription(
2649 const CERT_ACCESS_DESCRIPTION *descr, BYTE *pbEncoded, DWORD *pcbEncoded)
2651 struct AsnEncodeSequenceItem items[] = {
2652 { descr->pszAccessMethod, CRYPT_AsnEncodeOid, 0 },
2653 { &descr->AccessLocation, CRYPT_AsnEncodeAltNameEntry, 0 },
2656 if (!descr->pszAccessMethod)
2658 SetLastError(E_INVALIDARG);
2661 return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
2662 sizeof(items) / sizeof(items[0]), 0, NULL, pbEncoded, pcbEncoded);
2665 static BOOL WINAPI CRYPT_AsnEncodeAuthorityInfoAccess(DWORD dwCertEncodingType,
2666 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2667 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2673 DWORD bytesNeeded, dataLen, lenBytes, i;
2674 const CERT_AUTHORITY_INFO_ACCESS *info = pvStructInfo;
2677 for (i = 0, dataLen = 0; ret && i < info->cAccDescr; i++)
2681 ret = CRYPT_AsnEncodeAccessDescription(&info->rgAccDescr[i], NULL,
2688 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2689 bytesNeeded = 1 + lenBytes + dataLen;
2691 *pcbEncoded = bytesNeeded;
2694 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2695 pbEncoded, pcbEncoded, bytesNeeded)))
2697 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2698 pbEncoded = *(BYTE **)pbEncoded;
2699 *pbEncoded++ = ASN_SEQUENCEOF;
2700 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2701 pbEncoded += lenBytes;
2702 for (i = 0; i < info->cAccDescr; i++)
2704 DWORD size = dataLen;
2706 ret = CRYPT_AsnEncodeAccessDescription(
2707 &info->rgAccDescr[i], pbEncoded, &size);
2717 SetLastError(STATUS_ACCESS_VIOLATION);
2724 static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints(DWORD dwCertEncodingType,
2725 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2726 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2732 const CERT_BASIC_CONSTRAINTS_INFO *info = pvStructInfo;
2733 struct AsnEncodeSequenceItem items[3] = {
2734 { &info->SubjectType, CRYPT_AsnEncodeBits, 0 },
2739 if (info->fPathLenConstraint)
2741 items[cItem].pvStructInfo = &info->dwPathLenConstraint;
2742 items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
2745 if (info->cSubtreesConstraint)
2747 items[cItem].pvStructInfo = &info->cSubtreesConstraint;
2748 items[cItem].encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
2751 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
2752 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2756 SetLastError(STATUS_ACCESS_VIOLATION);
2763 static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints2(DWORD dwCertEncodingType,
2764 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2765 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2771 const CERT_BASIC_CONSTRAINTS2_INFO *info = pvStructInfo;
2772 struct AsnEncodeSequenceItem items[2] = { { 0 } };
2777 items[cItem].pvStructInfo = &info->fCA;
2778 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
2781 if (info->fPathLenConstraint)
2783 items[cItem].pvStructInfo = &info->dwPathLenConstraint;
2784 items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
2787 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
2788 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2792 SetLastError(STATUS_ACCESS_VIOLATION);
2799 static BOOL WINAPI CRYPT_AsnEncodeCertPolicyQualifiers(DWORD dwCertEncodingType,
2800 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2801 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2803 const CERT_POLICY_INFO *info = pvStructInfo;
2806 if (!info->cPolicyQualifier)
2813 struct AsnEncodeSequenceItem items[2] = {
2814 { NULL, CRYPT_AsnEncodeOid, 0 },
2815 { NULL, CRYPT_CopyEncodedBlob, 0 },
2817 DWORD bytesNeeded = 0, lenBytes, size, i;
2820 for (i = 0; ret && i < info->cPolicyQualifier; i++)
2822 items[0].pvStructInfo =
2823 info->rgPolicyQualifier[i].pszPolicyQualifierId;
2824 items[1].pvStructInfo = &info->rgPolicyQualifier[i].Qualifier;
2825 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
2826 sizeof(items) / sizeof(items[0]),
2827 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &size);
2829 bytesNeeded += size;
2831 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
2832 bytesNeeded += 1 + lenBytes;
2836 *pcbEncoded = bytesNeeded;
2839 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2840 pbEncoded, pcbEncoded, bytesNeeded)))
2842 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2843 pbEncoded = *(BYTE **)pbEncoded;
2844 *pbEncoded++ = ASN_SEQUENCEOF;
2845 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
2847 pbEncoded += lenBytes;
2848 for (i = 0; ret && i < info->cPolicyQualifier; i++)
2850 items[0].pvStructInfo =
2851 info->rgPolicyQualifier[i].pszPolicyQualifierId;
2852 items[1].pvStructInfo =
2853 &info->rgPolicyQualifier[i].Qualifier;
2855 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
2856 sizeof(items) / sizeof(items[0]),
2857 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pbEncoded,
2862 bytesNeeded -= size;
2872 static BOOL CRYPT_AsnEncodeCertPolicy(DWORD dwCertEncodingType,
2873 const CERT_POLICY_INFO *info, DWORD dwFlags, BYTE *pbEncoded,
2876 struct AsnEncodeSequenceItem items[2] = {
2877 { info->pszPolicyIdentifier, CRYPT_AsnEncodeOid, 0 },
2878 { info, CRYPT_AsnEncodeCertPolicyQualifiers, 0 },
2882 if (!info->pszPolicyIdentifier)
2884 SetLastError(E_INVALIDARG);
2887 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
2888 sizeof(items) / sizeof(items[0]), dwFlags, NULL, pbEncoded, pcbEncoded);
2892 static BOOL WINAPI CRYPT_AsnEncodeCertPolicies(DWORD dwCertEncodingType,
2893 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2894 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2900 const CERT_POLICIES_INFO *info = pvStructInfo;
2901 DWORD bytesNeeded = 0, lenBytes, size, i;
2904 for (i = 0; ret && i < info->cPolicyInfo; i++)
2906 ret = CRYPT_AsnEncodeCertPolicy(dwCertEncodingType,
2907 &info->rgPolicyInfo[i], dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
2910 bytesNeeded += size;
2912 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
2913 bytesNeeded += 1 + lenBytes;
2917 *pcbEncoded = bytesNeeded;
2920 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2921 pbEncoded, pcbEncoded, bytesNeeded)))
2923 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2924 pbEncoded = *(BYTE **)pbEncoded;
2925 *pbEncoded++ = ASN_SEQUENCEOF;
2926 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
2928 pbEncoded += lenBytes;
2929 for (i = 0; ret && i < info->cPolicyInfo; i++)
2932 ret = CRYPT_AsnEncodeCertPolicy(dwCertEncodingType,
2933 &info->rgPolicyInfo[i],
2934 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, pbEncoded, &size);
2938 bytesNeeded -= size;
2947 SetLastError(STATUS_ACCESS_VIOLATION);
2953 static BOOL WINAPI CRYPT_AsnEncodeRsaPubKey(DWORD dwCertEncodingType,
2954 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2955 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2961 const BLOBHEADER *hdr = pvStructInfo;
2963 if (hdr->bType != PUBLICKEYBLOB)
2965 SetLastError(E_INVALIDARG);
2970 const RSAPUBKEY *rsaPubKey = (const RSAPUBKEY *)
2971 ((const BYTE *)pvStructInfo + sizeof(BLOBHEADER));
2972 CRYPT_INTEGER_BLOB blob = { rsaPubKey->bitlen / 8,
2973 (BYTE *)pvStructInfo + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) };
2974 struct AsnEncodeSequenceItem items[] = {
2975 { &blob, CRYPT_AsnEncodeUnsignedInteger, 0 },
2976 { &rsaPubKey->pubexp, CRYPT_AsnEncodeInt, 0 },
2979 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
2980 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
2986 SetLastError(STATUS_ACCESS_VIOLATION);
2993 BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
2994 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2995 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3001 const CRYPT_DATA_BLOB *blob = pvStructInfo;
3002 DWORD bytesNeeded, lenBytes;
3004 TRACE("(%d, %p), %08x, %p, %p, %d\n", blob->cbData, blob->pbData,
3005 dwFlags, pEncodePara, pbEncoded, *pcbEncoded);
3007 CRYPT_EncodeLen(blob->cbData, NULL, &lenBytes);
3008 bytesNeeded = 1 + lenBytes + blob->cbData;
3011 *pcbEncoded = bytesNeeded;
3016 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3017 pcbEncoded, bytesNeeded)))
3019 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3020 pbEncoded = *(BYTE **)pbEncoded;
3021 *pbEncoded++ = ASN_OCTETSTRING;
3022 CRYPT_EncodeLen(blob->cbData, pbEncoded, &lenBytes);
3023 pbEncoded += lenBytes;
3025 memcpy(pbEncoded, blob->pbData, blob->cbData);
3031 SetLastError(STATUS_ACCESS_VIOLATION);
3035 TRACE("returning %d (%08x)\n", ret, GetLastError());
3039 static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
3040 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3041 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3047 const CRYPT_BIT_BLOB *blob = pvStructInfo;
3048 DWORD bytesNeeded, lenBytes, dataBytes;
3051 /* yep, MS allows cUnusedBits to be >= 8 */
3052 if (!blob->cUnusedBits)
3054 dataBytes = blob->cbData;
3057 else if (blob->cbData * 8 > blob->cUnusedBits)
3059 dataBytes = (blob->cbData * 8 - blob->cUnusedBits) / 8 + 1;
3060 unusedBits = blob->cUnusedBits >= 8 ? blob->cUnusedBits / 8 :
3068 CRYPT_EncodeLen(dataBytes + 1, NULL, &lenBytes);
3069 bytesNeeded = 1 + lenBytes + dataBytes + 1;
3072 *pcbEncoded = bytesNeeded;
3077 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3078 pcbEncoded, bytesNeeded)))
3080 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3081 pbEncoded = *(BYTE **)pbEncoded;
3082 *pbEncoded++ = ASN_BITSTRING;
3083 CRYPT_EncodeLen(dataBytes + 1, pbEncoded, &lenBytes);
3084 pbEncoded += lenBytes;
3085 *pbEncoded++ = unusedBits;
3088 BYTE mask = 0xff << unusedBits;
3092 memcpy(pbEncoded, blob->pbData, dataBytes - 1);
3093 pbEncoded += dataBytes - 1;
3095 *pbEncoded = *(blob->pbData + dataBytes - 1) & mask;
3102 SetLastError(STATUS_ACCESS_VIOLATION);
3109 static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
3110 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3111 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3117 const CRYPT_BIT_BLOB *blob = pvStructInfo;
3118 CRYPT_BIT_BLOB newBlob = { blob->cbData, NULL, blob->cUnusedBits };
3123 newBlob.pbData = CryptMemAlloc(newBlob.cbData);
3128 for (i = 0; i < newBlob.cbData; i++)
3129 newBlob.pbData[newBlob.cbData - i - 1] = blob->pbData[i];
3135 ret = CRYPT_AsnEncodeBits(dwCertEncodingType, lpszStructType,
3136 &newBlob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3137 CryptMemFree(newBlob.pbData);
3141 SetLastError(STATUS_ACCESS_VIOLATION);
3148 static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
3149 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3150 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3152 CRYPT_INTEGER_BLOB blob = { sizeof(INT), (BYTE *)pvStructInfo };
3154 return CRYPT_AsnEncodeInteger(dwCertEncodingType, X509_MULTI_BYTE_INTEGER,
3155 &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3158 static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
3159 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3160 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3166 DWORD significantBytes, lenBytes, bytesNeeded;
3169 const CRYPT_INTEGER_BLOB *blob = pvStructInfo;
3171 significantBytes = blob->cbData;
3172 if (significantBytes)
3174 if (blob->pbData[significantBytes - 1] & 0x80)
3176 /* negative, lop off leading (little-endian) 0xffs */
3177 for (; significantBytes > 0 &&
3178 blob->pbData[significantBytes - 1] == 0xff; significantBytes--)
3180 if (blob->pbData[significantBytes - 1] < 0x80)
3188 /* positive, lop off leading (little-endian) zeroes */
3189 for (; significantBytes > 0 &&
3190 !blob->pbData[significantBytes - 1]; significantBytes--)
3192 if (significantBytes == 0)
3193 significantBytes = 1;
3194 if (blob->pbData[significantBytes - 1] > 0x7f)
3202 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
3204 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
3205 bytesNeeded = 1 + lenBytes + significantBytes;
3210 *pcbEncoded = bytesNeeded;
3215 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3216 pcbEncoded, bytesNeeded)))
3218 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3219 pbEncoded = *(BYTE **)pbEncoded;
3220 *pbEncoded++ = ASN_INTEGER;
3223 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
3224 pbEncoded += lenBytes;
3225 *pbEncoded++ = padByte;
3229 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
3230 pbEncoded += lenBytes;
3232 for (; significantBytes > 0; significantBytes--)
3233 *(pbEncoded++) = blob->pbData[significantBytes - 1];
3239 SetLastError(STATUS_ACCESS_VIOLATION);
3246 static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
3247 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3248 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3254 DWORD significantBytes, lenBytes, bytesNeeded;
3256 const CRYPT_INTEGER_BLOB *blob = pvStructInfo;
3258 significantBytes = blob->cbData;
3259 if (significantBytes)
3261 /* positive, lop off leading (little-endian) zeroes */
3262 for (; significantBytes > 0 && !blob->pbData[significantBytes - 1];
3265 if (significantBytes == 0)
3266 significantBytes = 1;
3267 if (blob->pbData[significantBytes - 1] > 0x7f)
3271 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
3273 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
3274 bytesNeeded = 1 + lenBytes + significantBytes;
3279 *pcbEncoded = bytesNeeded;
3284 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3285 pcbEncoded, bytesNeeded)))
3287 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3288 pbEncoded = *(BYTE **)pbEncoded;
3289 *pbEncoded++ = ASN_INTEGER;
3292 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
3293 pbEncoded += lenBytes;
3298 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
3299 pbEncoded += lenBytes;
3301 for (; significantBytes > 0; significantBytes--)
3302 *(pbEncoded++) = blob->pbData[significantBytes - 1];
3308 SetLastError(STATUS_ACCESS_VIOLATION);
3315 static BOOL WINAPI CRYPT_AsnEncodeEnumerated(DWORD dwCertEncodingType,
3316 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3317 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3319 CRYPT_INTEGER_BLOB blob;
3322 /* Encode as an unsigned integer, then change the tag to enumerated */
3323 blob.cbData = sizeof(DWORD);
3324 blob.pbData = (BYTE *)pvStructInfo;
3325 ret = CRYPT_AsnEncodeUnsignedInteger(dwCertEncodingType,
3326 X509_MULTI_BYTE_UINT, &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3327 if (ret && pbEncoded)
3329 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3330 pbEncoded = *(BYTE **)pbEncoded;
3331 pbEncoded[0] = ASN_ENUMERATED;
3336 static BOOL WINAPI CRYPT_AsnEncodeUtcTime(DWORD dwCertEncodingType,
3337 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3338 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3345 /* sorry, magic number: enough for tag, len, YYMMDDHHMMSSZ\0. I use a
3346 * temporary buffer because the output buffer is not NULL-terminated.
3349 static const DWORD bytesNeeded = sizeof(buf) - 1;
3353 *pcbEncoded = bytesNeeded;
3358 /* Sanity check the year, this is a two-digit year format */
3359 ret = FileTimeToSystemTime(pvStructInfo, &sysTime);
3360 if (ret && (sysTime.wYear < 1950 || sysTime.wYear > 2050))
3362 SetLastError(CRYPT_E_BAD_ENCODE);
3367 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3368 pbEncoded, pcbEncoded, bytesNeeded)))
3370 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3371 pbEncoded = *(BYTE **)pbEncoded;
3372 buf[0] = ASN_UTCTIME;
3373 buf[1] = bytesNeeded - 2;
3374 snprintf(buf + 2, sizeof(buf) - 2,
3375 "%02d%02d%02d%02d%02d%02dZ", sysTime.wYear >= 2000 ?
3376 sysTime.wYear - 2000 : sysTime.wYear - 1900,
3377 sysTime.wMonth, sysTime.wDay, sysTime.wHour,
3378 sysTime.wMinute, sysTime.wSecond);
3379 memcpy(pbEncoded, buf, bytesNeeded);
3386 SetLastError(STATUS_ACCESS_VIOLATION);
3393 static BOOL CRYPT_AsnEncodeGeneralizedTime(DWORD dwCertEncodingType,
3394 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3395 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3402 /* sorry, magic number: enough for tag, len, YYYYMMDDHHMMSSZ\0. I use a
3403 * temporary buffer because the output buffer is not NULL-terminated.
3406 static const DWORD bytesNeeded = sizeof(buf) - 1;
3410 *pcbEncoded = bytesNeeded;
3415 ret = FileTimeToSystemTime(pvStructInfo, &sysTime);
3417 ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3418 pcbEncoded, bytesNeeded);
3421 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3422 pbEncoded = *(BYTE **)pbEncoded;
3423 buf[0] = ASN_GENERALTIME;
3424 buf[1] = bytesNeeded - 2;
3425 snprintf(buf + 2, sizeof(buf) - 2, "%04d%02d%02d%02d%02d%02dZ",
3426 sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour,
3427 sysTime.wMinute, sysTime.wSecond);
3428 memcpy(pbEncoded, buf, bytesNeeded);
3434 SetLastError(STATUS_ACCESS_VIOLATION);
3441 static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
3442 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3443 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3451 /* Check the year, if it's in the UTCTime range call that encode func */
3452 if (!FileTimeToSystemTime(pvStructInfo, &sysTime))
3454 if (sysTime.wYear >= 1950 && sysTime.wYear <= 2050)
3455 ret = CRYPT_AsnEncodeUtcTime(dwCertEncodingType, lpszStructType,
3456 pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3458 ret = CRYPT_AsnEncodeGeneralizedTime(dwCertEncodingType,
3459 lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
3464 SetLastError(STATUS_ACCESS_VIOLATION);
3471 static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
3472 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3473 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3479 DWORD bytesNeeded, dataLen, lenBytes, i;
3480 const CRYPT_SEQUENCE_OF_ANY *seq = pvStructInfo;
3482 for (i = 0, dataLen = 0; i < seq->cValue; i++)
3483 dataLen += seq->rgValue[i].cbData;
3484 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
3485 bytesNeeded = 1 + lenBytes + dataLen;
3488 *pcbEncoded = bytesNeeded;
3493 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3494 pcbEncoded, bytesNeeded)))
3496 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3497 pbEncoded = *(BYTE **)pbEncoded;
3498 *pbEncoded++ = ASN_SEQUENCEOF;
3499 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
3500 pbEncoded += lenBytes;
3501 for (i = 0; i < seq->cValue; i++)
3503 memcpy(pbEncoded, seq->rgValue[i].pbData,
3504 seq->rgValue[i].cbData);
3505 pbEncoded += seq->rgValue[i].cbData;
3512 SetLastError(STATUS_ACCESS_VIOLATION);
3519 static BOOL CRYPT_AsnEncodeDistPoint(const CRL_DIST_POINT *distPoint,
3520 BYTE *pbEncoded, DWORD *pcbEncoded)
3523 struct AsnEncodeSequenceItem items[3] = { { 0 } };
3524 struct AsnConstructedItem constructed = { 0 };
3525 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
3526 DWORD cItem = 0, cSwapped = 0;
3528 switch (distPoint->DistPointName.dwDistPointNameChoice)
3530 case CRL_DIST_POINT_NO_NAME:
3533 case CRL_DIST_POINT_FULL_NAME:
3534 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3535 swapped[cSwapped].pvStructInfo = &distPoint->DistPointName.u.FullName;
3536 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
3537 constructed.tag = 0;
3538 constructed.pvStructInfo = &swapped[cSwapped];
3539 constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
3540 items[cItem].pvStructInfo = &constructed;
3541 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
3545 case CRL_DIST_POINT_ISSUER_RDN_NAME:
3546 FIXME("unimplemented for CRL_DIST_POINT_ISSUER_RDN_NAME\n");
3552 if (ret && distPoint->ReasonFlags.cbData)
3554 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3555 swapped[cSwapped].pvStructInfo = &distPoint->ReasonFlags;
3556 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
3557 items[cItem].pvStructInfo = &swapped[cSwapped];
3558 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3562 if (ret && distPoint->CRLIssuer.cAltEntry)
3564 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 2;
3565 swapped[cSwapped].pvStructInfo = &distPoint->CRLIssuer;
3566 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
3567 items[cItem].pvStructInfo = &swapped[cSwapped];
3568 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3573 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
3574 pbEncoded, pcbEncoded);
3578 static BOOL WINAPI CRYPT_AsnEncodeCRLDistPoints(DWORD dwCertEncodingType,
3579 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3580 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3586 const CRL_DIST_POINTS_INFO *info = pvStructInfo;
3588 if (!info->cDistPoint)
3590 SetLastError(E_INVALIDARG);
3595 DWORD bytesNeeded, dataLen, lenBytes, i;
3598 for (i = 0, dataLen = 0; ret && i < info->cDistPoint; i++)
3602 ret = CRYPT_AsnEncodeDistPoint(&info->rgDistPoint[i], NULL,
3606 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
3608 /* Have to propagate index of failing character */
3614 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
3615 bytesNeeded = 1 + lenBytes + dataLen;
3618 *pcbEncoded = bytesNeeded;
3623 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3624 pbEncoded, pcbEncoded, bytesNeeded)))
3626 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3627 pbEncoded = *(BYTE **)pbEncoded;
3628 *pbEncoded++ = ASN_SEQUENCEOF;
3629 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
3630 pbEncoded += lenBytes;
3631 for (i = 0; ret && i < info->cDistPoint; i++)
3633 DWORD len = dataLen;
3635 ret = CRYPT_AsnEncodeDistPoint(
3636 &info->rgDistPoint[i], pbEncoded, &len);
3650 SetLastError(STATUS_ACCESS_VIOLATION);
3657 static BOOL WINAPI CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType,
3658 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3659 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3665 const CERT_ENHKEY_USAGE *usage = pvStructInfo;
3666 DWORD bytesNeeded = 0, lenBytes, size, i;
3669 for (i = 0; ret && i < usage->cUsageIdentifier; i++)
3671 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
3672 usage->rgpszUsageIdentifier[i],
3673 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &size);
3675 bytesNeeded += size;
3677 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
3678 bytesNeeded += 1 + lenBytes;
3682 *pcbEncoded = bytesNeeded;
3685 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3686 pbEncoded, pcbEncoded, bytesNeeded)))
3688 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3689 pbEncoded = *(BYTE **)pbEncoded;
3690 *pbEncoded++ = ASN_SEQUENCEOF;
3691 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
3693 pbEncoded += lenBytes;
3694 for (i = 0; ret && i < usage->cUsageIdentifier; i++)
3697 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
3698 usage->rgpszUsageIdentifier[i],
3699 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pbEncoded,
3704 bytesNeeded -= size;
3713 SetLastError(STATUS_ACCESS_VIOLATION);
3720 static BOOL WINAPI CRYPT_AsnEncodeIssuingDistPoint(DWORD dwCertEncodingType,
3721 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3722 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3728 const CRL_ISSUING_DIST_POINT *point = pvStructInfo;
3729 struct AsnEncodeSequenceItem items[6] = { { 0 } };
3730 struct AsnConstructedItem constructed = { 0 };
3731 struct AsnEncodeTagSwappedItem swapped[5] = { { 0 } };
3732 DWORD cItem = 0, cSwapped = 0;
3735 switch (point->DistPointName.dwDistPointNameChoice)
3737 case CRL_DIST_POINT_NO_NAME:
3740 case CRL_DIST_POINT_FULL_NAME:
3741 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3742 swapped[cSwapped].pvStructInfo = &point->DistPointName.u.FullName;
3743 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
3744 constructed.tag = 0;
3745 constructed.pvStructInfo = &swapped[cSwapped];
3746 constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
3747 items[cItem].pvStructInfo = &constructed;
3748 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
3753 SetLastError(E_INVALIDARG);
3756 if (ret && point->fOnlyContainsUserCerts)
3758 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3759 swapped[cSwapped].pvStructInfo = &point->fOnlyContainsUserCerts;
3760 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3761 items[cItem].pvStructInfo = &swapped[cSwapped];
3762 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3766 if (ret && point->fOnlyContainsCACerts)
3768 swapped[cSwapped].tag = ASN_CONTEXT | 2;
3769 swapped[cSwapped].pvStructInfo = &point->fOnlyContainsCACerts;
3770 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3771 items[cItem].pvStructInfo = &swapped[cSwapped];
3772 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3776 if (ret && point->OnlySomeReasonFlags.cbData)
3778 swapped[cSwapped].tag = ASN_CONTEXT | 3;
3779 swapped[cSwapped].pvStructInfo = &point->OnlySomeReasonFlags;
3780 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
3781 items[cItem].pvStructInfo = &swapped[cSwapped];
3782 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3786 if (ret && point->fIndirectCRL)
3788 swapped[cSwapped].tag = ASN_CONTEXT | 4;
3789 swapped[cSwapped].pvStructInfo = &point->fIndirectCRL;
3790 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3791 items[cItem].pvStructInfo = &swapped[cSwapped];
3792 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3797 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
3798 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3802 SetLastError(STATUS_ACCESS_VIOLATION);
3809 static BOOL CRYPT_AsnEncodeGeneralSubtree(DWORD dwCertEncodingType,
3810 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3811 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3814 const CERT_GENERAL_SUBTREE *subtree = pvStructInfo;
3815 struct AsnEncodeSequenceItem items[3] = {
3816 { &subtree->Base, CRYPT_AsnEncodeAltNameEntry, 0 },
3819 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
3820 DWORD cItem = 1, cSwapped = 0;
3822 if (subtree->dwMinimum)
3824 swapped[cSwapped].tag = ASN_CONTEXT | 0;
3825 swapped[cSwapped].pvStructInfo = &subtree->dwMinimum;
3826 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
3827 items[cItem].pvStructInfo = &swapped[cSwapped];
3828 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3832 if (subtree->fMaximum)
3834 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3835 swapped[cSwapped].pvStructInfo = &subtree->dwMaximum;
3836 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
3837 items[cItem].pvStructInfo = &swapped[cSwapped];
3838 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3842 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem, dwFlags,
3843 pEncodePara, pbEncoded, pcbEncoded);
3847 static BOOL WINAPI CRYPT_AsnEncodeNameConstraints(DWORD dwCertEncodingType,
3848 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3849 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3852 CRYPT_BLOB_ARRAY permitted = { 0, NULL }, excluded = { 0, NULL };
3854 TRACE("%p\n", pvStructInfo);
3858 const CERT_NAME_CONSTRAINTS_INFO *constraints = pvStructInfo;
3859 struct AsnEncodeSequenceItem items[2] = { { 0 } };
3860 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
3861 DWORD i, cItem = 0, cSwapped = 0;
3864 if (constraints->cPermittedSubtree)
3866 permitted.rgBlob = CryptMemAlloc(
3867 constraints->cPermittedSubtree * sizeof(CRYPT_DER_BLOB));
3868 if (permitted.rgBlob)
3870 permitted.cBlob = constraints->cPermittedSubtree;
3871 memset(permitted.rgBlob, 0,
3872 permitted.cBlob * sizeof(CRYPT_DER_BLOB));
3873 for (i = 0; ret && i < permitted.cBlob; i++)
3874 ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
3875 NULL, &constraints->rgPermittedSubtree[i],
3876 CRYPT_ENCODE_ALLOC_FLAG, NULL,
3877 (BYTE *)&permitted.rgBlob[i].pbData,
3878 &permitted.rgBlob[i].cbData);
3881 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3882 swapped[cSwapped].pvStructInfo = &permitted;
3883 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
3884 items[cItem].pvStructInfo = &swapped[cSwapped];
3885 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3893 if (constraints->cExcludedSubtree)
3895 excluded.rgBlob = CryptMemAlloc(
3896 constraints->cExcludedSubtree * sizeof(CRYPT_DER_BLOB));
3897 if (excluded.rgBlob)
3899 excluded.cBlob = constraints->cExcludedSubtree;
3900 memset(excluded.rgBlob, 0,
3901 excluded.cBlob * sizeof(CRYPT_DER_BLOB));
3902 for (i = 0; ret && i < excluded.cBlob; i++)
3903 ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
3904 NULL, &constraints->rgExcludedSubtree[i],
3905 CRYPT_ENCODE_ALLOC_FLAG, NULL,
3906 (BYTE *)&excluded.rgBlob[i].pbData,
3907 &excluded.rgBlob[i].cbData);
3910 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
3911 swapped[cSwapped].pvStructInfo = &excluded;
3912 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
3913 items[cItem].pvStructInfo = &swapped[cSwapped];
3914 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3923 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
3924 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3925 for (i = 0; i < permitted.cBlob; i++)
3926 LocalFree(permitted.rgBlob[i].pbData);
3927 for (i = 0; i < excluded.cBlob; i++)
3928 LocalFree(excluded.rgBlob[i].pbData);
3932 SetLastError(STATUS_ACCESS_VIOLATION);
3935 CryptMemFree(permitted.rgBlob);
3936 CryptMemFree(excluded.rgBlob);
3937 TRACE("returning %d\n", ret);
3941 static BOOL WINAPI CRYPT_AsnEncodeIssuerSerialNumber(
3942 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
3943 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
3947 const CERT_ISSUER_SERIAL_NUMBER *issuerSerial = pvStructInfo;
3948 struct AsnEncodeSequenceItem items[] = {
3949 { &issuerSerial->Issuer, CRYPT_CopyEncodedBlob, 0 },
3950 { &issuerSerial->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
3953 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
3954 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
3959 static BOOL WINAPI CRYPT_AsnEncodePKCSSignerInfo(DWORD dwCertEncodingType,
3960 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3961 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3965 if (!(dwCertEncodingType & PKCS_7_ASN_ENCODING))
3967 SetLastError(E_INVALIDARG);
3973 const CMSG_SIGNER_INFO *info = pvStructInfo;
3975 if (!info->Issuer.cbData)
3976 SetLastError(E_INVALIDARG);
3979 struct AsnEncodeSequenceItem items[7] = {
3980 { &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
3981 { &info->Issuer, CRYPT_AsnEncodeIssuerSerialNumber, 0 },
3982 { &info->HashAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
3985 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
3986 DWORD cItem = 3, cSwapped = 0;
3988 if (info->AuthAttrs.cAttr)
3990 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3991 swapped[cSwapped].pvStructInfo = &info->AuthAttrs;
3992 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
3993 items[cItem].pvStructInfo = &swapped[cSwapped];
3994 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3998 items[cItem].pvStructInfo = &info->HashEncryptionAlgorithm;
3999 items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4001 items[cItem].pvStructInfo = &info->EncryptedHash;
4002 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
4004 if (info->UnauthAttrs.cAttr)
4006 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
4007 swapped[cSwapped].pvStructInfo = &info->UnauthAttrs;
4008 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4009 items[cItem].pvStructInfo = &swapped[cSwapped];
4010 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4014 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
4015 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4020 SetLastError(STATUS_ACCESS_VIOLATION);
4026 static BOOL WINAPI CRYPT_AsnEncodeCMSSignerInfo(DWORD dwCertEncodingType,
4027 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
4028 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
4032 if (!(dwCertEncodingType & PKCS_7_ASN_ENCODING))
4034 SetLastError(E_INVALIDARG);
4040 const CMSG_CMS_SIGNER_INFO *info = pvStructInfo;
4042 if (info->SignerId.dwIdChoice != CERT_ID_ISSUER_SERIAL_NUMBER &&
4043 info->SignerId.dwIdChoice != CERT_ID_KEY_IDENTIFIER)
4044 SetLastError(E_INVALIDARG);
4045 else if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER &&
4046 !info->SignerId.u.IssuerSerialNumber.Issuer.cbData)
4047 SetLastError(E_INVALIDARG);
4050 struct AsnEncodeSequenceItem items[7] = {
4051 { &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
4053 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
4054 DWORD cItem = 1, cSwapped = 0;
4056 if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
4058 items[cItem].pvStructInfo =
4059 &info->SignerId.u.IssuerSerialNumber.Issuer;
4060 items[cItem].encodeFunc =
4061 CRYPT_AsnEncodeIssuerSerialNumber;
4066 swapped[cSwapped].tag = ASN_CONTEXT | 0;
4067 swapped[cSwapped].pvStructInfo = &info->SignerId.u.KeyId;
4068 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeOctets;
4069 items[cItem].pvStructInfo = &swapped[cSwapped];
4070 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4074 items[cItem].pvStructInfo = &info->HashAlgorithm;
4075 items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4077 if (info->AuthAttrs.cAttr)
4079 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
4080 swapped[cSwapped].pvStructInfo = &info->AuthAttrs;
4081 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4082 items[cItem].pvStructInfo = &swapped[cSwapped];
4083 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4087 items[cItem].pvStructInfo = &info->HashEncryptionAlgorithm;
4088 items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4090 items[cItem].pvStructInfo = &info->EncryptedHash;
4091 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
4093 if (info->UnauthAttrs.cAttr)
4095 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
4096 swapped[cSwapped].pvStructInfo = &info->UnauthAttrs;
4097 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4098 items[cItem].pvStructInfo = &swapped[cSwapped];
4099 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4103 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
4104 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4109 SetLastError(STATUS_ACCESS_VIOLATION);
4115 BOOL CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO *signedInfo, void *pvData,
4118 struct AsnEncodeSequenceItem items[7] = {
4119 { &signedInfo->version, CRYPT_AsnEncodeInt, 0 },
4121 struct DERSetDescriptor digestAlgorithmsSet = { 0 }, certSet = { 0 };
4122 struct DERSetDescriptor crlSet = { 0 }, signerSet = { 0 };
4123 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
4124 DWORD cItem = 1, cSwapped = 0;
4127 if (signedInfo->cSignerInfo)
4129 digestAlgorithmsSet.cItems = signedInfo->cSignerInfo;
4130 digestAlgorithmsSet.items = signedInfo->rgSignerInfo;
4131 digestAlgorithmsSet.itemSize = sizeof(CMSG_CMS_SIGNER_INFO);
4132 digestAlgorithmsSet.itemOffset =
4133 offsetof(CMSG_CMS_SIGNER_INFO, HashAlgorithm);
4134 digestAlgorithmsSet.encode = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4135 items[cItem].pvStructInfo = &digestAlgorithmsSet;
4136 items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4139 items[cItem].pvStructInfo = &signedInfo->content;
4140 items[cItem].encodeFunc = CRYPT_AsnEncodePKCSContentInfoInternal;
4142 if (signedInfo->cCertEncoded)
4144 certSet.cItems = signedInfo->cCertEncoded;
4145 certSet.items = signedInfo->rgCertEncoded;
4146 certSet.itemSize = sizeof(CERT_BLOB);
4147 certSet.itemOffset = 0;
4148 certSet.encode = CRYPT_CopyEncodedBlob;
4149 swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 0;
4150 swapped[cSwapped].pvStructInfo = &certSet;
4151 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4152 items[cItem].pvStructInfo = &swapped[cSwapped];
4153 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4157 if (signedInfo->cCrlEncoded)
4159 crlSet.cItems = signedInfo->cCrlEncoded;
4160 crlSet.items = signedInfo->rgCrlEncoded;
4161 crlSet.itemSize = sizeof(CRL_BLOB);
4162 crlSet.itemOffset = 0;
4163 crlSet.encode = CRYPT_CopyEncodedBlob;
4164 swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 1;
4165 swapped[cSwapped].pvStructInfo = &crlSet;
4166 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4167 items[cItem].pvStructInfo = &swapped[cSwapped];
4168 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4172 if (ret && signedInfo->cSignerInfo)
4174 signerSet.cItems = signedInfo->cSignerInfo;
4175 signerSet.items = signedInfo->rgSignerInfo;
4176 signerSet.itemSize = sizeof(CMSG_CMS_SIGNER_INFO);
4177 signerSet.itemOffset = 0;
4178 signerSet.encode = CRYPT_AsnEncodeCMSSignerInfo;
4179 items[cItem].pvStructInfo = &signerSet;
4180 items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4184 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
4185 items, cItem, 0, NULL, pvData, pcbData);
4190 static CryptEncodeObjectExFunc CRYPT_GetBuiltinEncoder(DWORD dwCertEncodingType,
4191 LPCSTR lpszStructType)
4193 CryptEncodeObjectExFunc encodeFunc = NULL;
4195 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
4196 && (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
4198 SetLastError(ERROR_FILE_NOT_FOUND);
4202 if (!HIWORD(lpszStructType))
4204 switch (LOWORD(lpszStructType))
4206 case LOWORD(X509_CERT):
4207 encodeFunc = CRYPT_AsnEncodeCert;
4209 case LOWORD(X509_CERT_TO_BE_SIGNED):
4210 encodeFunc = CRYPT_AsnEncodeCertInfo;
4212 case LOWORD(X509_CERT_CRL_TO_BE_SIGNED):
4213 encodeFunc = CRYPT_AsnEncodeCRLInfo;
4215 case LOWORD(X509_EXTENSIONS):
4216 encodeFunc = CRYPT_AsnEncodeExtensions;
4218 case LOWORD(X509_NAME_VALUE):
4219 encodeFunc = CRYPT_AsnEncodeNameValue;
4221 case LOWORD(X509_NAME):
4222 encodeFunc = CRYPT_AsnEncodeName;
4224 case LOWORD(X509_PUBLIC_KEY_INFO):
4225 encodeFunc = CRYPT_AsnEncodePubKeyInfo;
4227 case LOWORD(X509_AUTHORITY_KEY_ID):
4228 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
4230 case LOWORD(X509_ALTERNATE_NAME):
4231 encodeFunc = CRYPT_AsnEncodeAltName;
4233 case LOWORD(X509_BASIC_CONSTRAINTS):
4234 encodeFunc = CRYPT_AsnEncodeBasicConstraints;
4236 case LOWORD(X509_BASIC_CONSTRAINTS2):
4237 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
4239 case LOWORD(X509_CERT_POLICIES):
4240 encodeFunc = CRYPT_AsnEncodeCertPolicies;
4242 case LOWORD(RSA_CSP_PUBLICKEYBLOB):
4243 encodeFunc = CRYPT_AsnEncodeRsaPubKey;
4245 case LOWORD(X509_UNICODE_NAME):
4246 encodeFunc = CRYPT_AsnEncodeUnicodeName;
4248 case LOWORD(PKCS_CONTENT_INFO):
4249 encodeFunc = CRYPT_AsnEncodePKCSContentInfo;
4251 case LOWORD(PKCS_ATTRIBUTE):
4252 encodeFunc = CRYPT_AsnEncodePKCSAttribute;
4254 case LOWORD(X509_UNICODE_NAME_VALUE):
4255 encodeFunc = CRYPT_AsnEncodeUnicodeNameValue;
4257 case LOWORD(X509_OCTET_STRING):
4258 encodeFunc = CRYPT_AsnEncodeOctets;
4260 case LOWORD(X509_BITS):
4261 case LOWORD(X509_KEY_USAGE):
4262 encodeFunc = CRYPT_AsnEncodeBits;
4264 case LOWORD(X509_INTEGER):
4265 encodeFunc = CRYPT_AsnEncodeInt;
4267 case LOWORD(X509_MULTI_BYTE_INTEGER):
4268 encodeFunc = CRYPT_AsnEncodeInteger;
4270 case LOWORD(X509_MULTI_BYTE_UINT):
4271 encodeFunc = CRYPT_AsnEncodeUnsignedInteger;
4273 case LOWORD(X509_ENUMERATED):
4274 encodeFunc = CRYPT_AsnEncodeEnumerated;
4276 case LOWORD(X509_CHOICE_OF_TIME):
4277 encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
4279 case LOWORD(X509_AUTHORITY_KEY_ID2):
4280 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
4282 case LOWORD(X509_AUTHORITY_INFO_ACCESS):
4283 encodeFunc = CRYPT_AsnEncodeAuthorityInfoAccess;
4285 case LOWORD(X509_SEQUENCE_OF_ANY):
4286 encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
4288 case LOWORD(PKCS_UTC_TIME):
4289 encodeFunc = CRYPT_AsnEncodeUtcTime;
4291 case LOWORD(X509_CRL_DIST_POINTS):
4292 encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
4294 case LOWORD(X509_ENHANCED_KEY_USAGE):
4295 encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
4297 case LOWORD(PKCS_CTL):
4298 encodeFunc = CRYPT_AsnEncodeCTL;
4300 case LOWORD(PKCS_SMIME_CAPABILITIES):
4301 encodeFunc = CRYPT_AsnEncodeSMIMECapabilities;
4303 case LOWORD(X509_PKIX_POLICY_QUALIFIER_USERNOTICE):
4304 encodeFunc = CRYPT_AsnEncodePolicyQualifierUserNotice;
4306 case LOWORD(PKCS_ATTRIBUTES):
4307 encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4309 case LOWORD(X509_ISSUING_DIST_POINT):
4310 encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
4312 case LOWORD(X509_NAME_CONSTRAINTS):
4313 encodeFunc = CRYPT_AsnEncodeNameConstraints;
4315 case LOWORD(PKCS7_SIGNER_INFO):
4316 encodeFunc = CRYPT_AsnEncodePKCSSignerInfo;
4318 case LOWORD(CMS_SIGNER_INFO):
4319 encodeFunc = CRYPT_AsnEncodeCMSSignerInfo;
4323 else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
4324 encodeFunc = CRYPT_AsnEncodeExtensions;
4325 else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
4326 encodeFunc = CRYPT_AsnEncodeUtcTime;
4327 else if (!strcmp(lpszStructType, szOID_RSA_SMIMECapabilities))
4328 encodeFunc = CRYPT_AsnEncodeUtcTime;
4329 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER))
4330 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
4331 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER2))
4332 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
4333 else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
4334 encodeFunc = CRYPT_AsnEncodeEnumerated;
4335 else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
4336 encodeFunc = CRYPT_AsnEncodeBits;
4337 else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
4338 encodeFunc = CRYPT_AsnEncodeOctets;
4339 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS))
4340 encodeFunc = CRYPT_AsnEncodeBasicConstraints;
4341 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
4342 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
4343 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
4344 encodeFunc = CRYPT_AsnEncodeAltName;
4345 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
4346 encodeFunc = CRYPT_AsnEncodeAltName;
4347 else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
4348 encodeFunc = CRYPT_AsnEncodeAltName;
4349 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
4350 encodeFunc = CRYPT_AsnEncodeAltName;
4351 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
4352 encodeFunc = CRYPT_AsnEncodeAltName;
4353 else if (!strcmp(lpszStructType, szOID_CRL_DIST_POINTS))
4354 encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
4355 else if (!strcmp(lpszStructType, szOID_CERT_POLICIES))
4356 encodeFunc = CRYPT_AsnEncodeCertPolicies;
4357 else if (!strcmp(lpszStructType, szOID_ENHANCED_KEY_USAGE))
4358 encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
4359 else if (!strcmp(lpszStructType, szOID_ISSUING_DIST_POINT))
4360 encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
4361 else if (!strcmp(lpszStructType, szOID_NAME_CONSTRAINTS))
4362 encodeFunc = CRYPT_AsnEncodeNameConstraints;
4363 else if (!strcmp(lpszStructType, szOID_AUTHORITY_INFO_ACCESS))
4364 encodeFunc = CRYPT_AsnEncodeAuthorityInfoAccess;
4365 else if (!strcmp(lpszStructType, szOID_PKIX_POLICY_QUALIFIER_USERNOTICE))
4366 encodeFunc = CRYPT_AsnEncodePolicyQualifierUserNotice;
4367 else if (!strcmp(lpszStructType, szOID_CTL))
4368 encodeFunc = CRYPT_AsnEncodeCTL;
4372 static CryptEncodeObjectFunc CRYPT_LoadEncoderFunc(DWORD dwCertEncodingType,
4373 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
4375 static HCRYPTOIDFUNCSET set = NULL;
4376 CryptEncodeObjectFunc encodeFunc = NULL;
4379 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_FUNC, 0);
4380 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
4381 (void **)&encodeFunc, hFunc);
4385 static CryptEncodeObjectExFunc CRYPT_LoadEncoderExFunc(DWORD dwCertEncodingType,
4386 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
4388 static HCRYPTOIDFUNCSET set = NULL;
4389 CryptEncodeObjectExFunc encodeFunc = NULL;
4392 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_EX_FUNC, 0);
4393 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
4394 (void **)&encodeFunc, hFunc);
4398 BOOL WINAPI CryptEncodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
4399 const void *pvStructInfo, BYTE *pbEncoded, DWORD *pcbEncoded)
4402 HCRYPTOIDFUNCADDR hFunc = NULL;
4403 CryptEncodeObjectFunc pCryptEncodeObject = NULL;
4404 CryptEncodeObjectExFunc pCryptEncodeObjectEx = NULL;
4406 TRACE_(crypt)("(0x%08x, %s, %p, %p, %p)\n", dwCertEncodingType,
4407 debugstr_a(lpszStructType), pvStructInfo, pbEncoded,
4410 if (!pbEncoded && !pcbEncoded)
4412 SetLastError(ERROR_INVALID_PARAMETER);
4416 if (!(pCryptEncodeObjectEx = CRYPT_GetBuiltinEncoder(dwCertEncodingType,
4419 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
4420 debugstr_a(lpszStructType));
4421 pCryptEncodeObject = CRYPT_LoadEncoderFunc(dwCertEncodingType,
4422 lpszStructType, &hFunc);
4423 if (!pCryptEncodeObject)
4424 pCryptEncodeObjectEx = CRYPT_LoadEncoderExFunc(dwCertEncodingType,
4425 lpszStructType, &hFunc);
4427 if (pCryptEncodeObject)
4428 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
4429 pvStructInfo, pbEncoded, pcbEncoded);
4430 else if (pCryptEncodeObjectEx)
4431 ret = pCryptEncodeObjectEx(dwCertEncodingType, lpszStructType,
4432 pvStructInfo, 0, NULL, pbEncoded, pcbEncoded);
4434 CryptFreeOIDFunctionAddress(hFunc, 0);
4435 TRACE_(crypt)("returning %d\n", ret);
4439 BOOL WINAPI CryptEncodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
4440 const void *pvStructInfo, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
4441 void *pvEncoded, DWORD *pcbEncoded)
4444 HCRYPTOIDFUNCADDR hFunc = NULL;
4445 CryptEncodeObjectExFunc encodeFunc = NULL;
4447 TRACE_(crypt)("(0x%08x, %s, %p, 0x%08x, %p, %p, %p)\n", dwCertEncodingType,
4448 debugstr_a(lpszStructType), pvStructInfo, dwFlags, pEncodePara,
4449 pvEncoded, pcbEncoded);
4451 if (!pvEncoded && !pcbEncoded)
4453 SetLastError(ERROR_INVALID_PARAMETER);
4457 SetLastError(NOERROR);
4458 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG && pvEncoded)
4459 *(BYTE **)pvEncoded = NULL;
4460 encodeFunc = CRYPT_GetBuiltinEncoder(dwCertEncodingType, lpszStructType);
4463 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
4464 debugstr_a(lpszStructType));
4465 encodeFunc = CRYPT_LoadEncoderExFunc(dwCertEncodingType, lpszStructType,
4469 ret = encodeFunc(dwCertEncodingType, lpszStructType, pvStructInfo,
4470 dwFlags, pEncodePara, pvEncoded, pcbEncoded);
4473 CryptEncodeObjectFunc pCryptEncodeObject =
4474 CRYPT_LoadEncoderFunc(dwCertEncodingType, lpszStructType, &hFunc);
4476 if (pCryptEncodeObject)
4478 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
4480 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
4481 pvStructInfo, NULL, pcbEncoded);
4482 if (ret && (ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
4483 pvEncoded, pcbEncoded, *pcbEncoded)))
4484 ret = pCryptEncodeObject(dwCertEncodingType,
4485 lpszStructType, pvStructInfo, *(BYTE **)pvEncoded,
4489 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
4490 pvStructInfo, pvEncoded, pcbEncoded);
4494 CryptFreeOIDFunctionAddress(hFunc, 0);
4495 TRACE_(crypt)("returning %d\n", ret);
4499 BOOL WINAPI PFXExportCertStore(HCERTSTORE hStore, CRYPT_DATA_BLOB *pPFX,
4500 LPCWSTR szPassword, DWORD dwFlags)
4502 return PFXExportCertStoreEx(hStore, pPFX, szPassword, NULL, dwFlags);
4505 BOOL WINAPI PFXExportCertStoreEx(HCERTSTORE hStore, CRYPT_DATA_BLOB *pPFX,
4506 LPCWSTR szPassword, void *pvReserved, DWORD dwFlags)
4508 FIXME_(crypt)("(%p, %p, %p, %p, %08x): stub\n", hStore, pPFX, szPassword,
4509 pvReserved, dwFlags);
4513 BOOL WINAPI CryptExportPublicKeyInfo(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
4514 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
4516 return CryptExportPublicKeyInfoEx(hCryptProv, dwKeySpec, dwCertEncodingType,
4517 NULL, 0, NULL, pInfo, pcbInfo);
4520 static BOOL WINAPI CRYPT_ExportRsaPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
4521 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
4522 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
4526 static CHAR oid[] = szOID_RSA_RSA;
4528 TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
4529 dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
4530 pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
4532 if (!pszPublicKeyObjId)
4533 pszPublicKeyObjId = oid;
4534 if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key)))
4538 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, NULL, &keySize);
4541 LPBYTE pubKey = CryptMemAlloc(keySize);
4545 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, pubKey,
4549 DWORD encodedLen = 0;
4551 ret = CryptEncodeObject(dwCertEncodingType,
4552 RSA_CSP_PUBLICKEYBLOB, pubKey, NULL, &encodedLen);
4555 DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) +
4556 strlen(pszPublicKeyObjId) + 1 + encodedLen;
4559 *pcbInfo = sizeNeeded;
4560 else if (*pcbInfo < sizeNeeded)
4562 SetLastError(ERROR_MORE_DATA);
4563 *pcbInfo = sizeNeeded;
4568 pInfo->Algorithm.pszObjId = (char *)pInfo +
4569 sizeof(CERT_PUBLIC_KEY_INFO);
4570 lstrcpyA(pInfo->Algorithm.pszObjId,
4572 pInfo->Algorithm.Parameters.cbData = 0;
4573 pInfo->Algorithm.Parameters.pbData = NULL;
4574 pInfo->PublicKey.pbData =
4575 (BYTE *)pInfo->Algorithm.pszObjId
4576 + lstrlenA(pInfo->Algorithm.pszObjId) + 1;
4577 pInfo->PublicKey.cbData = encodedLen;
4578 pInfo->PublicKey.cUnusedBits = 0;
4579 ret = CryptEncodeObject(dwCertEncodingType,
4580 RSA_CSP_PUBLICKEYBLOB, pubKey,
4581 pInfo->PublicKey.pbData, &pInfo->PublicKey.cbData);
4585 CryptMemFree(pubKey);
4590 CryptDestroyKey(key);
4595 typedef BOOL (WINAPI *ExportPublicKeyInfoExFunc)(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
4596 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
4597 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo);
4599 BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
4600 DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags,
4601 void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
4603 static HCRYPTOIDFUNCSET set = NULL;
4605 ExportPublicKeyInfoExFunc exportFunc = NULL;
4606 HCRYPTOIDFUNCADDR hFunc = NULL;
4608 TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
4609 dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
4610 pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
4614 SetLastError(ERROR_INVALID_PARAMETER);
4618 if (pszPublicKeyObjId)
4621 set = CryptInitOIDFunctionSet(CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC,
4623 CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId,
4624 0, (void **)&exportFunc, &hFunc);
4627 exportFunc = CRYPT_ExportRsaPublicKeyInfoEx;
4628 ret = exportFunc(hCryptProv, dwKeySpec, dwCertEncodingType,
4629 pszPublicKeyObjId, dwFlags, pvAuxInfo, pInfo, pcbInfo);
4631 CryptFreeOIDFunctionAddress(hFunc, 0);
4635 BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
4636 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
4638 return CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo,
4642 static BOOL WINAPI CRYPT_ImportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
4643 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
4644 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
4647 DWORD pubKeySize = 0;
4649 TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
4650 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
4652 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
4653 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize);
4656 LPBYTE pubKey = CryptMemAlloc(pubKeySize);
4660 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
4661 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, pubKey,
4666 ((BLOBHEADER*)pubKey)->aiKeyAlg = aiKeyAlg;
4667 ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0,
4670 CryptMemFree(pubKey);
4678 typedef BOOL (WINAPI *ImportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
4679 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
4680 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey);
4682 BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
4683 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
4684 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
4686 static HCRYPTOIDFUNCSET set = NULL;
4688 ImportPublicKeyInfoExFunc importFunc = NULL;
4689 HCRYPTOIDFUNCADDR hFunc = NULL;
4691 TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
4692 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
4695 set = CryptInitOIDFunctionSet(CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC, 0);
4696 CryptGetOIDFunctionAddress(set, dwCertEncodingType,
4697 pInfo->Algorithm.pszObjId, 0, (void **)&importFunc, &hFunc);
4699 importFunc = CRYPT_ImportRsaPublicKeyInfoEx;
4700 ret = importFunc(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags,
4703 CryptFreeOIDFunctionAddress(hFunc, 0);