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/rsa/RSAUtil.java
38830 views
1
/*
2
* Copyright (c) 2018, 2020, 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.rsa;
27
28
import java.io.IOException;
29
import java.security.*;
30
import java.security.spec.*;
31
import sun.security.util.ObjectIdentifier;
32
import sun.security.x509.AlgorithmId;
33
34
/**
35
* Utility class for SunRsaSign provider.
36
* Currently used by RSAKeyPairGenerator and RSAKeyFactory.
37
*
38
* @since 8
39
*/
40
public class RSAUtil {
41
42
public enum KeyType {
43
RSA ("RSA"),
44
PSS ("RSASSA-PSS")
45
;
46
47
private final String algo;
48
49
KeyType(String keyAlgo) {
50
this.algo = keyAlgo;
51
}
52
public String keyAlgo() {
53
return algo;
54
}
55
public static KeyType lookup(String name)
56
throws InvalidKeyException, ProviderException {
57
if (name == null) {
58
throw new InvalidKeyException("Null key algorithm");
59
}
60
for (KeyType kt : KeyType.values()) {
61
if (kt.keyAlgo().equalsIgnoreCase(name)) {
62
return kt;
63
}
64
}
65
// no match
66
throw new ProviderException("Unsupported algorithm " + name);
67
}
68
}
69
70
public static void checkParamsAgainstType(KeyType type,
71
AlgorithmParameterSpec paramSpec) throws ProviderException {
72
switch (type) {
73
case RSA:
74
if (paramSpec != null) {
75
throw new ProviderException("null params expected for " +
76
type.keyAlgo());
77
}
78
break;
79
case PSS:
80
if ((paramSpec != null) &&
81
!(paramSpec instanceof PSSParameterSpec)) {
82
throw new ProviderException
83
("PSSParmeterSpec expected for " + type.keyAlgo());
84
}
85
break;
86
default:
87
throw new ProviderException
88
("Unsupported RSA algorithm " + type);
89
}
90
}
91
92
public static AlgorithmId createAlgorithmId(KeyType type,
93
AlgorithmParameterSpec paramSpec) throws ProviderException {
94
95
checkParamsAgainstType(type, paramSpec);
96
97
ObjectIdentifier oid = null;
98
AlgorithmParameters params = null;
99
try {
100
switch (type) {
101
case RSA:
102
oid = AlgorithmId.RSAEncryption_oid;
103
break;
104
case PSS:
105
if (paramSpec != null) {
106
params = AlgorithmParameters.getInstance(type.keyAlgo());
107
params.init(paramSpec);
108
}
109
oid = AlgorithmId.RSASSA_PSS_oid;
110
break;
111
default:
112
throw new ProviderException
113
("Unsupported RSA algorithm " + type);
114
}
115
AlgorithmId result;
116
if (params == null) {
117
result = new AlgorithmId(oid);
118
} else {
119
result = new AlgorithmId(oid, params);
120
}
121
return result;
122
} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) {
123
// should not happen
124
throw new ProviderException(e);
125
}
126
}
127
128
public static AlgorithmParameterSpec getParamSpec(AlgorithmId algid)
129
throws ProviderException {
130
if (algid == null) {
131
throw new ProviderException("AlgorithmId should not be null");
132
}
133
return getParamSpec(algid.getParameters());
134
}
135
136
public static AlgorithmParameterSpec getParamSpec(AlgorithmParameters params)
137
throws ProviderException {
138
if (params == null) return null;
139
140
try {
141
String algName = params.getAlgorithm();
142
KeyType type = KeyType.lookup(algName);
143
Class<? extends AlgorithmParameterSpec> specCls;
144
switch (type) {
145
case RSA:
146
throw new ProviderException("No params accepted for " +
147
type.keyAlgo());
148
case PSS:
149
specCls = PSSParameterSpec.class;
150
break;
151
default:
152
throw new ProviderException("Unsupported RSA algorithm: " + algName);
153
}
154
return params.getParameterSpec(specCls);
155
} catch (ProviderException pe) {
156
// pass it up
157
throw pe;
158
} catch (Exception e) {
159
throw new ProviderException(e);
160
}
161
}
162
}
163
164