jdk

Форк
0
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_SIGNINIT
59
/*
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
 */
68
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignInit
69
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)
70
{
71
    CK_SESSION_HANDLE ckSessionHandle;
72
    CK_MECHANISM_PTR ckpMechanism = NULL;
73
    CK_OBJECT_HANDLE ckKeyHandle;
74
    CK_RV rv;
75

76
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
77
    if (ckpFunctions == NULL) { return; }
78

79
    TRACE0("DEBUG: C_SignInit\n");
80

81
    ckSessionHandle = jLongToCKULong(jSessionHandle);
82

83
    ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);
84
    if ((*env)->ExceptionCheck(env)) { return; }
85

86
    ckKeyHandle = jLongToCKULong(jKeyHandle);
87

88
    rv = (*ckpFunctions->C_SignInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);
89

90
    TRACE1("DEBUG C_SignInit: freed pMech = %p\n", ckpMechanism);
91
    freeCKMechanismPtr(ckpMechanism);
92
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
93
    TRACE0("FINISHED\n");
94
}
95
#endif
96

97
#ifdef P11_ENABLE_C_SIGN
98
/*
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
 */
109
JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Sign
110
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jData)
111
{
112
    CK_SESSION_HANDLE ckSessionHandle;
113
    CK_BYTE_PTR ckpData = NULL_PTR;
114
    CK_ULONG ckDataLength;
115
    CK_BYTE_PTR bufP;
116
    CK_ULONG ckSignatureLength;
117
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
118
    jbyteArray jSignature = NULL;
119
    CK_RV rv;
120

121
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
122
    if (ckpFunctions == NULL) { return NULL; }
123

124
    TRACE0("DEBUG: C_Sign\n");
125

126
    ckSessionHandle = jLongToCKULong(jSessionHandle);
127
    jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength);
128
    if ((*env)->ExceptionCheck(env)) {
129
        return NULL;
130
    }
131

132
    TRACE1("DEBUG C_Sign: data length = %lu\n", ckDataLength);
133

134
    // unknown signature length
135
    bufP = BUF;
136
    ckSignatureLength = MAX_STACK_BUFFER_LEN;
137

138
    rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength,
139
        bufP, &ckSignatureLength);
140

141
    TRACE1("DEBUG C_Sign: ret rv=0x%lX\n", rv);
142

143
    if (rv == CKR_BUFFER_TOO_SMALL) {
144
        bufP = (CK_BYTE_PTR) malloc(ckSignatureLength);
145
        if (bufP == NULL) {
146
            p11ThrowOutOfMemoryError(env, 0);
147
            goto cleanup;
148
        }
149
        rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength,
150
            bufP, &ckSignatureLength);
151
    }
152

153
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
154
        jSignature = ckByteArrayToJByteArray(env, bufP, ckSignatureLength);
155
        TRACE1("DEBUG C_Sign: signature length = %lu\n", ckSignatureLength);
156
    }
157

158
cleanup:
159
    free(ckpData);
160
    if (bufP != BUF) { free(bufP); }
161

162
    TRACE0("FINISHED\n");
163
    return jSignature;
164
}
165
#endif
166

167
#ifdef P11_ENABLE_C_SIGNUPDATE
168
/*
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
 */
177
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignUpdate
178
  (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen)
179
{
180
    CK_SESSION_HANDLE ckSessionHandle;
181
    CK_RV rv;
182
    CK_BYTE_PTR bufP;
183
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
184
    jsize bufLen;
185

186
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
187
    if (ckpFunctions == NULL) { return; }
188

189
    ckSessionHandle = jLongToCKULong(jSessionHandle);
190

191
    if (directIn != 0) {
192
        rv = (*ckpFunctions->C_SignUpdate)(ckSessionHandle, (CK_BYTE_PTR) jlong_to_ptr(directIn), jInLen);
193
        ckAssertReturnValueOK(env, rv);
194
        return;
195
    }
196

197
    if (jInLen <= MAX_STACK_BUFFER_LEN) {
198
        bufLen = MAX_STACK_BUFFER_LEN;
199
        bufP = BUF;
200
    } else {
201
        bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);
202
        bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);
203
        if (bufP == NULL) {
204
            p11ThrowOutOfMemoryError(env, 0);
205
            return;
206
        }
207
    }
208

209
    while (jInLen > 0) {
210
        jsize chunkLen = min(bufLen, jInLen);
211
        (*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);
212
        if ((*env)->ExceptionCheck(env)) {
213
            goto cleanup;
214
        }
215
        rv = (*ckpFunctions->C_SignUpdate)(ckSessionHandle, bufP, chunkLen);
216
        if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
217
            goto cleanup;
218
        }
