Search in sources :

Example 1 with DecryptRequest

use of com.amazonaws.services.kms.model.DecryptRequest in project herd by FINRAOS.

the class KmsDaoImpl method decrypt.

@Override
public String decrypt(AwsParamsDto awsParamsDto, String base64ciphertextBlob) {
    // Construct a new AWS KMS service client using the specified client configuration.
    // A credentials provider chain will be used that searches for credentials in this order:
    // - Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_KEY
    // - Java System Properties - aws.accessKeyId and aws.secretKey
    // - Instance Profile Credentials - delivered through the Amazon EC2 metadata service
    AWSKMSClient awsKmsClient = new AWSKMSClient(awsHelper.getClientConfiguration(awsParamsDto));
    // Decode the base64 encoded ciphertext.
    ByteBuffer ciphertextBlob = ByteBuffer.wrap(Base64.decodeBase64(base64ciphertextBlob));
    // Create the decrypt request.
    DecryptRequest decryptRequest = new DecryptRequest().withCiphertextBlob(ciphertextBlob);
    // Call AWS KMS decrypt service method.
    DecryptResult decryptResult = kmsOperations.decrypt(awsKmsClient, decryptRequest);
    // Get decrypted plaintext data.
    ByteBuffer plainText = decryptResult.getPlaintext();
    // Return the plain text as a string.
    return new String(plainText.array(), StandardCharsets.UTF_8);
}
Also used : DecryptResult(com.amazonaws.services.kms.model.DecryptResult) AWSKMSClient(com.amazonaws.services.kms.AWSKMSClient) ByteBuffer(java.nio.ByteBuffer) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest)

Example 2 with DecryptRequest

use of com.amazonaws.services.kms.model.DecryptRequest in project spring-cloud-config-aws-kms by zalando.

the class KmsTextEncryptorTest method setUp.

@Before
public void setUp() throws Exception {
    mockKms = mock(AWSKMS.class);
    textEncryptor = new KmsTextEncryptor(mockKms, KMS_KEY_ID);
    expectedEncryptRequest = new EncryptRequest();
    expectedEncryptRequest.setKeyId(KMS_KEY_ID);
    expectedEncryptRequest.setPlaintext(wrap(PLAINTEXT.getBytes()));
    encryptResult = new EncryptResult();
    encryptResult.setCiphertextBlob(wrap(CIPHER_TEXT.getBytes()));
    when(mockKms.encrypt(any(EncryptRequest.class))).thenReturn(encryptResult);
    expectedDecryptRequest = new DecryptRequest();
    expectedDecryptRequest.setCiphertextBlob(wrap(CIPHER_TEXT.getBytes()));
    decryptResult = new DecryptResult();
    decryptResult.setPlaintext(wrap(PLAINTEXT.getBytes()));
    when(mockKms.decrypt(any(DecryptRequest.class))).thenReturn(decryptResult);
}
Also used : DecryptResult(com.amazonaws.services.kms.model.DecryptResult) EncryptResult(com.amazonaws.services.kms.model.EncryptResult) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest) AWSKMS(com.amazonaws.services.kms.AWSKMS) EncryptRequest(com.amazonaws.services.kms.model.EncryptRequest) Before(org.junit.Before)

Example 3 with DecryptRequest

use of com.amazonaws.services.kms.model.DecryptRequest in project spring-cloud-config-aws-kms by zalando.

the class KmsTextEncryptor method decrypt.

@Override
public String decrypt(final String encryptedText) {
    if (encryptedText == null || encryptedText.isEmpty()) {
        return EMPTY_STRING;
    } else {
        // Extract the encryption context and the remaining part
        final Map<String, String> encryptionContext = extractEncryptionContext(encryptedText);
        final String encryptedValue = extractEncryptedValue(encryptedText);
        // Assuming the encryptedText is encoded in Base64
        final ByteBuffer encryptedBytes = ByteBuffer.wrap(Base64.decode(encryptedValue.getBytes()));
        final DecryptRequest decryptRequest = new DecryptRequest().withCiphertextBlob(encryptedBytes).withEncryptionContext(encryptionContext);
        return extractString(kms.decrypt(decryptRequest).getPlaintext());
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest)

Example 4 with DecryptRequest

use of com.amazonaws.services.kms.model.DecryptRequest in project bender by Nextdoor.

the class Passwords method decrypt.

public static String decrypt(String str, Region region) throws UnsupportedEncodingException {
    if (isJUnitTest()) {
        return str;
    }
    AWSKMS kms = AWSKMSClientBuilder.standard().withRegion(region.getName()).build();
    /*
     * The KMS ciphertext is base64 encoded and must be decoded before the request is made
     */
    String cipherString = str;
    byte[] cipherBytes = Base64.decode(cipherString);
    /*
     * Create decode request and decode
     */
    ByteBuffer cipherBuffer = ByteBuffer.wrap(cipherBytes);
    DecryptRequest req = new DecryptRequest().withCiphertextBlob(cipherBuffer);
    DecryptResult resp = kms.decrypt(req);
    /*
     * Convert the response plaintext bytes to a string
     */
    return new String(resp.getPlaintext().array(), Charset.forName("UTF-8"));
}
Also used : DecryptResult(com.amazonaws.services.kms.model.DecryptResult) ByteBuffer(java.nio.ByteBuffer) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest) AWSKMS(com.amazonaws.services.kms.AWSKMS)

Example 5 with DecryptRequest

use of com.amazonaws.services.kms.model.DecryptRequest in project tink by google.

the class AwsKmsAead method decrypt.

@Override
public byte[] decrypt(final byte[] ciphertext, final byte[] associatedData) throws GeneralSecurityException {
    try {
        DecryptRequest req = new DecryptRequest().withCiphertextBlob(ByteBuffer.wrap(ciphertext));
        if (associatedData != null && associatedData.length != 0) {
            req = req.addEncryptionContextEntry("associatedData", BinaryUtils.toHex(associatedData));
        }
        DecryptResult result = kmsClient.decrypt(req);
        if (!result.getKeyId().equals(keyArn)) {
            throw new GeneralSecurityException("decryption failed: wrong key id");
        }
        return result.getPlaintext().array();
    } catch (AmazonServiceException e) {
        throw new GeneralSecurityException("decryption failed", e);
    }
}
Also used : DecryptResult(com.amazonaws.services.kms.model.DecryptResult) GeneralSecurityException(java.security.GeneralSecurityException) AmazonServiceException(com.amazonaws.AmazonServiceException) DecryptRequest(com.amazonaws.services.kms.model.DecryptRequest)

Aggregations

DecryptRequest (com.amazonaws.services.kms.model.DecryptRequest)10 ByteBuffer (java.nio.ByteBuffer)7 DecryptResult (com.amazonaws.services.kms.model.DecryptResult)4 AWSKMS (com.amazonaws.services.kms.AWSKMS)3 AWSKMSClient (com.amazonaws.services.kms.AWSKMSClient)3 AWSCredentials (com.amazonaws.auth.AWSCredentials)2 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 EncryptRequest (com.amazonaws.services.kms.model.EncryptRequest)1 EncryptResult (com.amazonaws.services.kms.model.EncryptResult)1 GenerateDataKeyRequest (com.amazonaws.services.kms.model.GenerateDataKeyRequest)1 GenerateDataKeyResult (com.amazonaws.services.kms.model.GenerateDataKeyResult)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 WSPasswordCallback (org.apache.wss4j.common.ext.WSPasswordCallback)1 Base64DecodingException (org.apache.xml.security.exceptions.Base64DecodingException)1 Before (org.junit.Before)1