Source code of Windows XP (NT5)
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.

116 lines
2.8 KiB

  1. // HASH.CPP
  2. //
  3. // Hash utility functions for use in NetMeeting components.
  4. #include "precomp.h"
  5. #include "nb30.h"
  6. #include <regentry.h>
  7. #include <confreg.h>
  8. #include <strutil.h>
  9. CHash::CHash() : m_hProv(0), m_hHash(0), m_pbHashedData(NULL), m_cbHashedData(0), m_fReady(FALSE)
  10. {
  11. if (!CryptAcquireContext(&m_hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET))
  12. {
  13. // Create new if can not get default
  14. if (!CryptAcquireContext(&m_hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET))
  15. {
  16. goto ErrorExit;
  17. }
  18. }
  19. m_fReady = TRUE;
  20. return;
  21. ErrorExit:
  22. m_fReady = FALSE;
  23. }
  24. CHash::~CHash()
  25. {
  26. if (m_pbHashedData) delete []m_pbHashedData;
  27. CryptAcquireContext(&m_hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET | CRYPT_MACHINE_KEYSET);
  28. if (m_hHash) CryptDestroyHash(m_hHash);
  29. if (m_hProv) CryptReleaseContext(m_hProv, 0);
  30. }
  31. DWORD CHash::GetHashedData(PBYTE pbData, DWORD cbData, void ** ppvHashedData)
  32. {
  33. ASSERT(NULL != ppvHashedData);
  34. ASSERT(NULL != pbData);
  35. DWORD dwCount;
  36. RegEntry re(WINDOWS_KEY, HKEY_LOCAL_MACHINE);
  37. if (FALSE == m_fReady)
  38. {
  39. goto ErrorExit;
  40. }
  41. if (0 == cbData) {
  42. goto ErrorExit;
  43. }
  44. if (m_hHash) CryptDestroyHash(m_hHash);
  45. if (!CryptCreateHash(m_hProv, CALG_MD5, 0, 0, &m_hHash))
  46. {
  47. ERROR_OUT(("CHash::GetHashData() - Error creating crypt hash object."));
  48. goto ErrorExit;
  49. }
  50. if (!CryptHashData(m_hHash, pbData, cbData, 0))
  51. {
  52. ERROR_OUT(("CHash::GetHashData() - Error hashing data."));
  53. goto ErrorExit;
  54. }
  55. if (!CryptHashData(m_hHash, (PBYTE) re.GetString(REGVAL_REGISTERED_USER),
  56. lstrlen(re.GetString(REGVAL_REGISTERED_USER)), 0))
  57. {
  58. ERROR_OUT(("CHash::GetHashData() - Error hashing extra data."));
  59. goto ErrorExit;
  60. }
  61. NCB ncb;
  62. BYTE buf[sizeof(NCB) + 256];
  63. ZeroMemory ( &ncb, sizeof(ncb));
  64. ncb.ncb_command = NCBASTAT;
  65. ncb.ncb_buffer = buf;
  66. ncb.ncb_length = sizeof(buf);
  67. memcpy ( (PBYTE)ncb.ncb_callname, (PBYTE)"* ", NCBNAMSZ );
  68. if ( NRC_GOODRET == Netbios(&ncb) )
  69. {
  70. //
  71. // NOTE: the buffer filled by the Netbios ASTAT command starts
  72. // with a 6-byte encoded adapter address: just use the raw
  73. // buffer rather than casting back to PBYTE
  74. //
  75. if (!CryptHashData(m_hHash, buf, 6, 0 ))
  76. {
  77. ERROR_OUT(("CHash::GetHashData() - Error hashing ncb data."));
  78. goto ErrorExit;
  79. }
  80. }
  81. else
  82. {
  83. WARNING_OUT(("CHash::GetHashData: Netbios failed %x", ncb.ncb_retcode));
  84. }
  85. dwCount = sizeof(DWORD);
  86. if (!CryptGetHashParam(m_hHash, HP_HASHSIZE, (BYTE *)&m_cbHashedData, &dwCount, 0))
  87. {
  88. goto ErrorExit;
  89. }
  90. if (m_pbHashedData) delete [] m_pbHashedData;
  91. if (NULL == (m_pbHashedData = new BYTE[m_cbHashedData]))
  92. {
  93. goto ErrorExit;
  94. }
  95. if (!CryptGetHashParam(m_hHash, HP_HASHVAL, m_pbHashedData, &m_cbHashedData, 0)) {
  96. goto ErrorExit;
  97. }
  98. *ppvHashedData = m_pbHashedData;
  99. return m_cbHashedData;
  100. ErrorExit:
  101. return 0; // Hash data failed
  102. }