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.

340 lines
6.8 KiB

  1. #ifndef _SERVERCERT_HXX_
  2. #define _SERVERCERT_HXX_
  3. /*++
  4. Copyright (c) 1998 Microsoft Corporation
  5. Module Name :
  6. servercert.hxx
  7. Abstract:
  8. Server Certificate wrapper
  9. Author:
  10. Bilal Alam (BAlam) 29-March-2000
  11. Environment:
  12. Win32 - User Mode
  13. Project:
  14. Stream Filter Worker Process
  15. --*/
  16. class CREDENTIAL_ID
  17. {
  18. public:
  19. CREDENTIAL_ID()
  20. {
  21. _cbCredentialId = 0;
  22. }
  23. HRESULT
  24. Append(
  25. BYTE * pbHash,
  26. DWORD cbHash
  27. )
  28. {
  29. if ( !_bufId.Resize( _cbCredentialId + cbHash ) )
  30. {
  31. return HRESULT_FROM_WIN32( GetLastError() );
  32. }
  33. memcpy( (PBYTE) _bufId.QueryPtr() + _cbCredentialId,
  34. pbHash,
  35. cbHash );
  36. _cbCredentialId += cbHash;
  37. return NO_ERROR;
  38. }
  39. VOID *
  40. QueryBuffer(
  41. VOID
  42. )
  43. {
  44. return _bufId.QueryPtr();
  45. }
  46. DWORD
  47. QuerySize(
  48. VOID
  49. )
  50. {
  51. return _cbCredentialId;
  52. }
  53. static
  54. bool
  55. IsEqual(
  56. CREDENTIAL_ID * pId1,
  57. CREDENTIAL_ID * pId2
  58. )
  59. {
  60. return ( pId1->_cbCredentialId == pId2->_cbCredentialId ) &&
  61. ( !memcmp( pId1->_bufId.QueryPtr(),
  62. pId2->_bufId.QueryPtr(),
  63. min( pId1->_cbCredentialId, pId2->_cbCredentialId ) ) );
  64. }
  65. private:
  66. BUFFER _bufId;
  67. DWORD _cbCredentialId;
  68. };
  69. class SERVER_CERT_HASH;
  70. class CERT_STORE;
  71. #define SERVER_CERT_SIGNATURE (DWORD)'RCRS'
  72. #define SERVER_CERT_SIGNATURE_FREE (DWORD)'rcrs'
  73. class SERVER_CERT
  74. {
  75. public:
  76. SERVER_CERT(
  77. IN CREDENTIAL_ID * pCredentialId
  78. );
  79. virtual
  80. ~SERVER_CERT();
  81. BOOL
  82. CheckSignature(
  83. VOID
  84. ) const
  85. {
  86. return _dwSignature == SERVER_CERT_SIGNATURE;
  87. }
  88. CREDENTIAL_ID *
  89. QueryCredentialId(
  90. VOID
  91. ) const
  92. {
  93. return _pCredentialId;
  94. }
  95. VOID
  96. ReferenceServerCert(
  97. VOID
  98. )
  99. {
  100. InterlockedIncrement( &_cRefs );
  101. }
  102. VOID
  103. DereferenceServerCert(
  104. VOID
  105. )
  106. {
  107. if ( !InterlockedDecrement( &_cRefs ) )
  108. {
  109. delete this;
  110. }
  111. }
  112. PCCERT_CONTEXT *
  113. QueryCertContext(
  114. VOID
  115. )
  116. {
  117. return &_pCertContext;
  118. }
  119. HCERTSTORE
  120. QueryStore(
  121. VOID
  122. ) const
  123. {
  124. return _pCertStore->QueryStore();
  125. }
  126. STRA *
  127. QueryIssuer(
  128. VOID
  129. )
  130. {
  131. return &_strIssuer;
  132. }
  133. STRA *
  134. QuerySubject(
  135. VOID
  136. )
  137. {
  138. return &_strSubject;
  139. }
  140. USHORT
  141. QueryPublicKeySize(
  142. VOID
  143. ) const
  144. {
  145. return _usPublicKeySize;
  146. }
  147. BOOL
  148. QueryUsesHardwareAccelerator(
  149. VOID
  150. ) const
  151. {
  152. return _fUsesHardwareAccelerator;
  153. }
  154. HRESULT
  155. SetupCertificate(
  156. IN PBYTE pbSslCertHash,
  157. IN DWORD cbSslCertHash,
  158. IN WCHAR * pszSslCertStoreName
  159. );
  160. static
  161. HRESULT
  162. Initialize(
  163. VOID
  164. );
  165. static
  166. VOID
  167. Terminate(
  168. VOID
  169. );
  170. static
  171. HRESULT
  172. GetServerCertificate(
  173. IN PBYTE pbSslCertHash,
  174. IN DWORD cbSslCertHash,
  175. IN WCHAR * pszSslCertStoreName,
  176. OUT SERVER_CERT ** ppServerCert
  177. );
  178. static
  179. HRESULT
  180. BuildCredentialId(
  181. IN PBYTE pbSslCertHash,
  182. IN DWORD cbSslCertHash,
  183. OUT CREDENTIAL_ID * pCredentialId
  184. );
  185. static
  186. HRESULT
  187. FlushByStore(
  188. IN CERT_STORE * pCertStore
  189. );
  190. static
  191. VOID
  192. Cleanup(
  193. VOID
  194. );
  195. static
  196. LK_PREDICATE
  197. CertStorePredicate(
  198. IN SERVER_CERT * pServerCert,
  199. IN void * pvState
  200. );
  201. private:
  202. HRESULT DetermineUseOfSSLHardwareAccelerator(
  203. VOID
  204. );
  205. DWORD _dwSignature;
  206. LONG _cRefs;
  207. PCCERT_CONTEXT _pCertContext;
  208. CERT_STORE * _pCertStore;
  209. CREDENTIAL_ID * _pCredentialId;
  210. USHORT _usPublicKeySize;
  211. STRA _strIssuer;
  212. STRA _strSubject;
  213. //
  214. // indication that SSL hardware accelerator is used
  215. // (since currently there is no reliable way to ask
  216. // schannel of CAPI if SSL hardware accelerator is indeed used
  217. // we use optimistic lookup of config info based on magic
  218. // instructions from John Banes (see Windows Bugs 510131)
  219. // and based on that we make educated guess
  220. // The only purpose to care about this is for performance tuning
  221. // of the threadpool
  222. //
  223. BOOL _fUsesHardwareAccelerator;
  224. static SERVER_CERT_HASH * sm_pServerCertHash;
  225. };
  226. class SERVER_CERT_HASH
  227. : public CTypedHashTable<
  228. SERVER_CERT_HASH,
  229. SERVER_CERT,
  230. CREDENTIAL_ID *
  231. >
  232. {
  233. public:
  234. SERVER_CERT_HASH()
  235. : CTypedHashTable< SERVER_CERT_HASH,
  236. SERVER_CERT,
  237. CREDENTIAL_ID * > ( "SERVER_CERT_HASH" )
  238. {
  239. }
  240. static
  241. CREDENTIAL_ID *
  242. ExtractKey(
  243. const SERVER_CERT * pServerCert
  244. )
  245. {
  246. return pServerCert->QueryCredentialId();
  247. }
  248. static
  249. DWORD
  250. CalcKeyHash(
  251. CREDENTIAL_ID * pCredentialId
  252. )
  253. {
  254. return HashBlob( pCredentialId->QueryBuffer(),
  255. pCredentialId->QuerySize() );
  256. }
  257. static
  258. bool
  259. EqualKeys(
  260. CREDENTIAL_ID * pId1,
  261. CREDENTIAL_ID * pId2
  262. )
  263. {
  264. return CREDENTIAL_ID::IsEqual( pId1, pId2 );
  265. }
  266. static
  267. void
  268. AddRefRecord(
  269. SERVER_CERT * pServerCert,
  270. int nIncr
  271. )
  272. {
  273. if ( nIncr == +1 )
  274. {
  275. pServerCert->ReferenceServerCert();
  276. }
  277. else if ( nIncr == -1 )
  278. {
  279. pServerCert->DereferenceServerCert();
  280. }
  281. }
  282. private:
  283. //
  284. // Not implemented methods
  285. // Declarations present to prevent compiler
  286. // to generate default ones.
  287. //
  288. SERVER_CERT_HASH( const SERVER_CERT_HASH& );
  289. SERVER_CERT_HASH& operator=( const SERVER_CERT_HASH& );
  290. };
  291. #endif