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.

156 lines
4.2 KiB

  1. // KeyContext.cpp -- CKeyContext class definition
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1999. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #include "stdafx.h" // because handles.h uses the ASSERT macro
  8. #include <scuOsExc.h>
  9. #include <scuOsVersion.h>
  10. #include "KeyContext.h"
  11. using namespace std;
  12. using namespace scu;
  13. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  14. /////////////////////////// PUBLIC /////////////////////////////////
  15. // Types
  16. // C'tors/D'tors
  17. CKeyContext::~CKeyContext()
  18. {
  19. Close();
  20. }
  21. // Operators
  22. // Operations
  23. void
  24. CKeyContext::Close()
  25. {
  26. if (m_hKey)
  27. {
  28. CryptDestroyKey(m_hKey);
  29. m_hKey = NULL;
  30. }
  31. }
  32. void
  33. CKeyContext::Decrypt(HCRYPTHASH hAuxHash,
  34. BOOL fFinal,
  35. DWORD dwFlags,
  36. BYTE *pbData,
  37. DWORD *pdwDataLen)
  38. {
  39. ImportToAuxCSP();
  40. if (!CryptDecrypt(GetKey(), hAuxHash, fFinal, dwFlags,
  41. pbData, pdwDataLen))
  42. throw scu::OsException(GetLastError());
  43. }
  44. void
  45. CKeyContext::Encrypt(HCRYPTHASH hAuxHash,
  46. BOOL fFinal,
  47. DWORD dwFlags,
  48. BYTE *pbData,
  49. DWORD *pdwDataLen,
  50. DWORD dwBufLen)
  51. {
  52. ImportToAuxCSP();
  53. if (!CryptEncrypt(GetKey(), hAuxHash, fFinal, dwFlags, pbData,
  54. pdwDataLen, dwBufLen))
  55. throw scu::OsException(GetLastError());
  56. }
  57. // Access
  58. HCRYPTKEY
  59. CKeyContext::GetKey() const
  60. {
  61. return m_hKey;
  62. }
  63. HCRYPTKEY
  64. CKeyContext::KeyHandleInAuxCSP()
  65. {
  66. ImportToAuxCSP();
  67. return m_hKey;
  68. }
  69. DWORD
  70. CKeyContext::TypeOfKey() const
  71. {
  72. return m_dwTypeOfKey;
  73. }
  74. // Predicates
  75. // Static Variables
  76. /////////////////////////// PROTECTED /////////////////////////////////
  77. // C'tors/D'tors
  78. CKeyContext::CKeyContext(HCRYPTPROV hProv,
  79. DWORD dwTypeOfKey)
  80. : CHandle(),
  81. m_hKey(NULL),
  82. m_apabKey(),
  83. m_dwTypeOfKey(dwTypeOfKey),
  84. m_hAuxProvider(hProv)
  85. {}
  86. // Duplicate the key and its state
  87. CKeyContext::CKeyContext(CKeyContext const &rhs,
  88. DWORD const *pdwReserved,
  89. DWORD dwFlags)
  90. : CHandle(),
  91. m_hKey(rhs.m_hKey),
  92. m_apabKey(auto_ptr<AlignedBlob>(new AlignedBlob(*rhs.m_apabKey))),
  93. m_dwTypeOfKey(rhs.m_dwTypeOfKey),
  94. m_hAuxProvider(rhs.m_hAuxProvider)
  95. {
  96. #if defined(SLB_WIN2K_BUILD)
  97. if (!CryptDuplicateKey(KeyHandleInAuxCSP(),
  98. const_cast<DWORD *>(pdwReserved),
  99. dwFlags,
  100. &m_hKey))
  101. throw scu::OsException(GetLastError());
  102. #else
  103. throw scu::OsException(ERROR_NOT_SUPPORTED);
  104. #endif
  105. }
  106. // Operators
  107. // Operations
  108. // Access
  109. HCRYPTPROV
  110. CKeyContext::AuxProvider() const
  111. {
  112. return m_hAuxProvider;
  113. }
  114. // Predicates
  115. // Static Variables
  116. /////////////////////////// PRIVATE /////////////////////////////////
  117. // C'tors/D'tors
  118. // Operators
  119. // Operations
  120. // Access
  121. // Predicates
  122. // Static Variables