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.

77 lines
1.7 KiB

  1. #include "StdAfx.h"
  2. #include "GenerateKey.h"
  3. #include <AdmtCrypt.h>
  4. void __stdcall GeneratePasswordKey(LPCTSTR pszDomainName, LPCTSTR pszPassword, LPCTSTR pszFolder)
  5. {
  6. // validate parameters
  7. if ((pszFolder == NULL) || (pszFolder[0] == NULL))
  8. {
  9. ThrowError(E_INVALIDARG);
  10. }
  11. // generate full path to folder
  12. _TCHAR szPath[_MAX_PATH];
  13. LPTSTR pszFilePart;
  14. DWORD cchPath = GetFullPathName(pszFolder, _MAX_PATH, szPath, &pszFilePart);
  15. if ((cchPath == 0) || (cchPath >= _MAX_PATH))
  16. {
  17. ThrowError(HRESULT_FROM_WIN32(GetLastError()), IDS_E_INVALID_FOLDER, pszFolder);
  18. }
  19. // path must be terminated with path separator otherwise
  20. // _tsplitpath will treat last path component as file name
  21. if (szPath[cchPath - 1] != _T('\\'))
  22. {
  23. _tcscat(szPath, _T("\\"));
  24. }
  25. _TCHAR szDrive[_MAX_DRIVE];
  26. _TCHAR szDir[_MAX_DIR];
  27. _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
  28. // verify drive is a local drive
  29. _TCHAR szTestDrive[_MAX_PATH];
  30. _tmakepath(szTestDrive, szDrive, _T("\\"), NULL, NULL);
  31. if (GetDriveType(szTestDrive) == DRIVE_REMOTE)
  32. {
  33. ThrowError(E_INVALIDARG, IDS_E_NOT_LOCAL_DRIVE, pszFolder);
  34. }
  35. // generate random name
  36. static _TCHAR s_chName[] = _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
  37. BYTE bRandom[8];
  38. CCryptProvider crypt;
  39. crypt.GenerateRandom(bRandom, 8);
  40. _TCHAR szName[9];
  41. for (int i = 0; i < 8; i++)
  42. {
  43. szName[i] = s_chName[bRandom[i] % (countof(s_chName) - 1)];
  44. }
  45. szName[8] = _T('\0');
  46. // generate path to key file
  47. _TCHAR szKeyFile[_MAX_PATH];
  48. _tmakepath(szKeyFile, szDrive, szDir, szName, _T(".pes"));
  49. // generate key
  50. IPasswordMigrationPtr spPasswordMigration(__uuidof(PasswordMigration));
  51. spPasswordMigration->GenerateKey(pszDomainName, szKeyFile, pszPassword);
  52. }