|
1 | 1 | package software.amazon.encryption.s3; |
2 | 2 |
|
3 | 3 | import software.amazon.awssdk.services.s3.S3Client; |
4 | | -import software.amazon.encryption.s3.algorithms.AlgorithmSuite; |
5 | 4 | import software.amazon.encryption.s3.internal.InstructionFileConfig; |
6 | | -import software.amazon.encryption.s3.S3EncryptionClient; |
| 5 | +import software.amazon.encryption.s3.algorithms.AlgorithmSuite; |
7 | 6 | import software.amazon.encryption.s3.materials.AesKeyring; |
8 | 7 | import software.amazon.encryption.s3.materials.Keyring; |
9 | 8 | import software.amazon.encryption.s3.materials.KmsKeyring; |
|
31 | 30 | import java.util.UUID; |
32 | 31 |
|
33 | 32 | import static software.amazon.encryption.s3.CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT; |
34 | | -import static software.amazon.encryption.s3.model.EncryptionAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY; |
| 33 | +import static software.amazon.encryption.s3.CommitmentPolicy.REQUIRE_ENCRYPT_ALLOW_DECRYPT; |
| 34 | +import static software.amazon.encryption.s3.CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT; |
35 | 35 |
|
36 | 36 | public class CreateClientOperationImpl implements CreateClientOperation { |
37 | | - private Map<String, S3Client> clientCache_; |
38 | | - |
39 | | - public CreateClientOperationImpl(Map<String, S3Client> clientCache) { |
40 | | - clientCache_ = clientCache; |
41 | | - } |
42 | | - |
43 | | - // Copied from S3EC. |
44 | | - private boolean onlyOneNonNull(Object... values) { |
45 | | - boolean haveOneNonNull = false; |
46 | | - for (Object o : values) { |
47 | | - if (o != null) { |
48 | | - if (haveOneNonNull) { |
49 | | - return false; |
| 37 | + private final Map<String, S3Client> clientCache_; |
| 38 | + |
| 39 | + public CreateClientOperationImpl(Map<String, S3Client> clientCache) { |
| 40 | + clientCache_ = clientCache; |
| 41 | + } |
| 42 | + |
| 43 | + // Copied from S3EC. |
| 44 | + private boolean onlyOneNonNull(Object... values) { |
| 45 | + boolean haveOneNonNull = false; |
| 46 | + for (Object o : values) { |
| 47 | + if (o != null) { |
| 48 | + if (haveOneNonNull) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + haveOneNonNull = true; |
| 53 | + } |
50 | 54 | } |
51 | 55 |
|
52 | | - haveOneNonNull = true; |
53 | | - } |
| 56 | + return haveOneNonNull; |
54 | 57 | } |
55 | 58 |
|
56 | | - return haveOneNonNull; |
57 | | - } |
58 | | - |
59 | | - @Override |
60 | | - public CreateClientOutput createClient(CreateClientInput input, RequestContext context) { |
61 | | - try { |
62 | | - KeyMaterial key = input.getConfig().getKeyMaterial(); |
63 | | - if (!onlyOneNonNull(key.getAesKey(), key.getKmsKeyId(), key.getRsaKey())) { |
64 | | - throw new RuntimeException("KeyMaterial must be only one, non-null input!"); |
65 | | - } |
66 | | - Keyring keyring; |
67 | | - if (key.getAesKey() != null) { |
68 | | - byte[] keyBytes = new byte[key.getAesKey().remaining()]; |
69 | | - key.getAesKey().get(keyBytes); |
70 | | - keyring = AesKeyring.builder() |
71 | | - .wrappingKey(new SecretKeySpec(keyBytes, "AES")) |
72 | | - .enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms()) |
73 | | - .build(); |
74 | | - } else if (key.getRsaKey() != null) { |
| 59 | + @Override |
| 60 | + public CreateClientOutput createClient(CreateClientInput input, RequestContext context) { |
75 | 61 | try { |
76 | | - byte[] keyBytes = new byte[key.getRsaKey().remaining()]; |
77 | | - key.getRsaKey().get(keyBytes); |
78 | | - PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); |
79 | | - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
80 | | - RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyFactory.generatePrivate(keySpec); |
81 | | - RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec( |
82 | | - privateKey.getModulus(), |
83 | | - privateKey.getPublicExponent() |
84 | | - ); |
85 | | - |
86 | | - // Generate public key |
87 | | - PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); |
88 | | - |
89 | | - keyring = RsaKeyring.builder() |
90 | | - .enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms()) |
91 | | - .wrappingKeyPair(PartialRsaKeyPair.builder() |
92 | | - .publicKey(publicKey) |
93 | | - .privateKey(privateKey).build()) |
94 | | - .build(); |
95 | | - } catch (NoSuchAlgorithmException | InvalidKeySpecException nse) { |
96 | | - throw GenericServerError.builder() |
97 | | - .message(nse.getMessage()) |
98 | | - .build(); |
| 62 | + KeyMaterial key = input.getConfig().getKeyMaterial(); |
| 63 | + if (!onlyOneNonNull(key.getAesKey(), key.getKmsKeyId(), key.getRsaKey())) { |
| 64 | + throw new RuntimeException("KeyMaterial must be only one, non-null input!"); |
| 65 | + } |
| 66 | + Keyring keyring; |
| 67 | + if (key.getAesKey() != null) { |
| 68 | + byte[] keyBytes = new byte[key.getAesKey().remaining()]; |
| 69 | + key.getAesKey().get(keyBytes); |
| 70 | + keyring = AesKeyring.builder() |
| 71 | + .wrappingKey(new SecretKeySpec(keyBytes, "AES")) |
| 72 | + .enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms()) |
| 73 | + .build(); |
| 74 | + } else if (key.getRsaKey() != null) { |
| 75 | + try { |
| 76 | + byte[] keyBytes = new byte[key.getRsaKey().remaining()]; |
| 77 | + key.getRsaKey().get(keyBytes); |
| 78 | + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); |
| 79 | + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 80 | + RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyFactory.generatePrivate(keySpec); |
| 81 | + RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec( |
| 82 | + privateKey.getModulus(), |
| 83 | + privateKey.getPublicExponent() |
| 84 | + ); |
| 85 | + |
| 86 | + // Generate public key |
| 87 | + PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); |
| 88 | + |
| 89 | + keyring = RsaKeyring.builder() |
| 90 | + .enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms()) |
| 91 | + .wrappingKeyPair(PartialRsaKeyPair.builder() |
| 92 | + .publicKey(publicKey) |
| 93 | + .privateKey(privateKey).build()) |
| 94 | + .build(); |
| 95 | + } catch (NoSuchAlgorithmException | InvalidKeySpecException nse) { |
| 96 | + throw GenericServerError.builder() |
| 97 | + .message(nse.getMessage()) |
| 98 | + .build(); |
| 99 | + } |
| 100 | + } else if (key.getKmsKeyId() != null) { |
| 101 | + keyring = KmsKeyring.builder() |
| 102 | + .enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms()) |
| 103 | + .wrappingKeyId(key.getKmsKeyId()) |
| 104 | + .build(); |
| 105 | + } else { |
| 106 | + throw new RuntimeException("No KeyMaterial found!"); |
| 107 | + } |
| 108 | + |
| 109 | + // V3 Transition server configuration |
| 110 | + // Existing Builder defaults to FORBID_ENCRYPT and ALG_AES_256_GCM_IV12_TAG16_NO_KDF |
| 111 | + S3EncryptionClient.Builder s3ClientBuilder = S3EncryptionClient.builder() |
| 112 | + .keyring(keyring) |
| 113 | + .enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms()) |
| 114 | + .enableLegacyUnauthenticatedModes(input.getConfig().isEnableLegacyUnauthenticatedModes()); |
| 115 | + |
| 116 | + // Instruction File Put Configuration |
| 117 | + boolean instFilePut = false; |
| 118 | + if (input.getConfig().getInstructionFileConfig() != null) { |
| 119 | + instFilePut = input.getConfig().getInstructionFileConfig().isEnableInstructionFilePutObject(); |
| 120 | + s3ClientBuilder.instructionFileConfig(InstructionFileConfig.builder() |
| 121 | + .instructionFileClient(S3Client.create()) |
| 122 | + .enableInstructionFilePutObject(instFilePut) |
| 123 | + .build()); |
| 124 | + } |
| 125 | + |
| 126 | + // Configure commitment policy if provided |
| 127 | + if (input.getConfig().getCommitmentPolicy() != null) { |
| 128 | + CommitmentPolicy policy = getCommitmentPolicy(input.getConfig().getCommitmentPolicy()); |
| 129 | + s3ClientBuilder.commitmentPolicy(policy); |
| 130 | + } |
| 131 | + |
| 132 | + // Configure encryption algorithm if provided |
| 133 | + if (input.getConfig().getEncryptionAlgorithm() != null) { |
| 134 | + AlgorithmSuite algorithm = getAlgorithmSuite(input.getConfig().getEncryptionAlgorithm()); |
| 135 | + s3ClientBuilder.encryptionAlgorithm(algorithm); |
| 136 | + } |
| 137 | + |
| 138 | + S3Client s3Client = s3ClientBuilder.build(); |
| 139 | + |
| 140 | + UUID uuid = UUID.randomUUID(); |
| 141 | + String uuidString = uuid.toString(); |
| 142 | + clientCache_.put(uuidString, s3Client); |
| 143 | + return CreateClientOutput.builder() |
| 144 | + .clientId(uuidString) |
| 145 | + .build(); |
| 146 | + } catch (Exception e) { |
| 147 | + StringWriter sw = new StringWriter(); |
| 148 | + e.printStackTrace(new PrintWriter(sw)); |
| 149 | + String stackTrace = sw.toString(); |
| 150 | + throw GenericServerError.builder() |
| 151 | + .message(stackTrace) |
| 152 | + .build(); |
99 | 153 | } |
100 | | - } else if (key.getKmsKeyId() != null) { |
101 | | - keyring = KmsKeyring.builder() |
102 | | - .enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms()) |
103 | | - .wrappingKeyId(key.getKmsKeyId()) |
104 | | - .build(); |
105 | | - } else { |
106 | | - throw new RuntimeException("No KeyMaterial found!"); |
107 | | - } |
108 | | - |
109 | | - boolean instFilePut = false; |
110 | | - if (input.getConfig().getInstructionFileConfig() != null) { |
111 | | - instFilePut = input.getConfig().getInstructionFileConfig().isEnableInstructionFilePutObject(); |
112 | | - } |
113 | | - |
114 | | - // V3-Transitional server configuration |
115 | | - S3EncryptionClient.Builder clientBuilder = S3EncryptionClient.builder() |
116 | | - .instructionFileConfig(InstructionFileConfig.builder() |
117 | | - .instructionFileClient(S3Client.create()) |
118 | | - .enableInstructionFilePutObject(instFilePut) |
119 | | - .build()) |
120 | | - .keyring(keyring); |
121 | | - |
122 | | - // Configure commitment policy if provided ( feature) |
123 | | - if (input.getConfig().getCommitmentPolicy() != null) { |
124 | | - CommitmentPolicy policy = getCommitmentPolicy(input); |
125 | | - clientBuilder.commitmentPolicy(policy); |
126 | | - } |
127 | | - // V3-Transitional default: No commitment policy (null) for backward compatibility |
128 | | - |
129 | | - // Configure encryption algorithm if provided ( feature) |
130 | | - if (input.getConfig().getEncryptionAlgorithm() != null) { |
131 | | - AlgorithmSuite algorithm = getAlgorithmSuite(input); |
132 | | - clientBuilder.encryptionAlgorithm(algorithm); |
133 | | - } else { |
134 | | - // V3-Transitional default: Legacy algorithm for backward compatibility |
135 | | - clientBuilder.encryptionAlgorithm(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF); |
136 | | - } |
137 | | - |
138 | | - S3Client s3Client = clientBuilder.build(); |
139 | | - UUID uuid = UUID.randomUUID(); |
140 | | - String uuidString = uuid.toString(); |
141 | | - clientCache_.put(uuidString, s3Client); |
142 | | - return CreateClientOutput.builder() |
143 | | - .clientId(uuidString) |
144 | | - .build(); |
145 | | - } catch (Exception e) { |
146 | | - StringWriter sw = new StringWriter(); |
147 | | - e.printStackTrace(new PrintWriter(sw)); |
148 | | - String stackTrace = sw.toString(); |
149 | | - throw GenericServerError.builder() |
150 | | - .message(stackTrace) |
151 | | - .build(); |
152 | 154 | } |
153 | | - } |
154 | | - |
155 | | - private static AlgorithmSuite getAlgorithmSuite(CreateClientInput input) { |
156 | | - if (input.getConfig().getEncryptionAlgorithm().equals(EncryptionAlgorithm.ALG_AES_256_CBC_IV16_NO_KDF)) { |
157 | | - return AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF; |
158 | | - } else if (input.getConfig().getEncryptionAlgorithm().equals(EncryptionAlgorithm.ALG_AES_256_GCM_IV12_TAG16_NO_KDF)) { |
159 | | - return AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF; |
160 | | - } else if (input.getConfig().getEncryptionAlgorithm().equals(EncryptionAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY)) { |
161 | | - return AlgorithmSuite.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY; |
162 | | - } else { |
163 | | - throw new RuntimeException("Unknown encryption algorithm: " + input.getConfig().getEncryptionAlgorithm()); |
| 155 | + |
| 156 | + private static AlgorithmSuite getAlgorithmSuite(EncryptionAlgorithm input) { |
| 157 | + if (input.equals(EncryptionAlgorithm.ALG_AES_256_CBC_IV16_NO_KDF)) { |
| 158 | + return AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF; |
| 159 | + } else if (input.equals(EncryptionAlgorithm.ALG_AES_256_GCM_IV12_TAG16_NO_KDF)) { |
| 160 | + return AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF; |
| 161 | + } else if (input.equals(EncryptionAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY)) { |
| 162 | + return AlgorithmSuite.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY; |
| 163 | + } else { |
| 164 | + throw new RuntimeException("Unknown encryption algorithm: " + input); |
| 165 | + } |
164 | 166 | } |
165 | | - } |
166 | | - |
167 | | - private static software.amazon.encryption.s3.CommitmentPolicy getCommitmentPolicy(CreateClientInput input) { |
168 | | - if (input.getConfig().getCommitmentPolicy().equals(software.amazon.encryption.s3.model.CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT)) { |
169 | | - return FORBID_ENCRYPT_ALLOW_DECRYPT; |
170 | | - } else if (input.getConfig().getCommitmentPolicy().equals(software.amazon.encryption.s3.model.CommitmentPolicy.REQUIRE_ENCRYPT_ALLOW_DECRYPT)) { |
171 | | - return null; |
172 | | - } else if (input.getConfig().getCommitmentPolicy().equals(software.amazon.encryption.s3.model.CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT)) { |
173 | | - return null; |
174 | | - } else { |
175 | | - throw new RuntimeException("Unknown commitment policy: " + input.getConfig().getCommitmentPolicy()); |
| 167 | + |
| 168 | + private static software.amazon.encryption.s3.CommitmentPolicy getCommitmentPolicy(software.amazon.encryption.s3.model.CommitmentPolicy input) { |
| 169 | + if (input.equals(software.amazon.encryption.s3.model.CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT)) { |
| 170 | + return FORBID_ENCRYPT_ALLOW_DECRYPT; |
| 171 | + } else if (input.equals(software.amazon.encryption.s3.model.CommitmentPolicy.REQUIRE_ENCRYPT_ALLOW_DECRYPT)) { |
| 172 | + return REQUIRE_ENCRYPT_ALLOW_DECRYPT; |
| 173 | + } else if (input.equals(software.amazon.encryption.s3.model.CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT)) { |
| 174 | + return REQUIRE_ENCRYPT_REQUIRE_DECRYPT; |
| 175 | + } else { |
| 176 | + throw new RuntimeException("Unknown commitment policy: " + input); |
| 177 | + } |
176 | 178 | } |
177 | | - } |
178 | 179 | } |
0 commit comments