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.

302 lines
6.6 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. REQUEST_IMPORT_CERT,
  129. REQUEST_EXPORT_CERT,
  130. REQUEST_COPY_MOVE_FROM_REMOTE,
  131. REQUEST_COPY_MOVE_TO_REMOTE,
  132. STATUS_CODE_LAST
  133. };
  134. enum
  135. {
  136. USE_ERROR_STRING_PARAM = -2,
  137. USE_ERROR_STRING_ID = -1,
  138. USE_ERROR_STRING_DEFAULT = USE_DEFAULT_CAPTION
  139. };
  140. BOOL Init();
  141. BOOL IsCertObjInstalled();
  142. BOOL SaveSettings();
  143. BOOL SetSecuritySettings();
  144. BOOL WriteRequest();
  145. BOOL PrepareRequest();
  146. BOOL PrepareRequestString(CString& request_text, CCryptBlob& request_blob);
  147. BOOL WriteRequestString(CString& request);
  148. BOOL SubmitRequest();
  149. BOOL SubmitRenewalRequest();
  150. BOOL WriteRenewalRequest();
  151. void DumpHeader(CString& str);
  152. void DumpOnlineHeader(CString& str);
  153. BOOL GetSelectedCertDescription(CERT_DESCRIPTION& cd);
  154. BOOL GetPFXFileCertDescription(CERT_DESCRIPTION& cd)
  155. {
  156. return GetCertDescription(GetPFXFileCert(), cd);
  157. }
  158. BOOL GetKeyCertDescription(CERT_DESCRIPTION& cd)
  159. {
  160. return GetCertDescription(GetKeyRingCert(), cd);
  161. }
  162. BOOL GetInstalledCertDescription(CERT_DESCRIPTION& cd)
  163. {
  164. return GetCertDescription(GetInstalledCert(), cd);
  165. }
  166. BOOL GetResponseCertDescription(CERT_DESCRIPTION& cd);
  167. BOOL HasPendingRequest()
  168. {
  169. return (NULL != GetPendingRequest());
  170. }
  171. BOOL HasInstalledCert();
  172. BOOL InstallResponseCert();
  173. HRESULT UninstallCert();
  174. BOOL InstallSelectedCert();
  175. BOOL InstallKeyRingCert();
  176. BOOL InstallImportPFXCert();
  177. BOOL InstallExportPFXCert();
  178. BOOL InstallCopyMoveFromRemote();
  179. BOOL InstallCopyMoveToRemote();
  180. BOOL ReplaceInstalled();
  181. BOOL CancelRequest();
  182. PCCERT_CONTEXT GetPendingRequest();
  183. PCCERT_CONTEXT GetResponseCert();
  184. PCCERT_CONTEXT GetInstalledCert();
  185. PCCERT_CONTEXT GetKeyRingCert();
  186. PCCERT_CONTEXT GetPFXFileCert();
  187. void DeleteKeyRingCert()
  188. {
  189. if (m_pKeyRingCert != NULL)
  190. {
  191. CertFreeCertificateContext(m_pKeyRingCert);
  192. m_pKeyRingCert = NULL;
  193. }
  194. }
  195. IEnroll * GetEnrollObject();
  196. int GetStatusCode()
  197. {
  198. return m_status_code;
  199. }
  200. void SetStatusCode(int code)
  201. {
  202. ASSERT(code >= 0 && code < STATUS_CODE_LAST);
  203. m_status_code = code;
  204. }
  205. BOOL FindInstanceNameForResponse(CString& str);
  206. BOOL IsResponseInstalled(CString& str);
  207. BOOL GetCertDescList(CCertDescList& list);
  208. BOOL LoadRenewalData();
  209. int MyStoreCertCount();
  210. void SetBodyTextID(int nID)
  211. {
  212. m_idErrorText = nID;
  213. m_strErrorText.Empty();
  214. }
  215. void SetBodyTextString(const CString& str)
  216. {
  217. m_strErrorText = str;
  218. m_idErrorText = USE_ERROR_STRING_PARAM;
  219. }
  220. void GetCertificateTemplate(CString& str)
  221. {
  222. str = _T("CertificateTemplate:");
  223. str += m_CertificateTemplate;
  224. }
  225. protected:
  226. void CreateDN(CString& str);
  227. BOOL WriteHeader();
  228. BOOL WriteRequestBody();
  229. BOOL GetCertDescription(PCCERT_CONTEXT pCert,
  230. CERT_DESCRIPTION& desc);
  231. public:
  232. int m_CAType;
  233. CString m_ConfigCA;
  234. CString m_CertificateTemplate;
  235. CString m_FriendlyName;
  236. int m_KeyLength;
  237. CString m_CommonName;
  238. CString m_OrganizationUnit;
  239. CString m_Organization;
  240. CString m_Locality;
  241. CString m_State;
  242. CString m_Country;
  243. CStringList m_OnlineCAList;
  244. CString m_MachineName;
  245. CString m_WebSiteInstanceName;
  246. BOOL m_DeleteAfterCopy;
  247. CString m_MachineName_Remote;
  248. CString m_UserName_Remote;
  249. CString m_UserPassword_Remote;
  250. CString m_WebSiteInstanceName_Remote;
  251. CString m_ReqFileName;
  252. CString m_RespFileName;
  253. CString m_KeyFileName;
  254. CString m_KeyPassword;
  255. BOOL m_DefaultCSP;
  256. DWORD m_DefaultProviderType;
  257. DWORD m_CustomProviderType;
  258. CString m_CspName;
  259. CRYPT_HASH_BLOB * m_pSelectedCertHash;
  260. UINT m_idErrorText;
  261. CString m_strErrorText;
  262. CString m_strRenewalRequest;
  263. HRESULT m_hResult;
  264. BOOL m_CreateDirectory;
  265. BOOL m_SGCcertificat;
  266. BOOL m_ExportPFXPrivateKey;
  267. BOOL m_CertObjInstalled;
  268. BOOL m_MarkAsExportable;
  269. protected:
  270. PCCERT_CONTEXT m_pPendingRequest;
  271. PCCERT_CONTEXT m_RespCertContext;
  272. PCCERT_CONTEXT m_pInstalledCert;
  273. PCCERT_CONTEXT m_pKeyRingCert;
  274. IEnroll * m_pEnroll;
  275. int m_status_code; // what we are doing in this session
  276. };
  277. #endif // _CERTIFICAT_H