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.

302 lines
7.4 KiB

  1. // HashCtx.cpp -- definition of CHashContext
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1998. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #include "stdafx.h" // because handles.h uses the ASSERT macro
  8. #include <memory> // for auto_ptr
  9. #include <scuOsVersion.h>
  10. #include <scuArrayP.h>
  11. #include "HashCtx.h"
  12. #include "HashMD2.h"
  13. #include "HashMD4.h"
  14. #include "HashMD5.h"
  15. #include "HashSHA1.h"
  16. #include "HashSHAMD5.h"
  17. using namespace std;
  18. using namespace scu;
  19. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  20. /////////////////////////// PUBLIC /////////////////////////////////
  21. // Types
  22. // C'tors/D'tors
  23. CHashContext::~CHashContext() throw()
  24. {
  25. try
  26. {
  27. Close();
  28. }
  29. catch (...)
  30. {
  31. }
  32. }
  33. // Operators
  34. // Operations
  35. void
  36. CHashContext::Close()
  37. {
  38. if (m_HashHandle)
  39. {
  40. if (!CryptDestroyHash(m_HashHandle))
  41. throw scu::OsException(GetLastError());
  42. m_HashHandle = NULL;
  43. }
  44. }
  45. void
  46. CHashContext::ExportFromAuxCSP()
  47. {
  48. if (!m_HashHandle)
  49. throw scu::OsException(ERROR_INVALID_PARAMETER);
  50. if (m_fJustCreated)
  51. {
  52. DWORD dwNeedLen;
  53. DWORD dwDataLen = sizeof DWORD;
  54. if (!CryptGetHashParam(m_HashHandle, HP_HASHSIZE,
  55. reinterpret_cast<BYTE *>(&dwNeedLen),
  56. &dwDataLen, 0))
  57. throw scu::OsException(GetLastError());
  58. AutoArrayPtr<BYTE> apbHashValue(new BYTE[dwNeedLen]);
  59. if (!CryptGetHashParam(m_HashHandle, HP_HASHVAL,
  60. apbHashValue.Get(), &dwNeedLen, 0))
  61. throw scu::OsException(GetLastError());
  62. Value(Blob(apbHashValue.Get(), dwNeedLen));
  63. }
  64. }
  65. void CHashContext::Hash(BYTE const *pbData,
  66. DWORD dwLength)
  67. {
  68. HCRYPTHASH hch = HashHandleInAuxCSP();
  69. if (!CryptHashData(hch, pbData, dwLength, 0))
  70. throw scu::OsException(GetLastError());
  71. m_fJustCreated = false;
  72. }
  73. void
  74. CHashContext::ImportToAuxCSP()
  75. {
  76. if (!m_HashHandle)
  77. {
  78. if (!CryptCreateHash(m_rcryptctx.AuxContext(),
  79. m_algid, 0, 0, &m_HashHandle))
  80. throw scu::OsException(GetLastError());
  81. }
  82. if (!m_fJustCreated && !m_fDone)
  83. {
  84. if (!CryptSetHashParam(m_HashHandle, HP_HASHVAL,
  85. const_cast<Blob::value_type *>(Value().data()),
  86. 0))
  87. throw scu::OsException(GetLastError());
  88. }
  89. }
  90. void
  91. CHashContext::Initialize()
  92. {
  93. m_fDone = false;
  94. }
  95. auto_ptr<CHashContext>
  96. CHashContext::Make(ALG_ID algid,
  97. CryptContext const &rcryptctx)
  98. {
  99. auto_ptr<CHashContext> apHash;
  100. switch (algid)
  101. {
  102. case CALG_MD2:
  103. apHash = auto_ptr<CHashContext>(new CHashMD2(rcryptctx));
  104. break;
  105. case CALG_MD4:
  106. apHash = auto_ptr<CHashContext>(new CHashMD4(rcryptctx));
  107. break;
  108. case CALG_MD5:
  109. apHash = auto_ptr<CHashContext>(new CHashMD5(rcryptctx));
  110. break;
  111. case CALG_SHA:
  112. apHash = auto_ptr<CHashContext>(new CHashSHA1(rcryptctx));
  113. break;
  114. case CALG_SSL3_SHAMD5:
  115. apHash = auto_ptr<CHashContext>(new CHashSHAMD5(rcryptctx));
  116. break;
  117. default:
  118. throw scu::OsException(NTE_BAD_ALGID);
  119. break;
  120. }
  121. return apHash;
  122. }
  123. void
  124. CHashContext::Value(Blob const &rhs)
  125. {
  126. if (!m_fJustCreated)
  127. throw scu::OsException(NTE_PERM);
  128. m_blbValue = rhs;
  129. m_fJustCreated = false;
  130. m_fDone = true;
  131. }
  132. // Access
  133. ALG_ID
  134. CHashContext::AlgId()
  135. {
  136. return m_algid;
  137. }
  138. Blob
  139. CHashContext::EncodedValue()
  140. {
  141. return EncodedAlgorithmOid() + Value();
  142. }
  143. HCRYPTHASH
  144. CHashContext::HashHandleInAuxCSP()
  145. {
  146. ImportToAuxCSP();
  147. return m_HashHandle;
  148. }
  149. CHashContext::SizeType
  150. CHashContext::Length() const
  151. {
  152. SizeType cHashLength;
  153. if (!m_fDone)
  154. {
  155. DWORD dwData;
  156. DWORD dwDataLength = sizeof dwData;
  157. if (!CryptGetHashParam(m_HashHandle, HP_HASHSIZE,
  158. reinterpret_cast<BYTE *>(&dwData),
  159. &dwDataLength, 0))
  160. throw scu::OsException(GetLastError());
  161. cHashLength = dwData;
  162. }
  163. else
  164. cHashLength = m_blbValue.length();
  165. return cHashLength;
  166. }
  167. Blob
  168. CHashContext::Value()
  169. {
  170. if (!m_fDone)
  171. ExportFromAuxCSP();
  172. return m_blbValue;
  173. }
  174. // Predicates
  175. bool
  176. CHashContext::IsSupported(ALG_ID algid)
  177. {
  178. bool IsSupported = true;
  179. switch (algid)
  180. {
  181. case CALG_MD2:
  182. case CALG_MD4:
  183. case CALG_MD5:
  184. case CALG_SHA:
  185. case CALG_SSL3_SHAMD5:
  186. break;
  187. default:
  188. IsSupported = false;
  189. break;
  190. }
  191. return IsSupported;
  192. }
  193. // Static Variables
  194. /////////////////////////// PROTECTED /////////////////////////////////
  195. // C'tors/D'tors
  196. CHashContext::CHashContext(CryptContext const &rcryptctx,
  197. ALG_ID algid)
  198. : CHandle(),
  199. m_rcryptctx(rcryptctx),
  200. m_algid(algid),
  201. m_blbValue(),
  202. m_fDone(false),
  203. m_fJustCreated(true),
  204. m_HashHandle(NULL)
  205. {}
  206. // Duplicate the hash and its state
  207. CHashContext::CHashContext(CHashContext const &rhs,
  208. DWORD const *pdwReserved,
  209. DWORD dwFlags)
  210. : CHandle(),
  211. m_rcryptctx(rhs.m_rcryptctx),
  212. m_algid(rhs.m_algid),
  213. m_blbValue(rhs.m_blbValue),
  214. m_fDone(rhs.m_fDone),
  215. m_fJustCreated(rhs.m_fJustCreated)
  216. {
  217. #if defined(SLB_WIN2K_BUILD)
  218. if (!CryptDuplicateHash(HashHandleInAuxCSP(),
  219. const_cast<DWORD *>(pdwReserved),
  220. dwFlags,
  221. &m_HashHandle))
  222. throw scu::OsException(GetLastError());
  223. #else
  224. throw scu::OsException(ERROR_NOT_SUPPORTED);
  225. #endif
  226. }
  227. // Operators
  228. // Operations
  229. // Access
  230. // Predicates
  231. // Static Variables
  232. /////////////////////////// PRIVATE /////////////////////////////////
  233. // C'tors/D'tors
  234. // Operators
  235. // Operations
  236. // Access
  237. // Predicates
  238. // Static Variables