private void UnprotectAllAttachments() { if (!(this.decodedItem.TryGetProperty(MessageItemSchema.DRMServerLicense) is string)) { return; } foreach (AttachmentHandle handle in this.decodedItem.AttachmentCollection) { using (Attachment attachment = this.decodedItem.AttachmentCollection.Open(handle)) { Stream stream = null; StreamAttachment streamAttachment = attachment as StreamAttachment; if (StreamAttachment.TryOpenRestrictedContent(streamAttachment, this.orgId, out stream)) { using (stream) { using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { Util.StreamHandler.CopyStreamData(stream, contentStream); } } attachment.Save(); } } } }
private static void AttachICalToItem(Stream iCalStream, Item item) { iCalStream.Seek(0L, SeekOrigin.Begin); using (StreamAttachment streamAttachment = (StreamAttachment)item.AttachmentCollection.Create(AttachmentType.Stream)) { streamAttachment.FileName = CalendarUtil.NotSupportedInboundIcal; streamAttachment[AttachmentSchema.FailedInboundICalAsAttachment] = true; using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { Util.StreamHandler.CopyStreamData(iCalStream, contentStream); } streamAttachment.Save(); } }
internal static void SetSharingMessage(MessageItem item, SharingMessage sharingMessage) { using (StreamAttachment orCreateSharingMessageAttachment = SharingMessageAttachment.GetOrCreateSharingMessageAttachment(item)) { orCreateSharingMessageAttachment.PropertyBag[AttachmentSchema.AttachMimeTag] = "application/x-sharing-metadata-xml"; orCreateSharingMessageAttachment.PropertyBag[AttachmentSchema.AttachLongFileName] = "sharing_metadata.xml"; using (Stream contentStream = orCreateSharingMessageAttachment.GetContentStream(PropertyOpenMode.Create)) { sharingMessage.SerializeToStream(contentStream); contentStream.Flush(); } orCreateSharingMessageAttachment.Save(); } }
public StreamAttachment ConvertToImageAttachment(CoreAttachmentCollection collection, ImageFormat format) { base.CheckDisposed("ConvertToImageAttachment"); Util.ThrowOnNullArgument(collection, "collection"); EnumValidator.ThrowIfInvalid <ImageFormat>(format); StreamAttachment result; using (DisposeGuard disposeGuard = default(DisposeGuard)) { CoreAttachment coreAttachment = collection.InternalCreateCopy(new AttachmentType?(AttachmentType.Stream), base.CoreAttachment); disposeGuard.Add <CoreAttachment>(coreAttachment); StreamAttachment streamAttachment = (StreamAttachment)AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(AttachmentType.Stream)); disposeGuard.Add <StreamAttachment>(streamAttachment); string text = streamAttachment.FileName; if (string.IsNullOrEmpty(text)) { text = Attachment.GenerateFilename(); } string str = null; switch (format) { case ImageFormat.Jpeg: str = ".jpg"; break; case ImageFormat.Png: str = ".png"; break; } streamAttachment.FileName = text + str; streamAttachment.ContentType = "image/jpeg"; streamAttachment.IsInline = true; using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { if (!this.TryConvertToImage(contentStream, ImageFormat.Jpeg)) { ConvertUtils.SaveDefaultImage(contentStream); } } disposeGuard.Success(); result = streamAttachment; } return(result); }
public void UnprotectAttachment(AttachmentId attachmentId) { using (Attachment attachment = this.ProtectedAttachmentCollection.Open(attachmentId)) { StreamAttachment streamAttachment = attachment as StreamAttachment; if (streamAttachment == null) { throw new ObjectNotFoundException(ServerStrings.MapiCannotOpenAttachment); } using (Stream stream = StreamAttachment.OpenRestrictedContent(streamAttachment, this.orgId)) { using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { Util.StreamHandler.CopyStreamData(stream, contentStream); } } attachment.Save(); } }
protected override void OnBeforeSave() { if (this.decodedItem == null && !base.AttachmentCollection.IsDirty && base.IsRestricted && (base.Recipients.IsDirty || base.IsPropertyDirty(ItemSchema.Sender))) { this.EnsureIsDecoded(); } if (this.decodedItem != null) { string contentClass = base.TryGetProperty(InternalSchema.ContentClass) as string; if (this.rmsTemplate == null) { this.UnprotectAllAttachments(); RightsManagedMessageItem.CopyProtectableData(this.decodedItem, this); if (ObjectClass.IsRightsManagedContentClass(contentClass)) { base.Delete(StoreObjectSchema.ContentClass); } } else { this.charsetDetectionStringForProtectedData = new StringBuilder((int)Math.Min(this.ProtectedBody.Size, 2147483647L)); this.GetCharsetDetectionStringFromProtectedData(this.charsetDetectionStringForProtectedData); if (!ObjectClass.IsRightsManagedContentClass(contentClass)) { this[StoreObjectSchema.ContentClass] = "rpmsg.message"; } if (this.isSending) { byte[][] valueOrDefault = base.GetValueOrDefault <byte[][]>(InternalSchema.DRMLicense); if (valueOrDefault != null && valueOrDefault.Length == RightsManagedMessageItem.EmptyDrmLicense.Length && valueOrDefault[0].Length == RightsManagedMessageItem.EmptyDrmLicense[0].Length) { base.DeleteProperties(new PropertyDefinition[] { InternalSchema.DRMLicense }); } } else if (base.IsDraft && base.GetValueOrDefault <byte[][]>(InternalSchema.DRMLicense) == null) { this[InternalSchema.DRMLicense] = RightsManagedMessageItem.EmptyDrmLicense; } base.AttachmentCollection.RemoveAll(); using (StreamAttachment streamAttachment = base.AttachmentCollection.Create(AttachmentType.Stream) as StreamAttachment) { streamAttachment.FileName = "message.rpmsg"; streamAttachment.ContentType = "application/x-microsoft-rpmsg-message"; using (Stream stream = new PooledMemoryStream(131072)) { if (this.serverUseLicense == null || ((this.UsageRights & ContentRight.Owner) == ContentRight.Owner && this.rmsTemplate.RequiresRepublishingWhenRecipientsChange && this.CanRepublish && (base.Recipients.IsDirty || (base.IsPropertyDirty(ItemSchema.Sender) && this.conversationOwner == null)))) { if (this.ConversationOwner == null) { throw new InvalidOperationException("Conversation owner must be set before protecting the message."); } this.UnprotectAllAttachments(); using (MsgToRpMsgConverter msgToRpMsgConverter = new MsgToRpMsgConverter(this, this.ConversationOwner, this.orgId, this.rmsTemplate, this.options)) { msgToRpMsgConverter.Convert(this.decodedItem, stream); using (Stream stream2 = base.OpenPropertyStream(MessageItemSchema.DRMServerLicenseCompressed, PropertyOpenMode.Create)) { DrmEmailCompression.CompressUseLicense(msgToRpMsgConverter.ServerUseLicense, stream2); } if (this.InternalSession != null && this.InternalSession.MailboxOwner.Sid != null) { ExDateTime useLicenseExpiryTime = RmsClientManagerUtils.GetUseLicenseExpiryTime(msgToRpMsgConverter.ServerUseLicense, this.UsageRights); this[MessageItemSchema.DRMRights] = (int)this.UsageRights; this[MessageItemSchema.DRMExpiryTime] = useLicenseExpiryTime; using (RightsSignatureBuilder rightsSignatureBuilder = new RightsSignatureBuilder(msgToRpMsgConverter.ServerUseLicense, msgToRpMsgConverter.PublishLicense, RmsClientManager.EnvironmentHandle, msgToRpMsgConverter.LicensePair)) { this[MessageItemSchema.DRMPropsSignature] = rightsSignatureBuilder.Sign(this.UsageRights, useLicenseExpiryTime, this.InternalSession.MailboxOwner.Sid); } } goto IL_362; } } using (MsgToRpMsgConverter msgToRpMsgConverter2 = new MsgToRpMsgConverter(this, this.orgId, this.publishLicense, this.serverUseLicense, this.options)) { msgToRpMsgConverter2.Convert(this.decodedItem, stream); } IL_362: using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { stream.Seek(0L, SeekOrigin.Begin); Util.StreamHandler.CopyStreamData(stream, contentStream); } } bool flag = false; foreach (AttachmentHandle handle in this.decodedItem.AttachmentCollection) { if (!CoreAttachmentCollection.IsInlineAttachment(handle)) { flag = true; break; } } this[InternalSchema.AllAttachmentsHidden] = !flag; streamAttachment.Save(); } } this.decodedItem.Dispose(); this.decodedItem = null; this.effectiveRights = ContentRight.Owner; this.publishLicense = null; this.restrictionInfo = null; this.rmsTemplate = null; this.serverUseLicense = null; this.conversationOwner = null; } base.OnBeforeSave(); }
private void DecryptMsg(MessageItem rightsProtectedMessage, string useLicense, SafeRightsManagementHandle decryptorHandle) { if (decryptorHandle == null) { throw new ArgumentNullException("decryptorHandle"); } if (decryptorHandle.IsInvalid) { throw new ArgumentException("decryptorHandle"); } this.originalItem = rightsProtectedMessage; DrmEmailMessage drmEmailMessage = null; bool flag = false; try { ExTraceGlobals.RightsManagementTracer.TraceDebug((long)this.GetHashCode(), "Found a use license for server. Decrypting message"); DrmEmailMessageBinding messageBinding = new DrmEmailMessageBinding(this.drmMsgContainer.PublishLicense, decryptorHandle); this.drmMsgContainer.Bind(messageBinding, new CreateStreamCallbackDelegate(this.BodyStreamCallback), new CreateStreamCallbackDelegate(this.AttachmentsStreamCallback)); drmEmailMessage = this.drmMsgContainer.EmailMessage; this.SaveAndCloseCurrentAttachment(); this.decryptedItem.SafeSetProperty(InternalSchema.DRMServerLicense, useLicense); this.decryptedItem.SafeSetProperty(InternalSchema.DrmPublishLicense, this.drmMsgContainer.PublishLicense); if (drmEmailMessage.Attachments.Count > 0) { ExTraceGlobals.RightsManagementTracer.TraceDebug <int>((long)this.GetHashCode(), "Number of attachments in the rights protected message : {0}", drmEmailMessage.Attachments.Count); int num = 0; foreach (DrmEmailAttachment drmEmailAttachment in drmEmailMessage.Attachments) { using (Attachment attachment = this.decryptedItem.AttachmentCollection.Open(this.messageAttachmentIds[num++], null)) { attachment.FileName = drmEmailAttachment.FileName; attachment[InternalSchema.DisplayName] = drmEmailAttachment.DisplayName; attachment.ContentId = drmEmailAttachment.ContentId; attachment[InternalSchema.AttachContentLocation] = drmEmailAttachment.ContentLocation; attachment[InternalSchema.AttachMhtmlFlags] = drmEmailAttachment.AttachFlags; attachment[InternalSchema.AttachCalendarHidden] = drmEmailAttachment.AttachHidden; if (drmEmailAttachment.AttachHidden) { attachment.IsInline = true; } if (drmEmailMessage.BodyFormat == BodyFormat.Rtf) { attachment.RenderingPosition = (int)drmEmailAttachment.CharacterPosition; if (attachment.AttachmentType == AttachmentType.EmbeddedMessage || attachment.AttachmentType == AttachmentType.Stream) { attachment[InternalSchema.AttachRendering] = drmEmailAttachment.AttachRendering; } } if (this.decryptAttachments) { Stream stream = null; StreamAttachment streamAttachment = attachment as StreamAttachment; if (streamAttachment != null && StreamAttachment.TryOpenRestrictedContent(streamAttachment, this.orgId, out stream)) { using (stream) { using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create)) { Util.StreamHandler.CopyStreamData(stream, contentStream); } } } } attachment.Save(); } } } drmEmailMessage.Close(); drmEmailMessage = null; if (this.originalItem != null) { PersistablePropertyBag.CopyProperty(this.originalItem.PropertyBag, InternalSchema.TransportMessageHeaders, this.decryptedItem.PropertyBag); this.decryptedItem.Recipients.CopyRecipientsFrom(this.originalItem.Recipients); } this.decryptedItem.Save(SaveMode.NoConflictResolution); this.decryptedItem.Load(InternalSchema.ContentConversionProperties); flag = true; } finally { if (drmEmailMessage != null) { drmEmailMessage.Close(); drmEmailMessage = null; } if (!flag && this.decryptedItem != null) { this.decryptedItem.Dispose(); this.decryptedItem = null; } } }