Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Util.java
41154 views
1
/*
2
* Copyright (c) 2003, 2021, 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.pkcs11;
27
28
import java.math.BigInteger;
29
import java.security.*;
30
31
/**
32
* Collection of static utility methods.
33
*
34
* @author Andreas Sterbenz
35
* @since 1.5
36
*/
37
public final class P11Util {
38
39
private static Object LOCK = new Object();
40
41
private static volatile Provider sun, sunRsaSign, sunJce;
42
43
private P11Util() {
44
// empty
45
}
46
47
static Provider getSunProvider() {
48
Provider p = sun;
49
if (p == null) {
50
synchronized (LOCK) {
51
p = getProvider
52
(sun, "SUN", "sun.security.provider.Sun");
53
sun = p;
54
}
55
}
56
return p;
57
}
58
59
static Provider getSunRsaSignProvider() {
60
Provider p = sunRsaSign;
61
if (p == null) {
62
synchronized (LOCK) {
63
p = getProvider
64
(sunRsaSign, "SunRsaSign", "sun.security.rsa.SunRsaSign");
65
sunRsaSign = p;
66
}
67
}
68
return p;
69
}
70
71
static Provider getSunJceProvider() {
72
Provider p = sunJce;
73
if (p == null) {
74
synchronized (LOCK) {
75
p = getProvider
76
(sunJce, "SunJCE", "com.sun.crypto.provider.SunJCE");
77
sunJce = p;
78
}
79
}
80
return p;
81
}
82
83
@SuppressWarnings("removal")
84
private static Provider getProvider(Provider p, String providerName,
85
String className) {
86
if (p != null) {
87
return p;
88
}
89
p = Security.getProvider(providerName);
90
if (p == null) {
91
try {
92
final Class<?> c = Class.forName(className);
93
p = AccessController.doPrivileged(
94
new PrivilegedAction<Provider>() {
95
public Provider run() {
96
try {
97
@SuppressWarnings("deprecation")
98
Object o = c.newInstance();
99
return (Provider) o;
100
} catch (Exception e) {
101
throw new ProviderException(
102
"Could not find provider " +
103
providerName, e);
104
}
105
}
106
}, null, new RuntimePermission(
107
"accessClassInPackage." + c.getPackageName()));
108
} catch (ClassNotFoundException e) {
109
// Unexpected, as className is not a user but a
110
// P11Util-internal value.
111
throw new ProviderException("Could not find provider " +
112
providerName, e);
113
}
114
}
115
return p;
116
}
117
118
static byte[] convert(byte[] input, int offset, int len) {
119
if ((offset == 0) && (len == input.length)) {
120
return input;
121
} else {
122
byte[] t = new byte[len];
123
System.arraycopy(input, offset, t, 0, len);
124
return t;
125
}
126
}
127
128
static byte[] subarray(byte[] b, int ofs, int len) {
129
byte[] out = new byte[len];
130
System.arraycopy(b, ofs, out, 0, len);
131
return out;
132
}
133
134
static byte[] concat(byte[] b1, byte[] b2) {
135
byte[] b = new byte[b1.length + b2.length];
136
System.arraycopy(b1, 0, b, 0, b1.length);
137
System.arraycopy(b2, 0, b, b1.length, b2.length);
138
return b;
139
}
140
141
static long[] concat(long[] b1, long[] b2) {
142
if (b1.length == 0) {
143
return b2;
144
}
145
long[] b = new long[b1.length + b2.length];
146
System.arraycopy(b1, 0, b, 0, b1.length);
147
System.arraycopy(b2, 0, b, b1.length, b2.length);
148
return b;
149
}
150
151
public static byte[] getMagnitude(BigInteger bi) {
152
byte[] b = bi.toByteArray();
153
if ((b.length > 1) && (b[0] == 0)) {
154
int n = b.length - 1;
155
byte[] newarray = new byte[n];
156
System.arraycopy(b, 1, newarray, 0, n);
157
b = newarray;
158
}
159
return b;
160
}
161
162
static byte[] sha1(byte[] data) {
163
try {
164
MessageDigest md = MessageDigest.getInstance("SHA-1");
165
md.update(data);
166
return md.digest();
167
} catch (GeneralSecurityException e) {
168
throw new ProviderException(e);
169
}
170
}
171
172
private static final char[] hexDigits = "0123456789abcdef".toCharArray();
173
174
static String toString(byte[] b) {
175
if (b == null) {
176
return "(null)";
177
}
178
StringBuilder sb = new StringBuilder(b.length * 3);
179
for (int i = 0; i < b.length; i++) {
180
int k = b[i] & 0xff;
181
if (i != 0) {
182
sb.append(':');
183
}
184
sb.append(hexDigits[k >>> 4]);
185
sb.append(hexDigits[k & 0xf]);
186
}
187
return sb.toString();
188
}
189
190
}
191
192