//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth.ChannelElements { using System; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; using DotNetOpenAuth.Messaging; /// /// A binding element that signs outgoing messages and verifies the signature on incoming messages. /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sha", Justification = "Acronym")] public class RsaSha1ConsumerSigningBindingElement : RsaSha1SigningBindingElement { /// /// Initializes a new instance of the class. /// /// The certificate used to sign outgoing messages. public RsaSha1ConsumerSigningBindingElement(X509Certificate2 signingCertificate) { Requires.NotNull(signingCertificate, "signingCertificate"); this.SigningCertificate = signingCertificate; } /// /// Gets or sets the certificate used to sign outgoing messages. Used only by Consumers. /// public X509Certificate2 SigningCertificate { get; set; } /// /// Determines whether the signature on some message is valid. /// /// The message to check the signature on. /// /// true if the signature on the message is valid; otherwise, false. /// protected override bool IsSignatureValid(ITamperResistantOAuthMessage message) { throw new NotImplementedException(); } /// /// Calculates a signature for a given message. /// /// The message to sign. /// The signature for the message. /// /// This method signs the message per OAuth 1.0 section 9.3. /// protected override string GetSignature(ITamperResistantOAuthMessage message) { ErrorUtilities.VerifyOperation(this.SigningCertificate != null, OAuthStrings.X509CertificateNotProvidedForSigning); string signatureBaseString = ConstructSignatureBaseString(message, this.Channel.MessageDescriptions.GetAccessor(message)); byte[] data = Encoding.ASCII.GetBytes(signatureBaseString); var provider = (RSACryptoServiceProvider)this.SigningCertificate.PrivateKey; byte[] binarySignature = provider.SignData(data, "SHA1"); string base64Signature = Convert.ToBase64String(binarySignature); return base64Signature; } /// /// Creates a new object that is a copy of the current instance. /// /// /// A new object that is a copy of this instance. /// protected override ITamperProtectionChannelBindingElement Clone() { return new RsaSha1ConsumerSigningBindingElement(this.SigningCertificate); } } }