Search in sources :

Example 1 with KeyOperationResult

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

the class KeyVaultClientImpl method decryptWithServiceResponseAsync.

/**
     * Decrypts a single block of encrypted data.
     *
     * @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
     * @param keyName The name of the key.
     * @param keyVersion The version of the key.
     * @param algorithm algorithm identifier. Possible values include: 'RSA-OAEP', 'RSA1_5'
     * @param value the Base64Url value
     * @return the observable to the KeyOperationResult object
     */
public Observable<ServiceResponse<KeyOperationResult>> decryptWithServiceResponseAsync(String vaultBaseUrl, String keyName, String keyVersion, JsonWebKeyEncryptionAlgorithm algorithm, byte[] value) {
    if (vaultBaseUrl == null) {
        throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
    }
    if (keyName == null) {
        throw new IllegalArgumentException("Parameter keyName is required and cannot be null.");
    }
    if (keyVersion == null) {
        throw new IllegalArgumentException("Parameter keyVersion is required and cannot be null.");
    }
    if (this.apiVersion() == null) {
        throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
    }
    if (algorithm == null) {
        throw new IllegalArgumentException("Parameter algorithm is required and cannot be null.");
    }
    if (value == null) {
        throw new IllegalArgumentException("Parameter value is required and cannot be null.");
    }
    KeyOperationsParameters parameters = new KeyOperationsParameters();
    parameters.withAlgorithm(algorithm);
    parameters.withValue(value);
    String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
    return service.decrypt(keyName, keyVersion, this.apiVersion(), this.acceptLanguage(), parameters, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<KeyOperationResult>>>() {

        @Override
        public Observable<ServiceResponse<KeyOperationResult>> call(Response<ResponseBody> response) {
            try {
                ServiceResponse<KeyOperationResult> clientResponse = decryptDelegate(response);
                return Observable.just(clientResponse);
            } catch (Throwable t) {
                return Observable.error(t);
            }
        }
    });
}
Also used : Response(retrofit2.Response) ServiceResponse(com.microsoft.rest.ServiceResponse) ServiceResponse(com.microsoft.rest.ServiceResponse) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) KeyOperationsParameters(com.microsoft.azure.keyvault.models.KeyOperationsParameters) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody)

Example 2 with KeyOperationResult

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

the class SQLServerColumnEncryptionAzureKeyVaultProvider method AzureKeyVaultWrap.

/**
 * Encrypt the text using specified Azure Key Vault key.
 *
 * @param masterKeyPath
 *            - Azure Key Vault key url.
 * @param encryptionAlgorithm
 *            - Encryption Algorithm.
 * @param columnEncryptionKey
 *            - Plain text Column Encryption Key.
 * @return Returns an encrypted blob or throws an exception if there are any errors.
 * @throws SQLServerException
 */
private byte[] AzureKeyVaultWrap(String masterKeyPath, String encryptionAlgorithm, byte[] columnEncryptionKey) throws SQLServerException {
    if (null == columnEncryptionKey) {
        throw new SQLServerException(SQLServerException.getErrString("R_CEKNull"), null);
    }
    JsonWebKeyEncryptionAlgorithm jsonEncryptionAlgorithm = new JsonWebKeyEncryptionAlgorithm(encryptionAlgorithm);
    KeyOperationResult wrappedKey = keyVaultClient.wrapKey(masterKeyPath, jsonEncryptionAlgorithm, columnEncryptionKey);
    return wrappedKey.result();
}
Also used : KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKeyEncryptionAlgorithm(com.microsoft.azure.keyvault.webkey.JsonWebKeyEncryptionAlgorithm)

Example 3 with KeyOperationResult

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

the class RangerAzureKeyVaultKeyGenerator method decryptZoneKey.

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

Example 4 with KeyOperationResult

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

the class AsyncOperationsTest method keyAsync.

