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.

276 lines
5.9 KiB

  1. //
  2. // Certificat.h
  3. //
  4. #ifndef _CERTIFICAT_H
  5. #define _CERTIFICAT_H
  6. #include <xenroll.h>
  7. #define CERTWIZ_INSTANCE_NAME_PROP_ID (CERT_FIRST_USER_PROP_ID + 0x1000)
  8. #define CERTWIZ_REQUEST_FLAG_PROP_ID (CERT_FIRST_USER_PROP_ID + 0x1001)
  9. #define CERTWIZ_REQUEST_TEXT_PROP_ID (CERT_FIRST_USER_PROP_ID + 0x1002)
  10. #define CERT_HASH_LENGTH 40
  11. typedef struct _CERT_DESCRIPTION
  12. {
  13. CString m_CommonName;
  14. CString m_FriendlyName;
  15. CString m_Country;
  16. CString m_State;
  17. CString m_Locality;
  18. CString m_Organization;
  19. CString m_OrganizationUnit;
  20. CString m_CAName;
  21. CString m_ExpirationDate;
  22. CString m_Usage;
  23. BYTE m_hash[CERT_HASH_LENGTH];
  24. DWORD m_hash_length;
  25. } CERT_DESCRIPTION;
  26. class CCertDescList : public CList<CERT_DESCRIPTION *, CERT_DESCRIPTION *&>
  27. {
  28. public:
  29. CCertDescList() {}
  30. ~CCertDescList();
  31. };
  32. class CCryptBlob
  33. {
  34. public:
  35. CCryptBlob()
  36. {
  37. m_blob.cbData = 0;
  38. m_blob.pbData = NULL;
  39. }
  40. virtual ~CCryptBlob()
  41. {
  42. }
  43. DWORD GetSize() {return m_blob.cbData;}
  44. BYTE * GetData() {return m_blob.pbData;}
  45. void Set(DWORD cb, BYTE * pb)
  46. {
  47. Destroy();
  48. m_blob.cbData = cb;
  49. m_blob.pbData = pb;
  50. }
  51. BOOL Resize(DWORD cb);
  52. operator CRYPT_DATA_BLOB *()
  53. {
  54. return &m_blob;
  55. }
  56. protected:
  57. void Destroy()
  58. {
  59. if (m_blob.pbData != NULL)
  60. Free(m_blob.pbData);
  61. }
  62. virtual BYTE * Realloc(BYTE * pb, DWORD cb) = 0;
  63. virtual void Free(BYTE * pb) = 0;
  64. CRYPT_DATA_BLOB m_blob;
  65. };
  66. class CCryptBlobIMalloc : public CCryptBlob
  67. {
  68. public:
  69. virtual ~CCryptBlobIMalloc()
  70. {
  71. CCryptBlob::Destroy();
  72. }
  73. protected:
  74. virtual BYTE * Realloc(BYTE * pb, DWORD cb)
  75. {
  76. return (BYTE *)CoTaskMemRealloc(pb, cb);
  77. }
  78. virtual void Free(BYTE * pb)
  79. {
  80. CoTaskMemFree(pb);
  81. }
  82. };
  83. class CCryptBlobLocal : public CCryptBlob
  84. {
  85. public:
  86. virtual ~CCryptBlobLocal()
  87. {
  88. CCryptBlob::Destroy();
  89. }
  90. protected:
  91. virtual BYTE * Realloc(BYTE * pb, DWORD cb)
  92. {
  93. return (BYTE *)realloc(pb, cb);
  94. }
  95. virtual void Free(BYTE * pb)
  96. {
  97. free(pb);
  98. }
  99. };
  100. extern const TCHAR szState[];
  101. extern const TCHAR szStateMRU[];
  102. extern const TCHAR szLocality[];
  103. extern const TCHAR szLocalityMRU[];
  104. extern const TCHAR szOrganization[];
  105. extern const TCHAR szOrganizationMRU[];
  106. extern const TCHAR szOrganizationUnit[];
  107. extern const TCHAR szOrganizationUnitMRU[];
  108. class CCertificate : public CObject
  109. {
  110. DECLARE_DYNCREATE(CCertificate)
  111. public:
  112. CCertificate();
  113. ~CCertificate();
  114. enum
  115. {
  116. CA_OFFLINE = 0,
  117. CA_ONLINE = 1
  118. };
  119. enum
  120. {
  121. REQUEST_UNDEFINED,
  122. REQUEST_NEW_CERT, // if we generating fresh new certificate
  123. REQUEST_RENEW_CERT, // if we generating cert for renewal
  124. REQUEST_REPLACE_CERT, // replace currect cert by someone from MY store
  125. REQUEST_INSTALL_CERT, // get existing certificate for empty web server
  126. REQUEST_PROCESS_PENDING, // accept and install response from CA
  127. REQUEST_IMPORT_KEYRING,
  128. STATUS_CODE_LAST
  129. };
  130. enum
  131. {
  132. USE_ERROR_STRING_PARAM = -2,
  133. USE_ERROR_STRING_ID = -1,
  134. USE_ERROR_STRING_DEFAULT = USE_DEFAULT_CAPTION
  135. };
  136. BOOL Init();
  137. BOOL SaveSettings();
  138. BOOL SetSecuritySettings();
  139. BOOL WriteRequest();
  140. BOOL PrepareRequest();
  141. BOOL PrepareRequestString(CString& request_text, CCryptBlob& request_blob);
  142. BOOL WriteRequestString(CString& request);
  143. BOOL SubmitRequest();
  144. BOOL SubmitRenewalRequest();
  145. BOOL WriteRenewalRequest();
  146. void DumpHeader(CString& str);
  147. void DumpOnlineHeader(CString& str);
  148. BOOL GetSelectedCertDescription(CERT_DESCRIPTION& cd);
  149. BOOL GetKeyCertDescription(CERT_DESCRIPTION& cd)
  150. {
  151. return GetCertDescription(GetKeyRingCert(), cd);
  152. }
  153. BOOL GetInstalledCertDescription(CERT_DESCRIPTION& cd)
  154. {
  155. return GetCertDescription(GetInstalledCert(), cd);
  156. }
  157. BOOL GetResponseCertDescription(CERT_DESCRIPTION& cd);
  158. BOOL HasPendingRequest()
  159. {
  160. return (NULL != GetPendingRequest());
  161. }
  162. BOOL HasInstalledCert();
  163. BOOL InstallResponseCert();
  164. HRESULT UninstallCert();
  165. BOOL InstallSelectedCert();
  166. BOOL InstallKeyRingCert();
  167. BOOL ReplaceInstalled();
  168. BOOL CancelRequest();
  169. PCCERT_CONTEXT GetPendingRequest();
  170. PCCERT_CONTEXT GetResponseCert();
  171. PCCERT_CONTEXT GetInstalledCert();
  172. PCCERT_CONTEXT GetKeyRingCert();
  173. void DeleteKeyRingCert()
  174. {
  175. if (m_pKeyRingCert != NULL)
  176. {
  177. CertFreeCertificateContext(m_pKeyRingCert);
  178. m_pKeyRingCert = NULL;
  179. }
  180. }
  181. IEnroll * GetEnrollObject();
  182. int GetStatusCode()
  183. {
  184. return m_status_code;
  185. }
  186. void SetStatusCode(int code)
  187. {
  188. ASSERT(code >= 0 && code < STATUS_CODE_LAST);
  189. m_status_code = code;
  190. }
  191. BOOL FindInstanceNameForResponse(CString& str);
  192. BOOL IsResponseInstalled(CString& str);
  193. BOOL GetCertDescList(CCertDescList& list);
  194. BOOL LoadRenewalData();
  195. int MyStoreCertCount();
  196. void SetBodyTextID(int nID)
  197. {
  198. m_idErrorText = nID;
  199. m_strErrorText.Empty();
  200. }
  201. void SetBodyTextString(const CString& str)
  202. {
  203. m_strErrorText = str;
  204. m_idErrorText = USE_ERROR_STRING_PARAM;
  205. }
  206. void GetCertificateTemplate(CString& str)
  207. {
  208. str = _T("CertificateTemplate:");
  209. str += m_CertificateTemplate;
  210. }
  211. protected:
  212. void CreateDN(CString& str);
  213. BOOL WriteHeader();
  214. BOOL WriteRequestBody();
  215. BOOL GetCertDescription(PCCERT_CONTEXT pCert,
  216. CERT_DESCRIPTION& desc);
  217. public:
  218. int m_CAType;
  219. CString m_ConfigCA;
  220. CString m_CertificateTemplate;
  221. CString m_FriendlyName;
  222. int m_KeyLength;
  223. CString m_CommonName;
  224. CString m_OrganizationUnit;
  225. CString m_Organization;
  226. CString m_Locality;
  227. CString m_State;
  228. CString m_Country;
  229. CStringList m_OnlineCAList;
  230. CString m_MachineName;
  231. CString m_WebSiteInstanceName;
  232. CString m_ReqFileName;
  233. CString m_RespFileName;
  234. CString m_KeyFileName;
  235. CString m_KeyPassword;
  236. BOOL m_DefaultCSP;
  237. DWORD m_DefaultProviderType;
  238. DWORD m_CustomProviderType;
  239. CString m_CspName;
  240. CRYPT_HASH_BLOB * m_pSelectedCertHash;
  241. UINT m_idErrorText;
  242. CString m_strErrorText;
  243. CString m_strRenewalRequest;
  244. HRESULT m_hResult;
  245. BOOL m_CreateDirectory;
  246. BOOL m_SGCcertificat;
  247. protected:
  248. PCCERT_CONTEXT m_pPendingRequest;
  249. PCCERT_CONTEXT m_RespCertContext;
  250. PCCERT_CONTEXT m_pInstalledCert;
  251. PCCERT_CONTEXT m_pKeyRingCert;
  252. IEnroll * m_pEnroll;
  253. int m_status_code; // what we are doing in this session
  254. };
  255. #endif // _CERTIFICAT_H