crypt32: Abstract contexts.

- create an abstract interface for contexts, and use it for certificates
- create a simple context list, and use it for memory stores
This commit is contained in:
Juan Lang 2006-05-22 13:59:52 -07:00 committed by Alexandre Julliard
parent 42b576e869
commit e48a0a3c23
4 changed files with 643 additions and 445 deletions

View File

@ -9,6 +9,7 @@ IMPORTS = user32 advapi32 kernel32 ntdll
C_SRCS = \
cert.c \
context.c \
decode.c \
encode.c \
oid.c \

298
dlls/crypt32/context.c Normal file
View File

@ -0,0 +1,298 @@
/*
* Copyright 2006 Juan Lang
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <assert.h>
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "wincrypt.h"
#include "wine/debug.h"
#include "wine/list.h"
#include "crypt32_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
typedef enum _ContextType {
ContextTypeData,
ContextTypeLink,
} ContextType;
typedef struct _BASE_CONTEXT
{
LONG ref;
ContextType type;
} BASE_CONTEXT, *PBASE_CONTEXT;
typedef struct _DATA_CONTEXT
{
LONG ref;
ContextType type; /* always ContextTypeData */
PCONTEXT_PROPERTY_LIST properties;
} DATA_CONTEXT, *PDATA_CONTEXT;
typedef struct _LINK_CONTEXT
{
LONG ref;
ContextType type; /* always ContextTypeLink */
PBASE_CONTEXT linked;
} LINK_CONTEXT, *PLINK_CONTEXT;
#define CONTEXT_FROM_BASE_CONTEXT(p, s) ((LPBYTE)(p) - (s))
#define BASE_CONTEXT_FROM_CONTEXT(p, s) (PBASE_CONTEXT)((LPBYTE)(p) + (s))
void *Context_CreateDataContext(size_t contextSize)
{
void *ret = CryptMemAlloc(contextSize + sizeof(DATA_CONTEXT));
if (ret)
{
PDATA_CONTEXT context = (PDATA_CONTEXT)((LPBYTE)ret + contextSize);
context->ref = 1;
context->type = ContextTypeData;
context->properties = ContextPropertyList_Create();
if (!context->properties)
{
CryptMemFree(ret);
ret = NULL;
}
}
return ret;
}
void *Context_CreateLinkContext(size_t contextSize, void *linked, size_t extra,
BOOL addRef)
{
void *context = CryptMemAlloc(contextSize + sizeof(LINK_CONTEXT) + extra);
TRACE("(%d, %p, %d)\n", contextSize, linked, extra);
if (context)
{
PLINK_CONTEXT linkContext = (PLINK_CONTEXT)BASE_CONTEXT_FROM_CONTEXT(
context, contextSize);
PBASE_CONTEXT linkedBase = BASE_CONTEXT_FROM_CONTEXT(linked,
contextSize);
memcpy(context, linked, contextSize);
linkContext->ref = 1;
linkContext->type = ContextTypeLink;
linkContext->linked = linkedBase;
if (addRef)
InterlockedIncrement(&linkedBase->ref);
TRACE("%p's ref count is %ld\n", context, linkContext->ref);
}
return context;
}
void Context_AddRef(void *context, size_t contextSize)
{
PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
InterlockedIncrement(&baseContext->ref);
}
void *Context_GetExtra(const void *context, size_t contextSize)
{
PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
assert(baseContext->type == ContextTypeLink);
return (LPBYTE)baseContext + sizeof(LINK_CONTEXT);
}
void *Context_GetLinkedContext(void *context, size_t contextSize)
{
PBASE_CONTEXT baseContext = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
assert(baseContext->type == ContextTypeLink);
return CONTEXT_FROM_BASE_CONTEXT(((PLINK_CONTEXT)baseContext)->linked,
contextSize);
}
PCONTEXT_PROPERTY_LIST Context_GetProperties(void *context, size_t contextSize)
{
PBASE_CONTEXT ptr = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
while (ptr && ptr->type == ContextTypeLink)
ptr = ((PLINK_CONTEXT)ptr)->linked;
return (ptr && ptr->type == ContextTypeData) ?
((PDATA_CONTEXT)ptr)->properties : NULL;
}
void Context_Release(void *context, size_t contextSize,
ContextFreeFunc dataContextFree)
{
PBASE_CONTEXT base = BASE_CONTEXT_FROM_CONTEXT(context, contextSize);
if (InterlockedDecrement(&base->ref) == 0)
{
TRACE("freeing %p\n", context);
switch (base->type)
{
case ContextTypeData:
ContextPropertyList_Free(((PDATA_CONTEXT)base)->properties);
dataContextFree(context);
break;
case ContextTypeLink:
/* The linked context is of the same type as this, so release
* it as well, using the same offset and data free function.
*/
Context_Release(CONTEXT_FROM_BASE_CONTEXT(
((PLINK_CONTEXT)base)->linked, contextSize), contextSize,
dataContextFree);
break;
default:
assert(0);
}
CryptMemFree(context);
}
else
TRACE("%p's ref count is %ld\n", context, base->ref);
}
struct ContextList
{
PCWINE_CONTEXT_INTERFACE contextInterface;
size_t contextSize;
CRITICAL_SECTION cs;
struct list contexts;
};
struct ContextList *ContextList_Create(
PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize)
{
struct ContextList *list = CryptMemAlloc(sizeof(struct ContextList));
if (list)
{
list->contextInterface = contextInterface;
list->contextSize = contextSize;
InitializeCriticalSection(&list->cs);
list_init(&list->contexts);
}
return list;
}
static inline struct list *ContextList_ContextToEntry(struct ContextList *list,
const void *context)
{
struct list *ret;
if (context)
ret = (struct list *)Context_GetExtra(context, list->contextSize);
else
ret = NULL;
return ret;
}
static inline void *ContextList_EntryToContext(struct ContextList *list,
struct list *entry)
{
return (LPBYTE)entry - sizeof(LINK_CONTEXT) - list->contextSize;
}
void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace)
{
void *context;
TRACE("(%p, %p, %p)\n", list, toLink, toReplace);
context = Context_CreateLinkContext(list->contextSize, toLink,
sizeof(struct list), TRUE);
if (context)
{
struct list *entry = ContextList_ContextToEntry(list, context);
TRACE("adding %p\n", context);
EnterCriticalSection(&list->cs);
if (toReplace)
{
struct list *existing = ContextList_ContextToEntry(list, toReplace);
entry->prev = existing->prev;
entry->next = existing->next;
entry->prev->next = entry;
entry->next->prev = entry;
existing->prev = existing->next = existing;
list->contextInterface->free(toReplace);
}
else
list_add_tail(&list->contexts, entry);
LeaveCriticalSection(&list->cs);
}
return context;
}
void *ContextList_Enum(struct ContextList *list, void *pPrev)
{
struct list *listNext;
void *ret;
EnterCriticalSection(&list->cs);
if (pPrev)
{
struct list *prevEntry = ContextList_ContextToEntry(list, pPrev);
listNext = list_next(&list->contexts, prevEntry);
list->contextInterface->free(pPrev);
}
else
listNext = list_next(&list->contexts, &list->contexts);
LeaveCriticalSection(&list->cs);
if (listNext)
{
ret = ContextList_EntryToContext(list, listNext);
list->contextInterface->duplicate(ret);
}
else
ret = NULL;
return ret;
}
void ContextList_Delete(struct ContextList *list, void *context)
{
struct list *entry = ContextList_ContextToEntry(list, context);
EnterCriticalSection(&list->cs);
list_remove(entry);
LeaveCriticalSection(&list->cs);
list->contextInterface->free(context);
}
void ContextList_Empty(struct ContextList *list)
{
struct list *entry, *next;
EnterCriticalSection(&list->cs);
LIST_FOR_EACH_SAFE(entry, next, &list->contexts)
{
const void *context = ContextList_EntryToContext(list, entry);
TRACE("removing %p\n", context);
list_remove(entry);
list->contextInterface->free(context);
}
LeaveCriticalSection(&list->cs);
}
void ContextList_Free(struct ContextList *list)
{
ContextList_Empty(list);
DeleteCriticalSection(&list->cs);
CryptMemFree(list);
}

