J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Portions Copyright 2003-2007 Sun Microsystems, Inc. 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; |
| 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 | |
| 85 | rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength); |
| 86 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } |
| 87 | |
| 88 | ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE)); |
| 89 | |
| 90 | rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength); |
| 91 | |
| 92 | jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength); |
| 93 | free(ckpPart); |
| 94 | free(ckpEncryptedPart); |
| 95 | |
| 96 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } |
| 97 | |
| 98 | return jEncryptedPart ; |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | #ifdef P11_ENABLE_C_DECRYPTDIGESTUPDATE |
| 103 | /* |
| 104 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 105 | * Method: C_DecryptDigestUpdate |
| 106 | * Signature: (J[B)[B |
| 107 | * Parametermapping: *PKCS11* |
| 108 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 109 | * @param jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart |
| 110 | * CK_ULONG ulEncryptedPartLen |
| 111 | * @return jbyteArray jPart CK_BYTE_PTR pPart |
| 112 | * CK_ULONG_PTR pulPartLen |
| 113 | */ |
| 114 | JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptDigestUpdate |
| 115 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jEncryptedPart) |
| 116 | { |
| 117 | CK_SESSION_HANDLE ckSessionHandle; |
| 118 | CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR; |
| 119 | CK_ULONG ckPartLength = 0, ckEncryptedPartLength; |
| 120 | jbyteArray jPart; |
| 121 | CK_RV rv; |
| 122 | |
| 123 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 124 | if (ckpFunctions == NULL) { return NULL; } |
| 125 | |
| 126 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 127 | jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength); |
| 128 | |
| 129 | rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength); |
| 130 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL; } |
| 131 | |
| 132 | ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE)); |
| 133 | |
| 134 | rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength); |
| 135 | |
| 136 | jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength); |
| 137 | free(ckpPart); |
| 138 | free(ckpEncryptedPart); |
| 139 | |
| 140 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } |
| 141 | |
| 142 | return jPart ; |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | #ifdef P11_ENABLE_C_SIGNENCRYPTUPDATE |
| 147 | /* |
| 148 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 149 | * Method: C_SignEncryptUpdate |
| 150 | * Signature: (J[B)[B |
| 151 | * Parametermapping: *PKCS11* |
| 152 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 153 | * @param jbyteArray jPart CK_BYTE_PTR pPart |
| 154 | * CK_ULONG ulPartLen |
| 155 | * @return jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart |
| 156 | * CK_ULONG_PTR pulEncryptedPartLen |
| 157 | */ |
| 158 | JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignEncryptUpdate |
| 159 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jPart) |
| 160 | { |
| 161 | CK_SESSION_HANDLE ckSessionHandle; |
| 162 | CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart; |
| 163 | CK_ULONG ckPartLength, ckEncryptedPartLength = 0; |
| 164 | jbyteArray jEncryptedPart; |
| 165 | CK_RV rv; |
| 166 | |
| 167 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 168 | if (ckpFunctions == NULL) { return NULL; } |
| 169 | |
| 170 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 171 | jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength); |
| 172 | |
| 173 | rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength); |
| 174 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } |
| 175 | |
| 176 | ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE)); |
| 177 | |
| 178 | rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength); |
| 179 | |
| 180 | jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength); |
| 181 | free(ckpPart); |
| 182 | free(ckpEncryptedPart); |
| 183 | |
| 184 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } |
| 185 | |
| 186 | return jEncryptedPart ; |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #ifdef P11_ENABLE_C_DECRYPTVERIFYUPDATE |
| 191 | /* |
| 192 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 193 | * Method: C_DecryptVerifyUpdate |
| 194 | * Signature: (J[B)[B |
| 195 | * Parametermapping: *PKCS11* |
| 196 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 197 | * @param jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart |
| 198 | * CK_ULONG ulEncryptedPartLen |
| 199 | * @return jbyteArray jPart CK_BYTE_PTR pPart |
| 200 | * CK_ULONG_PTR pulPartLen |
| 201 | */ |
| 202 | JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptVerifyUpdate |
| 203 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jEncryptedPart) |
| 204 | { |
| 205 | CK_SESSION_HANDLE ckSessionHandle; |
| 206 | CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR; |
| 207 | CK_ULONG ckPartLength = 0, ckEncryptedPartLength; |
| 208 | jbyteArray jPart; |
| 209 | CK_RV rv; |
| 210 | |
| 211 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 212 | if (ckpFunctions == NULL) { return NULL; } |
| 213 | |
| 214 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 215 | jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength); |
| 216 | |
| 217 | rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength); |
| 218 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } |
| 219 | |
| 220 | ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE)); |
| 221 | |
| 222 | rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength); |
| 223 | |
| 224 | jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength); |
| 225 | free(ckpPart); |
| 226 | free(ckpEncryptedPart); |
| 227 | |
| 228 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } |
| 229 | |
| 230 | return jPart ; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | #ifdef P11_ENABLE_C_GETFUNCTIONSTATUS |
| 235 | /* |
| 236 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 237 | * Method: C_GetFunctionStatus |
| 238 | * Signature: (J)V |
| 239 | * Parametermapping: *PKCS11* |
| 240 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 241 | */ |
| 242 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetFunctionStatus |
| 243 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
| 244 | { |
| 245 | CK_SESSION_HANDLE ckSessionHandle; |
| 246 | CK_RV rv; |
| 247 | |
| 248 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 249 | if (ckpFunctions == NULL) { return; } |
| 250 | |
| 251 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 252 | |
| 253 | /* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */ |
| 254 | rv = (*ckpFunctions->C_GetFunctionStatus)(ckSessionHandle); |
| 255 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | #ifdef P11_ENABLE_C_CANCELFUNCTION |
| 260 | /* |
| 261 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 262 | * Method: C_CancelFunction |
| 263 | * Signature: (J)V |
| 264 | * Parametermapping: *PKCS11* |
| 265 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 266 | */ |
| 267 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CancelFunction |
| 268 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
| 269 | { |
| 270 | CK_SESSION_HANDLE ckSessionHandle; |
| 271 | CK_RV rv; |
| 272 | |
| 273 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 274 | if (ckpFunctions == NULL) { return; } |
| 275 | |
| 276 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 277 | |
| 278 | /* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */ |
| 279 | rv = (*ckpFunctions->C_CancelFunction)(ckSessionHandle); |
| 280 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
| 281 | } |
| 282 | #endif |