public IPotentiallySigned <ServiceMetadata> ParseServiceMetadata(FetcherResponse fetcherResponse) { try { XmlDocument doc = new XmlDocument(); doc.Load(fetcherResponse.InputStream); var o = ClassFactory.FromXmlStream(fetcherResponse.InputStream); X509Certificate signer = null; if (o is SignedServiceMetadata) { signer = XmldsigVerifier.Verify(doc); } ServiceInformationType serviceInformation = ((tns.ServiceMetadata)o).ServiceInformation; List <ProcessMetadata <Endpoint> > processMetadatas = new List <ProcessMetadata <Endpoint> >(); foreach (ProcessType processType in serviceInformation.ProcessList.Process) { List <Endpoint> endpoints = new List <Endpoint>(); foreach (EndpointType endpointType in processType.ServiceEndpointList.Endpoint) { endpoints.Add( Endpoint.Of( TransportProfile.Of(endpointType.TransportProfile), new Uri(endpointType.EndpointURI), this.CertificateInstance(endpointType.Certificate.Data))); } processMetadatas.Add( ProcessMetadata <Endpoint> .Of( ProcessIdentifier.Of( processType.ProcessIdentifier.PrimitiveValue, Scheme.Of(processType.ProcessIdentifier.Scheme)), endpoints)); } return(Signed <ServiceMetadata> .Of( ServiceMetadata.Of( ParticipantIdentifier.Of( serviceInformation.ParticipantIdentifier.PrimitiveValue, Scheme.Of(serviceInformation.ParticipantIdentifier.Scheme)), DocumentTypeIdentifier.Of( serviceInformation.DocumentIdentifier.PrimitiveValue, Scheme.Of(serviceInformation.DocumentIdentifier.Scheme)), processMetadatas), signer)); } catch (Exception e) when(e is CertificateException | e is IOException) { throw new Exception(e.Message, e); } }
public IPotentiallySigned <ServiceMetadata> ParseServiceMetadata(FetcherResponse fetcherResponse) { try { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.Load(fetcherResponse.InputStream); var o = ClassFactory.FromXmlElement(doc.DocumentElement); Xml.Busdox.tns.ServiceMetadata serviceMetadata = o as Xml.Busdox.tns.ServiceMetadata; X509Certificate signer = null; if (o is SignedServiceMetadata) { signer = XmldsigVerifier.Verify(doc); serviceMetadata = ((SignedServiceMetadata)o).ServiceMetadata; } if (serviceMetadata == null) { throw new LookupException("ServiceMetadata element not found"); } ServiceInformationType serviceInformation = serviceMetadata.ServiceInformation; List <ProcessMetadata <Endpoint> > processMetadatas = new List <ProcessMetadata <Endpoint> >(); foreach (ProcessType processType in serviceInformation.ProcessList.Process) { List <Endpoint> endpoints = new List <Endpoint>(); foreach (EndpointType endpointType in processType.ServiceEndpointList.Endpoint) { var certificate = this.CertificateInstance(Convert.FromBase64String(endpointType.Certificate)); var endpointUri = new Uri(endpointType.EndpointReference.Address.PrimitiveValue); var profile = TransportProfile.Of(endpointType.TransportProfile); endpoints.Add(Endpoint.Of(profile, endpointUri, certificate)); } processMetadatas.Add( ProcessMetadata <Endpoint> .Of( ProcessIdentifier.Of( processType.ProcessIdentifier.PrimitiveValue, Scheme.Of(processType.ProcessIdentifier.Scheme)), endpoints)); } return(Signed <ServiceMetadata> .Of( ServiceMetadata.Of( ParticipantIdentifier.Of( serviceInformation.ParticipantIdentifier.PrimitiveValue, Scheme.Of(serviceInformation.ParticipantIdentifier.Scheme)), DocumentTypeIdentifier.Of( serviceInformation.DocumentIdentifier.PrimitiveValue, Scheme.Of(serviceInformation.DocumentIdentifier.Scheme)), processMetadatas), signer)); } catch (Exception e) when(e is CertificateException | e is IOException) { throw new Exception(e.Message, e); } }
public XmlDocument CreateXml(IList <SmpService> services) { var firstService = services.First(); var participant = Peppol.Common.Model.ParticipantIdentifier.Parse(firstService.PeppolParticipant.Identifier); var documentType = DocumentTypeIdentifier.Parse(firstService.PeppolDocument.Identifier); var signedServiceMetadata = new SignedServiceMetadata(); var serviceMetadata = new ServiceMetadata(); signedServiceMetadata.ServiceMetadata = serviceMetadata; var serviceInfo = new ServiceInformationType(); serviceMetadata.ServiceInformation = serviceInfo; serviceInfo.ParticipantIdentifier = new ParticipantIdentifier(); serviceInfo.ParticipantIdentifier.Scheme = participant.Scheme.Identifier; serviceInfo.ParticipantIdentifier.PrimitiveValue = participant.Identifier; serviceInfo.DocumentIdentifier = new DocumentIdentifier(); serviceInfo.DocumentIdentifier.Scheme = documentType.Scheme.Identifier; serviceInfo.DocumentIdentifier.PrimitiveValue = documentType.Identifier; serviceInfo.ProcessList = new ProcessListType(); foreach (var service in services) { var process = new ProcessType(); serviceInfo.ProcessList.Process.Add(process); var processIdentifier = Peppol.Common.Model.ProcessIdentifier.Parse(service.PeppolProcess.Identifier); process.ProcessIdentifier = new ProcessIdentifier(); process.ProcessIdentifier.Scheme = processIdentifier.Scheme.Identifier; process.ProcessIdentifier.PrimitiveValue = processIdentifier.Identifier; process.ServiceEndpointList = new ServiceEndpointList(); foreach (var endpoint in service.Endpoints) { var xEndpoint = new EndpointType(); xEndpoint.EndpointReference = new EndpointReference(); xEndpoint.EndpointReference.Address.PrimitiveValue = endpoint.Endpoint; xEndpoint.EndpointReference.ReferenceParameters = new ReferenceParameters(); xEndpoint.EndpointReference.Metadata = new Metadata(); xEndpoint.TransportProfile = "busdox-transport-as2-ver1p0"; xEndpoint.Certificate = endpoint.Certificate; xEndpoint.MinimumAuthenticationLevel = endpoint.MinimumAuthenticationLevel; xEndpoint.RequireBusinessLevelSignature = endpoint.RequireBusinessLevelSignature; xEndpoint.ServiceExpirationDate = new XmlDateTime(endpoint.ServiceExpirationDate); xEndpoint.ServiceActivationDate = new XmlDateTime(endpoint.ServiceActivationDate); xEndpoint.ServiceDescription = endpoint.ServiceDescription; xEndpoint.TechnicalContactUrl = endpoint.TechnicalContactUrl; process.ServiceEndpointList.Endpoint.Add(xEndpoint); } } var certPath = "conf\\test-keystore.pfx"; var certPass = "******"; var collection = new X509Certificate2Collection(); collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet); AsymmetricAlgorithm key = null; X509Certificate cert = null; foreach (var certificate in collection) { if (certificate.HasPrivateKey) { key = certificate.PrivateKey; cert = certificate; } } signedServiceMetadata.Signature = new Xml.Busdox.ds.Signature(); signedServiceMetadata.Signature.SignedInfo = new Xml.Busdox.ds.SignedInfo(); signedServiceMetadata.Signature.SignedInfo.Reference.Add(new Xml.Busdox.ds.Reference()); var xmlRoot = signedServiceMetadata.ToXmlElement(true, Encoding.UTF8, EOLType.CRLF); xmlRoot.RemoveChild(xmlRoot.LastChild); var xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml(xmlRoot.OuterXml); var signedXml = new SignedXml(xmlDoc); signedXml.SigningKey = key; var reference = new Reference(); reference.Uri = ""; var env = new XmlDsigEnvelopedSignatureTransform(); reference.AddTransform(env); signedXml.AddReference(reference); signedXml.ComputeSignature(); signedXml.KeyInfo = new KeyInfo(); var keyClause = new KeyInfoX509Data(cert); Debug.Assert(cert != null, nameof(cert) + " != null"); keyClause.AddSubjectName(cert.Subject); signedXml.KeyInfo.AddClause(keyClause); var xmlDigitalSignature = signedXml.GetXml(); xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true)); return(xmlDoc); }