Package org.jose4j.jwk

Examples of org.jose4j.jwk.JsonWebKey$Factory


        PublicJsonWebKey ephemeralJwk = PublicJsonWebKey.Factory.newPublicJwk(ephemeralJwkJson);

        String name = "jwk";
        headers.setJwkHeaderValue(name, ephemeralJwk);

        JsonWebKey jwk = headers.getJwkHeaderValue(name);

        assertThat(ephemeralJwk.getKey(), is(equalTo(jwk.getKey())));

        String encodedHeader = headers.getEncodedHeader();

        Headers parsedHeaders = new Headers();
        parsedHeaders.setEncodedHeader(encodedHeader);

        JsonWebKey jwkFromParsed = parsedHeaders.getJwkHeaderValue(name);
        assertThat(ephemeralJwk.getKey(), is(equalTo(jwkFromParsed.getKey())));
    }
View Full Code Here


        String cs = "eyJ6aXAiOiJiYWQiLCJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.." +
                "ZZZ0nR5f80ikJtaPot4RpQ." +
                "BlDAYKzn9oLH1fhZcR60ZKye7UHslg7s0h7s1ecNZ5A1Df1pq2pBWUwdRKjJRxJAEFbDFoXTFYjV-cLCCE2Uxw." +
                "zasDvsZ3U4YkTDgIUchjiA";

        JsonWebKey jsonWebKey = JsonWebKey.Factory.newJwk("{\"kty\":\"oct\",\"k\":\"q1qm8z2sLFt_CPqwpLuGm-fX6ZKQKnukPHpoJOeykCw\"}");

        JsonWebEncryption jwe = new JsonWebEncryption();
        jwe.setKey(jsonWebKey.getKey());
        jwe.setCompactSerialization(cs);

        try
        {
            String plaintextString = jwe.getPlaintextString();
View Full Code Here

        String alg = AlgorithmIdentifiers.RSA_USING_SHA256;

        // verify consuming the JWS
        JsonWebSignature jws = new JsonWebSignature();
        jws.setCompactSerialization(jwsCompactSerialization);
        JsonWebKey jwk = JsonWebKey.Factory.newJwk(figure3RsaJwkJsonString);
        jws.setKey(jwk.getKey());
        assertThat(jws.verifySignature(), is(true));
        assertThat(jws.getPayload(), equalTo(jwsPayload));
        assertThat(jws.getKeyIdHeaderValue(), equalTo(jwk.getKeyId()));
        assertThat(alg, equalTo(jws.getAlgorithmHeaderValue()));

        // verify reproducing it (it's just luck that using the setters for the headers results in the exact same
        // JSON representation of the header)
        jws = new JsonWebSignature();
        jws.setPayload(jwsPayload);
        jws.setAlgorithmHeaderValue(alg);
        jws.setKeyIdHeaderValue(jwk.getKeyId());
        PublicJsonWebKey rsaJwk = (PublicJsonWebKey) jwk;
        jws.setKey(rsaJwk.getPrivateKey());
        String compactSerialization = jws.getCompactSerialization();
        assertThat(jwsCompactSerialization, equalTo(compactSerialization));
    }
View Full Code Here

        String alg = AlgorithmIdentifiers.ECDSA_USING_P521_CURVE_AND_SHA512;

        // verify consuming the JWS
        JsonWebSignature jws = new JsonWebSignature();
        jws.setCompactSerialization(jwsCompactSerialization);
        JsonWebKey jwk = JsonWebKey.Factory.newJwk(jwkJson);

        jws.setKey(jwk.getKey());
        assertThat(jws.getUnverifiedPayload(), equalTo(jwsPayload));

        assertThat(jws.verifySignature(), is(true));
        assertThat(jws.getPayload(), equalTo(jwsPayload));

        assertThat(jws.getKeyIdHeaderValue(), equalTo(jwk.getKeyId()));
        assertThat(alg, equalTo(jws.getAlgorithmHeaderValue()));

        // can't really verify reproducing ECDSA
    }
View Full Code Here

        String alg = AlgorithmIdentifiers.HMAC_SHA256;

        // verify consuming the JWS
        JsonWebSignature jws = new JsonWebSignature();
        jws.setCompactSerialization(jwsCompactSerialization);
        JsonWebKey jwk = JsonWebKey.Factory.newJwk(jwkJson);
        jws.setKey(jwk.getKey());
        assertThat(jws.verifySignature(), is(true));
        assertThat(jws.getPayload(), equalTo(jwsPayload));
        assertThat(jws.getKeyIdHeaderValue(), equalTo(jwk.getKeyId()));
        assertThat(alg, equalTo(jws.getAlgorithmHeaderValue()));

        // verify reproducing it
        jws = new JsonWebSignature();
        jws.setPayload(jwsPayload);
        jws.setAlgorithmHeaderValue(alg);
        jws.setKeyIdHeaderValue(jwk.getKeyId());
        jws.setKey(jwk.getKey());
        String compactSerialization = jws.getCompactSerialization();
        assertThat(jwsCompactSerialization, equalTo(compactSerialization));
    }
