J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Portions Copyright 2003 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 | #include "pkcs11wrapper.h" |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | #include <assert.h> |
| 54 | |
| 55 | #include "sun_security_pkcs11_wrapper_PKCS11.h" |
| 56 | |
| 57 | #ifdef P11_ENABLE_C_DIGESTINIT |
| 58 | /* |
| 59 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 60 | * Method: C_DigestInit |
| 61 | * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;)V |
| 62 | * Parametermapping: *PKCS11* |
| 63 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 64 | * @param jobject jMechanism CK_MECHANISM_PTR pMechanism |
| 65 | */ |
| 66 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestInit |
| 67 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism) |
| 68 | { |
| 69 | CK_SESSION_HANDLE ckSessionHandle; |
| 70 | CK_MECHANISM ckMechanism; |
| 71 | CK_RV rv; |
| 72 | |
| 73 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 74 | if (ckpFunctions == NULL) { return; } |
| 75 | |
| 76 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 77 | jMechanismToCKMechanism(env, jMechanism, &ckMechanism); |
| 78 | |
| 79 | rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism); |
| 80 | |
| 81 | if (ckMechanism.pParameter != NULL_PTR) { |
| 82 | free(ckMechanism.pParameter); |
| 83 | } |
| 84 | |
| 85 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | #ifdef P11_ENABLE_C_DIGEST |
| 90 | /* |
| 91 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 92 | * Method: C_Digest |
| 93 | * Signature: (J[BII[BII)I |
| 94 | * Parametermapping: *PKCS11* |
| 95 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 96 | * @param jbyteArray jData CK_BYTE_PTR pData |
| 97 | * CK_ULONG ulDataLen |
| 98 | * @return jbyteArray jDigest CK_BYTE_PTR pDigest |
| 99 | * CK_ULONG_PTR pulDigestLen |
| 100 | */ |
| 101 | JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestSingle |
| 102 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jbyteArray jIn, jint jInOfs, jint jInLen, jbyteArray jDigest, jint jDigestOfs, jint jDigestLen) |
| 103 | { |
| 104 | CK_SESSION_HANDLE ckSessionHandle; |
| 105 | CK_RV rv; |
| 106 | CK_BYTE_PTR bufP; |
| 107 | CK_BYTE BUF[MAX_STACK_BUFFER_LEN]; |
| 108 | CK_BYTE DIGESTBUF[MAX_DIGEST_LEN]; |
| 109 | CK_ULONG ckDigestLength = min(MAX_DIGEST_LEN, jDigestLen); |
| 110 | CK_MECHANISM ckMechanism; |
| 111 | |
| 112 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 113 | if (ckpFunctions == NULL) { return 0; } |
| 114 | |
| 115 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 116 | jMechanismToCKMechanism(env, jMechanism, &ckMechanism); |
| 117 | |
| 118 | rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism); |
| 119 | |
| 120 | if (ckMechanism.pParameter != NULL_PTR) { |
| 121 | free(ckMechanism.pParameter); |
| 122 | } |
| 123 | |
| 124 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0; } |
| 125 | |
| 126 | if (jInLen <= MAX_STACK_BUFFER_LEN) { |
| 127 | bufP = BUF; |
| 128 | } else { |
| 129 | /* always use single part op, even for large data */ |
| 130 | bufP = (CK_BYTE_PTR)malloc((size_t)jInLen); |
| 131 | } |
| 132 | |
| 133 | (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)bufP); |
| 134 | rv = (*ckpFunctions->C_Digest)(ckSessionHandle, bufP, jInLen, DIGESTBUF, &ckDigestLength); |
| 135 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { |
| 136 | if (bufP != BUF) { |
| 137 | free(bufP); |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | (*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)DIGESTBUF); |
| 143 | |
| 144 | if (bufP != BUF) { |
| 145 | free(bufP); |
| 146 | } |
| 147 | return ckDigestLength; |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | #ifdef P11_ENABLE_C_DIGESTUPDATE |
| 152 | /* |
| 153 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 154 | * Method: C_DigestUpdate |
| 155 | * Signature: (J[B)V |
| 156 | * Parametermapping: *PKCS11* |
| 157 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 158 | * @param jbyteArray jData CK_BYTE_PTR pData |
| 159 | * CK_ULONG ulDataLen |
| 160 | */ |
| 161 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestUpdate |
| 162 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen) |
| 163 | { |
| 164 | CK_SESSION_HANDLE ckSessionHandle; |
| 165 | CK_RV rv; |
| 166 | CK_BYTE_PTR bufP; |
| 167 | CK_BYTE BUF[MAX_STACK_BUFFER_LEN]; |
| 168 | jsize bufLen; |
| 169 | |
| 170 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 171 | if (ckpFunctions == NULL) { return; } |
| 172 | |
| 173 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 174 | |
| 175 | if (directIn != 0) { |
| 176 | rv = (*ckpFunctions->C_DigestUpdate)(ckSessionHandle, (CK_BYTE_PTR)directIn, jInLen); |
| 177 | ckAssertReturnValueOK(env, rv); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (jInLen <= MAX_STACK_BUFFER_LEN) { |
| 182 | bufLen = MAX_STACK_BUFFER_LEN; |
| 183 | bufP = BUF; |
| 184 | } else { |
| 185 | bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen); |
| 186 | bufP = (CK_BYTE_PTR)malloc((size_t)bufLen); |
| 187 | } |
| 188 | |
| 189 | while (jInLen > 0) { |
| 190 | jsize chunkLen = min(bufLen, jInLen); |
| 191 | (*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP); |
| 192 | rv = (*ckpFunctions->C_DigestUpdate)(ckSessionHandle, bufP, chunkLen); |
| 193 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { |
| 194 | if (bufP != BUF) { |
| 195 | free(bufP); |
| 196 | } |
| 197 | return; |
| 198 | } |
| 199 | jInOfs += chunkLen; |
| 200 | jInLen -= chunkLen; |
| 201 | } |
| 202 | |
| 203 | if (bufP != BUF) { |
| 204 | free(bufP); |
| 205 | } |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | #ifdef P11_ENABLE_C_DIGESTKEY |
| 210 | /* |
| 211 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 212 | * Method: C_DigestKey |
| 213 | * Signature: (JJ)V |
| 214 | * Parametermapping: *PKCS11* |
| 215 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 216 | * @param jlong jKeyHandle CK_OBJECT_HANDLE hKey |
| 217 | */ |
| 218 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestKey |
| 219 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jKeyHandle) |
| 220 | { |
| 221 | CK_SESSION_HANDLE ckSessionHandle; |
| 222 | CK_ULONG ckKeyHandle; |
| 223 | CK_RV rv; |
| 224 | |
| 225 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 226 | if (ckpFunctions == NULL) { return; } |
| 227 | |
| 228 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 229 | ckKeyHandle = jLongToCKULong(jKeyHandle); |
| 230 | |
| 231 | rv = (*ckpFunctions->C_DigestKey)(ckSessionHandle, ckKeyHandle); |
| 232 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
| 233 | } |
| 234 | #endif |
| 235 | |
| 236 | #ifdef P11_ENABLE_C_DIGESTFINAL |
| 237 | /* |
| 238 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 239 | * Method: C_DigestFinal |
| 240 | * Signature: (J[BII)I |
| 241 | * Parametermapping: *PKCS11* |
| 242 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 243 | * @return jbyteArray jDigest CK_BYTE_PTR pDigest |
| 244 | * CK_ULONG_PTR pulDigestLen |
| 245 | */ |
| 246 | JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestFinal |
| 247 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jDigest, jint jDigestOfs, jint jDigestLen) |
| 248 | { |
| 249 | CK_SESSION_HANDLE ckSessionHandle; |
| 250 | CK_RV rv; |
| 251 | CK_BYTE BUF[MAX_DIGEST_LEN]; |
| 252 | CK_ULONG ckDigestLength = min(MAX_DIGEST_LEN, jDigestLen); |
| 253 | |
| 254 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 255 | if (ckpFunctions == NULL) { return 0; } |
| 256 | |
| 257 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 258 | |
| 259 | rv = (*ckpFunctions->C_DigestFinal)(ckSessionHandle, BUF, &ckDigestLength); |
| 260 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0 ; } |
| 261 | |
| 262 | (*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)BUF); |
| 263 | |
| 264 | return ckDigestLength; |
| 265 | } |
| 266 | #endif |
| 267 | |
| 268 | #ifdef P11_ENABLE_C_SEEDRANDOM |
| 269 | /* |
| 270 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 271 | * Method: C_SeedRandom |
| 272 | * Signature: (J[B)V |
| 273 | * Parametermapping: *PKCS11* |
| 274 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 275 | * @param jbyteArray jSeed CK_BYTE_PTR pSeed |
| 276 | * CK_ULONG ulSeedLen |
| 277 | */ |
| 278 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SeedRandom |
| 279 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jSeed) |
| 280 | { |
| 281 | CK_SESSION_HANDLE ckSessionHandle; |
| 282 | CK_BYTE_PTR ckpSeed = NULL_PTR; |
| 283 | CK_ULONG ckSeedLength; |
| 284 | CK_RV rv; |
| 285 | |
| 286 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 287 | if (ckpFunctions == NULL) { return; } |
| 288 | |
| 289 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 290 | jByteArrayToCKByteArray(env, jSeed, &ckpSeed, &ckSeedLength); |
| 291 | |
| 292 | rv = (*ckpFunctions->C_SeedRandom)(ckSessionHandle, ckpSeed, ckSeedLength); |
| 293 | |
| 294 | free(ckpSeed); |
| 295 | |
| 296 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | #ifdef P11_ENABLE_C_GENERATERANDOM |
| 301 | /* |
| 302 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 303 | * Method: C_GenerateRandom |
| 304 | * Signature: (J[B)V |
| 305 | * Parametermapping: *PKCS11* |
| 306 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 307 | * @param jbyteArray jRandomData CK_BYTE_PTR pRandomData |
| 308 | * CK_ULONG ulRandomDataLen |
| 309 | */ |
| 310 | JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateRandom |
| 311 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jRandomData) |
| 312 | { |
| 313 | CK_SESSION_HANDLE ckSessionHandle; |
| 314 | jbyte *jRandomBuffer; |
| 315 | jlong jRandomBufferLength; |
| 316 | CK_RV rv; |
| 317 | |
| 318 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 319 | if (ckpFunctions == NULL) { return; } |
| 320 | |
| 321 | ckSessionHandle = jLongToCKULong(jSessionHandle); |
| 322 | |
| 323 | jRandomBufferLength = (*env)->GetArrayLength(env, jRandomData); |
| 324 | jRandomBuffer = (*env)->GetByteArrayElements(env, jRandomData, NULL); |
| 325 | |
| 326 | rv = (*ckpFunctions->C_GenerateRandom)(ckSessionHandle, |
| 327 | (CK_BYTE_PTR) jRandomBuffer, |
| 328 | jLongToCKULong(jRandomBufferLength)); |
| 329 | |
| 330 | /* copy back generated bytes */ |
| 331 | (*env)->ReleaseByteArrayElements(env, jRandomData, jRandomBuffer, 0); |
| 332 | |
| 333 | if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } |
| 334 | } |
| 335 | #endif |