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.

209 lines
5.3 KiB

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