Advertisement
akass

Password

Jun 29th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1.     HRESULT ProtectIfNecessaryAndCopyPassword(
  2.             __in PCWSTR pwzPassword,
  3.             __in CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
  4.             __deref_out PWSTR* ppwzProtectedPassword
  5.             )
  6.         {
  7.             *ppwzProtectedPassword = NULL;
  8.  
  9.             HRESULT hr;
  10.  
  11.             // ProtectAndCopyString is intended for non-empty strings only.  Empty passwords
  12.             // do not need to be encrypted.
  13.             if (pwzPassword && *pwzPassword)
  14.             {
  15.                 // pwzPassword is const, but CredIsProtected takes a non-const string.
  16.                 // So, ake a copy that we know isn't const.
  17.                 PWSTR pwzPasswordCopy;
  18.                 hr = SHStrDupW(pwzPassword, &pwzPasswordCopy);
  19.                 if (SUCCEEDED(hr))
  20.                 {
  21.                     bool bCredAlreadyEncrypted = false;
  22.                     CRED_PROTECTION_TYPE protectionType;
  23.  
  24.                     // If the password is already encrypted, we should not encrypt it again.
  25.                     // An encrypted password may be received through SetSerialization in the
  26.                     // CPUS_LOGON scenario during a Terminal Services connection, for instance.
  27.                     if (CredIsProtectedW(pwzPasswordCopy, &protectionType))
  28.                     {
  29.                         if(CredUnprotected != protectionType)
  30.                         {
  31.                             bCredAlreadyEncrypted = true;
  32.                         }
  33.                     }
  34.  
  35.                     // Passwords should not be encrypted in the CPUS_CREDUI scenario.  We
  36.                     // cannot know if our caller expects or can handle an encryped password.
  37.                     if (CPUS_CREDUI == cpus || bCredAlreadyEncrypted)
  38.                     {
  39.                         hr = SHStrDupW(pwzPasswordCopy, ppwzProtectedPassword);
  40.                     }
  41.                     else
  42.                     {
  43.                         hr = _ProtectAndCopyString(pwzPasswordCopy, ppwzProtectedPassword);
  44.                     }
  45.            
  46.                     CoTaskMemFree(pwzPasswordCopy);
  47.                 }
  48.             }
  49.             else
  50.             {
  51.                 hr = SHStrDupW(L"", ppwzProtectedPassword);
  52.             }
  53.  
  54.             return hr;
  55.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement