author | valeriep |
Tue, 08 Oct 2019 00:01:20 +0000 | |
changeset 58489 | 2faeaa5933a6 |
parent 53257 | 5170dc2bcf64 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58489
2faeaa5933a6
8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents:
53257
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.pkcs11; |
|
27 |
||
28 |
import java.nio.ByteBuffer; |
|
29 |
||
30 |
import java.security.*; |
|
31 |
import java.security.spec.AlgorithmParameterSpec; |
|
32 |
||
33 |
import javax.crypto.MacSpi; |
|
34 |
||
35 |
import sun.nio.ch.DirectBuffer; |
|
36 |
||
37 |
import sun.security.pkcs11.wrapper.*; |
|
38 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
|
39 |
||
40 |
/** |
|
41 |
* MAC implementation class. This class currently supports HMAC using |
|
12685 | 42 |
* MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 and the SSL3 MAC |
43 |
* using MD5 and SHA-1. |
|
2 | 44 |
* |
45 |
* Note that unlike other classes (e.g. Signature), this does not |
|
46 |
* composite various operations if the token only supports part of the |
|
47 |
* required functionality. The MAC implementations in SunJCE already |
|
48 |
* do exactly that by implementing an MAC on top of MessageDigests. We |
|
49 |
* could not do any better than they. |
|
50 |
* |
|
51 |
* @author Andreas Sterbenz |
|
52 |
* @since 1.5 |
|
53 |
*/ |
|
54 |
final class P11Mac extends MacSpi { |
|
55 |
||
56 |
// token instance |
|
57 |
private final Token token; |
|
58 |
||
59 |
// algorithm name |
|
60 |
private final String algorithm; |
|
61 |
||
62 |
// mechanism object |
|
63 |
private final CK_MECHANISM ckMechanism; |
|
64 |
||
65 |
// length of the MAC in bytes |
|
66 |
private final int macLength; |
|
67 |
||
68 |
// key instance used, if operation active |
|
69 |
private P11Key p11Key; |
|
70 |
||
71 |
// associated session, if any |
|
72 |
private Session session; |
|
73 |
||
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
74 |
// initialization status |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
75 |
private boolean initialized; |
2 | 76 |
|
77 |
// one byte buffer for the update(byte) method, initialized on demand |
|
78 |
private byte[] oneByte; |
|
79 |
||
80 |
P11Mac(Token token, String algorithm, long mechanism) |
|
81 |
throws PKCS11Exception { |
|
82 |
super(); |
|
83 |
this.token = token; |
|
84 |
this.algorithm = algorithm; |
|
85 |
Long params = null; |
|
86 |
switch ((int)mechanism) { |
|
87 |
case (int)CKM_MD5_HMAC: |
|
88 |
macLength = 16; |
|
89 |
break; |
|
90 |
case (int)CKM_SHA_1_HMAC: |
|
91 |
macLength = 20; |
|
92 |
break; |
|
12685 | 93 |
case (int)CKM_SHA224_HMAC: |
58489
2faeaa5933a6
8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents:
53257
diff
changeset
|
94 |
case (int)CKM_SHA512_224_HMAC: |
12685 | 95 |
macLength = 28; |
96 |
break; |
|
2 | 97 |
case (int)CKM_SHA256_HMAC: |
58489
2faeaa5933a6
8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents:
53257
diff
changeset
|
98 |
case (int)CKM_SHA512_256_HMAC: |
2 | 99 |
macLength = 32; |
100 |
break; |
|
101 |
case (int)CKM_SHA384_HMAC: |
|
102 |
macLength = 48; |
|
103 |
break; |
|
104 |
case (int)CKM_SHA512_HMAC: |
|
105 |
macLength = 64; |
|
106 |
break; |
|
107 |
case (int)CKM_SSL3_MD5_MAC: |
|
108 |
macLength = 16; |
|
109 |
params = Long.valueOf(16); |
|
110 |
break; |
|
111 |
case (int)CKM_SSL3_SHA1_MAC: |
|
112 |
macLength = 20; |
|
113 |
params = Long.valueOf(20); |
|
114 |
break; |
|
115 |
default: |
|
116 |
throw new ProviderException("Unknown mechanism: " + mechanism); |
|
117 |
} |
|
118 |
ckMechanism = new CK_MECHANISM(mechanism, params); |
|
119 |
} |
|
120 |
||
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
121 |
// reset the states to the pre-initialized values |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
122 |
private void reset(boolean doCancel) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
123 |
if (!initialized) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
124 |
return; |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
125 |
} |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
126 |
initialized = false; |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
127 |
try { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
128 |
if (session == null) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
129 |
return; |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
130 |
} |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
131 |
if (doCancel && token.explicitCancel) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
132 |
cancelOperation(); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
133 |
} |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
134 |
} finally { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
135 |
p11Key.releaseKeyID(); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
136 |
session = token.releaseSession(session); |
2 | 137 |
} |
138 |
} |
|
139 |
||
140 |
private void cancelOperation() { |
|
141 |
token.ensureValid(); |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
142 |
if (session.hasObjects() == false) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
143 |
session = token.killSession(session); |
2 | 144 |
return; |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
145 |
} else { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
146 |
try { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
147 |
token.p11.C_SignFinal(session.id(), 0); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
148 |
} catch (PKCS11Exception e) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
149 |
throw new ProviderException("Cancel failed", e); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
150 |
} |
2 | 151 |
} |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
152 |
} |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
153 |
|
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
154 |
private void ensureInitialized() throws PKCS11Exception { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
155 |
if (!initialized) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
156 |
initialize(); |
2 | 157 |
} |
158 |
} |
|
159 |
||
160 |
private void initialize() throws PKCS11Exception { |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
161 |
if (p11Key == null) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
162 |
throw new ProviderException( |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
163 |
"Operation cannot be performed without calling engineInit first"); |
2 | 164 |
} |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
165 |
token.ensureValid(); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
166 |
long p11KeyID = p11Key.getKeyID(); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
167 |
try { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
168 |
if (session == null) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
169 |
session = token.getOpSession(); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
170 |
} |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
171 |
token.p11.C_SignInit(session.id(), ckMechanism, p11KeyID); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
172 |
} catch (PKCS11Exception e) { |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
173 |
p11Key.releaseKeyID(); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
174 |
session = token.releaseSession(session); |
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
175 |
throw e; |
2 | 176 |
} |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
177 |
initialized = true; |
2 | 178 |
} |
179 |
||
180 |
// see JCE spec |
|
181 |
protected int engineGetMacLength() { |
|
182 |
return macLength; |
|
183 |
} |
|
184 |
||
185 |
// see JCE spec |
|
186 |
protected void engineReset() { |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
187 |
reset(true); |
2 | 188 |
} |
189 |
||
190 |
// see JCE spec |
|
191 |
protected void engineInit(Key key, AlgorithmParameterSpec params) |
|
192 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
193 |
if (params != null) { |
|
194 |
throw new InvalidAlgorithmParameterException |
|
195 |
("Parameters not supported"); |
|
196 |
} |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
197 |
reset(true); |
2 | 198 |
p11Key = P11SecretKeyFactory.convertKey(token, key, algorithm); |
199 |
try { |
|
200 |
initialize(); |
|
201 |
} catch (PKCS11Exception e) { |
|
202 |
throw new InvalidKeyException("init() failed", e); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
// see JCE spec |
|
207 |
protected byte[] engineDoFinal() { |
|
208 |
try { |
|
209 |
ensureInitialized(); |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
210 |
return token.p11.C_SignFinal(session.id(), 0); |
2 | 211 |
} catch (PKCS11Exception e) { |
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
212 |
reset(true); |
2 | 213 |
throw new ProviderException("doFinal() failed", e); |
214 |
} finally { |
|
53257
5170dc2bcf64
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents:
47216
diff
changeset
|
215 |
reset(false); |
2 | 216 |
} |
217 |
} |
|
218 |
||
219 |
// see JCE spec |
|
220 |
protected void engineUpdate(byte input) { |
|
221 |
if (oneByte == null) { |
|
222 |
oneByte = new byte[1]; |
|
223 |
} |
|
224 |
oneByte[0] = input; |
|
225 |
engineUpdate(oneByte, 0, 1); |
|
226 |
} |
|
227 |
||
228 |
// see JCE spec |
|
229 |
protected void engineUpdate(byte[] b, int ofs, int len) { |
|
230 |
try { |
|
231 |
ensureInitialized(); |
|
232 |
token.p11.C_SignUpdate(session.id(), 0, b, ofs, len); |
|
233 |
} catch (PKCS11Exception e) { |
|
234 |
throw new ProviderException("update() failed", e); |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
// see JCE spec |
|
239 |
protected void engineUpdate(ByteBuffer byteBuffer) { |
|
240 |
try { |
|
241 |
ensureInitialized(); |
|
242 |
int len = byteBuffer.remaining(); |
|
243 |
if (len <= 0) { |
|
244 |
return; |
|
245 |
} |
|
246 |
if (byteBuffer instanceof DirectBuffer == false) { |
|
247 |
super.engineUpdate(byteBuffer); |
|
248 |
return; |
|
249 |
} |
|
250 |
long addr = ((DirectBuffer)byteBuffer).address(); |
|
251 |
int ofs = byteBuffer.position(); |
|
252 |
token.p11.C_SignUpdate(session.id(), addr + ofs, null, 0, len); |
|
253 |
byteBuffer.position(ofs + len); |
|
254 |
} catch (PKCS11Exception e) { |
|
255 |
throw new ProviderException("update() failed", e); |
|
256 |
} |
|
257 |
} |
|
258 |
} |