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.

110 lines
2.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. DWORD dwError = GetLastError();
  18. HRESULT hr = (dwError != ERROR_SUCCESS) ? HRESULT_FROM_WIN32(dwError) : E_INVALIDARG;
  19. ThrowError(hr, IDS_E_INVALID_FOLDER, pszFolder);
  20. }
  21. // path must be terminated with path separator otherwise
  22. // _tsplitpath will treat last path component as file name
  23. if (szPath[cchPath - 1] != _T('\\'))
  24. {
  25. _tcscat(szPath, _T("\\"));
  26. }
  27. _TCHAR szDrive[_MAX_DRIVE];
  28. _TCHAR szDir[_MAX_DIR];
  29. _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
  30. // verify drive is a local drive
  31. _TCHAR szTestDrive[_MAX_PATH];
  32. _tmakepath(szTestDrive, szDrive, _T("\\"), NULL, NULL);
  33. if (GetDriveType(szTestDrive) == DRIVE_REMOTE)
  34. {
  35. ThrowError(E_INVALIDARG, IDS_E_NOT_LOCAL_DRIVE, pszFolder);
  36. }
  37. // generate random name
  38. static _TCHAR s_chName[] = _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
  39. BYTE bRandom[8];
  40. try
  41. {
  42. CCryptProvider crypt;
  43. crypt.GenerateRandom(bRandom, 8);
  44. }
  45. catch (_com_error& ce)
  46. {
  47. //
  48. // The message 'keyset not defined' is returned when
  49. // the enhanced provider (128 bit) is not available
  50. // therefore return a more meaningful message to user.
  51. //
  52. if (ce.Error() == NTE_KEYSET_NOT_DEF)
  53. {
  54. ThrowError(ce, IDS_E_HIGH_ENCRYPTION_NOT_INSTALLED);
  55. }
  56. else
  57. {
  58. throw;
  59. }
  60. }
  61. _TCHAR szName[9];
  62. for (int i = 0; i < 8; i++)
  63. {
  64. szName[i] = s_chName[bRandom[i] % (countof(s_chName) - 1)];
  65. }
  66. szName[8] = _T('\0');
  67. // generate path to key file
  68. _TCHAR szKeyFile[_MAX_PATH];
  69. _tmakepath(szKeyFile, szDrive, szDir, szName, _T(".pes"));
  70. // generate key
  71. IPasswordMigrationPtr spPasswordMigration(__uuidof(PasswordMigration));
  72. spPasswordMigration->GenerateKey(pszDomainName, szKeyFile, pszPassword);
  73. // print success message to console
  74. _TCHAR szFormat[256];
  75. if (LoadString(GetModuleHandle(NULL), IDS_MSG_KEY_CREATED, szFormat, countof(szFormat)) > 0)
  76. {
  77. My_fwprintf(szFormat, pszDomainName, szKeyFile);
  78. }
  79. }