View Full Code Here

                "." +
                "s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0";

        JsonWebSignature jws = new JsonWebSignature();
        jws.setCompactSerialization(detachedCs);
        JsonWebKey jwk = JsonWebKey.Factory.newJwk(jwkJsonString);
        jws.setKey(jwk.getKey());
        jws.setEncodedPayload(encodedJwsPayload);
        assertThat(jws.verifySignature(), is(true));
        assertThat(jws.getPayload(), equalTo(jwsPayload));

        // verify reproducing it (it's just luck that using the setters for the headers results in the exact same
        // JSON representation of the header)
        jws = new JsonWebSignature();
        jws.setPayload(jwsPayload);
        jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
        jws.setKeyIdHeaderValue(jwk.getKeyId());
        jws.setKey(jwk.getKey());
        // To create a detached signature, sign and then concatenate the encoded header, two dots "..", and the encoded signature
        jws.sign();
        String encodedHeader = jws.getHeaders().getEncodedHeader();
        String encodedSignature = jws.getEncodedSignature();
        String reproducedDetachedCs = encodedHeader + ".." + encodedSignature;
View Full Code Here

        jceProviderTestSupport.runWithBouncyCastleProviderIfNeeded(new RunnableTest()
        {
            @Override
            public void runTest() throws Exception
            {
                JsonWebKey jwk = JsonWebKey.Factory.newJwk(
                        "   {\n" +
                        "     \"kty\": \"oct\",\n" +
                        "     \"kid\": \"77c7e2b8-6e13-45cf-8672-617b5b45243a\",\n" +
                        "     \"use\": \"enc\",\n" +
                        "     \"alg\": \"A128GCM\",\n" +
                        "     \"k\": \"XctOhJAkA-pD9Lh7ZgW_2A\"\n" +
                        "   }");

                String cs =
                        "eyJhbGciOiJkaXIiLCJraWQiOiI3N2M3ZTJiOC02ZTEzLTQ1Y2YtODY3Mi02MT" +
                        "diNWI0NTI0M2EiLCJlbmMiOiJBMTI4R0NNIn0" +
                        "." +
                        "." +
                        "refa467QzzKx6QAB" +
                        "." +
                        "JW_i_f52hww_ELQPGaYyeAB6HYGcR559l9TYnSovc23XJoBcW29rHP8yZOZG7Y" +
                        "hLpT1bjFuvZPjQS-m0IFtVcXkZXdH_lr_FrdYt9HRUYkshtrMmIUAyGmUnd9zM" +
                        "DB2n0cRDIHAzFVeJUDxkUwVAE7_YGRPdcqMyiBoCO-FBdE-Nceb4h3-FtBP-c_" +
                        "BIwCPTjb9o0SbdcdREEMJMyZBH8ySWMVi1gPD9yxi-aQpGbSv_F9N4IZAxscj5" +
                        "g-NJsUPbjk29-s7LJAGb15wEBtXphVCgyy53CoIKLHHeJHXex45Uz9aKZSRSIn" +
                        "ZI-wjsY0yu3cT4_aQ3i1o-tiE-F8Ios61EKgyIQ4CWao8PFMj8TTnp" +
                        "." +
                        "vbb32Xvllea2OtmHAdccRQ";

                // decrypt the example
                JsonWebEncryption jwe = new JsonWebEncryption();
                jwe.setKey(jwk.getKey());
                jwe.setCompactSerialization(cs);
                assertThat(jwePlaintext, equalTo(jwe.getPlaintextString()));


                // verify that we can reproduce it
                jwe = new JsonWebEncryption();
                jwe.setPlaintext(jwePlaintext);
                jwe.setKey(jwk.getKey());
                jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.DIRECT);
                jwe.setKeyIdHeaderValue(jwk.getKeyId());
                jwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_GCM);

            }
        });
    }
