2 * Copyright 2005-2007 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);
97 BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
98 BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded)
102 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
104 if (pEncodePara && pEncodePara->pfnAlloc)
105 *(BYTE **)pbEncoded = pEncodePara->pfnAlloc(bytesNeeded);
107 *(BYTE **)pbEncoded = LocalAlloc(0, bytesNeeded);
108 if (!*(BYTE **)pbEncoded)
111 *pcbEncoded = bytesNeeded;
113 else if (bytesNeeded > *pcbEncoded)
115 *pcbEncoded = bytesNeeded;
116 SetLastError(ERROR_MORE_DATA);
120 *pcbEncoded = bytesNeeded;
124 BOOL CRYPT_EncodeLen(DWORD len, BYTE *pbEncoded, DWORD *pcbEncoded)
126 DWORD bytesNeeded, significantBytes = 0;
134 for (temp = len, significantBytes = sizeof(temp); !(temp & 0xff000000);
135 temp <<= 8, significantBytes--)
137 bytesNeeded = significantBytes + 1;
141 *pcbEncoded = bytesNeeded;
144 if (*pcbEncoded < bytesNeeded)
146 SetLastError(ERROR_MORE_DATA);
150 *pbEncoded = (BYTE)len;
155 *pbEncoded++ = significantBytes | 0x80;
156 for (i = 0; i < significantBytes; i++)
158 *(pbEncoded + significantBytes - i - 1) = (BYTE)(len & 0xff);
162 *pcbEncoded = bytesNeeded;
166 BOOL WINAPI CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType,
167 struct AsnEncodeSequenceItem items[], DWORD cItem, DWORD dwFlags,
168 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
171 DWORD i, dataLen = 0;
173 TRACE("%p, %d, %08x, %p, %p, %d\n", items, cItem, dwFlags, pEncodePara,
174 pbEncoded, *pcbEncoded);
175 for (i = 0, ret = TRUE; ret && i < cItem; i++)
177 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
178 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
179 NULL, &items[i].size);
180 /* Some functions propagate their errors through the size */
182 *pcbEncoded = items[i].size;
183 dataLen += items[i].size;
187 DWORD lenBytes, bytesNeeded;
189 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
190 bytesNeeded = 1 + lenBytes + dataLen;
192 *pcbEncoded = bytesNeeded;
195 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
196 pcbEncoded, bytesNeeded)))
198 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
199 pbEncoded = *(BYTE **)pbEncoded;
200 *pbEncoded++ = ASN_SEQUENCE;
201 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
202 pbEncoded += lenBytes;
203 for (i = 0; ret && i < cItem; i++)
205 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
206 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG,
207 NULL, pbEncoded, &items[i].size);
208 /* Some functions propagate their errors through the size */
210 *pcbEncoded = items[i].size;
211 pbEncoded += items[i].size;
216 TRACE("returning %d (%08x)\n", ret, GetLastError());
220 BOOL WINAPI CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType,
221 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
222 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
225 const struct AsnConstructedItem *item =
226 (const struct AsnConstructedItem *)pvStructInfo;
229 if ((ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
230 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &len)))
232 DWORD dataLen, bytesNeeded;
234 CRYPT_EncodeLen(len, NULL, &dataLen);
235 bytesNeeded = 1 + dataLen + len;
237 *pcbEncoded = bytesNeeded;
238 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
239 pbEncoded, pcbEncoded, bytesNeeded)))
241 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
242 pbEncoded = *(BYTE **)pbEncoded;
243 *pbEncoded++ = ASN_CONTEXT | ASN_CONSTRUCTOR | item->tag;
244 CRYPT_EncodeLen(len, pbEncoded, &dataLen);
245 pbEncoded += dataLen;
246 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
247 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
251 /* Some functions propagate their errors through the size */
258 /* Some functions propagate their errors through the size */
264 struct AsnEncodeTagSwappedItem
267 const void *pvStructInfo;
268 CryptEncodeObjectExFunc encodeFunc;
271 /* Sort of a wacky hack, it encodes something using the struct
272 * AsnEncodeTagSwappedItem's encodeFunc, then replaces the tag byte with the tag
273 * given in the struct AsnEncodeTagSwappedItem.
275 static BOOL WINAPI CRYPT_AsnEncodeSwapTag(DWORD dwCertEncodingType,
276 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
277 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
280 const struct AsnEncodeTagSwappedItem *item =
281 (const struct AsnEncodeTagSwappedItem *)pvStructInfo;
283 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
284 item->pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
285 if (ret && pbEncoded)
286 *pbEncoded = item->tag;
290 static BOOL WINAPI CRYPT_AsnEncodeCertVersion(DWORD dwCertEncodingType,
291 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
292 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
294 const DWORD *ver = (const DWORD *)pvStructInfo;
297 /* CERT_V1 is not encoded */
305 struct AsnConstructedItem item = { 0, ver, CRYPT_AsnEncodeInt };
307 ret = CRYPT_AsnEncodeConstructed(dwCertEncodingType, X509_INTEGER,
308 &item, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
313 static BOOL WINAPI CRYPT_CopyEncodedBlob(DWORD dwCertEncodingType,
314 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
315 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
317 const CRYPT_DER_BLOB *blob = (const CRYPT_DER_BLOB *)pvStructInfo;
322 *pcbEncoded = blob->cbData;
327 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
328 pcbEncoded, blob->cbData)))
330 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
331 pbEncoded = *(BYTE **)pbEncoded;
333 memcpy(pbEncoded, blob->pbData, blob->cbData);
334 *pcbEncoded = blob->cbData;
341 static BOOL WINAPI CRYPT_AsnEncodeValidity(DWORD dwCertEncodingType,
342 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
343 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
346 /* This has two filetimes in a row, a NotBefore and a NotAfter */
347 const FILETIME *timePtr = (const FILETIME *)pvStructInfo;
348 struct AsnEncodeSequenceItem items[] = {
349 { timePtr, CRYPT_AsnEncodeChoiceOfTime, 0 },
350 { timePtr + 1, CRYPT_AsnEncodeChoiceOfTime, 0 },
353 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
354 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
359 /* Like CRYPT_AsnEncodeAlgorithmId, but encodes parameters as an asn.1 NULL
362 static BOOL WINAPI CRYPT_AsnEncodeAlgorithmIdWithNullParams(
363 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
364 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
367 const CRYPT_ALGORITHM_IDENTIFIER *algo =
368 (const CRYPT_ALGORITHM_IDENTIFIER *)pvStructInfo;
369 static const BYTE asn1Null[] = { ASN_NULL, 0 };
370 static const CRYPT_DATA_BLOB nullBlob = { sizeof(asn1Null),
373 struct AsnEncodeSequenceItem items[2] = {
374 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
375 { NULL, CRYPT_CopyEncodedBlob, 0 },
378 if (algo->Parameters.cbData)
379 items[1].pvStructInfo = &algo->Parameters;
381 items[1].pvStructInfo = &nullBlob;
382 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
383 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
388 static BOOL WINAPI CRYPT_AsnEncodeAlgorithmId(DWORD dwCertEncodingType,
389 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
390 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
392 const CRYPT_ALGORITHM_IDENTIFIER *algo =
393 (const CRYPT_ALGORITHM_IDENTIFIER *)pvStructInfo;
395 struct AsnEncodeSequenceItem items[] = {
396 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
397 { &algo->Parameters, CRYPT_CopyEncodedBlob, 0 },
400 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
401 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
406 static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
407 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
408 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
414 const CERT_PUBLIC_KEY_INFO *info =
415 (const CERT_PUBLIC_KEY_INFO *)pvStructInfo;
416 struct AsnEncodeSequenceItem items[] = {
417 { &info->Algorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
418 { &info->PublicKey, CRYPT_AsnEncodeBits, 0 },
421 TRACE("Encoding public key with OID %s\n",
422 debugstr_a(info->Algorithm.pszObjId));
423 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
424 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
429 SetLastError(STATUS_ACCESS_VIOLATION);
436 static BOOL WINAPI CRYPT_AsnEncodeCert(DWORD dwCertEncodingType,
437 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
438 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
444 const CERT_SIGNED_CONTENT_INFO *info =
445 (const CERT_SIGNED_CONTENT_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 = (const CERT_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 WINAPI 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 cCRLEntry = *(const DWORD *)pvStructInfo;
568 DWORD bytesNeeded, dataLen, lenBytes, i;
569 const CRL_ENTRY *rgCRLEntry = *(const CRL_ENTRY *const *)
570 ((const BYTE *)pvStructInfo + sizeof(DWORD));
573 for (i = 0, dataLen = 0; ret && i < cCRLEntry; i++)
577 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], NULL, &size);
581 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
582 bytesNeeded = 1 + lenBytes + dataLen;
584 *pcbEncoded = bytesNeeded;
587 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
588 pcbEncoded, bytesNeeded)))
590 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
591 pbEncoded = *(BYTE **)pbEncoded;
592 *pbEncoded++ = ASN_SEQUENCEOF;
593 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
594 pbEncoded += lenBytes;
595 for (i = 0; i < cCRLEntry; i++)
597 DWORD size = dataLen;
599 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], pbEncoded, &size);
608 static BOOL WINAPI CRYPT_AsnEncodeCRLVersion(DWORD dwCertEncodingType,
609 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
610 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
612 const DWORD *ver = (const DWORD *)pvStructInfo;
615 /* CRL_V1 is not encoded */
622 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
623 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
627 /* Like in Windows, this blithely ignores the validity of the passed-in
628 * CRL_INFO, and just encodes it as-is. The resulting encoded data may not
629 * decode properly, see CRYPT_AsnDecodeCRLInfo.
631 static BOOL WINAPI CRYPT_AsnEncodeCRLInfo(DWORD dwCertEncodingType,
632 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
633 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
639 const CRL_INFO *info = (const CRL_INFO *)pvStructInfo;
640 struct AsnEncodeSequenceItem items[7] = {
641 { &info->dwVersion, CRYPT_AsnEncodeCRLVersion, 0 },
642 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
643 { &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
644 { &info->ThisUpdate, CRYPT_AsnEncodeChoiceOfTime, 0 },
647 struct AsnConstructedItem constructed[1] = { { 0 } };
648 DWORD cItem = 4, cConstructed = 0;
650 if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
652 items[cItem].pvStructInfo = &info->NextUpdate;
653 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
658 items[cItem].pvStructInfo = &info->cCRLEntry;
659 items[cItem].encodeFunc = CRYPT_AsnEncodeCRLEntries;
662 if (info->cExtension)
664 constructed[cConstructed].tag = 0;
665 constructed[cConstructed].pvStructInfo = &info->cExtension;
666 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeExtensions;
667 items[cItem].pvStructInfo = &constructed[cConstructed];
668 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
673 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
674 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
678 SetLastError(STATUS_ACCESS_VIOLATION);
685 static BOOL CRYPT_AsnEncodeExtension(CERT_EXTENSION *ext, BYTE *pbEncoded,
689 struct AsnEncodeSequenceItem items[3] = {
690 { ext->pszObjId, CRYPT_AsnEncodeOid, 0 },
696 TRACE("%p, %p, %d\n", ext, pbEncoded, *pcbEncoded);
700 items[cItem].pvStructInfo = &ext->fCritical;
701 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
704 items[cItem].pvStructInfo = &ext->Value;
705 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
708 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
709 pbEncoded, pcbEncoded);
710 TRACE("returning %d (%08x)\n", ret, GetLastError());
714 static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
715 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
716 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
722 DWORD bytesNeeded, dataLen, lenBytes, i;
723 const CERT_EXTENSIONS *exts = (const CERT_EXTENSIONS *)pvStructInfo;
726 for (i = 0, dataLen = 0; ret && i < exts->cExtension; i++)
730 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i], NULL, &size);
734 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
735 bytesNeeded = 1 + lenBytes + dataLen;
737 *pcbEncoded = bytesNeeded;
740 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
741 pcbEncoded, bytesNeeded)))
743 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
744 pbEncoded = *(BYTE **)pbEncoded;
745 *pbEncoded++ = ASN_SEQUENCEOF;
746 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
747 pbEncoded += lenBytes;
748 for (i = 0; i < exts->cExtension; i++)
750 DWORD size = dataLen;
752 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i],
762 SetLastError(STATUS_ACCESS_VIOLATION);
769 BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
770 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
771 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
773 LPCSTR pszObjId = (LPCSTR)pvStructInfo;
774 DWORD bytesNeeded = 0, lenBytes;
779 TRACE("%s\n", debugstr_a(pszObjId));
786 if (sscanf(pszObjId, "%d.%d.%n", &val1, &val2, &firstPos) != 2)
788 SetLastError(CRYPT_E_ASN1_ERROR);
792 firstByte = val1 * 40 + val2;
793 ptr = pszObjId + firstPos;
798 /* note I assume each component is at most 32-bits long in base 2 */
799 if (sscanf(ptr, "%d%n", &val1, &pos) == 1)
801 if (val1 >= 0x10000000)
803 else if (val1 >= 0x200000)
805 else if (val1 >= 0x4000)
807 else if (val1 >= 0x80)
817 SetLastError(CRYPT_E_ASN1_ERROR);
821 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
825 bytesNeeded += 1 + lenBytes;
828 if (*pcbEncoded < bytesNeeded)
830 SetLastError(ERROR_MORE_DATA);
835 *pbEncoded++ = ASN_OBJECTIDENTIFIER;
836 CRYPT_EncodeLen(bytesNeeded - 1 - lenBytes, pbEncoded, &lenBytes);
837 pbEncoded += lenBytes;
843 *pbEncoded++ = firstByte;
844 ptr = pszObjId + firstPos;
847 sscanf(ptr, "%d%n", &val, &pos);
849 unsigned char outBytes[5];
852 if (val >= 0x10000000)
854 else if (val >= 0x200000)
856 else if (val >= 0x4000)
858 else if (val >= 0x80)
862 for (i = numBytes; i > 0; i--)
864 outBytes[i - 1] = val & 0x7f;
867 for (i = 0; i < numBytes - 1; i++)
868 *pbEncoded++ = outBytes[i] | 0x80;
869 *pbEncoded++ = outBytes[i];
878 *pcbEncoded = bytesNeeded;
882 static BOOL CRYPT_AsnEncodeStringCoerce(const CERT_NAME_VALUE *value,
883 BYTE tag, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
887 LPCSTR str = (LPCSTR)value->Value.pbData;
888 DWORD bytesNeeded, lenBytes, encodedLen;
890 encodedLen = value->Value.cbData ? value->Value.cbData : lstrlenA(str);
891 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
892 bytesNeeded = 1 + lenBytes + encodedLen;
894 *pcbEncoded = bytesNeeded;
897 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
898 pbEncoded, pcbEncoded, bytesNeeded)))
900 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
901 pbEncoded = *(BYTE **)pbEncoded;
903 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
904 pbEncoded += lenBytes;
905 memcpy(pbEncoded, str, encodedLen);
911 static BOOL CRYPT_AsnEncodeBMPString(const CERT_NAME_VALUE *value,
912 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
916 LPCWSTR str = (LPCWSTR)value->Value.pbData;
917 DWORD bytesNeeded, lenBytes, strLen;
919 if (value->Value.cbData)
920 strLen = value->Value.cbData / sizeof(WCHAR);
921 else if (value->Value.pbData)
922 strLen = lstrlenW(str);
925 CRYPT_EncodeLen(strLen * 2, NULL, &lenBytes);
926 bytesNeeded = 1 + lenBytes + strLen * 2;
928 *pcbEncoded = bytesNeeded;
931 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
932 pbEncoded, pcbEncoded, bytesNeeded)))
936 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
937 pbEncoded = *(BYTE **)pbEncoded;
938 *pbEncoded++ = ASN_BMPSTRING;
939 CRYPT_EncodeLen(strLen * 2, pbEncoded, &lenBytes);
940 pbEncoded += lenBytes;
941 for (i = 0; i < strLen; i++)
943 *pbEncoded++ = (str[i] & 0xff00) >> 8;
944 *pbEncoded++ = str[i] & 0x00ff;
951 static BOOL CRYPT_AsnEncodeUTF8String(const CERT_NAME_VALUE *value,
952 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
956 LPCWSTR str = (LPCWSTR)value->Value.pbData;
957 DWORD bytesNeeded, lenBytes, encodedLen, strLen;
959 strLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
961 encodedLen = WideCharToMultiByte(CP_UTF8, 0, str, strLen, NULL, 0, NULL,
963 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
964 bytesNeeded = 1 + lenBytes + encodedLen;
966 *pcbEncoded = bytesNeeded;
969 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
970 pbEncoded, pcbEncoded, bytesNeeded)))
972 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
973 pbEncoded = *(BYTE **)pbEncoded;
974 *pbEncoded++ = ASN_UTF8STRING;
975 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
976 pbEncoded += lenBytes;
977 WideCharToMultiByte(CP_UTF8, 0, str, strLen, (LPSTR)pbEncoded,
978 bytesNeeded - lenBytes - 1, NULL, NULL);
984 static BOOL WINAPI CRYPT_AsnEncodeNameValue(DWORD dwCertEncodingType,
985 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
986 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
992 const CERT_NAME_VALUE *value = (const CERT_NAME_VALUE *)pvStructInfo;
994 switch (value->dwValueType)
996 case CERT_RDN_ANY_TYPE:
997 /* explicitly disallowed */
998 SetLastError(E_INVALIDARG);
1001 case CERT_RDN_ENCODED_BLOB:
1002 ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL,
1003 &value->Value, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1005 case CERT_RDN_OCTET_STRING:
1006 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_OCTETSTRING,
1007 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1009 case CERT_RDN_NUMERIC_STRING:
1010 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_NUMERICSTRING,
1011 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1013 case CERT_RDN_PRINTABLE_STRING:
1014 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_PRINTABLESTRING,
1015 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1017 case CERT_RDN_TELETEX_STRING:
1018 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_T61STRING,
1019 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1021 case CERT_RDN_VIDEOTEX_STRING:
1022 ret = CRYPT_AsnEncodeStringCoerce(value,
1023 ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1025 case CERT_RDN_IA5_STRING:
1026 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_IA5STRING,
1027 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1029 case CERT_RDN_GRAPHIC_STRING:
1030 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GRAPHICSTRING,
1031 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1033 case CERT_RDN_VISIBLE_STRING:
1034 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_VISIBLESTRING,
1035 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1037 case CERT_RDN_GENERAL_STRING:
1038 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GENERALSTRING,
1039 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1041 case CERT_RDN_UNIVERSAL_STRING:
1042 FIXME("CERT_RDN_UNIVERSAL_STRING: unimplemented\n");
1043 SetLastError(CRYPT_E_ASN1_CHOICE);
1046 case CERT_RDN_BMP_STRING:
1047 ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
1048 pbEncoded, pcbEncoded);
1050 case CERT_RDN_UTF8_STRING:
1051 ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
1052 pbEncoded, pcbEncoded);
1055 SetLastError(CRYPT_E_ASN1_CHOICE);
1061 SetLastError(STATUS_ACCESS_VIOLATION);
1068 static BOOL WINAPI CRYPT_AsnEncodeRdnAttr(DWORD dwCertEncodingType,
1069 CERT_RDN_ATTR *attr, CryptEncodeObjectExFunc nameValueEncodeFunc,
1070 BYTE *pbEncoded, DWORD *pcbEncoded)
1072 DWORD bytesNeeded = 0, lenBytes, size;
1075 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL, attr->pszObjId,
1076 0, NULL, NULL, &size);
1079 bytesNeeded += size;
1080 /* hack: a CERT_RDN_ATTR is identical to a CERT_NAME_VALUE beginning
1081 * with dwValueType, so "cast" it to get its encoded size
1083 ret = nameValueEncodeFunc(dwCertEncodingType, NULL,
1084 (CERT_NAME_VALUE *)&attr->dwValueType, 0, NULL, NULL, &size);
1087 bytesNeeded += size;
1088 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1089 bytesNeeded += 1 + lenBytes;
1092 if (*pcbEncoded < bytesNeeded)
1094 SetLastError(ERROR_MORE_DATA);
1099 *pbEncoded++ = ASN_SEQUENCE;
1100 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1102 pbEncoded += lenBytes;
1103 size = bytesNeeded - 1 - lenBytes;
1104 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
1105 attr->pszObjId, 0, NULL, pbEncoded, &size);
1109 size = bytesNeeded - 1 - lenBytes - size;
1110 ret = nameValueEncodeFunc(dwCertEncodingType,
1111 NULL, (CERT_NAME_VALUE *)&attr->dwValueType,
1112 0, NULL, pbEncoded, &size);
1119 *pcbEncoded = bytesNeeded;
1123 /* Have to propagate index of failing character */
1130 static int BLOBComp(const void *l, const void *r)
1132 const CRYPT_DER_BLOB *a = (const CRYPT_DER_BLOB *)l, *b = (const CRYPT_DER_BLOB *)r;
1135 if (!(ret = memcmp(a->pbData, b->pbData, min(a->cbData, b->cbData))))
1136 ret = a->cbData - b->cbData;
1140 /* This encodes a SET OF, which in DER must be lexicographically sorted.
1142 static BOOL WINAPI CRYPT_DEREncodeSet(DWORD dwCertEncodingType,
1143 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1144 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1146 const CRYPT_BLOB_ARRAY *set = (const CRYPT_BLOB_ARRAY *)pvStructInfo;
1147 DWORD bytesNeeded = 0, lenBytes, i;
1150 for (i = 0; i < set->cBlob; i++)
1151 bytesNeeded += set->rgBlob[i].cbData;
1152 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1153 bytesNeeded += 1 + lenBytes;
1156 *pcbEncoded = bytesNeeded;
1159 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1160 pbEncoded, pcbEncoded, bytesNeeded)))
1162 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1163 pbEncoded = *(BYTE **)pbEncoded;
1164 qsort(set->rgBlob, set->cBlob, sizeof(CRYPT_DER_BLOB), BLOBComp);
1165 *pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
1166 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
1167 pbEncoded += lenBytes;
1168 for (i = 0; ret && i < set->cBlob; i++)
1170 memcpy(pbEncoded, set->rgBlob[i].pbData, set->rgBlob[i].cbData);
1171 pbEncoded += set->rgBlob[i].cbData;
1177 struct DERSetDescriptor
1183 CryptEncodeObjectExFunc encode;
1186 static BOOL WINAPI CRYPT_DEREncodeItemsAsSet(DWORD dwCertEncodingType,
1187 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1188 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1190 const struct DERSetDescriptor *desc =
1191 (const struct DERSetDescriptor *)pvStructInfo;
1192 CRYPT_BLOB_ARRAY setOf = { 0, NULL };
1198 setOf.rgBlob = CryptMemAlloc(desc->cItems * sizeof(CRYPT_DER_BLOB));
1203 setOf.cBlob = desc->cItems;
1204 memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
1207 for (i = 0; ret && i < setOf.cBlob; i++)
1209 ret = desc->encode(dwCertEncodingType, lpszStructType,
1210 (const BYTE *)desc->items + i * desc->itemSize + desc->itemOffset,
1211 0, NULL, NULL, &setOf.rgBlob[i].cbData);
1214 setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
1215 if (!setOf.rgBlob[i].pbData)
1218 ret = desc->encode(dwCertEncodingType, lpszStructType,
1219 (const BYTE *)desc->items + i * desc->itemSize +
1220 desc->itemOffset, 0, NULL, setOf.rgBlob[i].pbData,
1221 &setOf.rgBlob[i].cbData);
1223 /* Some functions propagate their errors through the size */
1225 *pcbEncoded = setOf.rgBlob[i].cbData;
1229 DWORD bytesNeeded = 0, lenBytes;
1231 for (i = 0; i < setOf.cBlob; i++)
1232 bytesNeeded += setOf.rgBlob[i].cbData;
1233 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1234 bytesNeeded += 1 + lenBytes;
1236 *pcbEncoded = bytesNeeded;
1237 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1238 pbEncoded, pcbEncoded, bytesNeeded)))
1240 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1241 pbEncoded = *(BYTE **)pbEncoded;
1242 qsort(setOf.rgBlob, setOf.cBlob, sizeof(CRYPT_DER_BLOB),
1244 *pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
1245 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
1246 pbEncoded += lenBytes;
1247 for (i = 0; i < setOf.cBlob; i++)
1249 memcpy(pbEncoded, setOf.rgBlob[i].pbData,
1250 setOf.rgBlob[i].cbData);
1251 pbEncoded += setOf.rgBlob[i].cbData;
1255 for (i = 0; i < setOf.cBlob; i++)
1256 CryptMemFree(setOf.rgBlob[i].pbData);
1257 CryptMemFree(setOf.rgBlob);
1261 static BOOL WINAPI CRYPT_AsnEncodeRdn(DWORD dwCertEncodingType, CERT_RDN *rdn,
1262 CryptEncodeObjectExFunc nameValueEncodeFunc, BYTE *pbEncoded,
1266 CRYPT_BLOB_ARRAY setOf = { 0, NULL };
1275 setOf.cBlob = rdn->cRDNAttr;
1276 setOf.rgBlob = CryptMemAlloc(rdn->cRDNAttr *
1277 sizeof(CRYPT_DER_BLOB));
1281 memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
1283 for (i = 0; ret && i < rdn->cRDNAttr; i++)
1285 setOf.rgBlob[i].cbData = 0;
1286 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType, &rdn->rgRDNAttr[i],
1287 nameValueEncodeFunc, NULL, &setOf.rgBlob[i].cbData);
1290 setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
1291 if (!setOf.rgBlob[i].pbData)
1294 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType,
1295 &rdn->rgRDNAttr[i], nameValueEncodeFunc,
1296 setOf.rgBlob[i].pbData, &setOf.rgBlob[i].cbData);
1300 /* Have to propagate index of failing character */
1301 *pcbEncoded = setOf.rgBlob[i].cbData;
1305 ret = CRYPT_DEREncodeSet(X509_ASN_ENCODING, NULL, &setOf, 0, NULL,
1306 pbEncoded, pcbEncoded);
1307 for (i = 0; i < setOf.cBlob; i++)
1308 CryptMemFree(setOf.rgBlob[i].pbData);
1312 SetLastError(STATUS_ACCESS_VIOLATION);
1316 CryptMemFree(setOf.rgBlob);
1320 static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
1321 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1322 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
1324 static BOOL WINAPI CRYPT_AsnEncodeOrCopyUnicodeNameValue(
1325 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1326 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1329 const CERT_NAME_VALUE *value = (const CERT_NAME_VALUE *)pvStructInfo;
1332 if (value->dwValueType == CERT_RDN_ENCODED_BLOB)
1333 ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL, &value->Value,
1334 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1336 ret = CRYPT_AsnEncodeUnicodeNameValue(dwCertEncodingType, NULL, value,
1337 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1341 static BOOL WINAPI CRYPT_AsnEncodeUnicodeName(DWORD dwCertEncodingType,
1342 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1343 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1349 const CERT_NAME_INFO *info = (const CERT_NAME_INFO *)pvStructInfo;
1350 DWORD bytesNeeded = 0, lenBytes, size, i;
1352 TRACE("encoding name with %d RDNs\n", info->cRDN);
1354 for (i = 0; ret && i < info->cRDN; i++)
1356 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
1357 CRYPT_AsnEncodeOrCopyUnicodeNameValue, NULL, &size);
1359 bytesNeeded += size;
1363 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1364 bytesNeeded += 1 + lenBytes;
1368 *pcbEncoded = bytesNeeded;
1371 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1372 pbEncoded, pcbEncoded, bytesNeeded)))
1374 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1375 pbEncoded = *(BYTE **)pbEncoded;
1376 *pbEncoded++ = ASN_SEQUENCEOF;
1377 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1379 pbEncoded += lenBytes;
1380 for (i = 0; ret && i < info->cRDN; i++)
1383 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
1384 &info->rgRDN[i], CRYPT_AsnEncodeOrCopyUnicodeNameValue,
1389 bytesNeeded -= size;
1400 SetLastError(STATUS_ACCESS_VIOLATION);
1407 static BOOL WINAPI CRYPT_AsnEncodePKCSAttribute(DWORD dwCertEncodingType,
1408 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1409 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1415 const CRYPT_ATTRIBUTE *attr = (const CRYPT_ATTRIBUTE *)pvStructInfo;
1417 if (!attr->pszObjId)
1418 SetLastError(E_INVALIDARG);
1421 struct AsnEncodeSequenceItem items[2] = {
1422 { attr->pszObjId, CRYPT_AsnEncodeOid, 0 },
1423 { &attr->cValue, CRYPT_DEREncodeSet, 0 },
1426 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1427 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
1433 SetLastError(STATUS_ACCESS_VIOLATION);
1439 static BOOL WINAPI CRYPT_AsnEncodePKCSAttributes(DWORD dwCertEncodingType,
1440 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1441 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1447 const CRYPT_ATTRIBUTES *attributes =
1448 (const CRYPT_ATTRIBUTES *)pvStructInfo;
1449 struct DERSetDescriptor desc = { attributes->cAttr, attributes->rgAttr,
1450 sizeof(CRYPT_ATTRIBUTE), 0, CRYPT_AsnEncodePKCSAttribute };
1452 ret = CRYPT_DEREncodeItemsAsSet(X509_ASN_ENCODING, lpszStructType,
1453 &desc, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1457 SetLastError(STATUS_ACCESS_VIOLATION);
1463 /* Like CRYPT_AsnEncodePKCSContentInfo, but allows the OID to be NULL */
1464 static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfoInternal(
1465 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1466 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1469 const CRYPT_CONTENT_INFO *info = (const CRYPT_CONTENT_INFO *)pvStructInfo;
1470 struct AsnEncodeSequenceItem items[2] = {
1471 { info->pszObjId, CRYPT_AsnEncodeOid, 0 },
1474 struct AsnConstructedItem constructed = { 0 };
1477 if (info->Content.cbData)
1479 constructed.tag = 0;
1480 constructed.pvStructInfo = &info->Content;
1481 constructed.encodeFunc = CRYPT_CopyEncodedBlob;
1482 items[cItem].pvStructInfo = &constructed;
1483 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
1486 return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1487 cItem, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1490 BOOL CRYPT_AsnEncodePKCSDigestedData(CRYPT_DIGESTED_DATA *digestedData,
1491 void *pvData, DWORD *pcbData)
1493 struct AsnEncodeSequenceItem items[] = {
1494 { &digestedData->version, CRYPT_AsnEncodeInt, 0 },
1495 { &digestedData->DigestAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
1497 { &digestedData->ContentInfo, CRYPT_AsnEncodePKCSContentInfoInternal, 0 },
1498 { &digestedData->hash, CRYPT_AsnEncodeOctets, 0 },
1501 return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
1502 sizeof(items) / sizeof(items[0]), 0, NULL, pvData, pcbData);
1505 static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfo(DWORD dwCertEncodingType,
1506 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1507 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1513 const CRYPT_CONTENT_INFO *info =
1514 (const CRYPT_CONTENT_INFO *)pvStructInfo;
1516 if (!info->pszObjId)
1517 SetLastError(E_INVALIDARG);
1519 ret = CRYPT_AsnEncodePKCSContentInfoInternal(dwCertEncodingType,
1520 lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
1525 SetLastError(STATUS_ACCESS_VIOLATION);
1531 static BOOL CRYPT_AsnEncodeUnicodeStringCoerce(const CERT_NAME_VALUE *value,
1532 BYTE tag, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1536 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1537 DWORD bytesNeeded, lenBytes, encodedLen;
1539 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
1541 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1542 bytesNeeded = 1 + lenBytes + encodedLen;
1544 *pcbEncoded = bytesNeeded;
1547 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1548 pbEncoded, pcbEncoded, bytesNeeded)))
1552 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1553 pbEncoded = *(BYTE **)pbEncoded;
1555 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
1556 pbEncoded += lenBytes;
1557 for (i = 0; i < encodedLen; i++)
1558 *pbEncoded++ = (BYTE)str[i];
1564 static BOOL CRYPT_AsnEncodeNumericString(const CERT_NAME_VALUE *value,
1565 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1569 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1570 DWORD bytesNeeded, lenBytes, encodedLen;
1572 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
1574 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1575 bytesNeeded = 1 + lenBytes + encodedLen;
1577 *pcbEncoded = bytesNeeded;
1580 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1581 pbEncoded, pcbEncoded, bytesNeeded)))
1586 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1587 ptr = *(BYTE **)pbEncoded;
1590 *ptr++ = ASN_NUMERICSTRING;
1591 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
1593 for (i = 0; ret && i < encodedLen; i++)
1595 if (isdigitW(str[i]))
1596 *ptr++ = (BYTE)str[i];
1600 SetLastError(CRYPT_E_INVALID_NUMERIC_STRING);
1604 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
1605 CryptMemFree(*(BYTE **)pbEncoded);
1611 static inline int isprintableW(WCHAR wc)
1613 return isalnumW(wc) || isspaceW(wc) || wc == '\'' || wc == '(' ||
1614 wc == ')' || wc == '+' || wc == ',' || wc == '-' || wc == '.' ||
1615 wc == '/' || wc == ':' || wc == '=' || wc == '?';
1618 static BOOL CRYPT_AsnEncodePrintableString(const CERT_NAME_VALUE *value,
1619 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1623 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1624 DWORD bytesNeeded, lenBytes, encodedLen;
1626 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
1628 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1629 bytesNeeded = 1 + lenBytes + encodedLen;
1631 *pcbEncoded = bytesNeeded;
1634 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1635 pbEncoded, pcbEncoded, bytesNeeded)))
1640 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1641 ptr = *(BYTE **)pbEncoded;
1644 *ptr++ = ASN_PRINTABLESTRING;
1645 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
1647 for (i = 0; ret && i < encodedLen; i++)
1649 if (isprintableW(str[i]))
1650 *ptr++ = (BYTE)str[i];
1654 SetLastError(CRYPT_E_INVALID_PRINTABLE_STRING);
1658 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
1659 CryptMemFree(*(BYTE **)pbEncoded);
1665 static BOOL CRYPT_AsnEncodeIA5String(const CERT_NAME_VALUE *value,
1666 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1670 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1671 DWORD bytesNeeded, lenBytes, encodedLen;
1673 encodedLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
1675 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1676 bytesNeeded = 1 + lenBytes + encodedLen;
1678 *pcbEncoded = bytesNeeded;
1681 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1682 pbEncoded, pcbEncoded, bytesNeeded)))
1687 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1688 ptr = *(BYTE **)pbEncoded;
1691 *ptr++ = ASN_IA5STRING;
1692 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
1694 for (i = 0; ret && i < encodedLen; i++)
1697 *ptr++ = (BYTE)str[i];
1701 SetLastError(CRYPT_E_INVALID_IA5_STRING);
1705 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
1706 CryptMemFree(*(BYTE **)pbEncoded);
1712 static BOOL CRYPT_AsnEncodeUniversalString(const CERT_NAME_VALUE *value,
1713 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1717 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1718 DWORD bytesNeeded, lenBytes, strLen;
1720 /* FIXME: doesn't handle composite characters */
1721 strLen = value->Value.cbData ? value->Value.cbData / sizeof(WCHAR) :
1723 CRYPT_EncodeLen(strLen * 4, NULL, &lenBytes);
1724 bytesNeeded = 1 + lenBytes + strLen * 4;
1726 *pcbEncoded = bytesNeeded;
1729 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1730 pbEncoded, pcbEncoded, bytesNeeded)))
1734 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1735 pbEncoded = *(BYTE **)pbEncoded;
1736 *pbEncoded++ = ASN_UNIVERSALSTRING;
1737 CRYPT_EncodeLen(strLen * 4, pbEncoded, &lenBytes);
1738 pbEncoded += lenBytes;
1739 for (i = 0; i < strLen; i++)
1743 *pbEncoded++ = (BYTE)((str[i] & 0xff00) >> 8);
1744 *pbEncoded++ = (BYTE)(str[i] & 0x00ff);
1751 static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
1752 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1753 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1759 const CERT_NAME_VALUE *value = (const CERT_NAME_VALUE *)pvStructInfo;
1761 switch (value->dwValueType)
1763 case CERT_RDN_ANY_TYPE:
1764 case CERT_RDN_ENCODED_BLOB:
1765 case CERT_RDN_OCTET_STRING:
1766 SetLastError(CRYPT_E_NOT_CHAR_STRING);
1768 case CERT_RDN_NUMERIC_STRING:
1769 ret = CRYPT_AsnEncodeNumericString(value, dwFlags, pEncodePara,
1770 pbEncoded, pcbEncoded);
1772 case CERT_RDN_PRINTABLE_STRING:
1773 ret = CRYPT_AsnEncodePrintableString(value, dwFlags, pEncodePara,
1774 pbEncoded, pcbEncoded);
1776 case CERT_RDN_TELETEX_STRING:
1777 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_T61STRING,
1778 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1780 case CERT_RDN_VIDEOTEX_STRING:
1781 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value,
1782 ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1784 case CERT_RDN_IA5_STRING:
1785 ret = CRYPT_AsnEncodeIA5String(value, dwFlags, pEncodePara,
1786 pbEncoded, pcbEncoded);
1788 case CERT_RDN_GRAPHIC_STRING:
1789 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GRAPHICSTRING,
1790 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1792 case CERT_RDN_VISIBLE_STRING:
1793 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_VISIBLESTRING,
1794 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1796 case CERT_RDN_GENERAL_STRING:
1797 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GENERALSTRING,
1798 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1800 case CERT_RDN_UNIVERSAL_STRING:
1801 ret = CRYPT_AsnEncodeUniversalString(value, dwFlags, pEncodePara,
1802 pbEncoded, pcbEncoded);
1804 case CERT_RDN_BMP_STRING:
1805 ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
1806 pbEncoded, pcbEncoded);
1808 case CERT_RDN_UTF8_STRING:
1809 ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
1810 pbEncoded, pcbEncoded);
1813 SetLastError(CRYPT_E_ASN1_CHOICE);
1818 SetLastError(STATUS_ACCESS_VIOLATION);
1824 static BOOL WINAPI CRYPT_AsnEncodeName(DWORD dwCertEncodingType,
1825 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1826 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1832 const CERT_NAME_INFO *info = (const CERT_NAME_INFO *)pvStructInfo;
1833 DWORD bytesNeeded = 0, lenBytes, size, i;
1835 TRACE("encoding name with %d RDNs\n", info->cRDN);
1837 for (i = 0; ret && i < info->cRDN; i++)
1839 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
1840 CRYPT_AsnEncodeNameValue, NULL, &size);
1842 bytesNeeded += size;
1844 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1845 bytesNeeded += 1 + lenBytes;
1849 *pcbEncoded = bytesNeeded;
1852 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1853 pbEncoded, pcbEncoded, bytesNeeded)))
1855 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1856 pbEncoded = *(BYTE **)pbEncoded;
1857 *pbEncoded++ = ASN_SEQUENCEOF;
1858 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1860 pbEncoded += lenBytes;
1861 for (i = 0; ret && i < info->cRDN; i++)
1864 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
1865 &info->rgRDN[i], CRYPT_AsnEncodeNameValue, pbEncoded,
1870 bytesNeeded -= size;
1879 SetLastError(STATUS_ACCESS_VIOLATION);
1886 static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
1887 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1888 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1890 BOOL val = *(const BOOL *)pvStructInfo, ret;
1899 else if (*pcbEncoded < 3)
1902 SetLastError(ERROR_MORE_DATA);
1908 *pbEncoded++ = ASN_BOOL;
1910 *pbEncoded++ = val ? 0xff : 0;
1913 TRACE("returning %d (%08x)\n", ret, GetLastError());
1917 static BOOL WINAPI CRYPT_AsnEncodeAltNameEntry(DWORD dwCertEncodingType,
1918 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1919 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1921 const CERT_ALT_NAME_ENTRY *entry =
1922 (const CERT_ALT_NAME_ENTRY *)pvStructInfo;
1928 switch (entry->dwAltNameChoice)
1930 case CERT_ALT_NAME_RFC822_NAME:
1931 case CERT_ALT_NAME_DNS_NAME:
1932 case CERT_ALT_NAME_URL:
1933 tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
1934 if (entry->u.pwszURL)
1938 /* Not + 1: don't encode the NULL-terminator */
1939 dataLen = lstrlenW(entry->u.pwszURL);
1940 for (i = 0; ret && i < dataLen; i++)
1942 if (entry->u.pwszURL[i] > 0x7f)
1944 SetLastError(CRYPT_E_INVALID_IA5_STRING);
1953 case CERT_ALT_NAME_DIRECTORY_NAME:
1954 tag = ASN_CONTEXT | ASN_CONSTRUCTOR | (entry->dwAltNameChoice - 1);
1955 dataLen = entry->u.DirectoryName.cbData;
1957 case CERT_ALT_NAME_IP_ADDRESS:
1958 tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
1959 dataLen = entry->u.IPAddress.cbData;
1961 case CERT_ALT_NAME_REGISTERED_ID:
1963 struct AsnEncodeTagSwappedItem swapped =
1964 { ASN_CONTEXT | (entry->dwAltNameChoice - 1), entry->u.pszRegisteredID,
1965 CRYPT_AsnEncodeOid };
1967 return CRYPT_AsnEncodeSwapTag(0, NULL, &swapped, 0, NULL, pbEncoded,
1970 case CERT_ALT_NAME_OTHER_NAME:
1971 FIXME("name type %d unimplemented\n", entry->dwAltNameChoice);
1974 SetLastError(E_INVALIDARG);
1979 DWORD bytesNeeded, lenBytes;
1981 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1982 bytesNeeded = 1 + dataLen + lenBytes;
1984 *pcbEncoded = bytesNeeded;
1985 else if (*pcbEncoded < bytesNeeded)
1987 SetLastError(ERROR_MORE_DATA);
1988 *pcbEncoded = bytesNeeded;
1994 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
1995 pbEncoded += lenBytes;
1996 switch (entry->dwAltNameChoice)
1998 case CERT_ALT_NAME_RFC822_NAME:
1999 case CERT_ALT_NAME_DNS_NAME:
2000 case CERT_ALT_NAME_URL:
2004 for (i = 0; i < dataLen; i++)
2005 *pbEncoded++ = (BYTE)entry->u.pwszURL[i];
2008 case CERT_ALT_NAME_DIRECTORY_NAME:
2009 memcpy(pbEncoded, entry->u.DirectoryName.pbData, dataLen);
2011 case CERT_ALT_NAME_IP_ADDRESS:
2012 memcpy(pbEncoded, entry->u.IPAddress.pbData, dataLen);
2016 *pcbEncoded = bytesNeeded;
2019 TRACE("returning %d (%08x)\n", ret, GetLastError());
2023 static BOOL WINAPI CRYPT_AsnEncodeIntegerSwapBytes(DWORD dwCertEncodingType,
2024 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2025 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2031 const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvStructInfo;
2032 CRYPT_DATA_BLOB newBlob = { blob->cbData, NULL };
2037 newBlob.pbData = CryptMemAlloc(newBlob.cbData);
2042 for (i = 0; i < newBlob.cbData; i++)
2043 newBlob.pbData[newBlob.cbData - i - 1] = blob->pbData[i];
2049 ret = CRYPT_AsnEncodeInteger(dwCertEncodingType, lpszStructType,
2050 &newBlob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2051 CryptMemFree(newBlob.pbData);
2055 SetLastError(STATUS_ACCESS_VIOLATION);
2062 static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId(DWORD dwCertEncodingType,
2063 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2064 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2070 const CERT_AUTHORITY_KEY_ID_INFO *info =
2071 (const CERT_AUTHORITY_KEY_ID_INFO *)pvStructInfo;
2072 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2073 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2074 struct AsnConstructedItem constructed = { 0 };
2075 DWORD cItem = 0, cSwapped = 0;
2077 if (info->KeyId.cbData)
2079 swapped[cSwapped].tag = ASN_CONTEXT | 0;
2080 swapped[cSwapped].pvStructInfo = &info->KeyId;
2081 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeIntegerSwapBytes;
2082 items[cItem].pvStructInfo = &swapped[cSwapped];
2083 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2087 if (info->CertIssuer.cbData)
2089 constructed.tag = 1;
2090 constructed.pvStructInfo = &info->CertIssuer;
2091 constructed.encodeFunc = CRYPT_CopyEncodedBlob;
2092 items[cItem].pvStructInfo = &constructed;
2093 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
2096 if (info->CertSerialNumber.cbData)
2098 swapped[cSwapped].tag = ASN_CONTEXT | 2;
2099 swapped[cSwapped].pvStructInfo = &info->CertSerialNumber;
2100 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
2101 items[cItem].pvStructInfo = &swapped[cSwapped];
2102 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2106 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
2107 pEncodePara, pbEncoded, pcbEncoded);
2111 SetLastError(STATUS_ACCESS_VIOLATION);
2118 static BOOL WINAPI CRYPT_AsnEncodeAltName(DWORD dwCertEncodingType,
2119 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2120 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2126 const CERT_ALT_NAME_INFO *info =
2127 (const CERT_ALT_NAME_INFO *)pvStructInfo;
2128 DWORD bytesNeeded, dataLen, lenBytes, i;
2131 /* FIXME: should check that cAltEntry is not bigger than 0xff, since we
2132 * can't encode an erroneous entry index if it's bigger than this.
2134 for (i = 0, dataLen = 0; ret && i < info->cAltEntry; i++)
2138 ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType, NULL,
2139 &info->rgAltEntry[i], 0, NULL, NULL, &len);
2142 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
2144 /* CRYPT_AsnEncodeAltNameEntry encoded the index of
2145 * the bad character, now set the index of the bad
2148 *pcbEncoded = (BYTE)i <<
2149 CERT_ALT_NAME_ENTRY_ERR_INDEX_SHIFT | len;
2154 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2155 bytesNeeded = 1 + lenBytes + dataLen;
2158 *pcbEncoded = bytesNeeded;
2163 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2164 pbEncoded, pcbEncoded, bytesNeeded)))
2166 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2167 pbEncoded = *(BYTE **)pbEncoded;
2168 *pbEncoded++ = ASN_SEQUENCEOF;
2169 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2170 pbEncoded += lenBytes;
2171 for (i = 0; ret && i < info->cAltEntry; i++)
2173 DWORD len = dataLen;
2175 ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType,
2176 NULL, &info->rgAltEntry[i], 0, NULL, pbEncoded, &len);
2189 SetLastError(STATUS_ACCESS_VIOLATION);
2196 static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId2(DWORD dwCertEncodingType,
2197 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2198 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2204 const CERT_AUTHORITY_KEY_ID2_INFO *info =
2205 (const CERT_AUTHORITY_KEY_ID2_INFO *)pvStructInfo;
2206 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2207 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2208 DWORD cItem = 0, cSwapped = 0;
2210 if (info->KeyId.cbData)
2212 swapped[cSwapped].tag = ASN_CONTEXT | 0;
2213 swapped[cSwapped].pvStructInfo = &info->KeyId;
2214 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeIntegerSwapBytes;
2215 items[cItem].pvStructInfo = &swapped[cSwapped];
2216 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2220 if (info->AuthorityCertIssuer.cAltEntry)
2222 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
2223 swapped[cSwapped].pvStructInfo = &info->AuthorityCertIssuer;
2224 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
2225 items[cItem].pvStructInfo = &swapped[cSwapped];
2226 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2230 if (info->AuthorityCertSerialNumber.cbData)
2232 swapped[cSwapped].tag = ASN_CONTEXT | 2;
2233 swapped[cSwapped].pvStructInfo = &info->AuthorityCertSerialNumber;
2234 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
2235 items[cItem].pvStructInfo = &swapped[cSwapped];
2236 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2240 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
2241 pEncodePara, pbEncoded, pcbEncoded);
2245 SetLastError(STATUS_ACCESS_VIOLATION);
2252 static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints(DWORD dwCertEncodingType,
2253 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2254 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2260 const CERT_BASIC_CONSTRAINTS_INFO *info =
2261 (const CERT_BASIC_CONSTRAINTS_INFO *)pvStructInfo;
2262 struct AsnEncodeSequenceItem items[3] = {
2263 { &info->SubjectType, CRYPT_AsnEncodeBits, 0 },
2268 if (info->fPathLenConstraint)
2270 items[cItem].pvStructInfo = &info->dwPathLenConstraint;
2271 items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
2274 if (info->cSubtreesConstraint)
2276 items[cItem].pvStructInfo = &info->cSubtreesConstraint;
2277 items[cItem].encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
2280 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
2281 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2285 SetLastError(STATUS_ACCESS_VIOLATION);
2292 static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints2(DWORD dwCertEncodingType,
2293 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2294 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2300 const CERT_BASIC_CONSTRAINTS2_INFO *info =
2301 (const CERT_BASIC_CONSTRAINTS2_INFO *)pvStructInfo;
2302 struct AsnEncodeSequenceItem items[2] = { { 0 } };
2307 items[cItem].pvStructInfo = &info->fCA;
2308 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
2311 if (info->fPathLenConstraint)
2313 items[cItem].pvStructInfo = &info->dwPathLenConstraint;
2314 items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
2317 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
2318 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2322 SetLastError(STATUS_ACCESS_VIOLATION);
2329 static BOOL WINAPI CRYPT_AsnEncodeRsaPubKey(DWORD dwCertEncodingType,
2330 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2331 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2337 const BLOBHEADER *hdr =
2338 (const BLOBHEADER *)pvStructInfo;
2340 if (hdr->bType != PUBLICKEYBLOB)
2342 SetLastError(E_INVALIDARG);
2347 const RSAPUBKEY *rsaPubKey = (const RSAPUBKEY *)
2348 ((const BYTE *)pvStructInfo + sizeof(BLOBHEADER));
2349 CRYPT_INTEGER_BLOB blob = { rsaPubKey->bitlen / 8,
2350 (BYTE *)pvStructInfo + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) };
2351 struct AsnEncodeSequenceItem items[] = {
2352 { &blob, CRYPT_AsnEncodeUnsignedInteger, 0 },
2353 { &rsaPubKey->pubexp, CRYPT_AsnEncodeInt, 0 },
2356 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
2357 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
2363 SetLastError(STATUS_ACCESS_VIOLATION);
2370 BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
2371 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2372 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2378 const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvStructInfo;
2379 DWORD bytesNeeded, lenBytes;
2381 TRACE("(%d, %p), %08x, %p, %p, %d\n", blob->cbData, blob->pbData,
2382 dwFlags, pEncodePara, pbEncoded, *pcbEncoded);
2384 CRYPT_EncodeLen(blob->cbData, NULL, &lenBytes);
2385 bytesNeeded = 1 + lenBytes + blob->cbData;
2388 *pcbEncoded = bytesNeeded;
2393 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
2394 pcbEncoded, bytesNeeded)))
2396 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2397 pbEncoded = *(BYTE **)pbEncoded;
2398 *pbEncoded++ = ASN_OCTETSTRING;
2399 CRYPT_EncodeLen(blob->cbData, pbEncoded, &lenBytes);
2400 pbEncoded += lenBytes;
2402 memcpy(pbEncoded, blob->pbData, blob->cbData);
2408 SetLastError(STATUS_ACCESS_VIOLATION);
2412 TRACE("returning %d (%08x)\n", ret, GetLastError());
2416 static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
2417 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2418 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2424 const CRYPT_BIT_BLOB *blob = (const CRYPT_BIT_BLOB *)pvStructInfo;
2425 DWORD bytesNeeded, lenBytes, dataBytes;
2428 /* yep, MS allows cUnusedBits to be >= 8 */
2429 if (!blob->cUnusedBits)
2431 dataBytes = blob->cbData;
2434 else if (blob->cbData * 8 > blob->cUnusedBits)
2436 dataBytes = (blob->cbData * 8 - blob->cUnusedBits) / 8 + 1;
2437 unusedBits = blob->cUnusedBits >= 8 ? blob->cUnusedBits / 8 :
2445 CRYPT_EncodeLen(dataBytes + 1, NULL, &lenBytes);
2446 bytesNeeded = 1 + lenBytes + dataBytes + 1;
2449 *pcbEncoded = bytesNeeded;
2454 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
2455 pcbEncoded, bytesNeeded)))
2457 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2458 pbEncoded = *(BYTE **)pbEncoded;
2459 *pbEncoded++ = ASN_BITSTRING;
2460 CRYPT_EncodeLen(dataBytes + 1, pbEncoded, &lenBytes);
2461 pbEncoded += lenBytes;
2462 *pbEncoded++ = unusedBits;
2465 BYTE mask = 0xff << unusedBits;
2469 memcpy(pbEncoded, blob->pbData, dataBytes - 1);
2470 pbEncoded += dataBytes - 1;
2472 *pbEncoded = *(blob->pbData + dataBytes - 1) & mask;
2479 SetLastError(STATUS_ACCESS_VIOLATION);
2486 static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
2487 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2488 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2494 const CRYPT_BIT_BLOB *blob = (const CRYPT_BIT_BLOB *)pvStructInfo;
2495 CRYPT_BIT_BLOB newBlob = { blob->cbData, NULL, blob->cUnusedBits };
2500 newBlob.pbData = CryptMemAlloc(newBlob.cbData);
2505 for (i = 0; i < newBlob.cbData; i++)
2506 newBlob.pbData[newBlob.cbData - i - 1] = blob->pbData[i];
2512 ret = CRYPT_AsnEncodeBits(dwCertEncodingType, lpszStructType,
2513 &newBlob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2514 CryptMemFree(newBlob.pbData);
2518 SetLastError(STATUS_ACCESS_VIOLATION);
2525 BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
2526 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2527 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2529 CRYPT_INTEGER_BLOB blob = { sizeof(INT), (BYTE *)pvStructInfo };
2531 return CRYPT_AsnEncodeInteger(dwCertEncodingType, X509_MULTI_BYTE_INTEGER,
2532 &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2535 static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
2536 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2537 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2543 DWORD significantBytes, lenBytes, bytesNeeded;
2546 const CRYPT_INTEGER_BLOB *blob =
2547 (const CRYPT_INTEGER_BLOB *)pvStructInfo;
2549 significantBytes = blob->cbData;
2550 if (significantBytes)
2552 if (blob->pbData[significantBytes - 1] & 0x80)
2554 /* negative, lop off leading (little-endian) 0xffs */
2555 for (; significantBytes > 0 &&
2556 blob->pbData[significantBytes - 1] == 0xff; significantBytes--)
2558 if (blob->pbData[significantBytes - 1] < 0x80)
2566 /* positive, lop off leading (little-endian) zeroes */
2567 for (; significantBytes > 0 &&
2568 !blob->pbData[significantBytes - 1]; significantBytes--)
2570 if (significantBytes == 0)
2571 significantBytes = 1;
2572 if (blob->pbData[significantBytes - 1] > 0x7f)
2580 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
2582 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
2583 bytesNeeded = 1 + lenBytes + significantBytes;
2588 *pcbEncoded = bytesNeeded;
2593 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
2594 pcbEncoded, bytesNeeded)))
2596 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2597 pbEncoded = *(BYTE **)pbEncoded;
2598 *pbEncoded++ = ASN_INTEGER;
2601 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
2602 pbEncoded += lenBytes;
2603 *pbEncoded++ = padByte;
2607 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
2608 pbEncoded += lenBytes;
2610 for (; significantBytes > 0; significantBytes--)
2611 *(pbEncoded++) = blob->pbData[significantBytes - 1];
2617 SetLastError(STATUS_ACCESS_VIOLATION);
2624 static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
2625 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2626 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2632 DWORD significantBytes, lenBytes, bytesNeeded;
2634 const CRYPT_INTEGER_BLOB *blob =
2635 (const CRYPT_INTEGER_BLOB *)pvStructInfo;
2637 significantBytes = blob->cbData;
2638 if (significantBytes)
2640 /* positive, lop off leading (little-endian) zeroes */
2641 for (; significantBytes > 0 && !blob->pbData[significantBytes - 1];
2644 if (significantBytes == 0)
2645 significantBytes = 1;
2646 if (blob->pbData[significantBytes - 1] > 0x7f)
2650 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
2652 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
2653 bytesNeeded = 1 + lenBytes + significantBytes;
2658 *pcbEncoded = bytesNeeded;
2663 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
2664 pcbEncoded, bytesNeeded)))
2666 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2667 pbEncoded = *(BYTE **)pbEncoded;
2668 *pbEncoded++ = ASN_INTEGER;
2671 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
2672 pbEncoded += lenBytes;
2677 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
2678 pbEncoded += lenBytes;
2680 for (; significantBytes > 0; significantBytes--)
2681 *(pbEncoded++) = blob->pbData[significantBytes - 1];
2687 SetLastError(STATUS_ACCESS_VIOLATION);
2694 static BOOL WINAPI CRYPT_AsnEncodeEnumerated(DWORD dwCertEncodingType,
2695 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2696 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2698 CRYPT_INTEGER_BLOB blob;
2701 /* Encode as an unsigned integer, then change the tag to enumerated */
2702 blob.cbData = sizeof(DWORD);
2703 blob.pbData = (BYTE *)pvStructInfo;
2704 ret = CRYPT_AsnEncodeUnsignedInteger(dwCertEncodingType,
2705 X509_MULTI_BYTE_UINT, &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2706 if (ret && pbEncoded)
2708 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2709 pbEncoded = *(BYTE **)pbEncoded;
2710 pbEncoded[0] = ASN_ENUMERATED;
2715 static BOOL WINAPI CRYPT_AsnEncodeUtcTime(DWORD dwCertEncodingType,
2716 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2717 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2724 /* sorry, magic number: enough for tag, len, YYMMDDHHMMSSZ\0. I use a
2725 * temporary buffer because the output buffer is not NULL-terminated.
2728 static const DWORD bytesNeeded = sizeof(buf) - 1;
2732 *pcbEncoded = bytesNeeded;
2737 /* Sanity check the year, this is a two-digit year format */
2738 ret = FileTimeToSystemTime((const FILETIME *)pvStructInfo,
2740 if (ret && (sysTime.wYear < 1950 || sysTime.wYear > 2050))
2742 SetLastError(CRYPT_E_BAD_ENCODE);
2747 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2748 pbEncoded, pcbEncoded, bytesNeeded)))
2750 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2751 pbEncoded = *(BYTE **)pbEncoded;
2752 buf[0] = ASN_UTCTIME;
2753 buf[1] = bytesNeeded - 2;
2754 snprintf(buf + 2, sizeof(buf) - 2,
2755 "%02d%02d%02d%02d%02d%02dZ", sysTime.wYear >= 2000 ?
2756 sysTime.wYear - 2000 : sysTime.wYear - 1900,
2757 sysTime.wMonth, sysTime.wDay, sysTime.wHour,
2758 sysTime.wMinute, sysTime.wSecond);
2759 memcpy(pbEncoded, buf, bytesNeeded);
2766 SetLastError(STATUS_ACCESS_VIOLATION);
2773 static BOOL WINAPI CRYPT_AsnEncodeGeneralizedTime(DWORD dwCertEncodingType,
2774 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2775 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2782 /* sorry, magic number: enough for tag, len, YYYYMMDDHHMMSSZ\0. I use a
2783 * temporary buffer because the output buffer is not NULL-terminated.
2786 static const DWORD bytesNeeded = sizeof(buf) - 1;
2790 *pcbEncoded = bytesNeeded;
2795 ret = FileTimeToSystemTime((const FILETIME *)pvStructInfo,
2798 ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
2799 pcbEncoded, bytesNeeded);
2802 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2803 pbEncoded = *(BYTE **)pbEncoded;
2804 buf[0] = ASN_GENERALTIME;
2805 buf[1] = bytesNeeded - 2;
2806 snprintf(buf + 2, sizeof(buf) - 2, "%04d%02d%02d%02d%02d%02dZ",
2807 sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour,
2808 sysTime.wMinute, sysTime.wSecond);
2809 memcpy(pbEncoded, buf, bytesNeeded);
2815 SetLastError(STATUS_ACCESS_VIOLATION);
2822 static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
2823 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2824 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2832 /* Check the year, if it's in the UTCTime range call that encode func */
2833 if (!FileTimeToSystemTime((const FILETIME *)pvStructInfo, &sysTime))
2835 if (sysTime.wYear >= 1950 && sysTime.wYear <= 2050)
2836 ret = CRYPT_AsnEncodeUtcTime(dwCertEncodingType, lpszStructType,
2837 pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2839 ret = CRYPT_AsnEncodeGeneralizedTime(dwCertEncodingType,
2840 lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
2845 SetLastError(STATUS_ACCESS_VIOLATION);
2852 static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
2853 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2854 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2860 DWORD bytesNeeded, dataLen, lenBytes, i;
2861 const CRYPT_SEQUENCE_OF_ANY *seq =
2862 (const CRYPT_SEQUENCE_OF_ANY *)pvStructInfo;
2864 for (i = 0, dataLen = 0; i < seq->cValue; i++)
2865 dataLen += seq->rgValue[i].cbData;
2866 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2867 bytesNeeded = 1 + lenBytes + dataLen;
2870 *pcbEncoded = bytesNeeded;
2875 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
2876 pcbEncoded, bytesNeeded)))
2878 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2879 pbEncoded = *(BYTE **)pbEncoded;
2880 *pbEncoded++ = ASN_SEQUENCEOF;
2881 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2882 pbEncoded += lenBytes;
2883 for (i = 0; i < seq->cValue; i++)
2885 memcpy(pbEncoded, seq->rgValue[i].pbData,
2886 seq->rgValue[i].cbData);
2887 pbEncoded += seq->rgValue[i].cbData;
2894 SetLastError(STATUS_ACCESS_VIOLATION);
2901 static BOOL CRYPT_AsnEncodeDistPoint(const CRL_DIST_POINT *distPoint,
2902 BYTE *pbEncoded, DWORD *pcbEncoded)
2905 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2906 struct AsnConstructedItem constructed = { 0 };
2907 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2908 DWORD cItem = 0, cSwapped = 0;
2910 switch (distPoint->DistPointName.dwDistPointNameChoice)
2912 case CRL_DIST_POINT_NO_NAME:
2915 case CRL_DIST_POINT_FULL_NAME:
2916 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
2917 swapped[cSwapped].pvStructInfo = &distPoint->DistPointName.u.FullName;
2918 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
2919 constructed.tag = 0;
2920 constructed.pvStructInfo = &swapped[cSwapped];
2921 constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
2922 items[cItem].pvStructInfo = &constructed;
2923 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
2927 case CRL_DIST_POINT_ISSUER_RDN_NAME:
2928 FIXME("unimplemented for CRL_DIST_POINT_ISSUER_RDN_NAME\n");
2934 if (ret && distPoint->ReasonFlags.cbData)
2936 swapped[cSwapped].tag = ASN_CONTEXT | 1;
2937 swapped[cSwapped].pvStructInfo = &distPoint->ReasonFlags;
2938 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
2939 items[cItem].pvStructInfo = &swapped[cSwapped];
2940 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2944 if (ret && distPoint->CRLIssuer.cAltEntry)
2946 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 2;
2947 swapped[cSwapped].pvStructInfo = &distPoint->CRLIssuer;
2948 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
2949 items[cItem].pvStructInfo = &swapped[cSwapped];
2950 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2955 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
2956 pbEncoded, pcbEncoded);
2960 static BOOL WINAPI CRYPT_AsnEncodeCRLDistPoints(DWORD dwCertEncodingType,
2961 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2962 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2968 const CRL_DIST_POINTS_INFO *info =
2969 (const CRL_DIST_POINTS_INFO *)pvStructInfo;
2971 if (!info->cDistPoint)
2973 SetLastError(E_INVALIDARG);
2978 DWORD bytesNeeded, dataLen, lenBytes, i;
2981 for (i = 0, dataLen = 0; ret && i < info->cDistPoint; i++)
2985 ret = CRYPT_AsnEncodeDistPoint(&info->rgDistPoint[i], NULL,
2989 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
2991 /* Have to propagate index of failing character */
2997 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2998 bytesNeeded = 1 + lenBytes + dataLen;
3001 *pcbEncoded = bytesNeeded;
3006 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3007 pbEncoded, pcbEncoded, bytesNeeded)))
3009 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3010 pbEncoded = *(BYTE **)pbEncoded;
3011 *pbEncoded++ = ASN_SEQUENCEOF;
3012 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
3013 pbEncoded += lenBytes;
3014 for (i = 0; ret && i < info->cDistPoint; i++)
3016 DWORD len = dataLen;
3018 ret = CRYPT_AsnEncodeDistPoint(
3019 &info->rgDistPoint[i], pbEncoded, &len);
3033 SetLastError(STATUS_ACCESS_VIOLATION);
3040 static BOOL WINAPI CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType,
3041 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3042 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3048 const CERT_ENHKEY_USAGE *usage =
3049 (const CERT_ENHKEY_USAGE *)pvStructInfo;
3050 DWORD bytesNeeded = 0, lenBytes, size, i;
3053 for (i = 0; ret && i < usage->cUsageIdentifier; i++)
3055 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
3056 usage->rgpszUsageIdentifier[i],
3057 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &size);
3059 bytesNeeded += size;
3061 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
3062 bytesNeeded += 1 + lenBytes;
3066 *pcbEncoded = bytesNeeded;
3069 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3070 pbEncoded, pcbEncoded, bytesNeeded)))
3072 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3073 pbEncoded = *(BYTE **)pbEncoded;
3074 *pbEncoded++ = ASN_SEQUENCEOF;
3075 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
3077 pbEncoded += lenBytes;
3078 for (i = 0; ret && i < usage->cUsageIdentifier; i++)
3081 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
3082 usage->rgpszUsageIdentifier[i],
3083 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, pbEncoded,
3088 bytesNeeded -= size;
3097 SetLastError(STATUS_ACCESS_VIOLATION);
3104 static BOOL WINAPI CRYPT_AsnEncodeIssuingDistPoint(DWORD dwCertEncodingType,
3105 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3106 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3112 const CRL_ISSUING_DIST_POINT *point =
3113 (const CRL_ISSUING_DIST_POINT *)pvStructInfo;
3114 struct AsnEncodeSequenceItem items[6] = { { 0 } };
3115 struct AsnConstructedItem constructed = { 0 };
3116 struct AsnEncodeTagSwappedItem swapped[5] = { { 0 } };
3117 DWORD cItem = 0, cSwapped = 0;
3120 switch (point->DistPointName.dwDistPointNameChoice)
3122 case CRL_DIST_POINT_NO_NAME:
3125 case CRL_DIST_POINT_FULL_NAME:
3126 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3127 swapped[cSwapped].pvStructInfo = &point->DistPointName.u.FullName;
3128 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
3129 constructed.tag = 0;
3130 constructed.pvStructInfo = &swapped[cSwapped];
3131 constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
3132 items[cItem].pvStructInfo = &constructed;
3133 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
3138 SetLastError(E_INVALIDARG);
3141 if (ret && point->fOnlyContainsUserCerts)
3143 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3144 swapped[cSwapped].pvStructInfo = &point->fOnlyContainsUserCerts;
3145 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3146 items[cItem].pvStructInfo = &swapped[cSwapped];
3147 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3151 if (ret && point->fOnlyContainsCACerts)
3153 swapped[cSwapped].tag = ASN_CONTEXT | 2;
3154 swapped[cSwapped].pvStructInfo = &point->fOnlyContainsCACerts;
3155 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3156 items[cItem].pvStructInfo = &swapped[cSwapped];
3157 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3161 if (ret && point->OnlySomeReasonFlags.cbData)
3163 swapped[cSwapped].tag = ASN_CONTEXT | 3;
3164 swapped[cSwapped].pvStructInfo = &point->OnlySomeReasonFlags;
3165 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
3166 items[cItem].pvStructInfo = &swapped[cSwapped];
3167 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3171 if (ret && point->fIndirectCRL)
3173 swapped[cSwapped].tag = ASN_CONTEXT | 4;
3174 swapped[cSwapped].pvStructInfo = &point->fIndirectCRL;
3175 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3176 items[cItem].pvStructInfo = &swapped[cSwapped];
3177 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3182 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
3183 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3187 SetLastError(STATUS_ACCESS_VIOLATION);
3194 static BOOL WINAPI CRYPT_AsnEncodeGeneralSubtree(DWORD dwCertEncodingType,
3195 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3196 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3199 const CERT_GENERAL_SUBTREE *subtree =
3200 (const CERT_GENERAL_SUBTREE *)pvStructInfo;
3201 struct AsnEncodeSequenceItem items[3] = {
3202 { &subtree->Base, CRYPT_AsnEncodeAltNameEntry, 0 },
3205 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
3206 DWORD cItem = 1, cSwapped = 0;
3208 if (subtree->dwMinimum)
3210 swapped[cSwapped].tag = ASN_CONTEXT | 0;
3211 swapped[cSwapped].pvStructInfo = &subtree->dwMinimum;
3212 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
3213 items[cItem].pvStructInfo = &swapped[cSwapped];
3214 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3218 if (subtree->fMaximum)
3220 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3221 swapped[cSwapped].pvStructInfo = &subtree->dwMaximum;
3222 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
3223 items[cItem].pvStructInfo = &swapped[cSwapped];
3224 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3228 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem, dwFlags,
3229 pEncodePara, pbEncoded, pcbEncoded);
3233 static BOOL WINAPI CRYPT_AsnEncodeNameConstraints(DWORD dwCertEncodingType,
3234 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3235 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3238 CRYPT_BLOB_ARRAY permitted = { 0, NULL }, excluded = { 0, NULL };
3240 TRACE("%p\n", pvStructInfo);
3244 const CERT_NAME_CONSTRAINTS_INFO *constraints =
3245 (const CERT_NAME_CONSTRAINTS_INFO *)pvStructInfo;
3246 struct AsnEncodeSequenceItem items[2] = { { 0 } };
3247 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
3248 DWORD i, cItem = 0, cSwapped = 0;
3251 if (constraints->cPermittedSubtree)
3253 permitted.rgBlob = CryptMemAlloc(
3254 constraints->cPermittedSubtree * sizeof(CRYPT_DER_BLOB));
3255 if (permitted.rgBlob)
3257 permitted.cBlob = constraints->cPermittedSubtree;
3258 memset(permitted.rgBlob, 0,
3259 permitted.cBlob * sizeof(CRYPT_DER_BLOB));
3260 for (i = 0; ret && i < permitted.cBlob; i++)
3261 ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
3262 NULL, &constraints->rgPermittedSubtree[i],
3263 CRYPT_ENCODE_ALLOC_FLAG, NULL,
3264 (BYTE *)&permitted.rgBlob[i].pbData,
3265 &permitted.rgBlob[i].cbData);
3268 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3269 swapped[cSwapped].pvStructInfo = &permitted;
3270 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
3271 items[cItem].pvStructInfo = &swapped[cSwapped];
3272 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3280 if (constraints->cExcludedSubtree)
3282 excluded.rgBlob = CryptMemAlloc(
3283 constraints->cExcludedSubtree * sizeof(CRYPT_DER_BLOB));
3284 if (excluded.rgBlob)
3286 excluded.cBlob = constraints->cExcludedSubtree;
3287 memset(excluded.rgBlob, 0,
3288 excluded.cBlob * sizeof(CRYPT_DER_BLOB));
3289 for (i = 0; ret && i < excluded.cBlob; i++)
3290 ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
3291 NULL, &constraints->rgExcludedSubtree[i],
3292 CRYPT_ENCODE_ALLOC_FLAG, NULL,
3293 (BYTE *)&excluded.rgBlob[i].pbData,
3294 &excluded.rgBlob[i].cbData);
3297 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
3298 swapped[cSwapped].pvStructInfo = &excluded;
3299 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
3300 items[cItem].pvStructInfo = &swapped[cSwapped];
3301 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3310 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
3311 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3312 for (i = 0; i < permitted.cBlob; i++)
3313 LocalFree(permitted.rgBlob[i].pbData);
3314 for (i = 0; i < excluded.cBlob; i++)
3315 LocalFree(excluded.rgBlob[i].pbData);
3319 SetLastError(STATUS_ACCESS_VIOLATION);
3322 CryptMemFree(permitted.rgBlob);
3323 CryptMemFree(excluded.rgBlob);
3324 TRACE("returning %d\n", ret);
3328 static BOOL WINAPI CRYPT_AsnEncodeIssuerSerialNumber(
3329 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
3330 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
3334 const CERT_ISSUER_SERIAL_NUMBER *issuerSerial =
3335 (const CERT_ISSUER_SERIAL_NUMBER *)pvStructInfo;
3336 struct AsnEncodeSequenceItem items[] = {
3337 { &issuerSerial->Issuer, CRYPT_CopyEncodedBlob, 0 },
3338 { &issuerSerial->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
3341 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
3342 sizeof(items) / sizeof(items[0]), dwFlags, pEncodePara, pbEncoded,
3347 static BOOL WINAPI CRYPT_AsnEncodePKCSSignerInfo(DWORD dwCertEncodingType,
3348 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3349 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3353 if (!(dwCertEncodingType & PKCS_7_ASN_ENCODING))
3355 SetLastError(E_INVALIDARG);
3361 const CMSG_SIGNER_INFO *info = (const CMSG_SIGNER_INFO *)pvStructInfo;
3363 if (!info->Issuer.cbData)
3364 SetLastError(E_INVALIDARG);
3367 struct AsnEncodeSequenceItem items[7] = {
3368 { &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
3369 { &info->Issuer, CRYPT_AsnEncodeIssuerSerialNumber, 0 },
3370 { &info->HashAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
3373 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
3374 DWORD cItem = 3, cSwapped = 0;
3376 if (info->AuthAttrs.cAttr)
3378 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3379 swapped[cSwapped].pvStructInfo = &info->AuthAttrs;
3380 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
3381 items[cItem].pvStructInfo = &swapped[cSwapped];
3382 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3386 items[cItem].pvStructInfo = &info->HashEncryptionAlgorithm;
3387 items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
3389 items[cItem].pvStructInfo = &info->EncryptedHash;
3390 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
3392 if (info->UnauthAttrs.cAttr)
3394 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
3395 swapped[cSwapped].pvStructInfo = &info->UnauthAttrs;
3396 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
3397 items[cItem].pvStructInfo = &swapped[cSwapped];
3398 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3402 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
3403 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3408 SetLastError(STATUS_ACCESS_VIOLATION);
3414 BOOL CRYPT_AsnEncodePKCSSignedInfo(CRYPT_SIGNED_INFO *signedInfo, void *pvData,
3417 struct AsnEncodeSequenceItem items[7] = {
3418 { &signedInfo->version, CRYPT_AsnEncodeInt, 0 },
3420 struct DERSetDescriptor digestAlgorithmsSet = { 0 }, certSet = { 0 };
3421 struct DERSetDescriptor crlSet = { 0 }, signerSet = { 0 };
3422 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
3423 DWORD cItem = 1, cSwapped = 0;
3426 if (signedInfo->cSignerInfo)
3428 digestAlgorithmsSet.cItems = signedInfo->cSignerInfo;
3429 digestAlgorithmsSet.items = signedInfo->rgSignerInfo;
3430 digestAlgorithmsSet.itemSize = sizeof(CMSG_SIGNER_INFO);
3431 digestAlgorithmsSet.itemOffset =
3432 offsetof(CMSG_SIGNER_INFO, HashAlgorithm);
3433 digestAlgorithmsSet.encode = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
3434 items[cItem].pvStructInfo = &digestAlgorithmsSet;
3435 items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
3438 items[cItem].pvStructInfo = &signedInfo->content;
3439 items[cItem].encodeFunc = CRYPT_AsnEncodePKCSContentInfoInternal;
3441 if (signedInfo->cCertEncoded)
3443 certSet.cItems = signedInfo->cCertEncoded;
3444 certSet.items = signedInfo->rgCertEncoded;
3445 certSet.itemSize = sizeof(CERT_BLOB);
3446 certSet.itemOffset = 0;
3447 certSet.encode = CRYPT_CopyEncodedBlob;
3448 swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 0;
3449 swapped[cSwapped].pvStructInfo = &certSet;
3450 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
3451 items[cItem].pvStructInfo = &swapped[cSwapped];
3452 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3456 if (signedInfo->cCrlEncoded)
3458 crlSet.cItems = signedInfo->cCrlEncoded;
3459 crlSet.items = signedInfo->rgCrlEncoded;
3460 crlSet.itemSize = sizeof(CRL_BLOB);
3461 crlSet.itemOffset = 0;
3462 crlSet.encode = CRYPT_CopyEncodedBlob;
3463 swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 1;
3464 swapped[cSwapped].pvStructInfo = &crlSet;
3465 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
3466 items[cItem].pvStructInfo = &swapped[cSwapped];
3467 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3471 if (ret && signedInfo->cSignerInfo)
3473 signerSet.cItems = signedInfo->cSignerInfo;
3474 signerSet.items = signedInfo->rgSignerInfo;
3475 signerSet.itemSize = sizeof(CMSG_SIGNER_INFO);
3476 signerSet.itemOffset = 0;
3477 signerSet.encode = CRYPT_AsnEncodePKCSSignerInfo;
3478 items[cItem].pvStructInfo = &signerSet;
3479 items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
3483 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
3484 items, cItem, 0, NULL, pvData, pcbData);
3489 static CryptEncodeObjectExFunc CRYPT_GetBuiltinEncoder(DWORD dwCertEncodingType,
3490 LPCSTR lpszStructType)
3492 CryptEncodeObjectExFunc encodeFunc = NULL;
3494 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
3495 && (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
3497 SetLastError(ERROR_FILE_NOT_FOUND);
3501 if (!HIWORD(lpszStructType))
3503 switch (LOWORD(lpszStructType))
3505 case LOWORD(X509_CERT):
3506 encodeFunc = CRYPT_AsnEncodeCert;
3508 case LOWORD(X509_CERT_TO_BE_SIGNED):
3509 encodeFunc = CRYPT_AsnEncodeCertInfo;
3511 case LOWORD(X509_CERT_CRL_TO_BE_SIGNED):
3512 encodeFunc = CRYPT_AsnEncodeCRLInfo;
3514 case LOWORD(X509_EXTENSIONS):
3515 encodeFunc = CRYPT_AsnEncodeExtensions;
3517 case LOWORD(X509_NAME_VALUE):
3518 encodeFunc = CRYPT_AsnEncodeNameValue;
3520 case LOWORD(X509_NAME):
3521 encodeFunc = CRYPT_AsnEncodeName;
3523 case LOWORD(X509_PUBLIC_KEY_INFO):
3524 encodeFunc = CRYPT_AsnEncodePubKeyInfo;
3526 case LOWORD(X509_AUTHORITY_KEY_ID):
3527 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
3529 case LOWORD(X509_ALTERNATE_NAME):
3530 encodeFunc = CRYPT_AsnEncodeAltName;
3532 case LOWORD(X509_BASIC_CONSTRAINTS):
3533 encodeFunc = CRYPT_AsnEncodeBasicConstraints;
3535 case LOWORD(X509_BASIC_CONSTRAINTS2):
3536 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
3538 case LOWORD(RSA_CSP_PUBLICKEYBLOB):
3539 encodeFunc = CRYPT_AsnEncodeRsaPubKey;
3541 case LOWORD(X509_UNICODE_NAME):
3542 encodeFunc = CRYPT_AsnEncodeUnicodeName;
3544 case LOWORD(PKCS_CONTENT_INFO):
3545 encodeFunc = CRYPT_AsnEncodePKCSContentInfo;
3547 case LOWORD(PKCS_ATTRIBUTE):
3548 encodeFunc = CRYPT_AsnEncodePKCSAttribute;
3550 case LOWORD(X509_UNICODE_NAME_VALUE):
3551 encodeFunc = CRYPT_AsnEncodeUnicodeNameValue;
3553 case LOWORD(X509_OCTET_STRING):
3554 encodeFunc = CRYPT_AsnEncodeOctets;
3556 case LOWORD(X509_BITS):
3557 case LOWORD(X509_KEY_USAGE):
3558 encodeFunc = CRYPT_AsnEncodeBits;
3560 case LOWORD(X509_INTEGER):
3561 encodeFunc = CRYPT_AsnEncodeInt;
3563 case LOWORD(X509_MULTI_BYTE_INTEGER):
3564 encodeFunc = CRYPT_AsnEncodeInteger;
3566 case LOWORD(X509_MULTI_BYTE_UINT):
3567 encodeFunc = CRYPT_AsnEncodeUnsignedInteger;
3569 case LOWORD(X509_ENUMERATED):
3570 encodeFunc = CRYPT_AsnEncodeEnumerated;
3572 case LOWORD(X509_CHOICE_OF_TIME):
3573 encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
3575 case LOWORD(X509_AUTHORITY_KEY_ID2):
3576 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
3578 case LOWORD(X509_SEQUENCE_OF_ANY):
3579 encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
3581 case LOWORD(PKCS_UTC_TIME):
3582 encodeFunc = CRYPT_AsnEncodeUtcTime;
3584 case LOWORD(X509_CRL_DIST_POINTS):
3585 encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
3587 case LOWORD(X509_ENHANCED_KEY_USAGE):
3588 encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
3590 case LOWORD(PKCS_ATTRIBUTES):
3591 encodeFunc = CRYPT_AsnEncodePKCSAttributes;
3593 case LOWORD(X509_ISSUING_DIST_POINT):
3594 encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
3596 case LOWORD(X509_NAME_CONSTRAINTS):
3597 encodeFunc = CRYPT_AsnEncodeNameConstraints;
3599 case LOWORD(PKCS7_SIGNER_INFO):
3600 encodeFunc = CRYPT_AsnEncodePKCSSignerInfo;
3604 else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
3605 encodeFunc = CRYPT_AsnEncodeExtensions;
3606 else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
3607 encodeFunc = CRYPT_AsnEncodeUtcTime;
3608 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER))
3609 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
3610 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER2))
3611 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
3612 else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
3613 encodeFunc = CRYPT_AsnEncodeEnumerated;
3614 else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
3615 encodeFunc = CRYPT_AsnEncodeBits;
3616 else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
3617 encodeFunc = CRYPT_AsnEncodeOctets;
3618 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS))
3619 encodeFunc = CRYPT_AsnEncodeBasicConstraints;
3620 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
3621 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
3622 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
3623 encodeFunc = CRYPT_AsnEncodeAltName;
3624 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
3625 encodeFunc = CRYPT_AsnEncodeAltName;
3626 else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
3627 encodeFunc = CRYPT_AsnEncodeAltName;
3628 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
3629 encodeFunc = CRYPT_AsnEncodeAltName;
3630 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
3631 encodeFunc = CRYPT_AsnEncodeAltName;
3632 else if (!strcmp(lpszStructType, szOID_CRL_DIST_POINTS))
3633 encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
3634 else if (!strcmp(lpszStructType, szOID_ENHANCED_KEY_USAGE))
3635 encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
3636 else if (!strcmp(lpszStructType, szOID_ISSUING_DIST_POINT))
3637 encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
3638 else if (!strcmp(lpszStructType, szOID_NAME_CONSTRAINTS))
3639 encodeFunc = CRYPT_AsnEncodeNameConstraints;
3643 static CryptEncodeObjectFunc CRYPT_LoadEncoderFunc(DWORD dwCertEncodingType,
3644 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
3646 static HCRYPTOIDFUNCSET set = NULL;
3647 CryptEncodeObjectFunc encodeFunc = NULL;
3650 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_FUNC, 0);
3651 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
3652 (void **)&encodeFunc, hFunc);
3656 static CryptEncodeObjectExFunc CRYPT_LoadEncoderExFunc(DWORD dwCertEncodingType,
3657 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
3659 static HCRYPTOIDFUNCSET set = NULL;
3660 CryptEncodeObjectExFunc encodeFunc = NULL;
3663 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_EX_FUNC, 0);
3664 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
3665 (void **)&encodeFunc, hFunc);
3669 BOOL WINAPI CryptEncodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
3670 const void *pvStructInfo, BYTE *pbEncoded, DWORD *pcbEncoded)
3673 HCRYPTOIDFUNCADDR hFunc = NULL;
3674 CryptEncodeObjectFunc pCryptEncodeObject = NULL;
3675 CryptEncodeObjectExFunc pCryptEncodeObjectEx = NULL;
3677 TRACE_(crypt)("(0x%08x, %s, %p, %p, %p)\n", dwCertEncodingType,
3678 debugstr_a(lpszStructType), pvStructInfo, pbEncoded,
3681 if (!pbEncoded && !pcbEncoded)
3683 SetLastError(ERROR_INVALID_PARAMETER);
3687 if (!(pCryptEncodeObjectEx = CRYPT_GetBuiltinEncoder(dwCertEncodingType,
3690 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
3691 debugstr_a(lpszStructType));
3692 pCryptEncodeObject = CRYPT_LoadEncoderFunc(dwCertEncodingType,
3693 lpszStructType, &hFunc);
3694 if (!pCryptEncodeObject)
3695 pCryptEncodeObjectEx = CRYPT_LoadEncoderExFunc(dwCertEncodingType,
3696 lpszStructType, &hFunc);
3698 if (pCryptEncodeObject)
3699 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
3700 pvStructInfo, pbEncoded, pcbEncoded);
3701 else if (pCryptEncodeObjectEx)
3702 ret = pCryptEncodeObjectEx(dwCertEncodingType, lpszStructType,
3703 pvStructInfo, 0, NULL, pbEncoded, pcbEncoded);
3705 CryptFreeOIDFunctionAddress(hFunc, 0);
3706 TRACE_(crypt)("returning %d\n", ret);
3710 BOOL WINAPI CryptEncodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
3711 const void *pvStructInfo, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
3712 void *pvEncoded, DWORD *pcbEncoded)
3715 HCRYPTOIDFUNCADDR hFunc = NULL;
3716 CryptEncodeObjectExFunc encodeFunc = NULL;
3718 TRACE_(crypt)("(0x%08x, %s, %p, 0x%08x, %p, %p, %p)\n", dwCertEncodingType,
3719 debugstr_a(lpszStructType), pvStructInfo, dwFlags, pEncodePara,
3720 pvEncoded, pcbEncoded);
3722 if (!pvEncoded && !pcbEncoded)
3724 SetLastError(ERROR_INVALID_PARAMETER);
3728 SetLastError(NOERROR);
3729 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG && pvEncoded)
3730 *(BYTE **)pvEncoded = NULL;
3731 encodeFunc = CRYPT_GetBuiltinEncoder(dwCertEncodingType, lpszStructType);
3734 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
3735 debugstr_a(lpszStructType));
3736 encodeFunc = CRYPT_LoadEncoderExFunc(dwCertEncodingType, lpszStructType,
3740 ret = encodeFunc(dwCertEncodingType, lpszStructType, pvStructInfo,
3741 dwFlags, pEncodePara, pvEncoded, pcbEncoded);
3744 CryptEncodeObjectFunc pCryptEncodeObject =
3745 CRYPT_LoadEncoderFunc(dwCertEncodingType, lpszStructType, &hFunc);
3747 if (pCryptEncodeObject)
3749 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3751 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
3752 pvStructInfo, NULL, pcbEncoded);
3753 if (ret && (ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3754 pvEncoded, pcbEncoded, *pcbEncoded)))
3755 ret = pCryptEncodeObject(dwCertEncodingType,
3756 lpszStructType, pvStructInfo, *(BYTE **)pvEncoded,
3760 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
3761 pvStructInfo, pvEncoded, pcbEncoded);
3765 CryptFreeOIDFunctionAddress(hFunc, 0);
3766 TRACE_(crypt)("returning %d\n", ret);
3770 BOOL WINAPI CryptExportPublicKeyInfo(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
3771 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
3773 return CryptExportPublicKeyInfoEx(hCryptProv, dwKeySpec, dwCertEncodingType,
3774 NULL, 0, NULL, pInfo, pcbInfo);
3777 static BOOL WINAPI CRYPT_ExportRsaPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
3778 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
3779 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
3783 static CHAR oid[] = szOID_RSA_RSA;
3785 TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
3786 dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
3787 pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
3789 if (!pszPublicKeyObjId)
3790 pszPublicKeyObjId = oid;
3791 if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key)))
3795 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, NULL, &keySize);
3798 LPBYTE pubKey = CryptMemAlloc(keySize);
3802 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, pubKey,
3806 DWORD encodedLen = 0;
3808 ret = CryptEncodeObject(dwCertEncodingType,
3809 RSA_CSP_PUBLICKEYBLOB, pubKey, NULL, &encodedLen);
3812 DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) +
3813 strlen(pszPublicKeyObjId) + 1 + encodedLen;
3816 *pcbInfo = sizeNeeded;
3817 else if (*pcbInfo < sizeNeeded)
3819 SetLastError(ERROR_MORE_DATA);
3820 *pcbInfo = sizeNeeded;
3825 pInfo->Algorithm.pszObjId = (char *)pInfo +
3826 sizeof(CERT_PUBLIC_KEY_INFO);
3827 lstrcpyA(pInfo->Algorithm.pszObjId,
3829 pInfo->Algorithm.Parameters.cbData = 0;
3830 pInfo->Algorithm.Parameters.pbData = NULL;
3831 pInfo->PublicKey.pbData =
3832 (BYTE *)pInfo->Algorithm.pszObjId
3833 + lstrlenA(pInfo->Algorithm.pszObjId) + 1;
3834 pInfo->PublicKey.cbData = encodedLen;
3835 pInfo->PublicKey.cUnusedBits = 0;
3836 ret = CryptEncodeObject(dwCertEncodingType,
3837 RSA_CSP_PUBLICKEYBLOB, pubKey,
3838 pInfo->PublicKey.pbData, &pInfo->PublicKey.cbData);
3842 CryptMemFree(pubKey);
3847 CryptDestroyKey(key);
3852 typedef BOOL (WINAPI *ExportPublicKeyInfoExFunc)(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
3853 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
3854 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo);
3856 BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
3857 DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags,
3858 void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
3860 static HCRYPTOIDFUNCSET set = NULL;
3862 ExportPublicKeyInfoExFunc exportFunc = NULL;
3863 HCRYPTOIDFUNCADDR hFunc = NULL;
3865 TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
3866 dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
3867 pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
3871 SetLastError(ERROR_INVALID_PARAMETER);
3875 if (pszPublicKeyObjId)
3878 set = CryptInitOIDFunctionSet(CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC,
3880 CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId,
3881 0, (void **)&exportFunc, &hFunc);
3884 exportFunc = CRYPT_ExportRsaPublicKeyInfoEx;
3885 ret = exportFunc(hCryptProv, dwKeySpec, dwCertEncodingType,
3886 pszPublicKeyObjId, dwFlags, pvAuxInfo, pInfo, pcbInfo);
3888 CryptFreeOIDFunctionAddress(hFunc, 0);
3892 BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
3893 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
3895 return CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo,
3899 static BOOL WINAPI CRYPT_ImportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv,
3900 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
3901 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
3904 DWORD pubKeySize = 0;
3906 TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
3907 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
3909 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
3910 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize);
3913 LPBYTE pubKey = CryptMemAlloc(pubKeySize);
3917 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
3918 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, pubKey,
3923 ((BLOBHEADER*)pubKey)->aiKeyAlg = aiKeyAlg;
3924 ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0,
3927 CryptMemFree(pubKey);
3935 typedef BOOL (WINAPI *ImportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
3936 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
3937 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey);
3939 BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
3940 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
3941 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
3943 static HCRYPTOIDFUNCSET set = NULL;
3945 ImportPublicKeyInfoExFunc importFunc = NULL;
3946 HCRYPTOIDFUNCADDR hFunc = NULL;
3948 TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
3949 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
3952 set = CryptInitOIDFunctionSet(CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC, 0);
3953 CryptGetOIDFunctionAddress(set, dwCertEncodingType,
3954 pInfo->Algorithm.pszObjId, 0, (void **)&importFunc, &hFunc);
3956 importFunc = CRYPT_ImportRsaPublicKeyInfoEx;
3957 ret = importFunc(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags,
3960 CryptFreeOIDFunctionAddress(hFunc, 0);