Ejemplo n.º 1
0
        protected virtual ISigner MakeSigner(SignatureAndHashAlgorithm algorithm, bool raw, bool forSigning, ICipherParameters cp)
        {
            if (algorithm != null != TlsUtilities.IsTlsV12(mContext))
            {
                throw new InvalidOperationException();
            }
            if (algorithm != null && algorithm.Signature != SignatureAlgorithm)
            {
                throw new InvalidOperationException();
            }
            byte   hashAlgorithm = algorithm?.Hash ?? 2;
            object digest2;

            if (raw)
            {
                IDigest digest = new NullDigest();
                digest2 = digest;
            }
            else
            {
                digest2 = TlsUtilities.CreateHash(hashAlgorithm);
            }
            IDigest digest3 = (IDigest)digest2;
            ISigner signer  = new DsaDigestSigner(CreateDsaImpl(hashAlgorithm), digest3);

            signer.Init(forSigning, MakeInitParameters(forSigning, cp));
            return(signer);
        }
Ejemplo n.º 2
0
        public static ISigner GetSigner(AsymmetricSigningAlgorithm asymmetricSigningAlgorithm, IDigest hash, Error error)
        {
            if (error == null)
            {
                return(null);
            }
            if (hash == null)
            {
                error.setError("AE008", "Hash digest is null");
                return(null);
            }
            ISigner sig = null;

            switch (asymmetricSigningAlgorithm)
            {
            case AsymmetricSigningAlgorithm.RSA:
                sig = new RsaDigestSigner(hash);
                break;

            case AsymmetricSigningAlgorithm.ECDSA:
                ECDsaSigner dsaSigner = new ECDsaSigner();
                sig = new DsaDigestSigner(dsaSigner, hash);
                break;
            }
            return(sig);
        }
        private ISigner GetSigner(TSignatureAlgorithm signatureAlgorithm, THashAlgorithm hashAlgorithm, AsymmetricKeyParameter serverPrivateKey)
        {
            ISigner result = null;

            switch (signatureAlgorithm)
            {
            case TSignatureAlgorithm.Anonymous:
                break;

            case TSignatureAlgorithm.RSA:
                break;

            case TSignatureAlgorithm.DSA:
                break;

            case TSignatureAlgorithm.ECDSA:
                result = new DsaDigestSigner(new ECDsaSigner(), GetDigest(hashAlgorithm));
                break;

            default:
                break;
            }
            result.Init(true, serverPrivateKey);
            //result.Init(true, new ParametersWithRandom(serverPrivateKey, this.mContext.SecureRandom));
            return(result);
        }
Ejemplo n.º 4
0
        public virtual ISigner CreateVerifyer(AsymmetricKeyParameter publicKey)
        {
            ISigner s = new DsaDigestSigner(CreateDsaImpl(), new Sha1Digest());

            s.Init(false, publicKey);
            return(s);
        }
Ejemplo n.º 5
0
        protected virtual ISigner MakeSigner(IDigest d, bool forSigning, ICipherParameters cp)
        {
            ISigner s = new DsaDigestSigner(CreateDsaImpl(), d);

            s.Init(forSigning, cp);
            return(s);
        }
Ejemplo n.º 6
0
            public ISigner CreateEngine(EngineUsage usage)
            {
                ISigner sig = new DsaDigestSigner(DSA_PROVIDER.CreateEngine(usage), CreateDigest(parameters.DigestAlgorithm));

                sig.Init((usage == EngineUsage.SIGNING), sigParams);

                return(sig);
            }
Ejemplo n.º 7
0
            public ISigner CreateEngine(EngineUsage usage)
            {
                ISigner sig = new DsaDigestSigner(new DsaSigner(new HMacDsaKCalculator(FipsShs.CreateHmac(parameters.DigestAlgorithm))), FipsShs.CreateDigest(parameters.DigestAlgorithm));

                sig.Init((usage == EngineUsage.SIGNING), sigParams);

                return(sig);
            }
Ejemplo n.º 8
0
        public byte[] CalculateRawSignature(AsymmetricKeyParameter privateKey, byte[] md5andsha1)
        {
            // Note: Only use the SHA1 part of the hash
            ISigner sig = new DsaDigestSigner(new DsaSigner(), new NullDigest());

            sig.Init(true, privateKey);
            sig.BlockUpdate(md5andsha1, 16, 20);
            return(sig.GenerateSignature());
        }
