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.

1497 lines
35 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1999
  3. Module Name:
  4. logcsp
  5. Abstract:
  6. This module provides the standard CSP entrypoints for the Logging CSP.
  7. The Logging CSP provides for additional control over loading CSPs, and
  8. for tracing of the activities of CSPs.
  9. Author:
  10. Doug Barlow (dbarlow) 12/7/1999
  11. Notes:
  12. ?Notes?
  13. --*/
  14. #ifndef WIN32_LEAN_AND_MEAN
  15. #define WIN32_LEAN_AND_MEAN
  16. #endif
  17. #include <windows.h>
  18. #include "logcsp.h"
  19. #define LOGCSPAPI BOOL WINAPI
  20. typedef struct {
  21. HCRYPTPROV hProv;
  22. CLoggingContext *pCtx;
  23. } LogProvider;
  24. CDynamicPointerArray<CLoggingContext> *g_prgCtxs = NULL;
  25. /*
  26. - CPAcquireContext
  27. -
  28. * Purpose:
  29. * The CPAcquireContext function is used to acquire a context
  30. * handle to a cryptograghic service provider (CSP).
  31. *
  32. *
  33. * Parameters:
  34. * OUT phProv - Handle to a CSP
  35. * IN pszContainer - Pointer to a string of key container
  36. * IN dwFlags - Flags values
  37. * IN pVTable - Pointer to table of function pointers
  38. *
  39. * Returns:
  40. */
  41. LOGCSPAPI
  42. CPAcquireContext(
  43. OUT HCRYPTPROV *phProv,
  44. IN LPCTSTR pszContainer,
  45. IN DWORD dwFlags,
  46. IN PVTableProvStruc pVTable)
  47. {
  48. DWORD dwReturn;
  49. DWORD dwIndex;
  50. CLoggingContext *pTmpCtx;
  51. CLoggingContext *pCtx = NULL;
  52. LogProvider *pProv = NULL;
  53. HINSTANCE hInst;
  54. CRegistry regRoot;
  55. LPCTSTR szImage;
  56. //
  57. // Make sure we're initialized.
  58. //
  59. entrypoint
  60. if (NULL == g_prgCtxs)
  61. {
  62. g_prgCtxs = new CDynamicPointerArray<CLoggingContext>;
  63. if (NULL == g_prgCtxs)
  64. {
  65. dwReturn = NTE_NO_MEMORY;
  66. goto ErrorExit;
  67. }
  68. }
  69. //
  70. // Get the CSP image name.
  71. //
  72. switch (pVTable->Version)
  73. {
  74. //
  75. // These cases are older versions of the operating systems that don't
  76. // tell us which CSP is being loaded. Hence we need to pick up the
  77. // information from a separate registry setting.
  78. //
  79. case 1:
  80. case 2:
  81. regRoot.Open(HKEY_LOCAL_MACHINE, g_szLogCspRegistry, KEY_READ);
  82. if (ERROR_SUCCESS != regRoot.Status(TRUE))
  83. {
  84. dwReturn = ERROR_SERVICE_NOT_FOUND;
  85. goto ErrorExit;
  86. }
  87. break;
  88. //
  89. // This must be at least a Win2k or Millennium system. We can see which
  90. // CSP is being loaded, so we can do logging to different files for each
  91. // CSP.
  92. //
  93. case 3:
  94. if ((NULL == pVTable->pszProvName) || (0 == *pVTable->pszProvName))
  95. {
  96. regRoot.Open(HKEY_LOCAL_MACHINE, g_szLogCspRegistry, KEY_READ);
  97. if (ERROR_SUCCESS != regRoot.Status(TRUE))
  98. {
  99. dwReturn = ERROR_SERVICE_NOT_FOUND;
  100. goto ErrorExit;
  101. }
  102. }
  103. else
  104. {
  105. try
  106. {
  107. CRegistry regCrypt(
  108. HKEY_LOCAL_MACHINE,
  109. g_szCspRegistry,
  110. KEY_READ);
  111. regRoot.Open(regCrypt, pVTable->pszProvName, KEY_READ);
  112. regRoot.Status();
  113. }
  114. catch (...)
  115. {
  116. dwReturn = ERROR_SERVICE_NOT_FOUND;
  117. goto ErrorExit;
  118. }
  119. }
  120. break;
  121. //
  122. // Either this file is out of date, or we've gotten onto a really old
  123. // version of windows who's advapi is just supplying us with the address
  124. // of the signature verification subroutine.
  125. //
  126. default:
  127. if (1024 < pVTable->Version)
  128. dwReturn = ERROR_OLD_WIN_VERSION;
  129. else
  130. dwReturn = ERROR_RMODE_APP;
  131. goto ErrorExit;
  132. }
  133. //
  134. // regRoot now provides a handle to to a point in the registry from
  135. // which we can read additional parameters. Get the name of dll to be
  136. // loaded.
  137. //
  138. try
  139. {
  140. szImage = regRoot.GetStringValue(g_szSavedImagePath);
  141. }
  142. catch (...)
  143. {
  144. dwReturn = ERROR_SERVICE_NOT_FOUND;
  145. // ?BUGBUG? Might also be Out of Memory.
  146. goto ErrorExit;
  147. }
  148. //
  149. // Is this CSP in our cache?
  150. //
  151. pTmpCtx = NULL;
  152. hInst = GetModuleHandle(szImage);
  153. if (NULL != hInst)
  154. {
  155. for (dwIndex = g_prgCtxs->Count(); 0 < dwIndex;)
  156. {
  157. pTmpCtx = (*g_prgCtxs)[--dwIndex];
  158. if (NULL != pTmpCtx)
  159. {
  160. if (hInst == pTmpCtx->Module())
  161. break;
  162. else
  163. pTmpCtx = NULL;
  164. }
  165. }
  166. }
  167. if (NULL == pTmpCtx)
  168. {
  169. pCtx = new CLoggingContext();
  170. if (NULL == pCtx)
  171. {
  172. dwReturn = NTE_NO_MEMORY;
  173. goto ErrorExit;
  174. }
  175. for (dwIndex = 0; NULL != (*g_prgCtxs)[dwIndex]; dwIndex += 1)
  176. ; // Empty loop
  177. g_prgCtxs->Set(dwIndex, pCtx);
  178. pCtx->m_dwIndex = dwIndex;
  179. dwReturn = pCtx->Initialize(pVTable, regRoot);
  180. if (ERROR_SUCCESS != dwReturn)
  181. goto ErrorExit;
  182. }
  183. else
  184. pCtx = pTmpCtx->AddRef();
  185. pProv = new LogProvider;
  186. if (NULL == pProv)
  187. {
  188. dwReturn = NTE_NO_MEMORY;
  189. goto ErrorExit;
  190. }
  191. ZeroMemory(pProv, sizeof(LogProvider));
  192. //
  193. // Now we can really call the CSP.
  194. //
  195. dwReturn = pCtx->AcquireContext(
  196. &pProv->hProv,
  197. pszContainer,
  198. dwFlags,
  199. pVTable);
  200. if (ERROR_SUCCESS != dwReturn)
  201. goto ErrorExit;
  202. pProv->pCtx = pCtx;
  203. pCtx = NULL;
  204. *phProv = (HCRYPTPROV)pProv;
  205. pProv = NULL;
  206. return TRUE;
  207. ErrorExit:
  208. if (NULL != pCtx)
  209. pCtx->Release();
  210. if (NULL != pProv)
  211. {
  212. if (NULL != pProv->pCtx)
  213. pProv->pCtx->Release();
  214. delete pProv;
  215. }
  216. SetLastError(dwReturn);
  217. return FALSE;
  218. }
  219. /*
  220. - CPGetProvParam
  221. -
  222. * Purpose:
  223. * Allows applications to get various aspects of the
  224. * operations of a provider
  225. *
  226. * Parameters:
  227. * IN hProv - Handle to a CSP
  228. * IN dwParam - Parameter number
  229. * IN pbData - Pointer to data
  230. * IN OUT pdwDataLen - Length of parameter data
  231. * IN dwFlags - Flags values
  232. *
  233. * Returns:
  234. */
  235. LOGCSPAPI
  236. CPGetProvParam(
  237. IN HCRYPTPROV hProv,
  238. IN DWORD dwParam,
  239. OUT BYTE *pbData,
  240. IN OUT DWORD *pdwDataLen,
  241. IN DWORD dwFlags)
  242. {
  243. BOOL fReturn;
  244. DWORD dwReturn;
  245. LogProvider *pProv = (LogProvider *)hProv;
  246. CLoggingContext *pCtx = pProv->pCtx;
  247. entrypoint
  248. dwReturn = pCtx->GetProvParam(
  249. pProv->hProv,
  250. dwParam,
  251. pbData,
  252. pdwDataLen,
  253. dwFlags);
  254. if (ERROR_SUCCESS != dwReturn)
  255. {
  256. fReturn = FALSE;
  257. SetLastError(dwReturn);
  258. }
  259. else
  260. fReturn = TRUE;
  261. return fReturn;
  262. }
  263. /*
  264. - CPReleaseContext
  265. -
  266. * Purpose:
  267. * The CPReleaseContext function is used to release a
  268. * context created by CrytAcquireContext.
  269. *
  270. * Parameters:
  271. * IN phProv - Handle to a CSP
  272. * IN dwFlags - Flags values
  273. *
  274. * Returns:
  275. */
  276. LOGCSPAPI
  277. CPReleaseContext(
  278. IN HCRYPTPROV hProv,
  279. IN DWORD dwFlags)
  280. {
  281. BOOL fReturn;
  282. DWORD dwReturn;
  283. LogProvider *pProv = (LogProvider *)hProv;
  284. CLoggingContext *pCtx = pProv->pCtx;
  285. entrypoint
  286. dwReturn = pCtx->ReleaseContext(
  287. pProv->hProv,
  288. dwFlags);
  289. if (ERROR_SUCCESS != dwReturn)
  290. {
  291. fReturn = FALSE;
  292. SetLastError(dwReturn);
  293. }
  294. else
  295. {
  296. pCtx->Release();
  297. delete pProv;
  298. fReturn = TRUE;
  299. }
  300. return fReturn;
  301. }
  302. /*
  303. - CPSetProvParam
  304. -
  305. * Purpose:
  306. * Allows applications to customize various aspects of the
  307. * operations of a provider
  308. *
  309. * Parameters:
  310. * IN hProv - Handle to a CSP
  311. * IN dwParam - Parameter number
  312. * IN pbData - Pointer to data
  313. * IN dwFlags - Flags values
  314. *
  315. * Returns:
  316. */
  317. LOGCSPAPI
  318. CPSetProvParam(
  319. IN HCRYPTPROV hProv,
  320. IN DWORD dwParam,
  321. IN BYTE *pbData,
  322. IN DWORD dwFlags)
  323. {
  324. BOOL fReturn;
  325. DWORD dwReturn;
  326. LogProvider *pProv = (LogProvider *)hProv;
  327. CLoggingContext *pCtx = pProv->pCtx;
  328. entrypoint
  329. dwReturn = pCtx->SetProvParam(
  330. pProv->hProv,
  331. dwParam,
  332. pbData,
  333. dwFlags);
  334. if (ERROR_SUCCESS != dwReturn)
  335. {
  336. fReturn = FALSE;
  337. SetLastError(dwReturn);
  338. }
  339. else
  340. fReturn = TRUE;
  341. return fReturn;
  342. }
  343. /*
  344. - CPDeriveKey
  345. -
  346. * Purpose:
  347. * Derive cryptographic keys from base data
  348. *
  349. *
  350. * Parameters:
  351. * IN hProv - Handle to a CSP
  352. * IN Algid - Algorithm identifier
  353. * IN hHash - Handle to hash
  354. * IN dwFlags - Flags values
  355. * OUT phKey - Handle to a generated key
  356. *
  357. * Returns:
  358. */
  359. LOGCSPAPI
  360. CPDeriveKey(
  361. IN HCRYPTPROV hProv,
  362. IN ALG_ID Algid,
  363. IN HCRYPTHASH hHash,
  364. IN DWORD dwFlags,
  365. OUT HCRYPTKEY * phKey)
  366. {
  367. BOOL fReturn;
  368. DWORD dwReturn;
  369. LogProvider *pProv = (LogProvider *)hProv;
  370. CLoggingContext *pCtx = pProv->pCtx;
  371. entrypoint
  372. dwReturn = pCtx->DeriveKey(
  373. pProv->hProv,
  374. Algid,
  375. hHash,
  376. dwFlags,
  377. phKey);
  378. if (ERROR_SUCCESS != dwReturn)
  379. {
  380. fReturn = FALSE;
  381. SetLastError(dwReturn);
  382. }
  383. else
  384. fReturn = TRUE;
  385. return fReturn;
  386. }
  387. /*
  388. - CPDestroyKey
  389. -
  390. * Purpose:
  391. * Destroys the cryptographic key that is being referenced
  392. * with the hKey parameter
  393. *
  394. *
  395. * Parameters:
  396. * IN hProv - Handle to a CSP
  397. * IN hKey - Handle to a key
  398. *
  399. * Returns:
  400. */
  401. LOGCSPAPI
  402. CPDestroyKey(
  403. IN HCRYPTPROV hProv,
  404. IN HCRYPTKEY hKey)
  405. {
  406. BOOL fReturn;
  407. DWORD dwReturn;
  408. LogProvider *pProv = (LogProvider *)hProv;
  409. CLoggingContext *pCtx = pProv->pCtx;
  410. entrypoint
  411. dwReturn = pCtx->DestroyKey(
  412. pProv->hProv,
  413. hKey);
  414. if (ERROR_SUCCESS != dwReturn)
  415. {
  416. fReturn = FALSE;
  417. SetLastError(dwReturn);
  418. }
  419. else
  420. fReturn = TRUE;
  421. return fReturn;
  422. }
  423. /*
  424. - CPExportKey
  425. -
  426. * Purpose:
  427. * Export cryptographic keys out of a CSP in a secure manner
  428. *
  429. *
  430. * Parameters:
  431. * IN hProv - Handle to the CSP user
  432. * IN hKey - Handle to the key to export
  433. * IN hPubKey - Handle to the exchange public key value of
  434. * the destination user
  435. * IN dwBlobType - Type of key blob to be exported
  436. * IN dwFlags - Flags values
  437. * OUT pbData - Key blob data
  438. * IN OUT pdwDataLen - Length of key blob in bytes
  439. *
  440. * Returns:
  441. */
  442. LOGCSPAPI
  443. CPExportKey(
  444. IN HCRYPTPROV hProv,
  445. IN HCRYPTKEY hKey,
  446. IN HCRYPTKEY hPubKey,
  447. IN DWORD dwBlobType,
  448. IN DWORD dwFlags,
  449. OUT BYTE *pbData,
  450. IN OUT DWORD *pdwDataLen)
  451. {
  452. BOOL fReturn;
  453. DWORD dwReturn;
  454. LogProvider *pProv = (LogProvider *)hProv;
  455. CLoggingContext *pCtx = pProv->pCtx;
  456. entrypoint
  457. dwReturn = pCtx->ExportKey(
  458. pProv->hProv,
  459. hKey,
  460. hPubKey,
  461. dwBlobType,
  462. dwFlags,
  463. pbData,
  464. pdwDataLen);
  465. if (ERROR_SUCCESS != dwReturn)
  466. {
  467. fReturn = FALSE;
  468. SetLastError(dwReturn);
  469. }
  470. else
  471. fReturn = TRUE;
  472. return fReturn;
  473. }
  474. /*
  475. - CPGenKey
  476. -
  477. * Purpose:
  478. * Generate cryptographic keys
  479. *
  480. *
  481. * Parameters:
  482. * IN hProv - Handle to a CSP
  483. * IN Algid - Algorithm identifier
  484. * IN dwFlags - Flags values
  485. * OUT phKey - Handle to a generated key
  486. *
  487. * Returns:
  488. */
  489. LOGCSPAPI
  490. CPGenKey(
  491. IN HCRYPTPROV hProv,
  492. IN ALG_ID Algid,
  493. IN DWORD dwFlags,
  494. OUT HCRYPTKEY *phKey)
  495. {
  496. BOOL fReturn;
  497. DWORD dwReturn;
  498. LogProvider *pProv = (LogProvider *)hProv;
  499. CLoggingContext *pCtx = pProv->pCtx;
  500. entrypoint
  501. dwReturn = pCtx->GenKey(
  502. pProv->hProv,
  503. Algid,
  504. dwFlags,
  505. phKey);
  506. if (ERROR_SUCCESS != dwReturn)
  507. {
  508. fReturn = FALSE;
  509. SetLastError(dwReturn);
  510. }
  511. else
  512. fReturn = TRUE;
  513. return fReturn;
  514. }
  515. /*
  516. - CPGetKeyParam
  517. -
  518. * Purpose:
  519. * Allows applications to get various aspects of the
  520. * operations of a key
  521. *
  522. * Parameters:
  523. * IN hProv - Handle to a CSP
  524. * IN hKey - Handle to a key
  525. * IN dwParam - Parameter number
  526. * OUT pbData - Pointer to data
  527. * IN pdwDataLen - Length of parameter data
  528. * IN dwFlags - Flags values
  529. *
  530. * Returns:
  531. */
  532. LOGCSPAPI
  533. CPGetKeyParam(
  534. IN HCRYPTPROV hProv,
  535. IN HCRYPTKEY hKey,
  536. IN DWORD dwParam,
  537. OUT BYTE *pbData,
  538. IN OUT DWORD *pdwDataLen,
  539. IN DWORD dwFlags)
  540. {
  541. BOOL fReturn;
  542. DWORD dwReturn;
  543. LogProvider *pProv = (LogProvider *)hProv;
  544. CLoggingContext *pCtx = pProv->pCtx;
  545. entrypoint
  546. dwReturn = pCtx->GetKeyParam(
  547. pProv->hProv,
  548. hKey,
  549. dwParam,
  550. pbData,
  551. pdwDataLen,
  552. dwFlags);
  553. if (ERROR_SUCCESS != dwReturn)
  554. {
  555. fReturn = FALSE;
  556. SetLastError(dwReturn);
  557. }
  558. else
  559. fReturn = TRUE;
  560. return fReturn;
  561. }
  562. /*
  563. - CPGenRandom
  564. -
  565. * Purpose:
  566. * Used to fill a buffer with random bytes
  567. *
  568. *
  569. * Parameters:
  570. * IN hProv - Handle to the user identifcation
  571. * IN dwLen - Number of bytes of random data requested
  572. * IN OUT pbBuffer- Pointer to the buffer where the random
  573. * bytes are to be placed
  574. *
  575. * Returns:
  576. */
  577. LOGCSPAPI
  578. CPGenRandom(
  579. IN HCRYPTPROV hProv,
  580. IN DWORD dwLen,
  581. IN OUT BYTE *pbBuffer)
  582. {
  583. BOOL fReturn;
  584. DWORD dwReturn;
  585. LogProvider *pProv = (LogProvider *)hProv;
  586. CLoggingContext *pCtx = pProv->pCtx;
  587. entrypoint
  588. dwReturn = pCtx->GenRandom(
  589. pProv->hProv,
  590. dwLen,
  591. pbBuffer);
  592. if (ERROR_SUCCESS != dwReturn)
  593. {
  594. fReturn = FALSE;
  595. SetLastError(dwReturn);
  596. }
  597. else
  598. fReturn = TRUE;
  599. return fReturn;
  600. }
  601. /*
  602. - CPGetUserKey
  603. -
  604. * Purpose:
  605. * Gets a handle to a permanent user key
  606. *
  607. *
  608. * Parameters:
  609. * IN hProv - Handle to the user identifcation
  610. * IN dwKeySpec - Specification of the key to retrieve
  611. * OUT phUserKey - Pointer to key handle of retrieved key
  612. *
  613. * Returns:
  614. */
  615. LOGCSPAPI
  616. CPGetUserKey(
  617. IN HCRYPTPROV hProv,
  618. IN DWORD dwKeySpec,
  619. OUT HCRYPTKEY *phUserKey)
  620. {
  621. BOOL fReturn;
  622. DWORD dwReturn;
  623. LogProvider *pProv = (LogProvider *)hProv;
  624. CLoggingContext *pCtx = pProv->pCtx;
  625. entrypoint
  626. dwReturn = pCtx->GetUserKey(
  627. pProv->hProv,
  628. dwKeySpec,
  629. phUserKey);
  630. if (ERROR_SUCCESS != dwReturn)
  631. {
  632. fReturn = FALSE;
  633. SetLastError(dwReturn);
  634. }
  635. else
  636. fReturn = TRUE;
  637. return fReturn;
  638. }
  639. /*
  640. - CPImportKey
  641. -
  642. * Purpose:
  643. * Import cryptographic keys
  644. *
  645. *
  646. * Parameters:
  647. * IN hProv - Handle to the CSP user
  648. * IN pbData - Key blob data
  649. * IN dwDataLen - Length of the key blob data
  650. * IN hPubKey - Handle to the exchange public key value of
  651. * the destination user
  652. * IN dwFlags - Flags values
  653. * OUT phKey - Pointer to the handle to the key which was
  654. * Imported
  655. *
  656. * Returns:
  657. */
  658. LOGCSPAPI
  659. CPImportKey(
  660. IN HCRYPTPROV hProv,
  661. IN CONST BYTE *pbData,
  662. IN DWORD dwDataLen,
  663. IN HCRYPTKEY hPubKey,
  664. IN DWORD dwFlags,
  665. OUT HCRYPTKEY *phKey)
  666. {
  667. BOOL fReturn;
  668. DWORD dwReturn;
  669. LogProvider *pProv = (LogProvider *)hProv;
  670. CLoggingContext *pCtx = pProv->pCtx;
  671. entrypoint
  672. dwReturn = pCtx->ImportKey(
  673. pProv->hProv,
  674. pbData,
  675. dwDataLen,
  676. hPubKey,
  677. dwFlags,
  678. phKey);
  679. if (ERROR_SUCCESS != dwReturn)
  680. {
  681. fReturn = FALSE;
  682. SetLastError(dwReturn);
  683. }
  684. else
  685. fReturn = TRUE;
  686. return fReturn;
  687. }
  688. /*
  689. - CPSetKeyParam
  690. -
  691. * Purpose:
  692. * Allows applications to customize various aspects of the
  693. * operations of a key
  694. *
  695. * Parameters:
  696. * IN hProv - Handle to a CSP
  697. * IN hKey - Handle to a key
  698. * IN dwParam - Parameter number
  699. * IN pbData - Pointer to data
  700. * IN dwFlags - Flags values
  701. *
  702. * Returns:
  703. */
  704. LOGCSPAPI
  705. CPSetKeyParam(
  706. IN HCRYPTPROV hProv,
  707. IN HCRYPTKEY hKey,
  708. IN DWORD dwParam,
  709. IN BYTE *pbData,
  710. IN DWORD dwFlags)
  711. {
  712. BOOL fReturn;
  713. DWORD dwReturn;
  714. LogProvider *pProv = (LogProvider *)hProv;
  715. CLoggingContext *pCtx = pProv->pCtx;
  716. entrypoint
  717. dwReturn = pCtx->SetKeyParam(
  718. pProv->hProv,
  719. hKey,
  720. dwParam,
  721. pbData,
  722. dwFlags);
  723. if (ERROR_SUCCESS != dwReturn)
  724. {
  725. fReturn = FALSE;
  726. SetLastError(dwReturn);
  727. }
  728. else
  729. fReturn = TRUE;
  730. return fReturn;
  731. }
  732. /*
  733. - CPEncrypt
  734. -
  735. * Purpose:
  736. * Encrypt data
  737. *
  738. *
  739. * Parameters:
  740. * IN hProv - Handle to the CSP user
  741. * IN hKey - Handle to the key
  742. * IN hHash - Optional handle to a hash
  743. * IN Final - Boolean indicating if this is the final
  744. * block of plaintext
  745. * IN dwFlags - Flags values
  746. * IN OUT pbData - Data to be encrypted
  747. * IN OUT pdwDataLen - Pointer to the length of the data to be
  748. * encrypted
  749. * IN dwBufLen - Size of Data buffer
  750. *
  751. * Returns:
  752. */
  753. LOGCSPAPI
  754. CPEncrypt(
  755. IN HCRYPTPROV hProv,
  756. IN HCRYPTKEY hKey,
  757. IN HCRYPTHASH hHash,
  758. IN BOOL Final,
  759. IN DWORD dwFlags,
  760. IN OUT BYTE *pbData,
  761. IN OUT DWORD *pdwDataLen,
  762. IN DWORD dwBufLen)
  763. {
  764. BOOL fReturn;
  765. DWORD dwReturn;
  766. LogProvider *pProv = (LogProvider *)hProv;
  767. CLoggingContext *pCtx = pProv->pCtx;
  768. entrypoint
  769. dwReturn = pCtx->Encrypt(
  770. pProv->hProv,
  771. hKey,
  772. hHash,
  773. Final,
  774. dwFlags,
  775. pbData,
  776. pdwDataLen,
  777. dwBufLen);
  778. if (ERROR_SUCCESS != dwReturn)
  779. {
  780. fReturn = FALSE;
  781. SetLastError(dwReturn);
  782. }
  783. else
  784. fReturn = TRUE;
  785. return fReturn;
  786. }
  787. /*
  788. - CPDecrypt
  789. -
  790. * Purpose:
  791. * Decrypt data
  792. *
  793. *
  794. * Parameters:
  795. * IN hProv - Handle to the CSP user
  796. * IN hKey - Handle to the key
  797. * IN hHash - Optional handle to a hash
  798. * IN Final - Boolean indicating if this is the final
  799. * block of ciphertext
  800. * IN dwFlags - Flags values
  801. * IN OUT pbData - Data to be decrypted
  802. * IN OUT pdwDataLen - Pointer to the length of the data to be
  803. * decrypted
  804. *
  805. * Returns:
  806. */
  807. LOGCSPAPI
  808. CPDecrypt(
  809. IN HCRYPTPROV hProv,
  810. IN HCRYPTKEY hKey,
  811. IN HCRYPTHASH hHash,
  812. IN BOOL Final,
  813. IN DWORD dwFlags,
  814. IN OUT BYTE *pbData,
  815. IN OUT DWORD *pdwDataLen)
  816. {
  817. BOOL fReturn;
  818. DWORD dwReturn;
  819. LogProvider *pProv = (LogProvider *)hProv;
  820. CLoggingContext *pCtx = pProv->pCtx;
  821. entrypoint
  822. dwReturn = pCtx->Decrypt(
  823. pProv->hProv,
  824. hKey,
  825. hHash,
  826. Final,
  827. dwFlags,
  828. pbData,
  829. pdwDataLen);
  830. if (ERROR_SUCCESS != dwReturn)
  831. {
  832. fReturn = FALSE;
  833. SetLastError(dwReturn);
  834. }
  835. else
  836. fReturn = TRUE;
  837. return fReturn;
  838. }
  839. /*
  840. - CPCreateHash
  841. -
  842. * Purpose:
  843. * initate the hashing of a stream of data
  844. *
  845. *
  846. * Parameters:
  847. * IN hUID - Handle to the user identifcation
  848. * IN Algid - Algorithm identifier of the hash algorithm
  849. * to be used
  850. * IN hKey - Optional key for MAC algorithms
  851. * IN dwFlags - Flags values
  852. * OUT pHash - Handle to hash object
  853. *
  854. * Returns:
  855. */
  856. LOGCSPAPI
  857. CPCreateHash(
  858. IN HCRYPTPROV hProv,
  859. IN ALG_ID Algid,
  860. IN HCRYPTKEY hKey,
  861. IN DWORD dwFlags,
  862. OUT HCRYPTHASH *phHash)
  863. {
  864. BOOL fReturn;
  865. DWORD dwReturn;
  866. LogProvider *pProv = (LogProvider *)hProv;
  867. CLoggingContext *pCtx = pProv->pCtx;
  868. entrypoint
  869. dwReturn = pCtx->CreateHash(
  870. pProv->hProv,
  871. Algid,
  872. hKey,
  873. dwFlags,
  874. phHash);
  875. if (ERROR_SUCCESS != dwReturn)
  876. {
  877. fReturn = FALSE;
  878. SetLastError(dwReturn);
  879. }
  880. else
  881. fReturn = TRUE;
  882. return fReturn;
  883. }
  884. /*
  885. - CPDestoryHash
  886. -
  887. * Purpose:
  888. * Destory the hash object
  889. *
  890. *
  891. * Parameters:
  892. * IN hProv - Handle to the user identifcation
  893. * IN hHash - Handle to hash object
  894. *
  895. * Returns:
  896. */
  897. LOGCSPAPI
  898. CPDestroyHash(
  899. IN HCRYPTPROV hProv,
  900. IN HCRYPTHASH hHash)
  901. {
  902. BOOL fReturn;
  903. DWORD dwReturn;
  904. LogProvider *pProv = (LogProvider *)hProv;
  905. CLoggingContext *pCtx = pProv->pCtx;
  906. entrypoint
  907. dwReturn = pCtx->DestroyHash(
  908. pProv->hProv,
  909. hHash);
  910. if (ERROR_SUCCESS != dwReturn)
  911. {
  912. fReturn = FALSE;
  913. SetLastError(dwReturn);
  914. }
  915. else
  916. fReturn = TRUE;
  917. return fReturn;
  918. }
  919. /*
  920. - CPGetHashParam
  921. -
  922. * Purpose:
  923. * Allows applications to get various aspects of the
  924. * operations of a hash
  925. *
  926. * Parameters:
  927. * IN hProv - Handle to a CSP
  928. * IN hHash - Handle to a hash
  929. * IN dwParam - Parameter number
  930. * OUT pbData - Pointer to data
  931. * IN pdwDataLen - Length of parameter data
  932. * IN dwFlags - Flags values
  933. *
  934. * Returns:
  935. */
  936. LOGCSPAPI
  937. CPGetHashParam(
  938. IN HCRYPTPROV hProv,
  939. IN HCRYPTHASH hHash,
  940. IN DWORD dwParam,
  941. OUT BYTE *pbData,
  942. IN OUT DWORD *pdwDataLen,
  943. IN DWORD dwFlags)
  944. {
  945. BOOL fReturn;
  946. DWORD dwReturn;
  947. LogProvider *pProv = (LogProvider *)hProv;
  948. CLoggingContext *pCtx = pProv->pCtx;
  949. entrypoint
  950. dwReturn = pCtx->GetHashParam(
  951. pProv->hProv,
  952. hHash,
  953. dwParam,
  954. pbData,
  955. pdwDataLen,
  956. dwFlags);
  957. if (ERROR_SUCCESS != dwReturn)
  958. {
  959. fReturn = FALSE;
  960. SetLastError(dwReturn);
  961. }
  962. else
  963. fReturn = TRUE;
  964. return fReturn;
  965. }
  966. /*
  967. - CPHashData
  968. -
  969. * Purpose:
  970. * Compute the cryptograghic hash on a stream of data
  971. *
  972. *
  973. * Parameters:
  974. * IN hProv - Handle to the user identifcation
  975. * IN hHash - Handle to hash object
  976. * IN pbData - Pointer to data to be hashed
  977. * IN dwDataLen - Length of the data to be hashed
  978. * IN dwFlags - Flags values
  979. * IN pdwMaxLen - Maximum length of the data stream the CSP
  980. * module may handle
  981. *
  982. * Returns:
  983. */
  984. LOGCSPAPI
  985. CPHashData(
  986. IN HCRYPTPROV hProv,
  987. IN HCRYPTHASH hHash,
  988. IN CONST BYTE *pbData,
  989. IN DWORD dwDataLen,
  990. IN DWORD dwFlags)
  991. {
  992. BOOL fReturn;
  993. DWORD dwReturn;
  994. LogProvider *pProv = (LogProvider *)hProv;
  995. CLoggingContext *pCtx = pProv->pCtx;
  996. entrypoint
  997. dwReturn = pCtx->HashData(
  998. pProv->hProv,
  999. hHash,
  1000. pbData,
  1001. dwDataLen,
  1002. dwFlags);
  1003. if (ERROR_SUCCESS != dwReturn)
  1004. {
  1005. fReturn = FALSE;
  1006. SetLastError(dwReturn);
  1007. }
  1008. else
  1009. fReturn = TRUE;
  1010. return fReturn;
  1011. }
  1012. /*
  1013. - CPHashSessionKey
  1014. -
  1015. * Purpose:
  1016. * Compute the cryptograghic hash on a key object.
  1017. *
  1018. *
  1019. * Parameters:
  1020. * IN hProv - Handle to the user identifcation
  1021. * IN hHash - Handle to hash object
  1022. * IN hKey - Handle to a key object
  1023. * IN dwFlags - Flags values
  1024. *
  1025. * Returns:
  1026. * CRYPT_FAILED
  1027. * CRYPT_SUCCEED
  1028. */
  1029. LOGCSPAPI
  1030. CPHashSessionKey(
  1031. IN HCRYPTPROV hProv,
  1032. IN HCRYPTHASH hHash,
  1033. IN HCRYPTKEY hKey,
  1034. IN DWORD dwFlags)
  1035. {
  1036. BOOL fReturn;
  1037. DWORD dwReturn;
  1038. LogProvider *pProv = (LogProvider *)hProv;
  1039. CLoggingContext *pCtx = pProv->pCtx;
  1040. entrypoint
  1041. dwReturn = pCtx->HashSessionKey(
  1042. pProv->hProv,
  1043. hHash,
  1044. hKey,
  1045. dwFlags);
  1046. if (ERROR_SUCCESS != dwReturn)
  1047. {
  1048. fReturn = FALSE;
  1049. SetLastError(dwReturn);
  1050. }
  1051. else
  1052. fReturn = TRUE;
  1053. return fReturn;
  1054. }
  1055. /*
  1056. - CPSetHashParam
  1057. -
  1058. * Purpose:
  1059. * Allows applications to customize various aspects of the
  1060. * operations of a hash
  1061. *
  1062. * Parameters:
  1063. * IN hProv - Handle to a CSP
  1064. * IN hHash - Handle to a hash
  1065. * IN dwParam - Parameter number
  1066. * IN pbData - Pointer to data
  1067. * IN dwFlags - Flags values
  1068. *
  1069. * Returns:
  1070. */
  1071. LOGCSPAPI
  1072. CPSetHashParam(
  1073. IN HCRYPTPROV hProv,
  1074. IN HCRYPTHASH hHash,
  1075. IN DWORD dwParam,
  1076. IN BYTE *pbData,
  1077. IN DWORD dwFlags)
  1078. {
  1079. BOOL fReturn;
  1080. DWORD dwReturn;
  1081. LogProvider *pProv = (LogProvider *)hProv;
  1082. CLoggingContext *pCtx = pProv->pCtx;
  1083. entrypoint
  1084. dwReturn = pCtx->SetHashParam(
  1085. pProv->hProv,
  1086. hHash,
  1087. dwParam,
  1088. pbData,
  1089. dwFlags);
  1090. if (ERROR_SUCCESS != dwReturn)
  1091. {
  1092. fReturn = FALSE;
  1093. SetLastError(dwReturn);
  1094. }
  1095. else
  1096. fReturn = TRUE;
  1097. return fReturn;
  1098. }
  1099. /*
  1100. - CPSignHash
  1101. -
  1102. * Purpose:
  1103. * Create a digital signature from a hash
  1104. *
  1105. *
  1106. * Parameters:
  1107. * IN hProv - Handle to the user identifcation
  1108. * IN hHash - Handle to hash object
  1109. * IN dwKeySpec - Key pair that is used to sign with
  1110. * IN sDescription - Description of data to be signed
  1111. * IN dwFlags - Flags values
  1112. * OUT pbSignture - Pointer to signature data
  1113. * IN OUT pdwSignLen- Pointer to the len of the signature data
  1114. *
  1115. * Returns:
  1116. */
  1117. LOGCSPAPI
  1118. CPSignHash(
  1119. IN HCRYPTPROV hProv,
  1120. IN HCRYPTHASH hHash,
  1121. IN DWORD dwKeySpec,
  1122. IN LPCTSTR sDescription,
  1123. IN DWORD dwFlags,
  1124. OUT BYTE *pbSignature,
  1125. IN OUT DWORD *pdwSigLen)
  1126. {
  1127. BOOL fReturn;
  1128. DWORD dwReturn;
  1129. LogProvider *pProv = (LogProvider *)hProv;
  1130. CLoggingContext *pCtx = pProv->pCtx;
  1131. entrypoint
  1132. dwReturn = pCtx->SignHash(
  1133. pProv->hProv,
  1134. hHash,
  1135. dwKeySpec,
  1136. sDescription,
  1137. dwFlags,
  1138. pbSignature,
  1139. pdwSigLen);
  1140. if (ERROR_SUCCESS != dwReturn)
  1141. {
  1142. fReturn = FALSE;
  1143. SetLastError(dwReturn);
  1144. }
  1145. else
  1146. fReturn = TRUE;
  1147. return fReturn;
  1148. }
  1149. /*
  1150. - CPVerifySignature
  1151. -
  1152. * Purpose:
  1153. * Used to verify a signature against a hash object
  1154. *
  1155. *
  1156. * Parameters:
  1157. * IN hProv - Handle to the user identifcation
  1158. * IN hHash - Handle to hash object
  1159. * IN pbSignture - Pointer to signature data
  1160. * IN dwSigLen - Length of the signature data
  1161. * IN hPubKey - Handle to the public key for verifying
  1162. * the signature
  1163. * IN sDescription - Description of data to be signed
  1164. * IN dwFlags - Flags values
  1165. *
  1166. * Returns:
  1167. */
  1168. LOGCSPAPI
  1169. CPVerifySignature(
  1170. IN HCRYPTPROV hProv,
  1171. IN HCRYPTHASH hHash,
  1172. IN CONST BYTE *pbSignature,
  1173. IN DWORD dwSigLen,
  1174. IN HCRYPTKEY hPubKey,
  1175. IN LPCTSTR sDescription,
  1176. IN DWORD dwFlags)
  1177. {
  1178. BOOL fReturn;
  1179. DWORD dwReturn;
  1180. LogProvider *pProv = (LogProvider *)hProv;
  1181. CLoggingContext *pCtx = pProv->pCtx;
  1182. entrypoint
  1183. dwReturn = pCtx->VerifySignature(
  1184. pProv->hProv,
  1185. hHash,
  1186. pbSignature,
  1187. dwSigLen,
  1188. hPubKey,
  1189. sDescription,
  1190. dwFlags);
  1191. if (ERROR_SUCCESS != dwReturn)
  1192. {
  1193. fReturn = FALSE;
  1194. SetLastError(dwReturn);
  1195. }
  1196. else
  1197. fReturn = TRUE;
  1198. return fReturn;
  1199. }
  1200. /*
  1201. - CPDuplicateHash
  1202. -
  1203. * Purpose:
  1204. * Duplicates the state of a hash and returns a handle to it
  1205. *
  1206. * Parameters:
  1207. * IN hUID - Handle to a CSP
  1208. * IN hHash - Handle to a hash
  1209. * IN pdwReserved - Reserved
  1210. * IN dwFlags - Flags
  1211. * IN phHash - Handle to the new hash
  1212. *
  1213. * Returns:
  1214. */
  1215. LOGCSPAPI
  1216. CPDuplicateHash(
  1217. IN HCRYPTPROV hProv,
  1218. IN HCRYPTHASH hHash,
  1219. IN DWORD *pdwReserved,
  1220. IN DWORD dwFlags,
  1221. IN HCRYPTHASH *phHash)
  1222. {
  1223. BOOL fReturn;
  1224. DWORD dwReturn;
  1225. LogProvider *pProv = (LogProvider *)hProv;
  1226. CLoggingContext *pCtx = pProv->pCtx;
  1227. entrypoint
  1228. dwReturn = pCtx->DuplicateHash(
  1229. pProv->hProv,
  1230. hHash,
  1231. pdwReserved,
  1232. dwFlags,
  1233. phHash);
  1234. if (ERROR_SUCCESS != dwReturn)
  1235. {
  1236. fReturn = FALSE;
  1237. SetLastError(dwReturn);
  1238. }
  1239. else
  1240. fReturn = TRUE;
  1241. return fReturn;
  1242. }
  1243. /*
  1244. - CPDuplicateKey
  1245. -
  1246. * Purpose:
  1247. * Duplicates the state of a key and returns a handle to it
  1248. *
  1249. * Parameters:
  1250. * IN hUID - Handle to a CSP
  1251. * IN hKey - Handle to a key
  1252. * IN pdwReserved - Reserved
  1253. * IN dwFlags - Flags
  1254. * IN phKey - Handle to the new key
  1255. *
  1256. * Returns:
  1257. */
  1258. LOGCSPAPI
  1259. CPDuplicateKey(
  1260. IN HCRYPTPROV hProv,
  1261. IN HCRYPTKEY hKey,
  1262. IN DWORD *pdwReserved,
  1263. IN DWORD dwFlags,
  1264. IN HCRYPTKEY *phKey)
  1265. {
  1266. BOOL fReturn;
  1267. DWORD dwReturn;
  1268. LogProvider *pProv = (LogProvider *)hProv;
  1269. CLoggingContext *pCtx = pProv->pCtx;
  1270. entrypoint
  1271. dwReturn = pCtx->DuplicateKey(
  1272. pProv->hProv,
  1273. hKey,
  1274. pdwReserved,
  1275. dwFlags,
  1276. phKey);
  1277. if (ERROR_SUCCESS != dwReturn)
  1278. {
  1279. fReturn = FALSE;
  1280. SetLastError(dwReturn);
  1281. }
  1282. else
  1283. fReturn = TRUE;
  1284. return fReturn;
  1285. }
  1286. /*++
  1287. DllMain:
  1288. This routine is called during DLL initialization. It collects any startup
  1289. and shutdown work that needs to be done. (Currently, none.)
  1290. Arguments:
  1291. hinstDLL - handle to the DLL module
  1292. fdwReason - reason for calling function
  1293. lpvReserved - reserved
  1294. Return Value:
  1295. ?return-value?
  1296. Remarks:
  1297. ?Remarks?
  1298. Author:
  1299. Doug Barlow (dbarlow) 4/9/2001
  1300. --*/
  1301. #undef __SUBROUTINE__
  1302. #define __SUBROUTINE__ TEXT("DllMain")
  1303. BOOL WINAPI
  1304. DllInitialize(
  1305. HINSTANCE hinstDLL,
  1306. DWORD fdwReason,
  1307. LPVOID lpvReserved)
  1308. {
  1309. BOOL fReturn = FALSE;
  1310. switch (fdwReason)
  1311. {
  1312. case DLL_PROCESS_ATTACH:
  1313. fReturn = TRUE;
  1314. break;
  1315. default:
  1316. fReturn = TRUE;
  1317. }
  1318. return fReturn;
  1319. }