View Full Code Here

        jceProviderTestSupport.runWithBouncyCastleProviderIfNeeded(new RunnableTest()
        {
            @Override
            public void runTest() throws Exception
            {
                JsonWebKey jwk = JsonWebKey.Factory.newJwk(
                        "   {\n" +
                        "     \"kty\": \"oct\",\n" +
                        "     \"kid\": \"18ec08e1-bfa9-4d95-b205-2b4dd1d4321d\",\n" +
                        "     \"use\": \"enc\",\n" +
                        "     \"alg\": \"A256GCMKW\",\n" +
                        "     \"k\": \"qC57l_uxcm7Nm3K-ct4GFjx8tM1U8CZ0NLBvdQstiS8\"\n" +
                        "   }");

                String cs =
                        "eyJhbGciOiJBMjU2R0NNS1ciLCJraWQiOiIxOGVjMDhlMS1iZmE5LTRkOTUtYj" +
                        "IwNS0yYjRkZDFkNDMyMWQiLCJ0YWciOiJrZlBkdVZRM1QzSDZ2bmV3dC0ta3N3" +
                        "IiwiaXYiOiJLa1lUMEdYXzJqSGxmcU5fIiwiZW5jIjoiQTEyOENCQy1IUzI1Ni" +
                        "J9" +
                        "." +
                        "lJf3HbOApxMEBkCMOoTnnABxs_CvTWUmZQ2ElLvYNok" +
                        "." +
                        "gz6NjyEFNm_vm8Gj6FwoFQ" +
                        "." +
                        "Jf5p9-ZhJlJy_IQ_byKFmI0Ro7w7G1QiaZpI8OaiVgD8EqoDZHyFKFBupS8iaE" +
                        "eVIgMqWmsuJKuoVgzR3YfzoMd3GxEm3VxNhzWyWtZKX0gxKdy6HgLvqoGNbZCz" +
                        "LjqcpDiF8q2_62EVAbr2uSc2oaxFmFuIQHLcqAHxy51449xkjZ7ewzZaGV3eFq" +
                        "hpco8o4DijXaG5_7kp3h2cajRfDgymuxUbWgLqaeNQaJtvJmSMFuEOSAzw9Hde" +
                        "b6yhdTynCRmu-kqtO5Dec4lT2OMZKpnxc_F1_4yDJFcqb5CiDSmA-psB2k0Jtj" +
                        "xAj4UPI61oONK7zzFIu4gBfjJCndsZfdvG7h8wGjV98QhrKEnR7xKZ3KCr0_qR" +
                        "1B-gxpNk3xWU" +
                        "." +
                        "DKW7jrb4WaRSNfbXVPlT5g";


                // verify recreating it (or what we can)
                JsonWebEncryption jwe = new JsonWebEncryption();
                jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A256GCMKW);
                jwe.setKeyIdHeaderValue(jwk.getKeyId());
                jwe.setHeader(HeaderParameterNames.INITIALIZATION_VECTOR, "KkYT0GX_2jHlfqN_");
                jwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);

                jwe.setKey(jwk.getKey());
                jwe.setEncodedIv("gz6NjyEFNm_vm8Gj6FwoFQ");
                jwe.setEncodedContentEncryptionKey("UWxARpat23nL9ReIj4WG3D1ee9I4r-Mv5QLuFXdy_rE");
                jwe.setPayload(jwePlaintext);


                String compactSerialization = jwe.getCompactSerialization();
                String[] exampleParts = CompactSerializer.deserialize(cs);
                String[] producedParts = CompactSerializer.deserialize(compactSerialization);

                // [0] header is different because of the order of params
                assertThat(exampleParts[1], equalTo(producedParts[1]));
                assertThat(exampleParts[2], equalTo(producedParts[2]));
                assertThat(exampleParts[3], equalTo(producedParts[3]));
                // [4] tag is different because of the header being different


                // verify decrypting it
                jwe = new JsonWebEncryption();
                jwe.setKey(jwk.getKey());
                jwe.setCompactSerialization(cs);
                assertThat(jwePlaintext, equalTo(jwe.getPlaintextString()));
            }
        });
    }
View Full Code Here

        jceProviderTestSupport.runWithBouncyCastleProviderIfNeeded(new RunnableTest()
        {
            @Override
            public void runTest() throws Exception
            {
                JsonWebKey jwk = JsonWebKey.Factory.newJwk(
                        "   {\n" +
                        "     \"kty\": \"oct\",\n" +
                        "     \"kid\": \"81b20965-8332-43d9-a468-82160ad91ac8\",\n" +
                        "     \"use\": \"enc\",\n" +
                        "     \"alg\": \"A128KW\",\n" +
                        "     \"k\": \"GZy6sIZ6wl9NJOKB-jnmVQ\"\n" +
                        "   }\n");

                JsonWebEncryption jwe = new JsonWebEncryption();
                jwe.setKey(jwk.getKey());
                jwe.setCompactSerialization(cs);
                assertThat(jwePlaintext, equalTo(jwe.getPlaintextString()));
            }
        });
    }
View Full Code Here

    @Test (expected = InvalidAlgorithmException.class)
    public void testBlackListEncAlg() throws JoseException
    {
        String jwecs = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..LpJAcwq3RzCs-zPRQzT-jg.IO0ZwAhWnSF05dslZwaBKcHYOAKlSpt_l7Dl5ABrUS0.0KfkxQTFqTQjzfJIm8MNjg";
        JsonWebKey jsonWebKey = JsonWebKey.Factory.newJwk("{\"kty\":\"oct\",\"k\":\"I95jRMEyRvD0t3LRgL1GSWTgkX5jznuhX4mce9bYV_A\"}");

        JsonWebEncryption jwe = new JsonWebEncryption();
        jwe.setContentEncryptionAlgorithmConstraints(new AlgorithmConstraints(BLACKLIST, AES_128_CBC_HMAC_SHA_256));
        jwe.setCompactSerialization(jwecs);
        jwe.setKey(jsonWebKey.getKey());
        jwe.getPayload();
    }
View Full Code Here

TOP

Related Classes of org.jose4j.jwk.JsonWebKey$Factory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.