Search in sources :

Example 11 with JsonWebKey

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

the class AesValidationTests method octHashCode.

@Test
public void octHashCode() throws Exception {
    JsonWebKey key = getAes();
    // Compare hash codes for unequal JWK that would not map to the same hash
    Assert.assertNotEquals(key.hashCode(), new JsonWebKey().withK(key.k()).hashCode());
    Assert.assertNotEquals(key.hashCode(), new JsonWebKey().withKty(key.kty()).hashCode());
    // Compare hash codes for unequal JWK that would map to the same hash
    Assert.assertEquals(key.hashCode(), new JsonWebKey().withK(key.k()).withKty(key.kty()).hashCode());
}
Also used : JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) Test(org.junit.Test)

Example 12 with JsonWebKey

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

the class RsaValidationTests method rsaPublicKeyValidation.

@Test
public void rsaPublicKeyValidation() throws Exception {
    for (String keyStr : keys.values()) {
        ObjectMapper mapper = new ObjectMapper();
        JsonWebKey key = mapper.readValue(keyStr, JsonWebKey.class);
        Assert.assertTrue(key.hasPrivateKey());
        Assert.assertTrue(key.isValid());
        KeyPair keyPair = key.toRSA();
        validateRsaKey(keyPair, key);
        Assert.assertNull(keyPair.getPrivate());
        // Compare equal JSON web keys
        JsonWebKey sameKey = mapper.readValue(keyStr, JsonWebKey.class);
        Assert.assertEquals(key, key);
        Assert.assertEquals(key, sameKey);
        Assert.assertEquals(key.hashCode(), sameKey.hashCode());
    }
}
Also used : KeyPair(java.security.KeyPair) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 13 with JsonWebKey

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

the class RsaValidationTests method rsaPrivateKeyValidation.

@Test
public void rsaPrivateKeyValidation() throws Exception {
    for (String keyStr : keys.values()) {
        ObjectMapper mapper = new ObjectMapper();
        JsonWebKey key = mapper.readValue(keyStr, JsonWebKey.class);
        KeyPair keyPairWithPrivate = key.toRSA(true);
        validateRsaKey(keyPairWithPrivate, key);
        encryptDecrypt(keyPairWithPrivate.getPublic(), keyPairWithPrivate.getPrivate());
    }
}
Also used : KeyPair(java.security.KeyPair) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with JsonWebKey

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

the class KeyOperationsTest method checkImportOperation.

private void checkImportOperation(KeyBundle keyBundle, boolean importToHardware) throws Exception {
    Attributes attribute = new KeyAttributes().withEnabled(true).withExpires(new DateTime().withYear(2050).withMonthOfYear(1)).withNotBefore(new DateTime().withYear(2000).withMonthOfYear(1));
    Map<String, String> tags = new HashMap<String, String>();
    tags.put("foo", "baz");
    JsonWebKey importedJwk = keyBundle.key();
    KeyBundle importResultBundle = keyVaultClient.importKey(new ImportKeyRequest.Builder(getVaultUri(), KEY_NAME, keyBundle.key()).withHsm(importToHardware).withAttributes(attribute).withTags(tags).build());
    validateRsaKeyBundle(importResultBundle, getVaultUri(), KEY_NAME, importToHardware ? JsonWebKeyType.RSA_HSM : JsonWebKeyType.RSA, importedJwk.keyOps(), attribute);
    checkEncryptDecryptSequence(importedJwk, importResultBundle);
    Assert.assertTrue(importResultBundle.key().isValid());
}
Also used : HashMap(java.util.HashMap) KeyAttributes(com.microsoft.azure.keyvault.models.KeyAttributes) ImportKeyRequest(com.microsoft.azure.keyvault.requests.ImportKeyRequest) Attributes(com.microsoft.azure.keyvault.models.Attributes) KeyAttributes(com.microsoft.azure.keyvault.models.KeyAttributes) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) DateTime(org.joda.time.DateTime)

Example 15 with JsonWebKey

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

the class KeyOperationsTest method importKeyOperation.

@Test
public void importKeyOperation() throws Exception {
    KeyBundle keyBundle = new KeyBundle();
    JsonWebKey key = JsonWebKey.fromRSA(getTestKeyMaterial());
    key.withKeyOps(Arrays.asList(JsonWebKeyOperation.ENCRYPT, JsonWebKeyOperation.DECRYPT));
    keyBundle.withKey(key);
    checkImportOperation(keyBundle, false);
    checkImportOperation(keyBundle, true);
}
Also used : KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) 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