3645N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A/* Copyright (c) 2002 Graz University of Technology. All rights reserved. 0N/A * Redistribution and use in source and binary forms, with or without 0N/A * modification, are permitted provided that the following conditions are met: 0N/A * 1. Redistributions of source code must retain the above copyright notice, 0N/A * this list of conditions and the following disclaimer. 0N/A * 2. Redistributions in binary form must reproduce the above copyright notice, 0N/A * this list of conditions and the following disclaimer in the documentation 0N/A * and/or other materials provided with the distribution. 0N/A * 3. The end-user documentation included with the redistribution, if any, must 0N/A * include the following acknowledgment: 0N/A * "This product includes software developed by IAIK of Graz University of 0N/A * Alternately, this acknowledgment may appear in the software itself, if 0N/A * and wherever such third-party acknowledgments normally appear. 0N/A * 4. The names "Graz University of Technology" and "IAIK of Graz University of 0N/A * Technology" must not be used to endorse or promote products derived from 0N/A * this software without prior written permission. 0N/A * 5. Products derived from this software may not be called 0N/A * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior 0N/A * written permission of Graz University of Technology. 0N/A * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 0N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 0N/A * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE 0N/A * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 0N/A * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 0N/A * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 0N/A * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 0N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 0N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 0N/A * POSSIBILITY OF SUCH DAMAGE. 0N/A * This is the implementation of the native functions of the Java to PKCS#11 interface. 0N/A * All function use some helper functions to convert the JNI types to PKCS#11 types. 0N/A * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at> 0N/A * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at> 0N/A/* declare file private functions */ 0N/A * converts a pointer to a CK_DATE structure into a Java CK_DATE Object. 0N/A * @param env - used to call JNI funktions to create the new Java object 0N/A * @param ckpValue - the pointer to the CK_DATE structure 0N/A * @return - the new Java CK_DATE object 0N/A /* load CK_DATE class */ 0N/A /* load CK_DATE constructor */ 0N/A /* prep all fields */ 0N/A /* create new CK_DATE object */ 0N/A /* free local references */ 0N/A * converts a pointer to a CK_VERSION structure into a Java CK_VERSION Object. 0N/A * @param env - used to call JNI funktions to create the new Java object 0N/A * @param ckpVersion - the pointer to the CK_VERSION structure 0N/A * @return - the new Java CK_VERSION object 0N/A /* load CK_VERSION class */ 0N/A /* load CK_VERSION constructor */ 0N/A /* prep both fields */ 0N/A /* create new CK_VERSION object */ 0N/A /* free local references */ 0N/A * converts a pointer to a CK_SESSION_INFO structure into a Java CK_SESSION_INFO Object. 0N/A * @param env - used to call JNI funktions to create the new Java object 0N/A * @param ckpSessionInfo - the pointer to the CK_SESSION_INFO structure 0N/A * @return - the new Java CK_SESSION_INFO object 0N/A /* load CK_SESSION_INFO class */ 0N/A /* load CK_SESSION_INFO constructor */ 0N/A /* prep all fields */ 0N/A /* create new CK_SESSION_INFO object */ 0N/A /* free local references */ 0N/A * converts a pointer to a CK_ATTRIBUTE structure into a Java CK_ATTRIBUTE Object. 0N/A * @param env - used to call JNI funktions to create the new Java object 0N/A * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure 0N/A * @return - the new Java CK_ATTRIBUTE object 0N/A /* load CK_INFO constructor */ 0N/A /* prep both fields */ 0N/A /* create new CK_ATTRIBUTE object */ 0N/A /* free local references */ 0N/A * converts a Java CK_VERSION object into a pointer to a CK_VERSION structure 0N/A * @param env - used to call JNI funktions to get the values out of the Java object 0N/A * @param jVersion - the Java CK_VERSION object to convert 0N/A * @return - the pointer to the new CK_VERSION structure 0N/A /* get CK_VERSION class */ 936N/A /* allocate memory for CK_VERSION pointer */ 0N/A * converts a Java CK_DATE object into a pointer to a CK_DATE structure 0N/A * @param env - used to call JNI funktions to get the values out of the Java object 0N/A * @param jVersion - the Java CK_DATE object to convert 0N/A * @return - the pointer to the new CK_DATE structure 0N/A /* get CK_DATE class */ 936N/A /* allocate memory for CK_DATE pointer */ 0N/A * converts a Java CK_ATTRIBUTE object into a CK_ATTRIBUTE structure 0N/A * @param env - used to call JNI funktions to get the values out of the Java object 0N/A * @param jAttribute - the Java CK_ATTRIBUTE object to convert 0N/A * @return - the new CK_ATTRIBUTE structure 936N/A // TBD: what if jAttribute == NULL?! 0N/A /* get CK_ATTRIBUTE class */ 0N/A TRACE0(
", converting pValue to primitive object");
0N/A /* convert the Java pValue object to a CK-type pValue pointer */ 0N/A * converts the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to a 0N/A * CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to convert 0N/A * @return - the new CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure 0N/A // XXX don't return structs 0N/A // XXX prefetch class and field ids 0N/A /* get RandomInfo */ 0N/A /* get pClientRandom and ulClientRandomLength out of RandomInfo */ 0N/A /* get pServerRandom and ulServerRandomLength out of RandomInfo */ 936N/A /* populate java values */ 0N/A * converts the Java CK_TLS_PRF_PARAMS object to a CK_TLS_PRF_PARAMS structure 936N/A // TBD: what if jParam == NULL?! 936N/A /* populate java values */ 0N/A * converts the Java CK_SSL3_KEY_MAT_PARAMS object to a CK_SSL3_KEY_MAT_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_SSL3_KEY_MAT_PARAMS object to convert 0N/A * @return - the new CK_SSL3_KEY_MAT_PARAMS structure 0N/A // XXX don't return structs 0N/A // XXX prefetch class and field ids 0N/A /* get ulMacSizeInBits */ 0N/A /* get ulKeySizeInBits */ 0N/A /* get ulIVSizeInBits */ 0N/A /* get RandomInfo */ 0N/A /* get pClientRandom and ulClientRandomLength out of RandomInfo */ 0N/A /* get pServerRandom and ulServerRandomLength out of RandomInfo */ 0N/A /* get pReturnedKeyMaterial */ 936N/A /* get pIVClient out of pReturnedKeyMaterial */ 936N/A /* get pIVServer out of pReturnedKeyMaterial */ 936N/A /* populate java values */ 0N/A /* allocate memory for pRetrunedKeyMaterial */ 0N/A // the handles are output params only, no need to fetch them from Java 3645N/A * converts the Java CK_AES_CTR_PARAMS object to a CK_AES_CTR_PARAMS structure 3645N/A * @param env - used to call JNI funktions to get the Java classes and objects 3645N/A * @param jParam - the Java CK_AES_CTR_PARAMS object to convert 3645N/A * @param ckpParam - pointer to the new CK_AES_CTR_PARAMS structure 0N/A * converts a Java CK_MECHANISM object into a CK_MECHANISM structure 0N/A * @param env - used to call JNI funktions to get the values out of the Java object 0N/A * @param jMechanism - the Java CK_MECHANISM object to convert 0N/A * @return - the new CK_MECHANISM structure 0N/A /* convert the specific Java mechanism parameter object to a pointer to a CK-type mechanism 0N/A * the following functions convert Attribute and Mechanism value pointers 0N/A * jobject ckAttributeValueToJObject(JNIEnv *env, 0N/A * const CK_ATTRIBUTE_PTR ckpAttribute); 0N/A * void jObjectToPrimitiveCKObjectPtrPtr(JNIEnv *env, 0N/A * CK_VOID_PTR *ckpObjectPtr, 0N/A * CK_ULONG *pLength); 0N/A * void jMechanismParameterToCKMechanismParameter(JNIEnv *env, 0N/A * CK_VOID_PTR *ckpParamPtr, 0N/A * CK_ULONG *ckpLength); 0N/A * These functions are used if a PKCS#11 mechanism or attribute structure gets 0N/A * convertet to a Java attribute or mechanism object or vice versa. 0N/A * ckAttributeValueToJObject converts a PKCS#11 attribute value pointer to a Java 0N/A * object depending on the type of the Attribute. A PKCS#11 attribute value can 0N/A * be a CK_ULONG, CK_BYTE[], CK_CHAR[], big integer, CK_BBOOL, CK_UTF8CHAR[], 0N/A * CK_DATE or CK_FLAGS that gets converted to a corresponding Java object. 0N/A * jObjectToPrimitiveCKObjectPtrPtr is used by jAttributeToCKAttributePtr for 0N/A * converting the Java attribute value to a PKCS#11 attribute value pointer. 0N/A * For now only primitive datatypes and arrays of primitive datatypes can get 0N/A * converted. Otherwise this function throws a PKCS#11Exception with the 0N/A * errorcode CKR_VENDOR_DEFINED. 0N/A * jMechanismParameterToCKMechanismParameter converts a Java mechanism parameter 0N/A * to a PKCS#11 mechanism parameter. First this function determines what mechanism 0N/A * parameter the Java object is, then it allocates the memory for the new PKCS#11 0N/A * structure and calls the corresponding function to convert the Java object to 0N/A * a PKCS#11 mechanism parameter structure. 0N/A * converts the pValue of a CK_ATTRIBUTE structure into a Java Object by checking the type 0N/A * @param env - used to call JNI funktions to create the new Java object 0N/A * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure that contains the type 0N/A * and the pValue to convert 0N/A * @return - the new Java object of the CK-type pValue 0N/A /* value CK_OBJECT_CLASS, defacto a CK_ULONG */ 0N/A /* value CK_KEY_TYPE, defacto a CK_ULONG */ 0N/A /* value CK_CERTIFICATE_TYPE, defacto a CK_ULONG */ 0N/A /* value CK_HW_FEATURE_TYPE, defacto a CK_ULONG */ 0N/A /* value CK_ULONG */ 0N/A /* can be CK_BYTE[],CK_CHAR[] or big integer; defacto always CK_BYTE[] */ 0N/A /* CKA_EC_PARAMS is the same, these two are equivalent */ 0N/A /* value CK_BYTE[] */ 0N/A /* value CK_BBOOL */ 0N/A /* value RFC 2279 (UTF-8) string */ 0N/A /* value big integer, i.e. CK_BYTE[] */ 0N/A /* value FLAGS, defacto a CK_ULONG */ 0N/A /* we make a CK_BYTE[] out of this */ 0N/A // Netscape trust attributes 0N/A /* value CK_ULONG */ 0N/A /* we make a CK_BYTE[] out of this */ 0N/A * the following functions convert a Java mechanism parameter object to a PKCS#11 0N/A * mechanism parameter structure 0N/A * CK_<Param>_PARAMS j<Param>ParamToCK<Param>Param(JNIEnv *env, 0N/A * These functions get a Java object, that must be the right Java mechanism 0N/A * object and they return the new PKCS#11 mechanism parameter structure. 0N/A * Every field of the Java object is retrieved, gets converted to a corresponding 0N/A * PKCS#11 type and is set in the new PKCS#11 structure. 0N/A * converts the given Java mechanism parameter to a CK mechanism parameter structure 0N/A * and store the length in bytes in the length variable. 0N/A * The memory of *ckpParamPtr has to be freed after use! 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java mechanism parameter object to convert 0N/A * @param ckpParamPtr - the reference of the new pointer to the new CK mechanism parameter 0N/A * @param ckpLength - the reference of the length in bytes of the new CK mechanism parameter 936N/A TRACE0(
"\nSLOW PATH jMechanismParameterToCKMechanismParameter\n");
0N/A /* get all Java mechanism parameter classes */ 0N/A TRACE0(
"\nDEBUG: jMechanismParameterToCKMechanismParameter");
936N/A /* most common cases, i.e. NULL/byte[]/long, are already handled by 936N/A * jMechanismParameterToCKMechanismParameter before calling this method. 0N/A * CK_VERSION used by CKM_SSL3_PRE_MASTER_KEY_GEN 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_SSL3_MASTER_KEY_DERIVE_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_SSL3_KEY_MAT_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 936N/A /* convert jParameter to CKParameter */ 936N/A /* get length and pointer of parameter */ 3645N/A /* convert jParameter to CKParameter */ 3645N/A /* get length and pointer of parameter */ 0N/A * CK_RSA_PKCS_OAEP_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_PKCS5_PBKD2_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_RSA_PKCS_PSS_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_ECDH1_DERIVE_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_ECDH2_DERIVE_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_X9_42_DH1_DERIVE_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 0N/A * CK_X9_42_DH2_DERIVE_PARAMS 0N/A /* convert jParameter to CKParameter */ 0N/A /* get length and pointer of parameter */ 936N/A /* if everything faild up to here */ 936N/A /* try if the parameter is a primitive Java type */ 936N/A /* *ckpParamPtr = jObjectToCKVoidPtr(jParam); */ 0N/A/* the mechanism parameter convertion functions: */ 0N/A * converts the Java CK_RSA_PKCS_OAEP_PARAMS object to a CK_RSA_PKCS_OAEP_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_RSA_PKCS_OAEP_PARAMS object to convert 0N/A * @return - the new CK_RSA_PKCS_OAEP_PARAMS structure 0N/A /* get sourceData and sourceDataLength */ 936N/A /* populate java values */ 0N/A * converts the Java CK_PBE_PARAMS object to a CK_PBE_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_PBE_PARAMS object to convert 0N/A * @return - the new CK_PBE_PARAMS structure 0N/A /* get pInitVector */ 0N/A /* get pPassword and ulPasswordLength */ 0N/A /* get pSalt and ulSaltLength */ 0N/A /* get ulIteration */ 936N/A /* populate java values */ 0N/A * Copy back the initialization vector from the native structure to the 0N/A * Java object. This is only used for CKM_PBE_* mechanisms and their 0N/A * CK_PBE_PARAMS parameters. 0N/A /* we do not have maching types, this should not occur */ 0N/A /* get pParameter */ 0N/A /* copy the chars to the Java buffer */ 0N/A /* copy back the Java buffer to the object */ 0N/A * converts the Java CK_PKCS5_PBKD2_PARAMS object to a CK_PKCS5_PBKD2_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_PKCS5_PBKD2_PARAMS object to convert 0N/A * @return - the new CK_PKCS5_PBKD2_PARAMS structure 0N/A /* get saltSource */ 0N/A /* get pSaltSourceData */ 0N/A /* get iterations */ 0N/A /* get pPrfData and ulPrfDataLength in byte */ 936N/A /* populate java values */ 0N/A * converts the Java CK_RSA_PKCS_PSS_PARAMS object to a CK_RSA_PKCS_PSS_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_RSA_PKCS_PSS_PARAMS object to convert 0N/A * @return - the new CK_RSA_PKCS_PSS_PARAMS structure 936N/A /* populate java values */ 0N/A * converts the Java CK_ECDH1_DERIVE_PARAMS object to a CK_ECDH1_DERIVE_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_ECDH1_DERIVE_PARAMS object to convert 0N/A * @return - the new CK_ECDH1_DERIVE_PARAMS structure 0N/A /* get pSharedData and ulSharedDataLen */ 0N/A /* get pPublicData and ulPublicDataLen */ 936N/A /* populate java values */ 0N/A * converts the Java CK_ECDH2_DERIVE_PARAMS object to a CK_ECDH2_DERIVE_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_ECDH2_DERIVE_PARAMS object to convert 0N/A * @return - the new CK_ECDH2_DERIVE_PARAMS structure 0N/A /* get pSharedData and ulSharedDataLen */ 0N/A /* get pPublicData and ulPublicDataLen */ 0N/A /* get ulPrivateDataLen */ 0N/A /* get hPrivateData */ 0N/A /* get pPublicData2 and ulPublicDataLen2 */ 936N/A /* populate java values */ 0N/A * converts the Java CK_X9_42_DH1_DERIVE_PARAMS object to a CK_X9_42_DH1_DERIVE_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_X9_42_DH1_DERIVE_PARAMS object to convert 0N/A * @return - the new CK_X9_42_DH1_DERIVE_PARAMS structure 0N/A /* get pOtherInfo and ulOtherInfoLen */ 0N/A /* get pPublicData and ulPublicDataLen */ 936N/A /* populate java values */ 0N/A * converts the Java CK_X9_42_DH2_DERIVE_PARAMS object to a CK_X9_42_DH2_DERIVE_PARAMS structure 0N/A * @param env - used to call JNI funktions to get the Java classes and objects 0N/A * @param jParam - the Java CK_X9_42_DH2_DERIVE_PARAMS object to convert 0N/A * @return - the new CK_X9_42_DH2_DERIVE_PARAMS structure 0N/A /* get pOtherInfo and ulOtherInfoLen */ 0N/A /* get pPublicData and ulPublicDataLen */ 0N/A /* get ulPrivateDataLen */ 0N/A /* get hPrivateData */ 0N/A /* get pPublicData2 and ulPublicDataLen2 */ 936N/A /* populate java values */