Source code of Windows XP (NT5)
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.

1673 lines
47 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2000 Gemplus Canada Inc.
  4. //
  5. // Project:
  6. // Kenny (GPK CSP)
  7. //
  8. // Authors:
  9. // Thierry Tremblay
  10. // Francois Paradis
  11. //
  12. // Compiler:
  13. // Microsoft Visual C++ 6.0 - SP3
  14. // Platform SDK - January 2000
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////////////////
  17. #ifdef _UNICODE
  18. #define UNICODE
  19. #endif
  20. #include "gpkcsp.h"
  21. ///////////////////////////////////////////////////////////////////////////////////////////
  22. //
  23. // Prototypes
  24. //
  25. ///////////////////////////////////////////////////////////////////////////////////////////
  26. BOOL Coherent(HCRYPTPROV hProv);
  27. HWND GetAppWindow();
  28. void GpkLocalLock();
  29. void GpkLocalUnlock();
  30. DWORD Select_MF(HCRYPTPROV hProv);
  31. extern Prov_Context* ProvCont;
  32. extern const DWORD MAX_GPK_OBJ;
  33. #ifdef _DEBUG
  34. static DWORD dw1, dw2;
  35. #endif
  36. ///////////////////////////////////////////////////////////////////////////////////////////
  37. //
  38. // CSP API Wrappers
  39. //
  40. ///////////////////////////////////////////////////////////////////////////////////////////
  41. BOOL WINAPI CPAcquireContext( OUT HCRYPTPROV* phProv,
  42. IN LPCSTR pszContainer,
  43. IN DWORD dwFlags,
  44. IN PVTableProvStruc pVTable )
  45. {
  46. BOOL bResult = CRYPT_SUCCEED;
  47. DWORD errcode = ERROR_SUCCESS;
  48. GpkLocalLock();
  49. DBG_PRINT(TEXT("->CPAcquireContext(phProv:0x%p(0x%08X), pszContainer:%s, dwFlags:0x%08X, pVTable:0x%p)"),
  50. phProv, *phProv,
  51. pszContainer,
  52. dwFlags,
  53. pVTable);
  54. DBG_TIME1;
  55. __try
  56. {
  57. __try
  58. {
  59. #ifdef MS_BUILD
  60. // TT-START : MS - Whistler Beta 1 - Certificate overwrite
  61. if (dwFlags & CRYPT_NEWKEYSET)
  62. {
  63. // Extract reader name if any is specified
  64. char szReaderName[MAX_PATH];
  65. char szNewContainerName[MAX_PATH];
  66. ZeroMemory( szReaderName, sizeof(szReaderName) );
  67. ZeroMemory( szNewContainerName, sizeof(szNewContainerName) );
  68. if (pszContainer == 0 || *pszContainer == 0)
  69. {
  70. RETURN( CRYPT_FAILED, NTE_BAD_KEYSET_PARAM );
  71. }
  72. if (strlen(pszContainer) >= 4 && memcmp( pszContainer, "\\\\.\\", 4 )==0)
  73. {
  74. // We have a reader name, keep it
  75. char* pEnd = strchr( pszContainer+4, '\\' );
  76. if (pEnd==0)
  77. {
  78. //only a reader name
  79. strcpy( szReaderName, pszContainer );
  80. strcat( szReaderName, "\\" );
  81. }
  82. else
  83. {
  84. //there's also a container name
  85. memcpy( szReaderName, pszContainer, pEnd - pszContainer + 1 );
  86. strcpy( szNewContainerName, pEnd + 1 );
  87. }
  88. }
  89. else
  90. {
  91. //no reader name, copy the container name
  92. strcpy( szNewContainerName, pszContainer );
  93. }
  94. HCRYPTPROV hProv;
  95. if (MyCPAcquireContext( &hProv, szReaderName, dwFlags & CRYPT_SILENT, pVTable ))
  96. {
  97. // SCR#41
  98. char szExistingContainerName[MAX_PATH];
  99. DWORD len = sizeof(szExistingContainerName);
  100. ZeroMemory( szExistingContainerName, sizeof(szExistingContainerName) );
  101. //get the existing container name
  102. bResult = MyCPGetProvParam( hProv, PP_CONTAINER, (BYTE*)szExistingContainerName, &len, 0 );
  103. errcode = GetLastError();
  104. if( bResult )
  105. {
  106. if( strcmp( szExistingContainerName, szNewContainerName ) == 0 )
  107. {
  108. //the requested container exist in the token,
  109. bResult = CRYPT_FAILED;
  110. errcode = NTE_EXISTS;
  111. }
  112. else
  113. {
  114. //there already are a container in the token which isn't the one
  115. //requested
  116. bResult = CRYPT_FAILED;
  117. errcode = NTE_TOKEN_KEYSET_STORAGE_FULL;
  118. }
  119. }
  120. MyCPReleaseContext( hProv, 0 );
  121. }
  122. else
  123. {
  124. if (GetLastError()!=NTE_KEYSET_NOT_DEF)
  125. {
  126. bResult = CRYPT_FAILED;
  127. errcode = GetLastError();
  128. }
  129. }
  130. }
  131. // TT-END: MS - Whistler Beta 1 - Certificate overwrite
  132. #endif // MS_BUILD
  133. if (bResult)
  134. {
  135. bResult = MyCPAcquireContext( phProv, pszContainer, dwFlags, pVTable );
  136. errcode = GetLastError();
  137. }
  138. }
  139. __except(EXCEPTION_EXECUTE_HANDLER)
  140. {
  141. bResult = CRYPT_FAILED;
  142. errcode = E_UNEXPECTED;
  143. }
  144. }
  145. __finally
  146. {
  147. DBG_TIME2;
  148. DBG_PRINT(TEXT("<-CPAcquireContext(phProv:0x%p(0x%08X), pszContainer:%s, dwFlags:0x%08X, pVTable:0x%p)\n returns %d in %d msec"),
  149. phProv, *phProv,
  150. pszContainer,
  151. dwFlags,
  152. pVTable,
  153. bResult,
  154. DBG_DELTA);
  155. GpkLocalUnlock();
  156. }
  157. RETURN( bResult, errcode );
  158. }
  159. ///////////////////////////////////////////////////////////////////////////////////////////
  160. ///////////////////////////////////////////////////////////////////////////////////////////
  161. BOOL WINAPI CPGetProvParam( IN HCRYPTPROV hProv,
  162. IN DWORD dwParam,
  163. IN BYTE* pbData,
  164. IN DWORD* pdwDataLen,
  165. IN DWORD dwFlags )
  166. {
  167. BOOL bResult = CRYPT_SUCCEED;
  168. DWORD errcode = ERROR_SUCCESS;
  169. GpkLocalLock();
  170. DBG_PRINT(TEXT("->CPGetProvParam(hProv:0x%08X, dwParam:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwFlags:0x%08X)"),
  171. hProv,
  172. dwParam,
  173. pbData,
  174. pdwDataLen, *pdwDataLen,
  175. dwFlags);
  176. DBG_TIME1;
  177. __try
  178. {
  179. __try
  180. {
  181. // [FP] if we want to load a RSA private key into the GPK card,
  182. // we have to reconnect in exclusive mode
  183. if (dwParam == GPP_SESSION_RANDOM)
  184. {
  185. DWORD dwProto;
  186. errcode = SCardReconnect( ProvCont[hProv].hCard, SCARD_SHARE_EXCLUSIVE,
  187. SCARD_PROTOCOL_T0, SCARD_LEAVE_CARD, &dwProto );
  188. bResult = (errcode == SCARD_S_SUCCESS);
  189. }
  190. BOOL bDid = FALSE;
  191. if ((bResult) &&
  192. (((dwParam == PP_ENUMALGS) || (dwParam == PP_ENUMALGS_EX)) && (/*(Slot[ProvCont[hProv].Slot].GpkMaxSessionKey == 0) ||*/ (dwFlags == CRYPT_FIRST))) ||
  193. ((dwParam == PP_ENUMCONTAINERS) && (dwFlags == CRYPT_FIRST)) ||
  194. (dwParam == GPP_SERIAL_NUMBER) ||
  195. (dwParam == GPP_SESSION_RANDOM))
  196. {
  197. bResult = Coherent(hProv);
  198. errcode = GetLastError();
  199. bDid = TRUE;
  200. }
  201. if (bResult)
  202. {
  203. bResult = MyCPGetProvParam( hProv, dwParam, pbData, pdwDataLen, dwFlags );
  204. errcode = GetLastError();
  205. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  206. ProvCont[hProv].isContNameNullBlank)
  207. {
  208. // No access to the card has been done in this case
  209. }
  210. else if ((!ProvCont[hProv].bCardTransactionOpened) && (bDid))
  211. {
  212. // [FP] to be able to load a RSA private key into the GPK card,
  213. // the transaction should not be closed (only for PP_SESSION_RANDOM)
  214. // Select_MF(hProv); [NK] PIN not presented
  215. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  216. }
  217. }
  218. else
  219. {
  220. bResult = CRYPT_FAILED;
  221. errcode = GetLastError();
  222. }
  223. }
  224. __except(EXCEPTION_EXECUTE_HANDLER)
  225. {
  226. bResult = CRYPT_FAILED;
  227. errcode = E_UNEXPECTED;
  228. }
  229. }
  230. __finally
  231. {
  232. DBG_TIME2;
  233. DBG_PRINT(TEXT("<-CPGetProvParam(hProv:0x%08X, dwParam:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwFlags:0x%08X)\n returns %d in %d msec"),
  234. hProv,
  235. dwParam,
  236. pbData,
  237. pdwDataLen, *pdwDataLen,
  238. dwFlags,
  239. bResult,
  240. DBG_DELTA);
  241. GpkLocalUnlock();
  242. }
  243. RETURN( bResult, errcode );
  244. }
  245. ///////////////////////////////////////////////////////////////////////////////////////////
  246. ///////////////////////////////////////////////////////////////////////////////////////////
  247. BOOL WINAPI CPReleaseContext( IN HCRYPTPROV hProv,
  248. IN DWORD dwFlags )
  249. {
  250. BOOL bResult = CRYPT_SUCCEED;
  251. DWORD errcode = ERROR_SUCCESS;
  252. GpkLocalLock();
  253. DBG_PRINT(TEXT("->CPReleaseContext(hProv:0x%08X, dwFlags:0x%08X)"),
  254. hProv,
  255. dwFlags);
  256. DBG_TIME1;
  257. __try
  258. {
  259. __try
  260. {
  261. //if (Coherent(hProv))
  262. //{
  263. bResult = MyCPReleaseContext( hProv, dwFlags );
  264. errcode = GetLastError();
  265. //}
  266. //else
  267. //{
  268. // bResult = CRYPT_FAILED;
  269. // errcode = GetLastError();
  270. //}
  271. }
  272. __except(EXCEPTION_EXECUTE_HANDLER)
  273. {
  274. bResult = CRYPT_FAILED;
  275. errcode = E_UNEXPECTED;
  276. }
  277. }
  278. __finally
  279. {
  280. DBG_TIME2;
  281. DBG_PRINT(TEXT("<-CPReleaseContext(hProv:0x%08X, dwFlags:0x%08X)\n returns %d in %d msec"),
  282. hProv,
  283. dwFlags,
  284. bResult,
  285. DBG_DELTA);
  286. GpkLocalUnlock();
  287. }
  288. RETURN( bResult, errcode );
  289. }
  290. ///////////////////////////////////////////////////////////////////////////////////////////
  291. ///////////////////////////////////////////////////////////////////////////////////////////
  292. BOOL WINAPI CPSetProvParam( IN HCRYPTPROV hProv,
  293. IN DWORD dwParam,
  294. IN CONST BYTE* pbData,
  295. IN DWORD dwFlags )
  296. {
  297. BOOL bResult = CRYPT_SUCCEED;
  298. DWORD errcode = ERROR_SUCCESS;
  299. GpkLocalLock();
  300. DBG_PRINT(TEXT("->CPSetProvParam(hProv:0x%08X, dwParam:0x%08X, pbData:0x%p, dwFlags:0x%08X)"),
  301. hProv,
  302. dwParam,
  303. pbData,
  304. dwFlags);
  305. DBG_TIME1;
  306. __try
  307. {
  308. __try
  309. {
  310. // [FP] if we want to change the PIN,
  311. // we have to check the coherence
  312. if (dwParam == GPP_CHANGE_PIN)
  313. {
  314. bResult = Coherent(hProv);
  315. errcode = GetLastError();
  316. }
  317. if (bResult)
  318. {
  319. bResult = MyCPSetProvParam (hProv, dwParam, pbData, dwFlags);
  320. errcode = GetLastError();
  321. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  322. ProvCont[hProv].isContNameNullBlank)
  323. {
  324. // No access to the card has been done in this case
  325. }
  326. else
  327. {
  328. if (dwParam == GPP_CHANGE_PIN)
  329. {
  330. Select_MF(hProv);
  331. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  332. }
  333. }
  334. }
  335. else
  336. {
  337. bResult = CRYPT_FAILED;
  338. errcode = GetLastError();
  339. }
  340. }
  341. __except(EXCEPTION_EXECUTE_HANDLER)
  342. {
  343. bResult = CRYPT_FAILED;
  344. errcode = E_UNEXPECTED;
  345. }
  346. }
  347. __finally
  348. {
  349. DBG_TIME2;
  350. DBG_PRINT(TEXT("<-CPSetProvParam(hProv:0x%08X, dwParam:0x%08X, pbData:0x%p, dwFlags:0x%08X)\n returns %d in %d msec"),
  351. hProv,
  352. dwParam,
  353. pbData,
  354. dwFlags,
  355. bResult,
  356. DBG_DELTA);
  357. GpkLocalUnlock();
  358. }
  359. RETURN( bResult, errcode );
  360. }
  361. ///////////////////////////////////////////////////////////////////////////////////////////
  362. ///////////////////////////////////////////////////////////////////////////////////////////
  363. BOOL WINAPI CPDeriveKey( IN HCRYPTPROV hProv,
  364. IN ALG_ID Algid,
  365. IN HCRYPTHASH hHash,
  366. IN DWORD dwFlags,
  367. OUT HCRYPTKEY* phKey )
  368. {
  369. BOOL bResult = CRYPT_SUCCEED;
  370. DWORD errcode = ERROR_SUCCESS;
  371. GpkLocalLock();
  372. DBG_PRINT(TEXT("->CPDeriveKey(hProv:0x%08X, Algid:0x%08X, hHash:0x%08X, phKey:0x%p(0x%08X))"),
  373. hProv,
  374. Algid,
  375. hHash,
  376. dwFlags,
  377. phKey, *phKey);
  378. DBG_TIME1;
  379. __try
  380. {
  381. __try
  382. {
  383. // We do not have to check the coherence in this case since the operation does not
  384. // use the card info
  385. bResult = MyCPDeriveKey( hProv, Algid, hHash, dwFlags, phKey );
  386. errcode = GetLastError();
  387. }
  388. __except(EXCEPTION_EXECUTE_HANDLER)
  389. {
  390. bResult = CRYPT_FAILED;
  391. errcode = E_UNEXPECTED;
  392. }
  393. }
  394. __finally
  395. {
  396. DBG_TIME2;
  397. DBG_PRINT(TEXT("<-CPDeriveKey(hProv:0x%08X, Algid:0x%08X, hHash:0x%08X, phKey:0x%p(0x%08X))\n returns %d in %d msec"),
  398. hProv,
  399. Algid,
  400. hHash,
  401. dwFlags,
  402. phKey, *phKey,
  403. bResult,
  404. DBG_DELTA);
  405. GpkLocalUnlock();
  406. }
  407. RETURN( bResult, errcode );
  408. }
  409. ///////////////////////////////////////////////////////////////////////////////////////////
  410. ///////////////////////////////////////////////////////////////////////////////////////////
  411. BOOL WINAPI CPDestroyKey( IN HCRYPTPROV hProv,
  412. IN HCRYPTKEY hKey )
  413. {
  414. BOOL bResult = CRYPT_SUCCEED;
  415. DWORD errcode = ERROR_SUCCESS;
  416. GpkLocalLock();
  417. DBG_PRINT(TEXT("->CPDestroyKey(hProv:0x%08X, hKey:0x%08X)"),
  418. hProv,
  419. hKey);
  420. DBG_TIME1;
  421. __try
  422. {
  423. __try
  424. {
  425. // We do not have to check the coherence in this case since the operation does not
  426. // use the card info
  427. bResult = MyCPDestroyKey( hProv, hKey );
  428. errcode = GetLastError();
  429. }
  430. __except(EXCEPTION_EXECUTE_HANDLER)
  431. {
  432. bResult = CRYPT_FAILED;
  433. errcode = E_UNEXPECTED;
  434. }
  435. }
  436. __finally
  437. {
  438. DBG_TIME2;
  439. DBG_PRINT(TEXT("<-CPDestroyKey(hProv:0x%08X, hKey:0x%08X)\n returns %d in %d msec"),
  440. hProv,
  441. hKey,
  442. bResult,
  443. DBG_DELTA);
  444. GpkLocalUnlock();
  445. }
  446. RETURN( bResult, errcode );
  447. }
  448. ///////////////////////////////////////////////////////////////////////////////////////////
  449. ///////////////////////////////////////////////////////////////////////////////////////////
  450. BOOL WINAPI CPExportKey( IN HCRYPTPROV hProv,
  451. IN HCRYPTKEY hKey,
  452. IN HCRYPTKEY hPubKey,
  453. IN DWORD dwBlobType,
  454. IN DWORD dwFlags,
  455. OUT BYTE* pbData,
  456. OUT DWORD* pdwDataLen )
  457. {
  458. BOOL bResult = CRYPT_SUCCEED;
  459. DWORD errcode = ERROR_SUCCESS;
  460. GpkLocalLock();
  461. DBG_PRINT(TEXT("->CPExportKey(hProv:0x%08X, hKey:0x%08X, hPubKey:0x%08X, dwBlobType:0x%08X, dwFlags:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d))"),
  462. hProv,
  463. hKey,
  464. hPubKey,
  465. dwBlobType,
  466. dwFlags,
  467. pbData,
  468. pdwDataLen, *pdwDataLen);
  469. DBG_TIME1;
  470. __try
  471. {
  472. __try
  473. {
  474. if (Coherent(hProv))
  475. {
  476. bResult = MyCPExportKey( hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen );
  477. errcode = GetLastError();
  478. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  479. ProvCont[hProv].isContNameNullBlank)
  480. {
  481. // No access to the card has been done in this case
  482. }
  483. else
  484. {
  485. // Select_MF(hProv); [NK] PIN not presented
  486. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  487. }
  488. }
  489. else
  490. {
  491. bResult = CRYPT_FAILED;
  492. errcode = GetLastError();
  493. }
  494. }
  495. __except(EXCEPTION_EXECUTE_HANDLER)
  496. {
  497. bResult = CRYPT_FAILED;
  498. errcode = E_UNEXPECTED;
  499. }
  500. }
  501. __finally
  502. {
  503. DBG_TIME2;
  504. DBG_PRINT(TEXT("<-CPExportKey(hProv:0x%08X, hKey:0x%08X, hPubKey:0x%08X, dwBlobType:0x%08X, dwFlags:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d))\n returns %d in %d msec"),
  505. hProv,
  506. hKey,
  507. hPubKey,
  508. dwBlobType,
  509. dwFlags,
  510. pbData,
  511. pdwDataLen, *pdwDataLen,
  512. bResult,
  513. DBG_DELTA);
  514. GpkLocalUnlock();
  515. }
  516. RETURN( bResult, errcode );
  517. }
  518. ///////////////////////////////////////////////////////////////////////////////////////////
  519. ///////////////////////////////////////////////////////////////////////////////////////////
  520. BOOL WINAPI CPGenKey( IN HCRYPTPROV hProv,
  521. IN ALG_ID Algid,
  522. IN DWORD dwFlags,
  523. OUT HCRYPTKEY* phKey )
  524. {
  525. BOOL bResult = CRYPT_SUCCEED;
  526. DWORD errcode = ERROR_SUCCESS;
  527. GpkLocalLock();
  528. DBG_PRINT(TEXT("->CPGenKey(hProv:0x%08X, Algid:0x%08X, dwFlags:0x%08X, phKey:0x%p(0x%08X))"),
  529. hProv,
  530. Algid,
  531. dwFlags,
  532. dwFlags,
  533. phKey, *phKey);
  534. DBG_TIME1;
  535. __try
  536. {
  537. __try
  538. {
  539. if (Coherent(hProv))
  540. {
  541. bResult = MyCPGenKey( hProv, Algid, dwFlags, phKey );
  542. errcode = GetLastError();
  543. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  544. ProvCont[hProv].isContNameNullBlank)
  545. {
  546. // No access to the card has been done in this case
  547. }
  548. else
  549. {
  550. if ((Algid == AT_KEYEXCHANGE) || (Algid == AT_SIGNATURE))
  551. Select_MF(hProv);
  552. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  553. }
  554. }
  555. else
  556. {
  557. bResult = CRYPT_FAILED;
  558. errcode = GetLastError();
  559. }
  560. }
  561. __except(EXCEPTION_EXECUTE_HANDLER)
  562. {
  563. bResult = CRYPT_FAILED;
  564. errcode = E_UNEXPECTED;
  565. }
  566. }
  567. __finally
  568. {
  569. DBG_TIME2;
  570. DBG_PRINT(TEXT("<-CPGenKey(hProv:0x%p, Algid:0x%08X, dwFlags:0x%08X, phKey:0x%p(0x%p))\n returns %d in %d msec"),
  571. hProv,
  572. Algid,
  573. dwFlags,
  574. dwFlags,
  575. phKey, *phKey,
  576. bResult,
  577. DBG_DELTA);
  578. GpkLocalUnlock();
  579. }
  580. RETURN( bResult, errcode );
  581. }
  582. ///////////////////////////////////////////////////////////////////////////////////////////
  583. ///////////////////////////////////////////////////////////////////////////////////////////
  584. BOOL WINAPI CPGenRandom( IN HCRYPTPROV hProv,
  585. IN DWORD dwLen,
  586. IN OUT BYTE* pbBuffer )
  587. {
  588. BOOL bResult = CRYPT_SUCCEED;
  589. DWORD errcode = ERROR_SUCCESS;
  590. GpkLocalLock();
  591. DBG_PRINT(TEXT("->CPGenRandom(hProv:0x%08X, dwLen:%d, pbBuffer:0x%p)"),
  592. hProv,
  593. dwLen,
  594. pbBuffer);
  595. DBG_TIME1;
  596. __try
  597. {
  598. __try
  599. {
  600. if (Coherent(hProv))
  601. {
  602. bResult = MyCPGenRandom( hProv, dwLen, pbBuffer );
  603. errcode = GetLastError();
  604. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  605. ProvCont[hProv].isContNameNullBlank)
  606. {
  607. // No access to the card has been done in this case
  608. }
  609. else
  610. {
  611. // Select_MF(hProv); [FP] PIN not presented
  612. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  613. }
  614. }
  615. else
  616. {
  617. bResult = CRYPT_FAILED;
  618. errcode = GetLastError();
  619. }
  620. }
  621. __except(EXCEPTION_EXECUTE_HANDLER)
  622. {
  623. bResult = CRYPT_FAILED;
  624. errcode = E_UNEXPECTED;
  625. }
  626. }
  627. __finally
  628. {
  629. DBG_TIME2;
  630. DBG_PRINT(TEXT("<-CPGenRandom(hProv:0x%08X, dwLen:%d, pbBuffer:0x%p)\n returns %d in %d msec"),
  631. hProv,
  632. dwLen,
  633. pbBuffer,
  634. bResult,
  635. DBG_DELTA);
  636. GpkLocalUnlock();
  637. }
  638. RETURN( bResult, errcode );
  639. }
  640. ///////////////////////////////////////////////////////////////////////////////////////////
  641. ///////////////////////////////////////////////////////////////////////////////////////////
  642. BOOL WINAPI CPGetKeyParam( IN HCRYPTPROV hProv,
  643. IN HCRYPTKEY hKey,
  644. IN DWORD dwParam,
  645. IN BYTE* pbData,
  646. IN DWORD* pdwDataLen,
  647. IN DWORD dwFlags )
  648. {
  649. BOOL bResult = CRYPT_SUCCEED;
  650. DWORD errcode = ERROR_SUCCESS;
  651. GpkLocalLock();
  652. DBG_PRINT(TEXT("->CPGetKeyParam(hProv:0x%08X, hKey:0x%08X, dwParam:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwFlags:0x%08X)"),
  653. hProv,
  654. hKey,
  655. dwParam,
  656. pbData,
  657. pdwDataLen, *pdwDataLen,
  658. dwFlags);
  659. DBG_TIME1;
  660. __try
  661. {
  662. __try
  663. {
  664. if (hKey <= MAX_GPK_OBJ)
  665. {
  666. bResult = Coherent(hProv);
  667. errcode = GetLastError();
  668. }
  669. if (bResult)
  670. {
  671. bResult = MyCPGetKeyParam (hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
  672. errcode = GetLastError();
  673. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  674. ProvCont[hProv].isContNameNullBlank)
  675. {
  676. // No access to the card has been done in this case
  677. }
  678. else
  679. {
  680. if (hKey <= MAX_GPK_OBJ)
  681. {
  682. // Select_MF(hProv); [NK] PIN not presented
  683. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  684. }
  685. }
  686. }
  687. else
  688. {
  689. bResult = CRYPT_FAILED;
  690. errcode = GetLastError();
  691. }
  692. }
  693. __except(EXCEPTION_EXECUTE_HANDLER)
  694. {
  695. bResult = CRYPT_FAILED;
  696. errcode = E_UNEXPECTED;
  697. }
  698. }
  699. __finally
  700. {
  701. DBG_TIME2;
  702. DBG_PRINT(TEXT("<-CPGetKeyParam(hProv:0x%08X, hKey:0x%08X, dwParam:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwFlags:0x%08X)\n returns %d in %d msec"),
  703. hProv,
  704. hKey,
  705. dwParam,
  706. pbData,
  707. pdwDataLen, *pdwDataLen,
  708. dwFlags,
  709. bResult,
  710. DBG_DELTA);
  711. GpkLocalUnlock();
  712. }
  713. RETURN( bResult, errcode );
  714. }
  715. ///////////////////////////////////////////////////////////////////////////////////////////
  716. ///////////////////////////////////////////////////////////////////////////////////////////
  717. BOOL WINAPI CPGetUserKey( IN HCRYPTPROV hProv,
  718. IN DWORD dwKeySpec,
  719. OUT HCRYPTKEY* phUserKey )
  720. {
  721. BOOL bResult = CRYPT_SUCCEED;
  722. DWORD errcode = ERROR_SUCCESS;
  723. GpkLocalLock();
  724. DBG_PRINT(TEXT("->CPGetUserKey(hProv:0x%08Xp, dwKeySpec:0x%08X, phUserKey:0x%p(0x%08X))"),
  725. hProv,
  726. dwKeySpec,
  727. phUserKey, *phUserKey);
  728. DBG_TIME1;
  729. __try
  730. {
  731. __try
  732. {
  733. if (Coherent(hProv))
  734. {
  735. bResult = MyCPGetUserKey( hProv, dwKeySpec, phUserKey );
  736. errcode = GetLastError();
  737. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  738. ProvCont[hProv].isContNameNullBlank)
  739. {
  740. // No access to the card has been done in this case
  741. }
  742. else
  743. {
  744. // Select_MF(hProv); [NK] PIN not presented
  745. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  746. }
  747. }
  748. else
  749. {
  750. bResult = CRYPT_FAILED;
  751. errcode = GetLastError();
  752. }
  753. }
  754. __except(EXCEPTION_EXECUTE_HANDLER)
  755. {
  756. bResult = CRYPT_FAILED;
  757. errcode = E_UNEXPECTED;
  758. }
  759. }
  760. __finally
  761. {
  762. DBG_TIME2;
  763. DBG_PRINT(TEXT("<-CPGetUserKey(hProv:0x%08X, dwKeySpec:0x%08X, phUserKey:0x%p(0x%08X))\n returns %d in %d msec"),
  764. hProv,
  765. dwKeySpec,
  766. phUserKey, *phUserKey,
  767. bResult,
  768. DBG_DELTA);
  769. GpkLocalUnlock();
  770. }
  771. RETURN( bResult, errcode );
  772. }
  773. ///////////////////////////////////////////////////////////////////////////////////////////
  774. ///////////////////////////////////////////////////////////////////////////////////////////
  775. BOOL WINAPI CPImportKey( IN HCRYPTPROV hProv,
  776. IN CONST BYTE* pbData,
  777. IN DWORD dwDataLen,
  778. IN HCRYPTKEY hPubKey,
  779. IN DWORD dwFlags,
  780. OUT HCRYPTKEY* phKey )
  781. {
  782. BOOL bResult = CRYPT_SUCCEED;
  783. DWORD errcode = ERROR_SUCCESS;
  784. GpkLocalLock();
  785. DBG_PRINT(TEXT("->CPImportKey(hProv:0x%08X, pbData:0x%p, dwDataLen:%d, hPubKey:0x%08X, dwFlags:0x%08X, phKey:0x%p(0x%08X))"),
  786. hProv,
  787. pbData,
  788. dwDataLen,
  789. hPubKey,
  790. dwFlags,
  791. phKey, *phKey);
  792. DBG_TIME1;
  793. __try
  794. {
  795. __try
  796. {
  797. BLOBHEADER BlobHeader;
  798. memcpy(&BlobHeader, pbData, sizeof(BLOBHEADER));
  799. // [FP] if we want to load a RSA private key into the GPK card,
  800. // the transaction is already opened - do not check the coherence -
  801. if ((!ProvCont[hProv].bCardTransactionOpened) && (BlobHeader.bType != PUBLICKEYBLOB))
  802. {
  803. bResult = Coherent( hProv );
  804. errcode = GetLastError();
  805. }
  806. if (bResult)
  807. {
  808. bResult = MyCPImportKey( hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey );
  809. errcode = GetLastError();
  810. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  811. ProvCont[hProv].isContNameNullBlank)
  812. {
  813. // No access to the card has been done in this case
  814. }
  815. else
  816. {
  817. if (BlobHeader.bType != PUBLICKEYBLOB)
  818. {
  819. Select_MF(hProv);
  820. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  821. }
  822. }
  823. }
  824. else
  825. {
  826. bResult = CRYPT_FAILED;
  827. errcode = GetLastError();
  828. }
  829. // [FP] close the transaction and reconnect in shared mode
  830. if (ProvCont[hProv].bCardTransactionOpened)
  831. {
  832. DWORD dwProto;
  833. ProvCont[hProv].bCardTransactionOpened = FALSE;
  834. errcode = SCardReconnect(ProvCont[hProv].hCard, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, SCARD_LEAVE_CARD, &dwProto);
  835. if (errcode != SCARD_S_SUCCESS)
  836. bResult = CRYPT_FAILED;
  837. }
  838. }
  839. __except(EXCEPTION_EXECUTE_HANDLER)
  840. {
  841. bResult = CRYPT_FAILED;
  842. errcode = E_UNEXPECTED;
  843. }
  844. }
  845. __finally
  846. {
  847. DBG_TIME2;
  848. DBG_PRINT(TEXT("<-CPImportKey(hProv:0x%08X, pbData:0x%p, dwDataLen:%d, hPubKey:0x%08X, dwFlags:0x%08X, phKey:0x%p(0x%08X))\n returns %d in %d msec"),
  849. hProv,
  850. pbData,
  851. dwDataLen,
  852. hPubKey,
  853. dwFlags,
  854. phKey, *phKey,
  855. bResult,
  856. DBG_DELTA);
  857. GpkLocalUnlock();
  858. }
  859. RETURN( bResult, errcode );
  860. }
  861. ///////////////////////////////////////////////////////////////////////////////////////////
  862. ///////////////////////////////////////////////////////////////////////////////////////////
  863. BOOL WINAPI CPSetKeyParam( IN HCRYPTPROV hProv,
  864. IN HCRYPTKEY hKey,
  865. IN DWORD dwParam,
  866. IN CONST BYTE* pbData,
  867. IN DWORD dwFlags )
  868. {
  869. BOOL bResult = CRYPT_SUCCEED;
  870. DWORD errcode = ERROR_SUCCESS;
  871. GpkLocalLock();
  872. DBG_PRINT(TEXT("->CPSetKeyParam(hProv:0x%08X, hKey:0x%08X, dwParam:0x%08X, pbData:0x%p, dwFlags:0x%08X)"),
  873. hProv,
  874. hKey,
  875. dwParam,
  876. pbData,
  877. dwFlags);
  878. DBG_TIME1;
  879. __try
  880. {
  881. __try
  882. {
  883. if (hKey <= MAX_GPK_OBJ)
  884. {
  885. bResult = Coherent(hProv);
  886. errcode = GetLastError();
  887. }
  888. if (bResult)
  889. {
  890. bResult = MyCPSetKeyParam( hProv, hKey, dwParam, pbData, dwFlags );
  891. errcode = GetLastError();
  892. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  893. ProvCont[hProv].isContNameNullBlank)
  894. {
  895. // No access to the card has been done in this case
  896. }
  897. else
  898. {
  899. if (hKey <= MAX_GPK_OBJ)
  900. {
  901. Select_MF(hProv);
  902. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  903. }
  904. }
  905. }
  906. else
  907. {
  908. bResult = CRYPT_FAILED;
  909. errcode = GetLastError();
  910. }
  911. }
  912. __except(EXCEPTION_EXECUTE_HANDLER)
  913. {
  914. bResult = CRYPT_FAILED;
  915. errcode = E_UNEXPECTED;
  916. }
  917. }
  918. __finally
  919. {
  920. DBG_TIME2;
  921. DBG_PRINT(TEXT("<-CPSetKeyParam(hProv:0x%08X, hKey:0x%08X, dwParam:0x%08X, pbData:0x%p, dwFlags:0x%08X)\n returns %d in %d msec"),
  922. hProv,
  923. hKey,
  924. dwParam,
  925. pbData,
  926. dwFlags,
  927. bResult,
  928. DBG_DELTA);
  929. GpkLocalUnlock();
  930. }
  931. RETURN( bResult, errcode );
  932. }
  933. ///////////////////////////////////////////////////////////////////////////////////////////
  934. ///////////////////////////////////////////////////////////////////////////////////////////
  935. BOOL WINAPI CPDecrypt( IN HCRYPTPROV hProv,
  936. IN HCRYPTKEY hKey,
  937. IN HCRYPTHASH hHash,
  938. IN BOOL Final,
  939. IN DWORD dwFlags,
  940. IN OUT BYTE* pbData,
  941. IN OUT DWORD* pdwDataLen )
  942. {
  943. BOOL bResult = CRYPT_SUCCEED;
  944. DWORD errcode = ERROR_SUCCESS;
  945. GpkLocalLock();
  946. DBG_PRINT(TEXT("->CPDecrypt(hProv:0x%08X, hKey:0x%08X, hHash:0x%08X, Final:%d, dwFlags:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d))"),
  947. hProv,
  948. hKey,
  949. hHash,
  950. Final,
  951. dwFlags,
  952. pbData,
  953. pdwDataLen, *pdwDataLen);
  954. DBG_TIME1;
  955. __try
  956. {
  957. __try
  958. {
  959. // We do not have to check the coherence in this case since the operation does not
  960. // use the card info
  961. bResult = MyCPDecrypt( hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen );
  962. errcode = GetLastError();
  963. }
  964. __except(EXCEPTION_EXECUTE_HANDLER)
  965. {
  966. bResult = CRYPT_FAILED;
  967. errcode = E_UNEXPECTED;
  968. }
  969. }
  970. __finally
  971. {
  972. DBG_TIME2;
  973. DBG_PRINT(TEXT("<-CPDecrypt(hProv:0x%08X, hKey:0x%08X, hHash:0x%08X, Final:%d, dwFlags:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d))\n returns %d in %d msec"),
  974. hProv,
  975. hKey,
  976. hHash,
  977. Final,
  978. dwFlags,
  979. pbData,
  980. pdwDataLen, *pdwDataLen,
  981. bResult,
  982. DBG_DELTA);
  983. GpkLocalUnlock();
  984. }
  985. RETURN( bResult, errcode );
  986. }
  987. ///////////////////////////////////////////////////////////////////////////////////////////
  988. ///////////////////////////////////////////////////////////////////////////////////////////
  989. BOOL WINAPI CPEncrypt( IN HCRYPTPROV hProv,
  990. IN HCRYPTKEY hKey,
  991. IN HCRYPTHASH hHash,
  992. IN BOOL Final,
  993. IN DWORD dwFlags,
  994. IN OUT BYTE* pbData,
  995. IN OUT DWORD* pdwDataLen,
  996. IN DWORD dwBufLen )
  997. {
  998. BOOL bResult = CRYPT_SUCCEED;
  999. DWORD errcode = ERROR_SUCCESS;
  1000. GpkLocalLock();
  1001. DBG_PRINT(TEXT("->CPEncrypt(hProv:0x%08X, hKey:0x%08X, hHash:0x%08X, Final:%d, dwFlags:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwBufLen:%d)"),
  1002. hProv,
  1003. hKey,
  1004. hHash,
  1005. Final,
  1006. dwFlags,
  1007. pbData,
  1008. pdwDataLen, *pdwDataLen,
  1009. dwBufLen);
  1010. DBG_TIME1;
  1011. __try
  1012. {
  1013. __try
  1014. {
  1015. // We do not have to check the coherence in this case since the operation does not
  1016. // use the card info
  1017. bResult = MyCPEncrypt( hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen );
  1018. errcode = GetLastError();
  1019. }
  1020. __except(EXCEPTION_EXECUTE_HANDLER)
  1021. {
  1022. bResult = CRYPT_FAILED;
  1023. errcode = E_UNEXPECTED;
  1024. }
  1025. }
  1026. __finally
  1027. {
  1028. DBG_TIME2;
  1029. DBG_PRINT(TEXT("<-CPEncrypt(hProv:0x%08X, hKey:0x%08X, hHash:0x%08X, Final:%d, dwFlags:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwBufLen:%d)\n returns %d in %d msec"),
  1030. hProv,
  1031. hKey,
  1032. hHash,
  1033. Final,
  1034. dwFlags,
  1035. pbData,
  1036. pdwDataLen, *pdwDataLen,
  1037. dwBufLen,
  1038. bResult,
  1039. DBG_DELTA);
  1040. GpkLocalUnlock();
  1041. }
  1042. RETURN( bResult, errcode );
  1043. }
  1044. ///////////////////////////////////////////////////////////////////////////////////////////
  1045. ///////////////////////////////////////////////////////////////////////////////////////////
  1046. BOOL WINAPI CPCreateHash( IN HCRYPTPROV hProv,
  1047. IN ALG_ID Algid,
  1048. IN HCRYPTKEY hKey,
  1049. IN DWORD dwFlags,
  1050. OUT HCRYPTHASH* phHash )
  1051. {
  1052. BOOL bResult = CRYPT_SUCCEED;
  1053. DWORD errcode = ERROR_SUCCESS;
  1054. GpkLocalLock();
  1055. DBG_PRINT(TEXT("->CPCreateHash(hProv:0x%08X, Algid:0x%08X, hKey:0x%08X, dwFlags:0x%08X, phHash:0x%p(0x%08X))"),
  1056. hProv,
  1057. Algid,
  1058. hKey,
  1059. dwFlags,
  1060. phHash, *phHash);
  1061. DBG_TIME1;
  1062. __try
  1063. {
  1064. __try
  1065. {
  1066. // We do not have to check the coherence in this case since the operation does not
  1067. // use the card info
  1068. bResult = MyCPCreateHash( hProv, Algid, hKey, dwFlags, phHash );
  1069. errcode = GetLastError();
  1070. }
  1071. __except(EXCEPTION_EXECUTE_HANDLER)
  1072. {
  1073. bResult = CRYPT_FAILED;
  1074. errcode = E_UNEXPECTED;
  1075. }
  1076. }
  1077. __finally
  1078. {
  1079. DBG_TIME2;
  1080. DBG_PRINT(TEXT("<-CPCreateHash(hProv:0x%08X, Algid:0x%08X, hKey:0x%08X, dwFlags:0x%08X, phHash:0x%p(0x%08X))\n returns %d in %d msec"),
  1081. hProv,
  1082. Algid,
  1083. hKey,
  1084. dwFlags,
  1085. phHash, *phHash,
  1086. bResult,
  1087. DBG_DELTA);
  1088. GpkLocalUnlock();
  1089. }
  1090. RETURN( bResult, errcode );
  1091. }
  1092. ///////////////////////////////////////////////////////////////////////////////////////////
  1093. ///////////////////////////////////////////////////////////////////////////////////////////
  1094. BOOL WINAPI CPDestroyHash( IN HCRYPTPROV hProv,
  1095. IN HCRYPTHASH hHash )
  1096. {
  1097. BOOL bResult = CRYPT_SUCCEED;
  1098. DWORD errcode = ERROR_SUCCESS;
  1099. GpkLocalLock();
  1100. DBG_PRINT(TEXT("->CPDestroyHash(hProv:0x%08X, hHash:0x%08X)"),
  1101. hProv,
  1102. hHash);
  1103. DBG_TIME1;
  1104. __try
  1105. {
  1106. __try
  1107. {
  1108. // We do not have to check the coherence in this case since the operation does not
  1109. // use the card info
  1110. bResult = MyCPDestroyHash( hProv, hHash );
  1111. errcode = GetLastError();
  1112. }
  1113. __except(EXCEPTION_EXECUTE_HANDLER)
  1114. {
  1115. bResult = CRYPT_FAILED;
  1116. errcode = E_UNEXPECTED;
  1117. }
  1118. }
  1119. __finally
  1120. {
  1121. DBG_TIME2;
  1122. DBG_PRINT(TEXT("<-CPDestroyHash(hProv:0x%08X, hHash:0x%08X)\n returns %d in %d msec"),
  1123. hProv,
  1124. hHash,
  1125. bResult,
  1126. DBG_DELTA);
  1127. GpkLocalUnlock();
  1128. }
  1129. RETURN( bResult, errcode );
  1130. }
  1131. ///////////////////////////////////////////////////////////////////////////////////////////
  1132. ///////////////////////////////////////////////////////////////////////////////////////////
  1133. BOOL WINAPI CPGetHashParam( IN HCRYPTPROV hProv,
  1134. IN HCRYPTHASH hHash,
  1135. IN DWORD dwParam,
  1136. IN BYTE* pbData,
  1137. IN DWORD* pdwDataLen,
  1138. IN DWORD dwFlags )
  1139. {
  1140. BOOL bResult = CRYPT_SUCCEED;
  1141. DWORD errcode = ERROR_SUCCESS;
  1142. GpkLocalLock();
  1143. DBG_PRINT(TEXT("->CPGetHashParam(hProv:0x%08X, hHash:0x%08X, dwParam:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwFlags:0x%08X)"),
  1144. hProv,
  1145. hHash,
  1146. dwParam,
  1147. pbData,
  1148. pdwDataLen, *pdwDataLen,
  1149. dwFlags);
  1150. DBG_TIME1;
  1151. __try
  1152. {
  1153. __try
  1154. {
  1155. // We do not have to check the coherence in this case since the operation does not
  1156. // use the card info
  1157. bResult = MyCPGetHashParam( hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags );
  1158. errcode = GetLastError();
  1159. }
  1160. __except(EXCEPTION_EXECUTE_HANDLER)
  1161. {
  1162. bResult = CRYPT_FAILED;
  1163. errcode = E_UNEXPECTED;
  1164. }
  1165. }
  1166. __finally
  1167. {
  1168. DBG_TIME2;
  1169. DBG_PRINT(TEXT("<-CPGetHashParam(hProv:0x%08X, hHash:0x%08X, dwParam:0x%08X, pbData:0x%p, pdwDataLen:0x%p(%d), dwFlags:0x%08X)\n returns %d in %d msec"),
  1170. hProv,
  1171. hHash,
  1172. dwParam,
  1173. pbData,
  1174. pdwDataLen, *pdwDataLen,
  1175. dwFlags,
  1176. bResult,
  1177. DBG_DELTA);
  1178. GpkLocalUnlock();
  1179. }
  1180. RETURN( bResult, errcode );
  1181. }
  1182. ///////////////////////////////////////////////////////////////////////////////////////////
  1183. ///////////////////////////////////////////////////////////////////////////////////////////
  1184. BOOL WINAPI CPHashData( IN HCRYPTPROV hProv,
  1185. IN HCRYPTHASH hHash,
  1186. IN CONST BYTE* pbData,
  1187. IN DWORD dwDataLen,
  1188. IN DWORD dwFlags )
  1189. {
  1190. BOOL bResult = CRYPT_SUCCEED;
  1191. DWORD errcode = ERROR_SUCCESS;
  1192. GpkLocalLock();
  1193. DBG_PRINT(TEXT("->CPHashData(hProv:0x%08X, hHash:0x%08X, pbData:0x%p, dwDataLen:%d, dwFlags:0x%08X)"),
  1194. hProv,
  1195. hHash,
  1196. pbData,
  1197. dwDataLen,
  1198. dwFlags);
  1199. DBG_TIME1;
  1200. __try
  1201. {
  1202. __try
  1203. {
  1204. // We do not have to check the coherence in this case since the operation does not
  1205. // use the card info
  1206. bResult = MyCPHashData( hProv, hHash, pbData, dwDataLen, dwFlags );
  1207. errcode = GetLastError();
  1208. }
  1209. __except(EXCEPTION_EXECUTE_HANDLER)
  1210. {
  1211. bResult = CRYPT_FAILED;
  1212. errcode = E_UNEXPECTED;
  1213. }
  1214. }
  1215. __finally
  1216. {
  1217. DBG_TIME2;
  1218. DBG_PRINT(TEXT("<-CPHashData(hProv:0x%08X, hHash:0x%08X, pbData:0x%p, dwDataLen:%d, dwFlags:0x%08X)\n returns %d in %d msec"),
  1219. hProv,
  1220. hHash,
  1221. pbData,
  1222. dwDataLen,
  1223. dwFlags,
  1224. bResult,
  1225. DBG_DELTA);
  1226. GpkLocalUnlock();
  1227. }
  1228. RETURN( bResult, errcode );
  1229. }
  1230. ///////////////////////////////////////////////////////////////////////////////////////////
  1231. ///////////////////////////////////////////////////////////////////////////////////////////
  1232. BOOL WINAPI CPHashSessionKey( IN HCRYPTPROV hProv,
  1233. IN HCRYPTHASH hHash,
  1234. IN HCRYPTKEY hKey,
  1235. IN DWORD dwFlags )
  1236. {
  1237. BOOL bResult = CRYPT_SUCCEED;
  1238. DWORD errcode = ERROR_SUCCESS;
  1239. GpkLocalLock();
  1240. DBG_PRINT(TEXT("->CPHashSessionKey(hProv:0x%08X, hHash:0x%08X, hKey:0x%08X, dwFlags:0x%08X)"),
  1241. hProv,
  1242. hHash,
  1243. hKey,
  1244. dwFlags);
  1245. DBG_TIME1;
  1246. __try
  1247. {
  1248. __try
  1249. {
  1250. // We do not have to check the coherence in this case since the operation does not
  1251. // use the card info
  1252. bResult = MyCPHashSessionKey( hProv, hHash, hKey, dwFlags );
  1253. errcode = GetLastError();
  1254. }
  1255. __except(EXCEPTION_EXECUTE_HANDLER)
  1256. {
  1257. bResult = CRYPT_FAILED;
  1258. errcode = E_UNEXPECTED;
  1259. }
  1260. }
  1261. __finally
  1262. {
  1263. DBG_TIME2;
  1264. DBG_PRINT(TEXT("<-CPHashSessionKey(hProv:0x%08X, hHash:0x%08X, hKey:0x%08X, dwFlags:0x%08X)\n returns %d in %d msec"),
  1265. hProv,
  1266. hHash,
  1267. hKey,
  1268. dwFlags,
  1269. bResult,
  1270. DBG_DELTA);
  1271. GpkLocalUnlock();
  1272. }
  1273. RETURN( bResult, errcode );
  1274. }
  1275. ///////////////////////////////////////////////////////////////////////////////////////////
  1276. ///////////////////////////////////////////////////////////////////////////////////////////
  1277. BOOL WINAPI CPSetHashParam( IN HCRYPTPROV hProv,
  1278. IN HCRYPTHASH hHash,
  1279. IN DWORD dwParam,
  1280. IN CONST BYTE* pbData,
  1281. IN DWORD dwFlags )
  1282. {
  1283. BOOL bResult = CRYPT_SUCCEED;
  1284. DWORD errcode = ERROR_SUCCESS;
  1285. GpkLocalLock();
  1286. DBG_PRINT(TEXT("->CPSetHashParam(hProv:0x%08X, hHash:0x%08X, dwParam:0x%08X, pbData:0x%p, dwFlags:0x%08X)"),
  1287. hProv,
  1288. hHash,
  1289. dwParam,
  1290. pbData,
  1291. dwFlags);
  1292. DBG_TIME1;
  1293. __try
  1294. {
  1295. __try
  1296. {
  1297. // We do not have to check the coherence in this case since the operation does not
  1298. // use the card info
  1299. bResult = MyCPSetHashParam( hProv, hHash, dwParam, pbData, dwFlags );
  1300. errcode = GetLastError();
  1301. }
  1302. __except(EXCEPTION_EXECUTE_HANDLER)
  1303. {
  1304. bResult = CRYPT_FAILED;
  1305. errcode = E_UNEXPECTED;
  1306. }
  1307. }
  1308. __finally
  1309. {
  1310. DBG_TIME2;
  1311. DBG_PRINT(TEXT("<-CPSetHashParam(hProv:0x%08X, hHash:0x%08X, dwParam:0x%08X, pbData:0x%p, dwFlags:0x%08X)\n returns %d in %d msec"),
  1312. hProv,
  1313. hHash,
  1314. dwParam,
  1315. pbData,
  1316. dwFlags,
  1317. bResult,
  1318. DBG_DELTA);
  1319. GpkLocalUnlock();
  1320. }
  1321. RETURN( bResult, errcode );
  1322. }
  1323. ///////////////////////////////////////////////////////////////////////////////////////////
  1324. ///////////////////////////////////////////////////////////////////////////////////////////
  1325. BOOL WINAPI CPSignHash(IN HCRYPTPROV hProv,
  1326. IN HCRYPTHASH hHash,
  1327. IN DWORD dwKeySpec,
  1328. IN LPCWSTR sDescription,
  1329. IN DWORD dwFlags,
  1330. OUT BYTE* pbSignature,
  1331. OUT DWORD* pdwSigLen )
  1332. {
  1333. BOOL bResult = CRYPT_SUCCEED;
  1334. DWORD errcode = ERROR_SUCCESS;
  1335. GpkLocalLock();
  1336. DBG_PRINT(TEXT("->CPSignHash(hProv:0x%08X, hHash:0x%08X, dwKeySpec:0x%08X, sDescription:0x%p('%s'), dwFlags:0x%08X, pbSignature:0x%p, pdwSigLen:0x%p(%d))"),
  1337. hProv,
  1338. hHash,
  1339. dwKeySpec,
  1340. sDescription, sDescription,
  1341. dwFlags,
  1342. pbSignature,
  1343. pdwSigLen, *pdwSigLen);
  1344. DBG_TIME1;
  1345. __try
  1346. {
  1347. __try
  1348. {
  1349. if (Coherent(hProv))
  1350. {
  1351. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT)
  1352. {
  1353. bResult = CRYPT_FAILED;
  1354. errcode = NTE_PERM;
  1355. }
  1356. else
  1357. {
  1358. bResult = MyCPSignHash( hProv, hHash, dwKeySpec, sDescription, dwFlags, pbSignature, pdwSigLen );
  1359. errcode = GetLastError();
  1360. if (pbSignature != 0)
  1361. Select_MF(hProv);
  1362. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  1363. }
  1364. }
  1365. else
  1366. {
  1367. bResult = CRYPT_FAILED;
  1368. errcode = GetLastError();
  1369. }
  1370. }
  1371. __except(EXCEPTION_EXECUTE_HANDLER)
  1372. {
  1373. bResult = CRYPT_FAILED;
  1374. errcode = E_UNEXPECTED;
  1375. }
  1376. }
  1377. __finally
  1378. {
  1379. DBG_TIME2;
  1380. DBG_PRINT(TEXT("<-CPSignHash(hProv:0x%08X, hHash:0x%08X, dwKeySpec:0x%08X, sDescription:0x%p('%s'), dwFlags:0x%08X, pbSignature:0x%p, pdwSigLen:0x%p(%d))\n returns %d in %d msec"),
  1381. hProv,
  1382. hHash,
  1383. dwKeySpec,
  1384. sDescription, sDescription,
  1385. dwFlags,
  1386. pbSignature,
  1387. pdwSigLen, *pdwSigLen,
  1388. bResult,
  1389. DBG_DELTA);
  1390. GpkLocalUnlock();
  1391. }
  1392. RETURN( bResult, errcode );
  1393. }
  1394. ///////////////////////////////////////////////////////////////////////////////////////////
  1395. ///////////////////////////////////////////////////////////////////////////////////////////
  1396. BOOL WINAPI CPVerifySignature( IN HCRYPTPROV hProv,
  1397. IN HCRYPTHASH hHash,
  1398. IN CONST BYTE* pbSignature,
  1399. IN DWORD dwSigLen,
  1400. IN HCRYPTKEY hPubKey,
  1401. IN LPCWSTR sDescription,
  1402. IN DWORD dwFlags )
  1403. {
  1404. BOOL bResult = CRYPT_SUCCEED;
  1405. DWORD errcode = ERROR_SUCCESS;
  1406. GpkLocalLock();
  1407. DBG_PRINT(TEXT("->CPVerifySignature(hProv:0x%08X, hHash:0x%08X, pbSignature:0x%p, dwSigLen:%d, hPubKey:0x%08X, sDescription:0x%p('%s'), dwFlags:0x%08X)"),
  1408. hProv,
  1409. hHash,
  1410. pbSignature,
  1411. dwSigLen,
  1412. hPubKey,
  1413. sDescription, sDescription,
  1414. dwFlags);
  1415. DBG_TIME1;
  1416. __try
  1417. {
  1418. __try
  1419. {
  1420. if (hPubKey <= MAX_GPK_OBJ)
  1421. {
  1422. bResult = Coherent(hProv);
  1423. errcode = GetLastError();
  1424. }
  1425. if (bResult)
  1426. {
  1427. bResult = MyCPVerifySignature( hProv, hHash, pbSignature, dwSigLen, hPubKey, sDescription, dwFlags );
  1428. errcode = GetLastError();
  1429. if (ProvCont[hProv].Flags & CRYPT_VERIFYCONTEXT &&
  1430. ProvCont[hProv].isContNameNullBlank)
  1431. {
  1432. // No access to the card has been done in this case
  1433. }
  1434. else
  1435. {
  1436. if (hPubKey <= MAX_GPK_OBJ)
  1437. {
  1438. // Select_MF(hProv); // NK PIN not presented
  1439. SCardEndTransaction(ProvCont[hProv].hCard, SCARD_LEAVE_CARD);
  1440. }
  1441. }
  1442. }
  1443. else
  1444. {
  1445. bResult = CRYPT_FAILED;
  1446. errcode = GetLastError();
  1447. }
  1448. }
  1449. __except(EXCEPTION_EXECUTE_HANDLER)
  1450. {
  1451. bResult = CRYPT_FAILED;
  1452. errcode = E_UNEXPECTED;
  1453. }
  1454. }
  1455. __finally
  1456. {
  1457. DBG_TIME2;
  1458. DBG_PRINT(TEXT("<-CPVerifySignature(hProv:0x%08X, hHash:0x%08X, pbSignature:0x%p, dwSigLen:%d, hPubKey:0x%08X, sDescription:0x%p('%s'), dwFlags:0x%08X)\n returns %d in %d msec"),
  1459. hProv,
  1460. hHash,
  1461. pbSignature,
  1462. dwSigLen,
  1463. hPubKey,
  1464. sDescription, sDescription,
  1465. dwFlags,
  1466. bResult,
  1467. DBG_DELTA);
  1468. GpkLocalUnlock();
  1469. }
  1470. RETURN( bResult, errcode );
  1471. }