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