using dk.nsi.seal.Model.Constants; using dk.nsi.seal.Model.ModelBuilders; using System; using System.Collections.Generic; using System.Security.Cryptography.X509Certificates; using System.Xml.Linq; namespace dk.nsi.seal.Model { public class OioWsTrustMessage : AbstractDomInfoExtractor { public OioWsTrustMessage(XDocument doc) : base(doc) { } /// /// Retrieve the action part of the SOAP header. /// ///
		///   <soap:Header>
		///     <wsa:Action>http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue</wsa:Action>
		///     ...
		///   </soap:Header>
		/// 
///
public string Action => SafeGetTagTextContent(new List() { SoapTags.Envelope, SoapTags.Header, WsaTags.Action }); /// /// Retrieve the MessageID part of the SOAP header. /// ///
		///   <soap:Header>
		///     ...
		///     <wsa:MessageID>urn:uuid:99999777-0000-0000</wsa:MessageID>
		///     ...
		///   </soap:Header>
		/// 
///
public String MessageID => SafeGetTagTextContent(new List() { SoapTags.Envelope, SoapTags.Header, WsaTags.MessageId }); /// /// Gets the signing certificate /// /// Returns the certificate contained in the XML signature or null if the OIOWSTrust message was not signed public X509Certificate2 GetSigningCertificate() { var certificateElm = GetTag(new List() { SoapTags.Envelope, SoapTags.Header, WsseTags.Security, DsTags.Signature, DsTags.KeyInfo, DsTags.X509Data, DsTags.X509Certificate }); if (certificateElm == null) { return null; } else { return new X509Certificate2(Convert.FromBase64String(certificateElm.Value)); } } /// /// Checks the signature on the - no trust-checks are performed. /// public void ValidateSignature() { var signedXml = new SealSignedXml(dom); var envSignatureOk = signedXml.CheckEnvelopeSignature(); var assertionSignatureOk = signedXml.CheckAssertionSignature(); if(!assertionSignatureOk) { var assertion = dom.Element(SoapTags.Body.Ns + SoapTags.Body.TagName)? .Element(WstTags.RequestSecurityTokenResponseCollection.Ns + WstTags.RequestSecurityTokenResponseCollection.TagName)? .Element(WstTags.RequestSecurityTokenResponse.Ns + WstTags.RequestSecurityTokenResponse.TagName)? .Element(WstTags.RequestedSecurityToken.Ns + WstTags.RequestedSecurityToken.TagName)? .Element(SamlTags.Assertion.Ns + SamlTags.Assertion.TagName); assertionSignatureOk = assertion == null ? assertionSignatureOk : new SealSignedXml(assertion).CheckAssertionSignature(); } if (!envSignatureOk || !assertionSignatureOk) { throw new ModelBuildException("Liberty signature could not be validated"); } } } }