Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_objmgmt.c
41149 views
1
/*
2
* Copyright (c) 2003, 2021, 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
55
#include "sun_security_pkcs11_wrapper_PKCS11.h"
56
57
#ifdef P11_ENABLE_C_CREATEOBJECT
58
/*
59
* Class: sun_security_pkcs11_wrapper_PKCS11
60
* Method: C_CreateObject
61
* Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
62
* Parametermapping: *PKCS11*
63
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
64
* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
65
* CK_ULONG ulCount
66
* @return jlong jObjectHandle CK_OBJECT_HANDLE_PTR phObject
67
*/
68
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CreateObject
69
(JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)
70
{
71
CK_SESSION_HANDLE ckSessionHandle;
72
CK_OBJECT_HANDLE ckObjectHandle;
73
CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
74
CK_ULONG ckAttributesLength;
75
jlong jObjectHandle = 0L;
76
CK_RV rv;
77
78
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
79
if (ckpFunctions == NULL) { return 0L; }
80
81
ckSessionHandle = jLongToCKULong(jSessionHandle);
82
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
83
if ((*env)->ExceptionCheck(env)) { return 0L; }
84
85
rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle, ckpAttributes, ckAttributesLength, &ckObjectHandle);
86
87
jObjectHandle = ckULongToJLong(ckObjectHandle);
88
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
89
90
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
91
92
return jObjectHandle ;
93
}
94
#endif
95
96
#ifdef P11_ENABLE_C_COPYOBJECT
97
/*
98
* Class: sun_security_pkcs11_wrapper_PKCS11
99
* Method: C_CopyObject
100
* Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
101
* Parametermapping: *PKCS11*
102
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
103
* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
104
* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
105
* CK_ULONG ulCount
106
* @return jlong jNewObjectHandle CK_OBJECT_HANDLE_PTR phNewObject
107
*/
108
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CopyObject
109
(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
110
{
111
CK_SESSION_HANDLE ckSessionHandle;
112
CK_OBJECT_HANDLE ckObjectHandle;
113
CK_OBJECT_HANDLE ckNewObjectHandle;
114
CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
115
CK_ULONG ckAttributesLength;
116
jlong jNewObjectHandle = 0L;
117
CK_RV rv;
118
119
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
120
if (ckpFunctions == NULL) { return 0L; }
121
122
ckSessionHandle = jLongToCKULong(jSessionHandle);
123
ckObjectHandle = jLongToCKULong(jObjectHandle);
124
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
125
if ((*env)->ExceptionCheck(env)) { return 0L; }
126
127
rv = (*ckpFunctions->C_CopyObject)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength, &ckNewObjectHandle);
128
129
jNewObjectHandle = ckULongToJLong(ckNewObjectHandle);
130
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
131
132
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
133
134
return jNewObjectHandle ;
135
}
136
#endif
137
138
#ifdef P11_ENABLE_C_DESTROYOBJECT
139
/*
140
* Class: sun_security_pkcs11_wrapper_PKCS11
141
* Method: C_DestroyObject
142
* Signature: (JJ)V
143
* Parametermapping: *PKCS11*
144
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
145
* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
146
*/
147
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DestroyObject
148
(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)
149
{
150
CK_SESSION_HANDLE ckSessionHandle;
151
CK_OBJECT_HANDLE ckObjectHandle;
152
CK_RV rv;
153
154
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
155
if (ckpFunctions == NULL) { return; }
156
157
ckSessionHandle = jLongToCKULong(jSessionHandle);
158
ckObjectHandle = jLongToCKULong(jObjectHandle);
159
160
rv = (*ckpFunctions->C_DestroyObject)(ckSessionHandle, ckObjectHandle);
161
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
162
}
163
#endif
164
165
#ifdef P11_ENABLE_C_GETOBJECTSIZE
166
/*
167
* Class: sun_security_pkcs11_wrapper_PKCS11
168
* Method: C_GetObjectSize
169
* Signature: (JJ)J
170
* Parametermapping: *PKCS11*
171
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
172
* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
173
* @return jlong jObjectSize CK_ULONG_PTR pulSize
174
*/
175
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetObjectSize
176
(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)
177
{
178
CK_SESSION_HANDLE ckSessionHandle;
179
CK_OBJECT_HANDLE ckObjectHandle;
180
CK_ULONG ckObjectSize;
181
jlong jObjectSize = 0L;
182
CK_RV rv;
183
184
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
185
if (ckpFunctions == NULL) { return 0L; }
186
187
ckSessionHandle = jLongToCKULong(jSessionHandle);
188
ckObjectHandle = jLongToCKULong(jObjectHandle);
189
190
rv = (*ckpFunctions->C_GetObjectSize)(ckSessionHandle, ckObjectHandle, &ckObjectSize);
191
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
192
193
jObjectSize = ckULongToJLong(ckObjectSize);
194
195
return jObjectSize ;
196
}
197
#endif
198
199
#ifdef P11_ENABLE_C_GETATTRIBUTEVALUE
200
/*
201
* Class: sun_security_pkcs11_wrapper_PKCS11
202
* Method: C_GetAttributeValue
203
* Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;
204
* Parametermapping: *PKCS11*
205
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
206
* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
207
* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
208
* CK_ULONG ulCount
209
*/
210
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeValue
211
(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
212
{
213
CK_SESSION_HANDLE ckSessionHandle;
214
CK_OBJECT_HANDLE ckObjectHandle;
215
CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
216
CK_ULONG ckAttributesLength;
217
CK_ULONG ckBufferLength;
218
CK_ULONG i;
219
jobject jAttribute;
220
CK_RV rv;
221
char* msg = NULL;
222
char* temp1, *temp2;
223
224
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
225
if (ckpFunctions == NULL) { return; }
226
227
TRACE0("DEBUG: C_GetAttributeValue");
228
TRACE1(", hSession=%lld", (long long) jSessionHandle);
229
TRACE1(", hObject=%lld", (long long) jObjectHandle);
230
TRACE1(", pTemplate=%p", jTemplate);
231
TRACE0(" ... ");
232
233
ckSessionHandle = jLongToCKULong(jSessionHandle);
234
ckObjectHandle = jLongToCKULong(jObjectHandle);
235
TRACE1("jAttributeArrayToCKAttributeArray now with jTemplate = %p", jTemplate);
236
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
237
if ((*env)->ExceptionCheck(env)) { return; }
238
239
TRACE2("DEBUG: jAttributeArrayToCKAttributeArray finished with ckpAttribute = %p, Length = %lu\n", ckpAttributes, (unsigned long) ckAttributesLength);
240
241
/* first set all pValue to NULL, to get the needed buffer length */
242
for(i = 0; i < ckAttributesLength; i++) {
243
if (ckpAttributes[i].pValue != NULL_PTR) {
244
free(ckpAttributes[i].pValue);
245
ckpAttributes[i].pValue = NULL_PTR;
246
}
247
}
248
249
rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
250
251
if (rv != CKR_OK) {
252
if (rv == CKR_ATTRIBUTE_SENSITIVE || rv == CKR_ATTRIBUTE_TYPE_INVALID) {
253
msg = malloc(80); // should be more than sufficient
254
if (msg == NULL) {
255
throwOutOfMemoryError(env, 0);
256
free(ckpAttributes);
257
return;
258
}
259
// format msg w/ attribute(s) whose value is unavailable
260
temp1 = msg;
261
temp2 = msg + 80;
262
for (i = 0; i < ckAttributesLength && temp1 < temp2; i++) {
263
if (ckpAttributes[i].ulValueLen == CK_UNAVAILABLE_INFORMATION) {
264
temp1 += snprintf(temp1, (temp2-temp1), " 0x%lX",
265
ckpAttributes[i].type);
266
}
267
}
268
ckAssertReturnValueOK2(env, rv, msg);
269
free(msg);
270
} else {
271
ckAssertReturnValueOK(env, rv);
272
}
273
free(ckpAttributes);
274
return;
275
}
276
277
/* now, the ulValueLength field of each attribute should hold the exact
278
* buffer length needed.
279
*/
280
for (i = 0; i < ckAttributesLength; i++) {
281
ckBufferLength = sizeof(CK_BYTE) * ckpAttributes[i].ulValueLen;
282
ckpAttributes[i].pValue = (void *) malloc(ckBufferLength);
283
if (ckpAttributes[i].pValue == NULL) {
284
freeCKAttributeArray(ckpAttributes, i);
285
throwOutOfMemoryError(env, 0);
286
return;
287
}
288
ckpAttributes[i].ulValueLen = ckBufferLength;
289
}
290
291
/* now get all attribute values */
292
rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle,
293
ckObjectHandle, ckpAttributes, ckAttributesLength);
294
295
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
296
/* copy back the values to the Java attributes */
297
for (i = 0; i < ckAttributesLength; i++) {
298
jAttribute = ckAttributePtrToJAttribute(env, &(ckpAttributes[i]));
299
if (jAttribute == NULL) {
300
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
301
return;
302
}
303
(*env)->SetObjectArrayElement(env, jTemplate, i, jAttribute);
304
if ((*env)->ExceptionCheck(env)) {
305
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
306
return;
307
}
308
}
309
}
310
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
311
TRACE0("FINISHED\n");
312
}
313
#endif
314
315
#ifdef P11_ENABLE_C_SETATTRIBUTEVALUE
316
/*
317
* Class: sun_security_pkcs11_wrapper_PKCS11
318
* Method: C_SetAttributeValue
319
* Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V
320
* Parametermapping: *PKCS11*
321
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
322
* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
323
* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
324
* CK_ULONG ulCount
325
*/
326
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetAttributeValue
327
(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
328
{
329
CK_SESSION_HANDLE ckSessionHandle;
330
CK_OBJECT_HANDLE ckObjectHandle;
331
CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
332
CK_ULONG ckAttributesLength;
333
CK_RV rv;
334
335
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
336
if (ckpFunctions == NULL) { return; }
337
338
ckSessionHandle = jLongToCKULong(jSessionHandle);
339
ckObjectHandle = jLongToCKULong(jObjectHandle);
340
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
341
if ((*env)->ExceptionCheck(env)) { return; }
342
343
rv = (*ckpFunctions->C_SetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
344
345
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
346
347
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
348
}
349
#endif
350
351
#ifdef P11_ENABLE_C_FINDOBJECTSINIT
352
/*
353
* Class: sun_security_pkcs11_wrapper_PKCS11
354
* Method: C_FindObjectsInit
355
* Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V
356
* Parametermapping: *PKCS11*
357
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
358
* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
359
* CK_ULONG ulCount
360
*/
361
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsInit
362
(JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)
363
{
364
CK_SESSION_HANDLE ckSessionHandle;
365
CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
366
CK_ULONG ckAttributesLength;
367
CK_RV rv;
368
369
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
370
if (ckpFunctions == NULL) { return; }
371
372
TRACE0("DEBUG: C_FindObjectsInit");
373
TRACE1(", hSession=%lld", (long long int) jSessionHandle);
374
TRACE1(", pTemplate=%p", jTemplate);
375
TRACE0(" ... ");
376
377
ckSessionHandle = jLongToCKULong(jSessionHandle);
378
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
379
if ((*env)->ExceptionCheck(env)) { return; }
380
381
rv = (*ckpFunctions->C_FindObjectsInit)(ckSessionHandle, ckpAttributes, ckAttributesLength);
382
383
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
384
TRACE0("FINISHED\n");
385
386
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
387
}
388
#endif
389
390
#ifdef P11_ENABLE_C_FINDOBJECTS
391
/*
392
* Class: sun_security_pkcs11_wrapper_PKCS11
393
* Method: C_FindObjects
394
* Signature: (JJ)[J
395
* Parametermapping: *PKCS11*
396
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
397
* @param jlong jMaxObjectCount CK_ULONG ulMaxObjectCount
398
* @return jlongArray jObjectHandleArray CK_OBJECT_HANDLE_PTR phObject
399
* CK_ULONG_PTR pulObjectCount
400
*/
401
JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjects
402
(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jMaxObjectCount)
403
{
404
CK_RV rv;
405
CK_SESSION_HANDLE ckSessionHandle;
406
CK_ULONG ckMaxObjectLength;
407
CK_OBJECT_HANDLE_PTR ckpObjectHandleArray;
408
CK_ULONG ckActualObjectCount;
409
jlongArray jObjectHandleArray = NULL;
410
411
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
412
if (ckpFunctions == NULL) { return NULL; }
413
414
ckSessionHandle = jLongToCKULong(jSessionHandle);
415
ckMaxObjectLength = jLongToCKULong(jMaxObjectCount);
416
ckpObjectHandleArray = (CK_OBJECT_HANDLE_PTR) malloc(sizeof(CK_OBJECT_HANDLE) * ckMaxObjectLength);
417
if (ckpObjectHandleArray == NULL) {
418
throwOutOfMemoryError(env, 0);
419
return NULL;
420
}
421
422
rv = (*ckpFunctions->C_FindObjects)(ckSessionHandle, ckpObjectHandleArray, ckMaxObjectLength, &ckActualObjectCount);
423
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
424
jObjectHandleArray = ckULongArrayToJLongArray(env, ckpObjectHandleArray, ckActualObjectCount);
425
}
426
427
free(ckpObjectHandleArray);
428
429
return jObjectHandleArray ;
430
}
431
#endif
432
433
#ifdef P11_ENABLE_C_FINDOBJECTSFINAL
434
/*
435
* Class: sun_security_pkcs11_wrapper_PKCS11
436
* Method: C_FindObjectsFinal
437
* Signature: (J)V
438
* Parametermapping: *PKCS11*
439
* @param jlong jSessionHandle CK_SESSION_HANDLE hSession
440
*/
441
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsFinal
442
(JNIEnv *env, jobject obj, jlong jSessionHandle)
443
{
444
CK_SESSION_HANDLE ckSessionHandle;
445
CK_RV rv;
446
447
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
448
if (ckpFunctions == NULL) { return; }
449
450
ckSessionHandle = jLongToCKULong(jSessionHandle);
451
rv = (*ckpFunctions->C_FindObjectsFinal)(ckSessionHandle);
452
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
453
}
454
#endif
455
456