hlink: Use an iface instead of a vtbl pointer in HlinkBCImpl.
[wine] / dlls / crypt32 / crypt32_private.h
1 /*
2  * Copyright 2005 Juan Lang
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #ifndef __CRYPT32_PRIVATE_H__
20 #define __CRYPT32_PRIVATE_H__
21
22 /* a few asn.1 tags we need */
23 #define ASN_BOOL            (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x01)
24 #define ASN_BITSTRING       (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x03)
25 #define ASN_ENUMERATED      (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0a)
26 #define ASN_UTF8STRING      (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0c)
27 #define ASN_SETOF           (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x11)
28 #define ASN_NUMERICSTRING   (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x12)
29 #define ASN_PRINTABLESTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x13)
30 #define ASN_T61STRING       (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x14)
31 #define ASN_VIDEOTEXSTRING  (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x15)
32 #define ASN_IA5STRING       (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x16)
33 #define ASN_UTCTIME         (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x17)
34 #define ASN_GENERALTIME     (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x18)
35 #define ASN_GRAPHICSTRING   (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x19)
36 #define ASN_VISIBLESTRING   (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1a)
37 #define ASN_GENERALSTRING   (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1b)
38 #define ASN_UNIVERSALSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1c)
39 #define ASN_BMPSTRING       (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x1e)
40
41 BOOL CRYPT_EncodeLen(DWORD len, BYTE *pbEncoded, DWORD *pcbEncoded);
42
43 typedef BOOL (WINAPI *CryptEncodeObjectExFunc)(DWORD, LPCSTR, const void *,
44  DWORD, PCRYPT_ENCODE_PARA, BYTE *, DWORD *);
45
46 struct AsnEncodeSequenceItem
47 {
48     const void             *pvStructInfo;
49     CryptEncodeObjectExFunc encodeFunc;
50     DWORD                   size; /* used during encoding, not for your use */
51 };
52
53 BOOL WINAPI CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType,
54  struct AsnEncodeSequenceItem items[], DWORD cItem, DWORD dwFlags,
55  PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
56
57 struct AsnConstructedItem
58 {
59     BYTE                    tag;
60     const void             *pvStructInfo;
61     CryptEncodeObjectExFunc encodeFunc;
62 };
63
64 BOOL WINAPI CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType,
65  LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
66  PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
67 BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
68  LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
69  PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
70 BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
71  LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
72  PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
73
74 typedef struct _CRYPT_DIGESTED_DATA
75 {
76     DWORD                      version;
77     CRYPT_ALGORITHM_IDENTIFIER DigestAlgorithm;
78     CRYPT_CONTENT_INFO         ContentInfo;
79     CRYPT_HASH_BLOB            hash;
80 } CRYPT_DIGESTED_DATA;
81
82 BOOL CRYPT_AsnEncodePKCSDigestedData(const CRYPT_DIGESTED_DATA *digestedData,
83  void *pvData, DWORD *pcbData);
84
85 typedef struct _CRYPT_ENCRYPTED_CONTENT_INFO
86 {
87     LPSTR                      contentType;
88     CRYPT_ALGORITHM_IDENTIFIER contentEncryptionAlgorithm;
89     CRYPT_DATA_BLOB            encryptedContent;
90 } CRYPT_ENCRYPTED_CONTENT_INFO;
91
92 typedef struct _CRYPT_ENVELOPED_DATA
93 {
94     DWORD                          version;
95     DWORD                          cRecipientInfo;
96     PCMSG_KEY_TRANS_RECIPIENT_INFO rgRecipientInfo;
97     CRYPT_ENCRYPTED_CONTENT_INFO   encryptedContentInfo;
98 } CRYPT_ENVELOPED_DATA;
99
100 BOOL CRYPT_AsnEncodePKCSEnvelopedData(const CRYPT_ENVELOPED_DATA *envelopedData,
101  void *pvData, DWORD *pcbData);
102
103 typedef struct _CRYPT_SIGNED_INFO
104 {
105     DWORD                 version;
106     DWORD                 cCertEncoded;
107     PCERT_BLOB            rgCertEncoded;
108     DWORD                 cCrlEncoded;
109     PCRL_BLOB             rgCrlEncoded;
110     CRYPT_CONTENT_INFO    content;
111     DWORD                 cSignerInfo;
112     PCMSG_CMS_SIGNER_INFO rgSignerInfo;
113 } CRYPT_SIGNED_INFO;
114
115 BOOL CRYPT_AsnEncodeCMSSignedInfo(CRYPT_SIGNED_INFO *, void *pvData,
116  DWORD *pcbData);
117
118 BOOL CRYPT_AsnDecodeCMSSignedInfo(const BYTE *pbEncoded, DWORD cbEncoded,
119  DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
120  CRYPT_SIGNED_INFO *signedInfo, DWORD *pcbSignedInfo);
121
122 /* Helper function to check *pcbEncoded, set it to the required size, and
123  * optionally to allocate memory.  Assumes pbEncoded is not NULL.
124  * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
125  * pointer to the newly allocated memory.
126  */
127 BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, const CRYPT_ENCODE_PARA *pEncodePara,
128  BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded);
129
130 BOOL CRYPT_AsnDecodePKCSDigestedData(const BYTE *pbEncoded, DWORD cbEncoded,
131  DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
132  CRYPT_DIGESTED_DATA *digestedData, DWORD *pcbDigestedData);
133
134 BOOL WINAPI CRYPT_AsnEncodePubKeyInfoNoNull(DWORD dwCertEncodingType,
135  LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
136  PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
137
138 /* The following aren't defined in wincrypt.h, as they're "reserved" */
139 #define CERT_CERT_PROP_ID 32
140 #define CERT_CRL_PROP_ID  33
141 #define CERT_CTL_PROP_ID  34
142
143 /* Returns a handle to the default crypto provider; loads it if necessary.
144  * Returns NULL on failure.
145  */
146 HCRYPTPROV CRYPT_GetDefaultProvider(void);
147
148 HINSTANCE hInstance;
149
150 void crypt_oid_init(void);
151 void crypt_oid_free(void);
152 void crypt_sip_free(void);
153 void root_store_free(void);
154 void default_chain_engine_free(void);
155
156 /* Some typedefs that make it easier to abstract which type of context we're
157  * working with.
158  */
159 typedef const void *(WINAPI *CreateContextFunc)(DWORD dwCertEncodingType,
160  const BYTE *pbCertEncoded, DWORD cbCertEncoded);
161 typedef BOOL (WINAPI *AddContextToStoreFunc)(HCERTSTORE hCertStore,
162  const void *context, DWORD dwAddDisposition, const void **ppStoreContext);
163 typedef BOOL (WINAPI *AddEncodedContextToStoreFunc)(HCERTSTORE hCertStore,
164  DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
165  DWORD dwAddDisposition, const void **ppContext);
166 typedef const void *(WINAPI *DuplicateContextFunc)(const void *context);
167 typedef const void *(WINAPI *EnumContextsInStoreFunc)(HCERTSTORE hCertStore,
168  const void *pPrevContext);
169 typedef DWORD (WINAPI *EnumPropertiesFunc)(const void *context, DWORD dwPropId);
170 typedef BOOL (WINAPI *GetContextPropertyFunc)(const void *context,
171  DWORD dwPropID, void *pvData, DWORD *pcbData);
172 typedef BOOL (WINAPI *SetContextPropertyFunc)(const void *context,
173  DWORD dwPropID, DWORD dwFlags, const void *pvData);
174 typedef BOOL (WINAPI *SerializeElementFunc)(const void *context, DWORD dwFlags,
175  BYTE *pbElement, DWORD *pcbElement);
176 typedef BOOL (WINAPI *FreeContextFunc)(const void *context);
177 typedef BOOL (WINAPI *DeleteContextFunc)(const void *context);
178
179 /* An abstract context (certificate, CRL, or CTL) interface */
180 typedef struct _WINE_CONTEXT_INTERFACE
181 {
182     CreateContextFunc            create;
183     AddContextToStoreFunc        addContextToStore;
184     AddEncodedContextToStoreFunc addEncodedToStore;
185     DuplicateContextFunc         duplicate;
186     EnumContextsInStoreFunc      enumContextsInStore;
187     EnumPropertiesFunc           enumProps;
188     GetContextPropertyFunc       getProp;
189     SetContextPropertyFunc       setProp;
190     SerializeElementFunc         serialize;
191     FreeContextFunc              free;
192     DeleteContextFunc            deleteFromStore;
193 } WINE_CONTEXT_INTERFACE, *PWINE_CONTEXT_INTERFACE;
194 typedef const WINE_CONTEXT_INTERFACE *PCWINE_CONTEXT_INTERFACE;
195
196 extern PCWINE_CONTEXT_INTERFACE pCertInterface;
197 extern PCWINE_CONTEXT_INTERFACE pCRLInterface;
198 extern PCWINE_CONTEXT_INTERFACE pCTLInterface;
199
200 /* (Internal) certificate store types and functions */
201 struct WINE_CRYPTCERTSTORE;
202
203 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
204  DWORD dwFlags, const void *pvPara);
205
206 /* Called to enumerate the next context in a store. */
207 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
208
209 /* Called to add a context to a store.  If toReplace is not NULL,
210  * context replaces toReplace in the store, and access checks should not be
211  * performed.  Otherwise context is a new context, and it should only be
212  * added if the store allows it.  If ppStoreContext is not NULL, the added
213  * context should be returned in *ppStoreContext.
214  */
215 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
216  void *toReplace, const void **ppStoreContext);
217
218 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
219
220 typedef struct _CONTEXT_FUNCS
221 {
222     AddFunc    addContext;
223     EnumFunc   enumContext;
224     DeleteFunc deleteContext;
225 } CONTEXT_FUNCS, *PCONTEXT_FUNCS;
226
227 typedef enum _CertStoreType {
228     StoreTypeMem,
229     StoreTypeCollection,
230     StoreTypeProvider,
231 } CertStoreType;
232
233 struct _CONTEXT_PROPERTY_LIST;
234 typedef struct _CONTEXT_PROPERTY_LIST *PCONTEXT_PROPERTY_LIST;
235
236 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
237
238 /* A cert store is polymorphic through the use of function pointers.  A type
239  * is still needed to distinguish collection stores from other types.
240  * On the function pointers:
241  * - closeStore is called when the store's ref count becomes 0
242  * - control is optional, but should be implemented by any store that supports
243  *   persistence
244  */
245 typedef struct WINE_CRYPTCERTSTORE
246 {
247     DWORD                       dwMagic;
248     LONG                        ref;
249     DWORD                       dwOpenFlags;
250     CertStoreType               type;
251     PFN_CERT_STORE_PROV_CLOSE   closeStore;
252     CONTEXT_FUNCS               certs;
253     CONTEXT_FUNCS               crls;
254     CONTEXT_FUNCS               ctls;
255     PFN_CERT_STORE_PROV_CONTROL control; /* optional */
256     PCONTEXT_PROPERTY_LIST      properties;
257 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
258
259 void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, DWORD dwFlags,
260  CertStoreType type);
261 void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store);
262 BOOL WINAPI I_CertUpdateStore(HCERTSTORE store1, HCERTSTORE store2, DWORD unk0,
263  DWORD unk1);
264
265 PWINECRYPT_CERTSTORE CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
266  DWORD dwFlags, const void *pvPara);
267 PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(DWORD dwFlags,
268  PWINECRYPT_CERTSTORE memStore, const CERT_STORE_PROV_INFO *pProvInfo);
269 PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
270  DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
271  const void *pvPara);
272 PWINECRYPT_CERTSTORE CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
273  const void *pvPara);
274 PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
275  const void *pvPara);
276 PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
277  DWORD dwFlags, const void *pvPara);
278 PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
279  DWORD dwFlags, const void *pvPara);
280 PWINECRYPT_CERTSTORE CRYPT_RootOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags);
281
282 /* Allocates and initializes a certificate chain engine, but without creating
283  * the root store.  Instead, it uses root, and assumes the caller has done any
284  * checking necessary.
285  */
286 HCERTCHAINENGINE CRYPT_CreateChainEngine(HCERTSTORE root,
287  PCERT_CHAIN_ENGINE_CONFIG pConfig);
288
289 /* Helper function for store reading functions and
290  * CertAddSerializedElementToStore.  Returns a context of the appropriate type
291  * if it can, or NULL otherwise.  Doesn't validate any of the properties in
292  * the serialized context (for example, bad hashes are retained.)
293  * *pdwContentType is set to the type of the returned context.
294  */
295 const void *CRYPT_ReadSerializedElement(const BYTE *pbElement,
296  DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType);
297
298 /* Reads contexts serialized in the file into the memory store.  Returns FALSE
299  * if the file is not of the expected format.
300  */
301 BOOL CRYPT_ReadSerializedStoreFromFile(HANDLE file, HCERTSTORE store);
302
303 /* Reads contexts serialized in the blob into the memory store.  Returns FALSE
304  * if the file is not of the expected format.
305  */
306 BOOL CRYPT_ReadSerializedStoreFromBlob(const CRYPT_DATA_BLOB *blob,
307  HCERTSTORE store);
308
309 /* Fixes up the pointers in info, where info is assumed to be a
310  * CRYPT_KEY_PROV_INFO, followed by its container name, provider name, and any
311  * provider parameters, in a contiguous buffer, but where info's pointers are
312  * assumed to be invalid.  Upon return, info's pointers point to the
313  * appropriate memory locations.
314  */
315 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info);
316
317 /**
318  *  String functions
319  */
320
321 DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indent,
322  const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz);
323
324 /**
325  *  Context functions
326  */
327
328 /* Allocates a new data context, a context which owns properties directly.
329  * contextSize is the size of the public data type associated with context,
330  * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
331  * Free with Context_Release.
332  */
333 void *Context_CreateDataContext(size_t contextSize);
334
335 /* Creates a new link context with extra bytes.  The context refers to linked
336  * rather than owning its own properties.  If addRef is TRUE (which ordinarily
337  * it should be) linked is addref'd.
338  * Free with Context_Release.
339  */
340 void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra,
341  BOOL addRef);
342
343 /* Returns a pointer to the extra bytes allocated with context, which must be
344  * a link context.
345  */
346 void *Context_GetExtra(const void *context, size_t contextSize);
347
348 /* Gets the context linked to by context, which must be a link context. */
349 void *Context_GetLinkedContext(void *context, size_t contextSize);
350
351 /* Copies properties from fromContext to toContext. */
352 void Context_CopyProperties(const void *to, const void *from,
353  size_t contextSize);
354
355 /* Returns context's properties, or the linked context's properties if context
356  * is a link context.
357  */
358 PCONTEXT_PROPERTY_LIST Context_GetProperties(const void *context, size_t contextSize);
359
360 void Context_AddRef(void *context, size_t contextSize);
361
362 typedef void (*ContextFreeFunc)(void *context);
363
364 /* Decrements context's ref count.  If context is a link context, releases its
365  * linked context as well.
366  * If a data context has its ref count reach 0, calls dataContextFree on it.
367  * Returns FALSE if the reference count is <= 0 when called.
368  */
369 BOOL Context_Release(void *context, size_t contextSize,
370  ContextFreeFunc dataContextFree);
371
372 /**
373  *  Context property list functions
374  */
375
376 PCONTEXT_PROPERTY_LIST ContextPropertyList_Create(void);
377
378 /* Searches for the property with ID id in the context.  Returns TRUE if found,
379  * and copies the property's length and a pointer to its data to blob.
380  * Otherwise returns FALSE.
381  */
382 BOOL ContextPropertyList_FindProperty(PCONTEXT_PROPERTY_LIST list, DWORD id,
383  PCRYPT_DATA_BLOB blob);
384
385 BOOL ContextPropertyList_SetProperty(PCONTEXT_PROPERTY_LIST list, DWORD id,
386  const BYTE *pbData, size_t cbData);
387
388 void ContextPropertyList_RemoveProperty(PCONTEXT_PROPERTY_LIST list, DWORD id);
389
390 DWORD ContextPropertyList_EnumPropIDs(PCONTEXT_PROPERTY_LIST list, DWORD id);
391
392 void ContextPropertyList_Copy(PCONTEXT_PROPERTY_LIST to,
393  PCONTEXT_PROPERTY_LIST from);
394
395 void ContextPropertyList_Free(PCONTEXT_PROPERTY_LIST list);
396
397 /**
398  *  Context list functions.  A context list is a simple list of link contexts.
399  */
400 struct ContextList;
401
402 struct ContextList *ContextList_Create(
403  PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize);
404
405 void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace);
406
407 void *ContextList_Enum(struct ContextList *list, void *pPrev);
408
409 /* Removes a context from the list.  Returns TRUE if the context was removed,
410  * or FALSE if not.  (The context may have been duplicated, so subsequent
411  * removes have no effect.)
412  */
413 BOOL ContextList_Remove(struct ContextList *list, void *context);
414
415 void ContextList_Free(struct ContextList *list);
416
417 /**
418  *  Utilities.
419  */
420
421 /* Align up to a DWORD_PTR boundary
422  */
423 #define ALIGN_DWORD_PTR(x) (((x) + sizeof(DWORD_PTR) - 1) & ~(sizeof(DWORD_PTR) - 1))
424 #define POINTER_ALIGN_DWORD_PTR(p) ((LPVOID)ALIGN_DWORD_PTR((DWORD_PTR)(p)))
425
426 /* Check if the OID is a small int
427  */
428 #define IS_INTOID(x)    (((ULONG_PTR)(x) >> 16) == 0)
429
430 #endif