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.

95 lines
2.7 KiB

  1. #ifndef __POP3_AUTH_MD5_UTIL_H__
  2. #define __POP3_AUTH_MD5_UTIL_H__
  3. #define UnicodeToAnsi(A, cA, U, cU) WideCharToMultiByte(CP_ACP,0,(U),(cU),(A),(cA),NULL,NULL)
  4. #define AnsiToUnicode(A, cA, U, cU) MultiByteToWideChar(CP_ACP,0,(A),(cA),(U),(cU))
  5. #include <mailbox.h>
  6. #include <Pop3RegKeys.h>
  7. #include <WinCrypt.h>
  8. HRESULT GetMD5Password(BSTR bstrUserName, char szPassword[MAX_PATH])
  9. {
  10. if(NULL == bstrUserName)
  11. {
  12. return E_POINTER;
  13. }
  14. WCHAR wszAuthGuid[MAX_PATH];
  15. BYTE szEncryptedPswd[MAX_PATH];
  16. DWORD dwEncryptedPswd;
  17. DWORD dwAuthDataLen=MAX_PATH;
  18. DWORD dwCryptDataLen;
  19. HRESULT hr = E_FAIL;
  20. CMailBox mailboxX;
  21. HCRYPTPROV hProv=NULL;
  22. HCRYPTHASH hHash=NULL;
  23. HCRYPTKEY hKey=NULL;
  24. if ( mailboxX.OpenMailBox( bstrUserName ) )
  25. {
  26. if ( mailboxX.LockMailBox())
  27. {
  28. if ( mailboxX.GetEncyptedPassword( szEncryptedPswd, MAX_PATH, &dwEncryptedPswd ))
  29. {
  30. if(ERROR_SUCCESS == RegQueryAuthGuid(wszAuthGuid, &(dwAuthDataLen)) )
  31. {
  32. if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
  33. {
  34. goto EXIT;
  35. }
  36. if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
  37. {
  38. goto EXIT;
  39. }
  40. if(!CryptHashData(hHash, (LPBYTE)wszAuthGuid, dwAuthDataLen, 0))
  41. {
  42. goto EXIT;
  43. }
  44. if(!CryptDeriveKey(hProv, CALG_RC4, hHash, (128<<16),&hKey))
  45. {
  46. goto EXIT;
  47. }
  48. dwCryptDataLen=dwEncryptedPswd;
  49. if(CryptDecrypt(hKey, NULL, TRUE, 0, szEncryptedPswd, &dwCryptDataLen))
  50. {
  51. if(dwCryptDataLen < MAX_PATH -1)
  52. {
  53. UnicodeToAnsi(szPassword, dwCryptDataLen, (LPCWSTR)szEncryptedPswd, -1);
  54. szPassword[dwCryptDataLen]=0;
  55. hr=S_OK;
  56. }
  57. }
  58. }
  59. }
  60. mailboxX.UnlockMailBox();
  61. }
  62. }
  63. else if( GetLastError()==ERROR_ACCESS_DENIED)
  64. {
  65. hr=E_ACCESSDENIED;
  66. }
  67. EXIT:
  68. if(hKey)
  69. {
  70. CryptDestroyKey(hKey);
  71. }
  72. if(hHash)
  73. {
  74. CryptDestroyHash(hHash);
  75. }
  76. if(hProv)
  77. {
  78. CryptReleaseContext(hProv, 0);
  79. }
  80. return hr;
  81. }
  82. #endif //__POP3_AUTH_MD5_UTIL_H__