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.

261 lines
5.6 KiB

  1. #ifndef _CERTSTORE_HXX_
  2. #define _CERTSTORE_HXX_
  3. /*++
  4. Copyright (c) 1998 Microsoft Corporation
  5. Module Name :
  6. certstore.cxx
  7. Abstract:
  8. Wrapper of a certificate store
  9. Author:
  10. Bilal Alam (BAlam) 29-March-2000
  11. Environment:
  12. Win32 - User Mode
  13. Project:
  14. Stream Filter Worker Process
  15. --*/
  16. #define CERT_STORE_SIGNATURE (DWORD)'TSRC'
  17. #define CERT_STORE_SIGNATURE_FREE (DWORD)'tsrc'
  18. class CERT_STORE_HASH;
  19. class CERT_STORE
  20. {
  21. public:
  22. CERT_STORE();
  23. VOID
  24. ReferenceStore(
  25. VOID
  26. )
  27. {
  28. InterlockedIncrement( &_cRefs );
  29. }
  30. VOID
  31. DereferenceStore(
  32. VOID
  33. )
  34. {
  35. if ( !InterlockedDecrement( &_cRefs ) )
  36. {
  37. //
  38. // Deletion of the CERT_STORE object cannot happen
  39. // on the callers thead because of the implementation
  40. // limitations. Each instance of CERT_STORE handles
  41. // it's own CAPI store change notifications and there is
  42. // a problem of deadlock waiting for the change notification
  43. // thread to complete in the destructor
  44. //
  45. EnterCriticalSection( &sm_csToBeDeletedList );
  46. InsertHeadList( &sm_ToBeDeletedListHead,
  47. &_ToBeDeletedListEntry );
  48. LeaveCriticalSection( &sm_csToBeDeletedList );
  49. //
  50. // signal the deletion thread that there is a work to be done
  51. //
  52. SetEvent( sm_hWakeupEvent );
  53. }
  54. }
  55. BOOL
  56. CheckSignature(
  57. VOID
  58. ) const
  59. {
  60. return _dwSignature == CERT_STORE_SIGNATURE;
  61. }
  62. HCERTSTORE
  63. QueryStore(
  64. VOID
  65. ) const
  66. {
  67. return _hStore;
  68. }
  69. WCHAR *
  70. QueryStoreName(
  71. VOID
  72. ) const
  73. {
  74. return (WCHAR*) _strStoreName.QueryStr();
  75. }
  76. static
  77. HRESULT
  78. Initialize(
  79. VOID
  80. );
  81. static
  82. VOID
  83. Terminate(
  84. VOID
  85. );
  86. static
  87. VOID
  88. WaitForAllWorkerThreadsCompletion(
  89. VOID
  90. );
  91. static
  92. DWORD
  93. WINAPI
  94. DeletionWorkerThread(
  95. VOID * pvContext
  96. );
  97. static
  98. VOID
  99. DeleteAllPendingInstances(
  100. VOID
  101. );
  102. static
  103. HRESULT
  104. OpenStore(
  105. STRU & strStoreName,
  106. CERT_STORE ** ppCertStore
  107. );
  108. static
  109. VOID
  110. WINAPI
  111. CertStoreChangeRoutine(
  112. VOID * pvContext,
  113. BOOLEAN fTimedOut
  114. );
  115. static
  116. VOID
  117. Cleanup(
  118. VOID
  119. );
  120. private:
  121. virtual ~CERT_STORE();
  122. // private methods
  123. //
  124. HRESULT
  125. Open(
  126. STRU & strStoreName
  127. );
  128. private:
  129. DWORD _dwSignature;
  130. LONG _cRefs;
  131. LIST_ENTRY _ToBeDeletedListEntry;
  132. HCERTSTORE _hStore;
  133. STRU _strStoreName;
  134. // event signalled by CAPI if certificate store has changed
  135. HANDLE _hStoreChangeEvent;
  136. // handle for RegisterWaitForSingleObject
  137. HANDLE _hWaitHandle;
  138. // list of CERT_STORE entries to be deleted
  139. static LIST_ENTRY sm_ToBeDeletedListHead;
  140. // CS to maintain the list above
  141. static CRITICAL_SECTION sm_csToBeDeletedList;
  142. // flag that CS above was initialized
  143. static BOOL sm_fInitcsToBeDeletedList;
  144. // Hash table of CERT_STORE instances
  145. static CERT_STORE_HASH* sm_pCertStoreHash;
  146. // handle to the thread that takes care of the deletion
  147. // of CERT_STORE instances that are on the ToBeDeleted List
  148. static HANDLE sm_hDeletionThread;
  149. // indication for the "deletion thread" that it is
  150. // time to shutdown
  151. static BOOL sm_fDeletionThreadShutdown;
  152. // event to signal to deletion thread that there is a work to be done
  153. // of that there is a time for shutdown
  154. static HANDLE sm_hWakeupEvent;
  155. };
  156. class CERT_STORE_HASH
  157. : public CTypedHashTable<
  158. CERT_STORE_HASH,
  159. CERT_STORE,
  160. WCHAR *
  161. >
  162. {
  163. public:
  164. CERT_STORE_HASH()
  165. : CTypedHashTable< CERT_STORE_HASH,
  166. CERT_STORE,
  167. WCHAR * > ( "CERT_STORE_HASH" )
  168. {
  169. }
  170. static
  171. WCHAR *
  172. ExtractKey(
  173. const CERT_STORE * pCertStore
  174. )
  175. {
  176. return pCertStore->QueryStoreName();
  177. }
  178. static
  179. DWORD
  180. CalcKeyHash(
  181. WCHAR * pszStoreName
  182. )
  183. {
  184. return Hash( pszStoreName );
  185. }
  186. static
  187. bool
  188. EqualKeys(
  189. WCHAR * pszStore1,
  190. WCHAR * pszStore2
  191. )
  192. {
  193. return wcscmp( pszStore1, pszStore2 ) == 0;
  194. }
  195. static
  196. void
  197. AddRefRecord(
  198. CERT_STORE * pCertStore,
  199. int nIncr
  200. )
  201. {
  202. DBG_ASSERT( pCertStore != NULL );
  203. if ( nIncr == +1 )
  204. {
  205. pCertStore->ReferenceStore();
  206. }
  207. else if ( nIncr == -1 )
  208. {
  209. pCertStore->DereferenceStore();
  210. }
  211. }
  212. private:
  213. //
  214. // Not implemented methods
  215. // Declarations present to prevent compiler
  216. // to generate default ones.
  217. //
  218. CERT_STORE_HASH( const CERT_STORE_HASH& );
  219. CERT_STORE_HASH& operator=( const CERT_STORE_HASH& );
  220. };
  221. #endif