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 BOOL
CNG_ImportPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
) DECLSPEC_HIDDEN
;
26 /* a few asn.1 tags we need */
27 #define ASN_BOOL (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x01)
28 #define ASN_BITSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x03)
29 #define ASN_ENUMERATED (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0a)
30 #define ASN_UTF8STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0c)
31 #define ASN_SETOF (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x11)
32 #define ASN_NUMERICSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x12)
33 #define ASN_PRINTABLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x13)
34 #define ASN_T61STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x14)
35 #define ASN_VIDEOTEXSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x15)
36 #define ASN_IA5STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x16)
37 #define ASN_UTCTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x17)
38 #define ASN_GENERALTIME (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x18)
39 #define ASN_GRAPHICSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x19)
40 #define ASN_VISIBLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1a)
41 #define ASN_GENERALSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1b)
42 #define ASN_UNIVERSALSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1c)
43 #define ASN_BMPSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1e)
45 BOOL
CRYPT_EncodeLen(DWORD len
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
47 typedef BOOL (WINAPI
*CryptEncodeObjectExFunc
)(DWORD
, LPCSTR
, const void *,
48 DWORD
, PCRYPT_ENCODE_PARA
, BYTE
*, DWORD
*);
50 struct AsnEncodeSequenceItem
52 const void *pvStructInfo
;
53 CryptEncodeObjectExFunc encodeFunc
;
54 DWORD size
; /* used during encoding, not for your use */
57 BOOL WINAPI
CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType
,
58 struct AsnEncodeSequenceItem items
[], DWORD cItem
, DWORD dwFlags
,
59 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
61 struct AsnConstructedItem
64 const void *pvStructInfo
;
65 CryptEncodeObjectExFunc encodeFunc
;
68 BOOL WINAPI
CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType
,
69 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
70 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
71 BOOL WINAPI
CRYPT_AsnEncodeOid(DWORD dwCertEncodingType
,
72 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
73 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
74 BOOL WINAPI
CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType
,
75 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
76 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
78 typedef struct _CRYPT_DIGESTED_DATA
81 CRYPT_ALGORITHM_IDENTIFIER DigestAlgorithm
;
82 CRYPT_CONTENT_INFO ContentInfo
;
84 } CRYPT_DIGESTED_DATA
;
86 BOOL
CRYPT_AsnEncodePKCSDigestedData(const CRYPT_DIGESTED_DATA
*digestedData
,
87 void *pvData
, DWORD
*pcbData
) DECLSPEC_HIDDEN
;
89 typedef struct _CRYPT_ENCRYPTED_CONTENT_INFO
92 CRYPT_ALGORITHM_IDENTIFIER contentEncryptionAlgorithm
;
93 CRYPT_DATA_BLOB encryptedContent
;
94 } CRYPT_ENCRYPTED_CONTENT_INFO
;
96 typedef struct _CRYPT_ENVELOPED_DATA
100 PCMSG_KEY_TRANS_RECIPIENT_INFO rgRecipientInfo
;
101 CRYPT_ENCRYPTED_CONTENT_INFO encryptedContentInfo
;
102 } CRYPT_ENVELOPED_DATA
;
104 BOOL
CRYPT_AsnEncodePKCSEnvelopedData(const CRYPT_ENVELOPED_DATA
*envelopedData
,
105 void *pvData
, DWORD
*pcbData
) DECLSPEC_HIDDEN
;
107 BOOL
CRYPT_AsnDecodePKCSEnvelopedData(const BYTE
*pbEncoded
, DWORD cbEncoded
,
108 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
109 CRYPT_ENVELOPED_DATA
*envelopedData
, DWORD
*pcbEnvelopedData
) DECLSPEC_HIDDEN
;
111 typedef struct _CRYPT_SIGNED_INFO
115 PCERT_BLOB rgCertEncoded
;
117 PCRL_BLOB rgCrlEncoded
;
118 CRYPT_CONTENT_INFO content
;
120 PCMSG_CMS_SIGNER_INFO rgSignerInfo
;
121 PDWORD signerKeySpec
;
124 BOOL
CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO
*, void *pvData
,
125 DWORD
*pcbData
) DECLSPEC_HIDDEN
;
127 BOOL
CRYPT_AsnDecodeCMSSignedInfo(const BYTE
*pbEncoded
, DWORD cbEncoded
,
128 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
129 CRYPT_SIGNED_INFO
*signedInfo
, DWORD
*pcbSignedInfo
) DECLSPEC_HIDDEN
;
131 /* Helper function to check *pcbEncoded, set it to the required size, and
132 * optionally to allocate memory. Assumes pbEncoded is not NULL.
133 * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
134 * pointer to the newly allocated memory.
136 BOOL
CRYPT_EncodeEnsureSpace(DWORD dwFlags
, const CRYPT_ENCODE_PARA
*pEncodePara
,
137 BYTE
*pbEncoded
, DWORD
*pcbEncoded
, DWORD bytesNeeded
) DECLSPEC_HIDDEN
;
139 BOOL
CRYPT_AsnDecodePKCSDigestedData(const BYTE
*pbEncoded
, DWORD cbEncoded
,
140 DWORD dwFlags
, PCRYPT_DECODE_PARA pDecodePara
,
141 CRYPT_DIGESTED_DATA
*digestedData
, DWORD
*pcbDigestedData
) DECLSPEC_HIDDEN
;
143 BOOL WINAPI
CRYPT_AsnEncodePubKeyInfoNoNull(DWORD dwCertEncodingType
,
144 LPCSTR lpszStructType
, const void *pvStructInfo
, DWORD dwFlags
,
145 PCRYPT_ENCODE_PARA pEncodePara
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
) DECLSPEC_HIDDEN
;
147 /* The following aren't defined in wincrypt.h, as they're "reserved" */
148 #define CERT_CERT_PROP_ID 32
149 #define CERT_CRL_PROP_ID 33
150 #define CERT_CTL_PROP_ID 34
152 /* Returns a handle to the default crypto provider; loads it if necessary.
153 * Returns NULL on failure.
155 HCRYPTPROV WINAPI
I_CryptGetDefaultCryptProv(ALG_ID
);
157 extern HINSTANCE hInstance DECLSPEC_HIDDEN
;
159 void crypt_oid_init(void) DECLSPEC_HIDDEN
;
160 void crypt_oid_free(void) DECLSPEC_HIDDEN
;
161 void crypt_sip_free(void) DECLSPEC_HIDDEN
;
162 void root_store_free(void) DECLSPEC_HIDDEN
;
163 void default_chain_engine_free(void) DECLSPEC_HIDDEN
;
165 /* (Internal) certificate store types and functions */
166 struct WINE_CRYPTCERTSTORE
;
168 typedef struct _CONTEXT_PROPERTY_LIST CONTEXT_PROPERTY_LIST
;
170 typedef struct _context_t context_t
;
173 void (*free
)(context_t
*);
174 struct _context_t
*(*clone
)(context_t
*,struct WINE_CRYPTCERTSTORE
*,BOOL
);
178 const context_vtbl_t
*vtbl
;
180 struct WINE_CRYPTCERTSTORE
*store
;
181 struct _context_t
*linked
;
182 CONTEXT_PROPERTY_LIST
*properties
;
189 static inline context_t
*context_from_ptr(const void *ptr
)
191 return (context_t
*)ptr
-1;
194 static inline void *context_ptr(context_t
*context
)
204 static inline cert_t
*cert_from_ptr(const CERT_CONTEXT
*ptr
)
206 return CONTAINING_RECORD(ptr
, cert_t
, ctx
);
214 static inline crl_t
*crl_from_ptr(const CRL_CONTEXT
*ptr
)
216 return CONTAINING_RECORD(ptr
, crl_t
, ctx
);
224 static inline ctl_t
*ctl_from_ptr(const CTL_CONTEXT
*ptr
)
226 return CONTAINING_RECORD(ptr
, ctl_t
, ctx
);
229 /* Some typedefs that make it easier to abstract which type of context we're
232 typedef const void *(WINAPI
*CreateContextFunc
)(DWORD dwCertEncodingType
,
233 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
);
234 typedef BOOL (WINAPI
*AddContextToStoreFunc
)(HCERTSTORE hCertStore
,
235 const void *context
, DWORD dwAddDisposition
, const void **ppStoreContext
);
236 typedef BOOL (WINAPI
*AddEncodedContextToStoreFunc
)(HCERTSTORE hCertStore
,
237 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
238 DWORD dwAddDisposition
, const void **ppContext
);
239 typedef const void *(WINAPI
*EnumContextsInStoreFunc
)(HCERTSTORE hCertStore
,
240 const void *pPrevContext
);
241 typedef DWORD (WINAPI
*EnumPropertiesFunc
)(const void *context
, DWORD dwPropId
);
242 typedef BOOL (WINAPI
*GetContextPropertyFunc
)(const void *context
,
243 DWORD dwPropID
, void *pvData
, DWORD
*pcbData
);
244 typedef BOOL (WINAPI
*SetContextPropertyFunc
)(const void *context
,
245 DWORD dwPropID
, DWORD dwFlags
, const void *pvData
);
246 typedef BOOL (WINAPI
*SerializeElementFunc
)(const void *context
, DWORD dwFlags
,
247 BYTE
*pbElement
, DWORD
*pcbElement
);
248 typedef BOOL (WINAPI
*DeleteContextFunc
)(const void *contex
);
250 /* An abstract context (certificate, CRL, or CTL) interface */
251 typedef struct _WINE_CONTEXT_INTERFACE
253 CreateContextFunc create
;
254 AddContextToStoreFunc addContextToStore
;
255 AddEncodedContextToStoreFunc addEncodedToStore
;
256 EnumContextsInStoreFunc enumContextsInStore
;
257 EnumPropertiesFunc enumProps
;
258 GetContextPropertyFunc getProp
;
259 SetContextPropertyFunc setProp
;
260 SerializeElementFunc serialize
;
261 DeleteContextFunc deleteFromStore
;
262 } WINE_CONTEXT_INTERFACE
;
264 extern const WINE_CONTEXT_INTERFACE
*pCertInterface DECLSPEC_HIDDEN
;
265 extern const WINE_CONTEXT_INTERFACE
*pCRLInterface DECLSPEC_HIDDEN
;
266 extern const WINE_CONTEXT_INTERFACE
*pCTLInterface DECLSPEC_HIDDEN
;
268 typedef struct WINE_CRYPTCERTSTORE
* (*StoreOpenFunc
)(HCRYPTPROV hCryptProv
,
269 DWORD dwFlags
, const void *pvPara
);
271 typedef struct _CONTEXT_FUNCS
273 /* Called to add a context to a store. If toReplace is not NULL,
274 * context replaces toReplace in the store, and access checks should not be
275 * performed. Otherwise context is a new context, and it should only be
276 * added if the store allows it. If ppStoreContext is not NULL, the added
277 * context should be returned in *ppStoreContext.
279 BOOL (*addContext
)(struct WINE_CRYPTCERTSTORE
*,context_t
*,context_t
*,context_t
**,BOOL
);
280 context_t
*(*enumContext
)(struct WINE_CRYPTCERTSTORE
*store
, context_t
*prev
);
281 BOOL (*delete)(struct WINE_CRYPTCERTSTORE
*,context_t
*);
284 typedef enum _CertStoreType
{
291 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
293 /* A cert store is polymorphic through the use of function pointers. A type
294 * is still needed to distinguish collection stores from other types.
295 * On the function pointers:
296 * - closeStore is called when the store's ref count becomes 0
297 * - control is optional, but should be implemented by any store that supports
302 void (*addref
)(struct WINE_CRYPTCERTSTORE
*);
303 DWORD (*release
)(struct WINE_CRYPTCERTSTORE
*,DWORD
);
304 void (*releaseContext
)(struct WINE_CRYPTCERTSTORE
*,context_t
*);
305 BOOL (*control
)(struct WINE_CRYPTCERTSTORE
*,DWORD
,DWORD
,void const*);
311 typedef struct WINE_CRYPTCERTSTORE
317 const store_vtbl_t
*vtbl
;
318 CONTEXT_PROPERTY_LIST
*properties
;
319 } WINECRYPT_CERTSTORE
;
321 void CRYPT_InitStore(WINECRYPT_CERTSTORE
*store
, DWORD dwFlags
,
322 CertStoreType type
, const store_vtbl_t
*) DECLSPEC_HIDDEN
;
323 void CRYPT_FreeStore(WINECRYPT_CERTSTORE
*store
) DECLSPEC_HIDDEN
;
324 BOOL WINAPI
I_CertUpdateStore(HCERTSTORE store1
, HCERTSTORE store2
, DWORD unk0
,
325 DWORD unk1
) DECLSPEC_HIDDEN
;
327 WINECRYPT_CERTSTORE
*CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv
,
328 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
329 WINECRYPT_CERTSTORE
*CRYPT_ProvCreateStore(DWORD dwFlags
,
330 WINECRYPT_CERTSTORE
*memStore
, const CERT_STORE_PROV_INFO
*pProvInfo
) DECLSPEC_HIDDEN
;
331 WINECRYPT_CERTSTORE
*CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider
,
332 DWORD dwEncodingType
, HCRYPTPROV hCryptProv
, DWORD dwFlags
,
333 const void *pvPara
) DECLSPEC_HIDDEN
;
334 WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
335 const void *pvPara
) DECLSPEC_HIDDEN
;
336 WINECRYPT_CERTSTORE
*CRYPT_FileOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
337 const void *pvPara
) DECLSPEC_HIDDEN
;
338 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv
,
339 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
340 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv
,
341 DWORD dwFlags
, const void *pvPara
) DECLSPEC_HIDDEN
;
343 void CRYPT_ImportSystemRootCertsToReg(void) DECLSPEC_HIDDEN
;
344 BOOL
CRYPT_SerializeContextsToReg(HKEY key
, DWORD flags
, const WINE_CONTEXT_INTERFACE
*contextInterface
,
345 HCERTSTORE memStore
) DECLSPEC_HIDDEN
;
347 DWORD
CRYPT_IsCertificateSelfSigned(const CERT_CONTEXT
*cert
) DECLSPEC_HIDDEN
;
349 /* Allocates and initializes a certificate chain engine, but without creating
350 * the root store. Instead, it uses root, and assumes the caller has done any
351 * checking necessary.
353 HCERTCHAINENGINE
CRYPT_CreateChainEngine(HCERTSTORE
, DWORD
, const CERT_CHAIN_ENGINE_CONFIG
*) DECLSPEC_HIDDEN
;
355 /* Helper function for store reading functions and
356 * CertAddSerializedElementToStore. Returns a context of the appropriate type
357 * if it can, or NULL otherwise. Doesn't validate any of the properties in
358 * the serialized context (for example, bad hashes are retained.)
359 * *pdwContentType is set to the type of the returned context.
361 const void *CRYPT_ReadSerializedElement(const BYTE
*pbElement
,
362 DWORD cbElement
, DWORD dwContextTypeFlags
, DWORD
*pdwContentType
) DECLSPEC_HIDDEN
;
364 /* Reads contexts serialized in the file into the memory store. Returns FALSE
365 * if the file is not of the expected format.
367 BOOL
CRYPT_ReadSerializedStoreFromFile(HANDLE file
, HCERTSTORE store
) DECLSPEC_HIDDEN
;
369 /* Reads contexts serialized in the blob into the memory store. Returns FALSE
370 * if the file is not of the expected format.
372 BOOL
CRYPT_ReadSerializedStoreFromBlob(const CRYPT_DATA_BLOB
*blob
,
373 HCERTSTORE store
) DECLSPEC_HIDDEN
;
375 struct store_CERT_KEY_CONTEXT
381 void CRYPT_ConvertKeyContext(const struct store_CERT_KEY_CONTEXT
*src
, CERT_KEY_CONTEXT
*dst
) DECLSPEC_HIDDEN
;
387 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indent
,
388 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
) DECLSPEC_HIDDEN
;
394 /* Allocates a new data context, a context which owns properties directly.
395 * contextSize is the size of the public data type associated with context,
396 * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
397 * Free with Context_Release.
399 context_t
*Context_CreateDataContext(size_t contextSize
, const context_vtbl_t
*vtbl
, struct WINE_CRYPTCERTSTORE
*) DECLSPEC_HIDDEN
;
401 /* Creates a new link context. The context refers to linked
402 * rather than owning its own properties. If addRef is TRUE (which ordinarily
403 * it should be) linked is addref'd.
404 * Free with Context_Release.
406 context_t
*Context_CreateLinkContext(unsigned contextSize
, context_t
*linked
, struct WINE_CRYPTCERTSTORE
*) DECLSPEC_HIDDEN
;
408 /* Copies properties from fromContext to toContext. */
409 void Context_CopyProperties(const void *to
, const void *from
) DECLSPEC_HIDDEN
;
411 void Context_AddRef(context_t
*) DECLSPEC_HIDDEN
;
412 void Context_Release(context_t
*context
) DECLSPEC_HIDDEN
;
413 void Context_Free(context_t
*) DECLSPEC_HIDDEN
;
416 * Context property list functions
419 CONTEXT_PROPERTY_LIST
*ContextPropertyList_Create(void) DECLSPEC_HIDDEN
;
421 /* Searches for the property with ID id in the context. Returns TRUE if found,
422 * and copies the property's length and a pointer to its data to blob.
423 * Otherwise returns FALSE.
425 BOOL
ContextPropertyList_FindProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
,
426 PCRYPT_DATA_BLOB blob
) DECLSPEC_HIDDEN
;
428 BOOL
ContextPropertyList_SetProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
,
429 const BYTE
*pbData
, size_t cbData
) DECLSPEC_HIDDEN
;
431 void ContextPropertyList_RemoveProperty(CONTEXT_PROPERTY_LIST
*list
, DWORD id
) DECLSPEC_HIDDEN
;
433 DWORD
ContextPropertyList_EnumPropIDs(CONTEXT_PROPERTY_LIST
*list
, DWORD id
) DECLSPEC_HIDDEN
;
435 void ContextPropertyList_Copy(CONTEXT_PROPERTY_LIST
*to
,
436 CONTEXT_PROPERTY_LIST
*from
) DECLSPEC_HIDDEN
;
438 void ContextPropertyList_Free(CONTEXT_PROPERTY_LIST
*list
) DECLSPEC_HIDDEN
;
440 extern WINECRYPT_CERTSTORE empty_store DECLSPEC_HIDDEN
;
441 void init_empty_store(void) DECLSPEC_HIDDEN
;
447 /* Align up to a DWORD_PTR boundary
449 #define ALIGN_DWORD_PTR(x) (((x) + sizeof(DWORD_PTR) - 1) & ~(sizeof(DWORD_PTR) - 1))
450 #define POINTER_ALIGN_DWORD_PTR(p) ((LPVOID)ALIGN_DWORD_PTR((DWORD_PTR)(p)))
452 /* Check if the OID is a small int
454 #define IS_INTOID(x) (((ULONG_PTR)(x) >> 16) == 0)
460 BOOL (WINAPI
*enum_root_certs
)( void *buffer
, SIZE_T size
, SIZE_T
*needed
);
461 BOOL (WINAPI
*import_cert_store
)( CRYPT_DATA_BLOB
*pfx
, const WCHAR
*password
, DWORD flags
,
462 void **key_ret
, void ***chain_ret
, DWORD
*count_ret
);
465 extern const struct unix_funcs
*unix_funcs DECLSPEC_HIDDEN
;