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.

437 lines
15 KiB

  1. #include <windows.h>
  2. #include <wintrust.h>
  3. #include <shlwapi.h>
  4. #include <commctrl.h>
  5. #include <mimeole.h>
  6. #include <imnact.h>
  7. #include <imnxport.h>
  8. #include <wab.h>
  9. #include <reg.h>
  10. #include <cryptdlg.h>
  11. #include <smimepol.h>
  12. #include "demand.h"
  13. #include "..\..\ess\essout.h"
  14. #include "myassert.h"
  15. #include "smimetst.h"
  16. #include "data.h"
  17. #include "receipt.h"
  18. #include "attrib.h"
  19. class CMailListKey;
  20. const int TYPE_MSG = 1;
  21. const int TYPE_SIGN_INFO = 2;
  22. const int TYPE_ENV_INFO = 3;
  23. const int TYPE_SIGN_DATA = 4;
  24. const int TYPE_ENV_AGREE = 5;
  25. const int TYPE_ENV_TRANS = 6;
  26. const int TYPE_ENV_MAILLIST = 7;
  27. const int STATE_COMPOSE = 0x0000;
  28. const int STATE_READ = 0x1000;
  29. const int UM_SET_DATA = WM_USER + 1;
  30. const int UM_RESET = WM_USER + 2;
  31. const int UM_FILL = WM_USER + 3;
  32. class CItem
  33. {
  34. int m_type;
  35. int m_state;
  36. CItem * m_pSibling;
  37. CItem * m_pChild;
  38. CItem * m_pParent;
  39. public:
  40. CItem(int type, int state) {
  41. m_type = type;
  42. m_state = state;
  43. m_pSibling = NULL;
  44. m_pChild = NULL;
  45. m_pParent = NULL;
  46. }
  47. virtual ~CItem() {
  48. if (m_pParent != NULL) m_pParent->RemoveAsChild(this);
  49. }
  50. virtual HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND) {
  51. AssertSz(FALSE, "Should never get here. CItem::AddToMessage");
  52. return E_FAIL;
  53. }
  54. virtual HCERTSTORE GetAllStore(void) {
  55. if (m_pParent != NULL) return m_pParent->GetAllStore();
  56. return NULL;
  57. }
  58. virtual HCERTSTORE GetMyStore(void) {
  59. if (m_pParent != NULL) return m_pParent->GetMyStore();
  60. return NULL;
  61. }
  62. CItem * GetParent(void) const { return m_pParent; }
  63. int GetType(void) const {return m_type;}
  64. int GetState(void) const { return m_state; }
  65. CItem * Head(void) const { return m_pChild; }
  66. void MakeChild(CItem * pChild, CItem * pAfter);
  67. CItem * Next(void) const { return m_pSibling; }
  68. void RemoveAsChild(CItem * pChild);
  69. void SetParent(CItem * pParent) { m_pParent = pParent; }
  70. };
  71. class CSignInfo;
  72. class CSignData;
  73. class CMessage : public CItem, public IMimeSecurityCallback
  74. {
  75. HCERTSTORE m_hCertStoreAll;
  76. HCERTSTORE m_hCertStoreMy;
  77. int m_iterations;
  78. char m_rgchPlain[CCH_OPTION_STRING];
  79. char m_rgchCipher[CCH_OPTION_STRING];
  80. int m_fToFile:1;
  81. public:
  82. CMessage(int state=STATE_READ) : CItem(TYPE_MSG, state) {
  83. m_hCertStoreMy = NULL;
  84. strcpy(m_rgchPlain, "<none>");
  85. strcpy(m_rgchCipher, "c:\\test.eml");
  86. m_iterations = 1;
  87. m_fToFile = TRUE;
  88. }
  89. ~CMessage() {
  90. Assert(m_hCertStoreMy == NULL);
  91. }
  92. CSignInfo * Head(void) const { return (CSignInfo *) CItem::Head(); }
  93. HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND);
  94. HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND, CItem *);
  95. HRESULT Decode(HWND);
  96. HRESULT Encode(HWND);
  97. char * GetCipherFile(void) { return m_rgchCipher; }
  98. int GetFileNameSize(void) const { return CCH_OPTION_STRING; }
  99. HCERTSTORE GetAllStore(void);
  100. HCERTSTORE GetMyStore(void);
  101. char * GetPlainFile(void) { return m_rgchPlain; }
  102. int& GetIterationCount(void) { return m_iterations; }
  103. BOOL ResetMessage(void);
  104. void SetToFile(int i) { m_fToFile = i; }
  105. //
  106. STDMETHODIMP QueryInterface(REFIID, LPVOID *);
  107. STDMETHODIMP_(ULONG) AddRef(void);
  108. STDMETHODIMP_(ULONG) Release(void);
  109. STDMETHODIMP FindKeyFor(HWND, DWORD, DWORD, const CMSG_CMS_RECIPIENT_INFO *, DWORD *, CMS_CTRL_DECRYPT_INFO *, PCCERT_CONTEXT *);
  110. STDMETHODIMP GetParameters(PCCERT_CONTEXT, LPVOID, DWORD *, LPBYTE *);
  111. };
  112. class CSignInfo : public CItem
  113. {
  114. public:
  115. DWORD m_fBlob:1;
  116. CSignInfo(int state, CMessage * pparent) : CItem(TYPE_SIGN_INFO, state) {
  117. SetParent(pparent);
  118. m_fBlob = FALSE;
  119. }
  120. ~CSignInfo() {
  121. AssertSz(Head() == NULL, "Failed to release all children");
  122. AssertSz(Next() == NULL, "Failed to release all siblings");
  123. }
  124. HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND);
  125. int Count(void) const;
  126. CSignData * Head(void) const { return (CSignData *) CItem::Head(); }
  127. CSignInfo * Next(void) const { return (CSignInfo *) CItem::Next(); }
  128. };
  129. class CSignData : public CItem
  130. {
  131. CRYPT_ATTR_BLOB m_valLabel;
  132. CRYPT_ATTR_BLOB m_valReceipt;
  133. CRYPT_ATTR_BLOB m_valMLHistory;
  134. CRYPT_ATTR_BLOB m_valAuthAttrib;
  135. LPSTR m_szAuthAttribOID;
  136. CRYPT_ATTR_BLOB m_valUnAuthAttrib;
  137. LPSTR m_szUnAuthAttribOID;
  138. public:
  139. PCCERT_CONTEXT m_pccert;
  140. DWORD m_ulValidity;
  141. DWORD m_fReceipt:1;
  142. DWORD m_fLabel:1;
  143. DWORD m_fMLHistory:1;
  144. DWORD m_fUseSKI:1;
  145. DWORD m_fAuthAttrib:1;
  146. DWORD m_fUnAuthAttrib:1;
  147. CSignData(int state);
  148. ~CSignData(void) {
  149. CertFreeCertificateContext(m_pccert);
  150. AssertSz(Head() == NULL, "Should never have any children");
  151. AssertSz(Next() == NULL, "Failed to release all siblings");
  152. if (m_valLabel.pbData != NULL) free(m_valLabel.pbData);
  153. if (m_valReceipt.pbData != NULL) free(m_valReceipt.pbData);
  154. if (m_valMLHistory.pbData != NULL) free(m_valMLHistory.pbData);
  155. if (m_valAuthAttrib.pbData != NULL) free(m_valAuthAttrib.pbData);
  156. if (m_szAuthAttribOID != NULL) free(m_szAuthAttribOID);
  157. if (m_valUnAuthAttrib.pbData != NULL) free(m_valUnAuthAttrib.pbData);
  158. if (m_szUnAuthAttribOID != NULL) free(m_szUnAuthAttribOID);
  159. }
  160. HRESULT CSignData::BuildArrays(DWORD * pCount, DWORD * dwType,
  161. PROPVARIANT * rgvHash, PCCERT_CONTEXT * rgpccert,
  162. PROPVARIANT * rgvAuthAttr, HCERTSTORE hcertstore,
  163. IMimeSecurity2 * psmime2);
  164. void GetLabel(LPBYTE * ppb, DWORD * pcb) {
  165. *ppb = m_valLabel.pbData;
  166. *pcb = m_valLabel.cbData;
  167. }
  168. void GetMLHistory(LPBYTE * ppb, DWORD * pcb) {
  169. *ppb = m_valMLHistory.pbData;
  170. *pcb = m_valMLHistory.cbData;
  171. }
  172. void GetReceiptData(LPBYTE * ppbReceiptData,
  173. DWORD * pcbReceiptData) {
  174. *ppbReceiptData = m_valReceipt.pbData;
  175. *pcbReceiptData = m_valReceipt.cbData;
  176. }
  177. void GetAuthAttribData(LPBYTE * ppbAuthAttribData,
  178. DWORD * pcbAuthAttribData) {
  179. *ppbAuthAttribData = m_valAuthAttrib.pbData;
  180. *pcbAuthAttribData = m_valAuthAttrib.cbData;
  181. }
  182. LPSTR GetAuthAttribOID() {
  183. return m_szAuthAttribOID;
  184. }
  185. void GetUnAuthAttribData(LPBYTE * ppbUnAuthAttribData,
  186. DWORD * pcbUnAuthAttribData) {
  187. *ppbUnAuthAttribData = m_valUnAuthAttrib.pbData;
  188. *pcbUnAuthAttribData = m_valUnAuthAttrib.cbData;
  189. }
  190. LPSTR GetUnAuthAttribOID() {
  191. return m_szUnAuthAttribOID;
  192. }
  193. CSignInfo * GetParent(void) const {
  194. return (CSignInfo *) CItem::GetParent();
  195. }
  196. CSignData * Next(void) const { return (CSignData *) CItem::Next(); }
  197. void SetLabel(LPBYTE, DWORD);
  198. void SetMLHistory(LPBYTE pbMLHistoryData, DWORD cbMLHistoryData) {
  199. if (m_valMLHistory.pbData != NULL) {
  200. free(m_valMLHistory.pbData);
  201. m_valMLHistory.pbData = NULL;
  202. }
  203. if (cbMLHistoryData > 0) {
  204. m_valMLHistory.pbData = (LPBYTE) malloc(cbMLHistoryData);
  205. memcpy(m_valMLHistory.pbData, pbMLHistoryData, cbMLHistoryData);
  206. m_valMLHistory.cbData = cbMLHistoryData;
  207. }
  208. return;
  209. }
  210. void SetReceiptData(LPBYTE pbReceiptData, DWORD cbReceiptData) {
  211. if (m_valReceipt.pbData != NULL) {
  212. free(m_valReceipt.pbData);
  213. m_valReceipt.pbData = NULL;
  214. }
  215. if (cbReceiptData > 0) {
  216. m_valReceipt.pbData = (LPBYTE) malloc(cbReceiptData);
  217. memcpy(m_valReceipt.pbData, pbReceiptData, cbReceiptData);
  218. m_valReceipt.cbData = cbReceiptData;
  219. }
  220. return;
  221. }
  222. void SetAuthAttribData(LPBYTE pbAuthAttribData, DWORD cbAuthAttribData) {
  223. if (m_valAuthAttrib.pbData != NULL) {
  224. free(m_valAuthAttrib.pbData);
  225. m_valAuthAttrib.pbData = NULL;
  226. }
  227. if (cbAuthAttribData > 0) {
  228. m_valAuthAttrib.pbData = (LPBYTE) malloc(cbAuthAttribData);
  229. memcpy(m_valAuthAttrib.pbData, pbAuthAttribData, cbAuthAttribData);
  230. m_valAuthAttrib.cbData = cbAuthAttribData;
  231. }
  232. return;
  233. }
  234. void SetAuthAttribOID(LPSTR szAuthAttribOID) {
  235. if (m_szAuthAttribOID != NULL) {
  236. free(m_szAuthAttribOID);
  237. m_szAuthAttribOID = NULL;
  238. }
  239. if (szAuthAttribOID != NULL) {
  240. m_szAuthAttribOID = (LPSTR) malloc(strlen(szAuthAttribOID) + 1);
  241. strcpy(m_szAuthAttribOID, szAuthAttribOID);
  242. }
  243. return;
  244. }
  245. void SetUnAuthAttribData(LPBYTE pbUnAuthAttribData, DWORD cbUnAuthAttribData) {
  246. if (m_valUnAuthAttrib.pbData != NULL) {
  247. free(m_valUnAuthAttrib.pbData);
  248. m_valUnAuthAttrib.pbData = NULL;
  249. }
  250. if (cbUnAuthAttribData > 0) {
  251. m_valUnAuthAttrib.pbData = (LPBYTE) malloc(cbUnAuthAttribData);
  252. memcpy(m_valUnAuthAttrib.pbData, pbUnAuthAttribData, cbUnAuthAttribData);
  253. m_valUnAuthAttrib.cbData = cbUnAuthAttribData;
  254. }
  255. return;
  256. }
  257. void SetUnAuthAttribOID(LPSTR szUnAuthAttribOID) {
  258. if (m_szUnAuthAttribOID != NULL) {
  259. free(m_szUnAuthAttribOID);
  260. m_szUnAuthAttribOID = NULL;
  261. }
  262. if (szUnAuthAttribOID != NULL) {
  263. m_szUnAuthAttribOID = (LPSTR) malloc(strlen(szUnAuthAttribOID) + 1);
  264. strcpy(m_szUnAuthAttribOID, szUnAuthAttribOID);
  265. }
  266. return;
  267. }
  268. void SetDefaultCert() {
  269. if (SignHash.cbData != 0) {
  270. m_pccert = CertFindCertificateInStore(GetMyStore(), X509_ASN_ENCODING,
  271. 0, CERT_FIND_SHA1_HASH, &SignHash, NULL);
  272. }
  273. }
  274. };
  275. class CEnvData : public CItem
  276. {
  277. int m_iAlg;
  278. CRYPT_ATTR_BLOB m_valUnProtAttrib;
  279. LPSTR m_szUnProtAttribOID;
  280. public:
  281. DWORD m_fAttributes:1;
  282. DWORD m_fUnProtAttrib:1;
  283. CEnvData(int state, CMessage * pparent) : CItem(TYPE_ENV_INFO, state) {
  284. m_iAlg = 0;
  285. m_fAttributes = 0;
  286. m_fUnProtAttrib = FALSE;
  287. m_valUnProtAttrib.cbData = 0;
  288. m_valUnProtAttrib.pbData = NULL;
  289. m_szUnProtAttribOID = NULL;
  290. SetParent(pparent);
  291. }
  292. ~CEnvData(void) {
  293. AssertSz(Head() == NULL, "Should never have any children");
  294. AssertSz(Next() == NULL, "Failed to release all siblings");
  295. }
  296. HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND);
  297. int GetAlg(void) const { return m_iAlg; }
  298. void GetUnProtAttribData(LPBYTE * ppbUnProtAttribData,
  299. DWORD * pcbUnProtAttribData) {
  300. *ppbUnProtAttribData = m_valUnProtAttrib.pbData;
  301. *pcbUnProtAttribData = m_valUnProtAttrib.cbData;
  302. }
  303. LPSTR GetUnProtAttribOID() {
  304. return m_szUnProtAttribOID;
  305. }
  306. CEnvData * Next(void) const { return (CEnvData *) CItem::Next(); }
  307. void SetAlg(int iAlg) { m_iAlg = iAlg; }
  308. void SetUnProtAttribData(LPBYTE pbUnProtAttribData, DWORD cbUnProtAttribData) {
  309. if (m_valUnProtAttrib.pbData != NULL) {
  310. free(m_valUnProtAttrib.pbData);
  311. m_valUnProtAttrib.pbData = NULL;
  312. }
  313. if (cbUnProtAttribData > 0) {
  314. m_valUnProtAttrib.pbData = (LPBYTE) malloc(cbUnProtAttribData);
  315. memcpy(m_valUnProtAttrib.pbData, pbUnProtAttribData, cbUnProtAttribData);
  316. m_valUnProtAttrib.cbData = cbUnProtAttribData;
  317. }
  318. return;
  319. }
  320. void SetUnProtAttribOID(LPSTR szUnProtAttribOID) {
  321. if (m_szUnProtAttribOID != NULL) {
  322. free(m_szUnProtAttribOID);
  323. m_szUnProtAttribOID = NULL;
  324. }
  325. if (szUnProtAttribOID != NULL) {
  326. m_szUnProtAttribOID = (LPSTR) malloc(strlen(szUnProtAttribOID) + 1);
  327. strcpy(m_szUnProtAttribOID, szUnProtAttribOID);
  328. }
  329. return;
  330. }
  331. };
  332. class CEncItem : public CItem
  333. {
  334. public:
  335. DWORD m_cCerts;
  336. PCCERT_CONTEXT * m_rgpccert;
  337. BOOL m_fUseSKI:1;
  338. CEncItem(int a, int b) : CItem(a, b) {
  339. m_cCerts = 0;
  340. m_rgpccert = 0;
  341. }
  342. ~CEncItem() {
  343. for (DWORD i=0; i<m_cCerts; i++) {
  344. CertFreeCertificateContext(m_rgpccert[i]);
  345. }
  346. if (m_rgpccert != NULL) free(m_rgpccert);
  347. }
  348. CEnvData * GetParent(void) const {
  349. return (CEnvData *) CItem::GetParent();
  350. }
  351. };
  352. class CEnvCertTrans : public CEncItem
  353. {
  354. public:
  355. CEnvCertTrans(int state) : CEncItem(TYPE_ENV_TRANS, state) {
  356. }
  357. HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND);
  358. };
  359. class CEnvCertAgree : public CEncItem
  360. {
  361. public:
  362. CEnvCertAgree(int state) : CEncItem(TYPE_ENV_AGREE, state) {
  363. }
  364. HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND);
  365. };
  366. class CEnvMailList : public CItem
  367. {
  368. public:
  369. DWORD m_cKeys;
  370. CMailListKey ** m_rgKeys;
  371. int m_fUsePrivateCSPs:1;
  372. CEnvMailList(int state) : CItem(TYPE_ENV_MAILLIST, state) {
  373. m_cKeys = 0;
  374. m_rgKeys = NULL;
  375. m_fUsePrivateCSPs = FALSE;
  376. }
  377. ~CEnvMailList() {
  378. if (m_rgKeys != NULL) LocalFree(m_rgKeys);
  379. }
  380. HRESULT AddToMessage(DWORD * pulLayer, IMimeMessage * pmm, HWND);
  381. };