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/list.h"
24 /* a few asn.1 tags we need */
25 #define ASN_BOOL (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x01)
26 #define ASN_BITSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x03)
27 #define ASN_ENUMERATED (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0a)
28 #define ASN_UTF8STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0c)
29 #define ASN_SETOF (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x11)
30 #define ASN_NUMERICSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x12)
31 #define ASN_PRINTABLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x13)
32 #define ASN_T61STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x14)
33 #define ASN_VIDEOTEXSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x15)
34 #define ASN_IA5STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x16)
35 #define ASN_UTCTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x17)
36 #define ASN_GENERALTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x18)
37 #define ASN_GRAPHICSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x19)
38 #define ASN_VISIBLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1a)
39 #define ASN_GENERALSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1b)
40 #define ASN_UNIVERSALSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1c)
41 #define ASN_BMPSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1e)
43 BOOL
CRYPT_EncodeLen(DWORD len
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
45 typedef BOOL (WINAPI
*CryptEncodeObjectExFunc
)(DWORD
, LPCSTR
, const void *,
46 DWORD
, PCRYPT_ENCODE_PARA
, BYTE
*, DWORD
*);
48 struct AsnEncodeSequenceItem
50 const void *pvStructInfo
;
51 CryptEncodeObjectExFunc encodeFunc
;
52 DWORD size
; /* used during encoding, not for your use */
55 BOOL WINAPI
CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType
,
56 struct AsnEncodeSequenceItem items
[], DWORD cItem
, DWORD dwFlags
,
57 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
59 struct AsnConstructedItem
62 const void *pvStructInfo
;
63 CryptEncodeObjectExFunc encodeFunc
;
66 BOOL WINAPI
CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType
,
67 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
68 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
69 BOOL WINAPI
CRYPT_AsnEncodeOid(DWORD dwCertEncodingType
,
70 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
71 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
72 BOOL WINAPI
CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType
,
73 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
74 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
76 typedef struct _CRYPT_DIGESTED_DATA
79 CRYPT_ALGORITHM_IDENTIFIER DigestAlgorithm
;
80 CRYPT_CONTENT_INFO ContentInfo
;
82 } CRYPT_DIGESTED_DATA
;
84 BOOL
CRYPT_AsnEncodePKCSDigestedData(const CRYPT_DIGESTED_DATA
*digestedData
,
85 void *pvData
, DWORD
*pcbData
) DECLSPEC_HIDDEN
;
87 typedef struct _CRYPT_ENCRYPTED_CONTENT_INFO
90 CRYPT_ALGORITHM_IDENTIFIER contentEncryptionAlgorithm
;
91 CRYPT_DATA_BLOB encryptedContent
;
92 } CRYPT_ENCRYPTED_CONTENT_INFO
;
94 typedef struct _CRYPT_ENVELOPED_DATA
98 PCMSG_KEY_TRANS_RECIPIENT_INFO rgRecipientInfo
;
99 CRYPT_ENCRYPTED_CONTENT_INFO encryptedContentInfo
;
100 } CRYPT_ENVELOPED_DATA
;
102 BOOL
CRYPT_AsnEncodePKCSEnvelopedData(const CRYPT_ENVELOPED_DATA
*envelopedData
,
103 void *pvData
, DWORD
*pcbData
) DECLSPEC_HIDDEN
;
105 BOOL
CRYPT_AsnDecodePKCSEnvelopedData(const BYTE
*pbEncoded
, DWORD cbEncoded
,
106 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
107 CRYPT_ENVELOPED_DATA
*envelopedData
, DWORD
*pcbEnvelopedData
) DECLSPEC_HIDDEN
;
109 typedef struct _CRYPT_SIGNED_INFO
113 PCERT_BLOB rgCertEncoded
;
115 PCRL_BLOB rgCrlEncoded
;
116 CRYPT_CONTENT_INFO content
;
118 PCMSG_CMS_SIGNER_INFO rgSignerInfo
;
119 PDWORD signerKeySpec
;
122 BOOL
CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO
*, void *pvData
,
123 DWORD
*pcbData
) DECLSPEC_HIDDEN
;
125 BOOL
CRYPT_AsnDecodeCMSSignedInfo(const BYTE
*pbEncoded
, DWORD cbEncoded
,
126 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
127 CRYPT_SIGNED_INFO
*signedInfo
, DWORD
*pcbSignedInfo
) DECLSPEC_HIDDEN
;
129 /* Helper function to check *pcbEncoded, set it to the required size, and
130 * optionally to allocate memory. Assumes pbEncoded is not NULL.
131 * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
132 * pointer to the newly allocated memory.
134 BOOL
CRYPT_EncodeEnsureSpace(DWORD dwFlags
, const CRYPT_ENCODE_PARA
*pEncodePara
,
135 BYTE
*pbEncoded
, DWORD
*pcbEncoded
, DWORD bytesNeeded
) DECLSPEC_HIDDEN
;
137 BOOL
CRYPT_AsnDecodePKCSDigestedData(const BYTE
*pbEncoded
, DWORD cbEncoded
,
138 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
139 CRYPT_DIGESTED_DATA
*digestedData
, DWORD
*pcbDigestedData
) DECLSPEC_HIDDEN
;
141 BOOL WINAPI
CRYPT_AsnEncodePubKeyInfoNoNull(DWORD dwCertEncodingType
,
142 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
143 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
145 /* The following aren't defined in wincrypt.h, as they're "reserved" */
146 #define CERT_CERT_PROP_ID 32
147 #define CERT_CRL_PROP_ID 33
148 #define CERT_CTL_PROP_ID 34
150 /* Returns a handle to the default crypto provider; loads it if necessary.
151 * Returns NULL on failure.
153 HCRYPTPROV WINAPI
I_CryptGetDefaultCryptProv(ALG_ID
);
155 extern HINSTANCE hInstance DECLSPEC_HIDDEN
;
157 void crypt_oid_init(void) DECLSPEC_HIDDEN
;
158 void crypt_oid_free(void) DECLSPEC_HIDDEN
;
159 void crypt_sip_free(void) DECLSPEC_HIDDEN
;
160 void root_store_free(void) DECLSPEC_HIDDEN
;
161 void default_chain_engine_free(void) DECLSPEC_HIDDEN
;
163 /* (Internal) certificate store types and functions */
164 struct WINE_CRYPTCERTSTORE
;
166 typedef struct _CONTEXT_PROPERTY_LIST CONTEXT_PROPERTY_LIST
;
168 typedef struct _context_t context_t
;
171 void (*free
)(context_t
*);
172 struct _context_t
*(*clone
)(context_t
*,struct WINE_CRYPTCERTSTORE
*,BOOL
);
176 const context_vtbl_t
*vtbl
;
178 struct WINE_CRYPTCERTSTORE
*store
;
179 struct _context_t
*linked
;
180 CONTEXT_PROPERTY_LIST
*properties
;
187 static inline context_t
*context_from_ptr(const void *ptr
)
189 return (context_t
*)ptr
-1;
192 static inline void *context_ptr(context_t
*context
)
202 static inline cert_t
*cert_from_ptr(const CERT_CONTEXT
*ptr
)
204 return CONTAINING_RECORD(ptr
, cert_t
, ctx
);
212 static inline crl_t
*crl_from_ptr(const CRL_CONTEXT
*ptr
)
214 return CONTAINING_RECORD(ptr
, crl_t
, ctx
);
222 static inline ctl_t
*ctl_from_ptr(const CTL_CONTEXT
*ptr
)
224 return CONTAINING_RECORD(ptr
, ctl_t
, ctx
);
227 /* Some typedefs that make it easier to abstract which type of context we're
230 typedef const void *(WINAPI
*CreateContextFunc
)(DWORD dwCertEncodingType
,
231 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
);
232 typedef BOOL (WINAPI
*AddContextToStoreFunc
)(HCERTSTORE hCertStore
,
233 const void *context
, DWORD dwAddDisposition
, const void **ppStoreContext
);
234 typedef BOOL (WINAPI
*AddEncodedContextToStoreFunc
)(HCERTSTORE hCertStore
,
235 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
236 DWORD dwAddDisposition
, const void **ppContext
);
237 typedef const void *(WINAPI
*EnumContextsInStoreFunc
)(HCERTSTORE hCertStore
,
238 const void *pPrevContext
);
239 typedef DWORD (WINAPI
*EnumPropertiesFunc
)(const void *context
, DWORD dwPropId
);
240 typedef BOOL (WINAPI
*GetContextPropertyFunc
)(const void *context
,
241 DWORD dwPropID
, void *pvData
, DWORD
*pcbData
);
242 typedef BOOL (WINAPI
*SetContextPropertyFunc
)(const void *context
,
243 DWORD dwPropID
, DWORD dwFlags
, const void *pvData
);
244 typedef BOOL (WINAPI
*SerializeElementFunc
)(const void *context
, DWORD dwFlags
,
245 BYTE
*pbElement
, DWORD
*pcbElement
);
246 typedef BOOL (WINAPI
*DeleteContextFunc
)(const void *contex
);
248 /* An abstract context (certificate, CRL, or CTL) interface */
249 typedef struct _WINE_CONTEXT_INTERFACE
251 CreateContextFunc create
;
252 AddContextToStoreFunc addContextToStore
;
253 AddEncodedContextToStoreFunc addEncodedToStore
;
254 EnumContextsInStoreFunc enumContextsInStore
;
255 EnumPropertiesFunc enumProps
;
256 GetContextPropertyFunc getProp
;
257 SetContextPropertyFunc setProp
;
258 SerializeElementFunc serialize
;
259 DeleteContextFunc deleteFromStore
;
260 } WINE_CONTEXT_INTERFACE
;
262 extern const WINE_CONTEXT_INTERFACE
*pCertInterface DECLSPEC_HIDDEN
;
263 extern const WINE_CONTEXT_INTERFACE
*pCRLInterface DECLSPEC_HIDDEN
;
264 extern const WINE_CONTEXT_INTERFACE
*pCTLInterface DECLSPEC_HIDDEN
;
266 typedef struct WINE_CRYPTCERTSTORE
* (*StoreOpenFunc
)(HCRYPTPROV hCryptProv
,
267 DWORD dwFlags
, const void *pvPara
);
269 typedef struct _CONTEXT_FUNCS
271 /* Called to add a context to a store. If toReplace is not NULL,
272 * context replaces toReplace in the store, and access checks should not be
273 * performed. Otherwise context is a new context, and it should only be
274 * added if the store allows it. If ppStoreContext is not NULL, the added
275 * context should be returned in *ppStoreContext.
277 BOOL (*addContext
)(struct WINE_CRYPTCERTSTORE
*,context_t
*,context_t
*,context_t
**,BOOL
);
278 context_t
*(*enumContext
)(struct WINE_CRYPTCERTSTORE
*store
, context_t
*prev
);
279 BOOL (*delete)(struct WINE_CRYPTCERTSTORE
*,context_t
*);
282 typedef enum _CertStoreType
{
289 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
291 /* A cert store is polymorphic through the use of function pointers. A type
292 * is still needed to distinguish collection stores from other types.
293 * On the function pointers:
294 * - closeStore is called when the store's ref count becomes 0
295 * - control is optional, but should be implemented by any store that supports
300 void (*addref
)(struct WINE_CRYPTCERTSTORE
*);
301 DWORD (*release
)(struct WINE_CRYPTCERTSTORE
*,DWORD
);
302 void (*releaseContext
)(struct WINE_CRYPTCERTSTORE
*,context_t
*);
303 BOOL (*control
)(struct WINE_CRYPTCERTSTORE
*,DWORD
,DWORD
,void const*);
309 typedef struct WINE_CRYPTCERTSTORE
315 const store_vtbl_t
*vtbl
;
316 CONTEXT_PROPERTY_LIST
*properties
;
317 } WINECRYPT_CERTSTORE
;
319 void CRYPT_InitStore(WINECRYPT_CERTSTORE
*store
, DWORD dwFlags
,
320 CertStoreType type
, const store_vtbl_t
*) DECLSPEC_HIDDEN
;
321 void CRYPT_FreeStore(WINECRYPT_CERTSTORE
*store
) DECLSPEC_HIDDEN
;
322 BOOL WINAPI
I_CertUpdateStore(HCERTSTORE store1
, HCERTSTORE store2
, DWORD unk0
,
323 DWORD unk1
) DECLSPEC_HIDDEN
;
325 WINECRYPT_CERTSTORE
*CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv
,
326 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
327 WINECRYPT_CERTSTORE
*CRYPT_ProvCreateStore(DWORD dwFlags
,
328 WINECRYPT_CERTSTORE
*memStore
, const CERT_STORE_PROV_INFO
*pProvInfo
) DECLSPEC_HIDDEN
;
329 WINECRYPT_CERTSTORE
*CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider
,
330 DWORD dwEncodingType
, HCRYPTPROV hCryptProv
, DWORD dwFlags
,
331 const void *pvPara
) DECLSPEC_HIDDEN
;
332 WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
333 const void *pvPara
) DECLSPEC_HIDDEN
;
334 WINECRYPT_CERTSTORE
*CRYPT_FileOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
335 const void *pvPara
) DECLSPEC_HIDDEN
;
336 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv
,
337 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
338 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv
,
339 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
341 void CRYPT_ImportSystemRootCertsToReg(void) DECLSPEC_HIDDEN
;
342 BOOL
CRYPT_SerializeContextsToReg(HKEY key
, DWORD flags
, const WINE_CONTEXT_INTERFACE
*contextInterface
,
343 HCERTSTORE memStore
) DECLSPEC_HIDDEN
;
345 DWORD
CRYPT_IsCertificateSelfSigned(const CERT_CONTEXT
*cert
) DECLSPEC_HIDDEN
;
347 /* Allocates and initializes a certificate chain engine, but without creating
348 * the root store. Instead, it uses root, and assumes the caller has done any
349 * checking necessary.
351 HCERTCHAINENGINE
CRYPT_CreateChainEngine(HCERTSTORE
, DWORD
, const CERT_CHAIN_ENGINE_CONFIG
*) DECLSPEC_HIDDEN
;
353 /* Helper function for store reading functions and
354 * CertAddSerializedElementToStore. Returns a context of the appropriate type
355 * if it can, or NULL otherwise. Doesn't validate any of the properties in
356 * the serialized context (for example, bad hashes are retained.)
357 * *pdwContentType is set to the type of the returned context.
359 const void *CRYPT_ReadSerializedElement(const BYTE
*pbElement
,
360 DWORD cbElement
, DWORD dwContextTypeFlags
, DWORD
*pdwContentType
) DECLSPEC_HIDDEN
;
362 /* Reads contexts serialized in the file into the memory store. Returns FALSE
363 * if the file is not of the expected format.
365 BOOL
CRYPT_ReadSerializedStoreFromFile(HANDLE file
, HCERTSTORE store
) DECLSPEC_HIDDEN
;
367 /* Reads contexts serialized in the blob into the memory store. Returns FALSE
368 * if the file is not of the expected format.
370 BOOL
CRYPT_ReadSerializedStoreFromBlob(const CRYPT_DATA_BLOB
*blob
,
371 HCERTSTORE store
) DECLSPEC_HIDDEN
;
373 struct store_CERT_KEY_CONTEXT
379 void CRYPT_ConvertKeyContext(const struct store_CERT_KEY_CONTEXT
*src
, CERT_KEY_CONTEXT
*dst
) DECLSPEC_HIDDEN
;
385 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indent
,
386 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
) DECLSPEC_HIDDEN
;
392 /* Allocates a new data context, a context which owns properties directly.
393 * contextSize is the size of the public data type associated with context,
394 * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
395 * Free with Context_Release.
397 context_t
*Context_CreateDataContext(size_t contextSize
, const context_vtbl_t
*vtbl
, struct WINE_CRYPTCERTSTORE
*) DECLSPEC_HIDDEN
;
399 /* Creates a new link context. The context refers to linked
400 * rather than owning its own properties. If addRef is TRUE (which ordinarily
401 * it should be) linked is addref'd.
402 * Free with Context_Release.
404 context_t
*Context_CreateLinkContext(unsigned contextSize
, context_t
*linked
, struct WINE_CRYPTCERTSTORE
*) DECLSPEC_HIDDEN
;
406 /* Copies properties from fromContext to toContext. */
407 void Context_CopyProperties(const void *to
, const void *from
) DECLSPEC_HIDDEN
;
409 void Context_AddRef(context_t
*) DECLSPEC_HIDDEN
;
410 void Context_Release(context_t
*context
) DECLSPEC_HIDDEN
;
411 void Context_Free(context_t
*) DECLSPEC_HIDDEN
;
414 * Context property list functions
417 CONTEXT_PROPERTY_LIST
*ContextPropertyList_Create(void) DECLSPEC_HIDDEN
;
419 /* Searches for the property with ID id in the context. Returns TRUE if found,
420 * and copies the property's length and a pointer to its data to blob.
421 * Otherwise returns FALSE.
423 BOOL
ContextPropertyList_FindProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
,
424 PCRYPT_DATA_BLOB blob
) DECLSPEC_HIDDEN
;
426 BOOL
ContextPropertyList_SetProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
,
427 const BYTE
*pbData
, size_t cbData
) DECLSPEC_HIDDEN
;
429 void ContextPropertyList_RemoveProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
) DECLSPEC_HIDDEN
;
431 DWORD
ContextPropertyList_EnumPropIDs(CONTEXT_PROPERTY_LIST
*list
, DWORD id
) DECLSPEC_HIDDEN
;
433 void ContextPropertyList_Copy(CONTEXT_PROPERTY_LIST
*to
,
434 CONTEXT_PROPERTY_LIST
*from
) DECLSPEC_HIDDEN
;
436 void ContextPropertyList_Free(CONTEXT_PROPERTY_LIST
*list
) DECLSPEC_HIDDEN
;
438 extern WINECRYPT_CERTSTORE empty_store DECLSPEC_HIDDEN
;
439 void init_empty_store(void) DECLSPEC_HIDDEN
;
445 /* Align up to a DWORD_PTR boundary
447 #define ALIGN_DWORD_PTR(x) (((x) + sizeof(DWORD_PTR) - 1) & ~(sizeof(DWORD_PTR) - 1))
448 #define POINTER_ALIGN_DWORD_PTR(p) ((LPVOID)ALIGN_DWORD_PTR((DWORD_PTR)(p)))
450 /* Check if the OID is a small int
452 #define IS_INTOID(x) (((ULONG_PTR)(x) >> 16) == 0)
458 BOOL (WINAPI
*enum_root_certs
)( void *buffer
, SIZE_T size
, SIZE_T
*needed
);
459 BOOL (WINAPI
*import_cert_store
)( CRYPT_DATA_BLOB
*pfx
, const WCHAR
*password
, DWORD flags
,
460 void **key_ret
, void ***chain_ret
, DWORD
*count_ret
);
463 extern const struct unix_funcs
*unix_funcs DECLSPEC_HIDDEN
;