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

  1. // KeyManager.cpp: implementation of the CKeyManager class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "keycrypto.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. // this is a optional Entropy ...
  10. static const BYTE __STR_CRAP[] = "1^k\0\x99$\0\\*m$\0.)\nj#\t&H\0%!FhLG%@-<v";
  11. static LPCWSTR __STR_DESC = L"passport2.0";
  12. CKeyCrypto::CKeyCrypto()
  13. {
  14. m_EntropyBlob.pbData = (PBYTE)__STR_CRAP;
  15. m_EntropyBlob.cbData = (DWORD)sizeof(__STR_CRAP);
  16. }
  17. HRESULT CKeyCrypto::encryptKey(DATA_BLOB* input, DATA_BLOB* output)
  18. {
  19. if (!input || !output)
  20. return E_INVALIDARG;
  21. HRESULT hr = S_OK;
  22. if(!::CryptProtectData(input, __STR_DESC, &m_EntropyBlob, NULL, NULL,
  23. CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN,
  24. output))
  25. {
  26. hr = HRESULT_FROM_WIN32(::GetLastError());
  27. }
  28. return hr;
  29. }
  30. HRESULT CKeyCrypto::decryptKey(DATA_BLOB* input, DATA_BLOB* output)
  31. {
  32. if (!input || !output)
  33. return E_INVALIDARG;
  34. HRESULT hr = S_OK;
  35. LPWSTR pstrDesc = NULL;
  36. if(!::CryptUnprotectData(input, &pstrDesc, &m_EntropyBlob, NULL, NULL,
  37. CRYPTPROTECT_UI_FORBIDDEN, output))
  38. {
  39. hr = HRESULT_FROM_WIN32(::GetLastError());
  40. }
  41. // this error case should never happen -- if crytoAPI doing the right things
  42. if(!pstrDesc)
  43. hr = E_FAIL;
  44. else
  45. {
  46. if ( wcscmp(pstrDesc, __STR_DESC) != 0)
  47. hr = E_FAIL;
  48. ::LocalFree(pstrDesc);
  49. }
  50. return hr;
  51. }