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.

382 lines
12 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. incident.cpp
  5. Abstract:
  6. Encryption object
  7. Revision History:
  8. KalyaninN created 06/28/'00
  9. ********************************************************************/
  10. // SAFEncrypt.cpp : Implementation of CSAFEncrypt
  11. #include "stdafx.h"
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CSAFEncrypt
  14. #include <HCP_trace.h>
  15. /////////////////////////////////////////////////////////////////////////////
  16. // construction / destruction
  17. // **************************************************************************
  18. CSAFEncrypt::CSAFEncrypt()
  19. {
  20. m_EncryptionType = 1;
  21. }
  22. // **************************************************************************
  23. CSAFEncrypt::~CSAFEncrypt()
  24. {
  25. Cleanup();
  26. }
  27. // **************************************************************************
  28. void CSAFEncrypt::Cleanup(void)
  29. {
  30. }
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CSAFEncrypt properties
  33. STDMETHODIMP CSAFEncrypt::get_EncryptionType(long *pVal)
  34. {
  35. __HCP_BEGIN_PROPERTY_GET2("CSAFEncrypt::get_EncryptionType",hr,pVal,m_EncryptionType);
  36. __HCP_END_PROPERTY(hr);
  37. }
  38. STDMETHODIMP CSAFEncrypt::put_EncryptionType(long pVal)
  39. {
  40. __HCP_BEGIN_PROPERTY_PUT("CSAFEncrypt::put_EncryptionType",hr);
  41. if(pVal < 0)
  42. {
  43. __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  44. }
  45. m_EncryptionType = pVal;
  46. __HCP_END_PROPERTY(hr);
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CSAFEncrypt Methods
  50. STDMETHODIMP CSAFEncrypt::EncryptString(BSTR bstrEncryptionKey, BSTR bstrInputString, BSTR *bstrEncryptedString)
  51. {
  52. __HCP_FUNC_ENTRY( "CSAFEncrypt::EncryptString" );
  53. HRESULT hr;
  54. CComPtr<IStream> streamPlain;
  55. CComPtr<IStream> streamEnc;
  56. CComPtr<MPC::EncryptedStream> stream;
  57. MPC::Serializer_IStream streamSerializerPlain;
  58. CComBSTR bstrEncString;
  59. HGLOBAL hg;
  60. STATSTG stg; ::ZeroMemory( &stg, sizeof(stg) );
  61. DWORD dwLen;
  62. // Validate the input and output parameters.
  63. __MPC_PARAMCHECK_BEGIN(hr)
  64. __MPC_PARAMCHECK_POINTER_AND_SET(bstrEncryptedString, NULL);
  65. __MPC_PARAMCHECK_END();
  66. __MPC_EXIT_IF_METHOD_FAILS(hr, streamSerializerPlain << CComBSTR(bstrInputString));
  67. __MPC_EXIT_IF_METHOD_FAILS(hr, streamSerializerPlain.Reset());
  68. __MPC_EXIT_IF_METHOD_FAILS(hr, streamSerializerPlain.GetStream( &streamPlain ));
  69. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CreateStreamOnHGlobal( NULL, TRUE, &streamEnc));
  70. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  71. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->Init( streamEnc, bstrEncryptionKey ));
  72. // Use the STATSTG on the encrypted stream to get the size of the stream.
  73. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( streamPlain, stream));
  74. //Get HGlobal from EncryptedStream 'stream'.
  75. __MPC_EXIT_IF_METHOD_FAILS(hr, ::GetHGlobalFromStream( streamEnc, &hg ));
  76. // Get the size of the encrypted stream.
  77. __MPC_EXIT_IF_METHOD_FAILS(hr, streamEnc->Stat( &stg, STATFLAG_NONAME ));
  78. //
  79. // Sorry, we don't handle streams longer than 4GB!!
  80. //
  81. if(stg.cbSize.u.HighPart)
  82. {
  83. __MPC_SET_ERROR_AND_EXIT(hr, E_OUTOFMEMORY);
  84. }
  85. dwLen = stg.cbSize.u.LowPart;
  86. // ConvertHGlobaltoHex to finally get a string.
  87. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::ConvertHGlobalToHex( hg, bstrEncString, FALSE, &dwLen ));
  88. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::GetBSTR( bstrEncString, bstrEncryptedString));
  89. hr = S_OK;
  90. __MPC_FUNC_CLEANUP;
  91. __MPC_FUNC_EXIT(hr);
  92. }
  93. STDMETHODIMP CSAFEncrypt::DecryptString(BSTR bstrEncryptionKey, BSTR bstrInputString, BSTR *bstrDecryptedString)
  94. {
  95. __HCP_FUNC_ENTRY( "CSAFEncrypt::DecryptString" );
  96. HRESULT hr;
  97. CComPtr<MPC::EncryptedStream> stream;
  98. CComPtr<IStream> streamPlain;
  99. CComPtr<IStream> streamEncrypted;
  100. CComBSTR bstrDecryptString;
  101. HGLOBAL hg = NULL;
  102. LARGE_INTEGER liFilePos = { 0, 0 };
  103. __MPC_PARAMCHECK_BEGIN(hr)
  104. __MPC_PARAMCHECK_POINTER_AND_SET(bstrDecryptedString, NULL);
  105. __MPC_PARAMCHECK_END();
  106. // Convert Hex to HGlobal - i.e. Copy the encrypted string to global.
  107. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::ConvertHexToHGlobal( bstrInputString, hg ));
  108. // CreateStreamOnHGlobal - i.e. create a encrypted stream.
  109. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CreateStreamOnHGlobal( hg, FALSE, &streamEncrypted ));
  110. // You have the input as stream, now decrypt it .
  111. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  112. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->Init( streamEncrypted, bstrEncryptionKey ));
  113. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CreateStreamOnHGlobal( NULL, TRUE, &streamPlain ));
  114. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( stream, streamPlain ));
  115. // Rewind the Stream.
  116. __MPC_EXIT_IF_METHOD_FAILS(hr, streamPlain->Seek( liFilePos, STREAM_SEEK_SET, NULL ));
  117. // Now the decrypted plain stream is available. Get the string from it.
  118. {
  119. // Initialize the serializer with the plain stream.
  120. MPC::Serializer_IStream streamSerializerPlain( streamPlain );
  121. __MPC_EXIT_IF_METHOD_FAILS(hr, streamSerializerPlain >> bstrDecryptString);
  122. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::GetBSTR( bstrDecryptString, bstrDecryptedString));
  123. }
  124. hr = S_OK;
  125. __MPC_FUNC_CLEANUP;
  126. if(hg) ::GlobalFree( hg );
  127. __MPC_FUNC_EXIT(hr);
  128. }
  129. STDMETHODIMP CSAFEncrypt::EncryptFile(BSTR bstrEncryptionKey, BSTR bstrInputFile, BSTR bstrEncryptedFile)
  130. {
  131. __HCP_FUNC_ENTRY( "CSAFEncrypt::EncryptFile" );
  132. HRESULT hr;
  133. CComPtr<MPC::EncryptedStream> stream;
  134. CComPtr<IStream> streamPlain;
  135. CComPtr<IStream> streamEncrypted;
  136. MPC::wstring szTempFile;
  137. MPC::NocaseCompare cmpStrings;
  138. bool fTempFile = false;
  139. // Check to see if one of the input files is null. If it is fail!
  140. __MPC_PARAMCHECK_BEGIN(hr)
  141. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrInputFile);
  142. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrEncryptedFile);
  143. __MPC_PARAMCHECK_END();
  144. // Check to see if both files are same.
  145. if(cmpStrings(bstrInputFile, bstrEncryptedFile))
  146. {
  147. // Get temp Folder Location.
  148. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::GetTemporaryFileName( szTempFile ));
  149. // Copy the input file contents to the temporary file.
  150. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CopyFile(bstrInputFile, szTempFile.c_str()));
  151. // Copy the Temporary File Name over the Input File Name;
  152. bstrInputFile = (BSTR)szTempFile.c_str();
  153. fTempFile = true;
  154. }
  155. __MPC_EXIT_IF_METHOD_FAILS(hr, SVC::OpenStreamForRead( bstrInputFile , &streamPlain ));
  156. __MPC_EXIT_IF_METHOD_FAILS(hr, SVC::OpenStreamForWrite( bstrEncryptedFile, &streamEncrypted ));
  157. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  158. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->Init( streamEncrypted, bstrEncryptionKey ));
  159. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( streamPlain, stream ));
  160. hr = S_OK;
  161. __MPC_FUNC_CLEANUP;
  162. if(fTempFile) ::DeleteFileW( bstrInputFile );
  163. __MPC_FUNC_EXIT(hr);
  164. }
  165. STDMETHODIMP CSAFEncrypt::DecryptFile(BSTR bstrEncryptionKey, BSTR bstrInputFile, BSTR bstrDecryptedFile )
  166. {
  167. __HCP_FUNC_ENTRY( "CSAFEncrypt::DecryptFile" );
  168. HRESULT hr;
  169. CComPtr<MPC::EncryptedStream> stream;
  170. CComPtr<IStream> streamPlain;
  171. CComPtr<IStream> streamEncrypted;
  172. MPC::NocaseCompare cmpStrings;
  173. MPC::wstring szTempFile;
  174. bool fTempFile = false;
  175. __MPC_PARAMCHECK_BEGIN(hr)
  176. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrInputFile);
  177. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrDecryptedFile);
  178. __MPC_PARAMCHECK_END();
  179. // Check to see if both files are same.
  180. if(cmpStrings(bstrInputFile, bstrDecryptedFile))
  181. {
  182. // Get temp Folder Location.
  183. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::GetTemporaryFileName( szTempFile ));
  184. // Copy the input file contents to the temporary file.
  185. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CopyFile(bstrInputFile, szTempFile.c_str()));
  186. // Copy the Temporary File Name over the Input File Name;
  187. bstrInputFile = (BSTR)szTempFile.c_str();
  188. fTempFile = true;
  189. }
  190. __MPC_EXIT_IF_METHOD_FAILS(hr, SVC::OpenStreamForRead( bstrInputFile , &streamEncrypted ));
  191. __MPC_EXIT_IF_METHOD_FAILS(hr, SVC::OpenStreamForWrite( bstrDecryptedFile, &streamPlain ));
  192. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  193. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->Init( streamEncrypted, bstrEncryptionKey ));
  194. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( stream, streamPlain));
  195. hr = S_OK;
  196. __MPC_FUNC_CLEANUP;
  197. if(fTempFile) ::DeleteFileW( bstrInputFile );
  198. __MPC_FUNC_EXIT(hr);
  199. }
  200. STDMETHODIMP CSAFEncrypt::EncryptStream(BSTR bstrEncryptionKey, IUnknown *punkInStm, IUnknown **ppunkOutStm)
  201. {
  202. __HCP_FUNC_ENTRY( "CSAFEncrypt::EncryptStream" );
  203. HRESULT hr;
  204. CComPtr<MPC::EncryptedStream> stream;
  205. CComPtr<IStream> streamPlain;
  206. CComPtr<IStream> streamEncrypted;
  207. LARGE_INTEGER liFilePos = { 0, 0 };
  208. __MPC_PARAMCHECK_BEGIN(hr)
  209. __MPC_PARAMCHECK_NOTNULL(punkInStm);
  210. __MPC_PARAMCHECK_POINTER_AND_SET(ppunkOutStm, NULL);
  211. __MPC_PARAMCHECK_END();
  212. __MPC_EXIT_IF_METHOD_FAILS(hr, punkInStm->QueryInterface( IID_IStream, (void**)&streamPlain ));
  213. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CreateStreamOnHGlobal( NULL, TRUE, &streamEncrypted ));
  214. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  215. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->Init( streamEncrypted, bstrEncryptionKey ));
  216. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( streamPlain, stream ));
  217. // Rewind the Stream.
  218. __MPC_EXIT_IF_METHOD_FAILS(hr, streamEncrypted->Seek( liFilePos, STREAM_SEEK_SET, NULL ));
  219. __MPC_EXIT_IF_METHOD_FAILS(hr, streamEncrypted->QueryInterface( IID_IUnknown, (LPVOID *)ppunkOutStm ));
  220. hr = S_OK;
  221. __MPC_FUNC_CLEANUP;
  222. __MPC_FUNC_EXIT(hr);
  223. }
  224. STDMETHODIMP CSAFEncrypt::DecryptStream(BSTR bstrEncryptionKey, IUnknown *punkInStm, IUnknown **ppunkOutStm)
  225. {
  226. __HCP_FUNC_ENTRY( "CSAFEncrypt::DecryptStream" );
  227. HRESULT hr;
  228. CComPtr<MPC::EncryptedStream> stream;
  229. CComPtr<IStream> streamPlain;
  230. CComPtr<IStream> streamEncrypted;
  231. LARGE_INTEGER liFilePos = { 0, 0 };
  232. __MPC_PARAMCHECK_BEGIN(hr)
  233. __MPC_PARAMCHECK_NOTNULL(punkInStm);
  234. __MPC_PARAMCHECK_POINTER_AND_SET(ppunkOutStm, NULL);
  235. __MPC_PARAMCHECK_END();
  236. __MPC_EXIT_IF_METHOD_FAILS(hr, punkInStm->QueryInterface( IID_IStream, (void**)&streamEncrypted ));
  237. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CreateInstance( &stream ));
  238. __MPC_EXIT_IF_METHOD_FAILS(hr, stream->Init( streamEncrypted, bstrEncryptionKey ));
  239. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CreateStreamOnHGlobal( NULL, TRUE, &streamPlain ));
  240. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::BaseStream::TransferData( stream, streamPlain));
  241. // Rewind the Stream.
  242. __MPC_EXIT_IF_METHOD_FAILS(hr, streamPlain->Seek( liFilePos, STREAM_SEEK_SET, NULL ));
  243. __MPC_EXIT_IF_METHOD_FAILS(hr, streamPlain->QueryInterface( IID_IUnknown, (LPVOID *)ppunkOutStm ));
  244. hr = S_OK;
  245. __MPC_FUNC_CLEANUP;
  246. __MPC_FUNC_EXIT(hr);
  247. }