예제 #1
0
        /// <summary>
        /// Add the signature element to the memory stream.
        /// </summary>
        /// <param name="memoryStream">The memory stream.</param>
        internal override void Sign(MemoryStream memoryStream)
        {
            memoryStream.Position = 0;

            SafeXmlDocument document = new SafeXmlDocument();

            document.PreserveWhitespace = true;
            document.Load(memoryStream);

            WSSecurityUtilityIdSignedXml signedXml = new WSSecurityUtilityIdSignedXml(document);

            signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;

            //signedXml.AddReference("/soap:Envelope/soap:Header/t:ExchangeImpersonation");
            signedXml.AddReference("/soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp");

            signedXml.KeyInfo.AddClause(this.keyInfoNode);
            using (var hashedAlgorithm = new HMACSHA1(ExchangeServiceBase.SessionKey))
            {
                signedXml.ComputeSignature(hashedAlgorithm);
            }

            XmlElement signature = signedXml.GetXml();

            XmlNode wssecurityNode = document.SelectSingleNode(
                "/soap:Envelope/soap:Header/wsse:Security",
                WSSecurityBasedCredentials.NamespaceManager);

            wssecurityNode.AppendChild(signature);

            memoryStream.Position = 0;
            document.Save(memoryStream);
        }
예제 #2
0
        /// <summary>
        /// Add the signature element to the memory stream.
        /// </summary>
        /// <param name="memoryStream">The memory stream.</param>
        internal override void Sign(MemoryStream memoryStream)
        {
            memoryStream.Position = 0;

            SafeXmlDocument document = new SafeXmlDocument();

            document.PreserveWhitespace = true;
            document.Load(memoryStream);

            WSSecurityUtilityIdSignedXml signedXml = new WSSecurityUtilityIdSignedXml(document);

            signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;

            signedXml.SigningKey = this.certificate.PrivateKey;
            signedXml.AddReference("/soap:Envelope/soap:Header/wsa:To");
            signedXml.AddReference("/soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp");

            signedXml.KeyInfo.AddClause(this.keyInfoClause);
            signedXml.ComputeSignature();
            XmlElement signature = signedXml.GetXml();

            XmlNode wssecurityNode = document.SelectSingleNode(
                "/soap:Envelope/soap:Header/wsse:Security",
                WSSecurityBasedCredentials.NamespaceManager);

            wssecurityNode.AppendChild(signature);

            memoryStream.Position = 0;
            document.Save(memoryStream);
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="X509CertificateCredentials"/> class.
        /// </summary>
        /// <remarks>The X509Certificate2 argument should have private key in order to sign the message.</remarks>
        /// <param name="certificate">The X509Certificate2 object.</param>
        public X509CertificateCredentials(X509Certificate2 certificate)
            : base(null, true)
        {
            EwsUtilities.ValidateParam(certificate, "certificate");

            if (!certificate.HasPrivateKey)
            {
                throw new ServiceValidationException(Strings.CertificateHasNoPrivateKey);
            }

            this.certificate = certificate;

            string certId = WSSecurityUtilityIdSignedXml.GetUniqueId();

            this.SecurityToken = string.Format(
                X509CertificateCredentials.BinarySecurityTokenFormat,
                certId,
                Convert.ToBase64String(this.certificate.GetRawCertData()));

            SafeXmlDocument doc = new SafeXmlDocument();

            doc.PreserveWhitespace = true;
            doc.LoadXml(string.Format(X509CertificateCredentials.KeyInfoClauseFormat, certId));
            this.keyInfoClause = new KeyInfoNode(doc.DocumentElement);
        }
        /// <summary>
        /// Add the signature element to the memory stream.
        /// </summary>
        /// <param name="memoryStream">The memory stream.</param>
        internal override void Sign(MemoryStream memoryStream)
        {
            memoryStream.Position = 0;

            SafeXmlDocument document = new SafeXmlDocument();
            document.PreserveWhitespace = true;
            document.Load(memoryStream);

            WSSecurityUtilityIdSignedXml signedXml = new WSSecurityUtilityIdSignedXml(document);
            signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;

            signedXml.SigningKey = this.certificate.PrivateKey;
            signedXml.AddReference("/soap:Envelope/soap:Header/wsa:To");
            signedXml.AddReference("/soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp");

            signedXml.KeyInfo.AddClause(this.keyInfoClause);
            signedXml.ComputeSignature();
            XmlElement signature = signedXml.GetXml();

            XmlNode wssecurityNode = document.SelectSingleNode(
                "/soap:Envelope/soap:Header/wsse:Security",
                WSSecurityBasedCredentials.NamespaceManager);

            wssecurityNode.AppendChild(signature);

            memoryStream.Position = 0;
            document.Save(memoryStream);
        }
        /// <summary>
        /// Add the signature element to the memory stream.
        /// </summary>
        /// <param name="memoryStream">The memory stream.</param>
        internal override void Sign(MemoryStream memoryStream)
        {
            memoryStream.Position = 0;

            SafeXmlDocument document = new SafeXmlDocument();
            document.PreserveWhitespace = true;
            document.Load(memoryStream);

            WSSecurityUtilityIdSignedXml signedXml = new WSSecurityUtilityIdSignedXml(document);
            signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;

            //signedXml.AddReference("/soap:Envelope/soap:Header/t:ExchangeImpersonation");
            signedXml.AddReference("/soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp");

            signedXml.KeyInfo.AddClause(this.keyInfoNode);
            using (var hashedAlgorithm = new HMACSHA1(ExchangeServiceBase.SessionKey))
            {
                signedXml.ComputeSignature(hashedAlgorithm);
            }

            XmlElement signature = signedXml.GetXml();

            XmlNode wssecurityNode = document.SelectSingleNode(
                "/soap:Envelope/soap:Header/wsse:Security",
                WSSecurityBasedCredentials.NamespaceManager);

            wssecurityNode.AppendChild(signature);

            memoryStream.Position = 0;
            document.Save(memoryStream);
        }