@Test
public void keyAsync() throws Exception {
    String vault = getVaultUri();
    String keyname = "mykey";
    CreateKeyRequest createKeyRequest = new CreateKeyRequest.Builder(vault, keyname, JsonWebKeyType.RSA).build();
    KeyBundle keyBundle = keyVaultClient.createKeyAsync(createKeyRequest, null).get();
    Assert.assertNotNull(keyBundle);
    UpdateKeyRequest updateKeyRequest = new UpdateKeyRequest.Builder(keyBundle.key().kid()).build();
    keyBundle = keyVaultClient.updateKeyAsync(updateKeyRequest, null).get();
    Assert.assertNotNull(keyBundle);
    keyBundle = keyVaultClient.getKeyAsync(keyBundle.key().kid(), null).get();
    Assert.assertNotNull(keyBundle);
    List<KeyItem> keyItems = keyVaultClient.listKeysAsync(vault, 2, null).get();
    Assert.assertNotNull(keyItems);
    List<KeyItem> keyVersionItems = keyVaultClient.listKeyVersionsAsync(getVaultUri(), keyname, 2, null).get();
    Assert.assertNotNull(keyVersionItems);
    BackupKeyResult backupResult = keyVaultClient.backupKeyAsync(vault, keyname, null).get();
    Assert.assertNotNull(backupResult);
    keyVaultClient.deleteKeyAsync(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name(), null).get();
    KeyBundle restoreResult = keyVaultClient.restoreKeyAsync(vault, backupResult.value(), null).get();
    Assert.assertNotNull(restoreResult);
    KeyOperationResult encryptResult = keyVaultClient.encryptAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, new byte[100], null).get();
    Assert.assertNotNull(encryptResult);
    KeyOperationResult decryptResult = keyVaultClient.decryptAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, encryptResult.result(), null).get();
    Assert.assertNotNull(decryptResult);
    KeyOperationResult wrapResult = keyVaultClient.wrapKeyAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, new byte[100], null).get();
    Assert.assertNotNull(wrapResult);
    KeyOperationResult unwrapResult = keyVaultClient.unwrapKeyAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, wrapResult.result(), null).get();
    Assert.assertNotNull(unwrapResult);
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(plainText);
    byte[] digest = md.digest();
    KeyOperationResult signResult = keyVaultClient.signAsync(keyBundle.key().kid(), JsonWebKeySignatureAlgorithm.RS256, digest, null).get();
    Assert.assertNotNull(signResult);
    KeyVerifyResult verifypResult = keyVaultClient.verifyAsync(keyBundle.key().kid(), JsonWebKeySignatureAlgorithm.RS256, digest, signResult.result(), null).get();
    Assert.assertTrue(verifypResult.value());
    keyBundle = keyVaultClient.deleteKeyAsync(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name(), null).get();
    Assert.assertNotNull(keyBundle);
    try {
        keyVaultClient.deleteKeyAsync(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name(), null).get();
    } catch (ExecutionException ex) {
        Throwable t = ex.getCause();
        if (t instanceof KeyVaultErrorException) {
            Assert.assertEquals("KeyNotFound", ((KeyVaultErrorException) t).body().error().code());
        } else
            throw ex;
    }
}
Also used : KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) KeyItem(com.microsoft.azure.keyvault.models.KeyItem) BackupKeyResult(com.microsoft.azure.keyvault.models.BackupKeyResult) UpdateKeyRequest(com.microsoft.azure.keyvault.requests.UpdateKeyRequest) Random(java.util.Random) KeyVerifyResult(com.microsoft.azure.keyvault.models.KeyVerifyResult) CreateKeyRequest(com.microsoft.azure.keyvault.requests.CreateKeyRequest) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) MessageDigest(java.security.MessageDigest) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 5 with KeyOperationResult

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

the class KeyVaultClientImpl method unwrapKeyWithServiceResponseAsync.

/**
     * Unwraps a symmetric key using the specified key that was initially used for wrapping that key.
     *
     * @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
     * @param keyName The name of the key.
     * @param keyVersion The version of the key.
     * @param algorithm algorithm identifier. Possible values include: 'RSA-OAEP', 'RSA1_5'
     * @param value the Base64Url value
     * @return the observable to the KeyOperationResult object
     */
public Observable<ServiceResponse<KeyOperationResult>> unwrapKeyWithServiceResponseAsync(String vaultBaseUrl, String keyName, String keyVersion, JsonWebKeyEncryptionAlgorithm algorithm, byte[] value) {
    if (vaultBaseUrl == null) {
        throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
    }
    if (keyName == null) {
        throw new IllegalArgumentException("Parameter keyName is required and cannot be null.");
    }
    if (keyVersion == null) {
        throw new IllegalArgumentException("Parameter keyVersion is required and cannot be null.");
    }
    if (this.apiVersion() == null) {
        throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
    }
    if (algorithm == null) {
        throw new IllegalArgumentException("Parameter algorithm is required and cannot be null.");
    }
    if (value == null) {
        throw new IllegalArgumentException("Parameter value is required and cannot be null.");
    }
    KeyOperationsParameters parameters = new KeyOperationsParameters();
    parameters.withAlgorithm(algorithm);
    parameters.withValue(value);
    String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
    return service.unwrapKey(keyName, keyVersion, this.apiVersion(), this.acceptLanguage(), parameters, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<KeyOperationResult>>>() {

        @Override
        public Observable<ServiceResponse<KeyOperationResult>> call(Response<ResponseBody> response) {
            try {
                ServiceResponse<KeyOperationResult> clientResponse = unwrapKeyDelegate(response);
                return Observable.just(clientResponse);
            } catch (Throwable t) {
                return Observable.error(t);
            }
        }
    });
}
Also used : Response(retrofit2.Response) ServiceResponse(com.microsoft.rest.ServiceResponse) ServiceResponse(com.microsoft.rest.ServiceResponse) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) KeyOperationsParameters(com.microsoft.azure.keyvault.models.KeyOperationsParameters) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody)

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