-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDHKeyAgreement.java
More file actions
332 lines (287 loc) · 13 KB
/
DHKeyAgreement.java
File metadata and controls
332 lines (287 loc) · 13 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
/*
* Copyright IBM Corp. 2023, 2026
*
* This code is free software; you can redistribute it and/or modify it
* under the terms provided by IBM in the LICENSE file that accompanied
* this code, including the "Classpath" Exception described therein.
*/
package com.ibm.crypto.plus.provider;
import com.ibm.crypto.plus.provider.base.DHKey;
import com.ibm.crypto.plus.provider.base.NativeCryptoSelector;
import com.ibm.crypto.plus.provider.base.NativeException;
import com.ibm.crypto.plus.provider.base.NativeInterface;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.KeyAgreementSpi;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.security.util.KeyUtil;
public final class DHKeyAgreement extends KeyAgreementSpi {
private OpenJCEPlusProvider provider = null;
private NativeInterface nativeInterface;
private boolean generateSecret = false;
private BigInteger init_p = null;
private BigInteger init_g = null;
private BigInteger x; // the private value
private BigInteger y;
private DHKey ockDHKeyPub = null;
private DHKey ockDHKeyPriv = null;
private DHPublicKey dhPublicKey = null;
private DHPrivateKey dhPrivateKey = null;
private static class AllowKDF {
private static final boolean VALUE = getValue();
private static boolean getValue() {
return Boolean.parseBoolean(
System.getProperty("jdk.crypto.KeyAgreement.legacyKDF", "false"));
}
}
public DHKeyAgreement(OpenJCEPlusProvider provider) {
this.provider = provider;
this.nativeInterface = NativeCryptoSelector.selectBackend(provider, "KeyAgreement", "DiffieHellman");
}
@Override
protected Key engineDoPhase(Key key, boolean lastPhase)
throws InvalidKeyException, IllegalStateException {
if (!(key instanceof javax.crypto.interfaces.DHPublicKey)) {
throw new InvalidKeyException("Key is not a DHPublicKey");
}
javax.crypto.interfaces.DHPublicKey dhPubKey;
dhPubKey = (javax.crypto.interfaces.DHPublicKey) key;
if (init_p == null || init_g == null) {
throw new IllegalStateException("Not initialized");
}
// check if public key parameters are compatible with
// initialized ones
BigInteger pub_p = dhPubKey.getParams().getP();
BigInteger pub_g = dhPubKey.getParams().getG();
if (pub_p != null && !(init_p.equals(pub_p))) {
throw new InvalidKeyException("Incompatible parameters");
}
if (pub_g != null && !(init_g.equals(pub_g))) {
throw new InvalidKeyException("Incompatible parameters");
}
// validate the Diffie-Hellman public key
KeyUtil.validate(dhPubKey);
// store the y value
this.y = dhPubKey.getY();
if (!(dhPubKey instanceof com.ibm.crypto.plus.provider.DHPublicKey)) {
dhPublicKey = new DHPublicKey(provider, dhPubKey.getEncoded());
if (dhPublicKey.getY().compareTo(this.y) != 0) {
throw new InvalidKeyException("Public keys do not match");
}
} else {
dhPublicKey = new DHPublicKey(provider, dhPubKey.getEncoded());
}
ockDHKeyPub = dhPublicKey.getOCKKey();
// we've received a public key (from one of the other parties),
// so we are ready to create the secret, which may be an
// intermediate secret, in which case we wrap it into a
// Diffie-Hellman public key object and return it.
generateSecret = true;
if (lastPhase == false) {
throw new IllegalStateException("DH can only be between two parties.");
} else {
return null;
}
}
// There is a double lock on ockDHKeyPub and ockDHKeyPriv to ensure that the underlying native
// pointers are not concurrently used by another DH operation. This is needed as the method
// DHKey.computeDHSecret is not synchronized and not thread safe.
// The method DHKey.computeDHSecret should NOT be synchronized for performance as that would create a global lock.
@Override
protected byte[] engineGenerateSecret() throws IllegalStateException {
if (generateSecret == false) {
throw new IllegalStateException("Wrong state");
}
// Reset the key agreement here (in case anything goes wrong)
generateSecret = false;
byte[] secret = null;
try {
if (ockDHKeyPub == null) {
throw new IllegalStateException("ockDHKeyPub is null");
}
if (ockDHKeyPriv == null) {
throw new IllegalStateException("ockDHKeyPriv is null");
}
// Establish an order for which key to lock first based on their hashcode to avoid a deadlock
DHKey locker1;
DHKey locker2;
if (System.identityHashCode(ockDHKeyPub) < System.identityHashCode(ockDHKeyPriv)) {
locker1 = ockDHKeyPub;
locker2 = ockDHKeyPriv;
} else {
locker1 = ockDHKeyPriv;
locker2 = ockDHKeyPub;
}
synchronized (locker1) {
synchronized (locker2) {
secret = DHKey.computeDHSecret(this.nativeInterface,
ockDHKeyPub.getDHKeyId(), ockDHKeyPriv.getDHKeyId());
}
}
} catch (NativeException e) {
throw new IllegalStateException("engineGenerateSecret failed", e);
}
// Make the computed secert compatible with IBMJCE provider
BigInteger modulus = init_p;
int expectedLen = (modulus.bitLength() + 7) >>> 3;
// BigInteger.toByteArray will sometimes put a sign byte up front.
// However, Keys are always positive, and the above sign bit isn't
// actually used when representing keys. To obtain an array containing
// exactly expectedLen bytes of magnitude, we strip any extra
// leading 0's, or pad with 0's in case of a "short" secret.
// This requirement can be found in RFC2631 2.1.2
if (secret.length == expectedLen) {
return secret;
} else {
byte[] result = new byte[expectedLen];
// Array too short, pad it w/ leading 0s
if (secret.length < expectedLen) {
System.arraycopy(secret, 0, result, (expectedLen - secret.length), secret.length);
} else {
// Array too long, check and trim off the excess
if ((secret.length == (expectedLen + 1)) && secret[0] == 0) {
// ignore the leading sign byte
System.arraycopy(secret, 1, result, 0, expectedLen);
} else {
throw provider.providerException("Failed to generate secret",
new NativeException("secret is out-of-range"));
}
}
return result;
}
}
@Override
protected SecretKey engineGenerateSecret(String algorithm)
throws IllegalStateException, NoSuchAlgorithmException, InvalidKeyException {
if (algorithm == null) {
throw new NoSuchAlgorithmException("null algorithm");
}
if (!(algorithm.equalsIgnoreCase("TlsPremasterSecret")
|| algorithm.equalsIgnoreCase("Generic"))
&& !AllowKDF.VALUE) {
throw new NoSuchAlgorithmException(
"Unsupported secret key algorithm: " + algorithm);
}
byte[] secret = engineGenerateSecret();
if (algorithm.equalsIgnoreCase("DESede") || algorithm.equalsIgnoreCase("TripleDES")) {
// Triple DES
return new DESedeKey(provider, secret);
} else if (algorithm.equalsIgnoreCase("AES")) {
// AES
int keysize = secret.length;
SecretKeySpec skey = null;
int idx = AESConstants.AES_KEYSIZES.length - 1;
while (skey == null && idx >= 0) {
// Generate the strongest key using the shared secret
// assuming the key sizes in AESConstants class are
// in ascending order
if (keysize >= AESConstants.AES_KEYSIZES[idx]) {
keysize = AESConstants.AES_KEYSIZES[idx];
skey = new SecretKeySpec(secret, 0, keysize, "AES");
}
idx--;
}
if (skey == null) {
throw new InvalidKeyException("Key material is too short");
}
return skey;
} else if (algorithm.equalsIgnoreCase("TlsPremasterSecret")) {
// remove leading zero bytes per RFC 5246 Section 8.1.2
return new SecretKeySpec(
KeyUtil.trimZeroes(secret), "TlsPremasterSecret");
} else if (algorithm.equalsIgnoreCase("Generic")) {
return new SecretKeySpec(secret, algorithm);
} else {
throw new NoSuchAlgorithmException(
"Unsupported secret key algorithm: " + algorithm);
}
}
@Override
protected int engineGenerateSecret(byte[] sharedSecret, int offset)
throws IllegalStateException, ShortBufferException {
if (generateSecret == false) {
throw new IllegalStateException("Wrong state");
}
if (sharedSecret == null) {
throw new ShortBufferException("No buffer provided for shared secret");
}
byte[] secret = engineGenerateSecret();
if ((sharedSecret.length - offset) < secret.length) {
// allow user to regenerate secret with larger buffer
generateSecret = true;
throw new ShortBufferException("Buffer too short for shared secret");
}
System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
return secret.length;
}
@Override
protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException {
try {
engineInit(key, null, random);
} catch (InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException(
"DHKeyAgreement init failed with no AlgorithmParameterSpec specified");
}
}
@Override
protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException {
// ignore "random" parameter, because our implementation does not
// require any source of randomness
generateSecret = false;
init_p = null;
init_g = null;
if (params != null && !(params instanceof DHParameterSpec)) {
throw new InvalidAlgorithmParameterException("Diffie-Hellman parameters expected");
}
if (!(key instanceof javax.crypto.interfaces.DHPrivateKey)) {
throw new InvalidKeyException("Key is not a DHPrivateKey");
}
javax.crypto.interfaces.DHPrivateKey dhPrivKey;
dhPrivKey = (javax.crypto.interfaces.DHPrivateKey) key;
// check if private key parameters are compatible with
// initialized ones
if (params != null) {
init_p = ((DHParameterSpec) params).getP();
init_g = ((DHParameterSpec) params).getG();
}
BigInteger priv_p = dhPrivKey.getParams().getP();
BigInteger priv_g = dhPrivKey.getParams().getG();
if (init_p != null && priv_p != null && !(init_p.equals(priv_p))) {
throw new InvalidKeyException("Incompatible parameters");
}
if (init_g != null && priv_g != null && !(init_g.equals(priv_g))) {
throw new InvalidKeyException("Incompatible parameters");
}
if ((init_p == null && priv_p == null) || (init_g == null && priv_g == null)) {
throw new InvalidKeyException("Missing parameters");
}
init_p = priv_p;
init_g = priv_g;
// store the x value
this.x = dhPrivKey.getX();
if (!(dhPrivKey instanceof com.ibm.crypto.plus.provider.DHPrivateKey)) {
// Use this constructor to preserve the public key bytes
try {
this.dhPrivateKey = new com.ibm.crypto.plus.provider.DHPrivateKey(provider,
dhPrivKey.getEncoded());
} catch (IOException e) {
throw new InvalidKeyException("Error constructing DHPrivateKey", e);
}
if (this.dhPrivateKey.getX().compareTo(this.x) != 0) {
throw new InvalidKeyException("Private keys do not match");
}
} else
this.dhPrivateKey = (com.ibm.crypto.plus.provider.DHPrivateKey) dhPrivKey;
this.ockDHKeyPriv = dhPrivateKey.getOCKKey();
}
}