1 | /* |
2 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
3 | */ |
4 | |
5 | /* Copyright (c) 2002 Graz University of Technology. All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
14 | * this list of conditions and the following disclaimer in the documentation |
15 | * and/or other materials provided with the distribution. |
16 | * |
17 | * 3. The end-user documentation included with the redistribution, if any, must |
18 | * include the following acknowledgment: |
19 | * |
20 | * "This product includes software developed by IAIK of Graz University of |
21 | * Technology." |
22 | * |
23 | * Alternately, this acknowledgment may appear in the software itself, if |
24 | * and wherever such third-party acknowledgments normally appear. |
25 | * |
26 | * 4. The names "Graz University of Technology" and "IAIK of Graz University of |
27 | * Technology" must not be used to endorse or promote products derived from |
28 | * this software without prior written permission. |
29 | * |
30 | * 5. Products derived from this software may not be called |
31 | * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior |
32 | * written permission of Graz University of Technology. |
33 | * |
34 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
35 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
36 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
37 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE |
38 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
39 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
40 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
41 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
42 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
43 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
44 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
45 | * POSSIBILITY OF SUCH DAMAGE. |
46 | * =========================================================================== |
47 | */ |
48 | |
49 | #include "pkcs11wrapper.h" |
50 | |
51 | #include <stdio.h> |
52 | #include <stdlib.h> |
53 | #include <string.h> |
54 | #include <assert.h> |
55 | |
56 | #include "sun_security_pkcs11_wrapper_PKCS11.h" |
57 | |
58 | #ifdef P11_ENABLE_C_DIGESTENCRYPTUPDATE |
59 | /* |
60 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
61 | * Method: C_DigestEncryptUpdate |
62 | * Signature: (J[B)[B |
63 | * Parametermapping: *PKCS11* |
64 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
65 | * @param jbyteArray jPart CK_BYTE_PTR pPart |
66 | * CK_ULONG ulPartLen |
67 | * @return jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart |
68 | * CK_ULONG_PTR pulEncryptedPartLen |
69 | */ |
70 | JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestEncryptUpdate |
71 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jPart) |
72 | { |
73 | CK_SESSION_HANDLE ckSessionHandle; |
74 | CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart; |
75 | CK_ULONG ckPartLength, ckEncryptedPartLength = 0; |
76 | jbyteArray jEncryptedPart = NULL; |
77 | CK_RV rv; |
78 | |
79 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
80 | if (ckpFunctions == NULL) { return NULL; } |
81 | |
82 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
83 | jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength); |
84 | if ((*env)->ExceptionCheck(env)) { return NULL; } |
85 | |
86 | rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength); |
87 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { |
88 | free(ckpPart); |
89 | return NULL; |
90 | } |
91 | |
92 | ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE)); |
93 | if (ckpEncryptedPart == NULL) { |
94 | free(ckpPart); |
95 | throwOutOfMemoryError(env, 0); |
96 | return NULL; |
97 | } |
98 | |
99 | rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength); |
100 | if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { |
101 | jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength); |
102 | } |
103 | free(ckpPart); |
104 | free(ckpEncryptedPart); |
105 | |
106 | return jEncryptedPart ; |
107 | } |
108 | #endif |
109 | |
110 | #ifdef P11_ENABLE_C_DECRYPTDIGESTUPDATE |
111 | /* |
112 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
113 | * Method: C_DecryptDigestUpdate |
114 | * Signature: (J[B)[B |
115 | * Parametermapping: *PKCS11* |
116 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
117 | * @param jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart |
118 | * CK_ULONG ulEncryptedPartLen |
119 | * @return jbyteArray jPart CK_BYTE_PTR pPart |
120 | * CK_ULONG_PTR pulPartLen |
121 | */ |
122 | JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptDigestUpdate |
123 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jEncryptedPart) |
124 | { |
125 | CK_SESSION_HANDLE ckSessionHandle; |
126 | CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR; |
127 | CK_ULONG ckPartLength = 0, ckEncryptedPartLength; |
128 | jbyteArray jPart = NULL; |
129 | CK_RV rv; |
130 | |
131 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
132 | if (ckpFunctions == NULL) { return NULL; } |
133 | |
134 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
135 | jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength); |
136 | if ((*env)->ExceptionCheck(env)) { return NULL; } |
137 | |
138 | rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength); |
139 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { |
140 | free(ckpEncryptedPart); |
141 | return NULL; |
142 | } |
143 | |
144 | ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE)); |
145 | if (ckpPart == NULL) { |
146 | free(ckpEncryptedPart); |
147 | throwOutOfMemoryError(env, 0); |
148 | return NULL; |
149 | } |
150 | |
151 | rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength); |
152 | if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { |
153 | jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength); |
154 | } |
155 | free(ckpEncryptedPart); |
156 | free(ckpPart); |
157 | |
158 | return jPart ; |
159 | } |
160 | #endif |
161 | |
162 | #ifdef P11_ENABLE_C_SIGNENCRYPTUPDATE |
163 | /* |
164 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
165 | * Method: C_SignEncryptUpdate |
166 | * Signature: (J[B)[B |
167 | * Parametermapping: *PKCS11* |
168 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
169 | * @param jbyteArray jPart CK_BYTE_PTR pPart |
170 | * CK_ULONG ulPartLen |
171 | * @return jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart |
172 | * CK_ULONG_PTR pulEncryptedPartLen |
173 | */ |
174 | JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignEncryptUpdate |
175 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jPart) |
176 | { |
177 | CK_SESSION_HANDLE ckSessionHandle; |
178 | CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart; |
179 | CK_ULONG ckPartLength, ckEncryptedPartLength = 0; |
180 | jbyteArray jEncryptedPart = NULL; |
181 | CK_RV rv; |
182 | |
183 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
184 | if (ckpFunctions == NULL) { return NULL; } |
185 | |
186 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
187 | jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength); |
188 | if ((*env)->ExceptionCheck(env)) { return NULL; } |
189 | |
190 | rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength); |
191 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { |
192 | free(ckpPart); |
193 | return NULL; |
194 | } |
195 | |
196 | ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE)); |
197 | if (ckpEncryptedPart == NULL) { |
198 | free(ckpPart); |
199 | throwOutOfMemoryError(env, 0); |
200 | return NULL; |
201 | } |
202 | |
203 | rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength); |
204 | if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { |
205 | jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength); |
206 | } |
207 | free(ckpPart); |
208 | free(ckpEncryptedPart); |
209 | |
210 | return jEncryptedPart ; |
211 | } |
212 | #endif |
213 | |
214 | #ifdef P11_ENABLE_C_DECRYPTVERIFYUPDATE |
215 | /* |
216 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
217 | * Method: C_DecryptVerifyUpdate |
218 | * Signature: (J[B)[B |
219 | * Parametermapping: *PKCS11* |
220 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
221 | * @param jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart |
222 | * CK_ULONG ulEncryptedPartLen |
223 | * @return jbyteArray jPart CK_BYTE_PTR pPart |
224 | * CK_ULONG_PTR pulPartLen |
225 | */ |
226 | JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptVerifyUpdate |
227 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jEncryptedPart) |
228 | { |
229 | CK_SESSION_HANDLE ckSessionHandle; |
230 | CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR; |
231 | CK_ULONG ckPartLength = 0, ckEncryptedPartLength; |
232 | jbyteArray jPart = NULL; |
233 | CK_RV rv; |
234 | |
235 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
236 | if (ckpFunctions == NULL) { return NULL; } |
237 | |
238 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
239 | jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength); |
240 | if ((*env)->ExceptionCheck(env)) { return NULL; } |
241 | |
242 | rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength); |
243 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { |
244 | free(ckpEncryptedPart); |
245 | return NULL; |
246 | } |
247 | |
248 | ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE)); |
249 | if (ckpPart == NULL) { |
250 | free(ckpEncryptedPart); |
251 | throwOutOfMemoryError(env, 0); |
252 | return NULL; |
253 | } |
254 | |
255 | rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength); |
256 | |
257 | if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { |
258 | jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength); |
259 | } |
260 | free(ckpEncryptedPart); |
261 | free(ckpPart); |
262 | |
263 | return jPart ; |
264 | } |
265 | #endif |
266 | |
267 | #ifdef P11_ENABLE_C_GETFUNCTIONSTATUS |
268 | /* |
269 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
270 | * Method: C_GetFunctionStatus |
271 | * Signature: (J)V |
272 | * Parametermapping: *PKCS11* |
273 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
274 | */ |
275 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetFunctionStatus |
276 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
277 | { |
278 | CK_SESSION_HANDLE ckSessionHandle; |
279 | CK_RV rv; |
280 | |
281 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
282 | if (ckpFunctions == NULL) { return; } |
283 | |
284 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
285 | |
286 | /* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */ |
287 | rv = (*ckpFunctions->C_GetFunctionStatus)(ckSessionHandle); |
288 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
289 | } |
290 | #endif |
291 | |
292 | #ifdef P11_ENABLE_C_CANCELFUNCTION |
293 | /* |
294 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
295 | * Method: C_CancelFunction |
296 | * Signature: (J)V |
297 | * Parametermapping: *PKCS11* |
298 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
299 | */ |
300 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CancelFunction |
301 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
302 | { |
303 | CK_SESSION_HANDLE ckSessionHandle; |
304 | CK_RV rv; |
305 | |
306 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
307 | if (ckpFunctions == NULL) { return; } |
308 | |
309 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
310 | |
311 | /* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */ |
312 | rv = (*ckpFunctions->C_CancelFunction)(ckSessionHandle); |
313 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
314 | } |
315 | #endif |
316 | |