author | redestad |
Mon, 08 Oct 2018 18:16:03 +0200 | |
changeset 52040 | d8aebcc2d3ac |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
25669
daa21271c03b
8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents:
23010
diff
changeset
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
25669
daa21271c03b
8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents:
23010
diff
changeset
|
26 |
package sun.security.util; |
2 | 27 |
|
28 |
import java.io.IOException; |
|
29 |
||
30 |
import java.security.*; |
|
31 |
import java.security.spec.*; |
|
32 |
||
33 |
/** |
|
34 |
* This class implements encoding and decoding of Elliptic Curve parameters |
|
35 |
* as specified in RFC 3279. |
|
36 |
* |
|
37 |
* However, only named curves are currently supported. |
|
38 |
* |
|
39 |
* ASN.1 from RFC 3279 follows. Note that X9.62 (2005) has added some additional |
|
40 |
* options. |
|
41 |
* |
|
42 |
* <pre> |
|
43 |
* EcpkParameters ::= CHOICE { |
|
44 |
* ecParameters ECParameters, |
|
45 |
* namedCurve OBJECT IDENTIFIER, |
|
46 |
* implicitlyCA NULL } |
|
47 |
* |
|
48 |
* ECParameters ::= SEQUENCE { |
|
49 |
* version ECPVer, -- version is always 1 |
|
50 |
* fieldID FieldID, -- identifies the finite field over |
|
51 |
* -- which the curve is defined |
|
52 |
* curve Curve, -- coefficients a and b of the |
|
53 |
* -- elliptic curve |
|
54 |
* base ECPoint, -- specifies the base point P |
|
55 |
* -- on the elliptic curve |
|
56 |
* order INTEGER, -- the order n of the base point |
|
57 |
* cofactor INTEGER OPTIONAL -- The integer h = #E(Fq)/n |
|
58 |
* } |
|
59 |
* |
|
60 |
* ECPVer ::= INTEGER {ecpVer1(1)} |
|
61 |
* |
|
62 |
* Curve ::= SEQUENCE { |
|
63 |
* a FieldElement, |
|
64 |
* b FieldElement, |
|
65 |
* seed BIT STRING OPTIONAL } |
|
66 |
* |
|
67 |
* FieldElement ::= OCTET STRING |
|
68 |
* |
|
69 |
* ECPoint ::= OCTET STRING |
|
70 |
* </pre> |
|
71 |
* |
|
72 |
* @since 1.6 |
|
73 |
* @author Andreas Sterbenz |
|
74 |
*/ |
|
75 |
public final class ECParameters extends AlgorithmParametersSpi { |
|
76 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
77 |
// used by ECPublicKeyImpl and ECPrivateKeyImpl |
25669
daa21271c03b
8035166: Remove dependency on EC classes from pkcs11 provider
valeriep
parents:
23010
diff
changeset
|
78 |
public static AlgorithmParameters getAlgorithmParameters(ECParameterSpec spec) |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
79 |
throws InvalidKeyException { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
80 |
try { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
81 |
AlgorithmParameters params = |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
82 |
AlgorithmParameters.getInstance("EC", "SunEC"); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
83 |
params.init(spec); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
84 |
return params; |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
85 |
} catch (GeneralSecurityException e) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
86 |
throw new InvalidKeyException("EC parameters error", e); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
87 |
} |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
88 |
} |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
89 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
90 |
/* |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
91 |
* The parameters these AlgorithmParameters object represents. |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
92 |
* Currently, it is always an instance of NamedCurve. |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
93 |
*/ |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
94 |
private NamedCurve namedCurve; |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
95 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
96 |
// A public constructor is required by AlgorithmParameters class. |
2 | 97 |
public ECParameters() { |
98 |
// empty |
|
99 |
} |
|
100 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
101 |
// AlgorithmParameterSpi methods |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
102 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
103 |
protected void engineInit(AlgorithmParameterSpec paramSpec) |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
104 |
throws InvalidParameterSpecException { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
105 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
106 |
if (paramSpec == null) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
107 |
throw new InvalidParameterSpecException |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
108 |
("paramSpec must not be null"); |
2 | 109 |
} |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
110 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
111 |
if (paramSpec instanceof NamedCurve) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
112 |
namedCurve = (NamedCurve)paramSpec; |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
113 |
return; |
2 | 114 |
} |
115 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
116 |
if (paramSpec instanceof ECParameterSpec) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
117 |
namedCurve = CurveDB.lookup((ECParameterSpec)paramSpec); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
118 |
} else if (paramSpec instanceof ECGenParameterSpec) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
119 |
String name = ((ECGenParameterSpec)paramSpec).getName(); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
120 |
namedCurve = CurveDB.lookup(name); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
121 |
} else if (paramSpec instanceof ECKeySizeParameterSpec) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
122 |
int keySize = ((ECKeySizeParameterSpec)paramSpec).getKeySize(); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
123 |
namedCurve = CurveDB.lookup(keySize); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
124 |
} else { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
125 |
throw new InvalidParameterSpecException |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
126 |
("Only ECParameterSpec and ECGenParameterSpec supported"); |
2 | 127 |
} |
128 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
129 |
if (namedCurve == null) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
130 |
throw new InvalidParameterSpecException( |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
131 |
"Not a supported curve: " + paramSpec); |
2 | 132 |
} |
133 |
} |
|
134 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
135 |
protected void engineInit(byte[] params) throws IOException { |
2 | 136 |
DerValue encodedParams = new DerValue(params); |
137 |
if (encodedParams.tag == DerValue.tag_ObjectId) { |
|
138 |
ObjectIdentifier oid = encodedParams.getOID(); |
|
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
139 |
NamedCurve spec = CurveDB.lookup(oid.toString()); |
2 | 140 |
if (spec == null) { |
141 |
throw new IOException("Unknown named curve: " + oid); |
|
142 |
} |
|
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
143 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
144 |
namedCurve = spec; |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
145 |
return; |
2 | 146 |
} |
147 |
||
148 |
throw new IOException("Only named ECParameters supported"); |
|
149 |
||
150 |
// The code below is incomplete. |
|
151 |
// It is left as a starting point for a complete parsing implementation. |
|
152 |
||
153 |
/* |
|
154 |
if (encodedParams.tag != DerValue.tag_Sequence) { |
|
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
155 |
throw new IOException("Unsupported EC parameters, tag: " + |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
156 |
encodedParams.tag); |
2 | 157 |
} |
158 |
||
159 |
encodedParams.data.reset(); |
|
160 |
||
161 |
DerInputStream in = encodedParams.data; |
|
162 |
||
163 |
int version = in.getInteger(); |
|
164 |
if (version != 1) { |
|
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
165 |
throw new IOException("Unsupported EC parameters version: " + |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
166 |
version); |
2 | 167 |
} |
168 |
ECField field = parseField(in); |
|
169 |
EllipticCurve curve = parseCurve(in, field); |
|
170 |
ECPoint point = parsePoint(in, curve); |
|
171 |
||
172 |
BigInteger order = in.getBigInteger(); |
|
173 |
int cofactor = 0; |
|
174 |
||
175 |
if (in.available() != 0) { |
|
176 |
cofactor = in.getInteger(); |
|
177 |
} |
|
178 |
||
179 |
// XXX HashAlgorithm optional |
|
180 |
||
181 |
if (encodedParams.data.available() != 0) { |
|
182 |
throw new IOException("encoded params have " + |
|
183 |
encodedParams.data.available() + |
|
184 |
" extra bytes"); |
|
185 |
} |
|
186 |
||
187 |
return new ECParameterSpec(curve, point, order, cofactor); |
|
188 |
*/ |
|
189 |
} |
|
190 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
191 |
protected void engineInit(byte[] params, String decodingMethod) |
2 | 192 |
throws IOException { |
193 |
engineInit(params); |
|
194 |
} |
|
195 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
196 |
protected <T extends AlgorithmParameterSpec> T |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
197 |
engineGetParameterSpec(Class<T> spec) |
2 | 198 |
throws InvalidParameterSpecException { |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
199 |
|
2 | 200 |
if (spec.isAssignableFrom(ECParameterSpec.class)) { |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
201 |
return spec.cast(namedCurve); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
202 |
} |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
203 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
204 |
if (spec.isAssignableFrom(ECGenParameterSpec.class)) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
205 |
// Ensure the name is the Object ID |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
206 |
String name = namedCurve.getObjectId(); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
207 |
return spec.cast(new ECGenParameterSpec(name)); |
2 | 208 |
} |
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
209 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
210 |
if (spec.isAssignableFrom(ECKeySizeParameterSpec.class)) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
211 |
int keySize = namedCurve.getCurve().getField().getFieldSize(); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
212 |
return spec.cast(new ECKeySizeParameterSpec(keySize)); |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
213 |
} |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
214 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
215 |
throw new InvalidParameterSpecException( |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
216 |
"Only ECParameterSpec and ECGenParameterSpec supported"); |
2 | 217 |
} |
218 |
||
219 |
protected byte[] engineGetEncoded() throws IOException { |
|
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
220 |
return namedCurve.getEncoded(); |
2 | 221 |
} |
222 |
||
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
223 |
protected byte[] engineGetEncoded(String encodingMethod) |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
224 |
throws IOException { |
2 | 225 |
return engineGetEncoded(); |
226 |
} |
|
227 |
||
228 |
protected String engineToString() { |
|
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
229 |
if (namedCurve == null) { |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
230 |
return "Not initialized"; |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
231 |
} |
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
232 |
|
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
233 |
return namedCurve.toString(); |
2 | 234 |
} |
235 |
} |
|
17491
7a33824ec8c5
7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents:
13813
diff
changeset
|
236 |