/// <summary> /// Get Client Certificate of EndPoint Address with participant validation /// </summary> /// <param name="recipientParticipant"></param> /// <param name="businessIdScheme"></param> /// <param name="smlDomain"></param> /// <param name="documentIdScheme"></param> /// <param name="documentIdValue"></param> /// <returns>Client Certificate if participant exists and has SMP service capabilities</returns> public string GetClientCertificate(string recipientParticipant, string businessIdScheme, string smlDomain, string documentIdScheme, string documentIdValue) { string clientCertificate = null; string smpUrl = BuildSmpUrl(recipientParticipant, businessIdScheme, smlDomain); ServiceGroup serviceGroupElements = _serviceGroupParser.GetServiceGroup(smpUrl); if (serviceGroupElements != null) { string smpSignedServiceUrl = BuildSmpSignedServiceUrl(recipientParticipant, businessIdScheme, smlDomain, documentIdScheme, documentIdValue); SignedServiceMetadata signedServiceElements = _signedServiceParser.GetSignedService(smpSignedServiceUrl); if (signedServiceElements != null) { try { clientCertificate = signedServiceElements.ServiceMetadata.ServiceInformation.ProcessList.Process[0].ServiceEndpointList.Endpoint.Certificate; } catch (Exception) { clientCertificate = null; } } else { clientCertificate = null; } } else { clientCertificate = null; } return(clientCertificate); }
/// <summary> /// Get Signed Service deserialization objects /// </summary> /// <param name="recipientParticipant"></param> /// <param name="businessIdScheme"></param> /// <param name="smlDomain"></param> /// <param name="documentIdScheme"></param> /// <param name="documentIdValue"></param> /// <returns>signedServiceElements type of SignedServiceMetadata</returns> public SignedServiceMetadata GetSignedServiceObjects(string recipientParticipant, string businessIdScheme, string smlDomain, string documentIdScheme, string documentIdValue) { SignedServiceParser _signedServiceParser = new SignedServiceParser(); string smpSignedServiceUrl = BuildSmpSignedServiceUrl(recipientParticipant, businessIdScheme, smlDomain, documentIdScheme, documentIdValue); SignedServiceMetadata signedServiceElements = _signedServiceParser.GetSignedService(smpSignedServiceUrl); return(signedServiceElements); }
/// <summary> /// Get EndPoint Address with participant validation /// </summary> /// <param name="recipientParticipant"></param> /// <param name="businessIdScheme"></param> /// <param name="smlDomain"></param> /// <param name="documentIdScheme"></param> /// <param name="documentIdValue"></param> /// <returns>EndPointAddress if participant exists and has SMP service capabilities</returns> public string GetEndPointAddress(string recipientParticipant, string businessIdScheme, string smlDomain, string documentIdScheme, string documentIdValue) { string endPointAddress = null; string smpUrl = BuildSmpUrl(recipientParticipant, businessIdScheme, smlDomain); ServiceGroup serviceGroupElements = _serviceGroupParser.GetServiceGroup(smpUrl); if (serviceGroupElements != null) { string smpSignedServiceUrl = BuildSmpSignedServiceUrl(recipientParticipant, businessIdScheme, smlDomain, documentIdScheme, documentIdValue); SignedServiceMetadata signedServiceElements = _signedServiceParser.GetSignedService(smpSignedServiceUrl); if (signedServiceElements != null) { if (signedServiceElements.ServiceMetadata.ServiceInformation == null) { string href = signedServiceElements.ServiceMetadata.Redirect.href; SignedServiceMetadata redirectServiceElements = _signedServiceParser.GetSignedService(href); if (redirectServiceElements != null) { if (redirectServiceElements.ServiceMetadata.Redirect == null) { endPointAddress = redirectServiceElements.ServiceMetadata.ServiceInformation.ProcessList.Process[0].ServiceEndpointList.Endpoint.EndpointReference.Address; } else { throw new Exception("WARNING: More than one Redirection was found in Metadata for Participant."); } } else { throw new Exception("bden:DocumentTypeNotAccepted - The recipient does not accept documents of this type."); } } else { endPointAddress = signedServiceElements.ServiceMetadata.ServiceInformation.ProcessList.Process[0].ServiceEndpointList.Endpoint.EndpointReference.Address; } } else { throw new Exception("bden:DocumentTypeNotAccepted - The recipient does not accept documents of this type."); } } else { throw new Exception("bden:ChannelFull - The channel is not accepting messages for this destination."); } return(endPointAddress); }
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); }