Search in sources :

Example 6 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class RsaValidationTests method rsaHashCode.

@Test
public void rsaHashCode() throws Exception {
    String keyStr = (String) keys.values().toArray()[0];
    ObjectMapper mapper = new ObjectMapper();
    JsonWebKey key = mapper.readValue(keyStr, JsonWebKey.class);
    // Compare hash codes for unequal JWK that would not map to the same hash
    Assert.assertNotEquals(key.hashCode(), new JsonWebKey().withKid(key.kid()).withN(key.n()).hashCode());
    Assert.assertNotEquals(key.hashCode(), new JsonWebKey().withKid(key.kid()).withKty(key.kty()).hashCode());
    Assert.assertNotEquals(key.hashCode(), new JsonWebKey().withKid(key.kid()).withT(key.t()).hashCode());
    // Compare hash codes for unequal JWK that would map to the same hash
    Assert.assertEquals(key.hashCode(), new JsonWebKey().withN(key.n()).withKty(key.kty()).withKid(key.kid()).hashCode());
}
Also used : JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 7 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class RsaValidationTests method validateRsaKey.

private static void validateRsaKey(KeyPair keyPair, JsonWebKey key) throws Exception {
    JsonWebKey jsonWebKey = JsonWebKey.fromRSA(keyPair);
    boolean includePrivateKey = keyPair.getPrivate() != null;
    KeyPair keyPair2 = jsonWebKey.toRSA(includePrivateKey);
    Assert.assertTrue(includePrivateKey == jsonWebKey.hasPrivateKey());
    PublicKey publicKey = keyPair2.getPublic();
    PrivateKey privateKey = keyPair2.getPrivate();
    if (includePrivateKey) {
        Assert.assertNotNull(privateKey);
        // set the missing properties to compare the keys
        jsonWebKey.withKeyOps(new ArrayList<JsonWebKeyOperation>(key.keyOps()));
        jsonWebKey.withKid(new String(key.kid()));
        Assert.assertEquals(jsonWebKey, key);
        Assert.assertEquals(key.hashCode(), jsonWebKey.hashCode());
    }
    encryptDecrypt(publicKey, privateKey);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) JsonWebKeyOperation(com.microsoft.azure.keyvault.webkey.JsonWebKeyOperation) PublicKey(java.security.PublicKey) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey)

Example 8 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class RsaKeyTest method getTestRsaKey.

private RsaKey getTestRsaKey() throws Exception {
    String jwkString = "{\"kty\":\"RSA\",\"n\":\"rZ8pnmXkhfmmgNWVVdtNcYy2q0OAcCGIpeFzsN9URqJsiBEiWQfxlUxFTbM4kVWPqjauKt6byvApBGEeMA7Qs8kxwRVP-BD4orXRe9VPgliM92rH0UxQWHmCHUe7G7uUAFPwbiDVhWuFzELxNa6Kljg6Z9DuUKoddmQvlYWj8uSunofCtDi_zzlZKGYTOYJma5IYScHNww1yjLp8-b-Be2UdHbrPkCv6Nuwi6MVIKjPpEeRQgfefRmxDBJQKY3OfydMXZmEwukYXVkUcdIP8XwG2OxnfdRK0oAo0NDebNNVuT89k_3AyZLTr1KbDmx1nnjwa8uB8k-uLtcOC9igbTw\",\"e\":\"AQAB\",\"d\":\"H-z7hy_vVJ9yeZBMtIvt8qpQUK_J51STPwV085otcgud72tPKJXoW2658664ASl9kGwbnLBwb2G3-SEunuGqiNS_PGUB3niob6sFSUMRKsPDsB9HfPoOcCZvwZiWFGRqs6C7vlR1TuJVqRjKJ_ffbf4K51oo6FZPspx7j4AShLAwLUSQ60Ld5QPuxYMYZIMpdVbMVIVHJ26pR4Y18e_0GYmEGnbF5N0HkwqQmfmTiIK5aoGnD3GGgqHeHmWBwh6_WAq90ITLcX_zBeqQUgBSj-Z5v61SroO9Eang36T9mMoYrcPpYwemtAOb4HhQYDj8dCCfbeOcVmvZ9UJKWCX2oQ\",\"dp\":\"HW87UpwPoj3lPI9B9K1hJFeuGgarpakvtHuk1HpZ5hXWFGAJiXoWRV-jvYyjoM2k7RpSxPyuuFFmYHcIxiGFp2ES4HnP0BIhKVa2DyugUxIEcMK53C43Ub4mboJPZTSC3sapKgAmA2ue624sapWmshTPpx9qnUP2Oj3cSMkgMGE\",\"dq\":\"RhwEwb5FYio0GS2tmul8FAYsNH7JDehwI1yUApnTiakhSenFetml4PYyVkKR4csgLZEi3RY6J3R8Tg-36zrZuF7hxhVJn80L5_KETSpfEI3jcrXMVg4SRaMsWLY9Ahxflt2FJgUnHOmWRLmP6_hmaTcxxSACjbyUd_HhwNavD5E\",\"qi\":\"wYPZ4lKIslA1w3FaAzQifnNLABYXXUZ_KAA3a8T8fuxkdE4OP3xIFX7WHhnmBd6uOFiEcGoeq2jNQqDg91rV5661-5muQKcvp4uUsNId5rQw9EZw-kdDcwMtVFTEBfvVuyp83X974xYAHn1Jd8wWohSwrpi1QuH5cQMR5Fm6I1A\",\"p\":\"74Ot7MgxRu4euB31UWnGtrqYPjJmvbjYESS43jfDfo-s62ggV5a39P_YPg6oosgtGHNw0QDxunUOXNu9iriaYPf_imptRk69bKN8Nrl727Y-AaBYdLf1UZuwz8X07FqHAH5ghYpk79djld8QvkUUJLpx6rzcW8BJLTOi46DtzZE\",\"q\":\"uZJu-qenARIt28oj_Jlsk-p_KLnqdczczZfbRDd7XNp6csGLa8R0EyYqUB4xLWELQZsX4tAu9SaAO62tuuEy5wbOAmOVrq2ntoia1mGQSJdoeVq6OqtN300xVnaBc3us0rm8C6-824fEQ1PWXoulXLKcSqBhFT-hQahsYi-kat8\"}";
    ObjectMapper mapper = new ObjectMapper();
    JsonWebKey jwk = null;
    jwk = mapper.readValue(jwkString, JsonWebKey.class);
    return new RsaKey("foo", jwk.toRSA(true, _provider));
}
Also used : RsaKey(com.microsoft.azure.keyvault.cryptography.RsaKey) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class AesValidationTests method aesKeyValidation.

