public bool Verify() { var signatureXml = GetSignature(); var signedXml = new ExtendedSignedXml(Xml); signedXml.LoadXml(signatureXml); foreach (var attachment in Attachments) { var reference = new Reference(attachment.Stream) { Uri = "cid:" + attachment.ContentId, DigestMethod = SignedXml.XmlDsigSHA256Url }; reference.AddTransform(new AttachmentContentSignatureTransform()); signedXml.AddExternalReference(reference); } var securityXml = GetSecurity(); var security = XmlToObject.Deserialize <AS4.Security.Security>(securityXml); var certificate = new X509Certificate2(security.BinarySecurityToken.Value); return(signedXml.CheckSignature(certificate.GetRSAPublicKey())); }
public void Sign(XmlDocument xml, Stream sed) { var security = new Security { BinarySecurityToken = new BinarySecurityToken { Id = Guid.NewGuid().ToString(), EncodingType = Namespaces.Base64Binary, ValueType = Namespaces.X509TokenProfile, Value = certificate.GetRawCertData() } }; var securityXml = Serializer.Serialize(security); var signedXml = new ExtendedSignedXml(xml) { SigningKey = certificate.GetRSAPrivateKey() }; var namespaces = new XmlNamespaceManager(xml.NameTable); namespaces.AddNamespace("s", Namespaces.SoapEnvelope); namespaces.AddNamespace("eb", Namespaces.ElectronicBusinessMessagingService); namespaces.AddNamespace("wsu", Namespaces.WebServiceSecurityUtility); var messaging = xml.SelectSingleNode("/s:Envelope/s:Header/eb:Messaging", namespaces); var body = xml.SelectSingleNode("/s:Envelope/s:Body", namespaces); var messagingReference = new Reference { Uri = "#" + messaging.Attributes["wsu:Id"].Value, DigestMethod = SignedXml.XmlDsigSHA256Url }; messagingReference.AddTransform(new XmlDsigExcC14NTransform()); signedXml.AddReference(messagingReference); var bodyReference = new Reference { Uri = "#" + body.Attributes["wsu:Id"].Value, DigestMethod = SignedXml.XmlDsigSHA256Url }; bodyReference.AddTransform(new XmlDsigExcC14NTransform()); signedXml.AddReference(bodyReference); var sedReference = new Reference(new NonCloseableStream(sed)) { Uri = "cid:DefaultSED", DigestMethod = SignedXml.XmlDsigSHA256Url }; sedReference.AddTransform(new AttachmentContentSignatureTransform()); signedXml.AddExternalReference(sedReference); var keyInfo = new KeyInfo(); keyInfo.AddClause(new SecurityTokenReference(security.BinarySecurityToken.Id)); signedXml.KeyInfo = keyInfo; signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl; signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA256Url; signedXml.ComputeSignature(); var signature = signedXml.GetXml(); Insert(signature, securityXml.DocumentElement); var header = xml.SelectSingleNode("/s:Envelope/s:Header", namespaces); Insert(securityXml, header); }
public void Sign() { var security = new AS4.Security.Security { BinarySecurityToken = new BinarySecurityToken { Id = Guid.NewGuid().ToString(), EncodingType = Soap.Namespaces.Base64Binary, ValueType = Soap.Namespaces.X509TokenProfile, Value = Certificate.GetRawCertData() } }; var securityXml = ObjectToXml.Serialize(security); var signedXml = new ExtendedSignedXml(Xml) { SigningKey = Certificate.GetRSAPrivateKey() }; foreach (var uri in Uris) { var reference = new Reference { Uri = "#" + uri, DigestMethod = SignedXml.XmlDsigSHA256Url }; reference.AddTransform(new XmlDsigExcC14NTransform()); signedXml.AddReference(reference); } foreach (var attachment in Attachments) { var reference = new Reference(new NonCloseableStream(attachment.Stream)) { Uri = "cid:" + attachment.ContentId, DigestMethod = SignedXml.XmlDsigSHA256Url }; reference.AddTransform(new AttachmentContentSignatureTransform()); signedXml.AddExternalReference(reference); } var keyInfo = new KeyInfo(); keyInfo.AddClause(new SecurityTokenReference(security.BinarySecurityToken.Id)); signedXml.KeyInfo = keyInfo; signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl; signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA256Url; signedXml.ComputeSignature(); var signature = signedXml.GetXml(); Insert(signature, securityXml.DocumentElement); var namespaces = new XmlNamespaceManager(Xml.NameTable); namespaces.AddNamespace("s", Soap.Namespaces.SoapEnvelope); var header = Xml.SelectSingleNode("/s:Envelope/s:Header", namespaces); Insert(securityXml, header); }