📄 rsaprovider.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//
//
/*
RSAProvider.cpp
This source file contains the RSA support library that this CSP
uses to do things that do not involve the WfSC.
History:
jamesku - Created 2/19/2001
*/
#include "RSAProvider.h"
#include "assert.h"
#define INTERNAL_MAX_KEY_SIZE (1024)
#define INTERNAL_MIN_KEY_SIZE (512)
#define BASE_PROVIDER_NAME TEXT("SCWBaseProvider")
/*
CRSAProvider::CRSAProvider
Purpose:
Constructor for the RSA Provider which acquires a context
for the RSAENH or RSABASE provider.
Params:
None.
Returns:
void
*/
CRSAProvider::CRSAProvider()
{
}
/*
CRSAProvider::Initialize
Purpose:
Initializes this object
Params:
bool bSilent: true if silent, false otherwise.
Returns:
TRUE if it succeeds, FALSE otherwise
*/
bool CRSAProvider::Initialize(bool bSilent)
{
m_hProv = NULL;
//Attempt to acquire the context
//(Note: lazy execution causes GetKeySizes() not to be called
//if AcquireContext failed)
BOOL ac = AcquireContext(bSilent);
BOOL gks = GetKeySizes();
if(!ac || !gks)
{
SetLastError(NTE_PROVIDER_DLL_FAIL);
//If we managed to get a HCRYPTPROV but still failed, release it.
if(m_hProv)
{
CryptReleaseContext(m_hProv, 0);
m_hProv=NULL;
}
return false;
}
return true;
}
/*
CRSAProvider::~CRSAProvider
Purpose:
Cleans up when we are done with the RSA provider.
Params:
None.
Returns:
void
*/
CRSAProvider::~CRSAProvider()
{
if(m_hProv)
CryptReleaseContext(m_hProv, 0);
}
/*
CRSAProvider::GetKeySizes
Purpose:
Finds the key sizes available and stores them in the
member variables m_dwMaxKeySize and m_dwMinKeySize
Params:
None.
Returns:
BOOL : returns TRUE if it could find the keysizes, FALSE otherwise
*/
BOOL CRSAProvider::GetKeySizes()
{
DWORD dwFlags = CRYPT_FIRST;
PROV_ENUMALGS_EX pbData;
DWORD dwDataLen = sizeof(pbData);
//Get the key size.
//TODO
while(CryptGetProvParam(
*this,
PP_ENUMALGS_EX,
reinterpret_cast<BYTE*>(&pbData),
&dwDataLen,
dwFlags))
{
dwFlags = 0;
switch(pbData.aiAlgid)
{
case CALG_RSA_KEYX:
{
//Store the keysize in our class variables.
m_dwMinKeySize = (pbData.dwMinLen < INTERNAL_MIN_KEY_SIZE ? INTERNAL_MIN_KEY_SIZE : pbData.dwMinLen);
m_dwMaxKeySize = (pbData.dwMaxLen > INTERNAL_MAX_KEY_SIZE ? INTERNAL_MAX_KEY_SIZE : pbData.dwMaxLen);
return TRUE;
}
}
dwDataLen = sizeof(pbData);
}
return FALSE;
}
/*
CRSAProvider::AcquireContext
Purpose:
Acquire the context
Params:
bool IN bSilent: Whether or not the new context is allowed to disp any UI.
Returns:
BOOL : whether or not a new context was acquired.
*/
BOOL CRSAProvider::AcquireContext(bool IN bSilent)
{
DWORD dwSilentFlag = (bSilent ? CRYPT_SILENT : 0);
WCHAR szProviderName[MAX_PATH] = {0};
// first try the enhanced provider
//TODO
wcscpy(szProviderName, MS_ENHANCED_PROV);
// Attach to the default provider
if(CryptAcquireContext(
&m_hProv,
BASE_PROVIDER_NAME,
szProviderName,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
return TRUE;
}
// if object (container) exists, then we've just got to open the container
if(NTE_EXISTS == GetLastError())
{
BOOL tempRet = CryptAcquireContext(&m_hProv,BASE_PROVIDER_NAME,szProviderName,PROV_RSA_FULL,0);
if(tempRet)
{
//printf("Success.\n");
return TRUE;
}
}
// Enhanded provider not found (or some other error), try the base provider
wcscpy(szProviderName, MS_DEF_PROV);
// Attach to the provider
if(CryptAcquireContext(
&m_hProv,
BASE_PROVIDER_NAME,
szProviderName,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
return TRUE;
}
// if object (container) exists, then we've just got to open the container
if(NTE_EXISTS == GetLastError())
{
if(CryptAcquireContext(
&m_hProv,
BASE_PROVIDER_NAME,
szProviderName,
PROV_RSA_FULL,
0))
{
return TRUE;
}
}
m_hProv = NULL;
return FALSE;
}
/*
CRSAProvider::GetMaxKeySize
Purpose:
Returns the maximum key size.
Params:
DWORD OUT & dwKeySize: Returned key size
Returns:
BOOL : TRUE if it completed successfully, FALSE otherwise.
*/
BOOL CRSAProvider::GetMaxKeySize(DWORD OUT & dwKeySize)
{
//Get the required information.
if(GetHandle())
{
dwKeySize = m_dwMaxKeySize;
return TRUE;
}
return FALSE;
}
/*
CRSAProvider::GetMinKeySize
Purpose:
Find the minimum key size for the RSA provider CSP.
Params:
DWORD OUT & dwKeySize: the new keysize
Returns:
BOOL : TRUE if it completed successfully, FALSE otherwise.
*/
BOOL CRSAProvider::GetMinKeySize(DWORD OUT & dwKeySize)
{
//Get the required information.
if(GetHandle())
{
dwKeySize = m_dwMinKeySize;
return TRUE;
}
return FALSE;
}
/*
CRSAProvider::GetKeyStepping
Purpose:
Find the key stepping of the RSA provider CSP
Params:
DWORD OUT & dwKeyStepping: the output key stepping
Returns:
BOOL : TRUE if it is successful, FALSE otherwise.
*/
BOOL CRSAProvider::GetKeyStepping(DWORD OUT & dwKeyStepping)
{
//Get the required information.
if(GetHandle())
{
dwKeyStepping = m_dwMaxKeySize - m_dwMinKeySize;
return TRUE;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -