⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oid.c

📁 这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统
💻 C
📖 第 1 页 / 共 4 页
字号:
        SetLastError(rc);
    CryptMemFree(keyName);
    return ret;
}

BOOL WINAPI CryptGetOIDFunctionAddress(HCRYPTOIDFUNCSET hFuncSet,
 DWORD dwEncodingType, LPCSTR pszOID, DWORD dwFlags, void **ppvFuncAddr,
 HCRYPTOIDFUNCADDR *phFuncAddr)
{
    BOOL ret = FALSE;
    struct OIDFunctionSet *set = (struct OIDFunctionSet *)hFuncSet;

    TRACE("(%p, %d, %s, %08x, %p, %p)\n", hFuncSet, dwEncodingType,
     debugstr_a(pszOID), dwFlags, ppvFuncAddr, phFuncAddr);

    *ppvFuncAddr = NULL;
    if (!(dwFlags & CRYPT_GET_INSTALLED_OID_FUNC_FLAG))
    {
        struct OIDFunction *function;

        EnterCriticalSection(&set->cs);
        LIST_FOR_EACH_ENTRY(function, &set->functions, struct OIDFunction, next)
        {
            if (function->encoding == dwEncodingType)
            {
                if (HIWORD(pszOID))
                {
                    if (HIWORD(function->entry.pszOID) &&
                     !strcasecmp(function->entry.pszOID, pszOID))
                    {
                        *ppvFuncAddr = function->entry.pvFuncAddr;
                        *phFuncAddr = NULL; /* FIXME: what should it be? */
                        ret = TRUE;
                        break;
                    }
                }
                else if (function->entry.pszOID == pszOID)
                {
                    *ppvFuncAddr = function->entry.pvFuncAddr;
                    *phFuncAddr = NULL; /* FIXME: what should it be? */
                    ret = TRUE;
                    break;
                }
            }
        }
        LeaveCriticalSection(&set->cs);
    }
    if (!*ppvFuncAddr)
        ret = CRYPT_GetFuncFromReg(dwEncodingType, pszOID, set->name,
         ppvFuncAddr, phFuncAddr);
    return ret;
}

BOOL WINAPI CryptFreeOIDFunctionAddress(HCRYPTOIDFUNCADDR hFuncAddr,
 DWORD dwFlags)
{
    TRACE("(%p, %08x)\n", hFuncAddr, dwFlags);

    /* FIXME: as MSDN states, need to check for DllCanUnloadNow in the DLL,
     * and only unload it if it can be unloaded.  Also need to implement ref
     * counting on the functions.
     */
    FreeLibrary((HMODULE)hFuncAddr);
    return TRUE;
}

BOOL WINAPI CryptGetDefaultOIDFunctionAddress(HCRYPTOIDFUNCSET hFuncSet,
 DWORD dwEncodingType, LPCWSTR pwszDll, DWORD dwFlags, void *ppvFuncAddr,
 HCRYPTOIDFUNCADDR *phFuncAddr)
{
    FIXME("(%p, %d, %s, %08x, %p, %p): stub\n", hFuncSet, dwEncodingType,
     debugstr_w(pwszDll), dwFlags, ppvFuncAddr, phFuncAddr);
    return FALSE;
}

/***********************************************************************
 *             CryptRegisterOIDFunction (CRYPT32.@)
 *
 * Register the DLL and the functions it uses to cover the combination
 * of encoding type, functionname and OID.
 *
 * PARAMS
 *  dwEncodingType       [I] Encoding type to be used.
 *  pszFuncName          [I] Name of the function to be registered.
 *  pszOID               [I] OID of the function (numeric or string).
 *  pwszDll              [I] The DLL that is to be registered.
 *  pszOverrideFuncName  [I] Name of the function in the DLL.
 *
 * RETURNS
 *  Success: TRUE.
 *  Failure: FALSE. (Look at GetLastError()).
 *
 * NOTES
 *  Registry errors are always reported via SetLastError().
 */
