blob: 9b1fc279ebb65a6952b652e094ecd2c863a031a0 [file] [log] [blame]
Juan Lang02cb0c72005-08-25 09:51:59 +00001/*
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020016 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Juan Lang02cb0c72005-08-25 09:51:59 +000017 */
18
19#ifndef __CRYPT32_PRIVATE_H__
20#define __CRYPT32_PRIVATE_H__
21
Juan Langf224b342006-05-15 17:01:28 -070022/* 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)
Juan Langead21182006-07-19 07:44:48 -070026#define ASN_UTF8STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0c)
Juan Langf224b342006-05-15 17:01:28 -070027#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)
Juan Langdd4bd4a2006-06-02 13:20:13 -070030#define ASN_T61STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x14)
Juan Langead21182006-07-19 07:44:48 -070031#define ASN_VIDEOTEXSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x15)
Juan Langf224b342006-05-15 17:01:28 -070032#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)
Juan Langead21182006-07-19 07:44:48 -070035#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)
Juan Langf224b342006-05-15 17:01:28 -070040
Juan Lang48afa162007-07-12 14:09:00 -070041BOOL CRYPT_EncodeLen(DWORD len, BYTE *pbEncoded, DWORD *pcbEncoded);
42
43typedef BOOL (WINAPI *CryptEncodeObjectExFunc)(DWORD, LPCSTR, const void *,
44 DWORD, PCRYPT_ENCODE_PARA, BYTE *, DWORD *);
45
46struct AsnEncodeSequenceItem
47{
48 const void *pvStructInfo;
49 CryptEncodeObjectExFunc encodeFunc;
50 DWORD size; /* used during encoding, not for your use */
51};
52
53BOOL WINAPI CRYPT_AsnEncodeSequence(DWORD dwCertEncodingType,
54 struct AsnEncodeSequenceItem items[], DWORD cItem, DWORD dwFlags,
55 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
56
57struct AsnConstructedItem
58{
59 BYTE tag;
60 const void *pvStructInfo;
61 CryptEncodeObjectExFunc encodeFunc;
62};
63
64BOOL WINAPI CRYPT_AsnEncodeConstructed(DWORD dwCertEncodingType,
65 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
66 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
Juan Lang48afa162007-07-12 14:09:00 -070067BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType,
68 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
69 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
Juan Lang8599fd72007-07-12 14:59:27 -070070BOOL WINAPI CRYPT_AsnEncodeOctets(DWORD dwCertEncodingType,
71 LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags,
72 PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded);
Juan Lang48afa162007-07-12 14:09:00 -070073
Juan Lang703a0f62007-07-17 07:22:50 -070074typedef 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
82BOOL CRYPT_AsnEncodePKCSDigestedData(CRYPT_DIGESTED_DATA *digestedData,
83 void *pvData, DWORD *pcbData);
84
Juan Langb80101e2007-07-23 18:11:17 -070085typedef struct _CRYPT_SIGNED_INFO
86{
87 DWORD version;
88 DWORD cCertEncoded;
89 PCERT_BLOB rgCertEncoded;
90 DWORD cCrlEncoded;
91 PCRL_BLOB rgCrlEncoded;
92 DWORD cAttrCertEncoded;
93 PCERT_BLOB rgAttrCertEncoded;
94 CRYPT_CONTENT_INFO content;
95 DWORD cSignerInfo;
96 PCMSG_SIGNER_INFO rgSignerInfo;
97} CRYPT_SIGNED_INFO;
98
99BOOL CRYPT_AsnEncodePKCSSignedInfo(CRYPT_SIGNED_INFO *, void *pvData,
100 DWORD *pcbData);
101
Juan Lang48afa162007-07-12 14:09:00 -0700102/* Helper function to check *pcbEncoded, set it to the required size, and
103 * optionally to allocate memory. Assumes pbEncoded is not NULL.
104 * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a
105 * pointer to the newly allocated memory.
106 */
107BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara,
108 BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded);
109
Juan Lang35abf3a2007-07-17 07:25:31 -0700110BOOL CRYPT_AsnDecodePKCSDigestedData(const BYTE *pbEncoded, DWORD cbEncoded,
111 DWORD dwFlags, PCRYPT_DECODE_PARA pDecodePara,
112 CRYPT_DIGESTED_DATA *digestedData, DWORD *pcbDigestedData);
113
Juan Langc3a12512006-03-03 10:48:52 -0800114/* The following aren't defined in wincrypt.h, as they're "reserved" */
115#define CERT_CERT_PROP_ID 32
116#define CERT_CRL_PROP_ID 33
117#define CERT_CTL_PROP_ID 34
118
Juan Lang02cb0c72005-08-25 09:51:59 +0000119/* Returns a handle to the default crypto provider; loads it if necessary.
120 * Returns NULL on failure.
121 */
122HCRYPTPROV CRYPT_GetDefaultProvider(void);
123
Juan Lang690f9842006-02-23 13:18:13 -0800124void crypt_oid_init(HINSTANCE hinst);
125void crypt_oid_free(void);
Juan Lang19c3a092007-07-09 17:07:54 -0700126void crypt_sip_free(void);
Juan Langf7027942005-12-02 11:28:09 +0100127
Juan Langd9a02792006-05-18 07:31:53 -0700128/* Some typedefs that make it easier to abstract which type of context we're
129 * working with.
130 */
131typedef const void *(WINAPI *CreateContextFunc)(DWORD dwCertEncodingType,
132 const BYTE *pbCertEncoded, DWORD cbCertEncoded);
133typedef BOOL (WINAPI *AddContextToStoreFunc)(HCERTSTORE hCertStore,
134 const void *context, DWORD dwAddDisposition, const void **ppStoreContext);
135typedef BOOL (WINAPI *AddEncodedContextToStoreFunc)(HCERTSTORE hCertStore,
136 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
137 DWORD dwAddDisposition, const void **ppContext);
138typedef const void *(WINAPI *DuplicateContextFunc)(const void *context);
139typedef const void *(WINAPI *EnumContextsInStoreFunc)(HCERTSTORE hCertStore,
140 const void *pPrevContext);
Juan Langf94d49d2006-05-25 19:43:30 -0700141typedef DWORD (WINAPI *EnumPropertiesFunc)(const void *context, DWORD dwPropId);
Juan Langd9a02792006-05-18 07:31:53 -0700142typedef BOOL (WINAPI *GetContextPropertyFunc)(const void *context,
143 DWORD dwPropID, void *pvData, DWORD *pcbData);
144typedef BOOL (WINAPI *SetContextPropertyFunc)(const void *context,
145 DWORD dwPropID, DWORD dwFlags, const void *pvData);
146typedef BOOL (WINAPI *SerializeElementFunc)(const void *context, DWORD dwFlags,
147 BYTE *pbElement, DWORD *pcbElement);
148typedef BOOL (WINAPI *FreeContextFunc)(const void *context);
149typedef BOOL (WINAPI *DeleteContextFunc)(const void *context);
150
151/* An abstract context (certificate, CRL, or CTL) interface */
152typedef struct _WINE_CONTEXT_INTERFACE
153{
154 CreateContextFunc create;
155 AddContextToStoreFunc addContextToStore;
156 AddEncodedContextToStoreFunc addEncodedToStore;
157 DuplicateContextFunc duplicate;
158 EnumContextsInStoreFunc enumContextsInStore;
Juan Langf94d49d2006-05-25 19:43:30 -0700159 EnumPropertiesFunc enumProps;
Juan Langd9a02792006-05-18 07:31:53 -0700160 GetContextPropertyFunc getProp;
161 SetContextPropertyFunc setProp;
162 SerializeElementFunc serialize;
163 FreeContextFunc free;
164 DeleteContextFunc deleteFromStore;
165} WINE_CONTEXT_INTERFACE, *PWINE_CONTEXT_INTERFACE;
166typedef const WINE_CONTEXT_INTERFACE *PCWINE_CONTEXT_INTERFACE;
167
168extern PCWINE_CONTEXT_INTERFACE pCertInterface;
169extern PCWINE_CONTEXT_INTERFACE pCRLInterface;
170extern PCWINE_CONTEXT_INTERFACE pCTLInterface;
171
Juan Langc3a12512006-03-03 10:48:52 -0800172/* Helper function for store reading functions and
173 * CertAddSerializedElementToStore. Returns a context of the appropriate type
174 * if it can, or NULL otherwise. Doesn't validate any of the properties in
175 * the serialized context (for example, bad hashes are retained.)
176 * *pdwContentType is set to the type of the returned context.
177 */
178const void *CRYPT_ReadSerializedElement(const BYTE *pbElement,
179 DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType);
180
Juan Lang6e23b4a2006-07-25 16:29:50 -0700181/* Writes contexts from the memory store to the file. */
182BOOL CRYPT_WriteSerializedFile(HANDLE file, HCERTSTORE store);
183
184/* Reads contexts serialized in the file into the memory store. Returns FALSE
185 * if the file is not of the expected format.
186 */
187BOOL CRYPT_ReadSerializedFile(HANDLE file, HCERTSTORE store);
188
Juan Lang30bf5d92006-07-17 23:02:57 -0700189/* Fixes up the the pointers in info, where info is assumed to be a
190 * CRYPT_KEY_PROV_INFO, followed by its container name, provider name, and any
191 * provider parameters, in a contiguous buffer, but where info's pointers are
192 * assumed to be invalid. Upon return, info's pointers point to the
193 * appropriate memory locations.
194 */
195void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info);
196
Juan Lang91b80fc2006-03-09 19:06:42 -0800197/**
Juan Lange48a0a32006-05-22 13:59:52 -0700198 * Context functions
Juan Lang91b80fc2006-03-09 19:06:42 -0800199 */
Juan Lange48a0a32006-05-22 13:59:52 -0700200
201/* Allocates a new data context, a context which owns properties directly.
202 * contextSize is the size of the public data type associated with context,
203 * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
204 * Free with Context_Release.
205 */
206void *Context_CreateDataContext(size_t contextSize);
207
208/* Creates a new link context with extra bytes. The context refers to linked
209 * rather than owning its own properties. If addRef is TRUE (which ordinarily
210 * it should be) linked is addref'd.
211 * Free with Context_Release.
212 */
Alexandre Julliarddbd473f2006-06-13 14:14:00 +0200213void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra,
Juan Lange48a0a32006-05-22 13:59:52 -0700214 BOOL addRef);
215
216/* Returns a pointer to the extra bytes allocated with context, which must be
217 * a link context.
218 */
219void *Context_GetExtra(const void *context, size_t contextSize);
220
221/* Gets the context linked to by context, which must be a link context. */
222void *Context_GetLinkedContext(void *context, size_t contextSize);
223
Juan Langdc660a82006-05-26 09:41:37 -0700224/* Copies properties from fromContext to toContext. */
225void Context_CopyProperties(const void *to, const void *from,
226 size_t contextSize);
227
Juan Lang91b80fc2006-03-09 19:06:42 -0800228struct _CONTEXT_PROPERTY_LIST;
229typedef struct _CONTEXT_PROPERTY_LIST *PCONTEXT_PROPERTY_LIST;
230
Juan Lange48a0a32006-05-22 13:59:52 -0700231/* Returns context's properties, or the linked context's properties if context
232 * is a link context.
233 */
234PCONTEXT_PROPERTY_LIST Context_GetProperties(void *context, size_t contextSize);
235
236void Context_AddRef(void *context, size_t contextSize);
237
238typedef void (*ContextFreeFunc)(void *context);
239
240/* Decrements context's ref count. If context is a link context, releases its
241 * linked context as well.
242 * If a data context has its ref count reach 0, calls dataContextFree on it.
243 */
244void Context_Release(void *context, size_t contextSize,
245 ContextFreeFunc dataContextFree);
246
247/**
248 * Context property list functions
249 */
250
Juan Lang91b80fc2006-03-09 19:06:42 -0800251PCONTEXT_PROPERTY_LIST ContextPropertyList_Create(void);
252
253/* Searches for the property with ID id in the context. Returns TRUE if found,
254 * and copies the property's length and a pointer to its data to blob.
255 * Otherwise returns FALSE.
256 */
257BOOL ContextPropertyList_FindProperty(PCONTEXT_PROPERTY_LIST list, DWORD id,
258 PCRYPT_DATA_BLOB blob);
259
260BOOL ContextPropertyList_SetProperty(PCONTEXT_PROPERTY_LIST list, DWORD id,
261 const BYTE *pbData, size_t cbData);
262
263void ContextPropertyList_RemoveProperty(PCONTEXT_PROPERTY_LIST list, DWORD id);
264
265DWORD ContextPropertyList_EnumPropIDs(PCONTEXT_PROPERTY_LIST list, DWORD id);
266
267void ContextPropertyList_Copy(PCONTEXT_PROPERTY_LIST to,
268 PCONTEXT_PROPERTY_LIST from);
269
270void ContextPropertyList_Free(PCONTEXT_PROPERTY_LIST list);
271
Juan Lange48a0a32006-05-22 13:59:52 -0700272/**
273 * Context list functions. A context list is a simple list of link contexts.
274 */
275struct ContextList;
276
277struct ContextList *ContextList_Create(
278 PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize);
279
280void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace);
281
282void *ContextList_Enum(struct ContextList *list, void *pPrev);
283
284void ContextList_Delete(struct ContextList *list, void *context);
285
286void ContextList_Empty(struct ContextList *list);
287
288void ContextList_Free(struct ContextList *list);
289
Juan Lang02cb0c72005-08-25 09:51:59 +0000290#endif