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.

229 lines
6.2 KiB

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