BOOL WINAPI CryptRegisterOIDFunction(DWORD dwEncodingType, LPCSTR pszFuncName,
                  LPCSTR pszOID, LPCWSTR pwszDll, LPCSTR pszOverrideFuncName)
{
    LONG r;
    HKEY hKey;
    LPSTR szKey;

    TRACE("(%x, %s, %s, %s, %s)\n", dwEncodingType, pszFuncName,
     debugstr_a(pszOID), debugstr_w(pwszDll), pszOverrideFuncName);

    /* This only registers functions for encoding certs, not messages */
    if (!GET_CERT_ENCODING_TYPE(dwEncodingType))
        return TRUE;

    /* Native does nothing pwszDll is NULL */
    if (!pwszDll)
        return TRUE;

    /* I'm not matching MS bug for bug here, because I doubt any app depends on
     * it:  native "succeeds" if pszFuncName is NULL, but the nonsensical entry
     * it creates would never be used.
     */
    if (!pszFuncName || !pszOID)
    {
        SetLastError(E_INVALIDARG);
        return FALSE;
    }

    szKey = CRYPT_GetKeyName(dwEncodingType, pszFuncName, pszOID);
    TRACE("Key name is %s\n", debugstr_a(szKey));

    if (!szKey)
        return FALSE;

    r = RegCreateKeyA(HKEY_LOCAL_MACHINE, szKey, &hKey);
    CryptMemFree(szKey);

    if (r != ERROR_SUCCESS) goto error_close_key;

    /* write the values */
    if (pszOverrideFuncName)
    {
        r = RegSetValueExA(hKey, "FuncName", 0, REG_SZ,
             (const BYTE*)pszOverrideFuncName, lstrlenA(pszOverrideFuncName) + 1);
        if (r != ERROR_SUCCESS) goto error_close_key;
    }
    r = RegSetValueExW(hKey, DllW, 0, REG_SZ, (const BYTE*) pwszDll,
         (lstrlenW(pwszDll) + 1) * sizeof (WCHAR));

error_close_key:

    RegCloseKey(hKey);

    if (r != ERROR_SUCCESS) 
    {
        SetLastError(r);
        return FALSE;
    }

    return TRUE;
}

/***********************************************************************
 *             CryptUnregisterOIDFunction (CRYPT32.@)
 */
BOOL WINAPI CryptUnregisterOIDFunction(DWORD dwEncodingType, LPCSTR pszFuncName,
 LPCSTR pszOID)
{
    LPSTR szKey;
    LONG rc;

    TRACE("%x %s %s\n", dwEncodingType, pszFuncName, pszOID);

    if (!GET_CERT_ENCODING_TYPE(dwEncodingType))
        return TRUE;

    if (!pszFuncName || !pszOID)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    szKey = CRYPT_GetKeyName(dwEncodingType, pszFuncName, pszOID);
    rc = RegDeleteKeyA(HKEY_LOCAL_MACHINE, szKey);
    CryptMemFree(szKey);
    if (rc)
        SetLastError(rc);
    return rc ? FALSE : TRUE;
}

BOOL WINAPI CryptGetOIDFunctionValue(DWORD dwEncodingType, LPCSTR pszFuncName,
 LPCSTR pszOID, LPCWSTR pwszValueName, DWORD *pdwValueType, BYTE *pbValueData,
 DWORD *pcbValueData)
{
    LPSTR szKey;
    LONG rc;
    HKEY hKey;

    TRACE("%x %s %s %s %p %p %p\n", dwEncodingType, debugstr_a(pszFuncName),
     debugstr_a(pszOID), debugstr_w(pwszValueName), pdwValueType, pbValueData,
     pcbValueData);

    if (!GET_CERT_ENCODING_TYPE(dwEncodingType))
        return TRUE;

    if (!pszFuncName || !pszOID || !pwszValueName)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    szKey = CRYPT_GetKeyName(dwEncodingType, pszFuncName, pszOID);
    rc = RegOpenKeyA(HKEY_LOCAL_MACHINE, szKey, &hKey);
    CryptMemFree(szKey);
    if (rc)
        SetLastError(rc);
    else
    {
        rc = RegQueryValueExW(hKey, pwszValueName, NULL, pdwValueType,
         pbValueData, pcbValueData);
        if (rc)
            SetLastError(rc);
        RegCloseKey(hKey);
    }
    return rc ? FALSE : TRUE;
}

