|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except |
| 5 | + * in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.amazonaws.encryptionsdk.keyrings; |
| 15 | + |
| 16 | +import com.amazonaws.encryptionsdk.EncryptedDataKey; |
| 17 | +import com.amazonaws.encryptionsdk.internal.JceKeyCipher; |
| 18 | +import com.amazonaws.encryptionsdk.internal.Utils; |
| 19 | +import com.amazonaws.encryptionsdk.model.DecryptionMaterials; |
| 20 | +import com.amazonaws.encryptionsdk.model.EncryptionMaterials; |
| 21 | +import com.amazonaws.encryptionsdk.model.KeyBlob; |
| 22 | + |
| 23 | +import javax.crypto.SecretKey; |
| 24 | +import javax.crypto.spec.SecretKeySpec; |
| 25 | + |
| 26 | +import java.nio.charset.Charset; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.security.PrivateKey; |
| 29 | +import java.security.PublicKey; |
| 30 | +import java.util.List; |
| 31 | +import java.util.logging.Logger; |
| 32 | + |
| 33 | +import static org.apache.commons.lang3.Validate.notBlank; |
| 34 | +import static org.apache.commons.lang3.Validate.notNull; |
| 35 | + |
| 36 | +/** |
| 37 | + * A keyring supporting local encryption and decryption using either RSA or AES-GCM. |
| 38 | + */ |
| 39 | +public class RawKeyring implements Keyring { |
| 40 | + |
| 41 | + private final String keyNamespace; |
| 42 | + private final String keyName; |
| 43 | + private final JceKeyCipher jceKeyCipher; |
| 44 | + private static final Charset KEY_NAME_ENCODING = StandardCharsets.UTF_8; |
| 45 | + private static final Logger LOGGER = Logger.getLogger(RawKeyring.class.getName()); |
| 46 | + |
| 47 | + static Keyring aes(String keyNamespace, String keyName, SecretKey wrappingKey) { |
| 48 | + return new RawKeyring(keyNamespace, keyName, JceKeyCipher.aesGcm(wrappingKey)); |
| 49 | + } |
| 50 | + |
| 51 | + static Keyring rsa(String keyNamespace, String keyName, PublicKey publicKey, PrivateKey privateKey, String transformation) { |
| 52 | + return new RawKeyring(keyNamespace, keyName, JceKeyCipher.rsa(publicKey, privateKey, transformation)); |
| 53 | + } |
| 54 | + |
| 55 | + private RawKeyring(final String keyNamespace, final String keyName, JceKeyCipher jceKeyCipher) { |
| 56 | + notBlank(keyNamespace, "keyNamespace is required"); |
| 57 | + notBlank(keyName, "keyName is required"); |
| 58 | + notNull(jceKeyCipher, "jceKeyCipher is required"); |
| 59 | + |
| 60 | + this.keyNamespace = keyNamespace; |
| 61 | + this.keyName = keyName; |
| 62 | + this.jceKeyCipher = jceKeyCipher; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onEncrypt(EncryptionMaterials encryptionMaterials) { |
| 67 | + notNull(encryptionMaterials, "encryptionMaterials are required"); |
| 68 | + |
| 69 | + if (encryptionMaterials.getCleartextDataKey() == null) { |
| 70 | + generateDataKey(encryptionMaterials); |
| 71 | + } |
| 72 | + |
| 73 | + final SecretKey cleartextDataKey = encryptionMaterials.getCleartextDataKey(); |
| 74 | + |
| 75 | + if (!cleartextDataKey.getAlgorithm().equalsIgnoreCase(encryptionMaterials.getAlgorithm().getDataKeyAlgo())) { |
| 76 | + throw new IllegalArgumentException("Incorrect key algorithm. Expected " + cleartextDataKey.getAlgorithm() |
| 77 | + + " but got " + encryptionMaterials.getAlgorithm().getDataKeyAlgo()); |
| 78 | + } |
| 79 | + |
| 80 | + final EncryptedDataKey encryptedDataKey = jceKeyCipher.encryptKey( |
| 81 | + cleartextDataKey.getEncoded(), keyName, keyNamespace, encryptionMaterials.getEncryptionContext()); |
| 82 | + encryptionMaterials.getEncryptedDataKeys().add(new KeyBlob(encryptedDataKey)); |
| 83 | + |
| 84 | + encryptionMaterials.getKeyringTrace().add(keyNamespace, keyName, KeyringTraceFlag.ENCRYPTED_DATA_KEY); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onDecrypt(DecryptionMaterials decryptionMaterials, List<EncryptedDataKey> encryptedDataKeys) { |
| 89 | + notNull(decryptionMaterials, "decryptionMaterials are required"); |
| 90 | + notNull(encryptedDataKeys, "encryptedDataKeys are required"); |
| 91 | + |
| 92 | + if (decryptionMaterials.getCleartextDataKey() != null) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + final byte[] keyNameBytes = keyName.getBytes(KEY_NAME_ENCODING); |
| 97 | + |
| 98 | + for (EncryptedDataKey encryptedDataKey : encryptedDataKeys) { |
| 99 | + if (!keyNamespace.equals(encryptedDataKey.getProviderId())) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + if (!Utils.arrayPrefixEquals(encryptedDataKey.getProviderInformation(), keyNameBytes, keyNameBytes.length)) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + final byte[] decryptedKey = jceKeyCipher.decryptKey( |
| 109 | + encryptedDataKey, keyName, decryptionMaterials.getEncryptionContext()); |
| 110 | + decryptionMaterials.setCleartextDataKey( |
| 111 | + new SecretKeySpec(decryptedKey, decryptionMaterials.getAlgorithm().getDataKeyAlgo())); |
| 112 | + decryptionMaterials.getKeyringTrace().add(keyNamespace, keyName, KeyringTraceFlag.DECRYPTED_DATA_KEY); |
| 113 | + return; |
| 114 | + } catch (Exception e) { |
| 115 | + LOGGER.info("Could not decrypt key due to: " + e.getMessage()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + LOGGER.warning("Could not decrypt any data keys"); |
| 120 | + } |
| 121 | + |
| 122 | + private void generateDataKey(EncryptionMaterials encryptionMaterials) { |
| 123 | + if (encryptionMaterials.getCleartextDataKey() != null) { |
| 124 | + throw new IllegalStateException("Plaintext data key already exists"); |
| 125 | + } |
| 126 | + |
| 127 | + final byte[] rawKey = new byte[encryptionMaterials.getAlgorithm().getDataKeyLength()]; |
| 128 | + Utils.getSecureRandom().nextBytes(rawKey); |
| 129 | + final SecretKey key = new SecretKeySpec(rawKey, encryptionMaterials.getAlgorithm().getDataKeyAlgo()); |
| 130 | + |
| 131 | + encryptionMaterials.setCleartextDataKey(key); |
| 132 | + encryptionMaterials.getKeyringTrace().add(keyNamespace, keyName, KeyringTraceFlag.GENERATED_DATA_KEY); |
| 133 | + } |
| 134 | +} |
0 commit comments