📄 oid.c
字号:
/*
* Copyright 2002 Mike McCormack for CodeWeavers
* Copyright 2005-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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdio.h>
#include <stdarg.h>
#define NONAMELESSUNION
#include "windef.h"
#include "winbase.h"
#include "wincrypt.h"
#include "winreg.h"
#include "winuser.h"
#include "wine/debug.h"
#include "wine/list.h"
#include "crypt32_private.h"
#include "cryptres.h"
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
static const WCHAR DllW[] = { 'D','l','l',0 };
static void init_function_sets(void);
static void init_oid_info(HINSTANCE hinst);
static void free_function_sets(void);
static void free_oid_info(void);
void crypt_oid_init(HINSTANCE hinst)
{
init_function_sets();
init_oid_info(hinst);
}
void crypt_oid_free(void)
{
free_function_sets();
free_oid_info();
}
static CRITICAL_SECTION funcSetCS;
static struct list funcSets;
struct OIDFunctionSet
{
LPSTR name;
CRITICAL_SECTION cs; /* protects functions */
struct list functions;
struct list next;
};
struct OIDFunction
{
DWORD encoding;
CRYPT_OID_FUNC_ENTRY entry;
struct list next;
};
static void init_function_sets(void)
{
InitializeCriticalSection(&funcSetCS);
funcSetCS.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": funcSetCS");
list_init(&funcSets);
}
static void free_function_sets(void)
{
struct OIDFunctionSet *setCursor, *setNext;
LIST_FOR_EACH_ENTRY_SAFE(setCursor, setNext, &funcSets,
struct OIDFunctionSet, next)
{
struct OIDFunction *functionCursor, *funcNext;
list_remove(&setCursor->next);
CryptMemFree(setCursor->name);
LIST_FOR_EACH_ENTRY_SAFE(functionCursor, funcNext,
&setCursor->functions, struct OIDFunction, next)
{
list_remove(&functionCursor->next);
CryptMemFree(functionCursor);
}
setCursor->cs.DebugInfo->Spare[0] = 0;
DeleteCriticalSection(&setCursor->cs);
CryptMemFree(setCursor);
}
funcSetCS.DebugInfo->Spare[0] = 0;
DeleteCriticalSection(&funcSetCS);
}
/* There is no free function associated with this; therefore, the sets are
* freed when crypt32.dll is unloaded.
*/
HCRYPTOIDFUNCSET WINAPI CryptInitOIDFunctionSet(LPCSTR pszFuncName,
DWORD dwFlags)
{
struct OIDFunctionSet *cursor, *ret = NULL;
TRACE("(%s, %x)\n", debugstr_a(pszFuncName), dwFlags);
EnterCriticalSection(&funcSetCS);
LIST_FOR_EACH_ENTRY(cursor, &funcSets, struct OIDFunctionSet, next)
{
if (!strcasecmp(pszFuncName, cursor->name))
{
ret = (HCRYPTOIDFUNCSET)cursor;
break;
}
}
if (!ret)
{
ret = CryptMemAlloc(sizeof(struct OIDFunctionSet));
if (ret)
{
memset(ret, 0, sizeof(*ret));
ret->name = CryptMemAlloc(strlen(pszFuncName) + 1);
if (ret->name)
{
InitializeCriticalSection(&ret->cs);
ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": OIDFunctionSet.cs");
list_init(&ret->functions);
strcpy(ret->name, pszFuncName);
list_add_tail(&funcSets, &ret->next);
}
else
{
CryptMemFree(ret);
ret = NULL;
}
}
}
LeaveCriticalSection(&funcSetCS);
return (HCRYPTOIDFUNCSET)ret;
}
static char *CRYPT_GetKeyName(DWORD dwEncodingType, LPCSTR pszFuncName,
LPCSTR pszOID)
{
static const char szEncodingTypeFmt[] =
"Software\\Microsoft\\Cryptography\\OID\\EncodingType %d\\%s\\%s";
UINT len;
char numericOID[7]; /* enough for "#65535" */
const char *oid;
LPSTR szKey;
/* MSDN says the encoding type is a mask, but it isn't treated that way.
* (E.g., if dwEncodingType were 3, the key names "EncodingType 1" and
* "EncodingType 2" would be expected if it were a mask. Instead native
* stores values in "EncodingType 3".
*/
if (!HIWORD(pszOID))
{
snprintf(numericOID, sizeof(numericOID), "#%d", LOWORD(pszOID));
oid = numericOID;
}
else
oid = pszOID;
/* This is enough: the lengths of the two string parameters are explicitly
* counted, and we need up to five additional characters for the encoding
* type. These are covered by the "%d", "%s", and "%s" characters in the
* format specifier that are removed by sprintf.
*/
len = sizeof(szEncodingTypeFmt) + lstrlenA(pszFuncName) + lstrlenA(oid);
szKey = CryptMemAlloc(len);
if (szKey)
sprintf(szKey, szEncodingTypeFmt, dwEncodingType, pszFuncName, oid);
return szKey;
}
BOOL WINAPI CryptGetDefaultOIDDllList(HCRYPTOIDFUNCSET hFuncSet,
DWORD dwEncodingType, LPWSTR pwszDllList, DWORD *pcchDllList)
{
BOOL ret = TRUE;
struct OIDFunctionSet *set = (struct OIDFunctionSet *)hFuncSet;
char *keyName;
HKEY key;
long rc;
TRACE("(%p, %d, %p, %p)\n", hFuncSet, dwEncodingType, pwszDllList,
pcchDllList);
keyName = CRYPT_GetKeyName(dwEncodingType, set->name, "DEFAULT");
rc = RegCreateKeyExA(HKEY_LOCAL_MACHINE, keyName, 0, NULL, 0,
KEY_READ, NULL, &key, NULL);
if (!rc)
{
DWORD size = *pcchDllList * sizeof(WCHAR);
rc = RegQueryValueExW(key, DllW, NULL, NULL, (LPBYTE)pwszDllList,
&size);
if (!rc)
*pcchDllList = size / sizeof(WCHAR);
else
{
/* No value, return an empty list */
if (*pcchDllList)
*pwszDllList = '\0';
*pcchDllList = 1;
}
RegCloseKey(key);
}
else
{
SetLastError(rc);
ret = FALSE;
}
CryptMemFree(keyName);
return ret;
}
BOOL WINAPI CryptInstallOIDFunctionAddress(HMODULE hModule,
DWORD dwEncodingType, LPCSTR pszFuncName, DWORD cFuncEntry,
const CRYPT_OID_FUNC_ENTRY rgFuncEntry[], DWORD dwFlags)
{
BOOL ret = TRUE;
struct OIDFunctionSet *set;
TRACE("(%p, %d, %s, %d, %p, %08x)\n", hModule, dwEncodingType,
debugstr_a(pszFuncName), cFuncEntry, rgFuncEntry, dwFlags);
set = (struct OIDFunctionSet *)CryptInitOIDFunctionSet(pszFuncName, 0);
if (set)
{
DWORD i;
EnterCriticalSection(&set->cs);
for (i = 0; ret && i < cFuncEntry; i++)
{
struct OIDFunction *func;
if (HIWORD(rgFuncEntry[i].pszOID))
func = CryptMemAlloc(sizeof(struct OIDFunction)
+ strlen(rgFuncEntry[i].pszOID) + 1);
else
func = CryptMemAlloc(sizeof(struct OIDFunction));
if (func)
{
func->encoding = dwEncodingType;
if (HIWORD(rgFuncEntry[i].pszOID))
{
LPSTR oid;
oid = (LPSTR)((LPBYTE)func + sizeof(*func));
strcpy(oid, rgFuncEntry[i].pszOID);
func->entry.pszOID = oid;
}
else
func->entry.pszOID = rgFuncEntry[i].pszOID;
func->entry.pvFuncAddr = rgFuncEntry[i].pvFuncAddr;
list_add_tail(&set->functions, &func->next);
}
else
ret = FALSE;
}
LeaveCriticalSection(&set->cs);
}
else
ret = FALSE;
return ret;
}
static BOOL CRYPT_GetFuncFromReg(DWORD dwEncodingType, LPCSTR pszOID,
LPCSTR szFuncName, LPVOID *ppvFuncAddr, HCRYPTOIDFUNCADDR *phFuncAddr)
{
BOOL ret = FALSE;
char *keyName;
const char *funcName;
HKEY key;
long rc;
keyName = CRYPT_GetKeyName(dwEncodingType, szFuncName, pszOID);
rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &key);
if (!rc)
{
DWORD type, size = 0;
rc = RegQueryValueExA(key, "FuncName", NULL, &type, NULL, &size);
if (rc == ERROR_MORE_DATA && type == REG_SZ)
{
funcName = CryptMemAlloc(size);
rc = RegQueryValueExA(key, "FuncName", NULL, &type,
(LPBYTE)funcName, &size);
}
else
funcName = szFuncName;
rc = RegQueryValueExW(key, DllW, NULL, &type, NULL, &size);
if (rc == ERROR_MORE_DATA && type == REG_SZ)
{
LPWSTR dllName = CryptMemAlloc(size);
if (dllName)
{
rc = RegQueryValueExW(key, DllW, NULL, NULL,
(LPBYTE)dllName, &size);
if (!rc)
{
HMODULE lib;
/* This is a bit of a hack; MSDN describes a more
* complicated unload routine than this will allow.
* Still, this seems to suffice for now.
*/
lib = LoadLibraryW(dllName);
if (lib)
{
*ppvFuncAddr = GetProcAddress(lib, szFuncName);
if (*ppvFuncAddr)
{
*phFuncAddr = (HCRYPTOIDFUNCADDR)lib;
ret = TRUE;
}
else
{
/* Unload the library, the caller doesn't want
* to unload it when the return value is NULL.
*/
FreeLibrary(lib);
}
}
}
else
SetLastError(rc);
CryptMemFree(dllName);
}
}
else
SetLastError(rc);
if (funcName != szFuncName)
CryptMemFree((char *)funcName);
RegCloseKey(key);
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -