-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathHyperwalletEncryption.java
More file actions
406 lines (347 loc) · 15.6 KB
/
HyperwalletEncryption.java
File metadata and controls
406 lines (347 loc) · 15.6 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package com.hyperwallet.clientsdk.util;
import com.hyperwallet.clientsdk.HyperwalletException;
import com.nimbusds.jose.Algorithm;
import com.nimbusds.jose.EncryptionMethod;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.JWEDecrypter;
import com.nimbusds.jose.JWEEncrypter;
import com.nimbusds.jose.JWEHeader;
import com.nimbusds.jose.JWEObject;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSObject;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.JWSVerifier;
import com.nimbusds.jose.Payload;
import com.nimbusds.jose.crypto.ECDHDecrypter;
import com.nimbusds.jose.crypto.ECDHEncrypter;
import com.nimbusds.jose.crypto.ECDSASigner;
import com.nimbusds.jose.crypto.ECDSAVerifier;
import com.nimbusds.jose.crypto.RSADecrypter;
import com.nimbusds.jose.crypto.RSAEncrypter;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jose.crypto.RSASSAVerifier;
import com.nimbusds.jose.jwk.ECKey;
import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.KeyType;
import com.nimbusds.jose.jwk.RSAKey;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
public class HyperwalletEncryption {
private static final String EXPIRATION = "exp";
private static final Integer MILLISECONDS_IN_ONE_MINUTE = 60000;
private static final Long MILLISECONDS_IN_SECOND = 1000L;
private static final Integer EXPIRATION_MINUTES = 5;
private static final JWEAlgorithm ENCRYPTION_ALGORITHM = JWEAlgorithm.RSA_OAEP_256;
private static final JWSAlgorithm SIGN_ALGORITHM = JWSAlgorithm.RS256;
private static final EncryptionMethod ENCRYPTION_METHOD = EncryptionMethod.A256CBC_HS512;
private static final String INVALID_KEY_TYPE_STRING = "'kty' not supported = %s";
private static final List<JWEAlgorithm> SUPPORTED_JWE_ALGORITHMS = Arrays.asList(JWEAlgorithm.RSA_OAEP_256,
JWEAlgorithm.ECDH_ES,
JWEAlgorithm.ECDH_ES_A128KW,
JWEAlgorithm.ECDH_ES_A192KW,
JWEAlgorithm.ECDH_ES_A256KW);
private static final List<JWSAlgorithm> SUPPORTED_JWS_ALGORITHMS = Arrays.asList(JWSAlgorithm.RS256,
JWSAlgorithm.RS384,
JWSAlgorithm.RS512,
JWSAlgorithm.PS256,
JWSAlgorithm.PS384,
JWSAlgorithm.PS512,
JWSAlgorithm.ES256,
JWSAlgorithm.ES384,
JWSAlgorithm.ES512);
private static final List<EncryptionMethod> SUPPORTED_ENCRYPTION_METHODS = Arrays.asList(EncryptionMethod.A128CBC_HS256,
EncryptionMethod.A192CBC_HS384,
EncryptionMethod.A256CBC_HS512,
EncryptionMethod.A128GCM,
EncryptionMethod.A256GCM);
private final JWEAlgorithm encryptionAlgorithm;
private final JWSAlgorithm signAlgorithm;
private final EncryptionMethod encryptionMethod;
private final String clientPrivateKeySetLocation;
private final String hyperwalletKeySetLocation;
private final Integer jwsExpirationMinutes;
private Proxy proxy;
private String proxyUsername;
private String proxyPassword;
public HyperwalletEncryption(JWEAlgorithm encryptionAlgorithm, JWSAlgorithm signAlgorithm, EncryptionMethod encryptionMethod,
String clientPrivateKeySetLocation, String hyperwalletKeySetLocation, Integer jwsExpirationMinutes) {
this.encryptionAlgorithm = encryptionAlgorithm == null ? ENCRYPTION_ALGORITHM : encryptionAlgorithm;
this.signAlgorithm = signAlgorithm == null ? SIGN_ALGORITHM : signAlgorithm;
this.encryptionMethod = encryptionMethod == null ? ENCRYPTION_METHOD : encryptionMethod;
this.clientPrivateKeySetLocation = clientPrivateKeySetLocation;
this.hyperwalletKeySetLocation = hyperwalletKeySetLocation;
this.jwsExpirationMinutes = jwsExpirationMinutes == null ? EXPIRATION_MINUTES : jwsExpirationMinutes;
if (!SUPPORTED_JWS_ALGORITHMS.contains(this.signAlgorithm)) {
throw new IllegalArgumentException("Unsupported signing algorithm " + this.signAlgorithm);
}
if (!SUPPORTED_JWE_ALGORITHMS.contains(this.encryptionAlgorithm)) {
throw new IllegalArgumentException("Unsupported encryption algorithm " + this.encryptionAlgorithm);
}
if (!SUPPORTED_ENCRYPTION_METHODS.contains(this.encryptionMethod)) {
throw new IllegalArgumentException("Unsupported encryption method " + this.encryptionMethod);
}
}
public String encrypt(String body) throws JOSEException, IOException, ParseException {
JWK clientPrivateKey = getKeyByAlgorithm(loadClientPrivateKeySet(), signAlgorithm);
JWK hyperwalletPublicKey = getKeyByAlgorithm(loadHyperwalletKeySet(), encryptionAlgorithm);
JWSSigner jwsSigner = getJWSSigner(clientPrivateKey);
JWEEncrypter jweEncrypter = getJWEEncrypter(hyperwalletPublicKey);
JWSObject jwsObject = new JWSObject(
new JWSHeader.Builder(signAlgorithm)
.keyID(clientPrivateKey.getKeyID())
.criticalParams(new HashSet<>(Collections.singletonList(EXPIRATION)))
.customParam(EXPIRATION, getJWSExpirationMillis()).build(),
new Payload(body));
jwsObject.sign(jwsSigner);
JWEObject jweObject = new JWEObject(
new JWEHeader.Builder(encryptionAlgorithm, encryptionMethod)
.keyID(hyperwalletPublicKey.getKeyID()).build(),
new Payload(jwsObject));
jweObject.encrypt(jweEncrypter);
return jweObject.serialize();
}
public String decrypt(String body) throws ParseException, IOException, JOSEException {
JWK privateKeyToDecrypt = getKeyByAlgorithm(loadClientPrivateKeySet(), encryptionAlgorithm);
JWK publicKeyToSign = getKeyByAlgorithm(loadHyperwalletKeySet(), signAlgorithm);
JWEDecrypter jweDecrypter = getJWEDecrypter(privateKeyToDecrypt);
JWSVerifier jwsVerifier = getJWSVerifier(publicKeyToSign);
JWEObject jweObject = JWEObject.parse(body);
jweObject.decrypt(jweDecrypter);
JWSObject jwsObject = jweObject.getPayload().toJWSObject();
verifySignatureExpirationDate(jwsObject.getHeader().getCustomParam(EXPIRATION));
boolean verifyStatus = jwsObject.verify(jwsVerifier);
if (!verifyStatus) {
throw new HyperwalletException("JWS signature is incorrect");
}
return jwsObject.getPayload().toString();
}
/**
* Allows clients to implement a custom loading of their private JWK set.
*/
protected JWKSet loadClientPrivateKeySet() throws IOException, ParseException {
return loadKeySet(clientPrivateKeySetLocation);
}
/**
* Allows clients to implement a custom loading of Hyperwallet public JWK set.
*/
protected JWKSet loadHyperwalletKeySet() throws IOException, ParseException {
return loadKeySet(hyperwalletKeySetLocation);
}
public void verifySignatureExpirationDate(Object signatureExpirationDate) {
if (signatureExpirationDate == null) {
throw new HyperwalletException("exp JWS header param was null");
}
if (!(signatureExpirationDate instanceof Long)) {
throw new HyperwalletException("exp JWS header must be of type Long");
}
long expirationTimeSeconds = (long) signatureExpirationDate;
if (new Date().getTime() / MILLISECONDS_IN_SECOND > expirationTimeSeconds) {
throw new HyperwalletException("Response message signature(JWS) has expired");
}
}
public JWEAlgorithm getEncryptionAlgorithm() {
return encryptionAlgorithm;
}
public JWSAlgorithm getSignAlgorithm() {
return signAlgorithm;
}
public EncryptionMethod getEncryptionMethod() {
return encryptionMethod;
}
public String getClientPrivateKeySetLocation() {
return clientPrivateKeySetLocation;
}
public String getHyperwalletKeySetLocation() {
return hyperwalletKeySetLocation;
}
public Integer getJwsExpirationMinutes() {
return jwsExpirationMinutes;
}
private JWKSet loadKeySet(String keySetLocation) throws IOException, ParseException {
URL url;
try {
url = new URL(keySetLocation);
} catch (MalformedURLException e) {
checkKeySetLocationIsFile(keySetLocation);
return JWKSet.load(new File(keySetLocation));
}
if (usesProxy()) {
if (proxyUsername != null && proxyPassword != null) {
Authenticator authenticator = new Request.DefaultPasswordAuthenticator(
proxyUsername, proxyPassword);
Authenticator.setDefault(authenticator);
}
return JWKSet.load(url, 0, 0, 0, getProxy());
}
return JWKSet.load(url);
}
private long getJWSExpirationMillis() {
return new Date(new Date().getTime() + MILLISECONDS_IN_ONE_MINUTE * jwsExpirationMinutes).getTime() / MILLISECONDS_IN_SECOND;
}
private <T extends Algorithm> JWK getKeyByAlgorithm(JWKSet keySet, T algorithm) {
for (JWK key : keySet.getKeys()) {
if (key.getAlgorithm().equals(algorithm)) {
return key;
}
}
throw new IllegalStateException("Algorithm = " + algorithm + " is not found in client or Hyperwallet key set");
}
private void checkKeySetLocationIsFile(String keySetLocation) {
if (Files.notExists(Paths.get(keySetLocation))) {
throw new IllegalArgumentException("Wrong client JWK set location");
}
}
protected JWSSigner getJWSSigner(JWK jwk) {
try {
KeyType kty = jwk.getKeyType();
if (kty.equals(KeyType.RSA)) {
return new RSASSASigner((RSAKey) jwk);
} else if (kty.equals(KeyType.EC)) {
return new ECDSASigner((ECKey) jwk);
} else {
throw new IllegalArgumentException(String.format(INVALID_KEY_TYPE_STRING, kty));
}
} catch (JOSEException e) {
throw new HyperwalletException("Unable to create signer");
}
}
protected JWEEncrypter getJWEEncrypter(JWK jwk) {
try {
KeyType kty = jwk.getKeyType();
if (kty.equals(KeyType.RSA)) {
return new RSAEncrypter((RSAKey) jwk);
} else if (kty.equals(KeyType.EC)) {
return new ECDHEncrypter((ECKey) jwk);
} else {
throw new IllegalArgumentException(String.format(INVALID_KEY_TYPE_STRING, kty));
}
} catch (JOSEException e) {
throw new HyperwalletException("Unable to create encrypter");
}
}
protected JWSVerifier getJWSVerifier(JWK jwk) {
try {
KeyType kty = jwk.getKeyType();
if (kty.equals(KeyType.RSA)) {
return new RSASSAVerifier(((RSAKey) jwk).toRSAPublicKey(), new HashSet<>(Collections.singletonList(EXPIRATION)));
} else if (kty.equals(KeyType.EC)) {
return new ECDSAVerifier(((ECKey) jwk).toECPublicKey(), new HashSet<>(Collections.singletonList(EXPIRATION)));
} else {
throw new IllegalArgumentException(String.format(INVALID_KEY_TYPE_STRING, kty));
}
} catch (JOSEException e) {
throw new HyperwalletException("Unable to create verifier");
}
}
protected JWEDecrypter getJWEDecrypter(JWK jwk) {
try {
KeyType kty = jwk.getKeyType();
if (kty.equals(KeyType.RSA)) {
return new RSADecrypter((RSAKey) jwk);
} else if (kty.equals(KeyType.EC)) {
return new ECDHDecrypter((ECKey) jwk);
} else {
throw new IllegalArgumentException(String.format(INVALID_KEY_TYPE_STRING, kty));
}
} catch (JOSEException e) {
throw new HyperwalletException("Unable to create decrypter");
}
}
public Boolean usesProxy() {
return proxy != null;
}
public void setProxy(String url, Integer port) {
this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(url, port));
}
public void setProxy(Proxy proxy) {
this.proxy = proxy;
}
public Proxy getProxy() {
return proxy;
}
public String getProxyUsername() {
return proxyUsername;
}
public void setProxyUsername(String proxyUsername) {
this.proxyUsername = proxyUsername;
}
public String getProxyPassword() {
return proxyPassword;
}
public void setProxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword;
}
public static class HyperwalletEncryptionBuilder {
private JWEAlgorithm encryptionAlgorithm;
private JWSAlgorithm signAlgorithm;
private EncryptionMethod encryptionMethod;
private String clientPrivateKeySetLocation;
private String hyperwalletKeySetLocation;
private Integer jwsExpirationMinutes;
private Proxy proxy;
private String proxyUsername;
private String proxyPassword;
public HyperwalletEncryptionBuilder encryptionAlgorithm(JWEAlgorithm encryptionAlgorithm) {
this.encryptionAlgorithm = encryptionAlgorithm;
return this;
}
public HyperwalletEncryptionBuilder signAlgorithm(JWSAlgorithm signAlgorithm) {
this.signAlgorithm = signAlgorithm;
return this;
}
public HyperwalletEncryptionBuilder encryptionMethod(EncryptionMethod encryptionMethod) {
this.encryptionMethod = encryptionMethod;
return this;
}
public HyperwalletEncryptionBuilder clientPrivateKeySetLocation(String clientPrivateKeySetLocation) {
this.clientPrivateKeySetLocation = clientPrivateKeySetLocation;
return this;
}
public HyperwalletEncryptionBuilder hyperwalletKeySetLocation(String hyperwalletKeySetLocation) {
this.hyperwalletKeySetLocation = hyperwalletKeySetLocation;
return this;
}
public HyperwalletEncryptionBuilder jwsExpirationMinutes(Integer jwsExpirationMinutes) {
this.jwsExpirationMinutes = jwsExpirationMinutes;
return this;
}
public HyperwalletEncryptionBuilder proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public HyperwalletEncryptionBuilder proxyUsername(String proxyUsername) {
this.proxyUsername = proxyUsername;
return this;
}
public HyperwalletEncryptionBuilder proxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword;
return this;
}
public HyperwalletEncryption build() {
HyperwalletEncryption hyperwalletEncryption = new HyperwalletEncryption(encryptionAlgorithm, signAlgorithm, encryptionMethod,
clientPrivateKeySetLocation, hyperwalletKeySetLocation, jwsExpirationMinutes);
if (proxy != null) {
hyperwalletEncryption.setProxy(proxy);
}
if (proxyUsername != null) {
hyperwalletEncryption.setProxyUsername(proxyUsername);
}
if (proxyPassword != null) {
hyperwalletEncryption.setProxyPassword(proxyPassword);
}
return hyperwalletEncryption;
}
}
}