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.

236 lines
6.2 KiB

  1. // implements the exported CKeyCrackedData
  2. #include "stdafx.h"
  3. #include "CrackCrt.h"
  4. extern "C"
  5. {
  6. #include <wincrypt.h>
  7. #include <sslsp.h>
  8. }
  9. //-------------------------------------------------
  10. CCrackedCert:: CCrackedCert()
  11. : m_pData(NULL)
  12. {}
  13. //-------------------------------------------------
  14. CCrackedCert::~CCrackedCert()
  15. {
  16. PX509Certificate p509 = (PX509Certificate)m_pData;
  17. // if the cracked data is there, free it
  18. if ( p509 )
  19. SslFreeCertificate( (PX509Certificate)m_pData );
  20. }
  21. //-------------------------------------------------
  22. // adds a key to the service. They CKey object is added to the
  23. // array object below. If this Service is connected to a machine,
  24. // then the key is also added to the tree view below the service.
  25. //-------------------------------------------------
  26. BOOL CCrackedCert::CrackCert( PUCHAR pCert, DWORD cbCert )
  27. {
  28. PX509Certificate p509 = NULL;
  29. BOOL f;
  30. // if there already is a cracked cert, get rid of it
  31. if ( m_pData )
  32. {
  33. SslFreeCertificate( (PX509Certificate)m_pData );
  34. m_pData = NULL;
  35. }
  36. // crack the certificate
  37. f = SslCrackCertificate( pCert, cbCert, CF_CERT_FROM_FILE, &p509 );
  38. m_pData = (PVOID)p509;
  39. return f;
  40. }
  41. //-------------------------------------------------
  42. // The rest of the methods access the data in the cracked certificate
  43. //-------------------------------------------------
  44. DWORD CCrackedCert::GetVersion()
  45. {
  46. ASSERT(m_pData);
  47. PX509Certificate pCert = (PX509Certificate)m_pData;
  48. return pCert->Version;
  49. }
  50. //-------------------------------------------------
  51. // returns a pointer to a DWORD[4]
  52. DWORD* CCrackedCert::PGetSerialNumber()
  53. {
  54. ASSERT(m_pData);
  55. PX509Certificate pCert = (PX509Certificate)m_pData;
  56. return (DWORD*)&pCert->SerialNumber;
  57. }
  58. //-------------------------------------------------
  59. int CCrackedCert::GetSignatureAlgorithm()
  60. {
  61. ASSERT(m_pData);
  62. PX509Certificate pCert = (PX509Certificate)m_pData;
  63. return pCert->SignatureAlgorithm;
  64. }
  65. //-------------------------------------------------
  66. FILETIME CCrackedCert::GetValidFrom()
  67. {
  68. PX509Certificate pCert = (PX509Certificate)m_pData;
  69. ASSERT(m_pData);
  70. return pCert->ValidFrom;
  71. }
  72. //-------------------------------------------------
  73. FILETIME CCrackedCert::GetValidUntil()
  74. {
  75. PX509Certificate pCert = (PX509Certificate)m_pData;
  76. ASSERT(m_pData);
  77. return pCert->ValidUntil;
  78. }
  79. //-------------------------------------------------
  80. PVOID CCrackedCert::PSafePublicKey()
  81. {
  82. PX509Certificate pCert = (PX509Certificate)m_pData;
  83. ASSERT(m_pData);
  84. return pCert->pPublicKey;
  85. }
  86. //-------------------------------------------------
  87. DWORD CCrackedCert::GetBitLength()
  88. {
  89. PX509Certificate pCert = (PX509Certificate)m_pData;
  90. LPPUBLIC_KEY pPubKey = (LPPUBLIC_KEY)(pCert->pPublicKey);
  91. ASSERT(m_pData);
  92. return pPubKey->bitlen;
  93. }
  94. //-------------------------------------------------
  95. void CCrackedCert::GetIssuer( CString &sz )
  96. {
  97. PX509Certificate pCert = (PX509Certificate)m_pData;
  98. ASSERT(m_pData);
  99. sz = pCert->pszIssuer;
  100. }
  101. //-------------------------------------------------
  102. void CCrackedCert::GetSubject( CString &sz )
  103. {
  104. PX509Certificate pCert = (PX509Certificate)m_pData;
  105. ASSERT(m_pData);
  106. sz = pCert->pszSubject;
  107. }
  108. //-------------------------------------------------
  109. // gets a part of the subject's distinguishing information
  110. void CCrackedCert::GetSubjectDN( CString &szDN, LPCTSTR szKey )
  111. {
  112. // clear the szDN
  113. szDN.Empty();
  114. // start with the dn (aka subject) string
  115. CString szSubject;
  116. GetSubject( szSubject );
  117. // find the position of the key in the subject
  118. int cPos = szSubject.Find( szKey );
  119. // if we got it, get it
  120. if ( cPos >= 0 )
  121. {
  122. szDN = szKey;
  123. // get the string
  124. szDN = szSubject.Mid( cPos + szDN.GetLength() );
  125. // get the comma
  126. cPos = szDN.Find( _T(',') );
  127. // truncate at the comma
  128. if ( cPos >=0 )
  129. szDN = szDN.Left( cPos );
  130. }
  131. }
  132. //-------------------------------------------------
  133. // gets a part of the issuer's distinguishing information
  134. void CCrackedCert::GetIssuerDN( CString &szDN, LPCTSTR szKey )
  135. {
  136. // clear the szDN
  137. szDN.Empty();
  138. // start with the dn (aka subject) string
  139. CString szIssuer;
  140. GetIssuer( szIssuer );
  141. // find the position of the key in the subject
  142. int cPos = szIssuer.Find( szKey );
  143. // if we got it, get it
  144. if ( cPos >= 0 )
  145. {
  146. szDN = szKey;
  147. // get the string
  148. szDN = szIssuer.Mid( cPos + szDN.GetLength() );
  149. // get the comma
  150. cPos = szDN.Find( _T(',') );
  151. // truncate at the comma
  152. if ( cPos >=0 )
  153. szDN = szDN.Left( cPos );
  154. }
  155. }
  156. //-------------------------------------------------
  157. void CCrackedCert::GetSubjectCountry( CString &sz )
  158. {
  159. GetSubjectDN( sz, SZ_KEY_COUNTRY );
  160. }
  161. //-------------------------------------------------
  162. void CCrackedCert::GetSubjectState( CString &sz )
  163. {
  164. GetSubjectDN( sz, SZ_KEY_STATE );
  165. }
  166. //-------------------------------------------------
  167. void CCrackedCert::GetSubjectLocality( CString &sz )
  168. {
  169. GetSubjectDN( sz, SZ_KEY_LOCALITY );
  170. }
  171. //-------------------------------------------------
  172. void CCrackedCert::GetSubjectCommonName( CString &sz )
  173. {
  174. GetSubjectDN( sz, SZ_KEY_COMNAME );
  175. }
  176. //-------------------------------------------------
  177. void CCrackedCert::GetSubjectOrganization( CString &sz )
  178. {
  179. GetSubjectDN( sz, SZ_KEY_ORGANIZATION );
  180. }
  181. //-------------------------------------------------
  182. void CCrackedCert::GetSubjectUnit( CString &sz )
  183. {
  184. GetSubjectDN( sz, SZ_KEY_ORGUNIT );
  185. }
  186. //-------------------------------------------------
  187. void CCrackedCert::GetIssuerCountry( CString &sz )
  188. {
  189. GetIssuerDN( sz, SZ_KEY_COUNTRY );
  190. }
  191. //-------------------------------------------------
  192. void CCrackedCert::GetIssuerOrganization( CString &sz )
  193. {
  194. GetIssuerDN( sz, SZ_KEY_ORGANIZATION );
  195. }
  196. //-------------------------------------------------
  197. void CCrackedCert::GetIssuerUnit( CString &sz )
  198. {
  199. GetIssuerDN( sz, SZ_KEY_ORGUNIT );
  200. }