17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "sun_security_jgss_wrapper_GSSLibStub.h" 27 #include "NativeUtil.h" 28 #include "NativeFunc.h" 29 #include "jlong.h" 30 #include <jni.h> 31 32 /* Constants for indicating what type of info is needed for inquiries */ 33 const int TYPE_CRED_NAME = 10; 34 const int TYPE_CRED_TIME = 11; 35 const int TYPE_CRED_USAGE = 12; 36 37 /* 38 * Class: sun_security_jgss_wrapper_GSSLibStub 39 * Method: init 40 * Signature: (Ljava/lang/String;Z)Z 41 */ 42 JNIEXPORT jboolean JNICALL 43 Java_sun_security_jgss_wrapper_GSSLibStub_init(JNIEnv *env, 44 jclass jcls, 45 jstring jlibName, 46 jboolean jDebug) { 47 const char *libName; 48 int failed; 49 char *error = NULL; 50 51 if (!jDebug) { 52 JGSS_DEBUG = 0; 53 } else { 54 JGSS_DEBUG = 1; 55 } 56 57 if (jlibName == NULL) { 58 TRACE0("[GSSLibStub_init] GSS lib name is NULL"); 59 return JNI_FALSE; 60 } 61 62 libName = (*env)->GetStringUTFChars(env, jlibName, NULL); 63 if (libName == NULL) { 64 return JNI_FALSE; 65 } 66 TRACE1("[GSSLibStub_init] libName=%s", libName); 67 68 /* initialize global function table */ 69 failed = loadNative(libName); 70 (*env)->ReleaseStringUTFChars(env, jlibName, libName); 71 72 if (!failed) { 73 return JNI_TRUE; 74 } else { 75 if (JGSS_DEBUG) { 76 #ifdef WIN32 77 #define MAX_MSG_SIZE 256 78 static CHAR szMsgBuf[MAX_MSG_SIZE]; 79 DWORD dwRes; 80 DWORD dwError = GetLastError(); 81 dwRes = FormatMessage ( 82 FORMAT_MESSAGE_FROM_SYSTEM, 83 NULL, 84 dwError, 85 0, 86 szMsgBuf, 87 MAX_MSG_SIZE, 88 NULL); 89 if (0 == dwRes) { 90 printf("GSS-API: Unknown failure %d\n", dwError); 91 } else { 137 if (found != 1) { 138 checkStatus(env, NULL, GSS_S_BAD_MECH, 0, "[GSSLibStub_getMechPtr]"); 139 return ptr_to_jlong(NULL); 140 } else { 141 return ptr_to_jlong(cOid); 142 } 143 } else { 144 return ptr_to_jlong(GSS_C_NO_OID); 145 } 146 } 147 148 /* 149 * Utility routine which releases the specified gss_channel_bindings_t 150 * structure. 151 */ 152 void deleteGSSCB(gss_channel_bindings_t cb) { 153 154 if (cb == GSS_C_NO_CHANNEL_BINDINGS) return; 155 156 /* release initiator address */ 157 if (cb->initiator_addrtype != GSS_C_AF_NULLADDR) { 158 resetGSSBuffer(&(cb->initiator_address)); 159 } 160 /* release acceptor address */ 161 if (cb->acceptor_addrtype != GSS_C_AF_NULLADDR) { 162 resetGSSBuffer(&(cb->acceptor_address)); 163 } 164 /* release application data */ 165 if (cb->application_data.length != 0) { 166 resetGSSBuffer(&(cb->application_data)); 167 } 168 free(cb); 169 } 170 171 /* 172 * Utility routine which creates a gss_channel_bindings_t structure 173 * using the specified org.ietf.jgss.ChannelBinding object. 174 * NOTE: must call deleteGSSCB() to free up the resources. 175 */ 176 gss_channel_bindings_t newGSSCB(JNIEnv *env, jobject jcb) { 177 gss_channel_bindings_t cb; 178 jobject jinetAddr; 179 jbyteArray value; 180 181 if (jcb == NULL) { 182 return GSS_C_NO_CHANNEL_BINDINGS; 183 } 184 185 cb = malloc(sizeof(struct gss_channel_bindings_struct)); 186 if (cb == NULL) { 187 throwOutOfMemoryError(env,NULL); 188 return NULL; 189 } 190 191 // initialize addrtype in CB first 192 cb->initiator_addrtype = GSS_C_AF_NULLADDR; 193 cb->acceptor_addrtype = GSS_C_AF_NULLADDR; 194 195 // addresses needs to be initialized to empty 196 memset(&cb->initiator_address, 0, sizeof(cb->initiator_address)); 197 memset(&cb->acceptor_address, 0, sizeof(cb->acceptor_address)); 198 199 /* set up initiator address */ 200 jinetAddr = (*env)->CallObjectMethod(env, jcb, 201 MID_ChannelBinding_getInitiatorAddr); 202 if ((*env)->ExceptionCheck(env)) { 203 goto cleanup; 204 } 205 if (jinetAddr != NULL) { 206 value = (*env)->CallObjectMethod(env, jinetAddr, 207 MID_InetAddress_getAddr); 208 if ((*env)->ExceptionCheck(env)) { 209 goto cleanup; 210 } 211 cb->initiator_addrtype = GSS_C_AF_INET; 212 initGSSBuffer(env, value, &(cb->initiator_address)); 213 if ((*env)->ExceptionCheck(env)) { 214 goto cleanup; | 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "sun_security_jgss_wrapper_GSSLibStub.h" 27 #include "NativeUtil.h" 28 #include "NativeFunc.h" 29 #include "jlong.h" 30 #include <jni.h> 31 32 /* Constants for indicating what type of info is needed for inquiries */ 33 const int TYPE_CRED_NAME = 10; 34 const int TYPE_CRED_TIME = 11; 35 const int TYPE_CRED_USAGE = 12; 36 37 static jclass tlsCBCl = NULL; 38 39 /* 40 * Class: sun_security_jgss_wrapper_GSSLibStub 41 * Method: init 42 * Signature: (Ljava/lang/String;Z)Z 43 */ 44 JNIEXPORT jboolean JNICALL 45 Java_sun_security_jgss_wrapper_GSSLibStub_init(JNIEnv *env, 46 jclass jcls, 47 jstring jlibName, 48 jboolean jDebug) { 49 const char *libName; 50 int failed; 51 char *error = NULL; 52 53 if (!jDebug) { 54 JGSS_DEBUG = 0; 55 } else { 56 JGSS_DEBUG = 1; 57 } 58 59 if (jlibName == NULL) { 60 TRACE0("[GSSLibStub_init] GSS lib name is NULL"); 61 return JNI_FALSE; 62 } 63 64 libName = (*env)->GetStringUTFChars(env, jlibName, NULL); 65 if (libName == NULL) { 66 return JNI_FALSE; 67 } 68 TRACE1("[GSSLibStub_init] libName=%s", libName); 69 70 /* initialize global function table */ 71 failed = loadNative(libName); 72 (*env)->ReleaseStringUTFChars(env, jlibName, libName); 73 74 if (tlsCBCl == NULL) { 75 76 /* initialize TLS Channel Binding class wrapper */ 77 jclass cl = (*env)->FindClass(env, 78 "sun/security/jgss/krb5/internal/TlsChannelBindingImpl"); 79 if (cl == NULL) { /* exception thrown */ 80 return JNI_FALSE; 81 } 82 tlsCBCl = (*env)->NewGlobalRef(env, cl); 83 } 84 85 if (!failed) { 86 return JNI_TRUE; 87 } else { 88 if (JGSS_DEBUG) { 89 #ifdef WIN32 90 #define MAX_MSG_SIZE 256 91 static CHAR szMsgBuf[MAX_MSG_SIZE]; 92 DWORD dwRes; 93 DWORD dwError = GetLastError(); 94 dwRes = FormatMessage ( 95 FORMAT_MESSAGE_FROM_SYSTEM, 96 NULL, 97 dwError, 98 0, 99 szMsgBuf, 100 MAX_MSG_SIZE, 101 NULL); 102 if (0 == dwRes) { 103 printf("GSS-API: Unknown failure %d\n", dwError); 104 } else { 150 if (found != 1) { 151 checkStatus(env, NULL, GSS_S_BAD_MECH, 0, "[GSSLibStub_getMechPtr]"); 152 return ptr_to_jlong(NULL); 153 } else { 154 return ptr_to_jlong(cOid); 155 } 156 } else { 157 return ptr_to_jlong(GSS_C_NO_OID); 158 } 159 } 160 161 /* 162 * Utility routine which releases the specified gss_channel_bindings_t 163 * structure. 164 */ 165 void deleteGSSCB(gss_channel_bindings_t cb) { 166 167 if (cb == GSS_C_NO_CHANNEL_BINDINGS) return; 168 169 /* release initiator address */ 170 if (cb->initiator_addrtype != GSS_C_AF_NULLADDR && 171 cb->initiator_addrtype != GSS_C_AF_UNSPEC) { 172 resetGSSBuffer(&(cb->initiator_address)); 173 } 174 /* release acceptor address */ 175 if (cb->acceptor_addrtype != GSS_C_AF_NULLADDR && 176 cb->acceptor_addrtype != GSS_C_AF_UNSPEC) { 177 resetGSSBuffer(&(cb->acceptor_address)); 178 } 179 /* release application data */ 180 if (cb->application_data.length != 0) { 181 resetGSSBuffer(&(cb->application_data)); 182 } 183 free(cb); 184 } 185 186 /* 187 * Utility routine which creates a gss_channel_bindings_t structure 188 * using the specified org.ietf.jgss.ChannelBinding object. 189 * NOTE: must call deleteGSSCB() to free up the resources. 190 */ 191 gss_channel_bindings_t newGSSCB(JNIEnv *env, jobject jcb) { 192 gss_channel_bindings_t cb; 193 jobject jinetAddr; 194 jbyteArray value; 195 196 if (jcb == NULL) { 197 return GSS_C_NO_CHANNEL_BINDINGS; 198 } 199 200 cb = malloc(sizeof(struct gss_channel_bindings_struct)); 201 if (cb == NULL) { 202 throwOutOfMemoryError(env,NULL); 203 return NULL; 204 } 205 206 // initialize addrtype in CB first 207 // LDAP TLS Channel Binding requires GSS_C_AF_UNSPEC address type 208 // for unspecified initiator and acceptor addresses. 209 // GSS_C_AF_NULLADDR value should be used for unspecified address 210 // in all other cases. 211 212 if ((*env)->IsInstanceOf(env, jcb, tlsCBCl)) { 213 // TLS Channel Binding requires unspecified addrtype=0 214 cb->initiator_addrtype = GSS_C_AF_UNSPEC; 215 cb->acceptor_addrtype = GSS_C_AF_UNSPEC; 216 } else { 217 cb->initiator_addrtype = GSS_C_AF_NULLADDR; 218 cb->acceptor_addrtype = GSS_C_AF_NULLADDR; 219 } 220 // addresses needs to be initialized to empty 221 memset(&cb->initiator_address, 0, sizeof(cb->initiator_address)); 222 memset(&cb->acceptor_address, 0, sizeof(cb->acceptor_address)); 223 224 /* set up initiator address */ 225 jinetAddr = (*env)->CallObjectMethod(env, jcb, 226 MID_ChannelBinding_getInitiatorAddr); 227 if ((*env)->ExceptionCheck(env)) { 228 goto cleanup; 229 } 230 if (jinetAddr != NULL) { 231 value = (*env)->CallObjectMethod(env, jinetAddr, 232 MID_InetAddress_getAddr); 233 if ((*env)->ExceptionCheck(env)) { 234 goto cleanup; 235 } 236 cb->initiator_addrtype = GSS_C_AF_INET; 237 initGSSBuffer(env, value, &(cb->initiator_address)); 238 if ((*env)->ExceptionCheck(env)) { 239 goto cleanup; |