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.

450 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: async.cpp
  7. //
  8. // Contents: Async Parameter Management
  9. //
  10. // History: 05-Aug-97 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <global.hxx>
  14. #include <async.h>
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Function: CryptCreateAsyncHandle
  18. //
  19. // Synopsis: create async param handle
  20. //
  21. //----------------------------------------------------------------------------
  22. BOOL WINAPI
  23. CryptCreateAsyncHandle (
  24. IN DWORD dwFlags,
  25. OUT PHCRYPTASYNC phAsync
  26. )
  27. {
  28. CCryptAsyncHandle* pAsyncHandle;
  29. pAsyncHandle = new CCryptAsyncHandle( dwFlags );
  30. if ( pAsyncHandle == NULL )
  31. {
  32. SetLastError( (DWORD) E_OUTOFMEMORY );
  33. return( FALSE );
  34. }
  35. *phAsync = pAsyncHandle;
  36. return( TRUE );
  37. }
  38. //+---------------------------------------------------------------------------
  39. //
  40. // Function: CryptSetAsyncParam
  41. //
  42. // Synopsis: set async parameter
  43. //
  44. //----------------------------------------------------------------------------
  45. BOOL WINAPI
  46. CryptSetAsyncParam (
  47. IN HCRYPTASYNC hAsync,
  48. IN LPSTR pszParamOid,
  49. IN LPVOID pvParam,
  50. IN OPTIONAL PFN_CRYPT_ASYNC_PARAM_FREE_FUNC pfnFree
  51. )
  52. {
  53. return( ( ( CCryptAsyncHandle* )hAsync )->SetAsyncParam(
  54. pszParamOid,
  55. pvParam,
  56. pfnFree
  57. ) );
  58. }
  59. //+---------------------------------------------------------------------------
  60. //
  61. // Function: CryptGetAsyncParam
  62. //
  63. // Synopsis: get async parameter
  64. //
  65. //----------------------------------------------------------------------------
  66. BOOL WINAPI
  67. CryptGetAsyncParam (
  68. IN HCRYPTASYNC hAsync,
  69. IN LPSTR pszParamOid,
  70. OUT LPVOID* ppvParam,
  71. OUT OPTIONAL PFN_CRYPT_ASYNC_PARAM_FREE_FUNC* ppfnFree
  72. )
  73. {
  74. return( ( ( CCryptAsyncHandle* )hAsync )->GetAsyncParam(
  75. pszParamOid,
  76. ppvParam,
  77. ppfnFree
  78. ) );
  79. }
  80. //+---------------------------------------------------------------------------
  81. //
  82. // Function: CryptCloseAsyncHandle
  83. //
  84. // Synopsis: close async handle
  85. //
  86. //----------------------------------------------------------------------------
  87. BOOL WINAPI
  88. CryptCloseAsyncHandle (
  89. IN HCRYPTASYNC hAsync
  90. )
  91. {
  92. delete ( CCryptAsyncHandle * )hAsync;
  93. return( TRUE );
  94. }
  95. //+---------------------------------------------------------------------------
  96. //
  97. // Member: CCryptAsyncHandle::CCryptAsyncHandle, public
  98. //
  99. // Synopsis: Constructor
  100. //
  101. //----------------------------------------------------------------------------
  102. CCryptAsyncHandle::CCryptAsyncHandle (DWORD dwFlags)
  103. {
  104. m_pConstOidList = NULL;
  105. m_pStrOidList = NULL;
  106. Pki_InitializeCriticalSection( &m_AsyncLock );
  107. }
  108. //+---------------------------------------------------------------------------
  109. //
  110. // Member: CCryptAsyncHandle::~CCryptAsyncHandle, public
  111. //
  112. // Synopsis: Destructor
  113. //
  114. //----------------------------------------------------------------------------
  115. CCryptAsyncHandle::~CCryptAsyncHandle ()
  116. {
  117. FreeOidList( m_pConstOidList, TRUE );
  118. FreeOidList( m_pStrOidList, FALSE );
  119. DeleteCriticalSection( &m_AsyncLock );
  120. }
  121. //+---------------------------------------------------------------------------
  122. //
  123. // Member: CCryptAsyncHandle::SetAsyncParam, public
  124. //
  125. // Synopsis: set an async parameter, if the pvParam is NULL then
  126. // the parameter is removed and freed if a free function
  127. // has been specified
  128. //
  129. //----------------------------------------------------------------------------
  130. BOOL
  131. CCryptAsyncHandle::SetAsyncParam (
  132. LPSTR pszParamOid,
  133. LPVOID pvParam,
  134. PFN_CRYPT_ASYNC_PARAM_FREE_FUNC pfnFree
  135. )
  136. {
  137. BOOL fReturn = FALSE;
  138. PCRYPT_ASYNC_PARAM pParam = NULL;
  139. BOOL fConstOid = ( (DWORD_PTR)pszParamOid <= 0xFFFF );
  140. EnterCriticalSection( &m_AsyncLock );
  141. pParam = FindAsyncParam( pszParamOid, fConstOid );
  142. if ( pvParam == NULL )
  143. {
  144. if ( pParam != NULL )
  145. {
  146. RemoveAsyncParam( pParam );
  147. FreeAsyncParam( pParam, fConstOid );
  148. fReturn = TRUE;
  149. }
  150. else
  151. {
  152. SetLastError( (DWORD) E_INVALIDARG );
  153. }
  154. LeaveCriticalSection( &m_AsyncLock );
  155. return( fReturn );
  156. }
  157. if ( pParam != NULL )
  158. {
  159. if ( pParam->pfnFree != NULL )
  160. {
  161. (*pParam->pfnFree)( pszParamOid, pvParam );
  162. }
  163. pParam->pvParam = pvParam;
  164. LeaveCriticalSection( &m_AsyncLock );
  165. return( TRUE );
  166. }
  167. if ( AllocAsyncParam(
  168. pszParamOid,
  169. fConstOid,
  170. pvParam,
  171. pfnFree,
  172. &pParam
  173. ) == TRUE )
  174. {
  175. AddAsyncParam( pParam, fConstOid );
  176. fReturn = TRUE;
  177. }
  178. else
  179. {
  180. fReturn = FALSE;
  181. }
  182. LeaveCriticalSection( &m_AsyncLock );
  183. return( fReturn );
  184. }
  185. //+---------------------------------------------------------------------------
  186. //
  187. // Member: CCryptAsyncHandle::GetAsyncParam, public
  188. //
  189. // Synopsis: get an async parameter
  190. //
  191. //----------------------------------------------------------------------------
  192. BOOL
  193. CCryptAsyncHandle::GetAsyncParam (
  194. LPSTR pszParamOid,
  195. LPVOID* ppvParam,
  196. PFN_CRYPT_ASYNC_PARAM_FREE_FUNC* ppfnFree
  197. )
  198. {
  199. PCRYPT_ASYNC_PARAM pFoundParam = NULL;
  200. BOOL fConstOid = ( (DWORD_PTR)pszParamOid <= 0xFFFF );
  201. EnterCriticalSection( &m_AsyncLock );
  202. pFoundParam = FindAsyncParam( pszParamOid, fConstOid );
  203. if ( pFoundParam == NULL )
  204. {
  205. LeaveCriticalSection( &m_AsyncLock );
  206. SetLastError( (DWORD) E_INVALIDARG );
  207. return( FALSE );
  208. }
  209. *ppvParam = pFoundParam->pvParam;
  210. if ( ppfnFree != NULL )
  211. {
  212. *ppfnFree = pFoundParam->pfnFree;
  213. }
  214. LeaveCriticalSection( &m_AsyncLock );
  215. return( TRUE );
  216. }
  217. //+---------------------------------------------------------------------------
  218. //
  219. // Member: CCryptAsyncHandle::AllocAsyncParam, private
  220. //
  221. // Synopsis: allocate an async parameter block
  222. //
  223. //----------------------------------------------------------------------------
  224. BOOL
  225. CCryptAsyncHandle::AllocAsyncParam (
  226. LPSTR pszParamOid,
  227. BOOL fConstOid,
  228. LPVOID pvParam,
  229. PFN_CRYPT_ASYNC_PARAM_FREE_FUNC pfnFree,
  230. PCRYPT_ASYNC_PARAM* ppParam
  231. )
  232. {
  233. HRESULT hr = S_OK;
  234. PCRYPT_ASYNC_PARAM pParam;
  235. pParam = new CRYPT_ASYNC_PARAM;
  236. if ( pParam != NULL )
  237. {
  238. memset( pParam, 0, sizeof( CRYPT_ASYNC_PARAM ) );
  239. if ( fConstOid == FALSE )
  240. {
  241. pParam->pszOid = new CHAR [strlen( pszParamOid ) + 1];
  242. if ( pParam->pszOid != NULL )
  243. {
  244. strcpy( pParam->pszOid, pszParamOid );
  245. }
  246. else
  247. {
  248. hr = E_OUTOFMEMORY;
  249. }
  250. }
  251. else
  252. {
  253. pParam->pszOid = pszParamOid;
  254. }
  255. }
  256. else
  257. {
  258. hr = E_OUTOFMEMORY;
  259. }
  260. if ( hr != S_OK )
  261. {
  262. SetLastError( hr );
  263. return( FALSE );
  264. }
  265. pParam->pvParam = pvParam,
  266. pParam->pfnFree = pfnFree;
  267. *ppParam = pParam;
  268. return( TRUE );
  269. }
  270. //+---------------------------------------------------------------------------
  271. //
  272. // Member: CCryptAsyncHandle::FreeAsyncParam, private
  273. //
  274. // Synopsis: free an async param
  275. //
  276. //----------------------------------------------------------------------------
  277. VOID
  278. CCryptAsyncHandle::FreeAsyncParam (
  279. PCRYPT_ASYNC_PARAM pParam,
  280. BOOL fConstOid
  281. )
  282. {
  283. if ( pParam->pfnFree != NULL )
  284. {
  285. (*pParam->pfnFree)( pParam->pszOid, pParam->pvParam );
  286. }
  287. if ( fConstOid == FALSE )
  288. {
  289. delete pParam->pszOid;
  290. }
  291. delete pParam;
  292. }
  293. //+---------------------------------------------------------------------------
  294. //
  295. // Member: CCryptAsyncHandle::AddAsyncParam, private
  296. //
  297. // Synopsis: add an async parameter
  298. //
  299. //----------------------------------------------------------------------------
  300. VOID
  301. CCryptAsyncHandle::AddAsyncParam (
  302. PCRYPT_ASYNC_PARAM pParam,
  303. BOOL fConstOid
  304. )
  305. {
  306. PCRYPT_ASYNC_PARAM* ppOidList;
  307. if ( fConstOid == TRUE )
  308. {
  309. ppOidList = &m_pConstOidList;
  310. }
  311. else
  312. {
  313. ppOidList = &m_pStrOidList;
  314. }
  315. pParam->pNext = *ppOidList;
  316. pParam->pPrev = NULL;
  317. *ppOidList = pParam;
  318. }
  319. //+---------------------------------------------------------------------------
  320. //
  321. // Member: CCryptAsyncHandle::RemoveAsyncParam, private
  322. //
  323. // Synopsis: remove an async parameter
  324. //
  325. //----------------------------------------------------------------------------
  326. VOID
  327. CCryptAsyncHandle::RemoveAsyncParam (
  328. PCRYPT_ASYNC_PARAM pParam
  329. )
  330. {
  331. if ( pParam->pPrev != NULL )
  332. {
  333. pParam->pPrev->pNext = pParam->pNext;
  334. }
  335. if ( pParam->pNext != NULL )
  336. {
  337. pParam->pNext->pPrev = pParam->pPrev;
  338. }
  339. }
  340. //+---------------------------------------------------------------------------
  341. //
  342. // Member: CCryptAsyncHandle::FindAsyncParam, private
  343. //
  344. // Synopsis: find an async parameter
  345. //
  346. //----------------------------------------------------------------------------
  347. PCRYPT_ASYNC_PARAM
  348. CCryptAsyncHandle::FindAsyncParam (
  349. LPSTR pszParamOid,
  350. BOOL fConstOid
  351. )
  352. {
  353. PCRYPT_ASYNC_PARAM pParam;
  354. if ( fConstOid == TRUE )
  355. {
  356. pParam = m_pConstOidList;
  357. }
  358. else
  359. {
  360. pParam = m_pStrOidList;
  361. }
  362. while ( pParam != NULL )
  363. {
  364. if ( fConstOid == TRUE )
  365. {
  366. if ( pParam->pszOid == pszParamOid )
  367. {
  368. break;
  369. }
  370. }
  371. else
  372. {
  373. if ( _stricmp( pParam->pszOid, pszParamOid ) == 0 )
  374. {
  375. break;
  376. }
  377. }
  378. pParam = pParam->pNext;
  379. }
  380. return( pParam );
  381. }
  382. //+---------------------------------------------------------------------------
  383. //
  384. // Member: CCryptAsyncHandle::FreeOidList, private
  385. //
  386. // Synopsis: free an OID list
  387. //
  388. //----------------------------------------------------------------------------
  389. VOID
  390. CCryptAsyncHandle::FreeOidList (
  391. PCRYPT_ASYNC_PARAM pOidList,
  392. BOOL fConstOidList
  393. )
  394. {
  395. PCRYPT_ASYNC_PARAM pParam;
  396. PCRYPT_ASYNC_PARAM pParamNext;
  397. pParam = pOidList;
  398. while ( pParam != NULL )
  399. {
  400. pParamNext = pParam->pNext;
  401. FreeAsyncParam( pParam, fConstOidList );
  402. pParam = pParamNext;
  403. }
  404. }