public bool Decode(OutboundConversionOptions options, bool acquireLicense) { this.CheckDisposed("Decode"); Util.ThrowOnNullArgument(options, "options"); this.decryptionStatus = RightsManagedMessageDecryptionStatus.Success; if (this.decodedItem != null) { return(true); } if (this.InternalSession == null) { this.decryptionStatus = RightsManagedMessageDecryptionStatus.NotSupported; throw new InvalidOperationException("Decoding of in-memory messages is not supported."); } RightsManagedMessageItem.CheckSession(this.InternalSession); this.SetConversionOptions(options); using (DisposeGuard disposeGuard = default(DisposeGuard)) { try { MessageItem messageItem = ItemConversion.OpenRestrictedContent(this, this.orgId, acquireLicense, out this.licenseAcquired, out this.useLicenseValue, out this.restrictionInfo); if (messageItem == null) { ExTraceGlobals.StorageTracer.TraceError(0L, "Failed to decode protected message - no user license is present."); throw new RightsManagementPermanentException(RightsManagementFailureCode.UnknownFailure, ServerStrings.GenericFailureRMDecryption); } disposeGuard.Add <MessageItem>(messageItem); this.UpdateEffectiveRights(); this.conversationOwner = new Participant(this.restrictionInfo.ConversationOwner, this.restrictionInfo.ConversationOwner, "SMTP"); this.CheckPermission(ContentRight.View); messageItem.CoreItem.TopLevelItem = (base.CoreItem.TopLevelItem ?? base.CoreItem); this.serverUseLicense = (messageItem.TryGetProperty(MessageItemSchema.DRMServerLicense) as string); this.publishLicense = (messageItem.TryGetProperty(MessageItemSchema.DrmPublishLicense) as string); this.rmsTemplate = RmsTemplate.CreateFromPublishLicense(this.publishLicense); MsgToRpMsgConverter.CallRM(delegate { this.publishedByExternalRMS = !RmsClientManager.IsPublishedByOrganizationRMS(this.orgId, this.publishLicense); }, ServerStrings.FailedToCheckPublishLicenseOwnership(this.orgId.ToString())); this.decodedItem = messageItem; disposeGuard.Success(); } catch (RightsManagementPermanentException exception) { this.decryptionStatus = RightsManagedMessageDecryptionStatus.CreateFromException(exception); throw; } catch (RightsManagementTransientException exception2) { this.decryptionStatus = RightsManagedMessageDecryptionStatus.CreateFromException(exception2); throw; } } return(true); }
internal MsgToRpMsgConverter(MessageItem envelopeMessage, OrganizationId orgId, string publishLicense, string serverUseLicense, OutboundConversionOptions options) { using (DisposeGuard disposeGuard = this.Guard()) { this.serverUseLicense = serverUseLicense; this.publishLicense = publishLicense; this.options = options; this.charset = ConvertUtils.GetItemMimeCharset(envelopeMessage.CoreItem.PropertyBag); this.messageId = envelopeMessage.InternetMessageId; this.orgId = RmsClientManagerUtils.OrgIdFromPublishingLicenseOrDefault(publishLicense, orgId); this.InitTenantLicenses(); MsgToRpMsgConverter.CallRM(delegate { DrmClientUtils.GetContentIdFromLicense(this.publishLicense, out this.contentId, out this.contentIdType); }, ServerStrings.FailedToParseUseLicense); disposeGuard.Success(); } }
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(); }