public override IJoseAction Create()
        {
            JoseHeader header = new JoseHeader();
            header.SetSerialize(serializedSource);
            joseMethod = header.GetJoseMethod();

            switch (joseSerializeType)
            {
                case JoseSerializeType.COMPACT_SERIALIZATION:
                    if (JoseMethod.JWE == joseMethod && JoseActionType.DESERIALIZATION == joseActionType)
                    {
                        return new JweSerializer(serializedSource, key);
                    }
                    else if (JoseMethod.JWS == joseMethod && JoseActionType.DESERIALIZATION == joseActionType)
                    {
                        return new JwsSerializer(serializedSource, key);
                    }
                    else
                    {
                        throw new ArgumentException("unknown JoseSerializeType and JoseActionType");
                    }
                case JoseSerializeType.JSON_SERIALIZATION:
                    return null;
                default:
                    return null;
            }
        }
        public void TestAl()
        {
            int[] expected = { 0, 0, 0, 0, 0, 0, 1, 152 };

            JoseHeader header = new JoseHeader(JsonWebAlgorithm.A128KW, JsonWebAlgorithm.A128CBC_HS256);
            byte[] aad = StringUtils.StringToByte(header.GetSerialize());

            var obj = new AesEncryptionWithHmacSha(32, 16);
            byte[] actual = obj.GetAl(aad.Length);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], (int)actual[i] & 0xFF);
            }
        }
Пример #3
0
        public void TestSerializer()
        {
            var payload = "apple";
            var kid = "sample";
            var key = "1234567890123456";

            var header = new JoseHeader(JsonWebAlgorithm.A128KW, JsonWebAlgorithm.A128CBC_HS256, kid);
            var serializer = new JweSerializer(header, payload, key);
            var jweToken = serializer.CompactSerialization();

            var deserializer = new JweSerializer(jweToken, key);
            var actual = deserializer.CompactDeserialization();

            Assert.AreEqual(payload, actual);

            header = deserializer.GetHeader();
            Assert.AreEqual(kid, header.Kid);
        }
Пример #4
0
        public void TestSerializer()
        {
            var expected = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.YXBwbGU.9CofnzzDvqCYT_tuMqaBXAgX9yZSsbTMEAsz5EolTU4";
            var payload = "apple";
            var key = "1234567890123456";

            var joseHeader = new JoseHeader(JsonWebAlgorithm.HS256);
            joseHeader.SetHeader(JoseHeaderSpec.TYP, "JWT");

            var serializer = new JwsSerializer(joseHeader, payload, key);
            var jwsToken = serializer.CompactSerialization();

            Assert.AreEqual(expected, jwsToken);

            serializer = new JwsSerializer(jwsToken, key);
            var actual = serializer.CompactDeserialization();

            Assert.AreEqual(payload, actual);
        }
 public SerializationBuilder Header(JoseHeader header)
 {
     this.header = header;
     return this;
 }
 public SerializationBuilder(JoseMethod joseMethod, JoseActionType joseActionType)
 {
     base.CompactBuilder(joseMethod, joseActionType);
     header = new JoseHeader();
 }
Пример #7
0
        public string toJws(string iss, string alg, string key, string payload)
        {
            JoseHeader header = new JoseHeader();
            header.SetHeader(JoseHeaderSpec.ALG, alg);
            header.SetHeader(JoseHeaderSpec.KID, iss);

            return new Jose().Configuration(
                JoseBuilders.JsonSignatureCompactSerializationBuilder()
                    .Header(header)
                    .Payload(payload)
                    .Key(key)
                ).Serialization();
        }
        public void TestEncryption()
        {
            byte[] src = { (byte) 76, (byte) 105, (byte) 118, (byte) 101, (byte) 32, (byte) 108, (byte) 111, (byte) 110,
                (byte) 103, (byte) 32, (byte) 97, (byte) 110, (byte) 100, (byte) 32, (byte) 112, (byte) 114,
                (byte) 111, (byte) 115, (byte) 112, (byte) 101, (byte) 114, (byte) 46 };

            byte[] key = { (byte) 4, (byte) 211, (byte) 31, (byte) 197, (byte) 84, (byte) 157, (byte) 252, (byte) 254,
                (byte) 11, (byte) 100, (byte) 157, (byte) 250, (byte) 63, (byte) 170, (byte) 106, (byte) 206,
                (byte) 107, (byte) 124, (byte) 212, (byte) 45, (byte) 111, (byte) 107, (byte) 9, (byte) 219,
                (byte) 200, (byte) 177, (byte) 0, (byte) 240, (byte) 143, (byte) 156, (byte) 44, (byte) 207 };

            byte[] iv = { (byte) 3, (byte) 22, (byte) 60, (byte) 12, (byte) 43, (byte) 67, (byte) 104, (byte) 105,
                (byte) 108, (byte) 108, (byte) 105, (byte) 99, (byte) 111, (byte) 116, (byte) 104, (byte) 101 };

            JoseHeader header = new JoseHeader(JsonWebAlgorithm.A128KW, JsonWebAlgorithm.A128CBC_HS256);
            byte[] aad = StringUtils.StringToByte(header.GetSerialize());

            var obj = new AesEncryptionWithHmacSha(32, 16);
            var jweEncResult = obj.EncryptionAndSign(key, iv, src, aad);

            Assert.AreEqual("KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY", Base64.base64urlencode(jweEncResult.cipherText));
            Assert.AreEqual("U0m_YmjN04DJvceFICbCVQ", Base64.base64urlencode(jweEncResult.at));
        }
        public void TestMakeSignParts()
        {
            byte[] key = { (byte) 4, (byte) 211, (byte) 31, (byte) 197, (byte) 84, (byte) 157, (byte) 252, (byte) 254,
                (byte) 11, (byte) 100, (byte) 157, (byte) 250, (byte) 63, (byte) 170, (byte) 106, (byte) 206 };

            byte[] iv = { (byte) 3, (byte) 22, (byte) 60, (byte) 12, (byte) 43, (byte) 67, (byte) 104, (byte) 105,
                (byte) 108, (byte) 108, (byte) 105, (byte) 99, (byte) 111, (byte) 116, (byte) 104, (byte) 101 };

            byte[] src = { (byte) 40, (byte) 57, (byte) 83, (byte) 181, (byte) 119, (byte) 33, (byte) 133,
                (byte) 148, (byte) 198, (byte) 185, (byte) 243, (byte) 24, (byte) 152, (byte) 230, (byte) 6, (byte) 75,
                (byte) 129, (byte) 223, (byte) 127, (byte) 19, (byte) 210, (byte) 82, (byte) 183, (byte) 230,
                (byte) 168, (byte) 33, (byte) 215, (byte) 104, (byte) 143, (byte) 112, (byte) 56, (byte) 102 };

            byte[] expected = { (byte) 101, (byte) 121, (byte) 74, (byte) 104, (byte) 98, (byte) 71, (byte) 99, (byte) 105,
                (byte) 79, (byte) 105, (byte) 74, (byte) 66, (byte) 77, (byte) 84, (byte) 73, (byte) 52, (byte) 83,
                (byte) 49, (byte) 99, (byte) 105, (byte) 76, (byte) 67, (byte) 74, (byte) 108, (byte) 98, (byte) 109,
                (byte) 77, (byte) 105, (byte) 79, (byte) 105, (byte) 74, (byte) 66, (byte) 77, (byte) 84, (byte) 73,
                (byte) 52, (byte) 81, (byte) 48, (byte) 74, (byte) 68, (byte) 76, (byte) 85, (byte) 104, (byte) 84,
                (byte) 77, (byte) 106, (byte) 85, (byte) 50, (byte) 73, (byte) 110, (byte) 48, (byte) 3, (byte) 22,
                (byte) 60, (byte) 12, (byte) 43, (byte) 67, (byte) 104, (byte) 105, (byte) 108, (byte) 108, (byte) 105,
                (byte) 99, (byte) 111, (byte) 116, (byte) 104, (byte) 101, (byte) 40, (byte) 57, (byte) 83, (byte) 181,
                (byte) 119, (byte) 33, (byte) 133, (byte) 148, (byte) 198, (byte) 185, (byte) 243, (byte) 24,
                (byte) 152, (byte) 230, (byte) 6, (byte) 75, (byte) 129, (byte) 223, (byte) 127, (byte) 19, (byte) 210,
                (byte) 82, (byte) 183, (byte) 230, (byte) 168, (byte) 33, (byte) 215, (byte) 104, (byte) 143,
                (byte) 112, (byte) 56, (byte) 102, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                (byte) 1, (byte) 152 };

            JoseHeader header = new JoseHeader(JsonWebAlgorithm.A128KW, JsonWebAlgorithm.A128CBC_HS256);
            byte[] aad = StringUtils.StringToByte(header.GetSerialize());

            var obj = new AesEncryptionWithHmacSha(32, 16);
            var actual = obj.MakeSignParts(iv, aad, src);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], (int)actual[i] & 0xFF);
            }
        }