/// <summary> /// Creates a non-repudiation AS4 receipt that references a given <paramref name="includedUserMessage"/>. /// </summary> /// <param name="receiptMessageId"></param> /// <param name="includedUserMessage">The <see cref="Core.UserMessage"/> for which this receipt is created.</param> /// <param name="userMessageSecurityHeader">The security header to retrieve the signed references from to include in the receipt.</param> /// <param name="userMessageSendViaMultiHop"> /// Whether or not the user message was send in a multi-hop fashion or not. /// Setting this on <c>true</c> will result in a receipt with the referencing user message included in a RoutingInput element. /// </param> /// <exception cref="ArgumentNullException">The <paramref name="includedUserMessage"/> should not be <c>null</c>.</exception> public static Receipt CreateFor( string receiptMessageId, UserMessage includedUserMessage, SecurityHeader userMessageSecurityHeader, bool userMessageSendViaMultiHop = false) { if (includedUserMessage == null) { throw new ArgumentNullException(nameof(includedUserMessage)); } if (userMessageSecurityHeader != null) { IEnumerable <CryptoReference> signedReferences = userMessageSecurityHeader.GetReferences(); if (signedReferences.Any()) { var nonRepudiation = new NonRepudiationInformation( signedReferences.Select(Reference.CreateFromReferenceElement)); return(userMessageSendViaMultiHop.ThenMaybe(UserMessageMap.ConvertToRouting(includedUserMessage)) .Select(routing => new Receipt(receiptMessageId, includedUserMessage?.MessageId, nonRepudiation, routing)) .GetOrElse(() => new Receipt(receiptMessageId, includedUserMessage?.MessageId, nonRepudiation, routedUserMessage: null))); } } return(CreateFor(receiptMessageId, includedUserMessage, userMessageSendViaMultiHop)); }
/// <summary> /// Prevents a default instance of the <see cref="AS4Message"/> class from being created. /// </summary> /// <param name="serializeAsMultiHop">if set to <c>true</c> [serialize as multi hop].</param> private AS4Message(bool serializeAsMultiHop = false) { _serializeAsMultiHop = serializeAsMultiHop; _attachmens = new List <Attachment>(); _messageUnits = new List <MessageUnit>(); ContentType = "application/soap+xml"; SigningId = new SigningId(); SecurityHeader = new SecurityHeader(); }
/// <summary> /// Digitally signs the AS4Message using the given <paramref name="signatureConfiguration"/> /// </summary> /// <param name="signatureConfiguration"></param> public void Sign(CalculateSignatureConfig signatureConfiguration) { if (signatureConfiguration == null) { throw new ArgumentNullException(nameof(signatureConfiguration)); } SignStrategy signingStrategy = SignStrategy.ForAS4Message(this, signatureConfiguration); SecurityHeader.Sign(signingStrategy); }
/// <summary> /// Decrypt the AS4 Message using the specified <paramref name="certificate"/>. /// </summary> /// <param name="certificate"></param> public void Decrypt(X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException(nameof(certificate)); } var decryptor = DecryptionStrategyBuilder .Create(this) .WithCertificate(certificate) .Build(); SecurityHeader.Decrypt(decryptor); }
/// <summary> /// Encrypts the AS4 Message using the specified <paramref name="keyEncryptionConfig"/> /// and <paramref name="dataEncryptionConfig"/> /// </summary> /// <param name="keyEncryptionConfig"></param> /// <param name="dataEncryptionConfig"></param> public void Encrypt(KeyEncryptionConfiguration keyEncryptionConfig, DataEncryptionConfiguration dataEncryptionConfig) { if (keyEncryptionConfig == null) { throw new ArgumentNullException(nameof(keyEncryptionConfig)); } if (dataEncryptionConfig == null) { throw new ArgumentNullException(nameof(dataEncryptionConfig)); } var encryptor = EncryptionStrategyBuilder .Create(this, keyEncryptionConfig) .WithDataEncryptionConfiguration(dataEncryptionConfig) .Build(); SecurityHeader.Encrypt(encryptor); }
/// <summary> /// Creates message with a SOAP envelope. /// </summary> /// <param name="soapEnvelope">The SOAP envelope.</param> /// <param name="contentType">Type of the content.</param> /// <param name="securityHeader"></param> /// <param name="messagingHeader"></param> /// <param name="bodyElement"></param> ///<remarks>This method should only be used when creating an AS4 Message via deserialization.</remarks> /// <returns></returns> internal static async Task <AS4Message> CreateAsync( XmlDocument soapEnvelope, string contentType, SecurityHeader securityHeader, Xml.Messaging messagingHeader, Xml.Body1 bodyElement) { if (soapEnvelope == null) { throw new ArgumentNullException(nameof(soapEnvelope)); } if (String.IsNullOrWhiteSpace(contentType)) { throw new ArgumentException(@"ContentType must be defined.", nameof(contentType)); } if (securityHeader == null) { throw new ArgumentNullException(nameof(securityHeader)); } if (messagingHeader == null) { throw new ArgumentNullException(nameof(messagingHeader)); } if (bodyElement == null) { throw new ArgumentNullException(nameof(bodyElement)); } var result = new AS4Message { EnvelopeDocument = soapEnvelope, ContentType = contentType, SecurityHeader = securityHeader }; bool?IsMultihopAttributePresent() { const string messagingXPath = "/*[local-name()='Envelope']/*[local-name()='Header']/*[local-name()='Messaging']"; if (result.EnvelopeDocument?.SelectSingleNode(messagingXPath) is XmlElement messagingNode) { string role = messagingNode.GetAttribute("role", Constants.Namespaces.Soap12); return(!string.IsNullOrWhiteSpace(role) && role.Equals(Constants.Namespaces.EbmsNextMsh)); } return(null); } result.__hasMultiHopAttribute = IsMultihopAttributePresent(); string bodySecurityId = null; if (bodyElement.AnyAttr != null) { bodySecurityId = bodyElement.AnyAttr.FirstOrDefault(a => a.LocalName == "Id")?.Value; } result.SigningId = new SigningId(messagingHeader.SecurityId, bodySecurityId); result._messageUnits.AddRange( await SoapEnvelopeSerializer.GetMessageUnitsFromMessagingHeader(soapEnvelope, messagingHeader)); return(result); }