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.

142 lines
3.0 KiB

  1. // Copyright (c) 2000 Microsoft Corporation
  2. //
  3. // Wrappers of wincrui.h APIs
  4. //
  5. // 19 July 2000 sburns
  6. #include "headers.hxx"
  7. #include "CredentialUiHelpers.hpp"
  8. String
  9. CredUi::GetUsername(HWND credControl)
  10. {
  11. // LOG_FUNCTION(CredUi::GetUsername);
  12. ASSERT(Win::IsWindow(credControl));
  13. String result;
  14. LONG length = Credential_GetUserNameLength(credControl);
  15. // Length may be -1 if the control is not ready to supply the username.
  16. // This can happen with smartcards due to the asynchonous event nature
  17. // of the smartcard system.
  18. //
  19. // N.B.: if length == -1, then Credential_GetUserName may return FALSE.
  20. if (length > 0)
  21. {
  22. result.resize(length + 1, 0);
  23. BOOL succeeded =
  24. Credential_GetUserName(credControl,
  25. const_cast<WCHAR*>(result.c_str()),
  26. length);
  27. ASSERT(succeeded);
  28. if (succeeded)
  29. {
  30. // ISSUE-2002/02/25-sburns could probably remove this call to
  31. // wcslen and replace with length
  32. result.resize(wcslen(result.c_str()));
  33. }
  34. else
  35. {
  36. result.erase();
  37. }
  38. }
  39. // LOG(result);
  40. return result;
  41. }
  42. EncryptedString
  43. CredUi::GetPassword(HWND credControl)
  44. {
  45. LOG_FUNCTION(CredUi::GetPassword);
  46. ASSERT(Win::IsWindow(credControl));
  47. EncryptedString result;
  48. // add 1 for super-paranoid null terminator.
  49. size_t length = Credential_GetPasswordLength(credControl) + 1;
  50. if (length)
  51. {
  52. WCHAR* cleartext = new WCHAR[length];
  53. // REVIEWED-2002/02/25-sburns byte count correctly passed.
  54. ::ZeroMemory(cleartext, sizeof WCHAR * length);
  55. BOOL succeeded =
  56. Credential_GetPassword(
  57. credControl,
  58. cleartext,
  59. length - 1);
  60. ASSERT(succeeded);
  61. result.Encrypt(cleartext);
  62. // make sure we scribble out the cleartext.
  63. // REVIEWED-2002/02/25-sburns byte count correctly passed.
  64. ::SecureZeroMemory(cleartext, sizeof WCHAR * length);
  65. delete[] cleartext;
  66. }
  67. // don't log the password...
  68. return result;
  69. }
  70. HRESULT
  71. CredUi::SetUsername(HWND credControl, const String& username)
  72. {
  73. LOG_FUNCTION(CredUi::SetUsername);
  74. ASSERT(Win::IsWindow(credControl));
  75. HRESULT hr = S_OK;
  76. // username may be empty
  77. BOOL succeeded = Credential_SetUserName(credControl, username.c_str());
  78. ASSERT(succeeded);
  79. // BUGBUG what if it failed? Is GetLastError valid?
  80. return hr;
  81. }
  82. HRESULT
  83. CredUi::SetPassword(HWND credControl, const EncryptedString& password)
  84. {
  85. LOG_FUNCTION(CredUi::SetPassword);
  86. ASSERT(Win::IsWindow(credControl));
  87. HRESULT hr = S_OK;
  88. // password may be empty
  89. WCHAR* cleartext = password.GetClearTextCopy();
  90. BOOL succeeded = Credential_SetPassword(credControl, cleartext);
  91. ASSERT(succeeded);
  92. password.DestroyClearTextCopy(cleartext);
  93. // BUGBUG what if it failed? Is GetLastError valid?
  94. return hr;
  95. }