internal static void CheckValidOpenSslHandle(SafeHandle handle) { if (handle == null || handle.IsInvalid) { throw CreateOpenSslCryptographicException(); } }
/// <summary> /// Polls a File Descriptor for the passed in flags. /// </summary> /// <param name="fd">The descriptor to poll</param> /// <param name="events">The events to poll for</param> /// <param name="timeout">The amount of time to wait; -1 for infinite, 0 for immediate return, and a positive number is the number of milliseconds</param> /// <param name="triggered">The events that were returned by the poll call. May be PollEvents.POLLNONE in the case of a timeout.</param> /// <returns>An error or Error.SUCCESS.</returns> internal static unsafe Error Poll(SafeHandle fd, PollEvents events, int timeout, out PollEvents triggered) { bool gotRef = false; try { fd.DangerousAddRef(ref gotRef); var pollEvent = new PollEvent { FileDescriptor = fd.DangerousGetHandle().ToInt32(), Events = events, }; uint unused; Error err = Poll(&pollEvent, 1, timeout, &unused); triggered = pollEvent.TriggeredEvents; return err; } finally { if (gotRef) { fd.DangerousRelease(); } } }
internal static extern unsafe bool TransmitFile( SafeHandle socket, SafeHandle fileHandle, int numberOfBytesToWrite, int numberOfBytesPerSend, SafeHandle overlapped, TransmitFileBuffers* buffers, TransmitFileOptions flags);
public static IntPtr VirtualAlloc( SafeHandle baseAddress, UIntPtr size, int allocationType, int protection) { return Interop.Kernel32.VirtualAlloc(baseAddress, size, allocationType, protection); }
public static IntPtr VirtualAlloc( SafeHandle baseAddress, UIntPtr size, int allocationType, int protection) { return Interop.mincore.VirtualAllocFromApp(baseAddress, size, allocationType, protection); }
internal static extern bool DuplicateHandle( SafeProcessHandle hSourceProcessHandle, SafeHandle hSourceHandle, SafeProcessHandle hTargetProcess, out SafeWaitHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions );
internal static unsafe Error Accept(SafeHandle socket, byte* socketAddress, int* socketAddressLen, int* acceptedFd) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousAccept((int)socket.DangerousGetHandle(), socketAddress, socketAddressLen, acceptedFd); } finally { if (release) { socket.DangerousRelease(); } } }
internal static Error Listen(SafeHandle socket, int backlog) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousListen((int)socket.DangerousGetHandle(), backlog); } finally { if (release) { socket.DangerousRelease(); } } }
internal static unsafe Error GetSockName(SafeHandle socket, byte* socketAddress, int* socketAddressLen) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousGetSockName((int)socket.DangerousGetHandle(), socketAddress, socketAddressLen); } finally { if (release) { socket.DangerousRelease(); } } }
internal static unsafe Error SendMessage(SafeHandle socket, MessageHeader* messageHeader, SocketFlags flags, long* sent) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousSendMessage((int)socket.DangerousGetHandle(), messageHeader, flags, sent); } finally { if (release) { socket.DangerousRelease(); } } }
internal static unsafe Error SetSockOpt(SafeHandle socket, SocketOptionLevel optionLevel, SocketOptionName optionName, byte* optionValue, int optionLen) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousSetSockOpt((int)socket.DangerousGetHandle(), optionLevel, optionName, optionValue, optionLen); } finally { if (release) { socket.DangerousRelease(); } } }
internal static unsafe Error GetSocketErrorOption(SafeHandle socket, Error* socketError) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousGetSocketErrorOption((int)socket.DangerousGetHandle(), socketError); } finally { if (release) { socket.DangerousRelease(); } } }
internal static unsafe Error GetIPv6MulticastOption(SafeHandle socket, MulticastOption multicastOption, IPv6MulticastOption* option) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousGetIPv6MulticastOption((int)socket.DangerousGetHandle(), multicastOption, option); } finally { if (release) { socket.DangerousRelease(); } } }
internal static Error TryChangeSocketEventRegistration(int port, SafeHandle socket, SocketEvents currentEvents, SocketEvents newEvents, IntPtr data) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousTryChangeSocketEventRegistration(port, (int)socket.DangerousGetHandle(), currentEvents, newEvents, data); } finally { if (release) { socket.DangerousRelease(); } } }
internal static unsafe Error GetBytesAvailable(SafeHandle socket, int* available) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousGetBytesAvailable((int)socket.DangerousGetHandle(), available); } finally { if (release) { socket.DangerousRelease(); } } }
internal static Error Shutdown(SafeHandle socket, SocketShutdown how) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousShutdown((int)socket.DangerousGetHandle(), how); } finally { if (release) { socket.DangerousRelease(); } } }
internal static unsafe Error SetReceiveTimeout(SafeHandle socket, int millisecondsTimeout) { bool release = false; try { socket.DangerousAddRef(ref release); return DangerousSetReceiveTimeout((int)socket.DangerousGetHandle(), millisecondsTimeout); } finally { if (release) { socket.DangerousRelease(); } } }
public static bool Write(SafeHandle fileHandle, Option options, ExceptionInfo exceptionInfo) { Process currentProcess = Process.GetCurrentProcess(); IntPtr currentProcessHandle = currentProcess.Handle; uint currentProcessId = (uint)currentProcess.Id; MiniDumpExceptionInformation exp; exp.ThreadId = GetCurrentThreadId(); exp.ClientPointers = false; exp.ExceptionPointers = IntPtr.Zero; if (exceptionInfo == ExceptionInfo.Present) { exp.ExceptionPointers = System.Runtime.InteropServices.Marshal.GetExceptionPointers(); } bool bRet = false; if (exp.ExceptionPointers == IntPtr.Zero) { bRet = MiniDumpWriteDump(currentProcessHandle, currentProcessId, fileHandle, (uint)options, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } else { bRet = MiniDumpWriteDump(currentProcessHandle, currentProcessId, fileHandle, (uint)options, ref exp, IntPtr.Zero, IntPtr.Zero); } return bRet; }
public static IEnumerable<string> GetProcessRuntimes(ICLRMetaHost host, SafeHandle hProcess) { if (host != null) { var buffer = new StringBuilder(1024); IEnumUnknown ppEnumerator = host.EnumerateLoadedRuntimes(hProcess.DangerousGetHandle()); return ppEnumerator.Cast<ICLRRuntimeInfo>().Select(rti => { var bufferLength = (uint)buffer.Capacity; rti.GetVersionString(buffer, ref bufferLength); return buffer.ToString(0, (int)bufferLength - 1); }).ToList(); } else { string buffer = GetVersionFromProcess(hProcess); if (buffer != null) { return new[] { buffer }; } } return Enumerable.Empty<string>(); }
internal override int ldap_get_option(SafeHandle ld, int option, ref int value) => NativeMethodsLinux.ldap_get_option(ld, option, ref value);
internal override void LdapConnect(SafeHandle ld) { //no such method in openldap client library }
internal static extern uint GetLongPathNameW(SafeHandle lpszShortPath, SafeHandle lpszLongPath, uint cchBuffer);
public unsafe SecPkgContext_IssuerListInfoEx(SafeHandle handle, byte[] nativeBuffer) { aIssuers = handle; fixed (byte* voidPtr = nativeBuffer) { // TODO (Issue #3114): Properly marshal the struct instead of assuming no padding. cIssuers = *((uint*)(voidPtr + IntPtr.Size)); } }
internal override Native.LdapResultType ldap_result(SafeHandle ld, int msgid, int all, IntPtr timeout, ref IntPtr pMessage) => NativeMethodsLinux.ldap_result(ld, msgid, all, timeout, ref pMessage);
internal static unsafe extern bool CancelIoEx(SafeHandle handle, NativeOverlapped* lpOverlapped);
internal override int ldap_parse_reference(SafeHandle ld, IntPtr reference, ref string[] referralsp, ref IntPtr serverctrlsp, int freeit) => NativeMethodsLinux.ldap_parse_reference(ld, reference, ref referralsp, ref serverctrlsp, freeit);
internal override int ldap_parse_result(SafeHandle ld, IntPtr result, ref int errcodep, ref IntPtr matcheddnp, ref IntPtr errmsgp, ref IntPtr referralsp, ref IntPtr serverctrlsp, int freeit) => NativeMethodsLinux.ldap_parse_result(ld, result, ref errcodep, ref matcheddnp, ref errmsgp, ref referralsp, ref serverctrlsp, freeit);
internal override int Abandon(SafeHandle ld, int msgId, IntPtr serverctrls, IntPtr clientctrls) => NativeMethodsLinux.ldap_abandon_ext(ld, msgId, serverctrls, clientctrls);
internal override IntPtr ldap_next_attribute(SafeHandle ld, IntPtr entry, IntPtr pBer) => NativeMethodsLinux.ldap_next_attribute(ld, entry, pBer);
internal override IntPtr ldap_first_attribute(SafeHandle ld, IntPtr entry, ref IntPtr ppBer) => NativeMethodsLinux.ldap_first_attribute(ld, entry, ref ppBer);
internal override IntPtr ldap_get_dn(SafeHandle ld, IntPtr message) => NativeMethodsLinux.ldap_get_dn(ld, message);
internal override IntPtr ldap_next_entry(SafeHandle ld, IntPtr message) => NativeMethodsLinux.ldap_next_entry(ld, message);
internal override int ldap_set_option(SafeHandle ld, int option, IntPtr invalue) => NativeMethodsLinux.ldap_set_option(ld, option, invalue);
internal static extern unsafe Error GetSockOpt(SafeHandle socket, SocketOptionLevel optionLevel, SocketOptionName optionName, byte* optionValue, int* optionLen);
public static unsafe extern bool ReadConsoleW( SafeHandle hConsoleInput, void *lpBuffer, uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead, ReadConsoleControl *pInputControl);
internal override string GetAdditionalErrorInfo(SafeHandle ld) => NativeMethodsLinux.GetAdditionalErrorInfo(ld);
internal override int SetClientCertificate(SafeHandle ld, X509Certificate2 certificate) { const int verifyDepth = 6; var certData = MarshalUtils.ByteArrayToGnuTlsDatum(certificate.Export(X509ContentType.Cert)); var certs = MarshalUtils.AllocHGlobalIntPtrArray(verifyDepth); var privateKey = (RSA)certificate.PrivateKey; var keyData = MarshalUtils.ByteArrayToGnuTlsDatum(privateKey.ToRsaPrivateKey()); try { var max = verifyDepth; var tlsCtx = IntPtr.Zero; var isServer = 0; ThrowIfError(ld, ldap_set_option(new LdapHandle(IntPtr.Zero), (int)Native.LdapOption.LDAP_OPT_X_TLS_NEWCTX, ref isServer), nameof(ldap_set_option)); ThrowIfError(ld, ldap_get_option(new LdapHandle(IntPtr.Zero), (int)Native.LdapOption.LDAP_OPT_X_TLS_CTX, ref tlsCtx), nameof(ldap_set_option)); var key = IntPtr.Zero; var cred = Marshal.ReadIntPtr(tlsCtx); ThrowIfGnuTlsError(NativeMethodsLinux.gnutls_x509_privkey_init(ref key), nameof(NativeMethodsLinux.gnutls_x509_privkey_init)); ThrowIfGnuTlsError( NativeMethodsLinux.gnutls_x509_privkey_import(key, keyData, NativeMethodsLinux.GNUTLS_X509_FMT.GNUTLS_X509_FMT_DER), nameof(NativeMethodsLinux.gnutls_x509_privkey_import)); ThrowIfGnuTlsError( NativeMethodsLinux.gnutls_x509_crt_list_import(certs, ref max, certData, NativeMethodsLinux.GNUTLS_X509_FMT.GNUTLS_X509_FMT_DER, 0), nameof(NativeMethodsLinux.gnutls_x509_crt_list_import)); var cert = Marshal.ReadIntPtr(certs); // If there's only one cert and it's not self-signed, then we have to build the cert chain. if (max == 1 && !IsSelfSigned(cert)) { for (var i = 1; i < verifyDepth; i++) { cert = Marshal.ReadIntPtr(certs, (i - 1) * IntPtr.Size); var issuer = Marshal.ReadIntPtr(certs, i * IntPtr.Size); if (NativeMethodsLinux.gnutls_certificate_get_issuer(cred, cert, ref issuer, 0) != 0) { break; } Marshal.WriteIntPtr(certs, i * IntPtr.Size, issuer); max++; if (IsSelfSigned(issuer)) { break; } } } ThrowIfGnuTlsError(NativeMethodsLinux.gnutls_certificate_set_x509_key(cred, certs, max, key), nameof(NativeMethodsLinux.gnutls_certificate_set_x509_key)); NativeMethodsLinux.gnutls_certificate_set_verify_flags(cred, 0); return(ldap_set_option(new LdapHandle(IntPtr.Zero), (int)Native.LdapOption.LDAP_OPT_X_TLS_CTX, tlsCtx)); } finally { MarshalUtils.TlsDatumFree(certData); MarshalUtils.TlsDatumFree(keyData); if (certs != IntPtr.Zero) { Marshal.FreeHGlobal(certs); } } }
internal override int BindSimple(SafeHandle ld, string userDn, string password) => NativeMethodsLinux.ldap_simple_bind_s(ld, userDn, password);
internal override IntPtr ldap_get_values(SafeHandle ld, IntPtr entry, IntPtr pBer) => NativeMethodsLinux.ldap_get_values(ld, entry, pBer);
private IntPtr ToNative(SafeHandle ld, string mechanism, LdapCredential ldapCredential) { var saslDefaults = GetSaslDefaults(ld, mechanism); return(UnixSaslMethods.GetSaslCredentials(ldapCredential, saslDefaults)); }
internal override int ldap_delete_ext(SafeHandle ld, string dn, IntPtr serverctrls, IntPtr clientctrls, ref int msgidp) => NativeMethodsLinux.ldap_delete_ext(ld, dn, serverctrls, clientctrls, ref msgidp);
internal override int ldap_rename(SafeHandle ld, string dn, string newrdn, string newparent, int deleteoldrdn, IntPtr serverctrls, IntPtr clientctrls, ref int msgidp) => NativeMethodsLinux.ldap_rename(ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, ref msgidp);
public static extern bool GetNumberOfConsoleInputEvents( SafeHandle hConsoleInput, out uint lpcNumberOfEvents);
internal static unsafe extern int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped);
public static extern bool ReadConsoleInputW( SafeHandle hConsoleInput, ref InputRecord lpBuffer, uint nLength, out uint lpNumberOfEventsRead);
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections, NativeObjectSecurity.ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext);
public static extern bool SetConsoleMode( SafeHandle hConsoleHandle, uint lpMode);
protected void Persist(SafeHandle handle, AccessControlSections includeSections, object exceptionContext);
internal static extern unsafe string GetPeerUserName(SafeHandle socket);
internal static extern Error Listen(SafeHandle socket, int backlog);
unsafe internal static extern uint GetFullPathNameW(char* path, uint numBufferChars, SafeHandle buffer, IntPtr mustBeZero);
internal override void Dispose(SafeHandle ld) { //no actions }
internal override int ldap_extended_operation(SafeHandle ld, string requestoid, IntPtr requestdata, IntPtr serverctrls, IntPtr clientctrls, ref int msgidp) => NativeMethodsLinux.ldap_extended_operation(ld, requestoid, requestdata, serverctrls, clientctrls, ref msgidp);
public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, MinidumpType dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam);
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections);
internal override int ldap_parse_extended_result(SafeHandle ldapHandle, IntPtr result, ref IntPtr oid, ref IntPtr data, byte freeIt) => NativeMethodsLinux.ldap_parse_extended_result(ldapHandle, result, ref oid, ref data, freeIt);
protected sealed override void Persist(SafeHandle handle, AccessControlSections includeSections);
internal override int ldap_start_tls_s(SafeHandle ld, ref int serverReturnValue, ref IntPtr message, IntPtr serverctrls, IntPtr clientctrls) { return(NativeMethodsLinux.ldap_start_tls_s(ld, serverctrls, clientctrls)); }
protected virtual void Persist(SafeHandle handle, AccessControlSections includeSections);
internal override int ldap_stop_tls_s(SafeHandle ld) => 0;