Search in sources :

Example 11 with KeyOperationResult

use of com.microsoft.azure.keyvault.models.KeyOperationResult in project azure-sdk-for-java by Azure.

the class KeyOperationsTest method signVerifyOperations.

@Test
public void signVerifyOperations() throws Exception {
    JsonWebKey testKey = importTestKey();
    KeyIdentifier keyId = new KeyIdentifier(testKey.kid());
    // Test variables
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(plainText);
    byte[] digest = md.digest();
    byte[] signature;
    KeyOperationResult result;
    KeyVerifyResult verifyResult;
    // Using kid WO version
    {
        result = keyVaultClient.sign(keyId.baseIdentifier(), JsonWebKeySignatureAlgorithm.RS256, digest);
        signature = result.result();
        verifyResult = keyVaultClient.verify(keyId.baseIdentifier(), JsonWebKeySignatureAlgorithm.RS256, digest, signature);
        Assert.assertEquals(new Boolean(true), verifyResult.value());
    }
    // Using full kid
    {
        result = keyVaultClient.sign(testKey.kid(), JsonWebKeySignatureAlgorithm.RS256, digest);
        signature = result.result();
        verifyResult = keyVaultClient.verify(testKey.kid(), JsonWebKeySignatureAlgorithm.RS256, digest, signature);
        Assert.assertEquals(new Boolean(true), verifyResult.value());
    }
}
Also used : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) Random(java.util.Random) KeyVerifyResult(com.microsoft.azure.keyvault.models.KeyVerifyResult) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Example 12 with KeyOperationResult

use of com.microsoft.azure.keyvault.models.KeyOperationResult in project azure-sdk-for-java by Azure.

the class KeyOperationsTest method encryptDecryptOperations.

@Test
public void encryptDecryptOperations() throws Exception {
    JsonWebKey testKey = importTestKey();
    KeyIdentifier keyId = new KeyIdentifier(testKey.kid());
    // Test variables
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    byte[] cipherText;
    KeyOperationResult result;
    // encrypt and decrypt using kid WO version
    {
        result = keyVaultClient.encrypt(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.decrypt(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, cipherText);
        Assert.assertArrayEquals(plainText, result.result());
    }
    // encrypt and decrypt using full kid
    {
        result = keyVaultClient.encrypt(testKey.kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.decrypt(testKey.kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, cipherText);
        Assert.assertArrayEquals(plainText, result.result());
    }
}
Also used : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) Random(java.util.Random) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) Test(org.junit.Test)

Example 13 with KeyOperationResult

use of com.microsoft.azure.keyvault.models.KeyOperationResult in project mssql-jdbc by Microsoft.

the class SQLServerColumnEncryptionAzureKeyVaultProvider method AzureKeyVaultUnWrap.

/**
 * Encrypt the text using specified Azure Key Vault key.
 *
 * @param masterKeyPath
 *            - Azure Key Vault key url.
 * @param encryptionAlgorithm
 *            - Encrypted Column Encryption Key.
 * @param encryptedColumnEncryptionKey
 *            - Encrypted Column Encryption Key.
 * @return Returns the decrypted plaintext Column Encryption Key or throws an exception if there are any errors.
 * @throws SQLServerException
 */
private byte[] AzureKeyVaultUnWrap(String masterKeyPath, String encryptionAlgorithm, byte[] encryptedColumnEncryptionKey) throws SQLServerException {
    if (null == encryptedColumnEncryptionKey) {
        throw new SQLServerException(SQLServerException.getErrString("R_EncryptedCEKNull"), null);
    }
    if (0 == encryptedColumnEncryptionKey.length) {
        throw new SQLServerException(SQLServerException.getErrString("R_EmptyEncryptedCEK"), null);
    }
    JsonWebKeyEncryptionAlgorithm jsonEncryptionAlgorithm = new JsonWebKeyEncryptionAlgorithm(encryptionAlgorithm);
    KeyOperationResult unwrappedKey = keyVaultClient.unwrapKey(masterKeyPath, jsonEncryptionAlgorithm, encryptedColumnEncryptionKey);
    return unwrappedKey.result();
}
Also used : KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKeyEncryptionAlgorithm(com.microsoft.azure.keyvault.webkey.JsonWebKeyEncryptionAlgorithm)

Example 14 with KeyOperationResult

use of com.microsoft.azure.keyvault.models.KeyOperationResult in project ranger by apache.

the class RangerAzureKeyVaultKeyGenerator method encryptZoneKey.

@Override
public byte[] encryptZoneKey(Key zoneKey) throws Exception {
    JsonWebKeyEncryptionAlgorithm keyEncryptionAlgo = getZoneKeyEncryptionAlgo();
    KeyOperationResult encryptResult = null;
    if (masterKeyBundle == null) {
        masterKeyBundle = keyVaultClient.getKey(keyVaultURL, azureMasterKey);
    }
    try {
        encryptResult = keyVaultClient.encryptAsync(masterKeyBundle.key().kid(), keyEncryptionAlgo, zoneKey.getEncoded(), null).get();
    } catch (Exception e) {
        throw new Exception("Error while encrypting zone key." + e);
    }
    return encryptResult.result();
}
Also used : KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKeyEncryptionAlgorithm(com.microsoft.azure.keyvault.webkey.JsonWebKeyEncryptionAlgorithm)

Aggregations

KeyOperationResult (com.microsoft.azure.keyvault.models.KeyOperationResult)14 ServiceResponse (com.microsoft.rest.ServiceResponse)5 Random (java.util.Random)5 ResponseBody (okhttp3.ResponseBody)5 Response (retrofit2.Response)5 Observable (rx.Observable)5 KeyOperationsParameters (com.microsoft.azure.keyvault.models.KeyOperationsParameters)4 JsonWebKeyEncryptionAlgorithm (com.microsoft.azure.keyvault.webkey.JsonWebKeyEncryptionAlgorithm)4 Test (org.junit.Test)4 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)3 JsonWebKey (com.microsoft.azure.keyvault.webkey.JsonWebKey)3 KeyVerifyResult (com.microsoft.azure.keyvault.models.KeyVerifyResult)2 MessageDigest (java.security.MessageDigest)2 BackupKeyResult (com.microsoft.azure.keyvault.models.BackupKeyResult)1 KeyBundle (com.microsoft.azure.keyvault.models.KeyBundle)1 KeyItem (com.microsoft.azure.keyvault.models.KeyItem)1 KeySignParameters (com.microsoft.azure.keyvault.models.KeySignParameters)1 KeyVaultErrorException (com.microsoft.azure.keyvault.models.KeyVaultErrorException)1 CreateKeyRequest (com.microsoft.azure.keyvault.requests.CreateKeyRequest)1 UpdateKeyRequest (com.microsoft.azure.keyvault.requests.UpdateKeyRequest)1