Ejemplo n.º 9
0
        public virtual byte[] CalculateRawSignature(SecureRandom random,
                                                    AsymmetricKeyParameter privateKey, byte[] md5andsha1)
        {
            // Note: Only use the SHA1 part of the hash
            ISigner sig = new DsaDigestSigner(CreateDsaImpl(), new NullDigest());

            sig.Init(true, new ParametersWithRandom(privateKey, random));
            sig.BlockUpdate(md5andsha1, 16, 20);
            return(sig.GenerateSignature());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Verifies the given signature matches the supplied hash.
        /// </summary>
        public bool VerifyHash(byte[] hash, byte[] signature)
        {
            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }

            var signer = new DsaDigestSigner(new ECDsaSigner(), new NullDigest());

            return(Verify(hash, signature, signer));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Signs the supplied <paramref name="hash"/>.
        /// </summary>
        public byte[] SignHash(byte[] hash)
        {
            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }

            var signer = new DsaDigestSigner(new ECDsaSigner(), new NullDigest());

            return(Sign(hash, signer));
        }
        public byte[] Sign(byte[] data, string privateKey)
        {
            var key = PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));

            var dsaDigestSigner = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());

            dsaDigestSigner.Init(true, key);

            dsaDigestSigner.BlockUpdate(data, 0, data.Length);
            return(dsaDigestSigner.GenerateSignature());
        }
        public bool Verify(byte[] data, byte[] signature, string publicKey)
        {
            var key = PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));

            var dsaDigestSigner = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());

            dsaDigestSigner.Init(false, key);

            dsaDigestSigner.BlockUpdate(data, 0, data.Length);

            return(dsaDigestSigner.VerifySignature(signature));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Gets the signing stream.
        /// </summary>
        /// <returns></returns>
        public HashingStream GetSigningStream(Keyczar keyczar)
        {
            var digest = PublicKey.GetDigest();
            var signer = new DsaDigestSigner(new DsaSigner(), digest);
            var param  = new DsaPrivateKeyParameters(X.ToBouncyBigInteger(),
                                                     new DsaParameters(PublicKey.P.ToBouncyBigInteger(),
                                                                       PublicKey.Q.ToBouncyBigInteger(),
                                                                       PublicKey.G.ToBouncyBigInteger()));

            signer.Init(forSigning: true, parameters: new ParametersWithRandom(param, Secure.Random));

            return(new DigestStream(signer));
        }
Ejemplo n.º 15
0
        public void ComputeSignature()
        {
            if (key != null)
            {
                if (m_signature.SignedInfo.SignatureMethod == null)
                {
                    //defaults do RSA SHA256 Signature
                    m_signature.SignedInfo.SignatureMethod = XmlDsigConstants.XmlDsigRSASHA256Url;
                }

                var sd = SignatureMethodParser.Parse(m_signature.SignedInfo.SignatureMethod);

                IDigest hash = XmlEncHashes.GetHashByName(sd.HashName);

                DigestReferences();

                ISigner signer = null;
                // in need for a CryptoConfig factory
                if (key is DsaKeyParameters)
                {
                    if (sd.CipherName != "DSA")
                    {
                        throw new CryptographicException("DSA SignatureAlgorithm is not supported by the signing key.");
                    }
                    signer = new DsaDigestSigner(new DsaSigner(), hash);
                }
                else if (key is RsaKeyParameters)
                {
                    if (sd.CipherName != "RSA")
                    {
                        throw new CryptographicException("RSA SignatureAlgorithm is not supported by the signing key.");
                    }
                    signer = new RsaDigestSigner(hash);
                }

                if (signer != null)
                {
                    signer.Init(true, key);

                    byte[] signed = SignerHelper.ComputeSignature(signer, SignedInfoTransformed());

                    m_signature.SignatureValue = signed;
                }
            }
            else
            {
                throw new CryptographicException("signing key is not specified");
            }
        }
Ejemplo n.º 16
0
        protected virtual ISigner MakeSigner(SignatureAndHashAlgorithm algorithm, bool raw, bool forSigning, ICipherParameters cp)
        {
            if (algorithm != null != TlsUtilities.IsTlsV12(this.mContext))
            {
                throw new InvalidOperationException();
            }
            if (algorithm != null && algorithm.Signature != this.SignatureAlgorithm)
            {
                throw new InvalidOperationException();
            }
            byte    hashAlgorithm = (algorithm == null) ? 2 : algorithm.Hash;
            IDigest digest        = raw ? new NullDigest() : TlsUtilities.CreateHash(hashAlgorithm);
            ISigner signer        = new DsaDigestSigner(this.CreateDsaImpl(hashAlgorithm), digest);

            signer.Init(forSigning, this.MakeInitParameters(forSigning, cp));
            return(signer);
        }
