configure: Add a check for sys/ucontext.h and include it where appropriate.
[wine.git] / dlls / crypt32 / crypt32_private.h
blob02bd902fa4412943a7ddabb90eda62698e8215a4
1 /*
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
61 BYTE tag;
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
78 DWORD version;
79 CRYPT_ALGORITHM_IDENTIFIER DigestAlgorithm;
80 CRYPT_CONTENT_INFO ContentInfo;
81 CRYPT_HASH_BLOB hash;
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
89 LPSTR contentType;
90 CRYPT_ALGORITHM_IDENTIFIER contentEncryptionAlgorithm;
91 CRYPT_DATA_BLOB encryptedContent;
92 } CRYPT_ENCRYPTED_CONTENT_INFO;
94 typedef struct _CRYPT_ENVELOPED_DATA
96 DWORD version;
97 DWORD cRecipientInfo;
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
111 DWORD version;
112 DWORD cCertEncoded;
113 PCERT_BLOB rgCertEncoded;
114 DWORD cCrlEncoded;
115 PCRL_BLOB rgCrlEncoded;
116 CRYPT_CONTENT_INFO content;
117 DWORD cSignerInfo;
118 PCMSG_CMS_SIGNER_INFO rgSignerInfo;
119 } CRYPT_SIGNED_INFO;
121 BOOL CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO *, void *pvData,
122 DWORD *pcbData) DECLSPEC_HIDDEN;
124 BOOL CRYPT_AsnDecodeCMSSignedInfo(const BYTE *pbEncoded, DWORD cbEncoded,
125 DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
126 CRYPT_SIGNED_INFO *signedInfo, DWORD *pcbSignedInfo) DECLSPEC_HIDDEN;
128 /* Helper function to check *pcbEncoded, set it to the required size, and
129 * optionally to allocate memory. Assumes pbEncoded is not NULL.
130 * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
131 * pointer to the newly allocated memory.
133 BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara,
134 BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded) DECLSPEC_HIDDEN;
136 BOOL CRYPT_AsnDecodePKCSDigestedData(const BYTE *pbEncoded, DWORD cbEncoded,
137 DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
138 CRYPT_DIGESTED_DATA *digestedData, DWORD *pcbDigestedData) DECLSPEC_HIDDEN;
140 BOOL WINAPI CRYPT_AsnEncodePubKeyInfoNoNull(DWORD dwCertEncodingType,
141 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
142 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded) DECLSPEC_HIDDEN;
144 /* The following aren't defined in wincrypt.h, as they're "reserved" */
145 #define CERT_CERT_PROP_ID 32
146 #define CERT_CRL_PROP_ID 33
147 #define CERT_CTL_PROP_ID 34
149 /* Returns a handle to the default crypto provider; loads it if necessary.
150 * Returns NULL on failure.
152 HCRYPTPROV CRYPT_GetDefaultProvider(void) DECLSPEC_HIDDEN;
154 HINSTANCE hInstance DECLSPEC_HIDDEN;
156 void crypt_oid_init(void) DECLSPEC_HIDDEN;
157 void crypt_oid_free(void) DECLSPEC_HIDDEN;
158 void crypt_sip_free(void) DECLSPEC_HIDDEN;
159 void root_store_free(void) DECLSPEC_HIDDEN;
160 void default_chain_engine_free(void) DECLSPEC_HIDDEN;
162 /* (Internal) certificate store types and functions */
163 struct WINE_CRYPTCERTSTORE;
165 typedef struct _CONTEXT_PROPERTY_LIST CONTEXT_PROPERTY_LIST;
167 typedef struct _context_t context_t;
169 typedef struct {
170 void (*free)(context_t*);
171 struct _context_t *(*clone)(context_t*,struct WINE_CRYPTCERTSTORE*,BOOL);
172 } context_vtbl_t;
174 struct _context_t {
175 const context_vtbl_t *vtbl;
176 LONG ref;
177 struct WINE_CRYPTCERTSTORE *store;
178 struct _context_t *linked;
179 CONTEXT_PROPERTY_LIST *properties;
180 union {
181 struct list entry;
182 void *ptr;
183 } u;
186 static inline context_t *context_from_ptr(const void *ptr)
188 return (context_t*)ptr-1;
191 static inline void *context_ptr(context_t *context)
193 return context+1;
196 typedef struct {
197 context_t base;
198 CERT_CONTEXT ctx;
199 } cert_t;
201 static inline cert_t *cert_from_ptr(const CERT_CONTEXT *ptr)
203 return CONTAINING_RECORD(ptr, cert_t, ctx);
206 typedef struct {
207 context_t base;
208 CRL_CONTEXT ctx;
209 } crl_t;
211 static inline crl_t *crl_from_ptr(const CRL_CONTEXT *ptr)
213 return CONTAINING_RECORD(ptr, crl_t, ctx);
216 typedef struct {
217 context_t base;
218 CTL_CONTEXT ctx;
219 } ctl_t;
221 static inline ctl_t *ctl_from_ptr(const CTL_CONTEXT *ptr)
223 return CONTAINING_RECORD(ptr, ctl_t, ctx);
226 /* Some typedefs that make it easier to abstract which type of context we're
227 * working with.
229 typedef const void *(WINAPI *CreateContextFunc)(DWORD dwCertEncodingType,
230 const BYTE *pbCertEncoded, DWORD cbCertEncoded);
231 typedef BOOL (WINAPI *AddContextToStoreFunc)(HCERTSTORE hCertStore,
232 const void *context, DWORD dwAddDisposition, const void **ppStoreContext);
233 typedef BOOL (WINAPI *AddEncodedContextToStoreFunc)(HCERTSTORE hCertStore,
234 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
235 DWORD dwAddDisposition, const void **ppContext);
236 typedef const void *(WINAPI *EnumContextsInStoreFunc)(HCERTSTORE hCertStore,
237 const void *pPrevContext);
238 typedef DWORD (WINAPI *EnumPropertiesFunc)(const void *context, DWORD dwPropId);
239 typedef BOOL (WINAPI *GetContextPropertyFunc)(const void *context,
240 DWORD dwPropID, void *pvData, DWORD *pcbData);
241 typedef BOOL (WINAPI *SetContextPropertyFunc)(const void *context,
242 DWORD dwPropID, DWORD dwFlags, const void *pvData);
243 typedef BOOL (WINAPI *SerializeElementFunc)(const void *context, DWORD dwFlags,
244 BYTE *pbElement, DWORD *pcbElement);
245 typedef BOOL (WINAPI *DeleteContextFunc)(const void *contex);
247 /* An abstract context (certificate, CRL, or CTL) interface */
248 typedef struct _WINE_CONTEXT_INTERFACE
250 CreateContextFunc create;
251 AddContextToStoreFunc addContextToStore;
252 AddEncodedContextToStoreFunc addEncodedToStore;
253 EnumContextsInStoreFunc enumContextsInStore;
254 EnumPropertiesFunc enumProps;
255 GetContextPropertyFunc getProp;
256 SetContextPropertyFunc setProp;
257 SerializeElementFunc serialize;
258 DeleteContextFunc deleteFromStore;
259 } WINE_CONTEXT_INTERFACE;
261 extern const WINE_CONTEXT_INTERFACE *pCertInterface DECLSPEC_HIDDEN;
262 extern const WINE_CONTEXT_INTERFACE *pCRLInterface DECLSPEC_HIDDEN;
263 extern const WINE_CONTEXT_INTERFACE *pCTLInterface DECLSPEC_HIDDEN;
265 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
266 DWORD dwFlags, const void *pvPara);
268 typedef struct _CONTEXT_FUNCS
270 /* Called to add a context to a store. If toReplace is not NULL,
271 * context replaces toReplace in the store, and access checks should not be
272 * performed. Otherwise context is a new context, and it should only be
273 * added if the store allows it. If ppStoreContext is not NULL, the added
274 * context should be returned in *ppStoreContext.
276 BOOL (*addContext)(struct WINE_CRYPTCERTSTORE*,context_t*,context_t*,context_t**,BOOL);
277 context_t *(*enumContext)(struct WINE_CRYPTCERTSTORE *store, context_t *prev);
278 BOOL (*delete)(struct WINE_CRYPTCERTSTORE*,context_t*);
279 } CONTEXT_FUNCS;
281 typedef enum _CertStoreType {
282 StoreTypeMem,
283 StoreTypeCollection,
284 StoreTypeProvider,
285 StoreTypeEmpty
286 } CertStoreType;
288 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
290 /* A cert store is polymorphic through the use of function pointers. A type
291 * is still needed to distinguish collection stores from other types.
292 * On the function pointers:
293 * - closeStore is called when the store's ref count becomes 0
294 * - control is optional, but should be implemented by any store that supports
295 * persistence
298 typedef struct {
299 void (*addref)(struct WINE_CRYPTCERTSTORE*);
300 DWORD (*release)(struct WINE_CRYPTCERTSTORE*,DWORD);
301 void (*releaseContext)(struct WINE_CRYPTCERTSTORE*,context_t*);
302 BOOL (*control)(struct WINE_CRYPTCERTSTORE*,DWORD,DWORD,void const*);
303 CONTEXT_FUNCS certs;
304 CONTEXT_FUNCS crls;
305 CONTEXT_FUNCS ctls;
306 } store_vtbl_t;
308 typedef struct WINE_CRYPTCERTSTORE
310 DWORD dwMagic;
311 LONG ref;
312 DWORD dwOpenFlags;
313 CertStoreType type;
314 const store_vtbl_t *vtbl;
315 CONTEXT_PROPERTY_LIST *properties;
316 } WINECRYPT_CERTSTORE;
318 void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, DWORD dwFlags,
319 CertStoreType type, const store_vtbl_t*) DECLSPEC_HIDDEN;
320 void CRYPT_FreeStore(WINECRYPT_CERTSTORE *store) DECLSPEC_HIDDEN;
321 BOOL WINAPI I_CertUpdateStore(HCERTSTORE store1, HCERTSTORE store2, DWORD unk0,
322 DWORD unk1) DECLSPEC_HIDDEN;
324 WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
325 DWORD dwFlags, const void *pvPara) DECLSPEC_HIDDEN;
326 WINECRYPT_CERTSTORE *CRYPT_ProvCreateStore(DWORD dwFlags,
327 WINECRYPT_CERTSTORE *memStore, const CERT_STORE_PROV_INFO *pProvInfo) DECLSPEC_HIDDEN;
328 WINECRYPT_CERTSTORE *CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
329 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
330 const void *pvPara) DECLSPEC_HIDDEN;
331 WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
332 const void *pvPara) DECLSPEC_HIDDEN;
333 WINECRYPT_CERTSTORE *CRYPT_FileOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
334 const void *pvPara) DECLSPEC_HIDDEN;
335 WINECRYPT_CERTSTORE *CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
336 DWORD dwFlags, const void *pvPara) DECLSPEC_HIDDEN;
337 WINECRYPT_CERTSTORE *CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
338 DWORD dwFlags, const void *pvPara) DECLSPEC_HIDDEN;
339 WINECRYPT_CERTSTORE *CRYPT_RootOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags) DECLSPEC_HIDDEN;
341 /* Allocates and initializes a certificate chain engine, but without creating
342 * the root store. Instead, it uses root, and assumes the caller has done any
343 * checking necessary.
345 HCERTCHAINENGINE CRYPT_CreateChainEngine(HCERTSTORE, DWORD, const CERT_CHAIN_ENGINE_CONFIG*) DECLSPEC_HIDDEN;
347 /* Helper function for store reading functions and
348 * CertAddSerializedElementToStore. Returns a context of the appropriate type
349 * if it can, or NULL otherwise. Doesn't validate any of the properties in
350 * the serialized context (for example, bad hashes are retained.)
351 * *pdwContentType is set to the type of the returned context.
353 const void *CRYPT_ReadSerializedElement(const BYTE *pbElement,
354 DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType) DECLSPEC_HIDDEN;
356 /* Reads contexts serialized in the file into the memory store. Returns FALSE
357 * if the file is not of the expected format.
359 BOOL CRYPT_ReadSerializedStoreFromFile(HANDLE file, HCERTSTORE store) DECLSPEC_HIDDEN;
361 /* Reads contexts serialized in the blob into the memory store. Returns FALSE
362 * if the file is not of the expected format.
364 BOOL CRYPT_ReadSerializedStoreFromBlob(const CRYPT_DATA_BLOB *blob,
365 HCERTSTORE store) DECLSPEC_HIDDEN;
367 /* Fixes up the pointers in info, where info is assumed to be a
368 * CRYPT_KEY_PROV_INFO, followed by its container name, provider name, and any
369 * provider parameters, in a contiguous buffer, but where info's pointers are
370 * assumed to be invalid. Upon return, info's pointers point to the
371 * appropriate memory locations.
373 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info) DECLSPEC_HIDDEN;
376 * String functions
379 DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indent,
380 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz) DECLSPEC_HIDDEN;
383 * Context functions
386 /* Allocates a new data context, a context which owns properties directly.
387 * contextSize is the size of the public data type associated with context,
388 * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
389 * Free with Context_Release.
391 context_t *Context_CreateDataContext(size_t contextSize, const context_vtbl_t *vtbl, struct WINE_CRYPTCERTSTORE*) DECLSPEC_HIDDEN;
393 /* Creates a new link context. The context refers to linked
394 * rather than owning its own properties. If addRef is TRUE (which ordinarily
395 * it should be) linked is addref'd.
396 * Free with Context_Release.
398 context_t *Context_CreateLinkContext(unsigned contextSize, context_t *linked, struct WINE_CRYPTCERTSTORE*) DECLSPEC_HIDDEN;
400 /* Copies properties from fromContext to toContext. */
401 void Context_CopyProperties(const void *to, const void *from) DECLSPEC_HIDDEN;
403 void Context_AddRef(context_t*) DECLSPEC_HIDDEN;
404 void Context_Release(context_t *context) DECLSPEC_HIDDEN;
405 void Context_Free(context_t*) DECLSPEC_HIDDEN;
408 * Context property list functions
411 CONTEXT_PROPERTY_LIST *ContextPropertyList_Create(void) DECLSPEC_HIDDEN;
413 /* Searches for the property with ID id in the context. Returns TRUE if found,
414 * and copies the property's length and a pointer to its data to blob.
415 * Otherwise returns FALSE.
417 BOOL ContextPropertyList_FindProperty(CONTEXT_PROPERTY_LIST *list, DWORD id,
418 PCRYPT_DATA_BLOB blob) DECLSPEC_HIDDEN;
420 BOOL ContextPropertyList_SetProperty(CONTEXT_PROPERTY_LIST *list, DWORD id,
421 const BYTE *pbData, size_t cbData) DECLSPEC_HIDDEN;
423 void ContextPropertyList_RemoveProperty(CONTEXT_PROPERTY_LIST *list, DWORD id) DECLSPEC_HIDDEN;
425 DWORD ContextPropertyList_EnumPropIDs(CONTEXT_PROPERTY_LIST *list, DWORD id) DECLSPEC_HIDDEN;
427 void ContextPropertyList_Copy(CONTEXT_PROPERTY_LIST *to,
428 CONTEXT_PROPERTY_LIST *from) DECLSPEC_HIDDEN;
430 void ContextPropertyList_Free(CONTEXT_PROPERTY_LIST *list) DECLSPEC_HIDDEN;
432 extern WINECRYPT_CERTSTORE empty_store;
433 void init_empty_store(void) DECLSPEC_HIDDEN;
436 * Utilities.
439 /* Align up to a DWORD_PTR boundary
441 #define ALIGN_DWORD_PTR(x) (((x) + sizeof(DWORD_PTR) - 1) & ~(sizeof(DWORD_PTR) - 1))
442 #define POINTER_ALIGN_DWORD_PTR(p) ((LPVOID)ALIGN_DWORD_PTR((DWORD_PTR)(p)))
444 /* Check if the OID is a small int
446 #define IS_INTOID(x) (((ULONG_PTR)(x) >> 16) == 0)
448 #endif