示例#1
0
        public void ReadPublicKey_CorrectlyReads()
        {
            var sha256WithRSA = new ASN1ObjectIdentifier("1.2.840.113549.1.1.11");
            var keyAlgorithm  = new X509AlgorithmIdentifier(sha256WithRSA, new ASN1Object[] { new ASN1Null() });

            BitArray keyData;

            using (var ms = new MemoryStream())
            {
                var writer = new DERWriter(ms);

                writer.Write(new ASN1Sequence(new ASN1Object[]
                {
                    new ASN1Integer(123),
                    new ASN1Integer(456)
                }));

                keyData = new BitArray(ms.ToArray());
            }

            var key = new RSAKeyReader().ReadPublicKey(keyAlgorithm, keyData);

            var rsaKey = Assert.IsType <RSAPublicKey>(key);

            Assert.Equal(123, rsaKey.Modulus);
            Assert.Equal(456, rsaKey.Exponent);
        }
示例#2
0
        public override bool IsCompatible(CipherSuite cipherSuite, X509Certificate certificate)
        {
            // TODO check cipherSuite == RSA/DSS
            // cert signed with RSA
            if (!RSAKeyReader.IsRSAIdentifier(certificate.SignatureAlgorithm.Algorithm))
            {
                return(false);
            }

            // TODO ?
            return(true);
        }
示例#3
0
        public bool IsCompatible(CipherSuite cipherSuite, X509Certificate certificate)
        {
            // cert signed with RSA
            if (!RSAKeyReader.IsRSAIdentifier(certificate.SignatureAlgorithm.Algorithm))
            {
                return(false);
            }

            // cert has RSA public key
            if (!(certificate.SubjectPublicKey is RSAPublicKey))
            {
                return(false);
            }

            // TODO ?
            return(true);
        }
示例#4
0
        public virtual bool IsCompatible(CipherSuite cipherSuite, X509Certificate certificate)
        {
            var signatureAlgorithm = CipherSuitesRegistry.MapSignatureAlgorithm(cipherSuite);
            var requiresECKey      = Equals(CipherSuitesRegistry.MapKeyExchange(cipherSuite), ECIdentifiers.ECDH);

            if (signatureAlgorithm.Equals(ECIdentifiers.ECDSA))
            {
                if (certificate.SignatureAlgorithm.Algorithm != ECIdentifiers.ECDSAWithSHA256)
                {
                    return(false);
                }

                if (!(certificate.SubjectPublicKey is ECPublicKey))
                {
                    return(false);
                }

                return(true);
            }

            if (signatureAlgorithm.Equals(RSAIdentifiers.RSASig))
            {
                if (!RSAKeyReader.IsRSAIdentifier(certificate.SignatureAlgorithm.Algorithm))
                {
                    return(false);
                }

                if (requiresECKey && !(certificate.SubjectPublicKey is ECPublicKey))
                {
                    return(false);
                }

                if (!requiresECKey && !(certificate.SubjectPublicKey is RSAPublicKey))
                {
                    return(false);
                }

                return(true);
            }

            return(false);
        }
示例#5
0
        public void ReadPrivateKey_CorrectlyReads()
        {
            var keyData = Convert.FromBase64String(@"MGMCAQACEQC22aTrdWZfC+U35KxlhaNrAgMBAAECEAjmcyaa4k7B+mPPmFvm3QECCQDhYc7boBK6wQIJAM+wsIZ/2oUrAgkAxCFASQFAq0ECCQCDdjjWedlMzwIIT7KUwpmsGjE=");

            var modulus         = BigInteger.Parse("243049568621283441616897908737715839851");
            var privateExponent = BigInteger.Parse("11830387779451213492505862747361565953");
            var publicExponent  = BigInteger.Parse("65537");

            var sha256WithRSA = new ASN1ObjectIdentifier("1.2.840.113549.1.1.11");
            var keyAlgorithm  = new X509AlgorithmIdentifier(sha256WithRSA, new ASN1Object[] { new ASN1Null() });

            var key = new RSAKeyReader().ReadPrivateKey(keyAlgorithm, keyData);

            var rsaKey = Assert.IsType <RSAPrivateKey>(key);

            Assert.Equal(modulus, rsaKey.Modulus);
            Assert.Equal(privateExponent, rsaKey.Exponent);

            var rsaPublicKey = Assert.IsType <RSAPublicKey>(key.PublicKey);

            Assert.Equal(modulus, rsaPublicKey.Modulus);
            Assert.Equal(publicExponent, rsaPublicKey.Exponent);
        }