comparison src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c @ 55037:15ae25f7eefd

8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047 Summary: changed variable declaration type to byte* from long* to fix SIGBUS error Reviewed-by: ascarpino
author valeriep
date Wed, 20 Feb 2019 19:16:45 +0000
parents 5170dc2bcf64
children 895a6a380484
comparison
equal deleted inserted replaced
2:ef57d3c90dad 3:f557a9f85e91
1 /* 1 /*
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 */ 3 */
4 4
5 /* Copyright (c) 2002 Graz University of Technology. All rights reserved. 5 /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
149 jbyte* nativeKeyInfoWrappedKeyArrayRaw = NULL; 149 jbyte* nativeKeyInfoWrappedKeyArrayRaw = NULL;
150 unsigned int sensitiveAttributePosition = (unsigned int)-1; 150 unsigned int sensitiveAttributePosition = (unsigned int)-1;
151 unsigned int i = 0U; 151 unsigned int i = 0U;
152 unsigned long totalDataSize = 0UL, attributesCount = 0UL; 152 unsigned long totalDataSize = 0UL, attributesCount = 0UL;
153 unsigned long totalCkAttributesSize = 0UL, totalNativeKeyInfoArraySize = 0UL; 153 unsigned long totalCkAttributesSize = 0UL, totalNativeKeyInfoArraySize = 0UL;
154 unsigned long* wrappedKeySizePtr = NULL; 154 jbyte* wrappedKeySizePtr = NULL;
155 jbyte* nativeKeyInfoArrayRawCkAttributes = NULL; 155 jbyte* nativeKeyInfoArrayRawCkAttributes = NULL;
156 jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL; 156 jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL;
157 jbyte* nativeKeyInfoArrayRawDataPtr = NULL; 157 jbyte* nativeKeyInfoArrayRawDataPtr = NULL;
158 CK_MECHANISM ckMechanism; 158 CK_MECHANISM ckMechanism;
159 char iv[16] = {0x0}; 159 char iv[16] = {0x0};
160 CK_ULONG ckWrappedKeyLength = 0U; 160 CK_ULONG ckWrappedKeyLength = 0U;
161 unsigned long* wrappedKeySizeWrappedKeyArrayPtr = NULL; 161 jbyte* wrappedKeySizeWrappedKeyArrayPtr = NULL;
162 CK_BYTE_PTR wrappedKeyBufferPtr = NULL; 162 CK_BYTE_PTR wrappedKeyBufferPtr = NULL;
163 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); 163 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
164 CK_OBJECT_CLASS class; 164 CK_OBJECT_CLASS class;
165 CK_KEY_TYPE keyType; 165 CK_KEY_TYPE keyType;
166 CK_BBOOL sensitive; 166 CK_BBOOL sensitive;
232 // values-size, [ ... values ... ], wrapped-key-size, 232 // values-size, [ ... values ... ], wrapped-key-size,
233 // [ ... wrapped-key ... ] ] 233 // [ ... wrapped-key ... ] ]
234 // * sizes are expressed in bytes and data type is unsigned long 234 // * sizes are expressed in bytes and data type is unsigned long
235 totalCkAttributesSize = attributesCount * sizeof(CK_ATTRIBUTE); 235 totalCkAttributesSize = attributesCount * sizeof(CK_ATTRIBUTE);
236 TRACE1("DEBUG: GetNativeKeyInfo attributesCount = %lu\n", attributesCount); 236 TRACE1("DEBUG: GetNativeKeyInfo attributesCount = %lu\n", attributesCount);
237 TRACE1("DEBUG: GetNativeKeyInfo sizeof CK_ATTRIBUTE = %lu\n", sizeof(CK_ATTRIBUTE)); 237 TRACE1("DEBUG: GetNativeKeyInfo sizeof CK_ATTRIBUTE = %zu\n", sizeof(CK_ATTRIBUTE));
238 TRACE1("DEBUG: GetNativeKeyInfo totalCkAttributesSize = %lu\n", totalCkAttributesSize); 238 TRACE1("DEBUG: GetNativeKeyInfo totalCkAttributesSize = %lu\n", totalCkAttributesSize);
239 TRACE1("DEBUG: GetNativeKeyInfo totalDataSize = %lu\n", totalDataSize); 239 TRACE1("DEBUG: GetNativeKeyInfo totalDataSize = %lu\n", totalDataSize);
240 240
241 totalNativeKeyInfoArraySize = 241 totalNativeKeyInfoArraySize =
242 totalCkAttributesSize + sizeof(unsigned long) * 3 + totalDataSize; 242 totalCkAttributesSize + sizeof(unsigned long) * 3 + totalDataSize;
252 NULL); 252 NULL);
253 if (nativeKeyInfoArrayRaw == NULL) { 253 if (nativeKeyInfoArrayRaw == NULL) {
254 goto cleanup; 254 goto cleanup;
255 } 255 }
256 256
257 wrappedKeySizePtr = (unsigned long*)(nativeKeyInfoArrayRaw + 257 wrappedKeySizePtr = nativeKeyInfoArrayRaw +
258 sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize); 258 sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize;
259 memcpy(nativeKeyInfoArrayRaw, &totalCkAttributesSize, sizeof(unsigned long)); 259 memcpy(nativeKeyInfoArrayRaw, &totalCkAttributesSize, sizeof(unsigned long));
260 260
261 memcpy(nativeKeyInfoArrayRaw + sizeof(unsigned long) + totalCkAttributesSize, 261 memcpy(nativeKeyInfoArrayRaw + sizeof(unsigned long) + totalCkAttributesSize,
262 &totalDataSize, sizeof(unsigned long)); 262 &totalDataSize, sizeof(unsigned long));
263 263
328 goto cleanup; 328 goto cleanup;
329 } 329 }
330 memcpy(nativeKeyInfoWrappedKeyArrayRaw, nativeKeyInfoArrayRaw, 330 memcpy(nativeKeyInfoWrappedKeyArrayRaw, nativeKeyInfoArrayRaw,
331 totalNativeKeyInfoArraySize); 331 totalNativeKeyInfoArraySize);
332 wrappedKeySizeWrappedKeyArrayPtr = 332 wrappedKeySizeWrappedKeyArrayPtr =
333 (unsigned long*)(nativeKeyInfoWrappedKeyArrayRaw + 333 nativeKeyInfoWrappedKeyArrayRaw +
334 sizeof(unsigned long)*2 + totalCkAttributesSize + 334 sizeof(unsigned long)*2 + totalCkAttributesSize +
335 totalDataSize); 335 totalDataSize;
336 memcpy(wrappedKeySizeWrappedKeyArrayPtr, &ckWrappedKeyLength, sizeof(unsigned long)); 336 memcpy(wrappedKeySizeWrappedKeyArrayPtr, &ckWrappedKeyLength, sizeof(unsigned long));
337 TRACE1("DEBUG: GetNativeKeyInfo 1st C_WrapKey wrappedKeyLength = %lu\n", ckWrappedKeyLength); 337 TRACE1("DEBUG: GetNativeKeyInfo 1st C_WrapKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);
338 338
339 wrappedKeyBufferPtr = 339 wrappedKeyBufferPtr =
340 (unsigned char*)wrappedKeySizeWrappedKeyArrayPtr + 340 (CK_BYTE_PTR) (wrappedKeySizeWrappedKeyArrayPtr +
341 sizeof(unsigned long); 341 sizeof(unsigned long));
342 rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, 342 rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism,
343 jLongToCKULong(jWrappingKeyHandle),ckObjectHandle, 343 jLongToCKULong(jWrappingKeyHandle),ckObjectHandle,
344 wrappedKeyBufferPtr, &ckWrappedKeyLength); 344 wrappedKeyBufferPtr, &ckWrappedKeyLength);
345 if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { 345 if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
346 goto cleanup; 346 goto cleanup;
412 unsigned long nativeKeyInfoCkAttributesCount = 0UL; 412 unsigned long nativeKeyInfoCkAttributesCount = 0UL;
413 jbyte* nativeKeyInfoArrayRawCkAttributes = NULL; 413 jbyte* nativeKeyInfoArrayRawCkAttributes = NULL;
414 jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL; 414 jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL;
415 jbyte* nativeKeyInfoArrayRawDataPtr = NULL; 415 jbyte* nativeKeyInfoArrayRawDataPtr = NULL;
416 unsigned long totalDataSize = 0UL; 416 unsigned long totalDataSize = 0UL;
417 unsigned long* wrappedKeySizePtr = NULL; 417 jbyte* wrappedKeySizePtr = NULL;
418 unsigned int i = 0U; 418 unsigned int i = 0U;
419 CK_MECHANISM ckMechanism; 419 CK_MECHANISM ckMechanism;
420 char iv[16] = {0x0}; 420 char iv[16] = {0x0};
421 CK_ULONG ckWrappedKeyLength = 0UL; 421 CK_ULONG ckWrappedKeyLength = 0UL;
422 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); 422 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
441 totalCkAttributesSize + sizeof(unsigned long) * 2; 441 totalCkAttributesSize + sizeof(unsigned long) * 2;
442 memcpy(&totalDataSize, (nativeKeyInfoArrayRaw + totalCkAttributesSize + sizeof(unsigned long)), 442 memcpy(&totalDataSize, (nativeKeyInfoArrayRaw + totalCkAttributesSize + sizeof(unsigned long)),
443 sizeof(unsigned long)); 443 sizeof(unsigned long));
444 TRACE1("DEBUG: createNativeKey totalDataSize = %lu\n", totalDataSize); 444 TRACE1("DEBUG: createNativeKey totalDataSize = %lu\n", totalDataSize);
445 445
446 wrappedKeySizePtr = (unsigned long*)(nativeKeyInfoArrayRaw + 446 wrappedKeySizePtr = nativeKeyInfoArrayRaw +
447 sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize); 447 sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize;
448 448
449 memcpy(&ckWrappedKeyLength, wrappedKeySizePtr, sizeof(unsigned long)); 449 memcpy(&ckWrappedKeyLength, wrappedKeySizePtr, sizeof(unsigned long));
450 TRACE1("DEBUG: createNativeKey wrappedKeyLength = %lu\n", ckWrappedKeyLength); 450 TRACE1("DEBUG: createNativeKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);
451 451
452 for (i = 0; i < nativeKeyInfoCkAttributesCount; i++) { 452 for (i = 0; i < nativeKeyInfoCkAttributesCount; i++) {
468 } else { 468 } else {
469 // Wrapped key 469 // Wrapped key
470 jMechanismToCKMechanism(env, jWrappingMech, &ckMechanism); 470 jMechanismToCKMechanism(env, jWrappingMech, &ckMechanism);
471 rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, &ckMechanism, 471 rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, &ckMechanism,
472 jLongToCKULong(jWrappingKeyHandle), 472 jLongToCKULong(jWrappingKeyHandle),
473 (CK_BYTE_PTR)(wrappedKeySizePtr + 1), ckWrappedKeyLength, 473 (CK_BYTE_PTR)(wrappedKeySizePtr + sizeof(unsigned long)),
474 ckWrappedKeyLength,
474 (CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes, 475 (CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes,
475 jLongToCKULong(nativeKeyInfoCkAttributesCount), 476 jLongToCKULong(nativeKeyInfoCkAttributesCount),
476 &ckObjectHandle); 477 &ckObjectHandle);
477 } 478 }
478 if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { 479 if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {