Beispiel #1
0
        public static string GetSignedRequestXades(string request, X509Certificate2 certificate, string privateKeyPassword)
        {
            var provider = SigningKeyProvider.GetProvider(certificate);

            provider.SetCointainerPassword(privateKeyPassword);

            var originalDoc = new XmlDocument()
            {
                PreserveWhitespace = _PRESERVE_WHITESPACE
            };

            originalDoc.LoadXml(request);

            var signatureid = String.Format("xmldsig-{0}", Guid.NewGuid().ToString().ToLower());
            var signedXml   = GetXadesSignedXml(provider, originalDoc, signatureid);

            var keyInfo = GetKeyInfo(Convert.ToBase64String(certificate.GetRawCertData()));

            signedXml.KeyInfo = keyInfo;

            var xadesInfo = GetXadesInfo(certificate);

            var xadesObject = GetXadesObject(provider, xadesInfo, signatureid);

            signedXml.AddXadesObject(xadesObject, provider.DigestMethod);

            signedXml.ComputeSignature();

            InjectSignatureToOriginalDoc(signedXml, originalDoc);

            return(originalDoc.OuterXml);
        }
Beispiel #2
0
        public static XadesObject GetXadesObject(SigningKeyProvider provider, XadesInfo xadesInfo, string signatureid)
        {
            XadesObject xadesObject = new XadesObject();

            xadesObject.QualifyingProperties.Target = String.Format("#{0}", signatureid);
            xadesObject.QualifyingProperties.SignedProperties.Id = String.Format("{0}-signedprops", signatureid);
            SignedSignatureProperties signedSignatureProperties = xadesObject.QualifyingProperties.SignedProperties.SignedSignatureProperties;


            var             x509CertificateParser = new Org.BouncyCastle.X509.X509CertificateParser();
            X509Certificate bouncyCert            = x509CertificateParser.ReadCertificate(Convert.FromBase64String(xadesInfo.RawPK));

            var cert = new Cert
            {
                IssuerSerial =
                {
                    X509IssuerName   = GetOidRepresentation(bouncyCert.IssuerDN.ToString()),
                    X509SerialNumber = bouncyCert.SerialNumber.ToString()
                }
            };

            cert.CertDigest.DigestMethod.Algorithm = provider.DigestMethod;

            var rawCertData = Convert.FromBase64String(xadesInfo.RawPK);
            var pkHash      = HashAlgorithm.Create(provider.HashAlgorithmName);
            var hashValue   = pkHash.ComputeHash(rawCertData);

            cert.CertDigest.DigestValue = hashValue;

            signedSignatureProperties.SigningCertificate.CertCollection.Add(cert);

            signedSignatureProperties.SigningTime = GetSigningTimeOffset(xadesInfo.SigningDateTimeUTC, xadesInfo.TimeZoneOffsetMinutes);
            return(xadesObject);
        }
Beispiel #3
0
        public static XadesSignedXml GetXadesSignedXml(SigningKeyProvider provider, XmlDocument originalDoc, string signatureid)
        {
            var signedXml = new XadesSignedXml(originalDoc)
            {
                SigningKey = provider.SigningKey
            };

            signedXml.Signature.Id     = signatureid;
            signedXml.SignatureValueId = String.Format("{0}-sigvalue", signatureid);

            var reference = new Reference
            {
                Uri          = "#signed-data-container",
                DigestMethod = provider.DigestMethod,
                Id           = String.Format("{0}-ref0", signatureid)
            };

            reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
            reference.AddTransform(new XmlDsigExcC14NTransform());
            signedXml.AddReference(reference);

            signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationUrl;
            signedXml.SignedInfo.SignatureMethod        = provider.SignatureMethod;

            return(signedXml);
        }