public Signature(string signer, XadesReference xadesReference, SignatureStatus signatureStatus, DateTime dateTimeForStatus)
 {
     Signer            = signer;
     XadesReference    = xadesReference;
     SignatureStatus   = signatureStatus;
     DateTimeForStatus = dateTimeForStatus;
 }
Beispiel #2
0
        public TestHelper GetXades()
        {
            Assert_state(_xadesReference);

            _xadesReference = new XadesReference(TransformReferenceToCorrectEnvironment(_jobStatusChanged.Signatures.ElementAt(0).XadesReference.Url));
            _client.GetXades(_xadesReference).ConfigureAwait(false).GetAwaiter().GetResult();

            return(this);
        }
Beispiel #3
0
            public void Simple_constructor()
            {
                //Arrange
                var url       = new Uri("http://localhost/test");
                var reference = new XadesReference(url);

                //Act

                //Assert
                Assert.Equal(url, reference.Url);
            }
Beispiel #4
0
        internal SignedItem(XmlElement signatureElement, XadesReference reference, SignaturePolicy signaturePolicy)
        {
            // determine type from reference uri
            Uri = reference.URI;
            if (Uri == "" || Uri == "#xpointer(/)")
            {
                Type = SignedItemType.Document;
            }
            else if (reference.Type == Xades.CounterSignatureRefernceType)
            {
                Type = SignedItemType.Signature;
            }
            else if (Uri[0] == '#')
            {
                string     idref = Uri.Substring(1);
                XmlElement node  = signatureElement.OwnerDocument.SelectSingleNode("//*[@id='" + idref + "' or @Id='" + idref + "']") as XmlElement;
                if (node != null && node.LocalName == "SignatureValue" && node.NamespaceURI == SignedXml.XmlDsigNamespaceUrl)
                {
                    Type = SignedItemType.Signature;
                }
                else
                {
                    Type = SignedItemType.Fragment;
                }
            }
            else
            {
                Type = SignedItemType.File;
            }

            // store transform
            Transform = new TransformSet(null, reference.TransformChain);
            // find or add commitment type in/to policy
            if (reference.CommitmentTypeId != null)
            {
                SignaturePolicy.CommitmentType commitmentType = signaturePolicy.CommitmentTypes.FirstOrDefault(x => x.Identifier == reference.CommitmentTypeId.Identifier);
                if (commitmentType == null)
                {
                    commitmentType = new SignaturePolicy.CommitmentType()
                    {
                        Identifier = reference.CommitmentTypeId.Identifier, Description = reference.CommitmentTypeId.Description
                    };
                    signaturePolicy.CommitmentTypes.Add(commitmentType);
                }
                CommitmentType = commitmentType;
            }

            IsValid = reference.IsValid;
        }
