Esempio n. 1
0
        public static byte[] Sign(Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateKey, bool client, Version version, HandshakeInfo handshakeInfo, SignatureHashAlgorithm signatureHashAlgorithm, byte[] hash)
        {
            TlsSigner signer = null;

            switch (signatureHashAlgorithm.Signature)
            {
            case TSignatureAlgorithm.Anonymous:
                break;

            case TSignatureAlgorithm.RSA:
                signer = new TlsRsaSigner();
                break;

            case TSignatureAlgorithm.DSA:
                signer = new TlsDssSigner();
                break;

            case TSignatureAlgorithm.ECDSA:

                signer = new TlsECDsaSigner();
                break;

            default:
                break;
            }
            DTLSContext context = new DTLSContext(client, version, handshakeInfo);

            Org.BouncyCastle.Crypto.Prng.CryptoApiRandomGenerator randomGenerator = new Org.BouncyCastle.Crypto.Prng.CryptoApiRandomGenerator();
            context.SecureRandom = new Org.BouncyCastle.Security.SecureRandom(randomGenerator);

            signer.Init(context);
            if (TlsUtilities.IsTlsV12(context))
            {
                SignatureAndHashAlgorithm signatureAndHashAlgorithm = new SignatureAndHashAlgorithm((byte)signatureHashAlgorithm.Hash, (byte)signatureHashAlgorithm.Signature);
                return(signer.GenerateRawSignature(signatureAndHashAlgorithm, privateKey, hash));
            }
            else
            {
                return(signer.GenerateRawSignature(privateKey, hash));
            }
        }
Esempio n. 2
0
        public static byte[] Sign(AsymmetricKeyParameter privateKey, RSACryptoServiceProvider rsaKey, bool client, Version version, HandshakeInfo handshakeInfo,
                                  SignatureHashAlgorithm signatureHashAlgorithm, byte[] hash)
#endif
        {
            if (privateKey == null && rsaKey == null)
            {
                throw new ArgumentException("No key or Rsa CSP provided");
            }

            if (privateKey == null)
            {
                if (signatureHashAlgorithm.Signature == TSignatureAlgorithm.RSA)
                {
                    return(SignRsa(rsaKey, hash));
                }

                throw new ArgumentException("Need private key for non-RSA Algorithms");
            }

            if (version == null)
            {
                throw new ArgumentNullException(nameof(version));
            }

            if (handshakeInfo == null)
            {
                throw new ArgumentNullException(nameof(handshakeInfo));
            }

            if (signatureHashAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(signatureHashAlgorithm));
            }

            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            TlsSigner signer = null;

            switch (signatureHashAlgorithm.Signature)
            {
            case TSignatureAlgorithm.Anonymous:
                break;

            case TSignatureAlgorithm.RSA:
                signer = new TlsRsaSigner();
                break;

            case TSignatureAlgorithm.DSA:
                signer = new TlsDssSigner();
                break;

            case TSignatureAlgorithm.ECDSA:

                signer = new TlsECDsaSigner();
                break;

            default:
                break;
            }

            var context         = new DTLSContext(client, version, handshakeInfo);
            var randomGenerator = new CryptoApiRandomGenerator();

            context.SecureRandom = new SecureRandom(randomGenerator);

            signer.Init(context);
            if (TlsUtilities.IsTlsV12(context))
            {
                var signatureAndHashAlgorithm = new SignatureAndHashAlgorithm((byte)signatureHashAlgorithm.Hash, (byte)signatureHashAlgorithm.Signature);
                return(signer.GenerateRawSignature(signatureAndHashAlgorithm, privateKey, hash));
            }
            else
            {
                return(signer.GenerateRawSignature(privateKey, hash));
            }
        }