blob: 300bf7d6dd0ddf578f73b0890ff5089af868924c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package org.uic.barcode.utils;
import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AlgorithmNameResolver {
public static final String TYPE_KEY_GENERATOR_ALG = "KeyPairGenerator";
public static final String TYPE_SIGNATURE_ALG = "Signature";
private static final Pattern KEY_TYPE_PATTERN = Pattern.compile("^(\\w+)[.].*$");
private static final Pattern KEY_ALIAS_TYPE_PATTERN = Pattern.compile("^Alg[.]Alias[.](\\w+).*$");
private static final Pattern KEY_OID_PATTERN = Pattern.compile(".*?(\\d+(?:[.]\\d+){3,})$");
public static String getSignatureAlgorithmName (String oid) throws Exception {
Provider[] provs = Security.getProviders();
for (Provider prov : provs) {
Service service = prov.getService(AlgorithmNameResolver.TYPE_SIGNATURE_ALG,oid);
if (service != null) {
return service.getAlgorithm();
}
}
return null;
}
public static String getName(String type, String oid) throws Exception {
Provider[] provs = Security.getProviders();
for (Provider prov : provs) {
SortedSet<String> typeAndOID = getTypeAndOIDStrings(prov);
for (String entry : typeAndOID) {
String[] typeAndOIDArray = entry.split("-");
String ptype = typeAndOIDArray[0];
if (ptype.equalsIgnoreCase(type)) {
String poid = typeAndOIDArray[1];
Service pservice = prov.getService(ptype, poid);
String palgo = pservice.getAlgorithm();
if (ptype.equalsIgnoreCase(type) && poid.equals(oid)) {
return palgo;
}
}
}
}
if (oid.startsWith("1.2.840.10045")) {
return "ECDSA";
} else if (oid.startsWith("1.2.840.10040")) {
return "DSA";
}
return null;
}
public static String getName(String type, String oid, String providerName) throws Exception {
Provider[] provs = Security.getProviders();
for (Provider prov : provs) {
if (providerName == null || prov.getName().equals(providerName)) {
SortedSet<String> typeAndOID = getTypeAndOIDStrings(prov);
for (String entry : typeAndOID) {
String[] typeAndOIDArray = entry.split("-");
String ptype = typeAndOIDArray[0];
if (ptype.equalsIgnoreCase(type)) {
String poid = typeAndOIDArray[1];
Service pservice = prov.getService(ptype, poid);
String palgo = pservice.getAlgorithm();
if (ptype.equalsIgnoreCase(type) && poid.equals(oid)) {
return palgo;
}
}
}
}
}
if (oid.startsWith("1.2.840.10045")) {
return "ECDSA";
} else if (oid.startsWith("1.2.840.10040")) {
return "DSA";
}
return null;
}
private static SortedSet<String> getTypeAndOIDStrings(Provider prov) {
SortedSet<String> typeAndOID = new TreeSet<>();
Set<Object> keys = prov.keySet();
for (Object key : keys) {
String keyString = key.toString();
Matcher oidMatcher = KEY_OID_PATTERN.matcher(keyString);
if (oidMatcher.matches()) {
// get OID from matched keyString
String oid = oidMatcher.group(1);
// determine type
String type;
Matcher aliasTypeMatcher = KEY_ALIAS_TYPE_PATTERN.matcher(keyString);
if (aliasTypeMatcher.matches()) {
type = aliasTypeMatcher.group(1);
} else {
Matcher typeMatcher = KEY_TYPE_PATTERN.matcher(keyString);
typeMatcher.matches();
type = typeMatcher.group(1);
}
// algorithm parameters are not algorithms, so skip them
if (type.equals("AlgorithmParameters")) {
continue;
}
// auto-removes dupes
typeAndOID.add(type + "-" + oid);
}
}
return typeAndOID;
}
public static String getSecurityNames() throws Exception {
Provider[] provs = Security.getProviders();
StringBuilder sb = new StringBuilder();
for (Provider prov : provs) {
SortedSet<String> typeAndOID = getTypeAndOIDStrings(prov);
for (String entry : typeAndOID) {
String[] typeAndOIDArray = entry.split("-");
String ptype = typeAndOIDArray[0];
String poid = typeAndOIDArray[1];
Service pservice = prov.getService(ptype, poid);
String palgo = "";
if ( pservice != null) {
palgo = pservice.getAlgorithm();
}
sb.append(prov).append(";");
sb.append(ptype).append(";");
sb.append(poid).append(";");
sb.append(palgo).append(";");
sb.append("\r\n");
}
}
return sb.toString();
}
}
|