Beispiel #1
0
        static KeyDescriptor CreateKeyDescriptor()
        {
            var keyDescriptor = new KeyDescriptor();

            keyDescriptor.KeyInfo = new DSigKeyInfo();
            var x509Data = new X509Data();

            x509Data.Certificates.Add(TestCertSignOnly);
            keyDescriptor.KeyInfo.Data.Add(x509Data);
            return(keyDescriptor);
        }
Beispiel #2
0
        static KeyDescriptor CreateKeyDescriptor(X509Certificate2 cert)
        {
            var keyDescriptor = new KeyDescriptor();

            keyDescriptor.KeyInfo = new DSigKeyInfo();
            var x509Data = new X509Data();

            x509Data.Certificates.Add(cert);
            keyDescriptor.KeyInfo.Data.Add(x509Data);
            return(keyDescriptor);
        }
Beispiel #3
0
        public void X509Data_HashSetCollectionTests()
        {
            var set = new HashSet <X509Data>();

            var x509Data = new X509Data();

            set.Add(x509Data);

            // modify each property to check that hashcode is stable
            x509Data.SKI          = "anotherSKI";
            x509Data.SubjectName  = "anotherSubjectName";
            x509Data.CRL          = "anotherCRL";
            x509Data.IssuerSerial = new IssuerSerial(string.Empty, string.Empty);
            x509Data.Certificates.Add(ReferenceMetadata.X509CertificateData1);

            bool inCollection = set.Contains(x509Data);

            Assert.True(inCollection);
        }
Beispiel #4
0
        public void X509Data_ListCollectionTests()
        {
            var x509Data = new X509Data()
            {
                SKI          = "anotherSKI",
                SubjectName  = "anotherSubjectName",
                CRL          = "anotherCRL",
                IssuerSerial = new IssuerSerial(string.Empty, string.Empty),
            };

            x509Data.Certificates.Add(ReferenceMetadata.X509CertificateData1);

            var secondx509Data = new X509Data();

            var list = new List <X509Data> {
                x509Data, secondx509Data
            };
            var secondList = new List <X509Data> {
                x509Data, secondx509Data
            };

            Assert.True(Enumerable.SequenceEqual(list, secondList));
        }
Beispiel #5
0
 public KeyInfo()
 {
     X509Data = new X509Data();
 }
        /// <summary>
        /// Attempts to read the <see cref="XmlSignatureConstants.Elements.X509Data"/> element conforming to https://www.w3.org/TR/2001/PR-xmldsig-core-20010820/#sec-X509Data.
        /// </summary>
        /// <param name="reader">A <see cref="XmlReader"/> positioned on a <see cref="XmlSignatureConstants.Elements.X509Data"/> element.</param>
        /// <param name="data">The parsed <see cref="XmlSignatureConstants.Elements.X509Data"/> element.</param>
        protected virtual bool TryReadX509Data(XmlReader reader, out X509Data data)
        {
            if (reader == null)
            {
                throw LogArgumentNullException(nameof(reader));
            }

            if (!reader.IsStartElement(XmlSignatureConstants.Elements.X509Data, XmlSignatureConstants.Namespace))
            {
                data = null;
                return(false);
            }

            data = new X509Data();

            if (reader.IsEmptyElement)
            {
                throw XmlUtil.LogReadException(GetLogMessage("IDX30108"));
            }

            reader.ReadStartElement(XmlSignatureConstants.Elements.X509Data, XmlSignatureConstants.Namespace);
            while (reader.IsStartElement())
            {
                if (reader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConstants.Namespace))
                {
                    data.Certificates.Add(reader.ReadElementContentAsString());
                }
                else if (reader.IsStartElement(XmlSignatureConstants.Elements.X509IssuerSerial, XmlSignatureConstants.Namespace))
                {
                    if (data.IssuerSerial != null)
                    {
                        throw XmlUtil.LogReadException(GetLogMessage("IDX30015"), XmlSignatureConstants.Elements.X509IssuerSerial);
                    }
                    data.IssuerSerial = ReadIssuerSerial(reader);
                }
                else if (reader.IsStartElement(XmlSignatureConstants.Elements.X509SKI, XmlSignatureConstants.Namespace))
                {
                    if (data.SKI != null)
                    {
                        throw XmlUtil.LogReadException(GetLogMessage("IDX30015"), XmlSignatureConstants.Elements.X509SKI);
                    }
                    data.SKI = reader.ReadElementContentAsString();
                }
                else if (reader.IsStartElement(XmlSignatureConstants.Elements.X509SubjectName, XmlSignatureConstants.Namespace))
                {
                    if (data.SubjectName != null)
                    {
                        throw XmlUtil.LogReadException(GetLogMessage("IDX30015"), XmlSignatureConstants.Elements.X509SubjectName);
                    }
                    data.SubjectName = reader.ReadElementContentAsString();
                }
                else if (reader.IsStartElement(XmlSignatureConstants.Elements.X509CRL, XmlSignatureConstants.Namespace))
                {
                    if (data.CRL != null)
                    {
                        throw XmlUtil.LogReadException(GetLogMessage("IDX30015"), XmlSignatureConstants.Elements.X509CRL);
                    }
                    data.CRL = reader.ReadElementContentAsString();
                }
                else
                {
                    // Skip the element since it is not one of  <X509Certificate>, <X509IssuerSerial>, <X509SKI>, <X509SubjectName>, <X509CRL>
                    LogHelper.LogWarning(GetLogMessage("IDX30300"), reader.ReadOuterXml());
                }
            }

            // </X509Data>
            reader.ReadEndElement();

            return(true);
        }
