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.

193 lines
4.9 KiB

  1. /* CRYPTDBG.CPP
  2. **
  3. **
  4. **
  5. ** Owner: t-erikne
  6. ** Created: 8/26/96
  7. **
  8. */
  9. #include "pch.hxx"
  10. #ifdef DEBUG
  11. #include "capitype.h"
  12. #include "cryptdbg.h"
  13. #include <shlwapi.h>
  14. #include <dllmain.h> // DllAddRef, global critsec
  15. #include <demand.h>
  16. ASSERTDATA
  17. static s_fInit = FALSE;
  18. static BOOL DecodeName(BYTE *pbEncoded, DWORD cbEncoded);
  19. static void PrintLastError(LPCSTR pszMsg);
  20. static void PrintCrlEntries(DWORD cEntry, PCRL_ENTRY pEntry);
  21. ///////////////////////////////////////////////////////////////////////////
  22. //
  23. // DOUT stuff
  24. //
  25. void PrefDOUT(DWORD dwLevel, LPSTR szPref, LPSTR szFmt, va_list arglist);
  26. void CSSDOUT(LPSTR szFmt, ...)
  27. {
  28. va_list arglist;
  29. va_start(arglist, szFmt);
  30. PrefDOUT(DOUTL_CSS, "CSS: ", szFmt, arglist);
  31. va_end(arglist);
  32. }
  33. void SMDOUT(LPSTR szFmt, ...)
  34. {
  35. va_list arglist;
  36. va_start(arglist, szFmt);
  37. PrefDOUT(DOUTL_SMIME, "SMIME: ", szFmt, arglist);
  38. va_end(arglist);
  39. }
  40. void CRDOUT(LPSTR szFmt, ...)
  41. {
  42. va_list arglist;
  43. va_start(arglist, szFmt);
  44. PrefDOUT(DOUTL_SMIME, "CRYPT: ", szFmt, arglist);
  45. va_end(arglist);
  46. }
  47. void PrefDOUT(DWORD dwLevel, LPSTR szPref, LPSTR szFmt, va_list arglist)
  48. {
  49. char sz[MAX_PATH];
  50. StrCpyN(sz, szPref, ARRAYSIZE(sz));
  51. StrCatBuff(sz, szFmt, ARRAYSIZE(sz));
  52. vDOUTL(dwLevel, sz, arglist);
  53. }
  54. BOOL InitDebugHelpers(HINSTANCE hLib)
  55. {
  56. s_fInit = TRUE;
  57. return TRUE;
  58. }
  59. //+-------------------------------------------------------------------------
  60. // Error output routines
  61. //--------------------------------------------------------------------------
  62. static void PrintError(LPCSTR pszMsg)
  63. {
  64. DOUTL(CRYPT_LEVEL,"%s\n", pszMsg);
  65. }
  66. static void PrintLastError(LPCSTR pszMsg)
  67. {
  68. DWORD dwErr = GetLastError();
  69. DOUTL(CRYPT_LEVEL,"%s failed => 0x%x (%d) \n", pszMsg, dwErr, dwErr);
  70. }
  71. //+-------------------------------------------------------------------------
  72. // Helpful util functions
  73. //--------------------------------------------------------------------------
  74. static BOOL DecodeName(BYTE *pbEncoded, DWORD cbEncoded)
  75. {
  76. BOOL fResult = FALSE;
  77. PCERT_NAME_INFO pInfo = NULL;
  78. DWORD cbInfo;
  79. DWORD i,j;
  80. PCERT_RDN pRDN;
  81. PCERT_RDN_ATTR pAttr;
  82. Assert(s_fInit);
  83. cbInfo = 0;
  84. if (pInfo = (PCERT_NAME_INFO) PVDecodeObject(pbEncoded, cbEncoded, (LPCSTR)X509_NAME, &cbInfo)) {
  85. for (i = 0, pRDN = pInfo->rgRDN; i < pInfo->cRDN; i++, pRDN++) {
  86. for (j = 0, pAttr = pRDN->rgRDNAttr; j < pRDN->cRDNAttr; j++, pAttr++) {
  87. LPSTR pszObjId = pAttr->pszObjId;
  88. if (pszObjId == NULL)
  89. pszObjId = "<NULL OBJID>";
  90. if ((pAttr->dwValueType == CERT_RDN_ENCODED_BLOB) ||
  91. (pAttr->dwValueType == CERT_RDN_OCTET_STRING)) {
  92. DOUTL(CRYPT_LEVEL," [%d,%d] %s ValueType: %d\n",
  93. i, j, pszObjId, pAttr->dwValueType);
  94. } else
  95. DOUTL(CRYPT_LEVEL," [%d,%d] %s %s\n",
  96. i, j, pszObjId, pAttr->Value.pbData);
  97. }
  98. }
  99. fResult = TRUE;
  100. }
  101. SafeMemFree(pInfo);
  102. return fResult;
  103. }
  104. void DisplayCert(PCCERT_CONTEXT pCert)
  105. {
  106. Assert(s_fInit);
  107. if (!pCert)
  108. {
  109. DOUTL(CRYPT_LEVEL, "No certificate.");
  110. return;
  111. }
  112. DOUTL(CRYPT_LEVEL,"Subject::\n");
  113. DecodeName(pCert->pCertInfo->Subject.pbData,
  114. pCert->pCertInfo->Subject.cbData);
  115. DOUTL(CRYPT_LEVEL,"Issuer::\n");
  116. DecodeName(pCert->pCertInfo->Issuer.pbData,
  117. pCert->pCertInfo->Issuer.cbData);
  118. {
  119. DWORD cb;
  120. BYTE *pb;
  121. DOUTL(CRYPT_LEVEL,"SerialNumber::");
  122. for (cb = pCert->pCertInfo->SerialNumber.cbData,
  123. pb = pCert->pCertInfo->SerialNumber.pbData + (cb - 1);
  124. cb > 0; cb--, pb++) {
  125. DOUTL(CRYPT_LEVEL," %02X", *pb);
  126. }
  127. DOUTL(CRYPT_LEVEL,"\n");
  128. }
  129. }
  130. static void PrintCrlEntries(DWORD cEntry, PCRL_ENTRY pEntry)
  131. {
  132. DWORD i;
  133. Assert(s_fInit);
  134. for (i = 0; i < cEntry; i++, pEntry++) {
  135. DWORD cb;
  136. BYTE *pb;
  137. DOUTL(CRYPT_LEVEL," [%d] SerialNumber::", i);
  138. for (cb = pEntry->SerialNumber.cbData,
  139. pb = pEntry->SerialNumber.pbData + (cb - 1); cb > 0; cb--, pb++) {
  140. DOUTL(CRYPT_LEVEL," %02X", *pb);
  141. }
  142. DOUTL(CRYPT_LEVEL,"\n");
  143. }
  144. }
  145. void DisplayCrl(PCCRL_CONTEXT pCrl)
  146. {
  147. Assert(s_fInit);
  148. DOUTL(CRYPT_LEVEL,"Issuer::\n");
  149. DecodeName(pCrl->pCrlInfo->Issuer.pbData,
  150. pCrl->pCrlInfo->Issuer.cbData);
  151. if (pCrl->pCrlInfo->cCRLEntry == 0)
  152. DOUTL(CRYPT_LEVEL,"Entries:: NONE\n");
  153. else {
  154. DOUTL(CRYPT_LEVEL,"Entries::\n");
  155. PrintCrlEntries(pCrl->pCrlInfo->cCRLEntry,
  156. pCrl->pCrlInfo->rgCRLEntry);
  157. }
  158. }
  159. #endif // debug