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.

382 lines
20 KiB

  1. // --------------------------------------------------------------------------------
  2. // BookBody.h
  3. // Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  4. // Steven J. Bailey
  5. // --------------------------------------------------------------------------------
  6. #ifndef __BOOKBODY_H
  7. #define __BOOKBODY_H
  8. // --------------------------------------------------------------------------------
  9. // IID_CMessageBody - {CDBC9B51-A489-11d0-85C4-00C04FD85AB4}
  10. // --------------------------------------------------------------------------------
  11. DEFINE_GUID(IID_CMessageBody, 0xcdbc9b51, 0xa489, 0x11d0, 0x85, 0xc4, 0x0, 0xc0, 0x4f, 0xd8, 0x5a, 0xb4);
  12. // --------------------------------------------------------------------------------
  13. // Includes
  14. // --------------------------------------------------------------------------------
  15. #include "containx.h"
  16. #include "privunk.h"
  17. // --------------------------------------------------------------------------------
  18. // Forward Refs
  19. // --------------------------------------------------------------------------------
  20. class CMessageBody;
  21. class CBodyLockBytes;
  22. typedef struct _CERT_CONTEXT CERT_CONTEXT;
  23. typedef const CERT_CONTEXT *PCCERT_CONTEXT;
  24. typedef void *HCERTSTORE;
  25. typedef ULONG_PTR HCRYPTPROV;
  26. typedef struct tagTREENODEINFO *LPTREENODEINFO;
  27. #ifdef _WIN64
  28. // --------------------------------------------------------------------------------
  29. // CERTARRAY
  30. // --------------------------------------------------------------------------------
  31. struct tagCERTARRAY
  32. {
  33. DWORD cCerts;
  34. PCCERT_CONTEXT *rgpCerts;
  35. };
  36. typedef struct tagCERTARRAY CERTARRAY;
  37. #endif // _WIN64
  38. // --------------------------------------------------------------------------------
  39. // STOREARRAY
  40. // --------------------------------------------------------------------------------
  41. struct tagSTOREARRAY
  42. {
  43. DWORD cStores;
  44. HCERTSTORE *rgStores;
  45. };
  46. typedef struct tagSTOREARRAY STOREARRAY;
  47. // --------------------------------------------------------------------------------
  48. // BODYOPTIONS
  49. // --------------------------------------------------------------------------------
  50. typedef struct tagBODYOPTIONS {
  51. ENCODINGTYPE ietTransmit; // User specified transmission encoding...
  52. ULONG cbMaxLine; // Max Line length
  53. BOOL fWrapText; // Wrap the text in this body
  54. BOOL fRemoveNBSP; // Remove NBSP on GetData
  55. BOOL fDBCSEscape8; // Treat Escape (0x1b) characters in a dbcs encoding as 8bit
  56. BOOL fHideTNEF; // Hide TNEF Attachments
  57. // Security Options
  58. ULONG ulSecurityType; // OID_SECURITY_TYPE
  59. BLOB * rgblobHash; // OID_SECURITY_ALG_HASH and OID_SECURITY_ALG_HASH_RG
  60. BLOB blobBulk; // OID_SECURITY_ALG_BULK
  61. PCCERT_CONTEXT * rgpcCertSigning; // OID_SECURITY_CERT_SIGNING and OID_SECURITY_CERT_SIGNING_RG
  62. PCCERT_CONTEXT pcCertDecryption; // OID_SECURITY_CERT_DECRYPTION
  63. #ifdef _WIN65
  64. CERTARRAY caEncrypt; // OID_SECURITY_RG_CERT_ENCRYPT
  65. #endif // _WIN65
  66. HCERTSTORE hCertStore; // OID_SECURITY_HCERTSTORE
  67. STOREARRAY saSearchStore; // OID_SECURITY_SEARCHSTORES
  68. DWORD cIASN;
  69. ULONG *rgIASN; // OID_SECURITY_RG_IASN
  70. #ifdef SMIME_V3
  71. PCRYPT_ATTRIBUTES * rgrgpattrs[3]; // OID_SECURITY_AUTHATTR_RG
  72. #else // !SMIME_V3
  73. BLOB * rgblobSymCaps; // OID_SECURITY_SYMCAPS and OID_SECURITY_SYMCAPS_RG
  74. BLOB * rgblobAuthAttr; // OID_SECURITY_AUTHATTR and OID_SECURITY_AUTHATTR_RG
  75. BLOB * rgblobUnauthAttr; // OID_SECURITY_UNAUTHATTR and OID_SECURITY_UNAUTHATTR_RG
  76. FILETIME * rgftSigning; // OID_SECURITY_SIGNTIME and OID_SECURITY_SIGNTIME_RG
  77. #endif // SMIME_V3
  78. ULONG * rgulUserDef; // OID_SECURITY_USER_VALIDITY and OID_SECURITY_USER_VALIDITY_RG
  79. ULONG * rgulROValid; // OID_SECURITY_RO_MSG_VALIDITY and OID_SECURITY_RO_MSG_VALIDITY_RG
  80. HCRYPTPROV hCryptProv; // OID_SECURITY_HCRYPTPROV
  81. ULONG ulEncodeFlags; // OID_SECURITY_ENCODE_FLAGS
  82. BOOL fCertWithMsg; // OID_SECURITY_CERT_INCLUDED
  83. HWND hwndOwner; // OID_SECURITY_HWND_OWNER
  84. ENCODINGTYPE ietRequested; // OID_SECURITY_REQUESTED_CTE
  85. #ifdef SMIME_V3
  86. BLOB * rgblobReceipt; // OID_SECURITY_RECEIPT_RG
  87. BLOB * rgblobMsgHash; // OID_SECURITY_MESSAGE_HASH_RG
  88. LPWSTR pwszKeyPrompt; // OID_SECURITY_KEY_PROMPT
  89. #endif // SMIME_V3
  90. BOOL fShowMacBin; // OID_SHOW_MACBINARY
  91. BOOL fExternalBody; // OID_SUPPORT_EXTERNAL_BODY
  92. ULONG cSigners; // OID_SECURITY_SIGNATURE_COUNT // How many signers?
  93. BOOL fNoSecurityOnSave; // OID_NOSECURITY_ON_SAVE
  94. #ifdef SMIME_V3
  95. DWORD cRecipients; // Actual Recipients
  96. DWORD cRecipsAllocated; // Allocated Recipients
  97. PCMS_RECIPIENT_INFO rgRecipients; // Recipient Information
  98. HCERTSTORE hstoreEncrypt; // OID_SECURITY_ENCRYPT_CERT_BAG
  99. #endif // SMIME_V3
  100. } BODYOPTIONS, *LPBODYOPTIONS;
  101. // --------------------------------------------------------------------------------
  102. // BODYSTORAGE
  103. // --------------------------------------------------------------------------------
  104. typedef struct tagBODYSTORAGE {
  105. IID riid; // Type of data source
  106. IUnknown *pUnkRelease; // Release this to free this object
  107. union {
  108. ILockBytes *pLockBytes; // IID_ILockBytes
  109. IMimeWebDocument *pWebDocument; // IID_IMimeWebDocument
  110. };
  111. } BODYSTORAGE, *LPBODYSTORAGE;
  112. // --------------------------------------------------------------------------------
  113. // FCANSETDATAIID - IIDs that can be passed into IMimeBody::SetData
  114. // --------------------------------------------------------------------------------
  115. #define FBODYSETDATAIID(_iid) \
  116. (IID_IStream == riid || \
  117. IID_ILockBytes == riid || \
  118. IID_IMimeBody == riid || \
  119. IID_IMimeMessage == riid || \
  120. IID_IMimeWebDocument == riid)
  121. // --------------------------------------------------------------------------------
  122. // BODYSTATE
  123. // --------------------------------------------------------------------------------
  124. #define BODYSTATE_DIRTY FLAG01 // The body is dirty
  125. #define BODYSTATE_CSETTAGGED FLAG02 // The body is tagged with a character set
  126. #define BODYSTATE_SECURE FLAG03 // The body is secure
  127. #define BODYSTATE_EXTERNAL FLAG04 // The body was content-Type: message/external-body
  128. #define BODYSTATE_SKIPCSET FLAG05 // The body character set has been set explicitly
  129. #ifdef SMIME_V3
  130. // --------------------------------------------------------------------------------
  131. // ReceiptNames
  132. // --------------------------------------------------------------------------------
  133. typedef struct {
  134. DWORD cNames;
  135. CERT_NAME_BLOB * rgNames;
  136. } ReceiptNames;
  137. #endif // SMIME_V3
  138. // --------------------------------------------------------------------------------
  139. // Definition of IMimeBody
  140. // --------------------------------------------------------------------------------
  141. class CMessageBody : public CPrivateUnknown, public IMimeBodyW
  142. #ifdef SMIME_V3
  143. , public IMimeSecurity2
  144. #endif // SMIME_V3
  145. {
  146. public:
  147. // ----------------------------------------------------------------------------
  148. // Construction
  149. // ----------------------------------------------------------------------------
  150. CMessageBody(LPTREENODEINFO pNode, IUnknown *pUnkOuter=NULL);
  151. ~CMessageBody(void);
  152. // ---------------------------------------------------------------------------
  153. // IUnknown members
  154. // ---------------------------------------------------------------------------
  155. virtual STDMETHODIMP QueryInterface(REFIID riid, LPVOID * ppvObj) {
  156. return CPrivateUnknown::QueryInterface(riid, ppvObj); };
  157. virtual STDMETHODIMP_(ULONG) AddRef(void) {
  158. return CPrivateUnknown::AddRef();};
  159. virtual STDMETHODIMP_(ULONG) Release(void) {
  160. return CPrivateUnknown::Release(); };
  161. // ---------------------------------------------------------------------------
  162. // IPersistStreamInit members
  163. // ---------------------------------------------------------------------------
  164. STDMETHODIMP GetClassID(CLSID *pClassID);
  165. STDMETHODIMP GetSizeMax(ULARGE_INTEGER* pcbSize);
  166. STDMETHODIMP InitNew(void);
  167. STDMETHODIMP IsDirty(void);
  168. STDMETHODIMP Load(LPSTREAM pStream);
  169. STDMETHODIMP Save(LPSTREAM pStream, BOOL fClearDirty);
  170. // ---------------------------------------------------------------------------
  171. // IMimePropertySet members
  172. // ---------------------------------------------------------------------------
  173. STDMETHODIMP GetProp(LPCSTR pszName, DWORD dwFlags, LPPROPVARIANT pValue);
  174. STDMETHODIMP SetProp(LPCSTR pszName, DWORD dwFlags, LPCPROPVARIANT pValue);
  175. STDMETHODIMP AppendProp(LPCSTR pszName, DWORD dwFlags, LPPROPVARIANT pValue);
  176. STDMETHODIMP DeleteProp(LPCSTR pszName);
  177. STDMETHODIMP CopyProps(ULONG cNames, LPCSTR *prgszName, IMimePropertySet *pPropertySet);
  178. STDMETHODIMP MoveProps(ULONG cNames, LPCSTR *prgszName, IMimePropertySet *pPropertySet);
  179. STDMETHODIMP DeleteExcept(ULONG cNames, LPCSTR *prgszName);
  180. STDMETHODIMP QueryProp(LPCSTR pszName, LPCSTR pszCriteria, boolean fSubString, boolean fCaseSensitive);
  181. STDMETHODIMP GetCharset(LPHCHARSET phCharset);
  182. STDMETHODIMP SetCharset(HCHARSET hCharset, CSETAPPLYTYPE applytype);
  183. STDMETHODIMP GetParameters(LPCSTR pszName, ULONG *pcParams, LPMIMEPARAMINFO *pprgParam);
  184. STDMETHODIMP IsContentType(LPCSTR pszCntType, LPCSTR pszSubType);
  185. STDMETHODIMP Clone(IMimePropertySet **ppPropertySet);
  186. STDMETHODIMP SetOption(const TYPEDID oid, LPCPROPVARIANT pValue);
  187. STDMETHODIMP GetOption(const TYPEDID oid, LPPROPVARIANT pValue);
  188. STDMETHODIMP BindToObject(REFIID riid, void **ppvObject);
  189. STDMETHODIMP GetPropInfo(LPCSTR pszName, LPMIMEPROPINFO pInfo);
  190. STDMETHODIMP SetPropInfo(LPCSTR pszName, LPCMIMEPROPINFO pInfo);
  191. STDMETHODIMP EnumProps(DWORD dwFlags, IMimeEnumProperties **ppEnum);
  192. // ---------------------------------------------------------------------------
  193. // IMimeBody members
  194. // ---------------------------------------------------------------------------
  195. STDMETHODIMP GetEstimatedSize(ENCODINGTYPE ietEncoding, ULONG *pcbSize);
  196. STDMETHODIMP GetDataHere(ENCODINGTYPE ietEncoding, IStream *pStream);
  197. STDMETHODIMP GetData(ENCODINGTYPE ietEncoding, IStream **ppStream);
  198. STDMETHODIMP SetData(ENCODINGTYPE ietEncoding, LPCSTR pszPriType, LPCSTR pszSubType, REFIID riid, LPVOID pvObject);
  199. STDMETHODIMP CopyTo(IMimeBody *pBody);
  200. STDMETHODIMP GetCurrentEncoding(ENCODINGTYPE *pietEncoding);
  201. STDMETHODIMP SetCurrentEncoding(ENCODINGTYPE ietEncoding);
  202. STDMETHODIMP GetHandle(LPHBODY phBody);
  203. STDMETHODIMP GetTransmitInfo(LPTRANSMITINFO pTransmit);
  204. STDMETHODIMP GetOffsets(LPBODYOFFSETS pOffsets);
  205. STDMETHODIMP SetDisplayName(LPCSTR pszDisplay);
  206. STDMETHODIMP GetDisplayName(LPSTR *ppszDisplay);
  207. STDMETHODIMP IsType(IMSGBODYTYPE type);
  208. STDMETHODIMP SaveToFile(ENCODINGTYPE ietEncoding, LPCSTR pszFilePath);
  209. STDMETHODIMP EmptyData(void);
  210. // ---------------------------------------------------------------------------
  211. // IMimeBodyW members
  212. // ---------------------------------------------------------------------------
  213. STDMETHODIMP SetDisplayNameW(LPCWSTR pwszDisplay);
  214. STDMETHODIMP GetDisplayNameW(LPWSTR *ppwszDisplay);
  215. STDMETHODIMP SetDataW(ENCODINGTYPE ietEncoding, LPCWSTR pwszPriType, LPCWSTR pwszSubType, REFIID riid, LPVOID pvObject);
  216. STDMETHODIMP SaveToFileW(ENCODINGTYPE ietEncoding, LPCWSTR pwszFilePath);
  217. #ifdef SMIME_V3
  218. // ---------------------------------------------------------------------------
  219. // IMimeSecurity2 members
  220. // ---------------------------------------------------------------------------
  221. STDMETHODIMP Encode(HWND hwnd, DWORD dwFlags);
  222. STDMETHODIMP Decode(HWND hwnd, DWORD dwFlags, IMimeSecurityCallback * pCallback);
  223. STDMETHODIMP GetRecipientCount(DWORD dwFlags, DWORD *pdwRecipCount);
  224. STDMETHODIMP AddRecipient(DWORD dwFlags, DWORD cRecipData, PCMS_RECIPIENT_INFO recipData);
  225. STDMETHODIMP GetRecipient(DWORD dwFlags, DWORD iRecipient, DWORD cRecipients, PCMS_RECIPIENT_INFO pRecipData);
  226. STDMETHODIMP DeleteRecipient(DWORD dwFlags, DWORD iRecipent, DWORD cRecipients);
  227. STDMETHODIMP GetAttribute(DWORD dwFlags, DWORD iSigner, DWORD iAttributeSet,
  228. DWORD iInstance, LPCSTR pszObjId,
  229. CRYPT_ATTRIBUTE ** ppattr);
  230. STDMETHODIMP SetAttribute(DWORD dwFlags, DWORD iSigner, DWORD iAttributeSet,
  231. const CRYPT_ATTRIBUTE * pattr);
  232. STDMETHODIMP DeleteAttribute(DWORD dwFlags, DWORD iSigner, DWORD iAttributeSet,
  233. DWORD iInstance, LPCSTR pszObjid);
  234. STDMETHODIMP CreateReceipt(DWORD dwFlags, DWORD cbFromNames,
  235. const BYTE * pbFromNames, DWORD cSignerCertificates,
  236. PCCERT_CONTEXT * rgSignerCertificates,
  237. IMimeMessage ** ppMimeMessageReceipt);
  238. STDMETHODIMP GetReceiptSendersList(DWORD dwFlags, DWORD * pcSendersList,
  239. CERT_NAME_BLOB ** rgSendersList);
  240. STDMETHODIMP VerifyReceipt(DWORD dwFlags, IMimeMessage * pMimeMessageReceipt);
  241. STDMETHODIMP CapabilitiesSupported(DWORD * pdwFeatures);
  242. #endif // SMIME_V3
  243. // ---------------------------------------------------------------------------
  244. // CMessageBody members
  245. // ---------------------------------------------------------------------------
  246. BOOL FExtractRfc822Subject(LPWSTR *ppwszVal);
  247. void ClearDirty(void);
  248. void SetState(DWORD dwState);
  249. void ClearState(DWORD dwState);
  250. HRESULT Load(CInternetStream *pInternet);
  251. HRESULT HrGetLockBytes(ILockBytes **ppLockBytes);
  252. HRESULT HrBindToTree(CStreamLockBytes *pStmLock, LPTREENODEINFO pNode);
  253. DWORD DwGetFlags(BOOL fHideTnef);
  254. void SwitchContainers(CMessageBody *pBody);
  255. void CopyOptionsTo(CMessageBody *pBody, BOOL fNewOnwer=FALSE);
  256. HRESULT InternalSetOption(const TYPEDID oid, LPCPROPVARIANT pValue, BOOL fInternal, BOOL fNoDirty);
  257. // ---------------------------------------------------------------------------
  258. // Inlines
  259. // ---------------------------------------------------------------------------
  260. LPINETCSETINFO PGetTaggedCset(void) {
  261. EnterCriticalSection(&m_cs);
  262. LPINETCSETINFO p = m_pCsetTagged;
  263. LeaveCriticalSection(&m_cs);
  264. return p;
  265. }
  266. HRESULT GetPreviousEncoding(ENCODINGTYPE * pietEncoding) {
  267. if (ISFLAGSET(m_dwState, BODYSTATE_DIRTY)) {
  268. *pietEncoding = m_ietPrevious = IET_UNKNOWN;
  269. }
  270. else {
  271. *pietEncoding = m_ietPrevious;
  272. }
  273. return S_OK;
  274. }
  275. HRESULT SetPreviousEncoding(ENCODINGTYPE ietEncoding) {
  276. m_ietPrevious = ietEncoding;
  277. return S_OK;
  278. }
  279. // ---------------------------------------------------------------------------
  280. // CMessageBody Accessor Functions
  281. // ---------------------------------------------------------------------------
  282. virtual HRESULT PrivateQueryInterface(REFIID riid, LPVOID * ppvObj);
  283. void RevokeTreeNode(void);
  284. private:
  285. // ----------------------------------------------------------------------------
  286. // Private Methods
  287. // ----------------------------------------------------------------------------
  288. DWORD _GetSecureTypeFlags();
  289. HRESULT _HrCopyDataStream(IStream *pstmSource, IStream *pstmDest);
  290. void _FreeOptions();
  291. void _BindToExternalBody(void);
  292. #ifdef SMIME_V3
  293. static HRESULT _HrGetAttrs(DWORD cAttrs, PCRYPT_ATTRIBUTES * rgpattrs, LPCSTR pszOid, PROPVARIANT FAR * pvOut);
  294. static PCRYPT_ATTRIBUTE _FindAttribute(PCRYPT_ATTRIBUTES pattrs, LPCSTR pszObjId, DWORD iInstance);
  295. static HRESULT _HrSetAttribute(DWORD dwFlags, PCRYPT_ATTRIBUTES * ppattrs, LPCSTR pszObjectId, DWORD cb, const BYTE * pb);
  296. #endif // SMIME_V3
  297. #ifdef _WIN64
  298. static HRESULT _CAULToCERTARRAY(const CAUL caul, CERTARRAY *pca);
  299. static HRESULT _CERTARRAYToCAUL(const CERTARRAY ca, CAUL *pcaul);
  300. static HRESULT _CAUHToCERTARRAY(const CAUH cauh, CERTARRAY *pca);
  301. static HRESULT _CERTARRAYToCAUH(const CERTARRAY ca, CAUH *pcauh);
  302. #endif // _WIN64
  303. static HRESULT _CAULToCertStore(const CAUL caul, HCERTSTORE * phcertstore);
  304. static HRESULT _CertStoreToCAUL(const HCERTSTORE hcertstore, CAUL *pcaul);
  305. static HRESULT _CAULToSTOREARRAY(const CAUL caul, STOREARRAY *psa);
  306. static HRESULT _STOREARRAYToCAUL(const STOREARRAY sa, CAUL *pcaul);
  307. static HRESULT _CAUHToCertStore(const CAUH cauh, HCERTSTORE * phcertstore);
  308. static HRESULT _CertStoreToCAUH(const HCERTSTORE hcertstore, CAUH *pcauh);
  309. static HRESULT _CAUHToSTOREARRAY(const CAUH cauh, STOREARRAY *psa);
  310. static HRESULT _STOREARRAYToCAUH(const STOREARRAY sa, CAUH *pcauh);
  311. HRESULT _HrEnsureBodyOptionLayers(LPCPROPVARIANT ppv);
  312. HRESULT _HrEnsureBodyOptionLayers(ULONG ulLayers);
  313. void _FreeLayerArrays(void);
  314. HRESULT _CompareCopyBlobArray(const PROPVARIANT FAR * pvSource, BLOB FAR * FAR * prgblDestination, BOOL fNoDirty);
  315. #ifdef SMIME_V3
  316. HRESULT _HrSizeOfRecipInfos(DWORD cItems, const CMS_RECIPIENT_INFO *);
  317. HRESULT _HrCopyRecipInfos(DWORD cItems, const CMS_RECIPIENT_INFO * precipSrc,
  318. PCMS_RECIPIENT_INFO precipDst);
  319. HRESULT _HrMapPublicKeyAlg(CERT_PUBLIC_KEY_INFO *, DWORD *,
  320. CRYPT_ALGORITHM_IDENTIFIER **);
  321. HRESULT _SetNames(ReceiptNames * pnames, DWORD cNames,
  322. CERT_NAME_BLOB * rgNames);
  323. HRESULT _MergeNames(ReceiptNames * pnames, DWORD cNames,
  324. CERT_NAME_BLOB * rgNames);
  325. STDMETHODIMP _GetReceiptRequest(DWORD dwFlags,
  326. PSMIME_RECEIPT_REQUEST *ppreq,
  327. ReceiptNames *pReceiptsTo,
  328. DWORD *pcbReceipt,
  329. LPBYTE *ppbReceipt,
  330. DWORD *pcbMsgHash,
  331. LPBYTE *ppbMsgHash);
  332. #endif // SMIME_V3
  333. private:
  334. // ----------------------------------------------------------------------------
  335. // Private Data
  336. // ----------------------------------------------------------------------------
  337. LONG m_cRef; // Reference Counting
  338. DWORD m_dwState; // State of this body
  339. DWORD m_cbExternal; // Size of external body if m_dwState, BODYSTATE_EXTERNAL
  340. LPWSTR m_pszDisplay; // Display Name
  341. BODYOPTIONS m_rOptions; // Body Options
  342. BODYSTORAGE m_rStorage; // Body Data Source/Storage
  343. ENCODINGTYPE m_ietEncoding; // Internal Body Format
  344. LPINETCSETINFO m_pCharset; // Character set to be sent with
  345. LPINETCSETINFO m_pCsetTagged; // Original Character Set
  346. LPCONTAINER m_pContainer; // Property Container
  347. LPTREENODEINFO m_pNode; // Tree NodeInfo
  348. CRITICAL_SECTION m_cs; // Thread Safety
  349. ENCODINGTYPE m_ietPrevious; // Previous CTE
  350. };
  351. // --------------------------------------------------------------------------------
  352. // Types
  353. // --------------------------------------------------------------------------------
  354. typedef CMessageBody *LPMESSAGEBODY;
  355. #endif // __BOOKBODY_H