Example #1
0
        internal static unsafe void SSLStreamSetApplicationProtocols(SafeSslHandle sslHandle, List <SslApplicationProtocol> protocols)
        {
            int count = protocols.Count;

            MemoryHandle[]            memHandles   = new MemoryHandle[count];
            ApplicationProtocolData[] protocolData = new ApplicationProtocolData[count];
            try
            {
                for (int i = 0; i < count; i++)
                {
                    ReadOnlyMemory <byte> protocol = protocols[i].Protocol;
                    memHandles[i]   = protocol.Pin();
                    protocolData[i] = new ApplicationProtocolData
                    {
                        Data   = (byte *)memHandles[i].Pointer,
                        Length = protocol.Length
                    };
                }
                int ret = SSLStreamSetApplicationProtocols(sslHandle, protocolData, count);
                if (ret != SUCCESS)
                {
                    throw new SslException();
                }
            }
            finally
            {
                foreach (MemoryHandle memHandle in memHandles)
                {
                    memHandle.Dispose();
                }
            }
        }
Example #2
0
        public static bool SslCheckHostnameMatch(SafeSslHandle handle, string hostName, DateTime notBefore)
        {
            int result;
            // The IdnMapping converts Unicode input into the IDNA punycode sequence.
            // It also does host case normalization.  The bypass logic would be something
            // like "all characters being within [a-z0-9.-]+"
            //
            // The SSL Policy (SecPolicyCreateSSL) has been verified as not inherently supporting
            // IDNA as of macOS 10.12.1 (Sierra).  If it supports low-level IDNA at a later date,
            // this code could be removed.
            //
            // It was verified as supporting case invariant match as of 10.12.1 (Sierra).
            string matchName = s_idnMapping.GetAscii(hostName);

            using (SafeCFDateHandle cfNotBefore = CoreFoundation.CFDateCreate(notBefore))
                using (SafeCreateHandle cfHostname = CoreFoundation.CFStringCreateWithCString(matchName))
                {
                    result = AppleCryptoNative_SslIsHostnameMatch(handle, cfHostname, cfNotBefore);
                }

            switch (result)
            {
            case 0:
                return(false);

            case 1:
                return(true);

            default:
                Debug.Fail($"AppleCryptoNative_SslIsHostnameMatch returned {result}");
                throw new SslException();
            }
        }
Example #3
0
        internal static IntPtr[]? SSLStreamGetPeerCertificates(SafeSslHandle ssl)
        {
            IntPtr[]? ptrs;
            int count;

            Interop.AndroidCrypto.SSLStreamGetPeerCertificates(ssl, out ptrs, out count);
            return(ptrs);
        }
Example #4
0
 internal static unsafe PAL_SSLStreamStatus SSLStreamWrite(
     SafeSslHandle sslHandle,
     ReadOnlyMemory <byte> buffer)
 {
     using (MemoryHandle memHandle = buffer.Pin())
     {
         return(SSLStreamWrite(sslHandle, (byte *)memHandle.Pointer, buffer.Length));
     }
 }
Example #5
0
        internal static void SslSetAcceptClientCert(SafeSslHandle sslHandle)
        {
            int osStatus = AppleCryptoNative_SslSetAcceptClientCert(sslHandle);

            if (osStatus != 0)
            {
                throw CreateExceptionForOSStatus(osStatus);
            }
        }
Example #6
0
        internal static void SslSetMaxProtocolVersion(SafeSslHandle sslHandle, SslProtocols maxProtocolId)
        {
            int osStatus = AppleCryptoNative_SslSetMaxProtocolVersion(sslHandle, maxProtocolId);

            if (osStatus != 0)
            {
                throw CreateExceptionForOSStatus(osStatus);
            }
        }
Example #7
0
        internal static void SSLStreamSetEnabledProtocols(SafeSslHandle sslHandle, ReadOnlySpan <SslProtocols> protocols)
        {
            int ret = SSLStreamSetEnabledProtocols(sslHandle, ref MemoryMarshal.GetReference(protocols), protocols.Length);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }
        }
Example #8
0
 internal static unsafe PAL_SSLStreamStatus SSLStreamRead(
     SafeSslHandle sslHandle,
     Span <byte> buffer,
     out int bytesRead)
 {
     fixed(byte *bufferPtr = buffer)
     {
         return(SSLStreamRead(sslHandle, bufferPtr, buffer.Length, out bytesRead));
     }
 }
Example #9
0
        internal static void SSLStreamSetTargetHost(
            SafeSslHandle sslHandle,
            string targetHost)
        {
            int ret = SSLStreamSetTargetHostImpl(sslHandle, targetHost);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }
        }
Example #10
0
        internal static void SSLStreamConfigureParameters(
            SafeSslHandle sslHandle,
            string targetHost)
        {
            int ret = SSLStreamConfigureParametersImpl(sslHandle, targetHost);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }
        }
Example #11
0
        internal static unsafe void SslCtxSetAlpnProtos(SafeSslHandle ctx, List <SslApplicationProtocol> protocols)
        {
            SafeCreateHandle cfProtocolsRefs = null;

            SafeCreateHandle[] cfProtocolsArrayRef = null;
            try
            {
                if (protocols.Count == 1 && protocols[0] == SslApplicationProtocol.Http2)
                {
                    cfProtocolsRefs = s_cfAlpnHttp211Protocols;
                }
                else if (protocols.Count == 1 && protocols[0] == SslApplicationProtocol.Http11)
                {
                    cfProtocolsRefs = s_cfAlpnHttp11Protocols;
                }
                else if (protocols.Count == 2 && protocols[0] == SslApplicationProtocol.Http2 && protocols[1] == SslApplicationProtocol.Http11)
                {
                    cfProtocolsRefs = s_cfAlpnHttp211Protocols;
                }
                else
                {
                    // we did not match common case. This is more expensive path allocating Core Foundation objects.
                    cfProtocolsArrayRef = new SafeCreateHandle[protocols.Count];
                    IntPtr[] protocolsPtr = new System.IntPtr[protocols.Count];

                    for (int i = 0; i < protocols.Count; i++)
                    {
                        cfProtocolsArrayRef[i] = CoreFoundation.CFStringCreateWithCString(protocols[i].ToString());
                        protocolsPtr[i]        = cfProtocolsArrayRef[i].DangerousGetHandle();
                    }

                    cfProtocolsRefs = CoreFoundation.CFArrayCreate(protocolsPtr, (UIntPtr)protocols.Count);
                }

                int osStatus;
                int result = SSLSetALPNProtocols(ctx, cfProtocolsRefs, out osStatus);
                if (result != 1)
                {
                    throw CreateExceptionForOSStatus(osStatus);
                }
            }
            finally
            {
                if (cfProtocolsArrayRef != null)
                {
                    for (int i = 0; i < cfProtocolsArrayRef.Length; i++)
                    {
                        cfProtocolsArrayRef[i]?.Dispose();
                    }

                    cfProtocolsRefs?.Dispose();
                }
            }
        }
Example #12
0
        internal static void SslSetCertificate(SafeSslHandle sslHandle, IntPtr[] certChainPtrs)
        {
            using (SafeCreateHandle cfCertRefs = CoreFoundation.CFArrayCreate(certChainPtrs, (UIntPtr)certChainPtrs.Length))
            {
                int osStatus = AppleCryptoNative_SslSetCertificate(sslHandle, cfCertRefs);

                if (osStatus != 0)
                {
                    throw CreateExceptionForOSStatus(osStatus);
                }
            }
        }
Example #13
0
        internal static SafeX509Handle SSLStreamGetPeerCertificate(SafeSslHandle ssl)
        {
            SafeX509Handle cert;
            int            ret = Interop.AndroidCrypto.SSLStreamGetPeerCertificate(ssl, out cert);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }

            return(cert);
        }
Example #14
0
        internal static IntPtr[] SSLStreamGetPeerCertificates(SafeSslHandle ssl)
        {
            IntPtr[] ptrs;
            int      count;
            int      ret = Interop.AndroidCrypto.SSLStreamGetPeerCertificates(ssl, out ptrs, out count);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }

            return(ptrs);
        }
Example #15
0
        internal static void SSLStreamInitialize(
            SafeSslHandle sslHandle,
            bool isServer,
            SSLReadCallback streamRead,
            SSLWriteCallback streamWrite,
            int appBufferSize)
        {
            int ret = SSLStreamInitializeImpl(sslHandle, isServer, streamRead, streamWrite, appBufferSize);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }
        }
Example #16
0
        internal static void SSLStreamSetTargetHost(
            SafeSslHandle sslHandle,
            string targetHost)
        {
            int ret = SSLStreamSetTargetHostImpl(sslHandle, targetHost);

            if (ret == UNSUPPORTED_API_LEVEL)
            {
                throw new PlatformNotSupportedException(SR.net_android_ssl_api_level_unsupported);
            }
            else if (ret != SUCCESS)
            {
                throw new SslException();
            }
        }
Example #17
0
        internal static void SslBreakOnClientAuth(SafeSslHandle sslHandle, bool setBreak)
        {
            int osStatus;
            int result = AppleCryptoNative_SslSetBreakOnClientAuth(sslHandle, setBreak ? 1 : 0, out osStatus);

            if (result == 1)
            {
                return;
            }

            if (result == 0)
            {
                throw CreateExceptionForOSStatus(osStatus);
            }

            Debug.Fail($"AppleCryptoNative_SslSetBreakOnClientAuth returned {result}");
            throw new SslException();
        }
Example #18
0
        internal static byte[] SslGetAlpnSelected(SafeSslHandle ssl)
        {
            SafeCFDataHandle protocol;

            if (SslGetAlpnSelected(ssl, out protocol) != 1 || protocol == null)
            {
                return(null);
            }

            try
            {
                byte[] result = Interop.CoreFoundation.CFGetData(protocol);
                return(result);
            }
            finally
            {
                protocol.Dispose();
            }
        }
Example #19
0
        internal static byte[]? SSLStreamGetApplicationProtocol(SafeSslHandle ssl)
        {
            int len = 0;
            int ret = SSLStreamGetApplicationProtocol(ssl, null, ref len);

            if (ret != INSUFFICIENT_BUFFER)
            {
                return(null);
            }

            byte[] bytes = new byte[len];
            ret = SSLStreamGetApplicationProtocol(ssl, bytes, ref len);
            if (ret != SUCCESS)
            {
                return(null);
            }

            return(bytes);
        }
Example #20
0
        internal static string SSLStreamGetCipherSuite(SafeSslHandle ssl)
        {
            IntPtr cipherSuitePtr;
            int    ret = SSLStreamGetCipherSuite(ssl, out cipherSuitePtr);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }

            if (cipherSuitePtr == IntPtr.Zero)
            {
                return(string.Empty);
            }

            string cipherSuite = Marshal.PtrToStringUni(cipherSuitePtr) !;

            Marshal.FreeHGlobal(cipherSuitePtr);
            return(cipherSuite);
        }
Example #21
0
        internal static string SSLStreamGetProtocol(SafeSslHandle ssl)
        {
            IntPtr protocolPtr;
            int    ret = SSLStreamGetProtocol(ssl, out protocolPtr);

            if (ret != SUCCESS)
            {
                throw new SslException();
            }

            if (protocolPtr == IntPtr.Zero)
            {
                return(string.Empty);
            }

            string protocol = Marshal.PtrToStringUni(protocolPtr) !;

            Marshal.FreeHGlobal(protocolPtr);
            return(protocol);
        }
Example #22
0
        internal static SafeCFArrayHandle SslCopyCADistinguishedNames(SafeSslHandle sslHandle)
        {
            SafeCFArrayHandle dnArray;
            int osStatus;
            int result = AppleCryptoNative_SslCopyCADistinguishedNames(sslHandle, out dnArray, out osStatus);

            if (result == 1)
            {
                return(dnArray);
            }

            dnArray.Dispose();

            if (result == 0)
            {
                throw CreateExceptionForOSStatus(osStatus);
            }

            Debug.Fail($"AppleCryptoNative_SslCopyCADistinguishedNames returned {result}");
            throw new SslException();
        }
Example #23
0
        internal static SafeX509ChainHandle SslCopyCertChain(SafeSslHandle sslHandle)
        {
            SafeX509ChainHandle chainHandle;
            int osStatus;
            int result = AppleCryptoNative_SslCopyCertChain(sslHandle, out chainHandle, out osStatus);

            if (result == 1)
            {
                return(chainHandle);
            }

            chainHandle.Dispose();

            if (result == 0)
            {
                throw CreateExceptionForOSStatus(osStatus);
            }

            Debug.Fail($"AppleCryptoNative_SslCopyCertChain returned {result}");
            throw new SslException();
        }
Example #24
0
        internal static void SslSetTargetName(SafeSslHandle sslHandle, string targetName)
        {
            Debug.Assert(!string.IsNullOrEmpty(targetName));

            int osStatus;
            int cbTargetName = System.Text.Encoding.UTF8.GetByteCount(targetName);

            int result = AppleCryptoNative_SslSetTargetName(sslHandle, targetName, cbTargetName, out osStatus);

            if (result == 1)
            {
                return;
            }

            if (result == 0)
            {
                throw CreateExceptionForOSStatus(osStatus);
            }

            Debug.Fail($"AppleCryptoNative_SslSetTargetName returned {result}");
            throw new SslException();
        }
Example #25
0
 internal static extern int SslGetCipherSuite(SafeSslHandle sslHandle, out TlsCipherSuite cipherSuite);
Example #26
0
 internal static extern int SslShutdown(SafeSslHandle sslHandle);
Example #27
0
 private static extern int AppleCryptoNative_SslIsHostnameMatch(
     SafeSslHandle handle,
     SafeCreateHandle cfHostname,
     SafeCFDateHandle cfValidTime);
Example #28
0
 internal static extern int SslSetIoCallbacks(
     SafeSslHandle sslHandle,
     SSLReadFunc readCallback,
     SSLWriteFunc writeCallback);
Example #29
0
 internal static extern unsafe PAL_TlsIo SslRead(SafeSslHandle sslHandle, byte *writeFrom, int count, out int bytesWritten);
Example #30
0
 internal static extern int SslGetProtocolVersion(SafeSslHandle sslHandle, out SslProtocols protocol);