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.

141 lines
2.5 KiB

  1. #include <wincrypt.h>
  2. #include <map>
  3. //
  4. // abstraction of CryptEncodeBlob/CryptDecodeBlob
  5. //
  6. class CEncryptedBlob
  7. {
  8. public:
  9. CEncryptedBlob(
  10. void * Buffer,
  11. size_t Length,
  12. LPCWSTR Description
  13. );
  14. CEncryptedBlob();
  15. //
  16. // used when unserializing
  17. //
  18. ~CEncryptedBlob();
  19. size_t GetLength()
  20. {
  21. return m_Length;
  22. }
  23. void
  24. Decrypt(
  25. void * Buffer,
  26. size_t Length
  27. );
  28. void Serialize( HANDLE hFile );
  29. void Unserialize( HANDLE hFile );
  30. protected:
  31. DATA_BLOB m_Blob;
  32. size_t m_Length;
  33. };
  34. class CEncryptedCredentials
  35. {
  36. public:
  37. CEncryptedCredentials( const BG_AUTH_CREDENTIALS & cred );
  38. ~CEncryptedCredentials();
  39. //
  40. // used by unserialize
  41. //
  42. CEncryptedCredentials()
  43. {
  44. m_Blob = 0;
  45. }
  46. BG_AUTH_CREDENTIALS * Decrypt();
  47. void Serialize( HANDLE hFile )
  48. {
  49. if (m_Blob)
  50. {
  51. SafeWriteFile( hFile, true );
  52. m_Blob->Serialize( hFile );
  53. }
  54. else
  55. {
  56. SafeWriteFile( hFile, false );
  57. }
  58. }
  59. void Unserialize( HANDLE hFile )
  60. {
  61. bool b;
  62. SafeReadFile( hFile, &b );
  63. if (b)
  64. {
  65. m_Blob = new CEncryptedBlob;
  66. m_Blob->Unserialize( hFile );
  67. }
  68. }
  69. protected:
  70. CEncryptedBlob * m_Blob;
  71. };
  72. //
  73. // a set of (encrypted) credentials
  74. //
  75. class CCredentialsContainer
  76. {
  77. typedef DWORD KEY;
  78. typedef std::map<KEY, CEncryptedCredentials *> Dictionary;
  79. public:
  80. typedef Dictionary::iterator Cookie;
  81. CCredentialsContainer();
  82. ~CCredentialsContainer();
  83. void Clear();
  84. HRESULT Update( const BG_AUTH_CREDENTIALS * Credentials );
  85. HRESULT Remove( BG_AUTH_TARGET Target, BG_AUTH_SCHEME Scheme );
  86. size_t GetSizeEstimate( const BG_AUTH_CREDENTIALS * Credentials ) const;
  87. HRESULT Find( BG_AUTH_TARGET Target, BG_AUTH_SCHEME Scheme, BG_AUTH_CREDENTIALS ** pCredentials ) const;
  88. BG_AUTH_CREDENTIALS * FindFirst( Cookie & cookie ) const throw( ComError );
  89. BG_AUTH_CREDENTIALS * FindNext( Cookie & cookie ) const throw( ComError );
  90. void Serialize( HANDLE hFile );
  91. void Unserialize( HANDLE hFile );
  92. protected:
  93. Dictionary m_Dictionary;
  94. //--------------------------------------------------------------------
  95. inline KEY MakeKey( BG_AUTH_TARGET Target, BG_AUTH_SCHEME Scheme ) const
  96. {
  97. return (WORD(Scheme) << 16) | WORD(Target);
  98. }
  99. };