56542
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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"),
|
|
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 |
for (KeyType kt : KeyType.values()) {
|
|
57 |
if (kt.keyAlgo().equalsIgnoreCase(name)) {
|
|
58 |
return kt;
|
|
59 |
}
|
|
60 |
}
|
|
61 |
// no match
|
|
62 |
throw new ProviderException("Unsupported algorithm " + name);
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
public static void checkParamsAgainstType(KeyType type,
|
|
67 |
AlgorithmParameterSpec paramSpec) throws ProviderException {
|
|
68 |
switch (type) {
|
|
69 |
case RSA:
|
|
70 |
if (paramSpec != null) {
|
|
71 |
throw new ProviderException("null params expected for " +
|
|
72 |
type.keyAlgo());
|
|
73 |
}
|
|
74 |
break;
|
|
75 |
case PSS:
|
|
76 |
if ((paramSpec != null) &&
|
|
77 |
!(paramSpec instanceof PSSParameterSpec)) {
|
|
78 |
throw new ProviderException
|
|
79 |
("PSSParmeterSpec expected for " + type.keyAlgo());
|
|
80 |
}
|
|
81 |
break;
|
|
82 |
default:
|
|
83 |
throw new ProviderException
|
|
84 |
("Unsupported RSA algorithm " + type);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
public static AlgorithmId createAlgorithmId(KeyType type,
|
|
89 |
AlgorithmParameterSpec paramSpec) throws ProviderException {
|
|
90 |
|
|
91 |
checkParamsAgainstType(type, paramSpec);
|
|
92 |
|
|
93 |
ObjectIdentifier oid = null;
|
|
94 |
AlgorithmParameters params = null;
|
|
95 |
try {
|
|
96 |
switch (type) {
|
|
97 |
case RSA:
|
|
98 |
oid = AlgorithmId.RSAEncryption_oid;
|
|
99 |
break;
|
|
100 |
case PSS:
|
|
101 |
if (paramSpec != null) {
|
|
102 |
params = AlgorithmParameters.getInstance(type.keyAlgo());
|
|
103 |
params.init(paramSpec);
|
|
104 |
}
|
|
105 |
oid = AlgorithmId.RSASSA_PSS_oid;
|
|
106 |
break;
|
|
107 |
default:
|
|
108 |
throw new ProviderException
|
|
109 |
("Unsupported RSA algorithm " + type);
|
|
110 |
}
|
|
111 |
AlgorithmId result;
|
|
112 |
if (params == null) {
|
|
113 |
result = new AlgorithmId(oid);
|
|
114 |
} else {
|
|
115 |
result = new AlgorithmId(oid, params);
|
|
116 |
}
|
|
117 |
return result;
|
|
118 |
} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) {
|
|
119 |
// should not happen
|
|
120 |
throw new ProviderException(e);
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
public static AlgorithmParameterSpec getParamSpec(AlgorithmId algid)
|
|
125 |
throws ProviderException {
|
|
126 |
if (algid == null) {
|
|
127 |
throw new ProviderException("AlgorithmId should not be null");
|
|
128 |
}
|
|
129 |
return getParamSpec(algid.getParameters());
|
|
130 |
}
|
|
131 |
|
|
132 |
public static AlgorithmParameterSpec getParamSpec(AlgorithmParameters params)
|
|
133 |
throws ProviderException {
|
|
134 |
if (params == null) return null;
|
|
135 |
|
|
136 |
String algName = params.getAlgorithm();
|
|
137 |
KeyType type = KeyType.lookup(algName);
|
|
138 |
Class<? extends AlgorithmParameterSpec> specCls;
|
|
139 |
switch (type) {
|
|
140 |
case RSA:
|
|
141 |
throw new ProviderException("No params accepted for " +
|
|
142 |
type.keyAlgo());
|
|
143 |
case PSS:
|
|
144 |
specCls = PSSParameterSpec.class;
|
|
145 |
break;
|
|
146 |
default:
|
|
147 |
throw new ProviderException("Unsupported RSA algorithm: " + algName);
|
|
148 |
}
|
|
149 |
try {
|
|
150 |
return params.getParameterSpec(specCls);
|
|
151 |
} catch (Exception e) {
|
|
152 |
throw new ProviderException(e);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
}
|