Juan Lang | 02cb0c7 | 2005-08-25 09:51:59 +0000 | [diff] [blame] | 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Juan Lang | 02cb0c7 | 2005-08-25 09:51:59 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #ifndef __CRYPT32_PRIVATE_H__ |
| 20 | #define __CRYPT32_PRIVATE_H__ |
| 21 | |
Juan Lang | f224b34 | 2006-05-15 17:01:28 -0700 | [diff] [blame] | 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) |
Juan Lang | ead2118 | 2006-07-19 07:44:48 -0700 | [diff] [blame] | 26 | #define ASN_UTF8STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x0c) |
Juan Lang | f224b34 | 2006-05-15 17:01:28 -0700 | [diff] [blame] | 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) |
Juan Lang | dd4bd4a | 2006-06-02 13:20:13 -0700 | [diff] [blame] | 30 | #define ASN_T61STRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x14) |
Juan Lang | ead2118 | 2006-07-19 07:44:48 -0700 | [diff] [blame] | 31 | #define ASN_VIDEOTEXSTRING (ASN_UNIVERSAL | ASN_PRIMITIVE | 0x15) |
Juan Lang | f224b34 | 2006-05-15 17:01:28 -0700 | [diff] [blame] | 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) |
Juan Lang | ead2118 | 2006-07-19 07:44:48 -0700 | [diff] [blame] | 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) |
Juan Lang | f224b34 | 2006-05-15 17:01:28 -0700 | [diff] [blame] | 40 | |
Juan Lang | 48afa16 | 2007-07-12 14:09:00 -0700 | [diff] [blame] | 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 | |
| 68 | BOOL WINAPI CRYPT_AsnEncodeOid(DWORD dwCertEncodingType, |
| 69 | LPCSTR lpszStructType, const void *pvStructInfo, DWORD dwFlags, |
| 70 | PCRYPT_ENCODE_PARA pEncodePara, BYTE *pbEncoded, DWORD *pcbEncoded); |
| 71 | |
| 72 | /* Helper function to check *pcbEncoded, set it to the required size, and |
| 73 | * optionally to allocate memory. Assumes pbEncoded is not NULL. |
| 74 | * If CRYPT_ENCODE_ALLOC_FLAG is set in dwFlags, *pbEncoded will be set to a |
| 75 | * pointer to the newly allocated memory. |
| 76 | */ |
| 77 | BOOL CRYPT_EncodeEnsureSpace(DWORD dwFlags, PCRYPT_ENCODE_PARA pEncodePara, |
| 78 | BYTE *pbEncoded, DWORD *pcbEncoded, DWORD bytesNeeded); |
| 79 | |
Juan Lang | c3a1251 | 2006-03-03 10:48:52 -0800 | [diff] [blame] | 80 | /* The following aren't defined in wincrypt.h, as they're "reserved" */ |
| 81 | #define CERT_CERT_PROP_ID 32 |
| 82 | #define CERT_CRL_PROP_ID 33 |
| 83 | #define CERT_CTL_PROP_ID 34 |
| 84 | |
Juan Lang | 02cb0c7 | 2005-08-25 09:51:59 +0000 | [diff] [blame] | 85 | /* Returns a handle to the default crypto provider; loads it if necessary. |
| 86 | * Returns NULL on failure. |
| 87 | */ |
| 88 | HCRYPTPROV CRYPT_GetDefaultProvider(void); |
| 89 | |
Juan Lang | 690f984 | 2006-02-23 13:18:13 -0800 | [diff] [blame] | 90 | void crypt_oid_init(HINSTANCE hinst); |
| 91 | void crypt_oid_free(void); |
Juan Lang | 19c3a09 | 2007-07-09 17:07:54 -0700 | [diff] [blame] | 92 | void crypt_sip_free(void); |
Juan Lang | f702794 | 2005-12-02 11:28:09 +0100 | [diff] [blame] | 93 | |
Juan Lang | d9a0279 | 2006-05-18 07:31:53 -0700 | [diff] [blame] | 94 | /* Some typedefs that make it easier to abstract which type of context we're |
| 95 | * working with. |
| 96 | */ |
| 97 | typedef const void *(WINAPI *CreateContextFunc)(DWORD dwCertEncodingType, |
| 98 | const BYTE *pbCertEncoded, DWORD cbCertEncoded); |
| 99 | typedef BOOL (WINAPI *AddContextToStoreFunc)(HCERTSTORE hCertStore, |
| 100 | const void *context, DWORD dwAddDisposition, const void **ppStoreContext); |
| 101 | typedef BOOL (WINAPI *AddEncodedContextToStoreFunc)(HCERTSTORE hCertStore, |
| 102 | DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded, |
| 103 | DWORD dwAddDisposition, const void **ppContext); |
| 104 | typedef const void *(WINAPI *DuplicateContextFunc)(const void *context); |
| 105 | typedef const void *(WINAPI *EnumContextsInStoreFunc)(HCERTSTORE hCertStore, |
| 106 | const void *pPrevContext); |
Juan Lang | f94d49d | 2006-05-25 19:43:30 -0700 | [diff] [blame] | 107 | typedef DWORD (WINAPI *EnumPropertiesFunc)(const void *context, DWORD dwPropId); |
Juan Lang | d9a0279 | 2006-05-18 07:31:53 -0700 | [diff] [blame] | 108 | typedef BOOL (WINAPI *GetContextPropertyFunc)(const void *context, |
| 109 | DWORD dwPropID, void *pvData, DWORD *pcbData); |
| 110 | typedef BOOL (WINAPI *SetContextPropertyFunc)(const void *context, |
| 111 | DWORD dwPropID, DWORD dwFlags, const void *pvData); |
| 112 | typedef BOOL (WINAPI *SerializeElementFunc)(const void *context, DWORD dwFlags, |
| 113 | BYTE *pbElement, DWORD *pcbElement); |
| 114 | typedef BOOL (WINAPI *FreeContextFunc)(const void *context); |
| 115 | typedef BOOL (WINAPI *DeleteContextFunc)(const void *context); |
| 116 | |
| 117 | /* An abstract context (certificate, CRL, or CTL) interface */ |
| 118 | typedef struct _WINE_CONTEXT_INTERFACE |
| 119 | { |
| 120 | CreateContextFunc create; |
| 121 | AddContextToStoreFunc addContextToStore; |
| 122 | AddEncodedContextToStoreFunc addEncodedToStore; |
| 123 | DuplicateContextFunc duplicate; |
| 124 | EnumContextsInStoreFunc enumContextsInStore; |
Juan Lang | f94d49d | 2006-05-25 19:43:30 -0700 | [diff] [blame] | 125 | EnumPropertiesFunc enumProps; |
Juan Lang | d9a0279 | 2006-05-18 07:31:53 -0700 | [diff] [blame] | 126 | GetContextPropertyFunc getProp; |
| 127 | SetContextPropertyFunc setProp; |
| 128 | SerializeElementFunc serialize; |
| 129 | FreeContextFunc free; |
| 130 | DeleteContextFunc deleteFromStore; |
| 131 | } WINE_CONTEXT_INTERFACE, *PWINE_CONTEXT_INTERFACE; |
| 132 | typedef const WINE_CONTEXT_INTERFACE *PCWINE_CONTEXT_INTERFACE; |
| 133 | |
| 134 | extern PCWINE_CONTEXT_INTERFACE pCertInterface; |
| 135 | extern PCWINE_CONTEXT_INTERFACE pCRLInterface; |
| 136 | extern PCWINE_CONTEXT_INTERFACE pCTLInterface; |
| 137 | |
Juan Lang | c3a1251 | 2006-03-03 10:48:52 -0800 | [diff] [blame] | 138 | /* Helper function for store reading functions and |
| 139 | * CertAddSerializedElementToStore. Returns a context of the appropriate type |
| 140 | * if it can, or NULL otherwise. Doesn't validate any of the properties in |
| 141 | * the serialized context (for example, bad hashes are retained.) |
| 142 | * *pdwContentType is set to the type of the returned context. |
| 143 | */ |
| 144 | const void *CRYPT_ReadSerializedElement(const BYTE *pbElement, |
| 145 | DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType); |
| 146 | |
Juan Lang | 6e23b4a | 2006-07-25 16:29:50 -0700 | [diff] [blame] | 147 | /* Writes contexts from the memory store to the file. */ |
| 148 | BOOL CRYPT_WriteSerializedFile(HANDLE file, HCERTSTORE store); |
| 149 | |
| 150 | /* Reads contexts serialized in the file into the memory store. Returns FALSE |
| 151 | * if the file is not of the expected format. |
| 152 | */ |
| 153 | BOOL CRYPT_ReadSerializedFile(HANDLE file, HCERTSTORE store); |
| 154 | |
Juan Lang | 30bf5d9 | 2006-07-17 23:02:57 -0700 | [diff] [blame] | 155 | /* Fixes up the the pointers in info, where info is assumed to be a |
| 156 | * CRYPT_KEY_PROV_INFO, followed by its container name, provider name, and any |
| 157 | * provider parameters, in a contiguous buffer, but where info's pointers are |
| 158 | * assumed to be invalid. Upon return, info's pointers point to the |
| 159 | * appropriate memory locations. |
| 160 | */ |
| 161 | void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info); |
| 162 | |
Juan Lang | 91b80fc | 2006-03-09 19:06:42 -0800 | [diff] [blame] | 163 | /** |
Juan Lang | e48a0a3 | 2006-05-22 13:59:52 -0700 | [diff] [blame] | 164 | * Context functions |
Juan Lang | 91b80fc | 2006-03-09 19:06:42 -0800 | [diff] [blame] | 165 | */ |
Juan Lang | e48a0a3 | 2006-05-22 13:59:52 -0700 | [diff] [blame] | 166 | |
| 167 | /* Allocates a new data context, a context which owns properties directly. |
| 168 | * contextSize is the size of the public data type associated with context, |
| 169 | * which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT. |
| 170 | * Free with Context_Release. |
| 171 | */ |
| 172 | void *Context_CreateDataContext(size_t contextSize); |
| 173 | |
| 174 | /* Creates a new link context with extra bytes. The context refers to linked |
| 175 | * rather than owning its own properties. If addRef is TRUE (which ordinarily |
| 176 | * it should be) linked is addref'd. |
| 177 | * Free with Context_Release. |
| 178 | */ |
Alexandre Julliard | dbd473f | 2006-06-13 14:14:00 +0200 | [diff] [blame] | 179 | void *Context_CreateLinkContext(unsigned int contextSize, void *linked, unsigned int extra, |
Juan Lang | e48a0a3 | 2006-05-22 13:59:52 -0700 | [diff] [blame] | 180 | BOOL addRef); |
| 181 | |
| 182 | /* Returns a pointer to the extra bytes allocated with context, which must be |
| 183 | * a link context. |
| 184 | */ |
| 185 | void *Context_GetExtra(const void *context, size_t contextSize); |
| 186 | |
| 187 | /* Gets the context linked to by context, which must be a link context. */ |
| 188 | void *Context_GetLinkedContext(void *context, size_t contextSize); |
| 189 | |
Juan Lang | dc660a8 | 2006-05-26 09:41:37 -0700 | [diff] [blame] | 190 | /* Copies properties from fromContext to toContext. */ |
| 191 | void Context_CopyProperties(const void *to, const void *from, |
| 192 | size_t contextSize); |
| 193 | |
Juan Lang | 91b80fc | 2006-03-09 19:06:42 -0800 | [diff] [blame] | 194 | struct _CONTEXT_PROPERTY_LIST; |
| 195 | typedef struct _CONTEXT_PROPERTY_LIST *PCONTEXT_PROPERTY_LIST; |
| 196 | |
Juan Lang | e48a0a3 | 2006-05-22 13:59:52 -0700 | [diff] [blame] | 197 | /* Returns context's properties, or the linked context's properties if context |
| 198 | * is a link context. |
| 199 | */ |
| 200 | PCONTEXT_PROPERTY_LIST Context_GetProperties(void *context, size_t contextSize); |
| 201 | |
| 202 | void Context_AddRef(void *context, size_t contextSize); |
| 203 | |
| 204 | typedef void (*ContextFreeFunc)(void *context); |
| 205 | |
| 206 | /* Decrements context's ref count. If context is a link context, releases its |
| 207 | * linked context as well. |
| 208 | * If a data context has its ref count reach 0, calls dataContextFree on it. |
| 209 | */ |
| 210 | void Context_Release(void *context, size_t contextSize, |
| 211 | ContextFreeFunc dataContextFree); |
| 212 | |
| 213 | /** |
| 214 | * Context property list functions |
| 215 | */ |
| 216 | |
Juan Lang | 91b80fc | 2006-03-09 19:06:42 -0800 | [diff] [blame] | 217 | PCONTEXT_PROPERTY_LIST ContextPropertyList_Create(void); |
| 218 | |
| 219 | /* Searches for the property with ID id in the context. Returns TRUE if found, |
| 220 | * and copies the property's length and a pointer to its data to blob. |
| 221 | * Otherwise returns FALSE. |
| 222 | */ |
| 223 | BOOL ContextPropertyList_FindProperty(PCONTEXT_PROPERTY_LIST list, DWORD id, |
| 224 | PCRYPT_DATA_BLOB blob); |
| 225 | |
| 226 | BOOL ContextPropertyList_SetProperty(PCONTEXT_PROPERTY_LIST list, DWORD id, |
| 227 | const BYTE *pbData, size_t cbData); |
| 228 | |
| 229 | void ContextPropertyList_RemoveProperty(PCONTEXT_PROPERTY_LIST list, DWORD id); |
| 230 | |
| 231 | DWORD ContextPropertyList_EnumPropIDs(PCONTEXT_PROPERTY_LIST list, DWORD id); |
| 232 | |
| 233 | void ContextPropertyList_Copy(PCONTEXT_PROPERTY_LIST to, |
| 234 | PCONTEXT_PROPERTY_LIST from); |
| 235 | |
| 236 | void ContextPropertyList_Free(PCONTEXT_PROPERTY_LIST list); |
| 237 | |
Juan Lang | e48a0a3 | 2006-05-22 13:59:52 -0700 | [diff] [blame] | 238 | /** |
| 239 | * Context list functions. A context list is a simple list of link contexts. |
| 240 | */ |
| 241 | struct ContextList; |
| 242 | |
| 243 | struct ContextList *ContextList_Create( |
| 244 | PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize); |
| 245 | |
| 246 | void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace); |
| 247 | |
| 248 | void *ContextList_Enum(struct ContextList *list, void *pPrev); |
| 249 | |
| 250 | void ContextList_Delete(struct ContextList *list, void *context); |
| 251 | |
| 252 | void ContextList_Empty(struct ContextList *list); |
| 253 | |
| 254 | void ContextList_Free(struct ContextList *list); |
| 255 | |
Juan Lang | 02cb0c7 | 2005-08-25 09:51:59 +0000 | [diff] [blame] | 256 | #endif |