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.

961 lines
32 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1998 - 1999
  3. Module Name:
  4. showTrace
  5. Abstract:
  6. This module implements the CSP Tracing interpretation
  7. Author:
  8. Doug Barlow (dbarlow) 5/16/1998
  9. Notes:
  10. ?Notes?
  11. --*/
  12. #ifndef WIN32_LEAN_AND_MEAN
  13. #define WIN32_LEAN_AND_MEAN
  14. #endif
  15. #include <windows.h>
  16. #ifndef _WIN32_WINNT
  17. #define _WIN32_WINNT 0x0500
  18. #endif
  19. #include <wincrypt.h>
  20. #include <stdlib.h>
  21. #include <iostream.h>
  22. #include <iomanip.h>
  23. #include <SCardLib.h>
  24. #include "cspTrace.h"
  25. //
  26. // Definitions duplicated from logcsp.
  27. //
  28. static LPCTSTR
  29. CPNames[]
  30. = {
  31. TEXT("CryptAcquireContext"),
  32. TEXT("CryptGetProvParam"),
  33. TEXT("CryptReleaseContext"),
  34. TEXT("CryptSetProvParam"),
  35. TEXT("CryptDeriveKey"),
  36. TEXT("CryptDestroyKey"),
  37. TEXT("CryptExportKey"),
  38. TEXT("CryptGenKey"),
  39. TEXT("CryptGetKeyParam"),
  40. TEXT("CryptGenRandom"),
  41. TEXT("CryptGetUserKey"),
  42. TEXT("CryptImportKey"),
  43. TEXT("CryptSetKeyParam"),
  44. TEXT("CryptEncrypt"),
  45. TEXT("CryptDecrypt"),
  46. TEXT("CryptCreateHash"),
  47. TEXT("CryptDestroyHash"),
  48. TEXT("CryptGetHashParam"),
  49. TEXT("CryptHashData"),
  50. TEXT("CryptHashSessionKey"),
  51. TEXT("CryptSetHashParam"),
  52. TEXT("CryptSignHash"),
  53. TEXT("CryptVerifySignature"),
  54. NULL };
  55. static void
  56. ShowBuf(
  57. LPCTSTR szName,
  58. LogBuffer &lb,
  59. ostream &outStr);
  60. static void
  61. dump(
  62. const BYTE *pbData,
  63. DWORD cbLen,
  64. ostream &outStr);
  65. static void
  66. MapValue(
  67. ostream &outStr,
  68. DWORD dwValue,
  69. LPCTSTR szLeader,
  70. const ValueMap *rgMap);
  71. static void
  72. MaskValue(
  73. ostream &outStr,
  74. DWORD dwValue,
  75. LPCTSTR szLeader,
  76. const ValueMap *rgMap);
  77. const ValueMap rgMapService[]
  78. = { MAP(AcquireContext), MAP(GetProvParam), MAP(ReleaseContext),
  79. MAP(SetProvParam), MAP(DeriveKey), MAP(DestroyKey),
  80. MAP(ExportKey), MAP(GenKey), MAP(GetKeyParam),
  81. MAP(GenRandom), MAP(GetUserKey), MAP(ImportKey),
  82. MAP(SetKeyParam), MAP(Encrypt), MAP(Decrypt),
  83. MAP(CreateHash), MAP(DestroyHash), MAP(GetHashParam),
  84. MAP(HashData), MAP(HashSessionKey), MAP(SetHashParam),
  85. MAP(SignHash), MAP(VerifySignature),
  86. { 0, NULL } };
  87. // dwFlags definitions for CryptAcquireContext
  88. const ValueMap rgMapAcquireFlags[]
  89. = { MAP(CRYPT_VERIFYCONTEXT), MAP(CRYPT_NEWKEYSET),
  90. MAP(CRYPT_DELETEKEYSET), MAP(CRYPT_MACHINE_KEYSET),
  91. MAP(CRYPT_SILENT),
  92. { 0, NULL } };
  93. // Parameter definitions for CryptGetProvParam
  94. const ValueMap rgMapGetProvParam[]
  95. = { MAP(PP_ENUMALGS), MAP(PP_ENUMCONTAINERS),
  96. MAP(PP_IMPTYPE), MAP(PP_NAME),
  97. MAP(PP_VERSION), MAP(PP_CONTAINER),
  98. MAP(PP_CHANGE_PASSWORD), MAP(PP_KEYSET_SEC_DESCR),
  99. MAP(PP_CERTCHAIN), MAP(PP_KEY_TYPE_SUBTYPE),
  100. MAP(PP_PROVTYPE), MAP(PP_KEYSTORAGE),
  101. MAP(PP_APPLI_CERT), MAP(PP_SYM_KEYSIZE),
  102. MAP(PP_SESSION_KEYSIZE), MAP(PP_UI_PROMPT),
  103. MAP(PP_ENUMALGS_EX), MAP(PP_ENUMMANDROOTS),
  104. MAP(PP_ENUMELECTROOTS), MAP(PP_KEYSET_TYPE),
  105. MAP(PP_ADMIN_PIN), MAP(PP_KEYEXCHANGE_PIN),
  106. MAP(PP_SIGNATURE_PIN), MAP(PP_SIG_KEYSIZE_INC),
  107. MAP(PP_KEYX_KEYSIZE_INC), MAP(PP_UNIQUE_CONTAINER),
  108. { 0, NULL } };
  109. // Flag definitions for CryptGetProvParam
  110. const ValueMap rgMapGetProvFlags[]
  111. = { MAP(CRYPT_FIRST), MAP(CRYPT_NEXT),
  112. { 0, NULL } };
  113. // Parameter definitions for CryptSetProvParam
  114. const ValueMap rgMapSetProvParam[]
  115. = {
  116. MAP(PP_CLIENT_HWND), MAP(PP_ENUMCONTAINERS),
  117. MAP(PP_IMPTYPE), MAP(PP_NAME),
  118. MAP(PP_VERSION), MAP(PP_CONTAINER),
  119. MAP(PP_CHANGE_PASSWORD), MAP(PP_KEYSET_SEC_DESCR),
  120. MAP(PP_CERTCHAIN), MAP(PP_KEY_TYPE_SUBTYPE),
  121. MAP(PP_CONTEXT_INFO), MAP(PP_KEYEXCHANGE_KEYSIZE),
  122. MAP(PP_SIGNATURE_KEYSIZE), MAP(PP_KEYEXCHANGE_ALG),
  123. MAP(PP_SIGNATURE_ALG), MAP(PP_PROVTYPE),
  124. MAP(PP_KEYSTORAGE), MAP(PP_APPLI_CERT),
  125. MAP(PP_SYM_KEYSIZE), MAP(PP_SESSION_KEYSIZE),
  126. MAP(PP_UI_PROMPT), MAP(PP_ENUMALGS_EX),
  127. MAP(PP_DELETEKEY), MAP(PP_ENUMMANDROOTS),
  128. MAP(PP_ENUMELECTROOTS), MAP(PP_KEYSET_TYPE),
  129. MAP(PP_ADMIN_PIN), MAP(PP_KEYEXCHANGE_PIN),
  130. MAP(PP_SIGNATURE_PIN),
  131. { 0, NULL } };
  132. // Parameter definitions for Hash Param
  133. const ValueMap rgMapHashParam[]
  134. = {
  135. MAP(HP_ALGID), MAP(HP_HASHVAL),
  136. MAP(HP_HASHSIZE), MAP(HP_HMAC_INFO),
  137. MAP(HP_TLS1PRF_LABEL), MAP(HP_TLS1PRF_SEED),
  138. { 0, NULL } };
  139. // dwFlag definitions for CryptGenKey
  140. const ValueMap rgMapGenKeyFlags[]
  141. = { MAP(CRYPT_EXPORTABLE), MAP(CRYPT_USER_PROTECTED),
  142. MAP(CRYPT_CREATE_SALT), MAP(CRYPT_UPDATE_KEY),
  143. MAP(CRYPT_NO_SALT), MAP(CRYPT_PREGEN),
  144. MAP(CRYPT_RECIPIENT), MAP(CRYPT_INITIATOR),
  145. MAP(CRYPT_ONLINE), MAP(CRYPT_SF),
  146. MAP(CRYPT_CREATE_IV), MAP(CRYPT_KEK),
  147. MAP(CRYPT_DATA_KEY), MAP(CRYPT_VOLATILE),
  148. { 0, NULL } };
  149. // dwFlags definitions for CryptDeriveKey
  150. const ValueMap rgMapDeriveKeyFlags[]
  151. = { MAP(CRYPT_SERVER),
  152. { 0, NULL } };
  153. // dwFlag definitions for CryptExportKey
  154. const ValueMap rgMapExportKeyFlags[]
  155. = { MAP(CRYPT_Y_ONLY), MAP(CRYPT_SSL2_FALLBACK),
  156. MAP(CRYPT_DESTROYKEY),
  157. { 0, NULL } };
  158. // Parameter IDs for Get and Set KeyParam
  159. const ValueMap rgMapKeyParam[]
  160. = { MAP(KP_IV), MAP(KP_SALT),
  161. MAP(KP_PADDING), MAP(KP_MODE),
  162. MAP(KP_MODE_BITS), MAP(KP_PERMISSIONS),
  163. MAP(KP_ALGID), MAP(KP_BLOCKLEN),
  164. MAP(KP_KEYLEN), MAP(KP_SALT_EX),
  165. MAP(KP_P), MAP(KP_G),
  166. MAP(KP_Q), MAP(KP_X),
  167. MAP(KP_Y), MAP(KP_RA),
  168. MAP(KP_RB), MAP(KP_INFO),
  169. MAP(KP_EFFECTIVE_KEYLEN), MAP(KP_SCHANNEL_ALG),
  170. MAP(KP_CLIENT_RANDOM), MAP(KP_SERVER_RANDOM),
  171. MAP(KP_RP), MAP(KP_PRECOMP_MD5),
  172. MAP(KP_PRECOMP_SHA), MAP(KP_CERTIFICATE),
  173. MAP(KP_CLEAR_KEY), MAP(KP_PUB_EX_LEN),
  174. MAP(KP_PUB_EX_VAL), MAP(KP_KEYVAL),
  175. MAP(KP_ADMIN_PIN), MAP(KP_KEYEXCHANGE_PIN),
  176. MAP(KP_SIGNATURE_PIN), MAP(KP_PREHASH),
  177. { 0, NULL } };
  178. // Key Type Id Definitions
  179. const ValueMap rgMapKeyId[]
  180. = { MAP(AT_KEYEXCHANGE), MAP(AT_SIGNATURE),
  181. { 0, NULL } };
  182. // exported key blob definitions
  183. const ValueMap rgMapBlobType[]
  184. = { MAP(SIMPLEBLOB), MAP(PUBLICKEYBLOB),
  185. MAP(PRIVATEKEYBLOB), MAP(PLAINTEXTKEYBLOB),
  186. MAP(OPAQUEKEYBLOB),
  187. { 0, NULL } };
  188. // algorithm identifier definitions
  189. const ValueMap rgMapAlgId[]
  190. = { MAP(AT_KEYEXCHANGE), MAP(AT_SIGNATURE),
  191. MAP(CALG_MD2), MAP(CALG_MD4),
  192. MAP(CALG_MD5), MAP(CALG_SHA),
  193. MAP(CALG_SHA1), MAP(CALG_MAC),
  194. MAP(CALG_RSA_SIGN), MAP(CALG_DSS_SIGN),
  195. MAP(CALG_RSA_KEYX), MAP(CALG_DES),
  196. MAP(CALG_3DES_112), MAP(CALG_3DES),
  197. MAP(CALG_RC2), MAP(CALG_RC4),
  198. MAP(CALG_SEAL), MAP(CALG_DH_SF),
  199. MAP(CALG_DH_EPHEM), MAP(CALG_AGREEDKEY_ANY),
  200. MAP(CALG_KEA_KEYX), MAP(CALG_HUGHES_MD5),
  201. MAP(CALG_SKIPJACK), MAP(CALG_TEK),
  202. MAP(CALG_CYLINK_MEK), MAP(CALG_SSL3_SHAMD5),
  203. MAP(CALG_SSL3_MASTER), MAP(CALG_SCHANNEL_MASTER_HASH),
  204. MAP(CALG_SCHANNEL_MAC_KEY), MAP(CALG_SCHANNEL_ENC_KEY),
  205. MAP(CALG_PCT1_MASTER), MAP(CALG_SSL2_MASTER),
  206. MAP(CALG_TLS1_MASTER), MAP(CALG_RC5),
  207. MAP(CALG_HMAC), MAP(CALG_TLS1PRF),
  208. { 0, NULL } };
  209. // ?Definitions?
  210. // MaskValue(cout, pld->dwFlags, TEXT("Flags: "), rgMap?what?Flags);
  211. static const ValueMap rgMapDemo[]
  212. = {
  213. { 0, NULL } };
  214. static LPBYTE l_pbLogData = NULL;
  215. static DWORD l_cbLogData = 0;
  216. /*++
  217. DoShowTrace:
  218. This routine interprets the given binary file, writing the output to stdout.
  219. Arguments:
  220. szInFile supplies the file name to be parsed.
  221. Return Value:
  222. None
  223. Remarks:
  224. ?Remarks?
  225. Author:
  226. Doug Barlow (dbarlow) 5/16/1998
  227. --*/
  228. void
  229. DoShowTrace(
  230. IN LPCTSTR szInFile)
  231. {
  232. HANDLE hLogFile = NULL;
  233. DWORD cbStructLen = 0;
  234. LPBYTE pbStruct = NULL;
  235. LogHeader *pLogObj;
  236. DWORD dwLen, dwRead;
  237. BOOL fSts;
  238. //
  239. // Open the log file.
  240. //
  241. hLogFile = CreateFile(
  242. szInFile,
  243. GENERIC_READ,
  244. FILE_SHARE_READ | FILE_SHARE_WRITE,
  245. NULL,
  246. OPEN_EXISTING,
  247. FILE_ATTRIBUTE_NORMAL,
  248. NULL);
  249. if (INVALID_HANDLE_VALUE == hLogFile)
  250. {
  251. cerr << TEXT("Can't open file ")
  252. << szInFile
  253. << ": "
  254. << CErrorString(GetLastError())
  255. << endl;
  256. goto ErrorExit;
  257. }
  258. //
  259. // Parse the file contents.
  260. //
  261. for (;;)
  262. {
  263. fSts = ReadFile(
  264. hLogFile,
  265. &dwLen,
  266. sizeof(DWORD),
  267. &dwRead,
  268. NULL);
  269. if ((!fSts) || (0 == dwRead))
  270. goto ErrorExit;
  271. if (cbStructLen < dwLen)
  272. {
  273. if (NULL != pbStruct)
  274. LocalFree(pbStruct);
  275. pbStruct = (LPBYTE)LocalAlloc(LPTR, dwLen);
  276. cbStructLen = dwLen;
  277. }
  278. fSts = ReadFile(
  279. hLogFile,
  280. &pbStruct[sizeof(DWORD)],
  281. dwLen - sizeof(DWORD),
  282. &dwRead,
  283. NULL);
  284. if (!fSts)
  285. {
  286. cerr << "File read error: " << CErrorString(GetLastError()) << endl;
  287. goto ErrorExit;
  288. }
  289. //
  290. // Parse the structure into bytesize chunks.
  291. //
  292. pLogObj = (LogHeader *)pbStruct;
  293. pLogObj->cbLength = dwLen;
  294. l_pbLogData = pbStruct + pLogObj->cbDataOffset;
  295. l_cbLogData = pLogObj->cbLength - pLogObj->cbDataOffset;
  296. //
  297. // We've got the structure, now display the contents.
  298. //
  299. cout
  300. << TEXT("-----------------------------------------------------\n")
  301. << flush;
  302. MapValue(cout, pLogObj->id, TEXT("Service: "), rgMapService);
  303. switch (pLogObj->status)
  304. {
  305. case logid_False:
  306. cout << TEXT("Error returned") << endl;
  307. // Fall through intentionally
  308. case logid_True:
  309. cout
  310. << TEXT("Status: ") << CErrorString(pLogObj->dwStatus)
  311. << endl;
  312. break;
  313. case logid_Exception:
  314. cout << TEXT("Exception Thrown\n") << flush;
  315. break;
  316. default:
  317. cerr << TEXT("Trace Log error: invalid Call Status.") << endl;
  318. goto ErrorExit;
  319. }
  320. cout
  321. << TEXT("Process/Thread: ")
  322. << PHex(pLogObj->dwProcId) << TEXT("/") << PHex(pLogObj->dwThreadId)
  323. << endl;
  324. cout
  325. << TEXT("Time: ")
  326. << PTime(pLogObj->startTime) << TEXT(" - ") << PTime(pLogObj->endTime)
  327. << endl;
  328. switch (pLogObj->id)
  329. {
  330. case AcquireContext:
  331. {
  332. struct TmpLog {
  333. LogHeader lh;
  334. LogBuffer bfContainer;
  335. DWORD dwFlags;
  336. LogBuffer bfVTable;
  337. HCRYPTPROV hProv;
  338. } *pld = (struct TmpLog *)pLogObj;
  339. ShowBuf(TEXT("Container: "), pld->bfContainer, cout);
  340. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), rgMapAcquireFlags);
  341. ShowBuf(TEXT("VTable "), pld->bfVTable, cout);
  342. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  343. break;
  344. }
  345. case GetProvParam:
  346. {
  347. struct TmpLog {
  348. LogHeader lh;
  349. HCRYPTPROV hProv;
  350. DWORD dwParam;
  351. DWORD dwDataLen;
  352. DWORD dwFlags;
  353. LogBuffer bfData;
  354. } *pld = (struct TmpLog *)pLogObj;
  355. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  356. MapValue(cout, pld->dwParam, TEXT("Param Id: "), rgMapGetProvParam);
  357. cout << TEXT("Buffer Space: ") << PHex(pld->dwDataLen) << endl;
  358. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), rgMapGetProvFlags);
  359. ShowBuf(TEXT("Returned Data: "), pld->bfData, cout);
  360. break;
  361. }
  362. case ReleaseContext:
  363. {
  364. struct TmpLog {
  365. LogHeader lh;
  366. HCRYPTPROV hProv;
  367. DWORD dwFlags;
  368. } *pld = (struct TmpLog *)pLogObj;
  369. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  370. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  371. break;
  372. }
  373. case SetProvParam:
  374. {
  375. struct TmpLog {
  376. LogHeader lh;
  377. HCRYPTPROV hProv;
  378. DWORD dwParam;
  379. LogBuffer bfData;
  380. DWORD dwFlags;
  381. } *pld = (struct TmpLog *)pLogObj;
  382. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  383. MapValue(cout, pld->dwParam, TEXT("Param Id: "), rgMapSetProvParam);
  384. ShowBuf(TEXT("Supplied Data: "), pld->bfData, cout);
  385. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  386. break;
  387. }
  388. case DeriveKey:
  389. {
  390. struct TmpLog {
  391. LogHeader lh;
  392. HCRYPTPROV hProv;
  393. ALG_ID Algid;
  394. HCRYPTHASH hHash;
  395. DWORD dwFlags;
  396. HCRYPTKEY hKey;
  397. } *pld = (struct TmpLog *)pLogObj;
  398. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  399. MapValue(cout, pld->Algid, TEXT("Algorithm: "), rgMapAlgId);
  400. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  401. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), rgMapDeriveKeyFlags);
  402. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  403. break;
  404. }
  405. case DestroyKey:
  406. {
  407. struct TmpLog {
  408. LogHeader lh;
  409. HCRYPTPROV hProv;
  410. HCRYPTKEY hKey;
  411. } *pld = (struct TmpLog *)pLogObj;
  412. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  413. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  414. break;
  415. }
  416. case ExportKey:
  417. {
  418. struct TmpLog {
  419. LogHeader lh;
  420. HCRYPTPROV hProv;
  421. HCRYPTKEY hKey;
  422. HCRYPTKEY hPubKey;
  423. DWORD dwBlobType;
  424. DWORD dwFlags;
  425. DWORD dwDataLen;
  426. LogBuffer bfData;
  427. } *pld = (struct TmpLog *)pLogObj;
  428. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  429. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  430. cout << TEXT("HCRYPTpubKEY: ") << PHex(pld->hPubKey) << endl;
  431. MapValue(cout, pld->dwBlobType, TEXT("BlobType: "), rgMapBlobType);
  432. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), rgMapExportKeyFlags);
  433. cout << TEXT("Buffer Space: ") << PHex(pld->dwDataLen) << endl;
  434. ShowBuf(TEXT("Returned Data: "), pld->bfData, cout);
  435. break;
  436. }
  437. case GenKey:
  438. {
  439. struct TmpLog {
  440. LogHeader lh;
  441. HCRYPTPROV hProv;
  442. ALG_ID Algid;
  443. DWORD dwFlags;
  444. HCRYPTKEY hKey;
  445. } *pld = (struct TmpLog *)pLogObj;
  446. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  447. MapValue(cout, pld->Algid, TEXT("Algorithm: "), rgMapAlgId);
  448. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), rgMapGenKeyFlags);
  449. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  450. break;
  451. }
  452. case GetKeyParam:
  453. {
  454. struct TmpLog {
  455. LogHeader lh;
  456. HCRYPTPROV hProv;
  457. HCRYPTKEY hKey;
  458. DWORD dwParam;
  459. DWORD dwDataLen;
  460. DWORD dwFlags;
  461. LogBuffer bfData;
  462. } *pld = (struct TmpLog *)pLogObj;
  463. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  464. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  465. MapValue(cout, pld->dwParam, TEXT("Param Id: "), rgMapKeyParam);
  466. cout << TEXT("Buffer Space: ") << PHex(pld->dwDataLen) << endl;
  467. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  468. ShowBuf(TEXT("Returned Data: "), pld->bfData, cout);
  469. break;
  470. }
  471. case GenRandom:
  472. {
  473. struct TmpLog {
  474. LogHeader lh;
  475. HCRYPTPROV hProv;
  476. DWORD dwLen;
  477. LogBuffer bfBuffer;
  478. } *pld = (struct TmpLog *)pLogObj;
  479. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  480. cout << TEXT("Length: ") << PHex(pld->dwLen) << endl;
  481. ShowBuf(TEXT("Returned Data: "), pld->bfBuffer, cout);
  482. break;
  483. }
  484. case GetUserKey:
  485. {
  486. struct TmpLog {
  487. LogHeader lh;
  488. HCRYPTPROV hProv;
  489. DWORD dwKeySpec;
  490. HCRYPTKEY hUserKey;
  491. } *pld = (struct TmpLog *)pLogObj;
  492. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  493. MapValue(cout, pld->dwKeySpec, TEXT("KeySpec: "), rgMapKeyId);
  494. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hUserKey) << endl;
  495. break;
  496. }
  497. case ImportKey:
  498. {
  499. struct TmpLog {
  500. LogHeader lh;
  501. HCRYPTPROV hProv;
  502. LogBuffer bfData;
  503. HCRYPTKEY hPubKey;
  504. DWORD dwFlags;
  505. HCRYPTKEY hKey;
  506. } *pld = (struct TmpLog *)pLogObj;
  507. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  508. ShowBuf(TEXT("Supplied Data: "), pld->bfData, cout);
  509. cout << TEXT("HCRYPTpubKEY: ") << PHex(pld->hPubKey) << endl;
  510. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  511. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  512. break;
  513. }
  514. case SetKeyParam:
  515. {
  516. struct TmpLog {
  517. LogHeader lh;
  518. HCRYPTPROV hProv;
  519. HCRYPTKEY hKey;
  520. DWORD dwParam;
  521. LogBuffer bfData;
  522. DWORD dwFlags;
  523. } *pld = (struct TmpLog *)pLogObj;
  524. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  525. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  526. MapValue(cout, pld->dwParam, TEXT("Param Id: "), rgMapKeyParam);
  527. ShowBuf(TEXT("Supplied Data: "), pld->bfData, cout);
  528. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  529. break;
  530. }
  531. case Encrypt:
  532. {
  533. struct TmpLog {
  534. LogHeader lh;
  535. HCRYPTPROV hProv;
  536. HCRYPTKEY hKey;
  537. HCRYPTHASH hHash;
  538. BOOL Final;
  539. DWORD dwFlags;
  540. LogBuffer bfInData;
  541. DWORD dwBufLen;
  542. LogBuffer bfOutData;
  543. } *pld = (struct TmpLog *)pLogObj;
  544. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  545. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  546. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  547. cout << TEXT("Final: ") << PHex(pld->Final) << endl;
  548. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  549. ShowBuf(TEXT("Supplied Data: "), pld->bfInData, cout);
  550. cout << TEXT("Buffer Space: ") << PHex(pld->dwBufLen) << endl;
  551. ShowBuf(TEXT("Received Data: "), pld->bfOutData, cout);
  552. break;
  553. }
  554. case Decrypt:
  555. {
  556. struct TmpLog {
  557. LogHeader lh;
  558. HCRYPTPROV hProv;
  559. HCRYPTKEY hKey;
  560. HCRYPTHASH hHash;
  561. BOOL Final;
  562. DWORD dwFlags;
  563. LogBuffer bfInData;
  564. LogBuffer bfOutData;
  565. } *pld = (struct TmpLog *)pLogObj;
  566. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  567. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  568. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  569. cout << TEXT("Final: ") << PHex(pld->Final) << endl;
  570. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  571. ShowBuf(TEXT("Supplied Data: "), pld->bfInData, cout);
  572. ShowBuf(TEXT("Received Data: "), pld->bfOutData, cout);
  573. break;
  574. }
  575. case CreateHash:
  576. {
  577. struct TmpLog {
  578. LogHeader lh;
  579. HCRYPTPROV hProv;
  580. ALG_ID Algid;
  581. HCRYPTKEY hKey;
  582. DWORD dwFlags;
  583. HCRYPTHASH hHash;
  584. } *pld = (struct TmpLog *)pLogObj;
  585. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  586. MapValue(cout, pld->Algid, TEXT("Algorithm: "), rgMapAlgId);
  587. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  588. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  589. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  590. break;
  591. }
  592. case DestroyHash:
  593. {
  594. struct TmpLog {
  595. LogHeader lh;
  596. HCRYPTPROV hProv;
  597. HCRYPTHASH hHash;
  598. } *pld = (struct TmpLog *)pLogObj;
  599. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  600. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  601. break;
  602. }
  603. case GetHashParam:
  604. {
  605. struct TmpLog {
  606. LogHeader lh;
  607. HCRYPTPROV hProv;
  608. HCRYPTHASH hHash;
  609. DWORD dwParam;
  610. DWORD dwDataLen;
  611. DWORD dwFlags;
  612. LogBuffer bfData;
  613. } *pld = (struct TmpLog *)pLogObj;
  614. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  615. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  616. MapValue(cout, pld->dwParam, TEXT("Param Id: "), rgMapHashParam);
  617. cout << TEXT("Buffer Space: ") << PHex(pld->dwDataLen) << endl;
  618. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  619. ShowBuf(TEXT("Returned Data: "), pld->bfData, cout);
  620. break;
  621. }
  622. case HashData:
  623. {
  624. struct TmpLog {
  625. LogHeader lh;
  626. HCRYPTPROV hProv;
  627. HCRYPTHASH hHash;
  628. LogBuffer bfData;
  629. DWORD dwFlags;
  630. } *pld = (struct TmpLog *)pLogObj;
  631. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  632. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  633. ShowBuf(TEXT("Supplied Data: "), pld->bfData, cout);
  634. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  635. break;
  636. }
  637. case HashSessionKey:
  638. {
  639. struct TmpLog {
  640. LogHeader lh;
  641. HCRYPTPROV hProv;
  642. HCRYPTHASH hHash;
  643. HCRYPTKEY hKey;
  644. DWORD dwFlags;
  645. } *pld = (struct TmpLog *)pLogObj;
  646. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  647. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  648. cout << TEXT("HCRYPTKEY: ") << PHex(pld->hKey) << endl;
  649. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  650. break;
  651. }
  652. case SetHashParam:
  653. {
  654. struct TmpLog {
  655. LogHeader lh;
  656. HCRYPTPROV hProv;
  657. HCRYPTHASH hHash;
  658. DWORD dwParam;
  659. LogBuffer bfData;
  660. DWORD dwFlags;
  661. } *pld = (struct TmpLog *)pLogObj;
  662. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  663. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  664. MapValue(cout, pld->dwParam, TEXT("Param Id: "), rgMapHashParam);
  665. ShowBuf(TEXT("Supplied Data: "), pld->bfData, cout);
  666. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  667. break;
  668. }
  669. case SignHash:
  670. {
  671. struct TmpLog {
  672. LogHeader lh;
  673. HCRYPTPROV hProv;
  674. HCRYPTHASH hHash;
  675. DWORD dwKeySpec;
  676. LogBuffer bfDescription;
  677. DWORD dwFlags;
  678. DWORD dwSigLen;
  679. LogBuffer bfSignature;
  680. } *pld = (struct TmpLog *)pLogObj;
  681. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  682. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  683. MapValue(cout, pld->dwKeySpec, TEXT("KeySpec: "), rgMapKeyId);
  684. ShowBuf(TEXT("Description: "), pld->bfDescription, cout);
  685. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  686. cout << TEXT("Buffer Space: ") << PHex(pld->dwSigLen) << endl;
  687. ShowBuf(TEXT("Signature: "), pld->bfSignature, cout);
  688. break;
  689. }
  690. case VerifySignature:
  691. {
  692. struct TmpLog {
  693. LogHeader lh;
  694. HCRYPTPROV hProv;
  695. HCRYPTHASH hHash;
  696. LogBuffer bfSignature;
  697. DWORD dwSigLen;
  698. HCRYPTKEY hPubKey;
  699. LogBuffer bfDescription;
  700. DWORD dwFlags;
  701. } *pld = (struct TmpLog *)pLogObj;
  702. cout << TEXT("HCRYPTPROV: ") << PHex(pld->hProv) << endl;
  703. cout << TEXT("HCRYPTHASH: ") << PHex(pld->hHash) << endl;
  704. ShowBuf(TEXT("Signature: "), pld->bfSignature, cout);
  705. cout << TEXT("HCRYPTpubKEY: ") << PHex(pld->hPubKey) << endl;
  706. ShowBuf(TEXT("Description: "), pld->bfDescription, cout);
  707. MaskValue(cout, pld->dwFlags, TEXT("Flags: "), NULL);
  708. break;
  709. }
  710. default:
  711. cerr << TEXT("Internal error") << endl;
  712. goto ErrorExit;
  713. break;
  714. }
  715. }
  716. ErrorExit:
  717. if (NULL == hLogFile)
  718. CloseHandle(hLogFile);
  719. }
  720. //
  721. ///////////////////////////////////////////////////////////////////////////////
  722. //
  723. // Suport routines
  724. //
  725. static void
  726. dump(
  727. const BYTE *pbData,
  728. DWORD cbLen,
  729. ostream &outStr)
  730. {
  731. unsigned long int
  732. b, i, lc;
  733. char
  734. buffer[8];
  735. lc = 0;
  736. while (0 < cbLen)
  737. {
  738. b = min(sizeof(buffer), cbLen);
  739. memcpy(buffer, pbData, b);
  740. pbData += b;
  741. cbLen -= b;
  742. if (0 < b)
  743. {
  744. outStr << TEXT(" ") << setw(8) << setfill(TEXT('0')) << hex << lc;
  745. for (i = 0; i < b; i += 1)
  746. outStr
  747. << " "
  748. << setw(2) << setfill('0') << hex
  749. << ((unsigned int)buffer[i] & 0xff);
  750. for (; i < sizeof(buffer) + 1; i += 1)
  751. outStr << " ";
  752. for (i = 0; i < b; i += 1)
  753. outStr
  754. << setw(0) << setfill(' ') << dec
  755. << ((0 != iscntrl((int)(0x7f & buffer[i])))
  756. ? TEXT('.')
  757. : buffer[i]);
  758. outStr << endl;
  759. lc += b;
  760. }
  761. }
  762. }
  763. static void
  764. MapValue(
  765. ostream &outStr,
  766. DWORD dwValue,
  767. LPCTSTR szLeader,
  768. const ValueMap *rgMap)
  769. {
  770. DWORD dwIndex;
  771. if (NULL != rgMap)
  772. {
  773. for (dwIndex = 0; NULL != rgMap[dwIndex].szValue; dwIndex += 1)
  774. if (rgMap[dwIndex].dwValue == dwValue)
  775. break;
  776. if (NULL != rgMap[dwIndex].szValue)
  777. outStr << szLeader << rgMap[dwIndex].szValue << endl;
  778. else
  779. outStr << szLeader << PHex(dwValue) << endl;
  780. }
  781. else
  782. outStr << szLeader << PHex(dwValue) << endl;
  783. }
  784. static void
  785. MaskValue(
  786. ostream &outStr,
  787. DWORD dwValue,
  788. LPCTSTR szLeader,
  789. const ValueMap *rgMap)
  790. {
  791. DWORD dwIndex;
  792. BOOL fSpace = FALSE;
  793. if (NULL != rgMap)
  794. {
  795. outStr << szLeader;
  796. for (dwIndex = 0; NULL != rgMap[dwIndex].szValue; dwIndex += 1)
  797. {
  798. if (rgMap[dwIndex].dwValue == (rgMap[dwIndex].dwValue & dwValue))
  799. {
  800. if (fSpace)
  801. outStr << TEXT(' ');
  802. else
  803. fSpace = TRUE;
  804. outStr << rgMap[dwIndex].szValue;
  805. dwValue &= ~rgMap[dwIndex].dwValue;
  806. }
  807. }
  808. if (0 != dwValue)
  809. {
  810. if (fSpace)
  811. {
  812. outStr << TEXT(' ');
  813. fSpace = TRUE;
  814. }
  815. outStr << PHex(dwValue);
  816. }
  817. else if (!fSpace)
  818. outStr << PHex(dwValue);
  819. outStr << endl;
  820. }
  821. else
  822. outStr << szLeader << PHex(dwValue) << endl;
  823. }
  824. static void
  825. ShowBuf(
  826. LPCTSTR szName,
  827. LogBuffer &lb,
  828. ostream &outStr)
  829. {
  830. if ((DWORD)(-1) == lb.cbOffset)
  831. {
  832. outStr << szName << TEXT("<NULL>\n")
  833. << TEXT("Length: ")
  834. << PDec(lb.cbLength)
  835. << TEXT(" (") << PHex(lb.cbLength) << TEXT(")")
  836. << endl;
  837. }
  838. else if (0 == lb.cbLength)
  839. {
  840. outStr << szName << TEXT("\n")
  841. << TEXT("Length: ")
  842. << PDec(lb.cbLength)
  843. << TEXT(" (") << PHex(lb.cbLength) << TEXT(")")
  844. << endl;
  845. }
  846. else if (l_cbLogData < lb.cbOffset + lb.cbLength)
  847. {
  848. outStr << szName << TEXT("<Buffer Overrun>\n")
  849. << TEXT("Length: ")
  850. << PDec(lb.cbLength)
  851. << TEXT(" (") << PHex(lb.cbLength) << TEXT(")")
  852. << endl;
  853. }
  854. else
  855. {
  856. outStr << szName << endl;
  857. dump(&l_pbLogData[lb.cbOffset], lb.cbLength, outStr);
  858. outStr << TEXT("Length: ")
  859. << PDec(lb.cbLength)
  860. << TEXT(" (") << PHex(lb.cbLength) << TEXT(")")
  861. << endl;
  862. }
  863. }