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.

169 lines
4.5 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Microsoft Windows, Copyright (C) Microsoft Corporation, 2000
  3. File: EncryptedData.h
  4. Content: Declaration of the CEncryptedData.
  5. History: 11-15-99 dsie created
  6. ------------------------------------------------------------------------------*/
  7. #ifndef __ENCRYPTEDDATA_H_
  8. #define __ENCRYPTEDDATA_H_
  9. #include "Resource.h"
  10. #include "Lock.h"
  11. #include "Error.h"
  12. #include "Debug.h"
  13. #include "Algorithm.h"
  14. ////////////////////
  15. //
  16. // Local defines.
  17. //
  18. typedef struct _EncryptedDataInfo
  19. {
  20. DATA_BLOB VersionBlob;
  21. DATA_BLOB AlgIDBlob;
  22. DATA_BLOB KeyLengthBlob;
  23. DATA_BLOB IVBlob;
  24. DATA_BLOB SaltBlob;
  25. DATA_BLOB CipherBlob;
  26. } CAPICOM_ENCTYPTED_DATA_INFO, * PCAPICOM_ENCRYPTED_DATA_INFO;
  27. ////////////////////////////////////////////////////////////////////////////////
  28. //
  29. // CEncryptedData
  30. //
  31. class ATL_NO_VTABLE CEncryptedData :
  32. public CComObjectRootEx<CComMultiThreadModel>,
  33. public CComCoClass<CEncryptedData, &CLSID_EncryptedData>,
  34. public ICAPICOMError<CEncryptedData, &IID_IEncryptedData>,
  35. public IDispatchImpl<IEncryptedData, &IID_IEncryptedData, &LIBID_CAPICOM,
  36. CAPICOM_MAJOR_VERSION, CAPICOM_MINOR_VERSION>,
  37. public IObjectSafetyImpl<CEncryptedData, INTERFACESAFE_FOR_UNTRUSTED_CALLER |
  38. INTERFACESAFE_FOR_UNTRUSTED_DATA>
  39. {
  40. public:
  41. CEncryptedData()
  42. {
  43. }
  44. DECLARE_REGISTRY_RESOURCEID(IDR_ENCRYPTEDDATA)
  45. DECLARE_GET_CONTROLLING_UNKNOWN()
  46. DECLARE_PROTECT_FINAL_CONSTRUCT()
  47. BEGIN_COM_MAP(CEncryptedData)
  48. COM_INTERFACE_ENTRY(IEncryptedData)
  49. COM_INTERFACE_ENTRY(IDispatch)
  50. COM_INTERFACE_ENTRY(IObjectSafety)
  51. COM_INTERFACE_ENTRY(ISupportErrorInfo)
  52. END_COM_MAP()
  53. BEGIN_CATEGORY_MAP(CEncryptedData)
  54. IMPLEMENTED_CATEGORY(CATID_SafeForScripting)
  55. IMPLEMENTED_CATEGORY(CATID_SafeForInitializing)
  56. END_CATEGORY_MAP()
  57. HRESULT FinalConstruct()
  58. {
  59. HRESULT hr;
  60. if (FAILED(hr = m_Lock.Initialized()))
  61. {
  62. DebugTrace("Error [%#x]: Critical section could not be created for EncryptedData object.\n", hr);
  63. return hr;
  64. }
  65. //
  66. // Create embeded IAlgorithm.
  67. //
  68. if (FAILED(hr = ::CreateAlgorithmObject(FALSE, TRUE, &m_pIAlgorithm)))
  69. {
  70. DebugTrace("Error [%#x]: CreateAlgorithmObject() failed inside CEncryptedData::FinalConstruct().\n", hr);
  71. return hr;
  72. }
  73. //
  74. // Update member variables.
  75. //
  76. m_ContentBlob.cbData = 0;
  77. m_ContentBlob.pbData = NULL;
  78. return S_OK;
  79. }
  80. void FinalRelease()
  81. {
  82. if (m_ContentBlob.pbData)
  83. {
  84. ::CoTaskMemFree(m_ContentBlob.pbData);
  85. }
  86. for (DWORD i = 0; i < m_bstrSecret.Length(); i++)
  87. {
  88. m_bstrSecret.m_str[i] = (WCHAR) 0x0aa0;
  89. m_bstrSecret.m_str[i] = (WCHAR) 0xa00a;
  90. }
  91. m_bstrSecret.Empty();
  92. m_pIAlgorithm.Release();
  93. }
  94. //
  95. // IEncryptedData
  96. //
  97. public:
  98. STDMETHOD(Decrypt)
  99. (/*[in]*/ BSTR EncryptedMessage);
  100. STDMETHOD(Encrypt)
  101. (/*[in, defaultvalue(CAPICOM_BASE64_ENCODE)]*/ CAPICOM_ENCODING_TYPE EncodingType,
  102. /*[out, retval]*/ BSTR * pVal);
  103. STDMETHOD(SetSecret)
  104. (/*[in]*/ BSTR newVal,
  105. /*[in, defaultvalue(SECRET_PASSWORD)]*/ CAPICOM_SECRET_TYPE SecretType);
  106. STDMETHOD(get_Algorithm)
  107. (/*[out, retval]*/ IAlgorithm ** pVal);
  108. STDMETHOD(get_Content)
  109. (/*[out, retval]*/ BSTR * pVal);
  110. STDMETHOD(put_Content)
  111. (/*[in]*/ BSTR newVal);
  112. private:
  113. CLock m_Lock;
  114. DATA_BLOB m_ContentBlob;
  115. CComBSTR m_bstrSecret;
  116. CAPICOM_SECRET_TYPE m_SecretType;
  117. CComPtr<IAlgorithm> m_pIAlgorithm;
  118. STDMETHOD(OpenToEncode)
  119. (DATA_BLOB * pSaltBlob,
  120. HCRYPTPROV * phCryptProv,
  121. HCRYPTKEY * phKey);
  122. STDMETHOD(OpenToDecode)
  123. (BSTR EncryptedMessage,
  124. HCRYPTPROV * phCryptProv,
  125. HCRYPTKEY * phKey,
  126. CAPICOM_ENCTYPTED_DATA_INFO * pEncryptedDataInfo);
  127. STDMETHOD(GenerateKey)
  128. (HCRYPTPROV hCryptProv,
  129. CAPICOM_ENCRYPTION_ALGORITHM AlgoName,
  130. CAPICOM_ENCRYPTION_KEY_LENGTH KeyLength,
  131. DATA_BLOB SaltBlob,
  132. HCRYPTKEY * phKey);
  133. };
  134. #endif //__ENCRYPTEDDATA_H_