Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/ConstructKeys.java
41161 views
1
/*
2
* Copyright (c) 1999, 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 com.sun.crypto.provider;
27
28
import java.security.Key;
29
import java.security.PublicKey;
30
import java.security.PrivateKey;
31
import java.security.KeyFactory;
32
import java.security.InvalidKeyException;
33
import java.security.NoSuchAlgorithmException;
34
import java.security.spec.PKCS8EncodedKeySpec;
35
import java.security.spec.X509EncodedKeySpec;
36
import java.security.spec.InvalidKeySpecException;
37
import java.util.Arrays;
38
import javax.crypto.SecretKey;
39
import javax.crypto.Cipher;
40
import javax.crypto.spec.SecretKeySpec;
41
42
/**
43
* This class is a helper class which construct key objects
44
* from encoded keys.
45
*
46
* @author Sharon Liu
47
*
48
*/
49
50
final class ConstructKeys {
51
/**
52
* Construct a public key from its encoding.
53
*
54
* @param encodedKey the encoding of a public key.
55
*
56
* @param encodedKeyAlgorithm the algorithm the encodedKey is for.
57
*
58
* @return a public key constructed from the encodedKey.
59
*/
60
private static final PublicKey constructPublicKey(byte[] encodedKey,
61
String encodedKeyAlgorithm)
62
throws InvalidKeyException, NoSuchAlgorithmException {
63
PublicKey key = null;
64
try {
65
KeyFactory keyFactory =
66
KeyFactory.getInstance(encodedKeyAlgorithm,
67
SunJCE.getInstance());
68
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
69
key = keyFactory.generatePublic(keySpec);
70
} catch (NoSuchAlgorithmException nsae) {
71
// Try to see whether there is another
72
// provider which supports this algorithm
73
try {
74
KeyFactory keyFactory =
75
KeyFactory.getInstance(encodedKeyAlgorithm);
76
X509EncodedKeySpec keySpec =
77
new X509EncodedKeySpec(encodedKey);
78
key = keyFactory.generatePublic(keySpec);
79
} catch (NoSuchAlgorithmException nsae2) {
80
throw new NoSuchAlgorithmException("No installed providers " +
81
"can create keys for the " +
82
encodedKeyAlgorithm +
83
"algorithm");
84
} catch (InvalidKeySpecException ikse2) {
85
InvalidKeyException ike =
86
new InvalidKeyException("Cannot construct public key");
87
ike.initCause(ikse2);
88
throw ike;
89
}
90
} catch (InvalidKeySpecException ikse) {
91
InvalidKeyException ike =
92
new InvalidKeyException("Cannot construct public key");
93
ike.initCause(ikse);
94
throw ike;
95
}
96
97
return key;
98
}
99
100
/**
101
* Construct a private key from its encoding.
102
*
103
* @param encodedKey the encoding of a private key.
104
*
105
* @param encodedKeyAlgorithm the algorithm the wrapped key is for.
106
*
107
* @return a private key constructed from the encodedKey.
108
*/
109
private static final PrivateKey constructPrivateKey(byte[] encodedKey,
110
String encodedKeyAlgorithm)
111
throws InvalidKeyException, NoSuchAlgorithmException {
112
PrivateKey key = null;
113
114
try {
115
KeyFactory keyFactory =
116
KeyFactory.getInstance(encodedKeyAlgorithm,
117
SunJCE.getInstance());
118
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
119
return keyFactory.generatePrivate(keySpec);
120
} catch (NoSuchAlgorithmException nsae) {
121
// Try to see whether there is another
122
// provider which supports this algorithm
123
try {
124
KeyFactory keyFactory =
125
KeyFactory.getInstance(encodedKeyAlgorithm);
126
PKCS8EncodedKeySpec keySpec =
127
new PKCS8EncodedKeySpec(encodedKey);
128
key = keyFactory.generatePrivate(keySpec);
129
} catch (NoSuchAlgorithmException nsae2) {
130
throw new NoSuchAlgorithmException("No installed providers " +
131
"can create keys for the " +
132
encodedKeyAlgorithm +
133
"algorithm");
134
} catch (InvalidKeySpecException ikse2) {
135
InvalidKeyException ike =
136
new InvalidKeyException("Cannot construct private key");
137
ike.initCause(ikse2);
138
throw ike;
139
}
140
} catch (InvalidKeySpecException ikse) {
141
InvalidKeyException ike =
142
new InvalidKeyException("Cannot construct private key");
143
ike.initCause(ikse);
144
throw ike;
145
}
146
147
return key;
148
}
149
150
/**
151
* Construct a secret key from its encoding.
152
*
153
* @param encodedKey the encoding of a secret key.
154
*
155
* @param encodedKeyAlgorithm the algorithm the secret key is for.
156
*
157
* @return a secret key constructed from the encodedKey.
158
*/
159
private static final SecretKey constructSecretKey(byte[] encodedKey,
160
int ofs, int len, String encodedKeyAlgorithm) {
161
return (new SecretKeySpec(encodedKey, ofs, len, encodedKeyAlgorithm));
162
}
163
164
static final Key constructKey(byte[] encoding, String keyAlgorithm,
165
int keyType) throws InvalidKeyException, NoSuchAlgorithmException {
166
return constructKey(encoding, 0, encoding.length, keyAlgorithm,
167
keyType);
168
}
169
170
static final Key constructKey(byte[] encoding, int ofs, int len,
171
String keyAlgorithm, int keyType)
172
throws InvalidKeyException, NoSuchAlgorithmException {
173
switch (keyType) {
174
case Cipher.SECRET_KEY:
175
try {
176
return ConstructKeys.constructSecretKey(encoding, ofs, len,
177
keyAlgorithm);
178
} finally {
179
Arrays.fill(encoding, ofs, len, (byte)0);
180
}
181
case Cipher.PRIVATE_KEY:
182
byte[] encoding2 = encoding;
183
try {
184
if (ofs != 0 || len != encoding.length) {
185
encoding2 = Arrays.copyOfRange(encoding, ofs, ofs + len);
186
}
187
return ConstructKeys.constructPrivateKey(encoding2,
188
keyAlgorithm);
189
} finally {
190
Arrays.fill(encoding, ofs, len, (byte)0);
191
if (encoding2 != encoding) {
192
Arrays.fill(encoding2, (byte)0);
193
}
194
}
195
case Cipher.PUBLIC_KEY:
196
if (ofs != 0 || len != encoding.length) {
197
encoding = Arrays.copyOfRange(encoding, ofs, ofs + len);
198
}
199
return ConstructKeys.constructPublicKey(encoding, keyAlgorithm);
200
default:
201
throw new NoSuchAlgorithmException("Unsupported key type");
202
}
203
}
204
}
205
206