crypt32: Add a function to serialize a store to an arbitrary stream.
[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(CRYPT_DIGESTED_DATA *digestedData,
83  void *pvData, DWORD *pcbData);
84
85 typedef struct _CRYPT_SIGNED_INFO
86 {
87     DWORD              version;
88     DWORD              cCertEncoded;
89     PCERT_BLOB         rgCertEncoded;
90     DWORD              cCrlEncoded;
91     PCRL_BLOB          rgCrlEncoded;
92     CRYPT_CONTENT_INFO content;
93     DWORD              cSignerInfo;
94     PCMSG_SIGNER_INFO  rgSignerInfo;
95 } CRYPT_SIGNED_INFO;
96
97 BOOL CRYPT_AsnEncodePKCSSignedInfo(CRYPT_SIGNED_INFO *, void *pvData,
98  DWORD *pcbData);
99
100 BOOL CRYPT_AsnDecodePKCSSignedInfo(const BYTE *pbEncoded, DWORD cbEncoded,
101  DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
102  CRYPT_SIGNED_INFO *signedInfo, DWORD *pcbSignedInfo);
103
104 /* Helper function to check *pcbEncoded, set it to the required size, and
105  * optionally to allocate memory.  Assumes pbEncoded is not NULL.
106  * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
107  * pointer to the newly allocated memory.
108  */
109 BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
110  BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded);
111
112 BOOL CRYPT_AsnDecodePKCSDigestedData(const BYTE *pbEncoded, DWORD cbEncoded,
113  DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
114  CRYPT_DIGESTED_DATA *digestedData, DWORD *pcbDigestedData);
115
116 /* The following aren't defined in wincrypt.h, as they're "reserved" */
117 #define CERT_CERT_PROP_ID 32
118 #define CERT_CRL_PROP_ID  33
119 #define CERT_CTL_PROP_ID  34
120
121 /* Returns a handle to the default crypto provider; loads it if necessary.
122  * Returns NULL on failure.
123  */
124 HCRYPTPROV CRYPT_GetDefaultProvider(void);
125
126 void crypt_oid_init(HINSTANCE hinst);
127 void crypt_oid_free(void);
128 void crypt_sip_free(void);
129 void default_chain_engine_free(void);
130
131 /* Some typedefs that make it easier to abstract which type of context we're
132  * working with.
133  */
134 typedef const void *(WINAPI *CreateContextFunc)(DWORD dwCertEncodingType,
135  const BYTE *pbCertEncoded, DWORD cbCertEncoded);
136 typedef BOOL (WINAPI *AddContextToStoreFunc)(HCERTSTORE hCertStore,
137  const void *context, DWORD dwAddDisposition, const void **ppStoreContext);
138 typedef BOOL (WINAPI *AddEncodedContextToStoreFunc)(HCERTSTORE hCertStore,
139  DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
140  DWORD dwAddDisposition, const void **ppContext);
141 typedef const void *(WINAPI *DuplicateContextFunc)(const void *context);
142 typedef const void *(WINAPI *EnumContextsInStoreFunc)(HCERTSTORE hCertStore,
143  const void *pPrevContext);
144 typedef DWORD (WINAPI *EnumPropertiesFunc)(const void *context, DWORD dwPropId);
145 typedef BOOL (WINAPI *GetContextPropertyFunc)(const void *context,
146  DWORD dwPropID, void *pvData, DWORD *pcbData);
147 typedef BOOL (WINAPI *SetContextPropertyFunc)(const void *context,
148  DWORD dwPropID, DWORD dwFlags, const void *pvData);
149 typedef BOOL (WINAPI *SerializeElementFunc)(const void *context, DWORD dwFlags,
150  BYTE *pbElement, DWORD *pcbElement);
151 typedef BOOL (WINAPI *FreeContextFunc)(const void *context);
152 typedef BOOL (WINAPI *DeleteContextFunc)(const void *context);
153
154 /* An abstract context (certificate, CRL, or CTL) interface */
155 typedef struct _WINE_CONTEXT_INTERFACE
156 {
157     CreateContextFunc            create;
158     AddContextToStoreFunc        addContextToStore;
159     AddEncodedContextToStoreFunc addEncodedToStore;
160     DuplicateContextFunc         duplicate;
161     EnumContextsInStoreFunc      enumContextsInStore;
162     EnumPropertiesFunc           enumProps;
163     GetContextPropertyFunc       getProp;
164     SetContextPropertyFunc       setProp;
165     SerializeElementFunc         serialize;
166     FreeContextFunc              free;
167     DeleteContextFunc            deleteFromStore;
168 } WINE_CONTEXT_INTERFACE, *PWINE_CONTEXT_INTERFACE;
169 typedef const WINE_CONTEXT_INTERFACE *PCWINE_CONTEXT_INTERFACE;
170
171 extern PCWINE_CONTEXT_INTERFACE pCertInterface;
172 extern PCWINE_CONTEXT_INTERFACE pCRLInterface;
173 extern PCWINE_CONTEXT_INTERFACE pCTLInterface;
174
175 /* (Internal) certificate store types and functions */
176 struct WINE_CRYPTCERTSTORE;
177
178 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
179  DWORD dwFlags, const void *pvPara);
180
181 /* Called to enumerate the next context in a store. */
182 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
183
184 /* Called to add a context to a store.  If toReplace is not NULL,
185  * context replaces toReplace in the store, and access checks should not be
186  * performed.  Otherwise context is a new context, and it should only be
187  * added if the store allows it.  If ppStoreContext is not NULL, the added
188  * context should be returned in *ppStoreContext.
189  */
190 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
191  void *toReplace, const void **ppStoreContext);
192
193 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
194
195 typedef struct _CONTEXT_FUNCS
196 {
197     AddFunc    addContext;
198     EnumFunc   enumContext;
199     DeleteFunc deleteContext;
200 } CONTEXT_FUNCS, *PCONTEXT_FUNCS;
201
202 typedef enum _CertStoreType {
203     StoreTypeMem,
204     StoreTypeCollection,
205     StoreTypeProvider,
206 } CertStoreType;
207
208 struct _CONTEXT_PROPERTY_LIST;
209 typedef struct _CONTEXT_PROPERTY_LIST *PCONTEXT_PROPERTY_LIST;
210
211 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
212
213 /* A cert store is polymorphic through the use of function pointers.  A type
214  * is still needed to distinguish collection stores from other types.
215  * On the function pointers:
216  * - closeStore is called when the store's ref count becomes 0
217  * - control is optional, but should be implemented by any store that supports
218  *   persistence
219  */
220 typedef struct WINE_CRYPTCERTSTORE
221 {
222     DWORD                       dwMagic;
223     LONG                        ref;
224     DWORD                       dwOpenFlags;
225     CertStoreType               type;
226     PFN_CERT_STORE_PROV_CLOSE   closeStore;
227     CONTEXT_FUNCS               certs;
228     CONTEXT_FUNCS               crls;
229     PFN_CERT_STORE_PROV_CONTROL control; /* optional */
230     PCONTEXT_PROPERTY_LIST      properties;
231 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
232
233 void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, DWORD dwFlags,
234  CertStoreType type);
235 void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store);
236 void CRYPT_EmptyStore(HCERTSTORE store);
237
238 PWINECRYPT_CERTSTORE CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
239  DWORD dwFlags, const void *pvPara);
240 PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(DWORD dwFlags,
241  PWINECRYPT_CERTSTORE memStore, const CERT_STORE_PROV_INFO *pProvInfo);
242 PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
243  DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
244  const void *pvPara);
245 PWINECRYPT_CERTSTORE CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
246  const void *pvPara);
247 PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
248  const void *pvPara);
249 PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
250  DWORD dwFlags, const void *pvPara);
251 PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
252  DWORD dwFlags, const void *pvPara);
253 PWINECRYPT_CERTSTORE CRYPT_RootOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags);
254
255 /* Allocates and initializes a certificate chain engine, but without creating
256  * the root store.  Instead, it uses root, and assumes the caller has done any
257  * checking necessary.
258  */
259 HCERTCHAINENGINE CRYPT_CreateChainEngine(HCERTSTORE root,
260  PCERT_CHAIN_ENGINE_CONFIG pConfig);
261
262 /* Helper function for store reading functions and
263  * CertAddSerializedElementToStore.  Returns a context of the appropriate type
264  * if it can, or NULL otherwise.  Doesn't validate any of the properties in
265  * the serialized context (for example, bad hashes are retained.)
266  * *pdwContentType is set to the type of the returned context.
267  */
268 const void *CRYPT_ReadSerializedElement(const BYTE *pbElement,
269  DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType);
270
271 typedef BOOL (*SerializedOutputFunc)(void *handle, const void *buffer,
272  DWORD size);
273
274 /* Writes contexts from the memory store to the output function, passing handle
275  * as the handle parameter to the output function.
276  */
277 BOOL CRYPT_WriteSerializedStoreToStream(HCERTSTORE store,
278  SerializedOutputFunc output, void *handle);
279
280 /* Writes contexts from the memory store to the file. */
281 BOOL CRYPT_WriteSerializedStoreToFile(HANDLE file, HCERTSTORE store);
282
283 /* Reads contexts serialized in the file into the memory store.  Returns FALSE
284  * if the file is not of the expected format.
285  */
286 BOOL CRYPT_ReadSerializedStoreFromFile(HANDLE file, HCERTSTORE store);
287
288 /* Fixes up the the pointers in info, where info is assumed to be a
289  * CRYPT_KEY_PROV_INFO, followed by its container name, provider name, and any
290  * provider parameters, in a contiguous buffer, but where info's pointers are
291  * assumed to be invalid.  Upon return, info's pointers point to the
292  * appropriate memory locations.
293  */
294 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info);
295
296 /**
297  *  Context functions
298  */
299
300 /* Allocates a new data context, a context which owns properties directly.
301  * contextSize is the size of the public data type associated with context,
302  * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
303  * Free with Context_Release.
304  */
305 void *Context_CreateDataContext(size_t contextSize);
306
307 /* Creates a new link context with extra bytes.  The context refers to linked
308  * rather than owning its own properties.  If addRef is TRUE (which ordinarily
309  * it should be) linked is addref'd.
310  * Free with Context_Release.
311  */
312 void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra,
313  BOOL addRef);
314
315 /* Returns a pointer to the extra bytes allocated with context, which must be
316  * a link context.
317  */
318 void *Context_GetExtra(const void *context, size_t contextSize);
319
320 /* Gets the context linked to by context, which must be a link context. */
321 void *Context_GetLinkedContext(void *context, size_t contextSize);
322
323 /* Copies properties from fromContext to toContext. */
324 void Context_CopyProperties(const void *to, const void *from,
325  size_t contextSize);
326
327 /* Returns context's properties, or the linked context's properties if context
328  * is a link context.
329  */
330 PCONTEXT_PROPERTY_LIST Context_GetProperties(void *context, size_t contextSize);
331
332 void Context_AddRef(void *context, size_t contextSize);
333
334 typedef void (*ContextFreeFunc)(void *context);
335
336 /* Decrements context's ref count.  If context is a link context, releases its
337  * linked context as well.
338  * If a data context has its ref count reach 0, calls dataContextFree on it.
339  */
340 void Context_Release(void *context, size_t contextSize,
341  ContextFreeFunc dataContextFree);
342
343 /**
344  *  Context property list functions
345  */
346
347 PCONTEXT_PROPERTY_LIST ContextPropertyList_Create(void);
348
349 /* Searches for the property with ID id in the context.  Returns TRUE if found,
350  * and copies the property's length and a pointer to its data to blob.
351  * Otherwise returns FALSE.
352  */
353 BOOL ContextPropertyList_FindProperty(PCONTEXT_PROPERTY_LIST list, DWORD id,
354  PCRYPT_DATA_BLOB blob);
355
356 BOOL ContextPropertyList_SetProperty(PCONTEXT_PROPERTY_LIST list, DWORD id,
357  const BYTE *pbData, size_t cbData);
358
359 void ContextPropertyList_RemoveProperty(PCONTEXT_PROPERTY_LIST list, DWORD id);
360
361 DWORD ContextPropertyList_EnumPropIDs(PCONTEXT_PROPERTY_LIST list, DWORD id);
362
363 void ContextPropertyList_Copy(PCONTEXT_PROPERTY_LIST to,
364  PCONTEXT_PROPERTY_LIST from);
365
366 void ContextPropertyList_Free(PCONTEXT_PROPERTY_LIST list);
367
368 /**
369  *  Context list functions.  A context list is a simple list of link contexts.
370  */
371 struct ContextList;
372
373 struct ContextList *ContextList_Create(
374  PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize);
375
376 void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace);
377
378 void *ContextList_Enum(struct ContextList *list, void *pPrev);
379
380 void ContextList_Delete(struct ContextList *list, void *context);
381
382 void ContextList_Empty(struct ContextList *list);
383
384 void ContextList_Free(struct ContextList *list);
385
386 #endif