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.

369 lines
9.0 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. CString m_AltSubject;
  24. BYTE * m_phash;
  25. DWORD m_hash_length;
  26. } CERT_DESCRIPTION;
  27. class CCertDescList : public CList<CERT_DESCRIPTION *, CERT_DESCRIPTION *&>
  28. {
  29. public:
  30. CCertDescList() {}
  31. ~CCertDescList();
  32. };
  33. class CCryptBlob
  34. {
  35. public:
  36. CCryptBlob()
  37. {
  38. m_blob.cbData = 0;
  39. m_blob.pbData = NULL;
  40. }
  41. virtual ~CCryptBlob()
  42. {
  43. }
  44. DWORD GetSize() {return m_blob.cbData;}
  45. BYTE * GetData() {return m_blob.pbData;}
  46. void Set(DWORD cb, BYTE * pb)
  47. {
  48. Destroy();
  49. m_blob.cbData = cb;
  50. m_blob.pbData = pb;
  51. }
  52. BOOL Resize(DWORD cb);
  53. operator CRYPT_DATA_BLOB *()
  54. {
  55. return &m_blob;
  56. }
  57. protected:
  58. void Destroy()
  59. {
  60. if (m_blob.pbData != NULL)
  61. Free(m_blob.pbData);
  62. }
  63. virtual BYTE * Realloc(BYTE * pb, DWORD cb) = 0;
  64. virtual void Free(BYTE * pb) = 0;
  65. CRYPT_DATA_BLOB m_blob;
  66. };
  67. class CCryptBlobIMalloc : public CCryptBlob
  68. {
  69. public:
  70. virtual ~CCryptBlobIMalloc()
  71. {
  72. CCryptBlob::Destroy();
  73. }
  74. protected:
  75. virtual BYTE * Realloc(BYTE * pb, DWORD cb)
  76. {
  77. return (BYTE *)CoTaskMemRealloc(pb, cb);
  78. }
  79. virtual void Free(BYTE * pb)
  80. {
  81. CoTaskMemFree(pb);
  82. }
  83. };
  84. class CCryptBlobLocal : public CCryptBlob
  85. {
  86. public:
  87. virtual ~CCryptBlobLocal()
  88. {
  89. CCryptBlob::Destroy();
  90. }
  91. protected:
  92. virtual BYTE * Realloc(BYTE * pb, DWORD cb)
  93. {
  94. return (BYTE *)realloc(pb, cb);
  95. }
  96. virtual void Free(BYTE * pb)
  97. {
  98. free(pb);
  99. }
  100. };
  101. extern const TCHAR szState[];
  102. extern const TCHAR szStateMRU[];
  103. extern const TCHAR szLocality[];
  104. extern const TCHAR szLocalityMRU[];
  105. extern const TCHAR szOrganization[];
  106. extern const TCHAR szOrganizationMRU[];
  107. extern const TCHAR szOrganizationUnit[];
  108. extern const TCHAR szOrganizationUnitMRU[];
  109. class CCertificate : public CObject
  110. {
  111. DECLARE_DYNCREATE(CCertificate)
  112. public:
  113. CCertificate();
  114. ~CCertificate();
  115. enum
  116. {
  117. CA_OFFLINE = 0,
  118. CA_ONLINE = 1
  119. };
  120. enum
  121. {
  122. REQUEST_UNDEFINED,
  123. REQUEST_NEW_CERT, // if we generating fresh new certificate
  124. REQUEST_RENEW_CERT, // if we generating cert for renewal
  125. REQUEST_REPLACE_CERT, // replace currect cert by someone from MY store
  126. REQUEST_INSTALL_CERT, // get existing certificate for empty web server
  127. REQUEST_PROCESS_PENDING, // accept and install response from CA
  128. REQUEST_IMPORT_KEYRING,
  129. REQUEST_IMPORT_CERT,
  130. REQUEST_EXPORT_CERT,
  131. REQUEST_COPY_MOVE_FROM_REMOTE,
  132. REQUEST_COPY_MOVE_TO_REMOTE,
  133. STATUS_CODE_LAST
  134. };
  135. enum
  136. {
  137. USE_ERROR_STRING_PARAM = -2,
  138. USE_ERROR_STRING_ID = -1,
  139. USE_ERROR_STRING_DEFAULT = USE_DEFAULT_CAPTION
  140. };
  141. BOOL Init();
  142. BOOL IsCertObjInstalled();
  143. BOOL SaveSettings();
  144. BOOL SetSecuritySettings();
  145. BOOL WriteRequest();
  146. BOOL PrepareRequest();
  147. BOOL PrepareRequestString(CString& request_text, CCryptBlob& request_blob);
  148. BOOL WriteRequestString(CString& request);
  149. BOOL SubmitRequest();
  150. BOOL SubmitRenewalRequest();
  151. BOOL WriteRenewalRequest();
  152. void DumpHeader(CString& str);
  153. void DumpOnlineHeader(CString& str);
  154. BOOL GetSelectedCertDescription(CERT_DESCRIPTION& cd);
  155. BOOL GetPFXFileCertDescription(CERT_DESCRIPTION& cd)
  156. {
  157. return GetCertDescription(GetPFXFileCert(), cd);
  158. }
  159. BOOL GetKeyCertDescription(CERT_DESCRIPTION& cd)
  160. {
  161. return GetCertDescription(GetKeyRingCert(), cd);
  162. }
  163. BOOL GetInstalledCertDescription(CERT_DESCRIPTION& cd)
  164. {
  165. return GetCertDescription(GetInstalledCert(), cd);
  166. }
  167. BOOL GetResponseCertDescription(CERT_DESCRIPTION& cd);
  168. BOOL GetRemoteFileCertDescription(CERT_DESCRIPTION& cd)
  169. {
  170. cd.m_CommonName = m_CommonName;
  171. cd.m_Country = m_Country;
  172. cd.m_Locality = m_Locality;
  173. cd.m_State = m_State;
  174. cd.m_Organization = m_Organization;
  175. cd.m_OrganizationUnit = m_OrganizationUnit;
  176. cd.m_CAName = m_CAName;
  177. cd.m_ExpirationDate = m_ExpirationDate;
  178. cd.m_Usage = m_Usage;
  179. cd.m_AltSubject = m_AltSubject;
  180. return TRUE;
  181. }
  182. BOOL HasPendingRequest()
  183. {
  184. return (NULL != GetPendingRequest());
  185. }
  186. BOOL HasInstalledCert();
  187. BOOL InstallResponseCert();
  188. HRESULT UninstallCert();
  189. BOOL InstallSelectedCert();
  190. BOOL InstallKeyRingCert();
  191. BOOL InstallImportPFXCert();
  192. BOOL InstallExportPFXCert();
  193. BOOL InstallCopyMoveFromRemote();
  194. BOOL InstallCopyMoveToRemote();
  195. BOOL ReplaceInstalled();
  196. BOOL CancelRequest();
  197. PCCERT_CONTEXT GetPendingRequest();
  198. PCCERT_CONTEXT GetResponseCert();
  199. PCCERT_CONTEXT GetInstalledCert();
  200. PCCERT_CONTEXT GetKeyRingCert();
  201. PCCERT_CONTEXT GetImportCert();
  202. PCCERT_CONTEXT GetPFXFileCert();
  203. void DeleteKeyRingCert()
  204. {
  205. if (m_pKeyRingCert != NULL)
  206. {
  207. CertFreeCertificateContext(m_pKeyRingCert);
  208. m_pKeyRingCert = NULL;
  209. }
  210. }
  211. IEnroll * GetEnrollObject();
  212. int GetStatusCode()
  213. {
  214. switch (m_status_code)
  215. {
  216. case REQUEST_UNDEFINED:
  217. IISDebugOutput(_T("GetStatusCode=REQUEST_UNDEFINED\r\n"));
  218. break;
  219. case REQUEST_NEW_CERT:
  220. IISDebugOutput(_T("GetStatusCode=REQUEST_NEW_CERT\r\n"));
  221. break;
  222. case REQUEST_RENEW_CERT:
  223. IISDebugOutput(_T("GetStatusCode=REQUEST_RENEW_CERT\r\n"));
  224. break;
  225. case REQUEST_REPLACE_CERT:
  226. IISDebugOutput(_T("GetStatusCode=REQUEST_REPLACE_CERT\r\n"));
  227. break;
  228. case REQUEST_INSTALL_CERT:
  229. IISDebugOutput(_T("GetStatusCode=REQUEST_INSTALL_CERT\r\n"));
  230. break;
  231. case REQUEST_PROCESS_PENDING:
  232. IISDebugOutput(_T("GetStatusCode=REQUEST_PROCESS_PENDING\r\n"));
  233. break;
  234. case REQUEST_IMPORT_KEYRING:
  235. IISDebugOutput(_T("GetStatusCode=REQUEST_IMPORT_KEYRING\r\n"));
  236. break;
  237. case REQUEST_IMPORT_CERT:
  238. IISDebugOutput(_T("GetStatusCode=REQUEST_IMPORT_CERT\r\n"));
  239. break;
  240. case REQUEST_EXPORT_CERT:
  241. IISDebugOutput(_T("GetStatusCode=REQUEST_EXPORT_CERT\r\n"));
  242. break;
  243. case REQUEST_COPY_MOVE_FROM_REMOTE:
  244. IISDebugOutput(_T("GetStatusCode=REQUEST_COPY_MOVE_FROM_REMOTE\r\n"));
  245. break;
  246. case REQUEST_COPY_MOVE_TO_REMOTE:
  247. IISDebugOutput(_T("GetStatusCode=REQUEST_COPY_MOVE_TO_REMOTE\r\n"));
  248. break;
  249. case STATUS_CODE_LAST:
  250. IISDebugOutput(_T("GetStatusCode=STATUS_CODE_LAST\r\n"));
  251. break;
  252. default:
  253. IISDebugOutput(_T("GetStatusCode=%d\r\n"),m_status_code);
  254. break;
  255. }
  256. return m_status_code;
  257. }
  258. void SetStatusCode(int code)
  259. {
  260. ASSERT(code >= 0 && code < STATUS_CODE_LAST);
  261. m_status_code = code;
  262. }
  263. BOOL FindInstanceNameForResponse(CString& str);
  264. BOOL IsResponseInstalled(CString& str);
  265. BOOL GetCertDescList(CCertDescList& list);
  266. BOOL LoadRenewalData();
  267. int MyStoreCertCount();
  268. void SetBodyTextID(int nID)
  269. {
  270. m_idErrorText = nID;
  271. m_strErrorText.Empty();
  272. }
  273. void SetBodyTextString(const CString& str)
  274. {
  275. m_strErrorText = str;
  276. m_idErrorText = USE_ERROR_STRING_PARAM;
  277. }
  278. void GetCertificateTemplate(CString& str)
  279. {
  280. str = _T("CertificateTemplate:");
  281. str += m_CertificateTemplate;
  282. }
  283. protected:
  284. void CreateDN(CString& str);
  285. BOOL WriteHeader();
  286. BOOL WriteRequestBody();
  287. BOOL GetCertDescription(PCCERT_CONTEXT pCert,
  288. CERT_DESCRIPTION& desc);
  289. public:
  290. int m_CAType;
  291. CString m_ConfigCA;
  292. CString m_CertificateTemplate;
  293. CString m_FriendlyName;
  294. int m_KeyLength;
  295. CString m_CommonName;
  296. CString m_OrganizationUnit;
  297. CString m_Organization;
  298. CString m_Locality;
  299. CString m_State;
  300. CString m_Country;
  301. // extra info for remote use
  302. CString m_CAName;
  303. CString m_ExpirationDate;
  304. CString m_Usage;
  305. CString m_AltSubject;
  306. CString m_SSLPort;
  307. CStringList m_OnlineCAList;
  308. CString m_MachineName;
  309. CString m_WebSiteInstanceName;
  310. BOOL m_OverWriteExisting;
  311. BOOL m_DeleteAfterCopy;
  312. CString m_MachineName_Remote;
  313. CString m_UserName_Remote;
  314. CStrPassword m_UserPassword_Remote;
  315. CString m_WebSiteInstanceName_Remote;
  316. CString m_ReqFileName;
  317. CString m_RespFileName;
  318. CString m_KeyFileName;
  319. CStrPassword m_KeyPassword;
  320. BOOL m_DefaultCSP;
  321. DWORD m_DefaultProviderType;
  322. DWORD m_CustomProviderType;
  323. CString m_CspName;
  324. CRYPT_HASH_BLOB * m_pSelectedCertHash;
  325. UINT m_idErrorText;
  326. CString m_strErrorText;
  327. CString m_strRenewalRequest;
  328. HRESULT m_hResult;
  329. BOOL m_CreateDirectory;
  330. BOOL m_SGCcertificat;
  331. BOOL m_ExportPFXPrivateKey;
  332. BOOL m_CertObjInstalled;
  333. BOOL m_MarkAsExportable;
  334. protected:
  335. PCCERT_CONTEXT m_pPendingRequest;
  336. PCCERT_CONTEXT m_RespCertContext;
  337. PCCERT_CONTEXT m_pInstalledCert;
  338. PCCERT_CONTEXT m_pKeyRingCert;
  339. IEnroll * m_pEnroll;
  340. int m_status_code; // what we are doing in this session
  341. };
  342. #endif // _CERTIFICAT_H