wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / crypt32 / encode.c
blob46f4b8f25e72a3217c2c13871065a5f5663f99ac
1 /*
2 * Copyright 2005-2008 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * This file implements ASN.1 DER encoding of a limited set of types.
19 * It isn't a full ASN.1 implementation. Microsoft implements BER
20 * encoding of many of the basic types in msasn1.dll, but that interface isn't
21 * implemented, so I implement them here.
23 * References:
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"
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
38 #define NONAMELESSUNION
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wincrypt.h"
43 #include "snmp.h"
44 #include "wine/debug.h"
45 #include "wine/exception.h"
46 #include "crypt32_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(cryptasn);
49 WINE_DECLARE_DEBUG_CHANNEL(crypt);
51 typedef BOOL (WINAPI *CryptEncodeObjectFunc)(DWORD, LPCSTR, const void *,
52 BYTE *, DWORD *);
54 /* Prototypes for built-in encoders. They follow the Ex style prototypes.
55 * The dwCertEncodingType and lpszStructType are ignored by the built-in
56 * functions, but the parameters are retained to simplify CryptEncodeObjectEx,
57 * since it must call functions in external DLLs that follow these signatures.
59 BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
60 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
61 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
62 static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
63 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
64 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
65 static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
66 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
67 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
68 static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
69 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
70 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
71 static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
72 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
73 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
74 static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
75 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
76 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
77 static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
78 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
79 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
80 static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
81 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
82 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
83 static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
84 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
85 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
86 static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
87 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
88 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
89 static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
90 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
91 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
92 static BOOL WINAPI CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType,
93 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
94 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
95 static BOOL WINAPI CRYPT_AsnEncodePKCSAttributes(DWORD dwCertEncodingType,
96 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
97 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
99 BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara,
100 BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded)
102 BOOL ret = TRUE;
104 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
106 if (pEncodePara && pEncodePara->pfnAlloc)
107 *(BYTE **)pbEncoded = pEncodePara->pfnAlloc(bytesNeeded);
108 else
109 *(BYTE **)pbEncoded = LocalAlloc(0, bytesNeeded);
110 if (!*(BYTE **)pbEncoded)
111 ret = FALSE;
112 else
113 *pcbEncoded = bytesNeeded;
115 else if (bytesNeeded > *pcbEncoded)
117 *pcbEncoded = bytesNeeded;
118 SetLastError(ERROR_MORE_DATA);
119 ret = FALSE;
121 else
122 *pcbEncoded = bytesNeeded;
123 return ret;
126 static void CRYPT_FreeSpace(const CRYPT_ENCODE_PARA *pEncodePara, LPVOID pv)
128 if (pEncodePara && pEncodePara->pfnFree)
129 pEncodePara->pfnFree(pv);
130 else
131 LocalFree(pv);
134 BOOL CRYPT_EncodeLen(DWORD len, BYTE *pbEncoded, DWORD *pcbEncoded)
136 DWORD bytesNeeded, significantBytes = 0;
138 if (len <= 0x7f)
139 bytesNeeded = 1;
140 else
142 DWORD temp;
144 for (temp = len, significantBytes = sizeof(temp); !(temp & 0xff000000);
145 temp <<= 8, significantBytes--)
147 bytesNeeded = significantBytes + 1;
149 if (!pbEncoded)
151 *pcbEncoded = bytesNeeded;
152 return TRUE;
154 if (*pcbEncoded < bytesNeeded)
156 SetLastError(ERROR_MORE_DATA);
157 return FALSE;
159 if (len <= 0x7f)
160 *pbEncoded = (BYTE)len;
161 else
163 DWORD i;
165 *pbEncoded++ = significantBytes | 0x80;
166 for (i = 0; i < significantBytes; i++)
168 *(pbEncoded + significantBytes - i - 1) = (BYTE)(len & 0xff);
169 len >>= 8;
172 *pcbEncoded = bytesNeeded;
173 return TRUE;
176 BOOL WINAPI CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType,
177 struct AsnEncodeSequenceItem items[], DWORD cItem, DWORD dwFlags,
178 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
180 BOOL ret;
181 DWORD i, dataLen = 0;
183 TRACE("%p, %d, %08x, %p, %p, %d\n", items, cItem, dwFlags, pEncodePara,
184 pbEncoded, pbEncoded ? *pcbEncoded : 0);
185 for (i = 0, ret = TRUE; ret && i < cItem; i++)
187 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
188 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
189 NULL, &items[i].size);
190 /* Some functions propagate their errors through the size */
191 if (!ret)
192 *pcbEncoded = items[i].size;
193 dataLen += items[i].size;
195 if (ret)
197 DWORD lenBytes, bytesNeeded;
199 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
200 bytesNeeded = 1 + lenBytes + dataLen;
201 if (!pbEncoded)
202 *pcbEncoded = bytesNeeded;
203 else
205 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
206 pcbEncoded, bytesNeeded)))
208 BYTE *out;
210 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
211 pbEncoded = *(BYTE **)pbEncoded;
212 out = pbEncoded;
213 *out++ = ASN_SEQUENCE;
214 CRYPT_EncodeLen(dataLen, out, &lenBytes);
215 out += lenBytes;
216 for (i = 0; ret && i < cItem; i++)
218 ret = items[i].encodeFunc(dwCertEncodingType, NULL,
219 items[i].pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG,
220 NULL, out, &items[i].size);
221 /* Some functions propagate their errors through the size */
222 if (!ret)
223 *pcbEncoded = items[i].size;
224 out += items[i].size;
226 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
227 CRYPT_FreeSpace(pEncodePara, pbEncoded);
231 TRACE("returning %d (%08x)\n", ret, GetLastError());
232 return ret;
235 BOOL WINAPI CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType,
236 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
237 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
239 BOOL ret;
240 const struct AsnConstructedItem *item = pvStructInfo;
241 DWORD len;
243 if ((ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
244 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &len)))
246 DWORD dataLen, bytesNeeded;
248 CRYPT_EncodeLen(len, NULL, &dataLen);
249 bytesNeeded = 1 + dataLen + len;
250 if (!pbEncoded)
251 *pcbEncoded = bytesNeeded;
252 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
253 pbEncoded, pcbEncoded, bytesNeeded)))
255 BYTE *out;
257 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
258 pbEncoded = *(BYTE **)pbEncoded;
259 out = pbEncoded;
260 *out++ = ASN_CONTEXT | ASN_CONSTRUCTOR | item->tag;
261 CRYPT_EncodeLen(len, out, &dataLen);
262 out += dataLen;
263 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
264 item->pvStructInfo, dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
265 out, &len);
266 if (!ret)
268 /* Some functions propagate their errors through the size */
269 *pcbEncoded = len;
270 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
271 CRYPT_FreeSpace(pEncodePara, pbEncoded);
275 else
277 /* Some functions propagate their errors through the size */
278 *pcbEncoded = len;
280 return ret;
283 struct AsnEncodeTagSwappedItem
285 BYTE tag;
286 const void *pvStructInfo;
287 CryptEncodeObjectExFunc encodeFunc;
290 /* Sort of a wacky hack, it encodes something using the struct
291 * AsnEncodeTagSwappedItem's encodeFunc, then replaces the tag byte with the tag
292 * given in the struct AsnEncodeTagSwappedItem.
294 static BOOL WINAPI CRYPT_AsnEncodeSwapTag(DWORD dwCertEncodingType,
295 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
296 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
298 BOOL ret;
299 const struct AsnEncodeTagSwappedItem *item = pvStructInfo;
301 ret = item->encodeFunc(dwCertEncodingType, lpszStructType,
302 item->pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
303 if (ret && pbEncoded)
304 *pbEncoded = item->tag;
305 return ret;
308 static BOOL WINAPI CRYPT_AsnEncodeCertVersion(DWORD dwCertEncodingType,
309 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
310 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
312 const DWORD *ver = pvStructInfo;
313 BOOL ret;
315 /* CERT_V1 is not encoded */
316 if (*ver == CERT_V1)
318 *pcbEncoded = 0;
319 ret = TRUE;
321 else
323 struct AsnConstructedItem item = { 0, ver, CRYPT_AsnEncodeInt };
325 ret = CRYPT_AsnEncodeConstructed(dwCertEncodingType, X509_INTEGER,
326 &item, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
328 return ret;
331 static BOOL WINAPI CRYPT_CopyEncodedBlob(DWORD dwCertEncodingType,
332 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
333 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
335 const CRYPT_DER_BLOB *blob = pvStructInfo;
336 BOOL ret;
338 if (!pbEncoded)
340 *pcbEncoded = blob->cbData;
341 ret = TRUE;
343 else
345 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
346 pcbEncoded, blob->cbData)))
348 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
349 pbEncoded = *(BYTE **)pbEncoded;
350 if (blob->cbData)
351 memcpy(pbEncoded, blob->pbData, blob->cbData);
352 *pcbEncoded = blob->cbData;
355 return ret;
358 static BOOL WINAPI CRYPT_AsnEncodeValidity(DWORD dwCertEncodingType,
359 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
360 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
362 BOOL ret;
363 /* This has two filetimes in a row, a NotBefore and a NotAfter */
364 const FILETIME *timePtr = pvStructInfo;
365 struct AsnEncodeSequenceItem items[] = {
366 { timePtr, CRYPT_AsnEncodeChoiceOfTime, 0 },
367 { timePtr + 1, CRYPT_AsnEncodeChoiceOfTime, 0 },
370 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
371 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
372 pcbEncoded);
373 return ret;
376 /* Like CRYPT_AsnEncodeAlgorithmId, but encodes parameters as an asn.1 NULL
377 * if they are empty.
379 static BOOL WINAPI CRYPT_AsnEncodeAlgorithmIdWithNullParams(
380 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
381 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
382 DWORD *pcbEncoded)
384 const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
385 static const BYTE asn1Null[] = { ASN_NULL, 0 };
386 static const CRYPT_DATA_BLOB nullBlob = { sizeof(asn1Null),
387 (LPBYTE)asn1Null };
388 BOOL ret;
389 struct AsnEncodeSequenceItem items[2] = {
390 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
391 { NULL, CRYPT_CopyEncodedBlob, 0 },
394 if (algo->Parameters.cbData)
395 items[1].pvStructInfo = &algo->Parameters;
396 else
397 items[1].pvStructInfo = &nullBlob;
398 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
399 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
400 pcbEncoded);
401 return ret;
404 static BOOL WINAPI CRYPT_AsnEncodeAlgorithmId(DWORD dwCertEncodingType,
405 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
406 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
408 const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
409 BOOL ret;
410 struct AsnEncodeSequenceItem items[] = {
411 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
412 { &algo->Parameters, CRYPT_CopyEncodedBlob, 0 },
415 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
416 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
417 pcbEncoded);
418 return ret;
421 static BOOL WINAPI CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType,
422 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
423 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
425 BOOL ret;
427 __TRY
429 const CERT_PUBLIC_KEY_INFO *info = pvStructInfo;
430 struct AsnEncodeSequenceItem items[] = {
431 { &info->Algorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams, 0 },
432 { &info->PublicKey, CRYPT_AsnEncodeBits, 0 },
435 TRACE("Encoding public key with OID %s\n",
436 debugstr_a(info->Algorithm.pszObjId));
437 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
438 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
439 pcbEncoded);
441 __EXCEPT_PAGE_FAULT
443 SetLastError(STATUS_ACCESS_VIOLATION);
444 ret = FALSE;
446 __ENDTRY
447 return ret;
450 static BOOL WINAPI CRYPT_AsnEncodeCert(DWORD dwCertEncodingType,
451 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
452 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
454 BOOL ret;
456 __TRY
458 const CERT_SIGNED_CONTENT_INFO *info = pvStructInfo;
459 struct AsnEncodeSequenceItem items[] = {
460 { &info->ToBeSigned, CRYPT_CopyEncodedBlob, 0 },
461 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
462 { &info->Signature, CRYPT_AsnEncodeBitsSwapBytes, 0 },
465 if (dwFlags & CRYPT_ENCODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG)
466 items[2].encodeFunc = CRYPT_AsnEncodeBits;
467 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
468 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
469 pcbEncoded);
471 __EXCEPT_PAGE_FAULT
473 SetLastError(STATUS_ACCESS_VIOLATION);
474 ret = FALSE;
476 __ENDTRY
477 return ret;
480 BOOL WINAPI CRYPT_AsnEncodePubKeyInfoNoNull(DWORD dwCertEncodingType,
481 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
482 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
484 BOOL ret;
485 const CERT_PUBLIC_KEY_INFO *info = pvStructInfo;
486 struct AsnEncodeSequenceItem items[] = {
487 { &info->Algorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
488 { &info->PublicKey, CRYPT_AsnEncodeBits, 0 },
491 TRACE("Encoding public key with OID %s\n",
492 debugstr_a(info->Algorithm.pszObjId));
493 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
494 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
495 pcbEncoded);
496 return ret;
499 /* Like in Windows, this blithely ignores the validity of the passed-in
500 * CERT_INFO, and just encodes it as-is. The resulting encoded data may not
501 * decode properly, see CRYPT_AsnDecodeCertInfo.
503 static BOOL WINAPI CRYPT_AsnEncodeCertInfo(DWORD dwCertEncodingType,
504 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
505 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
507 BOOL ret;
509 __TRY
511 const CERT_INFO *info = pvStructInfo;
512 struct AsnEncodeSequenceItem items[10] = {
513 { &info->dwVersion, CRYPT_AsnEncodeCertVersion, 0 },
514 { &info->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
515 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
516 { &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
517 { &info->NotBefore, CRYPT_AsnEncodeValidity, 0 },
518 { &info->Subject, CRYPT_CopyEncodedBlob, 0 },
519 { &info->SubjectPublicKeyInfo, CRYPT_AsnEncodePubKeyInfoNoNull, 0 },
520 { 0 }
522 struct AsnConstructedItem constructed = { 0 };
523 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
524 DWORD cItem = 7, cSwapped = 0;
526 if (info->IssuerUniqueId.cbData)
528 swapped[cSwapped].tag = ASN_CONTEXT | 1;
529 swapped[cSwapped].pvStructInfo = &info->IssuerUniqueId;
530 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
531 items[cItem].pvStructInfo = &swapped[cSwapped];
532 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
533 cSwapped++;
534 cItem++;
536 if (info->SubjectUniqueId.cbData)
538 swapped[cSwapped].tag = ASN_CONTEXT | 2;
539 swapped[cSwapped].pvStructInfo = &info->SubjectUniqueId;
540 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
541 items[cItem].pvStructInfo = &swapped[cSwapped];
542 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
543 cSwapped++;
544 cItem++;
546 if (info->cExtension)
548 constructed.tag = 3;
549 constructed.pvStructInfo = &info->cExtension;
550 constructed.encodeFunc = CRYPT_AsnEncodeExtensions;
551 items[cItem].pvStructInfo = &constructed;
552 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
553 cItem++;
556 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
557 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
559 __EXCEPT_PAGE_FAULT
561 SetLastError(STATUS_ACCESS_VIOLATION);
562 ret = FALSE;
564 __ENDTRY
565 return ret;
568 static BOOL CRYPT_AsnEncodeCRLEntry(const CRL_ENTRY *entry,
569 BYTE *pbEncoded, DWORD *pcbEncoded)
571 struct AsnEncodeSequenceItem items[3] = {
572 { &entry->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
573 { &entry->RevocationDate, CRYPT_AsnEncodeChoiceOfTime, 0 },
574 { 0 }
576 DWORD cItem = 2;
577 BOOL ret;
579 TRACE("%p, %p, %d\n", entry, pbEncoded, pbEncoded ? *pcbEncoded : 0);
581 if (entry->cExtension)
583 items[cItem].pvStructInfo = &entry->cExtension;
584 items[cItem].encodeFunc = CRYPT_AsnEncodeExtensions;
585 cItem++;
588 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
589 pbEncoded, pcbEncoded);
591 TRACE("returning %d (%08x)\n", ret, GetLastError());
592 return ret;
595 static BOOL WINAPI CRYPT_AsnEncodeCRLEntries(DWORD dwCertEncodingType,
596 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
597 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
599 DWORD bytesNeeded, dataLen, lenBytes, i;
600 const CRL_INFO *info = pvStructInfo;
601 const CRL_ENTRY *rgCRLEntry = info->rgCRLEntry;
602 BOOL ret = TRUE;
604 for (i = 0, dataLen = 0; ret && i < info->cCRLEntry; i++)
606 DWORD size;
608 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], NULL, &size);
609 if (ret)
610 dataLen += size;
612 if (ret)
614 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
615 bytesNeeded = 1 + lenBytes + dataLen;
616 if (!pbEncoded)
617 *pcbEncoded = bytesNeeded;
618 else
620 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
621 pcbEncoded, bytesNeeded)))
623 BYTE *out;
625 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
626 pbEncoded = *(BYTE **)pbEncoded;
627 out = pbEncoded;
628 *out++ = ASN_SEQUENCEOF;
629 CRYPT_EncodeLen(dataLen, out, &lenBytes);
630 out += lenBytes;
631 for (i = 0; i < info->cCRLEntry; i++)
633 DWORD size = dataLen;
635 ret = CRYPT_AsnEncodeCRLEntry(&rgCRLEntry[i], out, &size);
636 out += size;
637 dataLen -= size;
639 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
640 CRYPT_FreeSpace(pEncodePara, pbEncoded);
644 return ret;
647 static BOOL WINAPI CRYPT_AsnEncodeCRLVersion(DWORD dwCertEncodingType,
648 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
649 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
651 const DWORD *ver = pvStructInfo;
652 BOOL ret;
654 /* CRL_V1 is not encoded */
655 if (*ver == CRL_V1)
657 *pcbEncoded = 0;
658 ret = TRUE;
660 else
661 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
662 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
663 return ret;
666 /* Like in Windows, this blithely ignores the validity of the passed-in
667 * CRL_INFO, and just encodes it as-is. The resulting encoded data may not
668 * decode properly, see CRYPT_AsnDecodeCRLInfo.
670 static BOOL WINAPI CRYPT_AsnEncodeCRLInfo(DWORD dwCertEncodingType,
671 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
672 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
674 BOOL ret;
676 __TRY
678 const CRL_INFO *info = pvStructInfo;
679 struct AsnEncodeSequenceItem items[7] = {
680 { &info->dwVersion, CRYPT_AsnEncodeCRLVersion, 0 },
681 { &info->SignatureAlgorithm, CRYPT_AsnEncodeAlgorithmId, 0 },
682 { &info->Issuer, CRYPT_CopyEncodedBlob, 0 },
683 { &info->ThisUpdate, CRYPT_AsnEncodeChoiceOfTime, 0 },
684 { 0 }
686 struct AsnConstructedItem constructed[1] = { { 0 } };
687 DWORD cItem = 4, cConstructed = 0;
689 if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
691 items[cItem].pvStructInfo = &info->NextUpdate;
692 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
693 cItem++;
695 if (info->cCRLEntry)
697 items[cItem].pvStructInfo = info;
698 items[cItem].encodeFunc = CRYPT_AsnEncodeCRLEntries;
699 cItem++;
701 if (info->cExtension)
703 constructed[cConstructed].tag = 0;
704 constructed[cConstructed].pvStructInfo = &info->cExtension;
705 constructed[cConstructed].encodeFunc = CRYPT_AsnEncodeExtensions;
706 items[cItem].pvStructInfo = &constructed[cConstructed];
707 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
708 cConstructed++;
709 cItem++;
712 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
713 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
715 __EXCEPT_PAGE_FAULT
717 SetLastError(STATUS_ACCESS_VIOLATION);
718 ret = FALSE;
720 __ENDTRY
721 return ret;
724 static BOOL CRYPT_AsnEncodeExtension(CERT_EXTENSION *ext, BYTE *pbEncoded,
725 DWORD *pcbEncoded)
727 BOOL ret;
728 struct AsnEncodeSequenceItem items[3] = {
729 { ext->pszObjId, CRYPT_AsnEncodeOid, 0 },
730 { NULL, NULL, 0 },
731 { NULL, NULL, 0 },
733 DWORD cItem = 1;
735 TRACE("%p, %p, %d\n", ext, pbEncoded, pbEncoded ? *pcbEncoded : 0);
737 if (ext->fCritical)
739 items[cItem].pvStructInfo = &ext->fCritical;
740 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
741 cItem++;
743 items[cItem].pvStructInfo = &ext->Value;
744 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
745 cItem++;
747 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
748 pbEncoded, pcbEncoded);
749 TRACE("returning %d (%08x)\n", ret, GetLastError());
750 return ret;
753 static BOOL WINAPI CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType,
754 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
755 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
757 BOOL ret;
759 __TRY
761 DWORD bytesNeeded, dataLen, lenBytes, i;
762 const CERT_EXTENSIONS *exts = pvStructInfo;
764 ret = TRUE;
765 for (i = 0, dataLen = 0; ret && i < exts->cExtension; i++)
767 DWORD size;
769 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i], NULL, &size);
770 if (ret)
771 dataLen += size;
773 if (ret)
775 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
776 bytesNeeded = 1 + lenBytes + dataLen;
777 if (!pbEncoded)
778 *pcbEncoded = bytesNeeded;
779 else
781 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
782 pbEncoded, pcbEncoded, bytesNeeded)))
784 BYTE *out;
786 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
787 pbEncoded = *(BYTE **)pbEncoded;
788 out = pbEncoded;
789 *out++ = ASN_SEQUENCEOF;
790 CRYPT_EncodeLen(dataLen, out, &lenBytes);
791 out += lenBytes;
792 for (i = 0; i < exts->cExtension; i++)
794 DWORD size = dataLen;
796 ret = CRYPT_AsnEncodeExtension(&exts->rgExtension[i],
797 out, &size);
798 out += size;
799 dataLen -= size;
801 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
802 CRYPT_FreeSpace(pEncodePara, pbEncoded);
807 __EXCEPT_PAGE_FAULT
809 SetLastError(STATUS_ACCESS_VIOLATION);
810 ret = FALSE;
812 __ENDTRY
813 return ret;
816 BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
817 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
818 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
820 LPCSTR pszObjId = pvStructInfo;
821 DWORD bytesNeeded = 0, lenBytes;
822 BOOL ret = TRUE;
823 int firstPos = 0;
824 BYTE firstByte = 0;
826 TRACE("%s\n", debugstr_a(pszObjId));
828 if (pszObjId)
830 const char *ptr;
831 int val1, val2;
833 if (sscanf(pszObjId, "%d.%d%n", &val1, &val2, &firstPos) != 2)
835 SetLastError(CRYPT_E_ASN1_ERROR);
836 return FALSE;
838 bytesNeeded++;
839 firstByte = val1 * 40 + val2;
840 ptr = pszObjId + firstPos;
841 if (*ptr == '.')
843 ptr++;
844 firstPos++;
846 while (ret && *ptr)
848 int pos;
850 /* note I assume each component is at most 32-bits long in base 2 */
851 if (sscanf(ptr, "%d%n", &val1, &pos) == 1)
853 if (val1 >= 0x10000000)
854 bytesNeeded += 5;
855 else if (val1 >= 0x200000)
856 bytesNeeded += 4;
857 else if (val1 >= 0x4000)
858 bytesNeeded += 3;
859 else if (val1 >= 0x80)
860 bytesNeeded += 2;
861 else
862 bytesNeeded += 1;
863 ptr += pos;
864 if (*ptr == '.')
865 ptr++;
867 else
869 SetLastError(CRYPT_E_ASN1_ERROR);
870 return FALSE;
873 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
875 else
876 lenBytes = 1;
877 bytesNeeded += 1 + lenBytes;
878 if (pbEncoded)
880 if (*pcbEncoded < bytesNeeded)
882 SetLastError(ERROR_MORE_DATA);
883 ret = FALSE;
885 else
887 *pbEncoded++ = ASN_OBJECTIDENTIFIER;
888 CRYPT_EncodeLen(bytesNeeded - 1 - lenBytes, pbEncoded, &lenBytes);
889 pbEncoded += lenBytes;
890 if (pszObjId)
892 const char *ptr;
893 int val, pos;
895 *pbEncoded++ = firstByte;
896 ptr = pszObjId + firstPos;
897 while (ret && *ptr)
899 sscanf(ptr, "%d%n", &val, &pos);
901 unsigned char outBytes[5];
902 int numBytes, i;
904 if (val >= 0x10000000)
905 numBytes = 5;
906 else if (val >= 0x200000)
907 numBytes = 4;
908 else if (val >= 0x4000)
909 numBytes = 3;
910 else if (val >= 0x80)
911 numBytes = 2;
912 else
913 numBytes = 1;
914 for (i = numBytes; i > 0; i--)
916 outBytes[i - 1] = val & 0x7f;
917 val >>= 7;
919 for (i = 0; i < numBytes - 1; i++)
920 *pbEncoded++ = outBytes[i] | 0x80;
921 *pbEncoded++ = outBytes[i];
922 ptr += pos;
923 if (*ptr == '.')
924 ptr++;
930 *pcbEncoded = bytesNeeded;
931 return ret;
934 static BOOL CRYPT_AsnEncodeStringCoerce(const CERT_NAME_VALUE *value,
935 BYTE tag, DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
936 DWORD *pcbEncoded)
938 BOOL ret = TRUE;
939 LPCSTR str = (LPCSTR)value->Value.pbData;
940 DWORD bytesNeeded, lenBytes, encodedLen;
942 encodedLen = value->Value.cbData ? value->Value.cbData : strlen(str);
943 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
944 bytesNeeded = 1 + lenBytes + encodedLen;
945 if (!pbEncoded)
946 *pcbEncoded = bytesNeeded;
947 else
949 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
950 pbEncoded, pcbEncoded, bytesNeeded)))
952 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
953 pbEncoded = *(BYTE **)pbEncoded;
954 *pbEncoded++ = tag;
955 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
956 pbEncoded += lenBytes;
957 memcpy(pbEncoded, str, encodedLen);
960 return ret;
963 static BOOL CRYPT_AsnEncodeBMPString(const CERT_NAME_VALUE *value,
964 DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
965 DWORD *pcbEncoded)
967 BOOL ret = TRUE;
968 LPCWSTR str = (LPCWSTR)value->Value.pbData;
969 DWORD bytesNeeded, lenBytes, strLen;
971 if (value->Value.cbData)
972 strLen = value->Value.cbData / sizeof(WCHAR);
973 else if (value->Value.pbData)
974 strLen = lstrlenW(str);
975 else
976 strLen = 0;
977 CRYPT_EncodeLen(strLen * 2, NULL, &lenBytes);
978 bytesNeeded = 1 + lenBytes + strLen * 2;
979 if (!pbEncoded)
980 *pcbEncoded = bytesNeeded;
981 else
983 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
984 pbEncoded, pcbEncoded, bytesNeeded)))
986 DWORD i;
988 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
989 pbEncoded = *(BYTE **)pbEncoded;
990 *pbEncoded++ = ASN_BMPSTRING;
991 CRYPT_EncodeLen(strLen * 2, pbEncoded, &lenBytes);
992 pbEncoded += lenBytes;
993 for (i = 0; i < strLen; i++)
995 *pbEncoded++ = (str[i] & 0xff00) >> 8;
996 *pbEncoded++ = str[i] & 0x00ff;
1000 return ret;
1003 static BOOL CRYPT_AsnEncodeUTF8String(const CERT_NAME_VALUE *value,
1004 DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
1005 DWORD *pcbEncoded)
1007 BOOL ret = TRUE;
1008 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1009 DWORD bytesNeeded, lenBytes, encodedLen, strLen;
1011 if (value->Value.cbData)
1012 strLen = value->Value.cbData / sizeof(WCHAR);
1013 else if (str)
1014 strLen = lstrlenW(str);
1015 else
1016 strLen = 0;
1017 encodedLen = WideCharToMultiByte(CP_UTF8, 0, str, strLen, NULL, 0, NULL,
1018 NULL);
1019 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
1020 bytesNeeded = 1 + lenBytes + encodedLen;
1021 if (!pbEncoded)
1022 *pcbEncoded = bytesNeeded;
1023 else
1025 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1026 pbEncoded, pcbEncoded, bytesNeeded)))
1028 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1029 pbEncoded = *(BYTE **)pbEncoded;
1030 *pbEncoded++ = ASN_UTF8STRING;
1031 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
1032 pbEncoded += lenBytes;
1033 WideCharToMultiByte(CP_UTF8, 0, str, strLen, (LPSTR)pbEncoded,
1034 bytesNeeded - lenBytes - 1, NULL, NULL);
1037 return ret;
1040 static BOOL WINAPI CRYPT_AsnEncodeNameValue(DWORD dwCertEncodingType,
1041 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1042 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1044 BOOL ret = TRUE;
1046 __TRY
1048 const CERT_NAME_VALUE *value = pvStructInfo;
1050 switch (value->dwValueType)
1052 case CERT_RDN_ANY_TYPE:
1053 /* explicitly disallowed */
1054 SetLastError(E_INVALIDARG);
1055 ret = FALSE;
1056 break;
1057 case CERT_RDN_ENCODED_BLOB:
1058 ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL,
1059 &value->Value, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1060 break;
1061 case CERT_RDN_OCTET_STRING:
1062 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_OCTETSTRING,
1063 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1064 break;
1065 case CERT_RDN_NUMERIC_STRING:
1066 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_NUMERICSTRING,
1067 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1068 break;
1069 case CERT_RDN_PRINTABLE_STRING:
1070 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_PRINTABLESTRING,
1071 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1072 break;
1073 case CERT_RDN_TELETEX_STRING:
1074 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_T61STRING,
1075 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1076 break;
1077 case CERT_RDN_VIDEOTEX_STRING:
1078 ret = CRYPT_AsnEncodeStringCoerce(value,
1079 ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1080 break;
1081 case CERT_RDN_IA5_STRING:
1082 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_IA5STRING,
1083 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1084 break;
1085 case CERT_RDN_GRAPHIC_STRING:
1086 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GRAPHICSTRING,
1087 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1088 break;
1089 case CERT_RDN_VISIBLE_STRING:
1090 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_VISIBLESTRING,
1091 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1092 break;
1093 case CERT_RDN_GENERAL_STRING:
1094 ret = CRYPT_AsnEncodeStringCoerce(value, ASN_GENERALSTRING,
1095 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1096 break;
1097 case CERT_RDN_UNIVERSAL_STRING:
1098 FIXME("CERT_RDN_UNIVERSAL_STRING: unimplemented\n");
1099 SetLastError(CRYPT_E_ASN1_CHOICE);
1100 ret = FALSE;
1101 break;
1102 case CERT_RDN_BMP_STRING:
1103 ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
1104 pbEncoded, pcbEncoded);
1105 break;
1106 case CERT_RDN_UTF8_STRING:
1107 ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
1108 pbEncoded, pcbEncoded);
1109 break;
1110 default:
1111 SetLastError(CRYPT_E_ASN1_CHOICE);
1112 ret = FALSE;
1115 __EXCEPT_PAGE_FAULT
1117 SetLastError(STATUS_ACCESS_VIOLATION);
1118 ret = FALSE;
1120 __ENDTRY
1121 return ret;
1124 static BOOL CRYPT_AsnEncodeRdnAttr(DWORD dwCertEncodingType,
1125 const CERT_RDN_ATTR *attr, CryptEncodeObjectExFunc nameValueEncodeFunc,
1126 BYTE *pbEncoded, DWORD *pcbEncoded)
1128 DWORD bytesNeeded = 0, lenBytes, size;
1129 BOOL ret;
1131 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL, attr->pszObjId,
1132 0, NULL, NULL, &size);
1133 if (ret)
1135 bytesNeeded += size;
1136 /* hack: a CERT_RDN_ATTR is identical to a CERT_NAME_VALUE beginning
1137 * with dwValueType, so "cast" it to get its encoded size
1139 ret = nameValueEncodeFunc(dwCertEncodingType, NULL, &attr->dwValueType,
1140 0, NULL, NULL, &size);
1141 if (ret)
1143 bytesNeeded += size;
1144 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1145 bytesNeeded += 1 + lenBytes;
1146 if (pbEncoded)
1148 if (*pcbEncoded < bytesNeeded)
1150 SetLastError(ERROR_MORE_DATA);
1151 ret = FALSE;
1153 else
1155 *pbEncoded++ = ASN_SEQUENCE;
1156 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded,
1157 &lenBytes);
1158 pbEncoded += lenBytes;
1159 size = bytesNeeded - 1 - lenBytes;
1160 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
1161 attr->pszObjId, 0, NULL, pbEncoded, &size);
1162 if (ret)
1164 pbEncoded += size;
1165 size = bytesNeeded - 1 - lenBytes - size;
1166 ret = nameValueEncodeFunc(dwCertEncodingType, NULL,
1167 &attr->dwValueType, 0, NULL, pbEncoded, &size);
1168 if (!ret)
1169 *pcbEncoded = size;
1173 if (ret)
1174 *pcbEncoded = bytesNeeded;
1176 else
1178 /* Have to propagate index of failing character */
1179 *pcbEncoded = size;
1182 return ret;
1185 static int __cdecl BLOBComp(const void *l, const void *r)
1187 const CRYPT_DER_BLOB *a = l, *b = r;
1188 int ret;
1190 if (!(ret = memcmp(a->pbData, b->pbData, min(a->cbData, b->cbData))))
1191 ret = a->cbData - b->cbData;
1192 return ret;
1195 /* This encodes a SET OF, which in DER must be lexicographically sorted.
1197 static BOOL WINAPI CRYPT_DEREncodeSet(DWORD dwCertEncodingType,
1198 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1199 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1201 const CRYPT_BLOB_ARRAY *set = pvStructInfo;
1202 DWORD bytesNeeded = 0, lenBytes, i;
1203 BOOL ret;
1205 for (i = 0; i < set->cBlob; i++)
1206 bytesNeeded += set->rgBlob[i].cbData;
1207 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1208 bytesNeeded += 1 + lenBytes;
1209 if (!pbEncoded)
1211 *pcbEncoded = bytesNeeded;
1212 ret = TRUE;
1214 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1215 pbEncoded, pcbEncoded, bytesNeeded)))
1217 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1218 pbEncoded = *(BYTE **)pbEncoded;
1219 qsort(set->rgBlob, set->cBlob, sizeof(CRYPT_DER_BLOB), BLOBComp);
1220 *pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
1221 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
1222 pbEncoded += lenBytes;
1223 for (i = 0; i < set->cBlob; i++)
1225 memcpy(pbEncoded, set->rgBlob[i].pbData, set->rgBlob[i].cbData);
1226 pbEncoded += set->rgBlob[i].cbData;
1229 return ret;
1232 struct DERSetDescriptor
1234 DWORD cItems;
1235 const void *items;
1236 size_t itemSize;
1237 size_t itemOffset;
1238 CryptEncodeObjectExFunc encode;
1241 static BOOL WINAPI CRYPT_DEREncodeItemsAsSet(DWORD dwCertEncodingType,
1242 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1243 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1245 const struct DERSetDescriptor *desc = pvStructInfo;
1246 CRYPT_BLOB_ARRAY setOf = { 0, NULL };
1247 BOOL ret = TRUE;
1248 DWORD i;
1250 if (desc->cItems)
1252 setOf.rgBlob = CryptMemAlloc(desc->cItems * sizeof(CRYPT_DER_BLOB));
1253 if (!setOf.rgBlob)
1254 ret = FALSE;
1255 else
1257 setOf.cBlob = desc->cItems;
1258 memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
1261 for (i = 0; ret && i < setOf.cBlob; i++)
1263 ret = desc->encode(dwCertEncodingType, lpszStructType,
1264 (const BYTE *)desc->items + i * desc->itemSize + desc->itemOffset,
1265 0, NULL, NULL, &setOf.rgBlob[i].cbData);
1266 if (ret)
1268 setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
1269 if (!setOf.rgBlob[i].pbData)
1270 ret = FALSE;
1271 else
1272 ret = desc->encode(dwCertEncodingType, lpszStructType,
1273 (const BYTE *)desc->items + i * desc->itemSize +
1274 desc->itemOffset, 0, NULL, setOf.rgBlob[i].pbData,
1275 &setOf.rgBlob[i].cbData);
1277 /* Some functions propagate their errors through the size */
1278 if (!ret)
1279 *pcbEncoded = setOf.rgBlob[i].cbData;
1281 if (ret)
1283 DWORD bytesNeeded = 0, lenBytes;
1285 for (i = 0; i < setOf.cBlob; i++)
1286 bytesNeeded += setOf.rgBlob[i].cbData;
1287 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1288 bytesNeeded += 1 + lenBytes;
1289 if (!pbEncoded)
1290 *pcbEncoded = bytesNeeded;
1291 else if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1292 pbEncoded, pcbEncoded, bytesNeeded)))
1294 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1295 pbEncoded = *(BYTE **)pbEncoded;
1296 qsort(setOf.rgBlob, setOf.cBlob, sizeof(CRYPT_DER_BLOB),
1297 BLOBComp);
1298 *pbEncoded++ = ASN_CONSTRUCTOR | ASN_SETOF;
1299 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, pbEncoded, &lenBytes);
1300 pbEncoded += lenBytes;
1301 for (i = 0; i < setOf.cBlob; i++)
1303 memcpy(pbEncoded, setOf.rgBlob[i].pbData,
1304 setOf.rgBlob[i].cbData);
1305 pbEncoded += setOf.rgBlob[i].cbData;
1309 for (i = 0; i < setOf.cBlob; i++)
1310 CryptMemFree(setOf.rgBlob[i].pbData);
1311 CryptMemFree(setOf.rgBlob);
1312 return ret;
1315 static BOOL CRYPT_AsnEncodeRdn(DWORD dwCertEncodingType, const CERT_RDN *rdn,
1316 CryptEncodeObjectExFunc nameValueEncodeFunc, BYTE *pbEncoded,
1317 DWORD *pcbEncoded)
1319 BOOL ret;
1320 CRYPT_BLOB_ARRAY setOf = { 0, NULL };
1322 __TRY
1324 DWORD i;
1326 ret = TRUE;
1327 if (rdn->cRDNAttr)
1329 setOf.rgBlob = CryptMemAlloc(rdn->cRDNAttr *
1330 sizeof(CRYPT_DER_BLOB));
1331 if (!setOf.rgBlob)
1332 ret = FALSE;
1333 else
1335 setOf.cBlob = rdn->cRDNAttr;
1336 memset(setOf.rgBlob, 0, setOf.cBlob * sizeof(CRYPT_DER_BLOB));
1339 for (i = 0; ret && i < rdn->cRDNAttr; i++)
1341 setOf.rgBlob[i].cbData = 0;
1342 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType, &rdn->rgRDNAttr[i],
1343 nameValueEncodeFunc, NULL, &setOf.rgBlob[i].cbData);
1344 if (ret)
1346 setOf.rgBlob[i].pbData = CryptMemAlloc(setOf.rgBlob[i].cbData);
1347 if (!setOf.rgBlob[i].pbData)
1348 ret = FALSE;
1349 else
1350 ret = CRYPT_AsnEncodeRdnAttr(dwCertEncodingType,
1351 &rdn->rgRDNAttr[i], nameValueEncodeFunc,
1352 setOf.rgBlob[i].pbData, &setOf.rgBlob[i].cbData);
1354 if (!ret)
1356 /* Have to propagate index of failing character */
1357 *pcbEncoded = setOf.rgBlob[i].cbData;
1360 if (ret)
1361 ret = CRYPT_DEREncodeSet(X509_ASN_ENCODING, NULL, &setOf, 0, NULL,
1362 pbEncoded, pcbEncoded);
1363 for (i = 0; i < setOf.cBlob; i++)
1364 CryptMemFree(setOf.rgBlob[i].pbData);
1366 __EXCEPT_PAGE_FAULT
1368 SetLastError(STATUS_ACCESS_VIOLATION);
1369 ret = FALSE;
1371 __ENDTRY
1372 CryptMemFree(setOf.rgBlob);
1373 return ret;
1376 static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
1377 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1378 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
1380 static BOOL WINAPI CRYPT_AsnEncodeOrCopyUnicodeNameValue(
1381 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1382 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1383 DWORD *pcbEncoded)
1385 const CERT_NAME_VALUE *value = pvStructInfo;
1386 BOOL ret;
1388 if (value->dwValueType == CERT_RDN_ENCODED_BLOB)
1389 ret = CRYPT_CopyEncodedBlob(dwCertEncodingType, NULL, &value->Value,
1390 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1391 else
1392 ret = CRYPT_AsnEncodeUnicodeNameValue(dwCertEncodingType, NULL, value,
1393 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1394 return ret;
1397 static BOOL WINAPI CRYPT_AsnEncodeUnicodeName(DWORD dwCertEncodingType,
1398 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1399 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1401 BOOL ret = TRUE;
1403 __TRY
1405 const CERT_NAME_INFO *info = pvStructInfo;
1406 DWORD bytesNeeded = 0, lenBytes, size, i;
1408 TRACE("encoding name with %d RDNs\n", info->cRDN);
1409 ret = TRUE;
1410 for (i = 0; ret && i < info->cRDN; i++)
1412 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
1413 CRYPT_AsnEncodeOrCopyUnicodeNameValue, NULL, &size);
1414 if (ret)
1415 bytesNeeded += size;
1416 else
1417 *pcbEncoded = size;
1419 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
1420 bytesNeeded += 1 + lenBytes;
1421 if (ret)
1423 if (!pbEncoded)
1424 *pcbEncoded = bytesNeeded;
1425 else
1427 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1428 pbEncoded, pcbEncoded, bytesNeeded)))
1430 BYTE *out;
1432 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1433 pbEncoded = *(BYTE **)pbEncoded;
1434 out = pbEncoded;
1435 *out++ = ASN_SEQUENCEOF;
1436 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
1437 out += lenBytes;
1438 for (i = 0; ret && i < info->cRDN; i++)
1440 size = bytesNeeded;
1441 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
1442 &info->rgRDN[i], CRYPT_AsnEncodeOrCopyUnicodeNameValue,
1443 out, &size);
1444 if (ret)
1446 out += size;
1447 bytesNeeded -= size;
1449 else
1450 *pcbEncoded = size;
1452 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
1453 CRYPT_FreeSpace(pEncodePara, pbEncoded);
1458 __EXCEPT_PAGE_FAULT
1460 SetLastError(STATUS_ACCESS_VIOLATION);
1461 ret = FALSE;
1463 __ENDTRY
1464 return ret;
1467 static BOOL WINAPI CRYPT_AsnEncodeCTLVersion(DWORD dwCertEncodingType,
1468 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1469 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1471 const DWORD *ver = pvStructInfo;
1472 BOOL ret;
1474 /* CTL_V1 is not encoded */
1475 if (*ver == CTL_V1)
1477 *pcbEncoded = 0;
1478 ret = TRUE;
1480 else
1481 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER, ver,
1482 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1483 return ret;
1486 /* Like CRYPT_AsnEncodeAlgorithmId, but encodes parameters as an asn.1 NULL
1487 * if they are empty and the OID is not empty (otherwise omits them.)
1489 static BOOL WINAPI CRYPT_AsnEncodeCTLSubjectAlgorithm(
1490 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1491 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1492 DWORD *pcbEncoded)
1494 const CRYPT_ALGORITHM_IDENTIFIER *algo = pvStructInfo;
1495 BOOL ret;
1496 struct AsnEncodeSequenceItem items[2] = {
1497 { algo->pszObjId, CRYPT_AsnEncodeOid, 0 },
1499 DWORD cItem = 1;
1501 if (algo->pszObjId)
1503 static const BYTE asn1Null[] = { ASN_NULL, 0 };
1504 static const CRYPT_DATA_BLOB nullBlob = { sizeof(asn1Null),
1505 (LPBYTE)asn1Null };
1507 if (algo->Parameters.cbData)
1508 items[cItem].pvStructInfo = &algo->Parameters;
1509 else
1510 items[cItem].pvStructInfo = &nullBlob;
1511 items[cItem].encodeFunc = CRYPT_CopyEncodedBlob;
1512 cItem++;
1514 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
1515 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1516 return ret;
1519 static BOOL CRYPT_AsnEncodeCTLEntry(const CTL_ENTRY *entry,
1520 BYTE *pbEncoded, DWORD *pcbEncoded)
1522 struct AsnEncodeSequenceItem items[2] = {
1523 { &entry->SubjectIdentifier, CRYPT_AsnEncodeOctets, 0 },
1524 { &entry->cAttribute, CRYPT_AsnEncodePKCSAttributes, 0 },
1526 BOOL ret;
1528 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
1529 ARRAY_SIZE(items), 0, NULL, pbEncoded, pcbEncoded);
1530 return ret;
1533 struct CTLEntries
1535 DWORD cEntry;
1536 CTL_ENTRY *rgEntry;
1539 static BOOL WINAPI CRYPT_AsnEncodeCTLEntries(DWORD dwCertEncodingType,
1540 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1541 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1543 BOOL ret;
1544 DWORD bytesNeeded, dataLen, lenBytes, i;
1545 const struct CTLEntries *entries = pvStructInfo;
1547 ret = TRUE;
1548 for (i = 0, dataLen = 0; ret && i < entries->cEntry; i++)
1550 DWORD size;
1552 ret = CRYPT_AsnEncodeCTLEntry(&entries->rgEntry[i], NULL, &size);
1553 if (ret)
1554 dataLen += size;
1556 if (ret)
1558 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1559 bytesNeeded = 1 + lenBytes + dataLen;
1560 if (!pbEncoded)
1561 *pcbEncoded = bytesNeeded;
1562 else
1564 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1565 pbEncoded, pcbEncoded, bytesNeeded)))
1567 BYTE *out;
1569 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1570 pbEncoded = *(BYTE **)pbEncoded;
1571 out = pbEncoded;
1572 *out++ = ASN_SEQUENCEOF;
1573 CRYPT_EncodeLen(dataLen, out, &lenBytes);
1574 out += lenBytes;
1575 for (i = 0; ret && i < entries->cEntry; i++)
1577 DWORD size = dataLen;
1579 ret = CRYPT_AsnEncodeCTLEntry(&entries->rgEntry[i],
1580 out, &size);
1581 out += size;
1582 dataLen -= size;
1584 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
1585 CRYPT_FreeSpace(pEncodePara, pbEncoded);
1589 return ret;
1592 static BOOL WINAPI CRYPT_AsnEncodeCTL(DWORD dwCertEncodingType,
1593 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1594 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1596 BOOL ret = FALSE;
1598 __TRY
1600 const CTL_INFO *info = pvStructInfo;
1601 struct AsnEncodeSequenceItem items[9] = {
1602 { &info->dwVersion, CRYPT_AsnEncodeCTLVersion, 0 },
1603 { &info->SubjectUsage, CRYPT_AsnEncodeEnhancedKeyUsage, 0 },
1605 struct AsnConstructedItem constructed = { 0 };
1606 DWORD cItem = 2;
1608 if (info->ListIdentifier.cbData)
1610 items[cItem].pvStructInfo = &info->ListIdentifier;
1611 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
1612 cItem++;
1614 if (info->SequenceNumber.cbData)
1616 items[cItem].pvStructInfo = &info->SequenceNumber;
1617 items[cItem].encodeFunc = CRYPT_AsnEncodeInteger;
1618 cItem++;
1620 items[cItem].pvStructInfo = &info->ThisUpdate;
1621 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
1622 cItem++;
1623 if (info->NextUpdate.dwLowDateTime || info->NextUpdate.dwHighDateTime)
1625 items[cItem].pvStructInfo = &info->NextUpdate;
1626 items[cItem].encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
1627 cItem++;
1629 items[cItem].pvStructInfo = &info->SubjectAlgorithm;
1630 items[cItem].encodeFunc = CRYPT_AsnEncodeCTLSubjectAlgorithm;
1631 cItem++;
1632 if (info->cCTLEntry)
1634 items[cItem].pvStructInfo = &info->cCTLEntry;
1635 items[cItem].encodeFunc = CRYPT_AsnEncodeCTLEntries;
1636 cItem++;
1638 if (info->cExtension)
1640 constructed.tag = 0;
1641 constructed.pvStructInfo = &info->cExtension;
1642 constructed.encodeFunc = CRYPT_AsnEncodeExtensions;
1643 items[cItem].pvStructInfo = &constructed;
1644 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
1645 cItem++;
1647 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
1648 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1650 __EXCEPT_PAGE_FAULT
1652 SetLastError(STATUS_ACCESS_VIOLATION);
1654 __ENDTRY
1655 return ret;
1658 static BOOL CRYPT_AsnEncodeSMIMECapability(DWORD dwCertEncodingType,
1659 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1660 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1662 BOOL ret = FALSE;
1664 __TRY
1666 const CRYPT_SMIME_CAPABILITY *capability = pvStructInfo;
1668 if (!capability->pszObjId)
1669 SetLastError(E_INVALIDARG);
1670 else
1672 struct AsnEncodeSequenceItem items[] = {
1673 { capability->pszObjId, CRYPT_AsnEncodeOid, 0 },
1674 { &capability->Parameters, CRYPT_CopyEncodedBlob, 0 },
1677 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1678 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
1679 pcbEncoded);
1682 __EXCEPT_PAGE_FAULT
1684 SetLastError(STATUS_ACCESS_VIOLATION);
1686 __ENDTRY
1687 return ret;
1690 static BOOL WINAPI CRYPT_AsnEncodeSMIMECapabilities(DWORD dwCertEncodingType,
1691 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1692 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1694 BOOL ret = FALSE;
1696 __TRY
1698 DWORD bytesNeeded, dataLen, lenBytes, i;
1699 const CRYPT_SMIME_CAPABILITIES *capabilities = pvStructInfo;
1701 ret = TRUE;
1702 for (i = 0, dataLen = 0; ret && i < capabilities->cCapability; i++)
1704 DWORD size;
1706 ret = CRYPT_AsnEncodeSMIMECapability(dwCertEncodingType, NULL,
1707 &capabilities->rgCapability[i], 0, NULL, NULL, &size);
1708 if (ret)
1709 dataLen += size;
1711 if (ret)
1713 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1714 bytesNeeded = 1 + lenBytes + dataLen;
1715 if (!pbEncoded)
1716 *pcbEncoded = bytesNeeded;
1717 else
1719 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
1720 pbEncoded, pcbEncoded, bytesNeeded)))
1722 BYTE *out;
1724 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1725 pbEncoded = *(BYTE **)pbEncoded;
1726 out = pbEncoded;
1727 *out++ = ASN_SEQUENCEOF;
1728 CRYPT_EncodeLen(dataLen, out, &lenBytes);
1729 out += lenBytes;
1730 for (i = 0; i < capabilities->cCapability; i++)
1732 DWORD size = dataLen;
1734 ret = CRYPT_AsnEncodeSMIMECapability(dwCertEncodingType,
1735 NULL, &capabilities->rgCapability[i], 0, NULL,
1736 out, &size);
1737 out += size;
1738 dataLen -= size;
1740 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
1741 CRYPT_FreeSpace(pEncodePara, pbEncoded);
1746 __EXCEPT_PAGE_FAULT
1748 SetLastError(STATUS_ACCESS_VIOLATION);
1750 __ENDTRY
1751 return ret;
1754 static BOOL WINAPI CRYPT_AsnEncodeNoticeNumbers(DWORD dwCertEncodingType,
1755 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1756 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1758 const CERT_POLICY_QUALIFIER_NOTICE_REFERENCE *noticeRef = pvStructInfo;
1759 DWORD bytesNeeded, dataLen, lenBytes, i;
1760 BOOL ret = TRUE;
1762 for (i = 0, dataLen = 0; ret && i < noticeRef->cNoticeNumbers; i++)
1764 DWORD size;
1766 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER,
1767 &noticeRef->rgNoticeNumbers[i], 0, NULL, NULL, &size);
1768 if (ret)
1769 dataLen += size;
1771 if (ret)
1773 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
1774 bytesNeeded = 1 + lenBytes + dataLen;
1775 if (!pbEncoded)
1776 *pcbEncoded = bytesNeeded;
1777 else
1779 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
1780 pcbEncoded, bytesNeeded)))
1782 BYTE *out;
1784 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
1785 pbEncoded = *(BYTE **)pbEncoded;
1786 out = pbEncoded;
1787 *out++ = ASN_SEQUENCE;
1788 CRYPT_EncodeLen(dataLen, out, &lenBytes);
1789 out += lenBytes;
1790 for (i = 0; i < noticeRef->cNoticeNumbers; i++)
1792 DWORD size = dataLen;
1794 ret = CRYPT_AsnEncodeInt(dwCertEncodingType, X509_INTEGER,
1795 &noticeRef->rgNoticeNumbers[i], 0, NULL, out, &size);
1796 out += size;
1797 dataLen -= size;
1799 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
1800 CRYPT_FreeSpace(pEncodePara, pbEncoded);
1804 return ret;
1807 static BOOL WINAPI CRYPT_AsnEncodeNoticeReference(DWORD dwCertEncodingType,
1808 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1809 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1811 const CERT_POLICY_QUALIFIER_NOTICE_REFERENCE *noticeRef = pvStructInfo;
1812 BOOL ret;
1813 CERT_NAME_VALUE orgValue = { CERT_RDN_IA5_STRING,
1814 { 0, (LPBYTE)noticeRef->pszOrganization } };
1815 struct AsnEncodeSequenceItem items[] = {
1816 { &orgValue, CRYPT_AsnEncodeNameValue, 0 },
1817 { noticeRef, CRYPT_AsnEncodeNoticeNumbers, 0 },
1820 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1821 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
1822 pcbEncoded);
1823 return ret;
1826 static BOOL WINAPI CRYPT_AsnEncodePolicyQualifierUserNotice(
1827 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1828 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1829 DWORD *pcbEncoded)
1831 BOOL ret = FALSE;
1833 __TRY
1835 const CERT_POLICY_QUALIFIER_USER_NOTICE *notice = pvStructInfo;
1836 struct AsnEncodeSequenceItem items[2];
1837 CERT_NAME_VALUE displayTextValue;
1838 DWORD cItem = 0;
1840 if (notice->pNoticeReference)
1842 items[cItem].encodeFunc = CRYPT_AsnEncodeNoticeReference;
1843 items[cItem].pvStructInfo = notice->pNoticeReference;
1844 cItem++;
1846 if (notice->pszDisplayText)
1848 displayTextValue.dwValueType = CERT_RDN_BMP_STRING;
1849 displayTextValue.Value.cbData = 0;
1850 displayTextValue.Value.pbData = (LPBYTE)notice->pszDisplayText;
1851 items[cItem].encodeFunc = CRYPT_AsnEncodeNameValue;
1852 items[cItem].pvStructInfo = &displayTextValue;
1853 cItem++;
1855 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
1856 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1858 __EXCEPT_PAGE_FAULT
1860 SetLastError(STATUS_ACCESS_VIOLATION);
1862 __ENDTRY
1863 return ret;
1866 static BOOL WINAPI CRYPT_AsnEncodePKCSAttribute(DWORD dwCertEncodingType,
1867 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1868 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1870 BOOL ret = FALSE;
1872 __TRY
1874 const CRYPT_ATTRIBUTE *attr = pvStructInfo;
1876 if (!attr->pszObjId)
1877 SetLastError(E_INVALIDARG);
1878 else
1880 struct AsnEncodeSequenceItem items[2] = {
1881 { attr->pszObjId, CRYPT_AsnEncodeOid, 0 },
1882 { &attr->cValue, CRYPT_DEREncodeSet, 0 },
1885 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1886 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded,
1887 pcbEncoded);
1890 __EXCEPT_PAGE_FAULT
1892 SetLastError(STATUS_ACCESS_VIOLATION);
1894 __ENDTRY
1895 return ret;
1898 static BOOL WINAPI CRYPT_AsnEncodePKCSAttributes(DWORD dwCertEncodingType,
1899 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1900 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1902 BOOL ret = FALSE;
1904 __TRY
1906 const CRYPT_ATTRIBUTES *attributes = pvStructInfo;
1907 struct DERSetDescriptor desc = { attributes->cAttr, attributes->rgAttr,
1908 sizeof(CRYPT_ATTRIBUTE), 0, CRYPT_AsnEncodePKCSAttribute };
1910 ret = CRYPT_DEREncodeItemsAsSet(X509_ASN_ENCODING, lpszStructType,
1911 &desc, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1913 __EXCEPT_PAGE_FAULT
1915 SetLastError(STATUS_ACCESS_VIOLATION);
1917 __ENDTRY
1918 return ret;
1921 /* Like CRYPT_AsnEncodePKCSContentInfo, but allows the OID to be NULL */
1922 static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfoInternal(
1923 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
1924 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
1925 DWORD *pcbEncoded)
1927 const CRYPT_CONTENT_INFO *info = pvStructInfo;
1928 struct AsnEncodeSequenceItem items[2] = {
1929 { info->pszObjId, CRYPT_AsnEncodeOid, 0 },
1930 { NULL, NULL, 0 },
1932 struct AsnConstructedItem constructed = { 0 };
1933 DWORD cItem = 1;
1935 if (info->Content.cbData)
1937 constructed.tag = 0;
1938 constructed.pvStructInfo = &info->Content;
1939 constructed.encodeFunc = CRYPT_CopyEncodedBlob;
1940 items[cItem].pvStructInfo = &constructed;
1941 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
1942 cItem++;
1944 return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
1945 cItem, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
1948 BOOL CRYPT_AsnEncodePKCSDigestedData(const CRYPT_DIGESTED_DATA *digestedData,
1949 void *pvData, DWORD *pcbData)
1951 struct AsnEncodeSequenceItem items[] = {
1952 { &digestedData->version, CRYPT_AsnEncodeInt, 0 },
1953 { &digestedData->DigestAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
1954 0 },
1955 { &digestedData->ContentInfo, CRYPT_AsnEncodePKCSContentInfoInternal, 0 },
1956 { &digestedData->hash, CRYPT_AsnEncodeOctets, 0 },
1959 return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, ARRAY_SIZE(items), 0, NULL, pvData, pcbData);
1962 static BOOL WINAPI CRYPT_AsnEncodePKCSContentInfo(DWORD dwCertEncodingType,
1963 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
1964 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
1966 BOOL ret = FALSE;
1968 __TRY
1970 const CRYPT_CONTENT_INFO *info = pvStructInfo;
1972 if (!info->pszObjId)
1973 SetLastError(E_INVALIDARG);
1974 else
1975 ret = CRYPT_AsnEncodePKCSContentInfoInternal(dwCertEncodingType,
1976 lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
1977 pcbEncoded);
1979 __EXCEPT_PAGE_FAULT
1981 SetLastError(STATUS_ACCESS_VIOLATION);
1983 __ENDTRY
1984 return ret;
1987 static BOOL CRYPT_AsnEncodeUnicodeStringCoerce(const CERT_NAME_VALUE *value,
1988 BYTE tag, DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
1989 DWORD *pcbEncoded)
1991 BOOL ret = TRUE;
1992 LPCWSTR str = (LPCWSTR)value->Value.pbData;
1993 DWORD bytesNeeded, lenBytes, encodedLen;
1995 if (value->Value.cbData)
1996 encodedLen = value->Value.cbData / sizeof(WCHAR);
1997 else if (str)
1998 encodedLen = lstrlenW(str);
1999 else
2000 encodedLen = 0;
2001 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
2002 bytesNeeded = 1 + lenBytes + encodedLen;
2003 if (!pbEncoded)
2004 *pcbEncoded = bytesNeeded;
2005 else
2007 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2008 pbEncoded, pcbEncoded, bytesNeeded)))
2010 DWORD i;
2012 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2013 pbEncoded = *(BYTE **)pbEncoded;
2014 *pbEncoded++ = tag;
2015 CRYPT_EncodeLen(encodedLen, pbEncoded, &lenBytes);
2016 pbEncoded += lenBytes;
2017 for (i = 0; i < encodedLen; i++)
2018 *pbEncoded++ = (BYTE)str[i];
2021 return ret;
2024 static BOOL CRYPT_AsnEncodeNumericString(const CERT_NAME_VALUE *value,
2025 DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
2026 DWORD *pcbEncoded)
2028 BOOL ret = TRUE;
2029 LPCWSTR str = (LPCWSTR)value->Value.pbData;
2030 DWORD bytesNeeded, lenBytes, encodedLen;
2032 if (value->Value.cbData)
2033 encodedLen = value->Value.cbData / sizeof(WCHAR);
2034 else if (str)
2035 encodedLen = lstrlenW(str);
2036 else
2037 encodedLen = 0;
2038 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
2039 bytesNeeded = 1 + lenBytes + encodedLen;
2040 if (!pbEncoded)
2041 *pcbEncoded = bytesNeeded;
2042 else
2044 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2045 pbEncoded, pcbEncoded, bytesNeeded)))
2047 DWORD i;
2048 BYTE *ptr;
2050 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2051 ptr = *(BYTE **)pbEncoded;
2052 else
2053 ptr = pbEncoded;
2054 *ptr++ = ASN_NUMERICSTRING;
2055 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
2056 ptr += lenBytes;
2057 for (i = 0; ret && i < encodedLen; i++)
2059 if ('0' <= str[i] && str[i] <= '9')
2060 *ptr++ = (BYTE)str[i];
2061 else
2063 *pcbEncoded = i;
2064 SetLastError(CRYPT_E_INVALID_NUMERIC_STRING);
2065 ret = FALSE;
2068 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2069 CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
2072 return ret;
2075 static inline BOOL isprintableW(WCHAR wc)
2077 return wc && wcschr( L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '()+,-./:=?", wc );
2080 static BOOL CRYPT_AsnEncodePrintableString(const CERT_NAME_VALUE *value,
2081 DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
2082 DWORD *pcbEncoded)
2084 BOOL ret = TRUE;
2085 LPCWSTR str = (LPCWSTR)value->Value.pbData;
2086 DWORD bytesNeeded, lenBytes, encodedLen;
2088 if (value->Value.cbData)
2089 encodedLen = value->Value.cbData / sizeof(WCHAR);
2090 else if (str)
2091 encodedLen = lstrlenW(str);
2092 else
2093 encodedLen = 0;
2094 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
2095 bytesNeeded = 1 + lenBytes + encodedLen;
2096 if (!pbEncoded)
2097 *pcbEncoded = bytesNeeded;
2098 else
2100 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2101 pbEncoded, pcbEncoded, bytesNeeded)))
2103 DWORD i;
2104 BYTE *ptr;
2106 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2107 ptr = *(BYTE **)pbEncoded;
2108 else
2109 ptr = pbEncoded;
2110 *ptr++ = ASN_PRINTABLESTRING;
2111 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
2112 ptr += lenBytes;
2113 for (i = 0; ret && i < encodedLen; i++)
2115 if (isprintableW(str[i]))
2116 *ptr++ = (BYTE)str[i];
2117 else
2119 *pcbEncoded = i;
2120 SetLastError(CRYPT_E_INVALID_PRINTABLE_STRING);
2121 ret = FALSE;
2124 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2125 CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
2128 return ret;
2131 static BOOL CRYPT_AsnEncodeIA5String(const CERT_NAME_VALUE *value,
2132 DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
2133 DWORD *pcbEncoded)
2135 BOOL ret = TRUE;
2136 LPCWSTR str = (LPCWSTR)value->Value.pbData;
2137 DWORD bytesNeeded, lenBytes, encodedLen;
2139 if (value->Value.cbData)
2140 encodedLen = value->Value.cbData / sizeof(WCHAR);
2141 else if (str)
2142 encodedLen = lstrlenW(str);
2143 else
2144 encodedLen = 0;
2145 CRYPT_EncodeLen(encodedLen, NULL, &lenBytes);
2146 bytesNeeded = 1 + lenBytes + encodedLen;
2147 if (!pbEncoded)
2148 *pcbEncoded = bytesNeeded;
2149 else
2151 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2152 pbEncoded, pcbEncoded, bytesNeeded)))
2154 DWORD i;
2155 BYTE *ptr;
2157 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2158 ptr = *(BYTE **)pbEncoded;
2159 else
2160 ptr = pbEncoded;
2161 *ptr++ = ASN_IA5STRING;
2162 CRYPT_EncodeLen(encodedLen, ptr, &lenBytes);
2163 ptr += lenBytes;
2164 for (i = 0; ret && i < encodedLen; i++)
2166 if (str[i] <= 0x7f)
2167 *ptr++ = (BYTE)str[i];
2168 else
2170 *pcbEncoded = i;
2171 SetLastError(CRYPT_E_INVALID_IA5_STRING);
2172 ret = FALSE;
2175 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2176 CRYPT_FreeSpace(pEncodePara, *(BYTE **)pbEncoded);
2179 return ret;
2182 static BOOL CRYPT_AsnEncodeUniversalString(const CERT_NAME_VALUE *value,
2183 DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara, BYTE *pbEncoded,
2184 DWORD *pcbEncoded)
2186 BOOL ret = TRUE;
2187 LPCWSTR str = (LPCWSTR)value->Value.pbData;
2188 DWORD bytesNeeded, lenBytes, strLen;
2190 /* FIXME: doesn't handle composite characters */
2191 if (value->Value.cbData)
2192 strLen = value->Value.cbData / sizeof(WCHAR);
2193 else if (str)
2194 strLen = lstrlenW(str);
2195 else
2196 strLen = 0;
2197 CRYPT_EncodeLen(strLen * 4, NULL, &lenBytes);
2198 bytesNeeded = 1 + lenBytes + strLen * 4;
2199 if (!pbEncoded)
2200 *pcbEncoded = bytesNeeded;
2201 else
2203 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2204 pbEncoded, pcbEncoded, bytesNeeded)))
2206 DWORD i;
2208 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2209 pbEncoded = *(BYTE **)pbEncoded;
2210 *pbEncoded++ = ASN_UNIVERSALSTRING;
2211 CRYPT_EncodeLen(strLen * 4, pbEncoded, &lenBytes);
2212 pbEncoded += lenBytes;
2213 for (i = 0; i < strLen; i++)
2215 *pbEncoded++ = 0;
2216 *pbEncoded++ = 0;
2217 *pbEncoded++ = (BYTE)((str[i] & 0xff00) >> 8);
2218 *pbEncoded++ = (BYTE)(str[i] & 0x00ff);
2222 return ret;
2225 static BOOL WINAPI CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType,
2226 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2227 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2229 BOOL ret = FALSE;
2231 __TRY
2233 const CERT_NAME_VALUE *value = pvStructInfo;
2235 switch (value->dwValueType)
2237 case CERT_RDN_ANY_TYPE:
2238 case CERT_RDN_ENCODED_BLOB:
2239 case CERT_RDN_OCTET_STRING:
2240 SetLastError(CRYPT_E_NOT_CHAR_STRING);
2241 break;
2242 case CERT_RDN_NUMERIC_STRING:
2243 ret = CRYPT_AsnEncodeNumericString(value, dwFlags, pEncodePara,
2244 pbEncoded, pcbEncoded);
2245 break;
2246 case CERT_RDN_PRINTABLE_STRING:
2247 ret = CRYPT_AsnEncodePrintableString(value, dwFlags, pEncodePara,
2248 pbEncoded, pcbEncoded);
2249 break;
2250 case CERT_RDN_TELETEX_STRING:
2251 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_T61STRING,
2252 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2253 break;
2254 case CERT_RDN_VIDEOTEX_STRING:
2255 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value,
2256 ASN_VIDEOTEXSTRING, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2257 break;
2258 case CERT_RDN_IA5_STRING:
2259 ret = CRYPT_AsnEncodeIA5String(value, dwFlags, pEncodePara,
2260 pbEncoded, pcbEncoded);
2261 break;
2262 case CERT_RDN_GRAPHIC_STRING:
2263 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GRAPHICSTRING,
2264 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2265 break;
2266 case CERT_RDN_VISIBLE_STRING:
2267 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_VISIBLESTRING,
2268 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2269 break;
2270 case CERT_RDN_GENERAL_STRING:
2271 ret = CRYPT_AsnEncodeUnicodeStringCoerce(value, ASN_GENERALSTRING,
2272 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2273 break;
2274 case CERT_RDN_UNIVERSAL_STRING:
2275 ret = CRYPT_AsnEncodeUniversalString(value, dwFlags, pEncodePara,
2276 pbEncoded, pcbEncoded);
2277 break;
2278 case CERT_RDN_BMP_STRING:
2279 ret = CRYPT_AsnEncodeBMPString(value, dwFlags, pEncodePara,
2280 pbEncoded, pcbEncoded);
2281 break;
2282 case CERT_RDN_UTF8_STRING:
2283 ret = CRYPT_AsnEncodeUTF8String(value, dwFlags, pEncodePara,
2284 pbEncoded, pcbEncoded);
2285 break;
2286 default:
2287 SetLastError(CRYPT_E_ASN1_CHOICE);
2290 __EXCEPT_PAGE_FAULT
2292 SetLastError(STATUS_ACCESS_VIOLATION);
2294 __ENDTRY
2295 return ret;
2298 static BOOL WINAPI CRYPT_AsnEncodeName(DWORD dwCertEncodingType,
2299 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2300 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2302 BOOL ret;
2304 __TRY
2306 const CERT_NAME_INFO *info = pvStructInfo;
2307 DWORD bytesNeeded = 0, lenBytes, size, i;
2309 TRACE("encoding name with %d RDNs\n", info->cRDN);
2310 ret = TRUE;
2311 for (i = 0; ret && i < info->cRDN; i++)
2313 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType, &info->rgRDN[i],
2314 CRYPT_AsnEncodeNameValue, NULL, &size);
2315 if (ret)
2316 bytesNeeded += size;
2318 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
2319 bytesNeeded += 1 + lenBytes;
2320 if (ret)
2322 if (!pbEncoded)
2323 *pcbEncoded = bytesNeeded;
2324 else
2326 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2327 pbEncoded, pcbEncoded, bytesNeeded)))
2329 BYTE *out;
2331 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2332 pbEncoded = *(BYTE **)pbEncoded;
2333 out = pbEncoded;
2334 *out++ = ASN_SEQUENCEOF;
2335 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
2336 out += lenBytes;
2337 for (i = 0; ret && i < info->cRDN; i++)
2339 size = bytesNeeded;
2340 ret = CRYPT_AsnEncodeRdn(dwCertEncodingType,
2341 &info->rgRDN[i], CRYPT_AsnEncodeNameValue, out, &size);
2342 if (ret)
2344 out += size;
2345 bytesNeeded -= size;
2348 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2349 CRYPT_FreeSpace(pEncodePara, pbEncoded);
2354 __EXCEPT_PAGE_FAULT
2356 SetLastError(STATUS_ACCESS_VIOLATION);
2357 ret = FALSE;
2359 __ENDTRY
2360 return ret;
2363 static BOOL WINAPI CRYPT_AsnEncodeBool(DWORD dwCertEncodingType,
2364 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2365 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2367 BOOL val = *(const BOOL *)pvStructInfo, ret;
2369 TRACE("%d\n", val);
2371 if (!pbEncoded)
2373 *pcbEncoded = 3;
2374 ret = TRUE;
2376 else if (*pcbEncoded < 3)
2378 *pcbEncoded = 3;
2379 SetLastError(ERROR_MORE_DATA);
2380 ret = FALSE;
2382 else
2384 *pcbEncoded = 3;
2385 *pbEncoded++ = ASN_BOOL;
2386 *pbEncoded++ = 1;
2387 *pbEncoded++ = val ? 0xff : 0;
2388 ret = TRUE;
2390 TRACE("returning %d (%08x)\n", ret, GetLastError());
2391 return ret;
2394 static BOOL WINAPI CRYPT_AsnEncodeAltNameEntry(DWORD dwCertEncodingType,
2395 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2396 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2398 const CERT_ALT_NAME_ENTRY *entry = pvStructInfo;
2399 BOOL ret;
2400 DWORD dataLen;
2401 BYTE tag;
2403 ret = TRUE;
2404 switch (entry->dwAltNameChoice)
2406 case CERT_ALT_NAME_RFC822_NAME:
2407 case CERT_ALT_NAME_DNS_NAME:
2408 case CERT_ALT_NAME_URL:
2409 tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
2410 if (entry->u.pwszURL)
2412 DWORD i;
2414 /* Not + 1: don't encode the NULL-terminator */
2415 dataLen = lstrlenW(entry->u.pwszURL);
2416 for (i = 0; ret && i < dataLen; i++)
2418 if (entry->u.pwszURL[i] > 0x7f)
2420 SetLastError(CRYPT_E_INVALID_IA5_STRING);
2421 ret = FALSE;
2422 *pcbEncoded = i;
2426 else
2427 dataLen = 0;
2428 break;
2429 case CERT_ALT_NAME_DIRECTORY_NAME:
2430 tag = ASN_CONTEXT | ASN_CONSTRUCTOR | (entry->dwAltNameChoice - 1);
2431 dataLen = entry->u.DirectoryName.cbData;
2432 break;
2433 case CERT_ALT_NAME_IP_ADDRESS:
2434 tag = ASN_CONTEXT | (entry->dwAltNameChoice - 1);
2435 dataLen = entry->u.IPAddress.cbData;
2436 break;
2437 case CERT_ALT_NAME_REGISTERED_ID:
2439 struct AsnEncodeTagSwappedItem swapped =
2440 { ASN_CONTEXT | (entry->dwAltNameChoice - 1), entry->u.pszRegisteredID,
2441 CRYPT_AsnEncodeOid };
2443 return CRYPT_AsnEncodeSwapTag(0, NULL, &swapped, 0, NULL, pbEncoded,
2444 pcbEncoded);
2446 case CERT_ALT_NAME_OTHER_NAME:
2447 FIXME("name type %d unimplemented\n", entry->dwAltNameChoice);
2448 return FALSE;
2449 default:
2450 SetLastError(E_INVALIDARG);
2451 return FALSE;
2453 if (ret)
2455 DWORD bytesNeeded, lenBytes;
2457 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2458 bytesNeeded = 1 + dataLen + lenBytes;
2459 if (!pbEncoded)
2460 *pcbEncoded = bytesNeeded;
2461 else if (*pcbEncoded < bytesNeeded)
2463 SetLastError(ERROR_MORE_DATA);
2464 *pcbEncoded = bytesNeeded;
2465 ret = FALSE;
2467 else
2469 *pbEncoded++ = tag;
2470 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
2471 pbEncoded += lenBytes;
2472 switch (entry->dwAltNameChoice)
2474 case CERT_ALT_NAME_RFC822_NAME:
2475 case CERT_ALT_NAME_DNS_NAME:
2476 case CERT_ALT_NAME_URL:
2478 DWORD i;
2480 for (i = 0; i < dataLen; i++)
2481 *pbEncoded++ = (BYTE)entry->u.pwszURL[i];
2482 break;
2484 case CERT_ALT_NAME_DIRECTORY_NAME:
2485 memcpy(pbEncoded, entry->u.DirectoryName.pbData, dataLen);
2486 break;
2487 case CERT_ALT_NAME_IP_ADDRESS:
2488 memcpy(pbEncoded, entry->u.IPAddress.pbData, dataLen);
2489 break;
2491 if (ret)
2492 *pcbEncoded = bytesNeeded;
2495 TRACE("returning %d (%08x)\n", ret, GetLastError());
2496 return ret;
2499 static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId(DWORD dwCertEncodingType,
2500 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2501 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2503 BOOL ret;
2505 __TRY
2507 const CERT_AUTHORITY_KEY_ID_INFO *info = pvStructInfo;
2508 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2509 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2510 struct AsnConstructedItem constructed = { 0 };
2511 DWORD cItem = 0, cSwapped = 0;
2513 if (info->KeyId.cbData)
2515 swapped[cSwapped].tag = ASN_CONTEXT | 0;
2516 swapped[cSwapped].pvStructInfo = &info->KeyId;
2517 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeOctets;
2518 items[cItem].pvStructInfo = &swapped[cSwapped];
2519 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2520 cSwapped++;
2521 cItem++;
2523 if (info->CertIssuer.cbData)
2525 constructed.tag = 1;
2526 constructed.pvStructInfo = &info->CertIssuer;
2527 constructed.encodeFunc = CRYPT_CopyEncodedBlob;
2528 items[cItem].pvStructInfo = &constructed;
2529 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
2530 cItem++;
2532 if (info->CertSerialNumber.cbData)
2534 swapped[cSwapped].tag = ASN_CONTEXT | 2;
2535 swapped[cSwapped].pvStructInfo = &info->CertSerialNumber;
2536 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
2537 items[cItem].pvStructInfo = &swapped[cSwapped];
2538 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2539 cSwapped++;
2540 cItem++;
2542 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
2543 pEncodePara, pbEncoded, pcbEncoded);
2545 __EXCEPT_PAGE_FAULT
2547 SetLastError(STATUS_ACCESS_VIOLATION);
2548 ret = FALSE;
2550 __ENDTRY
2551 return ret;
2554 static BOOL WINAPI CRYPT_AsnEncodeAltName(DWORD dwCertEncodingType,
2555 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2556 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2558 BOOL ret;
2560 __TRY
2562 const CERT_ALT_NAME_INFO *info = pvStructInfo;
2563 DWORD bytesNeeded, dataLen, lenBytes, i;
2565 ret = TRUE;
2566 /* FIXME: should check that cAltEntry is not bigger than 0xff, since we
2567 * can't encode an erroneous entry index if it's bigger than this.
2569 for (i = 0, dataLen = 0; ret && i < info->cAltEntry; i++)
2571 DWORD len;
2573 ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType, NULL,
2574 &info->rgAltEntry[i], 0, NULL, NULL, &len);
2575 if (ret)
2576 dataLen += len;
2577 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
2579 /* CRYPT_AsnEncodeAltNameEntry encoded the index of
2580 * the bad character, now set the index of the bad
2581 * entry
2583 *pcbEncoded = (BYTE)i <<
2584 CERT_ALT_NAME_ENTRY_ERR_INDEX_SHIFT | len;
2587 if (ret)
2589 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2590 bytesNeeded = 1 + lenBytes + dataLen;
2591 if (!pbEncoded)
2593 *pcbEncoded = bytesNeeded;
2594 ret = TRUE;
2596 else
2598 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2599 pbEncoded, pcbEncoded, bytesNeeded)))
2601 BYTE *out;
2603 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2604 pbEncoded = *(BYTE **)pbEncoded;
2605 out = pbEncoded;
2606 *out++ = ASN_SEQUENCEOF;
2607 CRYPT_EncodeLen(dataLen, out, &lenBytes);
2608 out += lenBytes;
2609 for (i = 0; ret && i < info->cAltEntry; i++)
2611 DWORD len = dataLen;
2613 ret = CRYPT_AsnEncodeAltNameEntry(dwCertEncodingType,
2614 NULL, &info->rgAltEntry[i], 0, NULL, out, &len);
2615 if (ret)
2617 out += len;
2618 dataLen -= len;
2621 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2622 CRYPT_FreeSpace(pEncodePara, pbEncoded);
2627 __EXCEPT_PAGE_FAULT
2629 SetLastError(STATUS_ACCESS_VIOLATION);
2630 ret = FALSE;
2632 __ENDTRY
2633 return ret;
2636 static BOOL WINAPI CRYPT_AsnEncodeAuthorityKeyId2(DWORD dwCertEncodingType,
2637 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2638 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2640 BOOL ret;
2642 __TRY
2644 const CERT_AUTHORITY_KEY_ID2_INFO *info = pvStructInfo;
2645 struct AsnEncodeSequenceItem items[3] = { { 0 } };
2646 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
2647 DWORD cItem = 0, cSwapped = 0;
2649 if (info->KeyId.cbData)
2651 swapped[cSwapped].tag = ASN_CONTEXT | 0;
2652 swapped[cSwapped].pvStructInfo = &info->KeyId;
2653 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeOctets;
2654 items[cItem].pvStructInfo = &swapped[cSwapped];
2655 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2656 cSwapped++;
2657 cItem++;
2659 if (info->AuthorityCertIssuer.cAltEntry)
2661 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
2662 swapped[cSwapped].pvStructInfo = &info->AuthorityCertIssuer;
2663 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
2664 items[cItem].pvStructInfo = &swapped[cSwapped];
2665 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2666 cSwapped++;
2667 cItem++;
2669 if (info->AuthorityCertSerialNumber.cbData)
2671 swapped[cSwapped].tag = ASN_CONTEXT | 2;
2672 swapped[cSwapped].pvStructInfo = &info->AuthorityCertSerialNumber;
2673 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInteger;
2674 items[cItem].pvStructInfo = &swapped[cSwapped];
2675 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
2676 cSwapped++;
2677 cItem++;
2679 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, dwFlags,
2680 pEncodePara, pbEncoded, pcbEncoded);
2682 __EXCEPT_PAGE_FAULT
2684 SetLastError(STATUS_ACCESS_VIOLATION);
2685 ret = FALSE;
2687 __ENDTRY
2688 return ret;
2691 static BOOL CRYPT_AsnEncodeAccessDescription(
2692 const CERT_ACCESS_DESCRIPTION *descr, BYTE *pbEncoded, DWORD *pcbEncoded)
2694 struct AsnEncodeSequenceItem items[] = {
2695 { descr->pszAccessMethod, CRYPT_AsnEncodeOid, 0 },
2696 { &descr->AccessLocation, CRYPT_AsnEncodeAltNameEntry, 0 },
2699 if (!descr->pszAccessMethod)
2701 SetLastError(E_INVALIDARG);
2702 return FALSE;
2704 return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, ARRAY_SIZE(items), 0, NULL, pbEncoded, pcbEncoded);
2707 static BOOL WINAPI CRYPT_AsnEncodeAuthorityInfoAccess(DWORD dwCertEncodingType,
2708 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2709 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2711 BOOL ret;
2713 __TRY
2715 DWORD bytesNeeded, dataLen, lenBytes, i;
2716 const CERT_AUTHORITY_INFO_ACCESS *info = pvStructInfo;
2718 ret = TRUE;
2719 for (i = 0, dataLen = 0; ret && i < info->cAccDescr; i++)
2721 DWORD size;
2723 ret = CRYPT_AsnEncodeAccessDescription(&info->rgAccDescr[i], NULL,
2724 &size);
2725 if (ret)
2726 dataLen += size;
2728 if (ret)
2730 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
2731 bytesNeeded = 1 + lenBytes + dataLen;
2732 if (!pbEncoded)
2733 *pcbEncoded = bytesNeeded;
2734 else
2736 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2737 pbEncoded, pcbEncoded, bytesNeeded)))
2739 BYTE *out;
2741 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2742 pbEncoded = *(BYTE **)pbEncoded;
2743 out = pbEncoded;
2744 *out++ = ASN_SEQUENCEOF;
2745 CRYPT_EncodeLen(dataLen, out, &lenBytes);
2746 out += lenBytes;
2747 for (i = 0; i < info->cAccDescr; i++)
2749 DWORD size = dataLen;
2751 ret = CRYPT_AsnEncodeAccessDescription(
2752 &info->rgAccDescr[i], out, &size);
2753 out += size;
2754 dataLen -= size;
2756 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2757 CRYPT_FreeSpace(pEncodePara, pbEncoded);
2762 __EXCEPT_PAGE_FAULT
2764 SetLastError(STATUS_ACCESS_VIOLATION);
2765 ret = FALSE;
2767 __ENDTRY
2768 return ret;
2771 static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints(DWORD dwCertEncodingType,
2772 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2773 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2775 BOOL ret;
2777 __TRY
2779 const CERT_BASIC_CONSTRAINTS_INFO *info = pvStructInfo;
2780 struct AsnEncodeSequenceItem items[3] = {
2781 { &info->SubjectType, CRYPT_AsnEncodeBits, 0 },
2782 { 0 }
2784 DWORD cItem = 1;
2786 if (info->fPathLenConstraint)
2788 items[cItem].pvStructInfo = &info->dwPathLenConstraint;
2789 items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
2790 cItem++;
2792 if (info->cSubtreesConstraint)
2794 items[cItem].pvStructInfo = &info->cSubtreesConstraint;
2795 items[cItem].encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
2796 cItem++;
2798 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
2799 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2801 __EXCEPT_PAGE_FAULT
2803 SetLastError(STATUS_ACCESS_VIOLATION);
2804 ret = FALSE;
2806 __ENDTRY
2807 return ret;
2810 static BOOL WINAPI CRYPT_AsnEncodeBasicConstraints2(DWORD dwCertEncodingType,
2811 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2812 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2814 BOOL ret;
2816 __TRY
2818 const CERT_BASIC_CONSTRAINTS2_INFO *info = pvStructInfo;
2819 struct AsnEncodeSequenceItem items[2] = { { 0 } };
2820 DWORD cItem = 0;
2822 if (info->fCA)
2824 items[cItem].pvStructInfo = &info->fCA;
2825 items[cItem].encodeFunc = CRYPT_AsnEncodeBool;
2826 cItem++;
2828 if (info->fPathLenConstraint)
2830 items[cItem].pvStructInfo = &info->dwPathLenConstraint;
2831 items[cItem].encodeFunc = CRYPT_AsnEncodeInt;
2832 cItem++;
2834 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
2835 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
2837 __EXCEPT_PAGE_FAULT
2839 SetLastError(STATUS_ACCESS_VIOLATION);
2840 ret = FALSE;
2842 __ENDTRY
2843 return ret;
2846 static BOOL WINAPI CRYPT_AsnEncodeCertPolicyQualifiers(DWORD dwCertEncodingType,
2847 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2848 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2850 const CERT_POLICY_INFO *info = pvStructInfo;
2851 BOOL ret;
2853 if (!info->cPolicyQualifier)
2855 *pcbEncoded = 0;
2856 ret = TRUE;
2858 else
2860 struct AsnEncodeSequenceItem items[2] = {
2861 { NULL, CRYPT_AsnEncodeOid, 0 },
2862 { NULL, CRYPT_CopyEncodedBlob, 0 },
2864 DWORD bytesNeeded = 0, lenBytes, size, i;
2866 ret = TRUE;
2867 for (i = 0; ret && i < info->cPolicyQualifier; i++)
2869 items[0].pvStructInfo =
2870 info->rgPolicyQualifier[i].pszPolicyQualifierId;
2871 items[1].pvStructInfo = &info->rgPolicyQualifier[i].Qualifier;
2872 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items),
2873 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &size);
2874 if (ret)
2875 bytesNeeded += size;
2877 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
2878 bytesNeeded += 1 + lenBytes;
2879 if (ret)
2881 if (!pbEncoded)
2882 *pcbEncoded = bytesNeeded;
2883 else
2885 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2886 pbEncoded, pcbEncoded, bytesNeeded)))
2888 BYTE *out;
2890 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2891 pbEncoded = *(BYTE **)pbEncoded;
2892 out = pbEncoded;
2893 *out++ = ASN_SEQUENCEOF;
2894 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
2895 out += lenBytes;
2896 for (i = 0; ret && i < info->cPolicyQualifier; i++)
2898 items[0].pvStructInfo =
2899 info->rgPolicyQualifier[i].pszPolicyQualifierId;
2900 items[1].pvStructInfo =
2901 &info->rgPolicyQualifier[i].Qualifier;
2902 size = bytesNeeded;
2903 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items),
2904 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, out, &size);
2905 if (ret)
2907 out += size;
2908 bytesNeeded -= size;
2911 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2912 CRYPT_FreeSpace(pEncodePara, pbEncoded);
2917 return ret;
2920 static BOOL CRYPT_AsnEncodeCertPolicy(DWORD dwCertEncodingType,
2921 const CERT_POLICY_INFO *info, DWORD dwFlags, BYTE *pbEncoded,
2922 DWORD *pcbEncoded)
2924 struct AsnEncodeSequenceItem items[2] = {
2925 { info->pszPolicyIdentifier, CRYPT_AsnEncodeOid, 0 },
2926 { info, CRYPT_AsnEncodeCertPolicyQualifiers, 0 },
2928 BOOL ret;
2930 if (!info->pszPolicyIdentifier)
2932 SetLastError(E_INVALIDARG);
2933 return FALSE;
2935 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items), dwFlags, NULL, pbEncoded, pcbEncoded);
2936 return ret;
2939 static BOOL WINAPI CRYPT_AsnEncodeCertPolicies(DWORD dwCertEncodingType,
2940 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
2941 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
2943 BOOL ret = FALSE;
2945 __TRY
2947 const CERT_POLICIES_INFO *info = pvStructInfo;
2948 DWORD bytesNeeded = 0, lenBytes, size, i;
2950 ret = TRUE;
2951 for (i = 0; ret && i < info->cPolicyInfo; i++)
2953 ret = CRYPT_AsnEncodeCertPolicy(dwCertEncodingType,
2954 &info->rgPolicyInfo[i], dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL,
2955 &size);
2956 if (ret)
2957 bytesNeeded += size;
2959 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
2960 bytesNeeded += 1 + lenBytes;
2961 if (ret)
2963 if (!pbEncoded)
2964 *pcbEncoded = bytesNeeded;
2965 else
2967 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
2968 pbEncoded, pcbEncoded, bytesNeeded)))
2970 BYTE *out;
2972 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
2973 pbEncoded = *(BYTE **)pbEncoded;
2974 out = pbEncoded;
2975 *out++ = ASN_SEQUENCEOF;
2976 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
2977 out += lenBytes;
2978 for (i = 0; ret && i < info->cPolicyInfo; i++)
2980 size = bytesNeeded;
2981 ret = CRYPT_AsnEncodeCertPolicy(dwCertEncodingType,
2982 &info->rgPolicyInfo[i],
2983 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, out, &size);
2984 if (ret)
2986 out += size;
2987 bytesNeeded -= size;
2990 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
2991 CRYPT_FreeSpace(pEncodePara, pbEncoded);
2996 __EXCEPT_PAGE_FAULT
2998 SetLastError(STATUS_ACCESS_VIOLATION);
3000 __ENDTRY
3001 return ret;
3004 static BOOL CRYPT_AsnEncodeCertPolicyMapping(DWORD dwCertEncodingType,
3005 const CERT_POLICY_MAPPING *mapping, DWORD dwFlags, BYTE *pbEncoded,
3006 DWORD *pcbEncoded)
3008 struct AsnEncodeSequenceItem items[] = {
3009 { mapping->pszIssuerDomainPolicy, CRYPT_AsnEncodeOid, 0 },
3010 { mapping->pszSubjectDomainPolicy, CRYPT_AsnEncodeOid, 0 },
3013 if (!mapping->pszIssuerDomainPolicy || !mapping->pszSubjectDomainPolicy)
3015 SetLastError(E_INVALIDARG);
3016 return FALSE;
3018 return CRYPT_AsnEncodeSequence(dwCertEncodingType, items, ARRAY_SIZE(items), dwFlags, NULL, pbEncoded, pcbEncoded);
3021 static BOOL WINAPI CRYPT_AsnEncodeCertPolicyMappings(DWORD dwCertEncodingType,
3022 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3023 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3025 BOOL ret = FALSE;
3027 __TRY
3029 const CERT_POLICY_MAPPINGS_INFO *info = pvStructInfo;
3030 DWORD bytesNeeded = 0, lenBytes, size, i;
3032 ret = TRUE;
3033 for (i = 0; ret && i < info->cPolicyMapping; i++)
3035 ret = CRYPT_AsnEncodeCertPolicyMapping(dwCertEncodingType,
3036 &info->rgPolicyMapping[i], dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG,
3037 NULL, &size);
3038 if (ret)
3039 bytesNeeded += size;
3041 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
3042 bytesNeeded += 1 + lenBytes;
3043 if (ret)
3045 if (!pbEncoded)
3046 *pcbEncoded = bytesNeeded;
3047 else
3049 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3050 pbEncoded, pcbEncoded, bytesNeeded)))
3052 BYTE *out;
3054 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3055 pbEncoded = *(BYTE **)pbEncoded;
3056 out = pbEncoded;
3057 *out++ = ASN_SEQUENCEOF;
3058 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
3059 out += lenBytes;
3060 for (i = 0; ret && i < info->cPolicyMapping; i++)
3062 size = bytesNeeded;
3063 ret = CRYPT_AsnEncodeCertPolicyMapping(
3064 dwCertEncodingType, &info->rgPolicyMapping[i],
3065 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, out, &size);
3066 if (ret)
3068 out += size;
3069 bytesNeeded -= size;
3072 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
3073 CRYPT_FreeSpace(pEncodePara, pbEncoded);
3078 __EXCEPT_PAGE_FAULT
3080 SetLastError(STATUS_ACCESS_VIOLATION);
3082 __ENDTRY
3083 return ret;
3086 static BOOL WINAPI CRYPT_AsnEncodeCertPolicyConstraints(
3087 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
3088 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
3089 DWORD *pcbEncoded)
3091 BOOL ret = FALSE;
3093 __TRY
3095 const CERT_POLICY_CONSTRAINTS_INFO *info = pvStructInfo;
3096 struct AsnEncodeSequenceItem items[2];
3097 struct AsnEncodeTagSwappedItem swapped[2];
3098 DWORD cItem = 0, cSwapped = 0;
3100 if (info->fRequireExplicitPolicy)
3102 swapped[cSwapped].tag = ASN_CONTEXT | 0;
3103 swapped[cSwapped].pvStructInfo =
3104 &info->dwRequireExplicitPolicySkipCerts;
3105 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
3106 items[cItem].pvStructInfo = &swapped[cSwapped];
3107 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3108 cSwapped++;
3109 cItem++;
3111 if (info->fInhibitPolicyMapping)
3113 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3114 swapped[cSwapped].pvStructInfo =
3115 &info->dwInhibitPolicyMappingSkipCerts;
3116 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
3117 items[cItem].pvStructInfo = &swapped[cSwapped];
3118 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3119 cSwapped++;
3120 cItem++;
3122 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
3123 dwFlags, NULL, pbEncoded, pcbEncoded);
3125 __EXCEPT_PAGE_FAULT
3127 SetLastError(STATUS_ACCESS_VIOLATION);
3129 __ENDTRY
3130 return ret;
3133 static BOOL WINAPI CRYPT_AsnEncodeRsaPubKey(DWORD dwCertEncodingType,
3134 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3135 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3137 BOOL ret;
3139 __TRY
3141 const BLOBHEADER *hdr = pvStructInfo;
3143 if (hdr->bType != PUBLICKEYBLOB)
3145 SetLastError(E_INVALIDARG);
3146 ret = FALSE;
3148 else
3150 const RSAPUBKEY *rsaPubKey = (const RSAPUBKEY *)
3151 ((const BYTE *)pvStructInfo + sizeof(BLOBHEADER));
3152 CRYPT_INTEGER_BLOB blob = { rsaPubKey->bitlen / 8,
3153 (BYTE *)pvStructInfo + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) };
3154 struct AsnEncodeSequenceItem items[] = {
3155 { &blob, CRYPT_AsnEncodeUnsignedInteger, 0 },
3156 { &rsaPubKey->pubexp, CRYPT_AsnEncodeInt, 0 },
3159 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
3160 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3163 __EXCEPT_PAGE_FAULT
3165 SetLastError(STATUS_ACCESS_VIOLATION);
3166 ret = FALSE;
3168 __ENDTRY
3169 return ret;
3172 BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
3173 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3174 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3176 BOOL ret;
3178 __TRY
3180 const CRYPT_DATA_BLOB *blob = pvStructInfo;
3181 DWORD bytesNeeded, lenBytes;
3183 TRACE("(%d, %p), %08x, %p, %p, %d\n", blob->cbData, blob->pbData,
3184 dwFlags, pEncodePara, pbEncoded, pbEncoded ? *pcbEncoded : 0);
3186 CRYPT_EncodeLen(blob->cbData, NULL, &lenBytes);
3187 bytesNeeded = 1 + lenBytes + blob->cbData;
3188 if (!pbEncoded)
3190 *pcbEncoded = bytesNeeded;
3191 ret = TRUE;
3193 else
3195 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3196 pcbEncoded, bytesNeeded)))
3198 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3199 pbEncoded = *(BYTE **)pbEncoded;
3200 *pbEncoded++ = ASN_OCTETSTRING;
3201 CRYPT_EncodeLen(blob->cbData, pbEncoded, &lenBytes);
3202 pbEncoded += lenBytes;
3203 if (blob->cbData)
3204 memcpy(pbEncoded, blob->pbData, blob->cbData);
3208 __EXCEPT_PAGE_FAULT
3210 SetLastError(STATUS_ACCESS_VIOLATION);
3211 ret = FALSE;
3213 __ENDTRY
3214 TRACE("returning %d (%08x)\n", ret, GetLastError());
3215 return ret;
3218 static BOOL WINAPI CRYPT_AsnEncodeBits(DWORD dwCertEncodingType,
3219 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3220 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3222 BOOL ret;
3224 __TRY
3226 const CRYPT_BIT_BLOB *blob = pvStructInfo;
3227 DWORD bytesNeeded, lenBytes, dataBytes;
3228 BYTE unusedBits;
3230 /* yep, MS allows cUnusedBits to be >= 8 */
3231 if (!blob->cUnusedBits)
3233 dataBytes = blob->cbData;
3234 unusedBits = 0;
3236 else if (blob->cbData * 8 > blob->cUnusedBits)
3238 dataBytes = (blob->cbData * 8 - blob->cUnusedBits) / 8 + 1;
3239 unusedBits = blob->cUnusedBits >= 8 ? blob->cUnusedBits / 8 :
3240 blob->cUnusedBits;
3242 else
3244 dataBytes = 0;
3245 unusedBits = 0;
3247 CRYPT_EncodeLen(dataBytes + 1, NULL, &lenBytes);
3248 bytesNeeded = 1 + lenBytes + dataBytes + 1;
3249 if (!pbEncoded)
3251 *pcbEncoded = bytesNeeded;
3252 ret = TRUE;
3254 else
3256 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3257 pcbEncoded, bytesNeeded)))
3259 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3260 pbEncoded = *(BYTE **)pbEncoded;
3261 *pbEncoded++ = ASN_BITSTRING;
3262 CRYPT_EncodeLen(dataBytes + 1, pbEncoded, &lenBytes);
3263 pbEncoded += lenBytes;
3264 *pbEncoded++ = unusedBits;
3265 if (dataBytes)
3267 BYTE mask = 0xff << unusedBits;
3269 if (dataBytes > 1)
3271 memcpy(pbEncoded, blob->pbData, dataBytes - 1);
3272 pbEncoded += dataBytes - 1;
3274 *pbEncoded = *(blob->pbData + dataBytes - 1) & mask;
3279 __EXCEPT_PAGE_FAULT
3281 SetLastError(STATUS_ACCESS_VIOLATION);
3282 ret = FALSE;
3284 __ENDTRY
3285 return ret;
3288 static BOOL WINAPI CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType,
3289 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3290 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3292 BOOL ret;
3294 __TRY
3296 const CRYPT_BIT_BLOB *blob = pvStructInfo;
3297 CRYPT_BIT_BLOB newBlob = { blob->cbData, NULL, blob->cUnusedBits };
3299 ret = TRUE;
3300 if (newBlob.cbData)
3302 newBlob.pbData = CryptMemAlloc(newBlob.cbData);
3303 if (newBlob.pbData)
3305 DWORD i;
3307 for (i = 0; i < newBlob.cbData; i++)
3308 newBlob.pbData[newBlob.cbData - i - 1] = blob->pbData[i];
3310 else
3311 ret = FALSE;
3313 if (ret)
3314 ret = CRYPT_AsnEncodeBits(dwCertEncodingType, lpszStructType,
3315 &newBlob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3316 CryptMemFree(newBlob.pbData);
3318 __EXCEPT_PAGE_FAULT
3320 SetLastError(STATUS_ACCESS_VIOLATION);
3321 ret = FALSE;
3323 __ENDTRY
3324 return ret;
3327 static BOOL WINAPI CRYPT_AsnEncodeInt(DWORD dwCertEncodingType,
3328 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3329 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3331 CRYPT_INTEGER_BLOB blob = { sizeof(INT), (BYTE *)pvStructInfo };
3333 return CRYPT_AsnEncodeInteger(dwCertEncodingType, X509_MULTI_BYTE_INTEGER,
3334 &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3337 static BOOL WINAPI CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType,
3338 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3339 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3341 BOOL ret;
3343 __TRY
3345 DWORD significantBytes, lenBytes, bytesNeeded;
3346 BYTE padByte = 0;
3347 BOOL pad = FALSE;
3348 const CRYPT_INTEGER_BLOB *blob = pvStructInfo;
3350 significantBytes = blob->cbData;
3351 if (significantBytes)
3353 if (blob->pbData[significantBytes - 1] & 0x80)
3355 /* negative, lop off leading (little-endian) 0xffs */
3356 for (; significantBytes > 0 &&
3357 blob->pbData[significantBytes - 1] == 0xff; significantBytes--)
3359 if (blob->pbData[significantBytes - 1] < 0x80)
3361 padByte = 0xff;
3362 pad = TRUE;
3365 else
3367 /* positive, lop off leading (little-endian) zeroes */
3368 for (; significantBytes > 0 &&
3369 !blob->pbData[significantBytes - 1]; significantBytes--)
3371 if (significantBytes == 0)
3372 significantBytes = 1;
3373 if (blob->pbData[significantBytes - 1] > 0x7f)
3375 padByte = 0;
3376 pad = TRUE;
3380 if (pad)
3381 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
3382 else
3383 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
3384 bytesNeeded = 1 + lenBytes + significantBytes;
3385 if (pad)
3386 bytesNeeded++;
3387 if (!pbEncoded)
3389 *pcbEncoded = bytesNeeded;
3390 ret = TRUE;
3392 else
3394 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3395 pcbEncoded, bytesNeeded)))
3397 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3398 pbEncoded = *(BYTE **)pbEncoded;
3399 *pbEncoded++ = ASN_INTEGER;
3400 if (pad)
3402 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
3403 pbEncoded += lenBytes;
3404 *pbEncoded++ = padByte;
3406 else
3408 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
3409 pbEncoded += lenBytes;
3411 for (; significantBytes > 0; significantBytes--)
3412 *(pbEncoded++) = blob->pbData[significantBytes - 1];
3416 __EXCEPT_PAGE_FAULT
3418 SetLastError(STATUS_ACCESS_VIOLATION);
3419 ret = FALSE;
3421 __ENDTRY
3422 return ret;
3425 static BOOL WINAPI CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType,
3426 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3427 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3429 BOOL ret;
3431 __TRY
3433 DWORD significantBytes, lenBytes, bytesNeeded;
3434 BOOL pad = FALSE;
3435 const CRYPT_INTEGER_BLOB *blob = pvStructInfo;
3437 significantBytes = blob->cbData;
3438 if (significantBytes)
3440 /* positive, lop off leading (little-endian) zeroes */
3441 for (; significantBytes > 0 && !blob->pbData[significantBytes - 1];
3442 significantBytes--)
3444 if (significantBytes == 0)
3445 significantBytes = 1;
3446 if (blob->pbData[significantBytes - 1] > 0x7f)
3447 pad = TRUE;
3449 if (pad)
3450 CRYPT_EncodeLen(significantBytes + 1, NULL, &lenBytes);
3451 else
3452 CRYPT_EncodeLen(significantBytes, NULL, &lenBytes);
3453 bytesNeeded = 1 + lenBytes + significantBytes;
3454 if (pad)
3455 bytesNeeded++;
3456 if (!pbEncoded)
3458 *pcbEncoded = bytesNeeded;
3459 ret = TRUE;
3461 else
3463 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3464 pcbEncoded, bytesNeeded)))
3466 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3467 pbEncoded = *(BYTE **)pbEncoded;
3468 *pbEncoded++ = ASN_INTEGER;
3469 if (pad)
3471 CRYPT_EncodeLen(significantBytes + 1, pbEncoded, &lenBytes);
3472 pbEncoded += lenBytes;
3473 *pbEncoded++ = 0;
3475 else
3477 CRYPT_EncodeLen(significantBytes, pbEncoded, &lenBytes);
3478 pbEncoded += lenBytes;
3480 for (; significantBytes > 0; significantBytes--)
3481 *(pbEncoded++) = blob->pbData[significantBytes - 1];
3485 __EXCEPT_PAGE_FAULT
3487 SetLastError(STATUS_ACCESS_VIOLATION);
3488 ret = FALSE;
3490 __ENDTRY
3491 return ret;
3494 static BOOL WINAPI CRYPT_AsnEncodeEnumerated(DWORD dwCertEncodingType,
3495 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3496 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3498 CRYPT_INTEGER_BLOB blob;
3499 BOOL ret;
3501 /* Encode as an unsigned integer, then change the tag to enumerated */
3502 blob.cbData = sizeof(DWORD);
3503 blob.pbData = (BYTE *)pvStructInfo;
3504 ret = CRYPT_AsnEncodeUnsignedInteger(dwCertEncodingType,
3505 X509_MULTI_BYTE_UINT, &blob, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3506 if (ret && pbEncoded)
3508 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3509 pbEncoded = *(BYTE **)pbEncoded;
3510 pbEncoded[0] = ASN_ENUMERATED;
3512 return ret;
3515 static BOOL WINAPI CRYPT_AsnEncodeUtcTime(DWORD dwCertEncodingType,
3516 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3517 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3519 BOOL ret;
3521 __TRY
3523 SYSTEMTIME sysTime;
3524 /* sorry, magic number: enough for tag, len, YYMMDDHHMMSSZ. I use a
3525 * temporary buffer because the output buffer is not NULL-terminated.
3527 static const DWORD bytesNeeded = 15;
3528 char buf[40];
3530 if (!pbEncoded)
3532 *pcbEncoded = bytesNeeded;
3533 ret = TRUE;
3535 else
3537 /* Sanity check the year, this is a two-digit year format */
3538 ret = FileTimeToSystemTime(pvStructInfo, &sysTime);
3539 if (ret && (sysTime.wYear < 1950 || sysTime.wYear > 2050))
3541 SetLastError(CRYPT_E_BAD_ENCODE);
3542 ret = FALSE;
3544 if (ret)
3546 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3547 pbEncoded, pcbEncoded, bytesNeeded)))
3549 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3550 pbEncoded = *(BYTE **)pbEncoded;
3551 buf[0] = ASN_UTCTIME;
3552 buf[1] = bytesNeeded - 2;
3553 snprintf(buf + 2, sizeof(buf) - 2,
3554 "%02d%02d%02d%02d%02d%02dZ", sysTime.wYear >= 2000 ?
3555 sysTime.wYear - 2000 : sysTime.wYear - 1900,
3556 sysTime.wMonth, sysTime.wDay, sysTime.wHour,
3557 sysTime.wMinute, sysTime.wSecond);
3558 memcpy(pbEncoded, buf, bytesNeeded);
3563 __EXCEPT_PAGE_FAULT
3565 SetLastError(STATUS_ACCESS_VIOLATION);
3566 ret = FALSE;
3568 __ENDTRY
3569 return ret;
3572 static BOOL CRYPT_AsnEncodeGeneralizedTime(DWORD dwCertEncodingType,
3573 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3574 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3576 BOOL ret;
3578 __TRY
3580 SYSTEMTIME sysTime;
3581 /* sorry, magic number: enough for tag, len, YYYYMMDDHHMMSSZ. I use a
3582 * temporary buffer because the output buffer is not NULL-terminated.
3584 static const DWORD bytesNeeded = 17;
3585 char buf[40];
3587 if (!pbEncoded)
3589 *pcbEncoded = bytesNeeded;
3590 ret = TRUE;
3592 else
3594 ret = FileTimeToSystemTime(pvStructInfo, &sysTime);
3595 if (ret)
3596 ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3597 pcbEncoded, bytesNeeded);
3598 if (ret)
3600 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3601 pbEncoded = *(BYTE **)pbEncoded;
3602 buf[0] = ASN_GENERALTIME;
3603 buf[1] = bytesNeeded - 2;
3604 snprintf(buf + 2, sizeof(buf) - 2, "%04d%02d%02d%02d%02d%02dZ",
3605 sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour,
3606 sysTime.wMinute, sysTime.wSecond);
3607 memcpy(pbEncoded, buf, bytesNeeded);
3611 __EXCEPT_PAGE_FAULT
3613 SetLastError(STATUS_ACCESS_VIOLATION);
3614 ret = FALSE;
3616 __ENDTRY
3617 return ret;
3620 static BOOL WINAPI CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType,
3621 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3622 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3624 BOOL ret;
3626 __TRY
3628 SYSTEMTIME sysTime;
3630 /* Check the year, if it's in the UTCTime range call that encode func */
3631 if (!FileTimeToSystemTime(pvStructInfo, &sysTime))
3632 return FALSE;
3633 if (sysTime.wYear >= 1950 && sysTime.wYear <= 2050)
3634 ret = CRYPT_AsnEncodeUtcTime(dwCertEncodingType, lpszStructType,
3635 pvStructInfo, dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3636 else
3637 ret = CRYPT_AsnEncodeGeneralizedTime(dwCertEncodingType,
3638 lpszStructType, pvStructInfo, dwFlags, pEncodePara, pbEncoded,
3639 pcbEncoded);
3641 __EXCEPT_PAGE_FAULT
3643 SetLastError(STATUS_ACCESS_VIOLATION);
3644 ret = FALSE;
3646 __ENDTRY
3647 return ret;
3650 static BOOL WINAPI CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType,
3651 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3652 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3654 BOOL ret;
3656 __TRY
3658 DWORD bytesNeeded, dataLen, lenBytes, i;
3659 const CRYPT_SEQUENCE_OF_ANY *seq = pvStructInfo;
3661 for (i = 0, dataLen = 0; i < seq->cValue; i++)
3662 dataLen += seq->rgValue[i].cbData;
3663 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
3664 bytesNeeded = 1 + lenBytes + dataLen;
3665 if (!pbEncoded)
3667 *pcbEncoded = bytesNeeded;
3668 ret = TRUE;
3670 else
3672 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara, pbEncoded,
3673 pcbEncoded, bytesNeeded)))
3675 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3676 pbEncoded = *(BYTE **)pbEncoded;
3677 *pbEncoded++ = ASN_SEQUENCEOF;
3678 CRYPT_EncodeLen(dataLen, pbEncoded, &lenBytes);
3679 pbEncoded += lenBytes;
3680 for (i = 0; i < seq->cValue; i++)
3682 memcpy(pbEncoded, seq->rgValue[i].pbData,
3683 seq->rgValue[i].cbData);
3684 pbEncoded += seq->rgValue[i].cbData;
3689 __EXCEPT_PAGE_FAULT
3691 SetLastError(STATUS_ACCESS_VIOLATION);
3692 ret = FALSE;
3694 __ENDTRY
3695 return ret;
3698 static BOOL CRYPT_AsnEncodeDistPoint(const CRL_DIST_POINT *distPoint,
3699 BYTE *pbEncoded, DWORD *pcbEncoded)
3701 BOOL ret = TRUE;
3702 struct AsnEncodeSequenceItem items[3] = { { 0 } };
3703 struct AsnConstructedItem constructed = { 0 };
3704 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
3705 DWORD cItem = 0, cSwapped = 0;
3707 switch (distPoint->DistPointName.dwDistPointNameChoice)
3709 case CRL_DIST_POINT_NO_NAME:
3710 /* do nothing */
3711 break;
3712 case CRL_DIST_POINT_FULL_NAME:
3713 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3714 swapped[cSwapped].pvStructInfo = &distPoint->DistPointName.u.FullName;
3715 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
3716 constructed.tag = 0;
3717 constructed.pvStructInfo = &swapped[cSwapped];
3718 constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
3719 items[cItem].pvStructInfo = &constructed;
3720 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
3721 cSwapped++;
3722 cItem++;
3723 break;
3724 case CRL_DIST_POINT_ISSUER_RDN_NAME:
3725 FIXME("unimplemented for CRL_DIST_POINT_ISSUER_RDN_NAME\n");
3726 ret = FALSE;
3727 break;
3728 default:
3729 ret = FALSE;
3731 if (ret && distPoint->ReasonFlags.cbData)
3733 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3734 swapped[cSwapped].pvStructInfo = &distPoint->ReasonFlags;
3735 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
3736 items[cItem].pvStructInfo = &swapped[cSwapped];
3737 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3738 cSwapped++;
3739 cItem++;
3741 if (ret && distPoint->CRLIssuer.cAltEntry)
3743 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 2;
3744 swapped[cSwapped].pvStructInfo = &distPoint->CRLIssuer;
3745 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
3746 items[cItem].pvStructInfo = &swapped[cSwapped];
3747 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3748 cSwapped++;
3749 cItem++;
3751 if (ret)
3752 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items, cItem, 0, NULL,
3753 pbEncoded, pcbEncoded);
3754 return ret;
3757 static BOOL WINAPI CRYPT_AsnEncodeCRLDistPoints(DWORD dwCertEncodingType,
3758 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3759 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3761 BOOL ret;
3763 __TRY
3765 const CRL_DIST_POINTS_INFO *info = pvStructInfo;
3767 if (!info->cDistPoint)
3769 SetLastError(E_INVALIDARG);
3770 ret = FALSE;
3772 else
3774 DWORD bytesNeeded, dataLen, lenBytes, i;
3776 ret = TRUE;
3777 for (i = 0, dataLen = 0; ret && i < info->cDistPoint; i++)
3779 DWORD len;
3781 ret = CRYPT_AsnEncodeDistPoint(&info->rgDistPoint[i], NULL,
3782 &len);
3783 if (ret)
3784 dataLen += len;
3785 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING)
3787 /* Have to propagate index of failing character */
3788 *pcbEncoded = len;
3791 if (ret)
3793 CRYPT_EncodeLen(dataLen, NULL, &lenBytes);
3794 bytesNeeded = 1 + lenBytes + dataLen;
3795 if (!pbEncoded)
3797 *pcbEncoded = bytesNeeded;
3798 ret = TRUE;
3800 else
3802 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3803 pbEncoded, pcbEncoded, bytesNeeded)))
3805 BYTE *out;
3807 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3808 pbEncoded = *(BYTE **)pbEncoded;
3809 out = pbEncoded;
3810 *out++ = ASN_SEQUENCEOF;
3811 CRYPT_EncodeLen(dataLen, out, &lenBytes);
3812 out += lenBytes;
3813 for (i = 0; ret && i < info->cDistPoint; i++)
3815 DWORD len = dataLen;
3817 ret = CRYPT_AsnEncodeDistPoint(
3818 &info->rgDistPoint[i], out, &len);
3819 if (ret)
3821 out += len;
3822 dataLen -= len;
3825 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
3826 CRYPT_FreeSpace(pEncodePara, pbEncoded);
3832 __EXCEPT_PAGE_FAULT
3834 SetLastError(STATUS_ACCESS_VIOLATION);
3835 ret = FALSE;
3837 __ENDTRY
3838 return ret;
3841 static BOOL WINAPI CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType,
3842 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3843 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3845 BOOL ret;
3847 __TRY
3849 const CERT_ENHKEY_USAGE *usage = pvStructInfo;
3850 DWORD bytesNeeded = 0, lenBytes, size, i;
3852 ret = TRUE;
3853 for (i = 0; ret && i < usage->cUsageIdentifier; i++)
3855 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
3856 usage->rgpszUsageIdentifier[i],
3857 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, NULL, &size);
3858 if (ret)
3859 bytesNeeded += size;
3861 CRYPT_EncodeLen(bytesNeeded, NULL, &lenBytes);
3862 bytesNeeded += 1 + lenBytes;
3863 if (ret)
3865 if (!pbEncoded)
3866 *pcbEncoded = bytesNeeded;
3867 else
3869 if ((ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
3870 pbEncoded, pcbEncoded, bytesNeeded)))
3872 BYTE *out;
3874 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
3875 pbEncoded = *(BYTE **)pbEncoded;
3876 out = pbEncoded;
3877 *out++ = ASN_SEQUENCEOF;
3878 CRYPT_EncodeLen(bytesNeeded - lenBytes - 1, out, &lenBytes);
3879 out += lenBytes;
3880 for (i = 0; ret && i < usage->cUsageIdentifier; i++)
3882 size = bytesNeeded;
3883 ret = CRYPT_AsnEncodeOid(dwCertEncodingType, NULL,
3884 usage->rgpszUsageIdentifier[i],
3885 dwFlags & ~CRYPT_ENCODE_ALLOC_FLAG, NULL, out, &size);
3886 if (ret)
3888 out += size;
3889 bytesNeeded -= size;
3892 if (!ret && (dwFlags & CRYPT_ENCODE_ALLOC_FLAG))
3893 CRYPT_FreeSpace(pEncodePara, pbEncoded);
3898 __EXCEPT_PAGE_FAULT
3900 SetLastError(STATUS_ACCESS_VIOLATION);
3901 ret = FALSE;
3903 __ENDTRY
3904 return ret;
3907 static BOOL WINAPI CRYPT_AsnEncodeIssuingDistPoint(DWORD dwCertEncodingType,
3908 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3909 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
3911 BOOL ret;
3913 __TRY
3915 const CRL_ISSUING_DIST_POINT *point = pvStructInfo;
3916 struct AsnEncodeSequenceItem items[6] = { { 0 } };
3917 struct AsnConstructedItem constructed = { 0 };
3918 struct AsnEncodeTagSwappedItem swapped[5] = { { 0 } };
3919 DWORD cItem = 0, cSwapped = 0;
3921 ret = TRUE;
3922 switch (point->DistPointName.dwDistPointNameChoice)
3924 case CRL_DIST_POINT_NO_NAME:
3925 /* do nothing */
3926 break;
3927 case CRL_DIST_POINT_FULL_NAME:
3928 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
3929 swapped[cSwapped].pvStructInfo = &point->DistPointName.u.FullName;
3930 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeAltName;
3931 constructed.tag = 0;
3932 constructed.pvStructInfo = &swapped[cSwapped];
3933 constructed.encodeFunc = CRYPT_AsnEncodeSwapTag;
3934 items[cItem].pvStructInfo = &constructed;
3935 items[cItem].encodeFunc = CRYPT_AsnEncodeConstructed;
3936 cSwapped++;
3937 cItem++;
3938 break;
3939 default:
3940 SetLastError(E_INVALIDARG);
3941 ret = FALSE;
3943 if (ret && point->fOnlyContainsUserCerts)
3945 swapped[cSwapped].tag = ASN_CONTEXT | 1;
3946 swapped[cSwapped].pvStructInfo = &point->fOnlyContainsUserCerts;
3947 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3948 items[cItem].pvStructInfo = &swapped[cSwapped];
3949 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3950 cSwapped++;
3951 cItem++;
3953 if (ret && point->fOnlyContainsCACerts)
3955 swapped[cSwapped].tag = ASN_CONTEXT | 2;
3956 swapped[cSwapped].pvStructInfo = &point->fOnlyContainsCACerts;
3957 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3958 items[cItem].pvStructInfo = &swapped[cSwapped];
3959 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3960 cSwapped++;
3961 cItem++;
3963 if (ret && point->OnlySomeReasonFlags.cbData)
3965 swapped[cSwapped].tag = ASN_CONTEXT | 3;
3966 swapped[cSwapped].pvStructInfo = &point->OnlySomeReasonFlags;
3967 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBits;
3968 items[cItem].pvStructInfo = &swapped[cSwapped];
3969 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3970 cSwapped++;
3971 cItem++;
3973 if (ret && point->fIndirectCRL)
3975 swapped[cSwapped].tag = ASN_CONTEXT | 4;
3976 swapped[cSwapped].pvStructInfo = &point->fIndirectCRL;
3977 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeBool;
3978 items[cItem].pvStructInfo = &swapped[cSwapped];
3979 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
3980 cSwapped++;
3981 cItem++;
3983 if (ret)
3984 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
3985 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
3987 __EXCEPT_PAGE_FAULT
3989 SetLastError(STATUS_ACCESS_VIOLATION);
3990 ret = FALSE;
3992 __ENDTRY
3993 return ret;
3996 static BOOL CRYPT_AsnEncodeGeneralSubtree(DWORD dwCertEncodingType,
3997 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
3998 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
4000 BOOL ret;
4001 const CERT_GENERAL_SUBTREE *subtree = pvStructInfo;
4002 struct AsnEncodeSequenceItem items[3] = {
4003 { &subtree->Base, CRYPT_AsnEncodeAltNameEntry, 0 },
4004 { 0 }
4006 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
4007 DWORD cItem = 1, cSwapped = 0;
4009 if (subtree->dwMinimum)
4011 swapped[cSwapped].tag = ASN_CONTEXT | 0;
4012 swapped[cSwapped].pvStructInfo = &subtree->dwMinimum;
4013 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
4014 items[cItem].pvStructInfo = &swapped[cSwapped];
4015 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4016 cSwapped++;
4017 cItem++;
4019 if (subtree->fMaximum)
4021 swapped[cSwapped].tag = ASN_CONTEXT | 1;
4022 swapped[cSwapped].pvStructInfo = &subtree->dwMaximum;
4023 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeInt;
4024 items[cItem].pvStructInfo = &swapped[cSwapped];
4025 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4026 cSwapped++;
4027 cItem++;
4029 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem, dwFlags,
4030 pEncodePara, pbEncoded, pcbEncoded);
4031 return ret;
4034 static BOOL WINAPI CRYPT_AsnEncodeNameConstraints(DWORD dwCertEncodingType,
4035 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
4036 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
4038 BOOL ret = FALSE;
4039 CRYPT_BLOB_ARRAY permitted = { 0, NULL }, excluded = { 0, NULL };
4041 TRACE("%p\n", pvStructInfo);
4043 __TRY
4045 const CERT_NAME_CONSTRAINTS_INFO *constraints = pvStructInfo;
4046 struct AsnEncodeSequenceItem items[2] = { { 0 } };
4047 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
4048 DWORD i, cItem = 0, cSwapped = 0;
4050 ret = TRUE;
4051 if (constraints->cPermittedSubtree)
4053 permitted.rgBlob = CryptMemAlloc(
4054 constraints->cPermittedSubtree * sizeof(CRYPT_DER_BLOB));
4055 if (permitted.rgBlob)
4057 permitted.cBlob = constraints->cPermittedSubtree;
4058 memset(permitted.rgBlob, 0,
4059 permitted.cBlob * sizeof(CRYPT_DER_BLOB));
4060 for (i = 0; ret && i < permitted.cBlob; i++)
4061 ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
4062 NULL, &constraints->rgPermittedSubtree[i],
4063 CRYPT_ENCODE_ALLOC_FLAG, NULL,
4064 (BYTE *)&permitted.rgBlob[i].pbData,
4065 &permitted.rgBlob[i].cbData);
4066 if (ret)
4068 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
4069 swapped[cSwapped].pvStructInfo = &permitted;
4070 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
4071 items[cItem].pvStructInfo = &swapped[cSwapped];
4072 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4073 cSwapped++;
4074 cItem++;
4077 else
4078 ret = FALSE;
4080 if (constraints->cExcludedSubtree)
4082 excluded.rgBlob = CryptMemAlloc(
4083 constraints->cExcludedSubtree * sizeof(CRYPT_DER_BLOB));
4084 if (excluded.rgBlob)
4086 excluded.cBlob = constraints->cExcludedSubtree;
4087 memset(excluded.rgBlob, 0,
4088 excluded.cBlob * sizeof(CRYPT_DER_BLOB));
4089 for (i = 0; ret && i < excluded.cBlob; i++)
4090 ret = CRYPT_AsnEncodeGeneralSubtree(dwCertEncodingType,
4091 NULL, &constraints->rgExcludedSubtree[i],
4092 CRYPT_ENCODE_ALLOC_FLAG, NULL,
4093 (BYTE *)&excluded.rgBlob[i].pbData,
4094 &excluded.rgBlob[i].cbData);
4095 if (ret)
4097 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
4098 swapped[cSwapped].pvStructInfo = &excluded;
4099 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeSet;
4100 items[cItem].pvStructInfo = &swapped[cSwapped];
4101 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4102 cSwapped++;
4103 cItem++;
4106 else
4107 ret = FALSE;
4109 if (ret)
4110 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
4111 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4112 for (i = 0; i < permitted.cBlob; i++)
4113 LocalFree(permitted.rgBlob[i].pbData);
4114 for (i = 0; i < excluded.cBlob; i++)
4115 LocalFree(excluded.rgBlob[i].pbData);
4117 __EXCEPT_PAGE_FAULT
4119 SetLastError(STATUS_ACCESS_VIOLATION);
4121 __ENDTRY
4122 CryptMemFree(permitted.rgBlob);
4123 CryptMemFree(excluded.rgBlob);
4124 TRACE("returning %d\n", ret);
4125 return ret;
4128 static BOOL WINAPI CRYPT_AsnEncodeIssuerSerialNumber(
4129 DWORD dwCertEncodingType, LPCSTR lpszStructType, const void *pvStructInfo,
4130 DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded,
4131 DWORD *pcbEncoded)
4133 BOOL ret;
4134 const CERT_ISSUER_SERIAL_NUMBER *issuerSerial = pvStructInfo;
4135 struct AsnEncodeSequenceItem items[] = {
4136 { &issuerSerial->Issuer, CRYPT_CopyEncodedBlob, 0 },
4137 { &issuerSerial->SerialNumber, CRYPT_AsnEncodeInteger, 0 },
4140 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
4141 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4142 return ret;
4145 static BOOL WINAPI CRYPT_AsnEncodePKCSSignerInfo(DWORD dwCertEncodingType,
4146 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
4147 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
4149 BOOL ret = FALSE;
4151 if (!(dwCertEncodingType & PKCS_7_ASN_ENCODING))
4153 SetLastError(E_INVALIDARG);
4154 return FALSE;
4157 __TRY
4159 const CMSG_SIGNER_INFO *info = pvStructInfo;
4161 if (!info->Issuer.cbData)
4162 SetLastError(E_INVALIDARG);
4163 else
4165 struct AsnEncodeSequenceItem items[7] = {
4166 { &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
4167 { &info->Issuer, CRYPT_AsnEncodeIssuerSerialNumber, 0 },
4168 { &info->HashAlgorithm, CRYPT_AsnEncodeAlgorithmIdWithNullParams,
4169 0 },
4171 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
4172 DWORD cItem = 3, cSwapped = 0;
4174 if (info->AuthAttrs.cAttr)
4176 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
4177 swapped[cSwapped].pvStructInfo = &info->AuthAttrs;
4178 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4179 items[cItem].pvStructInfo = &swapped[cSwapped];
4180 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4181 cSwapped++;
4182 cItem++;
4184 items[cItem].pvStructInfo = &info->HashEncryptionAlgorithm;
4185 items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4186 cItem++;
4187 items[cItem].pvStructInfo = &info->EncryptedHash;
4188 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
4189 cItem++;
4190 if (info->UnauthAttrs.cAttr)
4192 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
4193 swapped[cSwapped].pvStructInfo = &info->UnauthAttrs;
4194 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4195 items[cItem].pvStructInfo = &swapped[cSwapped];
4196 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4197 cSwapped++;
4198 cItem++;
4200 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
4201 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4204 __EXCEPT_PAGE_FAULT
4206 SetLastError(STATUS_ACCESS_VIOLATION);
4208 __ENDTRY
4209 return ret;
4212 static BOOL WINAPI CRYPT_AsnEncodeCMSSignerInfo(DWORD dwCertEncodingType,
4213 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
4214 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
4216 BOOL ret = FALSE;
4218 if (!(dwCertEncodingType & PKCS_7_ASN_ENCODING))
4220 SetLastError(E_INVALIDARG);
4221 return FALSE;
4224 __TRY
4226 const CMSG_CMS_SIGNER_INFO *info = pvStructInfo;
4228 if (info->SignerId.dwIdChoice != CERT_ID_ISSUER_SERIAL_NUMBER &&
4229 info->SignerId.dwIdChoice != CERT_ID_KEY_IDENTIFIER)
4230 SetLastError(E_INVALIDARG);
4231 else if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER &&
4232 !info->SignerId.u.IssuerSerialNumber.Issuer.cbData)
4233 SetLastError(E_INVALIDARG);
4234 else
4236 struct AsnEncodeSequenceItem items[7] = {
4237 { &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
4239 struct AsnEncodeTagSwappedItem swapped[3] = { { 0 } };
4240 DWORD cItem = 1, cSwapped = 0;
4242 if (info->SignerId.dwIdChoice == CERT_ID_ISSUER_SERIAL_NUMBER)
4244 items[cItem].pvStructInfo =
4245 &info->SignerId.u.IssuerSerialNumber.Issuer;
4246 items[cItem].encodeFunc =
4247 CRYPT_AsnEncodeIssuerSerialNumber;
4248 cItem++;
4250 else
4252 swapped[cSwapped].tag = ASN_CONTEXT | 0;
4253 swapped[cSwapped].pvStructInfo = &info->SignerId.u.KeyId;
4254 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodeOctets;
4255 items[cItem].pvStructInfo = &swapped[cSwapped];
4256 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4257 cSwapped++;
4258 cItem++;
4260 items[cItem].pvStructInfo = &info->HashAlgorithm;
4261 items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4262 cItem++;
4263 if (info->AuthAttrs.cAttr)
4265 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 0;
4266 swapped[cSwapped].pvStructInfo = &info->AuthAttrs;
4267 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4268 items[cItem].pvStructInfo = &swapped[cSwapped];
4269 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4270 cSwapped++;
4271 cItem++;
4273 items[cItem].pvStructInfo = &info->HashEncryptionAlgorithm;
4274 items[cItem].encodeFunc = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4275 cItem++;
4276 items[cItem].pvStructInfo = &info->EncryptedHash;
4277 items[cItem].encodeFunc = CRYPT_AsnEncodeOctets;
4278 cItem++;
4279 if (info->UnauthAttrs.cAttr)
4281 swapped[cSwapped].tag = ASN_CONTEXT | ASN_CONSTRUCTOR | 1;
4282 swapped[cSwapped].pvStructInfo = &info->UnauthAttrs;
4283 swapped[cSwapped].encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4284 items[cItem].pvStructInfo = &swapped[cSwapped];
4285 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4286 cSwapped++;
4287 cItem++;
4289 ret = CRYPT_AsnEncodeSequence(dwCertEncodingType, items, cItem,
4290 dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4293 __EXCEPT_PAGE_FAULT
4295 SetLastError(STATUS_ACCESS_VIOLATION);
4297 __ENDTRY
4298 return ret;
4301 BOOL CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO *signedInfo, void *pvData,
4302 DWORD *pcbData)
4304 struct AsnEncodeSequenceItem items[7] = {
4305 { &signedInfo->version, CRYPT_AsnEncodeInt, 0 },
4307 struct DERSetDescriptor digestAlgorithmsSet = { 0 }, certSet = { 0 };
4308 struct DERSetDescriptor crlSet = { 0 }, signerSet = { 0 };
4309 struct AsnEncodeTagSwappedItem swapped[2] = { { 0 } };
4310 DWORD cItem = 1, cSwapped = 0;
4311 BOOL ret = TRUE;
4313 if (signedInfo->cSignerInfo)
4315 digestAlgorithmsSet.cItems = signedInfo->cSignerInfo;
4316 digestAlgorithmsSet.items = signedInfo->rgSignerInfo;
4317 digestAlgorithmsSet.itemSize = sizeof(CMSG_CMS_SIGNER_INFO);
4318 digestAlgorithmsSet.itemOffset =
4319 offsetof(CMSG_CMS_SIGNER_INFO, HashAlgorithm);
4320 digestAlgorithmsSet.encode = CRYPT_AsnEncodeAlgorithmIdWithNullParams;
4321 items[cItem].pvStructInfo = &digestAlgorithmsSet;
4322 items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4323 cItem++;
4325 items[cItem].pvStructInfo = &signedInfo->content;
4326 items[cItem].encodeFunc = CRYPT_AsnEncodePKCSContentInfoInternal;
4327 cItem++;
4328 if (signedInfo->cCertEncoded)
4330 certSet.cItems = signedInfo->cCertEncoded;
4331 certSet.items = signedInfo->rgCertEncoded;
4332 certSet.itemSize = sizeof(CERT_BLOB);
4333 certSet.itemOffset = 0;
4334 certSet.encode = CRYPT_CopyEncodedBlob;
4335 swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 0;
4336 swapped[cSwapped].pvStructInfo = &certSet;
4337 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4338 items[cItem].pvStructInfo = &swapped[cSwapped];
4339 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4340 cSwapped++;
4341 cItem++;
4343 if (signedInfo->cCrlEncoded)
4345 crlSet.cItems = signedInfo->cCrlEncoded;
4346 crlSet.items = signedInfo->rgCrlEncoded;
4347 crlSet.itemSize = sizeof(CRL_BLOB);
4348 crlSet.itemOffset = 0;
4349 crlSet.encode = CRYPT_CopyEncodedBlob;
4350 swapped[cSwapped].tag = ASN_CONSTRUCTOR | ASN_CONTEXT | 1;
4351 swapped[cSwapped].pvStructInfo = &crlSet;
4352 swapped[cSwapped].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4353 items[cItem].pvStructInfo = &swapped[cSwapped];
4354 items[cItem].encodeFunc = CRYPT_AsnEncodeSwapTag;
4355 cSwapped++;
4356 cItem++;
4358 if (ret && signedInfo->cSignerInfo)
4360 signerSet.cItems = signedInfo->cSignerInfo;
4361 signerSet.items = signedInfo->rgSignerInfo;
4362 signerSet.itemSize = sizeof(CMSG_CMS_SIGNER_INFO);
4363 signerSet.itemOffset = 0;
4364 signerSet.encode = CRYPT_AsnEncodeCMSSignerInfo;
4365 items[cItem].pvStructInfo = &signerSet;
4366 items[cItem].encodeFunc = CRYPT_DEREncodeItemsAsSet;
4367 cItem++;
4369 if (ret)
4370 ret = CRYPT_AsnEncodeSequence(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
4371 items, cItem, 0, NULL, pvData, pcbData);
4373 return ret;
4376 static BOOL WINAPI CRYPT_AsnEncodeRecipientInfo(DWORD dwCertEncodingType,
4377 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
4378 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
4380 const CMSG_KEY_TRANS_RECIPIENT_INFO *info = pvStructInfo;
4381 struct AsnEncodeSequenceItem items[] = {
4382 { &info->dwVersion, CRYPT_AsnEncodeInt, 0 },
4383 { &info->RecipientId.u.IssuerSerialNumber,
4384 CRYPT_AsnEncodeIssuerSerialNumber, 0 },
4385 { &info->KeyEncryptionAlgorithm,
4386 CRYPT_AsnEncodeAlgorithmIdWithNullParams, 0 },
4387 { &info->EncryptedKey, CRYPT_AsnEncodeOctets, 0 },
4390 return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
4391 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4394 static BOOL WINAPI CRYPT_AsnEncodeEncryptedContentInfo(DWORD dwCertEncodingType,
4395 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
4396 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded)
4398 const CRYPT_ENCRYPTED_CONTENT_INFO *info = pvStructInfo;
4399 struct AsnEncodeTagSwappedItem swapped = { ASN_CONTEXT | 0,
4400 &info->encryptedContent, CRYPT_AsnEncodeOctets };
4401 struct AsnEncodeSequenceItem items[] = {
4402 { info->contentType, CRYPT_AsnEncodeOid, 0 },
4403 { &info->contentEncryptionAlgorithm,
4404 CRYPT_AsnEncodeAlgorithmIdWithNullParams, 0 },
4405 { &swapped, CRYPT_AsnEncodeSwapTag, 0 },
4408 return CRYPT_AsnEncodeSequence(dwCertEncodingType, items,
4409 ARRAY_SIZE(items), dwFlags, pEncodePara, pbEncoded, pcbEncoded);
4412 BOOL CRYPT_AsnEncodePKCSEnvelopedData(const CRYPT_ENVELOPED_DATA *envelopedData,
4413 void *pvData, DWORD *pcbData)
4415 struct DERSetDescriptor recipientInfosSet = { envelopedData->cRecipientInfo,
4416 envelopedData->rgRecipientInfo, sizeof(CMSG_KEY_TRANS_RECIPIENT_INFO), 0,
4417 CRYPT_AsnEncodeRecipientInfo };
4418 struct AsnEncodeSequenceItem items[] = {
4419 { &envelopedData->version, CRYPT_AsnEncodeInt, 0 },
4420 { &recipientInfosSet, CRYPT_DEREncodeItemsAsSet, 0 },
4421 { &envelopedData->encryptedContentInfo,
4422 CRYPT_AsnEncodeEncryptedContentInfo, 0 },
4425 return CRYPT_AsnEncodeSequence(X509_ASN_ENCODING, items,
4426 ARRAY_SIZE(items), 0, NULL, pvData, pcbData);
4429 static CryptEncodeObjectExFunc CRYPT_GetBuiltinEncoder(DWORD dwCertEncodingType,
4430 LPCSTR lpszStructType)
4432 CryptEncodeObjectExFunc encodeFunc = NULL;
4434 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING
4435 && (dwCertEncodingType & CMSG_ENCODING_TYPE_MASK) != PKCS_7_ASN_ENCODING)
4437 SetLastError(ERROR_FILE_NOT_FOUND);
4438 return NULL;
4441 if (IS_INTOID(lpszStructType))
4443 switch (LOWORD(lpszStructType))
4445 case LOWORD(X509_CERT):
4446 encodeFunc = CRYPT_AsnEncodeCert;
4447 break;
4448 case LOWORD(X509_CERT_TO_BE_SIGNED):
4449 encodeFunc = CRYPT_AsnEncodeCertInfo;
4450 break;
4451 case LOWORD(X509_CERT_CRL_TO_BE_SIGNED):
4452 encodeFunc = CRYPT_AsnEncodeCRLInfo;
4453 break;
4454 case LOWORD(X509_EXTENSIONS):
4455 encodeFunc = CRYPT_AsnEncodeExtensions;
4456 break;
4457 case LOWORD(X509_NAME_VALUE):
4458 encodeFunc = CRYPT_AsnEncodeNameValue;
4459 break;
4460 case LOWORD(X509_NAME):
4461 encodeFunc = CRYPT_AsnEncodeName;
4462 break;
4463 case LOWORD(X509_PUBLIC_KEY_INFO):
4464 encodeFunc = CRYPT_AsnEncodePubKeyInfo;
4465 break;
4466 case LOWORD(X509_AUTHORITY_KEY_ID):
4467 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
4468 break;
4469 case LOWORD(X509_ALTERNATE_NAME):
4470 encodeFunc = CRYPT_AsnEncodeAltName;
4471 break;
4472 case LOWORD(X509_BASIC_CONSTRAINTS):
4473 encodeFunc = CRYPT_AsnEncodeBasicConstraints;
4474 break;
4475 case LOWORD(X509_BASIC_CONSTRAINTS2):
4476 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
4477 break;
4478 case LOWORD(X509_CERT_POLICIES):
4479 encodeFunc = CRYPT_AsnEncodeCertPolicies;
4480 break;
4481 case LOWORD(RSA_CSP_PUBLICKEYBLOB):
4482 encodeFunc = CRYPT_AsnEncodeRsaPubKey;
4483 break;
4484 case LOWORD(X509_UNICODE_NAME):
4485 encodeFunc = CRYPT_AsnEncodeUnicodeName;
4486 break;
4487 case LOWORD(PKCS_CONTENT_INFO):
4488 encodeFunc = CRYPT_AsnEncodePKCSContentInfo;
4489 break;
4490 case LOWORD(PKCS_ATTRIBUTE):
4491 encodeFunc = CRYPT_AsnEncodePKCSAttribute;
4492 break;
4493 case LOWORD(X509_UNICODE_NAME_VALUE):
4494 encodeFunc = CRYPT_AsnEncodeUnicodeNameValue;
4495 break;
4496 case LOWORD(X509_OCTET_STRING):
4497 encodeFunc = CRYPT_AsnEncodeOctets;
4498 break;
4499 case LOWORD(X509_BITS):
4500 case LOWORD(X509_KEY_USAGE):
4501 encodeFunc = CRYPT_AsnEncodeBits;
4502 break;
4503 case LOWORD(X509_INTEGER):
4504 encodeFunc = CRYPT_AsnEncodeInt;
4505 break;
4506 case LOWORD(X509_MULTI_BYTE_INTEGER):
4507 encodeFunc = CRYPT_AsnEncodeInteger;
4508 break;
4509 case LOWORD(X509_MULTI_BYTE_UINT):
4510 encodeFunc = CRYPT_AsnEncodeUnsignedInteger;
4511 break;
4512 case LOWORD(X509_ENUMERATED):
4513 encodeFunc = CRYPT_AsnEncodeEnumerated;
4514 break;
4515 case LOWORD(X509_CHOICE_OF_TIME):
4516 encodeFunc = CRYPT_AsnEncodeChoiceOfTime;
4517 break;
4518 case LOWORD(X509_AUTHORITY_KEY_ID2):
4519 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
4520 break;
4521 case LOWORD(X509_AUTHORITY_INFO_ACCESS):
4522 encodeFunc = CRYPT_AsnEncodeAuthorityInfoAccess;
4523 break;
4524 case LOWORD(X509_SEQUENCE_OF_ANY):
4525 encodeFunc = CRYPT_AsnEncodeSequenceOfAny;
4526 break;
4527 case LOWORD(PKCS_UTC_TIME):
4528 encodeFunc = CRYPT_AsnEncodeUtcTime;
4529 break;
4530 case LOWORD(X509_CRL_DIST_POINTS):
4531 encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
4532 break;
4533 case LOWORD(X509_ENHANCED_KEY_USAGE):
4534 encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
4535 break;
4536 case LOWORD(PKCS_CTL):
4537 encodeFunc = CRYPT_AsnEncodeCTL;
4538 break;
4539 case LOWORD(PKCS_SMIME_CAPABILITIES):
4540 encodeFunc = CRYPT_AsnEncodeSMIMECapabilities;
4541 break;
4542 case LOWORD(X509_PKIX_POLICY_QUALIFIER_USERNOTICE):
4543 encodeFunc = CRYPT_AsnEncodePolicyQualifierUserNotice;
4544 break;
4545 case LOWORD(PKCS_ATTRIBUTES):
4546 encodeFunc = CRYPT_AsnEncodePKCSAttributes;
4547 break;
4548 case LOWORD(X509_ISSUING_DIST_POINT):
4549 encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
4550 break;
4551 case LOWORD(X509_NAME_CONSTRAINTS):
4552 encodeFunc = CRYPT_AsnEncodeNameConstraints;
4553 break;
4554 case LOWORD(X509_POLICY_MAPPINGS):
4555 encodeFunc = CRYPT_AsnEncodeCertPolicyMappings;
4556 break;
4557 case LOWORD(X509_POLICY_CONSTRAINTS):
4558 encodeFunc = CRYPT_AsnEncodeCertPolicyConstraints;
4559 break;
4560 case LOWORD(PKCS7_SIGNER_INFO):
4561 encodeFunc = CRYPT_AsnEncodePKCSSignerInfo;
4562 break;
4563 case LOWORD(CMS_SIGNER_INFO):
4564 encodeFunc = CRYPT_AsnEncodeCMSSignerInfo;
4565 break;
4568 else if (!strcmp(lpszStructType, szOID_CERT_EXTENSIONS))
4569 encodeFunc = CRYPT_AsnEncodeExtensions;
4570 else if (!strcmp(lpszStructType, szOID_RSA_signingTime))
4571 encodeFunc = CRYPT_AsnEncodeUtcTime;
4572 else if (!strcmp(lpszStructType, szOID_RSA_SMIMECapabilities))
4573 encodeFunc = CRYPT_AsnEncodeUtcTime;
4574 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER))
4575 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId;
4576 else if (!strcmp(lpszStructType, szOID_LEGACY_POLICY_MAPPINGS))
4577 encodeFunc = CRYPT_AsnEncodeCertPolicyMappings;
4578 else if (!strcmp(lpszStructType, szOID_AUTHORITY_KEY_IDENTIFIER2))
4579 encodeFunc = CRYPT_AsnEncodeAuthorityKeyId2;
4580 else if (!strcmp(lpszStructType, szOID_CRL_REASON_CODE))
4581 encodeFunc = CRYPT_AsnEncodeEnumerated;
4582 else if (!strcmp(lpszStructType, szOID_KEY_USAGE))
4583 encodeFunc = CRYPT_AsnEncodeBits;
4584 else if (!strcmp(lpszStructType, szOID_SUBJECT_KEY_IDENTIFIER))
4585 encodeFunc = CRYPT_AsnEncodeOctets;
4586 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS))
4587 encodeFunc = CRYPT_AsnEncodeBasicConstraints;
4588 else if (!strcmp(lpszStructType, szOID_BASIC_CONSTRAINTS2))
4589 encodeFunc = CRYPT_AsnEncodeBasicConstraints2;
4590 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME))
4591 encodeFunc = CRYPT_AsnEncodeAltName;
4592 else if (!strcmp(lpszStructType, szOID_ISSUER_ALT_NAME2))
4593 encodeFunc = CRYPT_AsnEncodeAltName;
4594 else if (!strcmp(lpszStructType, szOID_NEXT_UPDATE_LOCATION))
4595 encodeFunc = CRYPT_AsnEncodeAltName;
4596 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME))
4597 encodeFunc = CRYPT_AsnEncodeAltName;
4598 else if (!strcmp(lpszStructType, szOID_SUBJECT_ALT_NAME2))
4599 encodeFunc = CRYPT_AsnEncodeAltName;
4600 else if (!strcmp(lpszStructType, szOID_CRL_DIST_POINTS))
4601 encodeFunc = CRYPT_AsnEncodeCRLDistPoints;
4602 else if (!strcmp(lpszStructType, szOID_CERT_POLICIES))
4603 encodeFunc = CRYPT_AsnEncodeCertPolicies;
4604 else if (!strcmp(lpszStructType, szOID_POLICY_MAPPINGS))
4605 encodeFunc = CRYPT_AsnEncodeCertPolicyMappings;
4606 else if (!strcmp(lpszStructType, szOID_POLICY_CONSTRAINTS))
4607 encodeFunc = CRYPT_AsnEncodeCertPolicyConstraints;
4608 else if (!strcmp(lpszStructType, szOID_ENHANCED_KEY_USAGE))
4609 encodeFunc = CRYPT_AsnEncodeEnhancedKeyUsage;
4610 else if (!strcmp(lpszStructType, szOID_ISSUING_DIST_POINT))
4611 encodeFunc = CRYPT_AsnEncodeIssuingDistPoint;
4612 else if (!strcmp(lpszStructType, szOID_NAME_CONSTRAINTS))
4613 encodeFunc = CRYPT_AsnEncodeNameConstraints;
4614 else if (!strcmp(lpszStructType, szOID_AUTHORITY_INFO_ACCESS))
4615 encodeFunc = CRYPT_AsnEncodeAuthorityInfoAccess;
4616 else if (!strcmp(lpszStructType, szOID_PKIX_POLICY_QUALIFIER_USERNOTICE))
4617 encodeFunc = CRYPT_AsnEncodePolicyQualifierUserNotice;
4618 else if (!strcmp(lpszStructType, szOID_CTL))
4619 encodeFunc = CRYPT_AsnEncodeCTL;
4620 return encodeFunc;
4623 static CryptEncodeObjectFunc CRYPT_LoadEncoderFunc(DWORD dwCertEncodingType,
4624 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
4626 static HCRYPTOIDFUNCSET set = NULL;
4627 CryptEncodeObjectFunc encodeFunc = NULL;
4629 if (!set)
4630 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_FUNC, 0);
4631 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
4632 (void **)&encodeFunc, hFunc);
4633 return encodeFunc;
4636 static CryptEncodeObjectExFunc CRYPT_LoadEncoderExFunc(DWORD dwCertEncodingType,
4637 LPCSTR lpszStructType, HCRYPTOIDFUNCADDR *hFunc)
4639 static HCRYPTOIDFUNCSET set = NULL;
4640 CryptEncodeObjectExFunc encodeFunc = NULL;
4642 if (!set)
4643 set = CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_EX_FUNC, 0);
4644 CryptGetOIDFunctionAddress(set, dwCertEncodingType, lpszStructType, 0,
4645 (void **)&encodeFunc, hFunc);
4646 return encodeFunc;
4649 BOOL WINAPI CryptEncodeObject(DWORD dwCertEncodingType, LPCSTR lpszStructType,
4650 const void *pvStructInfo, BYTE *pbEncoded, DWORD *pcbEncoded)
4652 BOOL ret = FALSE;
4653 HCRYPTOIDFUNCADDR hFunc = NULL;
4654 CryptEncodeObjectFunc pCryptEncodeObject = NULL;
4655 CryptEncodeObjectExFunc pCryptEncodeObjectEx = NULL;
4657 TRACE_(crypt)("(0x%08x, %s, %p, %p, %p)\n", dwCertEncodingType,
4658 debugstr_a(lpszStructType), pvStructInfo, pbEncoded,
4659 pcbEncoded);
4661 if (!pbEncoded && !pcbEncoded)
4663 SetLastError(ERROR_INVALID_PARAMETER);
4664 return FALSE;
4667 if (!(pCryptEncodeObjectEx = CRYPT_GetBuiltinEncoder(dwCertEncodingType,
4668 lpszStructType)))
4670 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
4671 debugstr_a(lpszStructType));
4672 pCryptEncodeObject = CRYPT_LoadEncoderFunc(dwCertEncodingType,
4673 lpszStructType, &hFunc);
4674 if (!pCryptEncodeObject)
4675 pCryptEncodeObjectEx = CRYPT_LoadEncoderExFunc(dwCertEncodingType,
4676 lpszStructType, &hFunc);
4678 if (pCryptEncodeObject)
4679 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
4680 pvStructInfo, pbEncoded, pcbEncoded);
4681 else if (pCryptEncodeObjectEx)
4682 ret = pCryptEncodeObjectEx(dwCertEncodingType, lpszStructType,
4683 pvStructInfo, 0, NULL, pbEncoded, pcbEncoded);
4684 if (hFunc)
4685 CryptFreeOIDFunctionAddress(hFunc, 0);
4686 TRACE_(crypt)("returning %d\n", ret);
4687 return ret;
4690 BOOL WINAPI CryptEncodeObjectEx(DWORD dwCertEncodingType, LPCSTR lpszStructType,
4691 const void *pvStructInfo, DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
4692 void *pvEncoded, DWORD *pcbEncoded)
4694 BOOL ret = FALSE;
4695 HCRYPTOIDFUNCADDR hFunc = NULL;
4696 CryptEncodeObjectExFunc encodeFunc = NULL;
4698 TRACE_(crypt)("(0x%08x, %s, %p, 0x%08x, %p, %p, %p)\n", dwCertEncodingType,
4699 debugstr_a(lpszStructType), pvStructInfo, dwFlags, pEncodePara,
4700 pvEncoded, pcbEncoded);
4702 if (!pvEncoded && !pcbEncoded)
4704 SetLastError(ERROR_INVALID_PARAMETER);
4705 return FALSE;
4708 SetLastError(NOERROR);
4709 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG) {
4710 if (!pvEncoded) {
4711 SetLastError(ERROR_INVALID_PARAMETER);
4712 return FALSE;
4714 *(BYTE **)pvEncoded = NULL;
4716 encodeFunc = CRYPT_GetBuiltinEncoder(dwCertEncodingType, lpszStructType);
4717 if (!encodeFunc)
4719 TRACE_(crypt)("OID %s not found or unimplemented, looking for DLL\n",
4720 debugstr_a(lpszStructType));
4721 encodeFunc = CRYPT_LoadEncoderExFunc(dwCertEncodingType, lpszStructType,
4722 &hFunc);
4724 if (encodeFunc)
4725 ret = encodeFunc(dwCertEncodingType, lpszStructType, pvStructInfo,
4726 dwFlags, pEncodePara, pvEncoded, pcbEncoded);
4727 else
4729 CryptEncodeObjectFunc pCryptEncodeObject =
4730 CRYPT_LoadEncoderFunc(dwCertEncodingType, lpszStructType, &hFunc);
4732 if (pCryptEncodeObject)
4734 if (dwFlags & CRYPT_ENCODE_ALLOC_FLAG)
4736 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
4737 pvStructInfo, NULL, pcbEncoded);
4738 if (ret && (ret = CRYPT_EncodeEnsureSpace(dwFlags, pEncodePara,
4739 pvEncoded, pcbEncoded, *pcbEncoded)))
4740 ret = pCryptEncodeObject(dwCertEncodingType,
4741 lpszStructType, pvStructInfo, *(BYTE **)pvEncoded,
4742 pcbEncoded);
4744 else
4745 ret = pCryptEncodeObject(dwCertEncodingType, lpszStructType,
4746 pvStructInfo, pvEncoded, pcbEncoded);
4749 if (hFunc)
4750 CryptFreeOIDFunctionAddress(hFunc, 0);
4751 TRACE_(crypt)("returning %d\n", ret);
4752 return ret;
4755 BOOL WINAPI CryptExportPublicKeyInfo(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
4756 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
4758 return CryptExportPublicKeyInfoEx(hCryptProv, dwKeySpec, dwCertEncodingType,
4759 NULL, 0, NULL, pInfo, pcbInfo);
4762 typedef BOOL (WINAPI *EncodePublicKeyAndParametersFunc)(DWORD dwCertEncodingType,
4763 LPSTR pszPublicKeyObjId, BYTE *pbPubKey, DWORD cbPubKey, DWORD dwFlags, void *pvAuxInfo,
4764 BYTE **ppPublicKey, DWORD *pcbPublicKey, BYTE **ppbParams, DWORD *pcbParams);
4766 static BOOL WINAPI CRYPT_ExportPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
4767 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
4768 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
4770 BOOL ret;
4771 HCRYPTKEY key;
4772 static CHAR rsa_oid[] = szOID_RSA_RSA;
4774 TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
4775 dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
4776 pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
4778 if ((ret = CryptGetUserKey(hCryptProv, dwKeySpec, &key)))
4780 DWORD keySize = 0;
4782 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, NULL, &keySize);
4783 if (ret)
4785 PUBLICKEYSTRUC *pubKey = CryptMemAlloc(keySize);
4787 if (pubKey)
4789 ret = CryptExportKey(key, 0, PUBLICKEYBLOB, 0, (BYTE *)pubKey, &keySize);
4790 if (ret)
4792 DWORD encodedLen;
4794 if (!pszPublicKeyObjId)
4796 static HCRYPTOIDFUNCSET set;
4797 EncodePublicKeyAndParametersFunc encodeFunc = NULL;
4798 HCRYPTOIDFUNCADDR hFunc = NULL;
4800 pszPublicKeyObjId = (LPSTR)CertAlgIdToOID(pubKey->aiKeyAlg);
4801 TRACE("public key algid %#x (%s)\n", pubKey->aiKeyAlg, debugstr_a(pszPublicKeyObjId));
4803 if (!set) /* FIXME: there is no a public macro */
4804 set = CryptInitOIDFunctionSet("CryptDllEncodePublicKeyAndParameters", 0);
4806 CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId, 0, (void **)&encodeFunc, &hFunc);
4807 if (encodeFunc)
4809 BYTE *key_data = NULL;
4810 DWORD key_size = 0;
4811 BYTE *params = NULL;
4812 DWORD params_size = 0;
4814 ret = encodeFunc(dwCertEncodingType, pszPublicKeyObjId, (BYTE *)pubKey, keySize,
4815 dwFlags, pvAuxInfo, &key_data, &key_size, &params, &params_size);
4816 if (ret)
4818 DWORD oid_size = strlen(pszPublicKeyObjId) + 1;
4819 DWORD size_needed = sizeof(*pInfo) + oid_size + key_size + params_size;
4821 if (!pInfo)
4822 *pcbInfo = size_needed;
4823 else if (*pcbInfo < size_needed)
4825 *pcbInfo = size_needed;
4826 SetLastError(ERROR_MORE_DATA);
4827 ret = FALSE;
4829 else
4831 *pcbInfo = size_needed;
4832 pInfo->Algorithm.pszObjId = (char *)(pInfo + 1);
4833 lstrcpyA(pInfo->Algorithm.pszObjId, pszPublicKeyObjId);
4834 if (params)
4836 pInfo->Algorithm.Parameters.cbData = params_size;
4837 pInfo->Algorithm.Parameters.pbData = (BYTE *)pInfo->Algorithm.pszObjId + oid_size;
4838 memcpy(pInfo->Algorithm.Parameters.pbData, params, params_size);
4840 else
4842 pInfo->Algorithm.Parameters.cbData = 0;
4843 pInfo->Algorithm.Parameters.pbData = NULL;
4845 pInfo->PublicKey.pbData = (BYTE *)pInfo->Algorithm.pszObjId + oid_size + params_size;
4846 pInfo->PublicKey.cbData = key_size;
4847 memcpy(pInfo->PublicKey.pbData, key_data, key_size);
4848 pInfo->PublicKey.cUnusedBits = 0;
4851 CryptMemFree(key_data);
4852 CryptMemFree(params);
4855 CryptMemFree(pubKey);
4856 CryptFreeOIDFunctionAddress(hFunc, 0);
4857 return ret;
4860 /* fallback to RSA */
4861 pszPublicKeyObjId = rsa_oid;
4864 encodedLen = 0;
4865 ret = CryptEncodeObject(dwCertEncodingType,
4866 RSA_CSP_PUBLICKEYBLOB, pubKey, NULL, &encodedLen);
4867 if (ret)
4869 DWORD sizeNeeded = sizeof(CERT_PUBLIC_KEY_INFO) +
4870 strlen(pszPublicKeyObjId) + 1 + encodedLen;
4872 if (!pInfo)
4873 *pcbInfo = sizeNeeded;
4874 else if (*pcbInfo < sizeNeeded)
4876 SetLastError(ERROR_MORE_DATA);
4877 *pcbInfo = sizeNeeded;
4878 ret = FALSE;
4880 else
4882 *pcbInfo = sizeNeeded;
4883 pInfo->Algorithm.pszObjId = (char *)pInfo +
4884 sizeof(CERT_PUBLIC_KEY_INFO);
4885 lstrcpyA(pInfo->Algorithm.pszObjId,
4886 pszPublicKeyObjId);
4887 pInfo->Algorithm.Parameters.cbData = 0;
4888 pInfo->Algorithm.Parameters.pbData = NULL;
4889 pInfo->PublicKey.pbData =
4890 (BYTE *)pInfo->Algorithm.pszObjId
4891 + lstrlenA(pInfo->Algorithm.pszObjId) + 1;
4892 pInfo->PublicKey.cbData = encodedLen;
4893 pInfo->PublicKey.cUnusedBits = 0;
4894 ret = CryptEncodeObject(dwCertEncodingType,
4895 RSA_CSP_PUBLICKEYBLOB, pubKey,
4896 pInfo->PublicKey.pbData, &pInfo->PublicKey.cbData);
4900 CryptMemFree(pubKey);
4902 else
4903 ret = FALSE;
4905 CryptDestroyKey(key);
4907 return ret;
4910 typedef BOOL (WINAPI *ExportPublicKeyInfoExFunc)(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
4911 DWORD dwKeySpec, DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId,
4912 DWORD dwFlags, void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo);
4914 BOOL WINAPI CryptExportPublicKeyInfoEx(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv, DWORD dwKeySpec,
4915 DWORD dwCertEncodingType, LPSTR pszPublicKeyObjId, DWORD dwFlags,
4916 void *pvAuxInfo, PCERT_PUBLIC_KEY_INFO pInfo, DWORD *pcbInfo)
4918 static HCRYPTOIDFUNCSET set = NULL;
4919 BOOL ret;
4920 ExportPublicKeyInfoExFunc exportFunc = NULL;
4921 HCRYPTOIDFUNCADDR hFunc = NULL;
4923 TRACE_(crypt)("(%08lx, %d, %08x, %s, %08x, %p, %p, %d)\n", hCryptProv,
4924 dwKeySpec, dwCertEncodingType, debugstr_a(pszPublicKeyObjId), dwFlags,
4925 pvAuxInfo, pInfo, pInfo ? *pcbInfo : 0);
4927 if (!hCryptProv)
4929 SetLastError(ERROR_INVALID_PARAMETER);
4930 return FALSE;
4933 if (pszPublicKeyObjId)
4935 if (!set)
4936 set = CryptInitOIDFunctionSet(CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC,
4938 CryptGetOIDFunctionAddress(set, dwCertEncodingType, pszPublicKeyObjId,
4939 0, (void **)&exportFunc, &hFunc);
4941 if (!exportFunc)
4942 exportFunc = CRYPT_ExportPublicKeyInfoEx;
4943 ret = exportFunc(hCryptProv, dwKeySpec, dwCertEncodingType,
4944 pszPublicKeyObjId, dwFlags, pvAuxInfo, pInfo, pcbInfo);
4945 if (hFunc)
4946 CryptFreeOIDFunctionAddress(hFunc, 0);
4947 return ret;
4950 BOOL WINAPI CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv,
4951 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
4953 return CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType, pInfo,
4954 0, 0, NULL, phKey);
4957 typedef BOOL (WINAPI *ConvertPublicKeyInfoFunc)(DWORD dwCertEncodingType,
4958 PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg, DWORD dwFlags,
4959 BYTE **ppbData, DWORD *dwDataLen);
4961 static BOOL WINAPI CRYPT_ImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
4962 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
4963 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
4965 static HCRYPTOIDFUNCSET set = NULL;
4966 ConvertPublicKeyInfoFunc convertFunc = NULL;
4967 HCRYPTOIDFUNCADDR hFunc = NULL;
4968 BOOL ret;
4969 DWORD pubKeySize;
4970 LPBYTE pubKey;
4972 TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
4973 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
4975 if (!set)
4976 set = CryptInitOIDFunctionSet(CRYPT_OID_CONVERT_PUBLIC_KEY_INFO_FUNC, 0);
4977 CryptGetOIDFunctionAddress(set, dwCertEncodingType, pInfo->Algorithm.pszObjId,
4978 0, (void **)&convertFunc, &hFunc);
4979 if (convertFunc)
4981 pubKey = NULL;
4982 pubKeySize = 0;
4983 ret = convertFunc(dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, &pubKey, &pubKeySize);
4984 if (ret)
4986 ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0, phKey);
4987 CryptMemFree(pubKey);
4990 CryptFreeOIDFunctionAddress(hFunc, 0);
4991 return ret;
4994 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
4995 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, NULL, &pubKeySize);
4996 if (ret)
4998 pubKey = CryptMemAlloc(pubKeySize);
5000 if (pubKey)
5002 ret = CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
5003 pInfo->PublicKey.pbData, pInfo->PublicKey.cbData, 0, pubKey,
5004 &pubKeySize);
5005 if (ret)
5007 if(aiKeyAlg)
5008 ((BLOBHEADER*)pubKey)->aiKeyAlg = aiKeyAlg;
5009 ret = CryptImportKey(hCryptProv, pubKey, pubKeySize, 0, 0,
5010 phKey);
5012 CryptMemFree(pubKey);
5014 else
5015 ret = FALSE;
5017 return ret;
5020 typedef BOOL (WINAPI *ImportPublicKeyInfoExFunc)(HCRYPTPROV hCryptProv,
5021 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5022 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey);
5024 BOOL WINAPI CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv,
5025 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, ALG_ID aiKeyAlg,
5026 DWORD dwFlags, void *pvAuxInfo, HCRYPTKEY *phKey)
5028 static HCRYPTOIDFUNCSET set = NULL;
5029 BOOL ret;
5030 ImportPublicKeyInfoExFunc importFunc = NULL;
5031 HCRYPTOIDFUNCADDR hFunc = NULL;
5033 TRACE_(crypt)("(%08lx, %08x, %p, %08x, %08x, %p, %p)\n", hCryptProv,
5034 dwCertEncodingType, pInfo, aiKeyAlg, dwFlags, pvAuxInfo, phKey);
5036 if (!set)
5037 set = CryptInitOIDFunctionSet(CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC, 0);
5038 CryptGetOIDFunctionAddress(set, dwCertEncodingType,
5039 pInfo->Algorithm.pszObjId, 0, (void **)&importFunc, &hFunc);
5040 if (!importFunc)
5041 importFunc = CRYPT_ImportPublicKeyInfoEx;
5042 ret = importFunc(hCryptProv, dwCertEncodingType, pInfo, aiKeyAlg, dwFlags,
5043 pvAuxInfo, phKey);
5044 if (hFunc)
5045 CryptFreeOIDFunctionAddress(hFunc, 0);
5046 return ret;
5049 BOOL WINAPI CryptImportPublicKeyInfoEx2(DWORD dwCertEncodingType,
5050 PCERT_PUBLIC_KEY_INFO pInfo, DWORD dwFlags, void *pvAuxInfo,
5051 BCRYPT_KEY_HANDLE *phKey)
5053 FIXME_(crypt)("(%d, %p, %08x, %p, %p): stub\n", dwCertEncodingType, pInfo,
5054 dwFlags, pvAuxInfo, phKey);
5055 SetLastError(ERROR_FILE_NOT_FOUND);
5056 return FALSE;