Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/rsa/RSAUtil.java
41159 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 11
39
*/
40
public class RSAUtil {
41
42
public enum KeyType {
43
RSA ("RSA", AlgorithmId.RSAEncryption_oid, null),
44
PSS ("RSASSA-PSS", AlgorithmId.RSASSA_PSS_oid, PSSParameterSpec.class)
45
;
46
47
final String keyAlgo;
48
final ObjectIdentifier oid;
49
final Class<? extends AlgorithmParameterSpec> paramSpecCls;
50
51
KeyType(String keyAlgo, ObjectIdentifier oid,
52
Class<? extends AlgorithmParameterSpec> paramSpecCls) {
53
this.keyAlgo = keyAlgo;
54
this.oid = oid;
55
this.paramSpecCls = paramSpecCls;
56
}
57
58
public static KeyType lookup(String name) throws ProviderException {
59
60
requireNonNull(name, "Key algorithm should not be null");
61
62
// match loosely in order to work with 3rd party providers which
63
// may not follow the standard names
64
if (name.indexOf("PSS") != -1) {
65
return PSS;
66
} else if (name.indexOf("RSA") != -1) {
67
return RSA;
68
} else { // no match
69
throw new ProviderException("Unsupported algorithm " + name);
70
}
71
}
72
}
73
74
private static void requireNonNull(Object obj, String msg) {
75
if (obj == null) throw new ProviderException(msg);
76
}
77
78
public static AlgorithmParameterSpec checkParamsAgainstType(KeyType type,
79
AlgorithmParameterSpec paramSpec) throws ProviderException {
80
81
// currently no check for null parameter spec
82
// assumption is parameter spec is optional and can be null
83
if (paramSpec == null) return null;
84
85
Class<? extends AlgorithmParameterSpec> expCls = type.paramSpecCls;
86
if (expCls == null) {
87
throw new ProviderException("null params expected for " +
88
type.keyAlgo);
89
} else if (!expCls.isInstance(paramSpec)) {
90
throw new ProviderException
91
(expCls + " expected for " + type.keyAlgo);
92
}
93
return paramSpec;
94
}
95
96
public static AlgorithmParameters getParams(KeyType type,
97
AlgorithmParameterSpec spec) throws ProviderException {
98
99
if (spec == null) return null;
100
101
try {
102
AlgorithmParameters params =
103
AlgorithmParameters.getInstance(type.keyAlgo);
104
params.init(spec);
105
return params;
106
} catch (NoSuchAlgorithmException | InvalidParameterSpecException ex) {
107
throw new ProviderException(ex);
108
}
109
}
110
111
public static AlgorithmId createAlgorithmId(KeyType type,
112
AlgorithmParameterSpec paramSpec) throws ProviderException {
113
114
checkParamsAgainstType(type, paramSpec);
115
116
ObjectIdentifier oid = type.oid;
117
AlgorithmParameters params = getParams(type, paramSpec);
118
return new AlgorithmId(oid, params);
119
}
120
121
public static AlgorithmParameterSpec getParamSpec(
122
AlgorithmParameters params) throws ProviderException {
123
124
if (params == null) return null;
125
126
String algName = params.getAlgorithm();
127
128
KeyType type = KeyType.lookup(algName);
129
Class<? extends AlgorithmParameterSpec> specCls = type.paramSpecCls;
130
if (specCls == null) {
131
throw new ProviderException("No params accepted for " +
132
type.keyAlgo);
133
}
134
try {
135
return params.getParameterSpec(specCls);
136
} catch (InvalidParameterSpecException ex) {
137
throw new ProviderException(ex);
138
}
139
}
140
141
public static Object[] getTypeAndParamSpec(AlgorithmId algid)
142
throws ProviderException {
143
144
requireNonNull(algid, "AlgorithmId should not be null");
145
146
Object[] result = new Object[2];
147
148
String algName = algid.getName();
149
try {
150
result[0] = KeyType.lookup(algName);
151
} catch (ProviderException pe) {
152
// accommodate RSA keys encoded with various RSA signature oids
153
// for backward compatibility
154
if (algName.indexOf("RSA") != -1) {
155
result[0] = KeyType.RSA;
156
} else {
157
// pass it up
158
throw pe;
159
}
160
}
161
162
result[1] = getParamSpec(algid.getParameters());
163
return result;
164
}
165
}
166
167