-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateClientOperationImpl.java
More file actions
136 lines (125 loc) · 5.17 KB
/
Copy pathCreateClientOperationImpl.java
File metadata and controls
136 lines (125 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package software.amazon.encryption.s3;
import software.amazon.awssdk.core.traits.Trait;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.encryption.s3.S3EncryptionClient;
import software.amazon.encryption.s3.internal.InstructionFileConfig;
import software.amazon.encryption.s3.materials.AesKeyring;
import software.amazon.encryption.s3.materials.Keyring;
import software.amazon.encryption.s3.materials.KmsKeyring;
import software.amazon.encryption.s3.materials.PartialRsaKeyPair;
import software.amazon.encryption.s3.materials.RsaKeyring;
import software.amazon.smithy.java.core.schema.Schema;
import software.amazon.smithy.java.server.RequestContext;
import software.amazon.encryption.s3.model.CreateClientInput;
import software.amazon.encryption.s3.model.CreateClientOutput;
import software.amazon.encryption.s3.model.GenericServerError;
import software.amazon.encryption.s3.model.KeyMaterial;
import software.amazon.encryption.s3.service.CreateClientOperation;
import javax.crypto.spec.SecretKeySpec;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class CreateClientOperationImpl implements CreateClientOperation {
private Map<String, S3Client> clientCache_;
public CreateClientOperationImpl(Map<String, S3Client> clientCache) {
clientCache_ = clientCache;
}
// Copied from S3EC.
private boolean onlyOneNonNull(Object... values) {
boolean haveOneNonNull = false;
for (Object o : values) {
if (o != null) {
if (haveOneNonNull) {
return false;
}
haveOneNonNull = true;
}
}
return haveOneNonNull;
}
@Override
public CreateClientOutput createClient(CreateClientInput input, RequestContext context) {
try {
// Key Material / Keyring Creation
KeyMaterial key = input.getConfig().getKeyMaterial();
if (!onlyOneNonNull(key.getAesKey(), key.getKmsKeyId(), key.getRsaKey())) {
throw new RuntimeException("KeyMaterial must be only one, non-null input!");
}
Keyring keyring;
if (key.getAesKey() != null) {
byte[] keyBytes = new byte[key.getAesKey().remaining()];
key.getAesKey().get(keyBytes);
keyring = AesKeyring.builder()
.wrappingKey(new SecretKeySpec(keyBytes, "AES"))
.enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms())
.build();
} else if (key.getRsaKey() != null) {
try {
byte[] keyBytes = new byte[key.getRsaKey().remaining()];
key.getRsaKey().get(keyBytes);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyFactory.generatePrivate(keySpec);
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(
privateKey.getModulus(),
privateKey.getPublicExponent()
);
// Generate public key
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
keyring = RsaKeyring.builder()
.enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms())
.wrappingKeyPair(PartialRsaKeyPair.builder()
.publicKey(publicKey)
.privateKey(privateKey).build())
.build();
} catch (NoSuchAlgorithmException | InvalidKeySpecException nse) {
throw GenericServerError.builder()
.message(nse.getMessage())
.build();
}
} else if (key.getKmsKeyId() != null) {
keyring = KmsKeyring.builder()
.enableLegacyWrappingAlgorithms(input.getConfig().isEnableLegacyWrappingAlgorithms())
.wrappingKeyId(key.getKmsKeyId())
.build();
} else {
throw new RuntimeException("No KeyMaterial found!");
}
// Client Creation
boolean instFilePut = false;
if (input.getConfig().getInstructionFileConfig() != null) {
instFilePut = input.getConfig().getInstructionFileConfig().isEnableInstructionFilePutObject();
}
S3Client s3Client = S3EncryptionClient.builder()
.instructionFileConfig(InstructionFileConfig.builder()
.instructionFileClient(S3Client.create())
.enableInstructionFilePutObject(instFilePut)
.build())
.keyring(keyring)
.build();
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString();
clientCache_.put(uuidString, s3Client);
return CreateClientOutput.builder()
.clientId(uuidString)
.build();
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();
throw GenericServerError.builder()
.message(stackTrace)
.build();
}
}
}