|
| 1 | +package com.amazonaws.crypto.examples.v2; |
| 2 | + |
| 3 | +import static com.amazonaws.encryptionsdk.internal.Utils.assertNonNull; |
| 4 | + |
| 5 | +import com.amazonaws.encryptionsdk.CommitmentPolicy; |
| 6 | +import com.amazonaws.encryptionsdk.CryptoAlgorithm; |
| 7 | +import com.amazonaws.encryptionsdk.CryptoMaterialsManager; |
| 8 | +import com.amazonaws.encryptionsdk.DataKey; |
| 9 | +import com.amazonaws.encryptionsdk.MasterKey; |
| 10 | +import com.amazonaws.encryptionsdk.MasterKeyProvider; |
| 11 | +import com.amazonaws.encryptionsdk.MasterKeyRequest; |
| 12 | +import com.amazonaws.encryptionsdk.exception.AwsCryptoException; |
| 13 | +import com.amazonaws.encryptionsdk.exception.CannotUnwrapDataKeyException; |
| 14 | +import com.amazonaws.encryptionsdk.internal.Constants; |
| 15 | +import com.amazonaws.encryptionsdk.internal.TrailingSignatureAlgorithm; |
| 16 | +import com.amazonaws.encryptionsdk.model.DecryptionMaterials; |
| 17 | +import com.amazonaws.encryptionsdk.model.DecryptionMaterialsRequest; |
| 18 | +import com.amazonaws.encryptionsdk.model.EncryptionMaterials; |
| 19 | +import com.amazonaws.encryptionsdk.model.EncryptionMaterialsRequest; |
| 20 | +import com.amazonaws.encryptionsdk.model.KeyBlob; |
| 21 | +import java.security.GeneralSecurityException; |
| 22 | +import java.security.KeyPair; |
| 23 | +import java.security.PublicKey; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/* |
| 30 | + This is a copy-paste of the DefaultCryptoMaterialsManager implementation |
| 31 | + from the final commit of the V2 ESDK: 1870a082358d59e32c60d74116d6f43c0efa466b |
| 32 | + ESDK V3 implicitly changed the contract between CMMs and the ESDK. |
| 33 | + After V3, DecryptMaterials has an `encryptionContext` attribute, |
| 34 | + and CMMs are expected to set this attribute. |
| 35 | + The V3 commit modified this DefaultCMM's `decryptMaterials` implementation |
| 36 | + to set encryptionContext on returned DecryptionMaterials objects. |
| 37 | + However, there are custom implementations of the legacy native CMM |
| 38 | + that do not set encryptionContext. |
| 39 | + This CMM is used to explicitly assert that the V2 implementation of |
| 40 | + the DefaultCMM is compatible with V3 logic, |
| 41 | + which implicitly asserts that custom implementations of V2-compatible CMMs |
| 42 | + are also compatible with V3 logic. |
| 43 | +*/ |
| 44 | +public class V2DefaultCryptoMaterialsManager implements CryptoMaterialsManager { |
| 45 | + private final MasterKeyProvider<?> mkp; |
| 46 | + |
| 47 | + private final CryptoAlgorithm DEFAULT_CRYPTO_ALGORITHM = |
| 48 | + CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384; |
| 49 | + |
| 50 | + /** @param mkp The master key provider to delegate to */ |
| 51 | + public V2DefaultCryptoMaterialsManager(MasterKeyProvider<?> mkp) { |
| 52 | + assertNonNull(mkp, "mkp"); |
| 53 | + this.mkp = mkp; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public EncryptionMaterials getMaterialsForEncrypt(EncryptionMaterialsRequest request) { |
| 58 | + Map<String, String> context = request.getContext(); |
| 59 | + |
| 60 | + CryptoAlgorithm algo = request.getRequestedAlgorithm(); |
| 61 | + CommitmentPolicy commitmentPolicy = request.getCommitmentPolicy(); |
| 62 | + // Set default according to commitment policy |
| 63 | + if (algo == null && commitmentPolicy == CommitmentPolicy.ForbidEncryptAllowDecrypt) { |
| 64 | + algo = CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384; |
| 65 | + } else if (algo == null) { |
| 66 | + algo = CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384; |
| 67 | + } |
| 68 | + |
| 69 | + KeyPair trailingKeys = null; |
| 70 | + if (algo.getTrailingSignatureLength() > 0) { |
| 71 | + try { |
| 72 | + trailingKeys = generateTrailingSigKeyPair(algo); |
| 73 | + if (context.containsKey(Constants.EC_PUBLIC_KEY_FIELD)) { |
| 74 | + throw new IllegalArgumentException( |
| 75 | + "EncryptionContext contains reserved field " + Constants.EC_PUBLIC_KEY_FIELD); |
| 76 | + } |
| 77 | + // make mutable |
| 78 | + context = new HashMap<>(context); |
| 79 | + context.put(Constants.EC_PUBLIC_KEY_FIELD, serializeTrailingKeyForEc(algo, trailingKeys)); |
| 80 | + } catch (final GeneralSecurityException ex) { |
| 81 | + throw new AwsCryptoException(ex); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + final MasterKeyRequest.Builder mkRequestBuilder = MasterKeyRequest.newBuilder(); |
| 86 | + mkRequestBuilder.setEncryptionContext(context); |
| 87 | + |
| 88 | + mkRequestBuilder.setStreaming(request.getPlaintextSize() == -1); |
| 89 | + if (request.getPlaintext() != null) { |
| 90 | + mkRequestBuilder.setPlaintext(request.getPlaintext()); |
| 91 | + } else { |
| 92 | + mkRequestBuilder.setSize(request.getPlaintextSize()); |
| 93 | + } |
| 94 | + |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + final List<MasterKey> mks = |
| 97 | + (List<MasterKey>) |
| 98 | + assertNonNull(mkp, "provider").getMasterKeysForEncryption(mkRequestBuilder.build()); |
| 99 | + |
| 100 | + if (mks.isEmpty()) { |
| 101 | + throw new IllegalArgumentException("No master keys provided"); |
| 102 | + } |
| 103 | + |
| 104 | + DataKey<?> dataKey = mks.get(0).generateDataKey(algo, context); |
| 105 | + |
| 106 | + List<KeyBlob> keyBlobs = new ArrayList<>(mks.size()); |
| 107 | + keyBlobs.add(new KeyBlob(dataKey)); |
| 108 | + |
| 109 | + for (int i = 1; i < mks.size(); i++) { |
| 110 | + //noinspection unchecked |
| 111 | + keyBlobs.add(new KeyBlob(mks.get(i).encryptDataKey(algo, context, dataKey))); |
| 112 | + } |
| 113 | + |
| 114 | + //noinspection unchecked |
| 115 | + return EncryptionMaterials.newBuilder() |
| 116 | + .setAlgorithm(algo) |
| 117 | + .setCleartextDataKey(dataKey.getKey()) |
| 118 | + .setEncryptedDataKeys(keyBlobs) |
| 119 | + .setEncryptionContext(context) |
| 120 | + .setTrailingSignatureKey(trailingKeys == null ? null : trailingKeys.getPrivate()) |
| 121 | + .setMasterKeys(mks) |
| 122 | + .build(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public DecryptionMaterials decryptMaterials(DecryptionMaterialsRequest request) { |
| 127 | + DataKey<?> dataKey = |
| 128 | + mkp.decryptDataKey( |
| 129 | + request.getAlgorithm(), request.getEncryptedDataKeys(), request.getEncryptionContext()); |
| 130 | + |
| 131 | + if (dataKey == null) { |
| 132 | + throw new CannotUnwrapDataKeyException("Could not decrypt any data keys"); |
| 133 | + } |
| 134 | + |
| 135 | + PublicKey pubKey = null; |
| 136 | + if (request.getAlgorithm().getTrailingSignatureLength() > 0) { |
| 137 | + try { |
| 138 | + String serializedPubKey = request.getEncryptionContext().get(Constants.EC_PUBLIC_KEY_FIELD); |
| 139 | + |
| 140 | + if (serializedPubKey == null) { |
| 141 | + throw new AwsCryptoException("Missing trailing signature public key"); |
| 142 | + } |
| 143 | + |
| 144 | + pubKey = deserializeTrailingKeyFromEc(request.getAlgorithm(), serializedPubKey); |
| 145 | + } catch (final IllegalStateException ex) { |
| 146 | + throw new AwsCryptoException(ex); |
| 147 | + } |
| 148 | + } else if (request.getEncryptionContext().containsKey(Constants.EC_PUBLIC_KEY_FIELD)) { |
| 149 | + throw new AwsCryptoException("Trailing signature public key found for non-signed algorithm"); |
| 150 | + } |
| 151 | + |
| 152 | + return DecryptionMaterials.newBuilder() |
| 153 | + .setDataKey(dataKey) |
| 154 | + .setTrailingSignatureKey(pubKey) |
| 155 | + .build(); |
| 156 | + } |
| 157 | + |
| 158 | + private PublicKey deserializeTrailingKeyFromEc(CryptoAlgorithm algo, String pubKey) { |
| 159 | + return TrailingSignatureAlgorithm.forCryptoAlgorithm(algo).deserializePublicKey(pubKey); |
| 160 | + } |
| 161 | + |
| 162 | + private static String serializeTrailingKeyForEc(CryptoAlgorithm algo, KeyPair trailingKeys) { |
| 163 | + return TrailingSignatureAlgorithm.forCryptoAlgorithm(algo) |
| 164 | + .serializePublicKey(trailingKeys.getPublic()); |
| 165 | + } |
| 166 | + |
| 167 | + private static KeyPair generateTrailingSigKeyPair(CryptoAlgorithm algo) |
| 168 | + throws GeneralSecurityException { |
| 169 | + return TrailingSignatureAlgorithm.forCryptoAlgorithm(algo).generateKey(); |
| 170 | + } |
| 171 | +} |
0 commit comments