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/util/AlgorithmDecomposer.java
38830 views
1
/*
2
* Copyright (c) 2015, 2017, 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.util;
27
28
import java.util.HashSet;
29
import java.util.Set;
30
import java.util.Arrays;
31
import java.util.Collection;
32
import java.util.regex.Pattern;
33
34
/**
35
* The class decomposes standard algorithms into sub-elements.
36
*/
37
public class AlgorithmDecomposer {
38
39
private static final Pattern transPattern = Pattern.compile("/");
40
private static final Pattern pattern =
41
Pattern.compile("with|and", Pattern.CASE_INSENSITIVE);
42
43
private static Set<String> decomposeImpl(String algorithm) {
44
45
// algorithm/mode/padding
46
String[] transTockens = transPattern.split(algorithm);
47
48
Set<String> elements = new HashSet<>();
49
for (String transTocken : transTockens) {
50
if (transTocken == null || transTocken.length() == 0) {
51
continue;
52
}
53
54
// PBEWith<digest>And<encryption>
55
// PBEWith<prf>And<encryption>
56
// OAEPWith<digest>And<mgf>Padding
57
// <digest>with<encryption>
58
// <digest>with<encryption>and<mgf>
59
String[] tokens = pattern.split(transTocken);
60
61
for (String token : tokens) {
62
if (token == null || token.length() == 0) {
63
continue;
64
}
65
66
elements.add(token);
67
}
68
}
69
return elements;
70
}
71
72
/**
73
* Decompose the standard algorithm name into sub-elements.
74
* <p>
75
* For example, we need to decompose "SHA1WithRSA" into "SHA1" and "RSA"
76
* so that we can check the "SHA1" and "RSA" algorithm constraints
77
* separately.
78
* <p>
79
* Please override the method if need to support more name pattern.
80
*/
81
public Set<String> decompose(String algorithm) {
82
if (algorithm == null || algorithm.length() == 0) {
83
return new HashSet<>();
84
}
85
86
Set<String> elements = decomposeImpl(algorithm);
87
88
// In Java standard algorithm name specification, for different
89
// purpose, the SHA-1 and SHA-2 algorithm names are different. For
90
// example, for MessageDigest, the standard name is "SHA-256", while
91
// for Signature, the digest algorithm component is "SHA256" for
92
// signature algorithm "SHA256withRSA". So we need to check both
93
// "SHA-256" and "SHA256" to make the right constraint checking.
94
95
// handle special name: SHA-1 and SHA1
96
if (elements.contains("SHA1") && !elements.contains("SHA-1")) {
97
elements.add("SHA-1");
98
}
99
if (elements.contains("SHA-1") && !elements.contains("SHA1")) {
100
elements.add("SHA1");
101
}
102
103
// handle special name: SHA-224 and SHA224
104
if (elements.contains("SHA224") && !elements.contains("SHA-224")) {
105
elements.add("SHA-224");
106
}
107
if (elements.contains("SHA-224") && !elements.contains("SHA224")) {
108
elements.add("SHA224");
109
}
110
111
// handle special name: SHA-256 and SHA256
112
if (elements.contains("SHA256") && !elements.contains("SHA-256")) {
113
elements.add("SHA-256");
114
}
115
if (elements.contains("SHA-256") && !elements.contains("SHA256")) {
116
elements.add("SHA256");
117
}
118
119
// handle special name: SHA-384 and SHA384
120
if (elements.contains("SHA384") && !elements.contains("SHA-384")) {
121
elements.add("SHA-384");
122
}
123
if (elements.contains("SHA-384") && !elements.contains("SHA384")) {
124
elements.add("SHA384");
125
}
126
127
// handle special name: SHA-512 and SHA512
128
if (elements.contains("SHA512") && !elements.contains("SHA-512")) {
129
elements.add("SHA-512");
130
}
131
if (elements.contains("SHA-512") && !elements.contains("SHA512")) {
132
elements.add("SHA512");
133
}
134
135
return elements;
136
}
137
138
/**
139
* Get aliases of the specified algorithm.
140
*
141
* May support more algorithms in the future.
142
*/
143
public static Collection<String> getAliases(String algorithm) {
144
String[] aliases;
145
if (algorithm.equalsIgnoreCase("DH") ||
146
algorithm.equalsIgnoreCase("DiffieHellman")) {
147
aliases = new String[] {"DH", "DiffieHellman"};
148
} else {
149
aliases = new String[] {algorithm};
150
}
151
152
return Arrays.asList(aliases);
153
}
154
155
private static void hasLoop(Set<String> elements, String find, String replace) {
156
if (elements.contains(find)) {
157
if (!elements.contains(replace)) {
158
elements.add(replace);
159
}
160
elements.remove(find);
161
}
162
}
163
164
/*
165
* This decomposes a standard name into sub-elements with a consistent
166
* message digest algorithm name to avoid overly complicated checking.
167
*/
168
public static Set<String> decomposeOneHash(String algorithm) {
169
if (algorithm == null || algorithm.length() == 0) {
170
return new HashSet<>();
171
}
172
173
Set<String> elements = decomposeImpl(algorithm);
174
175
hasLoop(elements, "SHA-1", "SHA1");
176
hasLoop(elements, "SHA-224", "SHA224");
177
hasLoop(elements, "SHA-256", "SHA256");
178
hasLoop(elements, "SHA-384", "SHA384");
179
hasLoop(elements, "SHA-512", "SHA512");
180
181
return elements;
182
}
183
184
/*
185
* The provided message digest algorithm name will return a consistent
186
* naming scheme.
187
*/
188
public static String hashName(String algorithm) {
189
return algorithm.replace("-", "");
190
}
191
}
192
193