219
        jInOfs += chunkLen;
220
        jInLen -= chunkLen;
221
    }
222

223
cleanup:
224
    if (bufP != BUF) { free(bufP); }
225

226
    return;
227
}
228
#endif
229

230
#ifdef P11_ENABLE_C_SIGNFINAL
231
/*
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
 */
240
JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignFinal
241
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jint jExpectedLength)
242
{
243
    CK_SESSION_HANDLE ckSessionHandle;
244
    jbyteArray jSignature = NULL;
245
    CK_RV rv;
246
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
247
    CK_BYTE_PTR bufP = BUF;
248
    CK_ULONG ckSignatureLength = MAX_STACK_BUFFER_LEN;
249

250
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
251
    if (ckpFunctions == NULL) { return NULL; }
252

253
    ckSessionHandle = jLongToCKULong(jSessionHandle);
254

255
    if ((jExpectedLength > 0) && ((CK_ULONG)jExpectedLength < ckSignatureLength)) {
256
        ckSignatureLength = jExpectedLength;
257
    }
258

259
    rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength);
260
    if (rv == CKR_BUFFER_TOO_SMALL) {
261
        bufP = (CK_BYTE_PTR) malloc(ckSignatureLength);
262
        if (bufP == NULL) {
263
            p11ThrowOutOfMemoryError(env, 0);
264
            return NULL;
265
        }
266
        rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength);
267
    }
268
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
269
        jSignature = ckByteArrayToJByteArray(env, bufP, ckSignatureLength);
270
    }
271

272
    if (bufP != BUF) { free(bufP); }
273

274
    return jSignature;
275
}
276
#endif
277

278
#ifdef P11_ENABLE_C_SIGNRECOVERINIT
279
/*
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
 */
288
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecoverInit
289
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)
290
{
291
    CK_SESSION_HANDLE ckSessionHandle;
292
    CK_MECHANISM_PTR ckpMechanism = NULL;
293
    CK_OBJECT_HANDLE ckKeyHandle;
294
    CK_RV rv;
295

296
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
297
    if (ckpFunctions == NULL) { return; }
298

299
    TRACE0("DEBUG: C_SignRecoverInit\n");
300

301
    ckSessionHandle = jLongToCKULong(jSessionHandle);
302
    ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);
303
    if ((*env)->ExceptionCheck(env)) { return; }
304

305
    ckKeyHandle = jLongToCKULong(jKeyHandle);
306

307
    rv = (*ckpFunctions->C_SignRecoverInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);
308

309
    TRACE1("DEBUG C_SignRecoverInit: freed pMech = %p\n", ckpMechanism);
310
    freeCKMechanismPtr(ckpMechanism);
311
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
312
    TRACE0("FINISHED\n");
313
}
314
#endif
315

316
#ifdef P11_ENABLE_C_SIGNRECOVER
317
/*
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
 */
328
JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecover
329
  (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jIn, jint jInOfs, jint jInLen, jbyteArray jOut, jint jOutOfs, jint jOutLen)
330
{
331
    CK_SESSION_HANDLE ckSessionHandle;
332
    CK_RV rv;
333
    CK_BYTE INBUF[MAX_STACK_BUFFER_LEN];
334
    CK_BYTE OUTBUF[MAX_STACK_BUFFER_LEN];
335
    CK_BYTE_PTR inBufP;
336
    CK_BYTE_PTR outBufP = OUTBUF;
337
    CK_ULONG ckSignatureLength = 0;
338

339
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
340
    if (ckpFunctions == NULL) { return 0; }
341

342
    ckSessionHandle = jLongToCKULong(jSessionHandle);
343

344
    if (jInLen <= MAX_STACK_BUFFER_LEN) {
345
        inBufP = INBUF;
346
        ckSignatureLength = MAX_STACK_BUFFER_LEN;
347
    } else {
348
        inBufP = (CK_BYTE_PTR) malloc((size_t)jInLen);
349
        if (inBufP == NULL) {
350
            p11ThrowOutOfMemoryError(env, 0);
351
            return 0;
352
        }
353
        ckSignatureLength = jInLen;
354
    }
355

356
    (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
357
    if ((*env)->ExceptionCheck(env)) {
358
        goto cleanup;
359
    }
360

361
    rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength);
362
    /* re-alloc larger buffer if it fits into our Java buffer */
363
    if ((rv == CKR_BUFFER_TOO_SMALL) && (ckSignatureLength <= jIntToCKULong(jOutLen))) {
364
        outBufP = (CK_BYTE_PTR) malloc(ckSignatureLength);
365
        if (outBufP == NULL) {
366
            p11ThrowOutOfMemoryError(env, 0);
367
            goto cleanup;
368
        }
369
        rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength);
370
    }