@Test
public void aesKeyValidation() throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(TRANSFORMATION);
    keyGen.init(256);
    SecretKey skey = keyGen.generateKey();
    JsonWebKey key = serializeDeserialize(skey);
    Assert.assertTrue(key.hasPrivateKey());
    Assert.assertTrue(key.isValid());
    SecretKey secretKey = key.toAes();
    encryptDecrypt(secretKey);
    // Compare equal JSON web keys
    JsonWebKey sameKey = JsonWebKey.fromAes(skey);
    Assert.assertEquals(key, key);
    Assert.assertEquals(key, sameKey);
    Assert.assertEquals(key.hashCode(), sameKey.hashCode());
}
Also used : SecretKey(javax.crypto.SecretKey) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

Example 10 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class AesValidationTests method invalidKeyOps.

@Test
public void invalidKeyOps() throws Exception {
    JsonWebKey key = getAes();
    key.withKeyOps(Arrays.asList(JsonWebKeyOperation.ENCRYPT, new JsonWebKeyOperation("foo")));
    Assert.assertFalse(key.isValid());
}
Also used : JsonWebKeyOperation(com.microsoft.azure.keyvault.webkey.JsonWebKeyOperation) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) Test(org.junit.Test)

Aggregations

JsonWebKey (com.microsoft.azure.keyvault.webkey.JsonWebKey)19 Test (org.junit.Test)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)3 KeyBundle (com.microsoft.azure.keyvault.models.KeyBundle)3 KeyOperationResult (com.microsoft.azure.keyvault.models.KeyOperationResult)3 KeyPair (java.security.KeyPair)3 Random (java.util.Random)3 JsonWebKeyOperation (com.microsoft.azure.keyvault.webkey.JsonWebKeyOperation)2 RsaKey (com.microsoft.azure.keyvault.cryptography.RsaKey)1 Attributes (com.microsoft.azure.keyvault.models.Attributes)1 KeyAttributes (com.microsoft.azure.keyvault.models.KeyAttributes)1 KeyVerifyResult (com.microsoft.azure.keyvault.models.KeyVerifyResult)1 ImportKeyRequest (com.microsoft.azure.keyvault.requests.ImportKeyRequest)1 MessageDigest (java.security.MessageDigest)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 HashMap (java.util.HashMap)1 KeyGenerator (javax.crypto.KeyGenerator)1 SecretKey (javax.crypto.SecretKey)1