📄 sadvapi.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// FILE : cryptapi.c //
// DESCRIPTION : Crypto API interface //
// AUTHOR : //
// HISTORY : //
// Dec 6 1994 larrys New //
// Nov 13 1995 philh Lean & mean version for the STB //
// Aug 18 1996 mattt sadvapi chgs from ecm tree //
// Oct 23 1997 jeffspel Checkin for SChannel use //
// Feb 08 1999 sfield avoid critical section, add exception handling //
// //
// Copyright (C) 1993 Microsoft Corporation All Rights Reserved //
/////////////////////////////////////////////////////////////////////////////
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>
#include <wincrypt.h>
#include "swincryp.h"
#include "scp.h"
typedef struct _VTableStruc {
HCRYPTPROV hProv; // Handle to provider
LONG Inuse;
} VTableStruc, *PVTableStruc;
typedef struct _VKeyStruc {
PVTableStruc pVTable; // pointer to provider
HCRYPTKEY hKey; // Handle to key
} VKeyStruc, *PVKeyStruc;
typedef struct _VHashStruc {
PVTableStruc pVTable; // pointer to provider
HCRYPTHASH hHash; // Handle to hash
} VHashStruc, *PVHashStruc;
void __inline EnterProviderCritSec(IN PVTableStruc pVTable);
LONG __inline LeaveProviderCritSec(IN PVTableStruc pVTable);
PVKeyStruc BuildVKey(IN PVTableStruc pVTable);
PVHashStruc BuildVHash(IN PVTableStruc pVTable);
/*
- CryptAcquireContextW
-
* Purpose:
* The CryptAcquireContext function is used to acquire a context
* handle to a cryptograghic service provider (CSP).
*
*
* Parameters:
* OUT phProv - Handle to a CSP
* IN OUT pszIdentity - Pointer to the name of the context's
* keyset.
* IN OUT pszProvider - Pointer to the name of the provider.
* IN dwProvType - Requested CSP type
* IN dwFlags - Flags values
*
* Returns:
*/
BOOL
WINAPI SCryptAcquireContextW(OUT HCRYPTPROV *phProv,
IN OUT LPCWSTR pwszIdentity,
IN OUT LPCWSTR pwszProvider,
IN DWORD dwProvType,
IN DWORD dwFlags)
{
CHAR *pszIdentity = NULL;
CHAR *pszProvider = NULL;
long c = 0;
long i;
BOOL fRet = FALSE;
if (pwszIdentity)
{
c = wcslen(pwszIdentity);
if (NULL == (pszIdentity = (CHAR*)LocalAlloc(LMEM_ZEROINIT,
(c+1) * sizeof(CHAR))))
goto Ret;
for (i=0;i<c;i++)
pszIdentity[i] = (CHAR)pwszIdentity[i];
pszIdentity[i] = 0;
}
if (pwszProvider)
{
c = wcslen(pwszProvider);
if (NULL == (pszProvider = (CHAR*)LocalAlloc(LMEM_ZEROINIT,
(c+1) * sizeof(CHAR))))
goto Ret;
for (i=0;i<c;i++)
pszProvider[i] = (CHAR)pwszProvider[i];
pszProvider[i] = 0;
}
fRet = SCryptAcquireContextA(
phProv,
pszIdentity,
pszProvider,
dwProvType,
dwFlags
);
Ret:
if (pszIdentity)
LocalFree(pszIdentity);
if (pszProvider)
LocalFree(pszProvider);
return fRet;
}
BOOL
WINAPI SCryptAcquireContextA(OUT HCRYPTPROV *phProv,
IN OUT LPCSTR pszIdentity,
IN OUT LPCSTR pszProvider, // ignored
IN DWORD dwProvType, // ignored
IN DWORD dwFlags)
{
PVTableStruc pVTable = NULL;
VTableProvStruc TableForProvider;
BOOL fRet = FALSE;
pVTable = (PVTableStruc)LocalAlloc(
LMEM_ZEROINIT,
sizeof(VTableStruc)
);
if( pVTable == NULL ) {
*phProv = 0;
return FALSE;
}
memset(&TableForProvider, 0, sizeof(TableForProvider));
TableForProvider.Version = 2;
TableForProvider.FuncVerifyImage = NULL;
TableForProvider.FuncReturnhWnd = 0;
TableForProvider.dwProvType = dwProvType;
TableForProvider.pbContextInfo = NULL;
TableForProvider.cbContextInfo = 0;
__try {
fRet = CPAcquireContext(
&pVTable->hProv,
(LPSTR)pszIdentity,
dwFlags,
&TableForProvider
);
} __except (EXCEPTION_EXECUTE_HANDLER) {
fRet = FALSE;
ASSERT( fRet );
}
if(!fRet)
{
LocalFree(pVTable);
pVTable = NULL;
}
else
{
if (dwFlags & CRYPT_DELETEKEYSET)
{
LocalFree(pVTable);
pVTable = NULL;
} else {
pVTable->Inuse = 1;
}
}
*phProv = (HCRYPTPROV)pVTable;
return fRet;
}
/*
- CryptReleaseContext
-
* Purpose:
* The CryptReleaseContext function is used to release a
* context created by CryptAcquireContext.
*
* Parameters:
* IN phProv - Handle to a CSP
* IN dwFlags - Flags values
*
* Returns:
*/
BOOL
WINAPI SCryptReleaseContext(IN HCRYPTPROV hProv,
IN DWORD dwFlags)
{
PVTableStruc pVTable = (PVTableStruc) hProv;
LONG ContextRefCount;
BOOL fRet = FALSE;
ContextRefCount = LeaveProviderCritSec( pVTable );
__try {
//
// for debug builds, catch fools leaking state.
//
ASSERT( ContextRefCount == 0 );
if( ContextRefCount != 0 ) {
SetLastError(ERROR_BUSY);
return FALSE;
}
fRet = CPReleaseContext(pVTable->hProv, dwFlags);
} __except ( EXCEPTION_EXECUTE_HANDLER ) {
fRet = FALSE;
ASSERT( fRet );
}
LocalFree(pVTable);
return fRet;
}
/*
- CryptGenKey
-
* Purpose:
* Generate cryptographic keys
*
*
* Parameters:
* IN hProv - Handle to a CSP
* IN Algid - Algorithm identifier
* IN dwFlags - Flags values
* OUT phKey - Handle to a generated key
*
* Returns:
*/
BOOL
WINAPI SCryptGenKey(IN HCRYPTPROV hProv,
IN ALG_ID Algid,
IN DWORD dwFlags,
OUT HCRYPTKEY * phKey)
{
PVTableStruc pVTable = (PVTableStruc) hProv;
PVKeyStruc pVKey;
BOOL fCritSec = FALSE;
BOOL fRet = FALSE;
__try {
*phKey = 0;
pVKey = BuildVKey(pVTable);
if( pVKey == NULL )
return FALSE;
EnterProviderCritSec(pVTable);
fCritSec = TRUE;
fRet = CPGenKey(pVTable->hProv, Algid, dwFlags, &pVKey->hKey);
} __except( EXCEPTION_EXECUTE_HANDLER ) {
fRet = FALSE;
ASSERT( fRet );
}
if( fCritSec ) {
LeaveProviderCritSec(pVTable);
}
if( fRet ) {
*phKey = (HCRYPTKEY) pVKey;
return TRUE;
}
if (pVKey)
LocalFree(pVKey);
__try {
*phKey = 0;
} __except (EXCEPTION_EXECUTE_HANDLER) {
; // gulp
}
return FALSE;
}
/*
- CryptDuplicateKey
-
* Purpose:
* Duplicate a cryptographic key
*
*
* Parameters:
* IN hKey - Handle to the key to be duplicated
* IN pdwReserved - Reserved for later use
* IN dwFlags - Flags values
* OUT phKey - Handle to the new duplicate key
*
* Returns:
*/
BOOL
WINAPI SCryptDuplicateKey(
IN HCRYPTKEY hKey,
IN DWORD *pdwReserved,
IN DWORD dwFlags,
OUT HCRYPTKEY * phKey
)
{
PVKeyStruc pVKey = (PVKeyStruc) hKey;
PVTableStruc pVTable;
PVKeyStruc pVNewKey;
BOOL fCritSec = FALSE;
BOOL fRet = FALSE;
__try {
*phKey = 0;
pVTable = pVKey->pVTable;
pVNewKey = BuildVKey(pVTable);
if( pVNewKey == NULL ) {
return FALSE;
}
EnterProviderCritSec(pVTable);
fCritSec = TRUE;
fRet = CPDuplicateKey(
pVTable->hProv,
pVKey->hKey,
pdwReserved,
dwFlags,
&pVNewKey->hKey
);
} __except( EXCEPTION_EXECUTE_HANDLER ) {
fRet = FALSE;
ASSERT( fRet );
}
if( fCritSec ) {
LeaveProviderCritSec(pVTable);
}
if( fRet ) {
*phKey = (HCRYPTKEY) pVNewKey;
return TRUE;
}
if (pVNewKey)
LocalFree(pVNewKey);
__try {
*phKey = 0;
} __except (EXCEPTION_EXECUTE_HANDLER) {
; // gulp
}
return FALSE;
}
/*
- CryptDeriveKey
-
* Purpose:
* Derive cryptographic keys from base data
*
*
* Parameters:
* IN hProv - Handle to a CSP
* IN Algid - Algorithm identifier
* IN hHash - Handle to hash of base data
* IN dwFlags - Flags values
* OUT phKey - Handle to a generated key
*
* Returns:
*/
BOOL
WINAPI SCryptDeriveKey(IN HCRYPTPROV hProv,
IN ALG_ID Algid,
IN HCRYPTHASH hHash,
IN DWORD dwFlags,
OUT HCRYPTKEY * phKey)
{
PVTableStruc pVTable = (PVTableStruc) hProv;
PVHashStruc pVHash = (PVHashStruc) hHash;
PVKeyStruc pVKey;
BOOL fCritSec = FALSE;
BOOL fRet = FALSE;
__try {
*phKey = 0;
if (pVHash->pVTable != pVTable)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
pVKey = BuildVKey(pVTable);
if( pVKey == NULL )
return FALSE;
EnterProviderCritSec(pVTable);
fCritSec = TRUE;
fRet = CPDeriveKey(
pVTable->hProv,
Algid,
pVHash->hHash,
dwFlags,
&pVKey->hKey
);
} __except (EXCEPTION_EXECUTE_HANDLER) {
fRet = FALSE;
ASSERT( fRet );
}
if( fCritSec ) {
LeaveProviderCritSec(pVTable);
}
if( fRet ) {
*phKey = (HCRYPTKEY) pVKey;
return TRUE;
}
if (pVKey)
LocalFree(pVKey);
__try {
*phKey = 0;
} __except (EXCEPTION_EXECUTE_HANDLER) {
; // gulp
}
return FALSE;
}
/*
- CryptDestroyKey
-
* Purpose:
* Destroys the cryptographic key that is being referenced
* with the hKey parameter
*
*
* Parameters:
* IN hKey - Handle to a key
*
* Returns:
*/
BOOL
WINAPI SCryptDestroyKey(IN HCRYPTKEY hKey)
{
PVKeyStruc pVKey = (PVKeyStruc) hKey;
PVTableStruc pVTable;
BOOL fCritSec = FALSE;
BOOL fRet = FALSE;
__try {
pVTable = pVKey->pVTable;
EnterProviderCritSec(pVTable);
fCritSec = TRUE;
fRet = CPDestroyKey(pVTable->hProv, pVKey->hKey);
} __except( EXCEPTION_EXECUTE_HANDLER ) {
fRet = FALSE;
ASSERT( fRet );
}
if( fCritSec ) {
LeaveProviderCritSec(pVTable);
}
LocalFree(pVKey);
return fRet;
}
/*
- CryptSetKeyParam
-
* Purpose:
* Allows applications to customize various aspects of the
* operations of a key
*
* Parameters:
* IN hKey - Handle to a key
* IN dwParam - Parameter number
* IN pbData - Pointer to data
* IN dwFlags - Flags values
*
* Returns:
*/
BOOL
WINAPI SCryptSetKeyParam(IN HCRYPTKEY hKey,
IN DWORD dwParam,
IN BYTE *pbData,
IN DWORD dwFlags)
{
PVKeyStruc pVKey = (PVKeyStruc) hKey;
PVTableStruc pVTable;
BOOL fCritSec = FALSE;
BOOL fRet = FALSE;
__try {
pVTable = pVKey->pVTable;
EnterProviderCritSec(pVTable);
fCritSec = TRUE;
fRet = CPSetKeyParam(
pVTable->hProv,
pVKey->hKey,
dwParam,
pbData,
dwFlags
);
} __except( EXCEPTION_EXECUTE_HANDLER ) {
fRet = FALSE;
ASSERT( fRet );
}
if( fCritSec )
LeaveProviderCritSec(pVTable);
return fRet;
}
/*
- CryptGetKeyParam
-
* Purpose:
* Allows applications to get various aspects of the
* operations of a key
*
* Parameters:
* IN hKey - Handle to a key
* IN dwParam - Parameter number
* IN pbData - Pointer to data
* IN pdwDataLen - Length of parameter data
* IN dwFlags - Flags values
*
* Returns:
*/
BOOL
WINAPI SCryptGetKeyParam(IN HCRYPTKEY hKey,
IN DWORD dwParam,
IN BYTE *pbData,
IN DWORD *pdwDataLen,
IN DWORD dwFlags)
{
PVKeyStruc pVKey = (PVKeyStruc) hKey;
PVTableStruc pVTable;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -