Path: blob/master/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c
41152 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2*/34/* Copyright (c) 2002 Graz University of Technology. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* 1. Redistributions of source code must retain the above copyright notice,10* this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15*16* 3. The end-user documentation included with the redistribution, if any, must17* include the following acknowledgment:18*19* "This product includes software developed by IAIK of Graz University of20* Technology."21*22* Alternately, this acknowledgment may appear in the software itself, if23* and wherever such third-party acknowledgments normally appear.24*25* 4. The names "Graz University of Technology" and "IAIK of Graz University of26* Technology" must not be used to endorse or promote products derived from27* this software without prior written permission.28*29* 5. Products derived from this software may not be called30* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior31* written permission of Graz University of Technology.32*33* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED34* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED35* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR36* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE37* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,38* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,39* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,40* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON41* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,42* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY43* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE44* POSSIBILITY OF SUCH DAMAGE.45*/4647#include "pkcs11wrapper.h"4849#include <stdio.h>50#include <stdlib.h>51#include <string.h>52#include <assert.h>5354#include "sun_security_pkcs11_wrapper_PKCS11.h"5556#ifdef P11_ENABLE_GETNATIVEKEYINFO5758#define CK_ATTRIBUTES_TEMPLATE_LENGTH (CK_ULONG)61U5960static CK_ATTRIBUTE ckpAttributesTemplate[CK_ATTRIBUTES_TEMPLATE_LENGTH] = {61{CKA_CLASS, 0, 0},62{CKA_TOKEN, 0, 0},63{CKA_PRIVATE, 0, 0},64{CKA_LABEL, 0, 0},65{CKA_APPLICATION, 0, 0},66{CKA_VALUE, 0, 0},67{CKA_OBJECT_ID, 0, 0},68{CKA_CERTIFICATE_TYPE, 0, 0},69{CKA_ISSUER, 0, 0},70{CKA_SERIAL_NUMBER, 0, 0},71{CKA_AC_ISSUER, 0, 0},72{CKA_OWNER, 0, 0},73{CKA_ATTR_TYPES, 0, 0},74{CKA_TRUSTED, 0, 0},75{CKA_KEY_TYPE, 0, 0},76{CKA_SUBJECT, 0, 0},77{CKA_ID, 0, 0},78{CKA_SENSITIVE, 0, 0},79{CKA_ENCRYPT, 0, 0},80{CKA_DECRYPT, 0, 0},81{CKA_WRAP, 0, 0},82{CKA_UNWRAP, 0, 0},83{CKA_SIGN, 0, 0},84{CKA_SIGN_RECOVER, 0, 0},85{CKA_VERIFY, 0, 0},86{CKA_VERIFY_RECOVER, 0, 0},87{CKA_DERIVE, 0, 0},88{CKA_START_DATE, 0, 0},89{CKA_END_DATE, 0, 0},90{CKA_MODULUS, 0, 0},91{CKA_MODULUS_BITS, 0, 0},92{CKA_PUBLIC_EXPONENT, 0, 0},93{CKA_PRIVATE_EXPONENT, 0, 0},94{CKA_PRIME_1, 0, 0},95{CKA_PRIME_2, 0, 0},96{CKA_EXPONENT_1, 0, 0},97{CKA_EXPONENT_2, 0, 0},98{CKA_COEFFICIENT, 0, 0},99{CKA_PRIME, 0, 0},100{CKA_SUBPRIME, 0, 0},101{CKA_BASE, 0, 0},102{CKA_PRIME_BITS, 0, 0},103{CKA_SUB_PRIME_BITS, 0, 0},104{CKA_VALUE_BITS, 0, 0},105{CKA_VALUE_LEN, 0, 0},106{CKA_EXTRACTABLE, 0, 0},107{CKA_LOCAL, 0, 0},108{CKA_NEVER_EXTRACTABLE, 0, 0},109{CKA_ALWAYS_SENSITIVE, 0, 0},110{CKA_KEY_GEN_MECHANISM, 0, 0},111{CKA_MODIFIABLE, 0, 0},112{CKA_ECDSA_PARAMS, 0, 0},113{CKA_EC_PARAMS, 0, 0},114{CKA_EC_POINT, 0, 0},115{CKA_SECONDARY_AUTH, 0, 0},116{CKA_AUTH_PIN_FLAGS, 0, 0},117{CKA_HW_FEATURE_TYPE, 0, 0},118{CKA_RESET_ON_INIT, 0, 0},119{CKA_HAS_RESET, 0, 0},120{CKA_VENDOR_DEFINED, 0, 0},121{CKA_NETSCAPE_DB, 0, 0},122};123124/*125* Class: sun_security_pkcs11_wrapper_PKCS11126* Method: getNativeKeyInfo127* Signature: (JJJLsun/security/pkcs11/wrapper/CK_MECHANISM;)[B128* Parametermapping: *PKCS11*129* @param jlong jSessionHandle CK_SESSION_HANDLE hSession130* @param jlong jKeyHandle CK_OBJECT_HANDLE hObject131* @param jlong jWrappingKeyHandle CK_OBJECT_HANDLE hObject132* @param jobject jWrappingMech CK_MECHANISM_PTR pMechanism133* @return jbyteArray jNativeKeyInfo -134*/135JNIEXPORT jbyteArray JNICALL136Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo137(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jKeyHandle,138jlong jWrappingKeyHandle, jobject jWrappingMech)139{140jbyteArray returnValue = NULL;141CK_SESSION_HANDLE ckSessionHandle = jLongToCKULong(jSessionHandle);142CK_OBJECT_HANDLE ckObjectHandle = jLongToCKULong(jKeyHandle);143CK_ATTRIBUTE_PTR ckpAttributes = NULL;144CK_RV rv;145jbyteArray nativeKeyInfoArray = NULL;146jbyteArray nativeKeyInfoWrappedKeyArray = NULL;147jbyte* nativeKeyInfoArrayRaw = NULL;148jbyte* nativeKeyInfoWrappedKeyArrayRaw = NULL;149unsigned int sensitiveAttributePosition = (unsigned int)-1;150unsigned int i = 0U;151unsigned long totalDataSize = 0UL, attributesCount = 0UL;152unsigned long totalCkAttributesSize = 0UL, totalNativeKeyInfoArraySize = 0UL;153jbyte* wrappedKeySizePtr = NULL;154jbyte* nativeKeyInfoArrayRawCkAttributes = NULL;155jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL;156jbyte* nativeKeyInfoArrayRawDataPtr = NULL;157CK_MECHANISM_PTR ckpMechanism = NULL;158char iv[16] = {0x0};159CK_ULONG ckWrappedKeyLength = 0U;160jbyte* wrappedKeySizeWrappedKeyArrayPtr = NULL;161CK_BYTE_PTR wrappedKeyBufferPtr = NULL;162CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);163CK_OBJECT_CLASS class;164CK_KEY_TYPE keyType;165CK_BBOOL sensitive;166CK_BBOOL netscapeAttributeValueNeeded = CK_FALSE;167CK_ATTRIBUTE ckNetscapeAttributesTemplate[4];168ckNetscapeAttributesTemplate[0].type = CKA_CLASS;169ckNetscapeAttributesTemplate[1].type = CKA_KEY_TYPE;170ckNetscapeAttributesTemplate[2].type = CKA_SENSITIVE;171ckNetscapeAttributesTemplate[3].type = CKA_NETSCAPE_DB;172ckNetscapeAttributesTemplate[0].pValue = &class;173ckNetscapeAttributesTemplate[1].pValue = &keyType;174ckNetscapeAttributesTemplate[2].pValue = &sensitive;175ckNetscapeAttributesTemplate[3].pValue = 0;176ckNetscapeAttributesTemplate[0].ulValueLen = sizeof(class);177ckNetscapeAttributesTemplate[1].ulValueLen = sizeof(keyType);178ckNetscapeAttributesTemplate[2].ulValueLen = sizeof(sensitive);179ckNetscapeAttributesTemplate[3].ulValueLen = 0;180181if (ckpFunctions == NULL) { goto cleanup; }182183// If key is private and of DSA or EC type, NSS may require CKA_NETSCAPE_DB184// attribute to unwrap it.185rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle,186ckNetscapeAttributesTemplate,187sizeof(ckNetscapeAttributesTemplate)/sizeof(CK_ATTRIBUTE));188189if (rv == CKR_OK && class == CKO_PRIVATE_KEY &&190(keyType == CKK_EC || keyType == CKK_DSA) &&191sensitive == CK_TRUE &&192ckNetscapeAttributesTemplate[3].ulValueLen == CK_UNAVAILABLE_INFORMATION) {193// We cannot set the attribute through C_SetAttributeValue here194// because it might be read-only. However, we can add it to195// the extracted buffer.196netscapeAttributeValueNeeded = CK_TRUE;197TRACE0("DEBUG: override CKA_NETSCAPE_DB attr value to TRUE\n");198}199200ckpAttributes = (CK_ATTRIBUTE_PTR) calloc(201CK_ATTRIBUTES_TEMPLATE_LENGTH, sizeof(CK_ATTRIBUTE));202if (ckpAttributes == NULL) {203throwOutOfMemoryError(env, 0);204goto cleanup;205}206memcpy(ckpAttributes, ckpAttributesTemplate,207CK_ATTRIBUTES_TEMPLATE_LENGTH * sizeof(CK_ATTRIBUTE));208209// Get sizes for value buffers210// NOTE: may return an error code but length values are filled anyways211(*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle,212ckpAttributes, CK_ATTRIBUTES_TEMPLATE_LENGTH);213214for (i = 0; i < CK_ATTRIBUTES_TEMPLATE_LENGTH; i++) {215if ((ckpAttributes+i)->ulValueLen != CK_UNAVAILABLE_INFORMATION) {216totalDataSize += (ckpAttributes+i)->ulValueLen;217if ((ckpAttributes+i)->type == CKA_SENSITIVE) {218sensitiveAttributePosition = attributesCount;219TRACE0("DEBUG: GetNativeKeyInfo key is sensitive");220}221attributesCount++;222}223}224225if (netscapeAttributeValueNeeded) {226attributesCount++;227}228229// Allocate a single buffer to hold valid attributes and attribute's values230// Buffer structure: [ attributes-size, [ ... attributes ... ],231// values-size, [ ... values ... ], wrapped-key-size,232// [ ... wrapped-key ... ] ]233// * sizes are expressed in bytes and data type is unsigned long234totalCkAttributesSize = attributesCount * sizeof(CK_ATTRIBUTE);235TRACE1("DEBUG: GetNativeKeyInfo attributesCount = %lu\n", attributesCount);236TRACE1("DEBUG: GetNativeKeyInfo sizeof CK_ATTRIBUTE = %zu\n", sizeof(CK_ATTRIBUTE));237TRACE1("DEBUG: GetNativeKeyInfo totalCkAttributesSize = %lu\n", totalCkAttributesSize);238TRACE1("DEBUG: GetNativeKeyInfo totalDataSize = %lu\n", totalDataSize);239240totalNativeKeyInfoArraySize =241totalCkAttributesSize + sizeof(unsigned long) * 3 + totalDataSize;242243TRACE1("DEBUG: GetNativeKeyInfo totalNativeKeyInfoArraySize = %lu\n", totalNativeKeyInfoArraySize);244245nativeKeyInfoArray = (*env)->NewByteArray(env, totalNativeKeyInfoArraySize);246if (nativeKeyInfoArray == NULL) {247goto cleanup;248}249250nativeKeyInfoArrayRaw = (*env)->GetByteArrayElements(env, nativeKeyInfoArray,251NULL);252if (nativeKeyInfoArrayRaw == NULL) {253goto cleanup;254}255256wrappedKeySizePtr = nativeKeyInfoArrayRaw +257sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize;258memcpy(nativeKeyInfoArrayRaw, &totalCkAttributesSize, sizeof(unsigned long));259260memcpy(nativeKeyInfoArrayRaw + sizeof(unsigned long) + totalCkAttributesSize,261&totalDataSize, sizeof(unsigned long));262263memset(wrappedKeySizePtr, 0, sizeof(unsigned long));264265nativeKeyInfoArrayRawCkAttributes = nativeKeyInfoArrayRaw +266sizeof(unsigned long);267nativeKeyInfoArrayRawCkAttributesPtr = nativeKeyInfoArrayRawCkAttributes;268nativeKeyInfoArrayRawDataPtr = nativeKeyInfoArrayRaw +269totalCkAttributesSize + sizeof(unsigned long) * 2;270271for (i = 0; i < CK_ATTRIBUTES_TEMPLATE_LENGTH; i++) {272if ((ckpAttributes+i)->ulValueLen != CK_UNAVAILABLE_INFORMATION) {273(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).type =274(ckpAttributes+i)->type;275(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen =276(ckpAttributes+i)->ulValueLen;277if ((ckpAttributes+i)->ulValueLen != 0) {278(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).pValue =279nativeKeyInfoArrayRawDataPtr;280} else {281(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).pValue = 0;282}283nativeKeyInfoArrayRawDataPtr +=284(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen;285nativeKeyInfoArrayRawCkAttributesPtr += sizeof(CK_ATTRIBUTE);286}287}288289TRACE0("DEBUG: GetNativeKeyInfo finished prepping nativeKeyInfoArray\n");290291// Get attribute's values292rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle,293(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes,294attributesCount);295if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {296goto cleanup;297}298299TRACE0("DEBUG: GetNativeKeyInfo 1st C_GetAttributeValue call passed\n");300301if (netscapeAttributeValueNeeded) {302(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).type = CKA_NETSCAPE_DB;303// Value is not needed, public key is not used304}305306if ((sensitiveAttributePosition != (unsigned int)-1) &&307*(CK_BBOOL*)(((CK_ATTRIBUTE_PTR)(((CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes)308+sensitiveAttributePosition))->pValue) == CK_TRUE) {309// Key is sensitive. Need to extract it wrapped.310if (jWrappingKeyHandle != 0) {311312ckpMechanism = jMechanismToCKMechanismPtr(env, jWrappingMech);313rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, ckpMechanism,314jLongToCKULong(jWrappingKeyHandle), ckObjectHandle,315NULL_PTR, &ckWrappedKeyLength);316if (ckWrappedKeyLength != 0) {317// Allocate space for getting the wrapped key318nativeKeyInfoWrappedKeyArray = (*env)->NewByteArray(env,319totalNativeKeyInfoArraySize + ckWrappedKeyLength);320if (nativeKeyInfoWrappedKeyArray == NULL) {321goto cleanup;322}323nativeKeyInfoWrappedKeyArrayRaw =324(*env)->GetByteArrayElements(env,325nativeKeyInfoWrappedKeyArray, NULL);326if (nativeKeyInfoWrappedKeyArrayRaw == NULL) {327goto cleanup;328}329memcpy(nativeKeyInfoWrappedKeyArrayRaw, nativeKeyInfoArrayRaw,330totalNativeKeyInfoArraySize);331wrappedKeySizeWrappedKeyArrayPtr =332nativeKeyInfoWrappedKeyArrayRaw +333sizeof(unsigned long)*2 + totalCkAttributesSize +334totalDataSize;335memcpy(wrappedKeySizeWrappedKeyArrayPtr, &ckWrappedKeyLength, sizeof(unsigned long));336TRACE1("DEBUG: GetNativeKeyInfo 1st C_WrapKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);337338wrappedKeyBufferPtr =339(CK_BYTE_PTR) (wrappedKeySizeWrappedKeyArrayPtr +340sizeof(unsigned long));341rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, ckpMechanism,342jLongToCKULong(jWrappingKeyHandle),ckObjectHandle,343wrappedKeyBufferPtr, &ckWrappedKeyLength);344if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {345goto cleanup;346}347memcpy(wrappedKeySizeWrappedKeyArrayPtr, &ckWrappedKeyLength, sizeof(unsigned long));348TRACE1("DEBUG: GetNativeKeyInfo 2nd C_WrapKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);349} else {350goto cleanup;351}352} else {353ckAssertReturnValueOK(env, CKR_KEY_HANDLE_INVALID);354goto cleanup;355}356returnValue = nativeKeyInfoWrappedKeyArray;357} else {358returnValue = nativeKeyInfoArray;359}360361cleanup:362if (ckpAttributes != NULL) {363free(ckpAttributes);364}365366if (nativeKeyInfoArrayRaw != NULL) {367(*env)->ReleaseByteArrayElements(env, nativeKeyInfoArray,368nativeKeyInfoArrayRaw, 0);369}370371if (nativeKeyInfoWrappedKeyArrayRaw != NULL) {372(*env)->ReleaseByteArrayElements(env, nativeKeyInfoWrappedKeyArray,373nativeKeyInfoWrappedKeyArrayRaw, 0);374}375376if (nativeKeyInfoArray != NULL && returnValue != nativeKeyInfoArray) {377(*env)->DeleteLocalRef(env, nativeKeyInfoArray);378}379380if (nativeKeyInfoWrappedKeyArray != NULL381&& returnValue != nativeKeyInfoWrappedKeyArray) {382(*env)->DeleteLocalRef(env, nativeKeyInfoWrappedKeyArray);383}384freeCKMechanismPtr(ckpMechanism);385386return returnValue;387}388#endif389390#ifdef P11_ENABLE_CREATENATIVEKEY391/*392* Class: sun_security_pkcs11_wrapper_PKCS11393* Method: createNativeKey394* Signature: (J[BJLsun/security/pkcs11/wrapper/CK_MECHANISM;)J395* Parametermapping: *PKCS11*396* @param jlong jSessionHandle CK_SESSION_HANDLE hSession397* @param jbyteArray jNativeKeyInfo -398* @param jlong jWrappingKeyHandle CK_OBJECT_HANDLE hObject399* @param jobject jWrappingMech CK_MECHANISM_PTR pMechanism400* @return jlong jKeyHandle CK_OBJECT_HANDLE hObject401*/402JNIEXPORT jlong JNICALL403Java_sun_security_pkcs11_wrapper_PKCS11_createNativeKey404(JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jNativeKeyInfo,405jlong jWrappingKeyHandle, jobject jWrappingMech)406{407CK_OBJECT_HANDLE ckObjectHandle;408CK_RV rv;409CK_SESSION_HANDLE ckSessionHandle = jLongToCKULong(jSessionHandle);410jbyte* nativeKeyInfoArrayRaw = NULL;411jlong jObjectHandle = 0L;412unsigned long totalCkAttributesSize = 0UL;413unsigned long nativeKeyInfoCkAttributesCount = 0UL;414jbyte* nativeKeyInfoArrayRawCkAttributes = NULL;415jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL;416jbyte* nativeKeyInfoArrayRawDataPtr = NULL;417unsigned long totalDataSize = 0UL;418jbyte* wrappedKeySizePtr = NULL;419unsigned int i = 0U;420CK_MECHANISM_PTR ckpMechanism = NULL;421char iv[16] = {0x0};422CK_ULONG ckWrappedKeyLength = 0UL;423CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);424425if (ckpFunctions == NULL) { goto cleanup; }426427nativeKeyInfoArrayRaw =428(*env)->GetByteArrayElements(env, jNativeKeyInfo, NULL);429if (nativeKeyInfoArrayRaw == NULL) {430goto cleanup;431}432433memcpy(&totalCkAttributesSize, nativeKeyInfoArrayRaw, sizeof(unsigned long));434TRACE1("DEBUG: createNativeKey totalCkAttributesSize = %lu\n", totalCkAttributesSize);435nativeKeyInfoCkAttributesCount = totalCkAttributesSize/sizeof(CK_ATTRIBUTE);436TRACE1("DEBUG: createNativeKey nativeKeyInfoCkAttributesCount = %lu\n", nativeKeyInfoCkAttributesCount);437438nativeKeyInfoArrayRawCkAttributes = nativeKeyInfoArrayRaw +439sizeof(unsigned long);440nativeKeyInfoArrayRawCkAttributesPtr = nativeKeyInfoArrayRawCkAttributes;441nativeKeyInfoArrayRawDataPtr = nativeKeyInfoArrayRaw +442totalCkAttributesSize + sizeof(unsigned long) * 2;443memcpy(&totalDataSize, (nativeKeyInfoArrayRaw + totalCkAttributesSize + sizeof(unsigned long)),444sizeof(unsigned long));445TRACE1("DEBUG: createNativeKey totalDataSize = %lu\n", totalDataSize);446447wrappedKeySizePtr = nativeKeyInfoArrayRaw +448sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize;449450memcpy(&ckWrappedKeyLength, wrappedKeySizePtr, sizeof(unsigned long));451TRACE1("DEBUG: createNativeKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);452453for (i = 0; i < nativeKeyInfoCkAttributesCount; i++) {454if ((*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen455> 0) {456(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).pValue =457nativeKeyInfoArrayRawDataPtr;458}459nativeKeyInfoArrayRawDataPtr +=460(*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen;461nativeKeyInfoArrayRawCkAttributesPtr += sizeof(CK_ATTRIBUTE);462}463464if (ckWrappedKeyLength == 0) {465// Not a wrapped key466rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle,467(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes,468jLongToCKULong(nativeKeyInfoCkAttributesCount), &ckObjectHandle);469} else {470// Wrapped key471ckpMechanism = jMechanismToCKMechanismPtr(env, jWrappingMech);472rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, ckpMechanism,473jLongToCKULong(jWrappingKeyHandle),474(CK_BYTE_PTR)(wrappedKeySizePtr + sizeof(unsigned long)),475ckWrappedKeyLength,476(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes,477jLongToCKULong(nativeKeyInfoCkAttributesCount),478&ckObjectHandle);479}480if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {481goto cleanup;482}483484jObjectHandle = ckULongToJLong(ckObjectHandle);485486cleanup:487488if (nativeKeyInfoArrayRaw != NULL) {489(*env)->ReleaseByteArrayElements(env, jNativeKeyInfo,490nativeKeyInfoArrayRaw, JNI_ABORT);491}492493freeCKMechanismPtr(ckpMechanism);494return jObjectHandle;495}496#endif497498#ifdef P11_ENABLE_C_GENERATEKEY499/*500* Class: sun_security_pkcs11_wrapper_PKCS11501* Method: C_GenerateKey502* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J503* Parametermapping: *PKCS11*504* @param jlong jSessionHandle CK_SESSION_HANDLE hSession505* @param jobject jMechanism CK_MECHANISM_PTR pMechanism506* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate507* CK_ULONG ulCount508* @return jlong jKeyHandle CK_OBJECT_HANDLE_PTR phKey509*/510JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKey511(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jobjectArray jTemplate)512{513CK_SESSION_HANDLE ckSessionHandle;514CK_MECHANISM_PTR ckpMechanism = NULL;515CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;516CK_ULONG ckAttributesLength = 0;517CK_OBJECT_HANDLE ckKeyHandle = 0;518jlong jKeyHandle = 0L;519CK_RV rv;520521CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);522if (ckpFunctions == NULL) { return 0L; }523524ckSessionHandle = jLongToCKULong(jSessionHandle);525ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);526if ((*env)->ExceptionCheck(env)) { return 0L ; }527528jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);529if ((*env)->ExceptionCheck(env)) {530goto cleanup;531}532533rv = (*ckpFunctions->C_GenerateKey)(ckSessionHandle, ckpMechanism, ckpAttributes, ckAttributesLength, &ckKeyHandle);534535if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {536jKeyHandle = ckULongToJLong(ckKeyHandle);537538/* cheack, if we must give a initialization vector back to Java */539switch (ckpMechanism->mechanism) {540case CKM_PBE_MD2_DES_CBC:541case CKM_PBE_MD5_DES_CBC:542case CKM_PBE_MD5_CAST_CBC:543case CKM_PBE_MD5_CAST3_CBC:544case CKM_PBE_MD5_CAST128_CBC:545/* case CKM_PBE_MD5_CAST5_CBC: the same as CKM_PBE_MD5_CAST128_CBC */546case CKM_PBE_SHA1_CAST128_CBC:547/* case CKM_PBE_SHA1_CAST5_CBC: the same as CKM_PBE_SHA1_CAST128_CBC */548/* we must copy back the initialization vector to the jMechanism object */549copyBackPBEInitializationVector(env, ckpMechanism, jMechanism);550break;551}552}553cleanup:554freeCKMechanismPtr(ckpMechanism);555freeCKAttributeArray(ckpAttributes, ckAttributesLength);556557return jKeyHandle ;558}559#endif560561#ifdef P11_ENABLE_C_GENERATEKEYPAIR562/*563* Class: sun_security_pkcs11_wrapper_PKCS11564* Method: C_GenerateKeyPair565* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)[J566* Parametermapping: *PKCS11*567* @param jlong jSessionHandle CK_SESSION_HANDLE hSession568* @param jobject jMechanism CK_MECHANISM_PTR pMechanism569* @param jobjectArray jPublicKeyTemplate CK_ATTRIBUTE_PTR pPublicKeyTemplate570* CK_ULONG ulPublicKeyAttributeCount571* @param jobjectArray jPrivateKeyTemplate CK_ATTRIBUTE_PTR pPrivateKeyTemplate572* CK_ULONG ulPrivateKeyAttributeCount573* @return jlongArray jKeyHandles CK_OBJECT_HANDLE_PTR phPublicKey574* CK_OBJECT_HANDLE_PTR phPublicKey575*/576JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKeyPair577(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism,578jobjectArray jPublicKeyTemplate, jobjectArray jPrivateKeyTemplate)579{580CK_SESSION_HANDLE ckSessionHandle;581CK_MECHANISM_PTR ckpMechanism = NULL;582CK_ATTRIBUTE_PTR ckpPublicKeyAttributes = NULL_PTR;583CK_ATTRIBUTE_PTR ckpPrivateKeyAttributes = NULL_PTR;584CK_ULONG ckPublicKeyAttributesLength = 0;585CK_ULONG ckPrivateKeyAttributesLength = 0;586CK_OBJECT_HANDLE_PTR ckpPublicKeyHandle; /* pointer to Public Key */587CK_OBJECT_HANDLE_PTR ckpPrivateKeyHandle; /* pointer to Private Key */588CK_OBJECT_HANDLE_PTR ckpKeyHandles = NULL; /* pointer to array with Public and Private Key */589jlongArray jKeyHandles = NULL;590CK_RV rv;591int attempts;592const int MAX_ATTEMPTS = 3;593594CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);595if (ckpFunctions == NULL) { return NULL; }596597ckSessionHandle = jLongToCKULong(jSessionHandle);598ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);599if ((*env)->ExceptionCheck(env)) { return NULL; }600601ckpKeyHandles = (CK_OBJECT_HANDLE_PTR) calloc(2, sizeof(CK_OBJECT_HANDLE));602if (ckpKeyHandles == NULL) {603throwOutOfMemoryError(env, 0);604goto cleanup;605}606ckpPublicKeyHandle = ckpKeyHandles; /* first element of array is Public Key */607ckpPrivateKeyHandle = (ckpKeyHandles + 1); /* second element of array is Private Key */608609jAttributeArrayToCKAttributeArray(env, jPublicKeyTemplate, &ckpPublicKeyAttributes, &ckPublicKeyAttributesLength);610if ((*env)->ExceptionCheck(env)) {611goto cleanup;612}613614jAttributeArrayToCKAttributeArray(env, jPrivateKeyTemplate, &ckpPrivateKeyAttributes, &ckPrivateKeyAttributesLength);615if ((*env)->ExceptionCheck(env)) {616goto cleanup;617}618619/*620* Workaround for NSS bug 1012786:621*622* Key generation may fail with CKR_FUNCTION_FAILED error623* if there is insufficient entropy to generate a random key.624*625* PKCS11 spec says the following about CKR_FUNCTION_FAILED error626* (see section 11.1.1):627*628* ... In any event, although the function call failed, the situation629* is not necessarily totally hopeless, as it is likely to be630* when CKR_GENERAL_ERROR is returned. Depending on what the root cause of631* the error actually was, it is possible that an attempt632* to make the exact same function call again would succeed.633*634* Call C_GenerateKeyPair() several times if CKR_FUNCTION_FAILED occurs.635*/636for (attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {637rv = (*ckpFunctions->C_GenerateKeyPair)(ckSessionHandle, ckpMechanism,638ckpPublicKeyAttributes, ckPublicKeyAttributesLength,639ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength,640ckpPublicKeyHandle, ckpPrivateKeyHandle);641if (rv == CKR_FUNCTION_FAILED) {642printDebug("C_1GenerateKeyPair(): C_GenerateKeyPair() failed \643with CKR_FUNCTION_FAILED error, try again\n");644} else {645break;646}647}648649if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {650jKeyHandles = ckULongArrayToJLongArray(env, ckpKeyHandles, 2);651}652653cleanup:654freeCKMechanismPtr(ckpMechanism);655free(ckpKeyHandles);656freeCKAttributeArray(ckpPublicKeyAttributes, ckPublicKeyAttributesLength);657freeCKAttributeArray(ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength);658return jKeyHandles ;659}660#endif661662#ifdef P11_ENABLE_C_WRAPKEY663/*664* Class: sun_security_pkcs11_wrapper_PKCS11665* Method: C_WrapKey666* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;JJ)[B667* Parametermapping: *PKCS11*668* @param jlong jSessionHandle CK_SESSION_HANDLE hSession669* @param jobject jMechanism CK_MECHANISM_PTR pMechanism670* @param jlong jWrappingKeyHandle CK_OBJECT_HANDLE hWrappingKey671* @param jlong jKeyHandle CK_OBJECT_HANDLE hKey672* @return jbyteArray jWrappedKey CK_BYTE_PTR pWrappedKey673* CK_ULONG_PTR pulWrappedKeyLen674*/675JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1WrapKey676(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jWrappingKeyHandle, jlong jKeyHandle)677{678CK_SESSION_HANDLE ckSessionHandle;679CK_MECHANISM_PTR ckpMechanism = NULL;680CK_OBJECT_HANDLE ckWrappingKeyHandle;681CK_OBJECT_HANDLE ckKeyHandle;682jbyteArray jWrappedKey = NULL;683CK_RV rv;684CK_BYTE BUF[MAX_STACK_BUFFER_LEN];685CK_BYTE_PTR ckpWrappedKey = BUF;686CK_ULONG ckWrappedKeyLength = MAX_STACK_BUFFER_LEN;687688CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);689if (ckpFunctions == NULL) { return NULL; }690691ckSessionHandle = jLongToCKULong(jSessionHandle);692ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);693if ((*env)->ExceptionCheck(env)) { return NULL; }694695ckWrappingKeyHandle = jLongToCKULong(jWrappingKeyHandle);696ckKeyHandle = jLongToCKULong(jKeyHandle);697698rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, ckpMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);699if (rv == CKR_BUFFER_TOO_SMALL) {700ckpWrappedKey = (CK_BYTE_PTR)701calloc(ckWrappedKeyLength, sizeof(CK_BYTE));702if (ckpWrappedKey == NULL) {703throwOutOfMemoryError(env, 0);704goto cleanup;705}706707rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, ckpMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);708}709if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {710jWrappedKey = ckByteArrayToJByteArray(env, ckpWrappedKey, ckWrappedKeyLength);711}712713cleanup:714if (ckpWrappedKey != BUF) { free(ckpWrappedKey); }715freeCKMechanismPtr(ckpMechanism);716717return jWrappedKey ;718}719#endif720721#ifdef P11_ENABLE_C_UNWRAPKEY722/*723* Class: sun_security_pkcs11_wrapper_PKCS11724* Method: C_UnwrapKey725* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[B[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J726* Parametermapping: *PKCS11*727* @param jlong jSessionHandle CK_SESSION_HANDLE hSession728* @param jobject jMechanism CK_MECHANISM_PTR pMechanism729* @param jlong jUnwrappingKeyHandle CK_OBJECT_HANDLE hUnwrappingKey730* @param jbyteArray jWrappedKey CK_BYTE_PTR pWrappedKey731* CK_ULONG_PTR pulWrappedKeyLen732* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate733* CK_ULONG ulCount734* @return jlong jKeyHandle CK_OBJECT_HANDLE_PTR phKey735*/736JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1UnwrapKey737(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jUnwrappingKeyHandle,738jbyteArray jWrappedKey, jobjectArray jTemplate)739{740CK_SESSION_HANDLE ckSessionHandle;741CK_MECHANISM_PTR ckpMechanism = NULL;742CK_OBJECT_HANDLE ckUnwrappingKeyHandle;743CK_BYTE_PTR ckpWrappedKey = NULL_PTR;744CK_ULONG ckWrappedKeyLength;745CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;746CK_ULONG ckAttributesLength = 0;747CK_OBJECT_HANDLE ckKeyHandle = 0;748jlong jKeyHandle = 0L;749CK_RV rv;750751CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);752if (ckpFunctions == NULL) { return 0L; }753754ckSessionHandle = jLongToCKULong(jSessionHandle);755ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);756if ((*env)->ExceptionCheck(env)) { return 0L; }757758ckUnwrappingKeyHandle = jLongToCKULong(jUnwrappingKeyHandle);759jByteArrayToCKByteArray(env, jWrappedKey, &ckpWrappedKey, &ckWrappedKeyLength);760if ((*env)->ExceptionCheck(env)) {761goto cleanup;762}763764jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);765if ((*env)->ExceptionCheck(env)) {766goto cleanup;767}768769770rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, ckpMechanism, ckUnwrappingKeyHandle,771ckpWrappedKey, ckWrappedKeyLength,772ckpAttributes, ckAttributesLength, &ckKeyHandle);773774if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {775jKeyHandle = ckLongToJLong(ckKeyHandle);776777#if 0778/* cheack, if we must give a initialization vector back to Java */779if (ckpMechanism->mechanism == CKM_KEY_WRAP_SET_OAEP) {780/* we must copy back the unwrapped key info to the jMechanism object */781copyBackSetUnwrappedKey(env, ckpMechanism, jMechanism);782}783#endif784}785cleanup:786freeCKMechanismPtr(ckpMechanism);787freeCKAttributeArray(ckpAttributes, ckAttributesLength);788free(ckpWrappedKey);789790return jKeyHandle ;791}792#endif793794#ifdef P11_ENABLE_C_DERIVEKEY795796/*797* Copy back the PRF output to Java.798*/799void copyBackTLSPrfParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism, jobject jMechanism)800{801jclass jMechanismClass, jTLSPrfParamsClass;802CK_TLS_PRF_PARAMS *ckTLSPrfParams;803jobject jTLSPrfParams;804jfieldID fieldID;805CK_MECHANISM_TYPE ckMechanismType;806jlong jMechanismType;807CK_BYTE_PTR output;808jobject jOutput;809jint jLength;810jbyte* jBytes;811int i;812813/* get mechanism */814jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);815if (jMechanismClass == NULL) { return; }816fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");817if (fieldID == NULL) { return; }818jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);819ckMechanismType = jLongToCKULong(jMechanismType);820if (ckMechanismType != ckpMechanism->mechanism) {821/* we do not have maching types, this should not occur */822return;823}824825/* get the native CK_TLS_PRF_PARAMS */826ckTLSPrfParams = (CK_TLS_PRF_PARAMS *) ckpMechanism->pParameter;827if (ckTLSPrfParams != NULL_PTR) {828/* get the Java CK_TLS_PRF_PARAMS object (pParameter) */829fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/lang/Object;");830if (fieldID == NULL) { return; }831jTLSPrfParams = (*env)->GetObjectField(env, jMechanism, fieldID);832833/* copy back the client IV */834jTLSPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);835if (jTLSPrfParamsClass == NULL) { return; }836fieldID = (*env)->GetFieldID(env, jTLSPrfParamsClass, "pOutput", "[B");837if (fieldID == NULL) { return; }838jOutput = (*env)->GetObjectField(env, jTLSPrfParams, fieldID);839output = ckTLSPrfParams->pOutput;840841// Note: we assume that the token returned exactly as many bytes as we842// requested. Anything else would not make sense.843if (jOutput != NULL) {844jLength = (*env)->GetArrayLength(env, jOutput);845jBytes = (*env)->GetByteArrayElements(env, jOutput, NULL);846if (jBytes == NULL) { return; }847848/* copy the bytes to the Java buffer */849for (i=0; i < jLength; i++) {850jBytes[i] = ckByteToJByte(output[i]);851}852/* copy back the Java buffer to the object */853(*env)->ReleaseByteArrayElements(env, jOutput, jBytes, 0);854}855}856}857858/*859* Class: sun_security_pkcs11_wrapper_PKCS11860* Method: C_DeriveKey861* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J862* Parametermapping: *PKCS11*863* @param jlong jSessionHandle CK_SESSION_HANDLE hSession864* @param jobject jMechanism CK_MECHANISM_PTR pMechanism865* @param jlong jBaseKeyHandle CK_OBJECT_HANDLE hBaseKey866* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate867* CK_ULONG ulCount868* @return jlong jKeyHandle CK_OBJECT_HANDLE_PTR phKey869*/870JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DeriveKey871(JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jBaseKeyHandle, jobjectArray jTemplate)872{873CK_SESSION_HANDLE ckSessionHandle;874CK_MECHANISM_PTR ckpMechanism = NULL;875CK_OBJECT_HANDLE ckBaseKeyHandle;876CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;877CK_ULONG ckAttributesLength = 0;878CK_OBJECT_HANDLE ckKeyHandle = 0;879jlong jKeyHandle = 0L;880CK_RV rv;881CK_OBJECT_HANDLE_PTR phKey = &ckKeyHandle;882883CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);884if (ckpFunctions == NULL) { return 0L; }885886ckSessionHandle = jLongToCKULong(jSessionHandle);887ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);888if ((*env)->ExceptionCheck(env)) { return 0L; }889890ckBaseKeyHandle = jLongToCKULong(jBaseKeyHandle);891jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);892if ((*env)->ExceptionCheck(env)) {893goto cleanup;894}895896switch (ckpMechanism->mechanism) {897case CKM_SSL3_KEY_AND_MAC_DERIVE:898case CKM_TLS_KEY_AND_MAC_DERIVE:899case CKM_TLS12_KEY_AND_MAC_DERIVE:900case CKM_TLS_PRF:901// these mechanism do not return a key handle via phKey902// set to NULL in case pedantic implementations check for it903phKey = NULL;904break;905default:906// empty907break;908}909910rv = (*ckpFunctions->C_DeriveKey)(ckSessionHandle, ckpMechanism, ckBaseKeyHandle,911ckpAttributes, ckAttributesLength, phKey);912913jKeyHandle = ckLongToJLong(ckKeyHandle);914915switch (ckpMechanism->mechanism) {916case CKM_SSL3_MASTER_KEY_DERIVE:917case CKM_TLS_MASTER_KEY_DERIVE:918/* we must copy back the client version */919ssl3CopyBackClientVersion(env, ckpMechanism, jMechanism);920break;921case CKM_TLS12_MASTER_KEY_DERIVE:922tls12CopyBackClientVersion(env, ckpMechanism, jMechanism);923break;924case CKM_SSL3_KEY_AND_MAC_DERIVE:925case CKM_TLS_KEY_AND_MAC_DERIVE:926/* we must copy back the unwrapped key info to the jMechanism object */927ssl3CopyBackKeyMatParams(env, ckpMechanism, jMechanism);928break;929case CKM_TLS12_KEY_AND_MAC_DERIVE:930/* we must copy back the unwrapped key info to the jMechanism object */931tls12CopyBackKeyMatParams(env, ckpMechanism, jMechanism);932break;933case CKM_TLS_PRF:934copyBackTLSPrfParams(env, ckpMechanism, jMechanism);935break;936default:937// empty938break;939}940if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {941jKeyHandle =0L;942}943944cleanup:945freeCKMechanismPtr(ckpMechanism);946freeCKAttributeArray(ckpAttributes, ckAttributesLength);947948return jKeyHandle ;949}950951static void copyBackClientVersion(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism, jobject jMechanism,952CK_VERSION *ckVersion, const char *class_master_key_derive_params)953{954jclass jMasterKeyDeriveParamsClass, jMechanismClass, jVersionClass;955jobject jMasterKeyDeriveParams;956jfieldID fieldID;957CK_MECHANISM_TYPE ckMechanismType;958jlong jMechanismType;959jobject jVersion;960961/* get mechanism */962jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);963if (jMechanismClass == NULL) { return; }964fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");965if (fieldID == NULL) { return; }966jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);967ckMechanismType = jLongToCKULong(jMechanismType);968if (ckMechanismType != ckpMechanism->mechanism) {969/* we do not have maching types, this should not occur */970return;971}972973if (ckVersion != NULL_PTR) {974/* get the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS (pParameter) */975fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/lang/Object;");976if (fieldID == NULL) { return; }977978jMasterKeyDeriveParams = (*env)->GetObjectField(env, jMechanism, fieldID);979980/* get the Java CK_VERSION */981jMasterKeyDeriveParamsClass = (*env)->FindClass(env, class_master_key_derive_params);982if (jMasterKeyDeriveParamsClass == NULL) { return; }983fieldID = (*env)->GetFieldID(env, jMasterKeyDeriveParamsClass,984"pVersion", "L"CLASS_VERSION";");985if (fieldID == NULL) { return; }986jVersion = (*env)->GetObjectField(env, jMasterKeyDeriveParams, fieldID);987988/* now copy back the version from the native structure to the Java structure */989990/* copy back the major version */991jVersionClass = (*env)->FindClass(env, CLASS_VERSION);992if (jVersionClass == NULL) { return; }993fieldID = (*env)->GetFieldID(env, jVersionClass, "major", "B");994if (fieldID == NULL) { return; }995(*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->major));996997/* copy back the minor version */998fieldID = (*env)->GetFieldID(env, jVersionClass, "minor", "B");999if (fieldID == NULL) { return; }1000(*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->minor));1001}1002}10031004/*1005* Copy back the client version information from the native1006* structure to the Java object. This is only used for1007* CKM_SSL3_MASTER_KEY_DERIVE and CKM_TLS_MASTER_KEY_DERIVE1008* mechanisms when used for deriving a key.1009*1010*/1011void ssl3CopyBackClientVersion(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,1012jobject jMechanism)1013{1014CK_SSL3_MASTER_KEY_DERIVE_PARAMS *ckSSL3MasterKeyDeriveParams;1015ckSSL3MasterKeyDeriveParams =1016(CK_SSL3_MASTER_KEY_DERIVE_PARAMS *)ckpMechanism->pParameter;1017if (ckSSL3MasterKeyDeriveParams != NULL_PTR) {1018copyBackClientVersion(env, ckpMechanism, jMechanism,1019ckSSL3MasterKeyDeriveParams->pVersion,1020CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);1021}1022}10231024/*1025* Copy back the client version information from the native1026* structure to the Java object. This is only used for1027* CKM_TLS12_MASTER_KEY_DERIVE mechanism when used for deriving a key.1028*1029*/1030void tls12CopyBackClientVersion(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,1031jobject jMechanism)1032{1033CK_TLS12_MASTER_KEY_DERIVE_PARAMS *ckTLS12MasterKeyDeriveParams;1034ckTLS12MasterKeyDeriveParams =1035(CK_TLS12_MASTER_KEY_DERIVE_PARAMS *)ckpMechanism->pParameter;1036if (ckTLS12MasterKeyDeriveParams != NULL_PTR) {1037copyBackClientVersion(env, ckpMechanism, jMechanism,1038ckTLS12MasterKeyDeriveParams->pVersion,1039CLASS_TLS12_MASTER_KEY_DERIVE_PARAMS);1040}1041}10421043static void copyBackKeyMatParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,1044jobject jMechanism, CK_SSL3_RANDOM_DATA *RandomInfo,1045CK_SSL3_KEY_MAT_OUT_PTR ckSSL3KeyMatOut, const char *class_key_mat_params)1046{1047jclass jMechanismClass, jKeyMatParamsClass, jSSL3KeyMatOutClass;1048jfieldID fieldID;1049CK_MECHANISM_TYPE ckMechanismType;1050jlong jMechanismType;1051CK_BYTE_PTR iv;1052jobject jKeyMatParam;1053jobject jSSL3KeyMatOut;1054jobject jIV;1055jint jLength;1056jbyte* jBytes;1057int i;10581059/* get mechanism */1060jMechanismClass= (*env)->FindClass(env, CLASS_MECHANISM);1061if (jMechanismClass == NULL) { return; }1062fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");1063if (fieldID == NULL) { return; }1064jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);1065ckMechanismType = jLongToCKULong(jMechanismType);1066if (ckMechanismType != ckpMechanism->mechanism) {1067/* we do not have maching types, this should not occur */1068return;1069}10701071if (ckSSL3KeyMatOut != NULL_PTR) {1072/* get the Java params object (pParameter) */1073fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter",1074"Ljava/lang/Object;");1075if (fieldID == NULL) { return; }1076jKeyMatParam = (*env)->GetObjectField(env, jMechanism, fieldID);10771078/* get the Java CK_SSL3_KEY_MAT_OUT */1079jKeyMatParamsClass = (*env)->FindClass(env, class_key_mat_params);1080if (jKeyMatParamsClass == NULL) { return; }1081fieldID = (*env)->GetFieldID(env, jKeyMatParamsClass,1082"pReturnedKeyMaterial", "L"CLASS_SSL3_KEY_MAT_OUT";");1083if (fieldID == NULL) { return; }1084jSSL3KeyMatOut = (*env)->GetObjectField(env, jKeyMatParam, fieldID);10851086/* now copy back all the key handles and the initialization vectors */1087/* copy back client MAC secret handle */1088jSSL3KeyMatOutClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_OUT);1089if (jSSL3KeyMatOutClass == NULL) { return; }1090fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass,1091"hClientMacSecret", "J");1092if (fieldID == NULL) { return; }1093(*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,1094ckULongToJLong(ckSSL3KeyMatOut->hClientMacSecret));10951096/* copy back server MAC secret handle */1097fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass,1098"hServerMacSecret", "J");1099if (fieldID == NULL) { return; }1100(*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,1101ckULongToJLong(ckSSL3KeyMatOut->hServerMacSecret));11021103/* copy back client secret key handle */1104fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "hClientKey", "J");1105if (fieldID == NULL) { return; }1106(*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,1107ckULongToJLong(ckSSL3KeyMatOut->hClientKey));11081109/* copy back server secret key handle */1110fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "hServerKey", "J");1111if (fieldID == NULL) { return; }1112(*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,1113ckULongToJLong(ckSSL3KeyMatOut->hServerKey));11141115/* copy back the client IV */1116fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "pIVClient", "[B");1117if (fieldID == NULL) { return; }1118jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);1119iv = ckSSL3KeyMatOut->pIVClient;11201121if (jIV != NULL) {1122jLength = (*env)->GetArrayLength(env, jIV);1123jBytes = (*env)->GetByteArrayElements(env, jIV, NULL);1124if (jBytes == NULL) { return; }1125/* copy the bytes to the Java buffer */1126for (i=0; i < jLength; i++) {1127jBytes[i] = ckByteToJByte(iv[i]);1128}1129/* copy back the Java buffer to the object */1130(*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);1131}11321133/* copy back the server IV */1134fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "pIVServer", "[B");1135if (fieldID == NULL) { return; }1136jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);1137iv = ckSSL3KeyMatOut->pIVServer;11381139if (jIV != NULL) {1140jLength = (*env)->GetArrayLength(env, jIV);1141jBytes = (*env)->GetByteArrayElements(env, jIV, NULL);1142if (jBytes == NULL) { return; }1143/* copy the bytes to the Java buffer */1144for (i=0; i < jLength; i++) {1145jBytes[i] = ckByteToJByte(iv[i]);1146}1147/* copy back the Java buffer to the object */1148(*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);1149}1150}1151}11521153/*1154* Copy back the derived keys and initialization vectors from the native1155* structure to the Java object. This is only used for1156* CKM_SSL3_KEY_AND_MAC_DERIVE and CKM_TLS_KEY_AND_MAC_DERIVE mechanisms1157* when used for deriving a key.1158*1159*/1160void ssl3CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,1161jobject jMechanism)1162{1163CK_SSL3_KEY_MAT_PARAMS *ckSSL3KeyMatParam;1164ckSSL3KeyMatParam = (CK_SSL3_KEY_MAT_PARAMS *)ckpMechanism->pParameter;1165if (ckSSL3KeyMatParam != NULL_PTR) {1166copyBackKeyMatParams(env, ckpMechanism, jMechanism,1167&(ckSSL3KeyMatParam->RandomInfo),1168ckSSL3KeyMatParam->pReturnedKeyMaterial,1169CLASS_SSL3_KEY_MAT_PARAMS);1170}1171}11721173/*1174* Copy back the derived keys and initialization vectors from the native1175* structure to the Java object. This is only used for1176* CKM_TLS12_KEY_AND_MAC_DERIVE mechanism when used for deriving a key.1177*1178*/1179void tls12CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,1180jobject jMechanism)1181{1182CK_TLS12_KEY_MAT_PARAMS *ckTLS12KeyMatParam;1183ckTLS12KeyMatParam = (CK_TLS12_KEY_MAT_PARAMS *)ckpMechanism->pParameter;1184if (ckTLS12KeyMatParam != NULL_PTR) {1185copyBackKeyMatParams(env, ckpMechanism, jMechanism,1186&(ckTLS12KeyMatParam->RandomInfo),1187ckTLS12KeyMatParam->pReturnedKeyMaterial,1188CLASS_TLS12_KEY_MAT_PARAMS);1189}1190}11911192#endif119311941195