Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/DSAKeyFactory.java
41159 views
1
/*
2
* Copyright (c) 1997, 2021, 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.provider;
27
28
import java.security.Key;
29
import java.security.PublicKey;
30
import java.security.PrivateKey;
31
import java.security.KeyFactorySpi;
32
import java.security.InvalidKeyException;
33
import java.security.interfaces.DSAParams;
34
import java.security.spec.DSAPublicKeySpec;
35
import java.security.spec.DSAPrivateKeySpec;
36
import java.security.spec.KeySpec;
37
import java.security.spec.InvalidKeySpecException;
38
import java.security.spec.X509EncodedKeySpec;
39
import java.security.spec.PKCS8EncodedKeySpec;
40
import java.util.Arrays;
41
42
/**
43
* This class implements the DSA key factory of the Sun provider.
44
*
45
* @author Jan Luehe
46
*
47
*
48
* @since 1.2
49
*/
50
51
public class DSAKeyFactory extends KeyFactorySpi {
52
/**
53
* Generates a public key object from the provided key specification
54
* (key material).
55
*
56
* @param keySpec the specification (key material) of the public key
57
*
58
* @return the public key
59
*
60
* @exception InvalidKeySpecException if the given key specification
61
* is inappropriate for this key factory to produce a public key.
62
*/
63
protected PublicKey engineGeneratePublic(KeySpec keySpec)
64
throws InvalidKeySpecException {
65
try {
66
if (keySpec instanceof DSAPublicKeySpec) {
67
DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
68
return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
69
dsaPubKeySpec.getP(),
70
dsaPubKeySpec.getQ(),
71
dsaPubKeySpec.getG());
72
} else if (keySpec instanceof X509EncodedKeySpec) {
73
return new DSAPublicKeyImpl
74
(((X509EncodedKeySpec)keySpec).getEncoded());
75
} else {
76
throw new InvalidKeySpecException
77
("Inappropriate key specification");
78
}
79
} catch (InvalidKeyException e) {
80
throw new InvalidKeySpecException
81
("Inappropriate key specification: " + e.getMessage());
82
}
83
}
84
85
/**
86
* Generates a private key object from the provided key specification
87
* (key material).
88
*
89
* @param keySpec the specification (key material) of the private key
90
*
91
* @return the private key
92
*
93
* @exception InvalidKeySpecException if the given key specification
94
* is inappropriate for this key factory to produce a private key.
95
*/
96
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
97
throws InvalidKeySpecException {
98
try {
99
if (keySpec instanceof DSAPrivateKeySpec) {
100
DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;
101
return new DSAPrivateKey(dsaPrivKeySpec.getX(),
102
dsaPrivKeySpec.getP(),
103
dsaPrivKeySpec.getQ(),
104
dsaPrivKeySpec.getG());
105
106
} else if (keySpec instanceof PKCS8EncodedKeySpec) {
107
byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
108
try {
109
return new DSAPrivateKey(encoded);
110
} finally {
111
Arrays.fill(encoded, (byte) 0);
112
}
113
} else {
114
throw new InvalidKeySpecException
115
("Inappropriate key specification");
116
}
117
} catch (InvalidKeyException e) {
118
throw new InvalidKeySpecException
119
("Inappropriate key specification: " + e.getMessage());
120
}
121
}
122
123
/**
124
* Returns a specification (key material) of the given key object
125
* in the requested format.
126
*
127
* @param key the key
128
*
129
* @param keySpec the requested format in which the key material shall be
130
* returned
131
*
132
* @return the underlying key specification (key material) in the
133
* requested format
134
*
135
* @exception InvalidKeySpecException if the requested key specification is
136
* inappropriate for the given key, or the given key cannot be processed
137
* (e.g., the given key has an unrecognized algorithm or format).
138
*/
139
protected <T extends KeySpec>
140
T engineGetKeySpec(Key key, Class<T> keySpec)
141
throws InvalidKeySpecException {
142
143
DSAParams params;
144
145
try {
146
147
if (key instanceof java.security.interfaces.DSAPublicKey) {
148
149
// Determine valid key specs
150
Class<?> dsaPubKeySpec = Class.forName
151
("java.security.spec.DSAPublicKeySpec");
152
Class<?> x509KeySpec = Class.forName
153
("java.security.spec.X509EncodedKeySpec");
154
155
if (keySpec.isAssignableFrom(dsaPubKeySpec)) {
156
java.security.interfaces.DSAPublicKey dsaPubKey
157
= (java.security.interfaces.DSAPublicKey)key;
158
params = dsaPubKey.getParams();
159
return keySpec.cast(new DSAPublicKeySpec(dsaPubKey.getY(),
160
params.getP(),
161
params.getQ(),
162
params.getG()));
163
164
} else if (keySpec.isAssignableFrom(x509KeySpec)) {
165
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
166
167
} else {
168
throw new InvalidKeySpecException
169
("Inappropriate key specification");
170
}
171
172
} else if (key instanceof java.security.interfaces.DSAPrivateKey) {
173
174
// Determine valid key specs
175
Class<?> dsaPrivKeySpec = Class.forName
176
("java.security.spec.DSAPrivateKeySpec");
177
Class<?> pkcs8KeySpec = Class.forName
178
("java.security.spec.PKCS8EncodedKeySpec");
179
180
if (keySpec.isAssignableFrom(dsaPrivKeySpec)) {
181
java.security.interfaces.DSAPrivateKey dsaPrivKey
182
= (java.security.interfaces.DSAPrivateKey)key;
183
params = dsaPrivKey.getParams();
184
return keySpec.cast(new DSAPrivateKeySpec(dsaPrivKey.getX(),
185
params.getP(),
186
params.getQ(),
187
params.getG()));
188
189
} else if (keySpec.isAssignableFrom(pkcs8KeySpec)) {
190
byte[] encoded = key.getEncoded();
191
try {
192
return keySpec.cast(new PKCS8EncodedKeySpec(encoded));
193
} finally {
194
Arrays.fill(encoded, (byte)0);
195
}
196
} else {
197
throw new InvalidKeySpecException
198
("Inappropriate key specification");
199
}
200
201
} else {
202
throw new InvalidKeySpecException("Inappropriate key type");
203
}
204
205
} catch (ClassNotFoundException e) {
206
throw new InvalidKeySpecException
207
("Unsupported key specification: " + e.getMessage());
208
}
209
}
210
211
/**
212
* Translates a key object, whose provider may be unknown or potentially
213
* untrusted, into a corresponding key object of this key factory.
214
*
215
* @param key the key whose provider is unknown or untrusted
216
*
217
* @return the translated key
218
*
219
* @exception InvalidKeyException if the given key cannot be processed by
220
* this key factory.
221
*/
222
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
223
224
try {
225
226
if (key instanceof java.security.interfaces.DSAPublicKey) {
227
// Check if key originates from this factory
228
if (key instanceof sun.security.provider.DSAPublicKey) {
229
return key;
230
}
231
// Convert key to spec
232
DSAPublicKeySpec dsaPubKeySpec
233
= engineGetKeySpec(key, DSAPublicKeySpec.class);
234
// Create key from spec, and return it
235
return engineGeneratePublic(dsaPubKeySpec);
236
237
} else if (key instanceof java.security.interfaces.DSAPrivateKey) {
238
// Check if key originates from this factory
239
if (key instanceof sun.security.provider.DSAPrivateKey) {
240
return key;
241
}
242
// Convert key to spec
243
DSAPrivateKeySpec dsaPrivKeySpec
244
= engineGetKeySpec(key, DSAPrivateKeySpec.class);
245
// Create key from spec, and return it
246
return engineGeneratePrivate(dsaPrivKeySpec);
247
248
} else {
249
throw new InvalidKeyException("Wrong algorithm type");
250
}
251
252
} catch (InvalidKeySpecException e) {
253
throw new InvalidKeyException("Cannot translate key: "
254
+ e.getMessage());
255
}
256
}
257
}
258
259