Beispiel #7
0
        public static bool createEnvelopedBes(string pinNo, string signXML, String outXML, bool bInTest)
        {
            bool res = false;

            cardPinNo = pinNo;
            TestEnvelopedSignatureInitialize();
            try
            {
                // here is our custom envelope xml
                //  XmlDocument envelopeDoc = newEnvelope("edefter.xml");


                XmlDocument envelopeDoc = Conn.newEnvelope(signXML);
                XmlElement  exts        = (XmlElement)envelopeDoc.GetElementsByTagName("ext:UBLExtensions").Item(0);
                XmlElement  ext         = (XmlElement)exts.GetElementsByTagName("ext:UBLExtension").Item(0);
                XmlElement  extContent  = (XmlElement)ext.GetElementsByTagName("ext:ExtensionContent").Item(0);
                UriBuilder  ub          = new UriBuilder(Conn.ROOT_DIR + "efatura\\config\\");
                // create context with working dir
                Context context = new Context(ub.Uri);

                //UriBuilder ub2 = new UriBuilder(Conn.ROOT_DIR + "efatura\\config\\xmlsignature-config.xml");
                context.Config = new Config(Conn.ROOT_DIR + "efatura\\config\\xmlsignature-config.xml");

                // define where signature belongs to
                context.Document = envelopeDoc;

                // create signature according to context,
                // with default type (XADES_BES)
                XMLSignature signature = new XMLSignature(context, false);

                String setID = "Signature_" + envelopeDoc.GetElementsByTagName("cbc:ID").Item(0).InnerText;
                signature.Id          = setID;
                signature.SigningTime = DateTime.Now;

                // attach signature to envelope
                //envelopeDoc.DocumentElement.AppendChild(signature.Element);
                extContent.AppendChild(signature.Element);

                //add transforms for efatura
                Transforms transforms = new Transforms(context);
                transforms.addTransform(new Transform(context, TransformType.ENVELOPED.Url));


                // add document as reference,
                //signature.addDocument("#data1", "text/xml", false);
                signature.addDocument("", "text/xml", transforms, DigestMethod.SHA_256, false);

                ECertificate certificate = SmartCardManager.getInstance().getEInvoiceCertificate(cardPinNo);// getSignatureCertificate(true, false);
                if (certificate.isMaliMuhurCertificate())
                {
                    ValidationPolicy policy     = new ValidationPolicy();
                    String           policyPath = Conn.ROOT_DIR + "efatura\\config\\certval-policy-malimuhur.xml";
                    policy = PolicyReader.readValidationPolicy(policyPath);
                    ValidationSystem vs = CertificateValidation.createValidationSystem(policy);
                    context.setCertValidationSystem(vs);
                }
                else
                {
                    ValidationPolicy policy     = new ValidationPolicy();
                    String           policyPath = Conn.ROOT_DIR + "efatura\\config\\certval-policy.xml";
                    policy = PolicyReader.readValidationPolicy(policyPath);
                    ValidationSystem vs = CertificateValidation.createValidationSystem(policy);
                    context.setCertValidationSystem(vs);
                }

                if (CertValidation.validateCertificate(certificate) || bInTest)
                {
                    BaseSigner signer = SmartCardManager.getInstance().getSigner(cardPinNo, certificate);

                    X509Certificate2 msCert = certificate.asX509Certificate2();
                    signature.addKeyInfo(msCert.PublicKey.Key);
                    signature.addKeyInfo(certificate);

                    KeyInfo keyInfo      = signature.createOrGetKeyInfo();
                    int     elementCount = keyInfo.ElementCount;
                    for (int k = 0; k < elementCount; k++)
                    {
                        KeyInfoElement kiElement = keyInfo.get(k);
                        if (kiElement.GetType().IsAssignableFrom(typeof(X509Data)))
                        {
                            X509Data        x509Data        = (X509Data)kiElement;
                            X509SubjectName x509SubjectName = new X509SubjectName(context,
                                                                                  certificate.getSubject().stringValue());
                            x509Data.add(x509SubjectName);
                            break;
                        }
                    }

                    //signature.addKeyInfo(certificate);

                    signature.SignedInfo.CanonicalizationMethod = C14nMethod.EXCLUSIVE_WITH_COMMENTS;

                    signature.sign(signer);

                    // this time we dont use signature.write because we need to write
                    // whole document instead of signature
                    using (Stream s = new FileStream(outXML, FileMode.Create))
                    {
                        try
                        {
                            envelopeDoc.Save(s);
                            s.Flush();
                            s.Close();

                            res = true;
                        }
                        catch (Exception e)
                        {
                            res = false;
                            MessageBox.Show("Dosya kaydedilirken hata oluştu " + e.Message.ToString());
                            s.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                res = false;
                MessageBox.Show("Hata Oluştu \r\n" + e.Message.ToString());
            }

            return(res);
        }
Beispiel #8
0
 public KeyInfo()
 {
     X509Data = new X509Data();
 }