View File

@ -95,11 +95,55 @@ const void *CRYPT_ReadSerializedElement(const BYTE *pbElement,
DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType);
/**
* Context property list functions
* Context functions
*/
/* Allocates a new data context, a context which owns properties directly.
* contextSize is the size of the public data type associated with context,
* which should be one of CERT_CONTEXT, CRL_CONTEXT, or CTL_CONTEXT.
* Free with Context_Release.
*/
void *Context_CreateDataContext(size_t contextSize);
/* Creates a new link context with extra bytes. The context refers to linked
* rather than owning its own properties. If addRef is TRUE (which ordinarily
* it should be) linked is addref'd.
* Free with Context_Release.
*/
void *Context_CreateLinkContext(size_t contextSize, void *linked, size_t extra,
BOOL addRef);
/* Returns a pointer to the extra bytes allocated with context, which must be
* a link context.
*/
void *Context_GetExtra(const void *context, size_t contextSize);
/* Gets the context linked to by context, which must be a link context. */
void *Context_GetLinkedContext(void *context, size_t contextSize);
struct _CONTEXT_PROPERTY_LIST;
typedef struct _CONTEXT_PROPERTY_LIST *PCONTEXT_PROPERTY_LIST;
/* Returns context's properties, or the linked context's properties if context
* is a link context.
*/
PCONTEXT_PROPERTY_LIST Context_GetProperties(void *context, size_t contextSize);
void Context_AddRef(void *context, size_t contextSize);
typedef void (*ContextFreeFunc)(void *context);
/* Decrements context's ref count. If context is a link context, releases its
* linked context as well.
* If a data context has its ref count reach 0, calls dataContextFree on it.
*/
void Context_Release(void *context, size_t contextSize,
ContextFreeFunc dataContextFree);
/**
* Context property list functions
*/
PCONTEXT_PROPERTY_LIST ContextPropertyList_Create(void);
/* Searches for the property with ID id in the context. Returns TRUE if found,
@ -121,4 +165,22 @@ void ContextPropertyList_Copy(PCONTEXT_PROPERTY_LIST to,
void ContextPropertyList_Free(PCONTEXT_PROPERTY_LIST list);
/**
* Context list functions. A context list is a simple list of link contexts.
*/
struct ContextList;
struct ContextList *ContextList_Create(
PCWINE_CONTEXT_INTERFACE contextInterface, size_t contextSize);
void *ContextList_Add(struct ContextList *list, void *toLink, void *toReplace);
void *ContextList_Enum(struct ContextList *list, void *pPrev);
void ContextList_Delete(struct ContextList *list, void *context);
void ContextList_Empty(struct ContextList *list);
void ContextList_Free(struct ContextList *list);
#endif

File diff suppressed because it is too large Load Diff