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
19 #ifndef __CRYPT32_PRIVATE_H__
20 #define __CRYPT32_PRIVATE_H__
22 #include <wine/config.h>
23 #include <wine/port.h>
30 #define COM_NO_WINDOWS_H
32 #define NONAMELESSUNION
40 #define CERT_CHAIN_PARA_HAS_EXTRA_FIELDS
41 #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
46 #include <wine/unicode.h>
47 #include <wine/list.h>
48 #include <wine/exception.h>
49 #include <wine/debug.h>
53 /* a few asn.1 tags we need */
54 #define ASN_BOOL (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x01)
55 #define ASN_BITSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x03)
56 #define ASN_ENUMERATED (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0a)
57 #define ASN_UTF8STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0c)
58 #define ASN_SETOF (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x11)
59 #define ASN_NUMERICSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x12)
60 #define ASN_PRINTABLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x13)
61 #define ASN_T61STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x14)
62 #define ASN_VIDEOTEXSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x15)
63 #define ASN_IA5STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x16)
64 #define ASN_UTCTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x17)
65 #define ASN_GENERALTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x18)
66 #define ASN_GRAPHICSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x19)
67 #define ASN_VISIBLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1a)
68 #define ASN_GENERALSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1b)
69 #define ASN_UNIVERSALSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1c)
70 #define ASN_BMPSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1e)
72 BOOL
CRYPT_EncodeLen(DWORD len
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
74 typedef BOOL (WINAPI
*CryptEncodeObjectExFunc
)(DWORD
, LPCSTR
, const void *,
75 DWORD
, PCRYPT_ENCODE_PARA
, BYTE
*, DWORD
*);
77 struct AsnEncodeSequenceItem
79 const void *pvStructInfo
;
80 CryptEncodeObjectExFunc encodeFunc
;
81 DWORD size
; /* used during encoding, not for your use */
84 BOOL WINAPI
CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType
,
85 struct AsnEncodeSequenceItem items
[], DWORD cItem
, DWORD dwFlags
,
86 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
88 struct AsnConstructedItem
91 const void *pvStructInfo
;
92 CryptEncodeObjectExFunc encodeFunc
;
95 BOOL WINAPI
CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType
,
96 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
97 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
98 BOOL WINAPI
CRYPT_AsnEncodeOid(DWORD dwCertEncodingType
,
99 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
100 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
101 BOOL WINAPI
CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType
,
102 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
103 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
105 typedef struct _CRYPT_DIGESTED_DATA
108 CRYPT_ALGORITHM_IDENTIFIER DigestAlgorithm
;
109 CRYPT_CONTENT_INFO ContentInfo
;
110 CRYPT_HASH_BLOB hash
;
111 } CRYPT_DIGESTED_DATA
;
113 BOOL
CRYPT_AsnEncodePKCSDigestedData(const CRYPT_DIGESTED_DATA
*digestedData
,
114 void *pvData
, DWORD
*pcbData
) DECLSPEC_HIDDEN
;
116 typedef struct _CRYPT_ENCRYPTED_CONTENT_INFO
119 CRYPT_ALGORITHM_IDENTIFIER contentEncryptionAlgorithm
;
120 CRYPT_DATA_BLOB encryptedContent
;
121 } CRYPT_ENCRYPTED_CONTENT_INFO
;
123 typedef struct _CRYPT_ENVELOPED_DATA
126 DWORD cRecipientInfo
;
127 PCMSG_KEY_TRANS_RECIPIENT_INFO rgRecipientInfo
;
128 CRYPT_ENCRYPTED_CONTENT_INFO encryptedContentInfo
;
129 } CRYPT_ENVELOPED_DATA
;
131 BOOL
CRYPT_AsnEncodePKCSEnvelopedData(const CRYPT_ENVELOPED_DATA
*envelopedData
,
132 void *pvData
, DWORD
*pcbData
) DECLSPEC_HIDDEN
;
134 BOOL
CRYPT_AsnDecodePKCSEnvelopedData(const BYTE
*pbEncoded
, DWORD cbEncoded
,
135 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
136 CRYPT_ENVELOPED_DATA
*envelopedData
, DWORD
*pcbEnvelopedData
) DECLSPEC_HIDDEN
;
138 typedef struct _CRYPT_SIGNED_INFO
142 PCERT_BLOB rgCertEncoded
;
144 PCRL_BLOB rgCrlEncoded
;
145 CRYPT_CONTENT_INFO content
;
147 PCMSG_CMS_SIGNER_INFO rgSignerInfo
;
150 BOOL
CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO
*, void *pvData
,
151 DWORD
*pcbData
) DECLSPEC_HIDDEN
;
153 BOOL
CRYPT_AsnDecodeCMSSignedInfo(const BYTE
*pbEncoded
, DWORD cbEncoded
,
154 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
155 CRYPT_SIGNED_INFO
*signedInfo
, DWORD
*pcbSignedInfo
) DECLSPEC_HIDDEN
;
157 /* Helper function to check *pcbEncoded, set it to the required size, and
158 * optionally to allocate memory. Assumes pbEncoded is not NULL.
159 * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
160 * pointer to the newly allocated memory.
162 BOOL
CRYPT_EncodeEnsureSpace(DWORD dwFlags
, const CRYPT_ENCODE_PARA
*pEncodePara
,
163 BYTE
*pbEncoded
, DWORD
*pcbEncoded
, DWORD bytesNeeded
) DECLSPEC_HIDDEN
;
165 BOOL
CRYPT_AsnDecodePKCSDigestedData(const BYTE
*pbEncoded
, DWORD cbEncoded
,
166 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
167 CRYPT_DIGESTED_DATA
*digestedData
, DWORD
*pcbDigestedData
) DECLSPEC_HIDDEN
;
169 BOOL WINAPI
CRYPT_AsnEncodePubKeyInfoNoNull(DWORD dwCertEncodingType
,
170 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
171 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
173 /* The following aren't defined in wincrypt.h, as they're "reserved" */
174 #define CERT_CERT_PROP_ID 32
175 #define CERT_CRL_PROP_ID 33
176 #define CERT_CTL_PROP_ID 34
178 /* Returns a handle to the default crypto provider; loads it if necessary.
179 * Returns NULL on failure.
181 HCRYPTPROV
CRYPT_GetDefaultProvider(void) DECLSPEC_HIDDEN
;
183 HINSTANCE hInstance DECLSPEC_HIDDEN
;
185 void crypt_oid_init(void) DECLSPEC_HIDDEN
;
186 void crypt_oid_free(void) DECLSPEC_HIDDEN
;
187 void crypt_sip_free(void) DECLSPEC_HIDDEN
;
188 void root_store_free(void) DECLSPEC_HIDDEN
;
189 void default_chain_engine_free(void) DECLSPEC_HIDDEN
;
191 /* (Internal) certificate store types and functions */
192 struct WINE_CRYPTCERTSTORE
;
194 typedef struct _CONTEXT_PROPERTY_LIST CONTEXT_PROPERTY_LIST
;
196 typedef struct _context_t context_t
;
199 void (*free
)(context_t
*);
200 struct _context_t
*(*clone
)(context_t
*,struct WINE_CRYPTCERTSTORE
*,BOOL
);
204 const context_vtbl_t
*vtbl
;
206 struct WINE_CRYPTCERTSTORE
*store
;
207 struct _context_t
*linked
;
208 CONTEXT_PROPERTY_LIST
*properties
;
215 static inline context_t
*context_from_ptr(const void *ptr
)
217 return (context_t
*)ptr
-1;
220 static inline void *context_ptr(context_t
*context
)
230 static inline cert_t
*cert_from_ptr(const CERT_CONTEXT
*ptr
)
232 return CONTAINING_RECORD(ptr
, cert_t
, ctx
);
240 static inline crl_t
*crl_from_ptr(const CRL_CONTEXT
*ptr
)
242 return CONTAINING_RECORD(ptr
, crl_t
, ctx
);
250 static inline ctl_t
*ctl_from_ptr(const CTL_CONTEXT
*ptr
)
252 return CONTAINING_RECORD(ptr
, ctl_t
, ctx
);
255 /* Some typedefs that make it easier to abstract which type of context we're
258 typedef const void *(WINAPI
*CreateContextFunc
)(DWORD dwCertEncodingType
,
259 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
);
260 typedef BOOL (WINAPI
*AddContextToStoreFunc
)(HCERTSTORE hCertStore
,
261 const void *context
, DWORD dwAddDisposition
, const void **ppStoreContext
);
262 typedef BOOL (WINAPI
*AddEncodedContextToStoreFunc
)(HCERTSTORE hCertStore
,
263 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
264 DWORD dwAddDisposition
, const void **ppContext
);
265 typedef const void *(WINAPI
*EnumContextsInStoreFunc
)(HCERTSTORE hCertStore
,
266 const void *pPrevContext
);
267 typedef DWORD (WINAPI
*EnumPropertiesFunc
)(const void *context
, DWORD dwPropId
);
268 typedef BOOL (WINAPI
*GetContextPropertyFunc
)(const void *context
,
269 DWORD dwPropID
, void *pvData
, DWORD
*pcbData
);
270 typedef BOOL (WINAPI
*SetContextPropertyFunc
)(const void *context
,
271 DWORD dwPropID
, DWORD dwFlags
, const void *pvData
);
272 typedef BOOL (WINAPI
*SerializeElementFunc
)(const void *context
, DWORD dwFlags
,
273 BYTE
*pbElement
, DWORD
*pcbElement
);
274 typedef BOOL (WINAPI
*DeleteContextFunc
)(const void *contex
);
276 /* An abstract context (certificate, CRL, or CTL) interface */
277 typedef struct _WINE_CONTEXT_INTERFACE
279 CreateContextFunc create
;
280 AddContextToStoreFunc addContextToStore
;
281 AddEncodedContextToStoreFunc addEncodedToStore
;
282 EnumContextsInStoreFunc enumContextsInStore
;
283 EnumPropertiesFunc enumProps
;
284 GetContextPropertyFunc getProp
;
285 SetContextPropertyFunc setProp
;
286 SerializeElementFunc serialize
;
287 DeleteContextFunc deleteFromStore
;
288 } WINE_CONTEXT_INTERFACE
;
290 extern const WINE_CONTEXT_INTERFACE
*pCertInterface DECLSPEC_HIDDEN
;
291 extern const WINE_CONTEXT_INTERFACE
*pCRLInterface DECLSPEC_HIDDEN
;
292 extern const WINE_CONTEXT_INTERFACE
*pCTLInterface DECLSPEC_HIDDEN
;
294 typedef struct WINE_CRYPTCERTSTORE
* (*StoreOpenFunc
)(HCRYPTPROV hCryptProv
,
295 DWORD dwFlags
, const void *pvPara
);
297 typedef struct _CONTEXT_FUNCS
299 /* Called to add a context to a store. If toReplace is not NULL,
300 * context replaces toReplace in the store, and access checks should not be
301 * performed. Otherwise context is a new context, and it should only be
302 * added if the store allows it. If ppStoreContext is not NULL, the added
303 * context should be returned in *ppStoreContext.
305 BOOL (*addContext
)(struct WINE_CRYPTCERTSTORE
*,context_t
*,context_t
*,context_t
**,BOOL
);
306 context_t
*(*enumContext
)(struct WINE_CRYPTCERTSTORE
*store
, context_t
*prev
);
307 BOOL (*delete)(struct WINE_CRYPTCERTSTORE
*,context_t
*);
310 typedef enum _CertStoreType
{
317 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
319 /* A cert store is polymorphic through the use of function pointers. A type
320 * is still needed to distinguish collection stores from other types.
321 * On the function pointers:
322 * - closeStore is called when the store's ref count becomes 0
323 * - control is optional, but should be implemented by any store that supports
328 void (*addref
)(struct WINE_CRYPTCERTSTORE
*);
329 DWORD (*release
)(struct WINE_CRYPTCERTSTORE
*,DWORD
);
330 void (*releaseContext
)(struct WINE_CRYPTCERTSTORE
*,context_t
*);
331 BOOL (*control
)(struct WINE_CRYPTCERTSTORE
*,DWORD
,DWORD
,void const*);
337 typedef struct WINE_CRYPTCERTSTORE
343 const store_vtbl_t
*vtbl
;
344 CONTEXT_PROPERTY_LIST
*properties
;
345 } WINECRYPT_CERTSTORE
;
347 void CRYPT_InitStore(WINECRYPT_CERTSTORE
*store
, DWORD dwFlags
,
348 CertStoreType type
, const store_vtbl_t
*) DECLSPEC_HIDDEN
;
349 void CRYPT_FreeStore(WINECRYPT_CERTSTORE
*store
) DECLSPEC_HIDDEN
;
350 BOOL WINAPI
I_CertUpdateStore(HCERTSTORE store1
, HCERTSTORE store2
, DWORD unk0
,
351 DWORD unk1
) DECLSPEC_HIDDEN
;
353 WINECRYPT_CERTSTORE
*CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv
,
354 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
355 WINECRYPT_CERTSTORE
*CRYPT_ProvCreateStore(DWORD dwFlags
,
356 WINECRYPT_CERTSTORE
*memStore
, const CERT_STORE_PROV_INFO
*pProvInfo
) DECLSPEC_HIDDEN
;
357 WINECRYPT_CERTSTORE
*CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider
,
358 DWORD dwEncodingType
, HCRYPTPROV hCryptProv
, DWORD dwFlags
,
359 const void *pvPara
) DECLSPEC_HIDDEN
;
360 WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
361 const void *pvPara
) DECLSPEC_HIDDEN
;
362 WINECRYPT_CERTSTORE
*CRYPT_FileOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
363 const void *pvPara
) DECLSPEC_HIDDEN
;
364 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv
,
365 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
366 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv
,
367 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
369 void CRYPT_ImportSystemRootCertsToReg(void) DECLSPEC_HIDDEN
;
370 BOOL
CRYPT_SerializeContextsToReg(HKEY key
, DWORD flags
, const WINE_CONTEXT_INTERFACE
*contextInterface
,
371 HCERTSTORE memStore
) DECLSPEC_HIDDEN
;
373 BOOL
CRYPT_IsCertificateSelfSigned(PCCERT_CONTEXT cert
) DECLSPEC_HIDDEN
;
375 /* Allocates and initializes a certificate chain engine, but without creating
376 * the root store. Instead, it uses root, and assumes the caller has done any
377 * checking necessary.
379 HCERTCHAINENGINE
CRYPT_CreateChainEngine(HCERTSTORE
, DWORD
, const CERT_CHAIN_ENGINE_CONFIG
*) DECLSPEC_HIDDEN
;
381 /* Helper function for store reading functions and
382 * CertAddSerializedElementToStore. Returns a context of the appropriate type
383 * if it can, or NULL otherwise. Doesn't validate any of the properties in
384 * the serialized context (for example, bad hashes are retained.)
385 * *pdwContentType is set to the type of the returned context.
387 const void *CRYPT_ReadSerializedElement(const BYTE
*pbElement
,
388 DWORD cbElement
, DWORD dwContextTypeFlags
, DWORD
*pdwContentType
) DECLSPEC_HIDDEN
;
390 /* Reads contexts serialized in the file into the memory store. Returns FALSE
391 * if the file is not of the expected format.
393 BOOL
CRYPT_ReadSerializedStoreFromFile(HANDLE file
, HCERTSTORE store
) DECLSPEC_HIDDEN
;
395 /* Reads contexts serialized in the blob into the memory store. Returns FALSE
396 * if the file is not of the expected format.
398 BOOL
CRYPT_ReadSerializedStoreFromBlob(const CRYPT_DATA_BLOB
*blob
,
399 HCERTSTORE store
) DECLSPEC_HIDDEN
;
401 /* Fixes up the pointers in info, where info is assumed to be a
402 * CRYPT_KEY_PROV_INFO, followed by its container name, provider name, and any
403 * provider parameters, in a contiguous buffer, but where info's pointers are
404 * assumed to be invalid. Upon return, info's pointers point to the
405 * appropriate memory locations.
407 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
) DECLSPEC_HIDDEN
;
413 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indent
,
414 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
) DECLSPEC_HIDDEN
;
420 /* Allocates a new data context, a context which owns properties directly.
421 * contextSize is the size of the public data type associated with context,
422 * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
423 * Free with Context_Release.
425 context_t
*Context_CreateDataContext(size_t contextSize
, const context_vtbl_t
*vtbl
, struct WINE_CRYPTCERTSTORE
*) DECLSPEC_HIDDEN
;
427 /* Creates a new link context. The context refers to linked
428 * rather than owning its own properties. If addRef is TRUE (which ordinarily
429 * it should be) linked is addref'd.
430 * Free with Context_Release.
432 context_t
*Context_CreateLinkContext(unsigned contextSize
, context_t
*linked
, struct WINE_CRYPTCERTSTORE
*) DECLSPEC_HIDDEN
;
434 /* Copies properties from fromContext to toContext. */
435 void Context_CopyProperties(const void *to
, const void *from
) DECLSPEC_HIDDEN
;
437 void Context_AddRef(context_t
*) DECLSPEC_HIDDEN
;
438 void Context_Release(context_t
*context
) DECLSPEC_HIDDEN
;
439 void Context_Free(context_t
*) DECLSPEC_HIDDEN
;
442 * Context property list functions
445 CONTEXT_PROPERTY_LIST
*ContextPropertyList_Create(void) DECLSPEC_HIDDEN
;
447 /* Searches for the property with ID id in the context. Returns TRUE if found,
448 * and copies the property's length and a pointer to its data to blob.
449 * Otherwise returns FALSE.
451 BOOL
ContextPropertyList_FindProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
,
452 PCRYPT_DATA_BLOB blob
) DECLSPEC_HIDDEN
;
454 BOOL
ContextPropertyList_SetProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
,
455 const BYTE
*pbData
, size_t cbData
) DECLSPEC_HIDDEN
;
457 void ContextPropertyList_RemoveProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
) DECLSPEC_HIDDEN
;
459 DWORD
ContextPropertyList_EnumPropIDs(CONTEXT_PROPERTY_LIST
*list
, DWORD id
) DECLSPEC_HIDDEN
;
461 void ContextPropertyList_Copy(CONTEXT_PROPERTY_LIST
*to
,
462 CONTEXT_PROPERTY_LIST
*from
) DECLSPEC_HIDDEN
;
464 void ContextPropertyList_Free(CONTEXT_PROPERTY_LIST
*list
) DECLSPEC_HIDDEN
;
466 extern WINECRYPT_CERTSTORE empty_store DECLSPEC_HIDDEN
;
467 void init_empty_store(void) DECLSPEC_HIDDEN
;
473 /* Align up to a DWORD_PTR boundary
475 #define ALIGN_DWORD_PTR(x) (((x) + sizeof(DWORD_PTR) - 1) & ~(sizeof(DWORD_PTR) - 1))
476 #define POINTER_ALIGN_DWORD_PTR(p) ((LPVOID)ALIGN_DWORD_PTR((DWORD_PTR)(p)))
478 /* Check if the OID is a small int
480 #define IS_INTOID(x) (((ULONG_PTR)(x) >> 16) == 0)
482 #endif /* __CRYPT32_PRIVATE_H__ */