jdk
672 строки · 22.7 Кб
1/*
2* Copyright (c) 2003, 2024, 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#include "pkcs11wrapper.h"49
50#include <stdio.h>51#include <stdlib.h>52#include <string.h>53#include <assert.h>54#include "jlong.h"55
56#include "sun_security_pkcs11_wrapper_PKCS11.h"57
58#ifdef P11_ENABLE_C_SIGNINIT59/*
60* Class: sun_security_pkcs11_wrapper_PKCS11
61* Method: C_SignInit
62* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J)V
63* Parametermapping: *PKCS11*
64* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
65* @param jobject jMechanism CK_MECHANISM_PTR pMechanism
66* @param jlong jKeyHandle CK_OBJECT_HANDLE hKey
67*/
68JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignInit69(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)70{
71CK_SESSION_HANDLE ckSessionHandle;72CK_MECHANISM_PTR ckpMechanism = NULL;73CK_OBJECT_HANDLE ckKeyHandle;74CK_RV rv;75
76CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);77if (ckpFunctions == NULL) { return; }78
79TRACE0("DEBUG: C_SignInit\n");80
81ckSessionHandle = jLongToCKULong(jSessionHandle);82
83ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);84if ((*env)->ExceptionCheck(env)) { return; }85
86ckKeyHandle = jLongToCKULong(jKeyHandle);87
88rv = (*ckpFunctions->C_SignInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);89
90TRACE1("DEBUG C_SignInit: freed pMech = %p\n", ckpMechanism);91freeCKMechanismPtr(ckpMechanism);92if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }93TRACE0("FINISHED\n");94}
95#endif96
97#ifdef P11_ENABLE_C_SIGN98/*
99* Class: sun_security_pkcs11_wrapper_PKCS11
100* Method: C_Sign
101* Signature: (J[BI)[B
102* Parametermapping: *PKCS11*
103* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
104* @param jbyteArray jData CK_BYTE_PTR pData
105* CK_ULONG ulDataLen
106* @return jbyteArray jSignature CK_BYTE_PTR pSignature
107* CK_ULONG_PTR pulSignatureLen
108*/
109JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Sign110(JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jData)111{
112CK_SESSION_HANDLE ckSessionHandle;113CK_BYTE_PTR ckpData = NULL_PTR;114CK_ULONG ckDataLength;115CK_BYTE_PTR bufP;116CK_ULONG ckSignatureLength;117CK_BYTE BUF[MAX_STACK_BUFFER_LEN];118jbyteArray jSignature = NULL;119CK_RV rv;120
121CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);122if (ckpFunctions == NULL) { return NULL; }123
124TRACE0("DEBUG: C_Sign\n");125
126ckSessionHandle = jLongToCKULong(jSessionHandle);127jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength);128if ((*env)->ExceptionCheck(env)) {129return NULL;130}131
132TRACE1("DEBUG C_Sign: data length = %lu\n", ckDataLength);133
134// unknown signature length135bufP = BUF;136ckSignatureLength = MAX_STACK_BUFFER_LEN;137
138rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength,139bufP, &ckSignatureLength);140
141TRACE1("DEBUG C_Sign: ret rv=0x%lX\n", rv);142
143if (rv == CKR_BUFFER_TOO_SMALL) {144bufP = (CK_BYTE_PTR) malloc(ckSignatureLength);145if (bufP == NULL) {146p11ThrowOutOfMemoryError(env, 0);147goto cleanup;148}149rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength,150bufP, &ckSignatureLength);151}152
153if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {154jSignature = ckByteArrayToJByteArray(env, bufP, ckSignatureLength);155TRACE1("DEBUG C_Sign: signature length = %lu\n", ckSignatureLength);156}157
158cleanup:159free(ckpData);160if (bufP != BUF) { free(bufP); }161
162TRACE0("FINISHED\n");163return jSignature;164}
165#endif166
167#ifdef P11_ENABLE_C_SIGNUPDATE168/*
169* Class: sun_security_pkcs11_wrapper_PKCS11
170* Method: C_SignUpdate
171* Signature: (J[BII)V
172* Parametermapping: *PKCS11*
173* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
174* @param jbyteArray jPart CK_BYTE_PTR pPart
175* CK_ULONG ulPartLen
176*/
177JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignUpdate178(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen)179{
180CK_SESSION_HANDLE ckSessionHandle;181CK_RV rv;182CK_BYTE_PTR bufP;183CK_BYTE BUF[MAX_STACK_BUFFER_LEN];184jsize bufLen;185
186CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);187if (ckpFunctions == NULL) { return; }188
189ckSessionHandle = jLongToCKULong(jSessionHandle);190
191if (directIn != 0) {192rv = (*ckpFunctions->C_SignUpdate)(ckSessionHandle, (CK_BYTE_PTR) jlong_to_ptr(directIn), jInLen);193ckAssertReturnValueOK(env, rv);194return;195}196
197if (jInLen <= MAX_STACK_BUFFER_LEN) {198bufLen = MAX_STACK_BUFFER_LEN;199bufP = BUF;200} else {201bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);202bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);203if (bufP == NULL) {204p11ThrowOutOfMemoryError(env, 0);205return;206}207}208
209while (jInLen > 0) {210jsize chunkLen = min(bufLen, jInLen);211(*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);212if ((*env)->ExceptionCheck(env)) {213goto cleanup;214}215rv = (*ckpFunctions->C_SignUpdate)(ckSessionHandle, bufP, chunkLen);216if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {217goto cleanup;218}219jInOfs += chunkLen;220jInLen -= chunkLen;221}222
223cleanup:224if (bufP != BUF) { free(bufP); }225
226return;227}
228#endif229
230#ifdef P11_ENABLE_C_SIGNFINAL231/*
232* Class: sun_security_pkcs11_wrapper_PKCS11
233* Method: C_SignFinal
234* Signature: (J)[B
235* Parametermapping: *PKCS11*
236* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
237* @return jbyteArray jSignature CK_BYTE_PTR pSignature
238* CK_ULONG_PTR pulSignatureLen
239*/
240JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignFinal241(JNIEnv *env, jobject obj, jlong jSessionHandle, jint jExpectedLength)242{
243CK_SESSION_HANDLE ckSessionHandle;244jbyteArray jSignature = NULL;245CK_RV rv;246CK_BYTE BUF[MAX_STACK_BUFFER_LEN];247CK_BYTE_PTR bufP = BUF;248CK_ULONG ckSignatureLength = MAX_STACK_BUFFER_LEN;249
250CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);251if (ckpFunctions == NULL) { return NULL; }252
253ckSessionHandle = jLongToCKULong(jSessionHandle);254
255if ((jExpectedLength > 0) && ((CK_ULONG)jExpectedLength < ckSignatureLength)) {256ckSignatureLength = jExpectedLength;257}258
259rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength);260if (rv == CKR_BUFFER_TOO_SMALL) {261bufP = (CK_BYTE_PTR) malloc(ckSignatureLength);262if (bufP == NULL) {263p11ThrowOutOfMemoryError(env, 0);264return NULL;265}266rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength);267}268if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {269jSignature = ckByteArrayToJByteArray(env, bufP, ckSignatureLength);270}271
272if (bufP != BUF) { free(bufP); }273
274return jSignature;275}
276#endif277
278#ifdef P11_ENABLE_C_SIGNRECOVERINIT279/*
280* Class: sun_security_pkcs11_wrapper_PKCS11
281* Method: C_SignRecoverInit
282* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J)V
283* Parametermapping: *PKCS11*
284* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
285* @param jobject jMechanism CK_MECHANISM_PTR pMechanism
286* @param jlong jKeyHandle CK_OBJECT_HANDLE hKey
287*/
288JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecoverInit289(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)290{
291CK_SESSION_HANDLE ckSessionHandle;292CK_MECHANISM_PTR ckpMechanism = NULL;293CK_OBJECT_HANDLE ckKeyHandle;294CK_RV rv;295
296CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);297if (ckpFunctions == NULL) { return; }298
299TRACE0("DEBUG: C_SignRecoverInit\n");300
301ckSessionHandle = jLongToCKULong(jSessionHandle);302ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);303if ((*env)->ExceptionCheck(env)) { return; }304
305ckKeyHandle = jLongToCKULong(jKeyHandle);306
307rv = (*ckpFunctions->C_SignRecoverInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);308
309TRACE1("DEBUG C_SignRecoverInit: freed pMech = %p\n", ckpMechanism);310freeCKMechanismPtr(ckpMechanism);311if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }312TRACE0("FINISHED\n");313}
314#endif315
316#ifdef P11_ENABLE_C_SIGNRECOVER317/*
318* Class: sun_security_pkcs11_wrapper_PKCS11
319* Method: C_SignRecover
320* Signature: (J[BII[BII)I
321* Parametermapping: *PKCS11*
322* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
323* @param jbyteArray jData CK_BYTE_PTR pData
324* CK_ULONG ulDataLen
325* @return jbyteArray jSignature CK_BYTE_PTR pSignature
326* CK_ULONG_PTR pulSignatureLen
327*/
328JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecover329(JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jIn, jint jInOfs, jint jInLen, jbyteArray jOut, jint jOutOfs, jint jOutLen)330{
331CK_SESSION_HANDLE ckSessionHandle;332CK_RV rv;333CK_BYTE INBUF[MAX_STACK_BUFFER_LEN];334CK_BYTE OUTBUF[MAX_STACK_BUFFER_LEN];335CK_BYTE_PTR inBufP;336CK_BYTE_PTR outBufP = OUTBUF;337CK_ULONG ckSignatureLength = 0;338
339CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);340if (ckpFunctions == NULL) { return 0; }341
342ckSessionHandle = jLongToCKULong(jSessionHandle);343
344if (jInLen <= MAX_STACK_BUFFER_LEN) {345inBufP = INBUF;346ckSignatureLength = MAX_STACK_BUFFER_LEN;347} else {348inBufP = (CK_BYTE_PTR) malloc((size_t)jInLen);349if (inBufP == NULL) {350p11ThrowOutOfMemoryError(env, 0);351return 0;352}353ckSignatureLength = jInLen;354}355
356(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);357if ((*env)->ExceptionCheck(env)) {358goto cleanup;359}360
361rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength);362/* re-alloc larger buffer if it fits into our Java buffer */363if ((rv == CKR_BUFFER_TOO_SMALL) && (ckSignatureLength <= jIntToCKULong(jOutLen))) {364outBufP = (CK_BYTE_PTR) malloc(ckSignatureLength);365if (outBufP == NULL) {366p11ThrowOutOfMemoryError(env, 0);367goto cleanup;368}369rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength);370}371if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {372(*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckSignatureLength, (jbyte *)outBufP);373}374cleanup:375if (inBufP != INBUF) { free(inBufP); }376if (outBufP != OUTBUF) { free(outBufP); }377
378return ckSignatureLength;379}
380#endif381
382#ifdef P11_ENABLE_C_VERIFYINIT383/*
384* Class: sun_security_pkcs11_wrapper_PKCS11
385* Method: C_VerifyInit
386* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J)V
387* Parametermapping: *PKCS11*
388* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
389* @param jobject jMechanism CK_MECHANISM_PTR pMechanism
390* @param jlong jKeyHandle CK_OBJECT_HANDLE hKey
391*/
392JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyInit393(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)394{
395CK_SESSION_HANDLE ckSessionHandle;396CK_MECHANISM_PTR ckpMechanism = NULL;397CK_OBJECT_HANDLE ckKeyHandle;398CK_RV rv;399
400CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);401if (ckpFunctions == NULL) { return; }402
403TRACE0("DEBUG: C_VerifyInit\n");404
405ckSessionHandle = jLongToCKULong(jSessionHandle);406ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);407if ((*env)->ExceptionCheck(env)) {408return;409}410
411ckKeyHandle = jLongToCKULong(jKeyHandle);412
413rv = (*ckpFunctions->C_VerifyInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);414
415TRACE1("DEBUG C_VerifyInit: freed pMech = %p\n", ckpMechanism);416freeCKMechanismPtr(ckpMechanism);417if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }418TRACE0("FINISHED\n");419}
420#endif421
422#ifdef P11_ENABLE_C_VERIFY423/*
424* Class: sun_security_pkcs11_wrapper_PKCS11
425* Method: C_Verify
426* Signature: (J[B[B)V
427* Parametermapping: *PKCS11*
428* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
429* @param jbyteArray jData CK_BYTE_PTR pData
430* CK_ULONG ulDataLen
431* @param jbyteArray jSignature CK_BYTE_PTR pSignature
432* CK_ULONG_PTR pulSignatureLen
433*/
434JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Verify435(JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jData, jbyteArray jSignature)436{
437CK_SESSION_HANDLE ckSessionHandle;438CK_BYTE_PTR ckpData = NULL_PTR;439CK_BYTE_PTR ckpSignature = NULL_PTR;440CK_ULONG ckDataLength;441CK_ULONG ckSignatureLength;442CK_RV rv = 0;443
444CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);445if (ckpFunctions == NULL) { return; }446
447ckSessionHandle = jLongToCKULong(jSessionHandle);448
449jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength);450if ((*env)->ExceptionCheck(env)) {451return;452}453
454jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength);455if ((*env)->ExceptionCheck(env)) {456goto cleanup;457}458
459/* verify the signature */460rv = (*ckpFunctions->C_Verify)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, ckSignatureLength);461
462cleanup:463free(ckpData);464free(ckpSignature);465
466ckAssertReturnValueOK(env, rv);467}
468#endif469
470#ifdef P11_ENABLE_C_VERIFYUPDATE471/*
472* Class: sun_security_pkcs11_wrapper_PKCS11
473* Method: C_VerifyUpdate
474* Signature: (J[BII)V
475* Parametermapping: *PKCS11*
476* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
477* @param jbyteArray jPart CK_BYTE_PTR pPart
478* CK_ULONG ulPartLen
479*/
480JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyUpdate481(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen)482{
483CK_SESSION_HANDLE ckSessionHandle;484CK_RV rv;485CK_BYTE_PTR bufP;486CK_BYTE BUF[MAX_STACK_BUFFER_LEN];487jsize bufLen;488
489CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);490if (ckpFunctions == NULL) { return; }491
492ckSessionHandle = jLongToCKULong(jSessionHandle);493
494if (directIn != 0) {495rv = (*ckpFunctions->C_VerifyUpdate)(ckSessionHandle, (CK_BYTE_PTR)jlong_to_ptr(directIn), jInLen);496ckAssertReturnValueOK(env, rv);497return;498}499
500if (jInLen <= MAX_STACK_BUFFER_LEN) {501bufLen = MAX_STACK_BUFFER_LEN;502bufP = BUF;503} else {504bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);505bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);506if (bufP == NULL) {507p11ThrowOutOfMemoryError(env, 0);508goto cleanup;509}510}511
512while (jInLen > 0) {513jsize chunkLen = min(bufLen, jInLen);514(*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);515if ((*env)->ExceptionCheck(env)) {516goto cleanup;517}518
519rv = (*ckpFunctions->C_VerifyUpdate)(ckSessionHandle, bufP, chunkLen);520if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {521goto cleanup;522}523jInOfs += chunkLen;524jInLen -= chunkLen;525}526
527cleanup:528if (bufP != BUF) { free(bufP); }529}
530#endif531
532#ifdef P11_ENABLE_C_VERIFYFINAL533/*
534* Class: sun_security_pkcs11_wrapper_PKCS11
535* Method: C_VerifyFinal
536* Signature: (J[B)V
537* Parametermapping: *PKCS11*
538* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
539* @param jbyteArray jSignature CK_BYTE_PTR pSignature
540* CK_ULONG ulSignatureLen
541*/
542JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyFinal543(JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jSignature)544{
545CK_SESSION_HANDLE ckSessionHandle;546CK_BYTE_PTR ckpSignature = NULL_PTR;547CK_ULONG ckSignatureLength;548CK_RV rv;549
550CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);551if (ckpFunctions == NULL) { return; }552
553ckSessionHandle = jLongToCKULong(jSessionHandle);554jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength);555if ((*env)->ExceptionCheck(env)) {556return;557}558
559/* verify the signature */560rv = (*ckpFunctions->C_VerifyFinal)(ckSessionHandle, ckpSignature, ckSignatureLength);561
562free(ckpSignature);563
564ckAssertReturnValueOK(env, rv);565}
566#endif567
568#ifdef P11_ENABLE_C_VERIFYRECOVERINIT569/*
570* Class: sun_security_pkcs11_wrapper_PKCS11
571* Method: C_VerifyRecoverInit
572* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J)V
573* Parametermapping: *PKCS11*
574* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
575* @param jobject jMechanism CK_MECHANISM_PTR pMechanism
576* @return jlong jKeyHandle CK_OBJECT_HANDLE hKey
577*/
578JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecoverInit579(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)580{
581CK_SESSION_HANDLE ckSessionHandle;582CK_MECHANISM_PTR ckpMechanism = NULL;583CK_OBJECT_HANDLE ckKeyHandle;584CK_RV rv;585
586CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);587if (ckpFunctions == NULL) { return; }588
589TRACE0("DEBUG: C_VerifyRecoverInit\n");590
591ckSessionHandle = jLongToCKULong(jSessionHandle);592ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);593if ((*env)->ExceptionCheck(env)) { return; }594
595ckKeyHandle = jLongToCKULong(jKeyHandle);596
597rv = (*ckpFunctions->C_VerifyRecoverInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);598
599TRACE1("DEBUG C_VerifyRecoverInit: freed pMech = %p\n", ckpMechanism);600freeCKMechanismPtr(ckpMechanism);601if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }602TRACE0("FINISHED\n");603}
604#endif605
606#ifdef P11_ENABLE_C_VERIFYRECOVER607/*
608* Class: sun_security_pkcs11_wrapper_PKCS11
609* Method: C_VerifyRecover
610* Signature: (J[BII[BII)I
611* Parametermapping: *PKCS11*
612* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
613* @param jbyteArray jSignature CK_BYTE_PTR pSignature
614* CK_ULONG ulSignatureLen
615* @return jbyteArray jData CK_BYTE_PTR pData
616* CK_ULONG_PTR pulDataLen
617*/
618JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecover619(JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jIn, jint jInOfs, jint jInLen, jbyteArray jOut, jint jOutOfs, jint jOutLen)620{
621CK_SESSION_HANDLE ckSessionHandle;622CK_RV rv;623CK_BYTE INBUF[MAX_STACK_BUFFER_LEN];624CK_BYTE OUTBUF[MAX_STACK_BUFFER_LEN];625CK_BYTE_PTR inBufP;626CK_BYTE_PTR outBufP = OUTBUF;627CK_ULONG ckDataLength = 0;628
629CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);630if (ckpFunctions == NULL) { return 0; }631
632ckSessionHandle = jLongToCKULong(jSessionHandle);633
634if (jInLen <= MAX_STACK_BUFFER_LEN) {635inBufP = INBUF;636ckDataLength = MAX_STACK_BUFFER_LEN;637} else {638inBufP = (CK_BYTE_PTR) malloc((size_t)jInLen);639if (inBufP == NULL) {640p11ThrowOutOfMemoryError(env, 0);641return 0;642}643ckDataLength = jInLen;644}645
646(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);647if ((*env)->ExceptionCheck(env)) {648goto cleanup;649}650
651rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength);652
653/* re-alloc larger buffer if it fits into our Java buffer */654if ((rv == CKR_BUFFER_TOO_SMALL) && (ckDataLength <= jIntToCKULong(jOutLen))) {655outBufP = (CK_BYTE_PTR) malloc(ckDataLength);656if (outBufP == NULL) {657p11ThrowOutOfMemoryError(env, 0);658goto cleanup;659}660rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength);661}662if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {663(*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckDataLength, (jbyte *)outBufP);664}665
666cleanup:667if (inBufP != INBUF) { free(inBufP); }668if (outBufP != OUTBUF) { free(outBufP); }669
670return ckDataLength;671}
672#endif673