|
| 1 | +package com.amazonaws.crypto.examples.v2; |
| 2 | + |
| 3 | +import com.amazonaws.encryptionsdk.CryptoAlgorithm; |
| 4 | +import com.amazonaws.encryptionsdk.DataKey; |
| 5 | +import com.amazonaws.encryptionsdk.EncryptedDataKey; |
| 6 | +import com.amazonaws.encryptionsdk.MasterKey; |
| 7 | +import com.amazonaws.encryptionsdk.exception.AwsCryptoException; |
| 8 | +import com.amazonaws.encryptionsdk.exception.CannotUnwrapDataKeyException; |
| 9 | +import com.amazonaws.encryptionsdk.exception.UnsupportedProviderException; |
| 10 | +import software.amazon.cryptography.materialproviders.IKeyring; |
| 11 | +import software.amazon.cryptography.materialproviders.MaterialProviders; |
| 12 | +import software.amazon.cryptography.materialproviders.model.*; |
| 13 | + |
| 14 | +import javax.crypto.SecretKey; |
| 15 | +import javax.crypto.spec.SecretKeySpec; |
| 16 | +import java.nio.ByteBuffer; |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +public class HKeyringMasterKey extends MasterKey<HKeyringMasterKey> { |
| 20 | + |
| 21 | + final private IKeyring hKeyring; |
| 22 | + final private CreateAwsKmsHierarchicalKeyringInput hKeyringInput; |
| 23 | + final private MaterialProviders mpl; |
| 24 | + |
| 25 | + public HKeyringMasterKey( |
| 26 | + CreateAwsKmsHierarchicalKeyringInput input |
| 27 | + ) { |
| 28 | + if (input.branchKeyIdSupplier() != null) throw new UnsupportedProviderException("branchKeyIdSupplier must be null"); |
| 29 | + if (input.branchKeyId() == null) throw new UnsupportedProviderException("branchKeyId cannot be null"); |
| 30 | + hKeyringInput = input; |
| 31 | + mpl = |
| 32 | + MaterialProviders.builder() |
| 33 | + .MaterialProvidersConfig(MaterialProvidersConfig.builder().build()) |
| 34 | + .build(); |
| 35 | + hKeyring = mpl.CreateAwsKmsHierarchicalKeyring(hKeyringInput); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getProviderId() { |
| 40 | + return "aws-kms-hierarchy"; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getKeyId() { |
| 45 | + return this.hKeyringInput.branchKeyId(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Generates a new {@link DataKey} which is protected by this {@link MasterKey} for use with |
| 50 | + * {@code algorithm} and associated with the provided {@code encryptionContext}. |
| 51 | + * |
| 52 | + * @param algorithm |
| 53 | + * @param encryptionContext |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public DataKey<HKeyringMasterKey> generateDataKey(CryptoAlgorithm algorithm, Map<String, String> encryptionContext) { |
| 57 | + AlgorithmSuiteInfo algorithmSuiteInfo = ValidateAndConvertAlgo(algorithm); |
| 58 | + EncryptionMaterials encryptionMaterials = EncryptionMaterials.builder() |
| 59 | + .algorithmSuite(algorithmSuiteInfo) |
| 60 | + .encryptionContext(encryptionContext) |
| 61 | + .encryptedDataKeys(Collections.emptyList()) |
| 62 | + .requiredEncryptionContextKeys(Collections.emptyList()) |
| 63 | + .build(); |
| 64 | + OnEncryptInput eInput = OnEncryptInput.builder() |
| 65 | + .materials(encryptionMaterials) |
| 66 | + .build(); |
| 67 | + OnEncryptOutput onEncryptOutput = hKeyring.OnEncrypt(eInput); |
| 68 | + software.amazon.cryptography.materialproviders.model.EncryptedDataKey encryptedDataKey = onEncryptOutput.materials().encryptedDataKeys().get(0); |
| 69 | + return new DataKey<>( |
| 70 | + new SecretKeySpec(onEncryptOutput.materials().plaintextDataKey().array(), algorithm.getDataKeyAlgo()), |
| 71 | + encryptedDataKey.ciphertext().array(), |
| 72 | + encryptedDataKey.keyProviderInfo().array(), |
| 73 | + this); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns a new copy of the provided {@code dataKey} which is protected by this {@link MasterKey} |
| 78 | + * for use with {@code algorithm} and associated with the provided {@code encryptionContext}. |
| 79 | + * |
| 80 | + * @param algorithm |
| 81 | + * @param encryptionContext |
| 82 | + * @param dataKey |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public DataKey<HKeyringMasterKey> encryptDataKey( |
| 86 | + CryptoAlgorithm algorithm, |
| 87 | + Map<String, String> encryptionContext, |
| 88 | + DataKey<?> dataKey |
| 89 | + ) { |
| 90 | + AlgorithmSuiteInfo algorithmSuiteInfo = ValidateAndConvertAlgo(algorithm); |
| 91 | + final SecretKey key = dataKey.getKey(); |
| 92 | + if (!key.getFormat().equals("RAW")) { |
| 93 | + throw new IllegalArgumentException( |
| 94 | + "Can only re-encrypt data keys which are in RAW format, not " |
| 95 | + + dataKey.getKey().getFormat()); |
| 96 | + } |
| 97 | + EncryptionMaterials encryptionMaterials = EncryptionMaterials.builder() |
| 98 | + .algorithmSuite(algorithmSuiteInfo) |
| 99 | + .encryptionContext(encryptionContext) |
| 100 | + .encryptedDataKeys(Collections.emptyList()) |
| 101 | + .requiredEncryptionContextKeys(Collections.emptyList()) |
| 102 | + .plaintextDataKey(ByteBuffer.wrap(dataKey.getKey().getEncoded())) |
| 103 | + .build(); |
| 104 | + OnEncryptInput eInput = OnEncryptInput.builder() |
| 105 | + .materials(encryptionMaterials) |
| 106 | + .build(); |
| 107 | + OnEncryptOutput onEncryptOutput = hKeyring.OnEncrypt(eInput); |
| 108 | + software.amazon.cryptography.materialproviders.model.EncryptedDataKey encryptedDataKey = onEncryptOutput.materials().encryptedDataKeys().get(0); |
| 109 | + return new DataKey<>( |
| 110 | + key, |
| 111 | + encryptedDataKey.ciphertext().array(), |
| 112 | + encryptedDataKey.keyProviderInfo().array(), |
| 113 | + this); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Iterates through {@code encryptedDataKeys} and returns the first one which can be successfully |
| 118 | + * decrypted. |
| 119 | + * |
| 120 | + * @param algorithm |
| 121 | + * @param encryptedDataKeys |
| 122 | + * @param encryptionContext |
| 123 | + * @return a DataKey if one can be decrypted, otherwise returns {@code null} |
| 124 | + * @throws UnsupportedProviderException if the {@code encryptedDataKey} is associated with an |
| 125 | + * unsupported provider |
| 126 | + * @throws CannotUnwrapDataKeyException if the {@code encryptedDataKey} cannot be decrypted |
| 127 | + */ |
| 128 | + @Override |
| 129 | + public DataKey<HKeyringMasterKey> decryptDataKey( |
| 130 | + CryptoAlgorithm algorithm, |
| 131 | + Collection<? extends EncryptedDataKey> encryptedDataKeys, |
| 132 | + Map<String, String> encryptionContext |
| 133 | + ) throws UnsupportedProviderException, AwsCryptoException |
| 134 | + { |
| 135 | + AlgorithmSuiteInfo algorithmSuiteInfo = ValidateAndConvertAlgo(algorithm); |
| 136 | + if (encryptedDataKeys.size() != 1) { |
| 137 | + // TODO: If needed, we could refactor this to properly support multiple EDKs, it would not be hard. |
| 138 | + throw new UnsupportedProviderException("Alas, this Master Key Provider can work with one (1) Encrypted Data Key; got " + encryptedDataKeys.size()); |
| 139 | + } |
| 140 | + List<EncryptedDataKey> nativeEDKS = EDKCollectionToNative(encryptedDataKeys); |
| 141 | + List<software.amazon.cryptography.materialproviders.model.EncryptedDataKey> mplEDKS = EDKCollectionToMPL(encryptedDataKeys); |
| 142 | + DecryptionMaterials decryptionMaterials = DecryptionMaterials.builder() |
| 143 | + .algorithmSuite(algorithmSuiteInfo) |
| 144 | + .encryptionContext(encryptionContext) |
| 145 | + .requiredEncryptionContextKeys(Collections.emptyList()) |
| 146 | + .build(); |
| 147 | + OnDecryptInput onDecryptInput = OnDecryptInput.builder() |
| 148 | + .encryptedDataKeys(mplEDKS) |
| 149 | + .materials(decryptionMaterials) |
| 150 | + .build(); |
| 151 | + OnDecryptOutput onDecryptOutput = this.hKeyring.OnDecrypt(onDecryptInput); |
| 152 | + if (onDecryptOutput.materials().plaintextDataKey() != null) { |
| 153 | + if (onDecryptOutput.materials().plaintextDataKey().array().length != algorithm.getDataKeyLength()) |
| 154 | + throw new AwsCryptoException("Decrypted Data Key is incorrect length!"); |
| 155 | + return new DataKey<>( |
| 156 | + new SecretKeySpec(onDecryptOutput.materials().plaintextDataKey().array(), algorithm.getDataKeyAlgo()), |
| 157 | + nativeEDKS.get(0).getEncryptedDataKey(), |
| 158 | + nativeEDKS.get(0).getProviderInformation(), |
| 159 | + this |
| 160 | + ); |
| 161 | + } |
| 162 | + return null; |
| 163 | + } |
| 164 | + |
| 165 | + private List<software.amazon.cryptography.materialproviders.model.EncryptedDataKey> EDKCollectionToMPL( |
| 166 | + Collection<? extends EncryptedDataKey> encryptedDataKeys |
| 167 | + ) { |
| 168 | + List<software.amazon.cryptography.materialproviders.model.EncryptedDataKey> mplEDKS = |
| 169 | + new ArrayList<>(encryptedDataKeys.size()); |
| 170 | + |
| 171 | + for (EncryptedDataKey keyBlob : encryptedDataKeys) { |
| 172 | + mplEDKS.add( |
| 173 | + software.amazon.cryptography.materialproviders.model.EncryptedDataKey.builder() |
| 174 | + .keyProviderId(keyBlob.getProviderId()) |
| 175 | + .keyProviderInfo( |
| 176 | + ByteBuffer.wrap( |
| 177 | + keyBlob.getProviderInformation(), 0, keyBlob.getProviderInformation().length)) |
| 178 | + .ciphertext( |
| 179 | + ByteBuffer.wrap( |
| 180 | + keyBlob.getEncryptedDataKey(), 0, keyBlob.getEncryptedDataKey().length)) |
| 181 | + .build()); |
| 182 | + } |
| 183 | + return mplEDKS; |
| 184 | + } |
| 185 | + |
| 186 | + private List<EncryptedDataKey> EDKCollectionToNative( |
| 187 | + Collection<? extends EncryptedDataKey> encryptedDataKeys |
| 188 | + ) { |
| 189 | + List<EncryptedDataKey> nativeEDKS = new ArrayList<>(encryptedDataKeys.size()); |
| 190 | + nativeEDKS.addAll(encryptedDataKeys); |
| 191 | + return nativeEDKS; |
| 192 | + } |
| 193 | + |
| 194 | + private AlgorithmSuiteInfo ValidateAndConvertAlgo( |
| 195 | + CryptoAlgorithm algorithm |
| 196 | + ) { |
| 197 | + if (algorithm.getTrailingSignatureAlgo() != null) { |
| 198 | + throw new UnsupportedProviderException("The HKeyringMasterKey provider does not support trailing signature algorithms!"); |
| 199 | + } |
| 200 | + return mpl.GetAlgorithmSuiteInfo(ByteBuffer.allocate(2).putShort((short) algorithm.getValue())); |
| 201 | + } |
| 202 | +} |
0 commit comments