private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt) { IntPtr intPtr; IntPtr intPtr1 = (IntPtr)0; SafeBerval[] safeBerval = null; int num = 0; try { if (tempValue != null) { int i = 0; intPtr1 = Utility.AllocHGlobalIntPtrArray((int)tempValue.Length + 1); int num1 = Marshal.SizeOf(typeof(SafeBerval)); safeBerval = new SafeBerval[(int)tempValue.Length]; for (i = 0; i < (int)tempValue.Length; i++) { byte[] numArray = tempValue[i]; safeBerval[i] = new SafeBerval(); if (numArray != null) { safeBerval[i].bv_len = (int)numArray.Length; safeBerval[i].bv_val = Marshal.AllocHGlobal((int)numArray.Length); Marshal.Copy(numArray, 0, safeBerval[i].bv_val, (int)numArray.Length); } else { safeBerval[i].bv_len = 0; safeBerval[i].bv_val = (IntPtr)0; } IntPtr intPtr2 = Marshal.AllocHGlobal(num1); Marshal.StructureToPtr(safeBerval[i], intPtr2, false); intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i)); Marshal.WriteIntPtr(intPtr, intPtr2); } intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i)); Marshal.WriteIntPtr(intPtr, (IntPtr)0); } num = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), intPtr1); GC.KeepAlive(safeBerval); } finally { if (intPtr1 != (IntPtr)0) { for (int j = 0; j < (int)tempValue.Length; j++) { IntPtr intPtr3 = Marshal.ReadIntPtr(intPtr1, Marshal.SizeOf(typeof(IntPtr)) * j); if (intPtr3 != (IntPtr)0) { Marshal.FreeHGlobal(intPtr3); } } Marshal.FreeHGlobal(intPtr1); } } return(num); }
private static int EncodingMultiByteArrayHelper(SafeBerHandle berElement, byte[][] tempValue, char fmt) { IntPtr berValArray = IntPtr.Zero; IntPtr tempPtr = IntPtr.Zero; berval[] managedBervalArray = null; int error = 0; try { if (tempValue != null) { int i = 0; berValArray = Utility.AllocHGlobalIntPtrArray(tempValue.Length + 1); int structSize = Marshal.SizeOf(typeof(berval)); managedBervalArray = new berval[tempValue.Length]; for (i = 0; i < tempValue.Length; i++) { byte[] byteArray = tempValue[i]; // construct the managed berval managedBervalArray[i] = new berval(); if (byteArray != null) { managedBervalArray[i].bv_len = byteArray.Length; managedBervalArray[i].bv_val = Marshal.AllocHGlobal(byteArray.Length); Marshal.Copy(byteArray, 0, managedBervalArray[i].bv_val, byteArray.Length); } // allocate memory for the unmanaged structure IntPtr valPtr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(managedBervalArray[i], valPtr, false); tempPtr = (IntPtr)((long)berValArray + IntPtr.Size * i); Marshal.WriteIntPtr(tempPtr, valPtr); } tempPtr = (IntPtr)((long)berValArray + IntPtr.Size * i); Marshal.WriteIntPtr(tempPtr, IntPtr.Zero); } error = BerPal.PrintBerArray(berElement, new string(fmt, 1), berValArray); } finally { if (berValArray != IntPtr.Zero) { for (int i = 0; i < tempValue.Length; i++) { IntPtr ptr = Marshal.ReadIntPtr(berValArray, IntPtr.Size * i); if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } Marshal.FreeHGlobal(berValArray); } if (managedBervalArray != null) { foreach (berval managedBerval in managedBervalArray) { if (managedBerval.bv_val != IntPtr.Zero) { Marshal.FreeHGlobal(managedBerval.bv_val); } } } } return(error); }
public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls) { IntPtr serverControlArray = (IntPtr)0; LdapControl[] managedServerControls = null; IntPtr clientControlArray = (IntPtr)0; LdapControl[] managedClientControls = null; IntPtr ldapResult = (IntPtr)0; IntPtr referral = (IntPtr)0; int serverError = 0; Uri[] responseReferral = null; if (_connection.disposed) { throw new ObjectDisposedException(GetType().Name); } try { IntPtr controlPtr = (IntPtr)0; IntPtr tempPtr = (IntPtr)0; // build server control managedServerControls = _connection.BuildControlArray(controls, true); int structSize = Marshal.SizeOf(typeof(LdapControl)); if (managedServerControls != null) { serverControlArray = Utility.AllocHGlobalIntPtrArray(managedServerControls.Length + 1); for (int i = 0; i < managedServerControls.Length; i++) { controlPtr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(managedServerControls[i], controlPtr, false); tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * i); Marshal.WriteIntPtr(tempPtr, controlPtr); } tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * managedServerControls.Length); Marshal.WriteIntPtr(tempPtr, (IntPtr)0); } // build client control managedClientControls = _connection.BuildControlArray(controls, false); if (managedClientControls != null) { clientControlArray = Utility.AllocHGlobalIntPtrArray(managedClientControls.Length + 1); for (int i = 0; i < managedClientControls.Length; i++) { controlPtr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(managedClientControls[i], controlPtr, false); tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * i); Marshal.WriteIntPtr(tempPtr, controlPtr); } tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * managedClientControls.Length); Marshal.WriteIntPtr(tempPtr, (IntPtr)0); } int error = Wldap32.ldap_start_tls(_connection.ldapHandle, ref serverError, ref ldapResult, serverControlArray, clientControlArray); if (ldapResult != (IntPtr)0) { // parsing the referral int resulterror = Wldap32.ldap_parse_result_referral(_connection.ldapHandle, ldapResult, (IntPtr)0, (IntPtr)0, (IntPtr)0, ref referral, (IntPtr)0, 0 /* not free it */); if (resulterror == 0) { // parsing referral if (referral != (IntPtr)0) { char ** referralPtr = (char **)referral; char * singleReferral = referralPtr[0]; int i = 0; ArrayList referralList = new ArrayList(); while (singleReferral != null) { string s = Marshal.PtrToStringUni((IntPtr)singleReferral); referralList.Add(s); i++; singleReferral = referralPtr[i]; } // free heap memory if (referral != (IntPtr)0) { Wldap32.ldap_value_free(referral); referral = (IntPtr)0; } if (referralList.Count > 0) { responseReferral = new Uri[referralList.Count]; for (int j = 0; j < referralList.Count; j++) { responseReferral[j] = new Uri((string)referralList[j]); } } } } } if (error != (int)ResultCode.Success) { string errorMessage = String.Format(CultureInfo.CurrentCulture, SR.DefaultLdapError); if (Utility.IsResultCode((ResultCode)error)) { //If the server failed request for whatever reason, the ldap_start_tls returns LDAP_OTHER // and the ServerReturnValue will contain the error code from the server. if (error == (int)ResultCode.Other) { error = serverError; } errorMessage = OperationErrorMappings.MapResultCode(error); ExtendedResponse response = new ExtendedResponse(null, null, (ResultCode)error, errorMessage, responseReferral); response.ResponseName = "1.3.6.1.4.1.1466.20037"; throw new TlsOperationException(response); } else if (Utility.IsLdapError((LdapError)error)) { errorMessage = LdapErrorMappings.MapResultCode(error); throw new LdapException(error, errorMessage); } } } finally { if (serverControlArray != (IntPtr)0) { //release the memory from the heap for (int i = 0; i < managedServerControls.Length; i++) { IntPtr tempPtr = Marshal.ReadIntPtr(serverControlArray, IntPtr.Size * i); if (tempPtr != (IntPtr)0) { Marshal.FreeHGlobal(tempPtr); } } Marshal.FreeHGlobal(serverControlArray); } if (managedServerControls != null) { for (int i = 0; i < managedServerControls.Length; i++) { if (managedServerControls[i].ldctl_oid != (IntPtr)0) { Marshal.FreeHGlobal(managedServerControls[i].ldctl_oid); } if (managedServerControls[i].ldctl_value != null) { if (managedServerControls[i].ldctl_value.bv_val != (IntPtr)0) { Marshal.FreeHGlobal(managedServerControls[i].ldctl_value.bv_val); } } } } if (clientControlArray != (IntPtr)0) { // release the memor from the heap for (int i = 0; i < managedClientControls.Length; i++) { IntPtr tempPtr = Marshal.ReadIntPtr(clientControlArray, IntPtr.Size * i); if (tempPtr != (IntPtr)0) { Marshal.FreeHGlobal(tempPtr); } } Marshal.FreeHGlobal(clientControlArray); } if (managedClientControls != null) { for (int i = 0; i < managedClientControls.Length; i++) { if (managedClientControls[i].ldctl_oid != (IntPtr)0) { Marshal.FreeHGlobal(managedClientControls[i].ldctl_oid); } if (managedClientControls[i].ldctl_value != null) { if (managedClientControls[i].ldctl_value.bv_val != (IntPtr)0) { Marshal.FreeHGlobal(managedClientControls[i].ldctl_value.bv_val); } } } } if (referral != (IntPtr)0) { Wldap32.ldap_value_free(referral); } } }
public override byte[] GetValue() { IntPtr control = (IntPtr)0; int structSize = Marshal.SizeOf(typeof(SortKey)); int keyCount = _keys.Length; IntPtr memHandle = Utility.AllocHGlobalIntPtrArray(keyCount + 1); try { IntPtr tempPtr = (IntPtr)0; IntPtr sortPtr = (IntPtr)0; int i = 0; for (i = 0; i < keyCount; i++) { sortPtr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(_keys[i], sortPtr, false); tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i); Marshal.WriteIntPtr(tempPtr, sortPtr); } tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i); Marshal.WriteIntPtr(tempPtr, (IntPtr)0); bool critical = IsCritical; int error = Wldap32.ldap_create_sort_control(UtilityHandle.GetHandle(), memHandle, critical ? (byte)1 : (byte)0, ref control); if (error != 0) { if (Utility.IsLdapError((LdapError)error)) { string errorMessage = LdapErrorMappings.MapResultCode(error); throw new LdapException(error, errorMessage); } else { throw new LdapException(error); } } LdapControl managedControl = new LdapControl(); Marshal.PtrToStructure(control, managedControl); berval value = managedControl.ldctl_value; // reinitialize the value directoryControlValue = null; if (value != null) { directoryControlValue = new byte[value.bv_len]; Marshal.Copy(value.bv_val, directoryControlValue, 0, value.bv_len); } } finally { if (control != (IntPtr)0) { Wldap32.ldap_control_free(control); } if (memHandle != (IntPtr)0) { //release the memory from the heap for (int i = 0; i < keyCount; i++) { IntPtr tempPtr = Marshal.ReadIntPtr(memHandle, Marshal.SizeOf(typeof(IntPtr)) * i); if (tempPtr != (IntPtr)0) { // free the marshalled name IntPtr ptr = Marshal.ReadIntPtr(tempPtr); if (ptr != (IntPtr)0) { Marshal.FreeHGlobal(ptr); } // free the marshalled rule ptr = Marshal.ReadIntPtr(tempPtr, Marshal.SizeOf(typeof(IntPtr))); if (ptr != (IntPtr)0) { Marshal.FreeHGlobal(ptr); } Marshal.FreeHGlobal(tempPtr); } } Marshal.FreeHGlobal(memHandle); } } return(base.GetValue()); }
public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls) { IntPtr intPtr; IntPtr intPtr1; IntPtr intPtr2 = (IntPtr)0; LdapControl[] ldapControlArray = null; IntPtr intPtr3 = (IntPtr)0; LdapControl[] ldapControlArray1 = null; IntPtr intPtr4 = (IntPtr)0; IntPtr intPtr5 = (IntPtr)0; int num = 0; Uri[] uri = null; if (!Utility.IsWin2kOS) { if (!this.connection.disposed) { try { ldapControlArray = this.connection.BuildControlArray(controls, true); int num1 = Marshal.SizeOf(typeof(LdapControl)); if (ldapControlArray != null) { intPtr2 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray.Length + 1); for (int i = 0; i < (int)ldapControlArray.Length; i++) { intPtr = Marshal.AllocHGlobal(num1); Marshal.StructureToPtr(ldapControlArray[i], intPtr, false); intPtr1 = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i)); Marshal.WriteIntPtr(intPtr1, intPtr); } intPtr1 = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray.Length)); Marshal.WriteIntPtr(intPtr1, (IntPtr)0); } ldapControlArray1 = this.connection.BuildControlArray(controls, false); if (ldapControlArray1 != null) { intPtr3 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray1.Length + 1); for (int j = 0; j < (int)ldapControlArray1.Length; j++) { intPtr = Marshal.AllocHGlobal(num1); Marshal.StructureToPtr(ldapControlArray1[j], intPtr, false); intPtr1 = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * j)); Marshal.WriteIntPtr(intPtr1, intPtr); } intPtr1 = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray1.Length)); Marshal.WriteIntPtr(intPtr1, (IntPtr)0); } int num2 = Wldap32.ldap_start_tls(this.connection.ldapHandle, ref num, ref intPtr4, intPtr2, intPtr3); if (intPtr4 != (IntPtr)0) { int num3 = Wldap32.ldap_parse_result_referral(this.connection.ldapHandle, intPtr4, (IntPtr)0, (IntPtr)0, (IntPtr)0, ref intPtr5, (IntPtr)0, 0); if (num3 == 0 && intPtr5 != (IntPtr)0) { char ** chrPointer = (char **)((void *)intPtr5); char * chrPointer1 = (char *)((void *)(*(chrPointer))); int num4 = 0; ArrayList arrayLists = new ArrayList(); while (chrPointer1 != null) { string stringUni = Marshal.PtrToStringUni((IntPtr)chrPointer1); arrayLists.Add(stringUni); num4++; chrPointer1 = (char *)((void *)(*(chrPointer + num4 * sizeof(char *)))); } if (intPtr5 != (IntPtr)0) { Wldap32.ldap_value_free(intPtr5); intPtr5 = (IntPtr)0; } if (arrayLists.Count > 0) { uri = new Uri[arrayLists.Count]; for (int k = 0; k < arrayLists.Count; k++) { uri[k] = new Uri((string)arrayLists[k]); } } } } if (num2 != 0) { string str = Res.GetString("DefaultLdapError"); if (!Utility.IsResultCode((ResultCode)num2)) { if (Utility.IsLdapError((LdapError)num2)) { str = LdapErrorMappings.MapResultCode(num2); throw new LdapException(num2, str); } } else { if (num2 == 80) { num2 = num; } str = OperationErrorMappings.MapResultCode(num2); ExtendedResponse extendedResponse = new ExtendedResponse(null, null, (ResultCode)num2, str, uri); extendedResponse.name = "1.3.6.1.4.1.1466.20037"; throw new TlsOperationException(extendedResponse); } } } finally { if (intPtr2 != (IntPtr)0) { for (int l = 0; l < (int)ldapControlArray.Length; l++) { IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * l); if (intPtr6 != (IntPtr)0) { Marshal.FreeHGlobal(intPtr6); } } Marshal.FreeHGlobal(intPtr2); } if (ldapControlArray != null) { for (int m = 0; m < (int)ldapControlArray.Length; m++) { if (ldapControlArray[m].ldctl_oid != (IntPtr)0) { Marshal.FreeHGlobal(ldapControlArray[m].ldctl_oid); } if (ldapControlArray[m].ldctl_value != null && ldapControlArray[m].ldctl_value.bv_val != (IntPtr)0) { Marshal.FreeHGlobal(ldapControlArray[m].ldctl_value.bv_val); } } } if (intPtr3 != (IntPtr)0) { for (int n = 0; n < (int)ldapControlArray1.Length; n++) { IntPtr intPtr7 = Marshal.ReadIntPtr(intPtr3, Marshal.SizeOf(typeof(IntPtr)) * n); if (intPtr7 != (IntPtr)0) { Marshal.FreeHGlobal(intPtr7); } } Marshal.FreeHGlobal(intPtr3); } if (ldapControlArray1 != null) { for (int o = 0; o < (int)ldapControlArray1.Length; o++) { if (ldapControlArray1[o].ldctl_oid != (IntPtr)0) { Marshal.FreeHGlobal(ldapControlArray1[o].ldctl_oid); } if (ldapControlArray1[o].ldctl_value != null && ldapControlArray1[o].ldctl_value.bv_val != (IntPtr)0) { Marshal.FreeHGlobal(ldapControlArray1[o].ldctl_value.bv_val); } } } if (intPtr5 != (IntPtr)0) { Wldap32.ldap_value_free(intPtr5); } } return; } else { throw new ObjectDisposedException(this.GetType().Name); } } else { throw new PlatformNotSupportedException(Res.GetString("TLSNotSupported")); } }
public override byte[] GetValue() { IntPtr intPtr; byte num; IntPtr intPtr1 = (IntPtr)0; int num1 = Marshal.SizeOf(typeof(SortKey)); IntPtr intPtr2 = Utility.AllocHGlobalIntPtrArray((int)this.keys.Length + 1); try { int i = 0; for (i = 0; i < (int)this.keys.Length; i++) { IntPtr intPtr3 = Marshal.AllocHGlobal(num1); Marshal.StructureToPtr(this.keys[i], intPtr3, false); intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i)); Marshal.WriteIntPtr(intPtr, intPtr3); } intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i)); Marshal.WriteIntPtr(intPtr, (IntPtr)0); bool isCritical = base.IsCritical; ConnectionHandle handle = UtilityHandle.GetHandle(); IntPtr intPtr4 = intPtr2; if (isCritical) { num = 1; } else { num = 0; } int num2 = Wldap32.ldap_create_sort_control(handle, intPtr4, num, ref intPtr1); if (num2 == 0) { LdapControl ldapControl = new LdapControl(); Marshal.PtrToStructure(intPtr1, ldapControl); berval ldctlValue = ldapControl.ldctl_value; this.directoryControlValue = null; if (ldctlValue != null) { this.directoryControlValue = new byte[ldctlValue.bv_len]; Marshal.Copy(ldctlValue.bv_val, this.directoryControlValue, 0, ldctlValue.bv_len); } } else { if (!Utility.IsLdapError((LdapError)num2)) { throw new LdapException(num2); } else { string str = LdapErrorMappings.MapResultCode(num2); throw new LdapException(num2, str); } } } finally { if (intPtr1 != (IntPtr)0) { Wldap32.ldap_control_free(intPtr1); } if (intPtr2 != (IntPtr)0) { for (int j = 0; j < (int)this.keys.Length; j++) { IntPtr intPtr5 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * j); if (intPtr5 != (IntPtr)0) { IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr5); if (intPtr6 != (IntPtr)0) { Marshal.FreeHGlobal(intPtr6); } intPtr6 = Marshal.ReadIntPtr(intPtr5, Marshal.SizeOf(typeof(IntPtr))); if (intPtr6 != (IntPtr)0) { Marshal.FreeHGlobal(intPtr6); } Marshal.FreeHGlobal(intPtr5); } } Marshal.FreeHGlobal(intPtr2); } } return(base.GetValue()); }
private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt) { IntPtr berValArray = (IntPtr)0; IntPtr tempPtr = (IntPtr)0; SafeBerval[] managedBerVal = null; int error = 0; try { if (tempValue != null) { int i = 0; berValArray = Utility.AllocHGlobalIntPtrArray(tempValue.Length + 1); int structSize = Marshal.SizeOf(typeof(SafeBerval)); managedBerVal = new SafeBerval[tempValue.Length]; for (i = 0; i < tempValue.Length; i++) { byte[] byteArray = tempValue[i]; // construct the managed berval managedBerVal[i] = new SafeBerval(); if (byteArray == null) { managedBerVal[i].bv_len = 0; managedBerVal[i].bv_val = (IntPtr)0; } else { managedBerVal[i].bv_len = byteArray.Length; managedBerVal[i].bv_val = Marshal.AllocHGlobal(byteArray.Length); Marshal.Copy(byteArray, 0, managedBerVal[i].bv_val, byteArray.Length); } // allocate memory for the unmanaged structure IntPtr valPtr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(managedBerVal[i], valPtr, false); tempPtr = (IntPtr)((long)berValArray + Marshal.SizeOf(typeof(IntPtr)) * i); Marshal.WriteIntPtr(tempPtr, valPtr); } tempPtr = (IntPtr)((long)berValArray + Marshal.SizeOf(typeof(IntPtr)) * i); Marshal.WriteIntPtr(tempPtr, (IntPtr)0); } error = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), berValArray); GC.KeepAlive(managedBerVal); } finally { if (berValArray != (IntPtr)0) { for (int i = 0; i < tempValue.Length; i++) { IntPtr ptr = Marshal.ReadIntPtr(berValArray, Marshal.SizeOf(typeof(IntPtr)) * i); if (ptr != (IntPtr)0) { Marshal.FreeHGlobal(ptr); } } Marshal.FreeHGlobal(berValArray); } } return(error); }
public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls) { IntPtr serverControlArray = IntPtr.Zero; LdapControl[] managedServerControls = null; IntPtr clientControlArray = IntPtr.Zero; LdapControl[] managedClientControls = null; //IntPtr ldapResult = IntPtr.Zero; //IntPtr referral = IntPtr.Zero; //int serverError = 0; //Uri[] responseReferral = null; if (_connection._disposed) { throw new ObjectDisposedException(GetType().Name); } try { IntPtr tempPtr = IntPtr.Zero; // build server control managedServerControls = _connection.BuildControlArray(controls, true); int structSize = Marshal.SizeOf(typeof(LdapControl)); if (managedServerControls != null) { serverControlArray = Utility.AllocHGlobalIntPtrArray(managedServerControls.Length + 1); for (int i = 0; i < managedServerControls.Length; i++) { IntPtr controlPtr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(managedServerControls[i], controlPtr, false); tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * i); Marshal.WriteIntPtr(tempPtr, controlPtr); } tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * managedServerControls.Length); Marshal.WriteIntPtr(tempPtr, IntPtr.Zero); } // Build client control. managedClientControls = _connection.BuildControlArray(controls, false); if (managedClientControls != null) { clientControlArray = Utility.AllocHGlobalIntPtrArray(managedClientControls.Length + 1); for (int i = 0; i < managedClientControls.Length; i++) { IntPtr controlPtr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(managedClientControls[i], controlPtr, false); tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * i); Marshal.WriteIntPtr(tempPtr, controlPtr); } tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * managedClientControls.Length); Marshal.WriteIntPtr(tempPtr, IntPtr.Zero); } // requested certificate. // - no certificate = no problem. // - bad certificate = no problem var require_cert = (int)LDAP_OPT_X_TLS.ALLOW; Wldap32.ldap_set_option_int(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_REQUIRE_CERT, ref require_cert); //new tls client context var off = 0; Wldap32.ldap_set_option_int(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_NEWCTX, ref off); //var test tls connect callback var result = Wldap32.ldap_set_option_TLS_CONNECT_CB(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_CONNECT_CB, _routine); int error = Wldap32.ldap_start_tls(_connection._ldapHandle, serverControlArray, clientControlArray); //IntPtr ret1 = IntPtr.Zero; //Wldap32.ldap_get_option_ptr(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_CACERTFILE, ref ret1); //Console.WriteLine(Encoding.PtrToStringUTF8(ret1)); //typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl, void *ctx, void *arg )); //TODO: check referrals equivalent with openldap /* if (ldapResult != IntPtr.Zero) * { * // Parse the referral. * int resultError = Wldap32.ldap_parse_result_referral(_connection._ldapHandle, ldapResult, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref referral, IntPtr.Zero, 0 ); * if (resultError == 0 && referral != IntPtr.Zero) * { * char** referralPtr = (char**)referral; * char* singleReferral = referralPtr[0]; * int i = 0; * ArrayList referralList = new ArrayList(); * while (singleReferral != null) * { * string s = Encoding.PtrToString((IntPtr)singleReferral); * referralList.Add(s); * * i++; * singleReferral = referralPtr[i]; * } * * // Free heap memory. * if (referral != IntPtr.Zero) * { * Wldap32.ldap_value_free(referral); * referral = IntPtr.Zero; * } * * if (referralList.Count > 0) * { * responseReferral = new Uri[referralList.Count]; * for (int j = 0; j < referralList.Count; j++) * { * responseReferral[j] = new Uri((string)referralList[j]); * } * } * } * } */ if (error != (int)ResultCode.Success) { string errorText = Encoding.PtrToString(Wldap32.ldap_err2string(error)); string errorAdditional = GetStringValueHelper(LdapOption.LDAP_OPT_DIAGNOSTIC_MESSAGE, true); if (error > 0) { //positive, indicating an LDAP resultCode other than 'success' ExtendedResponse response = new ExtendedResponse(null, null, (ResultCode)error, errorText, null); response.ResponseName = "1.3.6.1.4.1.1466.20037"; throw new TlsOperationException(response); } else if (error < 0) { //negative, indicating an API error code; error = Math.Abs(error) + 80; //convert negative number for exception for compatibilty with original S.DS.P throw new LdapException(error, errorText, errorAdditional); } } } finally { if (serverControlArray != IntPtr.Zero) { // Release the memory from the heap. for (int i = 0; i < managedServerControls.Length; i++) { IntPtr tempPtr = Marshal.ReadIntPtr(serverControlArray, IntPtr.Size * i); if (tempPtr != IntPtr.Zero) { Marshal.FreeHGlobal(tempPtr); } } Marshal.FreeHGlobal(serverControlArray); } if (managedServerControls != null) { for (int i = 0; i < managedServerControls.Length; i++) { if (managedServerControls[i].ldctl_oid != IntPtr.Zero) { Marshal.FreeHGlobal(managedServerControls[i].ldctl_oid); } if (managedServerControls[i].ldctl_value != null) { if (managedServerControls[i].ldctl_value.bv_val != IntPtr.Zero) { Marshal.FreeHGlobal(managedServerControls[i].ldctl_value.bv_val); } } } } if (clientControlArray != IntPtr.Zero) { // Release the memor from the heap. for (int i = 0; i < managedClientControls.Length; i++) { IntPtr tempPtr = Marshal.ReadIntPtr(clientControlArray, IntPtr.Size * i); if (tempPtr != IntPtr.Zero) { Marshal.FreeHGlobal(tempPtr); } } Marshal.FreeHGlobal(clientControlArray); } if (managedClientControls != null) { for (int i = 0; i < managedClientControls.Length; i++) { if (managedClientControls[i].ldctl_oid != IntPtr.Zero) { Marshal.FreeHGlobal(managedClientControls[i].ldctl_oid); } if (managedClientControls[i].ldctl_value != null) { if (managedClientControls[i].ldctl_value.bv_val != IntPtr.Zero) { Marshal.FreeHGlobal(managedClientControls[i].ldctl_value.bv_val); } } } } /*if (referral != IntPtr.Zero) * { * Wldap32.ldap_value_free(referral); * }*/ } }