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.

2924 lines
75 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1999
  3. Module Name:
  4. logctx
  5. Abstract:
  6. This module provides the implementation for the CLoggingContext object.
  7. Author:
  8. Doug Barlow (dbarlow) 12/7/1999
  9. Notes:
  10. ?Notes?
  11. --*/
  12. #ifndef WIN32_LEAN_AND_MEAN
  13. #define WIN32_LEAN_AND_MEAN
  14. #endif
  15. #include <windows.h>
  16. #include "logcsp.h"
  17. typedef enum {
  18. EndFlag = 0,
  19. AsnEncoding,
  20. AnsiString,
  21. UnicodeString,
  22. StructWithLength,
  23. SecDesc,
  24. Blob,
  25. Direct,
  26. Unknown // Must be last.
  27. } LengthEncoding;
  28. typedef struct {
  29. DWORD dwParamId;
  30. LengthEncoding leLengthType;
  31. DWORD cbLength;
  32. } LengthMap;
  33. static const LPCTSTR CPNames[]
  34. = { TEXT("CPAcquireContext"),
  35. TEXT("CPGetProvParam"),
  36. TEXT("CPReleaseContext"),
  37. TEXT("CPSetProvParam"),
  38. TEXT("CPDeriveKey"),
  39. TEXT("CPDestroyKey"),
  40. TEXT("CPExportKey"),
  41. TEXT("CPGenKey"),
  42. TEXT("CPGetKeyParam"),
  43. TEXT("CPGenRandom"),
  44. TEXT("CPGetUserKey"),
  45. TEXT("CPImportKey"),
  46. TEXT("CPSetKeyParam"),
  47. TEXT("CPEncrypt"),
  48. TEXT("CPDecrypt"),
  49. TEXT("CPCreateHash"),
  50. TEXT("CPDestroyHash"),
  51. TEXT("CPGetHashParam"),
  52. TEXT("CPHashData"),
  53. TEXT("CPHashSessionKey"),
  54. TEXT("CPSetHashParam"),
  55. TEXT("CPSignHash"),
  56. TEXT("CPVerifySignature"),
  57. TEXT("CPDuplicateHash"),
  58. TEXT("CPDuplicateKey"),
  59. NULL };
  60. static const LengthMap rglmProvParam[]
  61. = { { PP_CLIENT_HWND, Direct, sizeof(DWORD) },
  62. { PP_IMPTYPE, Direct, sizeof(DWORD) },
  63. { PP_NAME, AnsiString, 0 },
  64. { PP_VERSION, Direct, sizeof(DWORD) },
  65. { PP_CONTAINER, AnsiString, 0 },
  66. { PP_KEYSET_SEC_DESCR, SecDesc, 0 },
  67. { PP_CERTCHAIN, AsnEncoding, 0 },
  68. { PP_KEY_TYPE_SUBTYPE, Direct, sizeof(KEY_TYPE_SUBTYPE) },
  69. { PP_KEYEXCHANGE_KEYSIZE, Direct, sizeof(DWORD) },
  70. { PP_SIGNATURE_KEYSIZE, Direct, sizeof(DWORD) },
  71. { PP_KEYEXCHANGE_ALG, Direct, sizeof(DWORD) },
  72. { PP_SIGNATURE_ALG, Direct, sizeof(DWORD) },
  73. { PP_PROVTYPE, Direct, sizeof(DWORD) },
  74. { PP_SYM_KEYSIZE, Direct, sizeof(DWORD) },
  75. { PP_SESSION_KEYSIZE, Direct, sizeof(DWORD) },
  76. { PP_UI_PROMPT, UnicodeString, 0 },
  77. { PP_DELETEKEY, Direct, sizeof(DWORD) },
  78. { PP_ADMIN_PIN, AnsiString, 0 },
  79. { PP_KEYEXCHANGE_PIN, AnsiString, 0 },
  80. { PP_SIGNATURE_PIN, AnsiString, 0 },
  81. { PP_SIG_KEYSIZE_INC, Direct, sizeof(DWORD) },
  82. { PP_KEYX_KEYSIZE_INC, Direct, sizeof(DWORD) },
  83. { PP_SGC_INFO, Direct, sizeof(CERT_CONTEXT) }, // contains pointers
  84. { PP_USE_HARDWARE_RNG, Unknown, 0 }, // Nothing returned but status
  85. // { PP_ENUMEX_SIGNING_PROT, Unknown, 0 }, // Get only, zero length
  86. // { PP_KEYSPEC, Direct, sizeof(DWORD) }, // Get only
  87. // { PP_ENUMALGS Unknown, 0 }, // Get Only ENUMALGS structure
  88. // { PP_ENUMCONTAINERS AnsiString, 0 }, // Get Only
  89. // { PP_ENUMALGS_EX Unknown, 0 }, // Get Only ENUMALGSEX structure
  90. // { PP_KEYSTORAGE Direct, sizeof(DWORD) }, // Get Only
  91. // { PP_KEYSET_TYPE Direct, sizeof(DWORD) }, // Get Only
  92. // { PP_UNIQUE_CONTAINER AnsiString, 0 }, // Get Only
  93. // { PP_CHANGE_PASSWORD, Unknown, 0 }, // unused
  94. // { PP_CONTEXT_INFO, Unknown, 0 }, // unused
  95. // { PP_APPLI_CERT, Unknown, 0 }, // unused
  96. // { PP_ENUMMANDROOTS, Unknown, 0 }, // unused
  97. // { PP_ENUMELECTROOTS, Unknown, 0 }, // unused
  98. { 0, EndFlag, 0 } };
  99. static const LengthMap rglmKeyParam[]
  100. = { { KP_IV, Direct, 8 }, // RC2_BLOCKLEN
  101. { KP_SALT, Direct, 11 }, // 11 bytes in Base CSP, 0 bytes in Enh CSP
  102. { KP_PADDING, Direct, sizeof(DWORD) },
  103. { KP_MODE, Direct, sizeof(DWORD) },
  104. { KP_MODE_BITS, Direct, sizeof(DWORD) },
  105. { KP_PERMISSIONS, Direct, sizeof(DWORD) },
  106. { KP_ALGID, Direct, sizeof(DWORD) },
  107. { KP_BLOCKLEN, Direct, sizeof(DWORD) },
  108. { KP_KEYLEN, Direct, sizeof(DWORD) },
  109. { KP_SALT_EX, Blob, 0 },
  110. { KP_P, Blob, 0 },
  111. { KP_G, Blob, 0 },
  112. { KP_Q, Blob, 0 },
  113. { KP_X, Unknown, 0 }, // Must be NULL.
  114. { KP_EFFECTIVE_KEYLEN, Direct, sizeof(DWORD) },
  115. { KP_SCHANNEL_ALG, Direct, sizeof(SCHANNEL_ALG) },
  116. { KP_CLIENT_RANDOM, Blob, 0 },
  117. { KP_SERVER_RANDOM, Blob, 0 },
  118. { KP_CERTIFICATE, AsnEncoding, 0 },
  119. { KP_CLEAR_KEY, Blob, 0 },
  120. { KP_KEYVAL, Unknown, 0 }, // (aka KP_Z) length of key
  121. { KP_ADMIN_PIN, AnsiString, 0 },
  122. { KP_KEYEXCHANGE_PIN, AnsiString, 0 },
  123. { KP_SIGNATURE_PIN, AnsiString, 0 },
  124. { KP_OAEP_PARAMS, Blob, 0 },
  125. { KP_CMS_DH_KEY_INFO, Direct, sizeof(CMS_DH_KEY_INFO) }, //contains pointers
  126. { KP_PUB_PARAMS, Blob, 0 },
  127. { KP_HIGHEST_VERSION, Direct, sizeof(DWORD) },
  128. // { KP_VERIFY_PARAMS, Unknown, 0 }, // Get only, returns empty string w/ status
  129. // { KP_Y, Unknown, 0 }, // Unused
  130. // { KP_RA, Unknown, 0 }, // Unused
  131. // { KP_RB, Unknown, 0 }, // Unused
  132. // { KP_INFO, Unknown, 0 }, // Unused
  133. // { KP_RP, Unknown, 0 }, // Unused
  134. // { KP_PRECOMP_MD5, Unknown, 0 }, // Unused
  135. // { KP_PRECOMP_SHA, Unknown, 0 }, // Unused
  136. // { KP_PUB_EX_LEN, Unknown, 0 }, // Unused
  137. // { KP_PUB_EX_VAL, Unknown, 0 }, // Unused
  138. // { KP_PREHASH, Unknown, 0 }, // Unused
  139. // { KP_CMS_KEY_INFO, Unknown, 0 }, // Unused CMS_KEY_INFO structure
  140. { 0, EndFlag, 0 } };
  141. static const LengthMap rglmHashParam[]
  142. = { { HP_ALGID, Direct, sizeof(DWORD) },
  143. { HP_HASHVAL, Direct, 20 }, // (A_SHA_DIGEST_LEN) Length of hash
  144. { HP_HASHSIZE, Direct, sizeof(DWORD) },
  145. { HP_HMAC_INFO, Direct, sizeof(HMAC_INFO) }, // contains pointers
  146. { HP_TLS1PRF_LABEL, Blob, 0 },
  147. { HP_TLS1PRF_SEED, Blob, 0 },
  148. { 0, EndFlag, 0 } };
  149. const LPCTSTR
  150. g_szCspRegistry
  151. = TEXT("SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider"),
  152. g_szSignature = TEXT("Signature"),
  153. g_szImagePath = TEXT("Image Path"),
  154. g_szSigInFile = TEXT("SigInFile"),
  155. g_szType = TEXT("Type");
  156. const LPCTSTR
  157. g_szLogCspRegistry
  158. = TEXT("SOFTWARE\\Microsoft\\Cryptography\\CSPDK\\Logging Crypto Provider"),
  159. g_szLogFile = TEXT("Logging File"),
  160. g_szSavedImagePath = TEXT("Logging Image Path"),
  161. g_szSavedSignature = TEXT("Logging Signature"),
  162. g_szSavedSigInFile = TEXT("Logging SigInFile");
  163. const LPCTSTR
  164. g_szLogCsp = TEXT("LogCsp.dll");
  165. const LPCTSTR
  166. g_szCspDkRegistry
  167. = TEXT("SOFTWARE\\Microsoft\\Cryptography\\CSPDK\\Certificates");
  168. static DWORD
  169. MapLength(
  170. const LengthMap *rglmParamId,
  171. DWORD dwParam,
  172. LPCBYTE *ppbData,
  173. DWORD dwFlags);
  174. static DWORD
  175. ExtractTag(
  176. IN const BYTE *pbSrc,
  177. OUT LPDWORD pdwTag,
  178. OUT LPBOOL pfConstr);
  179. static DWORD
  180. ExtractLength(
  181. IN const BYTE *pbSrc,
  182. OUT LPDWORD pdwLen,
  183. OUT LPBOOL pfIndefinite);
  184. static DWORD
  185. Asn1Length(
  186. IN LPCBYTE pbAsn1);
  187. /*++
  188. CONSTRUCTOR:
  189. The constructor for this object simply initializes the properties to a
  190. known state. Use the Initialize member to actually build the object.
  191. Arguments:
  192. None
  193. Remarks:
  194. ?Remarks?
  195. Author:
  196. Doug Barlow (dbarlow) 12/7/1999
  197. --*/
  198. #undef __SUBROUTINE__
  199. #define __SUBROUTINE__ TEXT("CLoggingContext::CLoggingContext")
  200. CLoggingContext::CLoggingContext(
  201. void)
  202. : m_tzCspImage(),
  203. m_tzLogFile()
  204. {
  205. m_nRefCount = 1;
  206. m_hModule = NULL;
  207. ZeroMemory(&m_cspRedirect, sizeof(CSP_REDIRECT));
  208. }
  209. /*++
  210. DESTRUCTOR:
  211. The destructor for this object cleans up everything it can without
  212. generating an error.
  213. Remarks:
  214. ?Remarks?
  215. Author:
  216. Doug Barlow (dbarlow) 12/7/1999
  217. --*/
  218. #undef __SUBROUTINE__
  219. #define __SUBROUTINE__ TEXT("CLoggingContext::~CLoggingContext")
  220. CLoggingContext::~CLoggingContext()
  221. {
  222. g_prgCtxs->Set(m_dwIndex, NULL);
  223. if (NULL != m_hModule)
  224. FreeLibrary(m_hModule);
  225. }
  226. /*++
  227. Initialize:
  228. This function actually does the work of loading the target CSP.
  229. Arguments:
  230. pVTable supplies the VTable structure from the controlling ADVAPI32.dll.
  231. Return Value:
  232. ?return-value?
  233. Remarks:
  234. ?Remarks?
  235. Author:
  236. Doug Barlow (dbarlow) 12/7/1999
  237. --*/
  238. #undef __SUBROUTINE__
  239. #define __SUBROUTINE__ TEXT("CLoggingContext::Initialize")
  240. DWORD
  241. CLoggingContext::Initialize(
  242. IN PVTableProvStruc pVTable,
  243. IN CRegistry &regRoot)
  244. {
  245. DWORD dwSts;
  246. DWORD dwReturn;
  247. BOOL fVerified = FALSE;
  248. const LPCTSTR *psz;
  249. FARPROC *pf;
  250. //
  251. // Replace the system image validation function with ours.
  252. //
  253. pVTable->FuncVerifyImage = CspdkVerifyImage;
  254. //
  255. // regRoot provides a handle to to a point in the registry from
  256. // which we can read additional parameters. First, get the dll to be
  257. // loaded.
  258. //
  259. try
  260. {
  261. m_tzCspImage.Copy(regRoot.GetStringValue(g_szSavedImagePath));
  262. }
  263. catch (...)
  264. {
  265. dwReturn = ERROR_SERVICE_NOT_FOUND;
  266. goto ErrorExit;
  267. }
  268. //
  269. // Next get the Log File Name for this CSP. If there isn't one, we still
  270. // load the CSP, but we don't do logging.
  271. //
  272. try
  273. {
  274. if (regRoot.ValueExists(g_szLogFile))
  275. m_tzLogFile.Copy(regRoot.GetStringValue(g_szLogFile));
  276. }
  277. catch (...)
  278. {
  279. dwReturn = NTE_NO_MEMORY;
  280. goto ErrorExit;
  281. }
  282. //
  283. // Verify the signature of the proposed image. First, see if there's
  284. // a signature in the registry.
  285. //
  286. if (regRoot.ValueExists(g_szSavedSignature))
  287. {
  288. try
  289. {
  290. LPCBYTE pbSig = regRoot.GetBinaryValue(g_szSavedSignature);
  291. fVerified = CspdkVerifyImage(m_tzCspImage, pbSig);
  292. }
  293. catch (...)
  294. {
  295. dwReturn = NTE_NO_MEMORY;
  296. goto ErrorExit;
  297. }
  298. }
  299. //
  300. // If that didn't work, see if there's a signature in the file.
  301. //
  302. if (!fVerified)
  303. fVerified = CspdkVerifyImage(m_tzCspImage, NULL);
  304. //
  305. // We're out of options. If it hasn't verified by now, give up.
  306. //
  307. if (!fVerified)
  308. {
  309. dwReturn = NTE_BAD_SIGNATURE;
  310. goto ErrorExit;
  311. }
  312. //
  313. // The image has passed signature checks. Now load the image.
  314. //
  315. pf = (FARPROC *)&m_cspRedirect.pfAcquireContext;
  316. m_hModule = LoadLibrary(m_tzCspImage);
  317. if (NULL == m_hModule)
  318. {
  319. dwSts = GetLastError();
  320. goto ErrorExit;
  321. }
  322. for (psz = CPNames; NULL != *psz; psz += 1)
  323. {
  324. *pf = GetProcAddress(m_hModule, *psz);
  325. pf += 1;
  326. }
  327. return ERROR_SUCCESS;
  328. ErrorExit:
  329. return dwReturn;
  330. }
  331. /*++
  332. CLoggingContext::AddRef:
  333. Add a new reference to this object.
  334. Arguments:
  335. None
  336. Return Value:
  337. A pointer to this object.
  338. Author:
  339. Doug Barlow (dbarlow) 12/11/1999
  340. --*/
  341. #undef __SUBROUTINE__
  342. #define __SUBROUTINE__ TEXT("CLoggingContext::AddRef")
  343. CLoggingContext *
  344. CLoggingContext::AddRef(
  345. void)
  346. {
  347. m_nRefCount += 1;
  348. return this;
  349. }
  350. /*++
  351. CLoggingContext::Release:
  352. This routine decrements the number of references to this object. If there
  353. are no more references to this object, it deletes itself.
  354. Arguments:
  355. None
  356. Return Value:
  357. None
  358. Author:
  359. Doug Barlow (dbarlow) 12/11/1999
  360. --*/
  361. #undef __SUBROUTINE__
  362. #define __SUBROUTINE__ TEXT("CLoggingContext::Release")
  363. void
  364. CLoggingContext::Release(
  365. void)
  366. {
  367. if (0 == --m_nRefCount)
  368. delete this;
  369. }
  370. /*
  371. - CPAcquireContext
  372. -
  373. * Purpose:
  374. * The CPAcquireContext function is used to acquire a context
  375. * handle to a cryptograghic service provider (CSP).
  376. *
  377. *
  378. * Parameters:
  379. * IN pszContainer - Pointer to a string of key container
  380. * IN dwFlags - Flags values
  381. * IN pVTable - Pointer to table of function pointers
  382. *
  383. * Returns:
  384. */
  385. #undef __SUBROUTINE__
  386. #define __SUBROUTINE__ TEXT("CLoggingContext::AcquireContext")
  387. DWORD
  388. CLoggingContext::AcquireContext(
  389. OUT HCRYPTPROV *phProv,
  390. IN LPCTSTR pszContainer,
  391. IN DWORD dwFlags,
  392. IN PVTableProvStruc pVTable)
  393. {
  394. BOOL fReturn;
  395. DWORD dwReturn;
  396. {
  397. CLogAcquireContext logObj;
  398. logObj.Request(phProv,
  399. pszContainer,
  400. dwFlags,
  401. pVTable);
  402. if (NULL != m_cspRedirect.pfAcquireContext)
  403. {
  404. try
  405. {
  406. fReturn = (*m_cspRedirect.pfAcquireContext)(
  407. phProv,
  408. pszContainer,
  409. dwFlags,
  410. pVTable);
  411. dwReturn = GetLastError();
  412. logObj.Response(
  413. fReturn,
  414. phProv,
  415. pszContainer,
  416. dwFlags,
  417. pVTable);
  418. }
  419. catch (...)
  420. {
  421. logObj.LogException();
  422. fReturn = FALSE;
  423. dwReturn = ERROR_ARENA_TRASHED;
  424. }
  425. }
  426. else
  427. {
  428. fReturn = FALSE;
  429. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  430. logObj.LogNotCalled(dwReturn);
  431. }
  432. logObj.Log(m_tzLogFile);
  433. }
  434. if (!fReturn)
  435. {
  436. if (ERROR_SUCCESS == dwReturn)
  437. dwReturn = ERROR_DISCARDED;
  438. }
  439. else
  440. dwReturn = ERROR_SUCCESS;
  441. return dwReturn;
  442. }
  443. /*
  444. - CPGetProvParam
  445. -
  446. * Purpose:
  447. * Allows applications to get various aspects of the
  448. * operations of a provider
  449. *
  450. * Parameters:
  451. * IN hProv - Handle to a CSP
  452. * IN dwParam - Parameter number
  453. * IN pbData - Pointer to data
  454. * IN OUT pdwDataLen - Length of parameter data
  455. * IN dwFlags - Flags values
  456. *
  457. * Returns:
  458. */
  459. #undef __SUBROUTINE__
  460. #define __SUBROUTINE__ TEXT("CLoggingContext::GetProvParam")
  461. DWORD
  462. CLoggingContext::GetProvParam(
  463. IN HCRYPTPROV hProv,
  464. IN DWORD dwParam,
  465. OUT BYTE *pbData,
  466. IN OUT DWORD *pdwDataLen,
  467. IN DWORD dwFlags)
  468. {
  469. BOOL fReturn;
  470. DWORD dwReturn;
  471. {
  472. CLogGetProvParam logObj;
  473. logObj.Request(
  474. hProv,
  475. dwParam,
  476. pbData,
  477. pdwDataLen,
  478. dwFlags);
  479. if (NULL != m_cspRedirect.pfGetProvParam)
  480. {
  481. try
  482. {
  483. fReturn = (*m_cspRedirect.pfGetProvParam)(
  484. hProv,
  485. dwParam,
  486. pbData,
  487. pdwDataLen,
  488. dwFlags);
  489. dwReturn = GetLastError();
  490. logObj.Response(
  491. fReturn,
  492. hProv,
  493. dwParam,
  494. pbData,
  495. pdwDataLen,
  496. dwFlags);
  497. }
  498. catch (...)
  499. {
  500. logObj.LogException();
  501. fReturn = FALSE;
  502. dwReturn = ERROR_ARENA_TRASHED;
  503. }
  504. }
  505. else
  506. {
  507. fReturn = FALSE;
  508. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  509. logObj.LogNotCalled(dwReturn);
  510. }
  511. logObj.Log(m_tzLogFile);
  512. }
  513. if (!fReturn)
  514. {
  515. if (ERROR_SUCCESS == dwReturn)
  516. dwReturn = ERROR_DISCARDED;
  517. }
  518. else
  519. dwReturn = ERROR_SUCCESS;
  520. return dwReturn;
  521. }
  522. /*
  523. - CPReleaseContext
  524. -
  525. * Purpose:
  526. * The CPReleaseContext function is used to release a
  527. * context created by CrytAcquireContext.
  528. *
  529. * Parameters:
  530. * IN phProv - Handle to a CSP
  531. * IN dwFlags - Flags values
  532. *
  533. * Returns:
  534. */
  535. #undef __SUBROUTINE__
  536. #define __SUBROUTINE__ TEXT("CLoggingContext::ReleaseContext")
  537. DWORD
  538. CLoggingContext::ReleaseContext(
  539. IN HCRYPTPROV hProv,
  540. IN DWORD dwFlags)
  541. {
  542. BOOL fReturn;
  543. DWORD dwReturn;
  544. {
  545. CLogReleaseContext logObj;
  546. logObj.Request(
  547. hProv,
  548. dwFlags);
  549. if (NULL != m_cspRedirect.pfReleaseContext)
  550. {
  551. try
  552. {
  553. fReturn = (*m_cspRedirect.pfReleaseContext)(
  554. hProv,
  555. dwFlags);
  556. dwReturn = GetLastError();
  557. logObj.Response(
  558. fReturn,
  559. hProv,
  560. dwFlags);
  561. }
  562. catch (...)
  563. {
  564. logObj.LogException();
  565. fReturn = FALSE;
  566. dwReturn = ERROR_ARENA_TRASHED;
  567. }
  568. }
  569. else
  570. {
  571. fReturn = FALSE;
  572. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  573. logObj.LogNotCalled(dwReturn);
  574. }
  575. logObj.Log(m_tzLogFile);
  576. }
  577. if (!fReturn)
  578. {
  579. if (ERROR_SUCCESS == dwReturn)
  580. dwReturn = ERROR_DISCARDED;
  581. }
  582. else
  583. dwReturn = ERROR_SUCCESS;
  584. return dwReturn;
  585. }
  586. /*
  587. - CPSetProvParam
  588. -
  589. * Purpose:
  590. * Allows applications to customize various aspects of the
  591. * operations of a provider
  592. *
  593. * Parameters:
  594. * IN hProv - Handle to a CSP
  595. * IN dwParam - Parameter number
  596. * IN pbData - Pointer to data
  597. * IN dwFlags - Flags values
  598. *
  599. * Returns:
  600. */
  601. #undef __SUBROUTINE__
  602. #define __SUBROUTINE__ TEXT("CLoggingContext::SetProvParam")
  603. DWORD
  604. CLoggingContext::SetProvParam(
  605. IN HCRYPTPROV hProv,
  606. IN DWORD dwParam,
  607. IN CONST BYTE *pbData,
  608. IN DWORD dwFlags)
  609. {
  610. BOOL fReturn;
  611. DWORD dwReturn;
  612. {
  613. CLogSetProvParam logObj;
  614. DWORD dwLength;
  615. CONST BYTE *pbRealData = pbData;
  616. dwLength = MapLength(rglmProvParam, dwParam, &pbRealData, dwFlags);
  617. logObj.Request(
  618. hProv,
  619. dwParam,
  620. pbRealData,
  621. dwLength,
  622. dwFlags);
  623. if (NULL != m_cspRedirect.pfSetProvParam)
  624. {
  625. try
  626. {
  627. fReturn = (*m_cspRedirect.pfSetProvParam)(
  628. hProv,
  629. dwParam,
  630. pbData,
  631. dwFlags);
  632. dwReturn = GetLastError();
  633. logObj.Response(
  634. fReturn,
  635. hProv,
  636. dwParam,
  637. pbRealData,
  638. dwFlags);
  639. }
  640. catch (...)
  641. {
  642. logObj.LogException();
  643. fReturn = FALSE;
  644. dwReturn = ERROR_ARENA_TRASHED;
  645. }
  646. }
  647. else
  648. {
  649. fReturn = FALSE;
  650. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  651. logObj.LogNotCalled(dwReturn);
  652. }
  653. logObj.Log(m_tzLogFile);
  654. }
  655. if (!fReturn)
  656. {
  657. if (ERROR_SUCCESS == dwReturn)
  658. dwReturn = ERROR_DISCARDED;
  659. }
  660. else
  661. dwReturn = ERROR_SUCCESS;
  662. return dwReturn;
  663. }
  664. /*
  665. - CPDeriveKey
  666. -
  667. * Purpose:
  668. * Derive cryptographic keys from base data
  669. *
  670. *
  671. * Parameters:
  672. * IN hProv - Handle to a CSP
  673. * IN Algid - Algorithm identifier
  674. * IN hHash - Handle to hash
  675. * IN dwFlags - Flags values
  676. * OUT phKey - Handle to a generated key
  677. *
  678. * Returns:
  679. */
  680. #undef __SUBROUTINE__
  681. #define __SUBROUTINE__ TEXT("CLoggingContext::DeriveKey")
  682. DWORD
  683. CLoggingContext::DeriveKey(
  684. IN HCRYPTPROV hProv,
  685. IN ALG_ID Algid,
  686. IN HCRYPTHASH hHash,
  687. IN DWORD dwFlags,
  688. OUT HCRYPTKEY * phKey)
  689. {
  690. BOOL fReturn;
  691. DWORD dwReturn;
  692. {
  693. CLogDeriveKey logObj;
  694. logObj.Request(
  695. hProv,
  696. Algid,
  697. hHash,
  698. dwFlags,
  699. phKey);
  700. if (NULL != m_cspRedirect.pfDeriveKey)
  701. {
  702. try
  703. {
  704. fReturn = (*m_cspRedirect.pfDeriveKey)(
  705. hProv,
  706. Algid,
  707. hHash,
  708. dwFlags,
  709. phKey);
  710. dwReturn = GetLastError();
  711. logObj.Response(
  712. fReturn,
  713. hProv,
  714. Algid,
  715. hHash,
  716. dwFlags,
  717. phKey);
  718. }
  719. catch (...)
  720. {
  721. logObj.LogException();
  722. fReturn = FALSE;
  723. dwReturn = ERROR_ARENA_TRASHED;
  724. }
  725. }
  726. else
  727. {
  728. fReturn = FALSE;
  729. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  730. logObj.LogNotCalled(dwReturn);
  731. }
  732. logObj.Log(m_tzLogFile);
  733. }
  734. if (!fReturn)
  735. {
  736. if (ERROR_SUCCESS == dwReturn)
  737. dwReturn = ERROR_DISCARDED;
  738. }
  739. else
  740. dwReturn = ERROR_SUCCESS;
  741. return dwReturn;
  742. }
  743. /*
  744. - CPDestroyKey
  745. -
  746. * Purpose:
  747. * Destroys the cryptographic key that is being referenced
  748. * with the hKey parameter
  749. *
  750. *
  751. * Parameters:
  752. * IN hProv - Handle to a CSP
  753. * IN hKey - Handle to a key
  754. *
  755. * Returns:
  756. */
  757. #undef __SUBROUTINE__
  758. #define __SUBROUTINE__ TEXT("CLoggingContext::DestroyKey")
  759. DWORD
  760. CLoggingContext::DestroyKey(
  761. IN HCRYPTPROV hProv,
  762. IN HCRYPTKEY hKey)
  763. {
  764. BOOL fReturn;
  765. DWORD dwReturn;
  766. {
  767. CLogDestroyKey logObj;
  768. logObj.Request(
  769. hProv,
  770. hKey);
  771. if (NULL != m_cspRedirect.pfDestroyKey)
  772. {
  773. try
  774. {
  775. fReturn = (*m_cspRedirect.pfDestroyKey)(
  776. hProv,
  777. hKey);
  778. dwReturn = GetLastError();
  779. logObj.Response(
  780. fReturn,
  781. hProv,
  782. hKey);
  783. }
  784. catch (...)
  785. {
  786. logObj.LogException();
  787. fReturn = FALSE;
  788. dwReturn = ERROR_ARENA_TRASHED;
  789. }
  790. }
  791. else
  792. {
  793. fReturn = FALSE;
  794. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  795. logObj.LogNotCalled(dwReturn);
  796. }
  797. logObj.Log(m_tzLogFile);
  798. }
  799. if (!fReturn)
  800. {
  801. if (ERROR_SUCCESS == dwReturn)
  802. dwReturn = ERROR_DISCARDED;
  803. }
  804. else
  805. dwReturn = ERROR_SUCCESS;
  806. return dwReturn;
  807. }
  808. /*
  809. - CPExportKey
  810. -
  811. * Purpose:
  812. * Export cryptographic keys out of a CSP in a secure manner
  813. *
  814. *
  815. * Parameters:
  816. * IN hProv - Handle to the CSP user
  817. * IN hKey - Handle to the key to export
  818. * IN hPubKey - Handle to the exchange public key value of
  819. * the destination user
  820. * IN dwBlobType - Type of key blob to be exported
  821. * IN dwFlags - Flags values
  822. * OUT pbData - Key blob data
  823. * IN OUT pdwDataLen - Length of key blob in bytes
  824. *
  825. * Returns:
  826. */
  827. #undef __SUBROUTINE__
  828. #define __SUBROUTINE__ TEXT("CLoggingContext::ExportKey")
  829. DWORD
  830. CLoggingContext::ExportKey(
  831. IN HCRYPTPROV hProv,
  832. IN HCRYPTKEY hKey,
  833. IN HCRYPTKEY hPubKey,
  834. IN DWORD dwBlobType,
  835. IN DWORD dwFlags,
  836. OUT BYTE *pbData,
  837. IN OUT DWORD *pdwDataLen)
  838. {
  839. BOOL fReturn;
  840. DWORD dwReturn;
  841. {
  842. CLogExportKey logObj;
  843. logObj.Request(
  844. hProv,
  845. hKey,
  846. hPubKey,
  847. dwBlobType,
  848. dwFlags,
  849. pbData,
  850. pdwDataLen);
  851. if (NULL != m_cspRedirect.pfExportKey)
  852. {
  853. try
  854. {
  855. fReturn = (*m_cspRedirect.pfExportKey)(
  856. hProv,
  857. hKey,
  858. hPubKey,
  859. dwBlobType,
  860. dwFlags,
  861. pbData,
  862. pdwDataLen);
  863. dwReturn = GetLastError();
  864. logObj.Response(
  865. fReturn,
  866. hProv,
  867. hKey,
  868. hPubKey,
  869. dwBlobType,
  870. dwFlags,
  871. pbData,
  872. pdwDataLen);
  873. }
  874. catch (...)
  875. {
  876. logObj.LogException();
  877. fReturn = FALSE;
  878. dwReturn = ERROR_ARENA_TRASHED;
  879. }
  880. }
  881. else
  882. {
  883. fReturn = FALSE;
  884. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  885. logObj.LogNotCalled(dwReturn);
  886. }
  887. logObj.Log(m_tzLogFile);
  888. }
  889. if (!fReturn)
  890. {
  891. if (ERROR_SUCCESS == dwReturn)
  892. dwReturn = ERROR_DISCARDED;
  893. }
  894. else
  895. dwReturn = ERROR_SUCCESS;
  896. return dwReturn;
  897. }
  898. /*
  899. - CPGenKey
  900. -
  901. * Purpose:
  902. * Generate cryptographic keys
  903. *
  904. *
  905. * Parameters:
  906. * IN hProv - Handle to a CSP
  907. * IN Algid - Algorithm identifier
  908. * IN dwFlags - Flags values
  909. * OUT phKey - Handle to a generated key
  910. *
  911. * Returns:
  912. */
  913. #undef __SUBROUTINE__
  914. #define __SUBROUTINE__ TEXT("CLoggingContext::GenKey")
  915. DWORD
  916. CLoggingContext::GenKey(
  917. IN HCRYPTPROV hProv,
  918. IN ALG_ID Algid,
  919. IN DWORD dwFlags,
  920. OUT HCRYPTKEY *phKey)
  921. {
  922. BOOL fReturn;
  923. DWORD dwReturn;
  924. {
  925. CLogGenKey logObj;
  926. logObj.Request(
  927. hProv,
  928. Algid,
  929. dwFlags,
  930. phKey);
  931. if (NULL != m_cspRedirect.pfGenKey)
  932. {
  933. try
  934. {
  935. fReturn = (*m_cspRedirect.pfGenKey)(
  936. hProv,
  937. Algid,
  938. dwFlags,
  939. phKey);
  940. dwReturn = GetLastError();
  941. logObj.Response(
  942. fReturn,
  943. hProv,
  944. Algid,
  945. dwFlags,
  946. phKey);
  947. }
  948. catch (...)
  949. {
  950. logObj.LogException();
  951. fReturn = FALSE;
  952. dwReturn = ERROR_ARENA_TRASHED;
  953. }
  954. }
  955. else
  956. {
  957. fReturn = FALSE;
  958. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  959. logObj.LogNotCalled(dwReturn);
  960. }
  961. logObj.Log(m_tzLogFile);
  962. }
  963. if (!fReturn)
  964. {
  965. if (ERROR_SUCCESS == dwReturn)
  966. dwReturn = ERROR_DISCARDED;
  967. }
  968. else
  969. dwReturn = ERROR_SUCCESS;
  970. return dwReturn;
  971. }
  972. /*
  973. - CPGetKeyParam
  974. -
  975. * Purpose:
  976. * Allows applications to get various aspects of the
  977. * operations of a key
  978. *
  979. * Parameters:
  980. * IN hProv - Handle to a CSP
  981. * IN hKey - Handle to a key
  982. * IN dwParam - Parameter number
  983. * OUT pbData - Pointer to data
  984. * IN pdwDataLen - Length of parameter data
  985. * IN dwFlags - Flags values
  986. *
  987. * Returns:
  988. */
  989. #undef __SUBROUTINE__
  990. #define __SUBROUTINE__ TEXT("CLoggingContext::GetKeyParam")
  991. DWORD
  992. CLoggingContext::GetKeyParam(
  993. IN HCRYPTPROV hProv,
  994. IN HCRYPTKEY hKey,
  995. IN DWORD dwParam,
  996. OUT BYTE *pbData,
  997. IN OUT DWORD *pdwDataLen,
  998. IN DWORD dwFlags)
  999. {
  1000. BOOL fReturn;
  1001. DWORD dwReturn;
  1002. {
  1003. CLogGetKeyParam logObj;
  1004. logObj.Request(
  1005. hProv,
  1006. hKey,
  1007. dwParam,
  1008. pbData,
  1009. pdwDataLen,
  1010. dwFlags);
  1011. if (NULL != m_cspRedirect.pfGetKeyParam)
  1012. {
  1013. try
  1014. {
  1015. fReturn = (*m_cspRedirect.pfGetKeyParam)(
  1016. hProv,
  1017. hKey,
  1018. dwParam,
  1019. pbData,
  1020. pdwDataLen,
  1021. dwFlags);
  1022. dwReturn = GetLastError();
  1023. logObj.Response(
  1024. fReturn,
  1025. hProv,
  1026. hKey,
  1027. dwParam,
  1028. pbData,
  1029. pdwDataLen,
  1030. dwFlags);
  1031. }
  1032. catch (...)
  1033. {
  1034. logObj.LogException();
  1035. fReturn = FALSE;
  1036. dwReturn = ERROR_ARENA_TRASHED;
  1037. }
  1038. }
  1039. else
  1040. {
  1041. fReturn = FALSE;
  1042. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1043. logObj.LogNotCalled(dwReturn);
  1044. }
  1045. logObj.Log(m_tzLogFile);
  1046. }
  1047. if (!fReturn)
  1048. {
  1049. if (ERROR_SUCCESS == dwReturn)
  1050. dwReturn = ERROR_DISCARDED;
  1051. }
  1052. else
  1053. dwReturn = ERROR_SUCCESS;
  1054. return dwReturn;
  1055. }
  1056. /*
  1057. - CPGenRandom
  1058. -
  1059. * Purpose:
  1060. * Used to fill a buffer with random bytes
  1061. *
  1062. *
  1063. * Parameters:
  1064. * IN hProv - Handle to the user identifcation
  1065. * IN dwLen - Number of bytes of random data requested
  1066. * IN OUT pbBuffer- Pointer to the buffer where the random
  1067. * bytes are to be placed
  1068. *
  1069. * Returns:
  1070. */
  1071. #undef __SUBROUTINE__
  1072. #define __SUBROUTINE__ TEXT("CLoggingContext::GenRandom")
  1073. DWORD
  1074. CLoggingContext::GenRandom(
  1075. IN HCRYPTPROV hProv,
  1076. IN DWORD dwLen,
  1077. IN OUT BYTE *pbBuffer)
  1078. {
  1079. BOOL fReturn;
  1080. DWORD dwReturn;
  1081. {
  1082. CLogGenRandom logObj;
  1083. logObj.Request(
  1084. hProv,
  1085. dwLen,
  1086. pbBuffer);
  1087. if (NULL != m_cspRedirect.pfGenRandom)
  1088. {
  1089. try
  1090. {
  1091. fReturn = (*m_cspRedirect.pfGenRandom)(
  1092. hProv,
  1093. dwLen,
  1094. pbBuffer);
  1095. dwReturn = GetLastError();
  1096. logObj.Response(
  1097. fReturn,
  1098. hProv,
  1099. dwLen,
  1100. pbBuffer);
  1101. }
  1102. catch (...)
  1103. {
  1104. logObj.LogException();
  1105. fReturn = FALSE;
  1106. dwReturn = ERROR_ARENA_TRASHED;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. fReturn = FALSE;
  1112. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1113. logObj.LogNotCalled(dwReturn);
  1114. }
  1115. logObj.Log(m_tzLogFile);
  1116. }
  1117. if (!fReturn)
  1118. {
  1119. if (ERROR_SUCCESS == dwReturn)
  1120. dwReturn = ERROR_DISCARDED;
  1121. }
  1122. else
  1123. dwReturn = ERROR_SUCCESS;
  1124. return dwReturn;
  1125. }
  1126. /*
  1127. - CPGetUserKey
  1128. -
  1129. * Purpose:
  1130. * Gets a handle to a permanent user key
  1131. *
  1132. *
  1133. * Parameters:
  1134. * IN hProv - Handle to the user identifcation
  1135. * IN dwKeySpec - Specification of the key to retrieve
  1136. * OUT phUserKey - Pointer to key handle of retrieved key
  1137. *
  1138. * Returns:
  1139. */
  1140. #undef __SUBROUTINE__
  1141. #define __SUBROUTINE__ TEXT("CLoggingContext::GetUserKey")
  1142. DWORD
  1143. CLoggingContext::GetUserKey(
  1144. IN HCRYPTPROV hProv,
  1145. IN DWORD dwKeySpec,
  1146. OUT HCRYPTKEY *phUserKey)
  1147. {
  1148. BOOL fReturn;
  1149. DWORD dwReturn;
  1150. {
  1151. CLogGetUserKey logObj;
  1152. logObj.Request(
  1153. hProv,
  1154. dwKeySpec,
  1155. phUserKey);
  1156. if (NULL != m_cspRedirect.pfGetUserKey)
  1157. {
  1158. try
  1159. {
  1160. fReturn = (*m_cspRedirect.pfGetUserKey)(
  1161. hProv,
  1162. dwKeySpec,
  1163. phUserKey);
  1164. dwReturn = GetLastError();
  1165. logObj.Response(
  1166. fReturn,
  1167. hProv,
  1168. dwKeySpec,
  1169. phUserKey);
  1170. }
  1171. catch (...)
  1172. {
  1173. logObj.LogException();
  1174. fReturn = FALSE;
  1175. dwReturn = ERROR_ARENA_TRASHED;
  1176. }
  1177. }
  1178. else
  1179. {
  1180. fReturn = FALSE;
  1181. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1182. logObj.LogNotCalled(dwReturn);
  1183. }
  1184. logObj.Log(m_tzLogFile);
  1185. }
  1186. if (!fReturn)
  1187. {
  1188. if (ERROR_SUCCESS == dwReturn)
  1189. dwReturn = ERROR_DISCARDED;
  1190. }
  1191. else
  1192. dwReturn = ERROR_SUCCESS;
  1193. return dwReturn;
  1194. }
  1195. /*
  1196. - CPImportKey
  1197. -
  1198. * Purpose:
  1199. * Import cryptographic keys
  1200. *
  1201. *
  1202. * Parameters:
  1203. * IN hProv - Handle to the CSP user
  1204. * IN pbData - Key blob data
  1205. * IN dwDataLen - Length of the key blob data
  1206. * IN hPubKey - Handle to the exchange public key value of
  1207. * the destination user
  1208. * IN dwFlags - Flags values
  1209. * OUT phKey - Pointer to the handle to the key which was
  1210. * Imported
  1211. *
  1212. * Returns:
  1213. */
  1214. #undef __SUBROUTINE__
  1215. #define __SUBROUTINE__ TEXT("CLoggingContext::ImportKey")
  1216. DWORD
  1217. CLoggingContext::ImportKey(
  1218. IN HCRYPTPROV hProv,
  1219. IN CONST BYTE *pbData,
  1220. IN DWORD dwDataLen,
  1221. IN HCRYPTKEY hPubKey,
  1222. IN DWORD dwFlags,
  1223. OUT HCRYPTKEY *phKey)
  1224. {
  1225. BOOL fReturn;
  1226. DWORD dwReturn;
  1227. {
  1228. CLogImportKey logObj;
  1229. logObj.Request(
  1230. hProv,
  1231. pbData,
  1232. dwDataLen,
  1233. hPubKey,
  1234. dwFlags,
  1235. phKey);
  1236. if (NULL != m_cspRedirect.pfImportKey)
  1237. {
  1238. try
  1239. {
  1240. fReturn = (*m_cspRedirect.pfImportKey)(
  1241. hProv,
  1242. pbData,
  1243. dwDataLen,
  1244. hPubKey,
  1245. dwFlags,
  1246. phKey);
  1247. dwReturn = GetLastError();
  1248. logObj.Response(
  1249. fReturn,
  1250. hProv,
  1251. pbData,
  1252. dwDataLen,
  1253. hPubKey,
  1254. dwFlags,
  1255. phKey);
  1256. }
  1257. catch (...)
  1258. {
  1259. logObj.LogException();
  1260. fReturn = FALSE;
  1261. dwReturn = ERROR_ARENA_TRASHED;
  1262. }
  1263. }
  1264. else
  1265. {
  1266. fReturn = FALSE;
  1267. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1268. logObj.LogNotCalled(dwReturn);
  1269. }
  1270. logObj.Log(m_tzLogFile);
  1271. }
  1272. if (!fReturn)
  1273. {
  1274. if (ERROR_SUCCESS == dwReturn)
  1275. dwReturn = ERROR_DISCARDED;
  1276. }
  1277. else
  1278. dwReturn = ERROR_SUCCESS;
  1279. return dwReturn;
  1280. }
  1281. /*
  1282. - CPSetKeyParam
  1283. -
  1284. * Purpose:
  1285. * Allows applications to customize various aspects of the
  1286. * operations of a key
  1287. *
  1288. * Parameters:
  1289. * IN hProv - Handle to a CSP
  1290. * IN hKey - Handle to a key
  1291. * IN dwParam - Parameter number
  1292. * IN pbData - Pointer to data
  1293. * IN dwFlags - Flags values
  1294. *
  1295. * Returns:
  1296. */
  1297. #undef __SUBROUTINE__
  1298. #define __SUBROUTINE__ TEXT("CLoggingContext::SetKeyParam")
  1299. DWORD
  1300. CLoggingContext::SetKeyParam(
  1301. IN HCRYPTPROV hProv,
  1302. IN HCRYPTKEY hKey,
  1303. IN DWORD dwParam,
  1304. IN CONST BYTE *pbData,
  1305. IN DWORD dwFlags)
  1306. {
  1307. BOOL fReturn;
  1308. DWORD dwReturn;
  1309. {
  1310. CLogSetKeyParam logObj;
  1311. DWORD dwLength;
  1312. CONST BYTE *pbRealData = pbData;
  1313. dwLength = MapLength(rglmKeyParam, dwParam, &pbRealData, dwFlags);
  1314. logObj.Request(
  1315. hProv,
  1316. hKey,
  1317. dwParam,
  1318. pbRealData,
  1319. dwLength,
  1320. dwFlags);
  1321. if (NULL != m_cspRedirect.pfSetKeyParam)
  1322. {
  1323. try
  1324. {
  1325. fReturn = (*m_cspRedirect.pfSetKeyParam)(
  1326. hProv,
  1327. hKey,
  1328. dwParam,
  1329. pbData,
  1330. dwFlags);
  1331. dwReturn = GetLastError();
  1332. logObj.Response(
  1333. fReturn,
  1334. hProv,
  1335. hKey,
  1336. dwParam,
  1337. pbRealData,
  1338. dwFlags);
  1339. }
  1340. catch (...)
  1341. {
  1342. logObj.LogException();
  1343. fReturn = FALSE;
  1344. dwReturn = ERROR_ARENA_TRASHED;
  1345. }
  1346. }
  1347. else
  1348. {
  1349. fReturn = FALSE;
  1350. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1351. logObj.LogNotCalled(dwReturn);
  1352. }
  1353. logObj.Log(m_tzLogFile);
  1354. }
  1355. if (!fReturn)
  1356. {
  1357. if (ERROR_SUCCESS == dwReturn)
  1358. dwReturn = ERROR_DISCARDED;
  1359. }
  1360. else
  1361. dwReturn = ERROR_SUCCESS;
  1362. return dwReturn;
  1363. }
  1364. /*
  1365. - CPEncrypt
  1366. -
  1367. * Purpose:
  1368. * Encrypt data
  1369. *
  1370. *
  1371. * Parameters:
  1372. * IN hProv - Handle to the CSP user
  1373. * IN hKey - Handle to the key
  1374. * IN hHash - Optional handle to a hash
  1375. * IN Final - Boolean indicating if this is the final
  1376. * block of plaintext
  1377. * IN dwFlags - Flags values
  1378. * IN OUT pbData - Data to be encrypted
  1379. * IN OUT pdwDataLen - Pointer to the length of the data to be
  1380. * encrypted
  1381. * IN dwBufLen - Size of Data buffer
  1382. *
  1383. * Returns:
  1384. */
  1385. #undef __SUBROUTINE__
  1386. #define __SUBROUTINE__ TEXT("CLoggingContext::Encrypt")
  1387. DWORD
  1388. CLoggingContext::Encrypt(
  1389. IN HCRYPTPROV hProv,
  1390. IN HCRYPTKEY hKey,
  1391. IN HCRYPTHASH hHash,
  1392. IN BOOL Final,
  1393. IN DWORD dwFlags,
  1394. IN OUT BYTE *pbData,
  1395. IN OUT DWORD *pdwDataLen,
  1396. IN DWORD dwBufLen)
  1397. {
  1398. BOOL fReturn;
  1399. DWORD dwReturn;
  1400. {
  1401. CLogEncrypt logObj;
  1402. logObj.Request(
  1403. hProv,
  1404. hKey,
  1405. hHash,
  1406. Final,
  1407. dwFlags,
  1408. pbData,
  1409. pdwDataLen,
  1410. dwBufLen);
  1411. if (NULL != m_cspRedirect.pfEncrypt)
  1412. {
  1413. try
  1414. {
  1415. fReturn = (*m_cspRedirect.pfEncrypt)(
  1416. hProv,
  1417. hKey,
  1418. hHash,
  1419. Final,
  1420. dwFlags,
  1421. pbData,
  1422. pdwDataLen,
  1423. dwBufLen);
  1424. dwReturn = GetLastError();
  1425. logObj.Response(
  1426. fReturn,
  1427. hProv,
  1428. hKey,
  1429. hHash,
  1430. Final,
  1431. dwFlags,
  1432. pbData,
  1433. pdwDataLen,
  1434. dwBufLen);
  1435. }
  1436. catch (...)
  1437. {
  1438. logObj.LogException();
  1439. fReturn = FALSE;
  1440. dwReturn = ERROR_ARENA_TRASHED;
  1441. }
  1442. }
  1443. else
  1444. {
  1445. fReturn = FALSE;
  1446. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1447. logObj.LogNotCalled(dwReturn);
  1448. }
  1449. logObj.Log(m_tzLogFile);
  1450. }
  1451. if (!fReturn)
  1452. {
  1453. if (ERROR_SUCCESS == dwReturn)
  1454. dwReturn = ERROR_DISCARDED;
  1455. }
  1456. else
  1457. dwReturn = ERROR_SUCCESS;
  1458. return dwReturn;
  1459. }
  1460. /*
  1461. - CPDecrypt
  1462. -
  1463. * Purpose:
  1464. * Decrypt data
  1465. *
  1466. *
  1467. * Parameters:
  1468. * IN hProv - Handle to the CSP user
  1469. * IN hKey - Handle to the key
  1470. * IN hHash - Optional handle to a hash
  1471. * IN Final - Boolean indicating if this is the final
  1472. * block of ciphertext
  1473. * IN dwFlags - Flags values
  1474. * IN OUT pbData - Data to be decrypted
  1475. * IN OUT pdwDataLen - Pointer to the length of the data to be
  1476. * decrypted
  1477. *
  1478. * Returns:
  1479. */
  1480. #undef __SUBROUTINE__
  1481. #define __SUBROUTINE__ TEXT("CLoggingContext::Decrypt")
  1482. DWORD
  1483. CLoggingContext::Decrypt(
  1484. IN HCRYPTPROV hProv,
  1485. IN HCRYPTKEY hKey,
  1486. IN HCRYPTHASH hHash,
  1487. IN BOOL Final,
  1488. IN DWORD dwFlags,
  1489. IN OUT BYTE *pbData,
  1490. IN OUT DWORD *pdwDataLen)
  1491. {
  1492. BOOL fReturn;
  1493. DWORD dwReturn;
  1494. {
  1495. CLogDecrypt logObj;
  1496. logObj.Request(
  1497. hProv,
  1498. hKey,
  1499. hHash,
  1500. Final,
  1501. dwFlags,
  1502. pbData,
  1503. pdwDataLen);
  1504. if (NULL != m_cspRedirect.pfDecrypt)
  1505. {
  1506. try
  1507. {
  1508. fReturn = (*m_cspRedirect.pfDecrypt)(
  1509. hProv,
  1510. hKey,
  1511. hHash,
  1512. Final,
  1513. dwFlags,
  1514. pbData,
  1515. pdwDataLen);
  1516. dwReturn = GetLastError();
  1517. logObj.Response(
  1518. fReturn,
  1519. hProv,
  1520. hKey,
  1521. hHash,
  1522. Final,
  1523. dwFlags,
  1524. pbData,
  1525. pdwDataLen);
  1526. }
  1527. catch (...)
  1528. {
  1529. logObj.LogException();
  1530. fReturn = FALSE;
  1531. dwReturn = ERROR_ARENA_TRASHED;
  1532. }
  1533. }
  1534. else
  1535. {
  1536. fReturn = FALSE;
  1537. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1538. logObj.LogNotCalled(dwReturn);
  1539. }
  1540. logObj.Log(m_tzLogFile);
  1541. }
  1542. if (!fReturn)
  1543. {
  1544. if (ERROR_SUCCESS == dwReturn)
  1545. dwReturn = ERROR_DISCARDED;
  1546. }
  1547. else
  1548. dwReturn = ERROR_SUCCESS;
  1549. return dwReturn;
  1550. }
  1551. /*
  1552. - CPCreateHash
  1553. -
  1554. * Purpose:
  1555. * initate the hashing of a stream of data
  1556. *
  1557. *
  1558. * Parameters:
  1559. * IN hUID - Handle to the user identifcation
  1560. * IN Algid - Algorithm identifier of the hash algorithm
  1561. * to be used
  1562. * IN hKey - Optional key for MAC algorithms
  1563. * IN dwFlags - Flags values
  1564. * OUT pHash - Handle to hash object
  1565. *
  1566. * Returns:
  1567. */
  1568. #undef __SUBROUTINE__
  1569. #define __SUBROUTINE__ TEXT("CLoggingContext::CreateHash")
  1570. DWORD
  1571. CLoggingContext::CreateHash(
  1572. IN HCRYPTPROV hProv,
  1573. IN ALG_ID Algid,
  1574. IN HCRYPTKEY hKey,
  1575. IN DWORD dwFlags,
  1576. OUT HCRYPTHASH *phHash)
  1577. {
  1578. BOOL fReturn;
  1579. DWORD dwReturn;
  1580. {
  1581. CLogCreateHash logObj;
  1582. logObj.Request(
  1583. hProv,
  1584. Algid,
  1585. hKey,
  1586. dwFlags,
  1587. phHash);
  1588. if (NULL != m_cspRedirect.pfCreateHash)
  1589. {
  1590. try
  1591. {
  1592. fReturn = (*m_cspRedirect.pfCreateHash)(
  1593. hProv,
  1594. Algid,
  1595. hKey,
  1596. dwFlags,
  1597. phHash);
  1598. dwReturn = GetLastError();
  1599. logObj.Response(
  1600. fReturn,
  1601. hProv,
  1602. Algid,
  1603. hKey,
  1604. dwFlags,
  1605. phHash);
  1606. }
  1607. catch (...)
  1608. {
  1609. logObj.LogException();
  1610. fReturn = FALSE;
  1611. dwReturn = ERROR_ARENA_TRASHED;
  1612. }
  1613. }
  1614. else
  1615. {
  1616. fReturn = FALSE;
  1617. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1618. logObj.LogNotCalled(dwReturn);
  1619. }
  1620. logObj.Log(m_tzLogFile);
  1621. }
  1622. if (!fReturn)
  1623. {
  1624. if (ERROR_SUCCESS == dwReturn)
  1625. dwReturn = ERROR_DISCARDED;
  1626. }
  1627. else
  1628. dwReturn = ERROR_SUCCESS;
  1629. return dwReturn;
  1630. }
  1631. /*
  1632. - CPDestoryHash
  1633. -
  1634. * Purpose:
  1635. * Destory the hash object
  1636. *
  1637. *
  1638. * Parameters:
  1639. * IN hProv - Handle to the user identifcation
  1640. * IN hHash - Handle to hash object
  1641. *
  1642. * Returns:
  1643. */
  1644. #undef __SUBROUTINE__
  1645. #define __SUBROUTINE__ TEXT("CLoggingContext::DestroyHash")
  1646. DWORD
  1647. CLoggingContext::DestroyHash(
  1648. IN HCRYPTPROV hProv,
  1649. IN HCRYPTHASH hHash)
  1650. {
  1651. BOOL fReturn;
  1652. DWORD dwReturn;
  1653. {
  1654. CLogDestroyHash logObj;
  1655. logObj.Request(
  1656. hProv,
  1657. hHash);
  1658. if (NULL != m_cspRedirect.pfDestroyHash)
  1659. {
  1660. try
  1661. {
  1662. fReturn = (*m_cspRedirect.pfDestroyHash)(
  1663. hProv,
  1664. hHash);
  1665. dwReturn = GetLastError();
  1666. logObj.Response(
  1667. fReturn,
  1668. hProv,
  1669. hHash);
  1670. }
  1671. catch (...)
  1672. {
  1673. logObj.LogException();
  1674. fReturn = FALSE;
  1675. dwReturn = ERROR_ARENA_TRASHED;
  1676. }
  1677. }
  1678. else
  1679. {
  1680. fReturn = FALSE;
  1681. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1682. logObj.LogNotCalled(dwReturn);
  1683. }
  1684. logObj.Log(m_tzLogFile);
  1685. }
  1686. if (!fReturn)
  1687. {
  1688. if (ERROR_SUCCESS == dwReturn)
  1689. dwReturn = ERROR_DISCARDED;
  1690. }
  1691. else
  1692. dwReturn = ERROR_SUCCESS;
  1693. return dwReturn;
  1694. }
  1695. /*
  1696. - CPGetHashParam
  1697. -
  1698. * Purpose:
  1699. * Allows applications to get various aspects of the
  1700. * operations of a hash
  1701. *
  1702. * Parameters:
  1703. * IN hProv - Handle to a CSP
  1704. * IN hHash - Handle to a hash
  1705. * IN dwParam - Parameter number
  1706. * OUT pbData - Pointer to data
  1707. * IN pdwDataLen - Length of parameter data
  1708. * IN dwFlags - Flags values
  1709. *
  1710. * Returns:
  1711. */
  1712. #undef __SUBROUTINE__
  1713. #define __SUBROUTINE__ TEXT("CLoggingContext::GetHashParam")
  1714. DWORD
  1715. CLoggingContext::GetHashParam(
  1716. IN HCRYPTPROV hProv,
  1717. IN HCRYPTHASH hHash,
  1718. IN DWORD dwParam,
  1719. OUT BYTE *pbData,
  1720. IN OUT DWORD *pdwDataLen,
  1721. IN DWORD dwFlags)
  1722. {
  1723. BOOL fReturn;
  1724. DWORD dwReturn;
  1725. {
  1726. CLogGetHashParam logObj;
  1727. logObj.Request(
  1728. hProv,
  1729. hHash,
  1730. dwParam,
  1731. pbData,
  1732. pdwDataLen,
  1733. dwFlags);
  1734. if (NULL != m_cspRedirect.pfGetHashParam)
  1735. {
  1736. try
  1737. {
  1738. fReturn = (*m_cspRedirect.pfGetHashParam)(
  1739. hProv,
  1740. hHash,
  1741. dwParam,
  1742. pbData,
  1743. pdwDataLen,
  1744. dwFlags);
  1745. dwReturn = GetLastError();
  1746. logObj.Response(
  1747. fReturn,
  1748. hProv,
  1749. hHash,
  1750. dwParam,
  1751. pbData,
  1752. pdwDataLen,
  1753. dwFlags);
  1754. }
  1755. catch (...)
  1756. {
  1757. logObj.LogException();
  1758. fReturn = FALSE;
  1759. dwReturn = ERROR_ARENA_TRASHED;
  1760. }
  1761. }
  1762. else
  1763. {
  1764. fReturn = FALSE;
  1765. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1766. logObj.LogNotCalled(dwReturn);
  1767. }
  1768. logObj.Log(m_tzLogFile);
  1769. }
  1770. if (!fReturn)
  1771. {
  1772. if (ERROR_SUCCESS == dwReturn)
  1773. dwReturn = ERROR_DISCARDED;
  1774. }
  1775. else
  1776. dwReturn = ERROR_SUCCESS;
  1777. return dwReturn;
  1778. }
  1779. /*
  1780. - CPHashData
  1781. -
  1782. * Purpose:
  1783. * Compute the cryptograghic hash on a stream of data
  1784. *
  1785. *
  1786. * Parameters:
  1787. * IN hProv - Handle to the user identifcation
  1788. * IN hHash - Handle to hash object
  1789. * IN pbData - Pointer to data to be hashed
  1790. * IN dwDataLen - Length of the data to be hashed
  1791. * IN dwFlags - Flags values
  1792. * IN pdwMaxLen - Maximum length of the data stream the CSP
  1793. * module may handle
  1794. *
  1795. * Returns:
  1796. */
  1797. #undef __SUBROUTINE__
  1798. #define __SUBROUTINE__ TEXT("CLoggingContext::HashData")
  1799. DWORD
  1800. CLoggingContext::HashData(
  1801. IN HCRYPTPROV hProv,
  1802. IN HCRYPTHASH hHash,
  1803. IN CONST BYTE *pbData,
  1804. IN DWORD dwDataLen,
  1805. IN DWORD dwFlags)
  1806. {
  1807. BOOL fReturn;
  1808. DWORD dwReturn;
  1809. {
  1810. CLogHashData logObj;
  1811. logObj.Request(
  1812. hProv,
  1813. hHash,
  1814. pbData,
  1815. dwDataLen,
  1816. dwFlags);
  1817. if (NULL != m_cspRedirect.pfHashData)
  1818. {
  1819. try
  1820. {
  1821. fReturn = (*m_cspRedirect.pfHashData)(
  1822. hProv,
  1823. hHash,
  1824. pbData,
  1825. dwDataLen,
  1826. dwFlags);
  1827. dwReturn = GetLastError();
  1828. logObj.Response(
  1829. fReturn,
  1830. hProv,
  1831. hHash,
  1832. pbData,
  1833. dwDataLen,
  1834. dwFlags);
  1835. }
  1836. catch (...)
  1837. {
  1838. logObj.LogException();
  1839. fReturn = FALSE;
  1840. dwReturn = ERROR_ARENA_TRASHED;
  1841. }
  1842. }
  1843. else
  1844. {
  1845. fReturn = FALSE;
  1846. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1847. logObj.LogNotCalled(dwReturn);
  1848. }
  1849. logObj.Log(m_tzLogFile);
  1850. }
  1851. if (!fReturn)
  1852. {
  1853. if (ERROR_SUCCESS == dwReturn)
  1854. dwReturn = ERROR_DISCARDED;
  1855. }
  1856. else
  1857. dwReturn = ERROR_SUCCESS;
  1858. return dwReturn;
  1859. }
  1860. /*
  1861. - CPHashSessionKey
  1862. -
  1863. * Purpose:
  1864. * Compute the cryptograghic hash on a key object.
  1865. *
  1866. *
  1867. * Parameters:
  1868. * IN hProv - Handle to the user identifcation
  1869. * IN hHash - Handle to hash object
  1870. * IN hKey - Handle to a key object
  1871. * IN dwFlags - Flags values
  1872. *
  1873. * Returns:
  1874. * CRYPT_FAILED
  1875. * CRYPT_SUCCEED
  1876. */
  1877. #undef __SUBROUTINE__
  1878. #define __SUBROUTINE__ TEXT("CLoggingContext::HashSessionKey")
  1879. DWORD
  1880. CLoggingContext::HashSessionKey(
  1881. IN HCRYPTPROV hProv,
  1882. IN HCRYPTHASH hHash,
  1883. IN HCRYPTKEY hKey,
  1884. IN DWORD dwFlags)
  1885. {
  1886. BOOL fReturn;
  1887. DWORD dwReturn;
  1888. {
  1889. CLogHashSessionKey logObj;
  1890. logObj.Request(
  1891. hProv,
  1892. hHash,
  1893. hKey,
  1894. dwFlags);
  1895. if (NULL != m_cspRedirect.pfHashSessionKey)
  1896. {
  1897. try
  1898. {
  1899. fReturn = (*m_cspRedirect.pfHashSessionKey)(
  1900. hProv,
  1901. hHash,
  1902. hKey,
  1903. dwFlags);
  1904. dwReturn = GetLastError();
  1905. logObj.Response(
  1906. fReturn,
  1907. hProv,
  1908. hHash,
  1909. hKey,
  1910. dwFlags);
  1911. }
  1912. catch (...)
  1913. {
  1914. logObj.LogException();
  1915. fReturn = FALSE;
  1916. dwReturn = ERROR_ARENA_TRASHED;
  1917. }
  1918. }
  1919. else
  1920. {
  1921. fReturn = FALSE;
  1922. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  1923. logObj.LogNotCalled(dwReturn);
  1924. }
  1925. logObj.Log(m_tzLogFile);
  1926. }
  1927. if (!fReturn)
  1928. {
  1929. if (ERROR_SUCCESS == dwReturn)
  1930. dwReturn = ERROR_DISCARDED;
  1931. }
  1932. else
  1933. dwReturn = ERROR_SUCCESS;
  1934. return dwReturn;
  1935. }
  1936. /*
  1937. - CPSetHashParam
  1938. -
  1939. * Purpose:
  1940. * Allows applications to customize various aspects of the
  1941. * operations of a hash
  1942. *
  1943. * Parameters:
  1944. * IN hProv - Handle to a CSP
  1945. * IN hHash - Handle to a hash
  1946. * IN dwParam - Parameter number
  1947. * IN pbData - Pointer to data
  1948. * IN dwFlags - Flags values
  1949. *
  1950. * Returns:
  1951. */
  1952. #undef __SUBROUTINE__
  1953. #define __SUBROUTINE__ TEXT("CLoggingContext::SetHashParam")
  1954. DWORD
  1955. CLoggingContext::SetHashParam(
  1956. IN HCRYPTPROV hProv,
  1957. IN HCRYPTHASH hHash,
  1958. IN DWORD dwParam,
  1959. IN CONST BYTE *pbData,
  1960. IN DWORD dwFlags)
  1961. {
  1962. BOOL fReturn;
  1963. DWORD dwReturn;
  1964. {
  1965. CLogSetHashParam logObj;
  1966. DWORD dwLength;
  1967. CONST BYTE *pbRealData = pbData;
  1968. dwLength = MapLength(rglmHashParam, dwParam, &pbRealData, dwFlags);
  1969. logObj.Request(
  1970. hProv,
  1971. hHash,
  1972. dwParam,
  1973. pbRealData,
  1974. dwLength,
  1975. dwFlags);
  1976. if (NULL != m_cspRedirect.pfSetHashParam)
  1977. {
  1978. try
  1979. {
  1980. fReturn = (*m_cspRedirect.pfSetHashParam)(
  1981. hProv,
  1982. hHash,
  1983. dwParam,
  1984. pbData,
  1985. dwFlags);
  1986. dwReturn = GetLastError();
  1987. logObj.Response(
  1988. fReturn,
  1989. hProv,
  1990. hHash,
  1991. dwParam,
  1992. pbRealData,
  1993. dwFlags);
  1994. }
  1995. catch (...)
  1996. {
  1997. logObj.LogException();
  1998. fReturn = FALSE;
  1999. dwReturn = ERROR_ARENA_TRASHED;
  2000. }
  2001. }
  2002. else
  2003. {
  2004. fReturn = FALSE;
  2005. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  2006. logObj.LogNotCalled(dwReturn);
  2007. }
  2008. logObj.Log(m_tzLogFile);
  2009. }
  2010. if (!fReturn)
  2011. {
  2012. if (ERROR_SUCCESS == dwReturn)
  2013. dwReturn = ERROR_DISCARDED;
  2014. }
  2015. else
  2016. dwReturn = ERROR_SUCCESS;
  2017. return dwReturn;
  2018. }
  2019. /*
  2020. - CPSignHash
  2021. -
  2022. * Purpose:
  2023. * Create a digital signature from a hash
  2024. *
  2025. *
  2026. * Parameters:
  2027. * IN hProv - Handle to the user identifcation
  2028. * IN hHash - Handle to hash object
  2029. * IN dwKeySpec - Key pair that is used to sign with
  2030. * IN sDescription - Description of data to be signed
  2031. * IN dwFlags - Flags values
  2032. * OUT pbSignture - Pointer to signature data
  2033. * IN OUT pdwSignLen- Pointer to the len of the signature data
  2034. *
  2035. * Returns:
  2036. */
  2037. #undef __SUBROUTINE__
  2038. #define __SUBROUTINE__ TEXT("CLoggingContext::SignHash")
  2039. DWORD
  2040. CLoggingContext::SignHash(
  2041. IN HCRYPTPROV hProv,
  2042. IN HCRYPTHASH hHash,
  2043. IN DWORD dwKeySpec,
  2044. IN LPCTSTR sDescription,
  2045. IN DWORD dwFlags,
  2046. OUT BYTE *pbSignature,
  2047. IN OUT DWORD *pdwSigLen)
  2048. {
  2049. BOOL fReturn;
  2050. DWORD dwReturn;
  2051. {
  2052. CLogSignHash logObj;
  2053. logObj.Request(
  2054. hProv,
  2055. hHash,
  2056. dwKeySpec,
  2057. sDescription,
  2058. dwFlags,
  2059. pbSignature,
  2060. pdwSigLen);
  2061. if (NULL != m_cspRedirect.pfSignHash)
  2062. {
  2063. try
  2064. {
  2065. fReturn = (*m_cspRedirect.pfSignHash)(
  2066. hProv,
  2067. hHash,
  2068. dwKeySpec,
  2069. sDescription,
  2070. dwFlags,
  2071. pbSignature,
  2072. pdwSigLen);
  2073. dwReturn = GetLastError();
  2074. logObj.Response(
  2075. fReturn,
  2076. hProv,
  2077. hHash,
  2078. dwKeySpec,
  2079. sDescription,
  2080. dwFlags,
  2081. pbSignature,
  2082. pdwSigLen);
  2083. }
  2084. catch (...)
  2085. {
  2086. logObj.LogException();
  2087. fReturn = FALSE;
  2088. dwReturn = ERROR_ARENA_TRASHED;
  2089. }
  2090. }
  2091. else
  2092. {
  2093. fReturn = FALSE;
  2094. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  2095. logObj.LogNotCalled(dwReturn);
  2096. }
  2097. logObj.Log(m_tzLogFile);
  2098. }
  2099. if (!fReturn)
  2100. {
  2101. if (ERROR_SUCCESS == dwReturn)
  2102. dwReturn = ERROR_DISCARDED;
  2103. }
  2104. else
  2105. dwReturn = ERROR_SUCCESS;
  2106. return dwReturn;
  2107. }
  2108. /*
  2109. - CPVerifySignature
  2110. -
  2111. * Purpose:
  2112. * Used to verify a signature against a hash object
  2113. *
  2114. *
  2115. * Parameters:
  2116. * IN hProv - Handle to the user identifcation
  2117. * IN hHash - Handle to hash object
  2118. * IN pbSignture - Pointer to signature data
  2119. * IN dwSigLen - Length of the signature data
  2120. * IN hPubKey - Handle to the public key for verifying
  2121. * the signature
  2122. * IN sDescription - Description of data to be signed
  2123. * IN dwFlags - Flags values
  2124. *
  2125. * Returns:
  2126. */
  2127. #undef __SUBROUTINE__
  2128. #define __SUBROUTINE__ TEXT("CLoggingContext::VerifySignature")
  2129. DWORD
  2130. CLoggingContext::VerifySignature(
  2131. IN HCRYPTPROV hProv,
  2132. IN HCRYPTHASH hHash,
  2133. IN CONST BYTE *pbSignature,
  2134. IN DWORD dwSigLen,
  2135. IN HCRYPTKEY hPubKey,
  2136. IN LPCTSTR sDescription,
  2137. IN DWORD dwFlags)
  2138. {
  2139. BOOL fReturn;
  2140. DWORD dwReturn;
  2141. {
  2142. CLogVerifySignature logObj;
  2143. logObj.Request(
  2144. hProv,
  2145. hHash,
  2146. pbSignature,
  2147. dwSigLen,
  2148. hPubKey,
  2149. sDescription,
  2150. dwFlags);
  2151. if (NULL != m_cspRedirect.pfVerifySignature)
  2152. {
  2153. try
  2154. {
  2155. fReturn = (*m_cspRedirect.pfVerifySignature)(
  2156. hProv,
  2157. hHash,
  2158. pbSignature,
  2159. dwSigLen,
  2160. hPubKey,
  2161. sDescription,
  2162. dwFlags);
  2163. dwReturn = GetLastError();
  2164. logObj.Response(
  2165. fReturn,
  2166. hProv,
  2167. hHash,
  2168. pbSignature,
  2169. dwSigLen,
  2170. hPubKey,
  2171. sDescription,
  2172. dwFlags);
  2173. }
  2174. catch (...)
  2175. {
  2176. logObj.LogException();
  2177. fReturn = FALSE;
  2178. dwReturn = ERROR_ARENA_TRASHED;
  2179. }
  2180. }
  2181. else
  2182. {
  2183. fReturn = FALSE;
  2184. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  2185. logObj.LogNotCalled(dwReturn);
  2186. }
  2187. logObj.Log(m_tzLogFile);
  2188. }
  2189. if (!fReturn)
  2190. {
  2191. if (ERROR_SUCCESS == dwReturn)
  2192. dwReturn = ERROR_DISCARDED;
  2193. }
  2194. else
  2195. dwReturn = ERROR_SUCCESS;
  2196. return dwReturn;
  2197. }
  2198. /*
  2199. - CPDuplicateHash
  2200. -
  2201. * Purpose:
  2202. * Duplicates the state of a hash and returns a handle to it
  2203. *
  2204. * Parameters:
  2205. * IN hUID - Handle to a CSP
  2206. * IN hHash - Handle to a hash
  2207. * IN pdwReserved - Reserved
  2208. * IN dwFlags - Flags
  2209. * IN phHash - Handle to the new hash
  2210. *
  2211. * Returns:
  2212. */
  2213. #undef __SUBROUTINE__
  2214. #define __SUBROUTINE__ TEXT("CLoggingContext::DuplicateHash")
  2215. DWORD
  2216. CLoggingContext::DuplicateHash(
  2217. IN HCRYPTPROV hProv,
  2218. IN HCRYPTHASH hHash,
  2219. IN DWORD *pdwReserved,
  2220. IN DWORD dwFlags,
  2221. IN HCRYPTHASH *phHash)
  2222. {
  2223. BOOL fReturn;
  2224. DWORD dwReturn;
  2225. {
  2226. CLogDuplicateHash logObj;
  2227. logObj.Request(
  2228. hProv,
  2229. hHash,
  2230. pdwReserved,
  2231. dwFlags,
  2232. phHash);
  2233. if (NULL != m_cspRedirect.pfDuplicateHash)
  2234. {
  2235. try
  2236. {
  2237. fReturn = (*m_cspRedirect.pfDuplicateHash)(
  2238. hProv,
  2239. hHash,
  2240. pdwReserved,
  2241. dwFlags,
  2242. phHash);
  2243. dwReturn = GetLastError();
  2244. logObj.Response(
  2245. fReturn,
  2246. hProv,
  2247. hHash,
  2248. pdwReserved,
  2249. dwFlags,
  2250. phHash);
  2251. }
  2252. catch (...)
  2253. {
  2254. logObj.LogException();
  2255. fReturn = FALSE;
  2256. dwReturn = ERROR_ARENA_TRASHED;
  2257. }
  2258. }
  2259. else
  2260. {
  2261. fReturn = FALSE;
  2262. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  2263. logObj.LogNotCalled(dwReturn);
  2264. }
  2265. logObj.Log(m_tzLogFile);
  2266. }
  2267. if (!fReturn)
  2268. {
  2269. if (ERROR_SUCCESS == dwReturn)
  2270. dwReturn = ERROR_DISCARDED;
  2271. }
  2272. else
  2273. dwReturn = ERROR_SUCCESS;
  2274. return dwReturn;
  2275. }
  2276. /*
  2277. - CPDuplicateKey
  2278. -
  2279. * Purpose:
  2280. * Duplicates the state of a key and returns a handle to it
  2281. *
  2282. * Parameters:
  2283. * IN hUID - Handle to a CSP
  2284. * IN hKey - Handle to a key
  2285. * IN pdwReserved - Reserved
  2286. * IN dwFlags - Flags
  2287. * IN phKey - Handle to the new key
  2288. *
  2289. * Returns:
  2290. */
  2291. #undef __SUBROUTINE__
  2292. #define __SUBROUTINE__ TEXT("CLoggingContext::DuplicateKey")
  2293. DWORD
  2294. CLoggingContext::DuplicateKey(
  2295. IN HCRYPTPROV hProv,
  2296. IN HCRYPTKEY hKey,
  2297. IN DWORD *pdwReserved,
  2298. IN DWORD dwFlags,
  2299. IN HCRYPTKEY *phKey)
  2300. {
  2301. BOOL fReturn;
  2302. DWORD dwReturn;
  2303. {
  2304. CLogDuplicateKey logObj;
  2305. logObj.Request(
  2306. hProv,
  2307. hKey,
  2308. pdwReserved,
  2309. dwFlags,
  2310. phKey);
  2311. if (NULL != m_cspRedirect.pfDuplicateKey)
  2312. {
  2313. try
  2314. {
  2315. fReturn = (*m_cspRedirect.pfDuplicateKey)(
  2316. hProv,
  2317. hKey,
  2318. pdwReserved,
  2319. dwFlags,
  2320. phKey);
  2321. dwReturn = GetLastError();
  2322. logObj.Response(
  2323. fReturn,
  2324. hProv,
  2325. hKey,
  2326. pdwReserved,
  2327. dwFlags,
  2328. phKey);
  2329. }
  2330. catch (...)
  2331. {
  2332. logObj.LogException();
  2333. fReturn = FALSE;
  2334. dwReturn = ERROR_ARENA_TRASHED;
  2335. }
  2336. }
  2337. else
  2338. {
  2339. fReturn = FALSE;
  2340. dwReturn = ERROR_CALL_NOT_IMPLEMENTED;
  2341. logObj.LogNotCalled(dwReturn);
  2342. }
  2343. logObj.Log(m_tzLogFile);
  2344. }
  2345. if (!fReturn)
  2346. {
  2347. if (ERROR_SUCCESS == dwReturn)
  2348. dwReturn = ERROR_DISCARDED;
  2349. }
  2350. else
  2351. dwReturn = ERROR_SUCCESS;
  2352. return dwReturn;
  2353. }
  2354. //
  2355. ///////////////////////////////////////////////////////////////////////////////
  2356. //
  2357. #undef __SUBROUTINE__
  2358. #define __SUBROUTINE__ TEXT("MapLength")
  2359. static DWORD
  2360. MapLength(
  2361. const LengthMap *rglmParamId,
  2362. DWORD dwParam,
  2363. LPCBYTE *ppbData,
  2364. DWORD dwFlags)
  2365. {
  2366. DWORD dwIndex;
  2367. DWORD dwLength;
  2368. try
  2369. {
  2370. for (dwIndex = 0;
  2371. EndFlag != rglmParamId[dwIndex].leLengthType;
  2372. dwIndex += 1)
  2373. {
  2374. if (dwParam == rglmParamId[dwIndex].dwParamId)
  2375. break;
  2376. }
  2377. switch (rglmParamId[dwIndex].leLengthType)
  2378. {
  2379. case AsnEncoding:
  2380. dwLength = Asn1Length(*ppbData);
  2381. break;
  2382. case AnsiString:
  2383. dwLength = (lstrlenA((LPCSTR)(*ppbData)) + 1) * sizeof(CHAR);
  2384. break;
  2385. case UnicodeString:
  2386. dwLength = (lstrlenW((LPCWSTR)(*ppbData)) +1) *sizeof(WCHAR);
  2387. break;
  2388. case StructWithLength:
  2389. dwLength = *(const DWORD *)(*ppbData);
  2390. break;
  2391. case Blob:
  2392. {
  2393. const CRYPT_ATTR_BLOB *pBlob = (const CRYPT_ATTR_BLOB *)(*ppbData);
  2394. if (NULL != pBlob)
  2395. {
  2396. dwLength = pBlob->cbData;
  2397. *ppbData = pBlob->pbData;
  2398. }
  2399. else
  2400. dwLength = 0;
  2401. break;
  2402. }
  2403. case SecDesc:
  2404. dwLength = GetSecurityDescriptorLength((LPVOID)(*ppbData));
  2405. break;
  2406. case EndFlag:
  2407. case Unknown:
  2408. dwLength = 0;
  2409. break;
  2410. case Direct:
  2411. dwLength = rglmParamId[dwIndex].cbLength;
  2412. break;
  2413. default:
  2414. // Oops!
  2415. dwLength = 0;
  2416. }
  2417. }
  2418. catch (...)
  2419. {
  2420. dwLength = 0;
  2421. }
  2422. return dwLength;
  2423. }
  2424. /*++
  2425. ExtractTag:
  2426. This routine extracts a tag from an ASN.1 BER stream.
  2427. Arguments:
  2428. pbSrc supplies the buffer containing the ASN.1 stream.
  2429. pdwTag receives the tag.
  2430. Return Value:
  2431. The number of bytes extracted from the stream. Errors are thrown
  2432. as DWORD status codes.
  2433. Author:
  2434. Doug Barlow (dbarlow) 10/9/1995
  2435. Doug Barlow (dbarlow) 7/31/1997
  2436. --*/
  2437. #undef __SUBROUTINE__
  2438. #define __SUBROUTINE__ TEXT("ExtractTag")
  2439. static DWORD
  2440. ExtractTag(
  2441. IN const BYTE *pbSrc,
  2442. OUT LPDWORD pdwTag,
  2443. OUT LPBOOL pfConstr)
  2444. {
  2445. LONG lth = 0;
  2446. DWORD tagw;
  2447. BYTE tagc, cls;
  2448. tagc = pbSrc[lth++];
  2449. cls = tagc & 0xc0; // Top 2 bits.
  2450. if (NULL != pfConstr)
  2451. *pfConstr = (0 != (tagc & 0x20));
  2452. tagc &= 0x1f; // Bottom 5 bits.
  2453. if (31 > tagc)
  2454. tagw = tagc;
  2455. else
  2456. {
  2457. tagw = 0;
  2458. do
  2459. {
  2460. if (0 != (tagw & 0xfe000000))
  2461. throw (DWORD)ERROR_ARITHMETIC_OVERFLOW;
  2462. tagc = pbSrc[lth++];
  2463. tagw <<= 7;
  2464. tagw |= tagc & 0x7f;
  2465. } while (0 != (tagc & 0x80));
  2466. }
  2467. *pdwTag = tagw | (cls << 24);
  2468. return lth;
  2469. }
  2470. /*++
  2471. ExtractLength:
  2472. This routine extracts a length from an ASN.1 BER stream. If the
  2473. length is
  2474. indefinite, this routine recurses to figure out the real length. A
  2475. flag as
  2476. to whether or not the encoding was indefinite is optionally
  2477. returned.
  2478. Arguments:
  2479. pbSrc supplies the buffer containing the ASN.1 stream.
  2480. pdwLen receives the len.
  2481. pfIndefinite, if not NULL, receives a flag indicating whether or not
  2482. the
  2483. encoding was indefinite.
  2484. Return Value:
  2485. The number of bytes extracted from the stream. Errors are thrown as
  2486. DWORD status codes.
  2487. Author:
  2488. Doug Barlow (dbarlow) 10/9/1995
  2489. Doug Barlow (dbarlow) 7/31/1997
  2490. --*/
  2491. #undef __SUBROUTINE__
  2492. #define __SUBROUTINE__ TEXT("ExtractLength")
  2493. static DWORD
  2494. ExtractLength(
  2495. IN const BYTE *pbSrc,
  2496. OUT LPDWORD pdwLen,
  2497. OUT LPBOOL pfIndefinite)
  2498. {
  2499. DWORD ll, rslt, lth, lTotal = 0;
  2500. BOOL fInd = FALSE;
  2501. //
  2502. // Extract the Length.
  2503. //
  2504. if (0 == (pbSrc[lTotal] & 0x80))
  2505. {
  2506. //
  2507. // Short form encoding.
  2508. //
  2509. rslt = pbSrc[lTotal++];
  2510. }
  2511. else
  2512. {
  2513. rslt = 0;
  2514. ll = pbSrc[lTotal++] & 0x7f;
  2515. if (0 != ll)
  2516. {
  2517. //
  2518. // Long form encoding.
  2519. //
  2520. for (; 0 < ll; ll -= 1)
  2521. {
  2522. if (0 != (rslt & 0xff000000))
  2523. throw (DWORD)ERROR_ARITHMETIC_OVERFLOW;
  2524. rslt = (rslt << 8) | pbSrc[lTotal];
  2525. lTotal += 1;
  2526. }
  2527. }
  2528. else
  2529. {
  2530. DWORD ls = lTotal;
  2531. //
  2532. // Indefinite encoding.
  2533. //
  2534. fInd = TRUE;
  2535. while ((0 != pbSrc[ls]) || (0 != pbSrc[ls + 1]))
  2536. {
  2537. // Skip over the Type.
  2538. if (31 > (pbSrc[ls] & 0x1f))
  2539. ls += 1;
  2540. else
  2541. while (0 != (pbSrc[++ls] & 0x80)); // Empty loop body.
  2542. lth = ExtractLength(&pbSrc[ls], &ll, NULL);
  2543. ls += lth + ll;
  2544. }
  2545. rslt = ls - lTotal;
  2546. }
  2547. }
  2548. //
  2549. // Supply the caller with what we've learned.
  2550. //
  2551. *pdwLen = rslt;
  2552. if (NULL != pfIndefinite)
  2553. *pfIndefinite = fInd;
  2554. return lTotal;
  2555. }
  2556. /*++
  2557. Asn1Length:
  2558. This routine parses a given ASN.1 buffer and returns the complete
  2559. length of the encoding, including the leading tag and length bytes.
  2560. Arguments:
  2561. pbData supplies the buffer to be parsed.
  2562. Return Value:
  2563. The length of the entire ASN.1 buffer.
  2564. Throws:
  2565. Overflow errors are thrown as DWORD status codes.
  2566. Author:
  2567. Doug Barlow (dbarlow) 7/31/1997
  2568. --*/
  2569. #undef __SUBROUTINE__
  2570. #define __SUBROUTINE__ TEXT("Asn1Length")
  2571. static DWORD
  2572. Asn1Length(
  2573. IN LPCBYTE pbAsn1)
  2574. {
  2575. DWORD dwTagLen, dwLenLen, dwValLen;
  2576. DWORD dwTag;
  2577. dwTagLen = ExtractTag(pbAsn1, &dwTag, NULL);
  2578. dwLenLen = ExtractLength(&pbAsn1[dwTagLen], &dwValLen, NULL);
  2579. return dwTagLen + dwLenLen + dwValLen;
  2580. }