Beispiel #5
0
        public TestHelper ExpectJobStatusForSenderIs(JobStatus expectedStatus, Sender sender = null)
        {
            Assert_state(_jobResponse);

            _jobStatusChanged = GetCurrentReceipt(_jobResponse.JobId, _client, sender);
            Assert.Equal(expectedStatus, _jobStatusChanged.Status);

            if (_jobStatusChanged.Status == NoChanges)
            {
                return(this);
            }

            _confirmationReference = new ConfirmationReference(TransformReferenceToCorrectEnvironment(_jobStatusChanged.ConfirmationReference.Url));

            if (_jobStatusChanged.Status != CompletedSuccessfully)
            {
                return(this);
            }

            _xadesReference = new XadesReference(TransformReferenceToCorrectEnvironment(_jobStatusChanged.Signatures.First().XadesReference.Url));
            _padesReference = new PadesReference(TransformReferenceToCorrectEnvironment(_jobStatusChanged.PadesReference.Url));

            return(this);
        }
        public bool Countersign(X509Certificate2 cert, List <SignedItem> items, SignaturePolicy policy)
        {
            XmlDocument xmlDocument = XmlElement.OwnerDocument;

            // find or add id to signature value
            XmlElement signatureValue = XmlElement["SignatureValue", SignedXml.XmlDsigNamespaceUrl];

            if (signatureValue == null)
            {
                return(false);
            }
            string id = (signatureValue.Attributes["Id"] ?? signatureValue.Attributes["id"])?.Value;

            if (id == null)
            {
                HashSet <string> ids = new HashSet <string>(xmlDocument.SelectNodes("//@id | //@Id").OfType <XmlAttribute>().Select(x => x.Value));
                Random           rnd = new Random();
                for (; ;)
                {
                    id = $"signature-value-{rnd.Next():x8}";
                    if (!ids.Contains(id))
                    {
                        break;
                    }
                }
                signatureValue.SetAttribute("Id", id);
            }

            // find or add unsigned properties
            XmlElement qualifyingProperties = XmlElement.ChildNodes.OfType <XmlElement>().Where(x => x.LocalName == "Object" && x.NamespaceURI == SignedXml.XmlDsigNamespaceUrl).Select(x => x["QualifyingProperties", Xades.XadesNamespaceUrl]).SingleOrDefault(x => x != null);

            if (qualifyingProperties == null)
            {
                return(false);
            }
            XmlElement unsignedSignatureProperties = qualifyingProperties.GetOrCreate("UnsignedProperties", Xades.XadesNamespaceUrl).GetOrCreate("UnsignedSignatureProperties", Xades.XadesNamespaceUrl);
            XmlElement signatureLocation           = xmlDocument.CreateElement("CounterSignature", Xades.XadesNamespaceUrl);

            unsignedSignatureProperties.AppendChild(signatureLocation);

            // create signature
            Xades xades = Xades.Create(xmlDocument, signatureLocation);

            xades.SignatureProperties.SigningTime = DateTime.UtcNow;
            // add policy
            if (policy != null)
            {
                xades.SignatureProperties.PolicyId = new ObjectIdentifier()
                {
                    Identifier = policy.Identifier, Description = policy.Description
                };
                xades.SignatureProperties.PolicyURIs = new List <string>()
                {
                    policy.URI
                };
                // todo: determine transforms, calculate digest
            }
            // add countersignature reference
            XadesReference reference = new XadesReference($"#{id}", TransformSet.Signature.TransformChain)
            {
                Type = Xades.CounterSignatureRefernceType
            };
            SignedItem signatureItem = items.Single(x => x.Type == SignedItemType.Signature);

            if (policy != null && signatureItem.CommitmentType != null)
            {
                reference.CommitmentTypeId = new ObjectIdentifier()
                {
                    Identifier = signatureItem.CommitmentType.Identifier, Description = signatureItem.CommitmentType.Description
                }
            }
            ;
            xades.References.Add(reference);
            // add other references
            foreach (SignedItem item in items.Where(x => x.Type != SignedItemType.Signature))
            {
                reference = new XadesReference(item.Uri, item.Transform.TransformChain);
                if (policy != null && item.CommitmentType != null)
                {
                    reference.CommitmentTypeId = new ObjectIdentifier()
                    {
                        Identifier = signatureItem.CommitmentType.Identifier, Description = signatureItem.CommitmentType.Description
                    }
                }
                ;
                xades.References.Add(reference);
            }
            // sign
            string baseUri = xmlDocument.BaseURI;

            xades.UriResolver = u =>
            {
                Uri uri = baseUri == null ? new Uri(u) : new Uri(new Uri(baseUri), u);
                return(File.OpenRead(uri.LocalPath));
            };
            XmlElement result = xades.Sign(cert);

            signatureLocation.AppendChild(result);
            return(true);
        }
    }
}
Beispiel #7
0
 public async Task <Stream> GetXades(XadesReference xadesReference)
 {
     return(await RequestHelper.GetStream(xadesReference.Url).ConfigureAwait(false));
 }