Ejemplo n.º 1
0
        /// <summary>
        ///     Return how much bytes can be read out of the encrypted data. Be aware that this method will not increase
        ///     the readerIndex of the given <see cref="IByteBuffer"/>.
        /// </summary>
        /// <param name="buffer">
        ///     The <see cref="IByteBuffer"/> to read from. Be aware that it must have at least
        ///     <see cref="SSL_RECORD_HEADER_LENGTH"/> bytes to read,
        ///     otherwise it will throw an <see cref="ArgumentException"/>.
        /// </param>
        /// <param name="offset">Offset to record start.</param>
        /// <returns>
        ///     The length of the encrypted packet that is included in the buffer. This will
        ///     return <c>-1</c> if the given <see cref="IByteBuffer"/> is not encrypted at all.
        /// </returns>
        public static int GetEncryptedPacketLength(IByteStream buffer, int offset)
        {
            int packetLength = 0;

            // SSLv3 or TLS - Check ContentType
            switch (buffer.GetByte(offset))
            {
            case SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC:
            case SSL_CONTENT_TYPE_ALERT:
            case SSL_CONTENT_TYPE_HANDSHAKE:
            case SSL_CONTENT_TYPE_APPLICATION_DATA:
                break;

            default:
                // SSLv2 or bad data
                return(-1);
            }
            // SSLv3 or TLS - Check ProtocolVersion
            int majorVersion = buffer.GetByte(offset + 1);

            if (majorVersion == 3)
            {
                // SSLv3 or TLS
                packetLength = (ushort)buffer.GetShort(offset + 3) + SSL_RECORD_HEADER_LENGTH;
                if (packetLength <= SSL_RECORD_HEADER_LENGTH)
                {
                    // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
                    return(-1);
                }
            }
            else
            {
                // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
                return(-1);
            }

            return(packetLength);
        }
Ejemplo n.º 2
0
 public byte GetByte(int index)
 {
     return(_stream.GetByte(index));
 }