371
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
372
        (*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckSignatureLength, (jbyte *)outBufP);
373
    }
374
cleanup:
375
    if (inBufP != INBUF) { free(inBufP); }
376
    if (outBufP != OUTBUF) { free(outBufP); }
377

378
    return ckSignatureLength;
379
}
380
#endif
381

382
#ifdef P11_ENABLE_C_VERIFYINIT
383
/*
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
 */
392
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyInit
393
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)
394
{
395
    CK_SESSION_HANDLE ckSessionHandle;
396
    CK_MECHANISM_PTR ckpMechanism = NULL;
397
    CK_OBJECT_HANDLE ckKeyHandle;
398
    CK_RV rv;
399

400
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
401
    if (ckpFunctions == NULL) { return; }
402

403
    TRACE0("DEBUG: C_VerifyInit\n");
404

405
    ckSessionHandle = jLongToCKULong(jSessionHandle);
406
    ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);
407
    if ((*env)->ExceptionCheck(env)) {
408
        return;
409
    }
410

411
    ckKeyHandle = jLongToCKULong(jKeyHandle);
412

413
    rv = (*ckpFunctions->C_VerifyInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);
414

415
    TRACE1("DEBUG C_VerifyInit: freed pMech = %p\n", ckpMechanism);
416
    freeCKMechanismPtr(ckpMechanism);
417
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
418
    TRACE0("FINISHED\n");
419
}
420
#endif
421

422
#ifdef P11_ENABLE_C_VERIFY
423
/*
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
 */
434
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Verify
435
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jData, jbyteArray jSignature)
436
{
437
    CK_SESSION_HANDLE ckSessionHandle;
438
    CK_BYTE_PTR ckpData = NULL_PTR;
439
    CK_BYTE_PTR ckpSignature = NULL_PTR;
440
    CK_ULONG ckDataLength;
441
    CK_ULONG ckSignatureLength;
442
    CK_RV rv = 0;
443

444
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
445
    if (ckpFunctions == NULL) { return; }
446

447
    ckSessionHandle = jLongToCKULong(jSessionHandle);
448

449
    jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength);
450
    if ((*env)->ExceptionCheck(env)) {
451
        return;
452
    }
453

454
    jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength);
455
    if ((*env)->ExceptionCheck(env)) {
456
        goto cleanup;
457
    }
458

459
    /* verify the signature */
460
    rv = (*ckpFunctions->C_Verify)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, ckSignatureLength);
461

462
cleanup:
463
    free(ckpData);
464
    free(ckpSignature);
465

466
    ckAssertReturnValueOK(env, rv);
467
}
468
#endif
469

470
#ifdef P11_ENABLE_C_VERIFYUPDATE
471
/*
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
 */
480
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyUpdate
481
  (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen)
482
{
483
    CK_SESSION_HANDLE ckSessionHandle;
484
    CK_RV rv;
485
    CK_BYTE_PTR bufP;
486
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
487
    jsize bufLen;
488

489
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
490
    if (ckpFunctions == NULL) { return; }
491

492
    ckSessionHandle = jLongToCKULong(jSessionHandle);
493

494
    if (directIn != 0) {
495
        rv = (*ckpFunctions->C_VerifyUpdate)(ckSessionHandle, (CK_BYTE_PTR)jlong_to_ptr(directIn), jInLen);
496
        ckAssertReturnValueOK(env, rv);
497
        return;
498
    }
499

500
    if (jInLen <= MAX_STACK_BUFFER_LEN) {
501
        bufLen = MAX_STACK_BUFFER_LEN;
502
        bufP = BUF;
503
    } else {
504
        bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);
505
        bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);
506
        if (bufP == NULL) {
507
            p11ThrowOutOfMemoryError(env, 0);
508
            goto cleanup;
509
        }
510
    }
511

512
    while (jInLen > 0) {
513
        jsize chunkLen = min(bufLen, jInLen);
514
        (*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);
515
        if ((*env)->ExceptionCheck(env)) {
516
            goto cleanup;
517
        }
518

519
        rv = (*ckpFunctions->C_VerifyUpdate)(ckSessionHandle, bufP, chunkLen);
520
        if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
521
            goto cleanup;
522
        }
523
        jInOfs += chunkLen;
524
        jInLen -= chunkLen;