Ejemplo n.º 17
0
        protected virtual ISigner MakeSigner(SignatureAndHashAlgorithm algorithm, bool raw, bool forSigning, ICipherParameters cp)
        {
            //IL_0014: Unknown result type (might be due to invalid IL or missing references)
            //IL_002b: Unknown result type (might be due to invalid IL or missing references)
            if (algorithm != null != TlsUtilities.IsTlsV12(mContext))
            {
                throw new InvalidOperationException();
            }
            if (algorithm != null && algorithm.Signature != SignatureAlgorithm)
            {
                throw new InvalidOperationException();
            }
            byte    hashAlgorithm = algorithm?.Hash ?? 2;
            IDigest digest        = (raw ? new NullDigest() : TlsUtilities.CreateHash(hashAlgorithm));
            ISigner signer        = new DsaDigestSigner(CreateDsaImpl(hashAlgorithm), digest);

            signer.Init(forSigning, MakeInitParameters(forSigning, cp));
            return(signer);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Gets the verifying stream.
        /// </summary>
        /// <returns></returns>
        public VerifyingStream GetVerifyingStream(KeyczarBase keyczar)
        {
            var signer = new DsaSigner();

            signer.Init(forSigning: false, parameters: new DsaPublicKeyParameters(Y.ToBouncyBigInteger(),
                                                                                  new DsaParameters(
                                                                                      P.ToBouncyBigInteger(),
                                                                                      Q.ToBouncyBigInteger(),
                                                                                      G.ToBouncyBigInteger())));
            var digest       = GetDigest();
            var digestSigner = new DsaDigestSigner(signer, digest);

            return(new DigestStream(digestSigner, sigRepair: sig => {
                if (!keyczar.Config.StrictDsaVerification)
                {
                    return Utility.RemoveJunkFronAnsiObj(sig);
                }
                return sig;
            }));
        }
Ejemplo n.º 19
0
        protected virtual ISigner MakeSigner(SignatureAndHashAlgorithm algorithm, bool raw, bool forSigning,
                                             ICipherParameters cp)
        {
            if ((algorithm != null) != TlsUtilities.IsTlsV12(mContext))
            {
                throw new InvalidOperationException();
            }

            if (algorithm != null && algorithm.Signature != SignatureAlgorithm)
            {
                throw new InvalidOperationException();
            }

            byte    hashAlgorithm = algorithm == null ? HashAlgorithm.sha1 : algorithm.Hash;
            IDigest d             = raw ? new NullDigest() : TlsUtilities.CreateHash(hashAlgorithm);

            ISigner s = new DsaDigestSigner(CreateDsaImpl(hashAlgorithm), d);

            s.Init(forSigning, MakeInitParameters(forSigning, cp));
            return(s);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] publicKey, byte[] data)
        {
            var signer = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());

            DsaPublicKeyParameters pubKey = null;

            try
            {
                pubKey = (DsaPublicKeyParameters)CreateAsymmetricKeyParameterFromPublicKeyInfo(publicKey);
            }
            catch (Exception exception)
            {
                string message = "Public Key Creation Failure!\n" +
                                 $"{exception.Message}.\n" +
                                 $"The public key file is corrupted, verify public key file or try another key.\n" +
                                 $"If all fails create a new key pair.";
                throw new CryptoException(message, exception);
            }
            signer.Init(false, pubKey);
            signer.BlockUpdate(data, 0, data.Length);
            return(signer.VerifySignature(originalSignature));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Signs the passed in data with a private key
        /// </summary>
        /// <param name="privateKey">the private key used to create the signature</param>
        /// <param name="data">The data to sign</param>
        /// <returns>the signature as a byte array</returns>
        public byte[] Sign(byte[] privateKey, byte[] data)
        {
            var signer  = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());
            var privKey = (DsaPrivateKeyParameters)CreateAsymmetricKeyParameterFromPrivateKeyInfo(privateKey);

            signer.Init(true, privKey);
            signer.BlockUpdate(data, 0, data.Length);
            byte[] signature;
            try
            {
                signature = signer.GenerateSignature();
            }
            catch (Exception exception)
            {
                string message = "Signature Failure!\n" +
                                 $"{exception.Message}.\n" +
                                 $"The private key file is corrupted, verify private key file or try another key.\n" +
                                 $"If all fails create a new key pair.";
                throw new CryptoException(message, exception);
            }
            return(signature);
        }