2 * Copyright 2005 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 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 is
21 * undocumented, so I implement them here.
24 * "A Layman's Guide to a Subset of ASN.1, BER, and DER", by Burton Kaliski
25 * (available online, look for a PDF copy as the HTML versions tend to have
26 * translation errors.)
28 * RFC3280, http://www.faqs.org/rfcs/rfc3280.html
31 * http://msdn.microsoft.com/library/en-us/seccrypto/security/constants_for_cryptencodeobject_and_cryptdecodeobject.asp
39 #define NONAMELESSUNION
47 #include "wine/debug.h"
48 #include "wine/exception.h"
49 #include "wine/unicode.h"
50 #include "crypt32_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
54 typedef BOOL (WINAPI
*CryptEncodeObjectFunc
)(DWORD
, LPCSTR
, const void *,
56 typedef BOOL (WINAPI
*CryptEncodeObjectExFunc
)(DWORD
, LPCSTR
, const void *,
57 DWORD
, PCRYPT_ENCODE_PARA
, BYTE
*, DWORD
*);
59 /* Prototypes for built-in encoders. They follow the Ex style prototypes.
60 * The dwCertEncodingType and lpszStructType are ignored by the built-in
61 * functions, but the parameters are retained to simplify CryptEncodeObjectEx,
62 * since it must call functions in external DLLs that follow these signatures.
64 static BOOL WINAPI
CRYPT_AsnEncodeOid(DWORD dwCertEncodingType
,
65 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
66 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
67 static BOOL WINAPI
CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType
,
68 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
69 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
70 static BOOL WINAPI
CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType
,
71 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
72 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
73 static BOOL WINAPI
CRYPT_AsnEncodeBool(DWORD dwCertEncodingType
,
74 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
75 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
76 static BOOL WINAPI
CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType
,
77 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
78 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
79 static BOOL WINAPI
CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType
,
80 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
81 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
82 static BOOL WINAPI
CRYPT_AsnEncodeBits(DWORD dwCertEncodingType
,
83 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
84 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
85 static BOOL WINAPI
CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType
,
86 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
87 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
88 static BOOL WINAPI
CRYPT_AsnEncodeInt(DWORD dwCertEncodingType
,
89 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
90 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
91 static BOOL WINAPI
CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType
,
92 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
93 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
94 static BOOL WINAPI
CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType
,
95 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
96 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
97 static BOOL WINAPI
CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType
,
98 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
99 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
101 BOOL WINAPI
CryptEncodeObject(DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
102 const void *pvStructInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
104 static HCRYPTOIDFUNCSET set
= NULL
;
106 HCRYPTOIDFUNCADDR hFunc
;
107 CryptEncodeObjectFunc pCryptEncodeObject
;
109 TRACE("(0x%08lx, %s, %p, %p, %p)\n", dwCertEncodingType
,
110 debugstr_a(lpszStructType
), pvStructInfo
, pbEncoded
,
113 if (!pbEncoded
&& !pcbEncoded
)
115 SetLastError(ERROR_INVALID_PARAMETER
);
119 /* Try registered DLL first.. */
121 set
= CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_FUNC
, 0);
122 CryptGetOIDFunctionAddress(set
, dwCertEncodingType
, lpszStructType
, 0,
123 (void **)&pCryptEncodeObject
, &hFunc
);
124 if (pCryptEncodeObject
)
126 ret
= pCryptEncodeObject(dwCertEncodingType
, lpszStructType
,
127 pvStructInfo
, pbEncoded
, pcbEncoded
);
128 CryptFreeOIDFunctionAddress(hFunc
, 0);
132 /* If not, use CryptEncodeObjectEx */
133 ret
= CryptEncodeObjectEx(dwCertEncodingType
, lpszStructType
,
134 pvStructInfo
, 0, NULL
, pbEncoded
, pcbEncoded
);
139 /* Helper function to check *pcbEncoded, set it to the required size, and
140 * optionally to allocate memory. Assumes pbEncoded is not NULL.
141 * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
142 * pointer to the newly allocated memory.
144 static BOOL
CRYPT_EncodeEnsureSpace(DWORD dwFlags
,
145 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
150 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
152 if (pEncodePara
&& pEncodePara
->pfnAlloc
)
153 *(BYTE
**)pbEncoded
= pEncodePara
->pfnAlloc(bytesNeeded
);
155 *(BYTE
**)pbEncoded
= LocalAlloc(0, bytesNeeded
);
156 if (!*(BYTE
**)pbEncoded
)
159 *pcbEncoded
= bytesNeeded
;
161 else if (bytesNeeded
> *pcbEncoded
)
163 *pcbEncoded
= bytesNeeded
;
164 SetLastError(ERROR_MORE_DATA
);
168 *pcbEncoded
= bytesNeeded
;
172 static BOOL
CRYPT_EncodeLen(DWORD len
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
174 DWORD bytesNeeded
, significantBytes
= 0;
182 for (temp
= len
, significantBytes
= sizeof(temp
); !(temp
& 0xff000000);
183 temp
<<= 8, significantBytes
--)
185 bytesNeeded
= significantBytes
+ 1;
189 *pcbEncoded
= bytesNeeded
;
192 if (*pcbEncoded
< bytesNeeded
)
194 SetLastError(ERROR_MORE_DATA
);
198 *pbEncoded
= (BYTE
)len
;
203 *pbEncoded
++ = significantBytes
| 0x80;
204 for (i
= 0; i
< significantBytes
; i
++)
206 *(pbEncoded
+ significantBytes
- i
- 1) = (BYTE
)(len
& 0xff);
210 *pcbEncoded
= bytesNeeded
;
214 struct AsnEncodeSequenceItem
216 const void *pvStructInfo
;
217 CryptEncodeObjectExFunc encodeFunc
;
218 DWORD size
; /* used during encoding, not for your use */
221 static BOOL WINAPI
CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType
,
222 struct AsnEncodeSequenceItem items
[], DWORD cItem
, DWORD dwFlags
,
223 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
226 DWORD i
, dataLen
= 0;
228 TRACE("%p, %ld, %08lx, %p, %p, %ld\n", items
, cItem
, dwFlags
, pEncodePara
,
229 pbEncoded
, *pcbEncoded
);
230 for (i
= 0, ret
= TRUE
; ret
&& i
< cItem
; i
++)
232 ret
= items
[i
].encodeFunc(dwCertEncodingType
, NULL
,
233 items
[i
].pvStructInfo
, dwFlags
& ~CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
234 NULL
, &items
[i
].size
);
235 /* Some functions propagate their errors through the size */
237 *pcbEncoded
= items
[i
].size
;
238 dataLen
+= items
[i
].size
;
242 DWORD lenBytes
, bytesNeeded
;
244 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
245 bytesNeeded
= 1 + lenBytes
+ dataLen
;
247 *pcbEncoded
= bytesNeeded
;
250 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
251 pcbEncoded
, bytesNeeded
)))
253 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
254 pbEncoded
= *(BYTE
**)pbEncoded
;
255 *pbEncoded
++ = ASN_SEQUENCE
;
256 CRYPT_EncodeLen(dataLen
, pbEncoded
, &lenBytes
);
257 pbEncoded
+= lenBytes
;
258 for (i
= 0; ret
&& i
< cItem
; i
++)
260 ret
= items
[i
].encodeFunc(dwCertEncodingType
, NULL
,
261 items
[i
].pvStructInfo
, dwFlags
& ~CRYPT_ENCODE_ALLOC_FLAG
,
262 NULL
, pbEncoded
, &items
[i
].size
);
263 /* Some functions propagate their errors through the size */
265 *pcbEncoded
= items
[i
].size
;
266 pbEncoded
+= items
[i
].size
;
271 TRACE("returning %d (%08lx)\n", ret
, GetLastError());
275 struct AsnConstructedItem
278 const void *pvStructInfo
;
279 CryptEncodeObjectExFunc encodeFunc
;
282 static BOOL WINAPI
CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType
,
283 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
284 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
287 const struct AsnConstructedItem
*item
=
288 (const struct AsnConstructedItem
*)pvStructInfo
;
291 if ((ret
= item
->encodeFunc(dwCertEncodingType
, lpszStructType
,
292 item
->pvStructInfo
, dwFlags
& ~CRYPT_ENCODE_ALLOC_FLAG
, NULL
, NULL
, &len
)))
294 DWORD dataLen
, bytesNeeded
;
296 CRYPT_EncodeLen(len
, NULL
, &dataLen
);
297 bytesNeeded
= 1 + dataLen
+ len
;
299 *pcbEncoded
= bytesNeeded
;
300 else if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
301 pbEncoded
, pcbEncoded
, bytesNeeded
)))
303 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
304 pbEncoded
= *(BYTE
**)pbEncoded
;
305 *pbEncoded
++ = ASN_CONTEXT
| ASN_CONSTRUCTOR
| item
->tag
;
306 CRYPT_EncodeLen(len
, pbEncoded
, &dataLen
);
307 pbEncoded
+= dataLen
;
308 ret
= item
->encodeFunc(dwCertEncodingType
, lpszStructType
,
309 item
->pvStructInfo
, dwFlags
& ~CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
313 /* Some functions propagate their errors through the size */
320 /* Some functions propagate their errors through the size */
326 struct AsnEncodeTagSwappedItem
329 const void *pvStructInfo
;
330 CryptEncodeObjectExFunc encodeFunc
;
333 /* Sort of a wacky hack, it encodes something using the struct
334 * AsnEncodeTagSwappedItem's encodeFunc, then replaces the tag byte with the tag
335 * given in the struct AsnEncodeTagSwappedItem.
337 static BOOL WINAPI
CRYPT_AsnEncodeSwapTag(DWORD dwCertEncodingType
,
338 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
339 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
342 const struct AsnEncodeTagSwappedItem
*item
=
343 (const struct AsnEncodeTagSwappedItem
*)pvStructInfo
;
345 ret
= item
->encodeFunc(dwCertEncodingType
, lpszStructType
,
346 item
->pvStructInfo
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
347 if (ret
&& pbEncoded
)
348 *pbEncoded
= item
->tag
;
352 static BOOL WINAPI
CRYPT_AsnEncodeCertVersion(DWORD dwCertEncodingType
,
353 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
354 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
356 const DWORD
*ver
= (const DWORD
*)pvStructInfo
;
359 /* CERT_V1 is not encoded */
367 struct AsnConstructedItem item
= { 0, ver
, CRYPT_AsnEncodeInt
};
369 ret
= CRYPT_AsnEncodeConstructed(dwCertEncodingType
, X509_INTEGER
,
370 &item
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
375 static BOOL WINAPI
CRYPT_CopyEncodedBlob(DWORD dwCertEncodingType
,
376 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
377 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
379 const CRYPT_DER_BLOB
*blob
= (const CRYPT_DER_BLOB
*)pvStructInfo
;
384 *pcbEncoded
= blob
->cbData
;
389 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
390 pcbEncoded
, blob
->cbData
)))
392 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
393 pbEncoded
= *(BYTE
**)pbEncoded
;
395 memcpy(pbEncoded
, blob
->pbData
, blob
->cbData
);
396 *pcbEncoded
= blob
->cbData
;
403 static BOOL WINAPI
CRYPT_AsnEncodeValidity(DWORD dwCertEncodingType
,
404 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
405 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
408 /* This has two filetimes in a row, a NotBefore and a NotAfter */
409 const FILETIME
*timePtr
= (const FILETIME
*)pvStructInfo
;
410 struct AsnEncodeSequenceItem items
[] = {
411 { timePtr
++, CRYPT_AsnEncodeChoiceOfTime
, 0 },
412 { timePtr
, CRYPT_AsnEncodeChoiceOfTime
, 0 },
415 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
,
416 sizeof(items
) / sizeof(items
[0]), dwFlags
, pEncodePara
, pbEncoded
,
421 static BOOL WINAPI
CRYPT_AsnEncodeAlgorithmId(
422 DWORD dwCertEncodingType
, LPCSTR lpszStructType
, const void *pvStructInfo
,
423 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
426 const CRYPT_ALGORITHM_IDENTIFIER
*algo
=
427 (const CRYPT_ALGORITHM_IDENTIFIER
*)pvStructInfo
;
429 struct AsnEncodeSequenceItem items
[] = {
430 { algo
->pszObjId
, CRYPT_AsnEncodeOid
, 0 },
431 { &algo
->Parameters
, CRYPT_CopyEncodedBlob
, 0 },
434 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
,
435 sizeof(items
) / sizeof(items
[0]), dwFlags
, pEncodePara
, pbEncoded
,
440 static BOOL WINAPI
CRYPT_AsnEncodePubKeyInfo(DWORD dwCertEncodingType
,
441 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
442 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
448 const CERT_PUBLIC_KEY_INFO
*info
=
449 (const CERT_PUBLIC_KEY_INFO
*)pvStructInfo
;
450 struct AsnEncodeSequenceItem items
[] = {
451 { &info
->Algorithm
, CRYPT_AsnEncodeAlgorithmId
, 0 },
452 { &info
->PublicKey
, CRYPT_AsnEncodeBits
, 0 },
455 TRACE("Encoding public key with OID %s\n",
456 debugstr_a(info
->Algorithm
.pszObjId
));
457 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
,
458 sizeof(items
) / sizeof(items
[0]), dwFlags
, pEncodePara
, pbEncoded
,
463 SetLastError(STATUS_ACCESS_VIOLATION
);
470 static BOOL WINAPI
CRYPT_AsnEncodeCert(DWORD dwCertEncodingType
,
471 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
472 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
478 const CERT_SIGNED_CONTENT_INFO
*info
=
479 (const CERT_SIGNED_CONTENT_INFO
*)pvStructInfo
;
480 struct AsnEncodeSequenceItem items
[] = {
481 { &info
->ToBeSigned
, CRYPT_CopyEncodedBlob
, 0 },
482 { &info
->SignatureAlgorithm
, CRYPT_AsnEncodeAlgorithmId
, 0 },
483 { &info
->Signature
, CRYPT_AsnEncodeBitsSwapBytes
, 0 },
486 if (dwFlags
& CRYPT_ENCODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG
)
487 items
[2].encodeFunc
= CRYPT_AsnEncodeBits
;
488 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
,
489 sizeof(items
) / sizeof(items
[0]), dwFlags
, pEncodePara
, pbEncoded
,
494 SetLastError(STATUS_ACCESS_VIOLATION
);
501 /* Like in Windows, this blithely ignores the validity of the passed-in
502 * CERT_INFO, and just encodes it as-is. The resulting encoded data may not
503 * decode properly, see CRYPT_AsnDecodeCertInfo.
505 static BOOL WINAPI
CRYPT_AsnEncodeCertInfo(DWORD dwCertEncodingType
,
506 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
507 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
513 const CERT_INFO
*info
= (const CERT_INFO
*)pvStructInfo
;
514 struct AsnEncodeSequenceItem items
[10] = {
515 { &info
->dwVersion
, CRYPT_AsnEncodeCertVersion
, 0 },
516 { &info
->SerialNumber
, CRYPT_AsnEncodeInteger
, 0 },
517 { &info
->SignatureAlgorithm
, CRYPT_AsnEncodeAlgorithmId
, 0 },
518 { &info
->Issuer
, CRYPT_CopyEncodedBlob
, 0 },
519 { &info
->NotBefore
, CRYPT_AsnEncodeValidity
, 0 },
520 { &info
->Subject
, CRYPT_CopyEncodedBlob
, 0 },
521 { &info
->SubjectPublicKeyInfo
, CRYPT_AsnEncodePubKeyInfo
, 0 },
524 struct AsnConstructedItem constructed
[3] = { { 0 } };
525 DWORD cItem
= 7, cConstructed
= 0;
527 if (info
->IssuerUniqueId
.cbData
)
529 constructed
[cConstructed
].tag
= 1;
530 constructed
[cConstructed
].pvStructInfo
= &info
->IssuerUniqueId
;
531 constructed
[cConstructed
].encodeFunc
= CRYPT_AsnEncodeBits
;
532 items
[cItem
].pvStructInfo
= &constructed
[cConstructed
];
533 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeConstructed
;
537 if (info
->SubjectUniqueId
.cbData
)
539 constructed
[cConstructed
].tag
= 2;
540 constructed
[cConstructed
].pvStructInfo
= &info
->SubjectUniqueId
;
541 constructed
[cConstructed
].encodeFunc
= CRYPT_AsnEncodeBits
;
542 items
[cItem
].pvStructInfo
= &constructed
[cConstructed
];
543 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeConstructed
;
547 if (info
->cExtension
)
549 constructed
[cConstructed
].tag
= 3;
550 constructed
[cConstructed
].pvStructInfo
= &info
->cExtension
;
551 constructed
[cConstructed
].encodeFunc
= CRYPT_AsnEncodeExtensions
;
552 items
[cItem
].pvStructInfo
= &constructed
[cConstructed
];
553 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeConstructed
;
558 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
, cItem
,
559 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
563 SetLastError(STATUS_ACCESS_VIOLATION
);
570 static BOOL WINAPI
CRYPT_AsnEncodeCRLEntry(const CRL_ENTRY
*entry
,
571 BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
573 struct AsnEncodeSequenceItem items
[3] = {
574 { &entry
->SerialNumber
, CRYPT_AsnEncodeInteger
, 0 },
575 { &entry
->RevocationDate
, CRYPT_AsnEncodeChoiceOfTime
, 0 },
581 TRACE("%p, %p, %p\n", entry
, pbEncoded
, pcbEncoded
);
583 if (entry
->cExtension
)
585 items
[cItem
].pvStructInfo
= &entry
->cExtension
;
586 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeExtensions
;
590 ret
= CRYPT_AsnEncodeSequence(X509_ASN_ENCODING
, items
, cItem
, 0, NULL
,
591 pbEncoded
, pcbEncoded
);
593 TRACE("returning %d (%08lx)\n", ret
, GetLastError());
597 static BOOL WINAPI
CRYPT_AsnEncodeCRLEntries(DWORD dwCertEncodingType
,
598 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
599 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
601 DWORD cCRLEntry
= *(const DWORD
*)pvStructInfo
;
602 DWORD bytesNeeded
, dataLen
, lenBytes
, i
;
603 const CRL_ENTRY
*rgCRLEntry
= *(const CRL_ENTRY
**)
604 ((const BYTE
*)pvStructInfo
+ sizeof(DWORD
));
607 for (i
= 0, dataLen
= 0; ret
&& i
< cCRLEntry
; i
++)
611 ret
= CRYPT_AsnEncodeCRLEntry(&rgCRLEntry
[i
], NULL
, &size
);
615 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
616 bytesNeeded
= 1 + lenBytes
+ dataLen
;
618 *pcbEncoded
= bytesNeeded
;
621 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
622 pcbEncoded
, bytesNeeded
)))
624 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
625 pbEncoded
= *(BYTE
**)pbEncoded
;
626 *pbEncoded
++ = ASN_SEQUENCEOF
;
627 CRYPT_EncodeLen(dataLen
, pbEncoded
, &lenBytes
);
628 pbEncoded
+= lenBytes
;
629 for (i
= 0; i
< cCRLEntry
; i
++)
631 DWORD size
= dataLen
;
633 ret
= CRYPT_AsnEncodeCRLEntry(&rgCRLEntry
[i
], pbEncoded
, &size
);
642 static BOOL WINAPI
CRYPT_AsnEncodeCRLVersion(DWORD dwCertEncodingType
,
643 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
644 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
646 const DWORD
*ver
= (const DWORD
*)pvStructInfo
;
649 /* CRL_V1 is not encoded */
656 ret
= CRYPT_AsnEncodeInt(dwCertEncodingType
, X509_INTEGER
, ver
,
657 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
661 /* Like in Windows, this blithely ignores the validity of the passed-in
662 * CRL_INFO, and just encodes it as-is. The resulting encoded data may not
663 * decode properly, see CRYPT_AsnDecodeCRLInfo.
665 static BOOL WINAPI
CRYPT_AsnEncodeCRLInfo(DWORD dwCertEncodingType
,
666 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
667 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
673 const CRL_INFO
*info
= (const CRL_INFO
*)pvStructInfo
;
674 struct AsnEncodeSequenceItem items
[7] = {
675 { &info
->dwVersion
, CRYPT_AsnEncodeCRLVersion
, 0 },
676 { &info
->SignatureAlgorithm
, CRYPT_AsnEncodeAlgorithmId
, 0 },
677 { &info
->Issuer
, CRYPT_CopyEncodedBlob
, 0 },
678 { &info
->ThisUpdate
, CRYPT_AsnEncodeChoiceOfTime
, 0 },
681 struct AsnConstructedItem constructed
[1] = { { 0 } };
682 DWORD cItem
= 4, cConstructed
= 0;
684 if (info
->NextUpdate
.dwLowDateTime
|| info
->NextUpdate
.dwHighDateTime
)
686 items
[cItem
].pvStructInfo
= &info
->NextUpdate
;
687 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeChoiceOfTime
;
692 items
[cItem
].pvStructInfo
= &info
->cCRLEntry
;
693 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeCRLEntries
;
696 if (info
->cExtension
)
698 constructed
[cConstructed
].tag
= 0;
699 constructed
[cConstructed
].pvStructInfo
= &info
->cExtension
;
700 constructed
[cConstructed
].encodeFunc
= CRYPT_AsnEncodeExtensions
;
701 items
[cItem
].pvStructInfo
= &constructed
[cConstructed
];
702 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeConstructed
;
707 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
, cItem
,
708 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
712 SetLastError(STATUS_ACCESS_VIOLATION
);
719 static BOOL
CRYPT_AsnEncodeExtension(CERT_EXTENSION
*ext
, BYTE
*pbEncoded
,
723 struct AsnEncodeSequenceItem items
[3] = {
724 { ext
->pszObjId
, CRYPT_AsnEncodeOid
, 0 },
730 TRACE("%p, %p, %ld\n", ext
, pbEncoded
, *pcbEncoded
);
734 items
[cItem
].pvStructInfo
= &ext
->fCritical
;
735 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeBool
;
738 items
[cItem
].pvStructInfo
= &ext
->Value
;
739 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeOctets
;
742 ret
= CRYPT_AsnEncodeSequence(X509_ASN_ENCODING
, items
, cItem
, 0, NULL
,
743 pbEncoded
, pcbEncoded
);
744 TRACE("returning %d (%08lx)\n", ret
, GetLastError());
748 static BOOL WINAPI
CRYPT_AsnEncodeExtensions(DWORD dwCertEncodingType
,
749 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
750 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
756 DWORD bytesNeeded
, dataLen
, lenBytes
, i
;
757 const CERT_EXTENSIONS
*exts
= (const CERT_EXTENSIONS
*)pvStructInfo
;
760 for (i
= 0, dataLen
= 0; ret
&& i
< exts
->cExtension
; i
++)
764 ret
= CRYPT_AsnEncodeExtension(&exts
->rgExtension
[i
], NULL
, &size
);
768 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
769 bytesNeeded
= 1 + lenBytes
+ dataLen
;
771 *pcbEncoded
= bytesNeeded
;
774 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
775 pcbEncoded
, bytesNeeded
)))
777 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
778 pbEncoded
= *(BYTE
**)pbEncoded
;
779 *pbEncoded
++ = ASN_SEQUENCEOF
;
780 CRYPT_EncodeLen(dataLen
, pbEncoded
, &lenBytes
);
781 pbEncoded
+= lenBytes
;
782 for (i
= 0; i
< exts
->cExtension
; i
++)
784 DWORD size
= dataLen
;
786 ret
= CRYPT_AsnEncodeExtension(&exts
->rgExtension
[i
],
796 SetLastError(STATUS_ACCESS_VIOLATION
);
803 static BOOL WINAPI
CRYPT_AsnEncodeOid(DWORD dwCertEncodingType
,
804 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
805 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
807 LPCSTR pszObjId
= (LPCSTR
)pvStructInfo
;
808 DWORD bytesNeeded
= 0, lenBytes
;
813 TRACE("%s\n", debugstr_a(pszObjId
));
820 if (sscanf(pszObjId
, "%d.%d.%n", &val1
, &val2
, &firstPos
) != 2)
822 SetLastError(CRYPT_E_ASN1_ERROR
);
826 firstByte
= val1
* 40 + val2
;
827 ptr
= pszObjId
+ firstPos
;
832 /* note I assume each component is at most 32-bits long in base 2 */
833 if (sscanf(ptr
, "%d%n", &val1
, &pos
) == 1)
835 if (val1
>= 0x10000000)
837 else if (val1
>= 0x200000)
839 else if (val1
>= 0x4000)
841 else if (val1
>= 0x80)
851 SetLastError(CRYPT_E_ASN1_ERROR
);
855 CRYPT_EncodeLen(bytesNeeded
, NULL
, &lenBytes
);
859 bytesNeeded
+= 1 + lenBytes
;
862 if (*pcbEncoded
< bytesNeeded
)
864 SetLastError(ERROR_MORE_DATA
);
869 *pbEncoded
++ = ASN_OBJECTIDENTIFIER
;
870 CRYPT_EncodeLen(bytesNeeded
- 1 - lenBytes
, pbEncoded
, &lenBytes
);
871 pbEncoded
+= lenBytes
;
877 *pbEncoded
++ = firstByte
;
878 ptr
= pszObjId
+ firstPos
;
881 sscanf(ptr
, "%d%n", &val
, &pos
);
883 unsigned char outBytes
[5];
886 if (val
>= 0x10000000)
888 else if (val
>= 0x200000)
890 else if (val
>= 0x4000)
892 else if (val
>= 0x80)
896 for (i
= numBytes
; i
> 0; i
--)
898 outBytes
[i
- 1] = val
& 0x7f;
901 for (i
= 0; i
< numBytes
- 1; i
++)
902 *pbEncoded
++ = outBytes
[i
] | 0x80;
903 *pbEncoded
++ = outBytes
[i
];
912 *pcbEncoded
= bytesNeeded
;
916 static BOOL
CRYPT_AsnEncodeStringCoerce(const CERT_NAME_VALUE
*value
,
917 BYTE tag
, DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
921 LPCSTR str
= (LPCSTR
)value
->Value
.pbData
;
922 DWORD bytesNeeded
, lenBytes
, encodedLen
;
924 encodedLen
= value
->Value
.cbData
? value
->Value
.cbData
: lstrlenA(str
);
925 CRYPT_EncodeLen(encodedLen
, NULL
, &lenBytes
);
926 bytesNeeded
= 1 + lenBytes
+ encodedLen
;
928 *pcbEncoded
= bytesNeeded
;
931 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
932 pbEncoded
, pcbEncoded
, bytesNeeded
)))
934 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
935 pbEncoded
= *(BYTE
**)pbEncoded
;
937 CRYPT_EncodeLen(encodedLen
, pbEncoded
, &lenBytes
);
938 pbEncoded
+= lenBytes
;
939 memcpy(pbEncoded
, str
, encodedLen
);
945 static BOOL
CRYPT_AsnEncodeBMPString(const CERT_NAME_VALUE
*value
,
946 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
950 LPCWSTR str
= (LPCWSTR
)value
->Value
.pbData
;
951 DWORD bytesNeeded
, lenBytes
, strLen
;
953 strLen
= value
->Value
.cbData
? value
->Value
.cbData
/ sizeof(WCHAR
) :
955 CRYPT_EncodeLen(strLen
* 2, NULL
, &lenBytes
);
956 bytesNeeded
= 1 + lenBytes
+ strLen
* 2;
958 *pcbEncoded
= bytesNeeded
;
961 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
962 pbEncoded
, pcbEncoded
, bytesNeeded
)))
966 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
967 pbEncoded
= *(BYTE
**)pbEncoded
;
968 *pbEncoded
++ = ASN_BMPSTRING
;
969 CRYPT_EncodeLen(strLen
* 2, pbEncoded
, &lenBytes
);
970 pbEncoded
+= lenBytes
;
971 for (i
= 0; i
< strLen
; i
++)
973 *pbEncoded
++ = (str
[i
] & 0xff00) >> 8;
974 *pbEncoded
++ = str
[i
] & 0x00ff;
981 static BOOL
CRYPT_AsnEncodeUTF8String(const CERT_NAME_VALUE
*value
,
982 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
986 LPCWSTR str
= (LPCWSTR
)value
->Value
.pbData
;
987 DWORD bytesNeeded
, lenBytes
, encodedLen
, strLen
;
989 strLen
= value
->Value
.cbData
? value
->Value
.cbData
/ sizeof(WCHAR
) :
991 encodedLen
= WideCharToMultiByte(CP_UTF8
, 0, str
, strLen
, NULL
, 0, NULL
,
993 CRYPT_EncodeLen(encodedLen
, NULL
, &lenBytes
);
994 bytesNeeded
= 1 + lenBytes
+ encodedLen
;
996 *pcbEncoded
= bytesNeeded
;
999 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1000 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1002 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1003 pbEncoded
= *(BYTE
**)pbEncoded
;
1004 *pbEncoded
++ = ASN_UTF8STRING
;
1005 CRYPT_EncodeLen(encodedLen
, pbEncoded
, &lenBytes
);
1006 pbEncoded
+= lenBytes
;
1007 WideCharToMultiByte(CP_UTF8
, 0, str
, strLen
, (LPSTR
)pbEncoded
,
1008 bytesNeeded
- lenBytes
- 1, NULL
, NULL
);
1014 static BOOL WINAPI
CRYPT_AsnEncodeNameValue(DWORD dwCertEncodingType
,
1015 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1016 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1022 const CERT_NAME_VALUE
*value
= (CERT_NAME_VALUE
*)pvStructInfo
;
1024 switch (value
->dwValueType
)
1026 case CERT_RDN_ANY_TYPE
:
1027 /* explicitly disallowed */
1028 SetLastError(E_INVALIDARG
);
1031 case CERT_RDN_ENCODED_BLOB
:
1032 ret
= CRYPT_CopyEncodedBlob(dwCertEncodingType
, NULL
,
1033 &value
->Value
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1035 case CERT_RDN_OCTET_STRING
:
1036 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_OCTETSTRING
,
1037 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1039 case CERT_RDN_NUMERIC_STRING
:
1040 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_NUMERICSTRING
,
1041 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1043 case CERT_RDN_PRINTABLE_STRING
:
1044 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_PRINTABLESTRING
,
1045 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1047 case CERT_RDN_TELETEX_STRING
:
1048 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_T61STRING
,
1049 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1051 case CERT_RDN_VIDEOTEX_STRING
:
1052 ret
= CRYPT_AsnEncodeStringCoerce(value
,
1053 ASN_VIDEOTEXSTRING
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1055 case CERT_RDN_IA5_STRING
:
1056 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_IA5STRING
,
1057 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1059 case CERT_RDN_GRAPHIC_STRING
:
1060 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_GRAPHICSTRING
,
1061 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1063 case CERT_RDN_VISIBLE_STRING
:
1064 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_VISIBLESTRING
,
1065 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1067 case CERT_RDN_GENERAL_STRING
:
1068 ret
= CRYPT_AsnEncodeStringCoerce(value
, ASN_GENERALSTRING
,
1069 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1071 case CERT_RDN_UNIVERSAL_STRING
:
1072 FIXME("CERT_RDN_UNIVERSAL_STRING: unimplemented\n");
1073 SetLastError(CRYPT_E_ASN1_CHOICE
);
1076 case CERT_RDN_BMP_STRING
:
1077 ret
= CRYPT_AsnEncodeBMPString(value
, dwFlags
, pEncodePara
,
1078 pbEncoded
, pcbEncoded
);
1080 case CERT_RDN_UTF8_STRING
:
1081 ret
= CRYPT_AsnEncodeUTF8String(value
, dwFlags
, pEncodePara
,
1082 pbEncoded
, pcbEncoded
);
1085 SetLastError(CRYPT_E_ASN1_CHOICE
);
1091 SetLastError(STATUS_ACCESS_VIOLATION
);
1098 static BOOL WINAPI
CRYPT_AsnEncodeRdnAttr(DWORD dwCertEncodingType
,
1099 CERT_RDN_ATTR
*attr
, CryptEncodeObjectExFunc nameValueEncodeFunc
,
1100 BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1102 DWORD bytesNeeded
= 0, lenBytes
, size
;
1105 ret
= CRYPT_AsnEncodeOid(dwCertEncodingType
, NULL
, attr
->pszObjId
,
1106 0, NULL
, NULL
, &size
);
1109 bytesNeeded
+= size
;
1110 /* hack: a CERT_RDN_ATTR is identical to a CERT_NAME_VALUE beginning
1111 * with dwValueType, so "cast" it to get its encoded size
1113 ret
= nameValueEncodeFunc(dwCertEncodingType
, NULL
,
1114 (CERT_NAME_VALUE
*)&attr
->dwValueType
, 0, NULL
, NULL
, &size
);
1117 bytesNeeded
+= size
;
1118 CRYPT_EncodeLen(bytesNeeded
, NULL
, &lenBytes
);
1119 bytesNeeded
+= 1 + lenBytes
;
1122 if (*pcbEncoded
< bytesNeeded
)
1124 SetLastError(ERROR_MORE_DATA
);
1129 *pbEncoded
++ = ASN_SEQUENCE
;
1130 CRYPT_EncodeLen(bytesNeeded
- lenBytes
- 1, pbEncoded
,
1132 pbEncoded
+= lenBytes
;
1133 size
= bytesNeeded
- 1 - lenBytes
;
1134 ret
= CRYPT_AsnEncodeOid(dwCertEncodingType
, NULL
,
1135 attr
->pszObjId
, 0, NULL
, pbEncoded
, &size
);
1139 size
= bytesNeeded
- 1 - lenBytes
- size
;
1140 ret
= nameValueEncodeFunc(dwCertEncodingType
,
1141 NULL
, (CERT_NAME_VALUE
*)&attr
->dwValueType
,
1142 0, NULL
, pbEncoded
, &size
);
1149 *pcbEncoded
= bytesNeeded
;
1153 /* Have to propagate index of failing character */
1160 static int BLOBComp(const void *l
, const void *r
)
1162 CRYPT_DER_BLOB
*a
= (CRYPT_DER_BLOB
*)l
, *b
= (CRYPT_DER_BLOB
*)r
;
1165 if (!(ret
= memcmp(a
->pbData
, b
->pbData
, min(a
->cbData
, b
->cbData
))))
1166 ret
= a
->cbData
- b
->cbData
;
1170 /* This encodes as a SET OF, which in DER must be lexicographically sorted.
1172 static BOOL WINAPI
CRYPT_AsnEncodeRdn(DWORD dwCertEncodingType
, CERT_RDN
*rdn
,
1173 CryptEncodeObjectExFunc nameValueEncodeFunc
, BYTE
*pbEncoded
,
1177 CRYPT_DER_BLOB
*blobs
= NULL
;
1181 DWORD bytesNeeded
= 0, lenBytes
, i
;
1187 blobs
= CryptMemAlloc(rdn
->cRDNAttr
* sizeof(CRYPT_DER_BLOB
));
1191 memset(blobs
, 0, rdn
->cRDNAttr
* sizeof(CRYPT_DER_BLOB
));
1193 for (i
= 0; ret
&& i
< rdn
->cRDNAttr
; i
++)
1195 ret
= CRYPT_AsnEncodeRdnAttr(dwCertEncodingType
, &rdn
->rgRDNAttr
[i
],
1196 nameValueEncodeFunc
, NULL
, &blobs
[i
].cbData
);
1198 bytesNeeded
+= blobs
[i
].cbData
;
1201 /* Have to propagate index of failing character */
1202 *pcbEncoded
= blobs
[i
].cbData
;
1207 CRYPT_EncodeLen(bytesNeeded
, NULL
, &lenBytes
);
1208 bytesNeeded
+= 1 + lenBytes
;
1211 if (*pcbEncoded
< bytesNeeded
)
1213 SetLastError(ERROR_MORE_DATA
);
1218 for (i
= 0; ret
&& i
< rdn
->cRDNAttr
; i
++)
1220 blobs
[i
].pbData
= CryptMemAlloc(blobs
[i
].cbData
);
1221 if (!blobs
[i
].pbData
)
1225 ret
= CRYPT_AsnEncodeRdnAttr(dwCertEncodingType
,
1226 &rdn
->rgRDNAttr
[i
], nameValueEncodeFunc
,
1227 blobs
[i
].pbData
, &blobs
[i
].cbData
);
1229 *pcbEncoded
= blobs
[i
].cbData
;
1234 qsort(blobs
, rdn
->cRDNAttr
, sizeof(CRYPT_DER_BLOB
),
1236 *pbEncoded
++ = ASN_CONSTRUCTOR
| ASN_SETOF
;
1237 CRYPT_EncodeLen(bytesNeeded
- lenBytes
- 1, pbEncoded
,
1239 pbEncoded
+= lenBytes
;
1240 for (i
= 0; ret
&& i
< rdn
->cRDNAttr
; i
++)
1242 memcpy(pbEncoded
, blobs
[i
].pbData
, blobs
[i
].cbData
);
1243 pbEncoded
+= blobs
[i
].cbData
;
1249 *pcbEncoded
= bytesNeeded
;
1253 for (i
= 0; i
< rdn
->cRDNAttr
; i
++)
1254 CryptMemFree(blobs
[i
].pbData
);
1259 SetLastError(STATUS_ACCESS_VIOLATION
);
1263 CryptMemFree(blobs
);
1267 static BOOL WINAPI
CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType
,
1268 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1269 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
);
1271 static BOOL WINAPI
CRYPT_AsnEncodeOrCopyUnicodeNameValue(
1272 DWORD dwCertEncodingType
, LPCSTR lpszStructType
, const void *pvStructInfo
,
1273 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
1276 const CERT_NAME_VALUE
*value
= (const CERT_NAME_VALUE
*)pvStructInfo
;
1279 if (value
->dwValueType
== CERT_RDN_ENCODED_BLOB
)
1280 ret
= CRYPT_CopyEncodedBlob(dwCertEncodingType
, NULL
, &value
->Value
,
1281 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1283 ret
= CRYPT_AsnEncodeUnicodeNameValue(dwCertEncodingType
, NULL
, value
,
1284 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1288 static BOOL WINAPI
CRYPT_AsnEncodeUnicodeName(DWORD dwCertEncodingType
,
1289 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1290 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1296 const CERT_NAME_INFO
*info
= (const CERT_NAME_INFO
*)pvStructInfo
;
1297 DWORD bytesNeeded
= 0, lenBytes
, size
, i
;
1299 TRACE("encoding name with %ld RDNs\n", info
->cRDN
);
1301 for (i
= 0; ret
&& i
< info
->cRDN
; i
++)
1303 ret
= CRYPT_AsnEncodeRdn(dwCertEncodingType
, &info
->rgRDN
[i
],
1304 CRYPT_AsnEncodeOrCopyUnicodeNameValue
, NULL
, &size
);
1306 bytesNeeded
+= size
;
1310 CRYPT_EncodeLen(bytesNeeded
, NULL
, &lenBytes
);
1311 bytesNeeded
+= 1 + lenBytes
;
1315 *pcbEncoded
= bytesNeeded
;
1318 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1319 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1321 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1322 pbEncoded
= *(BYTE
**)pbEncoded
;
1323 *pbEncoded
++ = ASN_SEQUENCEOF
;
1324 CRYPT_EncodeLen(bytesNeeded
- lenBytes
- 1, pbEncoded
,
1326 pbEncoded
+= lenBytes
;
1327 for (i
= 0; ret
&& i
< info
->cRDN
; i
++)
1330 ret
= CRYPT_AsnEncodeRdn(dwCertEncodingType
,
1331 &info
->rgRDN
[i
], CRYPT_AsnEncodeOrCopyUnicodeNameValue
,
1336 bytesNeeded
-= size
;
1347 SetLastError(STATUS_ACCESS_VIOLATION
);
1354 static BOOL
CRYPT_AsnEncodeUnicodeStringCoerce(const CERT_NAME_VALUE
*value
,
1355 BYTE tag
, DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
1359 LPCWSTR str
= (LPCWSTR
)value
->Value
.pbData
;
1360 DWORD bytesNeeded
, lenBytes
, encodedLen
;
1362 encodedLen
= value
->Value
.cbData
? value
->Value
.cbData
/ sizeof(WCHAR
) :
1364 CRYPT_EncodeLen(encodedLen
, NULL
, &lenBytes
);
1365 bytesNeeded
= 1 + lenBytes
+ encodedLen
;
1367 *pcbEncoded
= bytesNeeded
;
1370 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1371 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1375 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1376 pbEncoded
= *(BYTE
**)pbEncoded
;
1378 CRYPT_EncodeLen(encodedLen
, pbEncoded
, &lenBytes
);
1379 pbEncoded
+= lenBytes
;
1380 for (i
= 0; i
< encodedLen
; i
++)
1381 *pbEncoded
++ = (BYTE
)str
[i
];
1387 static BOOL
CRYPT_AsnEncodeNumericString(const CERT_NAME_VALUE
*value
,
1388 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
1392 LPCWSTR str
= (LPCWSTR
)value
->Value
.pbData
;
1393 DWORD bytesNeeded
, lenBytes
, encodedLen
;
1395 encodedLen
= value
->Value
.cbData
? value
->Value
.cbData
/ sizeof(WCHAR
) :
1397 CRYPT_EncodeLen(encodedLen
, NULL
, &lenBytes
);
1398 bytesNeeded
= 1 + lenBytes
+ encodedLen
;
1400 *pcbEncoded
= bytesNeeded
;
1403 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1404 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1408 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1409 pbEncoded
= *(BYTE
**)pbEncoded
;
1410 *pbEncoded
++ = ASN_NUMERICSTRING
;
1411 CRYPT_EncodeLen(encodedLen
, pbEncoded
, &lenBytes
);
1412 pbEncoded
+= lenBytes
;
1413 for (i
= 0; ret
&& i
< encodedLen
; i
++)
1415 if (isdigitW(str
[i
]))
1416 *pbEncoded
++ = (BYTE
)str
[i
];
1420 SetLastError(CRYPT_E_INVALID_NUMERIC_STRING
);
1429 static inline int isprintableW(WCHAR wc
)
1431 return isalnumW(wc
) || isspaceW(wc
) || wc
== '\'' || wc
== '(' ||
1432 wc
== ')' || wc
== '+' || wc
== ',' || wc
== '-' || wc
== '.' ||
1433 wc
== '/' || wc
== ':' || wc
== '=' || wc
== '?';
1436 static BOOL
CRYPT_AsnEncodePrintableString(const CERT_NAME_VALUE
*value
,
1437 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
1441 LPCWSTR str
= (LPCWSTR
)value
->Value
.pbData
;
1442 DWORD bytesNeeded
, lenBytes
, encodedLen
;
1444 encodedLen
= value
->Value
.cbData
? value
->Value
.cbData
/ sizeof(WCHAR
) :
1446 CRYPT_EncodeLen(encodedLen
, NULL
, &lenBytes
);
1447 bytesNeeded
= 1 + lenBytes
+ encodedLen
;
1449 *pcbEncoded
= bytesNeeded
;
1452 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1453 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1457 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1458 pbEncoded
= *(BYTE
**)pbEncoded
;
1459 *pbEncoded
++ = ASN_PRINTABLESTRING
;
1460 CRYPT_EncodeLen(encodedLen
, pbEncoded
, &lenBytes
);
1461 pbEncoded
+= lenBytes
;
1462 for (i
= 0; ret
&& i
< encodedLen
; i
++)
1464 if (isprintableW(str
[i
]))
1465 *pbEncoded
++ = (BYTE
)str
[i
];
1469 SetLastError(CRYPT_E_INVALID_PRINTABLE_STRING
);
1478 static BOOL
CRYPT_AsnEncodeIA5String(const CERT_NAME_VALUE
*value
,
1479 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
1483 LPCWSTR str
= (LPCWSTR
)value
->Value
.pbData
;
1484 DWORD bytesNeeded
, lenBytes
, encodedLen
;
1486 encodedLen
= value
->Value
.cbData
? value
->Value
.cbData
/ sizeof(WCHAR
) :
1488 CRYPT_EncodeLen(encodedLen
, NULL
, &lenBytes
);
1489 bytesNeeded
= 1 + lenBytes
+ encodedLen
;
1491 *pcbEncoded
= bytesNeeded
;
1494 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1495 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1499 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1500 pbEncoded
= *(BYTE
**)pbEncoded
;
1501 *pbEncoded
++ = ASN_IA5STRING
;
1502 CRYPT_EncodeLen(encodedLen
, pbEncoded
, &lenBytes
);
1503 pbEncoded
+= lenBytes
;
1504 for (i
= 0; ret
&& i
< encodedLen
; i
++)
1507 *pbEncoded
++ = (BYTE
)str
[i
];
1511 SetLastError(CRYPT_E_INVALID_IA5_STRING
);
1520 static BOOL
CRYPT_AsnEncodeUniversalString(const CERT_NAME_VALUE
*value
,
1521 DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
,
1525 LPCWSTR str
= (LPCWSTR
)value
->Value
.pbData
;
1526 DWORD bytesNeeded
, lenBytes
, strLen
;
1528 /* FIXME: doesn't handle composite characters */
1529 strLen
= value
->Value
.cbData
? value
->Value
.cbData
/ sizeof(WCHAR
) :
1531 CRYPT_EncodeLen(strLen
* 4, NULL
, &lenBytes
);
1532 bytesNeeded
= 1 + lenBytes
+ strLen
* 4;
1534 *pcbEncoded
= bytesNeeded
;
1537 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1538 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1542 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1543 pbEncoded
= *(BYTE
**)pbEncoded
;
1544 *pbEncoded
++ = ASN_UNIVERSALSTRING
;
1545 CRYPT_EncodeLen(strLen
* 4, pbEncoded
, &lenBytes
);
1546 pbEncoded
+= lenBytes
;
1547 for (i
= 0; i
< strLen
; i
++)
1551 *pbEncoded
++ = (BYTE
)((str
[i
] & 0xff00) >> 8);
1552 *pbEncoded
++ = (BYTE
)(str
[i
] & 0x00ff);
1559 static BOOL WINAPI
CRYPT_AsnEncodeUnicodeNameValue(DWORD dwCertEncodingType
,
1560 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1561 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1567 const CERT_NAME_VALUE
*value
= (CERT_NAME_VALUE
*)pvStructInfo
;
1569 switch (value
->dwValueType
)
1571 case CERT_RDN_ANY_TYPE
:
1572 case CERT_RDN_ENCODED_BLOB
:
1573 case CERT_RDN_OCTET_STRING
:
1574 SetLastError(CRYPT_E_NOT_CHAR_STRING
);
1576 case CERT_RDN_NUMERIC_STRING
:
1577 ret
= CRYPT_AsnEncodeNumericString(value
, dwFlags
, pEncodePara
,
1578 pbEncoded
, pcbEncoded
);
1580 case CERT_RDN_PRINTABLE_STRING
:
1581 ret
= CRYPT_AsnEncodePrintableString(value
, dwFlags
, pEncodePara
,
1582 pbEncoded
, pcbEncoded
);
1584 case CERT_RDN_TELETEX_STRING
:
1585 ret
= CRYPT_AsnEncodeUnicodeStringCoerce(value
, ASN_T61STRING
,
1586 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1588 case CERT_RDN_VIDEOTEX_STRING
:
1589 ret
= CRYPT_AsnEncodeUnicodeStringCoerce(value
,
1590 ASN_VIDEOTEXSTRING
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1592 case CERT_RDN_IA5_STRING
:
1593 ret
= CRYPT_AsnEncodeIA5String(value
, dwFlags
, pEncodePara
,
1594 pbEncoded
, pcbEncoded
);
1596 case CERT_RDN_GRAPHIC_STRING
:
1597 ret
= CRYPT_AsnEncodeUnicodeStringCoerce(value
, ASN_GRAPHICSTRING
,
1598 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1600 case CERT_RDN_VISIBLE_STRING
:
1601 ret
= CRYPT_AsnEncodeUnicodeStringCoerce(value
, ASN_VISIBLESTRING
,
1602 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1604 case CERT_RDN_GENERAL_STRING
:
1605 ret
= CRYPT_AsnEncodeUnicodeStringCoerce(value
, ASN_GENERALSTRING
,
1606 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1608 case CERT_RDN_UNIVERSAL_STRING
:
1609 ret
= CRYPT_AsnEncodeUniversalString(value
, dwFlags
, pEncodePara
,
1610 pbEncoded
, pcbEncoded
);
1612 case CERT_RDN_BMP_STRING
:
1613 ret
= CRYPT_AsnEncodeBMPString(value
, dwFlags
, pEncodePara
,
1614 pbEncoded
, pcbEncoded
);
1616 case CERT_RDN_UTF8_STRING
:
1617 ret
= CRYPT_AsnEncodeUTF8String(value
, dwFlags
, pEncodePara
,
1618 pbEncoded
, pcbEncoded
);
1621 SetLastError(CRYPT_E_ASN1_CHOICE
);
1626 SetLastError(STATUS_ACCESS_VIOLATION
);
1632 static BOOL WINAPI
CRYPT_AsnEncodeName(DWORD dwCertEncodingType
,
1633 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1634 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1640 const CERT_NAME_INFO
*info
= (const CERT_NAME_INFO
*)pvStructInfo
;
1641 DWORD bytesNeeded
= 0, lenBytes
, size
, i
;
1643 TRACE("encoding name with %ld RDNs\n", info
->cRDN
);
1645 for (i
= 0; ret
&& i
< info
->cRDN
; i
++)
1647 ret
= CRYPT_AsnEncodeRdn(dwCertEncodingType
, &info
->rgRDN
[i
],
1648 CRYPT_AsnEncodeNameValue
, NULL
, &size
);
1650 bytesNeeded
+= size
;
1652 CRYPT_EncodeLen(bytesNeeded
, NULL
, &lenBytes
);
1653 bytesNeeded
+= 1 + lenBytes
;
1657 *pcbEncoded
= bytesNeeded
;
1660 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1661 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1663 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1664 pbEncoded
= *(BYTE
**)pbEncoded
;
1665 *pbEncoded
++ = ASN_SEQUENCEOF
;
1666 CRYPT_EncodeLen(bytesNeeded
- lenBytes
- 1, pbEncoded
,
1668 pbEncoded
+= lenBytes
;
1669 for (i
= 0; ret
&& i
< info
->cRDN
; i
++)
1672 ret
= CRYPT_AsnEncodeRdn(dwCertEncodingType
,
1673 &info
->rgRDN
[i
], CRYPT_AsnEncodeNameValue
, pbEncoded
,
1678 bytesNeeded
-= size
;
1687 SetLastError(STATUS_ACCESS_VIOLATION
);
1694 static BOOL WINAPI
CRYPT_AsnEncodeBool(DWORD dwCertEncodingType
,
1695 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1696 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1698 BOOL val
= *(const BOOL
*)pvStructInfo
, ret
;
1707 else if (*pcbEncoded
< 3)
1710 SetLastError(ERROR_MORE_DATA
);
1716 *pbEncoded
++ = ASN_BOOL
;
1718 *pbEncoded
++ = val
? 0xff : 0;
1721 TRACE("returning %d (%08lx)\n", ret
, GetLastError());
1725 static BOOL
CRYPT_AsnEncodeAltNameEntry(const CERT_ALT_NAME_ENTRY
*entry
,
1726 BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1732 switch (entry
->dwAltNameChoice
)
1734 case CERT_ALT_NAME_RFC822_NAME
:
1735 case CERT_ALT_NAME_DNS_NAME
:
1736 case CERT_ALT_NAME_URL
:
1737 if (entry
->u
.pwszURL
)
1741 /* Not + 1: don't encode the NULL-terminator */
1742 dataLen
= lstrlenW(entry
->u
.pwszURL
);
1743 for (i
= 0; ret
&& i
< dataLen
; i
++)
1745 if (entry
->u
.pwszURL
[i
] > 0x7f)
1747 SetLastError(CRYPT_E_INVALID_IA5_STRING
);
1756 case CERT_ALT_NAME_IP_ADDRESS
:
1757 dataLen
= entry
->u
.IPAddress
.cbData
;
1759 case CERT_ALT_NAME_REGISTERED_ID
:
1760 /* FIXME: encode OID */
1761 case CERT_ALT_NAME_OTHER_NAME
:
1762 case CERT_ALT_NAME_DIRECTORY_NAME
:
1763 FIXME("name type %ld unimplemented\n", entry
->dwAltNameChoice
);
1766 SetLastError(E_INVALIDARG
);
1771 DWORD bytesNeeded
, lenBytes
;
1773 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
1774 bytesNeeded
= 1 + dataLen
+ lenBytes
;
1776 *pcbEncoded
= bytesNeeded
;
1777 else if (*pcbEncoded
< bytesNeeded
)
1779 SetLastError(ERROR_MORE_DATA
);
1780 *pcbEncoded
= bytesNeeded
;
1785 *pbEncoded
++ = ASN_CONTEXT
| (entry
->dwAltNameChoice
- 1);
1786 CRYPT_EncodeLen(dataLen
, pbEncoded
, &lenBytes
);
1787 pbEncoded
+= lenBytes
;
1788 switch (entry
->dwAltNameChoice
)
1790 case CERT_ALT_NAME_RFC822_NAME
:
1791 case CERT_ALT_NAME_DNS_NAME
:
1792 case CERT_ALT_NAME_URL
:
1796 for (i
= 0; i
< dataLen
; i
++)
1797 *pbEncoded
++ = (BYTE
)entry
->u
.pwszURL
[i
];
1800 case CERT_ALT_NAME_IP_ADDRESS
:
1801 memcpy(pbEncoded
, entry
->u
.IPAddress
.pbData
, dataLen
);
1805 *pcbEncoded
= bytesNeeded
;
1808 TRACE("returning %d (%08lx)\n", ret
, GetLastError());
1812 static BOOL WINAPI
CRYPT_AsnEncodeAltName(DWORD dwCertEncodingType
,
1813 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1814 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1820 const CERT_ALT_NAME_INFO
*info
=
1821 (const CERT_ALT_NAME_INFO
*)pvStructInfo
;
1822 DWORD bytesNeeded
, dataLen
, lenBytes
, i
;
1825 /* FIXME: should check that cAltEntry is not bigger than 0xff, since we
1826 * can't encode an erroneous entry index if it's bigger than this.
1828 for (i
= 0, dataLen
= 0; ret
&& i
< info
->cAltEntry
; i
++)
1832 ret
= CRYPT_AsnEncodeAltNameEntry(&info
->rgAltEntry
[i
], NULL
,
1836 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
1838 /* CRYPT_AsnEncodeAltNameEntry encoded the index of
1839 * the bad character, now set the index of the bad
1842 *pcbEncoded
= (BYTE
)i
<<
1843 CERT_ALT_NAME_ENTRY_ERR_INDEX_SHIFT
| len
;
1848 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
1849 bytesNeeded
= 1 + lenBytes
+ dataLen
;
1852 *pcbEncoded
= bytesNeeded
;
1857 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
1858 pbEncoded
, pcbEncoded
, bytesNeeded
)))
1860 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
1861 pbEncoded
= *(BYTE
**)pbEncoded
;
1862 *pbEncoded
++ = ASN_SEQUENCEOF
;
1863 CRYPT_EncodeLen(dataLen
, pbEncoded
, &lenBytes
);
1864 pbEncoded
+= lenBytes
;
1865 for (i
= 0; ret
&& i
< info
->cAltEntry
; i
++)
1867 DWORD len
= dataLen
;
1869 ret
= CRYPT_AsnEncodeAltNameEntry(&info
->rgAltEntry
[i
],
1883 SetLastError(STATUS_ACCESS_VIOLATION
);
1890 static BOOL WINAPI
CRYPT_AsnEncodeBasicConstraints(DWORD dwCertEncodingType
,
1891 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1892 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1898 const CERT_BASIC_CONSTRAINTS_INFO
*info
=
1899 (const CERT_BASIC_CONSTRAINTS_INFO
*)pvStructInfo
;
1900 struct AsnEncodeSequenceItem items
[3] = {
1901 { &info
->SubjectType
, CRYPT_AsnEncodeBits
, 0 },
1906 if (info
->fPathLenConstraint
)
1908 items
[cItem
].pvStructInfo
= &info
->dwPathLenConstraint
;
1909 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeInt
;
1912 if (info
->cSubtreesConstraint
)
1914 items
[cItem
].pvStructInfo
= &info
->cSubtreesConstraint
;
1915 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeSequenceOfAny
;
1918 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
, cItem
,
1919 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1923 SetLastError(STATUS_ACCESS_VIOLATION
);
1930 static BOOL WINAPI
CRYPT_AsnEncodeBasicConstraints2(DWORD dwCertEncodingType
,
1931 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1932 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1938 const CERT_BASIC_CONSTRAINTS2_INFO
*info
=
1939 (const CERT_BASIC_CONSTRAINTS2_INFO
*)pvStructInfo
;
1940 struct AsnEncodeSequenceItem items
[2] = { { 0 } };
1945 items
[cItem
].pvStructInfo
= &info
->fCA
;
1946 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeBool
;
1949 if (info
->fPathLenConstraint
)
1951 items
[cItem
].pvStructInfo
= &info
->dwPathLenConstraint
;
1952 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeInt
;
1955 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
, cItem
,
1956 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
1960 SetLastError(STATUS_ACCESS_VIOLATION
);
1967 static BOOL WINAPI
CRYPT_AsnEncodeRsaPubKey(DWORD dwCertEncodingType
,
1968 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
1969 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1975 const BLOBHEADER
*hdr
=
1976 (const BLOBHEADER
*)pvStructInfo
;
1978 if (hdr
->bType
!= PUBLICKEYBLOB
)
1980 SetLastError(E_INVALIDARG
);
1985 const RSAPUBKEY
*rsaPubKey
= (const RSAPUBKEY
*)
1986 ((const BYTE
*)pvStructInfo
+ sizeof(BLOBHEADER
));
1987 CRYPT_INTEGER_BLOB blob
= { rsaPubKey
->bitlen
/ 8,
1988 (BYTE
*)pvStructInfo
+ sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) };
1989 struct AsnEncodeSequenceItem items
[] = {
1990 { &blob
, CRYPT_AsnEncodeUnsignedInteger
, 0 },
1991 { &rsaPubKey
->pubexp
, CRYPT_AsnEncodeInt
, 0 },
1994 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
,
1995 sizeof(items
) / sizeof(items
[0]), dwFlags
, pEncodePara
, pbEncoded
,
2001 SetLastError(STATUS_ACCESS_VIOLATION
);
2008 static BOOL WINAPI
CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType
,
2009 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2010 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2016 const CRYPT_DATA_BLOB
*blob
= (const CRYPT_DATA_BLOB
*)pvStructInfo
;
2017 DWORD bytesNeeded
, lenBytes
;
2019 TRACE("(%ld, %p), %08lx, %p, %p, %ld\n", blob
->cbData
, blob
->pbData
,
2020 dwFlags
, pEncodePara
, pbEncoded
, *pcbEncoded
);
2022 CRYPT_EncodeLen(blob
->cbData
, NULL
, &lenBytes
);
2023 bytesNeeded
= 1 + lenBytes
+ blob
->cbData
;
2026 *pcbEncoded
= bytesNeeded
;
2031 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
2032 pcbEncoded
, bytesNeeded
)))
2034 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2035 pbEncoded
= *(BYTE
**)pbEncoded
;
2036 *pbEncoded
++ = ASN_OCTETSTRING
;
2037 CRYPT_EncodeLen(blob
->cbData
, pbEncoded
, &lenBytes
);
2038 pbEncoded
+= lenBytes
;
2040 memcpy(pbEncoded
, blob
->pbData
, blob
->cbData
);
2046 SetLastError(STATUS_ACCESS_VIOLATION
);
2050 TRACE("returning %d (%08lx)\n", ret
, GetLastError());
2054 static BOOL WINAPI
CRYPT_AsnEncodeBits(DWORD dwCertEncodingType
,
2055 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2056 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2062 const CRYPT_BIT_BLOB
*blob
= (const CRYPT_BIT_BLOB
*)pvStructInfo
;
2063 DWORD bytesNeeded
, lenBytes
, dataBytes
;
2066 /* yep, MS allows cUnusedBits to be >= 8 */
2067 if (!blob
->cUnusedBits
)
2069 dataBytes
= blob
->cbData
;
2072 else if (blob
->cbData
* 8 > blob
->cUnusedBits
)
2074 dataBytes
= (blob
->cbData
* 8 - blob
->cUnusedBits
) / 8 + 1;
2075 unusedBits
= blob
->cUnusedBits
>= 8 ? blob
->cUnusedBits
/ 8 :
2083 CRYPT_EncodeLen(dataBytes
+ 1, NULL
, &lenBytes
);
2084 bytesNeeded
= 1 + lenBytes
+ dataBytes
+ 1;
2087 *pcbEncoded
= bytesNeeded
;
2092 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
2093 pcbEncoded
, bytesNeeded
)))
2095 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2096 pbEncoded
= *(BYTE
**)pbEncoded
;
2097 *pbEncoded
++ = ASN_BITSTRING
;
2098 CRYPT_EncodeLen(dataBytes
+ 1, pbEncoded
, &lenBytes
);
2099 pbEncoded
+= lenBytes
;
2100 *pbEncoded
++ = unusedBits
;
2103 BYTE mask
= 0xff << unusedBits
;
2107 memcpy(pbEncoded
, blob
->pbData
, dataBytes
- 1);
2108 pbEncoded
+= dataBytes
- 1;
2110 *pbEncoded
= *(blob
->pbData
+ dataBytes
- 1) & mask
;
2117 SetLastError(STATUS_ACCESS_VIOLATION
);
2124 static BOOL WINAPI
CRYPT_AsnEncodeBitsSwapBytes(DWORD dwCertEncodingType
,
2125 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2126 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2132 const CRYPT_BIT_BLOB
*blob
= (const CRYPT_BIT_BLOB
*)pvStructInfo
;
2133 CRYPT_BIT_BLOB newBlob
= { blob
->cbData
, NULL
, blob
->cUnusedBits
};
2138 newBlob
.pbData
= CryptMemAlloc(newBlob
.cbData
);
2143 for (i
= 0; i
< newBlob
.cbData
; i
++)
2144 newBlob
.pbData
[newBlob
.cbData
- i
- 1] = blob
->pbData
[i
];
2150 ret
= CRYPT_AsnEncodeBits(dwCertEncodingType
, lpszStructType
,
2151 &newBlob
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
2152 CryptMemFree(newBlob
.pbData
);
2156 SetLastError(STATUS_ACCESS_VIOLATION
);
2163 static BOOL WINAPI
CRYPT_AsnEncodeInt(DWORD dwCertEncodingType
,
2164 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2165 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2167 CRYPT_INTEGER_BLOB blob
= { sizeof(INT
), (BYTE
*)pvStructInfo
};
2169 return CRYPT_AsnEncodeInteger(dwCertEncodingType
, X509_MULTI_BYTE_INTEGER
,
2170 &blob
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
2173 static BOOL WINAPI
CRYPT_AsnEncodeInteger(DWORD dwCertEncodingType
,
2174 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2175 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2181 DWORD significantBytes
, lenBytes
;
2182 BYTE padByte
= 0, bytesNeeded
;
2184 const CRYPT_INTEGER_BLOB
*blob
=
2185 (const CRYPT_INTEGER_BLOB
*)pvStructInfo
;
2187 significantBytes
= blob
->cbData
;
2188 if (significantBytes
)
2190 if (blob
->pbData
[significantBytes
- 1] & 0x80)
2192 /* negative, lop off leading (little-endian) 0xffs */
2193 for (; significantBytes
> 0 &&
2194 blob
->pbData
[significantBytes
- 1] == 0xff; significantBytes
--)
2196 if (blob
->pbData
[significantBytes
- 1] < 0x80)
2204 /* positive, lop off leading (little-endian) zeroes */
2205 for (; significantBytes
> 0 &&
2206 !blob
->pbData
[significantBytes
- 1]; significantBytes
--)
2208 if (significantBytes
== 0)
2209 significantBytes
= 1;
2210 if (blob
->pbData
[significantBytes
- 1] > 0x7f)
2218 CRYPT_EncodeLen(significantBytes
+ 1, NULL
, &lenBytes
);
2220 CRYPT_EncodeLen(significantBytes
, NULL
, &lenBytes
);
2221 bytesNeeded
= 1 + lenBytes
+ significantBytes
;
2226 *pcbEncoded
= bytesNeeded
;
2231 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
2232 pcbEncoded
, bytesNeeded
)))
2234 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2235 pbEncoded
= *(BYTE
**)pbEncoded
;
2236 *pbEncoded
++ = ASN_INTEGER
;
2239 CRYPT_EncodeLen(significantBytes
+ 1, pbEncoded
, &lenBytes
);
2240 pbEncoded
+= lenBytes
;
2241 *pbEncoded
++ = padByte
;
2245 CRYPT_EncodeLen(significantBytes
, pbEncoded
, &lenBytes
);
2246 pbEncoded
+= lenBytes
;
2248 for (; significantBytes
> 0; significantBytes
--)
2249 *(pbEncoded
++) = blob
->pbData
[significantBytes
- 1];
2255 SetLastError(STATUS_ACCESS_VIOLATION
);
2262 static BOOL WINAPI
CRYPT_AsnEncodeUnsignedInteger(DWORD dwCertEncodingType
,
2263 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2264 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2270 DWORD significantBytes
, lenBytes
;
2273 const CRYPT_INTEGER_BLOB
*blob
=
2274 (const CRYPT_INTEGER_BLOB
*)pvStructInfo
;
2276 significantBytes
= blob
->cbData
;
2277 if (significantBytes
)
2279 /* positive, lop off leading (little-endian) zeroes */
2280 for (; significantBytes
> 0 && !blob
->pbData
[significantBytes
- 1];
2283 if (significantBytes
== 0)
2284 significantBytes
= 1;
2285 if (blob
->pbData
[significantBytes
- 1] > 0x7f)
2289 CRYPT_EncodeLen(significantBytes
+ 1, NULL
, &lenBytes
);
2291 CRYPT_EncodeLen(significantBytes
, NULL
, &lenBytes
);
2292 bytesNeeded
= 1 + lenBytes
+ significantBytes
;
2297 *pcbEncoded
= bytesNeeded
;
2302 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
2303 pcbEncoded
, bytesNeeded
)))
2305 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2306 pbEncoded
= *(BYTE
**)pbEncoded
;
2307 *pbEncoded
++ = ASN_INTEGER
;
2310 CRYPT_EncodeLen(significantBytes
+ 1, pbEncoded
, &lenBytes
);
2311 pbEncoded
+= lenBytes
;
2316 CRYPT_EncodeLen(significantBytes
, pbEncoded
, &lenBytes
);
2317 pbEncoded
+= lenBytes
;
2319 for (; significantBytes
> 0; significantBytes
--)
2320 *(pbEncoded
++) = blob
->pbData
[significantBytes
- 1];
2326 SetLastError(STATUS_ACCESS_VIOLATION
);
2333 static BOOL WINAPI
CRYPT_AsnEncodeEnumerated(DWORD dwCertEncodingType
,
2334 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2335 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2337 CRYPT_INTEGER_BLOB blob
;
2340 /* Encode as an unsigned integer, then change the tag to enumerated */
2341 blob
.cbData
= sizeof(DWORD
);
2342 blob
.pbData
= (BYTE
*)pvStructInfo
;
2343 ret
= CRYPT_AsnEncodeUnsignedInteger(dwCertEncodingType
,
2344 X509_MULTI_BYTE_UINT
, &blob
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
2345 if (ret
&& pbEncoded
)
2347 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2348 pbEncoded
= *(BYTE
**)pbEncoded
;
2349 pbEncoded
[0] = ASN_ENUMERATED
;
2354 static BOOL WINAPI
CRYPT_AsnEncodeUtcTime(DWORD dwCertEncodingType
,
2355 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2356 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2363 /* sorry, magic number: enough for tag, len, YYMMDDHHMMSSZ\0. I use a
2364 * temporary buffer because the output buffer is not NULL-terminated.
2367 static const DWORD bytesNeeded
= sizeof(buf
) - 1;
2371 *pcbEncoded
= bytesNeeded
;
2376 /* Sanity check the year, this is a two-digit year format */
2377 ret
= FileTimeToSystemTime((const FILETIME
*)pvStructInfo
,
2379 if (ret
&& (sysTime
.wYear
< 1950 || sysTime
.wYear
> 2050))
2381 SetLastError(CRYPT_E_BAD_ENCODE
);
2386 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
2387 pbEncoded
, pcbEncoded
, bytesNeeded
)))
2389 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2390 pbEncoded
= *(BYTE
**)pbEncoded
;
2391 buf
[0] = ASN_UTCTIME
;
2392 buf
[1] = bytesNeeded
- 2;
2393 snprintf(buf
+ 2, sizeof(buf
) - 2,
2394 "%02d%02d%02d%02d%02d%02dZ", sysTime
.wYear
>= 2000 ?
2395 sysTime
.wYear
- 2000 : sysTime
.wYear
- 1900,
2396 sysTime
.wMonth
, sysTime
.wDay
, sysTime
.wHour
,
2397 sysTime
.wMinute
, sysTime
.wSecond
);
2398 memcpy(pbEncoded
, buf
, bytesNeeded
);
2405 SetLastError(STATUS_ACCESS_VIOLATION
);
2412 static BOOL WINAPI
CRYPT_AsnEncodeGeneralizedTime(DWORD dwCertEncodingType
,
2413 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2414 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2421 /* sorry, magic number: enough for tag, len, YYYYMMDDHHMMSSZ\0. I use a
2422 * temporary buffer because the output buffer is not NULL-terminated.
2425 static const DWORD bytesNeeded
= sizeof(buf
) - 1;
2429 *pcbEncoded
= bytesNeeded
;
2434 ret
= FileTimeToSystemTime((const FILETIME
*)pvStructInfo
,
2437 ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
2438 pcbEncoded
, bytesNeeded
);
2441 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2442 pbEncoded
= *(BYTE
**)pbEncoded
;
2443 buf
[0] = ASN_GENERALTIME
;
2444 buf
[1] = bytesNeeded
- 2;
2445 snprintf(buf
+ 2, sizeof(buf
) - 2, "%04d%02d%02d%02d%02d%02dZ",
2446 sysTime
.wYear
, sysTime
.wMonth
, sysTime
.wDay
, sysTime
.wHour
,
2447 sysTime
.wMinute
, sysTime
.wSecond
);
2448 memcpy(pbEncoded
, buf
, bytesNeeded
);
2454 SetLastError(STATUS_ACCESS_VIOLATION
);
2461 static BOOL WINAPI
CRYPT_AsnEncodeChoiceOfTime(DWORD dwCertEncodingType
,
2462 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2463 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2471 /* Check the year, if it's in the UTCTime range call that encode func */
2472 if (!FileTimeToSystemTime((const FILETIME
*)pvStructInfo
, &sysTime
))
2474 if (sysTime
.wYear
>= 1950 && sysTime
.wYear
<= 2050)
2475 ret
= CRYPT_AsnEncodeUtcTime(dwCertEncodingType
, lpszStructType
,
2476 pvStructInfo
, dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
2478 ret
= CRYPT_AsnEncodeGeneralizedTime(dwCertEncodingType
,
2479 lpszStructType
, pvStructInfo
, dwFlags
, pEncodePara
, pbEncoded
,
2484 SetLastError(STATUS_ACCESS_VIOLATION
);
2491 static BOOL WINAPI
CRYPT_AsnEncodeSequenceOfAny(DWORD dwCertEncodingType
,
2492 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2493 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2499 DWORD bytesNeeded
, dataLen
, lenBytes
, i
;
2500 const CRYPT_SEQUENCE_OF_ANY
*seq
=
2501 (const CRYPT_SEQUENCE_OF_ANY
*)pvStructInfo
;
2503 for (i
= 0, dataLen
= 0; i
< seq
->cValue
; i
++)
2504 dataLen
+= seq
->rgValue
[i
].cbData
;
2505 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
2506 bytesNeeded
= 1 + lenBytes
+ dataLen
;
2509 *pcbEncoded
= bytesNeeded
;
2514 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
, pbEncoded
,
2515 pcbEncoded
, bytesNeeded
)))
2517 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2518 pbEncoded
= *(BYTE
**)pbEncoded
;
2519 *pbEncoded
++ = ASN_SEQUENCEOF
;
2520 CRYPT_EncodeLen(dataLen
, pbEncoded
, &lenBytes
);
2521 pbEncoded
+= lenBytes
;
2522 for (i
= 0; i
< seq
->cValue
; i
++)
2524 memcpy(pbEncoded
, seq
->rgValue
[i
].pbData
,
2525 seq
->rgValue
[i
].cbData
);
2526 pbEncoded
+= seq
->rgValue
[i
].cbData
;
2533 SetLastError(STATUS_ACCESS_VIOLATION
);
2540 static BOOL
CRYPT_AsnEncodeDistPoint(const CRL_DIST_POINT
*distPoint
,
2541 BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2544 struct AsnEncodeSequenceItem items
[3] = { { 0 } };
2545 struct AsnConstructedItem constructed
= { 0 };
2546 struct AsnEncodeTagSwappedItem swapped
[3] = { { 0 } };
2547 DWORD cItem
= 0, cSwapped
= 0;
2549 switch (distPoint
->DistPointName
.dwDistPointNameChoice
)
2551 case CRL_DIST_POINT_NO_NAME
:
2554 case CRL_DIST_POINT_FULL_NAME
:
2555 swapped
[cSwapped
].tag
= ASN_CONTEXT
| ASN_CONSTRUCTOR
| 0;
2556 swapped
[cSwapped
].pvStructInfo
= &distPoint
->DistPointName
.u
.FullName
;
2557 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeAltName
;
2558 constructed
.tag
= 0;
2559 constructed
.pvStructInfo
= &swapped
[cSwapped
];
2560 constructed
.encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2561 items
[cItem
].pvStructInfo
= &constructed
;
2562 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeConstructed
;
2566 case CRL_DIST_POINT_ISSUER_RDN_NAME
:
2567 FIXME("unimplemented for CRL_DIST_POINT_ISSUER_RDN_NAME\n");
2573 if (ret
&& distPoint
->ReasonFlags
.cbData
)
2575 swapped
[cSwapped
].tag
= ASN_CONTEXT
| 1;
2576 swapped
[cSwapped
].pvStructInfo
= &distPoint
->ReasonFlags
;
2577 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeBits
;
2578 items
[cItem
].pvStructInfo
= &swapped
[cSwapped
];
2579 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2583 if (ret
&& distPoint
->CRLIssuer
.cAltEntry
)
2585 swapped
[cSwapped
].tag
= ASN_CONTEXT
| ASN_CONSTRUCTOR
| 2;
2586 swapped
[cSwapped
].pvStructInfo
= &distPoint
->CRLIssuer
;
2587 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeAltName
;
2588 items
[cItem
].pvStructInfo
= &swapped
[cSwapped
];
2589 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2594 ret
= CRYPT_AsnEncodeSequence(X509_ASN_ENCODING
, items
, cItem
, 0, NULL
,
2595 pbEncoded
, pcbEncoded
);
2599 static BOOL WINAPI
CRYPT_AsnEncodeCRLDistPoints(DWORD dwCertEncodingType
,
2600 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2601 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2607 const CRL_DIST_POINTS_INFO
*info
=
2608 (const CRL_DIST_POINTS_INFO
*)pvStructInfo
;
2610 if (!info
->cDistPoint
)
2612 SetLastError(E_INVALIDARG
);
2617 DWORD bytesNeeded
, dataLen
, lenBytes
, i
;
2620 for (i
= 0, dataLen
= 0; ret
&& i
< info
->cDistPoint
; i
++)
2624 ret
= CRYPT_AsnEncodeDistPoint(&info
->rgDistPoint
[i
], NULL
,
2628 else if (GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
2630 /* Have to propagate index of failing character */
2636 CRYPT_EncodeLen(dataLen
, NULL
, &lenBytes
);
2637 bytesNeeded
= 1 + lenBytes
+ dataLen
;
2640 *pcbEncoded
= bytesNeeded
;
2645 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
2646 pbEncoded
, pcbEncoded
, bytesNeeded
)))
2648 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2649 pbEncoded
= *(BYTE
**)pbEncoded
;
2650 *pbEncoded
++ = ASN_SEQUENCEOF
;
2651 CRYPT_EncodeLen(dataLen
, pbEncoded
, &lenBytes
);
2652 pbEncoded
+= lenBytes
;
2653 for (i
= 0; ret
&& i
< info
->cDistPoint
; i
++)
2655 DWORD len
= dataLen
;
2657 ret
= CRYPT_AsnEncodeDistPoint(
2658 &info
->rgDistPoint
[i
], pbEncoded
, &len
);
2672 SetLastError(STATUS_ACCESS_VIOLATION
);
2679 static BOOL WINAPI
CRYPT_AsnEncodeEnhancedKeyUsage(DWORD dwCertEncodingType
,
2680 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2681 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2687 const CERT_ENHKEY_USAGE
*usage
=
2688 (const CERT_ENHKEY_USAGE
*)pvStructInfo
;
2689 DWORD bytesNeeded
= 0, lenBytes
, size
, i
;
2692 for (i
= 0; ret
&& i
< usage
->cUsageIdentifier
; i
++)
2694 ret
= CRYPT_AsnEncodeOid(dwCertEncodingType
, NULL
,
2695 usage
->rgpszUsageIdentifier
[i
],
2696 dwFlags
& ~CRYPT_ENCODE_ALLOC_FLAG
, NULL
, NULL
, &size
);
2698 bytesNeeded
+= size
;
2700 CRYPT_EncodeLen(bytesNeeded
, NULL
, &lenBytes
);
2701 bytesNeeded
+= 1 + lenBytes
;
2705 *pcbEncoded
= bytesNeeded
;
2708 if ((ret
= CRYPT_EncodeEnsureSpace(dwFlags
, pEncodePara
,
2709 pbEncoded
, pcbEncoded
, bytesNeeded
)))
2711 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
)
2712 pbEncoded
= *(BYTE
**)pbEncoded
;
2713 *pbEncoded
++ = ASN_SEQUENCEOF
;
2714 CRYPT_EncodeLen(bytesNeeded
- lenBytes
- 1, pbEncoded
,
2716 pbEncoded
+= lenBytes
;
2717 for (i
= 0; ret
&& i
< usage
->cUsageIdentifier
; i
++)
2720 ret
= CRYPT_AsnEncodeOid(dwCertEncodingType
, NULL
,
2721 usage
->rgpszUsageIdentifier
[i
],
2722 dwFlags
& ~CRYPT_ENCODE_ALLOC_FLAG
, NULL
, pbEncoded
,
2727 bytesNeeded
-= size
;
2736 SetLastError(STATUS_ACCESS_VIOLATION
);
2743 static BOOL WINAPI
CRYPT_AsnEncodeIssuingDistPoint(DWORD dwCertEncodingType
,
2744 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
2745 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2751 const CRL_ISSUING_DIST_POINT
*point
=
2752 (const CRL_ISSUING_DIST_POINT
*)pvStructInfo
;
2753 struct AsnEncodeSequenceItem items
[6] = { { 0 } };
2754 struct AsnConstructedItem constructed
= { 0 };
2755 struct AsnEncodeTagSwappedItem swapped
[5] = { { 0 } };
2756 DWORD cItem
= 0, cSwapped
= 0;
2759 switch (point
->DistPointName
.dwDistPointNameChoice
)
2761 case CRL_DIST_POINT_NO_NAME
:
2764 case CRL_DIST_POINT_FULL_NAME
:
2765 swapped
[cSwapped
].tag
= ASN_CONTEXT
| ASN_CONSTRUCTOR
| 0;
2766 swapped
[cSwapped
].pvStructInfo
= &point
->DistPointName
.u
.FullName
;
2767 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeAltName
;
2768 constructed
.tag
= 0;
2769 constructed
.pvStructInfo
= &swapped
[cSwapped
];
2770 constructed
.encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2771 items
[cItem
].pvStructInfo
= &constructed
;
2772 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeConstructed
;
2777 SetLastError(E_INVALIDARG
);
2780 if (ret
&& point
->fOnlyContainsUserCerts
)
2782 swapped
[cSwapped
].tag
= ASN_CONTEXT
| 1;
2783 swapped
[cSwapped
].pvStructInfo
= &point
->fOnlyContainsUserCerts
;
2784 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeBool
;
2785 items
[cItem
].pvStructInfo
= &swapped
[cSwapped
];
2786 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2790 if (ret
&& point
->fOnlyContainsCACerts
)
2792 swapped
[cSwapped
].tag
= ASN_CONTEXT
| 2;
2793 swapped
[cSwapped
].pvStructInfo
= &point
->fOnlyContainsCACerts
;
2794 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeBool
;
2795 items
[cItem
].pvStructInfo
= &swapped
[cSwapped
];
2796 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2800 if (ret
&& point
->OnlySomeReasonFlags
.cbData
)
2802 swapped
[cSwapped
].tag
= ASN_CONTEXT
| 3;
2803 swapped
[cSwapped
].pvStructInfo
= &point
->OnlySomeReasonFlags
;
2804 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeBits
;
2805 items
[cItem
].pvStructInfo
= &swapped
[cSwapped
];
2806 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2810 if (ret
&& point
->fIndirectCRL
)
2812 swapped
[cSwapped
].tag
= ASN_CONTEXT
| 4;
2813 swapped
[cSwapped
].pvStructInfo
= &point
->fIndirectCRL
;
2814 swapped
[cSwapped
].encodeFunc
= CRYPT_AsnEncodeBool
;
2815 items
[cItem
].pvStructInfo
= &swapped
[cSwapped
];
2816 items
[cItem
].encodeFunc
= CRYPT_AsnEncodeSwapTag
;
2821 ret
= CRYPT_AsnEncodeSequence(dwCertEncodingType
, items
, cItem
,
2822 dwFlags
, pEncodePara
, pbEncoded
, pcbEncoded
);
2826 SetLastError(STATUS_ACCESS_VIOLATION
);
2833 BOOL WINAPI
CryptEncodeObjectEx(DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2834 const void *pvStructInfo
, DWORD dwFlags
, PCRYPT_ENCODE_PARA pEncodePara
,
2835 void *pvEncoded
, DWORD
*pcbEncoded
)
2837 static HCRYPTOIDFUNCSET set
= NULL
;
2839 CryptEncodeObjectExFunc encodeFunc
= NULL
;
2840 HCRYPTOIDFUNCADDR hFunc
= NULL
;
2842 TRACE("(0x%08lx, %s, %p, 0x%08lx, %p, %p, %p)\n", dwCertEncodingType
,
2843 debugstr_a(lpszStructType
), pvStructInfo
, dwFlags
, pEncodePara
,
2844 pvEncoded
, pcbEncoded
);
2846 if (!pvEncoded
&& !pcbEncoded
)
2848 SetLastError(ERROR_INVALID_PARAMETER
);
2851 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
2852 && (dwCertEncodingType
& CMSG_ENCODING_TYPE_MASK
) != PKCS_7_ASN_ENCODING
)
2854 SetLastError(ERROR_FILE_NOT_FOUND
);
2858 SetLastError(NOERROR
);
2859 if (dwFlags
& CRYPT_ENCODE_ALLOC_FLAG
&& pvEncoded
)
2860 *(BYTE
**)pvEncoded
= NULL
;
2861 if (!HIWORD(lpszStructType
))
2863 switch (LOWORD(lpszStructType
))
2865 case (WORD
)X509_CERT
:
2866 encodeFunc
= CRYPT_AsnEncodeCert
;
2868 case (WORD
)X509_CERT_TO_BE_SIGNED
:
2869 encodeFunc
= CRYPT_AsnEncodeCertInfo
;
2871 case (WORD
)X509_CERT_CRL_TO_BE_SIGNED
:
2872 encodeFunc
= CRYPT_AsnEncodeCRLInfo
;
2874 case (WORD
)X509_EXTENSIONS
:
2875 encodeFunc
= CRYPT_AsnEncodeExtensions
;
2877 case (WORD
)X509_NAME_VALUE
:
2878 encodeFunc
= CRYPT_AsnEncodeNameValue
;
2880 case (WORD
)X509_NAME
:
2881 encodeFunc
= CRYPT_AsnEncodeName
;
2883 case (WORD
)X509_PUBLIC_KEY_INFO
:
2884 encodeFunc
= CRYPT_AsnEncodePubKeyInfo
;
2886 case (WORD
)X509_ALTERNATE_NAME
:
2887 encodeFunc
= CRYPT_AsnEncodeAltName
;
2889 case (WORD
)X509_BASIC_CONSTRAINTS
:
2890 encodeFunc
= CRYPT_AsnEncodeBasicConstraints
;
2892 case (WORD
)X509_BASIC_CONSTRAINTS2
:
2893 encodeFunc
= CRYPT_AsnEncodeBasicConstraints2
;
2895 case (WORD
)RSA_CSP_PUBLICKEYBLOB
:
2896 encodeFunc
= CRYPT_AsnEncodeRsaPubKey
;
2898 case (WORD
)X509_UNICODE_NAME
:
2899 encodeFunc
= CRYPT_AsnEncodeUnicodeName
;
2901 case (WORD
)X509_UNICODE_NAME_VALUE
:
2902 encodeFunc
= CRYPT_AsnEncodeUnicodeNameValue
;
2904 case (WORD
)X509_OCTET_STRING
:
2905 encodeFunc
= CRYPT_AsnEncodeOctets
;
2907 case (WORD
)X509_BITS
:
2908 case (WORD
)X509_KEY_USAGE
:
2909 encodeFunc
= CRYPT_AsnEncodeBits
;
2911 case (WORD
)X509_INTEGER
:
2912 encodeFunc
= CRYPT_AsnEncodeInt
;
2914 case (WORD
)X509_MULTI_BYTE_INTEGER
:
2915 encodeFunc
= CRYPT_AsnEncodeInteger
;
2917 case (WORD
)X509_MULTI_BYTE_UINT
:
2918 encodeFunc
= CRYPT_AsnEncodeUnsignedInteger
;
2920 case (WORD
)X509_ENUMERATED
:
2921 encodeFunc
= CRYPT_AsnEncodeEnumerated
;
2923 case (WORD
)X509_CHOICE_OF_TIME
:
2924 encodeFunc
= CRYPT_AsnEncodeChoiceOfTime
;
2926 case (WORD
)X509_SEQUENCE_OF_ANY
:
2927 encodeFunc
= CRYPT_AsnEncodeSequenceOfAny
;
2929 case (WORD
)PKCS_UTC_TIME
:
2930 encodeFunc
= CRYPT_AsnEncodeUtcTime
;
2932 case (WORD
)X509_CRL_DIST_POINTS
:
2933 encodeFunc
= CRYPT_AsnEncodeCRLDistPoints
;
2935 case (WORD
)X509_ENHANCED_KEY_USAGE
:
2936 encodeFunc
= CRYPT_AsnEncodeEnhancedKeyUsage
;
2938 case (WORD
)X509_ISSUING_DIST_POINT
:
2939 encodeFunc
= CRYPT_AsnEncodeIssuingDistPoint
;
2942 FIXME("%d: unimplemented\n", LOWORD(lpszStructType
));
2945 else if (!strcmp(lpszStructType
, szOID_CERT_EXTENSIONS
))
2946 encodeFunc
= CRYPT_AsnEncodeExtensions
;
2947 else if (!strcmp(lpszStructType
, szOID_RSA_signingTime
))
2948 encodeFunc
= CRYPT_AsnEncodeUtcTime
;
2949 else if (!strcmp(lpszStructType
, szOID_CRL_REASON_CODE
))
2950 encodeFunc
= CRYPT_AsnEncodeEnumerated
;
2951 else if (!strcmp(lpszStructType
, szOID_KEY_USAGE
))
2952 encodeFunc
= CRYPT_AsnEncodeBits
;
2953 else if (!strcmp(lpszStructType
, szOID_SUBJECT_KEY_IDENTIFIER
))
2954 encodeFunc
= CRYPT_AsnEncodeOctets
;
2955 else if (!strcmp(lpszStructType
, szOID_BASIC_CONSTRAINTS
))
2956 encodeFunc
= CRYPT_AsnEncodeBasicConstraints
;
2957 else if (!strcmp(lpszStructType
, szOID_BASIC_CONSTRAINTS2
))
2958 encodeFunc
= CRYPT_AsnEncodeBasicConstraints2
;
2959 else if (!strcmp(lpszStructType
, szOID_ISSUER_ALT_NAME
))
2960 encodeFunc
= CRYPT_AsnEncodeAltName
;
2961 else if (!strcmp(lpszStructType
, szOID_ISSUER_ALT_NAME2
))
2962 encodeFunc
= CRYPT_AsnEncodeAltName
;
2963 else if (!strcmp(lpszStructType
, szOID_NEXT_UPDATE_LOCATION
))
2964 encodeFunc
= CRYPT_AsnEncodeAltName
;
2965 else if (!strcmp(lpszStructType
, szOID_SUBJECT_ALT_NAME
))
2966 encodeFunc
= CRYPT_AsnEncodeAltName
;
2967 else if (!strcmp(lpszStructType
, szOID_SUBJECT_ALT_NAME2
))
2968 encodeFunc
= CRYPT_AsnEncodeAltName
;
2969 else if (!strcmp(lpszStructType
, szOID_CRL_DIST_POINTS
))
2970 encodeFunc
= CRYPT_AsnEncodeCRLDistPoints
;
2971 else if (!strcmp(lpszStructType
, szOID_ENHANCED_KEY_USAGE
))
2972 encodeFunc
= CRYPT_AsnEncodeEnhancedKeyUsage
;
2973 else if (!strcmp(lpszStructType
, szOID_ISSUING_DIST_POINT
))
2974 encodeFunc
= CRYPT_AsnEncodeIssuingDistPoint
;
2976 TRACE("OID %s not found or unimplemented, looking for DLL\n",
2977 debugstr_a(lpszStructType
));
2981 set
= CryptInitOIDFunctionSet(CRYPT_OID_ENCODE_OBJECT_EX_FUNC
, 0);
2982 CryptGetOIDFunctionAddress(set
, dwCertEncodingType
, lpszStructType
, 0,
2983 (void **)&encodeFunc
, &hFunc
);
2986 ret
= encodeFunc(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2987 dwFlags
, pEncodePara
, pvEncoded
, pcbEncoded
);
2989 SetLastError(ERROR_FILE_NOT_FOUND
);
2991 CryptFreeOIDFunctionAddress(hFunc
, 0);
2995 BOOL WINAPI
CryptExportPublicKeyInfo(HCRYPTPROV hCryptProv
, DWORD dwKeySpec
,
2996 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
, DWORD
*pcbInfo
)
2998 return CryptExportPublicKeyInfoEx(hCryptProv
, dwKeySpec
, dwCertEncodingType
,
2999 NULL
, 0, NULL
, pInfo
, pcbInfo
);
3002 static BOOL WINAPI
CRYPT_ExportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv
,
3003 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPSTR pszPublicKeyObjId
,
3004 DWORD dwFlags
, void *pvAuxInfo
, PCERT_PUBLIC_KEY_INFO pInfo
, DWORD
*pcbInfo
)
3008 static CHAR oid
[] = szOID_RSA_RSA
;
3010 TRACE("(%08lx, %ld, %08lx, %s, %08lx, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
3011 dwCertEncodingType
, debugstr_a(pszPublicKeyObjId
), dwFlags
, pvAuxInfo
,
3014 if (!pszPublicKeyObjId
)
3015 pszPublicKeyObjId
= oid
;
3016 if ((ret
= CryptGetUserKey(hCryptProv
, dwKeySpec
, &key
)))
3020 ret
= CryptExportKey(key
, 0, PUBLICKEYBLOB
, 0, NULL
, &keySize
);
3023 LPBYTE pubKey
= CryptMemAlloc(keySize
);
3027 ret
= CryptExportKey(key
, 0, PUBLICKEYBLOB
, 0, pubKey
,
3031 DWORD encodedLen
= 0;
3033 ret
= CryptEncodeObject(dwCertEncodingType
,
3034 RSA_CSP_PUBLICKEYBLOB
, pubKey
, NULL
, &encodedLen
);
3037 DWORD sizeNeeded
= sizeof(CERT_PUBLIC_KEY_INFO
) +
3038 strlen(pszPublicKeyObjId
) + 1 + encodedLen
;
3041 *pcbInfo
= sizeNeeded
;
3042 else if (*pcbInfo
< sizeNeeded
)
3044 SetLastError(ERROR_MORE_DATA
);
3045 *pcbInfo
= sizeNeeded
;
3050 pInfo
->Algorithm
.pszObjId
= (char *)pInfo
+
3051 sizeof(CERT_PUBLIC_KEY_INFO
);
3052 lstrcpyA(pInfo
->Algorithm
.pszObjId
,
3054 pInfo
->Algorithm
.Parameters
.cbData
= 0;
3055 pInfo
->Algorithm
.Parameters
.pbData
= NULL
;
3056 pInfo
->PublicKey
.pbData
=
3057 (BYTE
*)pInfo
->Algorithm
.pszObjId
3058 + lstrlenA(pInfo
->Algorithm
.pszObjId
) + 1;
3059 pInfo
->PublicKey
.cbData
= encodedLen
;
3060 pInfo
->PublicKey
.cUnusedBits
= 0;
3061 ret
= CryptEncodeObject(dwCertEncodingType
,
3062 RSA_CSP_PUBLICKEYBLOB
, pubKey
,
3063 pInfo
->PublicKey
.pbData
, &pInfo
->PublicKey
.cbData
);
3067 CryptMemFree(pubKey
);
3072 CryptDestroyKey(key
);
3077 typedef BOOL (WINAPI
*ExportPublicKeyInfoExFunc
)(HCRYPTPROV hCryptProv
,
3078 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPSTR pszPublicKeyObjId
,
3079 DWORD dwFlags
, void *pvAuxInfo
, PCERT_PUBLIC_KEY_INFO pInfo
, DWORD
*pcbInfo
);
3081 BOOL WINAPI
CryptExportPublicKeyInfoEx(HCRYPTPROV hCryptProv
, DWORD dwKeySpec
,
3082 DWORD dwCertEncodingType
, LPSTR pszPublicKeyObjId
, DWORD dwFlags
,
3083 void *pvAuxInfo
, PCERT_PUBLIC_KEY_INFO pInfo
, DWORD
*pcbInfo
)
3085 static HCRYPTOIDFUNCSET set
= NULL
;
3087 ExportPublicKeyInfoExFunc exportFunc
= NULL
;
3088 HCRYPTOIDFUNCADDR hFunc
= NULL
;
3090 TRACE("(%08lx, %ld, %08lx, %s, %08lx, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
3091 dwCertEncodingType
, debugstr_a(pszPublicKeyObjId
), dwFlags
, pvAuxInfo
,
3096 SetLastError(ERROR_INVALID_PARAMETER
);
3100 if (pszPublicKeyObjId
)
3103 set
= CryptInitOIDFunctionSet(CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC
,
3105 CryptGetOIDFunctionAddress(set
, dwCertEncodingType
, pszPublicKeyObjId
,
3106 0, (void **)&exportFunc
, &hFunc
);
3109 exportFunc
= CRYPT_ExportRsaPublicKeyInfoEx
;
3110 ret
= exportFunc(hCryptProv
, dwKeySpec
, dwCertEncodingType
,
3111 pszPublicKeyObjId
, dwFlags
, pvAuxInfo
, pInfo
, pcbInfo
);
3113 CryptFreeOIDFunctionAddress(hFunc
, 0);
3117 BOOL WINAPI
CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv
,
3118 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
, HCRYPTKEY
*phKey
)
3120 return CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
, pInfo
,
3124 static BOOL WINAPI
CRYPT_ImportRsaPublicKeyInfoEx(HCRYPTPROV hCryptProv
,
3125 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
, ALG_ID aiKeyAlg
,
3126 DWORD dwFlags
, void *pvAuxInfo
, HCRYPTKEY
*phKey
)
3129 DWORD pubKeySize
= 0;
3131 TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv
,
3132 dwCertEncodingType
, pInfo
, aiKeyAlg
, dwFlags
, pvAuxInfo
, phKey
);
3134 ret
= CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
3135 pInfo
->PublicKey
.pbData
, pInfo
->PublicKey
.cbData
, 0, NULL
, &pubKeySize
);
3138 LPBYTE pubKey
= CryptMemAlloc(pubKeySize
);
3142 ret
= CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
3143 pInfo
->PublicKey
.pbData
, pInfo
->PublicKey
.cbData
, 0, pubKey
,
3146 ret
= CryptImportKey(hCryptProv
, pubKey
, pubKeySize
, 0, 0,
3148 CryptMemFree(pubKey
);
3156 typedef BOOL (WINAPI
*ImportPublicKeyInfoExFunc
)(HCRYPTPROV hCryptProv
,
3157 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
, ALG_ID aiKeyAlg
,
3158 DWORD dwFlags
, void *pvAuxInfo
, HCRYPTKEY
*phKey
);
3160 BOOL WINAPI
CryptImportPublicKeyInfoEx(HCRYPTPROV hCryptProv
,
3161 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
, ALG_ID aiKeyAlg
,
3162 DWORD dwFlags
, void *pvAuxInfo
, HCRYPTKEY
*phKey
)
3164 static HCRYPTOIDFUNCSET set
= NULL
;
3166 ImportPublicKeyInfoExFunc importFunc
= NULL
;
3167 HCRYPTOIDFUNCADDR hFunc
= NULL
;
3169 TRACE("(%ld, %ld, %p, %d, %08lx, %p, %p)\n", hCryptProv
,
3170 dwCertEncodingType
, pInfo
, aiKeyAlg
, dwFlags
, pvAuxInfo
, phKey
);
3173 set
= CryptInitOIDFunctionSet(CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC
, 0);
3174 CryptGetOIDFunctionAddress(set
, dwCertEncodingType
,
3175 pInfo
->Algorithm
.pszObjId
, 0, (void **)&importFunc
, &hFunc
);
3177 importFunc
= CRYPT_ImportRsaPublicKeyInfoEx
;
3178 ret
= importFunc(hCryptProv
, dwCertEncodingType
, pInfo
, aiKeyAlg
, dwFlags
,
3181 CryptFreeOIDFunctionAddress(hFunc
, 0);