< prev index next >

src/jdk.crypto.ucrypto/solaris/native/libj2ucrypto/nativeCrypto.c

Print this page
rev 14307 : 8155211: Ucrypto Library leaks native memory
Reviewed-by:


 633   } else {
 634     (*env)->SetByteArrayRegion(env, jOut, outOfs, outLen, (jbyte *)bufOut);
 635     free(bufIn);
 636     free(bufOut);
 637     return outLen;
 638   }
 639 }
 640 
 641 /*
 642  * Class:     com_oracle_security_ucrypto_NativeCipher
 643  * Method:    nativeFinal
 644  * Signature: (JZ[BI)I
 645  */
 646 JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeCipher_nativeFinal
 647   (JNIEnv *env, jclass jCls, jlong contextID, jboolean encrypt,
 648    jbyteArray out, jint outOfs) {
 649   crypto_ctx_t *context;
 650   unsigned char *bufIn;
 651   unsigned char *bufOut;
 652   int outLen, rv = 0;

 653 
 654   context = (crypto_ctx_t *) contextID;
 655 
 656   // out is null when nativeFinal() is called solely for resource clean up
 657   if (out == NULL) {
 658     // Avoid null output buffer to workaround Solaris bug21481818 (fixed in S12)
 659     bufOut = (unsigned char *)(&outLen);
 660     outLen = 0;
 661   } else {
 662     outLen = (*env)->GetArrayLength(env, out) - outOfs;
 663     bufOut = calloc(outLen, sizeof(char));
 664     if (bufOut == NULL) {
 665       throwOutOfMemoryError(env, NULL);
 666       return 0;
 667     }
 668   }
 669   rv = CipherFinal(context, encrypt, bufOut, 0, &outLen);
 670   if (rv) {
 671     free(context);
 672     if (outLen != 0) {
 673       free(bufOut);
 674     }
 675     return -rv;
 676   } else {
 677     if (bufOut != NULL && outLen != 0) {
 678       (*env)->SetByteArrayRegion(env, out, outOfs, outLen, (jbyte *)bufOut);
 679       free(bufOut);

 680     }
 681     free(context);
 682     return outLen;

 683   }

 684 }
 685 
 686 
 687 /*
 688  * Class:     com_oracle_security_ucrypto_NativeKey
 689  * Method:    nativeFree
 690  * Signature: (JI)V
 691  */
 692 void JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree
 693   (jlong id, jint numOfComponents) {
 694   crypto_object_attribute_t* pKey;
 695   int i;
 696 
 697   pKey = (crypto_object_attribute_t*) id;
 698   for (i = 0; i < numOfComponents; i++) {
 699     free(pKey[i].oa_value);
 700   }
 701   free(pKey);
 702 }
 703 
 704 JNIEXPORT void JNICALL Java_com_oracle_security_ucrypto_NativeKey_nativeFree
 705   (JNIEnv *env, jclass jCls, jlong id, jint numOfComponents) {




 633   } else {
 634     (*env)->SetByteArrayRegion(env, jOut, outOfs, outLen, (jbyte *)bufOut);
 635     free(bufIn);
 636     free(bufOut);
 637     return outLen;
 638   }
 639 }
 640 
 641 /*
 642  * Class:     com_oracle_security_ucrypto_NativeCipher
 643  * Method:    nativeFinal
 644  * Signature: (JZ[BI)I
 645  */
 646 JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeCipher_nativeFinal
 647   (JNIEnv *env, jclass jCls, jlong contextID, jboolean encrypt,
 648    jbyteArray out, jint outOfs) {
 649   crypto_ctx_t *context;
 650   unsigned char *bufIn;
 651   unsigned char *bufOut;
 652   int outLen, rv = 0;
 653   jint rc;
 654 
 655   context = (crypto_ctx_t *) contextID;
 656 
 657   // out is null when nativeFinal() is called solely for resource clean up
 658   if (out == NULL) {
 659     // Avoid null output buffer to workaround Solaris bug21481818 (fixed in S12)
 660     bufOut = (unsigned char *)(&outLen);
 661     outLen = 0;
 662   } else {
 663     outLen = (*env)->GetArrayLength(env, out) - outOfs;
 664     bufOut = calloc(outLen, sizeof(char));
 665     if (bufOut == NULL) {
 666       throwOutOfMemoryError(env, NULL);
 667       return 0;
 668     }
 669   }
 670   rv = CipherFinal(context, encrypt, bufOut, 0, &outLen);
 671   if (rv) {
 672     rc = -rv;




 673   } else {
 674     if (outLen > 0) {
 675       (*env)->SetByteArrayRegion(env, out, outOfs, outLen, (jbyte *)bufOut);
 676     }
 677     rc = outLen;
 678   }
 679   free(context);
 680   if (bufOut != (unsigned char *)(&outLen)) {
 681     free(bufOut);
 682   }
 683   return rc;
 684 }

 685 
 686 /*
 687  * Class:     com_oracle_security_ucrypto_NativeKey
 688  * Method:    nativeFree
 689  * Signature: (JI)V
 690  */
 691 void JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree
 692   (jlong id, jint numOfComponents) {
 693   crypto_object_attribute_t* pKey;
 694   int i;
 695 
 696   pKey = (crypto_object_attribute_t*) id;
 697   for (i = 0; i < numOfComponents; i++) {
 698     free(pKey[i].oa_value);
 699   }
 700   free(pKey);
 701 }
 702 
 703 JNIEXPORT void JNICALL Java_com_oracle_security_ucrypto_NativeKey_nativeFree
 704   (JNIEnv *env, jclass jCls, jlong id, jint numOfComponents) {


< prev index next >