Search in sources :

Example 6 with KeyOperationResult

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

the class KeyVaultClientImpl method signWithServiceResponseAsync.

/**
     * Creates a signature from a digest using the specified 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 The signing/verification algorithm identifier. For more information on possible algorithm types, see JsonWebKeySignatureAlgorithm. Possible values include: 'RS256', 'RS384', 'RS512', 'RSNULL'
     * @param value the Base64Url value
     * @return the observable to the KeyOperationResult object
     */
public Observable<ServiceResponse<KeyOperationResult>> signWithServiceResponseAsync(String vaultBaseUrl, String keyName, String keyVersion, JsonWebKeySignatureAlgorithm 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.");
    }
    KeySignParameters parameters = new KeySignParameters();
    parameters.withAlgorithm(algorithm);
    parameters.withValue(value);
    String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
    return service.sign(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 = signDelegate(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) KeySignParameters(com.microsoft.azure.keyvault.models.KeySignParameters) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody)

Example 7 with KeyOperationResult

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

the class KeyOperationsTest method checkEncryptDecryptSequence.

private void checkEncryptDecryptSequence(JsonWebKey importedKey, KeyBundle importedKeyBundle) throws Exception {
    // Test variables
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    byte[] cipherText;
    // Encrypt in the service.
    {
        KeyOperationResult result = keyVaultClient.encrypt(importedKeyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
    }
    // Decrypt in the client, notice OAEP algorithm instance to use.
    {
        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
        cipher.init(Cipher.DECRYPT_MODE, importedKey.toRSA(true).getPrivate());
        byte[] beforeEncrypt = plainText;
        byte[] afterDecrypt = cipher.doFinal(cipherText);
        Assert.assertArrayEquals(beforeEncrypt, afterDecrypt);
    }
    // Encrypt in the client, using the service provided material. Also use
    // standard padding.
    {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, importedKeyBundle.key().toRSA().getPublic());
        cipherText = cipher.doFinal(plainText);
    }
    // Decrypt in the service.
    {
        KeyOperationResult result = keyVaultClient.decrypt(importedKeyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA1_5, cipherText);
        byte[] beforeEncrypt = plainText;
        byte[] afterDecrypt = result.result();
        Assert.assertArrayEquals(beforeEncrypt, afterDecrypt);
    }
}
Also used : Random(java.util.Random) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) Cipher(javax.crypto.Cipher)

Example 8 with KeyOperationResult

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

the class KeyOperationsTest method wrapUnwrapOperations.

@Test
public void wrapUnwrapOperations() 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;
    // wrap and unwrap using kid WO version
    {
        result = keyVaultClient.wrapKey(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.unwrapKey(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, cipherText);
        Assert.assertArrayEquals(plainText, result.result());
    }
    // wrap and unwrap using full kid
    {
        result = keyVaultClient.wrapKey(testKey.kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.unwrapKey(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 9 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 10 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)

Aggregations

KeyOperationResult (com.microsoft.azure.keyvault.models.KeyOperationResult)10 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 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 ExecutionException (java.util.concurrent.ExecutionException)1