BOOL WINAPI CryptSetOIDFunctionValue(DWORD dwEncodingType, LPCSTR pszFuncName,
 LPCSTR pszOID, LPCWSTR pwszValueName, DWORD dwValueType,
 const BYTE *pbValueData, DWORD cbValueData)
{
    LPSTR szKey;
    LONG rc;
    HKEY hKey;

    TRACE("%x %s %s %s %d %p %d\n", dwEncodingType, debugstr_a(pszFuncName),
     debugstr_a(pszOID), debugstr_w(pwszValueName), dwValueType, pbValueData,
     cbValueData);

    if (!GET_CERT_ENCODING_TYPE(dwEncodingType))
        return TRUE;

    if (!pszFuncName || !pszOID || !pwszValueName)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    szKey = CRYPT_GetKeyName(dwEncodingType, pszFuncName, pszOID);
    rc = RegOpenKeyA(HKEY_LOCAL_MACHINE, szKey, &hKey);
    CryptMemFree(szKey);
    if (rc)
        SetLastError(rc);
    else
    {
        rc = RegSetValueExW(hKey, pwszValueName, 0, dwValueType, pbValueData,
         cbValueData);
        if (rc)
            SetLastError(rc);
        RegCloseKey(hKey);
    }
    return rc ? FALSE : TRUE;
}

static LPCWSTR CRYPT_FindStringInMultiString(LPCWSTR multi, LPCWSTR toFind)
{
    LPCWSTR ret = NULL, ptr;

    for (ptr = multi; ptr && *ptr && !ret; ptr += lstrlenW(ptr) + 1)
    {
        if (!lstrcmpiW(ptr, toFind))
            ret = ptr;
    }
    return ret;
}

static DWORD CRYPT_GetMultiStringCharacterLen(LPCWSTR multi)
{
    DWORD ret;

    if (multi)
    {
        LPCWSTR ptr;

        /* Count terminating empty string */
        ret = 1;
        for (ptr = multi; *ptr; ptr += lstrlenW(ptr) + 1)
            ret += lstrlenW(ptr) + 1;
    }
    else
        ret = 0;
    return ret;
}

static LPWSTR CRYPT_AddStringToMultiString(LPWSTR multi, LPCWSTR toAdd,
 DWORD index)
{
    LPWSTR ret;

    if (!multi)
    {
        /* FIXME: ignoring index, is that okay? */
        ret = CryptMemAlloc((lstrlenW(toAdd) + 2) * sizeof(WCHAR));
        if (ret)
        {
            /* copy string, including NULL terminator */
            memcpy(ret, toAdd, (lstrlenW(toAdd) + 1) * sizeof(WCHAR));
            /* add terminating empty string */
            *(ret + lstrlenW(toAdd) + 1) = 0;
        }
    }
    else
    {
        DWORD len = CRYPT_GetMultiStringCharacterLen(multi);

        ret = CryptMemRealloc(multi, (len + lstrlenW(toAdd) + 1) *
         sizeof(WCHAR));
        if (ret)
        {
            LPWSTR spotToAdd;

            if (index == CRYPT_REGISTER_LAST_INDEX)
                spotToAdd = ret + len - 1;
            else
            {
                DWORD i;

                /* FIXME: if index is too large for the string, toAdd is
                 * added to the end.  Is that okay?
                 */
                for (i = 0, spotToAdd = ret; i < index && *spotToAdd;
                 spotToAdd += lstrlenW(spotToAdd) + 1)
                    ;
            }
            if (spotToAdd)
            {
                /* Copy existing string "right" */
                memmove(spotToAdd + lstrlenW(toAdd) + 1, spotToAdd,
                 (len - (spotToAdd - ret)) * sizeof(WCHAR));
                /* Copy new string */
                memcpy(spotToAdd, toAdd, (lstrlenW(toAdd) + 1) * sizeof(WCHAR));
            }
            else
            {
                CryptMemFree(ret);
                ret = NULL;
            }
        }
    }
    return ret;
}

static BOOL CRYPT_RemoveStringFromMultiString(LPWSTR multi, LPCWSTR toRemove)
{
    LPWSTR spotToRemove = (LPWSTR)CRYPT_FindStringInMultiString(multi,
     toRemove);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -