示例#1
0
        public void Equal()
        {
            var key = RsaJwk.GeneratePrivateKey(4096);

            Assert.True(key.Equals(key));
            Assert.Equal(key, key);
            var publicKey = key.AsPublicKey();

            Assert.NotEqual(key, publicKey);
            var copiedKey = Jwk.FromJson(key.ToString());

            Assert.Equal(key, copiedKey);

            // 'kid' is not a discriminant, excepted if the value is different.
            copiedKey.Kid = default;
            Assert.Equal(key, copiedKey);
            Assert.Equal(copiedKey, key);
            key.Kid = default;
            Assert.Equal(key, copiedKey);
            key.Kid       = JsonEncodedText.Encode("X");
            copiedKey.Kid = JsonEncodedText.Encode("Y");
            Assert.NotEqual(key, copiedKey);

            Assert.NotEqual(key, Jwk.None);
        }
示例#2
0
        public void CreateFromJson(string json, string kid, string alg)
        {
            var jwk = Jwk.FromJson(json);

            Assert.Equal(jwk.Kid, kid);
            if (!(jwk.Alg is null))
            {
                Assert.Equal(Encoding.UTF8.GetString(jwk.Alg), alg);
            }
        }
示例#3
0
        private static void ReadKeyFromJson()
        {
            // The Jwk.FromJson method accept a JSON-encoded string as input
            string json        = File.ReadAllText(@".\public_ec_key.json");
            var    keyFromJson = Jwk.FromJson(json);

            keyFromJson.Kid = "JSON";
            Console.WriteLine("JWK read from a JSON string:");
            Console.WriteLine(keyFromJson);
            Console.WriteLine();
        }
示例#4
0
        public void FromJson(string json)
        {
            var key = Jwk.FromJson(json);

            Assert.NotNull(key);
            var jwk = Assert.IsType <SymmetricJwk>(key);

            Assert.Equal(KeyManagementAlgorithm.A128KW, jwk.KeyManagementAlgorithm);
            Assert.Equal("kid1", jwk.Kid.ToString());
            Assert.True(jwk.K.SequenceEqual(Base64Url.Decode("GawgguFyGrWKav7AX4VKUg")));
            Assert.True(JwkUseValues.Sig.Equals(jwk.Use));
        }
示例#5
0
        public override void FromJson(string json)
        {
            var key = Jwk.FromJson(json);

            Assert.NotNull(key);
            var jwk = Assert.IsType <SymmetricJwk>(key);

            Assert.Equal(KeyManagementAlgorithm.Aes128KW.Utf8Name, jwk.Alg);
            Assert.Equal("kid1", jwk.Kid);
            Assert.True(jwk.K.SequenceEqual(Base64Url.Decode("GawgguFyGrWKav7AX4VKUg")));
            Assert.True(JwkUseNames.Sig.SequenceEqual(jwk.Use));
        }
示例#6
0
        public void FromJson(string crvName, string json)
        {
            // https://tools.ietf.org/html/rfc7517#appendix-A.1
            var key = Jwk.FromJson(json);

            Assert.NotNull(key);
            var jwk = Assert.IsType <ECJwk>(key);

            Assert.Equal("1", jwk.Kid.ToString());
            Assert.True(JwkUseValues.Enc.Equals(jwk.Use));

            Assert.Equal(crvName, jwk.Crv.Name.ToString());
            Assert.Equal(jwk.X.ToArray(), Base64Url.Decode("MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4"));
            Assert.Equal(jwk.Y.ToArray(), Base64Url.Decode("4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM"));
        }
示例#7
0
        public override void FromJson(string json)
        {
            // https://tools.ietf.org/html/rfc7517#appendix-A.1
            var key = Jwk.FromJson(json);

            Assert.NotNull(key);
            var jwk = Assert.IsType <ECJwk>(key);

            Assert.Equal("1", jwk.Kid);
            Assert.True(JwkUseNames.Enc.SequenceEqual(jwk.Use));

            Assert.Equal(Encoding.UTF8.GetBytes("P-256"), jwk.Crv.Name);
            Assert.Equal(jwk.X, Base64Url.Decode("MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4"));
            Assert.Equal(jwk.Y, Base64Url.Decode("4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM"));
        }
示例#8
0
        public override void FromJson_WithProperties(string json)
        {
            var key = Jwk.FromJson(json);

            Assert.NotNull(key);
            var jwk = Assert.IsType <ECJwk>(key);

            Assert.NotNull(jwk.X509CertificateChain);
            Assert.NotEmpty(jwk.X509CertificateChain);
            Assert.NotEmpty(jwk.X5c);

            Assert.Equal(Base64Url.Decode("dGhpcyBpcyBhIFNIQTEgdGVzdCE"), jwk.X5t);
            Assert.Equal(Base64Url.Decode("dGhpcyBpcyBhIFNIQTI1NiB0ZXN0ISAgICAgICAgICAgIA"), jwk.X5tS256);
            Assert.Equal(JwkKeyOpsValues.Sign, jwk.KeyOps[0]);
            Assert.Equal("https://example.com", jwk.X5u);
        }
示例#9
0
        public override void FromJson_WithProperties(string json)
        {
            // https://tools.ietf.org/html/rfc7517#appendix-B
            var key = Jwk.FromJson(json);

            Assert.NotNull(key);
            var jwk = Assert.IsType <SymmetricJwk>(key);

            Assert.NotNull(jwk.X509CertificateChain);
            Assert.NotEmpty(jwk.X509CertificateChain);
            Assert.NotEmpty(jwk.X5c);

            Assert.Equal(Base64Url.Decode("dGhpcyBpcyBhIFNIQTEgdGVzdCE"), jwk.X5t);
            Assert.Equal(Base64Url.Decode("dGhpcyBpcyBhIFNIQTI1NiB0ZXN0ISAgICAgICAgICAgIA"), jwk.X5tS256);
            Assert.Equal("sign", jwk.KeyOps[0]);
            Assert.Equal("https://example.com", jwk.X5u);
        }
示例#10
0
        public void FromJson(string json)
        {
            // https://tools.ietf.org/html/rfc7517#appendix-C.1
            var key = Jwk.FromJson(json);

            Assert.NotNull(key);
            var jwk = Assert.IsType <RsaJwk>(key);

            Assert.Equal("*****@*****.**", jwk.Kid.ToString());
            Assert.True(JwkUseValues.Enc.Equals(jwk.Use));

            Assert.Equal(jwk.N.ToArray(), Base64Url.Decode("t6Q8PWSi1dkJj9hTP8hNYFlvadM7DflW9mWepOJhJ66w7nyoK1gPNqFMSQRyO125Gp-TEkodhWr0iujjHVx7BcV0llS4w5ACGgPrcAd6ZcSR0-Iqom-QFcNP8Sjg086MwoqQU_LYywlAGZ21WSdS_PERyGFiNnj3QQlO8Yns5jCtLCRwLHL0Pb1fEv45AuRIuUfVcPySBWYnDyGxvjYGDSM-AqWS9zIQ2ZilgT-GqUmipg0XOC0Cc20rgLe2ymLHjpHciCKVAbY5-L32-lSeZO-Os6U15_aXrk9Gw8cPUaX1_I8sLGuSiVdt3C_Fn2PZ3Z8i744FPFGGcG1qs2Wz-Q"));
            Assert.Equal(jwk.E.ToArray(), Base64Url.Decode("AQAB"));
            Assert.Equal(jwk.D.ToArray(), Base64Url.Decode("GRtbIQmhOZtyszfgKdg4u_N-R_mZGU_9k7JQ_jn1DnfTuMdSNprTeaSTyWfSNkuaAwnOEbIQVy1IQbWVV25NY3ybc_IhUJtfri7bAXYEReWaCl3hdlPKXy9UvqPYGR0kIXTQRqns-dVJ7jahlI7LyckrpTmrM8dWBo4_PMaenNnPiQgO0xnuToxutRZJfJvG4Ox4ka3GORQd9CsCZ2vsUDmsXOfUENOyMqADC6p1M3h33tsurY15k9qMSpG9OX_IJAXmxzAh_tWiZOwk2K4yxH9tS3Lq1yX8C1EWmeRDkK2ahecG85-oLKQt5VEpWHKmjOi_gJSdSgqcN96X52esAQ"));
            Assert.Equal(jwk.P.ToArray(), Base64Url.Decode("2rnSOV4hKSN8sS4CgcQHFbs08XboFDqKum3sc4h3GRxrTmQdl1ZK9uw-PIHfQP0FkxXVrx-WE-ZEbrqivH_2iCLUS7wAl6XvARt1KkIaUxPPSYB9yk31s0Q8UK96E3_OrADAYtAJs-M3JxCLfNgqh56HDnETTQhH3rCT5T3yJws"));
            Assert.Equal(jwk.Q.ToArray(), Base64Url.Decode("1u_RiFDP7LBYh3N4GXLT9OpSKYP0uQZyiaZwBtOCBNJgQxaj10RWjsZu0c6Iedis4S7B_coSKB0Kj9PaPaBzg-IySRvvcQuPamQu66riMhjVtG6TlV8CLCYKrYl52ziqK0E_ym2QnkwsUX7eYTB7LbAHRK9GqocDE5B0f808I4s"));
            Assert.Equal(jwk.DP.ToArray(), Base64Url.Decode("KkMTWqBUefVwZ2_Dbj1pPQqyHSHjj90L5x_MOzqYAJMcLMZtbUtwKqvVDq3tbEo3ZIcohbDtt6SbfmWzggabpQxNxuBpoOOf_a_HgMXK_lhqigI4y_kqS1wY52IwjUn5rgRrJ-yYo1h41KR-vz2pYhEAeYrhttWtxVqLCRViD6c"));
            Assert.Equal(jwk.DQ.ToArray(), Base64Url.Decode("AvfS0-gRxvn0bwJoMSnFxYcK1WnuEjQFluMGfwGitQBWtfZ1Er7t1xDkbN9GQTB9yqpDoYaN06H7CFtrkxhJIBQaj6nkF5KKS3TQtQ5qCzkOkmxIe3KRbBymXxkb5qwUpX5ELD5xFc6FeiafWYY63TmmEAu_lRFCOJ3xDea-ots"));
            Assert.Equal(jwk.QI.ToArray(), Base64Url.Decode("lSQi-w9CpyUReMErP1RsBLk7wNtOvs5EQpPqmuMvqW57NBUczScEoPwmUqqabu9V0-Py4dQ57_bapoKRu1R90bvuFnU63SHWEFglZQvJDMeAvmj4sm-Fp0oYu_neotgQ0hzbI5gry7ajdYy9-2lNx_76aBZoOUu9HCJ-UsfSOI8"));
        }
示例#11
0
        public Jwk CanonicalizeKey(Jwk key)
        {
            key.Kid = JsonEncodedText.Encode("kid");
            key.Use = JwkUseValues.Sig;
            key.X5c.Add(new byte[0]);
            key.X5t     = Base64Url.Decode("XOf1YEg_zFLX0PtGjiEVvjM1WsA");
            key.X5tS256 = Base64Url.Decode("ZgPMqAT8BELhXwBa2nIT0OvdWtQCiF_g09nAyHhgCe0");
            key.X5u     = "https://example.com/jwks";
            var json             = key.Canonicalize();
            var canonicalizedKey = Jwk.FromJson(Encoding.UTF8.GetString(json));

            Assert.NotNull(canonicalizedKey);

            Assert.True(canonicalizedKey.Alg.EncodedUtf8Bytes.IsEmpty);
            Assert.True(canonicalizedKey.Kid.EncodedUtf8Bytes.IsEmpty);
            Assert.True(canonicalizedKey.Use.EncodedUtf8Bytes.IsEmpty);
            Assert.Empty(canonicalizedKey.X5c);
            Assert.Null(canonicalizedKey.X5t);
            Assert.Null(canonicalizedKey.X5tS256);
            Assert.Null(canonicalizedKey.X5u);

            return(canonicalizedKey);
        }
示例#12
0
        public Jwk CanonicalizeKey(Jwk key)
        {
            key.Kid = JsonEncodedText.Encode("kid");
            key.Use = JwkUseValues.Sig;
            key.X5c.Add(new byte[0]);
            key.X5t     = Encoding.UTF8.GetBytes("x5t");
            key.X5tS256 = Encoding.UTF8.GetBytes("x5t#256");
            key.X5u     = "https://example.com/jwks";
            var json             = key.Canonicalize();
            var canonicalizedKey = Jwk.FromJson(Encoding.UTF8.GetString(json));

            Assert.NotNull(canonicalizedKey);

            Assert.True(canonicalizedKey.Alg.EncodedUtf8Bytes.IsEmpty);
            Assert.True(canonicalizedKey.Kid.EncodedUtf8Bytes.IsEmpty);
            Assert.True(canonicalizedKey.Use.EncodedUtf8Bytes.IsEmpty);
            Assert.Empty(canonicalizedKey.X5c);
            Assert.Null(canonicalizedKey.X5t);
            Assert.Null(canonicalizedKey.X5tS256);
            Assert.Null(canonicalizedKey.X5u);

            return(canonicalizedKey);
        }
示例#13
0
        public Jwk CanonicalizeKey(Jwk key)
        {
            key.Alg = SignatureAlgorithm.HmacSha256.Utf8Name;
            key.Kid = "kid";
            key.Use = JwkUseNames.Sig.ToArray();
            key.X5c.Add(new byte[0]);
            key.X5t     = Encoding.UTF8.GetBytes("x5t");
            key.X5tS256 = Encoding.UTF8.GetBytes("x5t#256");
            key.X5u     = "https://example.com/jwks";
            var json             = key.Canonicalize();
            var canonicalizedKey = Jwk.FromJson(Encoding.UTF8.GetString(json));

            Assert.NotNull(canonicalizedKey);

            Assert.Null(canonicalizedKey.Alg);
            Assert.Null(canonicalizedKey.Kid);
            Assert.Null(canonicalizedKey.Use);
            Assert.Empty(canonicalizedKey.X5c);
            Assert.Null(canonicalizedKey.X5t);
            Assert.Null(canonicalizedKey.X5tS256);
            Assert.Null(canonicalizedKey.X5u);

            return(canonicalizedKey);
        }