Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

65 lines
1.7 KiB

// KeyManager.cpp: implementation of the CKeyManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "keycrypto.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// this is a optional Entropy ...
static const BYTE __STR_CRAP[] = "1^k\0\x99$\0\\*m$\0.)\nj#\t&H\0%!FhLG%@-<v";
static LPCWSTR __STR_DESC = L"passport2.0";
CKeyCrypto::CKeyCrypto()
{
m_EntropyBlob.pbData = (PBYTE)__STR_CRAP;
m_EntropyBlob.cbData = (DWORD)sizeof(__STR_CRAP);
}
HRESULT CKeyCrypto::encryptKey(DATA_BLOB* input, DATA_BLOB* output)
{
if (!input || !output)
return E_INVALIDARG;
HRESULT hr = S_OK;
if(!::CryptProtectData(input, __STR_DESC, &m_EntropyBlob, NULL, NULL,
CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN,
output))
{
hr = HRESULT_FROM_WIN32(::GetLastError());
}
return hr;
}
HRESULT CKeyCrypto::decryptKey(DATA_BLOB* input, DATA_BLOB* output)
{
if (!input || !output)
return E_INVALIDARG;
HRESULT hr = S_OK;
LPWSTR pstrDesc = NULL;
if(!::CryptUnprotectData(input, &pstrDesc, &m_EntropyBlob, NULL, NULL,
CRYPTPROTECT_UI_FORBIDDEN, output))
{
hr = HRESULT_FROM_WIN32(::GetLastError());
}
// this error case should never happen -- if crytoAPI doing the right things
if(!pstrDesc)
hr = E_FAIL;
else
{
if ( wcscmp(pstrDesc, __STR_DESC) != 0)
hr = E_FAIL;
::LocalFree(pstrDesc);
}
return hr;
}