src/share/native/sun/security/ec/ECC_JNI.cpp

Print this page




  45         env->ThrowNew(exceptionClazz, NULL);
  46     }
  47 }
  48 
  49 /*
  50  * Deep free of the ECParams struct
  51  */
  52 void FreeECParams(ECParams *ecparams, jboolean freeStruct)
  53 {
  54     // Use B_FALSE to free the SECItem->data element, but not the SECItem itself
  55     // Use B_TRUE to free both
  56 
  57     SECITEM_FreeItem(&ecparams->fieldID.u.prime, B_FALSE);
  58     SECITEM_FreeItem(&ecparams->curve.a, B_FALSE);
  59     SECITEM_FreeItem(&ecparams->curve.b, B_FALSE);
  60     SECITEM_FreeItem(&ecparams->curve.seed, B_FALSE);
  61     SECITEM_FreeItem(&ecparams->base, B_FALSE);
  62     SECITEM_FreeItem(&ecparams->order, B_FALSE);
  63     SECITEM_FreeItem(&ecparams->DEREncoding, B_FALSE);
  64     SECITEM_FreeItem(&ecparams->curveOID, B_FALSE);
  65     if (freeStruct)
  66         free(ecparams);

  67 }
  68 
  69 /*
  70  * Class:     sun_security_ec_ECKeyPairGenerator
  71  * Method:    generateECKeyPair
  72  * Signature: (I[B[B)[J
  73  */
  74 JNIEXPORT jlongArray
  75 JNICALL Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair
  76   (JNIEnv *env, jclass clazz, jint keySize, jbyteArray encodedParams, jbyteArray seed)
  77 {
  78     ECPrivateKey *privKey = NULL; /* contains both public and private values */
  79     ECParams *ecparams = NULL;
  80     SECKEYECParams params_item;
  81     jint jSeedLength;
  82     jbyte* pSeedBuffer = NULL;
  83     jlongArray result = NULL;
  84     jlong* resultElements = NULL;
  85 
  86     // Initialize the ECParams struct


 122     }
 123 
 124     resultElements[0] = (jlong) &(privKey->privateValue); // private big integer
 125     resultElements[1] = (jlong) &(privKey->publicValue); // encoded ec point
 126 
 127     // If the array is a copy then we must write back our changes
 128     if (isCopy == JNI_TRUE) {
 129         env->ReleaseLongArrayElements(result, resultElements, 0);
 130     }
 131 
 132 cleanup:
 133     {
 134         if (params_item.data)
 135             env->ReleaseByteArrayElements(encodedParams,
 136                 (jbyte *) params_item.data, JNI_ABORT);
 137 
 138         if (ecparams)
 139             FreeECParams(ecparams, true);
 140 
 141         if (privKey) {
 142             FreeECParams(&privKey->ecParams, false);
 143             SECITEM_FreeItem(&privKey->version, B_FALSE);
 144             // Don't free privKey->privateValue and privKey->publicValue



 145         }
 146 
 147         if (pSeedBuffer)
 148             delete [] pSeedBuffer;
 149     }
 150 
 151     return result;

























 152 }
 153 
 154 /*
 155  * Class:     sun_security_ec_ECKeyPairGenerator
 156  * Method:    getEncodedBytes
 157  * Signature: (J)[B
 158  */
 159 JNIEXPORT jbyteArray
 160 JNICALL Java_sun_security_ec_ECKeyPairGenerator_getEncodedBytes
 161   (JNIEnv *env, jclass clazz, jlong hSECItem)
 162 {
 163     SECItem *s = (SECItem *)hSECItem;
 164     jbyteArray jEncodedBytes = env->NewByteArray(s->len);
 165     if (jEncodedBytes == NULL) {
 166         return NULL;
 167     }
 168 
 169     // Copy bytes from a native SECItem buffer to Java byte array
 170     env->SetByteArrayRegion(jEncodedBytes, 0, s->len, (jbyte *)s->data);
 171 




  45         env->ThrowNew(exceptionClazz, NULL);
  46     }
  47 }
  48 
  49 /*
  50  * Deep free of the ECParams struct
  51  */
  52 void FreeECParams(ECParams *ecparams, jboolean freeStruct)
  53 {
  54     // Use B_FALSE to free the SECItem->data element, but not the SECItem itself
  55     // Use B_TRUE to free both
  56 
  57     SECITEM_FreeItem(&ecparams->fieldID.u.prime, B_FALSE);
  58     SECITEM_FreeItem(&ecparams->curve.a, B_FALSE);
  59     SECITEM_FreeItem(&ecparams->curve.b, B_FALSE);
  60     SECITEM_FreeItem(&ecparams->curve.seed, B_FALSE);
  61     SECITEM_FreeItem(&ecparams->base, B_FALSE);
  62     SECITEM_FreeItem(&ecparams->order, B_FALSE);
  63     SECITEM_FreeItem(&ecparams->DEREncoding, B_FALSE);
  64     SECITEM_FreeItem(&ecparams->curveOID, B_FALSE);
  65     if (freeStruct) {
  66         PORT_ZFree(ecparams, sizeof(ECParams));
  67     }
  68 }
  69 
  70 /*
  71  * Class:     sun_security_ec_ECKeyPairGenerator
  72  * Method:    generateECKeyPair
  73  * Signature: (I[B[B)[J
  74  */
  75 JNIEXPORT jlongArray
  76 JNICALL Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair
  77   (JNIEnv *env, jclass clazz, jint keySize, jbyteArray encodedParams, jbyteArray seed)
  78 {
  79     ECPrivateKey *privKey = NULL; /* contains both public and private values */
  80     ECParams *ecparams = NULL;
  81     SECKEYECParams params_item;
  82     jint jSeedLength;
  83     jbyte* pSeedBuffer = NULL;
  84     jlongArray result = NULL;
  85     jlong* resultElements = NULL;
  86 
  87     // Initialize the ECParams struct


 123     }
 124 
 125     resultElements[0] = (jlong) &(privKey->privateValue); // private big integer
 126     resultElements[1] = (jlong) &(privKey->publicValue); // encoded ec point
 127 
 128     // If the array is a copy then we must write back our changes
 129     if (isCopy == JNI_TRUE) {
 130         env->ReleaseLongArrayElements(result, resultElements, 0);
 131     }
 132 
 133 cleanup:
 134     {
 135         if (params_item.data)
 136             env->ReleaseByteArrayElements(encodedParams,
 137                 (jbyte *) params_item.data, JNI_ABORT);
 138 
 139         if (ecparams)
 140             FreeECParams(ecparams, true);
 141 
 142         if (privKey) {

 143             SECITEM_FreeItem(&privKey->version, B_FALSE);
 144             // Don't free privKey->privateValue and privKey->publicValue.
 145             // Also, don't free privKey->ecParams, because it contains our only
 146             // link to the arena.  If this code is ever migrated to use arenas,
 147             // then we will need to free it later.
 148         }
 149 
 150         if (pSeedBuffer)
 151             delete [] pSeedBuffer;
 152     }
 153 
 154     return result;
 155 }
 156 
 157 /*
 158  * Class:     sun_security_ec_ECKeyPairGenerator
 159  * Method:    releaseECKeyPairData
 160  * Signature: (JJ)V
 161  */
 162 JNIEXPORT void
 163 JNICALL Java_sun_security_ec_ECKeyPairGenerator_releaseECKeyPairData
 164   (JNIEnv *env, jclass clazz, jlong pk) {
 165   ECPrivateKey *privKey = reinterpret_cast<ECPrivateKey *>(pk);
 166   if (privKey) {
 167     PRArenaPool *arena = privKey->ecParams.arena;
 168 
 169     FreeECParams(&privKey->ecParams, false);
 170     SECITEM_FreeItem(&privKey->privateValue, B_FALSE);
 171     SECITEM_FreeItem(&privKey->publicValue, B_FALSE);
 172 
 173     // In principle, we should just be able to free the arena.
 174     // We add ZFree here because the JDK's FreeArena is currently a nop.
 175     PORT_ZFree(privKey, sizeof(ECPrivateKey));
 176     if (arena) {
 177       PORT_FreeArena(arena, B_TRUE);
 178     }
 179   }
 180 }
 181 
 182 /*
 183  * Class:     sun_security_ec_ECKeyPairGenerator
 184  * Method:    getEncodedBytes
 185  * Signature: (J)[B
 186  */
 187 JNIEXPORT jbyteArray
 188 JNICALL Java_sun_security_ec_ECKeyPairGenerator_getEncodedBytes
 189   (JNIEnv *env, jclass clazz, jlong hSECItem)
 190 {
 191     SECItem *s = (SECItem *)hSECItem;
 192     jbyteArray jEncodedBytes = env->NewByteArray(s->len);
 193     if (jEncodedBytes == NULL) {
 194         return NULL;
 195     }
 196 
 197     // Copy bytes from a native SECItem buffer to Java byte array
 198     env->SetByteArrayRegion(jEncodedBytes, 0, s->len, (jbyte *)s->data);
 199