Example #1
0
 void ComputeSignature(HashAlgorithm hash, AsymmetricSignatureFormatter formatter, string signatureMethod)
 {
     this.Signature.SignedInfo.ComputeReferenceDigests();
     this.Signature.SignedInfo.ComputeHash(hash);
     byte[] signature;
     if (SecurityUtils.RequiresFipsCompliance && signatureMethod == SecurityAlgorithms.RsaSha256Signature)
     {
         // This is to avoid the RSAPKCS1SignatureFormatter.CreateSignature from using SHA256Managed (non-FIPS-Compliant).
         // Hence we precompute the hash using SHA256CSP (FIPS compliant) and pass it to method.
         // NOTE: RSAPKCS1SignatureFormatter does not understand SHA256CSP inherently and hence this workaround. 
         formatter.SetHashAlgorithm("SHA256");
         signature = formatter.CreateSignature(hash.Hash);
     }
     else
     {
         signature = formatter.CreateSignature(hash);
     }
     this.Signature.SetSignatureValue(signature);
 }
 /// <summary>
 /// Wrapper that creates a signature for SHA256 taking into consideration the special logic required for FIPS compliance
 /// </summary>
 /// <param name="formatter">the signature formatter</param>
 /// <param name="hash">the hash algorithm</param>
 /// <returns>byte array representing the signature</returns>
 internal static byte[] CreateSignatureForSha256( AsymmetricSignatureFormatter formatter, HashAlgorithm hash )
 {
     if ( SecurityUtils.RequiresFipsCompliance )
     {
         //
         // When FIPS is turned ON. We need to set the hash algorithm specifically 
         // as we need to pass the pre-computed buffer to CreateSignature, else
         // for SHA256 and FIPS turned ON, the underlying formatter does not understand the 
         // OID for the hashing algorithm.
         //
         formatter.SetHashAlgorithm( "SHA256" );
         return formatter.CreateSignature( hash.Hash );
     }
     else
     {
         //
         // Calling the formatter with the object allows us to be Crypto-Agile
         //
         return formatter.CreateSignature( hash );
     }
 }
 public FaultingAsymmetricSecurityKey(AsymmetricSecurityKey key = null, AsymmetricAlgorithm agorithm = null, AsymmetricSignatureDeformatter deformatter = null, AsymmetricSignatureFormatter formatter = null, HashAlgorithm hash = null, bool hasPrivateKey = false)
 {
     Key = key;
 }
 private void ComputeSignature(HashAlgorithm hash, AsymmetricSignatureFormatter formatter, string signatureMethod)
 {
     byte[] buffer;
     this.Signature.SignedInfo.ComputeReferenceDigests();
     this.Signature.SignedInfo.ComputeHash(hash);
     if (System.IdentityModel.SecurityUtils.RequiresFipsCompliance && (signatureMethod == "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"))
     {
         formatter.SetHashAlgorithm("SHA256");
         buffer = formatter.CreateSignature(hash.Hash);
     }
     else
     {
         buffer = formatter.CreateSignature(hash);
     }
     this.Signature.SetSignatureValue(buffer);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="AsymmetricSignatureProvider"/> class used to create and verify signatures.
        /// </summary>
        /// <param name="key">
        /// The <see cref="AsymmetricSecurityKey"/> that will be used for cryptographic operations.
        /// </param>
        /// <param name="algorithm">
        /// The signature algorithm to apply.
        /// </param>
        /// <param name="willCreateSignatures">
        /// If this <see cref="AsymmetricSignatureProvider"/> is required to create signatures then set this to true.
        /// <para>
        /// Creating signatures requires that the <see cref="AsymmetricSecurityKey"/> has access to a private key. 
        /// Verifying signatures (the default), does not require access to the private key.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// 'key' is null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// 'algorithm' is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// 'algorithm' contains only whitespace.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// willCreateSignatures is true and <see cref="AsymmetricSecurityKey"/>.KeySize is less than <see cref="SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForSigning"/>.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <see cref="AsymmetricSecurityKey"/>.KeySize is less than <see cref="SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForVerifying"/>. Note: this is always checked.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSecurityKey.GetHashAlgorithmForSignature"/> throws.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSecurityKey.GetHashAlgorithmForSignature"/> returns null.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSecurityKey.GetSignatureFormatter"/> throws.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSecurityKey.GetSignatureFormatter"/> returns null.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSecurityKey.GetSignatureDeformatter"/> throws.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSecurityKey.GetSignatureDeformatter"/> returns null.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSignatureFormatter.SetHashAlgorithm"/> throws.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Is thrown if the <see cref="AsymmetricSignatureDeformatter.SetHashAlgorithm"/> throws.
        /// </exception>
        public AsymmetricSignatureProvider(AsymmetricSecurityKey key, string algorithm, bool willCreateSignatures = false)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (algorithm == null)
            {
                throw new ArgumentNullException("algorithm");
            }

            if (string.IsNullOrWhiteSpace(algorithm))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10002, "algorithm"));
            }

            if (willCreateSignatures)
            {
                if (key.KeySize < SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForSigning)
                {
                    throw new ArgumentOutOfRangeException("key.KeySize", key.KeySize, string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10631, key.GetType(), SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForSigning));
                }
            }

            if (key.KeySize < SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForVerifying)
            {
                throw new ArgumentOutOfRangeException("key.KeySize", key.KeySize, string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10630, key.GetType(), SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForVerifying));
            }

            this.key = key;
            try
            {
                this.hash = this.key.GetHashAlgorithmForSignature(algorithm);
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10618, algorithm, this.key.ToString(), ex), ex);
            }

            if (this.hash == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10611, algorithm, this.key.ToString()));
            }

            if (willCreateSignatures)
            {
                try
                {
                    this.formatter = this.key.GetSignatureFormatter(algorithm);
                    this.formatter.SetHashAlgorithm(this.hash.GetType().ToString());
                }
                catch (Exception ex)
                {
                    if (DiagnosticUtility.IsFatal(ex))
                    {
                        throw;
                    }

                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10614, algorithm, this.key.ToString(), ex), ex);
                }

                if (this.formatter == null)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10615, algorithm, this.key.ToString()));
                }
            }

            try
            {
                this.deformatter = this.key.GetSignatureDeformatter(algorithm);
                this.deformatter.SetHashAlgorithm(this.hash.GetType().ToString());
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }

                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10616, algorithm, this.key.ToString(), ex), ex);
            }

            if (this.deformatter == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10617, algorithm, this.key.ToString()));
            }
        }
 internal static void LogSigning(SignedXml signedXml, object key, SignatureDescription signatureDescription, HashAlgorithm hash, AsymmetricSignatureFormatter asymmetricSignatureFormatter)
 {
     if (InformationLoggingEnabled)
     {
         string data = string.Format(CultureInfo.InvariantCulture, SecurityResources.GetResourceString("Log_SigningAsymmetric"), new object[] { GetKeyName(key), signatureDescription.GetType().Name, hash.GetType().Name, asymmetricSignatureFormatter.GetType().Name });
         WriteLine(signedXml, TraceEventType.Information, SignedXmlDebugEvent.Signing, data);
     }
 }