525
    }
526

527
cleanup:
528
    if (bufP != BUF) { free(bufP); }
529
}
530
#endif
531

532
#ifdef P11_ENABLE_C_VERIFYFINAL
533
/*
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
 */
542
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyFinal
543
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jSignature)
544
{
545
    CK_SESSION_HANDLE ckSessionHandle;
546
    CK_BYTE_PTR ckpSignature = NULL_PTR;
547
    CK_ULONG ckSignatureLength;
548
    CK_RV rv;
549

550
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
551
    if (ckpFunctions == NULL) { return; }
552

553
    ckSessionHandle = jLongToCKULong(jSessionHandle);
554
    jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength);
555
    if ((*env)->ExceptionCheck(env)) {
556
        return;
557
    }
558

559
    /* verify the signature */
560
    rv = (*ckpFunctions->C_VerifyFinal)(ckSessionHandle, ckpSignature, ckSignatureLength);
561

562
    free(ckpSignature);
563

564
    ckAssertReturnValueOK(env, rv);
565
}
566
#endif
567

568
#ifdef P11_ENABLE_C_VERIFYRECOVERINIT
569
/*
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
 */
578
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecoverInit
579
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jKeyHandle)
580
{
581
    CK_SESSION_HANDLE ckSessionHandle;
582
    CK_MECHANISM_PTR ckpMechanism = NULL;
583
    CK_OBJECT_HANDLE ckKeyHandle;
584
    CK_RV rv;
585

586
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
587
    if (ckpFunctions == NULL) { return; }
588

589
    TRACE0("DEBUG: C_VerifyRecoverInit\n");
590

591
    ckSessionHandle = jLongToCKULong(jSessionHandle);
592
    ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);
593
    if ((*env)->ExceptionCheck(env)) { return; }
594

595
    ckKeyHandle = jLongToCKULong(jKeyHandle);
596

597
    rv = (*ckpFunctions->C_VerifyRecoverInit)(ckSessionHandle, ckpMechanism, ckKeyHandle);
598

599
    TRACE1("DEBUG C_VerifyRecoverInit: freed pMech = %p\n", ckpMechanism);
600
    freeCKMechanismPtr(ckpMechanism);
601
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
602
    TRACE0("FINISHED\n");
603
}
604
#endif
605

606
#ifdef P11_ENABLE_C_VERIFYRECOVER
607
/*
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
 */
618
JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecover
619
  (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jIn, jint jInOfs, jint jInLen, jbyteArray jOut, jint jOutOfs, jint jOutLen)
620
{
621
    CK_SESSION_HANDLE ckSessionHandle;
622
    CK_RV rv;
623
    CK_BYTE INBUF[MAX_STACK_BUFFER_LEN];
624
    CK_BYTE OUTBUF[MAX_STACK_BUFFER_LEN];
625
    CK_BYTE_PTR inBufP;
626
    CK_BYTE_PTR outBufP = OUTBUF;
627
    CK_ULONG ckDataLength = 0;
628

629
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
630
    if (ckpFunctions == NULL) { return 0; }
631

632
    ckSessionHandle = jLongToCKULong(jSessionHandle);
633

634
    if (jInLen <= MAX_STACK_BUFFER_LEN) {
635
        inBufP = INBUF;
636
        ckDataLength = MAX_STACK_BUFFER_LEN;
637
    } else {
638
        inBufP = (CK_BYTE_PTR) malloc((size_t)jInLen);
639
        if (inBufP == NULL) {
640
            p11ThrowOutOfMemoryError(env, 0);
641
            return 0;
642
        }
643
        ckDataLength = jInLen;
644
    }
645

646
    (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
647
    if ((*env)->ExceptionCheck(env)) {
648
        goto cleanup;
649
    }
650

651
    rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength);
652

653
    /* re-alloc larger buffer if it fits into our Java buffer */
654
    if ((rv == CKR_BUFFER_TOO_SMALL) && (ckDataLength <= jIntToCKULong(jOutLen))) {
655
        outBufP = (CK_BYTE_PTR) malloc(ckDataLength);
656
        if (outBufP == NULL) {
657
            p11ThrowOutOfMemoryError(env, 0);
658
            goto cleanup;
659
        }
660
        rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength);
661
    }
662
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
663
        (*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckDataLength, (jbyte *)outBufP);
664
    }
665

666
cleanup:
667
    if (inBufP != INBUF) { free(inBufP); }
668
    if (outBufP != OUTBUF) { free(outBufP); }
669

670
    return ckDataLength;
671
}
672
#endif
673

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.