示例#1
0
        public static void GetECDiffieHellmanPublicKey_ReturnsECDHKey()
        {
            PublicKey key = GetTestECDHKey();

            using (ECDiffieHellman ecdh = key.GetECDiffieHellmanPublicKey())
            {
                Assert.NotNull(ecdh);
                Assert.Equal(ecdh.ExportSubjectPublicKeyInfo(), key.ExportSubjectPublicKeyInfo());
            }
        }
示例#2
0
        public static void CreateFromSubjectPublicKeyInfo_AnyAlgorithm()
        {
            byte[]    spki = TestData.GostR3410SubjectPublicKeyInfo;
            PublicKey key  = PublicKey.CreateFromSubjectPublicKeyInfo(spki, out int read);

            Assert.Throws <NotSupportedException>(() => key.Key);
            Assert.Equal("1.2.643.2.2.19", key.Oid.Value);
            Assert.Equal(spki, key.ExportSubjectPublicKeyInfo());
            Assert.Equal(spki.Length, read);
        }
示例#3
0
        public static void GetECDsaPublicKey_ReturnsECDsaKey()
        {
            PublicKey key = GetTestECDsaKey();

            using (ECDsa ecdsa = key.GetECDsaPublicKey())
            {
                Assert.NotNull(ecdsa);
                Assert.Equal(ecdsa.ExportSubjectPublicKeyInfo(), key.ExportSubjectPublicKeyInfo());
            }
        }
示例#4
0
        public static void GetDSAPublicKey_ReturnsDsaKey()
        {
            PublicKey key = GetTestDsaKey();

            using (DSA dsa = key.GetDSAPublicKey())
            {
                Assert.NotNull(dsa);
                Assert.Equal(dsa.ExportSubjectPublicKeyInfo(), key.ExportSubjectPublicKeyInfo());
            }
        }
示例#5
0
        public static void GetRSAPublicKey_ReturnsRsaKey()
        {
            PublicKey key = GetTestRsaKey();

            using (RSA rsa = key.GetRSAPublicKey())
            {
                Assert.NotNull(rsa);
                Assert.Equal(rsa.ExportSubjectPublicKeyInfo(), key.ExportSubjectPublicKeyInfo());
            }
        }
示例#6
0
        public static void CreateFromSubjectPublicKeyInfo_Roundtrip_ECDH()
        {
            using ECDiffieHellman ecdh = ECDiffieHellman.Create();
            ecdh.ImportFromPem(TestData.EcDhPkcs8PublicKey);
            byte[] spki = ecdh.ExportSubjectPublicKeyInfo();

            PublicKey key = PublicKey.CreateFromSubjectPublicKeyInfo(spki, out int read);

            Assert.Throws <NotSupportedException>(() => key.Key);
            Assert.Equal("1.2.840.10045.2.1", key.Oid.Value);
            Assert.Equal(spki, key.ExportSubjectPublicKeyInfo());
            Assert.Equal(spki.Length, read);
        }
示例#7
0
        public static void CreateFromSubjectPublicKeyInfo_Roundtrip_DSA()
        {
            using DSA dsa = DSA.Create();
            dsa.ImportFromPem(TestData.DsaPkcs8PublicKey);
            byte[] spki = dsa.ExportSubjectPublicKeyInfo();

            PublicKey key = PublicKey.CreateFromSubjectPublicKeyInfo(spki, out int read);

            Assert.IsAssignableFrom <DSA>(key.Key);
            Assert.Equal("1.2.840.10040.4.1", key.Oid.Value);
            Assert.Equal(spki, key.ExportSubjectPublicKeyInfo());
            Assert.Equal(spki.Length, read);
        }
示例#8
0
        public static void CreateFromSubjectPublicKeyInfo_Roundtrip_RSA()
        {
            using RSA rsa = RSA.Create();
            rsa.ImportFromPem(TestData.RsaPkcs8PublicKey);
            byte[] spki = rsa.ExportSubjectPublicKeyInfo();

            PublicKey key = PublicKey.CreateFromSubjectPublicKeyInfo(spki, out int read);

            Assert.IsAssignableFrom <RSA>(key.Key);
            Assert.Equal("1.2.840.113549.1.1.1", key.Oid.Value);
            Assert.Equal(spki, key.ExportSubjectPublicKeyInfo());
            Assert.Equal(spki.Length, read);
        }
示例#9
0
        public static void CreateFromSubjectPublicKeyInfo_Roundtrip_DSA_InvalidKey()
        {
            // The DSA key is invalid here, but we should be able to round-trip the
            // parameters as-is.
            byte[] spki = Convert.FromHexString(
                "301B301306072A8648CE3804013008020100020300FFFF030400020103");

            PublicKey key = PublicKey.CreateFromSubjectPublicKeyInfo(spki, out int read);

            Assert.ThrowsAny <CryptographicException>(() => key.Key);
            Assert.Equal("1.2.840.10040.4.1", key.Oid.Value);
            Assert.Equal(spki, key.ExportSubjectPublicKeyInfo());
            Assert.Equal(spki.Length, read);
        }
示例#10
0
        public static void ExportSubjectPublicKeyInfo_ECDH()
        {
            using ECDiffieHellman ecdh = ECDiffieHellman.Create();
            ecdh.ImportFromPem(TestData.EcDhPkcs8Key);
            PublicKey key = new PublicKey(ecdh);

            Span <byte> algSpki = ecdh.ExportSubjectPublicKeyInfo();

            Assert.True(algSpki.SequenceEqual(key.ExportSubjectPublicKeyInfo()), "SequenceEquals(ExportSubjectPublicKeyInfo)");

            // Just right
            Assert.True(key.TryExportSubjectPublicKeyInfo(algSpki, out int written), nameof(key.TryExportSubjectPublicKeyInfo));
            Assert.Equal(algSpki.Length, written);

            // Too small
            Assert.False(key.TryExportSubjectPublicKeyInfo(algSpki.Slice(1), out written), nameof(key.TryExportSubjectPublicKeyInfo));
            Assert.Equal(0, written);
        }
示例#11
0
        public static void ExportSubjectPublicKeyInfo_DSA()
        {
            using DSA dsa = DSA.Create();
            dsa.ImportFromPem(TestData.DsaPkcs8PublicKey);
            PublicKey key = new PublicKey(dsa);

            Span <byte> algSpki = dsa.ExportSubjectPublicKeyInfo();

            Assert.True(algSpki.SequenceEqual(key.ExportSubjectPublicKeyInfo()), "SequenceEquals(ExportSubjectPublicKeyInfo)");

            // Just right
            Assert.True(key.TryExportSubjectPublicKeyInfo(algSpki, out int written), nameof(key.TryExportSubjectPublicKeyInfo));
            Assert.Equal(algSpki.Length, written);

            // Too small
            Assert.False(key.TryExportSubjectPublicKeyInfo(algSpki.Slice(1), out written), nameof(key.TryExportSubjectPublicKeyInfo));
            Assert.Equal(0, written);
        }