jdk/src/share/native/sun/security/jgss/wrapper/NativeUtil.c
changeset 22940 56b3ab8ec81c
parent 16488 816b9df68a9f
child 23010 6dadb192ad81
equal deleted inserted replaced
22939:fc08f20ea32f 22940:56b3ab8ec81c
    25 
    25 
    26 #include "NativeUtil.h"
    26 #include "NativeUtil.h"
    27 #include "NativeFunc.h"
    27 #include "NativeFunc.h"
    28 #include "jlong.h"
    28 #include "jlong.h"
    29 #include <jni.h>
    29 #include <jni.h>
       
    30 #include "jni_util.h"
    30 
    31 
    31 extern void throwOutOfMemoryError(JNIEnv *env, const char *message);
    32 extern void throwOutOfMemoryError(JNIEnv *env, const char *message);
    32 
    33 
    33 const int JAVA_DUPLICATE_TOKEN_CODE = 19; /* DUPLICATE_TOKEN */
    34 const int JAVA_DUPLICATE_TOKEN_CODE = 19; /* DUPLICATE_TOKEN */
    34 const int JAVA_OLD_TOKEN_CODE = 20; /* OLD_TOKEN */
    35 const int JAVA_OLD_TOKEN_CODE = 20; /* OLD_TOKEN */
   493   /* gss_display_status(...) => GSS_S_BAD_MECH, GSS_S_BAD_STATUS */
   494   /* gss_display_status(...) => GSS_S_BAD_MECH, GSS_S_BAD_STATUS */
   494   major = (*ftab->displayStatus)(&minor, statusValue, GSS_C_MECH_CODE, mech,
   495   major = (*ftab->displayStatus)(&minor, statusValue, GSS_C_MECH_CODE, mech,
   495                              &messageContext, &statusString);
   496                              &messageContext, &statusString);
   496   /* release intermediate buffers */
   497   /* release intermediate buffers */
   497   msg = getJavaString(env, &statusString);
   498   msg = getJavaString(env, &statusString);
       
   499   if (msg == NULL && !(*env)->ExceptionCheck(env)) {
       
   500     throwOutOfMemoryError(env, NULL);
       
   501   }
   498   (*ftab->releaseBuffer)(&minor, &statusString);
   502   (*ftab->releaseBuffer)(&minor, &statusString);
   499   return msg;
   503   return msg;
   500 }
   504 }
   501 /*
   505 /*
   502  * Utility routine checking the specified major and minor
   506  * Utility routine checking the specified major and minor
   527   jminor = minor;
   531   jminor = minor;
   528   if (jmajor != GSS_S_COMPLETE) {
   532   if (jmajor != GSS_S_COMPLETE) {
   529     jmsg = NULL;
   533     jmsg = NULL;
   530     if (minor != 0) {
   534     if (minor != 0) {
   531       jmsg = getMinorMessage(env, jstub, minor);
   535       jmsg = getMinorMessage(env, jstub, minor);
       
   536       CHECK_NULL(jmsg);
   532     }
   537     }
   533     gssEx = (*env)->NewObject(env, CLS_GSSException,
   538     gssEx = (*env)->NewObject(env, CLS_GSSException,
   534                               MID_GSSException_ctor3,
   539                               MID_GSSException_ctor3,
   535                               jmajor, jminor, jmsg);
   540                               jmajor, jminor, jmsg);
   536     (*env)->Throw(env, gssEx);
   541     (*env)->Throw(env, gssEx);
   543     } else {
   548     } else {
   544       msg = "A parameter was malformed";
   549       msg = "A parameter was malformed";
   545     }
   550     }
   546     jmajor = 13; /* use GSSException.FAILURE for now */
   551     jmajor = 13; /* use GSSException.FAILURE for now */
   547     jmsg = (*env)->NewStringUTF(env, msg);
   552     jmsg = (*env)->NewStringUTF(env, msg);
       
   553     CHECK_NULL(jmsg);
   548     gssEx = (*env)->NewObject(env, CLS_GSSException,
   554     gssEx = (*env)->NewObject(env, CLS_GSSException,
   549                               MID_GSSException_ctor3,
   555                               MID_GSSException_ctor3,
   550                               jmajor, jminor, jmsg);
   556                               jmajor, jminor, jmsg);
       
   557     CHECK_NULL(gssEx);
   551     (*env)->Throw(env, gssEx);
   558     (*env)->Throw(env, gssEx);
   552   }
   559   }
   553 }
   560 }
       
   561 
   554 /*
   562 /*
   555  * Utility routine for initializing gss_buffer_t structure
   563  * Utility routine for initializing gss_buffer_t structure
   556  * with the byte[] in the specified jbyteArray object.
   564  * with the byte[] in the specified jbyteArray object.
   557  * NOTE: need to call resetGSSBuffer(...) to free up
   565  * NOTE: need to call resetGSSBuffer(...) to free up
   558  * the resources.
   566  * the resources.
   559  */
   567  * Return JNI_TRUE if GetByteArrayElements() returns ok, JNI_FALSE otherwise
   560 void initGSSBuffer(JNIEnv *env, jbyteArray jbytes,
   568  * If JNI_FALSE returned, then an exception has been thrown.
       
   569  */
       
   570 int initGSSBuffer(JNIEnv *env, jbyteArray jbytes,
   561                    gss_buffer_t cbytes) {
   571                    gss_buffer_t cbytes) {
   562   if (jbytes != NULL) {
   572   if (jbytes != NULL) {
   563     cbytes->length = (*env)->GetArrayLength(env, jbytes);
   573     cbytes->length = (*env)->GetArrayLength(env, jbytes);
   564     cbytes->value = (*env)->GetByteArrayElements(env, jbytes, NULL);
   574     cbytes->value = (*env)->GetByteArrayElements(env, jbytes, NULL);
       
   575     if (cbytes->value == NULL) {
       
   576         if (!(*env)->ExceptionCheck(env)) {
       
   577             throwOutOfMemoryError(env, NULL);
       
   578         }
       
   579         return JNI_FALSE;
       
   580     }
   565   } else {
   581   } else {
   566     cbytes->length = 0;
   582     cbytes->length = 0;
   567     cbytes->value = NULL;
   583     cbytes->value = NULL;
   568   }
   584   }
   569 }
   585   return JNI_TRUE;
       
   586 }
       
   587 
   570 /*
   588 /*
   571  * Utility routine for unpinning/releasing the byte[]
   589  * Utility routine for unpinning/releasing the byte[]
   572  * associated with the specified jbyteArray object.
   590  * associated with the specified jbyteArray object.
   573  * NOTE: used in conjunction with initGSSBuffer(...).
   591  * NOTE: used in conjunction with initGSSBuffer(...).
   574  */
   592  */
   591   OM_uint32 minor; // don't care, just so it compiles
   609   OM_uint32 minor; // don't care, just so it compiles
   592 
   610 
   593   if ((cbytes != NULL) && (cbytes != GSS_C_NO_BUFFER) &&
   611   if ((cbytes != NULL) && (cbytes != GSS_C_NO_BUFFER) &&
   594       (cbytes->length != 0)) {
   612       (cbytes->length != 0)) {
   595     result = (*env)->NewByteArray(env, cbytes->length);
   613     result = (*env)->NewByteArray(env, cbytes->length);
   596     (*env)->SetByteArrayRegion(env, result, 0, cbytes->length,
   614     if (result != NULL)
       
   615         (*env)->SetByteArrayRegion(env, result, 0, cbytes->length,
   597                                cbytes->value);
   616                                cbytes->value);
   598     (*ftab->releaseBuffer)(&minor, cbytes);
   617     (*ftab->releaseBuffer)(&minor, cbytes);
   599     return result;
   618     return result;
   600   }
   619   }
   601   return NULL;
   620   return NULL;
   614   if (jOid != NULL) {
   633   if (jOid != NULL) {
   615     jbytes = (*env)->CallObjectMethod(env, jOid, MID_Oid_getDER);
   634     jbytes = (*env)->CallObjectMethod(env, jOid, MID_Oid_getDER);
   616     if ((*env)->ExceptionCheck(env)) {
   635     if ((*env)->ExceptionCheck(env)) {
   617       gssEx = (*env)->ExceptionOccurred(env);
   636       gssEx = (*env)->ExceptionOccurred(env);
   618       (*env)->Throw(env, gssEx);
   637       (*env)->Throw(env, gssEx);
       
   638       return GSS_C_NO_OID;
   619     }
   639     }
   620     cOid = malloc(sizeof(struct gss_OID_desc_struct));
   640     cOid = malloc(sizeof(struct gss_OID_desc_struct));
   621     if (cOid == NULL) {
   641     if (cOid == NULL) {
   622       throwOutOfMemoryError(env,NULL);
   642       throwOutOfMemoryError(env,NULL);
   623       return GSS_C_NO_OID;
   643       return GSS_C_NO_OID;
   663   }
   683   }
   664   cLen = cOid->length;
   684   cLen = cOid->length;
   665   oidHdr[0] = 6;
   685   oidHdr[0] = 6;
   666   oidHdr[1] = cLen;
   686   oidHdr[1] = cLen;
   667   jbytes = (*env)->NewByteArray(env, cLen+2);
   687   jbytes = (*env)->NewByteArray(env, cLen+2);
       
   688   CHECK_NULL_RETURN(jbytes, NULL);
   668   (*env)->SetByteArrayRegion(env, jbytes, 0, 2, (jbyte *) oidHdr);
   689   (*env)->SetByteArrayRegion(env, jbytes, 0, 2, (jbyte *) oidHdr);
   669   (*env)->SetByteArrayRegion(env, jbytes, 2, cLen, (jbyte *) cOid->elements);
   690   (*env)->SetByteArrayRegion(env, jbytes, 2, cLen, (jbyte *) cOid->elements);
   670 
   691 
   671   result = (*env)->NewObject(env, CLS_Oid, MID_Oid_ctor1, jbytes);
   692   result = (*env)->NewObject(env, CLS_Oid, MID_Oid_ctor1, jbytes);
   672   if ((*env)->ExceptionCheck(env)) {
   693   JNU_CHECK_EXCEPTION_RETURN(env, NULL);
   673     (*env)->Throw(env, (*env)->ExceptionOccurred(env));
       
   674   }
       
   675   (*env)->DeleteLocalRef(env, jbytes);
   694   (*env)->DeleteLocalRef(env, jbytes);
   676   return result;
   695   return result;
   677 }
   696 }
   678 /*
   697 /*
   679  * Utility routine for creating a gss_OID_set structure
   698  * Utility routine for creating a gss_OID_set structure
   720   jthrowable gssEx;
   739   jthrowable gssEx;
   721 
   740 
   722   if (cOidSet != NULL && cOidSet != GSS_C_NO_OID_SET) {
   741   if (cOidSet != NULL && cOidSet != GSS_C_NO_OID_SET) {
   723     numOfOids = cOidSet->count;
   742     numOfOids = cOidSet->count;
   724     jOidSet = (*env)->NewObjectArray(env, numOfOids, CLS_Oid, NULL);
   743     jOidSet = (*env)->NewObjectArray(env, numOfOids, CLS_Oid, NULL);
       
   744     if (jOidSet == NULL) {
       
   745       return NULL;
       
   746     }
   725     if (jOidSet != NULL) {
   747     if (jOidSet != NULL) {
   726       for (i = 0; i < numOfOids; i++) {
   748       for (i = 0; i < numOfOids; i++) {
   727         jOid = getJavaOID(env, &(cOidSet->elements[i]));
   749         jOid = getJavaOID(env, &(cOidSet->elements[i]));
   728         (*env)->SetObjectArrayElement(env, jOidSet, i, jOid);
   750         (*env)->SetObjectArrayElement(env, jOidSet, i, jOid);
   729         (*env)->DeleteLocalRef(env, jOid);
   751         (*env)->DeleteLocalRef(env, jOid);
   734   return NULL;
   756   return NULL;
   735 }
   757 }
   736 
   758 
   737 void debug(JNIEnv *env, char *msg) {
   759 void debug(JNIEnv *env, char *msg) {
   738   jstring jmsg = (*env)->NewStringUTF(env, msg);
   760   jstring jmsg = (*env)->NewStringUTF(env, msg);
       
   761   CHECK_NULL(jmsg);
   739   (*env)->CallStaticVoidMethod(env, CLS_SunNativeProvider,
   762   (*env)->CallStaticVoidMethod(env, CLS_SunNativeProvider,
   740                                MID_SunNativeProvider_debug, jmsg);
   763                                MID_SunNativeProvider_debug, jmsg);
   741   (*env)->DeleteLocalRef(env, jmsg);
   764   (*env)->DeleteLocalRef(env, jmsg);
   742 }
   765 }
   743 
   766