-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathEc2Key.java
More file actions
305 lines (268 loc) · 10.2 KB
/
Ec2Key.java
File metadata and controls
305 lines (268 loc) · 10.2 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
package com.google.cose;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.NegativeInteger;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.google.cose.exceptions.CoseException;
import com.google.cose.utils.Algorithm;
import com.google.cose.utils.CborUtils;
import com.google.cose.utils.CoseUtils;
import com.google.cose.utils.Headers;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECPoint;
/**
* Abstract class for generic Ec2 key
*/
public abstract class Ec2Key extends CoseKey {
private static final int SIGN_POSITIVE = 1;
private int curve;
protected KeyPair keyPair;
Ec2Key(DataItem cborKey) throws CborException, CoseException {
super(cborKey);
populateKeyFromCbor();
}
void populateKeyFromCbor() throws CborException, CoseException {
if (getKeyType() != Headers.KEY_TYPE_EC2) {
throw new CoseException("Expecting EC2 key (type 2), found type " + getKeyType());
}
// Get curve information
curve = CborUtils.asInteger(labels.get(Headers.KEY_PARAMETER_CURVE));
// Get private key.
final ECPrivateKey privateKey;
if (labels.containsKey(Headers.KEY_PARAMETER_D)) {
byte[] key = CborUtils.asByteString(labels.get(Headers.KEY_PARAMETER_D)).getBytes();
if (key.length == 0) {
throw new CoseException("Cannot decode private key. Missing coordinate information.");
}
privateKey = CoseUtils.getEc2PrivateKeyFromInteger(curve, new BigInteger(SIGN_POSITIVE, key));
} else {
privateKey = null;
}
if (!labels.containsKey(Headers.KEY_PARAMETER_X)) {
if (privateKey == null) {
throw new CoseException(CoseException.MISSING_KEY_MATERIAL_EXCEPTION_MESSAGE);
} else {
keyPair = new KeyPair(
CoseUtils.getEc2PublicKeyFromPrivateKey(curve, privateKey),
privateKey);
return;
}
}
final ByteString xCor = CborUtils.asByteString(labels.get(Headers.KEY_PARAMETER_X));
// Get the public key for EC2 key.
// We should not have a case where x is provided but y is not.
if (!labels.containsKey(Headers.KEY_PARAMETER_Y)) {
throw new IllegalStateException("X coordinate provided but Y coordinate is missing.");
}
final ByteString yCor = CborUtils.asByteString(labels.get(Headers.KEY_PARAMETER_Y));
final PublicKey publicKey = CoseUtils.getEc2PublicKeyFromCoordinates(
curve,
new BigInteger(SIGN_POSITIVE, xCor.getBytes()),
new BigInteger(SIGN_POSITIVE, yCor.getBytes())
);
keyPair = new KeyPair(publicKey, privateKey);
}
public ECPublicKey getPublicKey() {
return (ECPublicKey) this.keyPair.getPublic();
}
public abstract Ec2Key getPublic() throws CborException, CoseException;
public int getCurve() {
return curve;
}
void verifyAlgorithmAllowedByKey(Algorithm algorithm) throws CborException, CoseException {
Map keyMap = CborUtils.asMap(encode());
int curve = CborUtils.asInteger(keyMap.get(new NegativeInteger(Headers.KEY_PARAMETER_CURVE)));
boolean compatible;
switch (curve) {
case Headers.CURVE_EC2_P256:
compatible = algorithm == Algorithm.SIGNING_ALGORITHM_ECDSA_SHA_256;
break;
case Headers.CURVE_EC2_P384:
compatible = (algorithm == Algorithm.SIGNING_ALGORITHM_ECDSA_SHA_384);
break;
case Headers.CURVE_EC2_P521:
compatible = (algorithm == Algorithm.SIGNING_ALGORITHM_ECDSA_SHA_512);
break;
default:
throw new CoseException("Unsupported curve.");
}
if (!compatible) {
throw new CoseException("Algorithm not compatible with Ec2 key.");
}
}
// Big endian: Do not reuse for little endian encodings
private static byte[] arrayFromBigNum(BigInteger num, int keySize)
throws IllegalArgumentException {
// Roundup arithmetic from bits to bytes.
byte[] keyBytes = new byte[(keySize + 7) / 8];
byte[] keyBytes2 = num.toByteArray();
if (keyBytes.length == keyBytes2.length) {
return keyBytes2;
}
if (keyBytes2.length > keyBytes.length) {
// There should be no more than one padding(0) byte, invalid key otherwise.
if (keyBytes2.length - keyBytes.length > 1 && keyBytes2[0] != 0) {
throw new IllegalArgumentException();
}
System.arraycopy(keyBytes2, keyBytes2.length - keyBytes.length, keyBytes, 0, keyBytes.length);
} else {
System.arraycopy(
keyBytes2, 0, keyBytes, keyBytes.length - keyBytes2.length, keyBytes2.length);
}
return keyBytes;
}
/** Recursive builder to build out the Ec2 key and its subclasses. */
abstract static class Builder<T extends Builder<T>> extends CoseKey.Builder<T> {
private Integer curve = null;
private byte[] dParameter;
private byte[] xCor;
private byte[] yCor;
@Override
void verifyKeyMaterialPresentAndComplete() throws CoseException {
if (!isKeyMaterialPresent()) {
throw new CoseException(CoseException.MISSING_KEY_MATERIAL_EXCEPTION_MESSAGE);
}
if (isPublicKeyMaterialIncomplete()) {
throw new CoseException("Both coordinates of public key need to be provided.");
}
}
boolean isKeyMaterialPresent() {
return dParameter != null || (xCor != null && yCor != null);
}
boolean isPublicKeyMaterialIncomplete() {
return xCor == null ^ yCor == null;
}
@Override
protected Map compile() throws CoseException {
if (curve == null) {
throw new CoseException("Need curve information.");
}
withKeyType(Headers.KEY_TYPE_EC2);
Map cborKey = super.compile();
cborKey.put(new NegativeInteger(Headers.KEY_PARAMETER_CURVE), new UnsignedInteger(curve));
if (dParameter != null) {
cborKey.put(new NegativeInteger(Headers.KEY_PARAMETER_D), new ByteString(dParameter));
}
if (xCor != null) {
cborKey.put(new NegativeInteger(Headers.KEY_PARAMETER_X), new ByteString(xCor));
}
if (yCor != null) {
cborKey.put(new NegativeInteger(Headers.KEY_PARAMETER_Y), new ByteString(yCor));
}
return cborKey;
}
public T copyFrom(Ec2Key key) {
curve = key.curve;
int keySize;
switch (curve) {
case Headers.CURVE_EC2_P256:
keySize = 256;
break;
case Headers.CURVE_EC2_P384:
keySize = 384;
break;
case Headers.CURVE_EC2_P521:
keySize = 521;
break;
default:
throw new AssertionError();
}
ECPublicKey pubKey = (ECPublicKey) key.keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) key.keyPair.getPrivate();
xCor = arrayFromBigNum(pubKey.getW().getAffineX(), keySize);
yCor = arrayFromBigNum(pubKey.getW().getAffineY(), keySize);
dParameter = (privKey == null) ? null : privKey.getS().toByteArray();
return super.copyFrom(key);
}
public T withCurve(int curve) throws CoseException {
if ((curve < 0) || (curve > Headers.CURVE_EC2_P521)) {
throw new CoseException(CoseException.UNSUPPORTED_CURVE_EXCEPTION_MESSAGE);
}
this.curve = curve;
return self();
}
public T withXCoordinate(byte[] xCor) {
this.xCor = xCor;
return self();
}
public T withYCoordinate(byte[] yCor) {
this.yCor = yCor;
return self();
}
public T withGeneratedKeyPair(int curve) throws CoseException {
KeyPair keyPair;
int keySize;
String curveName;
switch (curve) {
case Headers.CURVE_EC2_P256:
curveName = "secp256r1";
keySize = 256;
break;
case Headers.CURVE_EC2_P384:
curveName = "secp384r1";
keySize = 384;
break;
case Headers.CURVE_EC2_P521:
curveName = "secp521r1";
keySize = 521;
break;
default:
throw new CoseException("Unsupported curve: " + curve);
}
try {
ECGenParameterSpec paramSpec = new ECGenParameterSpec(curveName);
KeyPairGenerator gen = KeyPairGenerator.getInstance("EC");
gen.initialize(paramSpec);
keyPair = gen.genKeyPair();
ECPoint pubPoint = ((ECPublicKey) keyPair.getPublic()).getW();
return withPrivateKeyRepresentation()
.withPkcs8EncodedBytes(keyPair.getPrivate().getEncoded())
.withXCoordinate(arrayFromBigNum(pubPoint.getAffineX(), keySize))
.withYCoordinate(arrayFromBigNum(pubPoint.getAffineY(), keySize))
.withCurve(curve);
} catch (GeneralSecurityException e) {
throw new CoseException("Failed to generate key pari for curve: " + curve, e);
}
}
public PrivateKeyRepresentationBuilder<T> withPrivateKeyRepresentation() {
return new PrivateKeyRepresentationBuilder<T>(this);
}
/**
* Helper class to get the raw bytes out of the encoded private keys.
*/
public static class PrivateKeyRepresentationBuilder<T extends Builder<T>> {
Builder<T> builder;
PrivateKeyRepresentationBuilder(Builder<T> builder) {
this.builder = builder;
}
public T withPrivateKey(ECPrivateKey privateKey) {
builder.dParameter = privateKey.getS().toByteArray();
return builder.self();
}
public T withPkcs8EncodedBytes(byte[] keyBytes) throws CoseException {
ECPrivateKey key = CoseUtils.getEc2PrivateKeyFromEncodedKeyBytes(keyBytes);
builder.dParameter = key.getS().toByteArray();
return builder.self();
}
/**
* This function expects the BigInteger byte array of the private key. This is typically the
* multiplier in the EC2 private key which can generate EC2 public key from generator point.
* @param rawBytes byte array representation of BigInteger
* @return {@link Builder}
*/
public T withDParameter(byte[] rawBytes) {
builder.dParameter = rawBytes;
return builder.self();
}
}
}
}