Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/ec/ECPrivateKeyImpl.java
38830 views
1
/*
2
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.ec;
27
28
import java.io.IOException;
29
import java.math.BigInteger;
30
31
import java.security.*;
32
import java.security.interfaces.*;
33
import java.security.spec.*;
34
35
import sun.security.util.ArrayUtil;
36
import sun.security.util.DerInputStream;
37
import sun.security.util.DerOutputStream;
38
import sun.security.util.DerValue;
39
import sun.security.util.ECParameters;
40
import sun.security.util.ECUtil;
41
import sun.security.x509.AlgorithmId;
42
import sun.security.pkcs.PKCS8Key;
43
44
/**
45
* Key implementation for EC private keys.
46
*
47
* ASN.1 syntax for EC private keys from SEC 1 v1.5 (draft):
48
*
49
* <pre>
50
* EXPLICIT TAGS
51
*
52
* ECPrivateKey ::= SEQUENCE {
53
* version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
54
* privateKey OCTET STRING,
55
* parameters [0] ECDomainParameters {{ SECGCurveNames }} OPTIONAL,
56
* publicKey [1] BIT STRING OPTIONAL
57
* }
58
* </pre>
59
*
60
* We currently ignore the optional parameters and publicKey fields. We
61
* require that the parameters are encoded as part of the AlgorithmIdentifier,
62
* not in the private key structure.
63
*
64
* @since 1.6
65
* @author Andreas Sterbenz
66
*/
67
public final class ECPrivateKeyImpl extends PKCS8Key implements ECPrivateKey {
68
69
private static final long serialVersionUID = 88695385615075129L;
70
71
private BigInteger s; // private value
72
private byte[] arrayS; // private value as a little-endian array
73
private ECParameterSpec params;
74
75
/**
76
* Construct a key from its encoding. Called by the ECKeyFactory.
77
*/
78
public ECPrivateKeyImpl(byte[] encoded) throws InvalidKeyException {
79
decode(encoded);
80
}
81
82
/**
83
* Construct a key from its components. Used by the
84
* KeyFactory.
85
*/
86
public ECPrivateKeyImpl(BigInteger s, ECParameterSpec params)
87
throws InvalidKeyException {
88
this.s = s;
89
this.params = params;
90
makeEncoding(s);
91
92
}
93
94
ECPrivateKeyImpl(byte[] s, ECParameterSpec params)
95
throws InvalidKeyException {
96
this.arrayS = s.clone();
97
this.params = params;
98
makeEncoding(s);
99
}
100
101
private void makeEncoding(byte[] s) throws InvalidKeyException {
102
algid = new AlgorithmId
103
(AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params));
104
try {
105
DerOutputStream out = new DerOutputStream();
106
out.putInteger(1); // version 1
107
byte[] privBytes = s.clone();
108
ArrayUtil.reverse(privBytes);
109
out.putOctetString(privBytes);
110
DerValue val =
111
new DerValue(DerValue.tag_Sequence, out.toByteArray());
112
key = val.toByteArray();
113
} catch (IOException exc) {
114
// should never occur
115
throw new InvalidKeyException(exc);
116
}
117
}
118
119
private void makeEncoding(BigInteger s) throws InvalidKeyException {
120
algid = new AlgorithmId
121
(AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params));
122
try {
123
byte[] sArr = s.toByteArray();
124
// convert to fixed-length array
125
int numOctets = (params.getOrder().bitLength() + 7) / 8;
126
byte[] sOctets = new byte[numOctets];
127
int inPos = Math.max(sArr.length - sOctets.length, 0);
128
int outPos = Math.max(sOctets.length - sArr.length, 0);
129
int length = Math.min(sArr.length, sOctets.length);
130
System.arraycopy(sArr, inPos, sOctets, outPos, length);
131
132
DerOutputStream out = new DerOutputStream();
133
out.putInteger(1); // version 1
134
out.putOctetString(sOctets);
135
DerValue val =
136
new DerValue(DerValue.tag_Sequence, out.toByteArray());
137
key = val.toByteArray();
138
} catch (IOException exc) {
139
// should never occur
140
throw new InvalidKeyException(exc);
141
}
142
}
143
144
// see JCA doc
145
public String getAlgorithm() {
146
return "EC";
147
}
148
149
// see JCA doc
150
public BigInteger getS() {
151
if (s == null) {
152
byte[] arrCopy = arrayS.clone();
153
ArrayUtil.reverse(arrCopy);
154
s = new BigInteger(1, arrCopy);
155
}
156
return s;
157
}
158
159
public byte[] getArrayS() {
160
if (arrayS == null) {
161
byte[] arr = getS().toByteArray();
162
ArrayUtil.reverse(arr);
163
int byteLength = (params.getOrder().bitLength() + 7) / 8;
164
arrayS = new byte[byteLength];
165
int length = Math.min(byteLength, arr.length);
166
System.arraycopy(arr, 0, arrayS, 0, length);
167
}
168
return arrayS.clone();
169
}
170
171
// see JCA doc
172
public ECParameterSpec getParams() {
173
return params;
174
}
175
176
/**
177
* Parse the key. Called by PKCS8Key.
178
*/
179
protected void parseKeyBits() throws InvalidKeyException {
180
try {
181
DerInputStream in = new DerInputStream(key);
182
DerValue derValue = in.getDerValue();
183
if (derValue.tag != DerValue.tag_Sequence) {
184
throw new IOException("Not a SEQUENCE");
185
}
186
DerInputStream data = derValue.data;
187
int version = data.getInteger();
188
if (version != 1) {
189
throw new IOException("Version must be 1");
190
}
191
byte[] privData = data.getOctetString();
192
ArrayUtil.reverse(privData);
193
arrayS = privData;
194
while (data.available() != 0) {
195
DerValue value = data.getDerValue();
196
if (value.isContextSpecific((byte) 0)) {
197
// ignore for now
198
} else if (value.isContextSpecific((byte) 1)) {
199
// ignore for now
200
} else {
201
throw new InvalidKeyException("Unexpected value: " + value);
202
}
203
}
204
AlgorithmParameters algParams = this.algid.getParameters();
205
if (algParams == null) {
206
throw new InvalidKeyException("EC domain parameters must be "
207
+ "encoded in the algorithm identifier");
208
}
209
params = algParams.getParameterSpec(ECParameterSpec.class);
210
} catch (IOException e) {
211
throw new InvalidKeyException("Invalid EC private key", e);
212
} catch (InvalidParameterSpecException e) {
213
throw new InvalidKeyException("Invalid EC private key", e);
214
}
215
}
216
}
217
218