Esempio n. 1
0
        public static bool TryGetFrameHeader(ReadOnlySpan <byte> frame, ref TlsFrameHeader header)
        {
            bool result = frame.Length > 4;

            if (frame.Length >= 1)
            {
                header.Type = (TlsContentType)frame[0];

                if (frame.Length >= 3)
                {
                    // SSLv3, TLS or later
                    if (frame[1] == 3)
                    {
                        if (frame.Length > 4)
                        {
                            header.Length = ((frame[3] << 8) | frame[4]);
                        }

                        header.Version = TlsMinorVersionToProtocol(frame[2]);
                    }
                    else
                    {
                        header.Length  = -1;
                        header.Version = SslProtocols.None;
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
        public static bool TryGetFrameHeader(ReadOnlySpan <byte> frame, ref TlsFrameHeader header)
        {
            bool result = frame.Length > 4;

            if (frame.Length >= 1)
            {
                header.Type = (TlsContentType)frame[0];

                if (frame.Length >= 3)
                {
                    // SSLv3, TLS or later
                    if (frame[1] == 3)
                    {
                        if (frame.Length > 4)
                        {
                            header.Length = ((frame[3] << 8) | frame[4]);
                        }

                        switch (frame[2])
                        {
                        case 4:
                            header.Version = SslProtocols.Tls13;
                            break;

                        case 3:
                            header.Version = SslProtocols.Tls12;
                            break;

                        case 2:
                            header.Version = SslProtocols.Tls11;
                            break;

                        case 1:
                            header.Version = SslProtocols.Tls;
                            break;

                        case 0:
#pragma warning disable 0618
                            header.Version = SslProtocols.Ssl3;
#pragma warning restore 0618
                            break;

                        default:
                            header.Version = SslProtocols.None;
                            break;
                        }
                    }
                    else
                    {
                        header.Length  = -1;
                        header.Version = SslProtocols.None;
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        public static bool TryGetFrameHeader(ReadOnlySpan <byte> frame, ref TlsFrameHeader header)
        {
            if (frame.Length < HeaderSize)
            {
                header.Length = -1;
                return(false);
            }

            header.Type = (TlsContentType)frame[0];

            // SSLv3, TLS or later
            if (frame[1] == 3)
            {
                header.Length  = ((frame[3] << 8) | frame[4]) + HeaderSize;
                header.Version = TlsMinorVersionToProtocol(frame[2]);
            }
            else if (frame[2] == (byte)TlsHandshakeType.ClientHello &&
                     frame[3] == 3) // SSL3 or above
            {
                int length;
                if ((frame[0] & 0x80) != 0)
                {
                    // Two bytes
                    length = (((frame[0] & 0x7f) << 8) | frame[1]) + 2;
                }
                else
                {
                    // Three bytes
                    length = (((frame[0] & 0x3f) << 8) | frame[1]) + 3;
                }


                // max frame for SSLv2 is 32767.
                // However, we expect something reasonable for initial HELLO
                // We don't have enough logic to verify full validity,
                // the limits bellow are queses.
#pragma warning disable CS0618 // Ssl2 and Ssl3 are obsolete
                header.Version = SslProtocols.Ssl2;
#pragma warning restore CS0618
                header.Length = length;
                header.Type   = TlsContentType.Handshake;
            }
            else
            {
                header.Length = -1;
            }

            return(true);
        }